xref: /titanic_44/usr/src/uts/common/inet/ip/ip_if.c (revision d4d1f7bf736e9deaf99b53c54fc31cecd3a9e435)
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 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3365 	/*
3366 	 * There are sub-capabilities. Process the ones we know about.
3367 	 * Loop until we don't have room for another sub-cap header..
3368 	 */
3369 	for (subp = SC(capp, capp->dl_sub_offset),
3370 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3371 	    subp <= endp;
3372 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3373 
3374 		switch (subp->dl_cap) {
3375 		case DL_CAPAB_ID_WRAPPER:
3376 			ill_capability_id_ack(ill, mp, subp);
3377 			break;
3378 		default:
3379 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3380 			break;
3381 		}
3382 	}
3383 #undef SC
3384 done:
3385 	inet_freemsg(mp);
3386 	ill_capability_done(ill);
3387 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
3388 }
3389 
3390 /*
3391  * This needs to be started in a taskq thread to provide a cv_waitable
3392  * context.
3393  */
3394 void
3395 ill_capability_ack(ill_t *ill, mblk_t *mp)
3396 {
3397 	ip_stack_t	*ipst = ill->ill_ipst;
3398 
3399 	mp->b_prev = (mblk_t *)ill;
3400 	if (taskq_dispatch(system_taskq, ill_capability_ack_thr, mp,
3401 	    TQ_NOSLEEP) != 0)
3402 		return;
3403 
3404 	/*
3405 	 * The taskq dispatch failed. Signal the ill_taskq_dispatch thread
3406 	 * which will do the dispatch using TQ_SLEEP to guarantee success.
3407 	 */
3408 	mutex_enter(&ipst->ips_capab_taskq_lock);
3409 	list_insert_tail(&ipst->ips_capab_taskq_list, mp);
3410 	cv_signal(&ipst->ips_capab_taskq_cv);
3411 	mutex_exit(&ipst->ips_capab_taskq_lock);
3412 }
3413 
3414 /*
3415  * This routine is called to scan the fragmentation reassembly table for
3416  * the specified ILL for any packets that are starting to smell.
3417  * dead_interval is the maximum time in seconds that will be tolerated.  It
3418  * will either be the value specified in ip_g_frag_timeout, or zero if the
3419  * ILL is shutting down and it is time to blow everything off.
3420  *
3421  * It returns the number of seconds (as a time_t) that the next frag timer
3422  * should be scheduled for, 0 meaning that the timer doesn't need to be
3423  * re-started.  Note that the method of calculating next_timeout isn't
3424  * entirely accurate since time will flow between the time we grab
3425  * current_time and the time we schedule the next timeout.  This isn't a
3426  * big problem since this is the timer for sending an ICMP reassembly time
3427  * exceeded messages, and it doesn't have to be exactly accurate.
3428  *
3429  * This function is
3430  * sometimes called as writer, although this is not required.
3431  */
3432 time_t
3433 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3434 {
3435 	ipfb_t	*ipfb;
3436 	ipfb_t	*endp;
3437 	ipf_t	*ipf;
3438 	ipf_t	*ipfnext;
3439 	mblk_t	*mp;
3440 	time_t	current_time = gethrestime_sec();
3441 	time_t	next_timeout = 0;
3442 	uint32_t	hdr_length;
3443 	mblk_t	*send_icmp_head;
3444 	mblk_t	*send_icmp_head_v6;
3445 	zoneid_t zoneid;
3446 	ip_stack_t *ipst = ill->ill_ipst;
3447 
3448 	ipfb = ill->ill_frag_hash_tbl;
3449 	if (ipfb == NULL)
3450 		return (B_FALSE);
3451 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3452 	/* Walk the frag hash table. */
3453 	for (; ipfb < endp; ipfb++) {
3454 		send_icmp_head = NULL;
3455 		send_icmp_head_v6 = NULL;
3456 		mutex_enter(&ipfb->ipfb_lock);
3457 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3458 			time_t frag_time = current_time - ipf->ipf_timestamp;
3459 			time_t frag_timeout;
3460 
3461 			if (frag_time < dead_interval) {
3462 				/*
3463 				 * There are some outstanding fragments
3464 				 * that will timeout later.  Make note of
3465 				 * the time so that we can reschedule the
3466 				 * next timeout appropriately.
3467 				 */
3468 				frag_timeout = dead_interval - frag_time;
3469 				if (next_timeout == 0 ||
3470 				    frag_timeout < next_timeout) {
3471 					next_timeout = frag_timeout;
3472 				}
3473 				break;
3474 			}
3475 			/* Time's up.  Get it out of here. */
3476 			hdr_length = ipf->ipf_nf_hdr_len;
3477 			ipfnext = ipf->ipf_hash_next;
3478 			if (ipfnext)
3479 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3480 			*ipf->ipf_ptphn = ipfnext;
3481 			mp = ipf->ipf_mp->b_cont;
3482 			for (; mp; mp = mp->b_cont) {
3483 				/* Extra points for neatness. */
3484 				IP_REASS_SET_START(mp, 0);
3485 				IP_REASS_SET_END(mp, 0);
3486 			}
3487 			mp = ipf->ipf_mp->b_cont;
3488 			atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
3489 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3490 			ipfb->ipfb_count -= ipf->ipf_count;
3491 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3492 			ipfb->ipfb_frag_pkts--;
3493 			/*
3494 			 * We do not send any icmp message from here because
3495 			 * we currently are holding the ipfb_lock for this
3496 			 * hash chain. If we try and send any icmp messages
3497 			 * from here we may end up via a put back into ip
3498 			 * trying to get the same lock, causing a recursive
3499 			 * mutex panic. Instead we build a list and send all
3500 			 * the icmp messages after we have dropped the lock.
3501 			 */
3502 			if (ill->ill_isv6) {
3503 				if (hdr_length != 0) {
3504 					mp->b_next = send_icmp_head_v6;
3505 					send_icmp_head_v6 = mp;
3506 				} else {
3507 					freemsg(mp);
3508 				}
3509 			} else {
3510 				if (hdr_length != 0) {
3511 					mp->b_next = send_icmp_head;
3512 					send_icmp_head = mp;
3513 				} else {
3514 					freemsg(mp);
3515 				}
3516 			}
3517 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3518 			freeb(ipf->ipf_mp);
3519 		}
3520 		mutex_exit(&ipfb->ipfb_lock);
3521 		/*
3522 		 * Now need to send any icmp messages that we delayed from
3523 		 * above.
3524 		 */
3525 		while (send_icmp_head_v6 != NULL) {
3526 			ip6_t *ip6h;
3527 
3528 			mp = send_icmp_head_v6;
3529 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3530 			mp->b_next = NULL;
3531 			if (mp->b_datap->db_type == M_CTL)
3532 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3533 			else
3534 				ip6h = (ip6_t *)mp->b_rptr;
3535 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3536 			    ill, ipst);
3537 			if (zoneid == ALL_ZONES) {
3538 				freemsg(mp);
3539 			} else {
3540 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3541 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3542 				    B_FALSE, zoneid, ipst);
3543 			}
3544 		}
3545 		while (send_icmp_head != NULL) {
3546 			ipaddr_t dst;
3547 
3548 			mp = send_icmp_head;
3549 			send_icmp_head = send_icmp_head->b_next;
3550 			mp->b_next = NULL;
3551 
3552 			if (mp->b_datap->db_type == M_CTL)
3553 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3554 			else
3555 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3556 
3557 			zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst);
3558 			if (zoneid == ALL_ZONES) {
3559 				freemsg(mp);
3560 			} else {
3561 				icmp_time_exceeded(ill->ill_wq, mp,
3562 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid,
3563 				    ipst);
3564 			}
3565 		}
3566 	}
3567 	/*
3568 	 * A non-dying ILL will use the return value to decide whether to
3569 	 * restart the frag timer, and for how long.
3570 	 */
3571 	return (next_timeout);
3572 }
3573 
3574 /*
3575  * This routine is called when the approximate count of mblk memory used
3576  * for the specified ILL has exceeded max_count.
3577  */
3578 void
3579 ill_frag_prune(ill_t *ill, uint_t max_count)
3580 {
3581 	ipfb_t	*ipfb;
3582 	ipf_t	*ipf;
3583 	size_t	count;
3584 
3585 	/*
3586 	 * If we are here within ip_min_frag_prune_time msecs remove
3587 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3588 	 * ill_frag_free_num_pkts.
3589 	 */
3590 	mutex_enter(&ill->ill_lock);
3591 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3592 	    (ip_min_frag_prune_time != 0 ?
3593 	    ip_min_frag_prune_time : msec_per_tick)) {
3594 
3595 		ill->ill_frag_free_num_pkts++;
3596 
3597 	} else {
3598 		ill->ill_frag_free_num_pkts = 0;
3599 	}
3600 	ill->ill_last_frag_clean_time = lbolt;
3601 	mutex_exit(&ill->ill_lock);
3602 
3603 	/*
3604 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3605 	 */
3606 	if (ill->ill_frag_free_num_pkts != 0) {
3607 		int ix;
3608 
3609 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3610 			ipfb = &ill->ill_frag_hash_tbl[ix];
3611 			mutex_enter(&ipfb->ipfb_lock);
3612 			if (ipfb->ipfb_ipf != NULL) {
3613 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3614 				    ill->ill_frag_free_num_pkts);
3615 			}
3616 			mutex_exit(&ipfb->ipfb_lock);
3617 		}
3618 	}
3619 	/*
3620 	 * While the reassembly list for this ILL is too big, prune a fragment
3621 	 * queue by age, oldest first.
3622 	 */
3623 	while (ill->ill_frag_count > max_count) {
3624 		int	ix;
3625 		ipfb_t	*oipfb = NULL;
3626 		uint_t	oldest = UINT_MAX;
3627 
3628 		count = 0;
3629 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3630 			ipfb = &ill->ill_frag_hash_tbl[ix];
3631 			mutex_enter(&ipfb->ipfb_lock);
3632 			ipf = ipfb->ipfb_ipf;
3633 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3634 				oldest = ipf->ipf_gen;
3635 				oipfb = ipfb;
3636 			}
3637 			count += ipfb->ipfb_count;
3638 			mutex_exit(&ipfb->ipfb_lock);
3639 		}
3640 		if (oipfb == NULL)
3641 			break;
3642 
3643 		if (count <= max_count)
3644 			return;	/* Somebody beat us to it, nothing to do */
3645 		mutex_enter(&oipfb->ipfb_lock);
3646 		ipf = oipfb->ipfb_ipf;
3647 		if (ipf != NULL) {
3648 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3649 		}
3650 		mutex_exit(&oipfb->ipfb_lock);
3651 	}
3652 }
3653 
3654 /*
3655  * free 'free_cnt' fragmented packets starting at ipf.
3656  */
3657 void
3658 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3659 {
3660 	size_t	count;
3661 	mblk_t	*mp;
3662 	mblk_t	*tmp;
3663 	ipf_t **ipfp = ipf->ipf_ptphn;
3664 
3665 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3666 	ASSERT(ipfp != NULL);
3667 	ASSERT(ipf != NULL);
3668 
3669 	while (ipf != NULL && free_cnt-- > 0) {
3670 		count = ipf->ipf_count;
3671 		mp = ipf->ipf_mp;
3672 		ipf = ipf->ipf_hash_next;
3673 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3674 			IP_REASS_SET_START(tmp, 0);
3675 			IP_REASS_SET_END(tmp, 0);
3676 		}
3677 		atomic_add_32(&ill->ill_frag_count, -count);
3678 		ASSERT(ipfb->ipfb_count >= count);
3679 		ipfb->ipfb_count -= count;
3680 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3681 		ipfb->ipfb_frag_pkts--;
3682 		freemsg(mp);
3683 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3684 	}
3685 
3686 	if (ipf)
3687 		ipf->ipf_ptphn = ipfp;
3688 	ipfp[0] = ipf;
3689 }
3690 
3691 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3692 	"obsolete and may be removed in a future release of Solaris.  Use " \
3693 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3694 
3695 /*
3696  * For obsolete per-interface forwarding configuration;
3697  * called in response to ND_GET.
3698  */
3699 /* ARGSUSED */
3700 static int
3701 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3702 {
3703 	ill_t *ill = (ill_t *)cp;
3704 
3705 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3706 
3707 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3708 	return (0);
3709 }
3710 
3711 /*
3712  * For obsolete per-interface forwarding configuration;
3713  * called in response to ND_SET.
3714  */
3715 /* ARGSUSED */
3716 static int
3717 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3718     cred_t *ioc_cr)
3719 {
3720 	long value;
3721 	int retval;
3722 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
3723 
3724 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3725 
3726 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3727 	    value < 0 || value > 1) {
3728 		return (EINVAL);
3729 	}
3730 
3731 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3732 	retval = ill_forward_set((ill_t *)cp, (value != 0));
3733 	rw_exit(&ipst->ips_ill_g_lock);
3734 	return (retval);
3735 }
3736 
3737 /*
3738  * Helper function for ill_forward_set().
3739  */
3740 static void
3741 ill_forward_set_on_ill(ill_t *ill, boolean_t enable)
3742 {
3743 	ip_stack_t	*ipst = ill->ill_ipst;
3744 
3745 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3746 
3747 	ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3748 	    (enable ? "Enabling" : "Disabling"),
3749 	    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3750 	mutex_enter(&ill->ill_lock);
3751 	if (enable)
3752 		ill->ill_flags |= ILLF_ROUTER;
3753 	else
3754 		ill->ill_flags &= ~ILLF_ROUTER;
3755 	mutex_exit(&ill->ill_lock);
3756 	if (ill->ill_isv6)
3757 		ill_set_nce_router_flags(ill, enable);
3758 	/* Notify routing socket listeners of this change. */
3759 	ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
3760 }
3761 
3762 /*
3763  * Set an ill's ILLF_ROUTER flag appropriately.  Send up RTS_IFINFO routing
3764  * socket messages for each interface whose flags we change.
3765  */
3766 int
3767 ill_forward_set(ill_t *ill, boolean_t enable)
3768 {
3769 	ipmp_illgrp_t *illg;
3770 	ip_stack_t *ipst = ill->ill_ipst;
3771 
3772 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3773 
3774 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3775 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
3776 		return (0);
3777 
3778 	if (IS_LOOPBACK(ill))
3779 		return (EINVAL);
3780 
3781 	if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
3782 		/*
3783 		 * Update all of the interfaces in the group.
3784 		 */
3785 		illg = ill->ill_grp;
3786 		ill = list_head(&illg->ig_if);
3787 		for (; ill != NULL; ill = list_next(&illg->ig_if, ill))
3788 			ill_forward_set_on_ill(ill, enable);
3789 
3790 		/*
3791 		 * Update the IPMP meta-interface.
3792 		 */
3793 		ill_forward_set_on_ill(ipmp_illgrp_ipmp_ill(illg), enable);
3794 		return (0);
3795 	}
3796 
3797 	ill_forward_set_on_ill(ill, enable);
3798 	return (0);
3799 }
3800 
3801 /*
3802  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3803  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3804  * set or clear.
3805  */
3806 static void
3807 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3808 {
3809 	ipif_t *ipif;
3810 	nce_t *nce;
3811 
3812 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3813 		/*
3814 		 * NOTE: we match across the illgrp because nce's for
3815 		 * addresses on IPMP interfaces have an nce_ill that points to
3816 		 * the bound underlying ill.
3817 		 */
3818 		nce = ndp_lookup_v6(ill, B_TRUE, &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 		ill_g_head_t ghead;
4375 
4376 		/*
4377 		 * allocate ill_if_t structure
4378 		 */
4379 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4380 		if (ill_interface == NULL) {
4381 			return (ENOMEM);
4382 		}
4383 
4384 		(void) strcpy(ill_interface->illif_name, name);
4385 		ill_interface->illif_name_len = name_length;
4386 
4387 		avl_create(&ill_interface->illif_avl_by_ppa,
4388 		    ill_compare_ppa, sizeof (ill_t),
4389 		    offsetof(struct ill_s, ill_avl_byppa));
4390 
4391 		/*
4392 		 * link the structure in the back to maintain order
4393 		 * of configuration for ifconfig output.
4394 		 */
4395 		ghead = ipst->ips_ill_g_heads[index];
4396 		insque(ill_interface, ghead.ill_g_list_tail);
4397 	}
4398 
4399 	if (ill->ill_ppa == UINT_MAX)
4400 		check_length = B_TRUE;
4401 
4402 	error = ill_alloc_ppa(ill_interface, ill);
4403 	if (error != 0) {
4404 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4405 			ill_delete_interface_type(ill->ill_ifptr);
4406 		return (error);
4407 	}
4408 
4409 	/*
4410 	 * When the ppa is choosen by the system, check that there is
4411 	 * enough space to insert ppa. if a specific ppa was passed in this
4412 	 * check is not required as the interface name passed in will have
4413 	 * the right ppa in it.
4414 	 */
4415 	if (check_length) {
4416 		/*
4417 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4418 		 */
4419 		char buf[sizeof (uint_t) * 3];
4420 
4421 		/*
4422 		 * convert ppa to string to calculate the amount of space
4423 		 * required for it in the name.
4424 		 */
4425 		numtos(ill->ill_ppa, buf);
4426 
4427 		/* Do we have enough space to insert ppa ? */
4428 
4429 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4430 			/* Free ppa and interface type struct */
4431 			if (ill_interface->illif_ppa_arena != NULL) {
4432 				vmem_free(ill_interface->illif_ppa_arena,
4433 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4434 			}
4435 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4436 				ill_delete_interface_type(ill->ill_ifptr);
4437 
4438 			return (EINVAL);
4439 		}
4440 	}
4441 
4442 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4443 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4444 
4445 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4446 	    &where);
4447 	ill->ill_ifptr = ill_interface;
4448 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4449 
4450 	ill_phyint_reinit(ill);
4451 	return (0);
4452 }
4453 
4454 /* Initialize the per phyint ipsq used for serialization */
4455 static boolean_t
4456 ipsq_init(ill_t *ill, boolean_t enter)
4457 {
4458 	ipsq_t  *ipsq;
4459 	ipxop_t	*ipx;
4460 
4461 	if ((ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP)) == NULL)
4462 		return (B_FALSE);
4463 
4464 	ill->ill_phyint->phyint_ipsq = ipsq;
4465 	ipx = ipsq->ipsq_xop = &ipsq->ipsq_ownxop;
4466 	ipx->ipx_ipsq = ipsq;
4467 	ipsq->ipsq_next = ipsq;
4468 	ipsq->ipsq_phyint = ill->ill_phyint;
4469 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4470 	mutex_init(&ipx->ipx_lock, NULL, MUTEX_DEFAULT, 0);
4471 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4472 	if (enter) {
4473 		ipx->ipx_writer = curthread;
4474 		ipx->ipx_forced = B_FALSE;
4475 		ipx->ipx_reentry_cnt = 1;
4476 #ifdef DEBUG
4477 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
4478 #endif
4479 	}
4480 	return (B_TRUE);
4481 }
4482 
4483 /*
4484  * ill_init is called by ip_open when a device control stream is opened.
4485  * It does a few initializations, and shoots a DL_INFO_REQ message down
4486  * to the driver.  The response is later picked up in ip_rput_dlpi and
4487  * used to set up default mechanisms for talking to the driver.  (Always
4488  * called as writer.)
4489  *
4490  * If this function returns error, ip_open will call ip_close which in
4491  * turn will call ill_delete to clean up any memory allocated here that
4492  * is not yet freed.
4493  */
4494 int
4495 ill_init(queue_t *q, ill_t *ill)
4496 {
4497 	int	count;
4498 	dl_info_req_t	*dlir;
4499 	mblk_t	*info_mp;
4500 	uchar_t *frag_ptr;
4501 
4502 	/*
4503 	 * The ill is initialized to zero by mi_alloc*(). In addition
4504 	 * some fields already contain valid values, initialized in
4505 	 * ip_open(), before we reach here.
4506 	 */
4507 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4508 
4509 	ill->ill_rq = q;
4510 	ill->ill_wq = WR(q);
4511 
4512 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4513 	    BPRI_HI);
4514 	if (info_mp == NULL)
4515 		return (ENOMEM);
4516 
4517 	/*
4518 	 * Allocate sufficient space to contain our fragment hash table and
4519 	 * the device name.
4520 	 */
4521 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4522 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4523 	if (frag_ptr == NULL) {
4524 		freemsg(info_mp);
4525 		return (ENOMEM);
4526 	}
4527 	ill->ill_frag_ptr = frag_ptr;
4528 	ill->ill_frag_free_num_pkts = 0;
4529 	ill->ill_last_frag_clean_time = 0;
4530 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4531 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4532 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4533 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4534 		    NULL, MUTEX_DEFAULT, NULL);
4535 	}
4536 
4537 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4538 	if (ill->ill_phyint == NULL) {
4539 		freemsg(info_mp);
4540 		mi_free(frag_ptr);
4541 		return (ENOMEM);
4542 	}
4543 
4544 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4545 	/*
4546 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4547 	 * at this point because of the following reason. If we can't
4548 	 * enter the ipsq at some point and cv_wait, the writer that
4549 	 * wakes us up tries to locate us using the list of all phyints
4550 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4551 	 * If we don't set it now, we risk a missed wakeup.
4552 	 */
4553 	ill->ill_phyint->phyint_illv4 = ill;
4554 	ill->ill_ppa = UINT_MAX;
4555 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4556 
4557 	if (!ipsq_init(ill, B_TRUE)) {
4558 		freemsg(info_mp);
4559 		mi_free(frag_ptr);
4560 		mi_free(ill->ill_phyint);
4561 		return (ENOMEM);
4562 	}
4563 
4564 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4565 
4566 	/* Frag queue limit stuff */
4567 	ill->ill_frag_count = 0;
4568 	ill->ill_ipf_gen = 0;
4569 
4570 	ill->ill_global_timer = INFINITY;
4571 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4572 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4573 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4574 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4575 
4576 	/*
4577 	 * Initialize IPv6 configuration variables.  The IP module is always
4578 	 * opened as an IPv4 module.  Instead tracking down the cases where
4579 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4580 	 * here for convenience, this has no effect until the ill is set to do
4581 	 * IPv6.
4582 	 */
4583 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4584 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4585 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4586 	ill->ill_max_buf = ND_MAX_Q;
4587 	ill->ill_refcnt = 0;
4588 
4589 	/* Send down the Info Request to the driver. */
4590 	info_mp->b_datap->db_type = M_PCPROTO;
4591 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4592 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4593 	dlir->dl_primitive = DL_INFO_REQ;
4594 
4595 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4596 
4597 	qprocson(q);
4598 	ill_dlpi_send(ill, info_mp);
4599 
4600 	return (0);
4601 }
4602 
4603 /*
4604  * ill_dls_info
4605  * creates datalink socket info from the device.
4606  */
4607 int
4608 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4609 {
4610 	size_t	len;
4611 	ill_t	*ill = ipif->ipif_ill;
4612 
4613 	sdl->sdl_family = AF_LINK;
4614 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4615 	sdl->sdl_type = ill->ill_type;
4616 	ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4617 	len = strlen(sdl->sdl_data);
4618 	ASSERT(len < 256);
4619 	sdl->sdl_nlen = (uchar_t)len;
4620 	sdl->sdl_alen = ill->ill_phys_addr_length;
4621 	sdl->sdl_slen = 0;
4622 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4623 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4624 
4625 	return (sizeof (struct sockaddr_dl));
4626 }
4627 
4628 /*
4629  * ill_xarp_info
4630  * creates xarp info from the device.
4631  */
4632 static int
4633 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4634 {
4635 	sdl->sdl_family = AF_LINK;
4636 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4637 	sdl->sdl_type = ill->ill_type;
4638 	ipif_get_name(ill->ill_ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4639 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4640 	sdl->sdl_alen = ill->ill_phys_addr_length;
4641 	sdl->sdl_slen = 0;
4642 	return (sdl->sdl_nlen);
4643 }
4644 
4645 static int
4646 loopback_kstat_update(kstat_t *ksp, int rw)
4647 {
4648 	kstat_named_t *kn;
4649 	netstackid_t	stackid;
4650 	netstack_t	*ns;
4651 	ip_stack_t	*ipst;
4652 
4653 	if (ksp == NULL || ksp->ks_data == NULL)
4654 		return (EIO);
4655 
4656 	if (rw == KSTAT_WRITE)
4657 		return (EACCES);
4658 
4659 	kn = KSTAT_NAMED_PTR(ksp);
4660 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4661 
4662 	ns = netstack_find_by_stackid(stackid);
4663 	if (ns == NULL)
4664 		return (-1);
4665 
4666 	ipst = ns->netstack_ip;
4667 	if (ipst == NULL) {
4668 		netstack_rele(ns);
4669 		return (-1);
4670 	}
4671 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4672 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4673 	netstack_rele(ns);
4674 	return (0);
4675 }
4676 
4677 /*
4678  * Has ifindex been plumbed already?
4679  */
4680 boolean_t
4681 phyint_exists(uint_t index, ip_stack_t *ipst)
4682 {
4683 	ASSERT(index != 0);
4684 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4685 
4686 	return (avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4687 	    &index, NULL) != NULL);
4688 }
4689 
4690 /* Pick a unique ifindex */
4691 boolean_t
4692 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4693 {
4694 	uint_t starting_index;
4695 
4696 	if (!ipst->ips_ill_index_wrap) {
4697 		*indexp = ipst->ips_ill_index++;
4698 		if (ipst->ips_ill_index == 0) {
4699 			/* Reached the uint_t limit Next time wrap  */
4700 			ipst->ips_ill_index_wrap = B_TRUE;
4701 		}
4702 		return (B_TRUE);
4703 	}
4704 
4705 	/*
4706 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4707 	 * at this point and don't want to call any function that attempts
4708 	 * to get the lock again.
4709 	 */
4710 	starting_index = ipst->ips_ill_index++;
4711 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
4712 		if (ipst->ips_ill_index != 0 &&
4713 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
4714 			/* found unused index - use it */
4715 			*indexp = ipst->ips_ill_index;
4716 			return (B_TRUE);
4717 		}
4718 	}
4719 
4720 	/*
4721 	 * all interface indicies are inuse.
4722 	 */
4723 	return (B_FALSE);
4724 }
4725 
4726 /*
4727  * Assign a unique interface index for the phyint.
4728  */
4729 static boolean_t
4730 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
4731 {
4732 	ASSERT(phyi->phyint_ifindex == 0);
4733 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
4734 }
4735 
4736 /*
4737  * Return a pointer to the ill which matches the supplied name.  Note that
4738  * the ill name length includes the null termination character.  (May be
4739  * called as writer.)
4740  * If do_alloc and the interface is "lo0" it will be automatically created.
4741  * Cannot bump up reference on condemned ills. So dup detect can't be done
4742  * using this func.
4743  */
4744 ill_t *
4745 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4746     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
4747     ip_stack_t *ipst)
4748 {
4749 	ill_t	*ill;
4750 	ipif_t	*ipif;
4751 	ipsq_t	*ipsq;
4752 	kstat_named_t	*kn;
4753 	boolean_t isloopback;
4754 	in6_addr_t ov6addr;
4755 
4756 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4757 
4758 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4759 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4760 	rw_exit(&ipst->ips_ill_g_lock);
4761 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4762 		return (ill);
4763 
4764 	/*
4765 	 * Couldn't find it.  Does this happen to be a lookup for the
4766 	 * loopback device and are we allowed to allocate it?
4767 	 */
4768 	if (!isloopback || !do_alloc)
4769 		return (NULL);
4770 
4771 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4772 
4773 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4774 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4775 		rw_exit(&ipst->ips_ill_g_lock);
4776 		return (ill);
4777 	}
4778 
4779 	/* Create the loopback device on demand */
4780 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4781 	    sizeof (ipif_loopback_name), BPRI_MED));
4782 	if (ill == NULL)
4783 		goto done;
4784 
4785 	*ill = ill_null;
4786 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4787 	ill->ill_ipst = ipst;
4788 	netstack_hold(ipst->ips_netstack);
4789 	/*
4790 	 * For exclusive stacks we set the zoneid to zero
4791 	 * to make IP operate as if in the global zone.
4792 	 */
4793 	ill->ill_zoneid = GLOBAL_ZONEID;
4794 
4795 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4796 	if (ill->ill_phyint == NULL)
4797 		goto done;
4798 
4799 	if (isv6)
4800 		ill->ill_phyint->phyint_illv6 = ill;
4801 	else
4802 		ill->ill_phyint->phyint_illv4 = ill;
4803 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4804 	ill->ill_max_frag = IP_LOOPBACK_MTU;
4805 	/* Add room for tcp+ip headers */
4806 	if (isv6) {
4807 		ill->ill_isv6 = B_TRUE;
4808 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
4809 	} else {
4810 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
4811 	}
4812 	if (!ill_allocate_mibs(ill))
4813 		goto done;
4814 	ill->ill_max_mtu = ill->ill_max_frag;
4815 	/*
4816 	 * ipif_loopback_name can't be pointed at directly because its used
4817 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
4818 	 * from the glist, ill_glist_delete() sets the first character of
4819 	 * ill_name to '\0'.
4820 	 */
4821 	ill->ill_name = (char *)ill + sizeof (*ill);
4822 	(void) strcpy(ill->ill_name, ipif_loopback_name);
4823 	ill->ill_name_length = sizeof (ipif_loopback_name);
4824 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
4825 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4826 
4827 	ill->ill_global_timer = INFINITY;
4828 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4829 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4830 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4831 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4832 
4833 	/* No resolver here. */
4834 	ill->ill_net_type = IRE_LOOPBACK;
4835 
4836 	/* Initialize the ipsq */
4837 	if (!ipsq_init(ill, B_FALSE))
4838 		goto done;
4839 
4840 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE, B_TRUE);
4841 	if (ipif == NULL)
4842 		goto done;
4843 
4844 	ill->ill_flags = ILLF_MULTICAST;
4845 
4846 	ov6addr = ipif->ipif_v6lcl_addr;
4847 	/* Set up default loopback address and mask. */
4848 	if (!isv6) {
4849 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
4850 
4851 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
4852 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4853 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
4854 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4855 		    ipif->ipif_v6subnet);
4856 		ill->ill_flags |= ILLF_IPV4;
4857 	} else {
4858 		ipif->ipif_v6lcl_addr = ipv6_loopback;
4859 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4860 		ipif->ipif_v6net_mask = ipv6_all_ones;
4861 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4862 		    ipif->ipif_v6subnet);
4863 		ill->ill_flags |= ILLF_IPV6;
4864 	}
4865 
4866 	/*
4867 	 * Chain us in at the end of the ill list. hold the ill
4868 	 * before we make it globally visible. 1 for the lookup.
4869 	 */
4870 	ill->ill_refcnt = 0;
4871 	ill_refhold(ill);
4872 
4873 	ill->ill_frag_count = 0;
4874 	ill->ill_frag_free_num_pkts = 0;
4875 	ill->ill_last_frag_clean_time = 0;
4876 
4877 	ipsq = ill->ill_phyint->phyint_ipsq;
4878 
4879 	if (ill_glist_insert(ill, "lo", isv6) != 0)
4880 		cmn_err(CE_PANIC, "cannot insert loopback interface");
4881 
4882 	/* Let SCTP know so that it can add this to its list */
4883 	sctp_update_ill(ill, SCTP_ILL_INSERT);
4884 
4885 	/*
4886 	 * We have already assigned ipif_v6lcl_addr above, but we need to
4887 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
4888 	 * requires to be after ill_glist_insert() since we need the
4889 	 * ill_index set. Pass on ipv6_loopback as the old address.
4890 	 */
4891 	sctp_update_ipif_addr(ipif, ov6addr);
4892 
4893 	/*
4894 	 * ill_glist_insert() -> ill_phyint_reinit() may have merged IPSQs.
4895 	 * If so, free our original one.
4896 	 */
4897 	if (ipsq != ill->ill_phyint->phyint_ipsq)
4898 		ipsq_delete(ipsq);
4899 
4900 	/*
4901 	 * Delay this till the ipif is allocated as ipif_allocate
4902 	 * de-references ill_phyint for getting the ifindex. We
4903 	 * can't do this before ipif_allocate because ill_phyint_reinit
4904 	 * -> phyint_assign_ifindex expects ipif to be present.
4905 	 */
4906 	mutex_enter(&ill->ill_phyint->phyint_lock);
4907 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
4908 	mutex_exit(&ill->ill_phyint->phyint_lock);
4909 
4910 	if (ipst->ips_loopback_ksp == NULL) {
4911 		/* Export loopback interface statistics */
4912 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
4913 		    ipif_loopback_name, "net",
4914 		    KSTAT_TYPE_NAMED, 2, 0,
4915 		    ipst->ips_netstack->netstack_stackid);
4916 		if (ipst->ips_loopback_ksp != NULL) {
4917 			ipst->ips_loopback_ksp->ks_update =
4918 			    loopback_kstat_update;
4919 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
4920 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
4921 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
4922 			ipst->ips_loopback_ksp->ks_private =
4923 			    (void *)(uintptr_t)ipst->ips_netstack->
4924 			    netstack_stackid;
4925 			kstat_install(ipst->ips_loopback_ksp);
4926 		}
4927 	}
4928 
4929 	if (error != NULL)
4930 		*error = 0;
4931 	*did_alloc = B_TRUE;
4932 	rw_exit(&ipst->ips_ill_g_lock);
4933 	ill_nic_event_dispatch(ill, MAP_IPIF_ID(ill->ill_ipif->ipif_id),
4934 	    NE_PLUMB, ill->ill_name, ill->ill_name_length);
4935 	return (ill);
4936 done:
4937 	if (ill != NULL) {
4938 		if (ill->ill_phyint != NULL) {
4939 			ipsq = ill->ill_phyint->phyint_ipsq;
4940 			if (ipsq != NULL) {
4941 				ipsq->ipsq_phyint = NULL;
4942 				ipsq_delete(ipsq);
4943 			}
4944 			mi_free(ill->ill_phyint);
4945 		}
4946 		ill_free_mib(ill);
4947 		if (ill->ill_ipst != NULL)
4948 			netstack_rele(ill->ill_ipst->ips_netstack);
4949 		mi_free(ill);
4950 	}
4951 	rw_exit(&ipst->ips_ill_g_lock);
4952 	if (error != NULL)
4953 		*error = ENOMEM;
4954 	return (NULL);
4955 }
4956 
4957 /*
4958  * For IPP calls - use the ip_stack_t for global stack.
4959  */
4960 ill_t *
4961 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
4962     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
4963 {
4964 	ip_stack_t	*ipst;
4965 	ill_t		*ill;
4966 
4967 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
4968 	if (ipst == NULL) {
4969 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
4970 		return (NULL);
4971 	}
4972 
4973 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
4974 	netstack_rele(ipst->ips_netstack);
4975 	return (ill);
4976 }
4977 
4978 /*
4979  * Return a pointer to the ill which matches the index and IP version type.
4980  */
4981 ill_t *
4982 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
4983     ipsq_func_t func, int *err, ip_stack_t *ipst)
4984 {
4985 	ill_t	*ill;
4986 	ipsq_t  *ipsq;
4987 	phyint_t *phyi;
4988 
4989 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
4990 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
4991 
4992 	if (err != NULL)
4993 		*err = 0;
4994 
4995 	/*
4996 	 * Indexes are stored in the phyint - a common structure
4997 	 * to both IPv4 and IPv6.
4998 	 */
4999 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5000 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5001 	    (void *) &index, NULL);
5002 	if (phyi != NULL) {
5003 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5004 		if (ill != NULL) {
5005 			/*
5006 			 * The block comment at the start of ipif_down
5007 			 * explains the use of the macros used below
5008 			 */
5009 			GRAB_CONN_LOCK(q);
5010 			mutex_enter(&ill->ill_lock);
5011 			if (ILL_CAN_LOOKUP(ill)) {
5012 				ill_refhold_locked(ill);
5013 				mutex_exit(&ill->ill_lock);
5014 				RELEASE_CONN_LOCK(q);
5015 				rw_exit(&ipst->ips_ill_g_lock);
5016 				return (ill);
5017 			} else if (ILL_CAN_WAIT(ill, q)) {
5018 				ipsq = ill->ill_phyint->phyint_ipsq;
5019 				mutex_enter(&ipsq->ipsq_lock);
5020 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
5021 				rw_exit(&ipst->ips_ill_g_lock);
5022 				mutex_exit(&ill->ill_lock);
5023 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5024 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
5025 				mutex_exit(&ipsq->ipsq_lock);
5026 				RELEASE_CONN_LOCK(q);
5027 				if (err != NULL)
5028 					*err = EINPROGRESS;
5029 				return (NULL);
5030 			}
5031 			RELEASE_CONN_LOCK(q);
5032 			mutex_exit(&ill->ill_lock);
5033 		}
5034 	}
5035 	rw_exit(&ipst->ips_ill_g_lock);
5036 	if (err != NULL)
5037 		*err = ENXIO;
5038 	return (NULL);
5039 }
5040 
5041 /*
5042  * Return the ifindex next in sequence after the passed in ifindex.
5043  * If there is no next ifindex for the given protocol, return 0.
5044  */
5045 uint_t
5046 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5047 {
5048 	phyint_t *phyi;
5049 	phyint_t *phyi_initial;
5050 	uint_t   ifindex;
5051 
5052 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5053 
5054 	if (index == 0) {
5055 		phyi = avl_first(
5056 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5057 	} else {
5058 		phyi = phyi_initial = avl_find(
5059 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5060 		    (void *) &index, NULL);
5061 	}
5062 
5063 	for (; phyi != NULL;
5064 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5065 	    phyi, AVL_AFTER)) {
5066 		/*
5067 		 * If we're not returning the first interface in the tree
5068 		 * and we still haven't moved past the phyint_t that
5069 		 * corresponds to index, avl_walk needs to be called again
5070 		 */
5071 		if (!((index != 0) && (phyi == phyi_initial))) {
5072 			if (isv6) {
5073 				if ((phyi->phyint_illv6) &&
5074 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5075 				    (phyi->phyint_illv6->ill_isv6 == 1))
5076 					break;
5077 			} else {
5078 				if ((phyi->phyint_illv4) &&
5079 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5080 				    (phyi->phyint_illv4->ill_isv6 == 0))
5081 					break;
5082 			}
5083 		}
5084 	}
5085 
5086 	rw_exit(&ipst->ips_ill_g_lock);
5087 
5088 	if (phyi != NULL)
5089 		ifindex = phyi->phyint_ifindex;
5090 	else
5091 		ifindex = 0;
5092 
5093 	return (ifindex);
5094 }
5095 
5096 /*
5097  * Return the ifindex for the named interface.
5098  * If there is no next ifindex for the interface, return 0.
5099  */
5100 uint_t
5101 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5102 {
5103 	phyint_t	*phyi;
5104 	avl_index_t	where = 0;
5105 	uint_t		ifindex;
5106 
5107 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5108 
5109 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5110 	    name, &where)) == NULL) {
5111 		rw_exit(&ipst->ips_ill_g_lock);
5112 		return (0);
5113 	}
5114 
5115 	ifindex = phyi->phyint_ifindex;
5116 
5117 	rw_exit(&ipst->ips_ill_g_lock);
5118 
5119 	return (ifindex);
5120 }
5121 
5122 /*
5123  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5124  * that gives a running thread a reference to the ill. This reference must be
5125  * released by the thread when it is done accessing the ill and related
5126  * objects. ill_refcnt can not be used to account for static references
5127  * such as other structures pointing to an ill. Callers must generally
5128  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5129  * or be sure that the ill is not being deleted or changing state before
5130  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5131  * ill won't change any of its critical state such as address, netmask etc.
5132  */
5133 void
5134 ill_refhold(ill_t *ill)
5135 {
5136 	mutex_enter(&ill->ill_lock);
5137 	ill->ill_refcnt++;
5138 	ILL_TRACE_REF(ill);
5139 	mutex_exit(&ill->ill_lock);
5140 }
5141 
5142 void
5143 ill_refhold_locked(ill_t *ill)
5144 {
5145 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5146 	ill->ill_refcnt++;
5147 	ILL_TRACE_REF(ill);
5148 }
5149 
5150 int
5151 ill_check_and_refhold(ill_t *ill)
5152 {
5153 	mutex_enter(&ill->ill_lock);
5154 	if (ILL_CAN_LOOKUP(ill)) {
5155 		ill_refhold_locked(ill);
5156 		mutex_exit(&ill->ill_lock);
5157 		return (0);
5158 	}
5159 	mutex_exit(&ill->ill_lock);
5160 	return (ILL_LOOKUP_FAILED);
5161 }
5162 
5163 /*
5164  * Must not be called while holding any locks. Otherwise if this is
5165  * the last reference to be released, there is a chance of recursive mutex
5166  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5167  * to restart an ioctl.
5168  */
5169 void
5170 ill_refrele(ill_t *ill)
5171 {
5172 	mutex_enter(&ill->ill_lock);
5173 	ASSERT(ill->ill_refcnt != 0);
5174 	ill->ill_refcnt--;
5175 	ILL_UNTRACE_REF(ill);
5176 	if (ill->ill_refcnt != 0) {
5177 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5178 		mutex_exit(&ill->ill_lock);
5179 		return;
5180 	}
5181 
5182 	/* Drops the ill_lock */
5183 	ipif_ill_refrele_tail(ill);
5184 }
5185 
5186 /*
5187  * Obtain a weak reference count on the ill. This reference ensures the
5188  * ill won't be freed, but the ill may change any of its critical state
5189  * such as netmask, address etc. Returns an error if the ill has started
5190  * closing.
5191  */
5192 boolean_t
5193 ill_waiter_inc(ill_t *ill)
5194 {
5195 	mutex_enter(&ill->ill_lock);
5196 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5197 		mutex_exit(&ill->ill_lock);
5198 		return (B_FALSE);
5199 	}
5200 	ill->ill_waiters++;
5201 	mutex_exit(&ill->ill_lock);
5202 	return (B_TRUE);
5203 }
5204 
5205 void
5206 ill_waiter_dcr(ill_t *ill)
5207 {
5208 	mutex_enter(&ill->ill_lock);
5209 	ill->ill_waiters--;
5210 	if (ill->ill_waiters == 0)
5211 		cv_broadcast(&ill->ill_cv);
5212 	mutex_exit(&ill->ill_lock);
5213 }
5214 
5215 /*
5216  * Named Dispatch routine to produce a formatted report on all ILLs.
5217  * This report is accessed by using the ndd utility to "get" ND variable
5218  * "ip_ill_status".
5219  */
5220 /* ARGSUSED */
5221 int
5222 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5223 {
5224 	ill_t		*ill;
5225 	ill_walk_context_t ctx;
5226 	ip_stack_t	*ipst;
5227 
5228 	ipst = CONNQ_TO_IPST(q);
5229 
5230 	(void) mi_mpprintf(mp,
5231 	    "ILL      " MI_COL_HDRPAD_STR
5232 	/*   01234567[89ABCDEF] */
5233 	    "rq       " MI_COL_HDRPAD_STR
5234 	/*   01234567[89ABCDEF] */
5235 	    "wq       " MI_COL_HDRPAD_STR
5236 	/*   01234567[89ABCDEF] */
5237 	    "upcnt mxfrg err name");
5238 	/*   12345 12345 123 xxxxxxxx  */
5239 
5240 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5241 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5242 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5243 		(void) mi_mpprintf(mp,
5244 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5245 		    "%05u %05u %03d %s",
5246 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5247 		    ill->ill_ipif_up_count,
5248 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5249 	}
5250 	rw_exit(&ipst->ips_ill_g_lock);
5251 
5252 	return (0);
5253 }
5254 
5255 /*
5256  * Named Dispatch routine to produce a formatted report on all IPIFs.
5257  * This report is accessed by using the ndd utility to "get" ND variable
5258  * "ip_ipif_status".
5259  */
5260 /* ARGSUSED */
5261 int
5262 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5263 {
5264 	char	buf1[INET6_ADDRSTRLEN];
5265 	char	buf2[INET6_ADDRSTRLEN];
5266 	char	buf3[INET6_ADDRSTRLEN];
5267 	char	buf4[INET6_ADDRSTRLEN];
5268 	char	buf5[INET6_ADDRSTRLEN];
5269 	char	buf6[INET6_ADDRSTRLEN];
5270 	char	buf[LIFNAMSIZ];
5271 	ill_t	*ill;
5272 	ipif_t	*ipif;
5273 	nv_t	*nvp;
5274 	uint64_t flags;
5275 	zoneid_t zoneid;
5276 	ill_walk_context_t ctx;
5277 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
5278 
5279 	(void) mi_mpprintf(mp,
5280 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5281 	    "\tlocal address\n"
5282 	    "\tsrc address\n"
5283 	    "\tsubnet\n"
5284 	    "\tmask\n"
5285 	    "\tbroadcast\n"
5286 	    "\tp-p-dst");
5287 
5288 	ASSERT(q->q_next == NULL);
5289 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5290 
5291 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5292 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5293 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5294 		for (ipif = ill->ill_ipif; ipif != NULL;
5295 		    ipif = ipif->ipif_next) {
5296 			if (zoneid != GLOBAL_ZONEID &&
5297 			    zoneid != ipif->ipif_zoneid &&
5298 			    ipif->ipif_zoneid != ALL_ZONES)
5299 				continue;
5300 
5301 			ipif_get_name(ipif, buf, sizeof (buf));
5302 			(void) mi_mpprintf(mp,
5303 			    MI_COL_PTRFMT_STR
5304 			    "%04u %05u %u/%u/%u %s %d",
5305 			    (void *)ipif,
5306 			    ipif->ipif_metric, ipif->ipif_mtu,
5307 			    ipif->ipif_ib_pkt_count,
5308 			    ipif->ipif_ob_pkt_count,
5309 			    ipif->ipif_fo_pkt_count,
5310 			    buf,
5311 			    ipif->ipif_zoneid);
5312 
5313 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5314 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5315 
5316 		/* Tack on text strings for any flags. */
5317 		nvp = ipif_nv_tbl;
5318 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5319 			if (nvp->nv_value & flags)
5320 				(void) mi_mpprintf_nr(mp, " %s",
5321 				    nvp->nv_name);
5322 		}
5323 		(void) mi_mpprintf(mp,
5324 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5325 		    inet_ntop(AF_INET6,
5326 		    &ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5327 		    inet_ntop(AF_INET6,
5328 		    &ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5329 		    inet_ntop(AF_INET6,
5330 		    &ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5331 		    inet_ntop(AF_INET6,
5332 		    &ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5333 		    inet_ntop(AF_INET6,
5334 		    &ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5335 		    inet_ntop(AF_INET6,
5336 		    &ipif->ipif_v6pp_dst_addr, buf6, sizeof (buf6)));
5337 		}
5338 	}
5339 	rw_exit(&ipst->ips_ill_g_lock);
5340 	return (0);
5341 }
5342 
5343 /*
5344  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5345  * driver.  We construct best guess defaults for lower level information that
5346  * we need.  If an interface is brought up without injection of any overriding
5347  * information from outside, we have to be ready to go with these defaults.
5348  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5349  * we primarely want the dl_provider_style.
5350  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5351  * at which point we assume the other part of the information is valid.
5352  */
5353 void
5354 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5355 {
5356 	uchar_t		*brdcst_addr;
5357 	uint_t		brdcst_addr_length, phys_addr_length;
5358 	t_scalar_t	sap_length;
5359 	dl_info_ack_t	*dlia;
5360 	ip_m_t		*ipm;
5361 	dl_qos_cl_sel1_t *sel1;
5362 	int		min_mtu;
5363 
5364 	ASSERT(IAM_WRITER_ILL(ill));
5365 
5366 	/*
5367 	 * Till the ill is fully up ILL_CHANGING will be set and
5368 	 * the ill is not globally visible. So no need for a lock.
5369 	 */
5370 	dlia = (dl_info_ack_t *)mp->b_rptr;
5371 	ill->ill_mactype = dlia->dl_mac_type;
5372 
5373 	ipm = ip_m_lookup(dlia->dl_mac_type);
5374 	if (ipm == NULL) {
5375 		ipm = ip_m_lookup(DL_OTHER);
5376 		ASSERT(ipm != NULL);
5377 	}
5378 	ill->ill_media = ipm;
5379 
5380 	/*
5381 	 * When the new DLPI stuff is ready we'll pull lengths
5382 	 * from dlia.
5383 	 */
5384 	if (dlia->dl_version == DL_VERSION_2) {
5385 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5386 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5387 		    brdcst_addr_length);
5388 		if (brdcst_addr == NULL) {
5389 			brdcst_addr_length = 0;
5390 		}
5391 		sap_length = dlia->dl_sap_length;
5392 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5393 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5394 		    brdcst_addr_length, sap_length, phys_addr_length));
5395 	} else {
5396 		brdcst_addr_length = 6;
5397 		brdcst_addr = ip_six_byte_all_ones;
5398 		sap_length = -2;
5399 		phys_addr_length = brdcst_addr_length;
5400 	}
5401 
5402 	ill->ill_bcast_addr_length = brdcst_addr_length;
5403 	ill->ill_phys_addr_length = phys_addr_length;
5404 	ill->ill_sap_length = sap_length;
5405 
5406 	/*
5407 	 * Synthetic DLPI types such as SUNW_DL_IPMP specify a zero SDU,
5408 	 * but we must ensure a minimum IP MTU is used since other bits of
5409 	 * IP will fly apart otherwise.
5410 	 */
5411 	min_mtu = ill->ill_isv6 ? IPV6_MIN_MTU : IP_MIN_MTU;
5412 	ill->ill_max_frag  = MAX(min_mtu, dlia->dl_max_sdu);
5413 	ill->ill_max_mtu = ill->ill_max_frag;
5414 
5415 	ill->ill_type = ipm->ip_m_type;
5416 
5417 	if (!ill->ill_dlpi_style_set) {
5418 		if (dlia->dl_provider_style == DL_STYLE2)
5419 			ill->ill_needs_attach = 1;
5420 
5421 		/*
5422 		 * Allocate the first ipif on this ill. We don't delay it
5423 		 * further as ioctl handling assumes atleast one ipif to
5424 		 * be present.
5425 		 *
5426 		 * At this point we don't know whether the ill is v4 or v6.
5427 		 * We will know this whan the SIOCSLIFNAME happens and
5428 		 * the correct value for ill_isv6 will be assigned in
5429 		 * ipif_set_values(). We need to hold the ill lock and
5430 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5431 		 * the wakeup.
5432 		 */
5433 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5434 		    dlia->dl_provider_style != DL_STYLE2, B_TRUE);
5435 		mutex_enter(&ill->ill_lock);
5436 		ASSERT(ill->ill_dlpi_style_set == 0);
5437 		ill->ill_dlpi_style_set = 1;
5438 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5439 		cv_broadcast(&ill->ill_cv);
5440 		mutex_exit(&ill->ill_lock);
5441 		freemsg(mp);
5442 		return;
5443 	}
5444 	ASSERT(ill->ill_ipif != NULL);
5445 	/*
5446 	 * We know whether it is IPv4 or IPv6 now, as this is the
5447 	 * second DL_INFO_ACK we are recieving in response to the
5448 	 * DL_INFO_REQ sent in ipif_set_values.
5449 	 */
5450 	if (ill->ill_isv6)
5451 		ill->ill_sap = IP6_DL_SAP;
5452 	else
5453 		ill->ill_sap = IP_DL_SAP;
5454 	/*
5455 	 * Set ipif_mtu which is used to set the IRE's
5456 	 * ire_max_frag value. The driver could have sent
5457 	 * a different mtu from what it sent last time. No
5458 	 * need to call ipif_mtu_change because IREs have
5459 	 * not yet been created.
5460 	 */
5461 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5462 	/*
5463 	 * Clear all the flags that were set based on ill_bcast_addr_length
5464 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5465 	 * changed now and we need to re-evaluate.
5466 	 */
5467 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5468 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5469 
5470 	/*
5471 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5472 	 * changed now.
5473 	 *
5474 	 * NOTE: The IPMP meta-interface is special-cased because it starts
5475 	 * with no underlying interfaces (and thus an unknown broadcast
5476 	 * address length), but we enforce that an interface is broadcast-
5477 	 * capable as part of allowing it to join a group.
5478 	 */
5479 	if (ill->ill_bcast_addr_length == 0 && !IS_IPMP(ill)) {
5480 		if (ill->ill_resolver_mp != NULL)
5481 			freemsg(ill->ill_resolver_mp);
5482 		if (ill->ill_bcast_mp != NULL)
5483 			freemsg(ill->ill_bcast_mp);
5484 		if (ill->ill_flags & ILLF_XRESOLV)
5485 			ill->ill_net_type = IRE_IF_RESOLVER;
5486 		else
5487 			ill->ill_net_type = IRE_IF_NORESOLVER;
5488 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5489 		    ill->ill_phys_addr_length,
5490 		    ill->ill_sap,
5491 		    ill->ill_sap_length);
5492 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5493 
5494 		if (ill->ill_isv6)
5495 			/*
5496 			 * Note: xresolv interfaces will eventually need NOARP
5497 			 * set here as well, but that will require those
5498 			 * external resolvers to have some knowledge of
5499 			 * that flag and act appropriately. Not to be changed
5500 			 * at present.
5501 			 */
5502 			ill->ill_flags |= ILLF_NONUD;
5503 		else
5504 			ill->ill_flags |= ILLF_NOARP;
5505 
5506 		if (ill->ill_phys_addr_length == 0) {
5507 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5508 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5509 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5510 			} else {
5511 				/* pt-pt supports multicast. */
5512 				ill->ill_flags |= ILLF_MULTICAST;
5513 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5514 			}
5515 		}
5516 	} else {
5517 		ill->ill_net_type = IRE_IF_RESOLVER;
5518 		if (ill->ill_bcast_mp != NULL)
5519 			freemsg(ill->ill_bcast_mp);
5520 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5521 		    ill->ill_bcast_addr_length, ill->ill_sap,
5522 		    ill->ill_sap_length);
5523 		/*
5524 		 * Later detect lack of DLPI driver multicast
5525 		 * capability by catching DL_ENABMULTI errors in
5526 		 * ip_rput_dlpi.
5527 		 */
5528 		ill->ill_flags |= ILLF_MULTICAST;
5529 		if (!ill->ill_isv6)
5530 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5531 	}
5532 
5533 	/* For IPMP, PHYI_IPMP should already be set by ipif_allocate() */
5534 	if (ill->ill_mactype == SUNW_DL_IPMP)
5535 		ASSERT(ill->ill_phyint->phyint_flags & PHYI_IPMP);
5536 
5537 	/* By default an interface does not support any CoS marking */
5538 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5539 
5540 	/*
5541 	 * If we get QoS information in DL_INFO_ACK, the device supports
5542 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5543 	 */
5544 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5545 	    dlia->dl_qos_length);
5546 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5547 		ill->ill_flags |= ILLF_COS_ENABLED;
5548 	}
5549 
5550 	/* Clear any previous error indication. */
5551 	ill->ill_error = 0;
5552 	freemsg(mp);
5553 }
5554 
5555 /*
5556  * Perform various checks to verify that an address would make sense as a
5557  * local, remote, or subnet interface address.
5558  */
5559 static boolean_t
5560 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5561 {
5562 	ipaddr_t	net_mask;
5563 
5564 	/*
5565 	 * Don't allow all zeroes, or all ones, but allow
5566 	 * all ones netmask.
5567 	 */
5568 	if ((net_mask = ip_net_mask(addr)) == 0)
5569 		return (B_FALSE);
5570 	/* A given netmask overrides the "guess" netmask */
5571 	if (subnet_mask != 0)
5572 		net_mask = subnet_mask;
5573 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5574 	    (addr == (addr | ~net_mask)))) {
5575 		return (B_FALSE);
5576 	}
5577 
5578 	/*
5579 	 * Even if the netmask is all ones, we do not allow address to be
5580 	 * 255.255.255.255
5581 	 */
5582 	if (addr == INADDR_BROADCAST)
5583 		return (B_FALSE);
5584 
5585 	if (CLASSD(addr))
5586 		return (B_FALSE);
5587 
5588 	return (B_TRUE);
5589 }
5590 
5591 #define	V6_IPIF_LINKLOCAL(p)	\
5592 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
5593 
5594 /*
5595  * Compare two given ipifs and check if the second one is better than
5596  * the first one using the order of preference (not taking deprecated
5597  * into acount) specified in ipif_lookup_multicast().
5598  */
5599 static boolean_t
5600 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
5601 {
5602 	/* Check the least preferred first. */
5603 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
5604 		/* If both ipifs are the same, use the first one. */
5605 		if (IS_LOOPBACK(new_ipif->ipif_ill))
5606 			return (B_FALSE);
5607 		else
5608 			return (B_TRUE);
5609 	}
5610 
5611 	/* For IPv6, check for link local address. */
5612 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
5613 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5614 		    V6_IPIF_LINKLOCAL(new_ipif)) {
5615 			/* The second one is equal or less preferred. */
5616 			return (B_FALSE);
5617 		} else {
5618 			return (B_TRUE);
5619 		}
5620 	}
5621 
5622 	/* Then check for point to point interface. */
5623 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
5624 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5625 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
5626 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
5627 			return (B_FALSE);
5628 		} else {
5629 			return (B_TRUE);
5630 		}
5631 	}
5632 
5633 	/* old_ipif is a normal interface, so no need to use the new one. */
5634 	return (B_FALSE);
5635 }
5636 
5637 /*
5638  * Find a mulitcast-capable ipif given an IP instance and zoneid.
5639  * The ipif must be up, and its ill must multicast-capable, not
5640  * condemned, not an underlying interface in an IPMP group, and
5641  * not a VNI interface.  Order of preference:
5642  *
5643  * 	1a. normal
5644  * 	1b. normal, but deprecated
5645  * 	2a. point to point
5646  * 	2b. point to point, but deprecated
5647  * 	3a. link local
5648  * 	3b. link local, but deprecated
5649  * 	4. loopback.
5650  */
5651 ipif_t *
5652 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
5653 {
5654 	ill_t			*ill;
5655 	ill_walk_context_t	ctx;
5656 	ipif_t			*ipif;
5657 	ipif_t			*saved_ipif = NULL;
5658 	ipif_t			*dep_ipif = NULL;
5659 
5660 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5661 	if (isv6)
5662 		ill = ILL_START_WALK_V6(&ctx, ipst);
5663 	else
5664 		ill = ILL_START_WALK_V4(&ctx, ipst);
5665 
5666 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5667 		mutex_enter(&ill->ill_lock);
5668 		if (IS_VNI(ill) || IS_UNDER_IPMP(ill) || !ILL_CAN_LOOKUP(ill) ||
5669 		    !(ill->ill_flags & ILLF_MULTICAST)) {
5670 			mutex_exit(&ill->ill_lock);
5671 			continue;
5672 		}
5673 		for (ipif = ill->ill_ipif; ipif != NULL;
5674 		    ipif = ipif->ipif_next) {
5675 			if (zoneid != ipif->ipif_zoneid &&
5676 			    zoneid != ALL_ZONES &&
5677 			    ipif->ipif_zoneid != ALL_ZONES) {
5678 				continue;
5679 			}
5680 			if (!(ipif->ipif_flags & IPIF_UP) ||
5681 			    !IPIF_CAN_LOOKUP(ipif)) {
5682 				continue;
5683 			}
5684 
5685 			/*
5686 			 * Found one candidate.  If it is deprecated,
5687 			 * remember it in dep_ipif.  If it is not deprecated,
5688 			 * remember it in saved_ipif.
5689 			 */
5690 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
5691 				if (dep_ipif == NULL) {
5692 					dep_ipif = ipif;
5693 				} else if (ipif_comp_multi(dep_ipif, ipif,
5694 				    isv6)) {
5695 					/*
5696 					 * If the previous dep_ipif does not
5697 					 * belong to the same ill, we've done
5698 					 * a ipif_refhold() on it.  So we need
5699 					 * to release it.
5700 					 */
5701 					if (dep_ipif->ipif_ill != ill)
5702 						ipif_refrele(dep_ipif);
5703 					dep_ipif = ipif;
5704 				}
5705 				continue;
5706 			}
5707 			if (saved_ipif == NULL) {
5708 				saved_ipif = ipif;
5709 			} else {
5710 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
5711 					if (saved_ipif->ipif_ill != ill)
5712 						ipif_refrele(saved_ipif);
5713 					saved_ipif = ipif;
5714 				}
5715 			}
5716 		}
5717 		/*
5718 		 * Before going to the next ill, do a ipif_refhold() on the
5719 		 * saved ones.
5720 		 */
5721 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
5722 			ipif_refhold_locked(saved_ipif);
5723 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
5724 			ipif_refhold_locked(dep_ipif);
5725 		mutex_exit(&ill->ill_lock);
5726 	}
5727 	rw_exit(&ipst->ips_ill_g_lock);
5728 
5729 	/*
5730 	 * If we have only the saved_ipif, return it.  But if we have both
5731 	 * saved_ipif and dep_ipif, check to see which one is better.
5732 	 */
5733 	if (saved_ipif != NULL) {
5734 		if (dep_ipif != NULL) {
5735 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
5736 				ipif_refrele(saved_ipif);
5737 				return (dep_ipif);
5738 			} else {
5739 				ipif_refrele(dep_ipif);
5740 				return (saved_ipif);
5741 			}
5742 		}
5743 		return (saved_ipif);
5744 	} else {
5745 		return (dep_ipif);
5746 	}
5747 }
5748 
5749 /*
5750  * This function is called when an application does not specify an interface
5751  * to be used for multicast traffic (joining a group/sending data).  It
5752  * calls ire_lookup_multi() to look for an interface route for the
5753  * specified multicast group.  Doing this allows the administrator to add
5754  * prefix routes for multicast to indicate which interface to be used for
5755  * multicast traffic in the above scenario.  The route could be for all
5756  * multicast (224.0/4), for a single multicast group (a /32 route) or
5757  * anything in between.  If there is no such multicast route, we just find
5758  * any multicast capable interface and return it.  The returned ipif
5759  * is refhold'ed.
5760  */
5761 ipif_t *
5762 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5763 {
5764 	ire_t			*ire;
5765 	ipif_t			*ipif;
5766 
5767 	ire = ire_lookup_multi(group, zoneid, ipst);
5768 	if (ire != NULL) {
5769 		ipif = ire->ire_ipif;
5770 		ipif_refhold(ipif);
5771 		ire_refrele(ire);
5772 		return (ipif);
5773 	}
5774 
5775 	return (ipif_lookup_multicast(ipst, zoneid, B_FALSE));
5776 }
5777 
5778 /*
5779  * Look for an ipif with the specified interface address and destination.
5780  * The destination address is used only for matching point-to-point interfaces.
5781  */
5782 ipif_t *
5783 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5784     ipsq_func_t func, int *error, ip_stack_t *ipst)
5785 {
5786 	ipif_t	*ipif;
5787 	ill_t	*ill;
5788 	ill_walk_context_t ctx;
5789 	ipsq_t	*ipsq;
5790 
5791 	if (error != NULL)
5792 		*error = 0;
5793 
5794 	/*
5795 	 * First match all the point-to-point interfaces
5796 	 * before looking at non-point-to-point interfaces.
5797 	 * This is done to avoid returning non-point-to-point
5798 	 * ipif instead of unnumbered point-to-point ipif.
5799 	 */
5800 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5801 	ill = ILL_START_WALK_V4(&ctx, ipst);
5802 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5803 		GRAB_CONN_LOCK(q);
5804 		mutex_enter(&ill->ill_lock);
5805 		for (ipif = ill->ill_ipif; ipif != NULL;
5806 		    ipif = ipif->ipif_next) {
5807 			/* Allow the ipif to be down */
5808 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5809 			    (ipif->ipif_lcl_addr == if_addr) &&
5810 			    (ipif->ipif_pp_dst_addr == dst)) {
5811 				/*
5812 				 * The block comment at the start of ipif_down
5813 				 * explains the use of the macros used below
5814 				 */
5815 				if (IPIF_CAN_LOOKUP(ipif)) {
5816 					ipif_refhold_locked(ipif);
5817 					mutex_exit(&ill->ill_lock);
5818 					RELEASE_CONN_LOCK(q);
5819 					rw_exit(&ipst->ips_ill_g_lock);
5820 					return (ipif);
5821 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5822 					ipsq = ill->ill_phyint->phyint_ipsq;
5823 					mutex_enter(&ipsq->ipsq_lock);
5824 					mutex_enter(&ipsq->ipsq_xop->ipx_lock);
5825 					mutex_exit(&ill->ill_lock);
5826 					rw_exit(&ipst->ips_ill_g_lock);
5827 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5828 					    ill);
5829 					mutex_exit(&ipsq->ipsq_xop->ipx_lock);
5830 					mutex_exit(&ipsq->ipsq_lock);
5831 					RELEASE_CONN_LOCK(q);
5832 					if (error != NULL)
5833 						*error = EINPROGRESS;
5834 					return (NULL);
5835 				}
5836 			}
5837 		}
5838 		mutex_exit(&ill->ill_lock);
5839 		RELEASE_CONN_LOCK(q);
5840 	}
5841 	rw_exit(&ipst->ips_ill_g_lock);
5842 
5843 	/* lookup the ipif based on interface address */
5844 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
5845 	    ipst);
5846 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5847 	return (ipif);
5848 }
5849 
5850 /*
5851  * Common function for ipif_lookup_addr() and ipif_lookup_addr_exact().
5852  */
5853 static ipif_t *
5854 ipif_lookup_addr_common(ipaddr_t addr, ill_t *match_ill, boolean_t match_illgrp,
5855     zoneid_t zoneid, queue_t *q, mblk_t *mp, ipsq_func_t func, int *error,
5856     ip_stack_t *ipst)
5857 {
5858 	ipif_t  *ipif;
5859 	ill_t   *ill;
5860 	boolean_t ptp = B_FALSE;
5861 	ipsq_t	*ipsq;
5862 	ill_walk_context_t	ctx;
5863 
5864 	if (error != NULL)
5865 		*error = 0;
5866 
5867 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5868 	/*
5869 	 * Repeat twice, first based on local addresses and
5870 	 * next time for pointopoint.
5871 	 */
5872 repeat:
5873 	ill = ILL_START_WALK_V4(&ctx, ipst);
5874 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5875 		if (match_ill != NULL && ill != match_ill &&
5876 		    (!match_illgrp || !IS_IN_SAME_ILLGRP(ill, match_ill))) {
5877 			continue;
5878 		}
5879 		GRAB_CONN_LOCK(q);
5880 		mutex_enter(&ill->ill_lock);
5881 		for (ipif = ill->ill_ipif; ipif != NULL;
5882 		    ipif = ipif->ipif_next) {
5883 			if (zoneid != ALL_ZONES &&
5884 			    zoneid != ipif->ipif_zoneid &&
5885 			    ipif->ipif_zoneid != ALL_ZONES)
5886 				continue;
5887 			/* Allow the ipif to be down */
5888 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5889 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5890 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
5891 			    (ipif->ipif_pp_dst_addr == addr))) {
5892 				/*
5893 				 * The block comment at the start of ipif_down
5894 				 * explains the use of the macros used below
5895 				 */
5896 				if (IPIF_CAN_LOOKUP(ipif)) {
5897 					ipif_refhold_locked(ipif);
5898 					mutex_exit(&ill->ill_lock);
5899 					RELEASE_CONN_LOCK(q);
5900 					rw_exit(&ipst->ips_ill_g_lock);
5901 					return (ipif);
5902 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5903 					ipsq = ill->ill_phyint->phyint_ipsq;
5904 					mutex_enter(&ipsq->ipsq_lock);
5905 					mutex_enter(&ipsq->ipsq_xop->ipx_lock);
5906 					mutex_exit(&ill->ill_lock);
5907 					rw_exit(&ipst->ips_ill_g_lock);
5908 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5909 					    ill);
5910 					mutex_exit(&ipsq->ipsq_xop->ipx_lock);
5911 					mutex_exit(&ipsq->ipsq_lock);
5912 					RELEASE_CONN_LOCK(q);
5913 					if (error != NULL)
5914 						*error = EINPROGRESS;
5915 					return (NULL);
5916 				}
5917 			}
5918 		}
5919 		mutex_exit(&ill->ill_lock);
5920 		RELEASE_CONN_LOCK(q);
5921 	}
5922 
5923 	/* If we already did the ptp case, then we are done */
5924 	if (ptp) {
5925 		rw_exit(&ipst->ips_ill_g_lock);
5926 		if (error != NULL)
5927 			*error = ENXIO;
5928 		return (NULL);
5929 	}
5930 	ptp = B_TRUE;
5931 	goto repeat;
5932 }
5933 
5934 /*
5935  * Check if the address exists in the system.
5936  * We don't hold the conn_lock as we will not perform defered ipsqueue
5937  * operation.
5938  */
5939 boolean_t
5940 ip_addr_exists(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
5941 {
5942 	ipif_t  *ipif;
5943 	ill_t   *ill;
5944 	ill_walk_context_t	ctx;
5945 
5946 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5947 
5948 	ill = ILL_START_WALK_V4(&ctx, ipst);
5949 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5950 		mutex_enter(&ill->ill_lock);
5951 		for (ipif = ill->ill_ipif; ipif != NULL;
5952 		    ipif = ipif->ipif_next) {
5953 			if (zoneid != ALL_ZONES &&
5954 			    zoneid != ipif->ipif_zoneid &&
5955 			    ipif->ipif_zoneid != ALL_ZONES)
5956 				continue;
5957 			/* Allow the ipif to be down */
5958 			/*
5959 			 * XXX Different from ipif_lookup_addr(), we don't do
5960 			 * twice lookups. As from bind()'s point of view, we
5961 			 * may return once we find a match.
5962 			 */
5963 			if (((ipif->ipif_lcl_addr == addr) &&
5964 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5965 			    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5966 			    (ipif->ipif_pp_dst_addr == addr))) {
5967 				/*
5968 				 * Allow bind() to be successful even if the
5969 				 * ipif is with IPIF_CHANGING bit set.
5970 				 */
5971 				mutex_exit(&ill->ill_lock);
5972 				rw_exit(&ipst->ips_ill_g_lock);
5973 				return (B_TRUE);
5974 			}
5975 		}
5976 		mutex_exit(&ill->ill_lock);
5977 	}
5978 
5979 	rw_exit(&ipst->ips_ill_g_lock);
5980 	return (B_FALSE);
5981 }
5982 
5983 /*
5984  * Lookup an ipif with the specified address.  For point-to-point links we
5985  * look for matches on either the destination address or the local address,
5986  * but we skip the local address check if IPIF_UNNUMBERED is set.  If the
5987  * `match_ill' argument is non-NULL, the lookup is restricted to that ill
5988  * (or illgrp if `match_ill' is in an IPMP group).
5989  */
5990 ipif_t *
5991 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
5992     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
5993 {
5994 	return (ipif_lookup_addr_common(addr, match_ill, B_TRUE, zoneid, q, mp,
5995 	    func, error, ipst));
5996 }
5997 
5998 /*
5999  * Special abbreviated version of ipif_lookup_addr() that doesn't match
6000  * `match_ill' across the IPMP group.  This function is only needed in some
6001  * corner-cases; almost everything should use ipif_lookup_addr().
6002  */
6003 static ipif_t *
6004 ipif_lookup_addr_exact(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6005 {
6006 	ASSERT(match_ill != NULL);
6007 	return (ipif_lookup_addr_common(addr, match_ill, B_FALSE, ALL_ZONES,
6008 	    NULL, NULL, NULL, NULL, ipst));
6009 }
6010 
6011 /*
6012  * Look for an ipif with the specified address. For point-point links
6013  * we look for matches on either the destination address and the local
6014  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6015  * is set.
6016  * If the `match_ill' argument is non-NULL, the lookup is restricted to that
6017  * ill (or illgrp if `match_ill' is in an IPMP group).
6018  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
6019  */
6020 zoneid_t
6021 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6022 {
6023 	zoneid_t zoneid;
6024 	ipif_t  *ipif;
6025 	ill_t   *ill;
6026 	boolean_t ptp = B_FALSE;
6027 	ill_walk_context_t	ctx;
6028 
6029 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6030 	/*
6031 	 * Repeat twice, first based on local addresses and
6032 	 * next time for pointopoint.
6033 	 */
6034 repeat:
6035 	ill = ILL_START_WALK_V4(&ctx, ipst);
6036 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6037 		if (match_ill != NULL && ill != match_ill &&
6038 		    !IS_IN_SAME_ILLGRP(ill, match_ill)) {
6039 			continue;
6040 		}
6041 		mutex_enter(&ill->ill_lock);
6042 		for (ipif = ill->ill_ipif; ipif != NULL;
6043 		    ipif = ipif->ipif_next) {
6044 			/* Allow the ipif to be down */
6045 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6046 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6047 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6048 			    (ipif->ipif_pp_dst_addr == addr)) &&
6049 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
6050 				zoneid = ipif->ipif_zoneid;
6051 				mutex_exit(&ill->ill_lock);
6052 				rw_exit(&ipst->ips_ill_g_lock);
6053 				/*
6054 				 * If ipif_zoneid was ALL_ZONES then we have
6055 				 * a trusted extensions shared IP address.
6056 				 * In that case GLOBAL_ZONEID works to send.
6057 				 */
6058 				if (zoneid == ALL_ZONES)
6059 					zoneid = GLOBAL_ZONEID;
6060 				return (zoneid);
6061 			}
6062 		}
6063 		mutex_exit(&ill->ill_lock);
6064 	}
6065 
6066 	/* If we already did the ptp case, then we are done */
6067 	if (ptp) {
6068 		rw_exit(&ipst->ips_ill_g_lock);
6069 		return (ALL_ZONES);
6070 	}
6071 	ptp = B_TRUE;
6072 	goto repeat;
6073 }
6074 
6075 /*
6076  * Look for an ipif that matches the specified remote address i.e. the
6077  * ipif that would receive the specified packet.
6078  * First look for directly connected interfaces and then do a recursive
6079  * IRE lookup and pick the first ipif corresponding to the source address in the
6080  * ire.
6081  * Returns: held ipif
6082  */
6083 ipif_t *
6084 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6085 {
6086 	ipif_t	*ipif;
6087 	ire_t	*ire;
6088 	ip_stack_t	*ipst = ill->ill_ipst;
6089 
6090 	ASSERT(!ill->ill_isv6);
6091 
6092 	/*
6093 	 * Someone could be changing this ipif currently or change it
6094 	 * after we return this. Thus  a few packets could use the old
6095 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6096 	 * will atomically be updated or cleaned up with the new value
6097 	 * Thus we don't need a lock to check the flags or other attrs below.
6098 	 */
6099 	mutex_enter(&ill->ill_lock);
6100 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6101 		if (!IPIF_CAN_LOOKUP(ipif))
6102 			continue;
6103 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6104 		    ipif->ipif_zoneid != ALL_ZONES)
6105 			continue;
6106 		/* Allow the ipif to be down */
6107 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6108 			if ((ipif->ipif_pp_dst_addr == addr) ||
6109 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6110 			    ipif->ipif_lcl_addr == addr)) {
6111 				ipif_refhold_locked(ipif);
6112 				mutex_exit(&ill->ill_lock);
6113 				return (ipif);
6114 			}
6115 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6116 			ipif_refhold_locked(ipif);
6117 			mutex_exit(&ill->ill_lock);
6118 			return (ipif);
6119 		}
6120 	}
6121 	mutex_exit(&ill->ill_lock);
6122 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6123 	    NULL, MATCH_IRE_RECURSIVE, ipst);
6124 	if (ire != NULL) {
6125 		/*
6126 		 * The callers of this function wants to know the
6127 		 * interface on which they have to send the replies
6128 		 * back. For IREs that have ire_stq and ire_ipif
6129 		 * derived from different ills, we really don't care
6130 		 * what we return here.
6131 		 */
6132 		ipif = ire->ire_ipif;
6133 		if (ipif != NULL) {
6134 			ipif_refhold(ipif);
6135 			ire_refrele(ire);
6136 			return (ipif);
6137 		}
6138 		ire_refrele(ire);
6139 	}
6140 	/* Pick the first interface */
6141 	ipif = ipif_get_next_ipif(NULL, ill);
6142 	return (ipif);
6143 }
6144 
6145 /*
6146  * This func does not prevent refcnt from increasing. But if
6147  * the caller has taken steps to that effect, then this func
6148  * can be used to determine whether the ill has become quiescent
6149  */
6150 static boolean_t
6151 ill_is_quiescent(ill_t *ill)
6152 {
6153 	ipif_t	*ipif;
6154 
6155 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6156 
6157 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6158 		if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6159 			return (B_FALSE);
6160 		}
6161 	}
6162 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6163 		return (B_FALSE);
6164 	}
6165 	return (B_TRUE);
6166 }
6167 
6168 boolean_t
6169 ill_is_freeable(ill_t *ill)
6170 {
6171 	ipif_t	*ipif;
6172 
6173 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6174 
6175 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6176 		if (ipif->ipif_refcnt != 0 || !IPIF_FREE_OK(ipif)) {
6177 			return (B_FALSE);
6178 		}
6179 	}
6180 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
6181 		return (B_FALSE);
6182 	}
6183 	return (B_TRUE);
6184 }
6185 
6186 /*
6187  * This func does not prevent refcnt from increasing. But if
6188  * the caller has taken steps to that effect, then this func
6189  * can be used to determine whether the ipif has become quiescent
6190  */
6191 static boolean_t
6192 ipif_is_quiescent(ipif_t *ipif)
6193 {
6194 	ill_t *ill;
6195 
6196 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6197 
6198 	if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6199 		return (B_FALSE);
6200 	}
6201 
6202 	ill = ipif->ipif_ill;
6203 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6204 	    ill->ill_logical_down) {
6205 		return (B_TRUE);
6206 	}
6207 
6208 	/* This is the last ipif going down or being deleted on this ill */
6209 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6210 		return (B_FALSE);
6211 	}
6212 
6213 	return (B_TRUE);
6214 }
6215 
6216 /*
6217  * return true if the ipif can be destroyed: the ipif has to be quiescent
6218  * with zero references from ire/nce/ilm to it.
6219  */
6220 static boolean_t
6221 ipif_is_freeable(ipif_t *ipif)
6222 {
6223 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6224 	ASSERT(ipif->ipif_id != 0);
6225 	return (ipif->ipif_refcnt == 0 && IPIF_FREE_OK(ipif));
6226 }
6227 
6228 /*
6229  * The ipif/ill/ire has been refreled. Do the tail processing.
6230  * Determine if the ipif or ill in question has become quiescent and if so
6231  * wakeup close and/or restart any queued pending ioctl that is waiting
6232  * for the ipif_down (or ill_down)
6233  */
6234 void
6235 ipif_ill_refrele_tail(ill_t *ill)
6236 {
6237 	mblk_t	*mp;
6238 	conn_t	*connp;
6239 	ipsq_t	*ipsq;
6240 	ipxop_t	*ipx;
6241 	ipif_t	*ipif;
6242 	dl_notify_ind_t *dlindp;
6243 
6244 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6245 
6246 	if ((ill->ill_state_flags & ILL_CONDEMNED) && ill_is_freeable(ill)) {
6247 		/* ip_modclose() may be waiting */
6248 		cv_broadcast(&ill->ill_cv);
6249 	}
6250 
6251 	ipsq = ill->ill_phyint->phyint_ipsq;
6252 	mutex_enter(&ipsq->ipsq_lock);
6253 	ipx = ipsq->ipsq_xop;
6254 	mutex_enter(&ipx->ipx_lock);
6255 	if (ipx->ipx_waitfor == 0)	/* no one's waiting; bail */
6256 		goto unlock;
6257 
6258 	ASSERT(ipx->ipx_pending_mp != NULL && ipx->ipx_pending_ipif != NULL);
6259 
6260 	ipif = ipx->ipx_pending_ipif;
6261 	if (ipif->ipif_ill != ill) 	/* wait is for another ill; bail */
6262 		goto unlock;
6263 
6264 	switch (ipx->ipx_waitfor) {
6265 	case IPIF_DOWN:
6266 		if (!ipif_is_quiescent(ipif))
6267 			goto unlock;
6268 		break;
6269 	case IPIF_FREE:
6270 		if (!ipif_is_freeable(ipif))
6271 			goto unlock;
6272 		break;
6273 	case ILL_DOWN:
6274 		if (!ill_is_quiescent(ill))
6275 			goto unlock;
6276 		break;
6277 	case ILL_FREE:
6278 		/*
6279 		 * ILL_FREE is only for loopback; normal ill teardown waits
6280 		 * synchronously in ip_modclose() without using ipx_waitfor,
6281 		 * handled by the cv_broadcast() at the top of this function.
6282 		 */
6283 		if (!ill_is_freeable(ill))
6284 			goto unlock;
6285 		break;
6286 	default:
6287 		cmn_err(CE_PANIC, "ipsq: %p unknown ipx_waitfor %d\n",
6288 		    (void *)ipsq, ipx->ipx_waitfor);
6289 	}
6290 
6291 	ill_refhold_locked(ill);	/* for qwriter_ip() call below */
6292 	mutex_exit(&ipx->ipx_lock);
6293 	mp = ipsq_pending_mp_get(ipsq, &connp);
6294 	mutex_exit(&ipsq->ipsq_lock);
6295 	mutex_exit(&ill->ill_lock);
6296 
6297 	ASSERT(mp != NULL);
6298 	/*
6299 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6300 	 * we can only get here when the current operation decides it
6301 	 * it needs to quiesce via ipsq_pending_mp_add().
6302 	 */
6303 	switch (mp->b_datap->db_type) {
6304 	case M_PCPROTO:
6305 	case M_PROTO:
6306 		/*
6307 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6308 		 */
6309 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6310 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6311 
6312 		switch (dlindp->dl_notification) {
6313 		case DL_NOTE_PHYS_ADDR:
6314 			qwriter_ip(ill, ill->ill_rq, mp,
6315 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6316 			return;
6317 		default:
6318 			ASSERT(0);
6319 			ill_refrele(ill);
6320 		}
6321 		break;
6322 
6323 	case M_ERROR:
6324 	case M_HANGUP:
6325 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6326 		    B_TRUE);
6327 		return;
6328 
6329 	case M_IOCTL:
6330 	case M_IOCDATA:
6331 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6332 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6333 		return;
6334 
6335 	default:
6336 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6337 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6338 	}
6339 	return;
6340 unlock:
6341 	mutex_exit(&ipsq->ipsq_lock);
6342 	mutex_exit(&ipx->ipx_lock);
6343 	mutex_exit(&ill->ill_lock);
6344 }
6345 
6346 #ifdef DEBUG
6347 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6348 static void
6349 th_trace_rrecord(th_trace_t *th_trace)
6350 {
6351 	tr_buf_t *tr_buf;
6352 	uint_t lastref;
6353 
6354 	lastref = th_trace->th_trace_lastref;
6355 	lastref++;
6356 	if (lastref == TR_BUF_MAX)
6357 		lastref = 0;
6358 	th_trace->th_trace_lastref = lastref;
6359 	tr_buf = &th_trace->th_trbuf[lastref];
6360 	tr_buf->tr_time = lbolt;
6361 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
6362 }
6363 
6364 static void
6365 th_trace_free(void *value)
6366 {
6367 	th_trace_t *th_trace = value;
6368 
6369 	ASSERT(th_trace->th_refcnt == 0);
6370 	kmem_free(th_trace, sizeof (*th_trace));
6371 }
6372 
6373 /*
6374  * Find or create the per-thread hash table used to track object references.
6375  * The ipst argument is NULL if we shouldn't allocate.
6376  *
6377  * Accesses per-thread data, so there's no need to lock here.
6378  */
6379 static mod_hash_t *
6380 th_trace_gethash(ip_stack_t *ipst)
6381 {
6382 	th_hash_t *thh;
6383 
6384 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
6385 		mod_hash_t *mh;
6386 		char name[256];
6387 		size_t objsize, rshift;
6388 		int retv;
6389 
6390 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
6391 			return (NULL);
6392 		(void) snprintf(name, sizeof (name), "th_trace_%p",
6393 		    (void *)curthread);
6394 
6395 		/*
6396 		 * We use mod_hash_create_extended here rather than the more
6397 		 * obvious mod_hash_create_ptrhash because the latter has a
6398 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
6399 		 * block.
6400 		 */
6401 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
6402 		    MAX(sizeof (ire_t), sizeof (nce_t)));
6403 		rshift = highbit(objsize);
6404 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
6405 		    th_trace_free, mod_hash_byptr, (void *)rshift,
6406 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
6407 		if (mh == NULL) {
6408 			kmem_free(thh, sizeof (*thh));
6409 			return (NULL);
6410 		}
6411 		thh->thh_hash = mh;
6412 		thh->thh_ipst = ipst;
6413 		/*
6414 		 * We trace ills, ipifs, ires, and nces.  All of these are
6415 		 * per-IP-stack, so the lock on the thread list is as well.
6416 		 */
6417 		rw_enter(&ip_thread_rwlock, RW_WRITER);
6418 		list_insert_tail(&ip_thread_list, thh);
6419 		rw_exit(&ip_thread_rwlock);
6420 		retv = tsd_set(ip_thread_data, thh);
6421 		ASSERT(retv == 0);
6422 	}
6423 	return (thh != NULL ? thh->thh_hash : NULL);
6424 }
6425 
6426 boolean_t
6427 th_trace_ref(const void *obj, ip_stack_t *ipst)
6428 {
6429 	th_trace_t *th_trace;
6430 	mod_hash_t *mh;
6431 	mod_hash_val_t val;
6432 
6433 	if ((mh = th_trace_gethash(ipst)) == NULL)
6434 		return (B_FALSE);
6435 
6436 	/*
6437 	 * Attempt to locate the trace buffer for this obj and thread.
6438 	 * If it does not exist, then allocate a new trace buffer and
6439 	 * insert into the hash.
6440 	 */
6441 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
6442 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
6443 		if (th_trace == NULL)
6444 			return (B_FALSE);
6445 
6446 		th_trace->th_id = curthread;
6447 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
6448 		    (mod_hash_val_t)th_trace) != 0) {
6449 			kmem_free(th_trace, sizeof (th_trace_t));
6450 			return (B_FALSE);
6451 		}
6452 	} else {
6453 		th_trace = (th_trace_t *)val;
6454 	}
6455 
6456 	ASSERT(th_trace->th_refcnt >= 0 &&
6457 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
6458 
6459 	th_trace->th_refcnt++;
6460 	th_trace_rrecord(th_trace);
6461 	return (B_TRUE);
6462 }
6463 
6464 /*
6465  * For the purpose of tracing a reference release, we assume that global
6466  * tracing is always on and that the same thread initiated the reference hold
6467  * is releasing.
6468  */
6469 void
6470 th_trace_unref(const void *obj)
6471 {
6472 	int retv;
6473 	mod_hash_t *mh;
6474 	th_trace_t *th_trace;
6475 	mod_hash_val_t val;
6476 
6477 	mh = th_trace_gethash(NULL);
6478 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
6479 	ASSERT(retv == 0);
6480 	th_trace = (th_trace_t *)val;
6481 
6482 	ASSERT(th_trace->th_refcnt > 0);
6483 	th_trace->th_refcnt--;
6484 	th_trace_rrecord(th_trace);
6485 }
6486 
6487 /*
6488  * If tracing has been disabled, then we assume that the reference counts are
6489  * now useless, and we clear them out before destroying the entries.
6490  */
6491 void
6492 th_trace_cleanup(const void *obj, boolean_t trace_disable)
6493 {
6494 	th_hash_t	*thh;
6495 	mod_hash_t	*mh;
6496 	mod_hash_val_t	val;
6497 	th_trace_t	*th_trace;
6498 	int		retv;
6499 
6500 	rw_enter(&ip_thread_rwlock, RW_READER);
6501 	for (thh = list_head(&ip_thread_list); thh != NULL;
6502 	    thh = list_next(&ip_thread_list, thh)) {
6503 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
6504 		    &val) == 0) {
6505 			th_trace = (th_trace_t *)val;
6506 			if (trace_disable)
6507 				th_trace->th_refcnt = 0;
6508 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
6509 			ASSERT(retv == 0);
6510 		}
6511 	}
6512 	rw_exit(&ip_thread_rwlock);
6513 }
6514 
6515 void
6516 ipif_trace_ref(ipif_t *ipif)
6517 {
6518 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6519 
6520 	if (ipif->ipif_trace_disable)
6521 		return;
6522 
6523 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
6524 		ipif->ipif_trace_disable = B_TRUE;
6525 		ipif_trace_cleanup(ipif);
6526 	}
6527 }
6528 
6529 void
6530 ipif_untrace_ref(ipif_t *ipif)
6531 {
6532 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6533 
6534 	if (!ipif->ipif_trace_disable)
6535 		th_trace_unref(ipif);
6536 }
6537 
6538 void
6539 ill_trace_ref(ill_t *ill)
6540 {
6541 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6542 
6543 	if (ill->ill_trace_disable)
6544 		return;
6545 
6546 	if (!th_trace_ref(ill, ill->ill_ipst)) {
6547 		ill->ill_trace_disable = B_TRUE;
6548 		ill_trace_cleanup(ill);
6549 	}
6550 }
6551 
6552 void
6553 ill_untrace_ref(ill_t *ill)
6554 {
6555 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6556 
6557 	if (!ill->ill_trace_disable)
6558 		th_trace_unref(ill);
6559 }
6560 
6561 /*
6562  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
6563  * failure, ipif_trace_disable is set.
6564  */
6565 static void
6566 ipif_trace_cleanup(const ipif_t *ipif)
6567 {
6568 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
6569 }
6570 
6571 /*
6572  * Called when ill is unplumbed or when memory alloc fails.  Note that on
6573  * failure, ill_trace_disable is set.
6574  */
6575 static void
6576 ill_trace_cleanup(const ill_t *ill)
6577 {
6578 	th_trace_cleanup(ill, ill->ill_trace_disable);
6579 }
6580 #endif /* DEBUG */
6581 
6582 void
6583 ipif_refhold_locked(ipif_t *ipif)
6584 {
6585 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6586 	ipif->ipif_refcnt++;
6587 	IPIF_TRACE_REF(ipif);
6588 }
6589 
6590 void
6591 ipif_refhold(ipif_t *ipif)
6592 {
6593 	ill_t	*ill;
6594 
6595 	ill = ipif->ipif_ill;
6596 	mutex_enter(&ill->ill_lock);
6597 	ipif->ipif_refcnt++;
6598 	IPIF_TRACE_REF(ipif);
6599 	mutex_exit(&ill->ill_lock);
6600 }
6601 
6602 /*
6603  * Must not be called while holding any locks. Otherwise if this is
6604  * the last reference to be released there is a chance of recursive mutex
6605  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6606  * to restart an ioctl.
6607  */
6608 void
6609 ipif_refrele(ipif_t *ipif)
6610 {
6611 	ill_t	*ill;
6612 
6613 	ill = ipif->ipif_ill;
6614 
6615 	mutex_enter(&ill->ill_lock);
6616 	ASSERT(ipif->ipif_refcnt != 0);
6617 	ipif->ipif_refcnt--;
6618 	IPIF_UNTRACE_REF(ipif);
6619 	if (ipif->ipif_refcnt != 0) {
6620 		mutex_exit(&ill->ill_lock);
6621 		return;
6622 	}
6623 
6624 	/* Drops the ill_lock */
6625 	ipif_ill_refrele_tail(ill);
6626 }
6627 
6628 ipif_t *
6629 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6630 {
6631 	ipif_t	*ipif;
6632 
6633 	mutex_enter(&ill->ill_lock);
6634 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6635 	    ipif != NULL; ipif = ipif->ipif_next) {
6636 		if (!IPIF_CAN_LOOKUP(ipif))
6637 			continue;
6638 		ipif_refhold_locked(ipif);
6639 		mutex_exit(&ill->ill_lock);
6640 		return (ipif);
6641 	}
6642 	mutex_exit(&ill->ill_lock);
6643 	return (NULL);
6644 }
6645 
6646 /*
6647  * TODO: make this table extendible at run time
6648  * Return a pointer to the mac type info for 'mac_type'
6649  */
6650 static ip_m_t *
6651 ip_m_lookup(t_uscalar_t mac_type)
6652 {
6653 	ip_m_t	*ipm;
6654 
6655 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6656 		if (ipm->ip_m_mac_type == mac_type)
6657 			return (ipm);
6658 	return (NULL);
6659 }
6660 
6661 /*
6662  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6663  * ipif_arg is passed in to associate it with the correct interface.
6664  * We may need to restart this operation if the ipif cannot be looked up
6665  * due to an exclusive operation that is currently in progress. The restart
6666  * entry point is specified by 'func'
6667  */
6668 int
6669 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6670     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ire_t **ire_arg,
6671     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
6672     struct rtsa_s *sp, ip_stack_t *ipst)
6673 {
6674 	ire_t	*ire;
6675 	ire_t	*gw_ire = NULL;
6676 	ipif_t	*ipif = NULL;
6677 	boolean_t ipif_refheld = B_FALSE;
6678 	uint_t	type;
6679 	int	match_flags = MATCH_IRE_TYPE;
6680 	int	error;
6681 	tsol_gc_t *gc = NULL;
6682 	tsol_gcgrp_t *gcgrp = NULL;
6683 	boolean_t gcgrp_xtraref = B_FALSE;
6684 
6685 	ip1dbg(("ip_rt_add:"));
6686 
6687 	if (ire_arg != NULL)
6688 		*ire_arg = NULL;
6689 
6690 	/*
6691 	 * If this is the case of RTF_HOST being set, then we set the netmask
6692 	 * to all ones (regardless if one was supplied).
6693 	 */
6694 	if (flags & RTF_HOST)
6695 		mask = IP_HOST_MASK;
6696 
6697 	/*
6698 	 * Prevent routes with a zero gateway from being created (since
6699 	 * interfaces can currently be plumbed and brought up no assigned
6700 	 * address).
6701 	 */
6702 	if (gw_addr == 0)
6703 		return (ENETUNREACH);
6704 	/*
6705 	 * Get the ipif, if any, corresponding to the gw_addr
6706 	 */
6707 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &error,
6708 	    ipst);
6709 	if (ipif != NULL) {
6710 		if (IS_VNI(ipif->ipif_ill)) {
6711 			ipif_refrele(ipif);
6712 			return (EINVAL);
6713 		}
6714 		ipif_refheld = B_TRUE;
6715 	} else if (error == EINPROGRESS) {
6716 		ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6717 		return (EINPROGRESS);
6718 	} else {
6719 		error = 0;
6720 	}
6721 
6722 	if (ipif != NULL) {
6723 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6724 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6725 	} else {
6726 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6727 	}
6728 
6729 	/*
6730 	 * GateD will attempt to create routes with a loopback interface
6731 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6732 	 * these routes to be added, but create them as interface routes
6733 	 * since the gateway is an interface address.
6734 	 */
6735 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6736 		flags &= ~RTF_GATEWAY;
6737 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6738 		    mask == IP_HOST_MASK) {
6739 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6740 			    ALL_ZONES, NULL, match_flags, ipst);
6741 			if (ire != NULL) {
6742 				ire_refrele(ire);
6743 				if (ipif_refheld)
6744 					ipif_refrele(ipif);
6745 				return (EEXIST);
6746 			}
6747 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6748 			    "for 0x%x\n", (void *)ipif,
6749 			    ipif->ipif_ire_type,
6750 			    ntohl(ipif->ipif_lcl_addr)));
6751 			ire = ire_create(
6752 			    (uchar_t *)&dst_addr,	/* dest address */
6753 			    (uchar_t *)&mask,		/* mask */
6754 			    (uchar_t *)&ipif->ipif_src_addr,
6755 			    NULL,			/* no gateway */
6756 			    &ipif->ipif_mtu,
6757 			    NULL,
6758 			    ipif->ipif_rq,		/* recv-from queue */
6759 			    NULL,			/* no send-to queue */
6760 			    ipif->ipif_ire_type,	/* LOOPBACK */
6761 			    ipif,
6762 			    0,
6763 			    0,
6764 			    0,
6765 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6766 			    RTF_PRIVATE : 0,
6767 			    &ire_uinfo_null,
6768 			    NULL,
6769 			    NULL,
6770 			    ipst);
6771 
6772 			if (ire == NULL) {
6773 				if (ipif_refheld)
6774 					ipif_refrele(ipif);
6775 				return (ENOMEM);
6776 			}
6777 			error = ire_add(&ire, q, mp, func, B_FALSE);
6778 			if (error == 0)
6779 				goto save_ire;
6780 			if (ipif_refheld)
6781 				ipif_refrele(ipif);
6782 			return (error);
6783 
6784 		}
6785 	}
6786 
6787 	/*
6788 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6789 	 * and the gateway address provided is one of the system's interface
6790 	 * addresses.  By using the routing socket interface and supplying an
6791 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6792 	 * specifying an interface route to be created is available which uses
6793 	 * the interface index that specifies the outgoing interface rather than
6794 	 * the address of an outgoing interface (which may not be able to
6795 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6796 	 * flag, routes can be specified which not only specify the next-hop to
6797 	 * be used when routing to a certain prefix, but also which outgoing
6798 	 * interface should be used.
6799 	 *
6800 	 * Previously, interfaces would have unique addresses assigned to them
6801 	 * and so the address assigned to a particular interface could be used
6802 	 * to identify a particular interface.  One exception to this was the
6803 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6804 	 *
6805 	 * With the advent of IPv6 and its link-local addresses, this
6806 	 * restriction was relaxed and interfaces could share addresses between
6807 	 * themselves.  In fact, typically all of the link-local interfaces on
6808 	 * an IPv6 node or router will have the same link-local address.  In
6809 	 * order to differentiate between these interfaces, the use of an
6810 	 * interface index is necessary and this index can be carried inside a
6811 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6812 	 * of using the interface index, however, is that all of the ipif's that
6813 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6814 	 * cannot be used to differentiate between ipif's (or logical
6815 	 * interfaces) that belong to the same ill (physical interface).
6816 	 *
6817 	 * For example, in the following case involving IPv4 interfaces and
6818 	 * logical interfaces
6819 	 *
6820 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6821 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6822 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6823 	 *
6824 	 * the ipif's corresponding to each of these interface routes can be
6825 	 * uniquely identified by the "gateway" (actually interface address).
6826 	 *
6827 	 * In this case involving multiple IPv6 default routes to a particular
6828 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6829 	 * default route is of interest:
6830 	 *
6831 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6832 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6833 	 */
6834 
6835 	/* RTF_GATEWAY not set */
6836 	if (!(flags & RTF_GATEWAY)) {
6837 		queue_t	*stq;
6838 
6839 		if (sp != NULL) {
6840 			ip2dbg(("ip_rt_add: gateway security attributes "
6841 			    "cannot be set with interface route\n"));
6842 			if (ipif_refheld)
6843 				ipif_refrele(ipif);
6844 			return (EINVAL);
6845 		}
6846 
6847 		/*
6848 		 * As the interface index specified with the RTA_IFP sockaddr is
6849 		 * the same for all ipif's off of an ill, the matching logic
6850 		 * below uses MATCH_IRE_ILL if such an index was specified.
6851 		 * This means that routes sharing the same prefix when added
6852 		 * using a RTA_IFP sockaddr must have distinct interface
6853 		 * indices (namely, they must be on distinct ill's).
6854 		 *
6855 		 * On the other hand, since the gateway address will usually be
6856 		 * different for each ipif on the system, the matching logic
6857 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6858 		 * route.  This means that interface routes for the same prefix
6859 		 * can be created if they belong to distinct ipif's and if a
6860 		 * RTA_IFP sockaddr is not present.
6861 		 */
6862 		if (ipif_arg != NULL) {
6863 			if (ipif_refheld)  {
6864 				ipif_refrele(ipif);
6865 				ipif_refheld = B_FALSE;
6866 			}
6867 			ipif = ipif_arg;
6868 			match_flags |= MATCH_IRE_ILL;
6869 		} else {
6870 			/*
6871 			 * Check the ipif corresponding to the gw_addr
6872 			 */
6873 			if (ipif == NULL)
6874 				return (ENETUNREACH);
6875 			match_flags |= MATCH_IRE_IPIF;
6876 		}
6877 		ASSERT(ipif != NULL);
6878 
6879 		/*
6880 		 * We check for an existing entry at this point.
6881 		 *
6882 		 * Since a netmask isn't passed in via the ioctl interface
6883 		 * (SIOCADDRT), we don't check for a matching netmask in that
6884 		 * case.
6885 		 */
6886 		if (!ioctl_msg)
6887 			match_flags |= MATCH_IRE_MASK;
6888 		ire = ire_ftable_lookup(dst_addr, mask, 0, IRE_INTERFACE, ipif,
6889 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
6890 		if (ire != NULL) {
6891 			ire_refrele(ire);
6892 			if (ipif_refheld)
6893 				ipif_refrele(ipif);
6894 			return (EEXIST);
6895 		}
6896 
6897 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
6898 		    ? ipif->ipif_rq : ipif->ipif_wq;
6899 
6900 		/*
6901 		 * Create a copy of the IRE_LOOPBACK,
6902 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
6903 		 * the modified address and netmask.
6904 		 */
6905 		ire = ire_create(
6906 		    (uchar_t *)&dst_addr,
6907 		    (uint8_t *)&mask,
6908 		    (uint8_t *)&ipif->ipif_src_addr,
6909 		    NULL,
6910 		    &ipif->ipif_mtu,
6911 		    NULL,
6912 		    NULL,
6913 		    stq,
6914 		    ipif->ipif_net_type,
6915 		    ipif,
6916 		    0,
6917 		    0,
6918 		    0,
6919 		    flags,
6920 		    &ire_uinfo_null,
6921 		    NULL,
6922 		    NULL,
6923 		    ipst);
6924 		if (ire == NULL) {
6925 			if (ipif_refheld)
6926 				ipif_refrele(ipif);
6927 			return (ENOMEM);
6928 		}
6929 
6930 		/*
6931 		 * Some software (for example, GateD and Sun Cluster) attempts
6932 		 * to create (what amount to) IRE_PREFIX routes with the
6933 		 * loopback address as the gateway.  This is primarily done to
6934 		 * set up prefixes with the RTF_REJECT flag set (for example,
6935 		 * when generating aggregate routes.)
6936 		 *
6937 		 * If the IRE type (as defined by ipif->ipif_net_type) is
6938 		 * IRE_LOOPBACK, then we map the request into a
6939 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
6940 		 * these interface routes, by definition, can only be that.
6941 		 *
6942 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
6943 		 * routine, but rather using ire_create() directly.
6944 		 *
6945 		 */
6946 		if (ipif->ipif_net_type == IRE_LOOPBACK) {
6947 			ire->ire_type = IRE_IF_NORESOLVER;
6948 			ire->ire_flags |= RTF_BLACKHOLE;
6949 		}
6950 
6951 		error = ire_add(&ire, q, mp, func, B_FALSE);
6952 		if (error == 0)
6953 			goto save_ire;
6954 
6955 		/*
6956 		 * In the result of failure, ire_add() will have already
6957 		 * deleted the ire in question, so there is no need to
6958 		 * do that here.
6959 		 */
6960 		if (ipif_refheld)
6961 			ipif_refrele(ipif);
6962 		return (error);
6963 	}
6964 	if (ipif_refheld) {
6965 		ipif_refrele(ipif);
6966 		ipif_refheld = B_FALSE;
6967 	}
6968 
6969 	/*
6970 	 * Get an interface IRE for the specified gateway.
6971 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
6972 	 * gateway, it is currently unreachable and we fail the request
6973 	 * accordingly.
6974 	 */
6975 	ipif = ipif_arg;
6976 	if (ipif_arg != NULL)
6977 		match_flags |= MATCH_IRE_ILL;
6978 again:
6979 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
6980 	    ALL_ZONES, 0, NULL, match_flags, ipst);
6981 	if (gw_ire == NULL) {
6982 		/*
6983 		 * With IPMP, we allow host routes to influence in.mpathd's
6984 		 * target selection.  However, if the test addresses are on
6985 		 * their own network, the above lookup will fail since the
6986 		 * underlying IRE_INTERFACEs are marked hidden.  So allow
6987 		 * hidden test IREs to be found and try again.
6988 		 */
6989 		if (!(match_flags & MATCH_IRE_MARK_TESTHIDDEN))  {
6990 			match_flags |= MATCH_IRE_MARK_TESTHIDDEN;
6991 			goto again;
6992 		}
6993 		return (ENETUNREACH);
6994 	}
6995 
6996 	/*
6997 	 * We create one of three types of IREs as a result of this request
6998 	 * based on the netmask.  A netmask of all ones (which is automatically
6999 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7000 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7001 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7002 	 * destination prefix.
7003 	 */
7004 	if (mask == IP_HOST_MASK)
7005 		type = IRE_HOST;
7006 	else if (mask == 0)
7007 		type = IRE_DEFAULT;
7008 	else
7009 		type = IRE_PREFIX;
7010 
7011 	/* check for a duplicate entry */
7012 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7013 	    NULL, ALL_ZONES, 0, NULL,
7014 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7015 	if (ire != NULL) {
7016 		ire_refrele(gw_ire);
7017 		ire_refrele(ire);
7018 		return (EEXIST);
7019 	}
7020 
7021 	/* Security attribute exists */
7022 	if (sp != NULL) {
7023 		tsol_gcgrp_addr_t ga;
7024 
7025 		/* find or create the gateway credentials group */
7026 		ga.ga_af = AF_INET;
7027 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7028 
7029 		/* we hold reference to it upon success */
7030 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7031 		if (gcgrp == NULL) {
7032 			ire_refrele(gw_ire);
7033 			return (ENOMEM);
7034 		}
7035 
7036 		/*
7037 		 * Create and add the security attribute to the group; a
7038 		 * reference to the group is made upon allocating a new
7039 		 * entry successfully.  If it finds an already-existing
7040 		 * entry for the security attribute in the group, it simply
7041 		 * returns it and no new reference is made to the group.
7042 		 */
7043 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7044 		if (gc == NULL) {
7045 			/* release reference held by gcgrp_lookup */
7046 			GCGRP_REFRELE(gcgrp);
7047 			ire_refrele(gw_ire);
7048 			return (ENOMEM);
7049 		}
7050 	}
7051 
7052 	/* Create the IRE. */
7053 	ire = ire_create(
7054 	    (uchar_t *)&dst_addr,		/* dest address */
7055 	    (uchar_t *)&mask,			/* mask */
7056 	    /* src address assigned by the caller? */
7057 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7058 	    (flags & RTF_SETSRC)) ?  &src_addr : NULL),
7059 	    (uchar_t *)&gw_addr,		/* gateway address */
7060 	    &gw_ire->ire_max_frag,
7061 	    NULL,				/* no src nce */
7062 	    NULL,				/* no recv-from queue */
7063 	    NULL,				/* no send-to queue */
7064 	    (ushort_t)type,			/* IRE type */
7065 	    ipif_arg,
7066 	    0,
7067 	    0,
7068 	    0,
7069 	    flags,
7070 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7071 	    gc,					/* security attribute */
7072 	    NULL,
7073 	    ipst);
7074 
7075 	/*
7076 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7077 	 * reference to the 'gcgrp'. We can now release the extra reference
7078 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7079 	 */
7080 	if (gcgrp_xtraref)
7081 		GCGRP_REFRELE(gcgrp);
7082 	if (ire == NULL) {
7083 		if (gc != NULL)
7084 			GC_REFRELE(gc);
7085 		ire_refrele(gw_ire);
7086 		return (ENOMEM);
7087 	}
7088 
7089 	/*
7090 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7091 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7092 	 */
7093 
7094 	/* Add the new IRE. */
7095 	error = ire_add(&ire, q, mp, func, B_FALSE);
7096 	if (error != 0) {
7097 		/*
7098 		 * In the result of failure, ire_add() will have already
7099 		 * deleted the ire in question, so there is no need to
7100 		 * do that here.
7101 		 */
7102 		ire_refrele(gw_ire);
7103 		return (error);
7104 	}
7105 
7106 	if (flags & RTF_MULTIRT) {
7107 		/*
7108 		 * Invoke the CGTP (multirouting) filtering module
7109 		 * to add the dst address in the filtering database.
7110 		 * Replicated inbound packets coming from that address
7111 		 * will be filtered to discard the duplicates.
7112 		 * It is not necessary to call the CGTP filter hook
7113 		 * when the dst address is a broadcast or multicast,
7114 		 * because an IP source address cannot be a broadcast
7115 		 * or a multicast.
7116 		 */
7117 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7118 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7119 		if (ire_dst != NULL) {
7120 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
7121 			ire_refrele(ire_dst);
7122 			goto save_ire;
7123 		}
7124 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
7125 		    !CLASSD(ire->ire_addr)) {
7126 			int res = ipst->ips_ip_cgtp_filter_ops->cfo_add_dest_v4(
7127 			    ipst->ips_netstack->netstack_stackid,
7128 			    ire->ire_addr,
7129 			    ire->ire_gateway_addr,
7130 			    ire->ire_src_addr,
7131 			    gw_ire->ire_src_addr);
7132 			if (res != 0) {
7133 				ire_refrele(gw_ire);
7134 				ire_delete(ire);
7135 				return (res);
7136 			}
7137 		}
7138 	}
7139 
7140 	/*
7141 	 * Now that the prefix IRE entry has been created, delete any
7142 	 * existing gateway IRE cache entries as well as any IRE caches
7143 	 * using the gateway, and force them to be created through
7144 	 * ip_newroute.
7145 	 */
7146 	if (gc != NULL) {
7147 		ASSERT(gcgrp != NULL);
7148 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7149 	}
7150 
7151 save_ire:
7152 	if (gw_ire != NULL) {
7153 		ire_refrele(gw_ire);
7154 	}
7155 	if (ipif != NULL) {
7156 		/*
7157 		 * Save enough information so that we can recreate the IRE if
7158 		 * the interface goes down and then up.  The metrics associated
7159 		 * with the route will be saved as well when rts_setmetrics() is
7160 		 * called after the IRE has been created.  In the case where
7161 		 * memory cannot be allocated, none of this information will be
7162 		 * saved.
7163 		 */
7164 		ipif_save_ire(ipif, ire);
7165 	}
7166 	if (ioctl_msg)
7167 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7168 	if (ire_arg != NULL) {
7169 		/*
7170 		 * Store the ire that was successfully added into where ire_arg
7171 		 * points to so that callers don't have to look it up
7172 		 * themselves (but they are responsible for ire_refrele()ing
7173 		 * the ire when they are finished with it).
7174 		 */
7175 		*ire_arg = ire;
7176 	} else {
7177 		ire_refrele(ire);		/* Held in ire_add */
7178 	}
7179 	if (ipif_refheld)
7180 		ipif_refrele(ipif);
7181 	return (0);
7182 }
7183 
7184 /*
7185  * ip_rt_delete is called to delete an IPv4 route.
7186  * ipif_arg is passed in to associate it with the correct interface.
7187  * We may need to restart this operation if the ipif cannot be looked up
7188  * due to an exclusive operation that is currently in progress. The restart
7189  * entry point is specified by 'func'
7190  */
7191 /* ARGSUSED4 */
7192 int
7193 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7194     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, boolean_t ioctl_msg,
7195     queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst)
7196 {
7197 	ire_t	*ire = NULL;
7198 	ipif_t	*ipif;
7199 	boolean_t ipif_refheld = B_FALSE;
7200 	uint_t	type;
7201 	uint_t	match_flags = MATCH_IRE_TYPE;
7202 	int	err = 0;
7203 
7204 	ip1dbg(("ip_rt_delete:"));
7205 	/*
7206 	 * If this is the case of RTF_HOST being set, then we set the netmask
7207 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7208 	 */
7209 	if (flags & RTF_HOST) {
7210 		mask = IP_HOST_MASK;
7211 		match_flags |= MATCH_IRE_MASK;
7212 	} else if (rtm_addrs & RTA_NETMASK) {
7213 		match_flags |= MATCH_IRE_MASK;
7214 	}
7215 
7216 	/*
7217 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7218 	 * we check if the gateway address is one of our interfaces first,
7219 	 * and fall back on RTF_GATEWAY routes.
7220 	 *
7221 	 * This makes it possible to delete an original
7222 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7223 	 *
7224 	 * As the interface index specified with the RTA_IFP sockaddr is the
7225 	 * same for all ipif's off of an ill, the matching logic below uses
7226 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7227 	 * sharing the same prefix and interface index as the the route
7228 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7229 	 * is specified in the request.
7230 	 *
7231 	 * On the other hand, since the gateway address will usually be
7232 	 * different for each ipif on the system, the matching logic
7233 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7234 	 * route.  This means that interface routes for the same prefix can be
7235 	 * uniquely identified if they belong to distinct ipif's and if a
7236 	 * RTA_IFP sockaddr is not present.
7237 	 *
7238 	 * For more detail on specifying routes by gateway address and by
7239 	 * interface index, see the comments in ip_rt_add().
7240 	 */
7241 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &err,
7242 	    ipst);
7243 	if (ipif != NULL)
7244 		ipif_refheld = B_TRUE;
7245 	else if (err == EINPROGRESS)
7246 		return (err);
7247 	else
7248 		err = 0;
7249 	if (ipif != NULL) {
7250 		if (ipif_arg != NULL) {
7251 			if (ipif_refheld) {
7252 				ipif_refrele(ipif);
7253 				ipif_refheld = B_FALSE;
7254 			}
7255 			ipif = ipif_arg;
7256 			match_flags |= MATCH_IRE_ILL;
7257 		} else {
7258 			match_flags |= MATCH_IRE_IPIF;
7259 		}
7260 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7261 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
7262 			    ALL_ZONES, NULL, match_flags, ipst);
7263 		}
7264 		if (ire == NULL) {
7265 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7266 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
7267 			    match_flags, ipst);
7268 		}
7269 	}
7270 
7271 	if (ire == NULL) {
7272 		/*
7273 		 * At this point, the gateway address is not one of our own
7274 		 * addresses or a matching interface route was not found.  We
7275 		 * set the IRE type to lookup based on whether
7276 		 * this is a host route, a default route or just a prefix.
7277 		 *
7278 		 * If an ipif_arg was passed in, then the lookup is based on an
7279 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7280 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7281 		 * set as the route being looked up is not a traditional
7282 		 * interface route.
7283 		 */
7284 		match_flags &= ~MATCH_IRE_IPIF;
7285 		match_flags |= MATCH_IRE_GW;
7286 		if (ipif_arg != NULL)
7287 			match_flags |= MATCH_IRE_ILL;
7288 		if (mask == IP_HOST_MASK)
7289 			type = IRE_HOST;
7290 		else if (mask == 0)
7291 			type = IRE_DEFAULT;
7292 		else
7293 			type = IRE_PREFIX;
7294 		ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7295 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7296 	}
7297 
7298 	if (ipif_refheld)
7299 		ipif_refrele(ipif);
7300 
7301 	/* ipif is not refheld anymore */
7302 	if (ire == NULL)
7303 		return (ESRCH);
7304 
7305 	if (ire->ire_flags & RTF_MULTIRT) {
7306 		/*
7307 		 * Invoke the CGTP (multirouting) filtering module
7308 		 * to remove the dst address from the filtering database.
7309 		 * Packets coming from that address will no longer be
7310 		 * filtered to remove duplicates.
7311 		 */
7312 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
7313 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
7314 			    ipst->ips_netstack->netstack_stackid,
7315 			    ire->ire_addr, ire->ire_gateway_addr);
7316 		}
7317 		ip_cgtp_bcast_delete(ire, ipst);
7318 	}
7319 
7320 	ipif = ire->ire_ipif;
7321 	if (ipif != NULL)
7322 		ipif_remove_ire(ipif, ire);
7323 	if (ioctl_msg)
7324 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7325 	ire_delete(ire);
7326 	ire_refrele(ire);
7327 	return (err);
7328 }
7329 
7330 /*
7331  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7332  */
7333 /* ARGSUSED */
7334 int
7335 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7336     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7337 {
7338 	ipaddr_t dst_addr;
7339 	ipaddr_t gw_addr;
7340 	ipaddr_t mask;
7341 	int error = 0;
7342 	mblk_t *mp1;
7343 	struct rtentry *rt;
7344 	ipif_t *ipif = NULL;
7345 	ip_stack_t	*ipst;
7346 
7347 	ASSERT(q->q_next == NULL);
7348 	ipst = CONNQ_TO_IPST(q);
7349 
7350 	ip1dbg(("ip_siocaddrt:"));
7351 	/* Existence of mp1 verified in ip_wput_nondata */
7352 	mp1 = mp->b_cont->b_cont;
7353 	rt = (struct rtentry *)mp1->b_rptr;
7354 
7355 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7356 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7357 
7358 	/*
7359 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7360 	 * to a particular host address.  In this case, we set the netmask to
7361 	 * all ones for the particular destination address.  Otherwise,
7362 	 * determine the netmask to be used based on dst_addr and the interfaces
7363 	 * in use.
7364 	 */
7365 	if (rt->rt_flags & RTF_HOST) {
7366 		mask = IP_HOST_MASK;
7367 	} else {
7368 		/*
7369 		 * Note that ip_subnet_mask returns a zero mask in the case of
7370 		 * default (an all-zeroes address).
7371 		 */
7372 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7373 	}
7374 
7375 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7376 	    B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7377 	if (ipif != NULL)
7378 		ipif_refrele(ipif);
7379 	return (error);
7380 }
7381 
7382 /*
7383  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7384  */
7385 /* ARGSUSED */
7386 int
7387 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7388     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7389 {
7390 	ipaddr_t dst_addr;
7391 	ipaddr_t gw_addr;
7392 	ipaddr_t mask;
7393 	int error;
7394 	mblk_t *mp1;
7395 	struct rtentry *rt;
7396 	ipif_t *ipif = NULL;
7397 	ip_stack_t	*ipst;
7398 
7399 	ASSERT(q->q_next == NULL);
7400 	ipst = CONNQ_TO_IPST(q);
7401 
7402 	ip1dbg(("ip_siocdelrt:"));
7403 	/* Existence of mp1 verified in ip_wput_nondata */
7404 	mp1 = mp->b_cont->b_cont;
7405 	rt = (struct rtentry *)mp1->b_rptr;
7406 
7407 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7408 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7409 
7410 	/*
7411 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7412 	 * to a particular host address.  In this case, we set the netmask to
7413 	 * all ones for the particular destination address.  Otherwise,
7414 	 * determine the netmask to be used based on dst_addr and the interfaces
7415 	 * in use.
7416 	 */
7417 	if (rt->rt_flags & RTF_HOST) {
7418 		mask = IP_HOST_MASK;
7419 	} else {
7420 		/*
7421 		 * Note that ip_subnet_mask returns a zero mask in the case of
7422 		 * default (an all-zeroes address).
7423 		 */
7424 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7425 	}
7426 
7427 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7428 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE, q,
7429 	    mp, ip_process_ioctl, ipst);
7430 	if (ipif != NULL)
7431 		ipif_refrele(ipif);
7432 	return (error);
7433 }
7434 
7435 /*
7436  * Enqueue the mp onto the ipsq, chained by b_next.
7437  * b_prev stores the function to be executed later, and b_queue the queue
7438  * where this mp originated.
7439  */
7440 void
7441 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7442     ill_t *pending_ill)
7443 {
7444 	conn_t	*connp;
7445 	ipxop_t *ipx = ipsq->ipsq_xop;
7446 
7447 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7448 	ASSERT(MUTEX_HELD(&ipx->ipx_lock));
7449 	ASSERT(func != NULL);
7450 
7451 	mp->b_queue = q;
7452 	mp->b_prev = (void *)func;
7453 	mp->b_next = NULL;
7454 
7455 	switch (type) {
7456 	case CUR_OP:
7457 		if (ipx->ipx_mptail != NULL) {
7458 			ASSERT(ipx->ipx_mphead != NULL);
7459 			ipx->ipx_mptail->b_next = mp;
7460 		} else {
7461 			ASSERT(ipx->ipx_mphead == NULL);
7462 			ipx->ipx_mphead = mp;
7463 		}
7464 		ipx->ipx_mptail = mp;
7465 		break;
7466 
7467 	case NEW_OP:
7468 		if (ipsq->ipsq_xopq_mptail != NULL) {
7469 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7470 			ipsq->ipsq_xopq_mptail->b_next = mp;
7471 		} else {
7472 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7473 			ipsq->ipsq_xopq_mphead = mp;
7474 		}
7475 		ipsq->ipsq_xopq_mptail = mp;
7476 		ipx->ipx_ipsq_queued = B_TRUE;
7477 		break;
7478 
7479 	case SWITCH_OP:
7480 		ASSERT(ipsq->ipsq_swxop != NULL);
7481 		/* only one switch operation is currently allowed */
7482 		ASSERT(ipsq->ipsq_switch_mp == NULL);
7483 		ipsq->ipsq_switch_mp = mp;
7484 		ipx->ipx_ipsq_queued = B_TRUE;
7485 		break;
7486 	default:
7487 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7488 	}
7489 
7490 	if (CONN_Q(q) && pending_ill != NULL) {
7491 		connp = Q_TO_CONN(q);
7492 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7493 		connp->conn_oper_pending_ill = pending_ill;
7494 	}
7495 }
7496 
7497 /*
7498  * Dequeue the next message that requested exclusive access to this IPSQ's
7499  * xop.  Specifically:
7500  *
7501  *  1. If we're still processing the current operation on `ipsq', then
7502  *     dequeue the next message for the operation (from ipx_mphead), or
7503  *     return NULL if there are no queued messages for the operation.
7504  *     These messages are queued via CUR_OP to qwriter_ip() and friends.
7505  *
7506  *  2. If the current operation on `ipsq' has completed (ipx_current_ipif is
7507  *     not set) see if the ipsq has requested an xop switch.  If so, switch
7508  *     `ipsq' to a different xop.  Xop switches only happen when joining or
7509  *     leaving IPMP groups and require a careful dance -- see the comments
7510  *     in-line below for details.  If we're leaving a group xop or if we're
7511  *     joining a group xop and become writer on it, then we proceed to (3).
7512  *     Otherwise, we return NULL and exit the xop.
7513  *
7514  *  3. For each IPSQ in the xop, return any switch operation stored on
7515  *     ipsq_switch_mp (set via SWITCH_OP); these must be processed before
7516  *     any other messages queued on the IPSQ.  Otherwise, dequeue the next
7517  *     exclusive operation (queued via NEW_OP) stored on ipsq_xopq_mphead.
7518  *     Note that if the phyint tied to `ipsq' is not using IPMP there will
7519  *     only be one IPSQ in the xop.  Otherwise, there will be one IPSQ for
7520  *     each phyint in the group, including the IPMP meta-interface phyint.
7521  */
7522 static mblk_t *
7523 ipsq_dq(ipsq_t *ipsq)
7524 {
7525 	ill_t	*illv4, *illv6;
7526 	mblk_t	*mp;
7527 	ipsq_t	*xopipsq;
7528 	ipsq_t	*leftipsq = NULL;
7529 	ipxop_t *ipx;
7530 	phyint_t *phyi = ipsq->ipsq_phyint;
7531 	ip_stack_t *ipst = ipsq->ipsq_ipst;
7532 	boolean_t emptied = B_FALSE;
7533 
7534 	/*
7535 	 * Grab all the locks we need in the defined order (ill_g_lock ->
7536 	 * ipsq_lock -> ipx_lock); ill_g_lock is needed to use ipsq_next.
7537 	 */
7538 	rw_enter(&ipst->ips_ill_g_lock,
7539 	    ipsq->ipsq_swxop != NULL ? RW_WRITER : RW_READER);
7540 	mutex_enter(&ipsq->ipsq_lock);
7541 	ipx = ipsq->ipsq_xop;
7542 	mutex_enter(&ipx->ipx_lock);
7543 
7544 	/*
7545 	 * Dequeue the next message associated with the current exclusive
7546 	 * operation, if any.
7547 	 */
7548 	if ((mp = ipx->ipx_mphead) != NULL) {
7549 		ipx->ipx_mphead = mp->b_next;
7550 		if (ipx->ipx_mphead == NULL)
7551 			ipx->ipx_mptail = NULL;
7552 		mp->b_next = (void *)ipsq;
7553 		goto out;
7554 	}
7555 
7556 	if (ipx->ipx_current_ipif != NULL)
7557 		goto empty;
7558 
7559 	if (ipsq->ipsq_swxop != NULL) {
7560 		/*
7561 		 * The exclusive operation that is now being completed has
7562 		 * requested a switch to a different xop.  This happens
7563 		 * when an interface joins or leaves an IPMP group.  Joins
7564 		 * happen through SIOCSLIFGROUPNAME (ip_sioctl_groupname()).
7565 		 * Leaves happen via SIOCSLIFGROUPNAME, interface unplumb
7566 		 * (phyint_free()), or interface plumb for an ill type
7567 		 * not in the IPMP group (ip_rput_dlpi_writer()).
7568 		 *
7569 		 * Xop switches are not allowed on the IPMP meta-interface.
7570 		 */
7571 		ASSERT(phyi == NULL || !(phyi->phyint_flags & PHYI_IPMP));
7572 		ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
7573 		DTRACE_PROBE1(ipsq__switch, (ipsq_t *), ipsq);
7574 
7575 		if (ipsq->ipsq_swxop == &ipsq->ipsq_ownxop) {
7576 			/*
7577 			 * We're switching back to our own xop, so we have two
7578 			 * xop's to drain/exit: our own, and the group xop
7579 			 * that we are leaving.
7580 			 *
7581 			 * First, pull ourselves out of the group ipsq list.
7582 			 * This is safe since we're writer on ill_g_lock.
7583 			 */
7584 			ASSERT(ipsq->ipsq_xop != &ipsq->ipsq_ownxop);
7585 
7586 			xopipsq = ipx->ipx_ipsq;
7587 			while (xopipsq->ipsq_next != ipsq)
7588 				xopipsq = xopipsq->ipsq_next;
7589 
7590 			xopipsq->ipsq_next = ipsq->ipsq_next;
7591 			ipsq->ipsq_next = ipsq;
7592 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
7593 			ipsq->ipsq_swxop = NULL;
7594 
7595 			/*
7596 			 * Second, prepare to exit the group xop.  The actual
7597 			 * ipsq_exit() is done at the end of this function
7598 			 * since we cannot hold any locks across ipsq_exit().
7599 			 * Note that although we drop the group's ipx_lock, no
7600 			 * threads can proceed since we're still ipx_writer.
7601 			 */
7602 			leftipsq = xopipsq;
7603 			mutex_exit(&ipx->ipx_lock);
7604 
7605 			/*
7606 			 * Third, set ipx to point to our own xop (which was
7607 			 * inactive and therefore can be entered).
7608 			 */
7609 			ipx = ipsq->ipsq_xop;
7610 			mutex_enter(&ipx->ipx_lock);
7611 			ASSERT(ipx->ipx_writer == NULL);
7612 			ASSERT(ipx->ipx_current_ipif == NULL);
7613 		} else {
7614 			/*
7615 			 * We're switching from our own xop to a group xop.
7616 			 * The requestor of the switch must ensure that the
7617 			 * group xop cannot go away (e.g. by ensuring the
7618 			 * phyint associated with the xop cannot go away).
7619 			 *
7620 			 * If we can become writer on our new xop, then we'll
7621 			 * do the drain.  Otherwise, the current writer of our
7622 			 * new xop will do the drain when it exits.
7623 			 *
7624 			 * First, splice ourselves into the group IPSQ list.
7625 			 * This is safe since we're writer on ill_g_lock.
7626 			 */
7627 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
7628 
7629 			xopipsq = ipsq->ipsq_swxop->ipx_ipsq;
7630 			while (xopipsq->ipsq_next != ipsq->ipsq_swxop->ipx_ipsq)
7631 				xopipsq = xopipsq->ipsq_next;
7632 
7633 			xopipsq->ipsq_next = ipsq;
7634 			ipsq->ipsq_next = ipsq->ipsq_swxop->ipx_ipsq;
7635 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
7636 			ipsq->ipsq_swxop = NULL;
7637 
7638 			/*
7639 			 * Second, exit our own xop, since it's now unused.
7640 			 * This is safe since we've got the only reference.
7641 			 */
7642 			ASSERT(ipx->ipx_writer == curthread);
7643 			ipx->ipx_writer = NULL;
7644 			VERIFY(--ipx->ipx_reentry_cnt == 0);
7645 			ipx->ipx_ipsq_queued = B_FALSE;
7646 			mutex_exit(&ipx->ipx_lock);
7647 
7648 			/*
7649 			 * Third, set ipx to point to our new xop, and check
7650 			 * if we can become writer on it.  If we cannot, then
7651 			 * the current writer will drain the IPSQ group when
7652 			 * it exits.  Our ipsq_xop is guaranteed to be stable
7653 			 * because we're still holding ipsq_lock.
7654 			 */
7655 			ipx = ipsq->ipsq_xop;
7656 			mutex_enter(&ipx->ipx_lock);
7657 			if (ipx->ipx_writer != NULL ||
7658 			    ipx->ipx_current_ipif != NULL) {
7659 				goto out;
7660 			}
7661 		}
7662 
7663 		/*
7664 		 * Fourth, become writer on our new ipx before we continue
7665 		 * with the drain.  Note that we never dropped ipsq_lock
7666 		 * above, so no other thread could've raced with us to
7667 		 * become writer first.  Also, we're holding ipx_lock, so
7668 		 * no other thread can examine the ipx right now.
7669 		 */
7670 		ASSERT(ipx->ipx_current_ipif == NULL);
7671 		ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
7672 		VERIFY(ipx->ipx_reentry_cnt++ == 0);
7673 		ipx->ipx_writer = curthread;
7674 		ipx->ipx_forced = B_FALSE;
7675 #ifdef DEBUG
7676 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
7677 #endif
7678 	}
7679 
7680 	xopipsq = ipsq;
7681 	do {
7682 		/*
7683 		 * So that other operations operate on a consistent and
7684 		 * complete phyint, a switch message on an IPSQ must be
7685 		 * handled prior to any other operations on that IPSQ.
7686 		 */
7687 		if ((mp = xopipsq->ipsq_switch_mp) != NULL) {
7688 			xopipsq->ipsq_switch_mp = NULL;
7689 			ASSERT(mp->b_next == NULL);
7690 			mp->b_next = (void *)xopipsq;
7691 			goto out;
7692 		}
7693 
7694 		if ((mp = xopipsq->ipsq_xopq_mphead) != NULL) {
7695 			xopipsq->ipsq_xopq_mphead = mp->b_next;
7696 			if (xopipsq->ipsq_xopq_mphead == NULL)
7697 				xopipsq->ipsq_xopq_mptail = NULL;
7698 			mp->b_next = (void *)xopipsq;
7699 			goto out;
7700 		}
7701 	} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
7702 empty:
7703 	/*
7704 	 * There are no messages.  Further, we are holding ipx_lock, hence no
7705 	 * new messages can end up on any IPSQ in the xop.
7706 	 */
7707 	ipx->ipx_writer = NULL;
7708 	ipx->ipx_forced = B_FALSE;
7709 	VERIFY(--ipx->ipx_reentry_cnt == 0);
7710 	ipx->ipx_ipsq_queued = B_FALSE;
7711 	emptied = B_TRUE;
7712 #ifdef	DEBUG
7713 	ipx->ipx_depth = 0;
7714 #endif
7715 out:
7716 	mutex_exit(&ipx->ipx_lock);
7717 	mutex_exit(&ipsq->ipsq_lock);
7718 
7719 	/*
7720 	 * If we completely emptied the xop, then wake up any threads waiting
7721 	 * to enter any of the IPSQ's associated with it.
7722 	 */
7723 	if (emptied) {
7724 		xopipsq = ipsq;
7725 		do {
7726 			if ((phyi = xopipsq->ipsq_phyint) == NULL)
7727 				continue;
7728 
7729 			illv4 = phyi->phyint_illv4;
7730 			illv6 = phyi->phyint_illv6;
7731 
7732 			GRAB_ILL_LOCKS(illv4, illv6);
7733 			if (illv4 != NULL)
7734 				cv_broadcast(&illv4->ill_cv);
7735 			if (illv6 != NULL)
7736 				cv_broadcast(&illv6->ill_cv);
7737 			RELEASE_ILL_LOCKS(illv4, illv6);
7738 		} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
7739 	}
7740 	rw_exit(&ipst->ips_ill_g_lock);
7741 
7742 	/*
7743 	 * Now that all locks are dropped, exit the IPSQ we left.
7744 	 */
7745 	if (leftipsq != NULL)
7746 		ipsq_exit(leftipsq);
7747 
7748 	return (mp);
7749 }
7750 
7751 /*
7752  * Enter the ipsq corresponding to ill, by waiting synchronously till
7753  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7754  * will have to drain completely before ipsq_enter returns success.
7755  * ipx_current_ipif will be set if some exclusive op is in progress,
7756  * and the ipsq_exit logic will start the next enqueued op after
7757  * completion of the current op. If 'force' is used, we don't wait
7758  * for the enqueued ops. This is needed when a conn_close wants to
7759  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7760  * of an ill can also use this option. But we dont' use it currently.
7761  */
7762 #define	ENTER_SQ_WAIT_TICKS 100
7763 boolean_t
7764 ipsq_enter(ill_t *ill, boolean_t force, int type)
7765 {
7766 	ipsq_t	*ipsq;
7767 	ipxop_t *ipx;
7768 	boolean_t waited_enough = B_FALSE;
7769 
7770 	/*
7771 	 * Note that the relationship between ill and ipsq is fixed as long as
7772 	 * the ill is not ILL_CONDEMNED.  Holding ipsq_lock ensures the
7773 	 * relationship between the IPSQ and xop cannot change.  However,
7774 	 * since we cannot hold ipsq_lock across the cv_wait(), it may change
7775 	 * while we're waiting.  We wait on ill_cv and rely on ipsq_exit()
7776 	 * waking up all ills in the xop when it becomes available.
7777 	 */
7778 	mutex_enter(&ill->ill_lock);
7779 	for (;;) {
7780 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7781 			mutex_exit(&ill->ill_lock);
7782 			return (B_FALSE);
7783 		}
7784 
7785 		ipsq = ill->ill_phyint->phyint_ipsq;
7786 		mutex_enter(&ipsq->ipsq_lock);
7787 		ipx = ipsq->ipsq_xop;
7788 		mutex_enter(&ipx->ipx_lock);
7789 
7790 		if (ipx->ipx_writer == NULL && (type == CUR_OP ||
7791 		    ipx->ipx_current_ipif == NULL || waited_enough))
7792 			break;
7793 
7794 		if (!force || ipx->ipx_writer != NULL) {
7795 			mutex_exit(&ipx->ipx_lock);
7796 			mutex_exit(&ipsq->ipsq_lock);
7797 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7798 		} else {
7799 			mutex_exit(&ipx->ipx_lock);
7800 			mutex_exit(&ipsq->ipsq_lock);
7801 			(void) cv_timedwait(&ill->ill_cv,
7802 			    &ill->ill_lock, lbolt + ENTER_SQ_WAIT_TICKS);
7803 			waited_enough = B_TRUE;
7804 		}
7805 	}
7806 
7807 	ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
7808 	ASSERT(ipx->ipx_reentry_cnt == 0);
7809 	ipx->ipx_writer = curthread;
7810 	ipx->ipx_forced = (ipx->ipx_current_ipif != NULL);
7811 	ipx->ipx_reentry_cnt++;
7812 #ifdef DEBUG
7813 	ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
7814 #endif
7815 	mutex_exit(&ipx->ipx_lock);
7816 	mutex_exit(&ipsq->ipsq_lock);
7817 	mutex_exit(&ill->ill_lock);
7818 	return (B_TRUE);
7819 }
7820 
7821 boolean_t
7822 ill_perim_enter(ill_t *ill)
7823 {
7824 	return (ipsq_enter(ill, B_FALSE, CUR_OP));
7825 }
7826 
7827 void
7828 ill_perim_exit(ill_t *ill)
7829 {
7830 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
7831 }
7832 
7833 /*
7834  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7835  * certain critical operations like plumbing (i.e. most set ioctls), multicast
7836  * joins, igmp/mld timers, etc.  There is one ipsq per phyint. The ipsq
7837  * serializes exclusive ioctls issued by applications on a per ipsq basis in
7838  * ipsq_xopq_mphead. It also protects against multiple threads executing in
7839  * the ipsq. Responses from the driver pertain to the current ioctl (say a
7840  * DL_BIND_ACK in response to a DL_BIND_REQ initiated as part of bringing
7841  * up the interface) and are enqueued in ipx_mphead.
7842  *
7843  * If a thread does not want to reenter the ipsq when it is already writer,
7844  * it must make sure that the specified reentry point to be called later
7845  * when the ipsq is empty, nor any code path starting from the specified reentry
7846  * point must never ever try to enter the ipsq again. Otherwise it can lead
7847  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7848  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7849  * dequeues the requests waiting to become exclusive in ipx_mphead and calls
7850  * the reentry point. When the list at ipx_mphead becomes empty ipsq_exit
7851  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7852  * ioctl if the current ioctl has completed. If the current ioctl is still
7853  * in progress it simply returns. The current ioctl could be waiting for
7854  * a response from another module (arp or the driver or could be waiting for
7855  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipx_pending_mp
7856  * and ipx_pending_ipif are set. ipx_current_ipif is set throughout the
7857  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7858  * ipx_current_ipif is NULL which happens only once the ioctl is complete and
7859  * all associated DLPI operations have completed.
7860  */
7861 
7862 /*
7863  * Try to enter the IPSQ corresponding to `ipif' or `ill' exclusively (`ipif'
7864  * and `ill' cannot both be specified).  Returns a pointer to the entered IPSQ
7865  * on success, or NULL on failure.  The caller ensures ipif/ill is valid by
7866  * refholding it as necessary.  If the IPSQ cannot be entered and `func' is
7867  * non-NULL, then `func' will be called back with `q' and `mp' once the IPSQ
7868  * can be entered.  If `func' is NULL, then `q' and `mp' are ignored.
7869  */
7870 ipsq_t *
7871 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7872     ipsq_func_t func, int type, boolean_t reentry_ok)
7873 {
7874 	ipsq_t	*ipsq;
7875 	ipxop_t	*ipx;
7876 
7877 	/* Only 1 of ipif or ill can be specified */
7878 	ASSERT((ipif != NULL) ^ (ill != NULL));
7879 	if (ipif != NULL)
7880 		ill = ipif->ipif_ill;
7881 
7882 	/*
7883 	 * lock ordering: conn_lock -> ill_lock -> ipsq_lock -> ipx_lock.
7884 	 * ipx of an ipsq can't change when ipsq_lock is held.
7885 	 */
7886 	GRAB_CONN_LOCK(q);
7887 	mutex_enter(&ill->ill_lock);
7888 	ipsq = ill->ill_phyint->phyint_ipsq;
7889 	mutex_enter(&ipsq->ipsq_lock);
7890 	ipx = ipsq->ipsq_xop;
7891 	mutex_enter(&ipx->ipx_lock);
7892 
7893 	/*
7894 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7895 	 *    (Note: If the caller does not specify reentry_ok then neither
7896 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7897 	 *    again. Otherwise it can lead to an infinite loop
7898 	 * 2. Enter the ipsq if there is no current writer and this attempted
7899 	 *    entry is part of the current operation
7900 	 * 3. Enter the ipsq if there is no current writer and this is a new
7901 	 *    operation and the operation queue is empty and there is no
7902 	 *    operation currently in progress
7903 	 */
7904 	if ((ipx->ipx_writer == curthread && reentry_ok) ||
7905 	    (ipx->ipx_writer == NULL && (type == CUR_OP || (type == NEW_OP &&
7906 	    !ipx->ipx_ipsq_queued && ipx->ipx_current_ipif == NULL)))) {
7907 		/* Success. */
7908 		ipx->ipx_reentry_cnt++;
7909 		ipx->ipx_writer = curthread;
7910 		ipx->ipx_forced = B_FALSE;
7911 		mutex_exit(&ipx->ipx_lock);
7912 		mutex_exit(&ipsq->ipsq_lock);
7913 		mutex_exit(&ill->ill_lock);
7914 		RELEASE_CONN_LOCK(q);
7915 #ifdef DEBUG
7916 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
7917 #endif
7918 		return (ipsq);
7919 	}
7920 
7921 	if (func != NULL)
7922 		ipsq_enq(ipsq, q, mp, func, type, ill);
7923 
7924 	mutex_exit(&ipx->ipx_lock);
7925 	mutex_exit(&ipsq->ipsq_lock);
7926 	mutex_exit(&ill->ill_lock);
7927 	RELEASE_CONN_LOCK(q);
7928 	return (NULL);
7929 }
7930 
7931 /*
7932  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7933  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7934  * cannot be entered, the mp is queued for completion.
7935  */
7936 void
7937 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7938     boolean_t reentry_ok)
7939 {
7940 	ipsq_t	*ipsq;
7941 
7942 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
7943 
7944 	/*
7945 	 * Drop the caller's refhold on the ill.  This is safe since we either
7946 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
7947 	 * IPSQ, in which case we return without accessing ill anymore.  This
7948 	 * is needed because func needs to see the correct refcount.
7949 	 * e.g. removeif can work only then.
7950 	 */
7951 	ill_refrele(ill);
7952 	if (ipsq != NULL) {
7953 		(*func)(ipsq, q, mp, NULL);
7954 		ipsq_exit(ipsq);
7955 	}
7956 }
7957 
7958 /*
7959  * Exit the specified IPSQ.  If this is the final exit on it then drain it
7960  * prior to exiting.  Caller must be writer on the specified IPSQ.
7961  */
7962 void
7963 ipsq_exit(ipsq_t *ipsq)
7964 {
7965 	mblk_t *mp;
7966 	ipsq_t *mp_ipsq;
7967 	queue_t	*q;
7968 	phyint_t *phyi;
7969 	ipsq_func_t func;
7970 
7971 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7972 
7973 	ASSERT(ipsq->ipsq_xop->ipx_reentry_cnt >= 1);
7974 	if (ipsq->ipsq_xop->ipx_reentry_cnt != 1) {
7975 		ipsq->ipsq_xop->ipx_reentry_cnt--;
7976 		return;
7977 	}
7978 
7979 	for (;;) {
7980 		phyi = ipsq->ipsq_phyint;
7981 		mp = ipsq_dq(ipsq);
7982 		mp_ipsq = (mp == NULL) ? NULL : (ipsq_t *)mp->b_next;
7983 
7984 		/*
7985 		 * If we've changed to a new IPSQ, and the phyint associated
7986 		 * with the old one has gone away, free the old IPSQ.  Note
7987 		 * that this cannot happen while the IPSQ is in a group.
7988 		 */
7989 		if (mp_ipsq != ipsq && phyi == NULL) {
7990 			ASSERT(ipsq->ipsq_next == ipsq);
7991 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
7992 			ipsq_delete(ipsq);
7993 		}
7994 
7995 		if (mp == NULL)
7996 			break;
7997 
7998 		q = mp->b_queue;
7999 		func = (ipsq_func_t)mp->b_prev;
8000 		ipsq = mp_ipsq;
8001 		mp->b_next = mp->b_prev = NULL;
8002 		mp->b_queue = NULL;
8003 
8004 		/*
8005 		 * If 'q' is an conn queue, it is valid, since we did a
8006 		 * a refhold on the conn at the start of the ioctl.
8007 		 * If 'q' is an ill queue, it is valid, since close of an
8008 		 * ill will clean up its IPSQ.
8009 		 */
8010 		(*func)(ipsq, q, mp, NULL);
8011 	}
8012 }
8013 
8014 /*
8015  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
8016  * and `ioccmd'.
8017  */
8018 void
8019 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
8020 {
8021 	ipxop_t *ipx = ipsq->ipsq_xop;
8022 
8023 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8024 	ASSERT(ipx->ipx_current_ipif == NULL);
8025 	ASSERT(ipx->ipx_current_ioctl == 0);
8026 
8027 	ipx->ipx_current_done = B_FALSE;
8028 	ipx->ipx_current_ioctl = ioccmd;
8029 	mutex_enter(&ipx->ipx_lock);
8030 	ipx->ipx_current_ipif = ipif;
8031 	mutex_exit(&ipx->ipx_lock);
8032 }
8033 
8034 /*
8035  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
8036  * the next exclusive operation to begin once we ipsq_exit().  However, if
8037  * pending DLPI operations remain, then we will wait for the queue to drain
8038  * before allowing the next exclusive operation to begin.  This ensures that
8039  * DLPI operations from one exclusive operation are never improperly processed
8040  * as part of a subsequent exclusive operation.
8041  */
8042 void
8043 ipsq_current_finish(ipsq_t *ipsq)
8044 {
8045 	ipxop_t	*ipx = ipsq->ipsq_xop;
8046 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
8047 	ipif_t	*ipif = ipx->ipx_current_ipif;
8048 
8049 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8050 
8051 	/*
8052 	 * For SIOCLIFREMOVEIF, the ipif has been already been blown away
8053 	 * (but in that case, IPIF_CHANGING will already be clear and no
8054 	 * pending DLPI messages can remain).
8055 	 */
8056 	if (ipx->ipx_current_ioctl != SIOCLIFREMOVEIF) {
8057 		ill_t *ill = ipif->ipif_ill;
8058 
8059 		mutex_enter(&ill->ill_lock);
8060 		dlpi_pending = ill->ill_dlpi_pending;
8061 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
8062 		mutex_exit(&ill->ill_lock);
8063 	}
8064 
8065 	ASSERT(!ipx->ipx_current_done);
8066 	ipx->ipx_current_done = B_TRUE;
8067 	ipx->ipx_current_ioctl = 0;
8068 	if (dlpi_pending == DL_PRIM_INVAL) {
8069 		mutex_enter(&ipx->ipx_lock);
8070 		ipx->ipx_current_ipif = NULL;
8071 		mutex_exit(&ipx->ipx_lock);
8072 	}
8073 }
8074 
8075 /*
8076  * The ill is closing. Flush all messages on the ipsq that originated
8077  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
8078  * for this ill since ipsq_enter could not have entered until then.
8079  * New messages can't be queued since the CONDEMNED flag is set.
8080  */
8081 static void
8082 ipsq_flush(ill_t *ill)
8083 {
8084 	queue_t	*q;
8085 	mblk_t	*prev;
8086 	mblk_t	*mp;
8087 	mblk_t	*mp_next;
8088 	ipxop_t	*ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
8089 
8090 	ASSERT(IAM_WRITER_ILL(ill));
8091 
8092 	/*
8093 	 * Flush any messages sent up by the driver.
8094 	 */
8095 	mutex_enter(&ipx->ipx_lock);
8096 	for (prev = NULL, mp = ipx->ipx_mphead; mp != NULL; mp = mp_next) {
8097 		mp_next = mp->b_next;
8098 		q = mp->b_queue;
8099 		if (q == ill->ill_rq || q == ill->ill_wq) {
8100 			/* dequeue mp */
8101 			if (prev == NULL)
8102 				ipx->ipx_mphead = mp->b_next;
8103 			else
8104 				prev->b_next = mp->b_next;
8105 			if (ipx->ipx_mptail == mp) {
8106 				ASSERT(mp_next == NULL);
8107 				ipx->ipx_mptail = prev;
8108 			}
8109 			inet_freemsg(mp);
8110 		} else {
8111 			prev = mp;
8112 		}
8113 	}
8114 	mutex_exit(&ipx->ipx_lock);
8115 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8116 	ipsq_xopq_mp_cleanup(ill, NULL);
8117 	ill_pending_mp_cleanup(ill);
8118 }
8119 
8120 /*
8121  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8122  * refhold and return the associated ipif
8123  */
8124 /* ARGSUSED */
8125 int
8126 ip_extract_tunreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8127     cmd_info_t *ci, ipsq_func_t func)
8128 {
8129 	boolean_t exists;
8130 	struct iftun_req *ta;
8131 	ipif_t  *ipif;
8132 	ill_t   *ill;
8133 	boolean_t isv6;
8134 	mblk_t  *mp1;
8135 	int error;
8136 	conn_t  *connp;
8137 	ip_stack_t  *ipst;
8138 
8139 	/* Existence verified in ip_wput_nondata */
8140 	mp1 = mp->b_cont->b_cont;
8141 	ta = (struct iftun_req *)mp1->b_rptr;
8142 	/*
8143 	 * Null terminate the string to protect against buffer
8144 	 * overrun. String was generated by user code and may not
8145 	 * be trusted.
8146 	 */
8147 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8148 
8149 	connp = Q_TO_CONN(q);
8150 	isv6 = connp->conn_af_isv6;
8151 	ipst = connp->conn_netstack->netstack_ip;
8152 
8153 	/* Disallows implicit create */
8154 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8155 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8156 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8157 	if (ipif == NULL)
8158 		return (error);
8159 
8160 	if (ipif->ipif_id != 0) {
8161 		/*
8162 		 * We really don't want to set/get tunnel parameters
8163 		 * on virtual tunnel interfaces.  Only allow the
8164 		 * base tunnel to do these.
8165 		 */
8166 		ipif_refrele(ipif);
8167 		return (EINVAL);
8168 	}
8169 
8170 	/*
8171 	 * Send down to tunnel mod for ioctl processing.
8172 	 * Will finish ioctl in ip_rput_other().
8173 	 */
8174 	ill = ipif->ipif_ill;
8175 	if (ill->ill_net_type == IRE_LOOPBACK) {
8176 		ipif_refrele(ipif);
8177 		return (EOPNOTSUPP);
8178 	}
8179 
8180 	if (ill->ill_wq == NULL) {
8181 		ipif_refrele(ipif);
8182 		return (ENXIO);
8183 	}
8184 	/*
8185 	 * Mark the ioctl as coming from an IPv6 interface for
8186 	 * tun's convenience.
8187 	 */
8188 	if (ill->ill_isv6)
8189 		ta->ifta_flags |= 0x80000000;
8190 	ci->ci_ipif = ipif;
8191 	return (0);
8192 }
8193 
8194 /*
8195  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8196  * and return the associated ipif.
8197  * Return value:
8198  *	Non zero: An error has occurred. ci may not be filled out.
8199  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8200  *	a held ipif in ci.ci_ipif.
8201  */
8202 int
8203 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8204     cmd_info_t *ci, ipsq_func_t func)
8205 {
8206 	char		*name;
8207 	struct ifreq    *ifr;
8208 	struct lifreq    *lifr;
8209 	ipif_t		*ipif = NULL;
8210 	ill_t		*ill;
8211 	conn_t		*connp;
8212 	boolean_t	isv6;
8213 	boolean_t	exists;
8214 	int		err;
8215 	mblk_t		*mp1;
8216 	zoneid_t	zoneid;
8217 	ip_stack_t	*ipst;
8218 
8219 	if (q->q_next != NULL) {
8220 		ill = (ill_t *)q->q_ptr;
8221 		isv6 = ill->ill_isv6;
8222 		connp = NULL;
8223 		zoneid = ALL_ZONES;
8224 		ipst = ill->ill_ipst;
8225 	} else {
8226 		ill = NULL;
8227 		connp = Q_TO_CONN(q);
8228 		isv6 = connp->conn_af_isv6;
8229 		zoneid = connp->conn_zoneid;
8230 		if (zoneid == GLOBAL_ZONEID) {
8231 			/* global zone can access ipifs in all zones */
8232 			zoneid = ALL_ZONES;
8233 		}
8234 		ipst = connp->conn_netstack->netstack_ip;
8235 	}
8236 
8237 	/* Has been checked in ip_wput_nondata */
8238 	mp1 = mp->b_cont->b_cont;
8239 
8240 	if (ipip->ipi_cmd_type == IF_CMD) {
8241 		/* This a old style SIOC[GS]IF* command */
8242 		ifr = (struct ifreq *)mp1->b_rptr;
8243 		/*
8244 		 * Null terminate the string to protect against buffer
8245 		 * overrun. String was generated by user code and may not
8246 		 * be trusted.
8247 		 */
8248 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8249 		name = ifr->ifr_name;
8250 		ci->ci_sin = (sin_t *)&ifr->ifr_addr;
8251 		ci->ci_sin6 = NULL;
8252 		ci->ci_lifr = (struct lifreq *)ifr;
8253 	} else {
8254 		/* This a new style SIOC[GS]LIF* command */
8255 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
8256 		lifr = (struct lifreq *)mp1->b_rptr;
8257 		/*
8258 		 * Null terminate the string to protect against buffer
8259 		 * overrun. String was generated by user code and may not
8260 		 * be trusted.
8261 		 */
8262 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8263 		name = lifr->lifr_name;
8264 		ci->ci_sin = (sin_t *)&lifr->lifr_addr;
8265 		ci->ci_sin6 = (sin6_t *)&lifr->lifr_addr;
8266 		ci->ci_lifr = lifr;
8267 	}
8268 
8269 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
8270 		/*
8271 		 * The ioctl will be failed if the ioctl comes down
8272 		 * an conn stream
8273 		 */
8274 		if (ill == NULL) {
8275 			/*
8276 			 * Not an ill queue, return EINVAL same as the
8277 			 * old error code.
8278 			 */
8279 			return (ENXIO);
8280 		}
8281 		ipif = ill->ill_ipif;
8282 		ipif_refhold(ipif);
8283 	} else {
8284 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8285 		    &exists, isv6, zoneid,
8286 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8287 		    ipst);
8288 		if (ipif == NULL) {
8289 			if (err == EINPROGRESS)
8290 				return (err);
8291 			err = 0;	/* Ensure we don't use it below */
8292 		}
8293 	}
8294 
8295 	/*
8296 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8297 	 */
8298 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
8299 		ipif_refrele(ipif);
8300 		return (ENXIO);
8301 	}
8302 
8303 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8304 	    name[0] == '\0') {
8305 		/*
8306 		 * Handle a or a SIOC?IF* with a null name
8307 		 * during plumb (on the ill queue before the I_PLINK).
8308 		 */
8309 		ipif = ill->ill_ipif;
8310 		ipif_refhold(ipif);
8311 	}
8312 
8313 	if (ipif == NULL)
8314 		return (ENXIO);
8315 
8316 	ci->ci_ipif = ipif;
8317 	return (0);
8318 }
8319 
8320 /*
8321  * Return the total number of ipifs.
8322  */
8323 static uint_t
8324 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8325 {
8326 	uint_t numifs = 0;
8327 	ill_t	*ill;
8328 	ill_walk_context_t	ctx;
8329 	ipif_t	*ipif;
8330 
8331 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8332 	ill = ILL_START_WALK_V4(&ctx, ipst);
8333 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8334 		if (IS_UNDER_IPMP(ill))
8335 			continue;
8336 		for (ipif = ill->ill_ipif; ipif != NULL;
8337 		    ipif = ipif->ipif_next) {
8338 			if (ipif->ipif_zoneid == zoneid ||
8339 			    ipif->ipif_zoneid == ALL_ZONES)
8340 				numifs++;
8341 		}
8342 	}
8343 	rw_exit(&ipst->ips_ill_g_lock);
8344 	return (numifs);
8345 }
8346 
8347 /*
8348  * Return the total number of ipifs.
8349  */
8350 static uint_t
8351 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8352 {
8353 	uint_t numifs = 0;
8354 	ill_t	*ill;
8355 	ipif_t	*ipif;
8356 	ill_walk_context_t	ctx;
8357 
8358 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8359 
8360 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8361 	if (family == AF_INET)
8362 		ill = ILL_START_WALK_V4(&ctx, ipst);
8363 	else if (family == AF_INET6)
8364 		ill = ILL_START_WALK_V6(&ctx, ipst);
8365 	else
8366 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8367 
8368 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8369 		if (IS_UNDER_IPMP(ill) && !(lifn_flags & LIFC_UNDER_IPMP))
8370 			continue;
8371 
8372 		for (ipif = ill->ill_ipif; ipif != NULL;
8373 		    ipif = ipif->ipif_next) {
8374 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8375 			    !(lifn_flags & LIFC_NOXMIT))
8376 				continue;
8377 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8378 			    !(lifn_flags & LIFC_TEMPORARY))
8379 				continue;
8380 			if (((ipif->ipif_flags &
8381 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8382 			    IPIF_DEPRECATED)) ||
8383 			    IS_LOOPBACK(ill) ||
8384 			    !(ipif->ipif_flags & IPIF_UP)) &&
8385 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8386 				continue;
8387 
8388 			if (zoneid != ipif->ipif_zoneid &&
8389 			    ipif->ipif_zoneid != ALL_ZONES &&
8390 			    (zoneid != GLOBAL_ZONEID ||
8391 			    !(lifn_flags & LIFC_ALLZONES)))
8392 				continue;
8393 
8394 			numifs++;
8395 		}
8396 	}
8397 	rw_exit(&ipst->ips_ill_g_lock);
8398 	return (numifs);
8399 }
8400 
8401 uint_t
8402 ip_get_lifsrcofnum(ill_t *ill)
8403 {
8404 	uint_t numifs = 0;
8405 	ill_t	*ill_head = ill;
8406 	ip_stack_t	*ipst = ill->ill_ipst;
8407 
8408 	/*
8409 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8410 	 * other thread may be trying to relink the ILLs in this usesrc group
8411 	 * and adjusting the ill_usesrc_grp_next pointers
8412 	 */
8413 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8414 	if ((ill->ill_usesrc_ifindex == 0) &&
8415 	    (ill->ill_usesrc_grp_next != NULL)) {
8416 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8417 		    ill = ill->ill_usesrc_grp_next)
8418 			numifs++;
8419 	}
8420 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8421 
8422 	return (numifs);
8423 }
8424 
8425 /* Null values are passed in for ipif, sin, and ifreq */
8426 /* ARGSUSED */
8427 int
8428 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8429     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8430 {
8431 	int *nump;
8432 	conn_t *connp = Q_TO_CONN(q);
8433 
8434 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8435 
8436 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8437 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8438 
8439 	*nump = ip_get_numifs(connp->conn_zoneid,
8440 	    connp->conn_netstack->netstack_ip);
8441 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8442 	return (0);
8443 }
8444 
8445 /* Null values are passed in for ipif, sin, and ifreq */
8446 /* ARGSUSED */
8447 int
8448 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8449     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8450 {
8451 	struct lifnum *lifn;
8452 	mblk_t	*mp1;
8453 	conn_t *connp = Q_TO_CONN(q);
8454 
8455 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8456 
8457 	/* Existence checked in ip_wput_nondata */
8458 	mp1 = mp->b_cont->b_cont;
8459 
8460 	lifn = (struct lifnum *)mp1->b_rptr;
8461 	switch (lifn->lifn_family) {
8462 	case AF_UNSPEC:
8463 	case AF_INET:
8464 	case AF_INET6:
8465 		break;
8466 	default:
8467 		return (EAFNOSUPPORT);
8468 	}
8469 
8470 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8471 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8472 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8473 	return (0);
8474 }
8475 
8476 /* ARGSUSED */
8477 int
8478 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8479     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8480 {
8481 	STRUCT_HANDLE(ifconf, ifc);
8482 	mblk_t *mp1;
8483 	struct iocblk *iocp;
8484 	struct ifreq *ifr;
8485 	ill_walk_context_t	ctx;
8486 	ill_t	*ill;
8487 	ipif_t	*ipif;
8488 	struct sockaddr_in *sin;
8489 	int32_t	ifclen;
8490 	zoneid_t zoneid;
8491 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8492 
8493 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8494 
8495 	ip1dbg(("ip_sioctl_get_ifconf"));
8496 	/* Existence verified in ip_wput_nondata */
8497 	mp1 = mp->b_cont->b_cont;
8498 	iocp = (struct iocblk *)mp->b_rptr;
8499 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8500 
8501 	/*
8502 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8503 	 * the user buffer address and length into which the list of struct
8504 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8505 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8506 	 * the SIOCGIFCONF operation was redefined to simply provide
8507 	 * a large output buffer into which we are supposed to jam the ifreq
8508 	 * array.  The same ioctl command code was used, despite the fact that
8509 	 * both the applications and the kernel code had to change, thus making
8510 	 * it impossible to support both interfaces.
8511 	 *
8512 	 * For reasons not good enough to try to explain, the following
8513 	 * algorithm is used for deciding what to do with one of these:
8514 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8515 	 * form with the output buffer coming down as the continuation message.
8516 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8517 	 * and we have to copy in the ifconf structure to find out how big the
8518 	 * output buffer is and where to copy out to.  Sure no problem...
8519 	 *
8520 	 */
8521 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8522 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8523 		int numifs = 0;
8524 		size_t ifc_bufsize;
8525 
8526 		/*
8527 		 * Must be (better be!) continuation of a TRANSPARENT
8528 		 * IOCTL.  We just copied in the ifconf structure.
8529 		 */
8530 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8531 		    (struct ifconf *)mp1->b_rptr);
8532 
8533 		/*
8534 		 * Allocate a buffer to hold requested information.
8535 		 *
8536 		 * If ifc_len is larger than what is needed, we only
8537 		 * allocate what we will use.
8538 		 *
8539 		 * If ifc_len is smaller than what is needed, return
8540 		 * EINVAL.
8541 		 *
8542 		 * XXX: the ill_t structure can hava 2 counters, for
8543 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8544 		 * number of interfaces for a device, so we don't need
8545 		 * to count them here...
8546 		 */
8547 		numifs = ip_get_numifs(zoneid, ipst);
8548 
8549 		ifclen = STRUCT_FGET(ifc, ifc_len);
8550 		ifc_bufsize = numifs * sizeof (struct ifreq);
8551 		if (ifc_bufsize > ifclen) {
8552 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8553 				/* old behaviour */
8554 				return (EINVAL);
8555 			} else {
8556 				ifc_bufsize = ifclen;
8557 			}
8558 		}
8559 
8560 		mp1 = mi_copyout_alloc(q, mp,
8561 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8562 		if (mp1 == NULL)
8563 			return (ENOMEM);
8564 
8565 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8566 	}
8567 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8568 	/*
8569 	 * the SIOCGIFCONF ioctl only knows about
8570 	 * IPv4 addresses, so don't try to tell
8571 	 * it about interfaces with IPv6-only
8572 	 * addresses. (Last parm 'isv6' is B_FALSE)
8573 	 */
8574 
8575 	ifr = (struct ifreq *)mp1->b_rptr;
8576 
8577 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8578 	ill = ILL_START_WALK_V4(&ctx, ipst);
8579 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8580 		if (IS_UNDER_IPMP(ill))
8581 			continue;
8582 		for (ipif = ill->ill_ipif; ipif != NULL;
8583 		    ipif = ipif->ipif_next) {
8584 			if (zoneid != ipif->ipif_zoneid &&
8585 			    ipif->ipif_zoneid != ALL_ZONES)
8586 				continue;
8587 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8588 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8589 					/* old behaviour */
8590 					rw_exit(&ipst->ips_ill_g_lock);
8591 					return (EINVAL);
8592 				} else {
8593 					goto if_copydone;
8594 				}
8595 			}
8596 			ipif_get_name(ipif, ifr->ifr_name,
8597 			    sizeof (ifr->ifr_name));
8598 			sin = (sin_t *)&ifr->ifr_addr;
8599 			*sin = sin_null;
8600 			sin->sin_family = AF_INET;
8601 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8602 			ifr++;
8603 		}
8604 	}
8605 if_copydone:
8606 	rw_exit(&ipst->ips_ill_g_lock);
8607 	mp1->b_wptr = (uchar_t *)ifr;
8608 
8609 	if (STRUCT_BUF(ifc) != NULL) {
8610 		STRUCT_FSET(ifc, ifc_len,
8611 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8612 	}
8613 	return (0);
8614 }
8615 
8616 /*
8617  * Get the interfaces using the address hosted on the interface passed in,
8618  * as a source adddress
8619  */
8620 /* ARGSUSED */
8621 int
8622 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8623     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8624 {
8625 	mblk_t *mp1;
8626 	ill_t	*ill, *ill_head;
8627 	ipif_t	*ipif, *orig_ipif;
8628 	int	numlifs = 0;
8629 	size_t	lifs_bufsize, lifsmaxlen;
8630 	struct	lifreq *lifr;
8631 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8632 	uint_t	ifindex;
8633 	zoneid_t zoneid;
8634 	int err = 0;
8635 	boolean_t isv6 = B_FALSE;
8636 	struct	sockaddr_in	*sin;
8637 	struct	sockaddr_in6	*sin6;
8638 	STRUCT_HANDLE(lifsrcof, lifs);
8639 	ip_stack_t		*ipst;
8640 
8641 	ipst = CONNQ_TO_IPST(q);
8642 
8643 	ASSERT(q->q_next == NULL);
8644 
8645 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8646 
8647 	/* Existence verified in ip_wput_nondata */
8648 	mp1 = mp->b_cont->b_cont;
8649 
8650 	/*
8651 	 * Must be (better be!) continuation of a TRANSPARENT
8652 	 * IOCTL.  We just copied in the lifsrcof structure.
8653 	 */
8654 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8655 	    (struct lifsrcof *)mp1->b_rptr);
8656 
8657 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8658 		return (EINVAL);
8659 
8660 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8661 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8662 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8663 	    ip_process_ioctl, &err, ipst);
8664 	if (ipif == NULL) {
8665 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8666 		    ifindex));
8667 		return (err);
8668 	}
8669 
8670 	/* Allocate a buffer to hold requested information */
8671 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8672 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8673 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8674 	/* The actual size needed is always returned in lifs_len */
8675 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8676 
8677 	/* If the amount we need is more than what is passed in, abort */
8678 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8679 		ipif_refrele(ipif);
8680 		return (0);
8681 	}
8682 
8683 	mp1 = mi_copyout_alloc(q, mp,
8684 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8685 	if (mp1 == NULL) {
8686 		ipif_refrele(ipif);
8687 		return (ENOMEM);
8688 	}
8689 
8690 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8691 	bzero(mp1->b_rptr, lifs_bufsize);
8692 
8693 	lifr = (struct lifreq *)mp1->b_rptr;
8694 
8695 	ill = ill_head = ipif->ipif_ill;
8696 	orig_ipif = ipif;
8697 
8698 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8699 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8700 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8701 
8702 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8703 	for (; (ill != NULL) && (ill != ill_head);
8704 	    ill = ill->ill_usesrc_grp_next) {
8705 
8706 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8707 			break;
8708 
8709 		ipif = ill->ill_ipif;
8710 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
8711 		if (ipif->ipif_isv6) {
8712 			sin6 = (sin6_t *)&lifr->lifr_addr;
8713 			*sin6 = sin6_null;
8714 			sin6->sin6_family = AF_INET6;
8715 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8716 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8717 			    &ipif->ipif_v6net_mask);
8718 		} else {
8719 			sin = (sin_t *)&lifr->lifr_addr;
8720 			*sin = sin_null;
8721 			sin->sin_family = AF_INET;
8722 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8723 			lifr->lifr_addrlen = ip_mask_to_plen(
8724 			    ipif->ipif_net_mask);
8725 		}
8726 		lifr++;
8727 	}
8728 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8729 	rw_exit(&ipst->ips_ill_g_lock);
8730 	ipif_refrele(orig_ipif);
8731 	mp1->b_wptr = (uchar_t *)lifr;
8732 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8733 
8734 	return (0);
8735 }
8736 
8737 /* ARGSUSED */
8738 int
8739 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8740     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8741 {
8742 	mblk_t *mp1;
8743 	int	list;
8744 	ill_t	*ill;
8745 	ipif_t	*ipif;
8746 	int	flags;
8747 	int	numlifs = 0;
8748 	size_t	lifc_bufsize;
8749 	struct	lifreq *lifr;
8750 	sa_family_t	family;
8751 	struct	sockaddr_in	*sin;
8752 	struct	sockaddr_in6	*sin6;
8753 	ill_walk_context_t	ctx;
8754 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8755 	int32_t	lifclen;
8756 	zoneid_t zoneid;
8757 	STRUCT_HANDLE(lifconf, lifc);
8758 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8759 
8760 	ip1dbg(("ip_sioctl_get_lifconf"));
8761 
8762 	ASSERT(q->q_next == NULL);
8763 
8764 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8765 
8766 	/* Existence verified in ip_wput_nondata */
8767 	mp1 = mp->b_cont->b_cont;
8768 
8769 	/*
8770 	 * An extended version of SIOCGIFCONF that takes an
8771 	 * additional address family and flags field.
8772 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8773 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8774 	 * interfaces are omitted.
8775 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8776 	 * unless LIFC_TEMPORARY is specified.
8777 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8778 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8779 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8780 	 * has priority over LIFC_NOXMIT.
8781 	 */
8782 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8783 
8784 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8785 		return (EINVAL);
8786 
8787 	/*
8788 	 * Must be (better be!) continuation of a TRANSPARENT
8789 	 * IOCTL.  We just copied in the lifconf structure.
8790 	 */
8791 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8792 
8793 	family = STRUCT_FGET(lifc, lifc_family);
8794 	flags = STRUCT_FGET(lifc, lifc_flags);
8795 
8796 	switch (family) {
8797 	case AF_UNSPEC:
8798 		/*
8799 		 * walk all ILL's.
8800 		 */
8801 		list = MAX_G_HEADS;
8802 		break;
8803 	case AF_INET:
8804 		/*
8805 		 * walk only IPV4 ILL's.
8806 		 */
8807 		list = IP_V4_G_HEAD;
8808 		break;
8809 	case AF_INET6:
8810 		/*
8811 		 * walk only IPV6 ILL's.
8812 		 */
8813 		list = IP_V6_G_HEAD;
8814 		break;
8815 	default:
8816 		return (EAFNOSUPPORT);
8817 	}
8818 
8819 	/*
8820 	 * Allocate a buffer to hold requested information.
8821 	 *
8822 	 * If lifc_len is larger than what is needed, we only
8823 	 * allocate what we will use.
8824 	 *
8825 	 * If lifc_len is smaller than what is needed, return
8826 	 * EINVAL.
8827 	 */
8828 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
8829 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8830 	lifclen = STRUCT_FGET(lifc, lifc_len);
8831 	if (lifc_bufsize > lifclen) {
8832 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8833 			return (EINVAL);
8834 		else
8835 			lifc_bufsize = lifclen;
8836 	}
8837 
8838 	mp1 = mi_copyout_alloc(q, mp,
8839 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8840 	if (mp1 == NULL)
8841 		return (ENOMEM);
8842 
8843 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8844 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8845 
8846 	lifr = (struct lifreq *)mp1->b_rptr;
8847 
8848 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8849 	ill = ill_first(list, list, &ctx, ipst);
8850 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8851 		if (IS_UNDER_IPMP(ill) && !(flags & LIFC_UNDER_IPMP))
8852 			continue;
8853 
8854 		for (ipif = ill->ill_ipif; ipif != NULL;
8855 		    ipif = ipif->ipif_next) {
8856 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8857 			    !(flags & LIFC_NOXMIT))
8858 				continue;
8859 
8860 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8861 			    !(flags & LIFC_TEMPORARY))
8862 				continue;
8863 
8864 			if (((ipif->ipif_flags &
8865 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8866 			    IPIF_DEPRECATED)) ||
8867 			    IS_LOOPBACK(ill) ||
8868 			    !(ipif->ipif_flags & IPIF_UP)) &&
8869 			    (flags & LIFC_EXTERNAL_SOURCE))
8870 				continue;
8871 
8872 			if (zoneid != ipif->ipif_zoneid &&
8873 			    ipif->ipif_zoneid != ALL_ZONES &&
8874 			    (zoneid != GLOBAL_ZONEID ||
8875 			    !(flags & LIFC_ALLZONES)))
8876 				continue;
8877 
8878 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8879 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8880 					rw_exit(&ipst->ips_ill_g_lock);
8881 					return (EINVAL);
8882 				} else {
8883 					goto lif_copydone;
8884 				}
8885 			}
8886 
8887 			ipif_get_name(ipif, lifr->lifr_name,
8888 			    sizeof (lifr->lifr_name));
8889 			lifr->lifr_type = ill->ill_type;
8890 			if (ipif->ipif_isv6) {
8891 				sin6 = (sin6_t *)&lifr->lifr_addr;
8892 				*sin6 = sin6_null;
8893 				sin6->sin6_family = AF_INET6;
8894 				sin6->sin6_addr =
8895 				    ipif->ipif_v6lcl_addr;
8896 				lifr->lifr_addrlen =
8897 				    ip_mask_to_plen_v6(
8898 				    &ipif->ipif_v6net_mask);
8899 			} else {
8900 				sin = (sin_t *)&lifr->lifr_addr;
8901 				*sin = sin_null;
8902 				sin->sin_family = AF_INET;
8903 				sin->sin_addr.s_addr =
8904 				    ipif->ipif_lcl_addr;
8905 				lifr->lifr_addrlen =
8906 				    ip_mask_to_plen(
8907 				    ipif->ipif_net_mask);
8908 			}
8909 			lifr++;
8910 		}
8911 	}
8912 lif_copydone:
8913 	rw_exit(&ipst->ips_ill_g_lock);
8914 
8915 	mp1->b_wptr = (uchar_t *)lifr;
8916 	if (STRUCT_BUF(lifc) != NULL) {
8917 		STRUCT_FSET(lifc, lifc_len,
8918 		    (int)((uchar_t *)lifr - mp1->b_rptr));
8919 	}
8920 	return (0);
8921 }
8922 
8923 static void
8924 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
8925 {
8926 	ip6_asp_t *table;
8927 	size_t table_size;
8928 	mblk_t *data_mp;
8929 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8930 	ip_stack_t	*ipst;
8931 
8932 	if (q->q_next == NULL)
8933 		ipst = CONNQ_TO_IPST(q);
8934 	else
8935 		ipst = ILLQ_TO_IPST(q);
8936 
8937 	/* These two ioctls are I_STR only */
8938 	if (iocp->ioc_count == TRANSPARENT) {
8939 		miocnak(q, mp, 0, EINVAL);
8940 		return;
8941 	}
8942 
8943 	data_mp = mp->b_cont;
8944 	if (data_mp == NULL) {
8945 		/* The user passed us a NULL argument */
8946 		table = NULL;
8947 		table_size = iocp->ioc_count;
8948 	} else {
8949 		/*
8950 		 * The user provided a table.  The stream head
8951 		 * may have copied in the user data in chunks,
8952 		 * so make sure everything is pulled up
8953 		 * properly.
8954 		 */
8955 		if (MBLKL(data_mp) < iocp->ioc_count) {
8956 			mblk_t *new_data_mp;
8957 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
8958 			    NULL) {
8959 				miocnak(q, mp, 0, ENOMEM);
8960 				return;
8961 			}
8962 			freemsg(data_mp);
8963 			data_mp = new_data_mp;
8964 			mp->b_cont = data_mp;
8965 		}
8966 		table = (ip6_asp_t *)data_mp->b_rptr;
8967 		table_size = iocp->ioc_count;
8968 	}
8969 
8970 	switch (iocp->ioc_cmd) {
8971 	case SIOCGIP6ADDRPOLICY:
8972 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
8973 		if (iocp->ioc_rval == -1)
8974 			iocp->ioc_error = EINVAL;
8975 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8976 		else if (table != NULL &&
8977 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
8978 			ip6_asp_t *src = table;
8979 			ip6_asp32_t *dst = (void *)table;
8980 			int count = table_size / sizeof (ip6_asp_t);
8981 			int i;
8982 
8983 			/*
8984 			 * We need to do an in-place shrink of the array
8985 			 * to match the alignment attributes of the
8986 			 * 32-bit ABI looking at it.
8987 			 */
8988 			/* LINTED: logical expression always true: op "||" */
8989 			ASSERT(sizeof (*src) > sizeof (*dst));
8990 			for (i = 1; i < count; i++)
8991 				bcopy(src + i, dst + i, sizeof (*dst));
8992 		}
8993 #endif
8994 		break;
8995 
8996 	case SIOCSIP6ADDRPOLICY:
8997 		ASSERT(mp->b_prev == NULL);
8998 		mp->b_prev = (void *)q;
8999 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9000 		/*
9001 		 * We pass in the datamodel here so that the ip6_asp_replace()
9002 		 * routine can handle converting from 32-bit to native formats
9003 		 * where necessary.
9004 		 *
9005 		 * A better way to handle this might be to convert the inbound
9006 		 * data structure here, and hang it off a new 'mp'; thus the
9007 		 * ip6_asp_replace() logic would always be dealing with native
9008 		 * format data structures..
9009 		 *
9010 		 * (An even simpler way to handle these ioctls is to just
9011 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9012 		 * and just recompile everything that depends on it.)
9013 		 */
9014 #endif
9015 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
9016 		    iocp->ioc_flag & IOC_MODELS);
9017 		return;
9018 	}
9019 
9020 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9021 	qreply(q, mp);
9022 }
9023 
9024 static void
9025 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9026 {
9027 	mblk_t 		*data_mp;
9028 	struct dstinforeq	*dir;
9029 	uint8_t		*end, *cur;
9030 	in6_addr_t	*daddr, *saddr;
9031 	ipaddr_t	v4daddr;
9032 	ire_t		*ire;
9033 	char		*slabel, *dlabel;
9034 	boolean_t	isipv4;
9035 	int		match_ire;
9036 	ill_t		*dst_ill;
9037 	ipif_t		*src_ipif, *ire_ipif;
9038 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9039 	zoneid_t	zoneid;
9040 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
9041 
9042 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9043 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9044 
9045 	/*
9046 	 * This ioctl is I_STR only, and must have a
9047 	 * data mblk following the M_IOCTL mblk.
9048 	 */
9049 	data_mp = mp->b_cont;
9050 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9051 		miocnak(q, mp, 0, EINVAL);
9052 		return;
9053 	}
9054 
9055 	if (MBLKL(data_mp) < iocp->ioc_count) {
9056 		mblk_t *new_data_mp;
9057 
9058 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9059 			miocnak(q, mp, 0, ENOMEM);
9060 			return;
9061 		}
9062 		freemsg(data_mp);
9063 		data_mp = new_data_mp;
9064 		mp->b_cont = data_mp;
9065 	}
9066 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9067 
9068 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9069 	    end - cur >= sizeof (struct dstinforeq);
9070 	    cur += sizeof (struct dstinforeq)) {
9071 		dir = (struct dstinforeq *)cur;
9072 		daddr = &dir->dir_daddr;
9073 		saddr = &dir->dir_saddr;
9074 
9075 		/*
9076 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9077 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9078 		 * and ipif_select_source[_v6]() do not.
9079 		 */
9080 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9081 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9082 
9083 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9084 		if (isipv4) {
9085 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9086 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9087 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9088 		} else {
9089 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9090 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9091 		}
9092 		if (ire == NULL) {
9093 			dir->dir_dreachable = 0;
9094 
9095 			/* move on to next dst addr */
9096 			continue;
9097 		}
9098 		dir->dir_dreachable = 1;
9099 
9100 		ire_ipif = ire->ire_ipif;
9101 		if (ire_ipif == NULL)
9102 			goto next_dst;
9103 
9104 		/*
9105 		 * We expect to get back an interface ire or a
9106 		 * gateway ire cache entry.  For both types, the
9107 		 * output interface is ire_ipif->ipif_ill.
9108 		 */
9109 		dst_ill = ire_ipif->ipif_ill;
9110 		dir->dir_dmactype = dst_ill->ill_mactype;
9111 
9112 		if (isipv4) {
9113 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9114 		} else {
9115 			src_ipif = ipif_select_source_v6(dst_ill,
9116 			    daddr, B_FALSE, IPV6_PREFER_SRC_DEFAULT, zoneid);
9117 		}
9118 		if (src_ipif == NULL)
9119 			goto next_dst;
9120 
9121 		*saddr = src_ipif->ipif_v6lcl_addr;
9122 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9123 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9124 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9125 		dir->dir_sdeprecated =
9126 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9127 		ipif_refrele(src_ipif);
9128 next_dst:
9129 		ire_refrele(ire);
9130 	}
9131 	miocack(q, mp, iocp->ioc_count, 0);
9132 }
9133 
9134 /*
9135  * Check if this is an address assigned to this machine.
9136  * Skips interfaces that are down by using ire checks.
9137  * Translates mapped addresses to v4 addresses and then
9138  * treats them as such, returning true if the v4 address
9139  * associated with this mapped address is configured.
9140  * Note: Applications will have to be careful what they do
9141  * with the response; use of mapped addresses limits
9142  * what can be done with the socket, especially with
9143  * respect to socket options and ioctls - neither IPv4
9144  * options nor IPv6 sticky options/ancillary data options
9145  * may be used.
9146  */
9147 /* ARGSUSED */
9148 int
9149 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9150     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9151 {
9152 	struct sioc_addrreq *sia;
9153 	sin_t *sin;
9154 	ire_t *ire;
9155 	mblk_t *mp1;
9156 	zoneid_t zoneid;
9157 	ip_stack_t	*ipst;
9158 
9159 	ip1dbg(("ip_sioctl_tmyaddr"));
9160 
9161 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9162 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9163 	ipst = CONNQ_TO_IPST(q);
9164 
9165 	/* Existence verified in ip_wput_nondata */
9166 	mp1 = mp->b_cont->b_cont;
9167 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9168 	sin = (sin_t *)&sia->sa_addr;
9169 	switch (sin->sin_family) {
9170 	case AF_INET6: {
9171 		sin6_t *sin6 = (sin6_t *)sin;
9172 
9173 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9174 			ipaddr_t v4_addr;
9175 
9176 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9177 			    v4_addr);
9178 			ire = ire_ctable_lookup(v4_addr, 0,
9179 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9180 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9181 		} else {
9182 			in6_addr_t v6addr;
9183 
9184 			v6addr = sin6->sin6_addr;
9185 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9186 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9187 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9188 		}
9189 		break;
9190 	}
9191 	case AF_INET: {
9192 		ipaddr_t v4addr;
9193 
9194 		v4addr = sin->sin_addr.s_addr;
9195 		ire = ire_ctable_lookup(v4addr, 0,
9196 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9197 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9198 		break;
9199 	}
9200 	default:
9201 		return (EAFNOSUPPORT);
9202 	}
9203 	if (ire != NULL) {
9204 		sia->sa_res = 1;
9205 		ire_refrele(ire);
9206 	} else {
9207 		sia->sa_res = 0;
9208 	}
9209 	return (0);
9210 }
9211 
9212 /*
9213  * Check if this is an address assigned on-link i.e. neighbor,
9214  * and makes sure it's reachable from the current zone.
9215  * Returns true for my addresses as well.
9216  * Translates mapped addresses to v4 addresses and then
9217  * treats them as such, returning true if the v4 address
9218  * associated with this mapped address is configured.
9219  * Note: Applications will have to be careful what they do
9220  * with the response; use of mapped addresses limits
9221  * what can be done with the socket, especially with
9222  * respect to socket options and ioctls - neither IPv4
9223  * options nor IPv6 sticky options/ancillary data options
9224  * may be used.
9225  */
9226 /* ARGSUSED */
9227 int
9228 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9229     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9230 {
9231 	struct sioc_addrreq *sia;
9232 	sin_t *sin;
9233 	mblk_t	*mp1;
9234 	ire_t *ire = NULL;
9235 	zoneid_t zoneid;
9236 	ip_stack_t	*ipst;
9237 
9238 	ip1dbg(("ip_sioctl_tonlink"));
9239 
9240 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9241 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9242 	ipst = CONNQ_TO_IPST(q);
9243 
9244 	/* Existence verified in ip_wput_nondata */
9245 	mp1 = mp->b_cont->b_cont;
9246 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9247 	sin = (sin_t *)&sia->sa_addr;
9248 
9249 	/*
9250 	 * Match addresses with a zero gateway field to avoid
9251 	 * routes going through a router.
9252 	 * Exclude broadcast and multicast addresses.
9253 	 */
9254 	switch (sin->sin_family) {
9255 	case AF_INET6: {
9256 		sin6_t *sin6 = (sin6_t *)sin;
9257 
9258 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9259 			ipaddr_t v4_addr;
9260 
9261 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9262 			    v4_addr);
9263 			if (!CLASSD(v4_addr)) {
9264 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9265 				    NULL, NULL, zoneid, NULL,
9266 				    MATCH_IRE_GW, ipst);
9267 			}
9268 		} else {
9269 			in6_addr_t v6addr;
9270 			in6_addr_t v6gw;
9271 
9272 			v6addr = sin6->sin6_addr;
9273 			v6gw = ipv6_all_zeros;
9274 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9275 				ire = ire_route_lookup_v6(&v6addr, 0,
9276 				    &v6gw, 0, NULL, NULL, zoneid,
9277 				    NULL, MATCH_IRE_GW, ipst);
9278 			}
9279 		}
9280 		break;
9281 	}
9282 	case AF_INET: {
9283 		ipaddr_t v4addr;
9284 
9285 		v4addr = sin->sin_addr.s_addr;
9286 		if (!CLASSD(v4addr)) {
9287 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9288 			    NULL, NULL, zoneid, NULL,
9289 			    MATCH_IRE_GW, ipst);
9290 		}
9291 		break;
9292 	}
9293 	default:
9294 		return (EAFNOSUPPORT);
9295 	}
9296 	sia->sa_res = 0;
9297 	if (ire != NULL) {
9298 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9299 		    IRE_LOCAL|IRE_LOOPBACK)) {
9300 			sia->sa_res = 1;
9301 		}
9302 		ire_refrele(ire);
9303 	}
9304 	return (0);
9305 }
9306 
9307 /*
9308  * TBD: implement when kernel maintaines a list of site prefixes.
9309  */
9310 /* ARGSUSED */
9311 int
9312 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9313     ip_ioctl_cmd_t *ipip, void *ifreq)
9314 {
9315 	return (ENXIO);
9316 }
9317 
9318 /* ARGSUSED */
9319 int
9320 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9321     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9322 {
9323 	ill_t		*ill;
9324 	mblk_t		*mp1;
9325 	conn_t		*connp;
9326 	boolean_t	success;
9327 
9328 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9329 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9330 	/* ioctl comes down on an conn */
9331 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9332 	connp = Q_TO_CONN(q);
9333 
9334 	mp->b_datap->db_type = M_IOCTL;
9335 
9336 	/*
9337 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9338 	 * The original mp contains contaminated b_next values due to 'mi',
9339 	 * which is needed to do the mi_copy_done. Unfortunately if we
9340 	 * send down the original mblk itself and if we are popped due to an
9341 	 * an unplumb before the response comes back from tunnel,
9342 	 * the streamhead (which does a freemsg) will see this contaminated
9343 	 * message and the assertion in freemsg about non-null b_next/b_prev
9344 	 * will panic a DEBUG kernel.
9345 	 */
9346 	mp1 = copymsg(mp);
9347 	if (mp1 == NULL)
9348 		return (ENOMEM);
9349 
9350 	ill = ipif->ipif_ill;
9351 	mutex_enter(&connp->conn_lock);
9352 	mutex_enter(&ill->ill_lock);
9353 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9354 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9355 		    mp, 0);
9356 	} else {
9357 		success = ill_pending_mp_add(ill, connp, mp);
9358 	}
9359 	mutex_exit(&ill->ill_lock);
9360 	mutex_exit(&connp->conn_lock);
9361 
9362 	if (success) {
9363 		ip1dbg(("sending down tunparam request "));
9364 		putnext(ill->ill_wq, mp1);
9365 		return (EINPROGRESS);
9366 	} else {
9367 		/* The conn has started closing */
9368 		freemsg(mp1);
9369 		return (EINTR);
9370 	}
9371 }
9372 
9373 /*
9374  * ARP IOCTLs.
9375  * How does IP get in the business of fronting ARP configuration/queries?
9376  * Well it's like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9377  * are by tradition passed in through a datagram socket.  That lands in IP.
9378  * As it happens, this is just as well since the interface is quite crude in
9379  * that it passes in no information about protocol or hardware types, or
9380  * interface association.  After making the protocol assumption, IP is in
9381  * the position to look up the name of the ILL, which ARP will need, and
9382  * format a request that can be handled by ARP.  The request is passed up
9383  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9384  * back a response.  ARP supports its own set of more general IOCTLs, in
9385  * case anyone is interested.
9386  */
9387 /* ARGSUSED */
9388 int
9389 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9390     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9391 {
9392 	mblk_t *mp1;
9393 	mblk_t *mp2;
9394 	mblk_t *pending_mp;
9395 	ipaddr_t ipaddr;
9396 	area_t *area;
9397 	struct iocblk *iocp;
9398 	conn_t *connp;
9399 	struct arpreq *ar;
9400 	struct xarpreq *xar;
9401 	int flags, alength;
9402 	uchar_t *lladdr;
9403 	ire_t *ire;
9404 	ip_stack_t *ipst;
9405 	ill_t *ill = ipif->ipif_ill;
9406 	ill_t *proxy_ill = NULL;
9407 	ipmp_arpent_t *entp = NULL;
9408 	boolean_t if_arp_ioctl = B_FALSE;
9409 	boolean_t proxyarp = B_FALSE;
9410 
9411 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9412 	connp = Q_TO_CONN(q);
9413 	ipst = connp->conn_netstack->netstack_ip;
9414 
9415 	if (ipip->ipi_cmd_type == XARP_CMD) {
9416 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9417 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9418 		ar = NULL;
9419 
9420 		flags = xar->xarp_flags;
9421 		lladdr = (uchar_t *)LLADDR(&xar->xarp_ha);
9422 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
9423 		/*
9424 		 * Validate against user's link layer address length
9425 		 * input and name and addr length limits.
9426 		 */
9427 		alength = ill->ill_phys_addr_length;
9428 		if (ipip->ipi_cmd == SIOCSXARP) {
9429 			if (alength != xar->xarp_ha.sdl_alen ||
9430 			    (alength + xar->xarp_ha.sdl_nlen >
9431 			    sizeof (xar->xarp_ha.sdl_data)))
9432 				return (EINVAL);
9433 		}
9434 	} else {
9435 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9436 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9437 		xar = NULL;
9438 
9439 		flags = ar->arp_flags;
9440 		lladdr = (uchar_t *)ar->arp_ha.sa_data;
9441 		/*
9442 		 * Theoretically, the sa_family could tell us what link
9443 		 * layer type this operation is trying to deal with. By
9444 		 * common usage AF_UNSPEC means ethernet. We'll assume
9445 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9446 		 * for now. Our new SIOC*XARP ioctls can be used more
9447 		 * generally.
9448 		 *
9449 		 * If the underlying media happens to have a non 6 byte
9450 		 * address, arp module will fail set/get, but the del
9451 		 * operation will succeed.
9452 		 */
9453 		alength = 6;
9454 		if ((ipip->ipi_cmd != SIOCDARP) &&
9455 		    (alength != ill->ill_phys_addr_length)) {
9456 			return (EINVAL);
9457 		}
9458 	}
9459 
9460 	ipaddr = sin->sin_addr.s_addr;
9461 
9462 	/*
9463 	 * IPMP ARP special handling:
9464 	 *
9465 	 * 1. Since ARP mappings must appear consistent across the group,
9466 	 *    prohibit changing ARP mappings on the underlying interfaces.
9467 	 *
9468 	 * 2. Since ARP mappings for IPMP data addresses are maintained by
9469 	 *    IP itself, prohibit changing them.
9470 	 *
9471 	 * 3. For proxy ARP, use a functioning hardware address in the group,
9472 	 *    provided one exists.  If one doesn't, just add the entry as-is;
9473 	 *    ipmp_illgrp_refresh_arpent() will refresh it if things change.
9474 	 */
9475 	if (IS_UNDER_IPMP(ill)) {
9476 		if (ipip->ipi_cmd != SIOCGARP && ipip->ipi_cmd != SIOCGXARP)
9477 			return (EPERM);
9478 	}
9479 	if (IS_IPMP(ill)) {
9480 		ipmp_illgrp_t *illg = ill->ill_grp;
9481 
9482 		switch (ipip->ipi_cmd) {
9483 		case SIOCSARP:
9484 		case SIOCSXARP:
9485 			proxy_ill = ipmp_illgrp_find_ill(illg, lladdr, alength);
9486 			if (proxy_ill != NULL) {
9487 				proxyarp = B_TRUE;
9488 				if (!ipmp_ill_is_active(proxy_ill))
9489 					proxy_ill = ipmp_illgrp_next_ill(illg);
9490 				if (proxy_ill != NULL)
9491 					lladdr = proxy_ill->ill_phys_addr;
9492 			}
9493 			/* FALLTHRU */
9494 		case SIOCDARP:
9495 		case SIOCDXARP:
9496 			ire = ire_ctable_lookup(ipaddr, 0, IRE_LOCAL, NULL,
9497 			    ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
9498 			if (ire != NULL) {
9499 				ire_refrele(ire);
9500 				return (EPERM);
9501 			}
9502 		}
9503 	}
9504 
9505 	/*
9506 	 * We are going to pass up to ARP a packet chain that looks
9507 	 * like:
9508 	 *
9509 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9510 	 *
9511 	 * Get a copy of the original IOCTL mblk to head the chain,
9512 	 * to be sent up (in mp1). Also get another copy to store
9513 	 * in the ill_pending_mp list, for matching the response
9514 	 * when it comes back from ARP.
9515 	 */
9516 	mp1 = copyb(mp);
9517 	pending_mp = copymsg(mp);
9518 	if (mp1 == NULL || pending_mp == NULL) {
9519 		if (mp1 != NULL)
9520 			freeb(mp1);
9521 		if (pending_mp != NULL)
9522 			inet_freemsg(pending_mp);
9523 		return (ENOMEM);
9524 	}
9525 
9526 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9527 	    (caddr_t)&ipaddr);
9528 	if (mp2 == NULL) {
9529 		freeb(mp1);
9530 		inet_freemsg(pending_mp);
9531 		return (ENOMEM);
9532 	}
9533 	/* Put together the chain. */
9534 	mp1->b_cont = mp2;
9535 	mp1->b_datap->db_type = M_IOCTL;
9536 	mp2->b_cont = mp;
9537 	mp2->b_datap->db_type = M_DATA;
9538 
9539 	iocp = (struct iocblk *)mp1->b_rptr;
9540 
9541 	/*
9542 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9543 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9544 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9545 	 * ioc_count field; set ioc_count to be correct.
9546 	 */
9547 	iocp->ioc_count = MBLKL(mp1->b_cont);
9548 
9549 	/*
9550 	 * Set the proper command in the ARP message.
9551 	 * Convert the SIOC{G|S|D}ARP calls into our
9552 	 * AR_ENTRY_xxx calls.
9553 	 */
9554 	area = (area_t *)mp2->b_rptr;
9555 	switch (iocp->ioc_cmd) {
9556 	case SIOCDARP:
9557 	case SIOCDXARP:
9558 		/*
9559 		 * We defer deleting the corresponding IRE until
9560 		 * we return from arp.
9561 		 */
9562 		area->area_cmd = AR_ENTRY_DELETE;
9563 		area->area_proto_mask_offset = 0;
9564 		break;
9565 	case SIOCGARP:
9566 	case SIOCGXARP:
9567 		area->area_cmd = AR_ENTRY_SQUERY;
9568 		area->area_proto_mask_offset = 0;
9569 		break;
9570 	case SIOCSARP:
9571 	case SIOCSXARP:
9572 		/*
9573 		 * Delete the corresponding ire to make sure IP will
9574 		 * pick up any change from arp.
9575 		 */
9576 		if (!if_arp_ioctl) {
9577 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9578 		} else {
9579 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9580 			if (ipif != NULL) {
9581 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9582 				    ipst);
9583 				ipif_refrele(ipif);
9584 			}
9585 		}
9586 		break;
9587 	}
9588 	iocp->ioc_cmd = area->area_cmd;
9589 
9590 	/*
9591 	 * Fill in the rest of the ARP operation fields.
9592 	 */
9593 	area->area_hw_addr_length = alength;
9594 	bcopy(lladdr, (char *)area + area->area_hw_addr_offset, alength);
9595 
9596 	/* Translate the flags. */
9597 	if (flags & ATF_PERM)
9598 		area->area_flags |= ACE_F_PERMANENT;
9599 	if (flags & ATF_PUBL)
9600 		area->area_flags |= ACE_F_PUBLISH;
9601 	if (flags & ATF_AUTHORITY)
9602 		area->area_flags |= ACE_F_AUTHORITY;
9603 
9604 	/*
9605 	 * If this is a permanent AR_ENTRY_ADD on the IPMP interface, track it
9606 	 * so that IP can update ARP as the active ills in the group change.
9607 	 */
9608 	if (IS_IPMP(ill) && area->area_cmd == AR_ENTRY_ADD &&
9609 	    (area->area_flags & ACE_F_PERMANENT)) {
9610 		entp = ipmp_illgrp_create_arpent(ill->ill_grp, mp2, proxyarp);
9611 
9612 		/*
9613 		 * The second part of the conditional below handles a corner
9614 		 * case: if this is proxy ARP and the IPMP group has no active
9615 		 * interfaces, we can't send the request to ARP now since it
9616 		 * won't be able to build an ACE.  So we return success and
9617 		 * notify ARP about the proxy ARP entry once an interface
9618 		 * becomes active.
9619 		 */
9620 		if (entp == NULL || (proxyarp && proxy_ill == NULL)) {
9621 			mp2->b_cont = NULL;
9622 			inet_freemsg(mp1);
9623 			inet_freemsg(pending_mp);
9624 			return (entp == NULL ? ENOMEM : 0);
9625 		}
9626 	}
9627 
9628 	/*
9629 	 * Before sending 'mp' to ARP, we have to clear the b_next
9630 	 * and b_prev. Otherwise if STREAMS encounters such a message
9631 	 * in freemsg(), (because ARP can close any time) it can cause
9632 	 * a panic. But mi code needs the b_next and b_prev values of
9633 	 * mp->b_cont, to complete the ioctl. So we store it here
9634 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9635 	 * when the response comes down from ARP.
9636 	 */
9637 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9638 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9639 	mp->b_cont->b_next = NULL;
9640 	mp->b_cont->b_prev = NULL;
9641 
9642 	mutex_enter(&connp->conn_lock);
9643 	mutex_enter(&ill->ill_lock);
9644 	/* conn has not yet started closing, hence this can't fail */
9645 	if (ipip->ipi_flags & IPI_WR) {
9646 		VERIFY(ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9647 		    pending_mp, 0) != 0);
9648 	} else {
9649 		VERIFY(ill_pending_mp_add(ill, connp, pending_mp) != 0);
9650 	}
9651 	mutex_exit(&ill->ill_lock);
9652 	mutex_exit(&connp->conn_lock);
9653 
9654 	/*
9655 	 * Up to ARP it goes.  The response will come back in ip_wput() as an
9656 	 * M_IOCACK, and will be handed to ip_sioctl_iocack() for completion.
9657 	 */
9658 	putnext(ill->ill_rq, mp1);
9659 
9660 	/*
9661 	 * If we created an IPMP ARP entry, mark that we've notified ARP.
9662 	 */
9663 	if (entp != NULL)
9664 		ipmp_illgrp_mark_arpent(ill->ill_grp, entp);
9665 
9666 	return (EINPROGRESS);
9667 }
9668 
9669 /*
9670  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
9671  * the associated sin and refhold and return the associated ipif via `ci'.
9672  */
9673 int
9674 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
9675     cmd_info_t *ci, ipsq_func_t func)
9676 {
9677 	mblk_t	*mp1;
9678 	int	err;
9679 	sin_t	*sin;
9680 	conn_t	*connp;
9681 	ipif_t	*ipif;
9682 	ire_t	*ire = NULL;
9683 	ill_t	*ill = NULL;
9684 	boolean_t exists;
9685 	ip_stack_t *ipst;
9686 	struct arpreq *ar;
9687 	struct xarpreq *xar;
9688 	struct sockaddr_dl *sdl;
9689 
9690 	/* ioctl comes down on a conn */
9691 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9692 	connp = Q_TO_CONN(q);
9693 	if (connp->conn_af_isv6)
9694 		return (ENXIO);
9695 
9696 	ipst = connp->conn_netstack->netstack_ip;
9697 
9698 	/* Verified in ip_wput_nondata */
9699 	mp1 = mp->b_cont->b_cont;
9700 
9701 	if (ipip->ipi_cmd_type == XARP_CMD) {
9702 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
9703 		xar = (struct xarpreq *)mp1->b_rptr;
9704 		sin = (sin_t *)&xar->xarp_pa;
9705 		sdl = &xar->xarp_ha;
9706 
9707 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
9708 			return (ENXIO);
9709 		if (sdl->sdl_nlen >= LIFNAMSIZ)
9710 			return (EINVAL);
9711 	} else {
9712 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
9713 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
9714 		ar = (struct arpreq *)mp1->b_rptr;
9715 		sin = (sin_t *)&ar->arp_pa;
9716 	}
9717 
9718 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
9719 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
9720 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, CONNP_TO_WQ(connp),
9721 		    mp, func, &err, ipst);
9722 		if (ipif == NULL)
9723 			return (err);
9724 		if (ipif->ipif_id != 0) {
9725 			ipif_refrele(ipif);
9726 			return (ENXIO);
9727 		}
9728 	} else {
9729 		/*
9730 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with an sdl_nlen
9731 		 * of 0: use the IP address to find the ipif.  If the IP
9732 		 * address is an IPMP test address, ire_ftable_lookup() will
9733 		 * find the wrong ill, so we first do an ipif_lookup_addr().
9734 		 */
9735 		ipif = ipif_lookup_addr(sin->sin_addr.s_addr, NULL, ALL_ZONES,
9736 		    CONNP_TO_WQ(connp), mp, func, &err, ipst);
9737 		if (ipif == NULL) {
9738 			ire = ire_ftable_lookup(sin->sin_addr.s_addr, 0, 0,
9739 			    IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0, NULL,
9740 			    MATCH_IRE_TYPE, ipst);
9741 			if (ire == NULL || ((ill = ire_to_ill(ire)) == NULL)) {
9742 				if (ire != NULL)
9743 					ire_refrele(ire);
9744 				return (ENXIO);
9745 			}
9746 			ipif = ill->ill_ipif;
9747 			ipif_refhold(ipif);
9748 			ire_refrele(ire);
9749 		}
9750 	}
9751 
9752 	if (ipif->ipif_net_type != IRE_IF_RESOLVER) {
9753 		ipif_refrele(ipif);
9754 		return (ENXIO);
9755 	}
9756 
9757 	ci->ci_sin = sin;
9758 	ci->ci_ipif = ipif;
9759 	return (0);
9760 }
9761 
9762 /*
9763  * Link or unlink the illgrp on IPMP meta-interface `ill' depending on the
9764  * value of `ioccmd'.  While an illgrp is linked to an ipmp_grp_t, it is
9765  * accessible from that ipmp_grp_t, which means SIOCSLIFGROUPNAME can look it
9766  * up and thus an ill can join that illgrp.
9767  *
9768  * We use I_PLINK/I_PUNLINK to do the link/unlink operations rather than
9769  * open()/close() primarily because close() is not allowed to fail or block
9770  * forever.  On the other hand, I_PUNLINK *can* fail, and there's no reason
9771  * why anyone should ever need to I_PUNLINK an in-use IPMP stream.  To ensure
9772  * symmetric behavior (e.g., doing an I_PLINK after and I_PUNLINK undoes the
9773  * I_PUNLINK) we defer linking to I_PLINK.  Separately, we also fail attempts
9774  * to I_LINK since I_UNLINK is optional and we'd end up in an inconsistent
9775  * state if I_UNLINK didn't occur.
9776  *
9777  * Note that for each plumb/unplumb operation, we may end up here more than
9778  * once because of the way ifconfig works.  However, it's OK to link the same
9779  * illgrp more than once, or unlink an illgrp that's already unlinked.
9780  */
9781 static int
9782 ip_sioctl_plink_ipmp(ill_t *ill, int ioccmd)
9783 {
9784 	int err;
9785 	ip_stack_t *ipst = ill->ill_ipst;
9786 
9787 	ASSERT(IS_IPMP(ill));
9788 	ASSERT(IAM_WRITER_ILL(ill));
9789 
9790 	switch (ioccmd) {
9791 	case I_LINK:
9792 		return (ENOTSUP);
9793 
9794 	case I_PLINK:
9795 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
9796 		ipmp_illgrp_link_grp(ill->ill_grp, ill->ill_phyint->phyint_grp);
9797 		rw_exit(&ipst->ips_ipmp_lock);
9798 		break;
9799 
9800 	case I_PUNLINK:
9801 		/*
9802 		 * Require all UP ipifs be brought down prior to unlinking the
9803 		 * illgrp so any associated IREs (and other state) is torched.
9804 		 */
9805 		if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0)
9806 			return (EBUSY);
9807 
9808 		/*
9809 		 * NOTE: We hold ipmp_lock across the unlink to prevent a race
9810 		 * with an SIOCSLIFGROUPNAME request from an ill trying to
9811 		 * join this group.  Specifically: ills trying to join grab
9812 		 * ipmp_lock and bump a "pending join" counter checked by
9813 		 * ipmp_illgrp_unlink_grp().  During the unlink no new pending
9814 		 * joins can occur (since we have ipmp_lock).  Once we drop
9815 		 * ipmp_lock, subsequent SIOCSLIFGROUPNAME requests will not
9816 		 * find the illgrp (since we unlinked it) and will return
9817 		 * EAFNOSUPPORT.  This will then take them back through the
9818 		 * IPMP meta-interface plumbing logic in ifconfig, and thus
9819 		 * back through I_PLINK above.
9820 		 */
9821 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
9822 		err = ipmp_illgrp_unlink_grp(ill->ill_grp);
9823 		rw_exit(&ipst->ips_ipmp_lock);
9824 		return (err);
9825 	default:
9826 		break;
9827 	}
9828 	return (0);
9829 }
9830 
9831 /*
9832  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9833  * atomically set/clear the muxids. Also complete the ioctl by acking or
9834  * naking it.  Note that the code is structured such that the link type,
9835  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9836  * its clones use the persistent link, while pppd(1M) and perhaps many
9837  * other daemons may use non-persistent link.  When combined with some
9838  * ill_t states, linking and unlinking lower streams may be used as
9839  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9840  */
9841 /* ARGSUSED */
9842 void
9843 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9844 {
9845 	mblk_t		*mp1, *mp2;
9846 	struct linkblk	*li;
9847 	struct ipmx_s	*ipmxp;
9848 	ill_t		*ill;
9849 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
9850 	int		err = 0;
9851 	boolean_t	entered_ipsq = B_FALSE;
9852 	boolean_t	islink;
9853 	ip_stack_t	*ipst;
9854 
9855 	if (CONN_Q(q))
9856 		ipst = CONNQ_TO_IPST(q);
9857 	else
9858 		ipst = ILLQ_TO_IPST(q);
9859 
9860 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
9861 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
9862 
9863 	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9864 
9865 	mp1 = mp->b_cont;	/* This is the linkblk info */
9866 	li = (struct linkblk *)mp1->b_rptr;
9867 
9868 	/*
9869 	 * ARP has added this special mblk, and the utility is asking us
9870 	 * to perform consistency checks, and also atomically set the
9871 	 * muxid. Ifconfig is an example.  It achieves this by using
9872 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9873 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9874 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9875 	 * and other comments in this routine for more details.
9876 	 */
9877 	mp2 = mp1->b_cont;	/* This is added by ARP */
9878 
9879 	/*
9880 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9881 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9882 	 * get the special mblk above.  For backward compatibility, we
9883 	 * request ip_sioctl_plink_ipmod() to skip the consistency checks.
9884 	 * The utility will use SIOCSLIFMUXID to store the muxids.  This is
9885 	 * not atomic, and can leave the streams unplumbable if the utility
9886 	 * is interrupted before it does the SIOCSLIFMUXID.
9887 	 */
9888 	if (mp2 == NULL) {
9889 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_FALSE);
9890 		if (err == EINPROGRESS)
9891 			return;
9892 		goto done;
9893 	}
9894 
9895 	/*
9896 	 * This is an I_{P}LINK sent down by ifconfig through the ARP module;
9897 	 * ARP has appended this last mblk to tell us whether the lower stream
9898 	 * is an arp-dev stream or an IP module stream.
9899 	 */
9900 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9901 	if (ipmxp->ipmx_arpdev_stream) {
9902 		/*
9903 		 * The lower stream is the arp-dev stream.
9904 		 */
9905 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9906 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
9907 		if (ill == NULL) {
9908 			if (err == EINPROGRESS)
9909 				return;
9910 			err = EINVAL;
9911 			goto done;
9912 		}
9913 
9914 		if (ipsq == NULL) {
9915 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9916 			    NEW_OP, B_FALSE);
9917 			if (ipsq == NULL) {
9918 				ill_refrele(ill);
9919 				return;
9920 			}
9921 			entered_ipsq = B_TRUE;
9922 		}
9923 		ASSERT(IAM_WRITER_ILL(ill));
9924 		ill_refrele(ill);
9925 
9926 		/*
9927 		 * To ensure consistency between IP and ARP, the following
9928 		 * LIFO scheme is used in plink/punlink. (IP first, ARP last).
9929 		 * This is because the muxid's are stored in the IP stream on
9930 		 * the ill.
9931 		 *
9932 		 * I_{P}LINK: ifconfig plinks the IP stream before plinking
9933 		 * the ARP stream. On an arp-dev stream, IP checks that it is
9934 		 * not yet plinked, and it also checks that the corresponding
9935 		 * IP stream is already plinked.
9936 		 *
9937 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream before
9938 		 * punlinking the IP stream. IP does not allow punlink of the
9939 		 * IP stream unless the arp stream has been punlinked.
9940 		 */
9941 		if ((islink &&
9942 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9943 		    (!islink && ill->ill_arp_muxid != li->l_index)) {
9944 			err = EINVAL;
9945 			goto done;
9946 		}
9947 
9948 		if (IS_IPMP(ill) &&
9949 		    (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
9950 			goto done;
9951 
9952 		ill->ill_arp_muxid = islink ? li->l_index : 0;
9953 	} else {
9954 		/*
9955 		 * The lower stream is probably an IP module stream.  Do
9956 		 * consistency checking.
9957 		 */
9958 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_TRUE);
9959 		if (err == EINPROGRESS)
9960 			return;
9961 	}
9962 done:
9963 	if (err == 0)
9964 		miocack(q, mp, 0, 0);
9965 	else
9966 		miocnak(q, mp, 0, err);
9967 
9968 	/* Conn was refheld in ip_sioctl_copyin_setup */
9969 	if (CONN_Q(q))
9970 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9971 	if (entered_ipsq)
9972 		ipsq_exit(ipsq);
9973 }
9974 
9975 /*
9976  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
9977  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
9978  * module stream).  If `doconsist' is set, then do the extended consistency
9979  * checks requested by ifconfig(1M) and (atomically) set ill_ip_muxid here.
9980  * Returns zero on success, EINPROGRESS if the operation is still pending, or
9981  * an error code on failure.
9982  */
9983 static int
9984 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
9985     struct linkblk *li, boolean_t doconsist)
9986 {
9987 	int		err = 0;
9988 	ill_t  		*ill;
9989 	queue_t		*ipwq, *dwq;
9990 	const char	*name;
9991 	struct qinit	*qinfo;
9992 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9993 	boolean_t	entered_ipsq = B_FALSE;
9994 
9995 	/*
9996 	 * Walk the lower stream to verify it's the IP module stream.
9997 	 * The IP module is identified by its name, wput function,
9998 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
9999 	 * (li->l_qbot) will not vanish until this ioctl completes.
10000 	 */
10001 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
10002 		qinfo = ipwq->q_qinfo;
10003 		name = qinfo->qi_minfo->mi_idname;
10004 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
10005 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
10006 			break;
10007 		}
10008 	}
10009 
10010 	/*
10011 	 * If this isn't an IP module stream, bail.
10012 	 */
10013 	if (ipwq == NULL)
10014 		return (0);
10015 
10016 	ill = ipwq->q_ptr;
10017 	ASSERT(ill != NULL);
10018 
10019 	if (ipsq == NULL) {
10020 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
10021 		    NEW_OP, B_FALSE);
10022 		if (ipsq == NULL)
10023 			return (EINPROGRESS);
10024 		entered_ipsq = B_TRUE;
10025 	}
10026 	ASSERT(IAM_WRITER_ILL(ill));
10027 
10028 	if (doconsist) {
10029 		/*
10030 		 * Consistency checking requires that I_{P}LINK occurs
10031 		 * prior to setting ill_ip_muxid, and that I_{P}UNLINK
10032 		 * occurs prior to clearing ill_arp_muxid.
10033 		 */
10034 		if ((islink && ill->ill_ip_muxid != 0) ||
10035 		    (!islink && ill->ill_arp_muxid != 0)) {
10036 			err = EINVAL;
10037 			goto done;
10038 		}
10039 	}
10040 
10041 	if (IS_IPMP(ill) && (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
10042 		goto done;
10043 
10044 	/*
10045 	 * As part of I_{P}LINKing, stash the number of downstream modules and
10046 	 * the read queue of the module immediately below IP in the ill.
10047 	 * These are used during the capability negotiation below.
10048 	 */
10049 	ill->ill_lmod_rq = NULL;
10050 	ill->ill_lmod_cnt = 0;
10051 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
10052 		ill->ill_lmod_rq = RD(dwq);
10053 		for (; dwq != NULL; dwq = dwq->q_next)
10054 			ill->ill_lmod_cnt++;
10055 	}
10056 
10057 	if (doconsist)
10058 		ill->ill_ip_muxid = islink ? li->l_index : 0;
10059 
10060 	/*
10061 	 * Mark the ipsq busy until the capability operations initiated below
10062 	 * complete. The PLINK/UNLINK ioctl itself completes when our caller
10063 	 * returns, but the capability operation may complete asynchronously
10064 	 * much later.
10065 	 */
10066 	ipsq_current_start(ipsq, ill->ill_ipif, ioccmd);
10067 	/*
10068 	 * If there's at least one up ipif on this ill, then we're bound to
10069 	 * the underlying driver via DLPI.  In that case, renegotiate
10070 	 * capabilities to account for any possible change in modules
10071 	 * interposed between IP and the driver.
10072 	 */
10073 	if (ill->ill_ipif_up_count > 0) {
10074 		if (islink)
10075 			ill_capability_probe(ill);
10076 		else
10077 			ill_capability_reset(ill, B_FALSE);
10078 	}
10079 	ipsq_current_finish(ipsq);
10080 done:
10081 	if (entered_ipsq)
10082 		ipsq_exit(ipsq);
10083 
10084 	return (err);
10085 }
10086 
10087 /*
10088  * Search the ioctl command in the ioctl tables and return a pointer
10089  * to the ioctl command information. The ioctl command tables are
10090  * static and fully populated at compile time.
10091  */
10092 ip_ioctl_cmd_t *
10093 ip_sioctl_lookup(int ioc_cmd)
10094 {
10095 	int index;
10096 	ip_ioctl_cmd_t *ipip;
10097 	ip_ioctl_cmd_t *ipip_end;
10098 
10099 	if (ioc_cmd == IPI_DONTCARE)
10100 		return (NULL);
10101 
10102 	/*
10103 	 * Do a 2 step search. First search the indexed table
10104 	 * based on the least significant byte of the ioctl cmd.
10105 	 * If we don't find a match, then search the misc table
10106 	 * serially.
10107 	 */
10108 	index = ioc_cmd & 0xFF;
10109 	if (index < ip_ndx_ioctl_count) {
10110 		ipip = &ip_ndx_ioctl_table[index];
10111 		if (ipip->ipi_cmd == ioc_cmd) {
10112 			/* Found a match in the ndx table */
10113 			return (ipip);
10114 		}
10115 	}
10116 
10117 	/* Search the misc table */
10118 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10119 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10120 		if (ipip->ipi_cmd == ioc_cmd)
10121 			/* Found a match in the misc table */
10122 			return (ipip);
10123 	}
10124 
10125 	return (NULL);
10126 }
10127 
10128 /*
10129  * Wrapper function for resuming deferred ioctl processing
10130  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10131  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10132  */
10133 /* ARGSUSED */
10134 void
10135 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10136     void *dummy_arg)
10137 {
10138 	ip_sioctl_copyin_setup(q, mp);
10139 }
10140 
10141 /*
10142  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10143  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10144  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10145  * We establish here the size of the block to be copied in.  mi_copyin
10146  * arranges for this to happen, an processing continues in ip_wput with
10147  * an M_IOCDATA message.
10148  */
10149 void
10150 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10151 {
10152 	int	copyin_size;
10153 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10154 	ip_ioctl_cmd_t *ipip;
10155 	cred_t *cr;
10156 	ip_stack_t	*ipst;
10157 
10158 	if (CONN_Q(q))
10159 		ipst = CONNQ_TO_IPST(q);
10160 	else
10161 		ipst = ILLQ_TO_IPST(q);
10162 
10163 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10164 	if (ipip == NULL) {
10165 		/*
10166 		 * The ioctl is not one we understand or own.
10167 		 * Pass it along to be processed down stream,
10168 		 * if this is a module instance of IP, else nak
10169 		 * the ioctl.
10170 		 */
10171 		if (q->q_next == NULL) {
10172 			goto nak;
10173 		} else {
10174 			putnext(q, mp);
10175 			return;
10176 		}
10177 	}
10178 
10179 	/*
10180 	 * If this is deferred, then we will do all the checks when we
10181 	 * come back.
10182 	 */
10183 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10184 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10185 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10186 		return;
10187 	}
10188 
10189 	/*
10190 	 * Only allow a very small subset of IP ioctls on this stream if
10191 	 * IP is a module and not a driver. Allowing ioctls to be processed
10192 	 * in this case may cause assert failures or data corruption.
10193 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10194 	 * ioctls allowed on an IP module stream, after which this stream
10195 	 * normally becomes a multiplexor (at which time the stream head
10196 	 * will fail all ioctls).
10197 	 */
10198 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10199 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10200 			/*
10201 			 * Pass common Streams ioctls which the IP
10202 			 * module does not own or consume along to
10203 			 * be processed down stream.
10204 			 */
10205 			putnext(q, mp);
10206 			return;
10207 		} else {
10208 			goto nak;
10209 		}
10210 	}
10211 
10212 	/* Make sure we have ioctl data to process. */
10213 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10214 		goto nak;
10215 
10216 	/*
10217 	 * Prefer dblk credential over ioctl credential; some synthesized
10218 	 * ioctls have kcred set because there's no way to crhold()
10219 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10220 	 * the framework; the caller of ioctl needs to hold the reference
10221 	 * for the duration of the call).
10222 	 */
10223 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10224 
10225 	/* Make sure normal users don't send down privileged ioctls */
10226 	if ((ipip->ipi_flags & IPI_PRIV) &&
10227 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10228 		/* We checked the privilege earlier but log it here */
10229 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10230 		return;
10231 	}
10232 
10233 	/*
10234 	 * The ioctl command tables can only encode fixed length
10235 	 * ioctl data. If the length is variable, the table will
10236 	 * encode the length as zero. Such special cases are handled
10237 	 * below in the switch.
10238 	 */
10239 	if (ipip->ipi_copyin_size != 0) {
10240 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10241 		return;
10242 	}
10243 
10244 	switch (iocp->ioc_cmd) {
10245 	case O_SIOCGIFCONF:
10246 	case SIOCGIFCONF:
10247 		/*
10248 		 * This IOCTL is hilarious.  See comments in
10249 		 * ip_sioctl_get_ifconf for the story.
10250 		 */
10251 		if (iocp->ioc_count == TRANSPARENT)
10252 			copyin_size = SIZEOF_STRUCT(ifconf,
10253 			    iocp->ioc_flag);
10254 		else
10255 			copyin_size = iocp->ioc_count;
10256 		mi_copyin(q, mp, NULL, copyin_size);
10257 		return;
10258 
10259 	case O_SIOCGLIFCONF:
10260 	case SIOCGLIFCONF:
10261 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10262 		mi_copyin(q, mp, NULL, copyin_size);
10263 		return;
10264 
10265 	case SIOCGLIFSRCOF:
10266 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10267 		mi_copyin(q, mp, NULL, copyin_size);
10268 		return;
10269 	case SIOCGIP6ADDRPOLICY:
10270 		ip_sioctl_ip6addrpolicy(q, mp);
10271 		ip6_asp_table_refrele(ipst);
10272 		return;
10273 
10274 	case SIOCSIP6ADDRPOLICY:
10275 		ip_sioctl_ip6addrpolicy(q, mp);
10276 		return;
10277 
10278 	case SIOCGDSTINFO:
10279 		ip_sioctl_dstinfo(q, mp);
10280 		ip6_asp_table_refrele(ipst);
10281 		return;
10282 
10283 	case I_PLINK:
10284 	case I_PUNLINK:
10285 	case I_LINK:
10286 	case I_UNLINK:
10287 		/*
10288 		 * We treat non-persistent link similarly as the persistent
10289 		 * link case, in terms of plumbing/unplumbing, as well as
10290 		 * dynamic re-plumbing events indicator.  See comments
10291 		 * in ip_sioctl_plink() for more.
10292 		 *
10293 		 * Request can be enqueued in the 'ipsq' while waiting
10294 		 * to become exclusive. So bump up the conn ref.
10295 		 */
10296 		if (CONN_Q(q))
10297 			CONN_INC_REF(Q_TO_CONN(q));
10298 		ip_sioctl_plink(NULL, q, mp, NULL);
10299 		return;
10300 
10301 	case ND_GET:
10302 	case ND_SET:
10303 		/*
10304 		 * Use of the nd table requires holding the reader lock.
10305 		 * Modifying the nd table thru nd_load/nd_unload requires
10306 		 * the writer lock.
10307 		 */
10308 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10309 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10310 			rw_exit(&ipst->ips_ip_g_nd_lock);
10311 
10312 			if (iocp->ioc_error)
10313 				iocp->ioc_count = 0;
10314 			mp->b_datap->db_type = M_IOCACK;
10315 			qreply(q, mp);
10316 			return;
10317 		}
10318 		rw_exit(&ipst->ips_ip_g_nd_lock);
10319 		/*
10320 		 * We don't understand this subioctl of ND_GET / ND_SET.
10321 		 * Maybe intended for some driver / module below us
10322 		 */
10323 		if (q->q_next) {
10324 			putnext(q, mp);
10325 		} else {
10326 			iocp->ioc_error = ENOENT;
10327 			mp->b_datap->db_type = M_IOCNAK;
10328 			iocp->ioc_count = 0;
10329 			qreply(q, mp);
10330 		}
10331 		return;
10332 
10333 	case IP_IOCTL:
10334 		ip_wput_ioctl(q, mp);
10335 		return;
10336 	default:
10337 		cmn_err(CE_PANIC, "should not happen ");
10338 	}
10339 nak:
10340 	if (mp->b_cont != NULL) {
10341 		freemsg(mp->b_cont);
10342 		mp->b_cont = NULL;
10343 	}
10344 	iocp->ioc_error = EINVAL;
10345 	mp->b_datap->db_type = M_IOCNAK;
10346 	iocp->ioc_count = 0;
10347 	qreply(q, mp);
10348 }
10349 
10350 /* ip_wput hands off ARP IOCTL responses to us */
10351 /* ARGSUSED3 */
10352 void
10353 ip_sioctl_iocack(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
10354 {
10355 	struct arpreq *ar;
10356 	struct xarpreq *xar;
10357 	area_t	*area;
10358 	mblk_t	*area_mp;
10359 	struct iocblk *iocp;
10360 	mblk_t	*orig_ioc_mp, *tmp;
10361 	struct iocblk	*orig_iocp;
10362 	ill_t *ill;
10363 	conn_t *connp = NULL;
10364 	mblk_t *pending_mp;
10365 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10366 	int *flagsp;
10367 	char *storage = NULL;
10368 	sin_t *sin;
10369 	ipaddr_t addr;
10370 	int err;
10371 	ip_stack_t *ipst;
10372 
10373 	ASSERT(ipsq == NULL || IAM_WRITER_IPSQ(ipsq));
10374 	ill = q->q_ptr;
10375 	ASSERT(ill != NULL);
10376 	ipst = ill->ill_ipst;
10377 
10378 	/*
10379 	 * We should get back from ARP a packet chain that looks like:
10380 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10381 	 */
10382 	if (!(area_mp = mp->b_cont) ||
10383 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10384 	    !(orig_ioc_mp = area_mp->b_cont) ||
10385 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10386 		freemsg(mp);
10387 		return;
10388 	}
10389 
10390 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10391 
10392 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10393 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10394 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10395 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10396 		x_arp_ioctl = B_TRUE;
10397 		xar = (struct xarpreq *)tmp->b_rptr;
10398 		sin = (sin_t *)&xar->xarp_pa;
10399 		flagsp = &xar->xarp_flags;
10400 		storage = xar->xarp_ha.sdl_data;
10401 		if (xar->xarp_ha.sdl_nlen != 0)
10402 			ifx_arp_ioctl = B_TRUE;
10403 	} else {
10404 		ar = (struct arpreq *)tmp->b_rptr;
10405 		sin = (sin_t *)&ar->arp_pa;
10406 		flagsp = &ar->arp_flags;
10407 		storage = ar->arp_ha.sa_data;
10408 	}
10409 
10410 	iocp = (struct iocblk *)mp->b_rptr;
10411 
10412 	/*
10413 	 * Find the pending message; if we're exclusive, it'll be on our IPSQ.
10414 	 * Otherwise, we can find it from our ioc_id.
10415 	 */
10416 	if (ipsq != NULL)
10417 		pending_mp = ipsq_pending_mp_get(ipsq, &connp);
10418 	else
10419 		pending_mp = ill_pending_mp_get(ill, &connp, iocp->ioc_id);
10420 
10421 	if (pending_mp == NULL) {
10422 		ASSERT(connp == NULL);
10423 		inet_freemsg(mp);
10424 		return;
10425 	}
10426 	ASSERT(connp != NULL);
10427 	q = CONNP_TO_WQ(connp);
10428 
10429 	/* Uncouple the internally generated IOCTL from the original one */
10430 	area = (area_t *)area_mp->b_rptr;
10431 	area_mp->b_cont = NULL;
10432 
10433 	/*
10434 	 * Restore the b_next and b_prev used by mi code. This is needed
10435 	 * to complete the ioctl using mi* functions. We stored them in
10436 	 * the pending mp prior to sending the request to ARP.
10437 	 */
10438 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10439 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10440 	inet_freemsg(pending_mp);
10441 
10442 	/*
10443 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10444 	 * Catch the case where there is an IRE_CACHE by no entry in the
10445 	 * arp table.
10446 	 */
10447 	addr = sin->sin_addr.s_addr;
10448 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10449 		ire_t			*ire;
10450 		dl_unitdata_req_t	*dlup;
10451 		mblk_t			*llmp;
10452 		int			addr_len;
10453 		ill_t			*ipsqill = NULL;
10454 
10455 		if (ifx_arp_ioctl) {
10456 			/*
10457 			 * There's no need to lookup the ill, since
10458 			 * we've already done that when we started
10459 			 * processing the ioctl and sent the message
10460 			 * to ARP on that ill.  So use the ill that
10461 			 * is stored in q->q_ptr.
10462 			 */
10463 			ipsqill = ill;
10464 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10465 			    ipsqill->ill_ipif, ALL_ZONES,
10466 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10467 		} else {
10468 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10469 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10470 			if (ire != NULL)
10471 				ipsqill = ire_to_ill(ire);
10472 		}
10473 
10474 		if ((x_arp_ioctl) && (ipsqill != NULL))
10475 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10476 
10477 		if (ire != NULL) {
10478 			/*
10479 			 * Since the ire obtained from cachetable is used for
10480 			 * mac addr copying below, treat an incomplete ire as if
10481 			 * as if we never found it.
10482 			 */
10483 			if (ire->ire_nce != NULL &&
10484 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10485 				ire_refrele(ire);
10486 				ire = NULL;
10487 				ipsqill = NULL;
10488 				goto errack;
10489 			}
10490 			*flagsp = ATF_INUSE;
10491 			llmp = (ire->ire_nce != NULL ?
10492 			    ire->ire_nce->nce_res_mp : NULL);
10493 			if (llmp != NULL && ipsqill != NULL) {
10494 				uchar_t *macaddr;
10495 
10496 				addr_len = ipsqill->ill_phys_addr_length;
10497 				if (x_arp_ioctl && ((addr_len +
10498 				    ipsqill->ill_name_length) >
10499 				    sizeof (xar->xarp_ha.sdl_data))) {
10500 					ire_refrele(ire);
10501 					freemsg(mp);
10502 					ip_ioctl_finish(q, orig_ioc_mp,
10503 					    EINVAL, NO_COPYOUT, ipsq);
10504 					return;
10505 				}
10506 				*flagsp |= ATF_COM;
10507 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10508 				if (ipsqill->ill_sap_length < 0)
10509 					macaddr = llmp->b_rptr +
10510 					    dlup->dl_dest_addr_offset;
10511 				else
10512 					macaddr = llmp->b_rptr +
10513 					    dlup->dl_dest_addr_offset +
10514 					    ipsqill->ill_sap_length;
10515 				/*
10516 				 * For SIOCGARP, MAC address length
10517 				 * validation has already been done
10518 				 * before the ioctl was issued to ARP to
10519 				 * allow it to progress only on 6 byte
10520 				 * addressable (ethernet like) media. Thus
10521 				 * the mac address copying can not overwrite
10522 				 * the sa_data area below.
10523 				 */
10524 				bcopy(macaddr, storage, addr_len);
10525 			}
10526 			/* Ditch the internal IOCTL. */
10527 			freemsg(mp);
10528 			ire_refrele(ire);
10529 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, ipsq);
10530 			return;
10531 		}
10532 	}
10533 
10534 	/*
10535 	 * If this was a failed AR_ENTRY_ADD or a successful AR_ENTRY_DELETE
10536 	 * on the IPMP meta-interface, ensure any ARP entries added in
10537 	 * ip_sioctl_arp() are deleted.
10538 	 */
10539 	if (IS_IPMP(ill) &&
10540 	    ((iocp->ioc_error != 0 && iocp->ioc_cmd == AR_ENTRY_ADD) ||
10541 	    ((iocp->ioc_error == 0 && iocp->ioc_cmd == AR_ENTRY_DELETE)))) {
10542 		ipmp_illgrp_t *illg = ill->ill_grp;
10543 		ipmp_arpent_t *entp;
10544 
10545 		if ((entp = ipmp_illgrp_lookup_arpent(illg, &addr)) != NULL)
10546 			ipmp_illgrp_destroy_arpent(illg, entp);
10547 	}
10548 
10549 	/*
10550 	 * Delete the coresponding IRE_CACHE if any.
10551 	 * Reset the error if there was one (in case there was no entry
10552 	 * in arp.)
10553 	 */
10554 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10555 		ipif_t *ipintf = NULL;
10556 
10557 		if (ifx_arp_ioctl) {
10558 			/*
10559 			 * There's no need to lookup the ill, since
10560 			 * we've already done that when we started
10561 			 * processing the ioctl and sent the message
10562 			 * to ARP on that ill.  So use the ill that
10563 			 * is stored in q->q_ptr.
10564 			 */
10565 			ipintf = ill->ill_ipif;
10566 		}
10567 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10568 			/*
10569 			 * The address in "addr" may be an entry for a
10570 			 * router. If that's true, then any off-net
10571 			 * IRE_CACHE entries that go through the router
10572 			 * with address "addr" must be clobbered. Use
10573 			 * ire_walk to achieve this goal.
10574 			 */
10575 			if (ifx_arp_ioctl)
10576 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10577 				    ire_delete_cache_gw, (char *)&addr, ill);
10578 			else
10579 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10580 				    ALL_ZONES, ipst);
10581 			iocp->ioc_error = 0;
10582 		}
10583 	}
10584 errack:
10585 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10586 		err = iocp->ioc_error;
10587 		freemsg(mp);
10588 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, ipsq);
10589 		return;
10590 	}
10591 
10592 	/*
10593 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10594 	 * the area_t into the struct {x}arpreq.
10595 	 */
10596 	if (x_arp_ioctl) {
10597 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10598 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10599 		    sizeof (xar->xarp_ha.sdl_data)) {
10600 			freemsg(mp);
10601 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10602 			    ipsq);
10603 			return;
10604 		}
10605 	}
10606 	*flagsp = ATF_INUSE;
10607 	if (area->area_flags & ACE_F_PERMANENT)
10608 		*flagsp |= ATF_PERM;
10609 	if (area->area_flags & ACE_F_PUBLISH)
10610 		*flagsp |= ATF_PUBL;
10611 	if (area->area_flags & ACE_F_AUTHORITY)
10612 		*flagsp |= ATF_AUTHORITY;
10613 	if (area->area_hw_addr_length != 0) {
10614 		*flagsp |= ATF_COM;
10615 		/*
10616 		 * For SIOCGARP, MAC address length validation has
10617 		 * already been done before the ioctl was issued to ARP
10618 		 * to allow it to progress only on 6 byte addressable
10619 		 * (ethernet like) media. Thus the mac address copying
10620 		 * can not overwrite the sa_data area below.
10621 		 */
10622 		bcopy((char *)area + area->area_hw_addr_offset,
10623 		    storage, area->area_hw_addr_length);
10624 	}
10625 
10626 	/* Ditch the internal IOCTL. */
10627 	freemsg(mp);
10628 	/* Complete the original. */
10629 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, ipsq);
10630 }
10631 
10632 /*
10633  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10634  * interface) create the next available logical interface for this
10635  * physical interface.
10636  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10637  * ipif with the specified name.
10638  *
10639  * If the address family is not AF_UNSPEC then set the address as well.
10640  *
10641  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10642  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10643  *
10644  * Executed as a writer on the ill.
10645  * So no lock is needed to traverse the ipif chain, or examine the
10646  * phyint flags.
10647  */
10648 /* ARGSUSED */
10649 int
10650 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10651     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10652 {
10653 	mblk_t	*mp1;
10654 	struct lifreq *lifr;
10655 	boolean_t	isv6;
10656 	boolean_t	exists;
10657 	char 	*name;
10658 	char	*endp;
10659 	char	*cp;
10660 	int	namelen;
10661 	ipif_t	*ipif;
10662 	long	id;
10663 	ipsq_t	*ipsq;
10664 	ill_t	*ill;
10665 	sin_t	*sin;
10666 	int	err = 0;
10667 	boolean_t found_sep = B_FALSE;
10668 	conn_t	*connp;
10669 	zoneid_t zoneid;
10670 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10671 
10672 	ASSERT(q->q_next == NULL);
10673 	ip1dbg(("ip_sioctl_addif\n"));
10674 	/* Existence of mp1 has been checked in ip_wput_nondata */
10675 	mp1 = mp->b_cont->b_cont;
10676 	/*
10677 	 * Null terminate the string to protect against buffer
10678 	 * overrun. String was generated by user code and may not
10679 	 * be trusted.
10680 	 */
10681 	lifr = (struct lifreq *)mp1->b_rptr;
10682 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10683 	name = lifr->lifr_name;
10684 	ASSERT(CONN_Q(q));
10685 	connp = Q_TO_CONN(q);
10686 	isv6 = connp->conn_af_isv6;
10687 	zoneid = connp->conn_zoneid;
10688 	namelen = mi_strlen(name);
10689 	if (namelen == 0)
10690 		return (EINVAL);
10691 
10692 	exists = B_FALSE;
10693 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10694 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10695 		/*
10696 		 * Allow creating lo0 using SIOCLIFADDIF.
10697 		 * can't be any other writer thread. So can pass null below
10698 		 * for the last 4 args to ipif_lookup_name.
10699 		 */
10700 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10701 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10702 		/* Prevent any further action */
10703 		if (ipif == NULL) {
10704 			return (ENOBUFS);
10705 		} else if (!exists) {
10706 			/* We created the ipif now and as writer */
10707 			ipif_refrele(ipif);
10708 			return (0);
10709 		} else {
10710 			ill = ipif->ipif_ill;
10711 			ill_refhold(ill);
10712 			ipif_refrele(ipif);
10713 		}
10714 	} else {
10715 		/* Look for a colon in the name. */
10716 		endp = &name[namelen];
10717 		for (cp = endp; --cp > name; ) {
10718 			if (*cp == IPIF_SEPARATOR_CHAR) {
10719 				found_sep = B_TRUE;
10720 				/*
10721 				 * Reject any non-decimal aliases for plumbing
10722 				 * of logical interfaces. Aliases with leading
10723 				 * zeroes are also rejected as they introduce
10724 				 * ambiguity in the naming of the interfaces.
10725 				 * Comparing with "0" takes care of all such
10726 				 * cases.
10727 				 */
10728 				if ((strncmp("0", cp+1, 1)) == 0)
10729 					return (EINVAL);
10730 
10731 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10732 				    id <= 0 || *endp != '\0') {
10733 					return (EINVAL);
10734 				}
10735 				*cp = '\0';
10736 				break;
10737 			}
10738 		}
10739 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10740 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10741 		if (found_sep)
10742 			*cp = IPIF_SEPARATOR_CHAR;
10743 		if (ill == NULL)
10744 			return (err);
10745 	}
10746 
10747 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10748 	    B_TRUE);
10749 
10750 	/*
10751 	 * Release the refhold due to the lookup, now that we are excl
10752 	 * or we are just returning
10753 	 */
10754 	ill_refrele(ill);
10755 
10756 	if (ipsq == NULL)
10757 		return (EINPROGRESS);
10758 
10759 	/* We are now exclusive on the IPSQ */
10760 	ASSERT(IAM_WRITER_ILL(ill));
10761 
10762 	if (found_sep) {
10763 		/* Now see if there is an IPIF with this unit number. */
10764 		for (ipif = ill->ill_ipif; ipif != NULL;
10765 		    ipif = ipif->ipif_next) {
10766 			if (ipif->ipif_id == id) {
10767 				err = EEXIST;
10768 				goto done;
10769 			}
10770 		}
10771 	}
10772 
10773 	/*
10774 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10775 	 * of lo0.  Plumbing for lo0:0 happens in ipif_lookup_on_name()
10776 	 * instead.
10777 	 */
10778 	if ((ipif = ipif_allocate(ill, found_sep ? id : -1, IRE_LOCAL,
10779 	    B_TRUE, B_TRUE)) == NULL) {
10780 		err = ENOBUFS;
10781 		goto done;
10782 	}
10783 
10784 	/* Return created name with ioctl */
10785 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10786 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10787 	ip1dbg(("created %s\n", lifr->lifr_name));
10788 
10789 	/* Set address */
10790 	sin = (sin_t *)&lifr->lifr_addr;
10791 	if (sin->sin_family != AF_UNSPEC) {
10792 		err = ip_sioctl_addr(ipif, sin, q, mp,
10793 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10794 	}
10795 
10796 done:
10797 	ipsq_exit(ipsq);
10798 	return (err);
10799 }
10800 
10801 /*
10802  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10803  * interface) delete it based on the IP address (on this physical interface).
10804  * Otherwise delete it based on the ipif_id.
10805  * Also, special handling to allow a removeif of lo0.
10806  */
10807 /* ARGSUSED */
10808 int
10809 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10810     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10811 {
10812 	conn_t		*connp;
10813 	ill_t		*ill = ipif->ipif_ill;
10814 	boolean_t	 success;
10815 	ip_stack_t	*ipst;
10816 
10817 	ipst = CONNQ_TO_IPST(q);
10818 
10819 	ASSERT(q->q_next == NULL);
10820 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10821 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10822 	ASSERT(IAM_WRITER_IPIF(ipif));
10823 
10824 	connp = Q_TO_CONN(q);
10825 	/*
10826 	 * Special case for unplumbing lo0 (the loopback physical interface).
10827 	 * If unplumbing lo0, the incoming address structure has been
10828 	 * initialized to all zeros. When unplumbing lo0, all its logical
10829 	 * interfaces must be removed too.
10830 	 *
10831 	 * Note that this interface may be called to remove a specific
10832 	 * loopback logical interface (eg, lo0:1). But in that case
10833 	 * ipif->ipif_id != 0 so that the code path for that case is the
10834 	 * same as any other interface (meaning it skips the code directly
10835 	 * below).
10836 	 */
10837 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10838 		if (sin->sin_family == AF_UNSPEC &&
10839 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10840 			/*
10841 			 * Mark it condemned. No new ref. will be made to ill.
10842 			 */
10843 			mutex_enter(&ill->ill_lock);
10844 			ill->ill_state_flags |= ILL_CONDEMNED;
10845 			for (ipif = ill->ill_ipif; ipif != NULL;
10846 			    ipif = ipif->ipif_next) {
10847 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10848 			}
10849 			mutex_exit(&ill->ill_lock);
10850 
10851 			ipif = ill->ill_ipif;
10852 			/* unplumb the loopback interface */
10853 			ill_delete(ill);
10854 			mutex_enter(&connp->conn_lock);
10855 			mutex_enter(&ill->ill_lock);
10856 
10857 			/* Are any references to this ill active */
10858 			if (ill_is_freeable(ill)) {
10859 				mutex_exit(&ill->ill_lock);
10860 				mutex_exit(&connp->conn_lock);
10861 				ill_delete_tail(ill);
10862 				mi_free(ill);
10863 				return (0);
10864 			}
10865 			success = ipsq_pending_mp_add(connp, ipif,
10866 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10867 			mutex_exit(&connp->conn_lock);
10868 			mutex_exit(&ill->ill_lock);
10869 			if (success)
10870 				return (EINPROGRESS);
10871 			else
10872 				return (EINTR);
10873 		}
10874 	}
10875 
10876 	if (ipif->ipif_id == 0) {
10877 		ipsq_t *ipsq;
10878 
10879 		/* Find based on address */
10880 		if (ipif->ipif_isv6) {
10881 			sin6_t *sin6;
10882 
10883 			if (sin->sin_family != AF_INET6)
10884 				return (EAFNOSUPPORT);
10885 
10886 			sin6 = (sin6_t *)sin;
10887 			/* We are a writer, so we should be able to lookup */
10888 			ipif = ipif_lookup_addr_exact_v6(&sin6->sin6_addr, ill,
10889 			    ipst);
10890 		} else {
10891 			if (sin->sin_family != AF_INET)
10892 				return (EAFNOSUPPORT);
10893 
10894 			/* We are a writer, so we should be able to lookup */
10895 			ipif = ipif_lookup_addr_exact(sin->sin_addr.s_addr, ill,
10896 			    ipst);
10897 		}
10898 		if (ipif == NULL) {
10899 			return (EADDRNOTAVAIL);
10900 		}
10901 
10902 		/*
10903 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
10904 		 * lifr_name of the physical interface but with an ip address
10905 		 * lifr_addr of a logical interface plumbed over it.
10906 		 * So update ipx_current_ipif now that ipif points to the
10907 		 * correct one.
10908 		 */
10909 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
10910 		ipsq->ipsq_xop->ipx_current_ipif = ipif;
10911 
10912 		/* This is a writer */
10913 		ipif_refrele(ipif);
10914 	}
10915 
10916 	/*
10917 	 * Can not delete instance zero since it is tied to the ill.
10918 	 */
10919 	if (ipif->ipif_id == 0)
10920 		return (EBUSY);
10921 
10922 	mutex_enter(&ill->ill_lock);
10923 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10924 	mutex_exit(&ill->ill_lock);
10925 
10926 	ipif_free(ipif);
10927 
10928 	mutex_enter(&connp->conn_lock);
10929 	mutex_enter(&ill->ill_lock);
10930 
10931 	/* Are any references to this ipif active */
10932 	if (ipif_is_freeable(ipif)) {
10933 		mutex_exit(&ill->ill_lock);
10934 		mutex_exit(&connp->conn_lock);
10935 		ipif_non_duplicate(ipif);
10936 		ipif_down_tail(ipif);
10937 		ipif_free_tail(ipif); /* frees ipif */
10938 		return (0);
10939 	}
10940 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10941 	    IPIF_FREE);
10942 	mutex_exit(&ill->ill_lock);
10943 	mutex_exit(&connp->conn_lock);
10944 	if (success)
10945 		return (EINPROGRESS);
10946 	else
10947 		return (EINTR);
10948 }
10949 
10950 /*
10951  * Restart the removeif ioctl. The refcnt has gone down to 0.
10952  * The ipif is already condemned. So can't find it thru lookups.
10953  */
10954 /* ARGSUSED */
10955 int
10956 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
10957     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10958 {
10959 	ill_t *ill = ipif->ipif_ill;
10960 
10961 	ASSERT(IAM_WRITER_IPIF(ipif));
10962 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
10963 
10964 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
10965 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
10966 
10967 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10968 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
10969 		ill_delete_tail(ill);
10970 		mi_free(ill);
10971 		return (0);
10972 	}
10973 
10974 	ipif_non_duplicate(ipif);
10975 	ipif_down_tail(ipif);
10976 	ipif_free_tail(ipif);
10977 
10978 	ILL_UNMARK_CHANGING(ill);
10979 	return (0);
10980 }
10981 
10982 /*
10983  * Set the local interface address.
10984  * Allow an address of all zero when the interface is down.
10985  */
10986 /* ARGSUSED */
10987 int
10988 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10989     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10990 {
10991 	int err = 0;
10992 	in6_addr_t v6addr;
10993 	boolean_t need_up = B_FALSE;
10994 
10995 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
10996 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10997 
10998 	ASSERT(IAM_WRITER_IPIF(ipif));
10999 
11000 	if (ipif->ipif_isv6) {
11001 		sin6_t *sin6;
11002 		ill_t *ill;
11003 		phyint_t *phyi;
11004 
11005 		if (sin->sin_family != AF_INET6)
11006 			return (EAFNOSUPPORT);
11007 
11008 		sin6 = (sin6_t *)sin;
11009 		v6addr = sin6->sin6_addr;
11010 		ill = ipif->ipif_ill;
11011 		phyi = ill->ill_phyint;
11012 
11013 		/*
11014 		 * Enforce that true multicast interfaces have a link-local
11015 		 * address for logical unit 0.
11016 		 */
11017 		if (ipif->ipif_id == 0 &&
11018 		    (ill->ill_flags & ILLF_MULTICAST) &&
11019 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11020 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11021 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11022 			return (EADDRNOTAVAIL);
11023 		}
11024 
11025 		/*
11026 		 * up interfaces shouldn't have the unspecified address
11027 		 * unless they also have the IPIF_NOLOCAL flags set and
11028 		 * have a subnet assigned.
11029 		 */
11030 		if ((ipif->ipif_flags & IPIF_UP) &&
11031 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11032 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11033 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11034 			return (EADDRNOTAVAIL);
11035 		}
11036 
11037 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11038 			return (EADDRNOTAVAIL);
11039 	} else {
11040 		ipaddr_t addr;
11041 
11042 		if (sin->sin_family != AF_INET)
11043 			return (EAFNOSUPPORT);
11044 
11045 		addr = sin->sin_addr.s_addr;
11046 
11047 		/* Allow 0 as the local address. */
11048 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11049 			return (EADDRNOTAVAIL);
11050 
11051 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11052 	}
11053 
11054 	/*
11055 	 * Even if there is no change we redo things just to rerun
11056 	 * ipif_set_default.
11057 	 */
11058 	if (ipif->ipif_flags & IPIF_UP) {
11059 		/*
11060 		 * Setting a new local address, make sure
11061 		 * we have net and subnet bcast ire's for
11062 		 * the old address if we need them.
11063 		 */
11064 		if (!ipif->ipif_isv6)
11065 			ipif_check_bcast_ires(ipif);
11066 		/*
11067 		 * If the interface is already marked up,
11068 		 * we call ipif_down which will take care
11069 		 * of ditching any IREs that have been set
11070 		 * up based on the old interface address.
11071 		 */
11072 		err = ipif_logical_down(ipif, q, mp);
11073 		if (err == EINPROGRESS)
11074 			return (err);
11075 		ipif_down_tail(ipif);
11076 		need_up = 1;
11077 	}
11078 
11079 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11080 	return (err);
11081 }
11082 
11083 int
11084 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11085     boolean_t need_up)
11086 {
11087 	in6_addr_t v6addr;
11088 	in6_addr_t ov6addr;
11089 	ipaddr_t addr;
11090 	sin6_t	*sin6;
11091 	int	sinlen;
11092 	int	err = 0;
11093 	ill_t	*ill = ipif->ipif_ill;
11094 	boolean_t need_dl_down;
11095 	boolean_t need_arp_down;
11096 	struct iocblk *iocp;
11097 
11098 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11099 
11100 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11101 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11102 	ASSERT(IAM_WRITER_IPIF(ipif));
11103 
11104 	/* Must cancel any pending timer before taking the ill_lock */
11105 	if (ipif->ipif_recovery_id != 0)
11106 		(void) untimeout(ipif->ipif_recovery_id);
11107 	ipif->ipif_recovery_id = 0;
11108 
11109 	if (ipif->ipif_isv6) {
11110 		sin6 = (sin6_t *)sin;
11111 		v6addr = sin6->sin6_addr;
11112 		sinlen = sizeof (struct sockaddr_in6);
11113 	} else {
11114 		addr = sin->sin_addr.s_addr;
11115 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11116 		sinlen = sizeof (struct sockaddr_in);
11117 	}
11118 	mutex_enter(&ill->ill_lock);
11119 	ov6addr = ipif->ipif_v6lcl_addr;
11120 	ipif->ipif_v6lcl_addr = v6addr;
11121 	sctp_update_ipif_addr(ipif, ov6addr);
11122 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11123 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11124 	} else {
11125 		ipif->ipif_v6src_addr = v6addr;
11126 	}
11127 	ipif->ipif_addr_ready = 0;
11128 
11129 	/*
11130 	 * If the interface was previously marked as a duplicate, then since
11131 	 * we've now got a "new" address, it should no longer be considered a
11132 	 * duplicate -- even if the "new" address is the same as the old one.
11133 	 * Note that if all ipifs are down, we may have a pending ARP down
11134 	 * event to handle.  This is because we want to recover from duplicates
11135 	 * and thus delay tearing down ARP until the duplicates have been
11136 	 * removed or disabled.
11137 	 */
11138 	need_dl_down = need_arp_down = B_FALSE;
11139 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11140 		need_arp_down = !need_up;
11141 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11142 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11143 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11144 			need_dl_down = B_TRUE;
11145 		}
11146 	}
11147 
11148 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11149 	    !ill->ill_is_6to4tun) {
11150 		queue_t *wqp = ill->ill_wq;
11151 
11152 		/*
11153 		 * The local address of this interface is a 6to4 address,
11154 		 * check if this interface is in fact a 6to4 tunnel or just
11155 		 * an interface configured with a 6to4 address.  We are only
11156 		 * interested in the former.
11157 		 */
11158 		if (wqp != NULL) {
11159 			while ((wqp->q_next != NULL) &&
11160 			    (wqp->q_next->q_qinfo != NULL) &&
11161 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11162 
11163 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11164 				    == TUN6TO4_MODID) {
11165 					/* set for use in IP */
11166 					ill->ill_is_6to4tun = 1;
11167 					break;
11168 				}
11169 				wqp = wqp->q_next;
11170 			}
11171 		}
11172 	}
11173 
11174 	ipif_set_default(ipif);
11175 
11176 	/*
11177 	 * When publishing an interface address change event, we only notify
11178 	 * the event listeners of the new address.  It is assumed that if they
11179 	 * actively care about the addresses assigned that they will have
11180 	 * already discovered the previous address assigned (if there was one.)
11181 	 *
11182 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11183 	 */
11184 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11185 		ill_nic_event_dispatch(ill, MAP_IPIF_ID(ipif->ipif_id),
11186 		    NE_ADDRESS_CHANGE, sin, sinlen);
11187 	}
11188 
11189 	mutex_exit(&ill->ill_lock);
11190 
11191 	if (need_up) {
11192 		/*
11193 		 * Now bring the interface back up.  If this
11194 		 * is the only IPIF for the ILL, ipif_up
11195 		 * will have to re-bind to the device, so
11196 		 * we may get back EINPROGRESS, in which
11197 		 * case, this IOCTL will get completed in
11198 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11199 		 */
11200 		err = ipif_up(ipif, q, mp);
11201 	}
11202 
11203 	if (need_dl_down)
11204 		ill_dl_down(ill);
11205 	if (need_arp_down)
11206 		ipif_resolver_down(ipif);
11207 
11208 	return (err);
11209 }
11210 
11211 /*
11212  * Restart entry point to restart the address set operation after the
11213  * refcounts have dropped to zero.
11214  */
11215 /* ARGSUSED */
11216 int
11217 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11218     ip_ioctl_cmd_t *ipip, void *ifreq)
11219 {
11220 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11221 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11222 	ASSERT(IAM_WRITER_IPIF(ipif));
11223 	ipif_down_tail(ipif);
11224 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11225 }
11226 
11227 /* ARGSUSED */
11228 int
11229 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11230     ip_ioctl_cmd_t *ipip, void *if_req)
11231 {
11232 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11233 	struct lifreq *lifr = (struct lifreq *)if_req;
11234 
11235 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11236 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11237 	/*
11238 	 * The net mask and address can't change since we have a
11239 	 * reference to the ipif. So no lock is necessary.
11240 	 */
11241 	if (ipif->ipif_isv6) {
11242 		*sin6 = sin6_null;
11243 		sin6->sin6_family = AF_INET6;
11244 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11245 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11246 		lifr->lifr_addrlen =
11247 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11248 	} else {
11249 		*sin = sin_null;
11250 		sin->sin_family = AF_INET;
11251 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11252 		if (ipip->ipi_cmd_type == LIF_CMD) {
11253 			lifr->lifr_addrlen =
11254 			    ip_mask_to_plen(ipif->ipif_net_mask);
11255 		}
11256 	}
11257 	return (0);
11258 }
11259 
11260 /*
11261  * Set the destination address for a pt-pt interface.
11262  */
11263 /* ARGSUSED */
11264 int
11265 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11266     ip_ioctl_cmd_t *ipip, void *if_req)
11267 {
11268 	int err = 0;
11269 	in6_addr_t v6addr;
11270 	boolean_t need_up = B_FALSE;
11271 
11272 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11273 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11274 	ASSERT(IAM_WRITER_IPIF(ipif));
11275 
11276 	if (ipif->ipif_isv6) {
11277 		sin6_t *sin6;
11278 
11279 		if (sin->sin_family != AF_INET6)
11280 			return (EAFNOSUPPORT);
11281 
11282 		sin6 = (sin6_t *)sin;
11283 		v6addr = sin6->sin6_addr;
11284 
11285 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11286 			return (EADDRNOTAVAIL);
11287 	} else {
11288 		ipaddr_t addr;
11289 
11290 		if (sin->sin_family != AF_INET)
11291 			return (EAFNOSUPPORT);
11292 
11293 		addr = sin->sin_addr.s_addr;
11294 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11295 			return (EADDRNOTAVAIL);
11296 
11297 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11298 	}
11299 
11300 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11301 		return (0);	/* No change */
11302 
11303 	if (ipif->ipif_flags & IPIF_UP) {
11304 		/*
11305 		 * If the interface is already marked up,
11306 		 * we call ipif_down which will take care
11307 		 * of ditching any IREs that have been set
11308 		 * up based on the old pp dst address.
11309 		 */
11310 		err = ipif_logical_down(ipif, q, mp);
11311 		if (err == EINPROGRESS)
11312 			return (err);
11313 		ipif_down_tail(ipif);
11314 		need_up = B_TRUE;
11315 	}
11316 	/*
11317 	 * could return EINPROGRESS. If so ioctl will complete in
11318 	 * ip_rput_dlpi_writer
11319 	 */
11320 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11321 	return (err);
11322 }
11323 
11324 static int
11325 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11326     boolean_t need_up)
11327 {
11328 	in6_addr_t v6addr;
11329 	ill_t	*ill = ipif->ipif_ill;
11330 	int	err = 0;
11331 	boolean_t need_dl_down;
11332 	boolean_t need_arp_down;
11333 
11334 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11335 	    ipif->ipif_id, (void *)ipif));
11336 
11337 	/* Must cancel any pending timer before taking the ill_lock */
11338 	if (ipif->ipif_recovery_id != 0)
11339 		(void) untimeout(ipif->ipif_recovery_id);
11340 	ipif->ipif_recovery_id = 0;
11341 
11342 	if (ipif->ipif_isv6) {
11343 		sin6_t *sin6;
11344 
11345 		sin6 = (sin6_t *)sin;
11346 		v6addr = sin6->sin6_addr;
11347 	} else {
11348 		ipaddr_t addr;
11349 
11350 		addr = sin->sin_addr.s_addr;
11351 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11352 	}
11353 	mutex_enter(&ill->ill_lock);
11354 	/* Set point to point destination address. */
11355 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11356 		/*
11357 		 * Allow this as a means of creating logical
11358 		 * pt-pt interfaces on top of e.g. an Ethernet.
11359 		 * XXX Undocumented HACK for testing.
11360 		 * pt-pt interfaces are created with NUD disabled.
11361 		 */
11362 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11363 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11364 		if (ipif->ipif_isv6)
11365 			ill->ill_flags |= ILLF_NONUD;
11366 	}
11367 
11368 	/*
11369 	 * If the interface was previously marked as a duplicate, then since
11370 	 * we've now got a "new" address, it should no longer be considered a
11371 	 * duplicate -- even if the "new" address is the same as the old one.
11372 	 * Note that if all ipifs are down, we may have a pending ARP down
11373 	 * event to handle.
11374 	 */
11375 	need_dl_down = need_arp_down = B_FALSE;
11376 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11377 		need_arp_down = !need_up;
11378 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11379 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11380 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11381 			need_dl_down = B_TRUE;
11382 		}
11383 	}
11384 
11385 	/* Set the new address. */
11386 	ipif->ipif_v6pp_dst_addr = v6addr;
11387 	/* Make sure subnet tracks pp_dst */
11388 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11389 	mutex_exit(&ill->ill_lock);
11390 
11391 	if (need_up) {
11392 		/*
11393 		 * Now bring the interface back up.  If this
11394 		 * is the only IPIF for the ILL, ipif_up
11395 		 * will have to re-bind to the device, so
11396 		 * we may get back EINPROGRESS, in which
11397 		 * case, this IOCTL will get completed in
11398 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11399 		 */
11400 		err = ipif_up(ipif, q, mp);
11401 	}
11402 
11403 	if (need_dl_down)
11404 		ill_dl_down(ill);
11405 	if (need_arp_down)
11406 		ipif_resolver_down(ipif);
11407 
11408 	return (err);
11409 }
11410 
11411 /*
11412  * Restart entry point to restart the dstaddress set operation after the
11413  * refcounts have dropped to zero.
11414  */
11415 /* ARGSUSED */
11416 int
11417 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11418     ip_ioctl_cmd_t *ipip, void *ifreq)
11419 {
11420 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11421 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11422 	ipif_down_tail(ipif);
11423 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11424 }
11425 
11426 /* ARGSUSED */
11427 int
11428 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11429     ip_ioctl_cmd_t *ipip, void *if_req)
11430 {
11431 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11432 
11433 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11434 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11435 	/*
11436 	 * Get point to point destination address. The addresses can't
11437 	 * change since we hold a reference to the ipif.
11438 	 */
11439 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11440 		return (EADDRNOTAVAIL);
11441 
11442 	if (ipif->ipif_isv6) {
11443 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11444 		*sin6 = sin6_null;
11445 		sin6->sin6_family = AF_INET6;
11446 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11447 	} else {
11448 		*sin = sin_null;
11449 		sin->sin_family = AF_INET;
11450 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11451 	}
11452 	return (0);
11453 }
11454 
11455 /*
11456  * Set interface flags.  Many flags require special handling (e.g.,
11457  * bringing the interface down); see below for details.
11458  *
11459  * NOTE : We really don't enforce that ipif_id zero should be used
11460  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11461  *	  is because applications generally does SICGLIFFLAGS and
11462  *	  ORs in the new flags (that affects the logical) and does a
11463  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11464  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11465  *	  flags that will be turned on is correct with respect to
11466  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11467  */
11468 /* ARGSUSED */
11469 int
11470 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11471     ip_ioctl_cmd_t *ipip, void *if_req)
11472 {
11473 	uint64_t turn_on;
11474 	uint64_t turn_off;
11475 	int	err = 0;
11476 	phyint_t *phyi;
11477 	ill_t *ill;
11478 	uint64_t intf_flags, cantchange_flags;
11479 	boolean_t phyint_flags_modified = B_FALSE;
11480 	uint64_t flags;
11481 	struct ifreq *ifr;
11482 	struct lifreq *lifr;
11483 	boolean_t set_linklocal = B_FALSE;
11484 	boolean_t zero_source = B_FALSE;
11485 
11486 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11487 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11488 
11489 	ASSERT(IAM_WRITER_IPIF(ipif));
11490 
11491 	ill = ipif->ipif_ill;
11492 	phyi = ill->ill_phyint;
11493 
11494 	if (ipip->ipi_cmd_type == IF_CMD) {
11495 		ifr = (struct ifreq *)if_req;
11496 		flags = (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11497 	} else {
11498 		lifr = (struct lifreq *)if_req;
11499 		flags = lifr->lifr_flags;
11500 	}
11501 
11502 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11503 
11504 	/*
11505 	 * Have the flags been set correctly until now?
11506 	 */
11507 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11508 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11509 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11510 	/*
11511 	 * Compare the new flags to the old, and partition
11512 	 * into those coming on and those going off.
11513 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11514 	 */
11515 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11516 		flags |= intf_flags & ~0xFFFF;
11517 
11518 	/*
11519 	 * Explicitly fail attempts to change flags that are always invalid on
11520 	 * an IPMP meta-interface.
11521 	 */
11522 	if (IS_IPMP(ill) && ((flags ^ intf_flags) & IFF_IPMP_INVALID))
11523 		return (EINVAL);
11524 
11525 	/*
11526 	 * Check which flags will change; silently ignore flags which userland
11527 	 * is not allowed to control.  (Because these flags may change between
11528 	 * SIOCGLIFFLAGS and SIOCSLIFFLAGS, and that's outside of userland's
11529 	 * control, we need to silently ignore them rather than fail.)
11530 	 */
11531 	cantchange_flags = IFF_CANTCHANGE;
11532 	if (IS_IPMP(ill))
11533 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
11534 
11535 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
11536 	if (turn_on == 0)
11537 		return (0);	/* No change */
11538 
11539 	turn_off = intf_flags & turn_on;
11540 	turn_on ^= turn_off;
11541 
11542 	/*
11543 	 * All test addresses must be IFF_DEPRECATED (to ensure source address
11544 	 * selection avoids them) -- so force IFF_DEPRECATED on, and do not
11545 	 * allow it to be turned off.
11546 	 */
11547 	if ((turn_off & (IFF_DEPRECATED|IFF_NOFAILOVER)) == IFF_DEPRECATED &&
11548 	    (turn_on|intf_flags) & IFF_NOFAILOVER)
11549 		return (EINVAL);
11550 
11551 	if (turn_on & IFF_NOFAILOVER) {
11552 		turn_on |= IFF_DEPRECATED;
11553 		flags |= IFF_DEPRECATED;
11554 	}
11555 
11556 	/*
11557 	 * On underlying interfaces, only allow applications to manage test
11558 	 * addresses -- otherwise, they may get confused when the address
11559 	 * moves as part of being brought up.  Likewise, prevent an
11560 	 * application-managed test address from being converted to a data
11561 	 * address.  To prevent migration of administratively up addresses in
11562 	 * the kernel, we don't allow them to be converted either.
11563 	 */
11564 	if (IS_UNDER_IPMP(ill)) {
11565 		const uint64_t appflags = IFF_DHCPRUNNING | IFF_ADDRCONF;
11566 
11567 		if ((turn_on & appflags) && !(flags & IFF_NOFAILOVER))
11568 			return (EINVAL);
11569 
11570 		if ((turn_off & IFF_NOFAILOVER) &&
11571 		    (flags & (appflags | IFF_UP | IFF_DUPLICATE)))
11572 			return (EINVAL);
11573 	}
11574 
11575 	/*
11576 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11577 	 * IPv6 interfaces.
11578 	 */
11579 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11580 		return (EINVAL);
11581 
11582 	/*
11583 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
11584 	 */
11585 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
11586 		return (EINVAL);
11587 
11588 	/*
11589 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11590 	 * interfaces.  It makes no sense in that context.
11591 	 */
11592 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11593 		return (EINVAL);
11594 
11595 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11596 		zero_source = B_TRUE;
11597 
11598 	/*
11599 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11600 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11601 	 * If the link local address isn't set, and can be set, it will get
11602 	 * set later on in this function.
11603 	 */
11604 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11605 	    (flags & IFF_UP) && !zero_source &&
11606 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11607 		if (ipif_cant_setlinklocal(ipif))
11608 			return (EINVAL);
11609 		set_linklocal = B_TRUE;
11610 	}
11611 
11612 	/*
11613 	 * If we modify physical interface flags, we'll potentially need to
11614 	 * send up two routing socket messages for the changes (one for the
11615 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11616 	 */
11617 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11618 		phyint_flags_modified = B_TRUE;
11619 
11620 	/*
11621 	 * All functioning PHYI_STANDBY interfaces start life PHYI_INACTIVE
11622 	 * (otherwise, we'd immediately use them, defeating standby).  Also,
11623 	 * since PHYI_INACTIVE has a separate meaning when PHYI_STANDBY is not
11624 	 * set, don't allow PHYI_STANDBY to be set if PHYI_INACTIVE is already
11625 	 * set, and clear PHYI_INACTIVE if PHYI_STANDBY is being cleared.  We
11626 	 * also don't allow PHYI_STANDBY if VNI is enabled since its semantics
11627 	 * will not be honored.
11628 	 */
11629 	if (turn_on & PHYI_STANDBY) {
11630 		/*
11631 		 * No need to grab ill_g_usesrc_lock here; see the
11632 		 * synchronization notes in ip.c.
11633 		 */
11634 		if (ill->ill_usesrc_grp_next != NULL ||
11635 		    intf_flags & PHYI_INACTIVE)
11636 			return (EINVAL);
11637 		if (!(flags & PHYI_FAILED)) {
11638 			flags |= PHYI_INACTIVE;
11639 			turn_on |= PHYI_INACTIVE;
11640 		}
11641 	}
11642 
11643 	if (turn_off & PHYI_STANDBY) {
11644 		flags &= ~PHYI_INACTIVE;
11645 		turn_off |= PHYI_INACTIVE;
11646 	}
11647 
11648 	/*
11649 	 * PHYI_FAILED and PHYI_INACTIVE are mutually exclusive; fail if both
11650 	 * would end up on.
11651 	 */
11652 	if ((flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
11653 	    (PHYI_FAILED | PHYI_INACTIVE))
11654 		return (EINVAL);
11655 
11656 	/*
11657 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11658 	 * status of the interface.
11659 	 */
11660 	if ((turn_on | turn_off) & ILLF_ROUTER)
11661 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
11662 
11663 	/*
11664 	 * If the interface is not UP and we are not going to
11665 	 * bring it UP, record the flags and return. When the
11666 	 * interface comes UP later, the right actions will be
11667 	 * taken.
11668 	 */
11669 	if (!(ipif->ipif_flags & IPIF_UP) &&
11670 	    !(turn_on & IPIF_UP)) {
11671 		/* Record new flags in their respective places. */
11672 		mutex_enter(&ill->ill_lock);
11673 		mutex_enter(&ill->ill_phyint->phyint_lock);
11674 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11675 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11676 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11677 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11678 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11679 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11680 		mutex_exit(&ill->ill_lock);
11681 		mutex_exit(&ill->ill_phyint->phyint_lock);
11682 
11683 		/*
11684 		 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the
11685 		 * same to the kernel: if any of them has been set by
11686 		 * userland, the interface cannot be used for data traffic.
11687 		 */
11688 		if ((turn_on|turn_off) &
11689 		    (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
11690 			ASSERT(!IS_IPMP(ill));
11691 			/*
11692 			 * It's possible the ill is part of an "anonymous"
11693 			 * IPMP group rather than a real group.  In that case,
11694 			 * there are no other interfaces in the group and thus
11695 			 * no need to call ipmp_phyint_refresh_active().
11696 			 */
11697 			if (IS_UNDER_IPMP(ill))
11698 				ipmp_phyint_refresh_active(phyi);
11699 		}
11700 
11701 		if (phyint_flags_modified) {
11702 			if (phyi->phyint_illv4 != NULL) {
11703 				ip_rts_ifmsg(phyi->phyint_illv4->
11704 				    ill_ipif, RTSQ_DEFAULT);
11705 			}
11706 			if (phyi->phyint_illv6 != NULL) {
11707 				ip_rts_ifmsg(phyi->phyint_illv6->
11708 				    ill_ipif, RTSQ_DEFAULT);
11709 			}
11710 		}
11711 		return (0);
11712 	} else if (set_linklocal || zero_source) {
11713 		mutex_enter(&ill->ill_lock);
11714 		if (set_linklocal)
11715 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11716 		if (zero_source)
11717 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11718 		mutex_exit(&ill->ill_lock);
11719 	}
11720 
11721 	/*
11722 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11723 	 * or point-to-point interfaces with an unspecified destination. We do
11724 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11725 	 * have a subnet assigned, which is how in.ndpd currently manages its
11726 	 * onlink prefix list when no addresses are configured with those
11727 	 * prefixes.
11728 	 */
11729 	if (ipif->ipif_isv6 &&
11730 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11731 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11732 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11733 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11734 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11735 		return (EINVAL);
11736 	}
11737 
11738 	/*
11739 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11740 	 * from being brought up.
11741 	 */
11742 	if (!ipif->ipif_isv6 &&
11743 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11744 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11745 		return (EINVAL);
11746 	}
11747 
11748 	/*
11749 	 * The only flag changes that we currently take specific action on are
11750 	 * IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL, ILLF_NOARP,
11751 	 * ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, IPIF_PREFERRED, and
11752 	 * IPIF_NOFAILOVER.  This is done by bring the ipif down, changing the
11753 	 * flags and bringing it back up again.  For IPIF_NOFAILOVER, the act
11754 	 * of bringing it back up will trigger the address to be moved.
11755 	 */
11756 	if ((turn_on|turn_off) &
11757 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11758 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED|
11759 	    IPIF_NOFAILOVER)) {
11760 		/*
11761 		 * Taking this ipif down, make sure we have
11762 		 * valid net and subnet bcast ire's for other
11763 		 * logical interfaces, if we need them.
11764 		 */
11765 		if (!ipif->ipif_isv6)
11766 			ipif_check_bcast_ires(ipif);
11767 
11768 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11769 		    !(turn_off & IPIF_UP)) {
11770 			if (ipif->ipif_flags & IPIF_UP)
11771 				ill->ill_logical_down = 1;
11772 			turn_on &= ~IPIF_UP;
11773 		}
11774 		err = ipif_down(ipif, q, mp);
11775 		ip1dbg(("ipif_down returns %d err ", err));
11776 		if (err == EINPROGRESS)
11777 			return (err);
11778 		ipif_down_tail(ipif);
11779 	}
11780 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
11781 }
11782 
11783 static int
11784 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
11785 {
11786 	ill_t	*ill;
11787 	phyint_t *phyi;
11788 	uint64_t turn_on, turn_off;
11789 	uint64_t intf_flags, cantchange_flags;
11790 	boolean_t phyint_flags_modified = B_FALSE;
11791 	int	err = 0;
11792 	boolean_t set_linklocal = B_FALSE;
11793 	boolean_t zero_source = B_FALSE;
11794 
11795 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
11796 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
11797 
11798 	ASSERT(IAM_WRITER_IPIF(ipif));
11799 
11800 	ill = ipif->ipif_ill;
11801 	phyi = ill->ill_phyint;
11802 
11803 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11804 	cantchange_flags = IFF_CANTCHANGE | IFF_UP;
11805 	if (IS_IPMP(ill))
11806 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
11807 
11808 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
11809 	turn_off = intf_flags & turn_on;
11810 	turn_on ^= turn_off;
11811 
11812 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11813 		phyint_flags_modified = B_TRUE;
11814 
11815 	/*
11816 	 * Now we change the flags. Track current value of
11817 	 * other flags in their respective places.
11818 	 */
11819 	mutex_enter(&ill->ill_lock);
11820 	mutex_enter(&phyi->phyint_lock);
11821 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11822 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11823 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11824 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11825 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11826 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11827 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
11828 		set_linklocal = B_TRUE;
11829 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
11830 	}
11831 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
11832 		zero_source = B_TRUE;
11833 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
11834 	}
11835 	mutex_exit(&ill->ill_lock);
11836 	mutex_exit(&phyi->phyint_lock);
11837 
11838 	if (set_linklocal)
11839 		(void) ipif_setlinklocal(ipif);
11840 
11841 	if (zero_source)
11842 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11843 	else
11844 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
11845 
11846 	/*
11847 	 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the same to
11848 	 * the kernel: if any of them has been set by userland, the interface
11849 	 * cannot be used for data traffic.
11850 	 */
11851 	if ((turn_on|turn_off) & (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
11852 		ASSERT(!IS_IPMP(ill));
11853 		/*
11854 		 * It's possible the ill is part of an "anonymous" IPMP group
11855 		 * rather than a real group.  In that case, there are no other
11856 		 * interfaces in the group and thus no need for us to call
11857 		 * ipmp_phyint_refresh_active().
11858 		 */
11859 		if (IS_UNDER_IPMP(ill))
11860 			ipmp_phyint_refresh_active(phyi);
11861 	}
11862 
11863 	if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
11864 		/*
11865 		 * XXX ipif_up really does not know whether a phyint flags
11866 		 * was modified or not. So, it sends up information on
11867 		 * only one routing sockets message. As we don't bring up
11868 		 * the interface and also set PHYI_ flags simultaneously
11869 		 * it should be okay.
11870 		 */
11871 		err = ipif_up(ipif, q, mp);
11872 	} else {
11873 		/*
11874 		 * Make sure routing socket sees all changes to the flags.
11875 		 * ipif_up_done* handles this when we use ipif_up.
11876 		 */
11877 		if (phyint_flags_modified) {
11878 			if (phyi->phyint_illv4 != NULL) {
11879 				ip_rts_ifmsg(phyi->phyint_illv4->
11880 				    ill_ipif, RTSQ_DEFAULT);
11881 			}
11882 			if (phyi->phyint_illv6 != NULL) {
11883 				ip_rts_ifmsg(phyi->phyint_illv6->
11884 				    ill_ipif, RTSQ_DEFAULT);
11885 			}
11886 		} else {
11887 			ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
11888 		}
11889 		/*
11890 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
11891 		 * this in need_up case.
11892 		 */
11893 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
11894 	}
11895 	return (err);
11896 }
11897 
11898 /*
11899  * Restart the flags operation now that the refcounts have dropped to zero.
11900  */
11901 /* ARGSUSED */
11902 int
11903 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11904     ip_ioctl_cmd_t *ipip, void *if_req)
11905 {
11906 	uint64_t flags;
11907 	struct ifreq *ifr = if_req;
11908 	struct lifreq *lifr = if_req;
11909 
11910 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
11911 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11912 
11913 	ipif_down_tail(ipif);
11914 	if (ipip->ipi_cmd_type == IF_CMD) {
11915 		/* cast to uint16_t prevents unwanted sign extension */
11916 		flags = (uint16_t)ifr->ifr_flags;
11917 	} else {
11918 		flags = lifr->lifr_flags;
11919 	}
11920 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
11921 }
11922 
11923 /*
11924  * Can operate on either a module or a driver queue.
11925  */
11926 /* ARGSUSED */
11927 int
11928 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11929     ip_ioctl_cmd_t *ipip, void *if_req)
11930 {
11931 	/*
11932 	 * Has the flags been set correctly till now ?
11933 	 */
11934 	ill_t *ill = ipif->ipif_ill;
11935 	phyint_t *phyi = ill->ill_phyint;
11936 
11937 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
11938 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11939 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11940 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11941 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11942 
11943 	/*
11944 	 * Need a lock since some flags can be set even when there are
11945 	 * references to the ipif.
11946 	 */
11947 	mutex_enter(&ill->ill_lock);
11948 	if (ipip->ipi_cmd_type == IF_CMD) {
11949 		struct ifreq *ifr = (struct ifreq *)if_req;
11950 
11951 		/* Get interface flags (low 16 only). */
11952 		ifr->ifr_flags = ((ipif->ipif_flags |
11953 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
11954 	} else {
11955 		struct lifreq *lifr = (struct lifreq *)if_req;
11956 
11957 		/* Get interface flags. */
11958 		lifr->lifr_flags = ipif->ipif_flags |
11959 		    ill->ill_flags | phyi->phyint_flags;
11960 	}
11961 	mutex_exit(&ill->ill_lock);
11962 	return (0);
11963 }
11964 
11965 /* ARGSUSED */
11966 int
11967 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11968     ip_ioctl_cmd_t *ipip, void *if_req)
11969 {
11970 	int mtu;
11971 	int ip_min_mtu;
11972 	struct ifreq	*ifr;
11973 	struct lifreq *lifr;
11974 	ire_t	*ire;
11975 	ip_stack_t *ipst;
11976 
11977 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
11978 	    ipif->ipif_id, (void *)ipif));
11979 	if (ipip->ipi_cmd_type == IF_CMD) {
11980 		ifr = (struct ifreq *)if_req;
11981 		mtu = ifr->ifr_metric;
11982 	} else {
11983 		lifr = (struct lifreq *)if_req;
11984 		mtu = lifr->lifr_mtu;
11985 	}
11986 
11987 	if (ipif->ipif_isv6)
11988 		ip_min_mtu = IPV6_MIN_MTU;
11989 	else
11990 		ip_min_mtu = IP_MIN_MTU;
11991 
11992 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
11993 		return (EINVAL);
11994 
11995 	/*
11996 	 * Change the MTU size in all relevant ire's.
11997 	 * Mtu change Vs. new ire creation - protocol below.
11998 	 * First change ipif_mtu and the ire_max_frag of the
11999 	 * interface ire. Then do an ire walk and change the
12000 	 * ire_max_frag of all affected ires. During ire_add
12001 	 * under the bucket lock, set the ire_max_frag of the
12002 	 * new ire being created from the ipif/ire from which
12003 	 * it is being derived. If an mtu change happens after
12004 	 * the ire is added, the new ire will be cleaned up.
12005 	 * Conversely if the mtu change happens before the ire
12006 	 * is added, ire_add will see the new value of the mtu.
12007 	 */
12008 	ipif->ipif_mtu = mtu;
12009 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12010 
12011 	if (ipif->ipif_isv6)
12012 		ire = ipif_to_ire_v6(ipif);
12013 	else
12014 		ire = ipif_to_ire(ipif);
12015 	if (ire != NULL) {
12016 		ire->ire_max_frag = ipif->ipif_mtu;
12017 		ire_refrele(ire);
12018 	}
12019 	ipst = ipif->ipif_ill->ill_ipst;
12020 	if (ipif->ipif_flags & IPIF_UP) {
12021 		if (ipif->ipif_isv6)
12022 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12023 			    ipst);
12024 		else
12025 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12026 			    ipst);
12027 	}
12028 	/* Update the MTU in SCTP's list */
12029 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12030 	return (0);
12031 }
12032 
12033 /* Get interface MTU. */
12034 /* ARGSUSED */
12035 int
12036 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12037 	ip_ioctl_cmd_t *ipip, void *if_req)
12038 {
12039 	struct ifreq	*ifr;
12040 	struct lifreq	*lifr;
12041 
12042 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12043 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12044 	if (ipip->ipi_cmd_type == IF_CMD) {
12045 		ifr = (struct ifreq *)if_req;
12046 		ifr->ifr_metric = ipif->ipif_mtu;
12047 	} else {
12048 		lifr = (struct lifreq *)if_req;
12049 		lifr->lifr_mtu = ipif->ipif_mtu;
12050 	}
12051 	return (0);
12052 }
12053 
12054 /* Set interface broadcast address. */
12055 /* ARGSUSED2 */
12056 int
12057 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12058 	ip_ioctl_cmd_t *ipip, void *if_req)
12059 {
12060 	ipaddr_t addr;
12061 	ire_t	*ire;
12062 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12063 
12064 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12065 	    ipif->ipif_id));
12066 
12067 	ASSERT(IAM_WRITER_IPIF(ipif));
12068 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12069 		return (EADDRNOTAVAIL);
12070 
12071 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12072 
12073 	if (sin->sin_family != AF_INET)
12074 		return (EAFNOSUPPORT);
12075 
12076 	addr = sin->sin_addr.s_addr;
12077 	if (ipif->ipif_flags & IPIF_UP) {
12078 		/*
12079 		 * If we are already up, make sure the new
12080 		 * broadcast address makes sense.  If it does,
12081 		 * there should be an IRE for it already.
12082 		 * Don't match on ipif, only on the ill
12083 		 * since we are sharing these now.
12084 		 */
12085 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12086 		    ipif, ALL_ZONES, NULL,
12087 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12088 		if (ire == NULL) {
12089 			return (EINVAL);
12090 		} else {
12091 			ire_refrele(ire);
12092 		}
12093 	}
12094 	/*
12095 	 * Changing the broadcast addr for this ipif.
12096 	 * Make sure we have valid net and subnet bcast
12097 	 * ire's for other logical interfaces, if needed.
12098 	 */
12099 	if (addr != ipif->ipif_brd_addr)
12100 		ipif_check_bcast_ires(ipif);
12101 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12102 	return (0);
12103 }
12104 
12105 /* Get interface broadcast address. */
12106 /* ARGSUSED */
12107 int
12108 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12109     ip_ioctl_cmd_t *ipip, void *if_req)
12110 {
12111 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12112 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12113 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12114 		return (EADDRNOTAVAIL);
12115 
12116 	/* IPIF_BROADCAST not possible with IPv6 */
12117 	ASSERT(!ipif->ipif_isv6);
12118 	*sin = sin_null;
12119 	sin->sin_family = AF_INET;
12120 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12121 	return (0);
12122 }
12123 
12124 /*
12125  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12126  */
12127 /* ARGSUSED */
12128 int
12129 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12130     ip_ioctl_cmd_t *ipip, void *if_req)
12131 {
12132 	int err = 0;
12133 	in6_addr_t v6mask;
12134 
12135 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12136 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12137 
12138 	ASSERT(IAM_WRITER_IPIF(ipif));
12139 
12140 	if (ipif->ipif_isv6) {
12141 		sin6_t *sin6;
12142 
12143 		if (sin->sin_family != AF_INET6)
12144 			return (EAFNOSUPPORT);
12145 
12146 		sin6 = (sin6_t *)sin;
12147 		v6mask = sin6->sin6_addr;
12148 	} else {
12149 		ipaddr_t mask;
12150 
12151 		if (sin->sin_family != AF_INET)
12152 			return (EAFNOSUPPORT);
12153 
12154 		mask = sin->sin_addr.s_addr;
12155 		V4MASK_TO_V6(mask, v6mask);
12156 	}
12157 
12158 	/*
12159 	 * No big deal if the interface isn't already up, or the mask
12160 	 * isn't really changing, or this is pt-pt.
12161 	 */
12162 	if (!(ipif->ipif_flags & IPIF_UP) ||
12163 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12164 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12165 		ipif->ipif_v6net_mask = v6mask;
12166 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12167 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12168 			    ipif->ipif_v6net_mask,
12169 			    ipif->ipif_v6subnet);
12170 		}
12171 		return (0);
12172 	}
12173 	/*
12174 	 * Make sure we have valid net and subnet broadcast ire's
12175 	 * for the old netmask, if needed by other logical interfaces.
12176 	 */
12177 	if (!ipif->ipif_isv6)
12178 		ipif_check_bcast_ires(ipif);
12179 
12180 	err = ipif_logical_down(ipif, q, mp);
12181 	if (err == EINPROGRESS)
12182 		return (err);
12183 	ipif_down_tail(ipif);
12184 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12185 	return (err);
12186 }
12187 
12188 static int
12189 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12190 {
12191 	in6_addr_t v6mask;
12192 	int err = 0;
12193 
12194 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12195 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12196 
12197 	if (ipif->ipif_isv6) {
12198 		sin6_t *sin6;
12199 
12200 		sin6 = (sin6_t *)sin;
12201 		v6mask = sin6->sin6_addr;
12202 	} else {
12203 		ipaddr_t mask;
12204 
12205 		mask = sin->sin_addr.s_addr;
12206 		V4MASK_TO_V6(mask, v6mask);
12207 	}
12208 
12209 	ipif->ipif_v6net_mask = v6mask;
12210 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12211 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12212 		    ipif->ipif_v6subnet);
12213 	}
12214 	err = ipif_up(ipif, q, mp);
12215 
12216 	if (err == 0 || err == EINPROGRESS) {
12217 		/*
12218 		 * The interface must be DL_BOUND if this packet has to
12219 		 * go out on the wire. Since we only go through a logical
12220 		 * down and are bound with the driver during an internal
12221 		 * down/up that is satisfied.
12222 		 */
12223 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12224 			/* Potentially broadcast an address mask reply. */
12225 			ipif_mask_reply(ipif);
12226 		}
12227 	}
12228 	return (err);
12229 }
12230 
12231 /* ARGSUSED */
12232 int
12233 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12234     ip_ioctl_cmd_t *ipip, void *if_req)
12235 {
12236 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12237 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12238 	ipif_down_tail(ipif);
12239 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12240 }
12241 
12242 /* Get interface net mask. */
12243 /* ARGSUSED */
12244 int
12245 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12246     ip_ioctl_cmd_t *ipip, void *if_req)
12247 {
12248 	struct lifreq *lifr = (struct lifreq *)if_req;
12249 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12250 
12251 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12252 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12253 
12254 	/*
12255 	 * net mask can't change since we have a reference to the ipif.
12256 	 */
12257 	if (ipif->ipif_isv6) {
12258 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12259 		*sin6 = sin6_null;
12260 		sin6->sin6_family = AF_INET6;
12261 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12262 		lifr->lifr_addrlen =
12263 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12264 	} else {
12265 		*sin = sin_null;
12266 		sin->sin_family = AF_INET;
12267 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12268 		if (ipip->ipi_cmd_type == LIF_CMD) {
12269 			lifr->lifr_addrlen =
12270 			    ip_mask_to_plen(ipif->ipif_net_mask);
12271 		}
12272 	}
12273 	return (0);
12274 }
12275 
12276 /* ARGSUSED */
12277 int
12278 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12279     ip_ioctl_cmd_t *ipip, void *if_req)
12280 {
12281 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12282 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12283 
12284 	/*
12285 	 * Since no applications should ever be setting metrics on underlying
12286 	 * interfaces, we explicitly fail to smoke 'em out.
12287 	 */
12288 	if (IS_UNDER_IPMP(ipif->ipif_ill))
12289 		return (EINVAL);
12290 
12291 	/*
12292 	 * Set interface metric.  We don't use this for
12293 	 * anything but we keep track of it in case it is
12294 	 * important to routing applications or such.
12295 	 */
12296 	if (ipip->ipi_cmd_type == IF_CMD) {
12297 		struct ifreq    *ifr;
12298 
12299 		ifr = (struct ifreq *)if_req;
12300 		ipif->ipif_metric = ifr->ifr_metric;
12301 	} else {
12302 		struct lifreq   *lifr;
12303 
12304 		lifr = (struct lifreq *)if_req;
12305 		ipif->ipif_metric = lifr->lifr_metric;
12306 	}
12307 	return (0);
12308 }
12309 
12310 /* ARGSUSED */
12311 int
12312 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12313     ip_ioctl_cmd_t *ipip, void *if_req)
12314 {
12315 	/* Get interface metric. */
12316 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12317 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12318 
12319 	if (ipip->ipi_cmd_type == IF_CMD) {
12320 		struct ifreq    *ifr;
12321 
12322 		ifr = (struct ifreq *)if_req;
12323 		ifr->ifr_metric = ipif->ipif_metric;
12324 	} else {
12325 		struct lifreq   *lifr;
12326 
12327 		lifr = (struct lifreq *)if_req;
12328 		lifr->lifr_metric = ipif->ipif_metric;
12329 	}
12330 
12331 	return (0);
12332 }
12333 
12334 /* ARGSUSED */
12335 int
12336 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12337     ip_ioctl_cmd_t *ipip, void *if_req)
12338 {
12339 
12340 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12341 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12342 	/*
12343 	 * Set the muxid returned from I_PLINK.
12344 	 */
12345 	if (ipip->ipi_cmd_type == IF_CMD) {
12346 		struct ifreq *ifr = (struct ifreq *)if_req;
12347 
12348 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12349 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12350 	} else {
12351 		struct lifreq *lifr = (struct lifreq *)if_req;
12352 
12353 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12354 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12355 	}
12356 	return (0);
12357 }
12358 
12359 /* ARGSUSED */
12360 int
12361 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12362     ip_ioctl_cmd_t *ipip, void *if_req)
12363 {
12364 
12365 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12366 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12367 	/*
12368 	 * Get the muxid saved in ill for I_PUNLINK.
12369 	 */
12370 	if (ipip->ipi_cmd_type == IF_CMD) {
12371 		struct ifreq *ifr = (struct ifreq *)if_req;
12372 
12373 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12374 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12375 	} else {
12376 		struct lifreq *lifr = (struct lifreq *)if_req;
12377 
12378 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12379 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12380 	}
12381 	return (0);
12382 }
12383 
12384 /*
12385  * Set the subnet prefix. Does not modify the broadcast address.
12386  */
12387 /* ARGSUSED */
12388 int
12389 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12390     ip_ioctl_cmd_t *ipip, void *if_req)
12391 {
12392 	int err = 0;
12393 	in6_addr_t v6addr;
12394 	in6_addr_t v6mask;
12395 	boolean_t need_up = B_FALSE;
12396 	int addrlen;
12397 
12398 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12399 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12400 
12401 	ASSERT(IAM_WRITER_IPIF(ipif));
12402 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12403 
12404 	if (ipif->ipif_isv6) {
12405 		sin6_t *sin6;
12406 
12407 		if (sin->sin_family != AF_INET6)
12408 			return (EAFNOSUPPORT);
12409 
12410 		sin6 = (sin6_t *)sin;
12411 		v6addr = sin6->sin6_addr;
12412 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12413 			return (EADDRNOTAVAIL);
12414 	} else {
12415 		ipaddr_t addr;
12416 
12417 		if (sin->sin_family != AF_INET)
12418 			return (EAFNOSUPPORT);
12419 
12420 		addr = sin->sin_addr.s_addr;
12421 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12422 			return (EADDRNOTAVAIL);
12423 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12424 		/* Add 96 bits */
12425 		addrlen += IPV6_ABITS - IP_ABITS;
12426 	}
12427 
12428 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12429 		return (EINVAL);
12430 
12431 	/* Check if bits in the address is set past the mask */
12432 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12433 		return (EINVAL);
12434 
12435 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12436 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12437 		return (0);	/* No change */
12438 
12439 	if (ipif->ipif_flags & IPIF_UP) {
12440 		/*
12441 		 * If the interface is already marked up,
12442 		 * we call ipif_down which will take care
12443 		 * of ditching any IREs that have been set
12444 		 * up based on the old interface address.
12445 		 */
12446 		err = ipif_logical_down(ipif, q, mp);
12447 		if (err == EINPROGRESS)
12448 			return (err);
12449 		ipif_down_tail(ipif);
12450 		need_up = B_TRUE;
12451 	}
12452 
12453 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12454 	return (err);
12455 }
12456 
12457 static int
12458 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12459     queue_t *q, mblk_t *mp, boolean_t need_up)
12460 {
12461 	ill_t	*ill = ipif->ipif_ill;
12462 	int	err = 0;
12463 
12464 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12465 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12466 
12467 	/* Set the new address. */
12468 	mutex_enter(&ill->ill_lock);
12469 	ipif->ipif_v6net_mask = v6mask;
12470 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12471 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12472 		    ipif->ipif_v6subnet);
12473 	}
12474 	mutex_exit(&ill->ill_lock);
12475 
12476 	if (need_up) {
12477 		/*
12478 		 * Now bring the interface back up.  If this
12479 		 * is the only IPIF for the ILL, ipif_up
12480 		 * will have to re-bind to the device, so
12481 		 * we may get back EINPROGRESS, in which
12482 		 * case, this IOCTL will get completed in
12483 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12484 		 */
12485 		err = ipif_up(ipif, q, mp);
12486 		if (err == EINPROGRESS)
12487 			return (err);
12488 	}
12489 	return (err);
12490 }
12491 
12492 /* ARGSUSED */
12493 int
12494 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12495     ip_ioctl_cmd_t *ipip, void *if_req)
12496 {
12497 	int	addrlen;
12498 	in6_addr_t v6addr;
12499 	in6_addr_t v6mask;
12500 	struct lifreq *lifr = (struct lifreq *)if_req;
12501 
12502 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12503 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12504 	ipif_down_tail(ipif);
12505 
12506 	addrlen = lifr->lifr_addrlen;
12507 	if (ipif->ipif_isv6) {
12508 		sin6_t *sin6;
12509 
12510 		sin6 = (sin6_t *)sin;
12511 		v6addr = sin6->sin6_addr;
12512 	} else {
12513 		ipaddr_t addr;
12514 
12515 		addr = sin->sin_addr.s_addr;
12516 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12517 		addrlen += IPV6_ABITS - IP_ABITS;
12518 	}
12519 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12520 
12521 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12522 }
12523 
12524 /* ARGSUSED */
12525 int
12526 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12527     ip_ioctl_cmd_t *ipip, void *if_req)
12528 {
12529 	struct lifreq *lifr = (struct lifreq *)if_req;
12530 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12531 
12532 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12533 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12534 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12535 
12536 	if (ipif->ipif_isv6) {
12537 		*sin6 = sin6_null;
12538 		sin6->sin6_family = AF_INET6;
12539 		sin6->sin6_addr = ipif->ipif_v6subnet;
12540 		lifr->lifr_addrlen =
12541 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12542 	} else {
12543 		*sin = sin_null;
12544 		sin->sin_family = AF_INET;
12545 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12546 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12547 	}
12548 	return (0);
12549 }
12550 
12551 /*
12552  * Set the IPv6 address token.
12553  */
12554 /* ARGSUSED */
12555 int
12556 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12557     ip_ioctl_cmd_t *ipi, void *if_req)
12558 {
12559 	ill_t *ill = ipif->ipif_ill;
12560 	int err;
12561 	in6_addr_t v6addr;
12562 	in6_addr_t v6mask;
12563 	boolean_t need_up = B_FALSE;
12564 	int i;
12565 	sin6_t *sin6 = (sin6_t *)sin;
12566 	struct lifreq *lifr = (struct lifreq *)if_req;
12567 	int addrlen;
12568 
12569 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12570 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12571 	ASSERT(IAM_WRITER_IPIF(ipif));
12572 
12573 	addrlen = lifr->lifr_addrlen;
12574 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12575 	if (ipif->ipif_id != 0)
12576 		return (EINVAL);
12577 
12578 	if (!ipif->ipif_isv6)
12579 		return (EINVAL);
12580 
12581 	if (addrlen > IPV6_ABITS)
12582 		return (EINVAL);
12583 
12584 	v6addr = sin6->sin6_addr;
12585 
12586 	/*
12587 	 * The length of the token is the length from the end.  To get
12588 	 * the proper mask for this, compute the mask of the bits not
12589 	 * in the token; ie. the prefix, and then xor to get the mask.
12590 	 */
12591 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12592 		return (EINVAL);
12593 	for (i = 0; i < 4; i++) {
12594 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12595 	}
12596 
12597 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12598 	    ill->ill_token_length == addrlen)
12599 		return (0);	/* No change */
12600 
12601 	if (ipif->ipif_flags & IPIF_UP) {
12602 		err = ipif_logical_down(ipif, q, mp);
12603 		if (err == EINPROGRESS)
12604 			return (err);
12605 		ipif_down_tail(ipif);
12606 		need_up = B_TRUE;
12607 	}
12608 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12609 	return (err);
12610 }
12611 
12612 static int
12613 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12614     mblk_t *mp, boolean_t need_up)
12615 {
12616 	in6_addr_t v6addr;
12617 	in6_addr_t v6mask;
12618 	ill_t	*ill = ipif->ipif_ill;
12619 	int	i;
12620 	int	err = 0;
12621 
12622 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12623 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12624 	v6addr = sin6->sin6_addr;
12625 	/*
12626 	 * The length of the token is the length from the end.  To get
12627 	 * the proper mask for this, compute the mask of the bits not
12628 	 * in the token; ie. the prefix, and then xor to get the mask.
12629 	 */
12630 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12631 	for (i = 0; i < 4; i++)
12632 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12633 
12634 	mutex_enter(&ill->ill_lock);
12635 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12636 	ill->ill_token_length = addrlen;
12637 	mutex_exit(&ill->ill_lock);
12638 
12639 	if (need_up) {
12640 		/*
12641 		 * Now bring the interface back up.  If this
12642 		 * is the only IPIF for the ILL, ipif_up
12643 		 * will have to re-bind to the device, so
12644 		 * we may get back EINPROGRESS, in which
12645 		 * case, this IOCTL will get completed in
12646 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12647 		 */
12648 		err = ipif_up(ipif, q, mp);
12649 		if (err == EINPROGRESS)
12650 			return (err);
12651 	}
12652 	return (err);
12653 }
12654 
12655 /* ARGSUSED */
12656 int
12657 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12658     ip_ioctl_cmd_t *ipi, void *if_req)
12659 {
12660 	ill_t *ill;
12661 	sin6_t *sin6 = (sin6_t *)sin;
12662 	struct lifreq *lifr = (struct lifreq *)if_req;
12663 
12664 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12665 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12666 	if (ipif->ipif_id != 0)
12667 		return (EINVAL);
12668 
12669 	ill = ipif->ipif_ill;
12670 	if (!ill->ill_isv6)
12671 		return (ENXIO);
12672 
12673 	*sin6 = sin6_null;
12674 	sin6->sin6_family = AF_INET6;
12675 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12676 	sin6->sin6_addr = ill->ill_token;
12677 	lifr->lifr_addrlen = ill->ill_token_length;
12678 	return (0);
12679 }
12680 
12681 /*
12682  * Set (hardware) link specific information that might override
12683  * what was acquired through the DL_INFO_ACK.
12684  * The logic is as follows.
12685  *
12686  * become exclusive
12687  * set CHANGING flag
12688  * change mtu on affected IREs
12689  * clear CHANGING flag
12690  *
12691  * An ire add that occurs before the CHANGING flag is set will have its mtu
12692  * changed by the ip_sioctl_lnkinfo.
12693  *
12694  * During the time the CHANGING flag is set, no new ires will be added to the
12695  * bucket, and ire add will fail (due the CHANGING flag).
12696  *
12697  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12698  * before it is added to the bucket.
12699  *
12700  * Obviously only 1 thread can set the CHANGING flag and we need to become
12701  * exclusive to set the flag.
12702  */
12703 /* ARGSUSED */
12704 int
12705 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12706     ip_ioctl_cmd_t *ipi, void *if_req)
12707 {
12708 	ill_t		*ill = ipif->ipif_ill;
12709 	ipif_t		*nipif;
12710 	int		ip_min_mtu;
12711 	boolean_t	mtu_walk = B_FALSE;
12712 	struct lifreq	*lifr = (struct lifreq *)if_req;
12713 	lif_ifinfo_req_t *lir;
12714 	ire_t		*ire;
12715 
12716 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12717 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12718 	lir = &lifr->lifr_ifinfo;
12719 	ASSERT(IAM_WRITER_IPIF(ipif));
12720 
12721 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12722 	if (ipif->ipif_id != 0)
12723 		return (EINVAL);
12724 
12725 	/* Set interface MTU. */
12726 	if (ipif->ipif_isv6)
12727 		ip_min_mtu = IPV6_MIN_MTU;
12728 	else
12729 		ip_min_mtu = IP_MIN_MTU;
12730 
12731 	/*
12732 	 * Verify values before we set anything. Allow zero to
12733 	 * mean unspecified.
12734 	 */
12735 	if (lir->lir_maxmtu != 0 &&
12736 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12737 	    lir->lir_maxmtu < ip_min_mtu))
12738 		return (EINVAL);
12739 	if (lir->lir_reachtime != 0 &&
12740 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12741 		return (EINVAL);
12742 	if (lir->lir_reachretrans != 0 &&
12743 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12744 		return (EINVAL);
12745 
12746 	mutex_enter(&ill->ill_lock);
12747 	ill->ill_state_flags |= ILL_CHANGING;
12748 	for (nipif = ill->ill_ipif; nipif != NULL;
12749 	    nipif = nipif->ipif_next) {
12750 		nipif->ipif_state_flags |= IPIF_CHANGING;
12751 	}
12752 
12753 	if (lir->lir_maxmtu != 0) {
12754 		ill->ill_max_mtu = lir->lir_maxmtu;
12755 		ill->ill_user_mtu = lir->lir_maxmtu;
12756 		mtu_walk = B_TRUE;
12757 	}
12758 	mutex_exit(&ill->ill_lock);
12759 
12760 	if (lir->lir_reachtime != 0)
12761 		ill->ill_reachable_time = lir->lir_reachtime;
12762 
12763 	if (lir->lir_reachretrans != 0)
12764 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12765 
12766 	ill->ill_max_hops = lir->lir_maxhops;
12767 
12768 	ill->ill_max_buf = ND_MAX_Q;
12769 
12770 	if (mtu_walk) {
12771 		/*
12772 		 * Set the MTU on all ipifs associated with this ill except
12773 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12774 		 */
12775 		for (nipif = ill->ill_ipif; nipif != NULL;
12776 		    nipif = nipif->ipif_next) {
12777 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12778 				continue;
12779 
12780 			nipif->ipif_mtu = ill->ill_max_mtu;
12781 
12782 			if (!(nipif->ipif_flags & IPIF_UP))
12783 				continue;
12784 
12785 			if (nipif->ipif_isv6)
12786 				ire = ipif_to_ire_v6(nipif);
12787 			else
12788 				ire = ipif_to_ire(nipif);
12789 			if (ire != NULL) {
12790 				ire->ire_max_frag = ipif->ipif_mtu;
12791 				ire_refrele(ire);
12792 			}
12793 
12794 			ire_walk_ill(MATCH_IRE_ILL, 0, ipif_mtu_change,
12795 			    nipif, ill);
12796 		}
12797 	}
12798 
12799 	mutex_enter(&ill->ill_lock);
12800 	for (nipif = ill->ill_ipif; nipif != NULL;
12801 	    nipif = nipif->ipif_next) {
12802 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
12803 	}
12804 	ILL_UNMARK_CHANGING(ill);
12805 	mutex_exit(&ill->ill_lock);
12806 
12807 	/*
12808 	 * Refresh IPMP meta-interface MTU if necessary.
12809 	 */
12810 	if (IS_UNDER_IPMP(ill))
12811 		ipmp_illgrp_refresh_mtu(ill->ill_grp);
12812 
12813 	return (0);
12814 }
12815 
12816 /* ARGSUSED */
12817 int
12818 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12819     ip_ioctl_cmd_t *ipi, void *if_req)
12820 {
12821 	struct lif_ifinfo_req *lir;
12822 	ill_t *ill = ipif->ipif_ill;
12823 
12824 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
12825 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12826 	if (ipif->ipif_id != 0)
12827 		return (EINVAL);
12828 
12829 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
12830 	lir->lir_maxhops = ill->ill_max_hops;
12831 	lir->lir_reachtime = ill->ill_reachable_time;
12832 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
12833 	lir->lir_maxmtu = ill->ill_max_mtu;
12834 
12835 	return (0);
12836 }
12837 
12838 /*
12839  * Return best guess as to the subnet mask for the specified address.
12840  * Based on the subnet masks for all the configured interfaces.
12841  *
12842  * We end up returning a zero mask in the case of default, multicast or
12843  * experimental.
12844  */
12845 static ipaddr_t
12846 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
12847 {
12848 	ipaddr_t net_mask;
12849 	ill_t	*ill;
12850 	ipif_t	*ipif;
12851 	ill_walk_context_t ctx;
12852 	ipif_t	*fallback_ipif = NULL;
12853 
12854 	net_mask = ip_net_mask(addr);
12855 	if (net_mask == 0) {
12856 		*ipifp = NULL;
12857 		return (0);
12858 	}
12859 
12860 	/* Let's check to see if this is maybe a local subnet route. */
12861 	/* this function only applies to IPv4 interfaces */
12862 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
12863 	ill = ILL_START_WALK_V4(&ctx, ipst);
12864 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
12865 		mutex_enter(&ill->ill_lock);
12866 		for (ipif = ill->ill_ipif; ipif != NULL;
12867 		    ipif = ipif->ipif_next) {
12868 			if (!IPIF_CAN_LOOKUP(ipif))
12869 				continue;
12870 			if (!(ipif->ipif_flags & IPIF_UP))
12871 				continue;
12872 			if ((ipif->ipif_subnet & net_mask) ==
12873 			    (addr & net_mask)) {
12874 				/*
12875 				 * Don't trust pt-pt interfaces if there are
12876 				 * other interfaces.
12877 				 */
12878 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
12879 					if (fallback_ipif == NULL) {
12880 						ipif_refhold_locked(ipif);
12881 						fallback_ipif = ipif;
12882 					}
12883 					continue;
12884 				}
12885 
12886 				/*
12887 				 * Fine. Just assume the same net mask as the
12888 				 * directly attached subnet interface is using.
12889 				 */
12890 				ipif_refhold_locked(ipif);
12891 				mutex_exit(&ill->ill_lock);
12892 				rw_exit(&ipst->ips_ill_g_lock);
12893 				if (fallback_ipif != NULL)
12894 					ipif_refrele(fallback_ipif);
12895 				*ipifp = ipif;
12896 				return (ipif->ipif_net_mask);
12897 			}
12898 		}
12899 		mutex_exit(&ill->ill_lock);
12900 	}
12901 	rw_exit(&ipst->ips_ill_g_lock);
12902 
12903 	*ipifp = fallback_ipif;
12904 	return ((fallback_ipif != NULL) ?
12905 	    fallback_ipif->ipif_net_mask : net_mask);
12906 }
12907 
12908 /*
12909  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
12910  */
12911 static void
12912 ip_wput_ioctl(queue_t *q, mblk_t *mp)
12913 {
12914 	IOCP	iocp;
12915 	ipft_t	*ipft;
12916 	ipllc_t	*ipllc;
12917 	mblk_t	*mp1;
12918 	cred_t	*cr;
12919 	int	error = 0;
12920 	conn_t	*connp;
12921 
12922 	ip1dbg(("ip_wput_ioctl"));
12923 	iocp = (IOCP)mp->b_rptr;
12924 	mp1 = mp->b_cont;
12925 	if (mp1 == NULL) {
12926 		iocp->ioc_error = EINVAL;
12927 		mp->b_datap->db_type = M_IOCNAK;
12928 		iocp->ioc_count = 0;
12929 		qreply(q, mp);
12930 		return;
12931 	}
12932 
12933 	/*
12934 	 * These IOCTLs provide various control capabilities to
12935 	 * upstream agents such as ULPs and processes.	There
12936 	 * are currently two such IOCTLs implemented.  They
12937 	 * are used by TCP to provide update information for
12938 	 * existing IREs and to forcibly delete an IRE for a
12939 	 * host that is not responding, thereby forcing an
12940 	 * attempt at a new route.
12941 	 */
12942 	iocp->ioc_error = EINVAL;
12943 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
12944 		goto done;
12945 
12946 	ipllc = (ipllc_t *)mp1->b_rptr;
12947 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
12948 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
12949 			break;
12950 	}
12951 	/*
12952 	 * prefer credential from mblk over ioctl;
12953 	 * see ip_sioctl_copyin_setup
12954 	 */
12955 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
12956 
12957 	/*
12958 	 * Refhold the conn in case the request gets queued up in some lookup
12959 	 */
12960 	ASSERT(CONN_Q(q));
12961 	connp = Q_TO_CONN(q);
12962 	CONN_INC_REF(connp);
12963 	if (ipft->ipft_pfi &&
12964 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
12965 	    pullupmsg(mp1, ipft->ipft_min_size))) {
12966 		error = (*ipft->ipft_pfi)(q,
12967 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
12968 	}
12969 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
12970 		/*
12971 		 * CONN_OPER_PENDING_DONE happens in the function called
12972 		 * through ipft_pfi above.
12973 		 */
12974 		return;
12975 	}
12976 
12977 	CONN_OPER_PENDING_DONE(connp);
12978 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
12979 		freemsg(mp);
12980 		return;
12981 	}
12982 	iocp->ioc_error = error;
12983 
12984 done:
12985 	mp->b_datap->db_type = M_IOCACK;
12986 	if (iocp->ioc_error)
12987 		iocp->ioc_count = 0;
12988 	qreply(q, mp);
12989 }
12990 
12991 /*
12992  * Lookup an ipif using the sequence id (ipif_seqid)
12993  */
12994 ipif_t *
12995 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
12996 {
12997 	ipif_t *ipif;
12998 
12999 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13000 
13001 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13002 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13003 			return (ipif);
13004 	}
13005 	return (NULL);
13006 }
13007 
13008 /*
13009  * Assign a unique id for the ipif. This is used later when we send
13010  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13011  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13012  * IRE is added, we verify that ipif has not disappeared.
13013  */
13014 
13015 static void
13016 ipif_assign_seqid(ipif_t *ipif)
13017 {
13018 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13019 
13020 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
13021 }
13022 
13023 /*
13024  * Clone the contents of `sipif' to `dipif'.  Requires that both ipifs are
13025  * administratively down (i.e., no DAD), of the same type, and locked.  Note
13026  * that the clone is complete -- including the seqid -- and the expectation is
13027  * that the caller will either free or overwrite `sipif' before it's unlocked.
13028  */
13029 static void
13030 ipif_clone(const ipif_t *sipif, ipif_t *dipif)
13031 {
13032 	ASSERT(MUTEX_HELD(&sipif->ipif_ill->ill_lock));
13033 	ASSERT(MUTEX_HELD(&dipif->ipif_ill->ill_lock));
13034 	ASSERT(!(sipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
13035 	ASSERT(!(dipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
13036 	ASSERT(sipif->ipif_ire_type == dipif->ipif_ire_type);
13037 	ASSERT(sipif->ipif_arp_del_mp == NULL);
13038 	ASSERT(dipif->ipif_arp_del_mp == NULL);
13039 	ASSERT(sipif->ipif_igmp_rpt == NULL);
13040 	ASSERT(dipif->ipif_igmp_rpt == NULL);
13041 	ASSERT(sipif->ipif_multicast_up == 0);
13042 	ASSERT(dipif->ipif_multicast_up == 0);
13043 	ASSERT(sipif->ipif_joined_allhosts == 0);
13044 	ASSERT(dipif->ipif_joined_allhosts == 0);
13045 
13046 	dipif->ipif_mtu = sipif->ipif_mtu;
13047 	dipif->ipif_flags = sipif->ipif_flags;
13048 	dipif->ipif_metric = sipif->ipif_metric;
13049 	dipif->ipif_zoneid = sipif->ipif_zoneid;
13050 	dipif->ipif_v6subnet = sipif->ipif_v6subnet;
13051 	dipif->ipif_v6lcl_addr = sipif->ipif_v6lcl_addr;
13052 	dipif->ipif_v6src_addr = sipif->ipif_v6src_addr;
13053 	dipif->ipif_v6net_mask = sipif->ipif_v6net_mask;
13054 	dipif->ipif_v6brd_addr = sipif->ipif_v6brd_addr;
13055 	dipif->ipif_v6pp_dst_addr = sipif->ipif_v6pp_dst_addr;
13056 
13057 	/*
13058 	 * While dipif is down right now, it might've been up before.  Since
13059 	 * it's changing identity, its packet counters need to be reset.
13060 	 */
13061 	dipif->ipif_ib_pkt_count = 0;
13062 	dipif->ipif_ob_pkt_count = 0;
13063 	dipif->ipif_fo_pkt_count = 0;
13064 
13065 	/*
13066 	 * As per the comment atop the function, we assume that these sipif
13067 	 * fields will be changed before sipif is unlocked.
13068 	 */
13069 	dipif->ipif_seqid = sipif->ipif_seqid;
13070 	dipif->ipif_saved_ire_mp = sipif->ipif_saved_ire_mp;
13071 	dipif->ipif_saved_ire_cnt = sipif->ipif_saved_ire_cnt;
13072 	dipif->ipif_state_flags = sipif->ipif_state_flags;
13073 }
13074 
13075 /*
13076  * Transfer the contents of `sipif' to `dipif', and then free (if `virgipif'
13077  * is NULL) or overwrite `sipif' with `virgipif', which must be a virgin
13078  * (unreferenced) ipif.  Also, if `sipif' is used by the current xop, then
13079  * transfer the xop to `dipif'.  Requires that all ipifs are administratively
13080  * down (i.e., no DAD), of the same type, and unlocked.
13081  */
13082 static void
13083 ipif_transfer(ipif_t *sipif, ipif_t *dipif, ipif_t *virgipif)
13084 {
13085 	ipsq_t *ipsq = sipif->ipif_ill->ill_phyint->phyint_ipsq;
13086 	int ipx_current_ioctl;
13087 
13088 	ASSERT(sipif != dipif);
13089 	ASSERT(sipif != virgipif);
13090 
13091 	/*
13092 	 * Grab all of the locks that protect the ipif in a defined order.
13093 	 */
13094 	GRAB_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
13095 	if (sipif > dipif) {
13096 		mutex_enter(&sipif->ipif_saved_ire_lock);
13097 		mutex_enter(&dipif->ipif_saved_ire_lock);
13098 	} else {
13099 		mutex_enter(&dipif->ipif_saved_ire_lock);
13100 		mutex_enter(&sipif->ipif_saved_ire_lock);
13101 	}
13102 
13103 	ipif_clone(sipif, dipif);
13104 	if (virgipif != NULL) {
13105 		ipif_clone(virgipif, sipif);
13106 		mi_free(virgipif);
13107 	}
13108 
13109 	mutex_exit(&sipif->ipif_saved_ire_lock);
13110 	mutex_exit(&dipif->ipif_saved_ire_lock);
13111 	RELEASE_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
13112 
13113 	/*
13114 	 * Transfer ownership of the current xop, if necessary.
13115 	 */
13116 	if (ipsq->ipsq_xop->ipx_current_ipif == sipif) {
13117 		ASSERT(ipsq->ipsq_xop->ipx_pending_ipif == NULL);
13118 		ipx_current_ioctl = ipsq->ipsq_xop->ipx_current_ioctl;
13119 		ipsq_current_finish(ipsq);
13120 		ipsq_current_start(ipsq, dipif, ipx_current_ioctl);
13121 	}
13122 
13123 	if (virgipif == NULL)
13124 		mi_free(sipif);
13125 }
13126 
13127 /*
13128  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13129  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13130  * be inserted into the first space available in the list. The value of
13131  * ipif_id will then be set to the appropriate value for its position.
13132  */
13133 static int
13134 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock)
13135 {
13136 	ill_t *ill;
13137 	ipif_t *tipif;
13138 	ipif_t **tipifp;
13139 	int id;
13140 	ip_stack_t	*ipst;
13141 
13142 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13143 	    IAM_WRITER_IPIF(ipif));
13144 
13145 	ill = ipif->ipif_ill;
13146 	ASSERT(ill != NULL);
13147 	ipst = ill->ill_ipst;
13148 
13149 	/*
13150 	 * In the case of lo0:0 we already hold the ill_g_lock.
13151 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13152 	 * ipif_insert.
13153 	 */
13154 	if (acquire_g_lock)
13155 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13156 	mutex_enter(&ill->ill_lock);
13157 	id = ipif->ipif_id;
13158 	tipifp = &(ill->ill_ipif);
13159 	if (id == -1) {	/* need to find a real id */
13160 		id = 0;
13161 		while ((tipif = *tipifp) != NULL) {
13162 			ASSERT(tipif->ipif_id >= id);
13163 			if (tipif->ipif_id != id)
13164 				break; /* non-consecutive id */
13165 			id++;
13166 			tipifp = &(tipif->ipif_next);
13167 		}
13168 		/* limit number of logical interfaces */
13169 		if (id >= ipst->ips_ip_addrs_per_if) {
13170 			mutex_exit(&ill->ill_lock);
13171 			if (acquire_g_lock)
13172 				rw_exit(&ipst->ips_ill_g_lock);
13173 			return (-1);
13174 		}
13175 		ipif->ipif_id = id; /* assign new id */
13176 	} else if (id < ipst->ips_ip_addrs_per_if) {
13177 		/* we have a real id; insert ipif in the right place */
13178 		while ((tipif = *tipifp) != NULL) {
13179 			ASSERT(tipif->ipif_id != id);
13180 			if (tipif->ipif_id > id)
13181 				break; /* found correct location */
13182 			tipifp = &(tipif->ipif_next);
13183 		}
13184 	} else {
13185 		mutex_exit(&ill->ill_lock);
13186 		if (acquire_g_lock)
13187 			rw_exit(&ipst->ips_ill_g_lock);
13188 		return (-1);
13189 	}
13190 
13191 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13192 
13193 	ipif->ipif_next = tipif;
13194 	*tipifp = ipif;
13195 	mutex_exit(&ill->ill_lock);
13196 	if (acquire_g_lock)
13197 		rw_exit(&ipst->ips_ill_g_lock);
13198 
13199 	return (0);
13200 }
13201 
13202 static void
13203 ipif_remove(ipif_t *ipif)
13204 {
13205 	ipif_t	**ipifp;
13206 	ill_t	*ill = ipif->ipif_ill;
13207 
13208 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13209 
13210 	mutex_enter(&ill->ill_lock);
13211 	ipifp = &ill->ill_ipif;
13212 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13213 		if (*ipifp == ipif) {
13214 			*ipifp = ipif->ipif_next;
13215 			break;
13216 		}
13217 	}
13218 	mutex_exit(&ill->ill_lock);
13219 }
13220 
13221 /*
13222  * Allocate and initialize a new interface control structure.  (Always
13223  * called as writer.)
13224  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13225  * is not part of the global linked list of ills. ipif_seqid is unique
13226  * in the system and to preserve the uniqueness, it is assigned only
13227  * when ill becomes part of the global list. At that point ill will
13228  * have a name. If it doesn't get assigned here, it will get assigned
13229  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13230  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13231  * the interface flags or any other information from the DL_INFO_ACK for
13232  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13233  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13234  * second DL_INFO_ACK comes in from the driver.
13235  */
13236 static ipif_t *
13237 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize,
13238     boolean_t insert)
13239 {
13240 	ipif_t	*ipif;
13241 	phyint_t *phyi = ill->ill_phyint;
13242 	ip_stack_t *ipst = ill->ill_ipst;
13243 
13244 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13245 	    ill->ill_name, id, (void *)ill));
13246 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13247 
13248 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13249 		return (NULL);
13250 	*ipif = ipif_zero;	/* start clean */
13251 
13252 	ipif->ipif_ill = ill;
13253 	ipif->ipif_id = id;	/* could be -1 */
13254 	/*
13255 	 * Inherit the zoneid from the ill; for the shared stack instance
13256 	 * this is always the global zone
13257 	 */
13258 	ipif->ipif_zoneid = ill->ill_zoneid;
13259 
13260 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13261 
13262 	ipif->ipif_refcnt = 0;
13263 	ipif->ipif_saved_ire_cnt = 0;
13264 
13265 	if (insert) {
13266 		if (ipif_insert(ipif, ire_type != IRE_LOOPBACK) != 0) {
13267 			mi_free(ipif);
13268 			return (NULL);
13269 		}
13270 		/* -1 id should have been replaced by real id */
13271 		id = ipif->ipif_id;
13272 		ASSERT(id >= 0);
13273 	}
13274 
13275 	if (ill->ill_name[0] != '\0')
13276 		ipif_assign_seqid(ipif);
13277 
13278 	/*
13279 	 * If this is ipif zero, configure ill/phyint-wide information.
13280 	 * Defer most configuration until we're guaranteed we're attached.
13281 	 */
13282 	if (id == 0) {
13283 		if (ill->ill_mactype == SUNW_DL_IPMP) {
13284 			/*
13285 			 * Set PHYI_IPMP and also set PHYI_FAILED since there
13286 			 * are no active interfaces.  Similarly, PHYI_RUNNING
13287 			 * isn't set until the group has an active interface.
13288 			 */
13289 			mutex_enter(&phyi->phyint_lock);
13290 			phyi->phyint_flags |= (PHYI_IPMP | PHYI_FAILED);
13291 			mutex_exit(&phyi->phyint_lock);
13292 
13293 			/*
13294 			 * Create the illgrp (which must not exist yet because
13295 			 * the zeroth ipif is created once per ill).  However,
13296 			 * do not not link it to the ipmp_grp_t until I_PLINK
13297 			 * is called; see ip_sioctl_plink_ipmp() for details.
13298 			 */
13299 			if (ipmp_illgrp_create(ill) == NULL) {
13300 				if (insert) {
13301 					rw_enter(&ipst->ips_ill_g_lock,
13302 					    RW_WRITER);
13303 					ipif_remove(ipif);
13304 					rw_exit(&ipst->ips_ill_g_lock);
13305 				}
13306 				mi_free(ipif);
13307 				return (NULL);
13308 			}
13309 		} else {
13310 			/*
13311 			 * By default, PHYI_RUNNING is set when the zeroth
13312 			 * ipif is created.  For other ipifs, we don't touch
13313 			 * it since DLPI notifications may have changed it.
13314 			 */
13315 			mutex_enter(&phyi->phyint_lock);
13316 			phyi->phyint_flags |= PHYI_RUNNING;
13317 			mutex_exit(&phyi->phyint_lock);
13318 		}
13319 	}
13320 
13321 	/*
13322 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13323 	 * The ipif is still not up and can't be looked up until the
13324 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13325 	 */
13326 	mutex_enter(&ill->ill_lock);
13327 	mutex_enter(&phyi->phyint_lock);
13328 
13329 	ipif->ipif_ire_type = ire_type;
13330 
13331 	if (ipif->ipif_isv6) {
13332 		ill->ill_flags |= ILLF_IPV6;
13333 	} else {
13334 		ipaddr_t inaddr_any = INADDR_ANY;
13335 
13336 		ill->ill_flags |= ILLF_IPV4;
13337 
13338 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13339 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13340 		    &ipif->ipif_v6lcl_addr);
13341 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13342 		    &ipif->ipif_v6src_addr);
13343 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13344 		    &ipif->ipif_v6subnet);
13345 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13346 		    &ipif->ipif_v6net_mask);
13347 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13348 		    &ipif->ipif_v6brd_addr);
13349 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13350 		    &ipif->ipif_v6pp_dst_addr);
13351 	}
13352 
13353 	/*
13354 	 * Don't set the interface flags etc. now, will do it in
13355 	 * ip_ll_subnet_defaults.
13356 	 */
13357 	if (!initialize)
13358 		goto out;
13359 
13360 	ipif->ipif_mtu = ill->ill_max_mtu;
13361 
13362 	/*
13363 	 * NOTE: The IPMP meta-interface is special-cased because it starts
13364 	 * with no underlying interfaces (and thus an unknown broadcast
13365 	 * address length), but all interfaces that can be placed into an IPMP
13366 	 * group are required to be broadcast-capable.
13367 	 */
13368 	if (ill->ill_bcast_addr_length != 0 || IS_IPMP(ill)) {
13369 		/*
13370 		 * Later detect lack of DLPI driver multicast
13371 		 * capability by catching DL_ENABMULTI errors in
13372 		 * ip_rput_dlpi.
13373 		 */
13374 		ill->ill_flags |= ILLF_MULTICAST;
13375 		if (!ipif->ipif_isv6)
13376 			ipif->ipif_flags |= IPIF_BROADCAST;
13377 	} else {
13378 		if (ill->ill_net_type != IRE_LOOPBACK) {
13379 			if (ipif->ipif_isv6)
13380 				/*
13381 				 * Note: xresolv interfaces will eventually need
13382 				 * NOARP set here as well, but that will require
13383 				 * those external resolvers to have some
13384 				 * knowledge of that flag and act appropriately.
13385 				 * Not to be changed at present.
13386 				 */
13387 				ill->ill_flags |= ILLF_NONUD;
13388 			else
13389 				ill->ill_flags |= ILLF_NOARP;
13390 		}
13391 		if (ill->ill_phys_addr_length == 0) {
13392 			if (ill->ill_mactype == SUNW_DL_VNI) {
13393 				ipif->ipif_flags |= IPIF_NOXMIT;
13394 				phyi->phyint_flags |= PHYI_VIRTUAL;
13395 			} else {
13396 				/* pt-pt supports multicast. */
13397 				ill->ill_flags |= ILLF_MULTICAST;
13398 				if (ill->ill_net_type == IRE_LOOPBACK) {
13399 					phyi->phyint_flags |=
13400 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13401 				} else {
13402 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13403 				}
13404 			}
13405 		}
13406 	}
13407 out:
13408 	mutex_exit(&phyi->phyint_lock);
13409 	mutex_exit(&ill->ill_lock);
13410 	return (ipif);
13411 }
13412 
13413 /*
13414  * If appropriate, send a message up to the resolver delete the entry
13415  * for the address of this interface which is going out of business.
13416  * (Always called as writer).
13417  *
13418  * NOTE : We need to check for NULL mps as some of the fields are
13419  *	  initialized only for some interface types. See ipif_resolver_up()
13420  *	  for details.
13421  */
13422 void
13423 ipif_resolver_down(ipif_t *ipif)
13424 {
13425 	mblk_t	*mp;
13426 	ill_t	*ill = ipif->ipif_ill;
13427 
13428 	ip1dbg(("ipif_resolver_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13429 	ASSERT(IAM_WRITER_IPIF(ipif));
13430 
13431 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13432 		return;
13433 
13434 	/* Delete the mapping for the local address */
13435 	mp = ipif->ipif_arp_del_mp;
13436 	if (mp != NULL) {
13437 		ip1dbg(("ipif_resolver_down: arp cmd %x for %s:%u\n",
13438 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13439 		putnext(ill->ill_rq, mp);
13440 		ipif->ipif_arp_del_mp = NULL;
13441 	}
13442 
13443 	/*
13444 	 * Make IPMP aware of the deleted data address.
13445 	 */
13446 	if (IS_IPMP(ill))
13447 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
13448 
13449 	/*
13450 	 * If this is the last ipif that is going down and there are no
13451 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13452 	 * clean up ARP completely.
13453 	 */
13454 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13455 		/*
13456 		 * If this was the last ipif on an IPMP interface, purge any
13457 		 * IPMP ARP entries associated with it.
13458 		 */
13459 		if (IS_IPMP(ill))
13460 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
13461 
13462 		/* Send up AR_INTERFACE_DOWN message */
13463 		mp = ill->ill_arp_down_mp;
13464 		if (mp != NULL) {
13465 			ip1dbg(("ipif_resolver_down: arp cmd %x for %s:%u\n",
13466 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13467 			    ipif->ipif_id));
13468 			putnext(ill->ill_rq, mp);
13469 			ill->ill_arp_down_mp = NULL;
13470 		}
13471 
13472 		/* Tell ARP to delete the multicast mappings */
13473 		mp = ill->ill_arp_del_mapping_mp;
13474 		if (mp != NULL) {
13475 			ip1dbg(("ipif_resolver_down: arp cmd %x for %s:%u\n",
13476 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13477 			    ipif->ipif_id));
13478 			putnext(ill->ill_rq, mp);
13479 			ill->ill_arp_del_mapping_mp = NULL;
13480 		}
13481 	}
13482 }
13483 
13484 /*
13485  * Set up the multicast mappings for `ipif' in ARP.  If `arp_add_mapping_mp'
13486  * is non-NULL, then upon success it will contain an mblk that can be passed
13487  * to ARP to create the mapping.  Otherwise, if it's NULL, upon success ARP
13488  * will have already been notified to create the mapping.  Returns zero on
13489  * success, -1 upon failure.
13490  */
13491 int
13492 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13493 {
13494 	mblk_t	*del_mp = NULL;
13495 	mblk_t *add_mp = NULL;
13496 	mblk_t *mp;
13497 	ill_t	*ill = ipif->ipif_ill;
13498 	phyint_t *phyi = ill->ill_phyint;
13499 	ipaddr_t addr, mask, extract_mask = 0;
13500 	arma_t	*arma;
13501 	uint8_t *maddr, *bphys_addr;
13502 	uint32_t hw_start;
13503 	dl_unitdata_req_t *dlur;
13504 
13505 	ASSERT(IAM_WRITER_IPIF(ipif));
13506 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13507 		return (0);
13508 
13509 	/*
13510 	 * IPMP meta-interfaces don't have any inherent multicast mappings,
13511 	 * and instead use the ones on the underlying interfaces.
13512 	 */
13513 	if (IS_IPMP(ill))
13514 		return (0);
13515 
13516 	/*
13517 	 * Delete the existing mapping from ARP.  Normally, ipif_down() ->
13518 	 * ipif_resolver_down() will send this up to ARP, but it may be that
13519 	 * we are enabling PHYI_MULTI_BCAST via ip_rput_dlpi_writer().
13520 	 */
13521 	mp = ill->ill_arp_del_mapping_mp;
13522 	if (mp != NULL) {
13523 		ip1dbg(("ipif_arp_setup_multicast: arp cmd %x for %s:%u\n",
13524 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13525 		putnext(ill->ill_rq, mp);
13526 		ill->ill_arp_del_mapping_mp = NULL;
13527 	}
13528 
13529 	if (arp_add_mapping_mp != NULL)
13530 		*arp_add_mapping_mp = NULL;
13531 
13532 	/*
13533 	 * Check that the address is not to long for the constant
13534 	 * length reserved in the template arma_t.
13535 	 */
13536 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13537 		return (-1);
13538 
13539 	/* Add mapping mblk */
13540 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13541 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13542 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13543 	    (caddr_t)&addr);
13544 	if (add_mp == NULL)
13545 		return (-1);
13546 	arma = (arma_t *)add_mp->b_rptr;
13547 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13548 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13549 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13550 
13551 	/*
13552 	 * Determine the broadcast address.
13553 	 */
13554 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13555 	if (ill->ill_sap_length < 0)
13556 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13557 	else
13558 		bphys_addr = (uchar_t *)dlur +
13559 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13560 	/*
13561 	 * Check PHYI_MULTI_BCAST and length of physical
13562 	 * address to determine if we use the mapping or the
13563 	 * broadcast address.
13564 	 */
13565 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13566 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13567 		    bphys_addr, maddr, &hw_start, &extract_mask))
13568 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13569 
13570 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13571 	    (ill->ill_flags & ILLF_MULTICAST)) {
13572 		/* Make sure this will not match the "exact" entry. */
13573 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13574 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13575 		    (caddr_t)&addr);
13576 		if (del_mp == NULL) {
13577 			freemsg(add_mp);
13578 			return (-1);
13579 		}
13580 		bcopy(&extract_mask, (char *)arma +
13581 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13582 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13583 			/* Use link-layer broadcast address for MULTI_BCAST */
13584 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13585 			ip2dbg(("ipif_arp_setup_multicast: adding"
13586 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13587 		} else {
13588 			arma->arma_hw_mapping_start = hw_start;
13589 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13590 			    " ARP setup for %s\n", ill->ill_name));
13591 		}
13592 	} else {
13593 		freemsg(add_mp);
13594 		ASSERT(del_mp == NULL);
13595 		/* It is neither MULTICAST nor MULTI_BCAST */
13596 		return (0);
13597 	}
13598 	ASSERT(add_mp != NULL && del_mp != NULL);
13599 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13600 	ill->ill_arp_del_mapping_mp = del_mp;
13601 	if (arp_add_mapping_mp != NULL) {
13602 		/* The caller just wants the mblks allocated */
13603 		*arp_add_mapping_mp = add_mp;
13604 	} else {
13605 		/* The caller wants us to send it to arp */
13606 		putnext(ill->ill_rq, add_mp);
13607 	}
13608 	return (0);
13609 }
13610 
13611 /*
13612  * Get the resolver set up for a new IP address.  (Always called as writer.)
13613  * Called both for IPv4 and IPv6 interfaces, though it only sets up the
13614  * resolver for v6 if it's an ILLF_XRESOLV interface.  Honors ILLF_NOARP.
13615  *
13616  * The enumerated value res_act tunes the behavior:
13617  * 	* Res_act_initial: set up all the resolver structures for a new
13618  *	  IP address.
13619  *	* Res_act_defend: tell ARP that it needs to send a single gratuitous
13620  *	  ARP message in defense of the address.
13621  *	* Res_act_rebind: tell ARP to change the hardware address for an IP
13622  *	  address (and issue gratuitous ARPs).  Used by ipmp_ill_bind_ipif().
13623  *
13624  * Returns zero on success, or an errno upon failure.
13625  */
13626 int
13627 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13628 {
13629 	mblk_t	*arp_up_mp = NULL;
13630 	mblk_t	*arp_down_mp = NULL;
13631 	mblk_t	*arp_add_mp = NULL;
13632 	mblk_t	*arp_del_mp = NULL;
13633 	mblk_t	*arp_add_mapping_mp = NULL;
13634 	mblk_t	*arp_del_mapping_mp = NULL;
13635 	ill_t	*ill = ipif->ipif_ill;
13636 	int	err = ENOMEM;
13637 	boolean_t added_ipif = B_FALSE;
13638 	boolean_t publish;
13639 	boolean_t was_dup;
13640 
13641 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13642 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13643 	ASSERT(IAM_WRITER_IPIF(ipif));
13644 
13645 	was_dup = B_FALSE;
13646 	if (res_act == Res_act_initial) {
13647 		ipif->ipif_addr_ready = 0;
13648 		/*
13649 		 * We're bringing an interface up here.  There's no way that we
13650 		 * should need to shut down ARP now.
13651 		 */
13652 		mutex_enter(&ill->ill_lock);
13653 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13654 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13655 			ill->ill_ipif_dup_count--;
13656 			was_dup = B_TRUE;
13657 		}
13658 		mutex_exit(&ill->ill_lock);
13659 	}
13660 	if (ipif->ipif_recovery_id != 0)
13661 		(void) untimeout(ipif->ipif_recovery_id);
13662 	ipif->ipif_recovery_id = 0;
13663 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13664 		ipif->ipif_addr_ready = 1;
13665 		return (0);
13666 	}
13667 	/* NDP will set the ipif_addr_ready flag when it's ready */
13668 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13669 		return (0);
13670 
13671 	if (ill->ill_isv6) {
13672 		/*
13673 		 * External resolver for IPv6
13674 		 */
13675 		ASSERT(res_act == Res_act_initial);
13676 		publish = !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr);
13677 	} else {
13678 		/*
13679 		 * IPv4 arp case. If the ARP stream has already started
13680 		 * closing, fail this request for ARP bringup. Else
13681 		 * record the fact that an ARP bringup is pending.
13682 		 */
13683 		mutex_enter(&ill->ill_lock);
13684 		if (ill->ill_arp_closing) {
13685 			mutex_exit(&ill->ill_lock);
13686 			err = EINVAL;
13687 			goto failed;
13688 		} else {
13689 			if (ill->ill_ipif_up_count == 0 &&
13690 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13691 				ill->ill_arp_bringup_pending = 1;
13692 			mutex_exit(&ill->ill_lock);
13693 		}
13694 		publish = (ipif->ipif_lcl_addr != INADDR_ANY);
13695 	}
13696 
13697 	if (IS_IPMP(ill) && publish) {
13698 		/*
13699 		 * If we're here via ipif_up(), then the ipif won't be bound
13700 		 * yet -- add it to the group, which will bind it if possible.
13701 		 * (We would add it in ipif_up(), but deleting on failure
13702 		 * there is gruesome.)  If we're here via ipmp_ill_bind_ipif(),
13703 		 * then the ipif has already been added to the group and we
13704 		 * just need to use the binding.
13705 		 */
13706 		if (ipmp_ipif_bound_ill(ipif) == NULL) {
13707 			if (ipmp_illgrp_add_ipif(ill->ill_grp, ipif) == NULL) {
13708 				/*
13709 				 * We couldn't bind the ipif to an ill yet,
13710 				 * so we have nothing to publish.
13711 				 */
13712 				publish = B_FALSE;
13713 			}
13714 			added_ipif = B_TRUE;
13715 		}
13716 	}
13717 
13718 	/*
13719 	 * Add an entry for the local address in ARP only if it
13720 	 * is not UNNUMBERED and it is suitable for publishing.
13721 	 */
13722 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && publish) {
13723 		if (res_act == Res_act_defend) {
13724 			arp_add_mp = ipif_area_alloc(ipif, ACE_F_DEFEND);
13725 			if (arp_add_mp == NULL)
13726 				goto failed;
13727 			/*
13728 			 * If we're just defending our address now, then
13729 			 * there's no need to set up ARP multicast mappings.
13730 			 * The publish command is enough.
13731 			 */
13732 			goto done;
13733 		}
13734 
13735 		/*
13736 		 * Allocate an ARP add message and an ARP delete message (the
13737 		 * latter is saved for use when the address goes down).
13738 		 */
13739 		if ((arp_add_mp = ipif_area_alloc(ipif, 0)) == NULL)
13740 			goto failed;
13741 
13742 		if ((arp_del_mp = ipif_ared_alloc(ipif)) == NULL)
13743 			goto failed;
13744 
13745 		if (res_act != Res_act_initial)
13746 			goto arp_setup_multicast;
13747 	} else {
13748 		if (res_act != Res_act_initial)
13749 			goto done;
13750 	}
13751 	/*
13752 	 * Need to bring up ARP or setup multicast mapping only
13753 	 * when the first interface is coming UP.
13754 	 */
13755 	if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0 || was_dup)
13756 		goto done;
13757 
13758 	/*
13759 	 * Allocate an ARP down message (to be saved) and an ARP up message.
13760 	 */
13761 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13762 	if (arp_down_mp == NULL)
13763 		goto failed;
13764 
13765 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13766 	if (arp_up_mp == NULL)
13767 		goto failed;
13768 
13769 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13770 		goto done;
13771 
13772 arp_setup_multicast:
13773 	/*
13774 	 * Setup the multicast mappings. This function initializes
13775 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13776 	 * IPv6, or for the IPMP interface (since it has no link-layer).
13777 	 */
13778 	if (!ill->ill_isv6 && !IS_IPMP(ill)) {
13779 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13780 		if (err != 0)
13781 			goto failed;
13782 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13783 		ASSERT(arp_add_mapping_mp != NULL);
13784 	}
13785 done:
13786 	if (arp_up_mp != NULL) {
13787 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13788 		    ill->ill_name, ipif->ipif_id));
13789 		putnext(ill->ill_rq, arp_up_mp);
13790 		arp_up_mp = NULL;
13791 	}
13792 	if (arp_add_mp != NULL) {
13793 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13794 		    ill->ill_name, ipif->ipif_id));
13795 		/*
13796 		 * If it's an extended ARP implementation, then we'll wait to
13797 		 * hear that DAD has finished before using the interface.
13798 		 */
13799 		if (!ill->ill_arp_extend)
13800 			ipif->ipif_addr_ready = 1;
13801 		putnext(ill->ill_rq, arp_add_mp);
13802 		arp_add_mp = NULL;
13803 	} else {
13804 		ipif->ipif_addr_ready = 1;
13805 	}
13806 	if (arp_add_mapping_mp != NULL) {
13807 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13808 		    ill->ill_name, ipif->ipif_id));
13809 		putnext(ill->ill_rq, arp_add_mapping_mp);
13810 		arp_add_mapping_mp = NULL;
13811 	}
13812 
13813 	if (res_act == Res_act_initial) {
13814 		if (ill->ill_flags & ILLF_NOARP)
13815 			err = ill_arp_off(ill);
13816 		else
13817 			err = ill_arp_on(ill);
13818 		if (err != 0) {
13819 			ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n",
13820 			    err));
13821 			goto failed;
13822 		}
13823 	}
13824 
13825 	if (arp_del_mp != NULL) {
13826 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13827 		ipif->ipif_arp_del_mp = arp_del_mp;
13828 	}
13829 	if (arp_down_mp != NULL) {
13830 		ASSERT(ill->ill_arp_down_mp == NULL);
13831 		ill->ill_arp_down_mp = arp_down_mp;
13832 	}
13833 	if (arp_del_mapping_mp != NULL) {
13834 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13835 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13836 	}
13837 
13838 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
13839 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
13840 failed:
13841 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13842 	if (added_ipif)
13843 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
13844 	freemsg(arp_add_mp);
13845 	freemsg(arp_del_mp);
13846 	freemsg(arp_add_mapping_mp);
13847 	freemsg(arp_up_mp);
13848 	freemsg(arp_down_mp);
13849 	ill->ill_arp_bringup_pending = 0;
13850 	return (err);
13851 }
13852 
13853 /*
13854  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
13855  * just gone back up.
13856  */
13857 static void
13858 ipif_arp_start_dad(ipif_t *ipif)
13859 {
13860 	ill_t *ill = ipif->ipif_ill;
13861 	mblk_t *arp_add_mp;
13862 
13863 	/* ACE_F_UNVERIFIED restarts DAD */
13864 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
13865 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13866 	    ipif->ipif_lcl_addr == INADDR_ANY ||
13867 	    (arp_add_mp = ipif_area_alloc(ipif, ACE_F_UNVERIFIED)) == NULL) {
13868 		/*
13869 		 * If we can't contact ARP for some reason, that's not really a
13870 		 * problem.  Just send out the routing socket notification that
13871 		 * DAD completion would have done, and continue.
13872 		 */
13873 		ipif_mask_reply(ipif);
13874 		ipif_up_notify(ipif);
13875 		ipif->ipif_addr_ready = 1;
13876 		return;
13877 	}
13878 
13879 	putnext(ill->ill_rq, arp_add_mp);
13880 }
13881 
13882 static void
13883 ipif_ndp_start_dad(ipif_t *ipif)
13884 {
13885 	nce_t *nce;
13886 
13887 	nce = ndp_lookup_v6(ipif->ipif_ill, B_TRUE, &ipif->ipif_v6lcl_addr,
13888 	    B_FALSE);
13889 	if (nce == NULL)
13890 		return;
13891 
13892 	if (!ndp_restart_dad(nce)) {
13893 		/*
13894 		 * If we can't restart DAD for some reason, that's not really a
13895 		 * problem.  Just send out the routing socket notification that
13896 		 * DAD completion would have done, and continue.
13897 		 */
13898 		ipif_up_notify(ipif);
13899 		ipif->ipif_addr_ready = 1;
13900 	}
13901 	NCE_REFRELE(nce);
13902 }
13903 
13904 /*
13905  * Restart duplicate address detection on all interfaces on the given ill.
13906  *
13907  * This is called when an interface transitions from down to up
13908  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
13909  *
13910  * Note that since the underlying physical link has transitioned, we must cause
13911  * at least one routing socket message to be sent here, either via DAD
13912  * completion or just by default on the first ipif.  (If we don't do this, then
13913  * in.mpathd will see long delays when doing link-based failure recovery.)
13914  */
13915 void
13916 ill_restart_dad(ill_t *ill, boolean_t went_up)
13917 {
13918 	ipif_t *ipif;
13919 
13920 	if (ill == NULL)
13921 		return;
13922 
13923 	/*
13924 	 * If layer two doesn't support duplicate address detection, then just
13925 	 * send the routing socket message now and be done with it.
13926 	 */
13927 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
13928 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
13929 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
13930 		return;
13931 	}
13932 
13933 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13934 		if (went_up) {
13935 			if (ipif->ipif_flags & IPIF_UP) {
13936 				if (ill->ill_isv6)
13937 					ipif_ndp_start_dad(ipif);
13938 				else
13939 					ipif_arp_start_dad(ipif);
13940 			} else if (ill->ill_isv6 &&
13941 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
13942 				/*
13943 				 * For IPv4, the ARP module itself will
13944 				 * automatically start the DAD process when it
13945 				 * sees DL_NOTE_LINK_UP.  We respond to the
13946 				 * AR_CN_READY at the completion of that task.
13947 				 * For IPv6, we must kick off the bring-up
13948 				 * process now.
13949 				 */
13950 				ndp_do_recovery(ipif);
13951 			} else {
13952 				/*
13953 				 * Unfortunately, the first ipif is "special"
13954 				 * and represents the underlying ill in the
13955 				 * routing socket messages.  Thus, when this
13956 				 * one ipif is down, we must still notify so
13957 				 * that the user knows the IFF_RUNNING status
13958 				 * change.  (If the first ipif is up, then
13959 				 * we'll handle eventual routing socket
13960 				 * notification via DAD completion.)
13961 				 */
13962 				if (ipif == ill->ill_ipif) {
13963 					ip_rts_ifmsg(ill->ill_ipif,
13964 					    RTSQ_DEFAULT);
13965 				}
13966 			}
13967 		} else {
13968 			/*
13969 			 * After link down, we'll need to send a new routing
13970 			 * message when the link comes back, so clear
13971 			 * ipif_addr_ready.
13972 			 */
13973 			ipif->ipif_addr_ready = 0;
13974 		}
13975 	}
13976 
13977 	/*
13978 	 * If we've torn down links, then notify the user right away.
13979 	 */
13980 	if (!went_up)
13981 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
13982 }
13983 
13984 static void
13985 ipsq_delete(ipsq_t *ipsq)
13986 {
13987 	ipxop_t *ipx = ipsq->ipsq_xop;
13988 
13989 	ipsq->ipsq_ipst = NULL;
13990 	ASSERT(ipsq->ipsq_phyint == NULL);
13991 	ASSERT(ipsq->ipsq_xop != NULL);
13992 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipx->ipx_mphead == NULL);
13993 	ASSERT(ipx->ipx_pending_mp == NULL);
13994 	kmem_free(ipsq, sizeof (ipsq_t));
13995 }
13996 
13997 static int
13998 ill_up_ipifs_on_ill(ill_t *ill, queue_t *q, mblk_t *mp)
13999 {
14000 	int err;
14001 	ipif_t *ipif;
14002 
14003 	if (ill == NULL)
14004 		return (0);
14005 
14006 	/*
14007 	 * Except for ipif_state_flags and ill_state_flags the other
14008 	 * fields of the ipif/ill that are modified below are protected
14009 	 * implicitly since we are a writer. We would have tried to down
14010 	 * even an ipif that was already down, in ill_down_ipifs. So we
14011 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14012 	 */
14013 	ASSERT(IAM_WRITER_ILL(ill));
14014 
14015 	ill->ill_up_ipifs = B_TRUE;
14016 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14017 		mutex_enter(&ill->ill_lock);
14018 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
14019 		mutex_exit(&ill->ill_lock);
14020 		if (ipif->ipif_was_up) {
14021 			if (!(ipif->ipif_flags & IPIF_UP))
14022 				err = ipif_up(ipif, q, mp);
14023 			ipif->ipif_was_up = B_FALSE;
14024 			if (err != 0) {
14025 				ASSERT(err == EINPROGRESS);
14026 				return (err);
14027 			}
14028 		}
14029 	}
14030 	mutex_enter(&ill->ill_lock);
14031 	ill->ill_state_flags &= ~ILL_CHANGING;
14032 	mutex_exit(&ill->ill_lock);
14033 	ill->ill_up_ipifs = B_FALSE;
14034 	return (0);
14035 }
14036 
14037 /*
14038  * This function is called to bring up all the ipifs that were up before
14039  * bringing the ill down via ill_down_ipifs().
14040  */
14041 int
14042 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14043 {
14044 	int err;
14045 
14046 	ASSERT(IAM_WRITER_ILL(ill));
14047 
14048 	err = ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv4, q, mp);
14049 	if (err != 0)
14050 		return (err);
14051 
14052 	return (ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv6, q, mp));
14053 }
14054 
14055 /*
14056  * Bring down any IPIF_UP ipifs on ill.
14057  */
14058 static void
14059 ill_down_ipifs(ill_t *ill)
14060 {
14061 	ipif_t *ipif;
14062 
14063 	ASSERT(IAM_WRITER_ILL(ill));
14064 
14065 	/*
14066 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14067 	 * are modified below are protected implicitly since we are a writer
14068 	 */
14069 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14070 		/*
14071 		 * We go through the ipif_down logic even if the ipif
14072 		 * is already down, since routes can be added based
14073 		 * on down ipifs. Going through ipif_down once again
14074 		 * will delete any IREs created based on these routes.
14075 		 */
14076 		if (ipif->ipif_flags & IPIF_UP)
14077 			ipif->ipif_was_up = B_TRUE;
14078 
14079 		mutex_enter(&ill->ill_lock);
14080 		ipif->ipif_state_flags |= IPIF_CHANGING;
14081 		mutex_exit(&ill->ill_lock);
14082 
14083 		/*
14084 		 * Need to re-create net/subnet bcast ires if
14085 		 * they are dependent on ipif.
14086 		 */
14087 		if (!ipif->ipif_isv6)
14088 			ipif_check_bcast_ires(ipif);
14089 		(void) ipif_logical_down(ipif, NULL, NULL);
14090 		ipif_non_duplicate(ipif);
14091 		ipif_down_tail(ipif);
14092 	}
14093 }
14094 
14095 /*
14096  * Redo source address selection.  This is called when a
14097  * non-NOLOCAL/DEPRECATED/ANYCAST ipif comes up.
14098  */
14099 void
14100 ill_update_source_selection(ill_t *ill)
14101 {
14102 	ipif_t *ipif;
14103 
14104 	ASSERT(IAM_WRITER_ILL(ill));
14105 
14106 	/*
14107 	 * Underlying interfaces are only used for test traffic and thus
14108 	 * should always send with their (deprecated) source addresses.
14109 	 */
14110 	if (IS_UNDER_IPMP(ill))
14111 		return;
14112 
14113 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14114 		if (ill->ill_isv6)
14115 			ipif_recreate_interface_routes_v6(NULL, ipif);
14116 		else
14117 			ipif_recreate_interface_routes(NULL, ipif);
14118 	}
14119 }
14120 
14121 /*
14122  * Finish the group join started in ip_sioctl_groupname().
14123  */
14124 /* ARGSUSED */
14125 static void
14126 ip_join_illgrps(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
14127 {
14128 	ill_t		*ill = q->q_ptr;
14129 	phyint_t	*phyi = ill->ill_phyint;
14130 	ipmp_grp_t	*grp = phyi->phyint_grp;
14131 	ip_stack_t	*ipst = ill->ill_ipst;
14132 
14133 	/* IS_UNDER_IPMP() won't work until ipmp_ill_join_illgrp() is called */
14134 	ASSERT(!IS_IPMP(ill) && grp != NULL);
14135 	ASSERT(IAM_WRITER_IPSQ(ipsq));
14136 
14137 	if (phyi->phyint_illv4 != NULL) {
14138 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
14139 		VERIFY(grp->gr_pendv4-- > 0);
14140 		rw_exit(&ipst->ips_ipmp_lock);
14141 		ipmp_ill_join_illgrp(phyi->phyint_illv4, grp->gr_v4);
14142 	}
14143 	if (phyi->phyint_illv6 != NULL) {
14144 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
14145 		VERIFY(grp->gr_pendv6-- > 0);
14146 		rw_exit(&ipst->ips_ipmp_lock);
14147 		ipmp_ill_join_illgrp(phyi->phyint_illv6, grp->gr_v6);
14148 	}
14149 	freemsg(mp);
14150 }
14151 
14152 /*
14153  * Process an SIOCSLIFGROUPNAME request.
14154  */
14155 /* ARGSUSED */
14156 int
14157 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14158     ip_ioctl_cmd_t *ipip, void *ifreq)
14159 {
14160 	struct lifreq	*lifr = ifreq;
14161 	ill_t		*ill = ipif->ipif_ill;
14162 	ip_stack_t	*ipst = ill->ill_ipst;
14163 	phyint_t	*phyi = ill->ill_phyint;
14164 	ipmp_grp_t	*grp = phyi->phyint_grp;
14165 	mblk_t		*ipsq_mp;
14166 	int		err = 0;
14167 
14168 	/*
14169 	 * Note that phyint_grp can only change here, where we're exclusive.
14170 	 */
14171 	ASSERT(IAM_WRITER_ILL(ill));
14172 
14173 	if (ipif->ipif_id != 0 || ill->ill_usesrc_grp_next != NULL ||
14174 	    (phyi->phyint_flags & PHYI_VIRTUAL))
14175 		return (EINVAL);
14176 
14177 	lifr->lifr_groupname[LIFGRNAMSIZ - 1] = '\0';
14178 
14179 	rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
14180 
14181 	/*
14182 	 * If the name hasn't changed, there's nothing to do.
14183 	 */
14184 	if (grp != NULL && strcmp(grp->gr_name, lifr->lifr_groupname) == 0)
14185 		goto unlock;
14186 
14187 	/*
14188 	 * Handle requests to rename an IPMP meta-interface.
14189 	 *
14190 	 * Note that creation of the IPMP meta-interface is handled in
14191 	 * userland through the standard plumbing sequence.  As part of the
14192 	 * plumbing the IPMP meta-interface, its initial groupname is set to
14193 	 * the name of the interface (see ipif_set_values_tail()).
14194 	 */
14195 	if (IS_IPMP(ill)) {
14196 		err = ipmp_grp_rename(grp, lifr->lifr_groupname);
14197 		goto unlock;
14198 	}
14199 
14200 	/*
14201 	 * Handle requests to add or remove an IP interface from a group.
14202 	 */
14203 	if (lifr->lifr_groupname[0] != '\0') {			/* add */
14204 		/*
14205 		 * Moves are handled by first removing the interface from
14206 		 * its existing group, and then adding it to another group.
14207 		 * So, fail if it's already in a group.
14208 		 */
14209 		if (IS_UNDER_IPMP(ill)) {
14210 			err = EALREADY;
14211 			goto unlock;
14212 		}
14213 
14214 		grp = ipmp_grp_lookup(lifr->lifr_groupname, ipst);
14215 		if (grp == NULL) {
14216 			err = ENOENT;
14217 			goto unlock;
14218 		}
14219 
14220 		/*
14221 		 * Check if the phyint and its ills are suitable for
14222 		 * inclusion into the group.
14223 		 */
14224 		if ((err = ipmp_grp_vet_phyint(grp, phyi)) != 0)
14225 			goto unlock;
14226 
14227 		/*
14228 		 * Checks pass; join the group, and enqueue the remaining
14229 		 * illgrp joins for when we've become part of the group xop
14230 		 * and are exclusive across its IPSQs.  Since qwriter_ip()
14231 		 * requires an mblk_t to scribble on, and since `mp' will be
14232 		 * freed as part of completing the ioctl, allocate another.
14233 		 */
14234 		if ((ipsq_mp = allocb(0, BPRI_MED)) == NULL) {
14235 			err = ENOMEM;
14236 			goto unlock;
14237 		}
14238 
14239 		/*
14240 		 * Before we drop ipmp_lock, bump gr_pend* to ensure that the
14241 		 * IPMP meta-interface ills needed by `phyi' cannot go away
14242 		 * before ip_join_illgrps() is called back.  See the comments
14243 		 * in ip_sioctl_plink_ipmp() for more.
14244 		 */
14245 		if (phyi->phyint_illv4 != NULL)
14246 			grp->gr_pendv4++;
14247 		if (phyi->phyint_illv6 != NULL)
14248 			grp->gr_pendv6++;
14249 
14250 		rw_exit(&ipst->ips_ipmp_lock);
14251 
14252 		ipmp_phyint_join_grp(phyi, grp);
14253 		ill_refhold(ill);
14254 		qwriter_ip(ill, ill->ill_rq, ipsq_mp, ip_join_illgrps,
14255 		    SWITCH_OP, B_FALSE);
14256 		return (0);
14257 	} else {
14258 		/*
14259 		 * Request to remove the interface from a group.  If the
14260 		 * interface is not in a group, this trivially succeeds.
14261 		 */
14262 		rw_exit(&ipst->ips_ipmp_lock);
14263 		if (IS_UNDER_IPMP(ill))
14264 			ipmp_phyint_leave_grp(phyi);
14265 		return (0);
14266 	}
14267 unlock:
14268 	rw_exit(&ipst->ips_ipmp_lock);
14269 	return (err);
14270 }
14271 
14272 /*
14273  * Process an SIOCGLIFBINDING request.
14274  */
14275 /* ARGSUSED */
14276 int
14277 ip_sioctl_get_binding(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14278     ip_ioctl_cmd_t *ipip, void *ifreq)
14279 {
14280 	ill_t		*bound_ill;
14281 	struct lifreq	*lifr = ifreq;
14282 
14283 	if (!IS_IPMP(ipif->ipif_ill))
14284 		return (EINVAL);
14285 
14286 	if ((bound_ill = ipmp_ipif_hold_bound_ill(ipif)) == NULL) {
14287 		lifr->lifr_binding[0] = '\0';
14288 		return (0);
14289 	}
14290 
14291 	(void) strlcpy(lifr->lifr_binding, bound_ill->ill_name, LIFNAMSIZ);
14292 	ill_refrele(bound_ill);
14293 	return (0);
14294 }
14295 
14296 /*
14297  * Process an SIOCGLIFGROUPNAME request.
14298  */
14299 /* ARGSUSED */
14300 int
14301 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14302     ip_ioctl_cmd_t *ipip, void *ifreq)
14303 {
14304 	ipmp_grp_t	*grp;
14305 	struct lifreq	*lifr = ifreq;
14306 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
14307 
14308 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
14309 	if ((grp = ipif->ipif_ill->ill_phyint->phyint_grp) == NULL)
14310 		lifr->lifr_groupname[0] = '\0';
14311 	else
14312 		(void) strlcpy(lifr->lifr_groupname, grp->gr_name, LIFGRNAMSIZ);
14313 	rw_exit(&ipst->ips_ipmp_lock);
14314 	return (0);
14315 }
14316 
14317 /*
14318  * Process an SIOCGLIFGROUPINFO request.
14319  */
14320 /* ARGSUSED */
14321 int
14322 ip_sioctl_groupinfo(ipif_t *dummy_ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14323     ip_ioctl_cmd_t *ipip, void *dummy)
14324 {
14325 	lifgroupinfo_t	*lifgr;
14326 	ipmp_grp_t	*grp;
14327 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
14328 
14329 	/* ip_wput_nondata() verified mp->b_cont->b_cont */
14330 	lifgr = (lifgroupinfo_t *)mp->b_cont->b_cont->b_rptr;
14331 	lifgr->gi_grname[LIFGRNAMSIZ - 1] = '\0';
14332 
14333 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
14334 	if ((grp = ipmp_grp_lookup(lifgr->gi_grname, ipst)) == NULL) {
14335 		rw_exit(&ipst->ips_ipmp_lock);
14336 		return (ENOENT);
14337 	}
14338 	ipmp_grp_info(grp, lifgr);
14339 	rw_exit(&ipst->ips_ipmp_lock);
14340 	return (0);
14341 }
14342 
14343 static void
14344 ill_dl_down(ill_t *ill)
14345 {
14346 	/*
14347 	 * The ill is down; unbind but stay attached since we're still
14348 	 * associated with a PPA. If we have negotiated DLPI capabilites
14349 	 * with the data link service provider (IDS_OK) then reset them.
14350 	 * The interval between unbinding and rebinding is potentially
14351 	 * unbounded hence we cannot assume things will be the same.
14352 	 * The DLPI capabilities will be probed again when the data link
14353 	 * is brought up.
14354 	 */
14355 	mblk_t	*mp = ill->ill_unbind_mp;
14356 
14357 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
14358 
14359 	ill->ill_unbind_mp = NULL;
14360 	if (mp != NULL) {
14361 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
14362 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
14363 		    ill->ill_name));
14364 		mutex_enter(&ill->ill_lock);
14365 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
14366 		mutex_exit(&ill->ill_lock);
14367 		/*
14368 		 * ip_rput does not pass up normal (M_PROTO) DLPI messages
14369 		 * after ILL_CONDEMNED is set. So in the unplumb case, we call
14370 		 * ill_capability_dld_disable disable rightaway. If this is not
14371 		 * an unplumb operation then the disable happens on receipt of
14372 		 * the capab ack via ip_rput_dlpi_writer ->
14373 		 * ill_capability_ack_thr. In both cases the order of
14374 		 * the operations seen by DLD is capability disable followed
14375 		 * by DL_UNBIND. Also the DLD capability disable needs a
14376 		 * cv_wait'able context.
14377 		 */
14378 		if (ill->ill_state_flags & ILL_CONDEMNED)
14379 			ill_capability_dld_disable(ill);
14380 		ill_capability_reset(ill, B_FALSE);
14381 		ill_dlpi_send(ill, mp);
14382 	}
14383 
14384 	/*
14385 	 * Toss all of our multicast memberships.  We could keep them, but
14386 	 * then we'd have to do bookkeeping of any joins and leaves performed
14387 	 * by the application while the the interface is down (we can't just
14388 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
14389 	 * on a downed interface).
14390 	 */
14391 	ill_leave_multicast(ill);
14392 
14393 	mutex_enter(&ill->ill_lock);
14394 	ill->ill_dl_up = 0;
14395 	ill_nic_event_dispatch(ill, 0, NE_DOWN, NULL, 0);
14396 	mutex_exit(&ill->ill_lock);
14397 }
14398 
14399 static void
14400 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
14401 {
14402 	union DL_primitives *dlp;
14403 	t_uscalar_t prim;
14404 
14405 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
14406 
14407 	dlp = (union DL_primitives *)mp->b_rptr;
14408 	prim = dlp->dl_primitive;
14409 
14410 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
14411 	    dl_primstr(prim), prim, ill->ill_name));
14412 
14413 	switch (prim) {
14414 	case DL_PHYS_ADDR_REQ:
14415 	{
14416 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
14417 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
14418 		break;
14419 	}
14420 	case DL_BIND_REQ:
14421 		mutex_enter(&ill->ill_lock);
14422 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
14423 		mutex_exit(&ill->ill_lock);
14424 		break;
14425 	}
14426 
14427 	/*
14428 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
14429 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
14430 	 * we only wait for the ACK of the DL_UNBIND_REQ.
14431 	 */
14432 	mutex_enter(&ill->ill_lock);
14433 	if (!(ill->ill_state_flags & ILL_CONDEMNED) || (prim == DL_UNBIND_REQ))
14434 		ill->ill_dlpi_pending = prim;
14435 
14436 	mutex_exit(&ill->ill_lock);
14437 	putnext(ill->ill_wq, mp);
14438 }
14439 
14440 /*
14441  * Helper function for ill_dlpi_send().
14442  */
14443 /* ARGSUSED */
14444 static void
14445 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
14446 {
14447 	ill_dlpi_send(q->q_ptr, mp);
14448 }
14449 
14450 /*
14451  * Send a DLPI control message to the driver but make sure there
14452  * is only one outstanding message. Uses ill_dlpi_pending to tell
14453  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
14454  * when an ACK or a NAK is received to process the next queued message.
14455  */
14456 void
14457 ill_dlpi_send(ill_t *ill, mblk_t *mp)
14458 {
14459 	mblk_t **mpp;
14460 
14461 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
14462 
14463 	/*
14464 	 * To ensure that any DLPI requests for current exclusive operation
14465 	 * are always completely sent before any DLPI messages for other
14466 	 * operations, require writer access before enqueuing.
14467 	 */
14468 	if (!IAM_WRITER_ILL(ill)) {
14469 		ill_refhold(ill);
14470 		/* qwriter_ip() does the ill_refrele() */
14471 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
14472 		    NEW_OP, B_TRUE);
14473 		return;
14474 	}
14475 
14476 	mutex_enter(&ill->ill_lock);
14477 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
14478 		/* Must queue message. Tail insertion */
14479 		mpp = &ill->ill_dlpi_deferred;
14480 		while (*mpp != NULL)
14481 			mpp = &((*mpp)->b_next);
14482 
14483 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
14484 		    ill->ill_name));
14485 
14486 		*mpp = mp;
14487 		mutex_exit(&ill->ill_lock);
14488 		return;
14489 	}
14490 	mutex_exit(&ill->ill_lock);
14491 	ill_dlpi_dispatch(ill, mp);
14492 }
14493 
14494 static void
14495 ill_capability_send(ill_t *ill, mblk_t *mp)
14496 {
14497 	ill->ill_capab_pending_cnt++;
14498 	ill_dlpi_send(ill, mp);
14499 }
14500 
14501 void
14502 ill_capability_done(ill_t *ill)
14503 {
14504 	ASSERT(ill->ill_capab_pending_cnt != 0);
14505 
14506 	ill_dlpi_done(ill, DL_CAPABILITY_REQ);
14507 
14508 	ill->ill_capab_pending_cnt--;
14509 	if (ill->ill_capab_pending_cnt == 0 &&
14510 	    ill->ill_dlpi_capab_state == IDCS_OK)
14511 		ill_capability_reset_alloc(ill);
14512 }
14513 
14514 /*
14515  * Send all deferred DLPI messages without waiting for their ACKs.
14516  */
14517 void
14518 ill_dlpi_send_deferred(ill_t *ill)
14519 {
14520 	mblk_t *mp, *nextmp;
14521 
14522 	/*
14523 	 * Clear ill_dlpi_pending so that the message is not queued in
14524 	 * ill_dlpi_send().
14525 	 */
14526 	mutex_enter(&ill->ill_lock);
14527 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
14528 	mp = ill->ill_dlpi_deferred;
14529 	ill->ill_dlpi_deferred = NULL;
14530 	mutex_exit(&ill->ill_lock);
14531 
14532 	for (; mp != NULL; mp = nextmp) {
14533 		nextmp = mp->b_next;
14534 		mp->b_next = NULL;
14535 		ill_dlpi_send(ill, mp);
14536 	}
14537 }
14538 
14539 /*
14540  * Check if the DLPI primitive `prim' is pending; print a warning if not.
14541  */
14542 boolean_t
14543 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
14544 {
14545 	t_uscalar_t pending;
14546 
14547 	mutex_enter(&ill->ill_lock);
14548 	if (ill->ill_dlpi_pending == prim) {
14549 		mutex_exit(&ill->ill_lock);
14550 		return (B_TRUE);
14551 	}
14552 
14553 	/*
14554 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
14555 	 * without waiting, so don't print any warnings in that case.
14556 	 */
14557 	if (ill->ill_state_flags & ILL_CONDEMNED) {
14558 		mutex_exit(&ill->ill_lock);
14559 		return (B_FALSE);
14560 	}
14561 	pending = ill->ill_dlpi_pending;
14562 	mutex_exit(&ill->ill_lock);
14563 
14564 	if (pending == DL_PRIM_INVAL) {
14565 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
14566 		    "received unsolicited ack for %s on %s\n",
14567 		    dl_primstr(prim), ill->ill_name);
14568 	} else {
14569 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
14570 		    "received unexpected ack for %s on %s (expecting %s)\n",
14571 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
14572 	}
14573 	return (B_FALSE);
14574 }
14575 
14576 /*
14577  * Complete the current DLPI operation associated with `prim' on `ill' and
14578  * start the next queued DLPI operation (if any).  If there are no queued DLPI
14579  * operations and the ill's current exclusive IPSQ operation has finished
14580  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
14581  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
14582  * the comments above ipsq_current_finish() for details.
14583  */
14584 void
14585 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
14586 {
14587 	mblk_t *mp;
14588 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
14589 	ipxop_t *ipx = ipsq->ipsq_xop;
14590 
14591 	ASSERT(IAM_WRITER_IPSQ(ipsq));
14592 	mutex_enter(&ill->ill_lock);
14593 
14594 	ASSERT(prim != DL_PRIM_INVAL);
14595 	ASSERT(ill->ill_dlpi_pending == prim);
14596 
14597 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
14598 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
14599 
14600 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
14601 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
14602 		if (ipx->ipx_current_done) {
14603 			mutex_enter(&ipx->ipx_lock);
14604 			ipx->ipx_current_ipif = NULL;
14605 			mutex_exit(&ipx->ipx_lock);
14606 		}
14607 		cv_signal(&ill->ill_cv);
14608 		mutex_exit(&ill->ill_lock);
14609 		return;
14610 	}
14611 
14612 	ill->ill_dlpi_deferred = mp->b_next;
14613 	mp->b_next = NULL;
14614 	mutex_exit(&ill->ill_lock);
14615 
14616 	ill_dlpi_dispatch(ill, mp);
14617 }
14618 
14619 void
14620 conn_delete_ire(conn_t *connp, caddr_t arg)
14621 {
14622 	ipif_t	*ipif = (ipif_t *)arg;
14623 	ire_t	*ire;
14624 
14625 	/*
14626 	 * Look at the cached ires on conns which has pointers to ipifs.
14627 	 * We just call ire_refrele which clears up the reference
14628 	 * to ire. Called when a conn closes. Also called from ipif_free
14629 	 * to cleanup indirect references to the stale ipif via the cached ire.
14630 	 */
14631 	mutex_enter(&connp->conn_lock);
14632 	ire = connp->conn_ire_cache;
14633 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
14634 		connp->conn_ire_cache = NULL;
14635 		mutex_exit(&connp->conn_lock);
14636 		IRE_REFRELE_NOTR(ire);
14637 		return;
14638 	}
14639 	mutex_exit(&connp->conn_lock);
14640 
14641 }
14642 
14643 /*
14644  * Some operations (e.g., ipif_down()) conditionally delete a number
14645  * of IREs. Those IREs may have been previously cached in the conn structure.
14646  * This ipcl_walk() walker function releases all references to such IREs based
14647  * on the condemned flag.
14648  */
14649 /* ARGSUSED */
14650 void
14651 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
14652 {
14653 	ire_t	*ire;
14654 
14655 	mutex_enter(&connp->conn_lock);
14656 	ire = connp->conn_ire_cache;
14657 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
14658 		connp->conn_ire_cache = NULL;
14659 		mutex_exit(&connp->conn_lock);
14660 		IRE_REFRELE_NOTR(ire);
14661 		return;
14662 	}
14663 	mutex_exit(&connp->conn_lock);
14664 }
14665 
14666 /*
14667  * Take down a specific interface, but don't lose any information about it.
14668  * (Always called as writer.)
14669  * This function goes through the down sequence even if the interface is
14670  * already down. There are 2 reasons.
14671  * a. Currently we permit interface routes that depend on down interfaces
14672  *    to be added. This behaviour itself is questionable. However it appears
14673  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
14674  *    time. We go thru the cleanup in order to remove these routes.
14675  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
14676  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
14677  *    down, but we need to cleanup i.e. do ill_dl_down and
14678  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
14679  *
14680  * IP-MT notes:
14681  *
14682  * Model of reference to interfaces.
14683  *
14684  * The following members in ipif_t track references to the ipif.
14685  *	int     ipif_refcnt;    Active reference count
14686  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
14687  *	uint_t  ipif_ilm_cnt;   Number of ilms's references this ipif.
14688  *
14689  * The following members in ill_t track references to the ill.
14690  *	int             ill_refcnt;     active refcnt
14691  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
14692  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
14693  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
14694  *
14695  * Reference to an ipif or ill can be obtained in any of the following ways.
14696  *
14697  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
14698  * Pointers to ipif / ill from other data structures viz ire and conn.
14699  * Implicit reference to the ipif / ill by holding a reference to the ire.
14700  *
14701  * The ipif/ill lookup functions return a reference held ipif / ill.
14702  * ipif_refcnt and ill_refcnt track the reference counts respectively.
14703  * This is a purely dynamic reference count associated with threads holding
14704  * references to the ipif / ill. Pointers from other structures do not
14705  * count towards this reference count.
14706  *
14707  * ipif_ire_cnt/ill_ire_cnt is the number of ire's
14708  * associated with the ipif/ill. This is incremented whenever a new
14709  * ire is created referencing the ipif/ill. This is done atomically inside
14710  * ire_add_v[46] where the ire is actually added to the ire hash table.
14711  * The count is decremented in ire_inactive where the ire is destroyed.
14712  *
14713  * nce's reference ill's thru nce_ill and the count of nce's associated with
14714  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
14715  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
14716  * table. Similarly it is decremented in ndp_inactive() where the nce
14717  * is destroyed.
14718  *
14719  * ilm's reference to the ipif (for IPv4 ilm's) or the ill (for IPv6 ilm's)
14720  * is incremented in ilm_add_v6() and decremented before the ilm is freed
14721  * in ilm_walker_cleanup() or ilm_delete().
14722  *
14723  * Flow of ioctls involving interface down/up
14724  *
14725  * The following is the sequence of an attempt to set some critical flags on an
14726  * up interface.
14727  * ip_sioctl_flags
14728  * ipif_down
14729  * wait for ipif to be quiescent
14730  * ipif_down_tail
14731  * ip_sioctl_flags_tail
14732  *
14733  * All set ioctls that involve down/up sequence would have a skeleton similar
14734  * to the above. All the *tail functions are called after the refcounts have
14735  * dropped to the appropriate values.
14736  *
14737  * The mechanism to quiesce an ipif is as follows.
14738  *
14739  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
14740  * on the ipif. Callers either pass a flag requesting wait or the lookup
14741  *  functions will return NULL.
14742  *
14743  * Delete all ires referencing this ipif
14744  *
14745  * Any thread attempting to do an ipif_refhold on an ipif that has been
14746  * obtained thru a cached pointer will first make sure that
14747  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
14748  * increment the refcount.
14749  *
14750  * The above guarantees that the ipif refcount will eventually come down to
14751  * zero and the ipif will quiesce, once all threads that currently hold a
14752  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
14753  * ipif_refcount has dropped to zero and all ire's associated with this ipif
14754  * have also been ire_inactive'd. i.e. when ipif_{ire, ill}_cnt and
14755  * ipif_refcnt both drop to zero. See also: comments above IPIF_DOWN_OK()
14756  * in ip.h
14757  *
14758  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
14759  *
14760  * Threads trying to lookup an ipif or ill can pass a flag requesting
14761  * wait and restart if the ipif / ill cannot be looked up currently.
14762  * For eg. bind, and route operations (Eg. route add / delete) cannot return
14763  * failure if the ipif is currently undergoing an exclusive operation, and
14764  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
14765  * is restarted by ipsq_exit() when the current exclusive operation completes.
14766  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
14767  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
14768  * change while the ill_lock is held. Before dropping the ill_lock we acquire
14769  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
14770  * until we release the ipsq_lock, even though the the ill/ipif state flags
14771  * can change after we drop the ill_lock.
14772  *
14773  * An attempt to send out a packet using an ipif that is currently
14774  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
14775  * operation and restart it later when the exclusive condition on the ipif ends.
14776  * This is an example of not passing the wait flag to the lookup functions. For
14777  * example an attempt to refhold and use conn->conn_multicast_ipif and send
14778  * out a multicast packet on that ipif will fail while the ipif is
14779  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
14780  * currently IPIF_CHANGING will also fail.
14781  */
14782 int
14783 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
14784 {
14785 	ill_t		*ill = ipif->ipif_ill;
14786 	conn_t		*connp;
14787 	boolean_t	success;
14788 	boolean_t	ipif_was_up = B_FALSE;
14789 	ip_stack_t	*ipst = ill->ill_ipst;
14790 
14791 	ASSERT(IAM_WRITER_IPIF(ipif));
14792 
14793 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
14794 
14795 	if (ipif->ipif_flags & IPIF_UP) {
14796 		mutex_enter(&ill->ill_lock);
14797 		ipif->ipif_flags &= ~IPIF_UP;
14798 		ASSERT(ill->ill_ipif_up_count > 0);
14799 		--ill->ill_ipif_up_count;
14800 		mutex_exit(&ill->ill_lock);
14801 		ipif_was_up = B_TRUE;
14802 		/* Update status in SCTP's list */
14803 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
14804 		ill_nic_event_dispatch(ipif->ipif_ill,
14805 		    MAP_IPIF_ID(ipif->ipif_id), NE_LIF_DOWN, NULL, 0);
14806 	}
14807 
14808 	/*
14809 	 * Blow away memberships we established in ipif_multicast_up().
14810 	 */
14811 	ipif_multicast_down(ipif);
14812 
14813 	/*
14814 	 * Remove from the mapping for __sin6_src_id. We insert only
14815 	 * when the address is not INADDR_ANY. As IPv4 addresses are
14816 	 * stored as mapped addresses, we need to check for mapped
14817 	 * INADDR_ANY also.
14818 	 */
14819 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
14820 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
14821 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14822 		int err;
14823 
14824 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
14825 		    ipif->ipif_zoneid, ipst);
14826 		if (err != 0) {
14827 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
14828 		}
14829 	}
14830 
14831 	/*
14832 	 * Delete all IRE's pointing at this ipif or its source address.
14833 	 */
14834 	if (ipif->ipif_isv6) {
14835 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
14836 		    ipst);
14837 	} else {
14838 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
14839 		    ipst);
14840 	}
14841 
14842 	if (ipif_was_up && ill->ill_ipif_up_count == 0) {
14843 		/*
14844 		 * Since the interface is now down, it may have just become
14845 		 * inactive.  Note that this needs to be done even for a
14846 		 * lll_logical_down(), or ARP entries will not get correctly
14847 		 * restored when the interface comes back up.
14848 		 */
14849 		if (IS_UNDER_IPMP(ill))
14850 			ipmp_ill_refresh_active(ill);
14851 	}
14852 
14853 	/*
14854 	 * Cleaning up the conn_ire_cache or conns must be done only after the
14855 	 * ires have been deleted above. Otherwise a thread could end up
14856 	 * caching an ire in a conn after we have finished the cleanup of the
14857 	 * conn. The caching is done after making sure that the ire is not yet
14858 	 * condemned. Also documented in the block comment above ip_output
14859 	 */
14860 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
14861 	/* Also, delete the ires cached in SCTP */
14862 	sctp_ire_cache_flush(ipif);
14863 
14864 	/*
14865 	 * Update any other ipifs which have used "our" local address as
14866 	 * a source address. This entails removing and recreating IRE_INTERFACE
14867 	 * entries for such ipifs.
14868 	 */
14869 	if (ipif->ipif_isv6)
14870 		ipif_update_other_ipifs_v6(ipif);
14871 	else
14872 		ipif_update_other_ipifs(ipif);
14873 
14874 	/*
14875 	 * neighbor-discovery or arp entries for this interface.
14876 	 */
14877 	ipif_ndp_down(ipif);
14878 
14879 	/*
14880 	 * If mp is NULL the caller will wait for the appropriate refcnt.
14881 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
14882 	 * and ill_delete -> ipif_free -> ipif_down
14883 	 */
14884 	if (mp == NULL) {
14885 		ASSERT(q == NULL);
14886 		return (0);
14887 	}
14888 
14889 	if (CONN_Q(q)) {
14890 		connp = Q_TO_CONN(q);
14891 		mutex_enter(&connp->conn_lock);
14892 	} else {
14893 		connp = NULL;
14894 	}
14895 	mutex_enter(&ill->ill_lock);
14896 	/*
14897 	 * Are there any ire's pointing to this ipif that are still active ?
14898 	 * If this is the last ipif going down, are there any ire's pointing
14899 	 * to this ill that are still active ?
14900 	 */
14901 	if (ipif_is_quiescent(ipif)) {
14902 		mutex_exit(&ill->ill_lock);
14903 		if (connp != NULL)
14904 			mutex_exit(&connp->conn_lock);
14905 		return (0);
14906 	}
14907 
14908 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
14909 	    ill->ill_name, (void *)ill));
14910 	/*
14911 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
14912 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
14913 	 * which in turn is called by the last refrele on the ipif/ill/ire.
14914 	 */
14915 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
14916 	if (!success) {
14917 		/* The conn is closing. So just return */
14918 		ASSERT(connp != NULL);
14919 		mutex_exit(&ill->ill_lock);
14920 		mutex_exit(&connp->conn_lock);
14921 		return (EINTR);
14922 	}
14923 
14924 	mutex_exit(&ill->ill_lock);
14925 	if (connp != NULL)
14926 		mutex_exit(&connp->conn_lock);
14927 	return (EINPROGRESS);
14928 }
14929 
14930 void
14931 ipif_down_tail(ipif_t *ipif)
14932 {
14933 	ill_t	*ill = ipif->ipif_ill;
14934 
14935 	/*
14936 	 * Skip any loopback interface (null wq).
14937 	 * If this is the last logical interface on the ill
14938 	 * have ill_dl_down tell the driver we are gone (unbind)
14939 	 * Note that lun 0 can ipif_down even though
14940 	 * there are other logical units that are up.
14941 	 * This occurs e.g. when we change a "significant" IFF_ flag.
14942 	 */
14943 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
14944 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
14945 	    ill->ill_dl_up) {
14946 		ill_dl_down(ill);
14947 	}
14948 	ill->ill_logical_down = 0;
14949 
14950 	/*
14951 	 * Has to be after removing the routes in ipif_down_delete_ire.
14952 	 */
14953 	ipif_resolver_down(ipif);
14954 
14955 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
14956 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif, RTSQ_DEFAULT);
14957 }
14958 
14959 /*
14960  * Bring interface logically down without bringing the physical interface
14961  * down e.g. when the netmask is changed. This avoids long lasting link
14962  * negotiations between an ethernet interface and a certain switches.
14963  */
14964 static int
14965 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
14966 {
14967 	/*
14968 	 * The ill_logical_down flag is a transient flag. It is set here
14969 	 * and is cleared once the down has completed in ipif_down_tail.
14970 	 * This flag does not indicate whether the ill stream is in the
14971 	 * DL_BOUND state with the driver. Instead this flag is used by
14972 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
14973 	 * the driver. The state of the ill stream i.e. whether it is
14974 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
14975 	 */
14976 	ipif->ipif_ill->ill_logical_down = 1;
14977 	return (ipif_down(ipif, q, mp));
14978 }
14979 
14980 /*
14981  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
14982  * If the usesrc client ILL is already part of a usesrc group or not,
14983  * in either case a ire_stq with the matching usesrc client ILL will
14984  * locate the IRE's that need to be deleted. We want IREs to be created
14985  * with the new source address.
14986  */
14987 static void
14988 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
14989 {
14990 	ill_t	*ucill = (ill_t *)ill_arg;
14991 
14992 	ASSERT(IAM_WRITER_ILL(ucill));
14993 
14994 	if (ire->ire_stq == NULL)
14995 		return;
14996 
14997 	if ((ire->ire_type == IRE_CACHE) &&
14998 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
14999 		ire_delete(ire);
15000 }
15001 
15002 /*
15003  * ire_walk routine to delete every IRE dependent on the interface
15004  * address that is going down.	(Always called as writer.)
15005  * Works for both v4 and v6.
15006  * In addition for checking for ire_ipif matches it also checks for
15007  * IRE_CACHE entries which have the same source address as the
15008  * disappearing ipif since ipif_select_source might have picked
15009  * that source. Note that ipif_down/ipif_update_other_ipifs takes
15010  * care of any IRE_INTERFACE with the disappearing source address.
15011  */
15012 static void
15013 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
15014 {
15015 	ipif_t	*ipif = (ipif_t *)ipif_arg;
15016 
15017 	ASSERT(IAM_WRITER_IPIF(ipif));
15018 	if (ire->ire_ipif == NULL)
15019 		return;
15020 
15021 	if (ire->ire_ipif != ipif) {
15022 		/*
15023 		 * Look for a matching source address.
15024 		 */
15025 		if (ire->ire_type != IRE_CACHE)
15026 			return;
15027 		if (ipif->ipif_flags & IPIF_NOLOCAL)
15028 			return;
15029 
15030 		if (ire->ire_ipversion == IPV4_VERSION) {
15031 			if (ire->ire_src_addr != ipif->ipif_src_addr)
15032 				return;
15033 		} else {
15034 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
15035 			    &ipif->ipif_v6lcl_addr))
15036 				return;
15037 		}
15038 		ire_delete(ire);
15039 		return;
15040 	}
15041 	/*
15042 	 * ire_delete() will do an ire_flush_cache which will delete
15043 	 * all ire_ipif matches
15044 	 */
15045 	ire_delete(ire);
15046 }
15047 
15048 /*
15049  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
15050  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
15051  * 2) when an interface is brought up or down (on that ill).
15052  * This ensures that the IRE_CACHE entries don't retain stale source
15053  * address selection results.
15054  */
15055 void
15056 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
15057 {
15058 	ill_t	*ill = (ill_t *)ill_arg;
15059 
15060 	ASSERT(IAM_WRITER_ILL(ill));
15061 	ASSERT(ire->ire_type == IRE_CACHE);
15062 
15063 	/*
15064 	 * We are called for IRE_CACHEs whose ire_stq or ire_ipif matches
15065 	 * ill, but we only want to delete the IRE if ire_ipif matches.
15066 	 */
15067 	ASSERT(ire->ire_ipif != NULL);
15068 	if (ill == ire->ire_ipif->ipif_ill)
15069 		ire_delete(ire);
15070 }
15071 
15072 /*
15073  * Delete all the IREs whose ire_stq's reference `ill_arg'.  IPMP uses this
15074  * instead of ill_ipif_cache_delete() because ire_ipif->ipif_ill references
15075  * the IPMP ill.
15076  */
15077 void
15078 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
15079 {
15080 	ill_t	*ill = (ill_t *)ill_arg;
15081 
15082 	ASSERT(IAM_WRITER_ILL(ill));
15083 	ASSERT(ire->ire_type == IRE_CACHE);
15084 
15085 	/*
15086 	 * We are called for IRE_CACHEs whose ire_stq or ire_ipif matches
15087 	 * ill, but we only want to delete the IRE if ire_stq matches.
15088 	 */
15089 	if (ire->ire_stq->q_ptr == ill_arg)
15090 		ire_delete(ire);
15091 }
15092 
15093 /*
15094  * Delete all broadcast IREs with a source address on `ill_arg'.
15095  */
15096 static void
15097 ill_broadcast_delete(ire_t *ire, char *ill_arg)
15098 {
15099 	ill_t *ill = (ill_t *)ill_arg;
15100 
15101 	ASSERT(IAM_WRITER_ILL(ill));
15102 	ASSERT(ire->ire_type == IRE_BROADCAST);
15103 
15104 	if (ire->ire_ipif->ipif_ill == ill)
15105 		ire_delete(ire);
15106 }
15107 
15108 /*
15109  * Initiate deallocate of an IPIF. Always called as writer. Called by
15110  * ill_delete or ip_sioctl_removeif.
15111  */
15112 static void
15113 ipif_free(ipif_t *ipif)
15114 {
15115 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15116 
15117 	ASSERT(IAM_WRITER_IPIF(ipif));
15118 
15119 	if (ipif->ipif_recovery_id != 0)
15120 		(void) untimeout(ipif->ipif_recovery_id);
15121 	ipif->ipif_recovery_id = 0;
15122 
15123 	/* Remove conn references */
15124 	reset_conn_ipif(ipif);
15125 
15126 	/*
15127 	 * Make sure we have valid net and subnet broadcast ire's for the
15128 	 * other ipif's which share them with this ipif.
15129 	 */
15130 	if (!ipif->ipif_isv6)
15131 		ipif_check_bcast_ires(ipif);
15132 
15133 	/*
15134 	 * Take down the interface. We can be called either from ill_delete
15135 	 * or from ip_sioctl_removeif.
15136 	 */
15137 	(void) ipif_down(ipif, NULL, NULL);
15138 
15139 	/*
15140 	 * Now that the interface is down, there's no chance it can still
15141 	 * become a duplicate.  Cancel any timer that may have been set while
15142 	 * tearing down.
15143 	 */
15144 	if (ipif->ipif_recovery_id != 0)
15145 		(void) untimeout(ipif->ipif_recovery_id);
15146 	ipif->ipif_recovery_id = 0;
15147 
15148 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15149 	/* Remove pointers to this ill in the multicast routing tables */
15150 	reset_mrt_vif_ipif(ipif);
15151 	/* If necessary, clear the cached source ipif rotor. */
15152 	if (ipif->ipif_ill->ill_src_ipif == ipif)
15153 		ipif->ipif_ill->ill_src_ipif = NULL;
15154 	rw_exit(&ipst->ips_ill_g_lock);
15155 }
15156 
15157 static void
15158 ipif_free_tail(ipif_t *ipif)
15159 {
15160 	mblk_t	*mp;
15161 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
15162 
15163 	/*
15164 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
15165 	 */
15166 	mutex_enter(&ipif->ipif_saved_ire_lock);
15167 	mp = ipif->ipif_saved_ire_mp;
15168 	ipif->ipif_saved_ire_mp = NULL;
15169 	mutex_exit(&ipif->ipif_saved_ire_lock);
15170 	freemsg(mp);
15171 
15172 	/*
15173 	 * Need to hold both ill_g_lock and ill_lock while
15174 	 * inserting or removing an ipif from the linked list
15175 	 * of ipifs hanging off the ill.
15176 	 */
15177 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15178 
15179 	ASSERT(ilm_walk_ipif(ipif) == 0);
15180 
15181 #ifdef DEBUG
15182 	ipif_trace_cleanup(ipif);
15183 #endif
15184 
15185 	/* Ask SCTP to take it out of it list */
15186 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
15187 
15188 	/* Get it out of the ILL interface list. */
15189 	ipif_remove(ipif);
15190 	rw_exit(&ipst->ips_ill_g_lock);
15191 
15192 	mutex_destroy(&ipif->ipif_saved_ire_lock);
15193 
15194 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
15195 	ASSERT(ipif->ipif_recovery_id == 0);
15196 
15197 	/* Free the memory. */
15198 	mi_free(ipif);
15199 }
15200 
15201 /*
15202  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
15203  * is zero.
15204  */
15205 void
15206 ipif_get_name(const ipif_t *ipif, char *buf, int len)
15207 {
15208 	char	lbuf[LIFNAMSIZ];
15209 	char	*name;
15210 	size_t	name_len;
15211 
15212 	buf[0] = '\0';
15213 	name = ipif->ipif_ill->ill_name;
15214 	name_len = ipif->ipif_ill->ill_name_length;
15215 	if (ipif->ipif_id != 0) {
15216 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
15217 		    ipif->ipif_id);
15218 		name = lbuf;
15219 		name_len = mi_strlen(name) + 1;
15220 	}
15221 	len -= 1;
15222 	buf[len] = '\0';
15223 	len = MIN(len, name_len);
15224 	bcopy(name, buf, len);
15225 }
15226 
15227 /*
15228  * Find an IPIF based on the name passed in.  Names can be of the
15229  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
15230  * The <phys> string can have forms like <dev><#> (e.g., le0),
15231  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
15232  * When there is no colon, the implied unit id is zero. <phys> must
15233  * correspond to the name of an ILL.  (May be called as writer.)
15234  */
15235 static ipif_t *
15236 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
15237     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
15238     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
15239 {
15240 	char	*cp;
15241 	char	*endp;
15242 	long	id;
15243 	ill_t	*ill;
15244 	ipif_t	*ipif;
15245 	uint_t	ire_type;
15246 	boolean_t did_alloc = B_FALSE;
15247 	ipsq_t	*ipsq;
15248 
15249 	if (error != NULL)
15250 		*error = 0;
15251 
15252 	/*
15253 	 * If the caller wants to us to create the ipif, make sure we have a
15254 	 * valid zoneid
15255 	 */
15256 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
15257 
15258 	if (namelen == 0) {
15259 		if (error != NULL)
15260 			*error = ENXIO;
15261 		return (NULL);
15262 	}
15263 
15264 	*exists = B_FALSE;
15265 	/* Look for a colon in the name. */
15266 	endp = &name[namelen];
15267 	for (cp = endp; --cp > name; ) {
15268 		if (*cp == IPIF_SEPARATOR_CHAR)
15269 			break;
15270 	}
15271 
15272 	if (*cp == IPIF_SEPARATOR_CHAR) {
15273 		/*
15274 		 * Reject any non-decimal aliases for logical
15275 		 * interfaces. Aliases with leading zeroes
15276 		 * are also rejected as they introduce ambiguity
15277 		 * in the naming of the interfaces.
15278 		 * In order to confirm with existing semantics,
15279 		 * and to not break any programs/script relying
15280 		 * on that behaviour, if<0>:0 is considered to be
15281 		 * a valid interface.
15282 		 *
15283 		 * If alias has two or more digits and the first
15284 		 * is zero, fail.
15285 		 */
15286 		if (&cp[2] < endp && cp[1] == '0') {
15287 			if (error != NULL)
15288 				*error = EINVAL;
15289 			return (NULL);
15290 		}
15291 	}
15292 
15293 	if (cp <= name) {
15294 		cp = endp;
15295 	} else {
15296 		*cp = '\0';
15297 	}
15298 
15299 	/*
15300 	 * Look up the ILL, based on the portion of the name
15301 	 * before the slash. ill_lookup_on_name returns a held ill.
15302 	 * Temporary to check whether ill exists already. If so
15303 	 * ill_lookup_on_name will clear it.
15304 	 */
15305 	ill = ill_lookup_on_name(name, do_alloc, isv6,
15306 	    q, mp, func, error, &did_alloc, ipst);
15307 	if (cp != endp)
15308 		*cp = IPIF_SEPARATOR_CHAR;
15309 	if (ill == NULL)
15310 		return (NULL);
15311 
15312 	/* Establish the unit number in the name. */
15313 	id = 0;
15314 	if (cp < endp && *endp == '\0') {
15315 		/* If there was a colon, the unit number follows. */
15316 		cp++;
15317 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
15318 			ill_refrele(ill);
15319 			if (error != NULL)
15320 				*error = ENXIO;
15321 			return (NULL);
15322 		}
15323 	}
15324 
15325 	GRAB_CONN_LOCK(q);
15326 	mutex_enter(&ill->ill_lock);
15327 	/* Now see if there is an IPIF with this unit number. */
15328 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15329 		if (ipif->ipif_id == id) {
15330 			if (zoneid != ALL_ZONES &&
15331 			    zoneid != ipif->ipif_zoneid &&
15332 			    ipif->ipif_zoneid != ALL_ZONES) {
15333 				mutex_exit(&ill->ill_lock);
15334 				RELEASE_CONN_LOCK(q);
15335 				ill_refrele(ill);
15336 				if (error != NULL)
15337 					*error = ENXIO;
15338 				return (NULL);
15339 			}
15340 			/*
15341 			 * The block comment at the start of ipif_down
15342 			 * explains the use of the macros used below
15343 			 */
15344 			if (IPIF_CAN_LOOKUP(ipif)) {
15345 				ipif_refhold_locked(ipif);
15346 				mutex_exit(&ill->ill_lock);
15347 				if (!did_alloc)
15348 					*exists = B_TRUE;
15349 				/*
15350 				 * Drop locks before calling ill_refrele
15351 				 * since it can potentially call into
15352 				 * ipif_ill_refrele_tail which can end up
15353 				 * in trying to acquire any lock.
15354 				 */
15355 				RELEASE_CONN_LOCK(q);
15356 				ill_refrele(ill);
15357 				return (ipif);
15358 			} else if (IPIF_CAN_WAIT(ipif, q)) {
15359 				ipsq = ill->ill_phyint->phyint_ipsq;
15360 				mutex_enter(&ipsq->ipsq_lock);
15361 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
15362 				mutex_exit(&ill->ill_lock);
15363 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
15364 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
15365 				mutex_exit(&ipsq->ipsq_lock);
15366 				RELEASE_CONN_LOCK(q);
15367 				ill_refrele(ill);
15368 				if (error != NULL)
15369 					*error = EINPROGRESS;
15370 				return (NULL);
15371 			}
15372 		}
15373 	}
15374 	RELEASE_CONN_LOCK(q);
15375 
15376 	if (!do_alloc) {
15377 		mutex_exit(&ill->ill_lock);
15378 		ill_refrele(ill);
15379 		if (error != NULL)
15380 			*error = ENXIO;
15381 		return (NULL);
15382 	}
15383 
15384 	/*
15385 	 * If none found, atomically allocate and return a new one.
15386 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
15387 	 * to support "receive only" use of lo0:1 etc. as is still done
15388 	 * below as an initial guess.
15389 	 * However, this is now likely to be overriden later in ipif_up_done()
15390 	 * when we know for sure what address has been configured on the
15391 	 * interface, since we might have more than one loopback interface
15392 	 * with a loopback address, e.g. in the case of zones, and all the
15393 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
15394 	 */
15395 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
15396 		ire_type = IRE_LOOPBACK;
15397 	else
15398 		ire_type = IRE_LOCAL;
15399 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE, B_TRUE);
15400 	if (ipif != NULL)
15401 		ipif_refhold_locked(ipif);
15402 	else if (error != NULL)
15403 		*error = ENOMEM;
15404 	mutex_exit(&ill->ill_lock);
15405 	ill_refrele(ill);
15406 	return (ipif);
15407 }
15408 
15409 /*
15410  * This routine is called whenever a new address comes up on an ipif.  If
15411  * we are configured to respond to address mask requests, then we are supposed
15412  * to broadcast an address mask reply at this time.  This routine is also
15413  * called if we are already up, but a netmask change is made.  This is legal
15414  * but might not make the system manager very popular.	(May be called
15415  * as writer.)
15416  */
15417 void
15418 ipif_mask_reply(ipif_t *ipif)
15419 {
15420 	icmph_t	*icmph;
15421 	ipha_t	*ipha;
15422 	mblk_t	*mp;
15423 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15424 
15425 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
15426 
15427 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
15428 		return;
15429 
15430 	/* ICMP mask reply is IPv4 only */
15431 	ASSERT(!ipif->ipif_isv6);
15432 	/* ICMP mask reply is not for a loopback interface */
15433 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
15434 
15435 	mp = allocb(REPLY_LEN, BPRI_HI);
15436 	if (mp == NULL)
15437 		return;
15438 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
15439 
15440 	ipha = (ipha_t *)mp->b_rptr;
15441 	bzero(ipha, REPLY_LEN);
15442 	*ipha = icmp_ipha;
15443 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
15444 	ipha->ipha_src = ipif->ipif_src_addr;
15445 	ipha->ipha_dst = ipif->ipif_brd_addr;
15446 	ipha->ipha_length = htons(REPLY_LEN);
15447 	ipha->ipha_ident = 0;
15448 
15449 	icmph = (icmph_t *)&ipha[1];
15450 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
15451 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
15452 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
15453 
15454 	put(ipif->ipif_wq, mp);
15455 
15456 #undef	REPLY_LEN
15457 }
15458 
15459 /*
15460  * When the mtu in the ipif changes, we call this routine through ire_walk
15461  * to update all the relevant IREs.
15462  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
15463  */
15464 static void
15465 ipif_mtu_change(ire_t *ire, char *ipif_arg)
15466 {
15467 	ipif_t *ipif = (ipif_t *)ipif_arg;
15468 
15469 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
15470 		return;
15471 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
15472 }
15473 
15474 /*
15475  * When the mtu in the ill changes, we call this routine through ire_walk
15476  * to update all the relevant IREs.
15477  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
15478  */
15479 void
15480 ill_mtu_change(ire_t *ire, char *ill_arg)
15481 {
15482 	ill_t	*ill = (ill_t *)ill_arg;
15483 
15484 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
15485 		return;
15486 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
15487 }
15488 
15489 /*
15490  * Join the ipif specific multicast groups.
15491  * Must be called after a mapping has been set up in the resolver.  (Always
15492  * called as writer.)
15493  */
15494 void
15495 ipif_multicast_up(ipif_t *ipif)
15496 {
15497 	int err;
15498 	ill_t *ill;
15499 
15500 	ASSERT(IAM_WRITER_IPIF(ipif));
15501 
15502 	ill = ipif->ipif_ill;
15503 
15504 	ip1dbg(("ipif_multicast_up\n"));
15505 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
15506 		return;
15507 
15508 	if (ipif->ipif_isv6) {
15509 		in6_addr_t v6allmc = ipv6_all_hosts_mcast;
15510 		in6_addr_t v6solmc = ipv6_solicited_node_mcast;
15511 
15512 		v6solmc.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
15513 
15514 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
15515 			return;
15516 
15517 		ip1dbg(("ipif_multicast_up - addmulti\n"));
15518 
15519 		/*
15520 		 * Join the all hosts multicast address.  We skip this for
15521 		 * underlying IPMP interfaces since they should be invisible.
15522 		 */
15523 		if (!IS_UNDER_IPMP(ill)) {
15524 			err = ip_addmulti_v6(&v6allmc, ill, ipif->ipif_zoneid,
15525 			    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
15526 			if (err != 0) {
15527 				ip0dbg(("ipif_multicast_up: "
15528 				    "all_hosts_mcast failed %d\n", err));
15529 				return;
15530 			}
15531 			ipif->ipif_joined_allhosts = 1;
15532 		}
15533 
15534 		/*
15535 		 * Enable multicast for the solicited node multicast address
15536 		 */
15537 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
15538 			err = ip_addmulti_v6(&v6solmc, ill, ipif->ipif_zoneid,
15539 			    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
15540 			if (err != 0) {
15541 				ip0dbg(("ipif_multicast_up: solicited MC"
15542 				    " failed %d\n", err));
15543 				if (ipif->ipif_joined_allhosts) {
15544 					(void) ip_delmulti_v6(&v6allmc, ill,
15545 					    ipif->ipif_zoneid, B_TRUE, B_TRUE);
15546 					ipif->ipif_joined_allhosts = 0;
15547 				}
15548 				return;
15549 			}
15550 		}
15551 	} else {
15552 		if (ipif->ipif_lcl_addr == INADDR_ANY || IS_UNDER_IPMP(ill))
15553 			return;
15554 
15555 		/* Join the all hosts multicast address */
15556 		ip1dbg(("ipif_multicast_up - addmulti\n"));
15557 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
15558 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
15559 		if (err) {
15560 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
15561 			return;
15562 		}
15563 	}
15564 	ipif->ipif_multicast_up = 1;
15565 }
15566 
15567 /*
15568  * Blow away any multicast groups that we joined in ipif_multicast_up().
15569  * (Explicit memberships are blown away in ill_leave_multicast() when the
15570  * ill is brought down.)
15571  */
15572 void
15573 ipif_multicast_down(ipif_t *ipif)
15574 {
15575 	int err;
15576 
15577 	ASSERT(IAM_WRITER_IPIF(ipif));
15578 
15579 	ip1dbg(("ipif_multicast_down\n"));
15580 	if (!ipif->ipif_multicast_up)
15581 		return;
15582 
15583 	ip1dbg(("ipif_multicast_down - delmulti\n"));
15584 
15585 	if (!ipif->ipif_isv6) {
15586 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
15587 		    B_TRUE);
15588 		if (err != 0)
15589 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
15590 
15591 		ipif->ipif_multicast_up = 0;
15592 		return;
15593 	}
15594 
15595 	/*
15596 	 * Leave the all-hosts multicast address.
15597 	 */
15598 	if (ipif->ipif_joined_allhosts) {
15599 		err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
15600 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
15601 		if (err != 0) {
15602 			ip0dbg(("ipif_multicast_down: all_hosts_mcast "
15603 			    "failed %d\n", err));
15604 		}
15605 		ipif->ipif_joined_allhosts = 0;
15606 	}
15607 
15608 	/*
15609 	 * Disable multicast for the solicited node multicast address
15610 	 */
15611 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
15612 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
15613 
15614 		ipv6_multi.s6_addr32[3] |=
15615 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
15616 
15617 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
15618 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
15619 		if (err != 0) {
15620 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
15621 			    err));
15622 		}
15623 	}
15624 
15625 	ipif->ipif_multicast_up = 0;
15626 }
15627 
15628 /*
15629  * Used when an interface comes up to recreate any extra routes on this
15630  * interface.
15631  */
15632 static ire_t **
15633 ipif_recover_ire(ipif_t *ipif)
15634 {
15635 	mblk_t	*mp;
15636 	ire_t	**ipif_saved_irep;
15637 	ire_t	**irep;
15638 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15639 
15640 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
15641 	    ipif->ipif_id));
15642 
15643 	mutex_enter(&ipif->ipif_saved_ire_lock);
15644 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
15645 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
15646 	if (ipif_saved_irep == NULL) {
15647 		mutex_exit(&ipif->ipif_saved_ire_lock);
15648 		return (NULL);
15649 	}
15650 
15651 	irep = ipif_saved_irep;
15652 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
15653 		ire_t		*ire;
15654 		queue_t		*rfq;
15655 		queue_t		*stq;
15656 		ifrt_t		*ifrt;
15657 		uchar_t		*src_addr;
15658 		uchar_t		*gateway_addr;
15659 		ushort_t	type;
15660 
15661 		/*
15662 		 * When the ire was initially created and then added in
15663 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
15664 		 * in the case of a traditional interface route, or as one of
15665 		 * the IRE_OFFSUBNET types (with the exception of
15666 		 * IRE_HOST types ire which is created by icmp_redirect() and
15667 		 * which we don't need to save or recover).  In the case where
15668 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
15669 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
15670 		 * to satisfy software like GateD and Sun Cluster which creates
15671 		 * routes using the the loopback interface's address as a
15672 		 * gateway.
15673 		 *
15674 		 * As ifrt->ifrt_type reflects the already updated ire_type,
15675 		 * ire_create() will be called in the same way here as
15676 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
15677 		 * the route looks like a traditional interface route (where
15678 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
15679 		 * the saved ifrt->ifrt_type.  This means that in the case where
15680 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
15681 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
15682 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
15683 		 */
15684 		ifrt = (ifrt_t *)mp->b_rptr;
15685 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
15686 		if (ifrt->ifrt_type & IRE_INTERFACE) {
15687 			rfq = NULL;
15688 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
15689 			    ? ipif->ipif_rq : ipif->ipif_wq;
15690 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
15691 			    ? (uint8_t *)&ifrt->ifrt_src_addr
15692 			    : (uint8_t *)&ipif->ipif_src_addr;
15693 			gateway_addr = NULL;
15694 			type = ipif->ipif_net_type;
15695 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
15696 			/* Recover multiroute broadcast IRE. */
15697 			rfq = ipif->ipif_rq;
15698 			stq = ipif->ipif_wq;
15699 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
15700 			    ? (uint8_t *)&ifrt->ifrt_src_addr
15701 			    : (uint8_t *)&ipif->ipif_src_addr;
15702 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
15703 			type = ifrt->ifrt_type;
15704 		} else {
15705 			rfq = NULL;
15706 			stq = NULL;
15707 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
15708 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
15709 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
15710 			type = ifrt->ifrt_type;
15711 		}
15712 
15713 		/*
15714 		 * Create a copy of the IRE with the saved address and netmask.
15715 		 */
15716 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
15717 		    "0x%x/0x%x\n",
15718 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
15719 		    ntohl(ifrt->ifrt_addr),
15720 		    ntohl(ifrt->ifrt_mask)));
15721 		ire = ire_create(
15722 		    (uint8_t *)&ifrt->ifrt_addr,
15723 		    (uint8_t *)&ifrt->ifrt_mask,
15724 		    src_addr,
15725 		    gateway_addr,
15726 		    &ifrt->ifrt_max_frag,
15727 		    NULL,
15728 		    rfq,
15729 		    stq,
15730 		    type,
15731 		    ipif,
15732 		    0,
15733 		    0,
15734 		    0,
15735 		    ifrt->ifrt_flags,
15736 		    &ifrt->ifrt_iulp_info,
15737 		    NULL,
15738 		    NULL,
15739 		    ipst);
15740 
15741 		if (ire == NULL) {
15742 			mutex_exit(&ipif->ipif_saved_ire_lock);
15743 			kmem_free(ipif_saved_irep,
15744 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
15745 			return (NULL);
15746 		}
15747 
15748 		/*
15749 		 * Some software (for example, GateD and Sun Cluster) attempts
15750 		 * to create (what amount to) IRE_PREFIX routes with the
15751 		 * loopback address as the gateway.  This is primarily done to
15752 		 * set up prefixes with the RTF_REJECT flag set (for example,
15753 		 * when generating aggregate routes.)
15754 		 *
15755 		 * If the IRE type (as defined by ipif->ipif_net_type) is
15756 		 * IRE_LOOPBACK, then we map the request into a
15757 		 * IRE_IF_NORESOLVER.
15758 		 */
15759 		if (ipif->ipif_net_type == IRE_LOOPBACK)
15760 			ire->ire_type = IRE_IF_NORESOLVER;
15761 		/*
15762 		 * ire held by ire_add, will be refreled' towards the
15763 		 * the end of ipif_up_done
15764 		 */
15765 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
15766 		*irep = ire;
15767 		irep++;
15768 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
15769 	}
15770 	mutex_exit(&ipif->ipif_saved_ire_lock);
15771 	return (ipif_saved_irep);
15772 }
15773 
15774 /*
15775  * Used to set the netmask and broadcast address to default values when the
15776  * interface is brought up.  (Always called as writer.)
15777  */
15778 static void
15779 ipif_set_default(ipif_t *ipif)
15780 {
15781 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
15782 
15783 	if (!ipif->ipif_isv6) {
15784 		/*
15785 		 * Interface holds an IPv4 address. Default
15786 		 * mask is the natural netmask.
15787 		 */
15788 		if (!ipif->ipif_net_mask) {
15789 			ipaddr_t	v4mask;
15790 
15791 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
15792 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
15793 		}
15794 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
15795 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
15796 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
15797 		} else {
15798 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
15799 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
15800 		}
15801 		/*
15802 		 * NOTE: SunOS 4.X does this even if the broadcast address
15803 		 * has been already set thus we do the same here.
15804 		 */
15805 		if (ipif->ipif_flags & IPIF_BROADCAST) {
15806 			ipaddr_t	v4addr;
15807 
15808 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
15809 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
15810 		}
15811 	} else {
15812 		/*
15813 		 * Interface holds an IPv6-only address.  Default
15814 		 * mask is all-ones.
15815 		 */
15816 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
15817 			ipif->ipif_v6net_mask = ipv6_all_ones;
15818 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
15819 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
15820 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
15821 		} else {
15822 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
15823 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
15824 		}
15825 	}
15826 }
15827 
15828 /*
15829  * Return 0 if this address can be used as local address without causing
15830  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
15831  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
15832  * Note that the same IPv6 link-local address is allowed as long as the ills
15833  * are not on the same link.
15834  */
15835 int
15836 ip_addr_availability_check(ipif_t *new_ipif)
15837 {
15838 	in6_addr_t our_v6addr;
15839 	ill_t *ill;
15840 	ipif_t *ipif;
15841 	ill_walk_context_t ctx;
15842 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
15843 
15844 	ASSERT(IAM_WRITER_IPIF(new_ipif));
15845 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
15846 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
15847 
15848 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
15849 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
15850 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
15851 		return (0);
15852 
15853 	our_v6addr = new_ipif->ipif_v6lcl_addr;
15854 
15855 	if (new_ipif->ipif_isv6)
15856 		ill = ILL_START_WALK_V6(&ctx, ipst);
15857 	else
15858 		ill = ILL_START_WALK_V4(&ctx, ipst);
15859 
15860 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
15861 		for (ipif = ill->ill_ipif; ipif != NULL;
15862 		    ipif = ipif->ipif_next) {
15863 			if ((ipif == new_ipif) ||
15864 			    !(ipif->ipif_flags & IPIF_UP) ||
15865 			    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
15866 			    !IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
15867 			    &our_v6addr))
15868 				continue;
15869 
15870 			if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
15871 				new_ipif->ipif_flags |= IPIF_UNNUMBERED;
15872 			else if (ipif->ipif_flags & IPIF_POINTOPOINT)
15873 				ipif->ipif_flags |= IPIF_UNNUMBERED;
15874 			else if ((IN6_IS_ADDR_LINKLOCAL(&our_v6addr) ||
15875 			    IN6_IS_ADDR_SITELOCAL(&our_v6addr)) &&
15876 			    !IS_ON_SAME_LAN(ill, new_ipif->ipif_ill))
15877 				continue;
15878 			else if (new_ipif->ipif_zoneid != ipif->ipif_zoneid &&
15879 			    ipif->ipif_zoneid != ALL_ZONES && IS_LOOPBACK(ill))
15880 				continue;
15881 			else if (new_ipif->ipif_ill == ill)
15882 				return (EADDRINUSE);
15883 			else
15884 				return (EADDRNOTAVAIL);
15885 		}
15886 	}
15887 
15888 	return (0);
15889 }
15890 
15891 /*
15892  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
15893  * IREs for the ipif.
15894  * When the routine returns EINPROGRESS then mp has been consumed and
15895  * the ioctl will be acked from ip_rput_dlpi.
15896  */
15897 int
15898 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
15899 {
15900 	ill_t		*ill = ipif->ipif_ill;
15901 	boolean_t 	isv6 = ipif->ipif_isv6;
15902 	int		err = 0;
15903 	boolean_t	success;
15904 	uint_t		ipif_orig_id;
15905 	ip_stack_t	*ipst = ill->ill_ipst;
15906 
15907 	ASSERT(IAM_WRITER_IPIF(ipif));
15908 
15909 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
15910 
15911 	/* Shouldn't get here if it is already up. */
15912 	if (ipif->ipif_flags & IPIF_UP)
15913 		return (EALREADY);
15914 
15915 	/*
15916 	 * If this is a request to bring up a data address on an interface
15917 	 * under IPMP, then move the address to its IPMP meta-interface and
15918 	 * try to bring it up.  One complication is that the zeroth ipif for
15919 	 * an ill is special, in that every ill always has one, and that code
15920 	 * throughout IP deferences ill->ill_ipif without holding any locks.
15921 	 */
15922 	if (IS_UNDER_IPMP(ill) && ipmp_ipif_is_dataaddr(ipif) &&
15923 	    (!ipif->ipif_isv6 || !V6_IPIF_LINKLOCAL(ipif))) {
15924 		ipif_t	*stubipif = NULL, *moveipif = NULL;
15925 		ill_t	*ipmp_ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
15926 
15927 		/*
15928 		 * The ipif being brought up should be quiesced.  If it's not,
15929 		 * something has gone amiss and we need to bail out.  (If it's
15930 		 * quiesced, we know it will remain so via IPIF_CHANGING.)
15931 		 */
15932 		mutex_enter(&ill->ill_lock);
15933 		if (!ipif_is_quiescent(ipif)) {
15934 			mutex_exit(&ill->ill_lock);
15935 			return (EINVAL);
15936 		}
15937 		mutex_exit(&ill->ill_lock);
15938 
15939 		/*
15940 		 * If we're going to need to allocate ipifs, do it prior
15941 		 * to starting the move (and grabbing locks).
15942 		 */
15943 		if (ipif->ipif_id == 0) {
15944 			moveipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
15945 			    B_FALSE);
15946 			stubipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
15947 			    B_FALSE);
15948 			if (moveipif == NULL || stubipif == NULL) {
15949 				mi_free(moveipif);
15950 				mi_free(stubipif);
15951 				return (ENOMEM);
15952 			}
15953 		}
15954 
15955 		/*
15956 		 * Grab or transfer the ipif to move.  During the move, keep
15957 		 * ill_g_lock held to prevent any ill walker threads from
15958 		 * seeing things in an inconsistent state.
15959 		 */
15960 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15961 		if (ipif->ipif_id != 0) {
15962 			ipif_remove(ipif);
15963 		} else {
15964 			ipif_transfer(ipif, moveipif, stubipif);
15965 			ipif = moveipif;
15966 		}
15967 
15968 		/*
15969 		 * Place the ipif on the IPMP ill.  If the zeroth ipif on
15970 		 * the IPMP ill is a stub (0.0.0.0 down address) then we
15971 		 * replace that one.  Otherwise, pick the next available slot.
15972 		 */
15973 		ipif->ipif_ill = ipmp_ill;
15974 		ipif_orig_id = ipif->ipif_id;
15975 
15976 		if (ipmp_ipif_is_stubaddr(ipmp_ill->ill_ipif)) {
15977 			ipif_transfer(ipif, ipmp_ill->ill_ipif, NULL);
15978 			ipif = ipmp_ill->ill_ipif;
15979 		} else {
15980 			ipif->ipif_id = -1;
15981 			if (ipif_insert(ipif, B_FALSE) != 0) {
15982 				/*
15983 				 * No more available ipif_id's -- put it back
15984 				 * on the original ill and fail the operation.
15985 				 * Since we're writer on the ill, we can be
15986 				 * sure our old slot is still available.
15987 				 */
15988 				ipif->ipif_id = ipif_orig_id;
15989 				ipif->ipif_ill = ill;
15990 				if (ipif_orig_id == 0) {
15991 					ipif_transfer(ipif, ill->ill_ipif,
15992 					    NULL);
15993 				} else {
15994 					VERIFY(ipif_insert(ipif, B_FALSE) == 0);
15995 				}
15996 				rw_exit(&ipst->ips_ill_g_lock);
15997 				return (ENOMEM);
15998 			}
15999 		}
16000 		rw_exit(&ipst->ips_ill_g_lock);
16001 
16002 		/*
16003 		 * Tell SCTP that the ipif has moved.  Note that even if we
16004 		 * had to allocate a new ipif, the original sequence id was
16005 		 * preserved and therefore SCTP won't know.
16006 		 */
16007 		sctp_move_ipif(ipif, ill, ipmp_ill);
16008 
16009 		/*
16010 		 * If the ipif being brought up was on slot zero, then we
16011 		 * first need to bring up the placeholder we stuck there.  In
16012 		 * ip_rput_dlpi_writer(), ip_arp_done(), or the recursive call
16013 		 * to ipif_up() itself, if we successfully bring up the
16014 		 * placeholder, we'll check ill_move_ipif and bring it up too.
16015 		 */
16016 		if (ipif_orig_id == 0) {
16017 			ASSERT(ill->ill_move_ipif == NULL);
16018 			ill->ill_move_ipif = ipif;
16019 			if ((err = ipif_up(ill->ill_ipif, q, mp)) == 0)
16020 				ASSERT(ill->ill_move_ipif == NULL);
16021 			if (err != EINPROGRESS)
16022 				ill->ill_move_ipif = NULL;
16023 			return (err);
16024 		}
16025 
16026 		/*
16027 		 * Bring it up on the IPMP ill.
16028 		 */
16029 		return (ipif_up(ipif, q, mp));
16030 	}
16031 
16032 	/* Skip arp/ndp for any loopback interface. */
16033 	if (ill->ill_wq != NULL) {
16034 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
16035 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
16036 
16037 		if (!ill->ill_dl_up) {
16038 			/*
16039 			 * ill_dl_up is not yet set. i.e. we are yet to
16040 			 * DL_BIND with the driver and this is the first
16041 			 * logical interface on the ill to become "up".
16042 			 * Tell the driver to get going (via DL_BIND_REQ).
16043 			 * Note that changing "significant" IFF_ flags
16044 			 * address/netmask etc cause a down/up dance, but
16045 			 * does not cause an unbind (DL_UNBIND) with the driver
16046 			 */
16047 			return (ill_dl_up(ill, ipif, mp, q));
16048 		}
16049 
16050 		/*
16051 		 * ipif_resolver_up may end up sending an
16052 		 * AR_INTERFACE_UP message to ARP, which would, in
16053 		 * turn send a DLPI message to the driver. ioctls are
16054 		 * serialized and so we cannot send more than one
16055 		 * interface up message at a time. If ipif_resolver_up
16056 		 * does send an interface up message to ARP, we get
16057 		 * EINPROGRESS and we will complete in ip_arp_done.
16058 		 */
16059 
16060 		ASSERT(connp != NULL || !CONN_Q(q));
16061 		if (connp != NULL)
16062 			mutex_enter(&connp->conn_lock);
16063 		mutex_enter(&ill->ill_lock);
16064 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
16065 		mutex_exit(&ill->ill_lock);
16066 		if (connp != NULL)
16067 			mutex_exit(&connp->conn_lock);
16068 		if (!success)
16069 			return (EINTR);
16070 
16071 		/*
16072 		 * Crank up the resolver.  For IPv6, this cranks up the
16073 		 * external resolver if one is configured, but even if an
16074 		 * external resolver isn't configured, it must be called to
16075 		 * reset DAD state.  For IPv6, if an external resolver is not
16076 		 * being used, ipif_resolver_up() will never return
16077 		 * EINPROGRESS, so we can always call ipif_ndp_up() here.
16078 		 * Note that if an external resolver is being used, there's no
16079 		 * need to call ipif_ndp_up() since it will do nothing.
16080 		 */
16081 		err = ipif_resolver_up(ipif, Res_act_initial);
16082 		if (err == EINPROGRESS) {
16083 			/* We will complete it in ip_arp_done() */
16084 			return (err);
16085 		}
16086 
16087 		if (isv6 && err == 0)
16088 			err = ipif_ndp_up(ipif, B_TRUE);
16089 
16090 		ASSERT(err != EINPROGRESS);
16091 		mp = ipsq_pending_mp_get(ipsq, &connp);
16092 		ASSERT(mp != NULL);
16093 		if (err != 0)
16094 			return (err);
16095 	} else {
16096 		/*
16097 		 * Interfaces without underlying hardware don't do duplicate
16098 		 * address detection.
16099 		 */
16100 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
16101 		ipif->ipif_addr_ready = 1;
16102 	}
16103 
16104 	err = isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif);
16105 	if (err == 0 && ill->ill_move_ipif != NULL) {
16106 		ipif = ill->ill_move_ipif;
16107 		ill->ill_move_ipif = NULL;
16108 		return (ipif_up(ipif, q, mp));
16109 	}
16110 	return (err);
16111 }
16112 
16113 /*
16114  * Perform a bind for the physical device.
16115  * When the routine returns EINPROGRESS then mp has been consumed and
16116  * the ioctl will be acked from ip_rput_dlpi.
16117  * Allocate an unbind message and save it until ipif_down.
16118  */
16119 static int
16120 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
16121 {
16122 	areq_t	*areq;
16123 	mblk_t	*areq_mp = NULL;
16124 	mblk_t	*bind_mp = NULL;
16125 	mblk_t	*unbind_mp = NULL;
16126 	conn_t	*connp;
16127 	boolean_t success;
16128 	uint16_t sap_addr;
16129 
16130 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
16131 	ASSERT(IAM_WRITER_ILL(ill));
16132 	ASSERT(mp != NULL);
16133 
16134 	/* Create a resolver cookie for ARP */
16135 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
16136 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
16137 		if (areq_mp == NULL)
16138 			return (ENOMEM);
16139 
16140 		freemsg(ill->ill_resolver_mp);
16141 		ill->ill_resolver_mp = areq_mp;
16142 		areq = (areq_t *)areq_mp->b_rptr;
16143 		sap_addr = ill->ill_sap;
16144 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
16145 	}
16146 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
16147 	    DL_BIND_REQ);
16148 	if (bind_mp == NULL)
16149 		goto bad;
16150 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
16151 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
16152 
16153 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
16154 	if (unbind_mp == NULL)
16155 		goto bad;
16156 
16157 	/*
16158 	 * Record state needed to complete this operation when the
16159 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
16160 	 */
16161 	ASSERT(WR(q)->q_next == NULL);
16162 	connp = Q_TO_CONN(q);
16163 
16164 	mutex_enter(&connp->conn_lock);
16165 	mutex_enter(&ipif->ipif_ill->ill_lock);
16166 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
16167 	mutex_exit(&ipif->ipif_ill->ill_lock);
16168 	mutex_exit(&connp->conn_lock);
16169 	if (!success)
16170 		goto bad;
16171 
16172 	/*
16173 	 * Save the unbind message for ill_dl_down(); it will be consumed when
16174 	 * the interface goes down.
16175 	 */
16176 	ASSERT(ill->ill_unbind_mp == NULL);
16177 	ill->ill_unbind_mp = unbind_mp;
16178 
16179 	ill_dlpi_send(ill, bind_mp);
16180 	/* Send down link-layer capabilities probe if not already done. */
16181 	ill_capability_probe(ill);
16182 
16183 	/*
16184 	 * Sysid used to rely on the fact that netboots set domainname
16185 	 * and the like. Now that miniroot boots aren't strictly netboots
16186 	 * and miniroot network configuration is driven from userland
16187 	 * these things still need to be set. This situation can be detected
16188 	 * by comparing the interface being configured here to the one
16189 	 * dhcifname was set to reference by the boot loader. Once sysid is
16190 	 * converted to use dhcp_ipc_getinfo() this call can go away.
16191 	 */
16192 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
16193 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
16194 	    (strlen(srpc_domain) == 0)) {
16195 		if (dhcpinit() != 0)
16196 			cmn_err(CE_WARN, "no cached dhcp response");
16197 	}
16198 
16199 	/*
16200 	 * This operation will complete in ip_rput_dlpi with either
16201 	 * a DL_BIND_ACK or DL_ERROR_ACK.
16202 	 */
16203 	return (EINPROGRESS);
16204 bad:
16205 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
16206 
16207 	freemsg(bind_mp);
16208 	freemsg(unbind_mp);
16209 	return (ENOMEM);
16210 }
16211 
16212 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
16213 
16214 /*
16215  * DLPI and ARP is up.
16216  * Create all the IREs associated with an interface bring up multicast.
16217  * Set the interface flag and finish other initialization
16218  * that potentially had to be differed to after DL_BIND_ACK.
16219  */
16220 int
16221 ipif_up_done(ipif_t *ipif)
16222 {
16223 	ire_t	*ire_array[20];
16224 	ire_t	**irep = ire_array;
16225 	ire_t	**irep1;
16226 	ipaddr_t net_mask = 0;
16227 	ipaddr_t subnet_mask, route_mask;
16228 	ill_t	*ill = ipif->ipif_ill;
16229 	queue_t	*stq;
16230 	ipif_t	 *src_ipif;
16231 	ipif_t   *tmp_ipif;
16232 	boolean_t	flush_ire_cache = B_TRUE;
16233 	int	err = 0;
16234 	ire_t	**ipif_saved_irep = NULL;
16235 	int ipif_saved_ire_cnt;
16236 	int	cnt;
16237 	boolean_t	src_ipif_held = B_FALSE;
16238 	boolean_t	loopback = B_FALSE;
16239 	ip_stack_t	*ipst = ill->ill_ipst;
16240 
16241 	ip1dbg(("ipif_up_done(%s:%u)\n",
16242 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
16243 	/* Check if this is a loopback interface */
16244 	if (ipif->ipif_ill->ill_wq == NULL)
16245 		loopback = B_TRUE;
16246 
16247 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
16248 	/*
16249 	 * If all other interfaces for this ill are down or DEPRECATED,
16250 	 * or otherwise unsuitable for source address selection, remove
16251 	 * any IRE_CACHE entries for this ill to make sure source
16252 	 * address selection gets to take this new ipif into account.
16253 	 * No need to hold ill_lock while traversing the ipif list since
16254 	 * we are writer
16255 	 */
16256 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
16257 	    tmp_ipif = tmp_ipif->ipif_next) {
16258 		if (((tmp_ipif->ipif_flags &
16259 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
16260 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
16261 		    (tmp_ipif == ipif))
16262 			continue;
16263 		/* first useable pre-existing interface */
16264 		flush_ire_cache = B_FALSE;
16265 		break;
16266 	}
16267 	if (flush_ire_cache)
16268 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
16269 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
16270 
16271 	/*
16272 	 * Figure out which way the send-to queue should go.  Only
16273 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
16274 	 * should show up here.
16275 	 */
16276 	switch (ill->ill_net_type) {
16277 	case IRE_IF_RESOLVER:
16278 		stq = ill->ill_rq;
16279 		break;
16280 	case IRE_IF_NORESOLVER:
16281 	case IRE_LOOPBACK:
16282 		stq = ill->ill_wq;
16283 		break;
16284 	default:
16285 		return (EINVAL);
16286 	}
16287 
16288 	if (IS_LOOPBACK(ill)) {
16289 		/*
16290 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
16291 		 * ipif_lookup_on_name(), but in the case of zones we can have
16292 		 * several loopback addresses on lo0. So all the interfaces with
16293 		 * loopback addresses need to be marked IRE_LOOPBACK.
16294 		 */
16295 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
16296 		    htonl(INADDR_LOOPBACK))
16297 			ipif->ipif_ire_type = IRE_LOOPBACK;
16298 		else
16299 			ipif->ipif_ire_type = IRE_LOCAL;
16300 	}
16301 
16302 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST) ||
16303 	    ((ipif->ipif_flags & IPIF_DEPRECATED) &&
16304 	    !(ipif->ipif_flags & IPIF_NOFAILOVER))) {
16305 		/*
16306 		 * Can't use our source address. Select a different
16307 		 * source address for the IRE_INTERFACE and IRE_LOCAL
16308 		 */
16309 		src_ipif = ipif_select_source(ipif->ipif_ill,
16310 		    ipif->ipif_subnet, ipif->ipif_zoneid);
16311 		if (src_ipif == NULL)
16312 			src_ipif = ipif;	/* Last resort */
16313 		else
16314 			src_ipif_held = B_TRUE;
16315 	} else {
16316 		src_ipif = ipif;
16317 	}
16318 
16319 	/* Create all the IREs associated with this interface */
16320 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
16321 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
16322 
16323 		/*
16324 		 * If we're on a labeled system then make sure that zone-
16325 		 * private addresses have proper remote host database entries.
16326 		 */
16327 		if (is_system_labeled() &&
16328 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
16329 		    !tsol_check_interface_address(ipif))
16330 			return (EINVAL);
16331 
16332 		/* Register the source address for __sin6_src_id */
16333 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
16334 		    ipif->ipif_zoneid, ipst);
16335 		if (err != 0) {
16336 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
16337 			return (err);
16338 		}
16339 
16340 		/* If the interface address is set, create the local IRE. */
16341 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
16342 		    (void *)ipif,
16343 		    ipif->ipif_ire_type,
16344 		    ntohl(ipif->ipif_lcl_addr)));
16345 		*irep++ = ire_create(
16346 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
16347 		    (uchar_t *)&ip_g_all_ones,		/* mask */
16348 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
16349 		    NULL,				/* no gateway */
16350 		    &ip_loopback_mtuplus,		/* max frag size */
16351 		    NULL,
16352 		    ipif->ipif_rq,			/* recv-from queue */
16353 		    NULL,				/* no send-to queue */
16354 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
16355 		    ipif,
16356 		    0,
16357 		    0,
16358 		    0,
16359 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
16360 		    RTF_PRIVATE : 0,
16361 		    &ire_uinfo_null,
16362 		    NULL,
16363 		    NULL,
16364 		    ipst);
16365 	} else {
16366 		ip1dbg((
16367 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
16368 		    ipif->ipif_ire_type,
16369 		    ntohl(ipif->ipif_lcl_addr),
16370 		    (uint_t)ipif->ipif_flags));
16371 	}
16372 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
16373 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
16374 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
16375 	} else {
16376 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
16377 	}
16378 
16379 	subnet_mask = ipif->ipif_net_mask;
16380 
16381 	/*
16382 	 * If mask was not specified, use natural netmask of
16383 	 * interface address. Also, store this mask back into the
16384 	 * ipif struct.
16385 	 */
16386 	if (subnet_mask == 0) {
16387 		subnet_mask = net_mask;
16388 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
16389 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
16390 		    ipif->ipif_v6subnet);
16391 	}
16392 
16393 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
16394 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
16395 	    ipif->ipif_subnet != INADDR_ANY) {
16396 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
16397 
16398 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
16399 			route_mask = IP_HOST_MASK;
16400 		} else {
16401 			route_mask = subnet_mask;
16402 		}
16403 
16404 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
16405 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
16406 		    (void *)ipif, (void *)ill,
16407 		    ill->ill_net_type,
16408 		    ntohl(ipif->ipif_subnet)));
16409 		*irep++ = ire_create(
16410 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
16411 		    (uchar_t *)&route_mask,		/* mask */
16412 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
16413 		    NULL,				/* no gateway */
16414 		    &ipif->ipif_mtu,			/* max frag */
16415 		    NULL,
16416 		    NULL,				/* no recv queue */
16417 		    stq,				/* send-to queue */
16418 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
16419 		    ipif,
16420 		    0,
16421 		    0,
16422 		    0,
16423 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
16424 		    &ire_uinfo_null,
16425 		    NULL,
16426 		    NULL,
16427 		    ipst);
16428 	}
16429 
16430 	/*
16431 	 * Create any necessary broadcast IREs.
16432 	 */
16433 	if (ipif->ipif_flags & IPIF_BROADCAST)
16434 		irep = ipif_create_bcast_ires(ipif, irep);
16435 
16436 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
16437 
16438 	/* If an earlier ire_create failed, get out now */
16439 	for (irep1 = irep; irep1 > ire_array; ) {
16440 		irep1--;
16441 		if (*irep1 == NULL) {
16442 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
16443 			err = ENOMEM;
16444 			goto bad;
16445 		}
16446 	}
16447 
16448 	/*
16449 	 * Need to atomically check for IP address availability under
16450 	 * ip_addr_avail_lock.  ill_g_lock is held as reader to ensure no new
16451 	 * ills or new ipifs can be added while we are checking availability.
16452 	 */
16453 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16454 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
16455 	/* Mark it up, and increment counters. */
16456 	ipif->ipif_flags |= IPIF_UP;
16457 	ill->ill_ipif_up_count++;
16458 	err = ip_addr_availability_check(ipif);
16459 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
16460 	rw_exit(&ipst->ips_ill_g_lock);
16461 
16462 	if (err != 0) {
16463 		/*
16464 		 * Our address may already be up on the same ill. In this case,
16465 		 * the ARP entry for our ipif replaced the one for the other
16466 		 * ipif. So we don't want to delete it (otherwise the other ipif
16467 		 * would be unable to send packets).
16468 		 * ip_addr_availability_check() identifies this case for us and
16469 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
16470 		 * which is the expected error code.
16471 		 */
16472 		if (err == EADDRINUSE) {
16473 			freemsg(ipif->ipif_arp_del_mp);
16474 			ipif->ipif_arp_del_mp = NULL;
16475 			err = EADDRNOTAVAIL;
16476 		}
16477 		ill->ill_ipif_up_count--;
16478 		ipif->ipif_flags &= ~IPIF_UP;
16479 		goto bad;
16480 	}
16481 
16482 	/*
16483 	 * Add in all newly created IREs.  ire_create_bcast() has
16484 	 * already checked for duplicates of the IRE_BROADCAST type.
16485 	 */
16486 	for (irep1 = irep; irep1 > ire_array; ) {
16487 		irep1--;
16488 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
16489 		/*
16490 		 * refheld by ire_add. refele towards the end of the func
16491 		 */
16492 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
16493 	}
16494 
16495 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
16496 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
16497 	ipif_saved_irep = ipif_recover_ire(ipif);
16498 
16499 	if (!loopback) {
16500 		/*
16501 		 * If the broadcast address has been set, make sure it makes
16502 		 * sense based on the interface address.
16503 		 * Only match on ill since we are sharing broadcast addresses.
16504 		 */
16505 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
16506 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
16507 			ire_t	*ire;
16508 
16509 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
16510 			    IRE_BROADCAST, ipif, ALL_ZONES,
16511 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
16512 
16513 			if (ire == NULL) {
16514 				/*
16515 				 * If there isn't a matching broadcast IRE,
16516 				 * revert to the default for this netmask.
16517 				 */
16518 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
16519 				mutex_enter(&ipif->ipif_ill->ill_lock);
16520 				ipif_set_default(ipif);
16521 				mutex_exit(&ipif->ipif_ill->ill_lock);
16522 			} else {
16523 				ire_refrele(ire);
16524 			}
16525 		}
16526 
16527 	}
16528 
16529 	if (ill->ill_need_recover_multicast) {
16530 		/*
16531 		 * Need to recover all multicast memberships in the driver.
16532 		 * This had to be deferred until we had attached.  The same
16533 		 * code exists in ipif_up_done_v6() to recover IPv6
16534 		 * memberships.
16535 		 *
16536 		 * Note that it would be preferable to unconditionally do the
16537 		 * ill_recover_multicast() in ill_dl_up(), but we cannot do
16538 		 * that since ill_join_allmulti() depends on ill_dl_up being
16539 		 * set, and it is not set until we receive a DL_BIND_ACK after
16540 		 * having called ill_dl_up().
16541 		 */
16542 		ill_recover_multicast(ill);
16543 	}
16544 
16545 	if (ill->ill_ipif_up_count == 1) {
16546 		/*
16547 		 * Since the interface is now up, it may now be active.
16548 		 */
16549 		if (IS_UNDER_IPMP(ill))
16550 			ipmp_ill_refresh_active(ill);
16551 
16552 		/*
16553 		 * If this is an IPMP interface, we may now be able to
16554 		 * establish ARP entries.
16555 		 */
16556 		if (IS_IPMP(ill))
16557 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
16558 	}
16559 
16560 	/* Join the allhosts multicast address */
16561 	ipif_multicast_up(ipif);
16562 
16563 	/*
16564 	 * See if anybody else would benefit from our new ipif.
16565 	 */
16566 	if (!loopback &&
16567 	    !(ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
16568 		ill_update_source_selection(ill);
16569 	}
16570 
16571 	for (irep1 = irep; irep1 > ire_array; ) {
16572 		irep1--;
16573 		if (*irep1 != NULL) {
16574 			/* was held in ire_add */
16575 			ire_refrele(*irep1);
16576 		}
16577 	}
16578 
16579 	cnt = ipif_saved_ire_cnt;
16580 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
16581 		if (*irep1 != NULL) {
16582 			/* was held in ire_add */
16583 			ire_refrele(*irep1);
16584 		}
16585 	}
16586 
16587 	if (!loopback && ipif->ipif_addr_ready) {
16588 		/* Broadcast an address mask reply. */
16589 		ipif_mask_reply(ipif);
16590 	}
16591 	if (ipif_saved_irep != NULL) {
16592 		kmem_free(ipif_saved_irep,
16593 		    ipif_saved_ire_cnt * sizeof (ire_t *));
16594 	}
16595 	if (src_ipif_held)
16596 		ipif_refrele(src_ipif);
16597 
16598 	/*
16599 	 * This had to be deferred until we had bound.  Tell routing sockets and
16600 	 * others that this interface is up if it looks like the address has
16601 	 * been validated.  Otherwise, if it isn't ready yet, wait for
16602 	 * duplicate address detection to do its thing.
16603 	 */
16604 	if (ipif->ipif_addr_ready)
16605 		ipif_up_notify(ipif);
16606 	return (0);
16607 
16608 bad:
16609 	ip1dbg(("ipif_up_done: FAILED \n"));
16610 
16611 	while (irep > ire_array) {
16612 		irep--;
16613 		if (*irep != NULL)
16614 			ire_delete(*irep);
16615 	}
16616 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
16617 
16618 	if (ipif_saved_irep != NULL) {
16619 		kmem_free(ipif_saved_irep,
16620 		    ipif_saved_ire_cnt * sizeof (ire_t *));
16621 	}
16622 	if (src_ipif_held)
16623 		ipif_refrele(src_ipif);
16624 
16625 	ipif_resolver_down(ipif);
16626 	return (err);
16627 }
16628 
16629 /*
16630  * Turn off the ARP with the ILLF_NOARP flag.
16631  */
16632 static int
16633 ill_arp_off(ill_t *ill)
16634 {
16635 	mblk_t	*arp_off_mp = NULL;
16636 	mblk_t	*arp_on_mp = NULL;
16637 
16638 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
16639 
16640 	ASSERT(IAM_WRITER_ILL(ill));
16641 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
16642 
16643 	/*
16644 	 * If the on message is still around we've already done
16645 	 * an arp_off without doing an arp_on thus there is no
16646 	 * work needed.
16647 	 */
16648 	if (ill->ill_arp_on_mp != NULL)
16649 		return (0);
16650 
16651 	/*
16652 	 * Allocate an ARP on message (to be saved) and an ARP off message
16653 	 */
16654 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
16655 	if (!arp_off_mp)
16656 		return (ENOMEM);
16657 
16658 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
16659 	if (!arp_on_mp)
16660 		goto failed;
16661 
16662 	ASSERT(ill->ill_arp_on_mp == NULL);
16663 	ill->ill_arp_on_mp = arp_on_mp;
16664 
16665 	/* Send an AR_INTERFACE_OFF request */
16666 	putnext(ill->ill_rq, arp_off_mp);
16667 	return (0);
16668 failed:
16669 
16670 	if (arp_off_mp)
16671 		freemsg(arp_off_mp);
16672 	return (ENOMEM);
16673 }
16674 
16675 /*
16676  * Turn on ARP by turning off the ILLF_NOARP flag.
16677  */
16678 static int
16679 ill_arp_on(ill_t *ill)
16680 {
16681 	mblk_t	*mp;
16682 
16683 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
16684 
16685 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
16686 
16687 	ASSERT(IAM_WRITER_ILL(ill));
16688 	/*
16689 	 * Send an AR_INTERFACE_ON request if we have already done
16690 	 * an arp_off (which allocated the message).
16691 	 */
16692 	if (ill->ill_arp_on_mp != NULL) {
16693 		mp = ill->ill_arp_on_mp;
16694 		ill->ill_arp_on_mp = NULL;
16695 		putnext(ill->ill_rq, mp);
16696 	}
16697 	return (0);
16698 }
16699 
16700 /*
16701  * Checks for availbility of a usable source address (if there is one) when the
16702  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
16703  * this selection is done regardless of the destination.
16704  */
16705 boolean_t
16706 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
16707 {
16708 	uint_t	ifindex;
16709 	ipif_t	*ipif = NULL;
16710 	ill_t	*uill;
16711 	boolean_t isv6;
16712 	ip_stack_t	*ipst = ill->ill_ipst;
16713 
16714 	ASSERT(ill != NULL);
16715 
16716 	isv6 = ill->ill_isv6;
16717 	ifindex = ill->ill_usesrc_ifindex;
16718 	if (ifindex != 0) {
16719 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
16720 		    NULL, ipst);
16721 		if (uill == NULL)
16722 			return (NULL);
16723 		mutex_enter(&uill->ill_lock);
16724 		for (ipif = uill->ill_ipif; ipif != NULL;
16725 		    ipif = ipif->ipif_next) {
16726 			if (!IPIF_CAN_LOOKUP(ipif))
16727 				continue;
16728 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
16729 				continue;
16730 			if (!(ipif->ipif_flags & IPIF_UP))
16731 				continue;
16732 			if (ipif->ipif_zoneid != zoneid)
16733 				continue;
16734 			if ((isv6 &&
16735 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
16736 			    (ipif->ipif_lcl_addr == INADDR_ANY))
16737 				continue;
16738 			mutex_exit(&uill->ill_lock);
16739 			ill_refrele(uill);
16740 			return (B_TRUE);
16741 		}
16742 		mutex_exit(&uill->ill_lock);
16743 		ill_refrele(uill);
16744 	}
16745 	return (B_FALSE);
16746 }
16747 
16748 /*
16749  * IP source address type, sorted from worst to best.  For a given type,
16750  * always prefer IP addresses on the same subnet.  All-zones addresses are
16751  * suboptimal because they pose problems with unlabeled destinations.
16752  */
16753 typedef enum {
16754 	IPIF_NONE,
16755 	IPIF_DIFFNET_DEPRECATED, 	/* deprecated and different subnet */
16756 	IPIF_SAMENET_DEPRECATED, 	/* deprecated and same subnet */
16757 	IPIF_DIFFNET_ALLZONES,		/* allzones and different subnet */
16758 	IPIF_SAMENET_ALLZONES,		/* allzones and same subnet */
16759 	IPIF_DIFFNET,			/* normal and different subnet */
16760 	IPIF_SAMENET			/* normal and same subnet */
16761 } ipif_type_t;
16762 
16763 /*
16764  * Pick the optimal ipif on `ill' for sending to destination `dst' from zone
16765  * `zoneid'.  We rate usable ipifs from low -> high as per the ipif_type_t
16766  * enumeration, and return the highest-rated ipif.  If there's a tie, we pick
16767  * the first one, unless IPMP is used in which case we round-robin among them;
16768  * see below for more.
16769  *
16770  * Returns NULL if there is no suitable source address for the ill.
16771  * This only occurs when there is no valid source address for the ill.
16772  */
16773 ipif_t *
16774 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
16775 {
16776 	ill_t	*usill = NULL;
16777 	ill_t	*ipmp_ill = NULL;
16778 	ipif_t	*start_ipif, *next_ipif, *ipif, *best_ipif;
16779 	ipif_type_t type, best_type;
16780 	tsol_tpc_t *src_rhtp, *dst_rhtp;
16781 	ip_stack_t *ipst = ill->ill_ipst;
16782 	boolean_t samenet;
16783 
16784 	if (ill->ill_usesrc_ifindex != 0) {
16785 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
16786 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
16787 		if (usill != NULL)
16788 			ill = usill;	/* Select source from usesrc ILL */
16789 		else
16790 			return (NULL);
16791 	}
16792 
16793 	/*
16794 	 * Test addresses should never be used for source address selection,
16795 	 * so if we were passed one, switch to the IPMP meta-interface.
16796 	 */
16797 	if (IS_UNDER_IPMP(ill)) {
16798 		if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL)
16799 			ill = ipmp_ill;	/* Select source from IPMP ill */
16800 		else
16801 			return (NULL);
16802 	}
16803 
16804 	/*
16805 	 * If we're dealing with an unlabeled destination on a labeled system,
16806 	 * make sure that we ignore source addresses that are incompatible with
16807 	 * the destination's default label.  That destination's default label
16808 	 * must dominate the minimum label on the source address.
16809 	 */
16810 	dst_rhtp = NULL;
16811 	if (is_system_labeled()) {
16812 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
16813 		if (dst_rhtp == NULL)
16814 			return (NULL);
16815 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
16816 			TPC_RELE(dst_rhtp);
16817 			dst_rhtp = NULL;
16818 		}
16819 	}
16820 
16821 	/*
16822 	 * Hold the ill_g_lock as reader. This makes sure that no ipif/ill
16823 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
16824 	 * After selecting the right ipif, under ill_lock make sure ipif is
16825 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
16826 	 * we retry. Inside the loop we still need to check for CONDEMNED,
16827 	 * but not under a lock.
16828 	 */
16829 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16830 retry:
16831 	/*
16832 	 * For source address selection, we treat the ipif list as circular
16833 	 * and continue until we get back to where we started.  This allows
16834 	 * IPMP to vary source address selection (which improves inbound load
16835 	 * spreading) by caching its last ending point and starting from
16836 	 * there.  NOTE: we don't have to worry about ill_src_ipif changing
16837 	 * ills since that can't happen on the IPMP ill.
16838 	 */
16839 	start_ipif = ill->ill_ipif;
16840 	if (IS_IPMP(ill) && ill->ill_src_ipif != NULL)
16841 		start_ipif = ill->ill_src_ipif;
16842 
16843 	ipif = start_ipif;
16844 	best_ipif = NULL;
16845 	best_type = IPIF_NONE;
16846 	do {
16847 		if ((next_ipif = ipif->ipif_next) == NULL)
16848 			next_ipif = ill->ill_ipif;
16849 
16850 		if (!IPIF_CAN_LOOKUP(ipif))
16851 			continue;
16852 		/* Always skip NOLOCAL and ANYCAST interfaces */
16853 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
16854 			continue;
16855 		if (!(ipif->ipif_flags & IPIF_UP) || !ipif->ipif_addr_ready)
16856 			continue;
16857 		if (ipif->ipif_zoneid != zoneid &&
16858 		    ipif->ipif_zoneid != ALL_ZONES)
16859 			continue;
16860 
16861 		/*
16862 		 * Interfaces with 0.0.0.0 address are allowed to be UP, but
16863 		 * are not valid as source addresses.
16864 		 */
16865 		if (ipif->ipif_lcl_addr == INADDR_ANY)
16866 			continue;
16867 
16868 		/*
16869 		 * Check compatibility of local address for destination's
16870 		 * default label if we're on a labeled system.	Incompatible
16871 		 * addresses can't be used at all.
16872 		 */
16873 		if (dst_rhtp != NULL) {
16874 			boolean_t incompat;
16875 
16876 			src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
16877 			    IPV4_VERSION, B_FALSE);
16878 			if (src_rhtp == NULL)
16879 				continue;
16880 			incompat = src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
16881 			    src_rhtp->tpc_tp.tp_doi !=
16882 			    dst_rhtp->tpc_tp.tp_doi ||
16883 			    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
16884 			    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
16885 			    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
16886 			    src_rhtp->tpc_tp.tp_sl_set_cipso));
16887 			TPC_RELE(src_rhtp);
16888 			if (incompat)
16889 				continue;
16890 		}
16891 
16892 		samenet = ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet);
16893 
16894 		if (ipif->ipif_flags & IPIF_DEPRECATED) {
16895 			type = samenet ? IPIF_SAMENET_DEPRECATED :
16896 			    IPIF_DIFFNET_DEPRECATED;
16897 		} else if (ipif->ipif_zoneid == ALL_ZONES) {
16898 			type = samenet ? IPIF_SAMENET_ALLZONES :
16899 			    IPIF_DIFFNET_ALLZONES;
16900 		} else {
16901 			type = samenet ? IPIF_SAMENET : IPIF_DIFFNET;
16902 		}
16903 
16904 		if (type > best_type) {
16905 			best_type = type;
16906 			best_ipif = ipif;
16907 			if (best_type == IPIF_SAMENET)
16908 				break; /* can't get better */
16909 		}
16910 	} while ((ipif = next_ipif) != start_ipif);
16911 
16912 	if ((ipif = best_ipif) != NULL) {
16913 		mutex_enter(&ipif->ipif_ill->ill_lock);
16914 		if (!IPIF_CAN_LOOKUP(ipif)) {
16915 			mutex_exit(&ipif->ipif_ill->ill_lock);
16916 			goto retry;
16917 		}
16918 		ipif_refhold_locked(ipif);
16919 
16920 		/*
16921 		 * For IPMP, update the source ipif rotor to the next ipif,
16922 		 * provided we can look it up.  (We must not use it if it's
16923 		 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after
16924 		 * ipif_free() checked ill_src_ipif.)
16925 		 */
16926 		if (IS_IPMP(ill) && ipif != NULL) {
16927 			next_ipif = ipif->ipif_next;
16928 			if (next_ipif != NULL && IPIF_CAN_LOOKUP(next_ipif))
16929 				ill->ill_src_ipif = next_ipif;
16930 			else
16931 				ill->ill_src_ipif = NULL;
16932 		}
16933 		mutex_exit(&ipif->ipif_ill->ill_lock);
16934 	}
16935 
16936 	rw_exit(&ipst->ips_ill_g_lock);
16937 	if (usill != NULL)
16938 		ill_refrele(usill);
16939 	if (ipmp_ill != NULL)
16940 		ill_refrele(ipmp_ill);
16941 	if (dst_rhtp != NULL)
16942 		TPC_RELE(dst_rhtp);
16943 
16944 #ifdef DEBUG
16945 	if (ipif == NULL) {
16946 		char buf1[INET6_ADDRSTRLEN];
16947 
16948 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
16949 		    ill->ill_name,
16950 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
16951 	} else {
16952 		char buf1[INET6_ADDRSTRLEN];
16953 		char buf2[INET6_ADDRSTRLEN];
16954 
16955 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
16956 		    ipif->ipif_ill->ill_name,
16957 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
16958 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
16959 		    buf2, sizeof (buf2))));
16960 	}
16961 #endif /* DEBUG */
16962 	return (ipif);
16963 }
16964 
16965 /*
16966  * If old_ipif is not NULL, see if ipif was derived from old
16967  * ipif and if so, recreate the interface route by re-doing
16968  * source address selection. This happens when ipif_down ->
16969  * ipif_update_other_ipifs calls us.
16970  *
16971  * If old_ipif is NULL, just redo the source address selection
16972  * if needed. This happens when ipif_up_done calls us.
16973  */
16974 static void
16975 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
16976 {
16977 	ire_t *ire;
16978 	ire_t *ipif_ire;
16979 	queue_t *stq;
16980 	ipif_t *nipif;
16981 	ill_t *ill;
16982 	boolean_t need_rele = B_FALSE;
16983 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
16984 
16985 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
16986 	ASSERT(IAM_WRITER_IPIF(ipif));
16987 
16988 	ill = ipif->ipif_ill;
16989 	if (!(ipif->ipif_flags &
16990 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
16991 		/*
16992 		 * Can't possibly have borrowed the source
16993 		 * from old_ipif.
16994 		 */
16995 		return;
16996 	}
16997 
16998 	/*
16999 	 * Is there any work to be done? No work if the address
17000 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
17001 	 * ipif_select_source() does not borrow addresses from
17002 	 * NOLOCAL and ANYCAST interfaces).
17003 	 */
17004 	if ((old_ipif != NULL) &&
17005 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
17006 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
17007 	    (old_ipif->ipif_flags &
17008 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
17009 		return;
17010 	}
17011 
17012 	/*
17013 	 * Perform the same checks as when creating the
17014 	 * IRE_INTERFACE in ipif_up_done.
17015 	 */
17016 	if (!(ipif->ipif_flags & IPIF_UP))
17017 		return;
17018 
17019 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
17020 	    (ipif->ipif_subnet == INADDR_ANY))
17021 		return;
17022 
17023 	ipif_ire = ipif_to_ire(ipif);
17024 	if (ipif_ire == NULL)
17025 		return;
17026 
17027 	/*
17028 	 * We know that ipif uses some other source for its
17029 	 * IRE_INTERFACE. Is it using the source of this
17030 	 * old_ipif?
17031 	 */
17032 	if (old_ipif != NULL &&
17033 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
17034 		ire_refrele(ipif_ire);
17035 		return;
17036 	}
17037 	if (ip_debug > 2) {
17038 		/* ip1dbg */
17039 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
17040 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
17041 	}
17042 
17043 	stq = ipif_ire->ire_stq;
17044 
17045 	/*
17046 	 * Can't use our source address. Select a different
17047 	 * source address for the IRE_INTERFACE.
17048 	 */
17049 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
17050 	if (nipif == NULL) {
17051 		/* Last resort - all ipif's have IPIF_NOLOCAL */
17052 		nipif = ipif;
17053 	} else {
17054 		need_rele = B_TRUE;
17055 	}
17056 
17057 	ire = ire_create(
17058 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
17059 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
17060 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
17061 	    NULL,				/* no gateway */
17062 	    &ipif->ipif_mtu,			/* max frag */
17063 	    NULL,				/* no src nce */
17064 	    NULL,				/* no recv from queue */
17065 	    stq,				/* send-to queue */
17066 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
17067 	    ipif,
17068 	    0,
17069 	    0,
17070 	    0,
17071 	    0,
17072 	    &ire_uinfo_null,
17073 	    NULL,
17074 	    NULL,
17075 	    ipst);
17076 
17077 	if (ire != NULL) {
17078 		ire_t *ret_ire;
17079 		int error;
17080 
17081 		/*
17082 		 * We don't need ipif_ire anymore. We need to delete
17083 		 * before we add so that ire_add does not detect
17084 		 * duplicates.
17085 		 */
17086 		ire_delete(ipif_ire);
17087 		ret_ire = ire;
17088 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
17089 		ASSERT(error == 0);
17090 		ASSERT(ire == ret_ire);
17091 		/* Held in ire_add */
17092 		ire_refrele(ret_ire);
17093 	}
17094 	/*
17095 	 * Either we are falling through from above or could not
17096 	 * allocate a replacement.
17097 	 */
17098 	ire_refrele(ipif_ire);
17099 	if (need_rele)
17100 		ipif_refrele(nipif);
17101 }
17102 
17103 /*
17104  * This old_ipif is going away.
17105  *
17106  * Determine if any other ipif's are using our address as
17107  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
17108  * IPIF_DEPRECATED).
17109  * Find the IRE_INTERFACE for such ipifs and recreate them
17110  * to use an different source address following the rules in
17111  * ipif_up_done.
17112  */
17113 static void
17114 ipif_update_other_ipifs(ipif_t *old_ipif)
17115 {
17116 	ipif_t	*ipif;
17117 	ill_t	*ill;
17118 	char	buf[INET6_ADDRSTRLEN];
17119 
17120 	ASSERT(IAM_WRITER_IPIF(old_ipif));
17121 
17122 	ill = old_ipif->ipif_ill;
17123 
17124 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n", ill->ill_name,
17125 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr, buf, sizeof (buf))));
17126 
17127 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17128 		if (ipif == old_ipif)
17129 			continue;
17130 		ipif_recreate_interface_routes(old_ipif, ipif);
17131 	}
17132 }
17133 
17134 /* ARGSUSED */
17135 int
17136 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
17137 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
17138 {
17139 	/*
17140 	 * ill_phyint_reinit merged the v4 and v6 into a single
17141 	 * ipsq.  We might not have been able to complete the
17142 	 * operation in ipif_set_values, if we could not become
17143 	 * exclusive.  If so restart it here.
17144 	 */
17145 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
17146 }
17147 
17148 /*
17149  * Can operate on either a module or a driver queue.
17150  * Returns an error if not a module queue.
17151  */
17152 /* ARGSUSED */
17153 int
17154 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
17155     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
17156 {
17157 	queue_t		*q1 = q;
17158 	char 		*cp;
17159 	char		interf_name[LIFNAMSIZ];
17160 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
17161 
17162 	if (q->q_next == NULL) {
17163 		ip1dbg((
17164 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
17165 		return (EINVAL);
17166 	}
17167 
17168 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
17169 		return (EALREADY);
17170 
17171 	do {
17172 		q1 = q1->q_next;
17173 	} while (q1->q_next);
17174 	cp = q1->q_qinfo->qi_minfo->mi_idname;
17175 	(void) sprintf(interf_name, "%s%d", cp, ppa);
17176 
17177 	/*
17178 	 * Here we are not going to delay the ioack until after
17179 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
17180 	 * original ioctl message before sending the requests.
17181 	 */
17182 	return (ipif_set_values(q, mp, interf_name, &ppa));
17183 }
17184 
17185 /* ARGSUSED */
17186 int
17187 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
17188     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
17189 {
17190 	return (ENXIO);
17191 }
17192 
17193 /*
17194  * Refresh all IRE_BROADCAST entries associated with `ill' to ensure the
17195  * minimum (but complete) set exist.  This is necessary when adding or
17196  * removing an interface to/from an IPMP group, since interfaces in an
17197  * IPMP group use the IRE_BROADCAST entries for the IPMP group (whenever
17198  * its test address subnets overlap with IPMP data addresses).	It's also
17199  * used to refresh the IRE_BROADCAST entries associated with the IPMP
17200  * interface when the nominated broadcast interface changes.
17201  */
17202 void
17203 ill_refresh_bcast(ill_t *ill)
17204 {
17205 	ire_t *ire_array[12];	/* max ipif_create_bcast_ires() can create */
17206 	ire_t **irep;
17207 	ipif_t *ipif;
17208 
17209 	ASSERT(!ill->ill_isv6);
17210 	ASSERT(IAM_WRITER_ILL(ill));
17211 
17212 	/*
17213 	 * Remove any old broadcast IREs.
17214 	 */
17215 	ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE, IRE_BROADCAST,
17216 	    ill_broadcast_delete, ill, ill);
17217 
17218 	/*
17219 	 * Create new ones for any ipifs that are up and broadcast-capable.
17220 	 */
17221 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17222 		if ((ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST)) !=
17223 		    (IPIF_UP|IPIF_BROADCAST))
17224 			continue;
17225 
17226 		irep = ipif_create_bcast_ires(ipif, ire_array);
17227 		while (irep-- > ire_array) {
17228 			(void) ire_add(irep, NULL, NULL, NULL, B_FALSE);
17229 			if (*irep != NULL)
17230 				ire_refrele(*irep);
17231 		}
17232 	}
17233 }
17234 
17235 /*
17236  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
17237  * `irep'.  Returns a pointer to the next free `irep' entry (just like
17238  * ire_check_and_create_bcast()).
17239  */
17240 static ire_t **
17241 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
17242 {
17243 	ipaddr_t addr;
17244 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
17245 	ipaddr_t subnetmask = ipif->ipif_net_mask;
17246 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
17247 
17248 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
17249 
17250 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
17251 
17252 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
17253 	    (ipif->ipif_flags & IPIF_NOLOCAL))
17254 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
17255 
17256 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
17257 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
17258 
17259 	/*
17260 	 * For backward compatibility, we create net broadcast IREs based on
17261 	 * the old "IP address class system", since some old machines only
17262 	 * respond to these class derived net broadcast.  However, we must not
17263 	 * create these net broadcast IREs if the subnetmask is shorter than
17264 	 * the IP address class based derived netmask.  Otherwise, we may
17265 	 * create a net broadcast address which is the same as an IP address
17266 	 * on the subnet -- and then TCP will refuse to talk to that address.
17267 	 */
17268 	if (netmask < subnetmask) {
17269 		addr = netmask & ipif->ipif_subnet;
17270 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
17271 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
17272 		    flags);
17273 	}
17274 
17275 	/*
17276 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
17277 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
17278 	 * created.  Creating these broadcast IREs will only create confusion
17279 	 * as `addr' will be the same as the IP address.
17280 	 */
17281 	if (subnetmask != 0xFFFFFFFF) {
17282 		addr = ipif->ipif_subnet;
17283 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
17284 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
17285 		    irep, flags);
17286 	}
17287 
17288 	return (irep);
17289 }
17290 
17291 /*
17292  * Broadcast IRE info structure used in the functions below.  Since we
17293  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
17294  */
17295 typedef struct bcast_ireinfo {
17296 	uchar_t		bi_type;	/* BCAST_* value from below */
17297 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
17298 			bi_needrep:1,	/* do we need to replace it? */
17299 			bi_haverep:1,	/* have we replaced it? */
17300 			bi_pad:5;
17301 	ipaddr_t	bi_addr;	/* IRE address */
17302 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
17303 } bcast_ireinfo_t;
17304 
17305 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
17306 
17307 /*
17308  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
17309  * return B_TRUE if it should immediately be used to recreate the IRE.
17310  */
17311 static boolean_t
17312 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
17313 {
17314 	ipaddr_t addr;
17315 
17316 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
17317 
17318 	switch (bireinfop->bi_type) {
17319 	case BCAST_NET:
17320 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
17321 		if (addr != bireinfop->bi_addr)
17322 			return (B_FALSE);
17323 		break;
17324 	case BCAST_SUBNET:
17325 		if (ipif->ipif_subnet != bireinfop->bi_addr)
17326 			return (B_FALSE);
17327 		break;
17328 	}
17329 
17330 	bireinfop->bi_needrep = 1;
17331 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
17332 		if (bireinfop->bi_backup == NULL)
17333 			bireinfop->bi_backup = ipif;
17334 		return (B_FALSE);
17335 	}
17336 	return (B_TRUE);
17337 }
17338 
17339 /*
17340  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
17341  * them ala ire_check_and_create_bcast().
17342  */
17343 static ire_t **
17344 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
17345 {
17346 	ipaddr_t mask, addr;
17347 
17348 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
17349 
17350 	addr = bireinfop->bi_addr;
17351 	irep = ire_create_bcast(ipif, addr, irep);
17352 
17353 	switch (bireinfop->bi_type) {
17354 	case BCAST_NET:
17355 		mask = ip_net_mask(ipif->ipif_subnet);
17356 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
17357 		break;
17358 	case BCAST_SUBNET:
17359 		mask = ipif->ipif_net_mask;
17360 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
17361 		break;
17362 	}
17363 
17364 	bireinfop->bi_haverep = 1;
17365 	return (irep);
17366 }
17367 
17368 /*
17369  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
17370  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
17371  * that are going away are still needed.  If so, have ipif_create_bcast()
17372  * recreate them (except for the deprecated case, as explained below).
17373  */
17374 static ire_t **
17375 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
17376     ire_t **irep)
17377 {
17378 	int i;
17379 	ipif_t *ipif;
17380 
17381 	ASSERT(!ill->ill_isv6);
17382 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17383 		/*
17384 		 * Skip this ipif if it's (a) the one being taken down, (b)
17385 		 * not in the same zone, or (c) has no valid local address.
17386 		 */
17387 		if (ipif == test_ipif ||
17388 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
17389 		    ipif->ipif_subnet == 0 ||
17390 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
17391 		    (IPIF_UP|IPIF_BROADCAST))
17392 			continue;
17393 
17394 		/*
17395 		 * For each dying IRE that hasn't yet been replaced, see if
17396 		 * `ipif' needs it and whether the IRE should be recreated on
17397 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
17398 		 * will return B_FALSE even if `ipif' needs the IRE on the
17399 		 * hopes that we'll later find a needy non-deprecated ipif.
17400 		 * However, the ipif is recorded in bi_backup for possible
17401 		 * subsequent use by ipif_check_bcast_ires().
17402 		 */
17403 		for (i = 0; i < BCAST_COUNT; i++) {
17404 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
17405 				continue;
17406 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
17407 				continue;
17408 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
17409 		}
17410 
17411 		/*
17412 		 * If we've replaced all of the broadcast IREs that are going
17413 		 * to be taken down, we know we're done.
17414 		 */
17415 		for (i = 0; i < BCAST_COUNT; i++) {
17416 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
17417 				break;
17418 		}
17419 		if (i == BCAST_COUNT)
17420 			break;
17421 	}
17422 	return (irep);
17423 }
17424 
17425 /*
17426  * Check if `test_ipif' (which is going away) is associated with any existing
17427  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
17428  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
17429  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
17430  *
17431  * This is necessary because broadcast IREs are shared.  In particular, a
17432  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
17433  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
17434  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
17435  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
17436  * same zone, they will share the same set of broadcast IREs.
17437  *
17438  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
17439  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
17440  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
17441  */
17442 static void
17443 ipif_check_bcast_ires(ipif_t *test_ipif)
17444 {
17445 	ill_t		*ill = test_ipif->ipif_ill;
17446 	ire_t		*ire, *ire_array[12]; 		/* see note above */
17447 	ire_t		**irep1, **irep = &ire_array[0];
17448 	uint_t 		i, willdie;
17449 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
17450 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
17451 
17452 	ASSERT(!test_ipif->ipif_isv6);
17453 	ASSERT(IAM_WRITER_IPIF(test_ipif));
17454 
17455 	/*
17456 	 * No broadcast IREs for the LOOPBACK interface
17457 	 * or others such as point to point and IPIF_NOXMIT.
17458 	 */
17459 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
17460 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
17461 		return;
17462 
17463 	bzero(bireinfo, sizeof (bireinfo));
17464 	bireinfo[0].bi_type = BCAST_ALLZEROES;
17465 	bireinfo[0].bi_addr = 0;
17466 
17467 	bireinfo[1].bi_type = BCAST_ALLONES;
17468 	bireinfo[1].bi_addr = INADDR_BROADCAST;
17469 
17470 	bireinfo[2].bi_type = BCAST_NET;
17471 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
17472 
17473 	if (test_ipif->ipif_net_mask != 0)
17474 		mask = test_ipif->ipif_net_mask;
17475 	bireinfo[3].bi_type = BCAST_SUBNET;
17476 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
17477 
17478 	/*
17479 	 * Figure out what (if any) broadcast IREs will die as a result of
17480 	 * `test_ipif' going away.  If none will die, we're done.
17481 	 */
17482 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
17483 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
17484 		    test_ipif, ALL_ZONES, NULL,
17485 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
17486 		if (ire != NULL) {
17487 			willdie++;
17488 			bireinfo[i].bi_willdie = 1;
17489 			ire_refrele(ire);
17490 		}
17491 	}
17492 
17493 	if (willdie == 0)
17494 		return;
17495 
17496 	/*
17497 	 * Walk through all the ipifs that will be affected by the dying IREs,
17498 	 * and recreate the IREs as necessary. Note that all interfaces in an
17499 	 * IPMP illgrp share the same broadcast IREs, and thus the entire
17500 	 * illgrp must be walked, starting with the IPMP meta-interface (so
17501 	 * that broadcast IREs end up on it whenever possible).
17502 	 */
17503 	if (IS_UNDER_IPMP(ill))
17504 		ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
17505 
17506 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
17507 
17508 	if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
17509 		ipmp_illgrp_t *illg = ill->ill_grp;
17510 
17511 		ill = list_head(&illg->ig_if);
17512 		for (; ill != NULL; ill = list_next(&illg->ig_if, ill)) {
17513 			for (i = 0; i < BCAST_COUNT; i++) {
17514 				if (bireinfo[i].bi_willdie &&
17515 				    !bireinfo[i].bi_haverep)
17516 					break;
17517 			}
17518 			if (i == BCAST_COUNT)
17519 				break;
17520 
17521 			irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
17522 		}
17523 	}
17524 
17525 	/*
17526 	 * Scan through the set of broadcast IREs and see if there are any
17527 	 * that we need to replace that have not yet been replaced.  If so,
17528 	 * replace them using the appropriate backup ipif.
17529 	 */
17530 	for (i = 0; i < BCAST_COUNT; i++) {
17531 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
17532 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
17533 			    &bireinfo[i], irep);
17534 	}
17535 
17536 	/*
17537 	 * If we can't create all of them, don't add any of them.  (Code in
17538 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
17539 	 * non-loopback copy and loopback copy for a given address.)
17540 	 */
17541 	for (irep1 = irep; irep1 > ire_array; ) {
17542 		irep1--;
17543 		if (*irep1 == NULL) {
17544 			ip0dbg(("ipif_check_bcast_ires: can't create "
17545 			    "IRE_BROADCAST, memory allocation failure\n"));
17546 			while (irep > ire_array) {
17547 				irep--;
17548 				if (*irep != NULL)
17549 					ire_delete(*irep);
17550 			}
17551 			return;
17552 		}
17553 	}
17554 
17555 	for (irep1 = irep; irep1 > ire_array; ) {
17556 		irep1--;
17557 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
17558 			ire_refrele(*irep1);		/* Held in ire_add */
17559 	}
17560 }
17561 
17562 /*
17563  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
17564  * from lifr_flags and the name from lifr_name.
17565  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
17566  * since ipif_lookup_on_name uses the _isv6 flags when matching.
17567  * Returns EINPROGRESS when mp has been consumed by queueing it on
17568  * ill_pending_mp and the ioctl will complete in ip_rput.
17569  *
17570  * Can operate on either a module or a driver queue.
17571  * Returns an error if not a module queue.
17572  */
17573 /* ARGSUSED */
17574 int
17575 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17576     ip_ioctl_cmd_t *ipip, void *if_req)
17577 {
17578 	ill_t	*ill = q->q_ptr;
17579 	phyint_t *phyi;
17580 	ip_stack_t *ipst;
17581 	struct lifreq *lifr = if_req;
17582 
17583 	ASSERT(ipif != NULL);
17584 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
17585 
17586 	if (q->q_next == NULL) {
17587 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
17588 		return (EINVAL);
17589 	}
17590 
17591 	/*
17592 	 * If we are not writer on 'q' then this interface exists already
17593 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
17594 	 * so return EALREADY.
17595 	 */
17596 	if (ill != ipif->ipif_ill)
17597 		return (EALREADY);
17598 
17599 	if (ill->ill_name[0] != '\0')
17600 		return (EALREADY);
17601 
17602 	/*
17603 	 * Set all the flags. Allows all kinds of override. Provide some
17604 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
17605 	 * unless there is either multicast/broadcast support in the driver
17606 	 * or it is a pt-pt link.
17607 	 */
17608 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
17609 		/* Meaningless to IP thus don't allow them to be set. */
17610 		ip1dbg(("ip_setname: EINVAL 1\n"));
17611 		return (EINVAL);
17612 	}
17613 
17614 	/*
17615 	 * If there's another ill already with the requested name, ensure
17616 	 * that it's of the same type.  Otherwise, ill_phyint_reinit() will
17617 	 * fuse together two unrelated ills, which will cause chaos.
17618 	 */
17619 	ipst = ill->ill_ipst;
17620 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
17621 	    lifr->lifr_name, NULL);
17622 	if (phyi != NULL) {
17623 		ill_t *ill_mate = phyi->phyint_illv4;
17624 
17625 		if (ill_mate == NULL)
17626 			ill_mate = phyi->phyint_illv6;
17627 		ASSERT(ill_mate != NULL);
17628 
17629 		if (ill_mate->ill_media->ip_m_mac_type !=
17630 		    ill->ill_media->ip_m_mac_type) {
17631 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
17632 			    "use the same ill name on differing media\n"));
17633 			return (EINVAL);
17634 		}
17635 	}
17636 
17637 	/*
17638 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
17639 	 * ill_bcast_addr_length info.
17640 	 */
17641 	if (!ill->ill_needs_attach &&
17642 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
17643 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
17644 	    ill->ill_bcast_addr_length == 0)) {
17645 		/* Link not broadcast/pt-pt capable i.e. no multicast */
17646 		ip1dbg(("ip_setname: EINVAL 2\n"));
17647 		return (EINVAL);
17648 	}
17649 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
17650 	    ((lifr->lifr_flags & IFF_IPV6) ||
17651 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
17652 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
17653 		ip1dbg(("ip_setname: EINVAL 3\n"));
17654 		return (EINVAL);
17655 	}
17656 	if (lifr->lifr_flags & IFF_UP) {
17657 		/* Can only be set with SIOCSLIFFLAGS */
17658 		ip1dbg(("ip_setname: EINVAL 4\n"));
17659 		return (EINVAL);
17660 	}
17661 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
17662 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
17663 		ip1dbg(("ip_setname: EINVAL 5\n"));
17664 		return (EINVAL);
17665 	}
17666 	/*
17667 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
17668 	 */
17669 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
17670 	    !(lifr->lifr_flags & IFF_IPV6) &&
17671 	    !(ipif->ipif_isv6)) {
17672 		ip1dbg(("ip_setname: EINVAL 6\n"));
17673 		return (EINVAL);
17674 	}
17675 
17676 	/*
17677 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
17678 	 * we have all the flags here. So, we assign rather than we OR.
17679 	 * We can't OR the flags here because we don't want to set
17680 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
17681 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
17682 	 * on lifr_flags value here.
17683 	 */
17684 	/*
17685 	 * This ill has not been inserted into the global list.
17686 	 * So we are still single threaded and don't need any lock
17687 	 */
17688 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS & ~IFF_DUPLICATE;
17689 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
17690 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
17691 
17692 	/* We started off as V4. */
17693 	if (ill->ill_flags & ILLF_IPV6) {
17694 		ill->ill_phyint->phyint_illv6 = ill;
17695 		ill->ill_phyint->phyint_illv4 = NULL;
17696 	}
17697 
17698 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
17699 }
17700 
17701 /* ARGSUSED */
17702 int
17703 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17704     ip_ioctl_cmd_t *ipip, void *if_req)
17705 {
17706 	/*
17707 	 * ill_phyint_reinit merged the v4 and v6 into a single
17708 	 * ipsq.  We might not have been able to complete the
17709 	 * slifname in ipif_set_values, if we could not become
17710 	 * exclusive.  If so restart it here
17711 	 */
17712 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
17713 }
17714 
17715 /*
17716  * Return a pointer to the ipif which matches the index, IP version type and
17717  * zoneid.
17718  */
17719 ipif_t *
17720 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
17721     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
17722 {
17723 	ill_t	*ill;
17724 	ipif_t	*ipif = NULL;
17725 
17726 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
17727 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
17728 
17729 	if (err != NULL)
17730 		*err = 0;
17731 
17732 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
17733 	if (ill != NULL) {
17734 		mutex_enter(&ill->ill_lock);
17735 		for (ipif = ill->ill_ipif; ipif != NULL;
17736 		    ipif = ipif->ipif_next) {
17737 			if (IPIF_CAN_LOOKUP(ipif) && (zoneid == ALL_ZONES ||
17738 			    zoneid == ipif->ipif_zoneid ||
17739 			    ipif->ipif_zoneid == ALL_ZONES)) {
17740 				ipif_refhold_locked(ipif);
17741 				break;
17742 			}
17743 		}
17744 		mutex_exit(&ill->ill_lock);
17745 		ill_refrele(ill);
17746 		if (ipif == NULL && err != NULL)
17747 			*err = ENXIO;
17748 	}
17749 	return (ipif);
17750 }
17751 
17752 /*
17753  * We first need to ensure that the new index is unique, and
17754  * then carry the change across both v4 and v6 ill representation
17755  * of the physical interface.
17756  */
17757 /* ARGSUSED */
17758 int
17759 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17760     ip_ioctl_cmd_t *ipip, void *ifreq)
17761 {
17762 	ill_t		*ill;
17763 	phyint_t	*phyi;
17764 	struct ifreq	*ifr = (struct ifreq *)ifreq;
17765 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17766 	uint_t	old_index, index;
17767 	ill_t	*ill_v4;
17768 	ill_t	*ill_v6;
17769 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
17770 
17771 	if (ipip->ipi_cmd_type == IF_CMD)
17772 		index = ifr->ifr_index;
17773 	else
17774 		index = lifr->lifr_index;
17775 
17776 	/*
17777 	 * Only allow on physical interface. Also, index zero is illegal.
17778 	 */
17779 	ill = ipif->ipif_ill;
17780 	phyi = ill->ill_phyint;
17781 	if (ipif->ipif_id != 0 || index == 0) {
17782 		return (EINVAL);
17783 	}
17784 
17785 	/* If the index is not changing, no work to do */
17786 	if (phyi->phyint_ifindex == index)
17787 		return (0);
17788 
17789 	/*
17790 	 * Use ill_lookup_on_ifindex to determine if the
17791 	 * new index is unused and if so allow the change.
17792 	 */
17793 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
17794 	    ipst);
17795 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
17796 	    ipst);
17797 	if (ill_v6 != NULL || ill_v4 != NULL) {
17798 		if (ill_v4 != NULL)
17799 			ill_refrele(ill_v4);
17800 		if (ill_v6 != NULL)
17801 			ill_refrele(ill_v6);
17802 		return (EBUSY);
17803 	}
17804 
17805 	/* The new index is unused. Set it in the phyint. */
17806 	old_index = phyi->phyint_ifindex;
17807 	phyi->phyint_ifindex = index;
17808 
17809 	/* Update SCTP's ILL list */
17810 	sctp_ill_reindex(ill, old_index);
17811 
17812 	/* Send the routing sockets message */
17813 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
17814 	if (ILL_OTHER(ill))
17815 		ip_rts_ifmsg(ILL_OTHER(ill)->ill_ipif, RTSQ_DEFAULT);
17816 
17817 	return (0);
17818 }
17819 
17820 /* ARGSUSED */
17821 int
17822 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17823     ip_ioctl_cmd_t *ipip, void *ifreq)
17824 {
17825 	struct ifreq	*ifr = (struct ifreq *)ifreq;
17826 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17827 
17828 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
17829 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17830 	/* Get the interface index */
17831 	if (ipip->ipi_cmd_type == IF_CMD) {
17832 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
17833 	} else {
17834 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
17835 	}
17836 	return (0);
17837 }
17838 
17839 /* ARGSUSED */
17840 int
17841 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17842     ip_ioctl_cmd_t *ipip, void *ifreq)
17843 {
17844 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17845 
17846 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
17847 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17848 	/* Get the interface zone */
17849 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
17850 	lifr->lifr_zoneid = ipif->ipif_zoneid;
17851 	return (0);
17852 }
17853 
17854 /*
17855  * Set the zoneid of an interface.
17856  */
17857 /* ARGSUSED */
17858 int
17859 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17860     ip_ioctl_cmd_t *ipip, void *ifreq)
17861 {
17862 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17863 	int err = 0;
17864 	boolean_t need_up = B_FALSE;
17865 	zone_t *zptr;
17866 	zone_status_t status;
17867 	zoneid_t zoneid;
17868 
17869 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
17870 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
17871 		if (!is_system_labeled())
17872 			return (ENOTSUP);
17873 		zoneid = GLOBAL_ZONEID;
17874 	}
17875 
17876 	/* cannot assign instance zero to a non-global zone */
17877 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
17878 		return (ENOTSUP);
17879 
17880 	/*
17881 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
17882 	 * the event of a race with the zone shutdown processing, since IP
17883 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
17884 	 * interface will be cleaned up even if the zone is shut down
17885 	 * immediately after the status check. If the interface can't be brought
17886 	 * down right away, and the zone is shut down before the restart
17887 	 * function is called, we resolve the possible races by rechecking the
17888 	 * zone status in the restart function.
17889 	 */
17890 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
17891 		return (EINVAL);
17892 	status = zone_status_get(zptr);
17893 	zone_rele(zptr);
17894 
17895 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
17896 		return (EINVAL);
17897 
17898 	if (ipif->ipif_flags & IPIF_UP) {
17899 		/*
17900 		 * If the interface is already marked up,
17901 		 * we call ipif_down which will take care
17902 		 * of ditching any IREs that have been set
17903 		 * up based on the old interface address.
17904 		 */
17905 		err = ipif_logical_down(ipif, q, mp);
17906 		if (err == EINPROGRESS)
17907 			return (err);
17908 		ipif_down_tail(ipif);
17909 		need_up = B_TRUE;
17910 	}
17911 
17912 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
17913 	return (err);
17914 }
17915 
17916 static int
17917 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
17918     queue_t *q, mblk_t *mp, boolean_t need_up)
17919 {
17920 	int	err = 0;
17921 	ip_stack_t	*ipst;
17922 
17923 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
17924 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17925 
17926 	if (CONN_Q(q))
17927 		ipst = CONNQ_TO_IPST(q);
17928 	else
17929 		ipst = ILLQ_TO_IPST(q);
17930 
17931 	/*
17932 	 * For exclusive stacks we don't allow a different zoneid than
17933 	 * global.
17934 	 */
17935 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
17936 	    zoneid != GLOBAL_ZONEID)
17937 		return (EINVAL);
17938 
17939 	/* Set the new zone id. */
17940 	ipif->ipif_zoneid = zoneid;
17941 
17942 	/* Update sctp list */
17943 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
17944 
17945 	if (need_up) {
17946 		/*
17947 		 * Now bring the interface back up.  If this
17948 		 * is the only IPIF for the ILL, ipif_up
17949 		 * will have to re-bind to the device, so
17950 		 * we may get back EINPROGRESS, in which
17951 		 * case, this IOCTL will get completed in
17952 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
17953 		 */
17954 		err = ipif_up(ipif, q, mp);
17955 	}
17956 	return (err);
17957 }
17958 
17959 /* ARGSUSED */
17960 int
17961 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17962     ip_ioctl_cmd_t *ipip, void *if_req)
17963 {
17964 	struct lifreq *lifr = (struct lifreq *)if_req;
17965 	zoneid_t zoneid;
17966 	zone_t *zptr;
17967 	zone_status_t status;
17968 
17969 	ASSERT(ipif->ipif_id != 0);
17970 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
17971 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
17972 		zoneid = GLOBAL_ZONEID;
17973 
17974 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
17975 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17976 
17977 	/*
17978 	 * We recheck the zone status to resolve the following race condition:
17979 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
17980 	 * 2) hme0:1 is up and can't be brought down right away;
17981 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
17982 	 * 3) zone "myzone" is halted; the zone status switches to
17983 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
17984 	 * the interfaces to remove - hme0:1 is not returned because it's not
17985 	 * yet in "myzone", so it won't be removed;
17986 	 * 4) the restart function for SIOCSLIFZONE is called; without the
17987 	 * status check here, we would have hme0:1 in "myzone" after it's been
17988 	 * destroyed.
17989 	 * Note that if the status check fails, we need to bring the interface
17990 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
17991 	 * ipif_up_done[_v6]().
17992 	 */
17993 	status = ZONE_IS_UNINITIALIZED;
17994 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
17995 		status = zone_status_get(zptr);
17996 		zone_rele(zptr);
17997 	}
17998 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
17999 		if (ipif->ipif_isv6) {
18000 			(void) ipif_up_done_v6(ipif);
18001 		} else {
18002 			(void) ipif_up_done(ipif);
18003 		}
18004 		return (EINVAL);
18005 	}
18006 
18007 	ipif_down_tail(ipif);
18008 
18009 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
18010 	    B_TRUE));
18011 }
18012 
18013 /*
18014  * Return the number of addresses on `ill' with one or more of the values
18015  * in `set' set and all of the values in `clear' clear.
18016  */
18017 static uint_t
18018 ill_flagaddr_cnt(const ill_t *ill, uint64_t set, uint64_t clear)
18019 {
18020 	ipif_t	*ipif;
18021 	uint_t	cnt = 0;
18022 
18023 	ASSERT(IAM_WRITER_ILL(ill));
18024 
18025 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
18026 		if ((ipif->ipif_flags & set) && !(ipif->ipif_flags & clear))
18027 			cnt++;
18028 
18029 	return (cnt);
18030 }
18031 
18032 /*
18033  * Return the number of migratable addresses on `ill' that are under
18034  * application control.
18035  */
18036 uint_t
18037 ill_appaddr_cnt(const ill_t *ill)
18038 {
18039 	return (ill_flagaddr_cnt(ill, IPIF_DHCPRUNNING | IPIF_ADDRCONF,
18040 	    IPIF_NOFAILOVER));
18041 }
18042 
18043 /*
18044  * Return the number of point-to-point addresses on `ill'.
18045  */
18046 uint_t
18047 ill_ptpaddr_cnt(const ill_t *ill)
18048 {
18049 	return (ill_flagaddr_cnt(ill, IPIF_POINTOPOINT, 0));
18050 }
18051 
18052 /* ARGSUSED */
18053 int
18054 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18055 	ip_ioctl_cmd_t *ipip, void *ifreq)
18056 {
18057 	struct lifreq	*lifr = ifreq;
18058 
18059 	ASSERT(q->q_next == NULL);
18060 	ASSERT(CONN_Q(q));
18061 
18062 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
18063 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
18064 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
18065 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
18066 
18067 	return (0);
18068 }
18069 
18070 /* Find the previous ILL in this usesrc group */
18071 static ill_t *
18072 ill_prev_usesrc(ill_t *uill)
18073 {
18074 	ill_t *ill;
18075 
18076 	for (ill = uill->ill_usesrc_grp_next;
18077 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
18078 	    ill = ill->ill_usesrc_grp_next)
18079 		/* do nothing */;
18080 	return (ill);
18081 }
18082 
18083 /*
18084  * Release all members of the usesrc group. This routine is called
18085  * from ill_delete when the interface being unplumbed is the
18086  * group head.
18087  */
18088 static void
18089 ill_disband_usesrc_group(ill_t *uill)
18090 {
18091 	ill_t *next_ill, *tmp_ill;
18092 	ip_stack_t	*ipst = uill->ill_ipst;
18093 
18094 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
18095 	next_ill = uill->ill_usesrc_grp_next;
18096 
18097 	do {
18098 		ASSERT(next_ill != NULL);
18099 		tmp_ill = next_ill->ill_usesrc_grp_next;
18100 		ASSERT(tmp_ill != NULL);
18101 		next_ill->ill_usesrc_grp_next = NULL;
18102 		next_ill->ill_usesrc_ifindex = 0;
18103 		next_ill = tmp_ill;
18104 	} while (next_ill->ill_usesrc_ifindex != 0);
18105 	uill->ill_usesrc_grp_next = NULL;
18106 }
18107 
18108 /*
18109  * Remove the client usesrc ILL from the list and relink to a new list
18110  */
18111 int
18112 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
18113 {
18114 	ill_t *ill, *tmp_ill;
18115 	ip_stack_t	*ipst = ucill->ill_ipst;
18116 
18117 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
18118 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
18119 
18120 	/*
18121 	 * Check if the usesrc client ILL passed in is not already
18122 	 * in use as a usesrc ILL i.e one whose source address is
18123 	 * in use OR a usesrc ILL is not already in use as a usesrc
18124 	 * client ILL
18125 	 */
18126 	if ((ucill->ill_usesrc_ifindex == 0) ||
18127 	    (uill->ill_usesrc_ifindex != 0)) {
18128 		return (-1);
18129 	}
18130 
18131 	ill = ill_prev_usesrc(ucill);
18132 	ASSERT(ill->ill_usesrc_grp_next != NULL);
18133 
18134 	/* Remove from the current list */
18135 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
18136 		/* Only two elements in the list */
18137 		ASSERT(ill->ill_usesrc_ifindex == 0);
18138 		ill->ill_usesrc_grp_next = NULL;
18139 	} else {
18140 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
18141 	}
18142 
18143 	if (ifindex == 0) {
18144 		ucill->ill_usesrc_ifindex = 0;
18145 		ucill->ill_usesrc_grp_next = NULL;
18146 		return (0);
18147 	}
18148 
18149 	ucill->ill_usesrc_ifindex = ifindex;
18150 	tmp_ill = uill->ill_usesrc_grp_next;
18151 	uill->ill_usesrc_grp_next = ucill;
18152 	ucill->ill_usesrc_grp_next =
18153 	    (tmp_ill != NULL) ? tmp_ill : uill;
18154 	return (0);
18155 }
18156 
18157 /*
18158  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
18159  * ip.c for locking details.
18160  */
18161 /* ARGSUSED */
18162 int
18163 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18164     ip_ioctl_cmd_t *ipip, void *ifreq)
18165 {
18166 	struct lifreq *lifr = (struct lifreq *)ifreq;
18167 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
18168 	    ill_flag_changed = B_FALSE;
18169 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
18170 	int err = 0, ret;
18171 	uint_t ifindex;
18172 	ipsq_t *ipsq = NULL;
18173 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
18174 
18175 	ASSERT(IAM_WRITER_IPIF(ipif));
18176 	ASSERT(q->q_next == NULL);
18177 	ASSERT(CONN_Q(q));
18178 
18179 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
18180 
18181 	ifindex = lifr->lifr_index;
18182 	if (ifindex == 0) {
18183 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
18184 			/* non usesrc group interface, nothing to reset */
18185 			return (0);
18186 		}
18187 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
18188 		/* valid reset request */
18189 		reset_flg = B_TRUE;
18190 	}
18191 
18192 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
18193 	    ip_process_ioctl, &err, ipst);
18194 	if (usesrc_ill == NULL) {
18195 		return (err);
18196 	}
18197 
18198 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
18199 	    NEW_OP, B_TRUE);
18200 	if (ipsq == NULL) {
18201 		err = EINPROGRESS;
18202 		/* Operation enqueued on the ipsq of the usesrc ILL */
18203 		goto done;
18204 	}
18205 
18206 	/* USESRC isn't currently supported with IPMP */
18207 	if (IS_IPMP(usesrc_ill) || IS_UNDER_IPMP(usesrc_ill)) {
18208 		err = ENOTSUP;
18209 		goto done;
18210 	}
18211 
18212 	/*
18213 	 * USESRC isn't compatible with the STANDBY flag.  (STANDBY is only
18214 	 * used by IPMP underlying interfaces, but someone might think it's
18215 	 * more general and try to use it independently with VNI.)
18216 	 */
18217 	if (usesrc_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
18218 		err = ENOTSUP;
18219 		goto done;
18220 	}
18221 
18222 	/*
18223 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
18224 	 * already a client then return EINVAL
18225 	 */
18226 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
18227 		err = EINVAL;
18228 		goto done;
18229 	}
18230 
18231 	/*
18232 	 * If the ill_usesrc_ifindex field is already set to what it needs to
18233 	 * be then this is a duplicate operation.
18234 	 */
18235 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
18236 		err = 0;
18237 		goto done;
18238 	}
18239 
18240 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
18241 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
18242 	    usesrc_ill->ill_isv6));
18243 
18244 	/*
18245 	 * The next step ensures that no new ires will be created referencing
18246 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
18247 	 * we go through an ire walk deleting all ire caches that reference
18248 	 * the client ill. New ires referencing the client ill that are added
18249 	 * to the ire table before the ILL_CHANGING flag is set, will be
18250 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
18251 	 * the client ill while the ILL_CHANGING flag is set will be failed
18252 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
18253 	 * checks (under the ill_g_usesrc_lock) that the ire being added
18254 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
18255 	 * belong to the same usesrc group.
18256 	 */
18257 	mutex_enter(&usesrc_cli_ill->ill_lock);
18258 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
18259 	mutex_exit(&usesrc_cli_ill->ill_lock);
18260 	ill_flag_changed = B_TRUE;
18261 
18262 	if (ipif->ipif_isv6)
18263 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
18264 		    ALL_ZONES, ipst);
18265 	else
18266 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
18267 		    ALL_ZONES, ipst);
18268 
18269 	/*
18270 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
18271 	 * and the ill_usesrc_ifindex fields
18272 	 */
18273 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
18274 
18275 	if (reset_flg) {
18276 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
18277 		if (ret != 0) {
18278 			err = EINVAL;
18279 		}
18280 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
18281 		goto done;
18282 	}
18283 
18284 	/*
18285 	 * Four possibilities to consider:
18286 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
18287 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
18288 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
18289 	 * 4. Both are part of their respective usesrc groups
18290 	 */
18291 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
18292 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
18293 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
18294 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
18295 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
18296 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
18297 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
18298 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
18299 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
18300 		/* Insert at head of list */
18301 		usesrc_cli_ill->ill_usesrc_grp_next =
18302 		    usesrc_ill->ill_usesrc_grp_next;
18303 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
18304 	} else {
18305 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
18306 		    ifindex);
18307 		if (ret != 0)
18308 			err = EINVAL;
18309 	}
18310 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
18311 
18312 done:
18313 	if (ill_flag_changed) {
18314 		mutex_enter(&usesrc_cli_ill->ill_lock);
18315 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
18316 		mutex_exit(&usesrc_cli_ill->ill_lock);
18317 	}
18318 	if (ipsq != NULL)
18319 		ipsq_exit(ipsq);
18320 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
18321 	ill_refrele(usesrc_ill);
18322 	return (err);
18323 }
18324 
18325 /*
18326  * comparison function used by avl.
18327  */
18328 static int
18329 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
18330 {
18331 
18332 	uint_t index;
18333 
18334 	ASSERT(phyip != NULL && index_ptr != NULL);
18335 
18336 	index = *((uint_t *)index_ptr);
18337 	/*
18338 	 * let the phyint with the lowest index be on top.
18339 	 */
18340 	if (((phyint_t *)phyip)->phyint_ifindex < index)
18341 		return (1);
18342 	if (((phyint_t *)phyip)->phyint_ifindex > index)
18343 		return (-1);
18344 	return (0);
18345 }
18346 
18347 /*
18348  * comparison function used by avl.
18349  */
18350 static int
18351 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
18352 {
18353 	ill_t *ill;
18354 	int res = 0;
18355 
18356 	ASSERT(phyip != NULL && name_ptr != NULL);
18357 
18358 	if (((phyint_t *)phyip)->phyint_illv4)
18359 		ill = ((phyint_t *)phyip)->phyint_illv4;
18360 	else
18361 		ill = ((phyint_t *)phyip)->phyint_illv6;
18362 	ASSERT(ill != NULL);
18363 
18364 	res = strcmp(ill->ill_name, (char *)name_ptr);
18365 	if (res > 0)
18366 		return (1);
18367 	else if (res < 0)
18368 		return (-1);
18369 	return (0);
18370 }
18371 
18372 /*
18373  * This function is called on the unplumb path via ill_glist_delete() when
18374  * there are no ills left on the phyint and thus the phyint can be freed.
18375  */
18376 static void
18377 phyint_free(phyint_t *phyi)
18378 {
18379 	ip_stack_t *ipst = PHYINT_TO_IPST(phyi);
18380 
18381 	ASSERT(phyi->phyint_illv4 == NULL && phyi->phyint_illv6 == NULL);
18382 
18383 	/*
18384 	 * If this phyint was an IPMP meta-interface, blow away the group.
18385 	 * This is safe to do because all of the illgrps have already been
18386 	 * removed by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find us.
18387 	 * If we're cleaning up as a result of failed initialization,
18388 	 * phyint_grp may be NULL.
18389 	 */
18390 	if ((phyi->phyint_flags & PHYI_IPMP) && (phyi->phyint_grp != NULL)) {
18391 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
18392 		ipmp_grp_destroy(phyi->phyint_grp);
18393 		phyi->phyint_grp = NULL;
18394 		rw_exit(&ipst->ips_ipmp_lock);
18395 	}
18396 
18397 	/*
18398 	 * If this interface was under IPMP, take it out of the group.
18399 	 */
18400 	if (phyi->phyint_grp != NULL)
18401 		ipmp_phyint_leave_grp(phyi);
18402 
18403 	/*
18404 	 * Delete the phyint and disassociate its ipsq.  The ipsq itself
18405 	 * will be freed in ipsq_exit().
18406 	 */
18407 	phyi->phyint_ipsq->ipsq_phyint = NULL;
18408 	phyi->phyint_name[0] = '\0';
18409 
18410 	mi_free(phyi);
18411 }
18412 
18413 /*
18414  * Attach the ill to the phyint structure which can be shared by both
18415  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
18416  * function is called from ipif_set_values and ill_lookup_on_name (for
18417  * loopback) where we know the name of the ill. We lookup the ill and if
18418  * there is one present already with the name use that phyint. Otherwise
18419  * reuse the one allocated by ill_init.
18420  */
18421 static void
18422 ill_phyint_reinit(ill_t *ill)
18423 {
18424 	boolean_t isv6 = ill->ill_isv6;
18425 	phyint_t *phyi_old;
18426 	phyint_t *phyi;
18427 	avl_index_t where = 0;
18428 	ill_t	*ill_other = NULL;
18429 	ip_stack_t	*ipst = ill->ill_ipst;
18430 
18431 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
18432 
18433 	phyi_old = ill->ill_phyint;
18434 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
18435 	    phyi_old->phyint_illv6 == NULL));
18436 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
18437 	    phyi_old->phyint_illv4 == NULL));
18438 	ASSERT(phyi_old->phyint_ifindex == 0);
18439 
18440 	/*
18441 	 * Now that our ill has a name, set it in the phyint.
18442 	 */
18443 	(void) strlcpy(ill->ill_phyint->phyint_name, ill->ill_name, LIFNAMSIZ);
18444 
18445 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
18446 	    ill->ill_name, &where);
18447 
18448 	/*
18449 	 * 1. We grabbed the ill_g_lock before inserting this ill into
18450 	 *    the global list of ills. So no other thread could have located
18451 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
18452 	 * 2. Now locate the other protocol instance of this ill.
18453 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
18454 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
18455 	 *    of neither ill can change.
18456 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
18457 	 *    other ill.
18458 	 * 5. Release all locks.
18459 	 */
18460 
18461 	/*
18462 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
18463 	 * we are initializing IPv4.
18464 	 */
18465 	if (phyi != NULL) {
18466 		ill_other = (isv6) ? phyi->phyint_illv4 : phyi->phyint_illv6;
18467 		ASSERT(ill_other->ill_phyint != NULL);
18468 		ASSERT((isv6 && !ill_other->ill_isv6) ||
18469 		    (!isv6 && ill_other->ill_isv6));
18470 		GRAB_ILL_LOCKS(ill, ill_other);
18471 		/*
18472 		 * We are potentially throwing away phyint_flags which
18473 		 * could be different from the one that we obtain from
18474 		 * ill_other->ill_phyint. But it is okay as we are assuming
18475 		 * that the state maintained within IP is correct.
18476 		 */
18477 		mutex_enter(&phyi->phyint_lock);
18478 		if (isv6) {
18479 			ASSERT(phyi->phyint_illv6 == NULL);
18480 			phyi->phyint_illv6 = ill;
18481 		} else {
18482 			ASSERT(phyi->phyint_illv4 == NULL);
18483 			phyi->phyint_illv4 = ill;
18484 		}
18485 
18486 		/*
18487 		 * Delete the old phyint and make its ipsq eligible
18488 		 * to be freed in ipsq_exit().
18489 		 */
18490 		phyi_old->phyint_illv4 = NULL;
18491 		phyi_old->phyint_illv6 = NULL;
18492 		phyi_old->phyint_ipsq->ipsq_phyint = NULL;
18493 		phyi_old->phyint_name[0] = '\0';
18494 		mi_free(phyi_old);
18495 	} else {
18496 		mutex_enter(&ill->ill_lock);
18497 		/*
18498 		 * We don't need to acquire any lock, since
18499 		 * the ill is not yet visible globally  and we
18500 		 * have not yet released the ill_g_lock.
18501 		 */
18502 		phyi = phyi_old;
18503 		mutex_enter(&phyi->phyint_lock);
18504 		/* XXX We need a recovery strategy here. */
18505 		if (!phyint_assign_ifindex(phyi, ipst))
18506 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
18507 
18508 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
18509 		    (void *)phyi, where);
18510 
18511 		(void) avl_find(&ipst->ips_phyint_g_list->
18512 		    phyint_list_avl_by_index,
18513 		    &phyi->phyint_ifindex, &where);
18514 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
18515 		    (void *)phyi, where);
18516 	}
18517 
18518 	/*
18519 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
18520 	 * pending mp is not affected because that is per ill basis.
18521 	 */
18522 	ill->ill_phyint = phyi;
18523 
18524 	/*
18525 	 * Now that the phyint's ifindex has been assigned, complete the
18526 	 * remaining
18527 	 */
18528 
18529 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
18530 	if (ill->ill_isv6) {
18531 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
18532 		    ill->ill_phyint->phyint_ifindex;
18533 		ill->ill_mcast_type = ipst->ips_mld_max_version;
18534 	} else {
18535 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
18536 	}
18537 
18538 	/*
18539 	 * Generate an event within the hooks framework to indicate that
18540 	 * a new interface has just been added to IP.  For this event to
18541 	 * be generated, the network interface must, at least, have an
18542 	 * ifindex assigned to it.
18543 	 *
18544 	 * This needs to be run inside the ill_g_lock perimeter to ensure
18545 	 * that the ordering of delivered events to listeners matches the
18546 	 * order of them in the kernel.
18547 	 *
18548 	 * This function could be called from ill_lookup_on_name. In that case
18549 	 * the interface is loopback "lo", which will not generate a NIC event.
18550 	 */
18551 	if (ill->ill_name_length <= 2 ||
18552 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
18553 		ill_nic_event_dispatch(ill, 0, NE_PLUMB, ill->ill_name,
18554 		    ill->ill_name_length);
18555 	}
18556 	RELEASE_ILL_LOCKS(ill, ill_other);
18557 	mutex_exit(&phyi->phyint_lock);
18558 }
18559 
18560 /*
18561  * Notify any downstream modules of the name of this interface.
18562  * An M_IOCTL is used even though we don't expect a successful reply.
18563  * Any reply message from the driver (presumably an M_IOCNAK) will
18564  * eventually get discarded somewhere upstream.  The message format is
18565  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
18566  * to IP.
18567  */
18568 static void
18569 ip_ifname_notify(ill_t *ill, queue_t *q)
18570 {
18571 	mblk_t *mp1, *mp2;
18572 	struct iocblk *iocp;
18573 	struct lifreq *lifr;
18574 
18575 	mp1 = mkiocb(SIOCSLIFNAME);
18576 	if (mp1 == NULL)
18577 		return;
18578 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
18579 	if (mp2 == NULL) {
18580 		freeb(mp1);
18581 		return;
18582 	}
18583 
18584 	mp1->b_cont = mp2;
18585 	iocp = (struct iocblk *)mp1->b_rptr;
18586 	iocp->ioc_count = sizeof (struct lifreq);
18587 
18588 	lifr = (struct lifreq *)mp2->b_rptr;
18589 	mp2->b_wptr += sizeof (struct lifreq);
18590 	bzero(lifr, sizeof (struct lifreq));
18591 
18592 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
18593 	lifr->lifr_ppa = ill->ill_ppa;
18594 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
18595 
18596 	putnext(q, mp1);
18597 }
18598 
18599 static int
18600 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
18601 {
18602 	int		err;
18603 	ip_stack_t	*ipst = ill->ill_ipst;
18604 	phyint_t	*phyi = ill->ill_phyint;
18605 
18606 	/* Set the obsolete NDD per-interface forwarding name. */
18607 	err = ill_set_ndd_name(ill);
18608 	if (err != 0) {
18609 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
18610 		    err);
18611 	}
18612 
18613 	/*
18614 	 * Now that ill_name is set, the configuration for the IPMP
18615 	 * meta-interface can be performed.
18616 	 */
18617 	if (IS_IPMP(ill)) {
18618 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
18619 		/*
18620 		 * If phyi->phyint_grp is NULL, then this is the first IPMP
18621 		 * meta-interface and we need to create the IPMP group.
18622 		 */
18623 		if (phyi->phyint_grp == NULL) {
18624 			/*
18625 			 * If someone has renamed another IPMP group to have
18626 			 * the same name as our interface, bail.
18627 			 */
18628 			if (ipmp_grp_lookup(ill->ill_name, ipst) != NULL) {
18629 				rw_exit(&ipst->ips_ipmp_lock);
18630 				return (EEXIST);
18631 			}
18632 			phyi->phyint_grp = ipmp_grp_create(ill->ill_name, phyi);
18633 			if (phyi->phyint_grp == NULL) {
18634 				rw_exit(&ipst->ips_ipmp_lock);
18635 				return (ENOMEM);
18636 			}
18637 		}
18638 		rw_exit(&ipst->ips_ipmp_lock);
18639 	}
18640 
18641 	/* Tell downstream modules where they are. */
18642 	ip_ifname_notify(ill, q);
18643 
18644 	/*
18645 	 * ill_dl_phys returns EINPROGRESS in the usual case.
18646 	 * Error cases are ENOMEM ...
18647 	 */
18648 	err = ill_dl_phys(ill, ipif, mp, q);
18649 
18650 	/*
18651 	 * If there is no IRE expiration timer running, get one started.
18652 	 * igmp and mld timers will be triggered by the first multicast
18653 	 */
18654 	if (ipst->ips_ip_ire_expire_id == 0) {
18655 		/*
18656 		 * acquire the lock and check again.
18657 		 */
18658 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
18659 		if (ipst->ips_ip_ire_expire_id == 0) {
18660 			ipst->ips_ip_ire_expire_id = timeout(
18661 			    ip_trash_timer_expire, ipst,
18662 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
18663 		}
18664 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
18665 	}
18666 
18667 	if (ill->ill_isv6) {
18668 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
18669 		if (ipst->ips_mld_slowtimeout_id == 0) {
18670 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
18671 			    (void *)ipst,
18672 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
18673 		}
18674 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
18675 	} else {
18676 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
18677 		if (ipst->ips_igmp_slowtimeout_id == 0) {
18678 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
18679 			    (void *)ipst,
18680 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
18681 		}
18682 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
18683 	}
18684 
18685 	return (err);
18686 }
18687 
18688 /*
18689  * Common routine for ppa and ifname setting. Should be called exclusive.
18690  *
18691  * Returns EINPROGRESS when mp has been consumed by queueing it on
18692  * ill_pending_mp and the ioctl will complete in ip_rput.
18693  *
18694  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
18695  * the new name and new ppa in lifr_name and lifr_ppa respectively.
18696  * For SLIFNAME, we pass these values back to the userland.
18697  */
18698 static int
18699 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
18700 {
18701 	ill_t	*ill;
18702 	ipif_t	*ipif;
18703 	ipsq_t	*ipsq;
18704 	char	*ppa_ptr;
18705 	char	*old_ptr;
18706 	char	old_char;
18707 	int	error;
18708 	ip_stack_t	*ipst;
18709 
18710 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
18711 	ASSERT(q->q_next != NULL);
18712 	ASSERT(interf_name != NULL);
18713 
18714 	ill = (ill_t *)q->q_ptr;
18715 	ipst = ill->ill_ipst;
18716 
18717 	ASSERT(ill->ill_ipst != NULL);
18718 	ASSERT(ill->ill_name[0] == '\0');
18719 	ASSERT(IAM_WRITER_ILL(ill));
18720 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
18721 	ASSERT(ill->ill_ppa == UINT_MAX);
18722 
18723 	/* The ppa is sent down by ifconfig or is chosen */
18724 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
18725 		return (EINVAL);
18726 	}
18727 
18728 	/*
18729 	 * make sure ppa passed in is same as ppa in the name.
18730 	 * This check is not made when ppa == UINT_MAX in that case ppa
18731 	 * in the name could be anything. System will choose a ppa and
18732 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
18733 	 */
18734 	if (*new_ppa_ptr != UINT_MAX) {
18735 		/* stoi changes the pointer */
18736 		old_ptr = ppa_ptr;
18737 		/*
18738 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
18739 		 * (they don't have an externally visible ppa).  We assign one
18740 		 * here so that we can manage the interface.  Note that in
18741 		 * the past this value was always 0 for DLPI 1 drivers.
18742 		 */
18743 		if (*new_ppa_ptr == 0)
18744 			*new_ppa_ptr = stoi(&old_ptr);
18745 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
18746 			return (EINVAL);
18747 	}
18748 	/*
18749 	 * terminate string before ppa
18750 	 * save char at that location.
18751 	 */
18752 	old_char = ppa_ptr[0];
18753 	ppa_ptr[0] = '\0';
18754 
18755 	ill->ill_ppa = *new_ppa_ptr;
18756 	/*
18757 	 * Finish as much work now as possible before calling ill_glist_insert
18758 	 * which makes the ill globally visible and also merges it with the
18759 	 * other protocol instance of this phyint. The remaining work is
18760 	 * done after entering the ipsq which may happen sometime later.
18761 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
18762 	 */
18763 	ipif = ill->ill_ipif;
18764 
18765 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
18766 	ipif_assign_seqid(ipif);
18767 
18768 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
18769 		ill->ill_flags |= ILLF_IPV4;
18770 
18771 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
18772 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
18773 
18774 	if (ill->ill_flags & ILLF_IPV6) {
18775 
18776 		ill->ill_isv6 = B_TRUE;
18777 		if (ill->ill_rq != NULL) {
18778 			ill->ill_rq->q_qinfo = &iprinitv6;
18779 			ill->ill_wq->q_qinfo = &ipwinitv6;
18780 		}
18781 
18782 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
18783 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
18784 		ipif->ipif_v6src_addr = ipv6_all_zeros;
18785 		ipif->ipif_v6subnet = ipv6_all_zeros;
18786 		ipif->ipif_v6net_mask = ipv6_all_zeros;
18787 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
18788 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
18789 		/*
18790 		 * point-to-point or Non-mulicast capable
18791 		 * interfaces won't do NUD unless explicitly
18792 		 * configured to do so.
18793 		 */
18794 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
18795 		    !(ill->ill_flags & ILLF_MULTICAST)) {
18796 			ill->ill_flags |= ILLF_NONUD;
18797 		}
18798 		/* Make sure IPv4 specific flag is not set on IPv6 if */
18799 		if (ill->ill_flags & ILLF_NOARP) {
18800 			/*
18801 			 * Note: xresolv interfaces will eventually need
18802 			 * NOARP set here as well, but that will require
18803 			 * those external resolvers to have some
18804 			 * knowledge of that flag and act appropriately.
18805 			 * Not to be changed at present.
18806 			 */
18807 			ill->ill_flags &= ~ILLF_NOARP;
18808 		}
18809 		/*
18810 		 * Set the ILLF_ROUTER flag according to the global
18811 		 * IPv6 forwarding policy.
18812 		 */
18813 		if (ipst->ips_ipv6_forward != 0)
18814 			ill->ill_flags |= ILLF_ROUTER;
18815 	} else if (ill->ill_flags & ILLF_IPV4) {
18816 		ill->ill_isv6 = B_FALSE;
18817 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
18818 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
18819 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
18820 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
18821 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
18822 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
18823 		/*
18824 		 * Set the ILLF_ROUTER flag according to the global
18825 		 * IPv4 forwarding policy.
18826 		 */
18827 		if (ipst->ips_ip_g_forward != 0)
18828 			ill->ill_flags |= ILLF_ROUTER;
18829 	}
18830 
18831 	ASSERT(ill->ill_phyint != NULL);
18832 
18833 	/*
18834 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
18835 	 * be completed in ill_glist_insert -> ill_phyint_reinit
18836 	 */
18837 	if (!ill_allocate_mibs(ill))
18838 		return (ENOMEM);
18839 
18840 	/*
18841 	 * Pick a default sap until we get the DL_INFO_ACK back from
18842 	 * the driver.
18843 	 */
18844 	if (ill->ill_sap == 0) {
18845 		if (ill->ill_isv6)
18846 			ill->ill_sap = IP6_DL_SAP;
18847 		else
18848 			ill->ill_sap = IP_DL_SAP;
18849 	}
18850 
18851 	ill->ill_ifname_pending = 1;
18852 	ill->ill_ifname_pending_err = 0;
18853 
18854 	/*
18855 	 * When the first ipif comes up in ipif_up_done(), multicast groups
18856 	 * that were joined while this ill was not bound to the DLPI link need
18857 	 * to be recovered by ill_recover_multicast().
18858 	 */
18859 	ill->ill_need_recover_multicast = 1;
18860 
18861 	ill_refhold(ill);
18862 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
18863 	if ((error = ill_glist_insert(ill, interf_name,
18864 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
18865 		ill->ill_ppa = UINT_MAX;
18866 		ill->ill_name[0] = '\0';
18867 		/*
18868 		 * undo null termination done above.
18869 		 */
18870 		ppa_ptr[0] = old_char;
18871 		rw_exit(&ipst->ips_ill_g_lock);
18872 		ill_refrele(ill);
18873 		return (error);
18874 	}
18875 
18876 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
18877 
18878 	/*
18879 	 * When we return the buffer pointed to by interf_name should contain
18880 	 * the same name as in ill_name.
18881 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
18882 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
18883 	 * so copy full name and update the ppa ptr.
18884 	 * When ppa passed in != UINT_MAX all values are correct just undo
18885 	 * null termination, this saves a bcopy.
18886 	 */
18887 	if (*new_ppa_ptr == UINT_MAX) {
18888 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
18889 		*new_ppa_ptr = ill->ill_ppa;
18890 	} else {
18891 		/*
18892 		 * undo null termination done above.
18893 		 */
18894 		ppa_ptr[0] = old_char;
18895 	}
18896 
18897 	/* Let SCTP know about this ILL */
18898 	sctp_update_ill(ill, SCTP_ILL_INSERT);
18899 
18900 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
18901 	    B_TRUE);
18902 
18903 	rw_exit(&ipst->ips_ill_g_lock);
18904 	ill_refrele(ill);
18905 	if (ipsq == NULL)
18906 		return (EINPROGRESS);
18907 
18908 	/*
18909 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
18910 	 */
18911 	if (ipsq->ipsq_xop->ipx_current_ipif == NULL)
18912 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
18913 	else
18914 		ASSERT(ipsq->ipsq_xop->ipx_current_ipif == ipif);
18915 
18916 	error = ipif_set_values_tail(ill, ipif, mp, q);
18917 	ipsq_exit(ipsq);
18918 	if (error != 0 && error != EINPROGRESS) {
18919 		/*
18920 		 * restore previous values
18921 		 */
18922 		ill->ill_isv6 = B_FALSE;
18923 	}
18924 	return (error);
18925 }
18926 
18927 void
18928 ipif_init(ip_stack_t *ipst)
18929 {
18930 	int i;
18931 
18932 	for (i = 0; i < MAX_G_HEADS; i++) {
18933 		ipst->ips_ill_g_heads[i].ill_g_list_head =
18934 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
18935 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
18936 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
18937 	}
18938 
18939 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
18940 	    ill_phyint_compare_index,
18941 	    sizeof (phyint_t),
18942 	    offsetof(struct phyint, phyint_avl_by_index));
18943 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
18944 	    ill_phyint_compare_name,
18945 	    sizeof (phyint_t),
18946 	    offsetof(struct phyint, phyint_avl_by_name));
18947 }
18948 
18949 /*
18950  * Lookup the ipif corresponding to the onlink destination address. For
18951  * point-to-point interfaces, it matches with remote endpoint destination
18952  * address. For point-to-multipoint interfaces it only tries to match the
18953  * destination with the interface's subnet address. The longest, most specific
18954  * match is found to take care of such rare network configurations like -
18955  * le0: 129.146.1.1/16
18956  * le1: 129.146.2.2/24
18957  *
18958  * This is used by SO_DONTROUTE and IP_NEXTHOP.  Since neither of those are
18959  * supported on underlying interfaces in an IPMP group, underlying interfaces
18960  * are ignored when looking up a match.  (If we didn't ignore them, we'd
18961  * risk using a test address as a source for outgoing traffic.)
18962  */
18963 ipif_t *
18964 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
18965 {
18966 	ipif_t	*ipif, *best_ipif;
18967 	ill_t	*ill;
18968 	ill_walk_context_t ctx;
18969 
18970 	ASSERT(zoneid != ALL_ZONES);
18971 	best_ipif = NULL;
18972 
18973 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
18974 	ill = ILL_START_WALK_V4(&ctx, ipst);
18975 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
18976 		if (IS_UNDER_IPMP(ill))
18977 			continue;
18978 		mutex_enter(&ill->ill_lock);
18979 		for (ipif = ill->ill_ipif; ipif != NULL;
18980 		    ipif = ipif->ipif_next) {
18981 			if (!IPIF_CAN_LOOKUP(ipif))
18982 				continue;
18983 			if (ipif->ipif_zoneid != zoneid &&
18984 			    ipif->ipif_zoneid != ALL_ZONES)
18985 				continue;
18986 			/*
18987 			 * Point-to-point case. Look for exact match with
18988 			 * destination address.
18989 			 */
18990 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
18991 				if (ipif->ipif_pp_dst_addr == addr) {
18992 					ipif_refhold_locked(ipif);
18993 					mutex_exit(&ill->ill_lock);
18994 					rw_exit(&ipst->ips_ill_g_lock);
18995 					if (best_ipif != NULL)
18996 						ipif_refrele(best_ipif);
18997 					return (ipif);
18998 				}
18999 			} else if (ipif->ipif_subnet == (addr &
19000 			    ipif->ipif_net_mask)) {
19001 				/*
19002 				 * Point-to-multipoint case. Looping through to
19003 				 * find the most specific match. If there are
19004 				 * multiple best match ipif's then prefer ipif's
19005 				 * that are UP. If there is only one best match
19006 				 * ipif and it is DOWN we must still return it.
19007 				 */
19008 				if ((best_ipif == NULL) ||
19009 				    (ipif->ipif_net_mask >
19010 				    best_ipif->ipif_net_mask) ||
19011 				    ((ipif->ipif_net_mask ==
19012 				    best_ipif->ipif_net_mask) &&
19013 				    ((ipif->ipif_flags & IPIF_UP) &&
19014 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
19015 					ipif_refhold_locked(ipif);
19016 					mutex_exit(&ill->ill_lock);
19017 					rw_exit(&ipst->ips_ill_g_lock);
19018 					if (best_ipif != NULL)
19019 						ipif_refrele(best_ipif);
19020 					best_ipif = ipif;
19021 					rw_enter(&ipst->ips_ill_g_lock,
19022 					    RW_READER);
19023 					mutex_enter(&ill->ill_lock);
19024 				}
19025 			}
19026 		}
19027 		mutex_exit(&ill->ill_lock);
19028 	}
19029 	rw_exit(&ipst->ips_ill_g_lock);
19030 	return (best_ipif);
19031 }
19032 
19033 /*
19034  * Save enough information so that we can recreate the IRE if
19035  * the interface goes down and then up.
19036  */
19037 static void
19038 ipif_save_ire(ipif_t *ipif, ire_t *ire)
19039 {
19040 	mblk_t	*save_mp;
19041 
19042 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
19043 	if (save_mp != NULL) {
19044 		ifrt_t	*ifrt;
19045 
19046 		save_mp->b_wptr += sizeof (ifrt_t);
19047 		ifrt = (ifrt_t *)save_mp->b_rptr;
19048 		bzero(ifrt, sizeof (ifrt_t));
19049 		ifrt->ifrt_type = ire->ire_type;
19050 		ifrt->ifrt_addr = ire->ire_addr;
19051 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
19052 		ifrt->ifrt_src_addr = ire->ire_src_addr;
19053 		ifrt->ifrt_mask = ire->ire_mask;
19054 		ifrt->ifrt_flags = ire->ire_flags;
19055 		ifrt->ifrt_max_frag = ire->ire_max_frag;
19056 		mutex_enter(&ipif->ipif_saved_ire_lock);
19057 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
19058 		ipif->ipif_saved_ire_mp = save_mp;
19059 		ipif->ipif_saved_ire_cnt++;
19060 		mutex_exit(&ipif->ipif_saved_ire_lock);
19061 	}
19062 }
19063 
19064 static void
19065 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
19066 {
19067 	mblk_t	**mpp;
19068 	mblk_t	*mp;
19069 	ifrt_t	*ifrt;
19070 
19071 	/* Remove from ipif_saved_ire_mp list if it is there */
19072 	mutex_enter(&ipif->ipif_saved_ire_lock);
19073 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
19074 	    mpp = &(*mpp)->b_cont) {
19075 		/*
19076 		 * On a given ipif, the triple of address, gateway and
19077 		 * mask is unique for each saved IRE (in the case of
19078 		 * ordinary interface routes, the gateway address is
19079 		 * all-zeroes).
19080 		 */
19081 		mp = *mpp;
19082 		ifrt = (ifrt_t *)mp->b_rptr;
19083 		if (ifrt->ifrt_addr == ire->ire_addr &&
19084 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
19085 		    ifrt->ifrt_mask == ire->ire_mask) {
19086 			*mpp = mp->b_cont;
19087 			ipif->ipif_saved_ire_cnt--;
19088 			freeb(mp);
19089 			break;
19090 		}
19091 	}
19092 	mutex_exit(&ipif->ipif_saved_ire_lock);
19093 }
19094 
19095 /*
19096  * IP multirouting broadcast routes handling
19097  * Append CGTP broadcast IREs to regular ones created
19098  * at ifconfig time.
19099  */
19100 static void
19101 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
19102 {
19103 	ire_t *ire_prim;
19104 
19105 	ASSERT(ire != NULL);
19106 	ASSERT(ire_dst != NULL);
19107 
19108 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
19109 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
19110 	if (ire_prim != NULL) {
19111 		/*
19112 		 * We are in the special case of broadcasts for
19113 		 * CGTP. We add an IRE_BROADCAST that holds
19114 		 * the RTF_MULTIRT flag, the destination
19115 		 * address of ire_dst and the low level
19116 		 * info of ire_prim. In other words, CGTP
19117 		 * broadcast is added to the redundant ipif.
19118 		 */
19119 		ipif_t *ipif_prim;
19120 		ire_t  *bcast_ire;
19121 
19122 		ipif_prim = ire_prim->ire_ipif;
19123 
19124 		ip2dbg(("ip_cgtp_filter_bcast_add: "
19125 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
19126 		    (void *)ire_dst, (void *)ire_prim,
19127 		    (void *)ipif_prim));
19128 
19129 		bcast_ire = ire_create(
19130 		    (uchar_t *)&ire->ire_addr,
19131 		    (uchar_t *)&ip_g_all_ones,
19132 		    (uchar_t *)&ire_dst->ire_src_addr,
19133 		    (uchar_t *)&ire->ire_gateway_addr,
19134 		    &ipif_prim->ipif_mtu,
19135 		    NULL,
19136 		    ipif_prim->ipif_rq,
19137 		    ipif_prim->ipif_wq,
19138 		    IRE_BROADCAST,
19139 		    ipif_prim,
19140 		    0,
19141 		    0,
19142 		    0,
19143 		    ire->ire_flags,
19144 		    &ire_uinfo_null,
19145 		    NULL,
19146 		    NULL,
19147 		    ipst);
19148 
19149 		if (bcast_ire != NULL) {
19150 
19151 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
19152 			    B_FALSE) == 0) {
19153 				ip2dbg(("ip_cgtp_filter_bcast_add: "
19154 				    "added bcast_ire %p\n",
19155 				    (void *)bcast_ire));
19156 
19157 				ipif_save_ire(bcast_ire->ire_ipif,
19158 				    bcast_ire);
19159 				ire_refrele(bcast_ire);
19160 			}
19161 		}
19162 		ire_refrele(ire_prim);
19163 	}
19164 }
19165 
19166 /*
19167  * IP multirouting broadcast routes handling
19168  * Remove the broadcast ire
19169  */
19170 static void
19171 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
19172 {
19173 	ire_t *ire_dst;
19174 
19175 	ASSERT(ire != NULL);
19176 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
19177 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
19178 	if (ire_dst != NULL) {
19179 		ire_t *ire_prim;
19180 
19181 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
19182 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
19183 		if (ire_prim != NULL) {
19184 			ipif_t *ipif_prim;
19185 			ire_t  *bcast_ire;
19186 
19187 			ipif_prim = ire_prim->ire_ipif;
19188 
19189 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
19190 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
19191 			    (void *)ire_dst, (void *)ire_prim,
19192 			    (void *)ipif_prim));
19193 
19194 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
19195 			    ire->ire_gateway_addr,
19196 			    IRE_BROADCAST,
19197 			    ipif_prim, ALL_ZONES,
19198 			    NULL,
19199 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
19200 			    MATCH_IRE_MASK, ipst);
19201 
19202 			if (bcast_ire != NULL) {
19203 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
19204 				    "looked up bcast_ire %p\n",
19205 				    (void *)bcast_ire));
19206 				ipif_remove_ire(bcast_ire->ire_ipif,
19207 				    bcast_ire);
19208 				ire_delete(bcast_ire);
19209 				ire_refrele(bcast_ire);
19210 			}
19211 			ire_refrele(ire_prim);
19212 		}
19213 		ire_refrele(ire_dst);
19214 	}
19215 }
19216 
19217 /*
19218  * IPsec hardware acceleration capabilities related functions.
19219  */
19220 
19221 /*
19222  * Free a per-ill IPsec capabilities structure.
19223  */
19224 static void
19225 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
19226 {
19227 	if (capab->auth_hw_algs != NULL)
19228 		kmem_free(capab->auth_hw_algs, capab->algs_size);
19229 	if (capab->encr_hw_algs != NULL)
19230 		kmem_free(capab->encr_hw_algs, capab->algs_size);
19231 	if (capab->encr_algparm != NULL)
19232 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
19233 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
19234 }
19235 
19236 /*
19237  * Allocate a new per-ill IPsec capabilities structure. This structure
19238  * is specific to an IPsec protocol (AH or ESP). It is implemented as
19239  * an array which specifies, for each algorithm, whether this algorithm
19240  * is supported by the ill or not.
19241  */
19242 static ill_ipsec_capab_t *
19243 ill_ipsec_capab_alloc(void)
19244 {
19245 	ill_ipsec_capab_t *capab;
19246 	uint_t nelems;
19247 
19248 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
19249 	if (capab == NULL)
19250 		return (NULL);
19251 
19252 	/* we need one bit per algorithm */
19253 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
19254 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
19255 
19256 	/* allocate memory to store algorithm flags */
19257 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
19258 	if (capab->encr_hw_algs == NULL)
19259 		goto nomem;
19260 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
19261 	if (capab->auth_hw_algs == NULL)
19262 		goto nomem;
19263 	/*
19264 	 * Leave encr_algparm NULL for now since we won't need it half
19265 	 * the time
19266 	 */
19267 	return (capab);
19268 
19269 nomem:
19270 	ill_ipsec_capab_free(capab);
19271 	return (NULL);
19272 }
19273 
19274 /*
19275  * Resize capability array.  Since we're exclusive, this is OK.
19276  */
19277 static boolean_t
19278 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
19279 {
19280 	ipsec_capab_algparm_t *nalp, *oalp;
19281 	uint32_t olen, nlen;
19282 
19283 	oalp = capab->encr_algparm;
19284 	olen = capab->encr_algparm_size;
19285 
19286 	if (oalp != NULL) {
19287 		if (algid < capab->encr_algparm_end)
19288 			return (B_TRUE);
19289 	}
19290 
19291 	nlen = (algid + 1) * sizeof (*nalp);
19292 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
19293 	if (nalp == NULL)
19294 		return (B_FALSE);
19295 
19296 	if (oalp != NULL) {
19297 		bcopy(oalp, nalp, olen);
19298 		kmem_free(oalp, olen);
19299 	}
19300 	capab->encr_algparm = nalp;
19301 	capab->encr_algparm_size = nlen;
19302 	capab->encr_algparm_end = algid + 1;
19303 
19304 	return (B_TRUE);
19305 }
19306 
19307 /*
19308  * Compare the capabilities of the specified ill with the protocol
19309  * and algorithms specified by the SA passed as argument.
19310  * If they match, returns B_TRUE, B_FALSE if they do not match.
19311  *
19312  * The ill can be passed as a pointer to it, or by specifying its index
19313  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
19314  *
19315  * Called by ipsec_out_is_accelerated() do decide whether an outbound
19316  * packet is eligible for hardware acceleration, and by
19317  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
19318  * to a particular ill.
19319  */
19320 boolean_t
19321 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
19322     ipsa_t *sa, netstack_t *ns)
19323 {
19324 	boolean_t sa_isv6;
19325 	uint_t algid;
19326 	struct ill_ipsec_capab_s *cpp;
19327 	boolean_t need_refrele = B_FALSE;
19328 	ip_stack_t	*ipst = ns->netstack_ip;
19329 
19330 	if (ill == NULL) {
19331 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
19332 		    NULL, NULL, NULL, ipst);
19333 		if (ill == NULL) {
19334 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
19335 			return (B_FALSE);
19336 		}
19337 		need_refrele = B_TRUE;
19338 	}
19339 
19340 	/*
19341 	 * Use the address length specified by the SA to determine
19342 	 * if it corresponds to a IPv6 address, and fail the matching
19343 	 * if the isv6 flag passed as argument does not match.
19344 	 * Note: this check is used for SADB capability checking before
19345 	 * sending SA information to an ill.
19346 	 */
19347 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
19348 	if (sa_isv6 != ill_isv6)
19349 		/* protocol mismatch */
19350 		goto done;
19351 
19352 	/*
19353 	 * Check if the ill supports the protocol, algorithm(s) and
19354 	 * key size(s) specified by the SA, and get the pointers to
19355 	 * the algorithms supported by the ill.
19356 	 */
19357 	switch (sa->ipsa_type) {
19358 
19359 	case SADB_SATYPE_ESP:
19360 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
19361 			/* ill does not support ESP acceleration */
19362 			goto done;
19363 		cpp = ill->ill_ipsec_capab_esp;
19364 		algid = sa->ipsa_auth_alg;
19365 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
19366 			goto done;
19367 		algid = sa->ipsa_encr_alg;
19368 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
19369 			goto done;
19370 		if (algid < cpp->encr_algparm_end) {
19371 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
19372 			if (sa->ipsa_encrkeybits < alp->minkeylen)
19373 				goto done;
19374 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
19375 				goto done;
19376 		}
19377 		break;
19378 
19379 	case SADB_SATYPE_AH:
19380 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
19381 			/* ill does not support AH acceleration */
19382 			goto done;
19383 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
19384 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
19385 			goto done;
19386 		break;
19387 	}
19388 
19389 	if (need_refrele)
19390 		ill_refrele(ill);
19391 	return (B_TRUE);
19392 done:
19393 	if (need_refrele)
19394 		ill_refrele(ill);
19395 	return (B_FALSE);
19396 }
19397 
19398 /*
19399  * Add a new ill to the list of IPsec capable ills.
19400  * Called from ill_capability_ipsec_ack() when an ACK was received
19401  * indicating that IPsec hardware processing was enabled for an ill.
19402  *
19403  * ill must point to the ill for which acceleration was enabled.
19404  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
19405  */
19406 static void
19407 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
19408 {
19409 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
19410 	uint_t sa_type;
19411 	uint_t ipproto;
19412 	ip_stack_t	*ipst = ill->ill_ipst;
19413 
19414 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
19415 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
19416 
19417 	switch (dl_cap) {
19418 	case DL_CAPAB_IPSEC_AH:
19419 		sa_type = SADB_SATYPE_AH;
19420 		ills = &ipst->ips_ipsec_capab_ills_ah;
19421 		ipproto = IPPROTO_AH;
19422 		break;
19423 	case DL_CAPAB_IPSEC_ESP:
19424 		sa_type = SADB_SATYPE_ESP;
19425 		ills = &ipst->ips_ipsec_capab_ills_esp;
19426 		ipproto = IPPROTO_ESP;
19427 		break;
19428 	}
19429 
19430 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
19431 
19432 	/*
19433 	 * Add ill index to list of hardware accelerators. If
19434 	 * already in list, do nothing.
19435 	 */
19436 	for (cur_ill = *ills; cur_ill != NULL &&
19437 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
19438 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
19439 		;
19440 
19441 	if (cur_ill == NULL) {
19442 		/* if this is a new entry for this ill */
19443 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
19444 		if (new_ill == NULL) {
19445 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19446 			return;
19447 		}
19448 
19449 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
19450 		new_ill->ill_isv6 = ill->ill_isv6;
19451 		new_ill->next = *ills;
19452 		*ills = new_ill;
19453 	} else if (!sadb_resync) {
19454 		/* not resync'ing SADB and an entry exists for this ill */
19455 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19456 		return;
19457 	}
19458 
19459 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19460 
19461 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
19462 		/*
19463 		 * IPsec module for protocol loaded, initiate dump
19464 		 * of the SADB to this ill.
19465 		 */
19466 		sadb_ill_download(ill, sa_type);
19467 }
19468 
19469 /*
19470  * Remove an ill from the list of IPsec capable ills.
19471  */
19472 static void
19473 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
19474 {
19475 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
19476 	ip_stack_t	*ipst = ill->ill_ipst;
19477 
19478 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
19479 	    dl_cap == DL_CAPAB_IPSEC_ESP);
19480 
19481 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
19482 	    &ipst->ips_ipsec_capab_ills_esp;
19483 
19484 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
19485 
19486 	prev_ill = NULL;
19487 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
19488 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
19489 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
19490 		;
19491 	if (cur_ill == NULL) {
19492 		/* entry not found */
19493 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19494 		return;
19495 	}
19496 	if (prev_ill == NULL) {
19497 		/* entry at front of list */
19498 		*ills = NULL;
19499 	} else {
19500 		prev_ill->next = cur_ill->next;
19501 	}
19502 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
19503 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19504 }
19505 
19506 /*
19507  * Called by SADB to send a DL_CONTROL_REQ message to every ill
19508  * supporting the specified IPsec protocol acceleration.
19509  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
19510  * We free the mblk and, if sa is non-null, release the held referece.
19511  */
19512 void
19513 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
19514     netstack_t *ns)
19515 {
19516 	ipsec_capab_ill_t *ici, *cur_ici;
19517 	ill_t *ill;
19518 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
19519 	ip_stack_t	*ipst = ns->netstack_ip;
19520 
19521 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
19522 	    ipst->ips_ipsec_capab_ills_esp;
19523 
19524 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
19525 
19526 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
19527 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
19528 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
19529 
19530 		/*
19531 		 * Handle the case where the ill goes away while the SADB is
19532 		 * attempting to send messages.  If it's going away, it's
19533 		 * nuking its shadow SADB, so we don't care..
19534 		 */
19535 
19536 		if (ill == NULL)
19537 			continue;
19538 
19539 		if (sa != NULL) {
19540 			/*
19541 			 * Make sure capabilities match before
19542 			 * sending SA to ill.
19543 			 */
19544 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
19545 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
19546 				ill_refrele(ill);
19547 				continue;
19548 			}
19549 
19550 			mutex_enter(&sa->ipsa_lock);
19551 			sa->ipsa_flags |= IPSA_F_HW;
19552 			mutex_exit(&sa->ipsa_lock);
19553 		}
19554 
19555 		/*
19556 		 * Copy template message, and add it to the front
19557 		 * of the mblk ship list. We want to avoid holding
19558 		 * the ipsec_capab_ills_lock while sending the
19559 		 * message to the ills.
19560 		 *
19561 		 * The b_next and b_prev are temporarily used
19562 		 * to build a list of mblks to be sent down, and to
19563 		 * save the ill to which they must be sent.
19564 		 */
19565 		nmp = copymsg(mp);
19566 		if (nmp == NULL) {
19567 			ill_refrele(ill);
19568 			continue;
19569 		}
19570 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
19571 		nmp->b_next = mp_ship_list;
19572 		mp_ship_list = nmp;
19573 		nmp->b_prev = (mblk_t *)ill;
19574 	}
19575 
19576 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19577 
19578 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
19579 		/* restore the mblk to a sane state */
19580 		next_mp = nmp->b_next;
19581 		nmp->b_next = NULL;
19582 		ill = (ill_t *)nmp->b_prev;
19583 		nmp->b_prev = NULL;
19584 
19585 		ill_dlpi_send(ill, nmp);
19586 		ill_refrele(ill);
19587 	}
19588 
19589 	if (sa != NULL)
19590 		IPSA_REFRELE(sa);
19591 	freemsg(mp);
19592 }
19593 
19594 /*
19595  * Derive an interface id from the link layer address.
19596  * Knows about IEEE 802 and IEEE EUI-64 mappings.
19597  */
19598 static boolean_t
19599 ip_ether_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19600 {
19601 	char		*addr;
19602 
19603 	if (ill->ill_phys_addr_length != ETHERADDRL)
19604 		return (B_FALSE);
19605 
19606 	/* Form EUI-64 like address */
19607 	addr = (char *)&v6addr->s6_addr32[2];
19608 	bcopy(ill->ill_phys_addr, addr, 3);
19609 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
19610 	addr[3] = (char)0xff;
19611 	addr[4] = (char)0xfe;
19612 	bcopy(ill->ill_phys_addr + 3, addr + 5, 3);
19613 	return (B_TRUE);
19614 }
19615 
19616 /* ARGSUSED */
19617 static boolean_t
19618 ip_nodef_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19619 {
19620 	return (B_FALSE);
19621 }
19622 
19623 typedef struct ipmp_ifcookie {
19624 	uint32_t	ic_hostid;
19625 	char		ic_ifname[LIFNAMSIZ];
19626 	char		ic_zonename[ZONENAME_MAX];
19627 } ipmp_ifcookie_t;
19628 
19629 /*
19630  * Construct a pseudo-random interface ID for the IPMP interface that's both
19631  * predictable and (almost) guaranteed to be unique.
19632  */
19633 static boolean_t
19634 ip_ipmp_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19635 {
19636 	zone_t		*zp;
19637 	uint8_t		*addr;
19638 	uchar_t		hash[16];
19639 	ulong_t 	hostid;
19640 	MD5_CTX		ctx;
19641 	ipmp_ifcookie_t	ic = { 0 };
19642 
19643 	ASSERT(IS_IPMP(ill));
19644 
19645 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
19646 	ic.ic_hostid = htonl((uint32_t)hostid);
19647 
19648 	(void) strlcpy(ic.ic_ifname, ill->ill_name, LIFNAMSIZ);
19649 
19650 	if ((zp = zone_find_by_id(ill->ill_zoneid)) != NULL) {
19651 		(void) strlcpy(ic.ic_zonename, zp->zone_name, ZONENAME_MAX);
19652 		zone_rele(zp);
19653 	}
19654 
19655 	MD5Init(&ctx);
19656 	MD5Update(&ctx, &ic, sizeof (ic));
19657 	MD5Final(hash, &ctx);
19658 
19659 	/*
19660 	 * Map the hash to an interface ID per the basic approach in RFC3041.
19661 	 */
19662 	addr = &v6addr->s6_addr8[8];
19663 	bcopy(hash + 8, addr, sizeof (uint64_t));
19664 	addr[0] &= ~0x2;				/* set local bit */
19665 
19666 	return (B_TRUE);
19667 }
19668 
19669 /* ARGSUSED */
19670 static boolean_t
19671 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
19672     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
19673 {
19674 	/*
19675 	 * Multicast address mappings used over Ethernet/802.X.
19676 	 * This address is used as a base for mappings.
19677 	 */
19678 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
19679 	    0x00, 0x00, 0x00};
19680 
19681 	/*
19682 	 * Extract low order 32 bits from IPv6 multicast address.
19683 	 * Or that into the link layer address, starting from the
19684 	 * second byte.
19685 	 */
19686 	*hw_start = 2;
19687 	v6_extract_mask->s6_addr32[0] = 0;
19688 	v6_extract_mask->s6_addr32[1] = 0;
19689 	v6_extract_mask->s6_addr32[2] = 0;
19690 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
19691 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
19692 	return (B_TRUE);
19693 }
19694 
19695 /*
19696  * Indicate by return value whether multicast is supported. If not,
19697  * this code should not touch/change any parameters.
19698  */
19699 /* ARGSUSED */
19700 static boolean_t
19701 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
19702     uint32_t *hw_start, ipaddr_t *extract_mask)
19703 {
19704 	/*
19705 	 * Multicast address mappings used over Ethernet/802.X.
19706 	 * This address is used as a base for mappings.
19707 	 */
19708 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
19709 	    0x00, 0x00, 0x00 };
19710 
19711 	if (phys_length != ETHERADDRL)
19712 		return (B_FALSE);
19713 
19714 	*extract_mask = htonl(0x007fffff);
19715 	*hw_start = 2;
19716 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
19717 	return (B_TRUE);
19718 }
19719 
19720 /*
19721  * Derive IPoIB interface id from the link layer address.
19722  */
19723 static boolean_t
19724 ip_ib_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19725 {
19726 	char		*addr;
19727 
19728 	if (ill->ill_phys_addr_length != 20)
19729 		return (B_FALSE);
19730 	addr = (char *)&v6addr->s6_addr32[2];
19731 	bcopy(ill->ill_phys_addr + 12, addr, 8);
19732 	/*
19733 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
19734 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
19735 	 * rules. In these cases, the IBA considers these GUIDs to be in
19736 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
19737 	 * required; vendors are required not to assign global EUI-64's
19738 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
19739 	 * of the interface identifier. Whether the GUID is in modified
19740 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
19741 	 * bit set to 1.
19742 	 */
19743 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
19744 	return (B_TRUE);
19745 }
19746 
19747 /*
19748  * Note on mapping from multicast IP addresses to IPoIB multicast link
19749  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
19750  * The format of an IPoIB multicast address is:
19751  *
19752  *  4 byte QPN      Scope Sign.  Pkey
19753  * +--------------------------------------------+
19754  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
19755  * +--------------------------------------------+
19756  *
19757  * The Scope and Pkey components are properties of the IBA port and
19758  * network interface. They can be ascertained from the broadcast address.
19759  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
19760  */
19761 
19762 static boolean_t
19763 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
19764     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
19765 {
19766 	/*
19767 	 * Base IPoIB IPv6 multicast address used for mappings.
19768 	 * Does not contain the IBA scope/Pkey values.
19769 	 */
19770 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
19771 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
19772 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
19773 
19774 	/*
19775 	 * Extract low order 80 bits from IPv6 multicast address.
19776 	 * Or that into the link layer address, starting from the
19777 	 * sixth byte.
19778 	 */
19779 	*hw_start = 6;
19780 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
19781 
19782 	/*
19783 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
19784 	 */
19785 	*(maddr + 5) = *(bphys_addr + 5);
19786 	*(maddr + 8) = *(bphys_addr + 8);
19787 	*(maddr + 9) = *(bphys_addr + 9);
19788 
19789 	v6_extract_mask->s6_addr32[0] = 0;
19790 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
19791 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
19792 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
19793 	return (B_TRUE);
19794 }
19795 
19796 static boolean_t
19797 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
19798     uint32_t *hw_start, ipaddr_t *extract_mask)
19799 {
19800 	/*
19801 	 * Base IPoIB IPv4 multicast address used for mappings.
19802 	 * Does not contain the IBA scope/Pkey values.
19803 	 */
19804 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
19805 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
19806 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
19807 
19808 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
19809 		return (B_FALSE);
19810 
19811 	/*
19812 	 * Extract low order 28 bits from IPv4 multicast address.
19813 	 * Or that into the link layer address, starting from the
19814 	 * sixteenth byte.
19815 	 */
19816 	*extract_mask = htonl(0x0fffffff);
19817 	*hw_start = 16;
19818 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
19819 
19820 	/*
19821 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
19822 	 */
19823 	*(maddr + 5) = *(bphys_addr + 5);
19824 	*(maddr + 8) = *(bphys_addr + 8);
19825 	*(maddr + 9) = *(bphys_addr + 9);
19826 	return (B_TRUE);
19827 }
19828 
19829 /*
19830  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
19831  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
19832  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
19833  * the link-local address is preferred.
19834  */
19835 boolean_t
19836 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
19837 {
19838 	ipif_t	*ipif;
19839 	ipif_t	*maybe_ipif = NULL;
19840 
19841 	mutex_enter(&ill->ill_lock);
19842 	if (ill->ill_state_flags & ILL_CONDEMNED) {
19843 		mutex_exit(&ill->ill_lock);
19844 		if (ipifp != NULL)
19845 			*ipifp = NULL;
19846 		return (B_FALSE);
19847 	}
19848 
19849 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19850 		if (!IPIF_CAN_LOOKUP(ipif))
19851 			continue;
19852 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
19853 		    ipif->ipif_zoneid != ALL_ZONES)
19854 			continue;
19855 		if ((ipif->ipif_flags & flags) != flags)
19856 			continue;
19857 
19858 		if (ipifp == NULL) {
19859 			mutex_exit(&ill->ill_lock);
19860 			ASSERT(maybe_ipif == NULL);
19861 			return (B_TRUE);
19862 		}
19863 		if (!ill->ill_isv6 ||
19864 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
19865 			ipif_refhold_locked(ipif);
19866 			mutex_exit(&ill->ill_lock);
19867 			*ipifp = ipif;
19868 			return (B_TRUE);
19869 		}
19870 		if (maybe_ipif == NULL)
19871 			maybe_ipif = ipif;
19872 	}
19873 	if (ipifp != NULL) {
19874 		if (maybe_ipif != NULL)
19875 			ipif_refhold_locked(maybe_ipif);
19876 		*ipifp = maybe_ipif;
19877 	}
19878 	mutex_exit(&ill->ill_lock);
19879 	return (maybe_ipif != NULL);
19880 }
19881 
19882 /*
19883  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
19884  * If a pointer to an ipif_t is returned then the caller will need to do
19885  * an ill_refrele().
19886  */
19887 ipif_t *
19888 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
19889     ip_stack_t *ipst)
19890 {
19891 	ipif_t *ipif;
19892 	ill_t *ill;
19893 
19894 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
19895 	    ipst);
19896 	if (ill == NULL)
19897 		return (NULL);
19898 
19899 	mutex_enter(&ill->ill_lock);
19900 	if (ill->ill_state_flags & ILL_CONDEMNED) {
19901 		mutex_exit(&ill->ill_lock);
19902 		ill_refrele(ill);
19903 		return (NULL);
19904 	}
19905 
19906 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19907 		if (!IPIF_CAN_LOOKUP(ipif))
19908 			continue;
19909 		if (lifidx == ipif->ipif_id) {
19910 			ipif_refhold_locked(ipif);
19911 			break;
19912 		}
19913 	}
19914 
19915 	mutex_exit(&ill->ill_lock);
19916 	ill_refrele(ill);
19917 	return (ipif);
19918 }
19919 
19920 /*
19921  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
19922  * There is one exceptions IRE_BROADCAST are difficult to recreate,
19923  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
19924  * for details.
19925  */
19926 void
19927 ill_fastpath_flush(ill_t *ill)
19928 {
19929 	ip_stack_t *ipst = ill->ill_ipst;
19930 
19931 	nce_fastpath_list_dispatch(ill, NULL, NULL);
19932 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
19933 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
19934 }
19935 
19936 /*
19937  * Set the physical address information for `ill' to the contents of the
19938  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
19939  * asynchronous if `ill' cannot immediately be quiesced -- in which case
19940  * EINPROGRESS will be returned.
19941  */
19942 int
19943 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
19944 {
19945 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
19946 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
19947 
19948 	ASSERT(IAM_WRITER_IPSQ(ipsq));
19949 
19950 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
19951 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
19952 		/* Changing DL_IPV6_TOKEN is not yet supported */
19953 		return (0);
19954 	}
19955 
19956 	/*
19957 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
19958 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
19959 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
19960 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
19961 	 */
19962 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
19963 		freemsg(mp);
19964 		return (ENOMEM);
19965 	}
19966 
19967 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
19968 
19969 	/*
19970 	 * If we can quiesce the ill, then set the address.  If not, then
19971 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
19972 	 */
19973 	ill_down_ipifs(ill);
19974 	mutex_enter(&ill->ill_lock);
19975 	if (!ill_is_quiescent(ill)) {
19976 		/* call cannot fail since `conn_t *' argument is NULL */
19977 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
19978 		    mp, ILL_DOWN);
19979 		mutex_exit(&ill->ill_lock);
19980 		return (EINPROGRESS);
19981 	}
19982 	mutex_exit(&ill->ill_lock);
19983 
19984 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
19985 	return (0);
19986 }
19987 
19988 /*
19989  * Once the ill associated with `q' has quiesced, set its physical address
19990  * information to the values in `addrmp'.  Note that two copies of `addrmp'
19991  * are passed (linked by b_cont), since we sometimes need to save two distinct
19992  * copies in the ill_t, and our context doesn't permit sleeping or allocation
19993  * failure (we'll free the other copy if it's not needed).  Since the ill_t
19994  * is quiesced, we know any stale IREs with the old address information have
19995  * already been removed, so we don't need to call ill_fastpath_flush().
19996  */
19997 /* ARGSUSED */
19998 static void
19999 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
20000 {
20001 	ill_t		*ill = q->q_ptr;
20002 	mblk_t		*addrmp2 = unlinkb(addrmp);
20003 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
20004 	uint_t		addrlen, addroff;
20005 
20006 	ASSERT(IAM_WRITER_IPSQ(ipsq));
20007 
20008 	addroff	= dlindp->dl_addr_offset;
20009 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
20010 
20011 	switch (dlindp->dl_data) {
20012 	case DL_IPV6_LINK_LAYER_ADDR:
20013 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
20014 		freemsg(addrmp2);
20015 		break;
20016 
20017 	case DL_CURR_PHYS_ADDR:
20018 		freemsg(ill->ill_phys_addr_mp);
20019 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
20020 		ill->ill_phys_addr_mp = addrmp;
20021 		ill->ill_phys_addr_length = addrlen;
20022 
20023 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
20024 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
20025 		else
20026 			freemsg(addrmp2);
20027 		break;
20028 	default:
20029 		ASSERT(0);
20030 	}
20031 
20032 	/*
20033 	 * If there are ipifs to bring up, ill_up_ipifs() will return
20034 	 * EINPROGRESS, and ipsq_current_finish() will be called by
20035 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
20036 	 * brought up.
20037 	 */
20038 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
20039 		ipsq_current_finish(ipsq);
20040 }
20041 
20042 /*
20043  * Helper routine for setting the ill_nd_lla fields.
20044  */
20045 void
20046 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
20047 {
20048 	freemsg(ill->ill_nd_lla_mp);
20049 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
20050 	ill->ill_nd_lla_mp = ndmp;
20051 	ill->ill_nd_lla_len = addrlen;
20052 }
20053 
20054 major_t IP_MAJ;
20055 #define	IP	"ip"
20056 
20057 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
20058 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
20059 
20060 /*
20061  * Issue REMOVEIF ioctls to have the loopback interfaces
20062  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
20063  * the former going away when the user-level processes in the zone
20064  * are killed  * and the latter are cleaned up by the stream head
20065  * str_stack_shutdown callback that undoes all I_PLINKs.
20066  */
20067 void
20068 ip_loopback_cleanup(ip_stack_t *ipst)
20069 {
20070 	int error;
20071 	ldi_handle_t	lh = NULL;
20072 	ldi_ident_t	li = NULL;
20073 	int		rval;
20074 	cred_t		*cr;
20075 	struct strioctl iocb;
20076 	struct lifreq	lifreq;
20077 
20078 	IP_MAJ = ddi_name_to_major(IP);
20079 
20080 #ifdef NS_DEBUG
20081 	(void) printf("ip_loopback_cleanup() stackid %d\n",
20082 	    ipst->ips_netstack->netstack_stackid);
20083 #endif
20084 
20085 	bzero(&lifreq, sizeof (lifreq));
20086 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
20087 
20088 	error = ldi_ident_from_major(IP_MAJ, &li);
20089 	if (error) {
20090 #ifdef DEBUG
20091 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
20092 		    error);
20093 #endif
20094 		return;
20095 	}
20096 
20097 	cr = zone_get_kcred(netstackid_to_zoneid(
20098 	    ipst->ips_netstack->netstack_stackid));
20099 	ASSERT(cr != NULL);
20100 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
20101 	if (error) {
20102 #ifdef DEBUG
20103 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
20104 		    error);
20105 #endif
20106 		goto out;
20107 	}
20108 	iocb.ic_cmd = SIOCLIFREMOVEIF;
20109 	iocb.ic_timout = 15;
20110 	iocb.ic_len = sizeof (lifreq);
20111 	iocb.ic_dp = (char *)&lifreq;
20112 
20113 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
20114 	/* LINTED - statement has no consequent */
20115 	if (error) {
20116 #ifdef NS_DEBUG
20117 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
20118 		    "UDP6 error %d\n", error);
20119 #endif
20120 	}
20121 	(void) ldi_close(lh, FREAD|FWRITE, cr);
20122 	lh = NULL;
20123 
20124 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
20125 	if (error) {
20126 #ifdef NS_DEBUG
20127 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
20128 		    error);
20129 #endif
20130 		goto out;
20131 	}
20132 
20133 	iocb.ic_cmd = SIOCLIFREMOVEIF;
20134 	iocb.ic_timout = 15;
20135 	iocb.ic_len = sizeof (lifreq);
20136 	iocb.ic_dp = (char *)&lifreq;
20137 
20138 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
20139 	/* LINTED - statement has no consequent */
20140 	if (error) {
20141 #ifdef NS_DEBUG
20142 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
20143 		    "UDP error %d\n", error);
20144 #endif
20145 	}
20146 	(void) ldi_close(lh, FREAD|FWRITE, cr);
20147 	lh = NULL;
20148 
20149 out:
20150 	/* Close layered handles */
20151 	if (lh)
20152 		(void) ldi_close(lh, FREAD|FWRITE, cr);
20153 	if (li)
20154 		ldi_ident_release(li);
20155 
20156 	crfree(cr);
20157 }
20158 
20159 /*
20160  * This needs to be in-sync with nic_event_t definition
20161  */
20162 static const char *
20163 ill_hook_event2str(nic_event_t event)
20164 {
20165 	switch (event) {
20166 	case NE_PLUMB:
20167 		return ("PLUMB");
20168 	case NE_UNPLUMB:
20169 		return ("UNPLUMB");
20170 	case NE_UP:
20171 		return ("UP");
20172 	case NE_DOWN:
20173 		return ("DOWN");
20174 	case NE_ADDRESS_CHANGE:
20175 		return ("ADDRESS_CHANGE");
20176 	case NE_LIF_UP:
20177 		return ("LIF_UP");
20178 	case NE_LIF_DOWN:
20179 		return ("LIF_DOWN");
20180 	default:
20181 		return ("UNKNOWN");
20182 	}
20183 }
20184 
20185 void
20186 ill_nic_event_dispatch(ill_t *ill, lif_if_t lif, nic_event_t event,
20187     nic_event_data_t data, size_t datalen)
20188 {
20189 	ip_stack_t		*ipst = ill->ill_ipst;
20190 	hook_nic_event_int_t	*info;
20191 	const char		*str = NULL;
20192 
20193 	/* create a new nic event info */
20194 	if ((info = kmem_alloc(sizeof (*info), KM_NOSLEEP)) == NULL)
20195 		goto fail;
20196 
20197 	info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
20198 	info->hnei_event.hne_lif = lif;
20199 	info->hnei_event.hne_event = event;
20200 	info->hnei_event.hne_protocol = ill->ill_isv6 ?
20201 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
20202 	info->hnei_event.hne_data = NULL;
20203 	info->hnei_event.hne_datalen = 0;
20204 	info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
20205 
20206 	if (data != NULL && datalen != 0) {
20207 		info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
20208 		if (info->hnei_event.hne_data == NULL)
20209 			goto fail;
20210 		bcopy(data, info->hnei_event.hne_data, datalen);
20211 		info->hnei_event.hne_datalen = datalen;
20212 	}
20213 
20214 	if (ddi_taskq_dispatch(eventq_queue_nic, ip_ne_queue_func, info,
20215 	    DDI_NOSLEEP) == DDI_SUCCESS)
20216 		return;
20217 
20218 fail:
20219 	if (info != NULL) {
20220 		if (info->hnei_event.hne_data != NULL) {
20221 			kmem_free(info->hnei_event.hne_data,
20222 			    info->hnei_event.hne_datalen);
20223 		}
20224 		kmem_free(info, sizeof (hook_nic_event_t));
20225 	}
20226 	str = ill_hook_event2str(event);
20227 	ip2dbg(("ill_nic_event_dispatch: could not dispatch %s nic event "
20228 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
20229 }
20230 
20231 void
20232 ipif_up_notify(ipif_t *ipif)
20233 {
20234 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
20235 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif, RTSQ_DEFAULT);
20236 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
20237 	ill_nic_event_dispatch(ipif->ipif_ill, MAP_IPIF_ID(ipif->ipif_id),
20238 	    NE_LIF_UP, NULL, 0);
20239 }
20240