xref: /titanic_51/usr/src/uts/common/inet/ip/ip_if.c (revision 7f0b8309074a5d8e9f9d8ffe7aad7bb0b1ee6b1f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 /*
28  * This file contains the interface control functions for IP.
29  */
30 
31 #include <sys/types.h>
32 #include <sys/stream.h>
33 #include <sys/dlpi.h>
34 #include <sys/stropts.h>
35 #include <sys/strsun.h>
36 #include <sys/sysmacros.h>
37 #include <sys/strsubr.h>
38 #include <sys/strlog.h>
39 #include <sys/ddi.h>
40 #include <sys/sunddi.h>
41 #include <sys/cmn_err.h>
42 #include <sys/kstat.h>
43 #include <sys/debug.h>
44 #include <sys/zone.h>
45 #include <sys/sunldi.h>
46 #include <sys/file.h>
47 #include <sys/bitmap.h>
48 #include <sys/cpuvar.h>
49 #include <sys/time.h>
50 #include <sys/ctype.h>
51 #include <sys/kmem.h>
52 #include <sys/systm.h>
53 #include <sys/param.h>
54 #include <sys/socket.h>
55 #include <sys/isa_defs.h>
56 #include <net/if.h>
57 #include <net/if_arp.h>
58 #include <net/if_types.h>
59 #include <net/if_dl.h>
60 #include <net/route.h>
61 #include <sys/sockio.h>
62 #include <netinet/in.h>
63 #include <netinet/ip6.h>
64 #include <netinet/icmp6.h>
65 #include <netinet/igmp_var.h>
66 #include <sys/policy.h>
67 #include <sys/ethernet.h>
68 #include <sys/callb.h>
69 #include <sys/md5.h>
70 
71 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
72 #include <inet/mi.h>
73 #include <inet/nd.h>
74 #include <inet/arp.h>
75 #include <inet/mib2.h>
76 #include <inet/ip.h>
77 #include <inet/ip6.h>
78 #include <inet/ip6_asp.h>
79 #include <inet/tcp.h>
80 #include <inet/ip_multi.h>
81 #include <inet/ip_ire.h>
82 #include <inet/ip_ftable.h>
83 #include <inet/ip_rts.h>
84 #include <inet/ip_ndp.h>
85 #include <inet/ip_if.h>
86 #include <inet/ip_impl.h>
87 #include <inet/tun.h>
88 #include <inet/sctp_ip.h>
89 #include <inet/ip_netinfo.h>
90 
91 #include <net/pfkeyv2.h>
92 #include <inet/ipsec_info.h>
93 #include <inet/sadb.h>
94 #include <inet/ipsec_impl.h>
95 #include <sys/iphada.h>
96 
97 #include <netinet/igmp.h>
98 #include <inet/ip_listutils.h>
99 #include <inet/ipclassifier.h>
100 #include <sys/mac_client.h>
101 #include <sys/dld.h>
102 
103 #include <sys/systeminfo.h>
104 #include <sys/bootconf.h>
105 
106 #include <sys/tsol/tndb.h>
107 #include <sys/tsol/tnet.h>
108 
109 /* The character which tells where the ill_name ends */
110 #define	IPIF_SEPARATOR_CHAR	':'
111 
112 /* IP ioctl function table entry */
113 typedef struct ipft_s {
114 	int	ipft_cmd;
115 	pfi_t	ipft_pfi;
116 	int	ipft_min_size;
117 	int	ipft_flags;
118 } ipft_t;
119 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
120 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
121 
122 typedef struct ip_sock_ar_s {
123 	union {
124 		area_t	ip_sock_area;
125 		ared_t	ip_sock_ared;
126 		areq_t	ip_sock_areq;
127 	} ip_sock_ar_u;
128 	queue_t	*ip_sock_ar_q;
129 } ip_sock_ar_t;
130 
131 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
132 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
133 		    char *value, caddr_t cp, cred_t *ioc_cr);
134 
135 static boolean_t ill_is_quiescent(ill_t *);
136 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
137 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
138 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
139     mblk_t *mp, boolean_t need_up);
140 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
141     mblk_t *mp, boolean_t need_up);
142 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
143     queue_t *q, mblk_t *mp, boolean_t need_up);
144 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
145     mblk_t *mp);
146 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
147     mblk_t *mp);
148 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
149     queue_t *q, mblk_t *mp, boolean_t need_up);
150 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
151     int ioccmd, struct linkblk *li, boolean_t doconsist);
152 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
153 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
154 static void	ipsq_flush(ill_t *ill);
155 
156 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
157     queue_t *q, mblk_t *mp, boolean_t need_up);
158 static void	ipsq_delete(ipsq_t *);
159 
160 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
161     boolean_t initialize, boolean_t insert);
162 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
163 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
164 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
165 		    boolean_t isv6);
166 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
167 static void	ipif_delete_cache_ire(ire_t *, char *);
168 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
169 static void	ipif_free(ipif_t *ipif);
170 static void	ipif_free_tail(ipif_t *ipif);
171 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
172 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
173 static void	ipif_set_default(ipif_t *ipif);
174 static int	ipif_set_values(queue_t *q, mblk_t *mp,
175     char *interf_name, uint_t *ppa);
176 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
177     queue_t *q);
178 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
179     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
180     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *);
181 static void	ipif_update_other_ipifs(ipif_t *old_ipif);
182 
183 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
184 static int	ill_arp_off(ill_t *ill);
185 static int	ill_arp_on(ill_t *ill);
186 static void	ill_delete_interface_type(ill_if_t *);
187 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
188 static void	ill_dl_down(ill_t *ill);
189 static void	ill_down(ill_t *ill);
190 static void	ill_downi(ire_t *ire, char *ill_arg);
191 static void	ill_free_mib(ill_t *ill);
192 static void	ill_glist_delete(ill_t *);
193 static void	ill_phyint_reinit(ill_t *ill);
194 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
195 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
196 static ip_v6intfid_func_t ip_ether_v6intfid, ip_ib_v6intfid;
197 static ip_v6intfid_func_t ip_ipmp_v6intfid, ip_nodef_v6intfid;
198 static ip_v6mapinfo_func_t ip_ether_v6mapinfo, ip_ib_v6mapinfo;
199 static ip_v4mapinfo_func_t ip_ether_v4mapinfo, ip_ib_v4mapinfo;
200 static void	ipif_save_ire(ipif_t *, ire_t *);
201 static void	ipif_remove_ire(ipif_t *, ire_t *);
202 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *, ip_stack_t *);
203 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
204 static void	phyint_free(phyint_t *);
205 
206 /*
207  * Per-ill IPsec capabilities management.
208  */
209 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
210 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
211 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
212 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
213 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
214 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
215     boolean_t);
216 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
217 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
218 static void ill_capability_mdt_reset_fill(ill_t *, mblk_t *);
219 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
220 static void ill_capability_ipsec_reset_fill(ill_t *, mblk_t *);
221 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
222 static void ill_capability_hcksum_reset_fill(ill_t *, mblk_t *);
223 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
224     dl_capability_sub_t *);
225 static void ill_capability_zerocopy_reset_fill(ill_t *, mblk_t *);
226 static int  ill_capability_ipsec_reset_size(ill_t *, int *, int *, int *,
227     int *);
228 static void	ill_capability_dld_reset_fill(ill_t *, mblk_t *);
229 static void	ill_capability_dld_ack(ill_t *, mblk_t *,
230 		    dl_capability_sub_t *);
231 static void	ill_capability_dld_enable(ill_t *);
232 static void	ill_capability_ack_thr(void *);
233 static void	ill_capability_lso_enable(ill_t *);
234 static void	ill_capability_send(ill_t *, mblk_t *);
235 
236 static ill_t	*ill_prev_usesrc(ill_t *);
237 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
238 static void	ill_disband_usesrc_group(ill_t *);
239 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
240 
241 #ifdef DEBUG
242 static  void    ill_trace_cleanup(const ill_t *);
243 static  void    ipif_trace_cleanup(const ipif_t *);
244 #endif
245 
246 /*
247  * if we go over the memory footprint limit more than once in this msec
248  * interval, we'll start pruning aggressively.
249  */
250 int ip_min_frag_prune_time = 0;
251 
252 /*
253  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
254  * and the IPsec DOI
255  */
256 #define	MAX_IPSEC_ALGS	256
257 
258 #define	BITSPERBYTE	8
259 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
260 
261 #define	IPSEC_ALG_ENABLE(algs, algid) \
262 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
263 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
264 
265 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
266 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
267 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
268 
269 typedef uint8_t ipsec_capab_elem_t;
270 
271 /*
272  * Per-algorithm parameters.  Note that at present, only encryption
273  * algorithms have variable keysize (IKE does not provide a way to negotiate
274  * auth algorithm keysize).
275  *
276  * All sizes here are in bits.
277  */
278 typedef struct
279 {
280 	uint16_t	minkeylen;
281 	uint16_t	maxkeylen;
282 } ipsec_capab_algparm_t;
283 
284 /*
285  * Per-ill capabilities.
286  */
287 struct ill_ipsec_capab_s {
288 	ipsec_capab_elem_t *encr_hw_algs;
289 	ipsec_capab_elem_t *auth_hw_algs;
290 	uint32_t algs_size;	/* size of _hw_algs in bytes */
291 	/* algorithm key lengths */
292 	ipsec_capab_algparm_t *encr_algparm;
293 	uint32_t encr_algparm_size;
294 	uint32_t encr_algparm_end;
295 };
296 
297 /*
298  * The field values are larger than strictly necessary for simple
299  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
300  */
301 static area_t	ip_area_template = {
302 	AR_ENTRY_ADD,			/* area_cmd */
303 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
304 					/* area_name_offset */
305 	/* area_name_length temporarily holds this structure length */
306 	sizeof (area_t),			/* area_name_length */
307 	IP_ARP_PROTO_TYPE,		/* area_proto */
308 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
309 	IP_ADDR_LEN,			/* area_proto_addr_length */
310 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
311 					/* area_proto_mask_offset */
312 	0,				/* area_flags */
313 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
314 					/* area_hw_addr_offset */
315 	/* Zero length hw_addr_length means 'use your idea of the address' */
316 	0				/* area_hw_addr_length */
317 };
318 
319 /*
320  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
321  * support
322  */
323 static area_t	ip6_area_template = {
324 	AR_ENTRY_ADD,			/* area_cmd */
325 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
326 					/* area_name_offset */
327 	/* area_name_length temporarily holds this structure length */
328 	sizeof (area_t),			/* area_name_length */
329 	IP_ARP_PROTO_TYPE,		/* area_proto */
330 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
331 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
332 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
333 					/* area_proto_mask_offset */
334 	0,				/* area_flags */
335 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
336 					/* area_hw_addr_offset */
337 	/* Zero length hw_addr_length means 'use your idea of the address' */
338 	0				/* area_hw_addr_length */
339 };
340 
341 static ared_t	ip_ared_template = {
342 	AR_ENTRY_DELETE,
343 	sizeof (ared_t) + IP_ADDR_LEN,
344 	sizeof (ared_t),
345 	IP_ARP_PROTO_TYPE,
346 	sizeof (ared_t),
347 	IP_ADDR_LEN,
348 	0
349 };
350 
351 static ared_t	ip6_ared_template = {
352 	AR_ENTRY_DELETE,
353 	sizeof (ared_t) + IPV6_ADDR_LEN,
354 	sizeof (ared_t),
355 	IP_ARP_PROTO_TYPE,
356 	sizeof (ared_t),
357 	IPV6_ADDR_LEN,
358 	0
359 };
360 
361 /*
362  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
363  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
364  * areq is used).
365  */
366 static areq_t	ip_areq_template = {
367 	AR_ENTRY_QUERY,			/* cmd */
368 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
369 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
370 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
371 	sizeof (areq_t),			/* target addr offset */
372 	IP_ADDR_LEN,			/* target addr_length */
373 	0,				/* flags */
374 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
375 	IP_ADDR_LEN,			/* sender addr length */
376 	AR_EQ_DEFAULT_XMIT_COUNT,	/* xmit_count */
377 	AR_EQ_DEFAULT_XMIT_INTERVAL,	/* (re)xmit_interval in milliseconds */
378 	AR_EQ_DEFAULT_MAX_BUFFERED	/* max # of requests to buffer */
379 	/* anything else filled in by the code */
380 };
381 
382 static arc_t	ip_aru_template = {
383 	AR_INTERFACE_UP,
384 	sizeof (arc_t),		/* Name offset */
385 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
386 };
387 
388 static arc_t	ip_ard_template = {
389 	AR_INTERFACE_DOWN,
390 	sizeof (arc_t),		/* Name offset */
391 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
392 };
393 
394 static arc_t	ip_aron_template = {
395 	AR_INTERFACE_ON,
396 	sizeof (arc_t),		/* Name offset */
397 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
398 };
399 
400 static arc_t	ip_aroff_template = {
401 	AR_INTERFACE_OFF,
402 	sizeof (arc_t),		/* Name offset */
403 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
404 };
405 
406 static arma_t	ip_arma_multi_template = {
407 	AR_MAPPING_ADD,
408 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
409 				/* Name offset */
410 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
411 	IP_ARP_PROTO_TYPE,
412 	sizeof (arma_t),			/* proto_addr_offset */
413 	IP_ADDR_LEN,				/* proto_addr_length */
414 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
415 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
416 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
417 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
418 	IP_MAX_HW_LEN,				/* hw_addr_length */
419 	0,					/* hw_mapping_start */
420 };
421 
422 static ipft_t	ip_ioctl_ftbl[] = {
423 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
424 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
425 		IPFT_F_NO_REPLY },
426 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
427 		IPFT_F_NO_REPLY },
428 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
429 	{ 0 }
430 };
431 
432 /* Simple ICMP IP Header Template */
433 static ipha_t icmp_ipha = {
434 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
435 };
436 
437 /* Flag descriptors for ip_ipif_report */
438 static nv_t	ipif_nv_tbl[] = {
439 	{ IPIF_UP,		"UP" },
440 	{ IPIF_BROADCAST,	"BROADCAST" },
441 	{ ILLF_DEBUG,		"DEBUG" },
442 	{ PHYI_LOOPBACK,	"LOOPBACK" },
443 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
444 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
445 	{ PHYI_RUNNING,		"RUNNING" },
446 	{ ILLF_NOARP,		"NOARP" },
447 	{ PHYI_PROMISC,		"PROMISC" },
448 	{ PHYI_ALLMULTI,	"ALLMULTI" },
449 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
450 	{ ILLF_MULTICAST,	"MULTICAST" },
451 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
452 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
453 	{ IPIF_DHCPRUNNING,	"DHCP" },
454 	{ IPIF_PRIVATE,		"PRIVATE" },
455 	{ IPIF_NOXMIT,		"NOXMIT" },
456 	{ IPIF_NOLOCAL,		"NOLOCAL" },
457 	{ IPIF_DEPRECATED,	"DEPRECATED" },
458 	{ IPIF_PREFERRED,	"PREFERRED" },
459 	{ IPIF_TEMPORARY,	"TEMPORARY" },
460 	{ IPIF_ADDRCONF,	"ADDRCONF" },
461 	{ PHYI_VIRTUAL,		"VIRTUAL" },
462 	{ ILLF_ROUTER,		"ROUTER" },
463 	{ ILLF_NONUD,		"NONUD" },
464 	{ IPIF_ANYCAST,		"ANYCAST" },
465 	{ ILLF_NORTEXCH,	"NORTEXCH" },
466 	{ ILLF_IPV4,		"IPV4" },
467 	{ ILLF_IPV6,		"IPV6" },
468 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
469 	{ PHYI_FAILED,		"FAILED" },
470 	{ PHYI_STANDBY,		"STANDBY" },
471 	{ PHYI_INACTIVE,	"INACTIVE" },
472 	{ PHYI_OFFLINE,		"OFFLINE" },
473 	{ PHYI_IPMP,		"IPMP" }
474 };
475 
476 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
477 
478 static ip_m_t   ip_m_tbl[] = {
479 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
480 	    ip_ether_v6intfid },
481 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
482 	    ip_nodef_v6intfid },
483 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
484 	    ip_nodef_v6intfid },
485 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
486 	    ip_nodef_v6intfid },
487 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
488 	    ip_ether_v6intfid },
489 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
490 	    ip_ib_v6intfid },
491 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL },
492 	{ SUNW_DL_IPMP, IFT_OTHER, NULL, NULL, ip_ipmp_v6intfid },
493 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
494 	    ip_nodef_v6intfid }
495 };
496 
497 static ill_t	ill_null;		/* Empty ILL for init. */
498 char	ipif_loopback_name[] = "lo0";
499 static char *ipv4_forward_suffix = ":ip_forwarding";
500 static char *ipv6_forward_suffix = ":ip6_forwarding";
501 static	sin6_t	sin6_null;	/* Zero address for quick clears */
502 static	sin_t	sin_null;	/* Zero address for quick clears */
503 
504 /* When set search for unused ipif_seqid */
505 static ipif_t	ipif_zero;
506 
507 /*
508  * ppa arena is created after these many
509  * interfaces have been plumbed.
510  */
511 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
512 
513 /*
514  * Allocate per-interface mibs.
515  * Returns true if ok. False otherwise.
516  *  ipsq  may not yet be allocated (loopback case ).
517  */
518 static boolean_t
519 ill_allocate_mibs(ill_t *ill)
520 {
521 	/* Already allocated? */
522 	if (ill->ill_ip_mib != NULL) {
523 		if (ill->ill_isv6)
524 			ASSERT(ill->ill_icmp6_mib != NULL);
525 		return (B_TRUE);
526 	}
527 
528 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
529 	    KM_NOSLEEP);
530 	if (ill->ill_ip_mib == NULL) {
531 		return (B_FALSE);
532 	}
533 
534 	/* Setup static information */
535 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
536 	    sizeof (mib2_ipIfStatsEntry_t));
537 	if (ill->ill_isv6) {
538 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
539 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
540 		    sizeof (mib2_ipv6AddrEntry_t));
541 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
542 		    sizeof (mib2_ipv6RouteEntry_t));
543 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
544 		    sizeof (mib2_ipv6NetToMediaEntry_t));
545 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
546 		    sizeof (ipv6_member_t));
547 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
548 		    sizeof (ipv6_grpsrc_t));
549 	} else {
550 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
551 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
552 		    sizeof (mib2_ipAddrEntry_t));
553 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
554 		    sizeof (mib2_ipRouteEntry_t));
555 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
556 		    sizeof (mib2_ipNetToMediaEntry_t));
557 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
558 		    sizeof (ip_member_t));
559 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
560 		    sizeof (ip_grpsrc_t));
561 
562 		/*
563 		 * For a v4 ill, we are done at this point, because per ill
564 		 * icmp mibs are only used for v6.
565 		 */
566 		return (B_TRUE);
567 	}
568 
569 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
570 	    KM_NOSLEEP);
571 	if (ill->ill_icmp6_mib == NULL) {
572 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
573 		ill->ill_ip_mib = NULL;
574 		return (B_FALSE);
575 	}
576 	/* static icmp info */
577 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
578 	    sizeof (mib2_ipv6IfIcmpEntry_t);
579 	/*
580 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
581 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
582 	 * -> ill_phyint_reinit
583 	 */
584 	return (B_TRUE);
585 }
586 
587 /*
588  * Common code for preparation of ARP commands.  Two points to remember:
589  * 	1) The ill_name is tacked on at the end of the allocated space so
590  *	   the templates name_offset field must contain the total space
591  *	   to allocate less the name length.
592  *
593  *	2) The templates name_length field should contain the *template*
594  *	   length.  We use it as a parameter to bcopy() and then write
595  *	   the real ill_name_length into the name_length field of the copy.
596  * (Always called as writer.)
597  */
598 mblk_t *
599 ill_arp_alloc(ill_t *ill, const uchar_t *template, caddr_t addr)
600 {
601 	arc_t	*arc = (arc_t *)template;
602 	char	*cp;
603 	int	len;
604 	mblk_t	*mp;
605 	uint_t	name_length = ill->ill_name_length;
606 	uint_t	template_len = arc->arc_name_length;
607 
608 	len = arc->arc_name_offset + name_length;
609 	mp = allocb(len, BPRI_HI);
610 	if (mp == NULL)
611 		return (NULL);
612 	cp = (char *)mp->b_rptr;
613 	mp->b_wptr = (uchar_t *)&cp[len];
614 	if (template_len)
615 		bcopy(template, cp, template_len);
616 	if (len > template_len)
617 		bzero(&cp[template_len], len - template_len);
618 	mp->b_datap->db_type = M_PROTO;
619 
620 	arc = (arc_t *)cp;
621 	arc->arc_name_length = name_length;
622 	cp = (char *)arc + arc->arc_name_offset;
623 	bcopy(ill->ill_name, cp, name_length);
624 
625 	if (addr) {
626 		area_t	*area = (area_t *)mp->b_rptr;
627 
628 		cp = (char *)area + area->area_proto_addr_offset;
629 		bcopy(addr, cp, area->area_proto_addr_length);
630 		if (area->area_cmd == AR_ENTRY_ADD) {
631 			cp = (char *)area;
632 			len = area->area_proto_addr_length;
633 			if (area->area_proto_mask_offset)
634 				cp += area->area_proto_mask_offset;
635 			else
636 				cp += area->area_proto_addr_offset + len;
637 			while (len-- > 0)
638 				*cp++ = (char)~0;
639 		}
640 	}
641 	return (mp);
642 }
643 
644 mblk_t *
645 ipif_area_alloc(ipif_t *ipif, uint_t optflags)
646 {
647 	caddr_t	addr;
648 	mblk_t 	*mp;
649 	area_t	*area;
650 	uchar_t	*areap;
651 	ill_t	*ill = ipif->ipif_ill;
652 
653 	if (ill->ill_isv6) {
654 		ASSERT(ill->ill_flags & ILLF_XRESOLV);
655 		addr = (caddr_t)&ipif->ipif_v6lcl_addr;
656 		areap = (uchar_t *)&ip6_area_template;
657 	} else {
658 		addr = (caddr_t)&ipif->ipif_lcl_addr;
659 		areap = (uchar_t *)&ip_area_template;
660 	}
661 
662 	if ((mp = ill_arp_alloc(ill, areap, addr)) == NULL)
663 		return (NULL);
664 
665 	/*
666 	 * IPMP requires that the hardware address be included in all
667 	 * AR_ENTRY_ADD requests so that ARP can deduce the arl to send on.
668 	 * If there are no active underlying ills in the group (and thus no
669 	 * hardware address, DAD will be deferred until an underlying ill
670 	 * becomes active.
671 	 */
672 	if (IS_IPMP(ill)) {
673 		if ((ill = ipmp_ipif_hold_bound_ill(ipif)) == NULL) {
674 			freemsg(mp);
675 			return (NULL);
676 		}
677 	} else {
678 		ill_refhold(ill);
679 	}
680 
681 	area = (area_t *)mp->b_rptr;
682 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR;
683 	area->area_flags |= optflags;
684 	area->area_hw_addr_length = ill->ill_phys_addr_length;
685 	bcopy(ill->ill_phys_addr, mp->b_rptr + area->area_hw_addr_offset,
686 	    area->area_hw_addr_length);
687 
688 	ill_refrele(ill);
689 	return (mp);
690 }
691 
692 mblk_t *
693 ipif_ared_alloc(ipif_t *ipif)
694 {
695 	caddr_t	addr;
696 	uchar_t	*aredp;
697 
698 	if (ipif->ipif_ill->ill_isv6) {
699 		ASSERT(ipif->ipif_ill->ill_flags & ILLF_XRESOLV);
700 		addr = (caddr_t)&ipif->ipif_v6lcl_addr;
701 		aredp = (uchar_t *)&ip6_ared_template;
702 	} else {
703 		addr = (caddr_t)&ipif->ipif_lcl_addr;
704 		aredp = (uchar_t *)&ip_ared_template;
705 	}
706 
707 	return (ill_arp_alloc(ipif->ipif_ill, aredp, addr));
708 }
709 
710 mblk_t *
711 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
712 {
713 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
714 	    (char *)&addr));
715 }
716 
717 mblk_t *
718 ill_arie_alloc(ill_t *ill, const char *grifname, const void *template)
719 {
720 	mblk_t	*mp = ill_arp_alloc(ill, template, 0);
721 	arie_t	*arie;
722 
723 	if (mp != NULL) {
724 		arie = (arie_t *)mp->b_rptr;
725 		(void) strlcpy(arie->arie_grifname, grifname, LIFNAMSIZ);
726 	}
727 	return (mp);
728 }
729 
730 /*
731  * Completely vaporize a lower level tap and all associated interfaces.
732  * ill_delete is called only out of ip_close when the device control
733  * stream is being closed.
734  */
735 void
736 ill_delete(ill_t *ill)
737 {
738 	ipif_t	*ipif;
739 	ill_t	*prev_ill;
740 	ip_stack_t	*ipst = ill->ill_ipst;
741 
742 	/*
743 	 * ill_delete may be forcibly entering the ipsq. The previous
744 	 * ioctl may not have completed and may need to be aborted.
745 	 * ipsq_flush takes care of it. If we don't need to enter the
746 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
747 	 * ill_delete_tail is sufficient.
748 	 */
749 	ipsq_flush(ill);
750 
751 	/*
752 	 * Nuke all interfaces.  ipif_free will take down the interface,
753 	 * remove it from the list, and free the data structure.
754 	 * Walk down the ipif list and remove the logical interfaces
755 	 * first before removing the main ipif. We can't unplumb
756 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
757 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
758 	 * POINTOPOINT.
759 	 *
760 	 * If ill_ipif was not properly initialized (i.e low on memory),
761 	 * then no interfaces to clean up. In this case just clean up the
762 	 * ill.
763 	 */
764 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
765 		ipif_free(ipif);
766 
767 	/*
768 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
769 	 * So nobody can be using this mp now. Free the mp allocated for
770 	 * honoring ILLF_NOARP
771 	 */
772 	freemsg(ill->ill_arp_on_mp);
773 	ill->ill_arp_on_mp = NULL;
774 
775 	/* Clean up msgs on pending upcalls for mrouted */
776 	reset_mrt_ill(ill);
777 
778 	/*
779 	 * ipif_free -> reset_conn_ipif will remove all multicast
780 	 * references for IPv4. For IPv6, we need to do it here as
781 	 * it points only at ills.
782 	 */
783 	reset_conn_ill(ill);
784 
785 	/*
786 	 * Remove multicast references added as a result of calls to
787 	 * ip_join_allmulti().
788 	 */
789 	ip_purge_allmulti(ill);
790 
791 	/*
792 	 * If the ill being deleted is under IPMP, boot it out of the illgrp.
793 	 */
794 	if (IS_UNDER_IPMP(ill))
795 		ipmp_ill_leave_illgrp(ill);
796 
797 	/*
798 	 * ill_down will arrange to blow off any IRE's dependent on this
799 	 * ILL, and shut down fragmentation reassembly.
800 	 */
801 	ill_down(ill);
802 
803 	/* Let SCTP know, so that it can remove this from its list. */
804 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
805 
806 	/*
807 	 * If an address on this ILL is being used as a source address then
808 	 * clear out the pointers in other ILLs that point to this ILL.
809 	 */
810 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
811 	if (ill->ill_usesrc_grp_next != NULL) {
812 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
813 			ill_disband_usesrc_group(ill);
814 		} else {	/* consumer of the usesrc ILL */
815 			prev_ill = ill_prev_usesrc(ill);
816 			prev_ill->ill_usesrc_grp_next =
817 			    ill->ill_usesrc_grp_next;
818 		}
819 	}
820 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
821 }
822 
823 static void
824 ipif_non_duplicate(ipif_t *ipif)
825 {
826 	ill_t *ill = ipif->ipif_ill;
827 	mutex_enter(&ill->ill_lock);
828 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
829 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
830 		ASSERT(ill->ill_ipif_dup_count > 0);
831 		ill->ill_ipif_dup_count--;
832 	}
833 	mutex_exit(&ill->ill_lock);
834 }
835 
836 /*
837  * ill_delete_tail is called from ip_modclose after all references
838  * to the closing ill are gone. The wait is done in ip_modclose
839  */
840 void
841 ill_delete_tail(ill_t *ill)
842 {
843 	mblk_t	**mpp;
844 	ipif_t	*ipif;
845 	ip_stack_t	*ipst = ill->ill_ipst;
846 
847 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
848 		ipif_non_duplicate(ipif);
849 		ipif_down_tail(ipif);
850 	}
851 
852 	ASSERT(ill->ill_ipif_dup_count == 0 &&
853 	    ill->ill_arp_down_mp == NULL &&
854 	    ill->ill_arp_del_mapping_mp == NULL);
855 
856 	/*
857 	 * If polling capability is enabled (which signifies direct
858 	 * upcall into IP and driver has ill saved as a handle),
859 	 * we need to make sure that unbind has completed before we
860 	 * let the ill disappear and driver no longer has any reference
861 	 * to this ill.
862 	 */
863 	mutex_enter(&ill->ill_lock);
864 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
865 		cv_wait(&ill->ill_cv, &ill->ill_lock);
866 	mutex_exit(&ill->ill_lock);
867 	ASSERT(!(ill->ill_capabilities &
868 	    (ILL_CAPAB_DLD | ILL_CAPAB_DLD_POLL | ILL_CAPAB_DLD_DIRECT)));
869 
870 	if (ill->ill_net_type != IRE_LOOPBACK)
871 		qprocsoff(ill->ill_rq);
872 
873 	/*
874 	 * We do an ipsq_flush once again now. New messages could have
875 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
876 	 * could also have landed up if an ioctl thread had looked up
877 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
878 	 * enqueued the ioctl when we did the ipsq_flush last time.
879 	 */
880 	ipsq_flush(ill);
881 
882 	/*
883 	 * Free capabilities.
884 	 */
885 	if (ill->ill_ipsec_capab_ah != NULL) {
886 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
887 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
888 		ill->ill_ipsec_capab_ah = NULL;
889 	}
890 
891 	if (ill->ill_ipsec_capab_esp != NULL) {
892 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
893 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
894 		ill->ill_ipsec_capab_esp = NULL;
895 	}
896 
897 	if (ill->ill_mdt_capab != NULL) {
898 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
899 		ill->ill_mdt_capab = NULL;
900 	}
901 
902 	if (ill->ill_hcksum_capab != NULL) {
903 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
904 		ill->ill_hcksum_capab = NULL;
905 	}
906 
907 	if (ill->ill_zerocopy_capab != NULL) {
908 		kmem_free(ill->ill_zerocopy_capab,
909 		    sizeof (ill_zerocopy_capab_t));
910 		ill->ill_zerocopy_capab = NULL;
911 	}
912 
913 	if (ill->ill_lso_capab != NULL) {
914 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
915 		ill->ill_lso_capab = NULL;
916 	}
917 
918 	if (ill->ill_dld_capab != NULL) {
919 		kmem_free(ill->ill_dld_capab, sizeof (ill_dld_capab_t));
920 		ill->ill_dld_capab = NULL;
921 	}
922 
923 	while (ill->ill_ipif != NULL)
924 		ipif_free_tail(ill->ill_ipif);
925 
926 	/*
927 	 * We have removed all references to ilm from conn and the ones joined
928 	 * within the kernel.
929 	 *
930 	 * We don't walk conns, mrts and ires because
931 	 *
932 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
933 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
934 	 *    ill references.
935 	 */
936 	ASSERT(ilm_walk_ill(ill) == 0);
937 
938 	/*
939 	 * If this ill is an IPMP meta-interface, blow away the illgrp.  This
940 	 * is safe to do because the illgrp has already been unlinked from the
941 	 * group by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find it.
942 	 */
943 	if (IS_IPMP(ill)) {
944 		ipmp_illgrp_destroy(ill->ill_grp);
945 		ill->ill_grp = NULL;
946 	}
947 
948 	/*
949 	 * Take us out of the list of ILLs. ill_glist_delete -> phyint_free
950 	 * could free the phyint. No more reference to the phyint after this
951 	 * point.
952 	 */
953 	(void) ill_glist_delete(ill);
954 
955 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
956 	if (ill->ill_ndd_name != NULL)
957 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
958 	rw_exit(&ipst->ips_ip_g_nd_lock);
959 
960 	if (ill->ill_frag_ptr != NULL) {
961 		uint_t count;
962 
963 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
964 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
965 		}
966 		mi_free(ill->ill_frag_ptr);
967 		ill->ill_frag_ptr = NULL;
968 		ill->ill_frag_hash_tbl = NULL;
969 	}
970 
971 	freemsg(ill->ill_nd_lla_mp);
972 	/* Free all retained control messages. */
973 	mpp = &ill->ill_first_mp_to_free;
974 	do {
975 		while (mpp[0]) {
976 			mblk_t  *mp;
977 			mblk_t  *mp1;
978 
979 			mp = mpp[0];
980 			mpp[0] = mp->b_next;
981 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
982 				mp1->b_next = NULL;
983 				mp1->b_prev = NULL;
984 			}
985 			freemsg(mp);
986 		}
987 	} while (mpp++ != &ill->ill_last_mp_to_free);
988 
989 	ill_free_mib(ill);
990 
991 #ifdef DEBUG
992 	ill_trace_cleanup(ill);
993 #endif
994 
995 	/* Drop refcnt here */
996 	netstack_rele(ill->ill_ipst->ips_netstack);
997 	ill->ill_ipst = NULL;
998 }
999 
1000 static void
1001 ill_free_mib(ill_t *ill)
1002 {
1003 	ip_stack_t *ipst = ill->ill_ipst;
1004 
1005 	/*
1006 	 * MIB statistics must not be lost, so when an interface
1007 	 * goes away the counter values will be added to the global
1008 	 * MIBs.
1009 	 */
1010 	if (ill->ill_ip_mib != NULL) {
1011 		if (ill->ill_isv6) {
1012 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
1013 			    ill->ill_ip_mib);
1014 		} else {
1015 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
1016 			    ill->ill_ip_mib);
1017 		}
1018 
1019 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
1020 		ill->ill_ip_mib = NULL;
1021 	}
1022 	if (ill->ill_icmp6_mib != NULL) {
1023 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
1024 		    ill->ill_icmp6_mib);
1025 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
1026 		ill->ill_icmp6_mib = NULL;
1027 	}
1028 }
1029 
1030 /*
1031  * Concatenate together a physical address and a sap.
1032  *
1033  * Sap_lengths are interpreted as follows:
1034  *   sap_length == 0	==>	no sap
1035  *   sap_length > 0	==>	sap is at the head of the dlpi address
1036  *   sap_length < 0	==>	sap is at the tail of the dlpi address
1037  */
1038 static void
1039 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
1040     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
1041 {
1042 	uint16_t sap_addr = (uint16_t)sap_src;
1043 
1044 	if (sap_length == 0) {
1045 		if (phys_src == NULL)
1046 			bzero(dst, phys_length);
1047 		else
1048 			bcopy(phys_src, dst, phys_length);
1049 	} else if (sap_length < 0) {
1050 		if (phys_src == NULL)
1051 			bzero(dst, phys_length);
1052 		else
1053 			bcopy(phys_src, dst, phys_length);
1054 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
1055 	} else {
1056 		bcopy(&sap_addr, dst, sizeof (sap_addr));
1057 		if (phys_src == NULL)
1058 			bzero((char *)dst + sap_length, phys_length);
1059 		else
1060 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
1061 	}
1062 }
1063 
1064 /*
1065  * Generate a dl_unitdata_req mblk for the device and address given.
1066  * addr_length is the length of the physical portion of the address.
1067  * If addr is NULL include an all zero address of the specified length.
1068  * TRUE? In any case, addr_length is taken to be the entire length of the
1069  * dlpi address, including the absolute value of sap_length.
1070  */
1071 mblk_t *
1072 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1073 		t_scalar_t sap_length)
1074 {
1075 	dl_unitdata_req_t *dlur;
1076 	mblk_t	*mp;
1077 	t_scalar_t	abs_sap_length;		/* absolute value */
1078 
1079 	abs_sap_length = ABS(sap_length);
1080 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1081 	    DL_UNITDATA_REQ);
1082 	if (mp == NULL)
1083 		return (NULL);
1084 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1085 	/* HACK: accomodate incompatible DLPI drivers */
1086 	if (addr_length == 8)
1087 		addr_length = 6;
1088 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1089 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1090 	dlur->dl_priority.dl_min = 0;
1091 	dlur->dl_priority.dl_max = 0;
1092 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1093 	    (uchar_t *)&dlur[1]);
1094 	return (mp);
1095 }
1096 
1097 /*
1098  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1099  * Return an error if we already have 1 or more ioctls in progress.
1100  * This is used only for non-exclusive ioctls. Currently this is used
1101  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1102  * and thus need to use ipsq_pending_mp_add.
1103  */
1104 boolean_t
1105 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1106 {
1107 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1108 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1109 	/*
1110 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1111 	 */
1112 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1113 	    (add_mp->b_datap->db_type == M_IOCTL));
1114 
1115 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1116 	/*
1117 	 * Return error if the conn has started closing. The conn
1118 	 * could have finished cleaning up the pending mp list,
1119 	 * If so we should not add another mp to the list negating
1120 	 * the cleanup.
1121 	 */
1122 	if (connp->conn_state_flags & CONN_CLOSING)
1123 		return (B_FALSE);
1124 	/*
1125 	 * Add the pending mp to the head of the list, chained by b_next.
1126 	 * Note down the conn on which the ioctl request came, in b_prev.
1127 	 * This will be used to later get the conn, when we get a response
1128 	 * on the ill queue, from some other module (typically arp)
1129 	 */
1130 	add_mp->b_next = (void *)ill->ill_pending_mp;
1131 	add_mp->b_queue = CONNP_TO_WQ(connp);
1132 	ill->ill_pending_mp = add_mp;
1133 	if (connp != NULL)
1134 		connp->conn_oper_pending_ill = ill;
1135 	return (B_TRUE);
1136 }
1137 
1138 /*
1139  * Retrieve the ill_pending_mp and return it. We have to walk the list
1140  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1141  */
1142 mblk_t *
1143 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1144 {
1145 	mblk_t	*prev = NULL;
1146 	mblk_t	*curr = NULL;
1147 	uint_t	id;
1148 	conn_t	*connp;
1149 
1150 	/*
1151 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1152 	 * up the pending mp, but it does not know the ioc_id and
1153 	 * passes in a zero for it.
1154 	 */
1155 	mutex_enter(&ill->ill_lock);
1156 	if (ioc_id != 0)
1157 		*connpp = NULL;
1158 
1159 	/* Search the list for the appropriate ioctl based on ioc_id */
1160 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1161 	    prev = curr, curr = curr->b_next) {
1162 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1163 		connp = Q_TO_CONN(curr->b_queue);
1164 		/* Match based on the ioc_id or based on the conn */
1165 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1166 			break;
1167 	}
1168 
1169 	if (curr != NULL) {
1170 		/* Unlink the mblk from the pending mp list */
1171 		if (prev != NULL) {
1172 			prev->b_next = curr->b_next;
1173 		} else {
1174 			ASSERT(ill->ill_pending_mp == curr);
1175 			ill->ill_pending_mp = curr->b_next;
1176 		}
1177 
1178 		/*
1179 		 * conn refcnt must have been bumped up at the start of
1180 		 * the ioctl. So we can safely access the conn.
1181 		 */
1182 		ASSERT(CONN_Q(curr->b_queue));
1183 		*connpp = Q_TO_CONN(curr->b_queue);
1184 		curr->b_next = NULL;
1185 		curr->b_queue = NULL;
1186 	}
1187 
1188 	mutex_exit(&ill->ill_lock);
1189 
1190 	return (curr);
1191 }
1192 
1193 /*
1194  * Add the pending mp to the list. There can be only 1 pending mp
1195  * in the list. Any exclusive ioctl that needs to wait for a response
1196  * from another module or driver needs to use this function to set
1197  * the ipx_pending_mp to the ioctl mblk and wait for the response from
1198  * the other module/driver. This is also used while waiting for the
1199  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1200  */
1201 boolean_t
1202 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1203     int waitfor)
1204 {
1205 	ipxop_t	*ipx = ipif->ipif_ill->ill_phyint->phyint_ipsq->ipsq_xop;
1206 
1207 	ASSERT(IAM_WRITER_IPIF(ipif));
1208 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1209 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1210 	ASSERT(ipx->ipx_pending_mp == NULL);
1211 	/*
1212 	 * The caller may be using a different ipif than the one passed into
1213 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
1214 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
1215 	 * that `ipx_current_ipif == ipif'.
1216 	 */
1217 	ASSERT(ipx->ipx_current_ipif != NULL);
1218 
1219 	/*
1220 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1221 	 * M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the driver.
1222 	 */
1223 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1224 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP) ||
1225 	    (DB_TYPE(add_mp) == M_PROTO) || (DB_TYPE(add_mp) == M_PCPROTO));
1226 
1227 	if (connp != NULL) {
1228 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1229 		/*
1230 		 * Return error if the conn has started closing. The conn
1231 		 * could have finished cleaning up the pending mp list,
1232 		 * If so we should not add another mp to the list negating
1233 		 * the cleanup.
1234 		 */
1235 		if (connp->conn_state_flags & CONN_CLOSING)
1236 			return (B_FALSE);
1237 	}
1238 	mutex_enter(&ipx->ipx_lock);
1239 	ipx->ipx_pending_ipif = ipif;
1240 	/*
1241 	 * Note down the queue in b_queue. This will be returned by
1242 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1243 	 * the processing
1244 	 */
1245 	add_mp->b_next = NULL;
1246 	add_mp->b_queue = q;
1247 	ipx->ipx_pending_mp = add_mp;
1248 	ipx->ipx_waitfor = waitfor;
1249 	mutex_exit(&ipx->ipx_lock);
1250 
1251 	if (connp != NULL)
1252 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1253 
1254 	return (B_TRUE);
1255 }
1256 
1257 /*
1258  * Retrieve the ipx_pending_mp and return it. There can be only 1 mp
1259  * queued in the list.
1260  */
1261 mblk_t *
1262 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1263 {
1264 	mblk_t	*curr = NULL;
1265 	ipxop_t	*ipx = ipsq->ipsq_xop;
1266 
1267 	*connpp = NULL;
1268 	mutex_enter(&ipx->ipx_lock);
1269 	if (ipx->ipx_pending_mp == NULL) {
1270 		mutex_exit(&ipx->ipx_lock);
1271 		return (NULL);
1272 	}
1273 
1274 	/* There can be only 1 such excl message */
1275 	curr = ipx->ipx_pending_mp;
1276 	ASSERT(curr->b_next == NULL);
1277 	ipx->ipx_pending_ipif = NULL;
1278 	ipx->ipx_pending_mp = NULL;
1279 	ipx->ipx_waitfor = 0;
1280 	mutex_exit(&ipx->ipx_lock);
1281 
1282 	if (CONN_Q(curr->b_queue)) {
1283 		/*
1284 		 * This mp did a refhold on the conn, at the start of the ioctl.
1285 		 * So we can safely return a pointer to the conn to the caller.
1286 		 */
1287 		*connpp = Q_TO_CONN(curr->b_queue);
1288 	} else {
1289 		*connpp = NULL;
1290 	}
1291 	curr->b_next = NULL;
1292 	curr->b_prev = NULL;
1293 	return (curr);
1294 }
1295 
1296 /*
1297  * Cleanup the ioctl mp queued in ipx_pending_mp
1298  * - Called in the ill_delete path
1299  * - Called in the M_ERROR or M_HANGUP path on the ill.
1300  * - Called in the conn close path.
1301  */
1302 boolean_t
1303 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1304 {
1305 	mblk_t	*mp;
1306 	ipxop_t	*ipx;
1307 	queue_t	*q;
1308 	ipif_t	*ipif;
1309 
1310 	ASSERT(IAM_WRITER_ILL(ill));
1311 	ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
1312 
1313 	/*
1314 	 * If connp is null, unconditionally clean up the ipx_pending_mp.
1315 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1316 	 * even if it is meant for another ill, since we have to enqueue
1317 	 * a new mp now in ipx_pending_mp to complete the ipif_down.
1318 	 * If connp is non-null we are called from the conn close path.
1319 	 */
1320 	mutex_enter(&ipx->ipx_lock);
1321 	mp = ipx->ipx_pending_mp;
1322 	if (mp == NULL || (connp != NULL &&
1323 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1324 		mutex_exit(&ipx->ipx_lock);
1325 		return (B_FALSE);
1326 	}
1327 	/* Now remove from the ipx_pending_mp */
1328 	ipx->ipx_pending_mp = NULL;
1329 	q = mp->b_queue;
1330 	mp->b_next = NULL;
1331 	mp->b_prev = NULL;
1332 	mp->b_queue = NULL;
1333 
1334 	ipif = ipx->ipx_pending_ipif;
1335 	ipx->ipx_pending_ipif = NULL;
1336 	ipx->ipx_waitfor = 0;
1337 	ipx->ipx_current_ipif = NULL;
1338 	ipx->ipx_current_ioctl = 0;
1339 	ipx->ipx_current_done = B_TRUE;
1340 	mutex_exit(&ipx->ipx_lock);
1341 
1342 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1343 		if (connp == NULL) {
1344 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1345 		} else {
1346 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
1347 			mutex_enter(&ipif->ipif_ill->ill_lock);
1348 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
1349 			mutex_exit(&ipif->ipif_ill->ill_lock);
1350 		}
1351 	} else {
1352 		/*
1353 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1354 		 * be just inet_freemsg. we have to restart it
1355 		 * otherwise the thread will be stuck.
1356 		 */
1357 		inet_freemsg(mp);
1358 	}
1359 	return (B_TRUE);
1360 }
1361 
1362 /*
1363  * The ill is closing. Cleanup all the pending mps. Called exclusively
1364  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1365  * knows this ill, and hence nobody can add an mp to this list
1366  */
1367 static void
1368 ill_pending_mp_cleanup(ill_t *ill)
1369 {
1370 	mblk_t	*mp;
1371 	queue_t	*q;
1372 
1373 	ASSERT(IAM_WRITER_ILL(ill));
1374 
1375 	mutex_enter(&ill->ill_lock);
1376 	/*
1377 	 * Every mp on the pending mp list originating from an ioctl
1378 	 * added 1 to the conn refcnt, at the start of the ioctl.
1379 	 * So bump it down now.  See comments in ip_wput_nondata()
1380 	 */
1381 	while (ill->ill_pending_mp != NULL) {
1382 		mp = ill->ill_pending_mp;
1383 		ill->ill_pending_mp = mp->b_next;
1384 		mutex_exit(&ill->ill_lock);
1385 
1386 		q = mp->b_queue;
1387 		ASSERT(CONN_Q(q));
1388 		mp->b_next = NULL;
1389 		mp->b_prev = NULL;
1390 		mp->b_queue = NULL;
1391 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1392 		mutex_enter(&ill->ill_lock);
1393 	}
1394 	ill->ill_pending_ipif = NULL;
1395 
1396 	mutex_exit(&ill->ill_lock);
1397 }
1398 
1399 /*
1400  * Called in the conn close path and ill delete path
1401  */
1402 static void
1403 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1404 {
1405 	ipsq_t	*ipsq;
1406 	mblk_t	*prev;
1407 	mblk_t	*curr;
1408 	mblk_t	*next;
1409 	queue_t	*q;
1410 	mblk_t	*tmp_list = NULL;
1411 
1412 	ASSERT(IAM_WRITER_ILL(ill));
1413 	if (connp != NULL)
1414 		q = CONNP_TO_WQ(connp);
1415 	else
1416 		q = ill->ill_wq;
1417 
1418 	ipsq = ill->ill_phyint->phyint_ipsq;
1419 	/*
1420 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1421 	 * In the case of ioctl from a conn, there can be only 1 mp
1422 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1423 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1424 	 * ioctls meant for this ill form conn's are not flushed. They will
1425 	 * be processed during ipsq_exit and will not find the ill and will
1426 	 * return error.
1427 	 */
1428 	mutex_enter(&ipsq->ipsq_lock);
1429 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1430 	    curr = next) {
1431 		next = curr->b_next;
1432 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1433 			/* Unlink the mblk from the pending mp list */
1434 			if (prev != NULL) {
1435 				prev->b_next = curr->b_next;
1436 			} else {
1437 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1438 				ipsq->ipsq_xopq_mphead = curr->b_next;
1439 			}
1440 			if (ipsq->ipsq_xopq_mptail == curr)
1441 				ipsq->ipsq_xopq_mptail = prev;
1442 			/*
1443 			 * Create a temporary list and release the ipsq lock
1444 			 * New elements are added to the head of the tmp_list
1445 			 */
1446 			curr->b_next = tmp_list;
1447 			tmp_list = curr;
1448 		} else {
1449 			prev = curr;
1450 		}
1451 	}
1452 	mutex_exit(&ipsq->ipsq_lock);
1453 
1454 	while (tmp_list != NULL) {
1455 		curr = tmp_list;
1456 		tmp_list = curr->b_next;
1457 		curr->b_next = NULL;
1458 		curr->b_prev = NULL;
1459 		curr->b_queue = NULL;
1460 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1461 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1462 			    CONN_CLOSE : NO_COPYOUT, NULL);
1463 		} else {
1464 			/*
1465 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1466 			 * this can't be just inet_freemsg. we have to
1467 			 * restart it otherwise the thread will be stuck.
1468 			 */
1469 			inet_freemsg(curr);
1470 		}
1471 	}
1472 }
1473 
1474 /*
1475  * This conn has started closing. Cleanup any pending ioctl from this conn.
1476  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1477  */
1478 void
1479 conn_ioctl_cleanup(conn_t *connp)
1480 {
1481 	mblk_t *curr;
1482 	ipsq_t	*ipsq;
1483 	ill_t	*ill;
1484 	boolean_t refheld;
1485 
1486 	/*
1487 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1488 	 * ioctl has not yet started, the mp is pending in the list headed by
1489 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1490 	 * ipx_pending_mp. If the ioctl timed out in the streamhead but
1491 	 * is currently executing now the mp is not queued anywhere but
1492 	 * conn_oper_pending_ill is null. The conn close will wait
1493 	 * till the conn_ref drops to zero.
1494 	 */
1495 	mutex_enter(&connp->conn_lock);
1496 	ill = connp->conn_oper_pending_ill;
1497 	if (ill == NULL) {
1498 		mutex_exit(&connp->conn_lock);
1499 		return;
1500 	}
1501 
1502 	curr = ill_pending_mp_get(ill, &connp, 0);
1503 	if (curr != NULL) {
1504 		mutex_exit(&connp->conn_lock);
1505 		CONN_DEC_REF(connp);
1506 		inet_freemsg(curr);
1507 		return;
1508 	}
1509 	/*
1510 	 * We may not be able to refhold the ill if the ill/ipif
1511 	 * is changing. But we need to make sure that the ill will
1512 	 * not vanish. So we just bump up the ill_waiter count.
1513 	 */
1514 	refheld = ill_waiter_inc(ill);
1515 	mutex_exit(&connp->conn_lock);
1516 	if (refheld) {
1517 		if (ipsq_enter(ill, B_TRUE, NEW_OP)) {
1518 			ill_waiter_dcr(ill);
1519 			/*
1520 			 * Check whether this ioctl has started and is
1521 			 * pending. If it is not found there then check
1522 			 * whether this ioctl has not even started and is in
1523 			 * the ipsq_xopq list.
1524 			 */
1525 			if (!ipsq_pending_mp_cleanup(ill, connp))
1526 				ipsq_xopq_mp_cleanup(ill, connp);
1527 			ipsq = ill->ill_phyint->phyint_ipsq;
1528 			ipsq_exit(ipsq);
1529 			return;
1530 		}
1531 	}
1532 
1533 	/*
1534 	 * The ill is also closing and we could not bump up the
1535 	 * ill_waiter_count or we could not enter the ipsq. Leave
1536 	 * the cleanup to ill_delete
1537 	 */
1538 	mutex_enter(&connp->conn_lock);
1539 	while (connp->conn_oper_pending_ill != NULL)
1540 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1541 	mutex_exit(&connp->conn_lock);
1542 	if (refheld)
1543 		ill_waiter_dcr(ill);
1544 }
1545 
1546 /*
1547  * ipcl_walk function for cleaning up conn_*_ill fields.
1548  */
1549 static void
1550 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1551 {
1552 	ill_t	*ill = (ill_t *)arg;
1553 	ire_t	*ire;
1554 
1555 	mutex_enter(&connp->conn_lock);
1556 	if (connp->conn_multicast_ill == ill) {
1557 		/* Revert to late binding */
1558 		connp->conn_multicast_ill = NULL;
1559 	}
1560 	if (connp->conn_incoming_ill == ill)
1561 		connp->conn_incoming_ill = NULL;
1562 	if (connp->conn_outgoing_ill == ill)
1563 		connp->conn_outgoing_ill = NULL;
1564 	if (connp->conn_dhcpinit_ill == ill) {
1565 		connp->conn_dhcpinit_ill = NULL;
1566 		ASSERT(ill->ill_dhcpinit != 0);
1567 		atomic_dec_32(&ill->ill_dhcpinit);
1568 	}
1569 	if (connp->conn_ire_cache != NULL) {
1570 		ire = connp->conn_ire_cache;
1571 		/*
1572 		 * Source address selection makes it possible for IRE_CACHE
1573 		 * entries to be created with ire_stq coming from interface X
1574 		 * and ipif coming from interface Y.  Thus whenever interface
1575 		 * X goes down, remove all references to it by checking both
1576 		 * on ire_ipif and ire_stq.
1577 		 */
1578 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1579 		    (ire->ire_type == IRE_CACHE &&
1580 		    ire->ire_stq == ill->ill_wq)) {
1581 			connp->conn_ire_cache = NULL;
1582 			mutex_exit(&connp->conn_lock);
1583 			ire_refrele_notr(ire);
1584 			return;
1585 		}
1586 	}
1587 	mutex_exit(&connp->conn_lock);
1588 }
1589 
1590 /* ARGSUSED */
1591 void
1592 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1593 {
1594 	ill_t	*ill = q->q_ptr;
1595 	ipif_t	*ipif;
1596 
1597 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1598 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1599 		ipif_non_duplicate(ipif);
1600 		ipif_down_tail(ipif);
1601 	}
1602 	freemsg(mp);
1603 	ipsq_current_finish(ipsq);
1604 }
1605 
1606 /*
1607  * ill_down_start is called when we want to down this ill and bring it up again
1608  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1609  * all interfaces, but don't tear down any plumbing.
1610  */
1611 boolean_t
1612 ill_down_start(queue_t *q, mblk_t *mp)
1613 {
1614 	ill_t	*ill = q->q_ptr;
1615 	ipif_t	*ipif;
1616 
1617 	ASSERT(IAM_WRITER_ILL(ill));
1618 
1619 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1620 		(void) ipif_down(ipif, NULL, NULL);
1621 
1622 	ill_down(ill);
1623 
1624 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1625 
1626 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1627 
1628 	/*
1629 	 * Atomically test and add the pending mp if references are active.
1630 	 */
1631 	mutex_enter(&ill->ill_lock);
1632 	if (!ill_is_quiescent(ill)) {
1633 		/* call cannot fail since `conn_t *' argument is NULL */
1634 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1635 		    mp, ILL_DOWN);
1636 		mutex_exit(&ill->ill_lock);
1637 		return (B_FALSE);
1638 	}
1639 	mutex_exit(&ill->ill_lock);
1640 	return (B_TRUE);
1641 }
1642 
1643 static void
1644 ill_down(ill_t *ill)
1645 {
1646 	ip_stack_t	*ipst = ill->ill_ipst;
1647 
1648 	/* Blow off any IREs dependent on this ILL. */
1649 	ire_walk(ill_downi, ill, ipst);
1650 
1651 	/* Remove any conn_*_ill depending on this ill */
1652 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1653 }
1654 
1655 /*
1656  * ire_walk routine used to delete every IRE that depends on queues
1657  * associated with 'ill'.  (Always called as writer.)
1658  */
1659 static void
1660 ill_downi(ire_t *ire, char *ill_arg)
1661 {
1662 	ill_t	*ill = (ill_t *)ill_arg;
1663 
1664 	/*
1665 	 * Source address selection makes it possible for IRE_CACHE
1666 	 * entries to be created with ire_stq coming from interface X
1667 	 * and ipif coming from interface Y.  Thus whenever interface
1668 	 * X goes down, remove all references to it by checking both
1669 	 * on ire_ipif and ire_stq.
1670 	 */
1671 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1672 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1673 		ire_delete(ire);
1674 	}
1675 }
1676 
1677 /*
1678  * Remove ire/nce from the fastpath list.
1679  */
1680 void
1681 ill_fastpath_nack(ill_t *ill)
1682 {
1683 	nce_fastpath_list_dispatch(ill, NULL, NULL);
1684 }
1685 
1686 /* Consume an M_IOCACK of the fastpath probe. */
1687 void
1688 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1689 {
1690 	mblk_t	*mp1 = mp;
1691 
1692 	/*
1693 	 * If this was the first attempt turn on the fastpath probing.
1694 	 */
1695 	mutex_enter(&ill->ill_lock);
1696 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1697 		ill->ill_dlpi_fastpath_state = IDS_OK;
1698 	mutex_exit(&ill->ill_lock);
1699 
1700 	/* Free the M_IOCACK mblk, hold on to the data */
1701 	mp = mp->b_cont;
1702 	freeb(mp1);
1703 	if (mp == NULL)
1704 		return;
1705 	if (mp->b_cont != NULL) {
1706 		/*
1707 		 * Update all IRE's or NCE's that are waiting for
1708 		 * fastpath update.
1709 		 */
1710 		nce_fastpath_list_dispatch(ill, ndp_fastpath_update, mp);
1711 		mp1 = mp->b_cont;
1712 		freeb(mp);
1713 		mp = mp1;
1714 	} else {
1715 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1716 	}
1717 
1718 	freeb(mp);
1719 }
1720 
1721 /*
1722  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1723  * The data portion of the request is a dl_unitdata_req_t template for
1724  * what we would send downstream in the absence of a fastpath confirmation.
1725  */
1726 int
1727 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1728 {
1729 	struct iocblk	*ioc;
1730 	mblk_t	*mp;
1731 
1732 	if (dlur_mp == NULL)
1733 		return (EINVAL);
1734 
1735 	mutex_enter(&ill->ill_lock);
1736 	switch (ill->ill_dlpi_fastpath_state) {
1737 	case IDS_FAILED:
1738 		/*
1739 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1740 		 * support it.
1741 		 */
1742 		mutex_exit(&ill->ill_lock);
1743 		return (ENOTSUP);
1744 	case IDS_UNKNOWN:
1745 		/* This is the first probe */
1746 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1747 		break;
1748 	default:
1749 		break;
1750 	}
1751 	mutex_exit(&ill->ill_lock);
1752 
1753 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1754 		return (EAGAIN);
1755 
1756 	mp->b_cont = copyb(dlur_mp);
1757 	if (mp->b_cont == NULL) {
1758 		freeb(mp);
1759 		return (EAGAIN);
1760 	}
1761 
1762 	ioc = (struct iocblk *)mp->b_rptr;
1763 	ioc->ioc_count = msgdsize(mp->b_cont);
1764 
1765 	putnext(ill->ill_wq, mp);
1766 	return (0);
1767 }
1768 
1769 void
1770 ill_capability_probe(ill_t *ill)
1771 {
1772 	mblk_t	*mp;
1773 
1774 	ASSERT(IAM_WRITER_ILL(ill));
1775 
1776 	if (ill->ill_dlpi_capab_state != IDCS_UNKNOWN &&
1777 	    ill->ill_dlpi_capab_state != IDCS_FAILED)
1778 		return;
1779 
1780 	/*
1781 	 * We are starting a new cycle of capability negotiation.
1782 	 * Free up the capab reset messages of any previous incarnation.
1783 	 * We will do a fresh allocation when we get the response to our probe
1784 	 */
1785 	if (ill->ill_capab_reset_mp != NULL) {
1786 		freemsg(ill->ill_capab_reset_mp);
1787 		ill->ill_capab_reset_mp = NULL;
1788 	}
1789 
1790 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1791 
1792 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t), DL_CAPABILITY_REQ);
1793 	if (mp == NULL)
1794 		return;
1795 
1796 	ill_capability_send(ill, mp);
1797 	ill->ill_dlpi_capab_state = IDCS_PROBE_SENT;
1798 }
1799 
1800 void
1801 ill_capability_reset(ill_t *ill, boolean_t reneg)
1802 {
1803 	ASSERT(IAM_WRITER_ILL(ill));
1804 
1805 	if (ill->ill_dlpi_capab_state != IDCS_OK)
1806 		return;
1807 
1808 	ill->ill_dlpi_capab_state = reneg ? IDCS_RENEG : IDCS_RESET_SENT;
1809 
1810 	ill_capability_send(ill, ill->ill_capab_reset_mp);
1811 	ill->ill_capab_reset_mp = NULL;
1812 	/*
1813 	 * We turn off all capabilities except those pertaining to
1814 	 * direct function call capabilities viz. ILL_CAPAB_DLD*
1815 	 * which will be turned off by the corresponding reset functions.
1816 	 */
1817 	ill->ill_capabilities &= ~(ILL_CAPAB_MDT | ILL_CAPAB_HCKSUM  |
1818 	    ILL_CAPAB_ZEROCOPY | ILL_CAPAB_AH | ILL_CAPAB_ESP);
1819 }
1820 
1821 static void
1822 ill_capability_reset_alloc(ill_t *ill)
1823 {
1824 	mblk_t *mp;
1825 	size_t	size = 0;
1826 	int	err;
1827 	dl_capability_req_t	*capb;
1828 
1829 	ASSERT(IAM_WRITER_ILL(ill));
1830 	ASSERT(ill->ill_capab_reset_mp == NULL);
1831 
1832 	if (ILL_MDT_CAPABLE(ill))
1833 		size += sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
1834 
1835 	if (ILL_HCKSUM_CAPABLE(ill)) {
1836 		size += sizeof (dl_capability_sub_t) +
1837 		    sizeof (dl_capab_hcksum_t);
1838 	}
1839 
1840 	if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) {
1841 		size += sizeof (dl_capability_sub_t) +
1842 		    sizeof (dl_capab_zerocopy_t);
1843 	}
1844 
1845 	if (ill->ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)) {
1846 		size += sizeof (dl_capability_sub_t);
1847 		size += ill_capability_ipsec_reset_size(ill, NULL, NULL,
1848 		    NULL, NULL);
1849 	}
1850 
1851 	if (ill->ill_capabilities & ILL_CAPAB_DLD) {
1852 		size += sizeof (dl_capability_sub_t) +
1853 		    sizeof (dl_capab_dld_t);
1854 	}
1855 
1856 	mp = allocb_wait(size + sizeof (dl_capability_req_t), BPRI_MED,
1857 	    STR_NOSIG, &err);
1858 
1859 	mp->b_datap->db_type = M_PROTO;
1860 	bzero(mp->b_rptr, size + sizeof (dl_capability_req_t));
1861 
1862 	capb = (dl_capability_req_t *)mp->b_rptr;
1863 	capb->dl_primitive = DL_CAPABILITY_REQ;
1864 	capb->dl_sub_offset = sizeof (dl_capability_req_t);
1865 	capb->dl_sub_length = size;
1866 
1867 	mp->b_wptr += sizeof (dl_capability_req_t);
1868 
1869 	/*
1870 	 * Each handler fills in the corresponding dl_capability_sub_t
1871 	 * inside the mblk,
1872 	 */
1873 	ill_capability_mdt_reset_fill(ill, mp);
1874 	ill_capability_hcksum_reset_fill(ill, mp);
1875 	ill_capability_zerocopy_reset_fill(ill, mp);
1876 	ill_capability_ipsec_reset_fill(ill, mp);
1877 	ill_capability_dld_reset_fill(ill, mp);
1878 
1879 	ill->ill_capab_reset_mp = mp;
1880 }
1881 
1882 static void
1883 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1884 {
1885 	dl_capab_id_t *id_ic;
1886 	uint_t sub_dl_cap = outers->dl_cap;
1887 	dl_capability_sub_t *inners;
1888 	uint8_t *capend;
1889 
1890 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1891 
1892 	/*
1893 	 * Note: range checks here are not absolutely sufficient to
1894 	 * make us robust against malformed messages sent by drivers;
1895 	 * this is in keeping with the rest of IP's dlpi handling.
1896 	 * (Remember, it's coming from something else in the kernel
1897 	 * address space)
1898 	 */
1899 
1900 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1901 	if (capend > mp->b_wptr) {
1902 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1903 		    "malformed sub-capability too long for mblk");
1904 		return;
1905 	}
1906 
1907 	id_ic = (dl_capab_id_t *)(outers + 1);
1908 
1909 	if (outers->dl_length < sizeof (*id_ic) ||
1910 	    (inners = &id_ic->id_subcap,
1911 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1912 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1913 		    "encapsulated capab type %d too long for mblk",
1914 		    inners->dl_cap);
1915 		return;
1916 	}
1917 
1918 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1919 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1920 		    "isn't as expected; pass-thru module(s) detected, "
1921 		    "discarding capability\n", inners->dl_cap));
1922 		return;
1923 	}
1924 
1925 	/* Process the encapsulated sub-capability */
1926 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1927 }
1928 
1929 /*
1930  * Process Multidata Transmit capability negotiation ack received from a
1931  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1932  * DL_CAPABILITY_ACK message.
1933  */
1934 static void
1935 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1936 {
1937 	mblk_t *nmp = NULL;
1938 	dl_capability_req_t *oc;
1939 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1940 	ill_mdt_capab_t **ill_mdt_capab;
1941 	uint_t sub_dl_cap = isub->dl_cap;
1942 	uint8_t *capend;
1943 
1944 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1945 
1946 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1947 
1948 	/*
1949 	 * Note: range checks here are not absolutely sufficient to
1950 	 * make us robust against malformed messages sent by drivers;
1951 	 * this is in keeping with the rest of IP's dlpi handling.
1952 	 * (Remember, it's coming from something else in the kernel
1953 	 * address space)
1954 	 */
1955 
1956 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1957 	if (capend > mp->b_wptr) {
1958 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1959 		    "malformed sub-capability too long for mblk");
1960 		return;
1961 	}
1962 
1963 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1964 
1965 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1966 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1967 		    "unsupported MDT sub-capability (version %d, expected %d)",
1968 		    mdt_ic->mdt_version, MDT_VERSION_2);
1969 		return;
1970 	}
1971 
1972 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1973 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1974 		    "capability isn't as expected; pass-thru module(s) "
1975 		    "detected, discarding capability\n"));
1976 		return;
1977 	}
1978 
1979 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
1980 
1981 		if (*ill_mdt_capab == NULL) {
1982 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
1983 			    KM_NOSLEEP);
1984 			if (*ill_mdt_capab == NULL) {
1985 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1986 				    "could not enable MDT version %d "
1987 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
1988 				    ill->ill_name);
1989 				return;
1990 			}
1991 		}
1992 
1993 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
1994 		    "MDT version %d (%d bytes leading, %d bytes trailing "
1995 		    "header spaces, %d max pld bufs, %d span limit)\n",
1996 		    ill->ill_name, MDT_VERSION_2,
1997 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
1998 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
1999 
2000 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
2001 		(*ill_mdt_capab)->ill_mdt_on = 1;
2002 		/*
2003 		 * Round the following values to the nearest 32-bit; ULP
2004 		 * may further adjust them to accomodate for additional
2005 		 * protocol headers.  We pass these values to ULP during
2006 		 * bind time.
2007 		 */
2008 		(*ill_mdt_capab)->ill_mdt_hdr_head =
2009 		    roundup(mdt_ic->mdt_hdr_head, 4);
2010 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
2011 		    roundup(mdt_ic->mdt_hdr_tail, 4);
2012 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
2013 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
2014 
2015 		ill->ill_capabilities |= ILL_CAPAB_MDT;
2016 	} else {
2017 		uint_t size;
2018 		uchar_t *rptr;
2019 
2020 		size = sizeof (dl_capability_req_t) +
2021 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
2022 
2023 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2024 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
2025 			    "could not enable MDT for %s (ENOMEM)\n",
2026 			    ill->ill_name);
2027 			return;
2028 		}
2029 
2030 		rptr = nmp->b_rptr;
2031 		/* initialize dl_capability_req_t */
2032 		oc = (dl_capability_req_t *)nmp->b_rptr;
2033 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
2034 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
2035 		    sizeof (dl_capab_mdt_t);
2036 		nmp->b_rptr += sizeof (dl_capability_req_t);
2037 
2038 		/* initialize dl_capability_sub_t */
2039 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2040 		nmp->b_rptr += sizeof (*isub);
2041 
2042 		/* initialize dl_capab_mdt_t */
2043 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2044 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2045 
2046 		nmp->b_rptr = rptr;
2047 
2048 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2049 		    "to enable MDT version %d\n", ill->ill_name,
2050 		    MDT_VERSION_2));
2051 
2052 		/* set ENABLE flag */
2053 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2054 
2055 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2056 		ill_capability_send(ill, nmp);
2057 	}
2058 }
2059 
2060 static void
2061 ill_capability_mdt_reset_fill(ill_t *ill, mblk_t *mp)
2062 {
2063 	dl_capab_mdt_t *mdt_subcap;
2064 	dl_capability_sub_t *dl_subcap;
2065 
2066 	if (!ILL_MDT_CAPABLE(ill))
2067 		return;
2068 
2069 	ASSERT(ill->ill_mdt_capab != NULL);
2070 
2071 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2072 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2073 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2074 
2075 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2076 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2077 	mdt_subcap->mdt_flags = 0;
2078 	mdt_subcap->mdt_hdr_head = 0;
2079 	mdt_subcap->mdt_hdr_tail = 0;
2080 
2081 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2082 }
2083 
2084 static void
2085 ill_capability_dld_reset_fill(ill_t *ill, mblk_t *mp)
2086 {
2087 	dl_capability_sub_t *dl_subcap;
2088 
2089 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
2090 		return;
2091 
2092 	/*
2093 	 * The dl_capab_dld_t that follows the dl_capability_sub_t is not
2094 	 * initialized below since it is not used by DLD.
2095 	 */
2096 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2097 	dl_subcap->dl_cap = DL_CAPAB_DLD;
2098 	dl_subcap->dl_length = sizeof (dl_capab_dld_t);
2099 
2100 	mp->b_wptr += sizeof (dl_capability_sub_t) + sizeof (dl_capab_dld_t);
2101 }
2102 
2103 /*
2104  * Send a DL_NOTIFY_REQ to the specified ill to enable
2105  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2106  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2107  * acceleration.
2108  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2109  */
2110 static boolean_t
2111 ill_enable_promisc_notify(ill_t *ill)
2112 {
2113 	mblk_t *mp;
2114 	dl_notify_req_t *req;
2115 
2116 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2117 
2118 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2119 	if (mp == NULL)
2120 		return (B_FALSE);
2121 
2122 	req = (dl_notify_req_t *)mp->b_rptr;
2123 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2124 	    DL_NOTE_PROMISC_OFF_PHYS;
2125 
2126 	ill_dlpi_send(ill, mp);
2127 
2128 	return (B_TRUE);
2129 }
2130 
2131 /*
2132  * Allocate an IPsec capability request which will be filled by our
2133  * caller to turn on support for one or more algorithms.
2134  */
2135 static mblk_t *
2136 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2137 {
2138 	mblk_t *nmp;
2139 	dl_capability_req_t	*ocap;
2140 	dl_capab_ipsec_t	*ocip;
2141 	dl_capab_ipsec_t	*icip;
2142 	uint8_t			*ptr;
2143 	icip = (dl_capab_ipsec_t *)(isub + 1);
2144 
2145 	/*
2146 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2147 	 * PROMISC_ON/OFF notification from the provider. We need to
2148 	 * do this before enabling the algorithms to avoid leakage of
2149 	 * cleartext packets.
2150 	 */
2151 
2152 	if (!ill_enable_promisc_notify(ill))
2153 		return (NULL);
2154 
2155 	/*
2156 	 * Allocate new mblk which will contain a new capability
2157 	 * request to enable the capabilities.
2158 	 */
2159 
2160 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2161 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2162 	if (nmp == NULL)
2163 		return (NULL);
2164 
2165 	ptr = nmp->b_rptr;
2166 
2167 	/* initialize dl_capability_req_t */
2168 	ocap = (dl_capability_req_t *)ptr;
2169 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2170 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2171 	ptr += sizeof (dl_capability_req_t);
2172 
2173 	/* initialize dl_capability_sub_t */
2174 	bcopy(isub, ptr, sizeof (*isub));
2175 	ptr += sizeof (*isub);
2176 
2177 	/* initialize dl_capab_ipsec_t */
2178 	ocip = (dl_capab_ipsec_t *)ptr;
2179 	bcopy(icip, ocip, sizeof (*icip));
2180 
2181 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2182 	return (nmp);
2183 }
2184 
2185 /*
2186  * Process an IPsec capability negotiation ack received from a DLS Provider.
2187  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2188  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2189  */
2190 static void
2191 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2192 {
2193 	dl_capab_ipsec_t	*icip;
2194 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2195 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2196 	uint_t cipher, nciphers;
2197 	mblk_t *nmp;
2198 	uint_t alg_len;
2199 	boolean_t need_sadb_dump;
2200 	uint_t sub_dl_cap = isub->dl_cap;
2201 	ill_ipsec_capab_t **ill_capab;
2202 	uint64_t ill_capab_flag;
2203 	uint8_t *capend, *ciphend;
2204 	boolean_t sadb_resync;
2205 
2206 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2207 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2208 
2209 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2210 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2211 		ill_capab_flag = ILL_CAPAB_AH;
2212 	} else {
2213 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2214 		ill_capab_flag = ILL_CAPAB_ESP;
2215 	}
2216 
2217 	/*
2218 	 * If the ill capability structure exists, then this incoming
2219 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2220 	 * If this is so, then we'd need to resynchronize the SADB
2221 	 * after re-enabling the offloaded ciphers.
2222 	 */
2223 	sadb_resync = (*ill_capab != NULL);
2224 
2225 	/*
2226 	 * Note: range checks here are not absolutely sufficient to
2227 	 * make us robust against malformed messages sent by drivers;
2228 	 * this is in keeping with the rest of IP's dlpi handling.
2229 	 * (Remember, it's coming from something else in the kernel
2230 	 * address space)
2231 	 */
2232 
2233 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2234 	if (capend > mp->b_wptr) {
2235 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2236 		    "malformed sub-capability too long for mblk");
2237 		return;
2238 	}
2239 
2240 	/*
2241 	 * There are two types of acks we process here:
2242 	 * 1. acks in reply to a (first form) generic capability req
2243 	 *    (no ENABLE flag set)
2244 	 * 2. acks in reply to a ENABLE capability req.
2245 	 *    (ENABLE flag set)
2246 	 *
2247 	 * We process the subcapability passed as argument as follows:
2248 	 * 1 do initializations
2249 	 *   1.1 initialize nmp = NULL
2250 	 *   1.2 set need_sadb_dump to B_FALSE
2251 	 * 2 for each cipher in subcapability:
2252 	 *   2.1 if ENABLE flag is set:
2253 	 *	2.1.1 update per-ill ipsec capabilities info
2254 	 *	2.1.2 set need_sadb_dump to B_TRUE
2255 	 *   2.2 if ENABLE flag is not set:
2256 	 *	2.2.1 if nmp is NULL:
2257 	 *		2.2.1.1 allocate and initialize nmp
2258 	 *		2.2.1.2 init current pos in nmp
2259 	 *	2.2.2 copy current cipher to current pos in nmp
2260 	 *	2.2.3 set ENABLE flag in nmp
2261 	 *	2.2.4 update current pos
2262 	 * 3 if nmp is not equal to NULL, send enable request
2263 	 *   3.1 send capability request
2264 	 * 4 if need_sadb_dump is B_TRUE
2265 	 *   4.1 enable promiscuous on/off notifications
2266 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2267 	 *	AH or ESP SA's to interface.
2268 	 */
2269 
2270 	nmp = NULL;
2271 	oalg = NULL;
2272 	need_sadb_dump = B_FALSE;
2273 	icip = (dl_capab_ipsec_t *)(isub + 1);
2274 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2275 
2276 	nciphers = icip->cip_nciphers;
2277 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2278 
2279 	if (ciphend > capend) {
2280 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2281 		    "too many ciphers for sub-capability len");
2282 		return;
2283 	}
2284 
2285 	for (cipher = 0; cipher < nciphers; cipher++) {
2286 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2287 
2288 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2289 			/*
2290 			 * TBD: when we provide a way to disable capabilities
2291 			 * from above, need to manage the request-pending state
2292 			 * and fail if we were not expecting this ACK.
2293 			 */
2294 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2295 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2296 
2297 			/*
2298 			 * Update IPsec capabilities for this ill
2299 			 */
2300 
2301 			if (*ill_capab == NULL) {
2302 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2303 				    ("ill_capability_ipsec_ack: "
2304 				    "allocating ipsec_capab for ill\n"));
2305 				*ill_capab = ill_ipsec_capab_alloc();
2306 
2307 				if (*ill_capab == NULL) {
2308 					cmn_err(CE_WARN,
2309 					    "ill_capability_ipsec_ack: "
2310 					    "could not enable IPsec Hardware "
2311 					    "acceleration for %s (ENOMEM)\n",
2312 					    ill->ill_name);
2313 					return;
2314 				}
2315 			}
2316 
2317 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2318 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2319 
2320 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2321 				cmn_err(CE_WARN,
2322 				    "ill_capability_ipsec_ack: "
2323 				    "malformed IPsec algorithm id %d",
2324 				    ialg->alg_prim);
2325 				continue;
2326 			}
2327 
2328 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2329 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2330 				    ialg->alg_prim);
2331 			} else {
2332 				ipsec_capab_algparm_t *alp;
2333 
2334 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2335 				    ialg->alg_prim);
2336 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2337 				    ialg->alg_prim)) {
2338 					cmn_err(CE_WARN,
2339 					    "ill_capability_ipsec_ack: "
2340 					    "no space for IPsec alg id %d",
2341 					    ialg->alg_prim);
2342 					continue;
2343 				}
2344 				alp = &((*ill_capab)->encr_algparm[
2345 				    ialg->alg_prim]);
2346 				alp->minkeylen = ialg->alg_minbits;
2347 				alp->maxkeylen = ialg->alg_maxbits;
2348 			}
2349 			ill->ill_capabilities |= ill_capab_flag;
2350 			/*
2351 			 * indicate that a capability was enabled, which
2352 			 * will be used below to kick off a SADB dump
2353 			 * to the ill.
2354 			 */
2355 			need_sadb_dump = B_TRUE;
2356 		} else {
2357 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2358 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2359 			    ialg->alg_prim));
2360 
2361 			if (nmp == NULL) {
2362 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2363 				if (nmp == NULL) {
2364 					/*
2365 					 * Sending the PROMISC_ON/OFF
2366 					 * notification request failed.
2367 					 * We cannot enable the algorithms
2368 					 * since the Provider will not
2369 					 * notify IP of promiscous mode
2370 					 * changes, which could lead
2371 					 * to leakage of packets.
2372 					 */
2373 					cmn_err(CE_WARN,
2374 					    "ill_capability_ipsec_ack: "
2375 					    "could not enable IPsec Hardware "
2376 					    "acceleration for %s (ENOMEM)\n",
2377 					    ill->ill_name);
2378 					return;
2379 				}
2380 				/* ptr to current output alg specifier */
2381 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2382 			}
2383 
2384 			/*
2385 			 * Copy current alg specifier, set ENABLE
2386 			 * flag, and advance to next output alg.
2387 			 * For now we enable all IPsec capabilities.
2388 			 */
2389 			ASSERT(oalg != NULL);
2390 			bcopy(ialg, oalg, alg_len);
2391 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2392 			nmp->b_wptr += alg_len;
2393 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2394 		}
2395 
2396 		/* move to next input algorithm specifier */
2397 		ialg = (dl_capab_ipsec_alg_t *)
2398 		    ((char *)ialg + alg_len);
2399 	}
2400 
2401 	if (nmp != NULL)
2402 		/*
2403 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2404 		 * IPsec hardware acceleration.
2405 		 */
2406 		ill_capability_send(ill, nmp);
2407 
2408 	if (need_sadb_dump)
2409 		/*
2410 		 * An acknowledgement corresponding to a request to
2411 		 * enable acceleration was received, notify SADB.
2412 		 */
2413 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2414 }
2415 
2416 /*
2417  * Given an mblk with enough space in it, create sub-capability entries for
2418  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2419  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2420  * in preparation for the reset the DL_CAPABILITY_REQ message.
2421  */
2422 static void
2423 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2424     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2425 {
2426 	dl_capab_ipsec_t *oipsec;
2427 	dl_capab_ipsec_alg_t *oalg;
2428 	dl_capability_sub_t *dl_subcap;
2429 	int i, k;
2430 
2431 	ASSERT(nciphers > 0);
2432 	ASSERT(ill_cap != NULL);
2433 	ASSERT(mp != NULL);
2434 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2435 
2436 	/* dl_capability_sub_t for "stype" */
2437 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2438 	dl_subcap->dl_cap = stype;
2439 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2440 	mp->b_wptr += sizeof (dl_capability_sub_t);
2441 
2442 	/* dl_capab_ipsec_t for "stype" */
2443 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2444 	oipsec->cip_version = 1;
2445 	oipsec->cip_nciphers = nciphers;
2446 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2447 
2448 	/* create entries for "stype" AUTH ciphers */
2449 	for (i = 0; i < ill_cap->algs_size; i++) {
2450 		for (k = 0; k < BITSPERBYTE; k++) {
2451 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2452 				continue;
2453 
2454 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2455 			bzero((void *)oalg, sizeof (*oalg));
2456 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2457 			oalg->alg_prim = k + (BITSPERBYTE * i);
2458 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2459 		}
2460 	}
2461 	/* create entries for "stype" ENCR ciphers */
2462 	for (i = 0; i < ill_cap->algs_size; i++) {
2463 		for (k = 0; k < BITSPERBYTE; k++) {
2464 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2465 				continue;
2466 
2467 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2468 			bzero((void *)oalg, sizeof (*oalg));
2469 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2470 			oalg->alg_prim = k + (BITSPERBYTE * i);
2471 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2472 		}
2473 	}
2474 }
2475 
2476 /*
2477  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2478  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2479  * POPC instruction, but our macro is more flexible for an arbitrary length
2480  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2481  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2482  * stays that way, we can reduce the number of iterations required.
2483  */
2484 #define	COUNT_1S(val, sum) {					\
2485 	uint8_t x = val & 0xff;					\
2486 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2487 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2488 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2489 }
2490 
2491 /* ARGSUSED */
2492 static int
2493 ill_capability_ipsec_reset_size(ill_t *ill, int *ah_cntp, int *ah_lenp,
2494     int *esp_cntp, int *esp_lenp)
2495 {
2496 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2497 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2498 	uint64_t ill_capabilities = ill->ill_capabilities;
2499 	int ah_cnt = 0, esp_cnt = 0;
2500 	int ah_len = 0, esp_len = 0;
2501 	int i, size = 0;
2502 
2503 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2504 		return (0);
2505 
2506 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2507 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2508 
2509 	/* Find out the number of ciphers for AH */
2510 	if (cap_ah != NULL) {
2511 		for (i = 0; i < cap_ah->algs_size; i++) {
2512 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2513 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2514 		}
2515 		if (ah_cnt > 0) {
2516 			size += sizeof (dl_capability_sub_t) +
2517 			    sizeof (dl_capab_ipsec_t);
2518 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2519 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2520 			size += ah_len;
2521 		}
2522 	}
2523 
2524 	/* Find out the number of ciphers for ESP */
2525 	if (cap_esp != NULL) {
2526 		for (i = 0; i < cap_esp->algs_size; i++) {
2527 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2528 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2529 		}
2530 		if (esp_cnt > 0) {
2531 			size += sizeof (dl_capability_sub_t) +
2532 			    sizeof (dl_capab_ipsec_t);
2533 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2534 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2535 			size += esp_len;
2536 		}
2537 	}
2538 
2539 	if (ah_cntp != NULL)
2540 		*ah_cntp = ah_cnt;
2541 	if (ah_lenp != NULL)
2542 		*ah_lenp = ah_len;
2543 	if (esp_cntp != NULL)
2544 		*esp_cntp = esp_cnt;
2545 	if (esp_lenp != NULL)
2546 		*esp_lenp = esp_len;
2547 
2548 	return (size);
2549 }
2550 
2551 /* ARGSUSED */
2552 static void
2553 ill_capability_ipsec_reset_fill(ill_t *ill, mblk_t *mp)
2554 {
2555 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2556 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2557 	int ah_cnt = 0, esp_cnt = 0;
2558 	int ah_len = 0, esp_len = 0;
2559 	int size;
2560 
2561 	size = ill_capability_ipsec_reset_size(ill, &ah_cnt, &ah_len,
2562 	    &esp_cnt, &esp_len);
2563 	if (size == 0)
2564 		return;
2565 
2566 	/*
2567 	 * Clear the capability flags for IPsec HA but retain the ill
2568 	 * capability structures since it's possible that another thread
2569 	 * is still referring to them.  The structures only get deallocated
2570 	 * when we destroy the ill.
2571 	 *
2572 	 * Various places check the flags to see if the ill is capable of
2573 	 * hardware acceleration, and by clearing them we ensure that new
2574 	 * outbound IPsec packets are sent down encrypted.
2575 	 */
2576 
2577 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2578 	if (ah_cnt > 0) {
2579 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2580 		    cap_ah, mp);
2581 	}
2582 
2583 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2584 	if (esp_cnt > 0) {
2585 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2586 		    cap_esp, mp);
2587 	}
2588 
2589 	/*
2590 	 * At this point we've composed a bunch of sub-capabilities to be
2591 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2592 	 * by the caller.  Upon receiving this reset message, the driver
2593 	 * must stop inbound decryption (by destroying all inbound SAs)
2594 	 * and let the corresponding packets come in encrypted.
2595 	 */
2596 }
2597 
2598 static void
2599 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2600     boolean_t encapsulated)
2601 {
2602 	boolean_t legacy = B_FALSE;
2603 
2604 	/*
2605 	 * Note that only the following two sub-capabilities may be
2606 	 * considered as "legacy", since their original definitions
2607 	 * do not incorporate the dl_mid_t module ID token, and hence
2608 	 * may require the use of the wrapper sub-capability.
2609 	 */
2610 	switch (subp->dl_cap) {
2611 	case DL_CAPAB_IPSEC_AH:
2612 	case DL_CAPAB_IPSEC_ESP:
2613 		legacy = B_TRUE;
2614 		break;
2615 	}
2616 
2617 	/*
2618 	 * For legacy sub-capabilities which don't incorporate a queue_t
2619 	 * pointer in their structures, discard them if we detect that
2620 	 * there are intermediate modules in between IP and the driver.
2621 	 */
2622 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2623 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2624 		    "%d discarded; %d module(s) present below IP\n",
2625 		    subp->dl_cap, ill->ill_lmod_cnt));
2626 		return;
2627 	}
2628 
2629 	switch (subp->dl_cap) {
2630 	case DL_CAPAB_IPSEC_AH:
2631 	case DL_CAPAB_IPSEC_ESP:
2632 		ill_capability_ipsec_ack(ill, mp, subp);
2633 		break;
2634 	case DL_CAPAB_MDT:
2635 		ill_capability_mdt_ack(ill, mp, subp);
2636 		break;
2637 	case DL_CAPAB_HCKSUM:
2638 		ill_capability_hcksum_ack(ill, mp, subp);
2639 		break;
2640 	case DL_CAPAB_ZEROCOPY:
2641 		ill_capability_zerocopy_ack(ill, mp, subp);
2642 		break;
2643 	case DL_CAPAB_DLD:
2644 		ill_capability_dld_ack(ill, mp, subp);
2645 		break;
2646 	default:
2647 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2648 		    subp->dl_cap));
2649 	}
2650 }
2651 
2652 /*
2653  * Process a hardware checksum offload capability negotiation ack received
2654  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
2655  * of a DL_CAPABILITY_ACK message.
2656  */
2657 static void
2658 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2659 {
2660 	dl_capability_req_t	*ocap;
2661 	dl_capab_hcksum_t	*ihck, *ohck;
2662 	ill_hcksum_capab_t	**ill_hcksum;
2663 	mblk_t			*nmp = NULL;
2664 	uint_t			sub_dl_cap = isub->dl_cap;
2665 	uint8_t			*capend;
2666 
2667 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
2668 
2669 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
2670 
2671 	/*
2672 	 * Note: range checks here are not absolutely sufficient to
2673 	 * make us robust against malformed messages sent by drivers;
2674 	 * this is in keeping with the rest of IP's dlpi handling.
2675 	 * (Remember, it's coming from something else in the kernel
2676 	 * address space)
2677 	 */
2678 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2679 	if (capend > mp->b_wptr) {
2680 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
2681 		    "malformed sub-capability too long for mblk");
2682 		return;
2683 	}
2684 
2685 	/*
2686 	 * There are two types of acks we process here:
2687 	 * 1. acks in reply to a (first form) generic capability req
2688 	 *    (no ENABLE flag set)
2689 	 * 2. acks in reply to a ENABLE capability req.
2690 	 *    (ENABLE flag set)
2691 	 */
2692 	ihck = (dl_capab_hcksum_t *)(isub + 1);
2693 
2694 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
2695 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
2696 		    "unsupported hardware checksum "
2697 		    "sub-capability (version %d, expected %d)",
2698 		    ihck->hcksum_version, HCKSUM_VERSION_1);
2699 		return;
2700 	}
2701 
2702 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
2703 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
2704 		    "checksum capability isn't as expected; pass-thru "
2705 		    "module(s) detected, discarding capability\n"));
2706 		return;
2707 	}
2708 
2709 #define	CURR_HCKSUM_CAPAB				\
2710 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
2711 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
2712 
2713 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
2714 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
2715 		/* do ENABLE processing */
2716 		if (*ill_hcksum == NULL) {
2717 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
2718 			    KM_NOSLEEP);
2719 
2720 			if (*ill_hcksum == NULL) {
2721 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
2722 				    "could not enable hcksum version %d "
2723 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
2724 				    ill->ill_name);
2725 				return;
2726 			}
2727 		}
2728 
2729 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
2730 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
2731 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
2732 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
2733 		    "has enabled hardware checksumming\n ",
2734 		    ill->ill_name));
2735 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
2736 		/*
2737 		 * Enabling hardware checksum offload
2738 		 * Currently IP supports {TCP,UDP}/IPv4
2739 		 * partial and full cksum offload and
2740 		 * IPv4 header checksum offload.
2741 		 * Allocate new mblk which will
2742 		 * contain a new capability request
2743 		 * to enable hardware checksum offload.
2744 		 */
2745 		uint_t	size;
2746 		uchar_t	*rptr;
2747 
2748 		size = sizeof (dl_capability_req_t) +
2749 		    sizeof (dl_capability_sub_t) + isub->dl_length;
2750 
2751 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2752 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
2753 			    "could not enable hardware cksum for %s (ENOMEM)\n",
2754 			    ill->ill_name);
2755 			return;
2756 		}
2757 
2758 		rptr = nmp->b_rptr;
2759 		/* initialize dl_capability_req_t */
2760 		ocap = (dl_capability_req_t *)nmp->b_rptr;
2761 		ocap->dl_sub_offset =
2762 		    sizeof (dl_capability_req_t);
2763 		ocap->dl_sub_length =
2764 		    sizeof (dl_capability_sub_t) +
2765 		    isub->dl_length;
2766 		nmp->b_rptr += sizeof (dl_capability_req_t);
2767 
2768 		/* initialize dl_capability_sub_t */
2769 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2770 		nmp->b_rptr += sizeof (*isub);
2771 
2772 		/* initialize dl_capab_hcksum_t */
2773 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
2774 		bcopy(ihck, ohck, sizeof (*ihck));
2775 
2776 		nmp->b_rptr = rptr;
2777 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2778 
2779 		/* Set ENABLE flag */
2780 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
2781 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
2782 
2783 		/*
2784 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2785 		 * hardware checksum acceleration.
2786 		 */
2787 		ill_capability_send(ill, nmp);
2788 	} else {
2789 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
2790 		    "advertised %x hardware checksum capability flags\n",
2791 		    ill->ill_name, ihck->hcksum_txflags));
2792 	}
2793 }
2794 
2795 static void
2796 ill_capability_hcksum_reset_fill(ill_t *ill, mblk_t *mp)
2797 {
2798 	dl_capab_hcksum_t *hck_subcap;
2799 	dl_capability_sub_t *dl_subcap;
2800 
2801 	if (!ILL_HCKSUM_CAPABLE(ill))
2802 		return;
2803 
2804 	ASSERT(ill->ill_hcksum_capab != NULL);
2805 
2806 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2807 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
2808 	dl_subcap->dl_length = sizeof (*hck_subcap);
2809 
2810 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
2811 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
2812 	hck_subcap->hcksum_txflags = 0;
2813 
2814 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*hck_subcap);
2815 }
2816 
2817 static void
2818 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2819 {
2820 	mblk_t *nmp = NULL;
2821 	dl_capability_req_t *oc;
2822 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
2823 	ill_zerocopy_capab_t **ill_zerocopy_capab;
2824 	uint_t sub_dl_cap = isub->dl_cap;
2825 	uint8_t *capend;
2826 
2827 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
2828 
2829 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
2830 
2831 	/*
2832 	 * Note: range checks here are not absolutely sufficient to
2833 	 * make us robust against malformed messages sent by drivers;
2834 	 * this is in keeping with the rest of IP's dlpi handling.
2835 	 * (Remember, it's coming from something else in the kernel
2836 	 * address space)
2837 	 */
2838 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2839 	if (capend > mp->b_wptr) {
2840 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
2841 		    "malformed sub-capability too long for mblk");
2842 		return;
2843 	}
2844 
2845 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
2846 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
2847 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
2848 		    "unsupported ZEROCOPY sub-capability (version %d, "
2849 		    "expected %d)", zc_ic->zerocopy_version,
2850 		    ZEROCOPY_VERSION_1);
2851 		return;
2852 	}
2853 
2854 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
2855 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
2856 		    "capability isn't as expected; pass-thru module(s) "
2857 		    "detected, discarding capability\n"));
2858 		return;
2859 	}
2860 
2861 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
2862 		if (*ill_zerocopy_capab == NULL) {
2863 			*ill_zerocopy_capab =
2864 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
2865 			    KM_NOSLEEP);
2866 
2867 			if (*ill_zerocopy_capab == NULL) {
2868 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
2869 				    "could not enable Zero-copy version %d "
2870 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
2871 				    ill->ill_name);
2872 				return;
2873 			}
2874 		}
2875 
2876 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
2877 		    "supports Zero-copy version %d\n", ill->ill_name,
2878 		    ZEROCOPY_VERSION_1));
2879 
2880 		(*ill_zerocopy_capab)->ill_zerocopy_version =
2881 		    zc_ic->zerocopy_version;
2882 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
2883 		    zc_ic->zerocopy_flags;
2884 
2885 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
2886 	} else {
2887 		uint_t size;
2888 		uchar_t *rptr;
2889 
2890 		size = sizeof (dl_capability_req_t) +
2891 		    sizeof (dl_capability_sub_t) +
2892 		    sizeof (dl_capab_zerocopy_t);
2893 
2894 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2895 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
2896 			    "could not enable zerocopy for %s (ENOMEM)\n",
2897 			    ill->ill_name);
2898 			return;
2899 		}
2900 
2901 		rptr = nmp->b_rptr;
2902 		/* initialize dl_capability_req_t */
2903 		oc = (dl_capability_req_t *)rptr;
2904 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
2905 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
2906 		    sizeof (dl_capab_zerocopy_t);
2907 		rptr += sizeof (dl_capability_req_t);
2908 
2909 		/* initialize dl_capability_sub_t */
2910 		bcopy(isub, rptr, sizeof (*isub));
2911 		rptr += sizeof (*isub);
2912 
2913 		/* initialize dl_capab_zerocopy_t */
2914 		zc_oc = (dl_capab_zerocopy_t *)rptr;
2915 		*zc_oc = *zc_ic;
2916 
2917 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
2918 		    "to enable zero-copy version %d\n", ill->ill_name,
2919 		    ZEROCOPY_VERSION_1));
2920 
2921 		/* set VMSAFE_MEM flag */
2922 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
2923 
2924 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
2925 		ill_capability_send(ill, nmp);
2926 	}
2927 }
2928 
2929 static void
2930 ill_capability_zerocopy_reset_fill(ill_t *ill, mblk_t *mp)
2931 {
2932 	dl_capab_zerocopy_t *zerocopy_subcap;
2933 	dl_capability_sub_t *dl_subcap;
2934 
2935 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
2936 		return;
2937 
2938 	ASSERT(ill->ill_zerocopy_capab != NULL);
2939 
2940 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2941 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
2942 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
2943 
2944 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
2945 	zerocopy_subcap->zerocopy_version =
2946 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
2947 	zerocopy_subcap->zerocopy_flags = 0;
2948 
2949 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
2950 }
2951 
2952 /*
2953  * DLD capability
2954  * Refer to dld.h for more information regarding the purpose and usage
2955  * of this capability.
2956  */
2957 static void
2958 ill_capability_dld_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2959 {
2960 	dl_capab_dld_t		*dld_ic, dld;
2961 	uint_t			sub_dl_cap = isub->dl_cap;
2962 	uint8_t			*capend;
2963 	ill_dld_capab_t		*idc;
2964 
2965 	ASSERT(IAM_WRITER_ILL(ill));
2966 	ASSERT(sub_dl_cap == DL_CAPAB_DLD);
2967 
2968 	/*
2969 	 * Note: range checks here are not absolutely sufficient to
2970 	 * make us robust against malformed messages sent by drivers;
2971 	 * this is in keeping with the rest of IP's dlpi handling.
2972 	 * (Remember, it's coming from something else in the kernel
2973 	 * address space)
2974 	 */
2975 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2976 	if (capend > mp->b_wptr) {
2977 		cmn_err(CE_WARN, "ill_capability_dld_ack: "
2978 		    "malformed sub-capability too long for mblk");
2979 		return;
2980 	}
2981 	dld_ic = (dl_capab_dld_t *)(isub + 1);
2982 	if (dld_ic->dld_version != DLD_CURRENT_VERSION) {
2983 		cmn_err(CE_CONT, "ill_capability_dld_ack: "
2984 		    "unsupported DLD sub-capability (version %d, "
2985 		    "expected %d)", dld_ic->dld_version,
2986 		    DLD_CURRENT_VERSION);
2987 		return;
2988 	}
2989 	if (!dlcapabcheckqid(&dld_ic->dld_mid, ill->ill_lmod_rq)) {
2990 		ip1dbg(("ill_capability_dld_ack: mid token for dld "
2991 		    "capability isn't as expected; pass-thru module(s) "
2992 		    "detected, discarding capability\n"));
2993 		return;
2994 	}
2995 
2996 	/*
2997 	 * Copy locally to ensure alignment.
2998 	 */
2999 	bcopy(dld_ic, &dld, sizeof (dl_capab_dld_t));
3000 
3001 	if ((idc = ill->ill_dld_capab) == NULL) {
3002 		idc = kmem_zalloc(sizeof (ill_dld_capab_t), KM_NOSLEEP);
3003 		if (idc == NULL) {
3004 			cmn_err(CE_WARN, "ill_capability_dld_ack: "
3005 			    "could not enable DLD version %d "
3006 			    "for %s (ENOMEM)\n", DLD_CURRENT_VERSION,
3007 			    ill->ill_name);
3008 			return;
3009 		}
3010 		idc->idc_capab_df = (ip_capab_func_t)dld.dld_capab;
3011 		idc->idc_capab_dh = (void *)dld.dld_capab_handle;
3012 		ill->ill_dld_capab = idc;
3013 	}
3014 	ip1dbg(("ill_capability_dld_ack: interface %s "
3015 	    "supports DLD version %d\n", ill->ill_name, DLD_CURRENT_VERSION));
3016 
3017 	ill_capability_dld_enable(ill);
3018 }
3019 
3020 /*
3021  * Typically capability negotiation between IP and the driver happens via
3022  * DLPI message exchange. However GLD also offers a direct function call
3023  * mechanism to exchange the DLD_DIRECT_CAPAB and DLD_POLL_CAPAB capabilities,
3024  * But arbitrary function calls into IP or GLD are not permitted, since both
3025  * of them are protected by their own perimeter mechanism. The perimeter can
3026  * be viewed as a coarse lock or serialization mechanism. The hierarchy of
3027  * these perimeters is IP -> MAC. Thus for example to enable the squeue
3028  * polling, IP needs to enter its perimeter, then call ill_mac_perim_enter
3029  * to enter the mac perimeter and then do the direct function calls into
3030  * GLD to enable squeue polling. The ring related callbacks from the mac into
3031  * the stack to add, bind, quiesce, restart or cleanup a ring are all
3032  * protected by the mac perimeter.
3033  */
3034 static void
3035 ill_mac_perim_enter(ill_t *ill, mac_perim_handle_t *mphp)
3036 {
3037 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
3038 	int			err;
3039 
3040 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mphp,
3041 	    DLD_ENABLE);
3042 	ASSERT(err == 0);
3043 }
3044 
3045 static void
3046 ill_mac_perim_exit(ill_t *ill, mac_perim_handle_t mph)
3047 {
3048 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
3049 	int			err;
3050 
3051 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mph,
3052 	    DLD_DISABLE);
3053 	ASSERT(err == 0);
3054 }
3055 
3056 boolean_t
3057 ill_mac_perim_held(ill_t *ill)
3058 {
3059 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
3060 
3061 	return (idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, NULL,
3062 	    DLD_QUERY));
3063 }
3064 
3065 static void
3066 ill_capability_direct_enable(ill_t *ill)
3067 {
3068 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
3069 	ill_dld_direct_t	*idd = &idc->idc_direct;
3070 	dld_capab_direct_t	direct;
3071 	int			rc;
3072 
3073 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
3074 
3075 	bzero(&direct, sizeof (direct));
3076 	direct.di_rx_cf = (uintptr_t)ip_input;
3077 	direct.di_rx_ch = ill;
3078 
3079 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT, &direct,
3080 	    DLD_ENABLE);
3081 	if (rc == 0) {
3082 		idd->idd_tx_df = (ip_dld_tx_t)direct.di_tx_df;
3083 		idd->idd_tx_dh = direct.di_tx_dh;
3084 		idd->idd_tx_cb_df = (ip_dld_callb_t)direct.di_tx_cb_df;
3085 		idd->idd_tx_cb_dh = direct.di_tx_cb_dh;
3086 		idd->idd_tx_fctl_df = (ip_dld_fctl_t)direct.di_tx_fctl_df;
3087 		idd->idd_tx_fctl_dh = direct.di_tx_fctl_dh;
3088 		/*
3089 		 * One time registration of flow enable callback function
3090 		 */
3091 		ill->ill_flownotify_mh = idd->idd_tx_cb_df(idd->idd_tx_cb_dh,
3092 		    ill_flow_enable, ill);
3093 		ill->ill_capabilities |= ILL_CAPAB_DLD_DIRECT;
3094 		DTRACE_PROBE1(direct_on, (ill_t *), ill);
3095 	} else {
3096 		cmn_err(CE_WARN, "warning: could not enable DIRECT "
3097 		    "capability, rc = %d\n", rc);
3098 		DTRACE_PROBE2(direct_off, (ill_t *), ill, (int), rc);
3099 	}
3100 }
3101 
3102 static void
3103 ill_capability_poll_enable(ill_t *ill)
3104 {
3105 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
3106 	dld_capab_poll_t	poll;
3107 	int			rc;
3108 
3109 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
3110 
3111 	bzero(&poll, sizeof (poll));
3112 	poll.poll_ring_add_cf = (uintptr_t)ip_squeue_add_ring;
3113 	poll.poll_ring_remove_cf = (uintptr_t)ip_squeue_clean_ring;
3114 	poll.poll_ring_quiesce_cf = (uintptr_t)ip_squeue_quiesce_ring;
3115 	poll.poll_ring_restart_cf = (uintptr_t)ip_squeue_restart_ring;
3116 	poll.poll_ring_bind_cf = (uintptr_t)ip_squeue_bind_ring;
3117 	poll.poll_ring_ch = ill;
3118 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL, &poll,
3119 	    DLD_ENABLE);
3120 	if (rc == 0) {
3121 		ill->ill_capabilities |= ILL_CAPAB_DLD_POLL;
3122 		DTRACE_PROBE1(poll_on, (ill_t *), ill);
3123 	} else {
3124 		ip1dbg(("warning: could not enable POLL "
3125 		    "capability, rc = %d\n", rc));
3126 		DTRACE_PROBE2(poll_off, (ill_t *), ill, (int), rc);
3127 	}
3128 }
3129 
3130 /*
3131  * Enable the LSO capability.
3132  */
3133 static void
3134 ill_capability_lso_enable(ill_t *ill)
3135 {
3136 	ill_dld_capab_t	*idc = ill->ill_dld_capab;
3137 	dld_capab_lso_t	lso;
3138 	int rc;
3139 
3140 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
3141 
3142 	if (ill->ill_lso_capab == NULL) {
3143 		ill->ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3144 		    KM_NOSLEEP);
3145 		if (ill->ill_lso_capab == NULL) {
3146 			cmn_err(CE_WARN, "ill_capability_lso_enable: "
3147 			    "could not enable LSO for %s (ENOMEM)\n",
3148 			    ill->ill_name);
3149 			return;
3150 		}
3151 	}
3152 
3153 	bzero(&lso, sizeof (lso));
3154 	if ((rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO, &lso,
3155 	    DLD_ENABLE)) == 0) {
3156 		ill->ill_lso_capab->ill_lso_flags = lso.lso_flags;
3157 		ill->ill_lso_capab->ill_lso_max = lso.lso_max;
3158 		ill->ill_capabilities |= ILL_CAPAB_DLD_LSO;
3159 		ip1dbg(("ill_capability_lso_enable: interface %s "
3160 		    "has enabled LSO\n ", ill->ill_name));
3161 	} else {
3162 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
3163 		ill->ill_lso_capab = NULL;
3164 		DTRACE_PROBE2(lso_off, (ill_t *), ill, (int), rc);
3165 	}
3166 }
3167 
3168 static void
3169 ill_capability_dld_enable(ill_t *ill)
3170 {
3171 	mac_perim_handle_t mph;
3172 
3173 	ASSERT(IAM_WRITER_ILL(ill));
3174 
3175 	if (ill->ill_isv6)
3176 		return;
3177 
3178 	ill_mac_perim_enter(ill, &mph);
3179 	if (!ill->ill_isv6) {
3180 		ill_capability_direct_enable(ill);
3181 		ill_capability_poll_enable(ill);
3182 		ill_capability_lso_enable(ill);
3183 	}
3184 	ill->ill_capabilities |= ILL_CAPAB_DLD;
3185 	ill_mac_perim_exit(ill, mph);
3186 }
3187 
3188 static void
3189 ill_capability_dld_disable(ill_t *ill)
3190 {
3191 	ill_dld_capab_t	*idc;
3192 	ill_dld_direct_t *idd;
3193 	mac_perim_handle_t	mph;
3194 
3195 	ASSERT(IAM_WRITER_ILL(ill));
3196 
3197 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
3198 		return;
3199 
3200 	ill_mac_perim_enter(ill, &mph);
3201 
3202 	idc = ill->ill_dld_capab;
3203 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT) != 0) {
3204 		/*
3205 		 * For performance we avoid locks in the transmit data path
3206 		 * and don't maintain a count of the number of threads using
3207 		 * direct calls. Thus some threads could be using direct
3208 		 * transmit calls to GLD, even after the capability mechanism
3209 		 * turns it off. This is still safe since the handles used in
3210 		 * the direct calls continue to be valid until the unplumb is
3211 		 * completed. Remove the callback that was added (1-time) at
3212 		 * capab enable time.
3213 		 */
3214 		mutex_enter(&ill->ill_lock);
3215 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_DIRECT;
3216 		mutex_exit(&ill->ill_lock);
3217 		if (ill->ill_flownotify_mh != NULL) {
3218 			idd = &idc->idc_direct;
3219 			idd->idd_tx_cb_df(idd->idd_tx_cb_dh, NULL,
3220 			    ill->ill_flownotify_mh);
3221 			ill->ill_flownotify_mh = NULL;
3222 		}
3223 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT,
3224 		    NULL, DLD_DISABLE);
3225 	}
3226 
3227 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_POLL) != 0) {
3228 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_POLL;
3229 		ip_squeue_clean_all(ill);
3230 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL,
3231 		    NULL, DLD_DISABLE);
3232 	}
3233 
3234 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_LSO) != 0) {
3235 		ASSERT(ill->ill_lso_capab != NULL);
3236 		/*
3237 		 * Clear the capability flag for LSO but retain the
3238 		 * ill_lso_capab structure since it's possible that another
3239 		 * thread is still referring to it.  The structure only gets
3240 		 * deallocated when we destroy the ill.
3241 		 */
3242 
3243 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_LSO;
3244 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO,
3245 		    NULL, DLD_DISABLE);
3246 	}
3247 
3248 	ill->ill_capabilities &= ~ILL_CAPAB_DLD;
3249 	ill_mac_perim_exit(ill, mph);
3250 }
3251 
3252 /*
3253  * Capability Negotiation protocol
3254  *
3255  * We don't wait for DLPI capability operations to finish during interface
3256  * bringup or teardown. Doing so would introduce more asynchrony and the
3257  * interface up/down operations will need multiple return and restarts.
3258  * Instead the 'ipsq_current_ipif' of the ipsq is not cleared as long as
3259  * the 'ill_dlpi_deferred' chain is non-empty. This ensures that the next
3260  * exclusive operation won't start until the DLPI operations of the previous
3261  * exclusive operation complete.
3262  *
3263  * The capability state machine is shown below.
3264  *
3265  * state		next state		event, action
3266  *
3267  * IDCS_UNKNOWN 	IDCS_PROBE_SENT		ill_capability_probe
3268  * IDCS_PROBE_SENT	IDCS_OK			ill_capability_ack
3269  * IDCS_PROBE_SENT	IDCS_FAILED		ip_rput_dlpi_writer (nack)
3270  * IDCS_OK		IDCS_RENEG		Receipt of DL_NOTE_CAPAB_RENEG
3271  * IDCS_OK		IDCS_RESET_SENT		ill_capability_reset
3272  * IDCS_RESET_SENT	IDCS_UNKNOWN		ill_capability_ack_thr
3273  * IDCS_RENEG		IDCS_PROBE_SENT		ill_capability_ack_thr ->
3274  *						    ill_capability_probe.
3275  */
3276 
3277 /*
3278  * Dedicated thread started from ip_stack_init that handles capability
3279  * disable. This thread ensures the taskq dispatch does not fail by waiting
3280  * for resources using TQ_SLEEP. The taskq mechanism is used to ensure
3281  * that direct calls to DLD are done in a cv_waitable context.
3282  */
3283 void
3284 ill_taskq_dispatch(ip_stack_t *ipst)
3285 {
3286 	callb_cpr_t cprinfo;
3287 	char 	name[64];
3288 	mblk_t	*mp;
3289 
3290 	(void) snprintf(name, sizeof (name), "ill_taskq_dispatch_%d",
3291 	    ipst->ips_netstack->netstack_stackid);
3292 	CALLB_CPR_INIT(&cprinfo, &ipst->ips_capab_taskq_lock, callb_generic_cpr,
3293 	    name);
3294 	mutex_enter(&ipst->ips_capab_taskq_lock);
3295 
3296 	for (;;) {
3297 		mp = list_head(&ipst->ips_capab_taskq_list);
3298 		while (mp != NULL) {
3299 			list_remove(&ipst->ips_capab_taskq_list, mp);
3300 			mutex_exit(&ipst->ips_capab_taskq_lock);
3301 			VERIFY(taskq_dispatch(system_taskq,
3302 			    ill_capability_ack_thr, mp, TQ_SLEEP) != 0);
3303 			mutex_enter(&ipst->ips_capab_taskq_lock);
3304 			mp = list_head(&ipst->ips_capab_taskq_list);
3305 		}
3306 
3307 		if (ipst->ips_capab_taskq_quit)
3308 			break;
3309 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
3310 		cv_wait(&ipst->ips_capab_taskq_cv, &ipst->ips_capab_taskq_lock);
3311 		CALLB_CPR_SAFE_END(&cprinfo, &ipst->ips_capab_taskq_lock);
3312 	}
3313 	VERIFY(list_head(&ipst->ips_capab_taskq_list) == NULL);
3314 	CALLB_CPR_EXIT(&cprinfo);
3315 	thread_exit();
3316 }
3317 
3318 /*
3319  * Consume a new-style hardware capabilities negotiation ack.
3320  * Called via taskq on receipt of DL_CAPABBILITY_ACK.
3321  */
3322 static void
3323 ill_capability_ack_thr(void *arg)
3324 {
3325 	mblk_t	*mp = arg;
3326 	dl_capability_ack_t *capp;
3327 	dl_capability_sub_t *subp, *endp;
3328 	ill_t	*ill;
3329 	boolean_t reneg;
3330 
3331 	ill = (ill_t *)mp->b_prev;
3332 	VERIFY(ipsq_enter(ill, B_FALSE, CUR_OP) == B_TRUE);
3333 
3334 	if (ill->ill_dlpi_capab_state == IDCS_RESET_SENT ||
3335 	    ill->ill_dlpi_capab_state == IDCS_RENEG) {
3336 		/*
3337 		 * We have received the ack for our DL_CAPAB reset request.
3338 		 * There isnt' anything in the message that needs processing.
3339 		 * All message based capabilities have been disabled, now
3340 		 * do the function call based capability disable.
3341 		 */
3342 		reneg = ill->ill_dlpi_capab_state == IDCS_RENEG;
3343 		ill_capability_dld_disable(ill);
3344 		ill->ill_dlpi_capab_state = IDCS_UNKNOWN;
3345 		if (reneg)
3346 			ill_capability_probe(ill);
3347 		goto done;
3348 	}
3349 
3350 	if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
3351 		ill->ill_dlpi_capab_state = IDCS_OK;
3352 
3353 	capp = (dl_capability_ack_t *)mp->b_rptr;
3354 
3355 	if (capp->dl_sub_length == 0) {
3356 		/* no new-style capabilities */
3357 		goto done;
3358 	}
3359 
3360 	/* make sure the driver supplied correct dl_sub_length */
3361 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3362 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3363 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3364 		goto done;
3365 	}
3366 
3367 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3368 	/*
3369 	 * There are sub-capabilities. Process the ones we know about.
3370 	 * Loop until we don't have room for another sub-cap header..
3371 	 */
3372 	for (subp = SC(capp, capp->dl_sub_offset),
3373 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3374 	    subp <= endp;
3375 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3376 
3377 		switch (subp->dl_cap) {
3378 		case DL_CAPAB_ID_WRAPPER:
3379 			ill_capability_id_ack(ill, mp, subp);
3380 			break;
3381 		default:
3382 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3383 			break;
3384 		}
3385 	}
3386 #undef SC
3387 done:
3388 	inet_freemsg(mp);
3389 	ill_capability_done(ill);
3390 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
3391 }
3392 
3393 /*
3394  * This needs to be started in a taskq thread to provide a cv_waitable
3395  * context.
3396  */
3397 void
3398 ill_capability_ack(ill_t *ill, mblk_t *mp)
3399 {
3400 	ip_stack_t	*ipst = ill->ill_ipst;
3401 
3402 	mp->b_prev = (mblk_t *)ill;
3403 	if (taskq_dispatch(system_taskq, ill_capability_ack_thr, mp,
3404 	    TQ_NOSLEEP) != 0)
3405 		return;
3406 
3407 	/*
3408 	 * The taskq dispatch failed. Signal the ill_taskq_dispatch thread
3409 	 * which will do the dispatch using TQ_SLEEP to guarantee success.
3410 	 */
3411 	mutex_enter(&ipst->ips_capab_taskq_lock);
3412 	list_insert_tail(&ipst->ips_capab_taskq_list, mp);
3413 	cv_signal(&ipst->ips_capab_taskq_cv);
3414 	mutex_exit(&ipst->ips_capab_taskq_lock);
3415 }
3416 
3417 /*
3418  * This routine is called to scan the fragmentation reassembly table for
3419  * the specified ILL for any packets that are starting to smell.
3420  * dead_interval is the maximum time in seconds that will be tolerated.  It
3421  * will either be the value specified in ip_g_frag_timeout, or zero if the
3422  * ILL is shutting down and it is time to blow everything off.
3423  *
3424  * It returns the number of seconds (as a time_t) that the next frag timer
3425  * should be scheduled for, 0 meaning that the timer doesn't need to be
3426  * re-started.  Note that the method of calculating next_timeout isn't
3427  * entirely accurate since time will flow between the time we grab
3428  * current_time and the time we schedule the next timeout.  This isn't a
3429  * big problem since this is the timer for sending an ICMP reassembly time
3430  * exceeded messages, and it doesn't have to be exactly accurate.
3431  *
3432  * This function is
3433  * sometimes called as writer, although this is not required.
3434  */
3435 time_t
3436 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3437 {
3438 	ipfb_t	*ipfb;
3439 	ipfb_t	*endp;
3440 	ipf_t	*ipf;
3441 	ipf_t	*ipfnext;
3442 	mblk_t	*mp;
3443 	time_t	current_time = gethrestime_sec();
3444 	time_t	next_timeout = 0;
3445 	uint32_t	hdr_length;
3446 	mblk_t	*send_icmp_head;
3447 	mblk_t	*send_icmp_head_v6;
3448 	zoneid_t zoneid;
3449 	ip_stack_t *ipst = ill->ill_ipst;
3450 
3451 	ipfb = ill->ill_frag_hash_tbl;
3452 	if (ipfb == NULL)
3453 		return (B_FALSE);
3454 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3455 	/* Walk the frag hash table. */
3456 	for (; ipfb < endp; ipfb++) {
3457 		send_icmp_head = NULL;
3458 		send_icmp_head_v6 = NULL;
3459 		mutex_enter(&ipfb->ipfb_lock);
3460 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3461 			time_t frag_time = current_time - ipf->ipf_timestamp;
3462 			time_t frag_timeout;
3463 
3464 			if (frag_time < dead_interval) {
3465 				/*
3466 				 * There are some outstanding fragments
3467 				 * that will timeout later.  Make note of
3468 				 * the time so that we can reschedule the
3469 				 * next timeout appropriately.
3470 				 */
3471 				frag_timeout = dead_interval - frag_time;
3472 				if (next_timeout == 0 ||
3473 				    frag_timeout < next_timeout) {
3474 					next_timeout = frag_timeout;
3475 				}
3476 				break;
3477 			}
3478 			/* Time's up.  Get it out of here. */
3479 			hdr_length = ipf->ipf_nf_hdr_len;
3480 			ipfnext = ipf->ipf_hash_next;
3481 			if (ipfnext)
3482 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3483 			*ipf->ipf_ptphn = ipfnext;
3484 			mp = ipf->ipf_mp->b_cont;
3485 			for (; mp; mp = mp->b_cont) {
3486 				/* Extra points for neatness. */
3487 				IP_REASS_SET_START(mp, 0);
3488 				IP_REASS_SET_END(mp, 0);
3489 			}
3490 			mp = ipf->ipf_mp->b_cont;
3491 			atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
3492 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3493 			ipfb->ipfb_count -= ipf->ipf_count;
3494 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3495 			ipfb->ipfb_frag_pkts--;
3496 			/*
3497 			 * We do not send any icmp message from here because
3498 			 * we currently are holding the ipfb_lock for this
3499 			 * hash chain. If we try and send any icmp messages
3500 			 * from here we may end up via a put back into ip
3501 			 * trying to get the same lock, causing a recursive
3502 			 * mutex panic. Instead we build a list and send all
3503 			 * the icmp messages after we have dropped the lock.
3504 			 */
3505 			if (ill->ill_isv6) {
3506 				if (hdr_length != 0) {
3507 					mp->b_next = send_icmp_head_v6;
3508 					send_icmp_head_v6 = mp;
3509 				} else {
3510 					freemsg(mp);
3511 				}
3512 			} else {
3513 				if (hdr_length != 0) {
3514 					mp->b_next = send_icmp_head;
3515 					send_icmp_head = mp;
3516 				} else {
3517 					freemsg(mp);
3518 				}
3519 			}
3520 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3521 			freeb(ipf->ipf_mp);
3522 		}
3523 		mutex_exit(&ipfb->ipfb_lock);
3524 		/*
3525 		 * Now need to send any icmp messages that we delayed from
3526 		 * above.
3527 		 */
3528 		while (send_icmp_head_v6 != NULL) {
3529 			ip6_t *ip6h;
3530 
3531 			mp = send_icmp_head_v6;
3532 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3533 			mp->b_next = NULL;
3534 			if (mp->b_datap->db_type == M_CTL)
3535 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3536 			else
3537 				ip6h = (ip6_t *)mp->b_rptr;
3538 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3539 			    ill, ipst);
3540 			if (zoneid == ALL_ZONES) {
3541 				freemsg(mp);
3542 			} else {
3543 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3544 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3545 				    B_FALSE, zoneid, ipst);
3546 			}
3547 		}
3548 		while (send_icmp_head != NULL) {
3549 			ipaddr_t dst;
3550 
3551 			mp = send_icmp_head;
3552 			send_icmp_head = send_icmp_head->b_next;
3553 			mp->b_next = NULL;
3554 
3555 			if (mp->b_datap->db_type == M_CTL)
3556 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3557 			else
3558 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3559 
3560 			zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst);
3561 			if (zoneid == ALL_ZONES) {
3562 				freemsg(mp);
3563 			} else {
3564 				icmp_time_exceeded(ill->ill_wq, mp,
3565 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid,
3566 				    ipst);
3567 			}
3568 		}
3569 	}
3570 	/*
3571 	 * A non-dying ILL will use the return value to decide whether to
3572 	 * restart the frag timer, and for how long.
3573 	 */
3574 	return (next_timeout);
3575 }
3576 
3577 /*
3578  * This routine is called when the approximate count of mblk memory used
3579  * for the specified ILL has exceeded max_count.
3580  */
3581 void
3582 ill_frag_prune(ill_t *ill, uint_t max_count)
3583 {
3584 	ipfb_t	*ipfb;
3585 	ipf_t	*ipf;
3586 	size_t	count;
3587 
3588 	/*
3589 	 * If we are here within ip_min_frag_prune_time msecs remove
3590 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3591 	 * ill_frag_free_num_pkts.
3592 	 */
3593 	mutex_enter(&ill->ill_lock);
3594 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3595 	    (ip_min_frag_prune_time != 0 ?
3596 	    ip_min_frag_prune_time : msec_per_tick)) {
3597 
3598 		ill->ill_frag_free_num_pkts++;
3599 
3600 	} else {
3601 		ill->ill_frag_free_num_pkts = 0;
3602 	}
3603 	ill->ill_last_frag_clean_time = lbolt;
3604 	mutex_exit(&ill->ill_lock);
3605 
3606 	/*
3607 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3608 	 */
3609 	if (ill->ill_frag_free_num_pkts != 0) {
3610 		int ix;
3611 
3612 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3613 			ipfb = &ill->ill_frag_hash_tbl[ix];
3614 			mutex_enter(&ipfb->ipfb_lock);
3615 			if (ipfb->ipfb_ipf != NULL) {
3616 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3617 				    ill->ill_frag_free_num_pkts);
3618 			}
3619 			mutex_exit(&ipfb->ipfb_lock);
3620 		}
3621 	}
3622 	/*
3623 	 * While the reassembly list for this ILL is too big, prune a fragment
3624 	 * queue by age, oldest first.
3625 	 */
3626 	while (ill->ill_frag_count > max_count) {
3627 		int	ix;
3628 		ipfb_t	*oipfb = NULL;
3629 		uint_t	oldest = UINT_MAX;
3630 
3631 		count = 0;
3632 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3633 			ipfb = &ill->ill_frag_hash_tbl[ix];
3634 			mutex_enter(&ipfb->ipfb_lock);
3635 			ipf = ipfb->ipfb_ipf;
3636 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3637 				oldest = ipf->ipf_gen;
3638 				oipfb = ipfb;
3639 			}
3640 			count += ipfb->ipfb_count;
3641 			mutex_exit(&ipfb->ipfb_lock);
3642 		}
3643 		if (oipfb == NULL)
3644 			break;
3645 
3646 		if (count <= max_count)
3647 			return;	/* Somebody beat us to it, nothing to do */
3648 		mutex_enter(&oipfb->ipfb_lock);
3649 		ipf = oipfb->ipfb_ipf;
3650 		if (ipf != NULL) {
3651 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3652 		}
3653 		mutex_exit(&oipfb->ipfb_lock);
3654 	}
3655 }
3656 
3657 /*
3658  * free 'free_cnt' fragmented packets starting at ipf.
3659  */
3660 void
3661 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3662 {
3663 	size_t	count;
3664 	mblk_t	*mp;
3665 	mblk_t	*tmp;
3666 	ipf_t **ipfp = ipf->ipf_ptphn;
3667 
3668 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3669 	ASSERT(ipfp != NULL);
3670 	ASSERT(ipf != NULL);
3671 
3672 	while (ipf != NULL && free_cnt-- > 0) {
3673 		count = ipf->ipf_count;
3674 		mp = ipf->ipf_mp;
3675 		ipf = ipf->ipf_hash_next;
3676 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3677 			IP_REASS_SET_START(tmp, 0);
3678 			IP_REASS_SET_END(tmp, 0);
3679 		}
3680 		atomic_add_32(&ill->ill_frag_count, -count);
3681 		ASSERT(ipfb->ipfb_count >= count);
3682 		ipfb->ipfb_count -= count;
3683 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3684 		ipfb->ipfb_frag_pkts--;
3685 		freemsg(mp);
3686 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3687 	}
3688 
3689 	if (ipf)
3690 		ipf->ipf_ptphn = ipfp;
3691 	ipfp[0] = ipf;
3692 }
3693 
3694 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3695 	"obsolete and may be removed in a future release of Solaris.  Use " \
3696 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3697 
3698 /*
3699  * For obsolete per-interface forwarding configuration;
3700  * called in response to ND_GET.
3701  */
3702 /* ARGSUSED */
3703 static int
3704 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3705 {
3706 	ill_t *ill = (ill_t *)cp;
3707 
3708 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3709 
3710 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3711 	return (0);
3712 }
3713 
3714 /*
3715  * For obsolete per-interface forwarding configuration;
3716  * called in response to ND_SET.
3717  */
3718 /* ARGSUSED */
3719 static int
3720 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3721     cred_t *ioc_cr)
3722 {
3723 	long value;
3724 	int retval;
3725 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
3726 
3727 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3728 
3729 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3730 	    value < 0 || value > 1) {
3731 		return (EINVAL);
3732 	}
3733 
3734 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3735 	retval = ill_forward_set((ill_t *)cp, (value != 0));
3736 	rw_exit(&ipst->ips_ill_g_lock);
3737 	return (retval);
3738 }
3739 
3740 /*
3741  * Helper function for ill_forward_set().
3742  */
3743 static void
3744 ill_forward_set_on_ill(ill_t *ill, boolean_t enable)
3745 {
3746 	ip_stack_t	*ipst = ill->ill_ipst;
3747 
3748 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3749 
3750 	ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3751 	    (enable ? "Enabling" : "Disabling"),
3752 	    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3753 	mutex_enter(&ill->ill_lock);
3754 	if (enable)
3755 		ill->ill_flags |= ILLF_ROUTER;
3756 	else
3757 		ill->ill_flags &= ~ILLF_ROUTER;
3758 	mutex_exit(&ill->ill_lock);
3759 	if (ill->ill_isv6)
3760 		ill_set_nce_router_flags(ill, enable);
3761 	/* Notify routing socket listeners of this change. */
3762 	ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
3763 }
3764 
3765 /*
3766  * Set an ill's ILLF_ROUTER flag appropriately.  Send up RTS_IFINFO routing
3767  * socket messages for each interface whose flags we change.
3768  */
3769 int
3770 ill_forward_set(ill_t *ill, boolean_t enable)
3771 {
3772 	ipmp_illgrp_t *illg;
3773 	ip_stack_t *ipst = ill->ill_ipst;
3774 
3775 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3776 
3777 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3778 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
3779 		return (0);
3780 
3781 	if (IS_LOOPBACK(ill))
3782 		return (EINVAL);
3783 
3784 	if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
3785 		/*
3786 		 * Update all of the interfaces in the group.
3787 		 */
3788 		illg = ill->ill_grp;
3789 		ill = list_head(&illg->ig_if);
3790 		for (; ill != NULL; ill = list_next(&illg->ig_if, ill))
3791 			ill_forward_set_on_ill(ill, enable);
3792 
3793 		/*
3794 		 * Update the IPMP meta-interface.
3795 		 */
3796 		ill_forward_set_on_ill(ipmp_illgrp_ipmp_ill(illg), enable);
3797 		return (0);
3798 	}
3799 
3800 	ill_forward_set_on_ill(ill, enable);
3801 	return (0);
3802 }
3803 
3804 /*
3805  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3806  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3807  * set or clear.
3808  */
3809 static void
3810 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3811 {
3812 	ipif_t *ipif;
3813 	nce_t *nce;
3814 
3815 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3816 		/*
3817 		 * NOTE: we match across the illgrp because nce's for
3818 		 * addresses on IPMP interfaces have an nce_ill that points to
3819 		 * the bound underlying ill.
3820 		 */
3821 		nce = ndp_lookup_v6(ill, B_TRUE, &ipif->ipif_v6lcl_addr,
3822 		    B_FALSE);
3823 		if (nce != NULL) {
3824 			mutex_enter(&nce->nce_lock);
3825 			if (enable)
3826 				nce->nce_flags |= NCE_F_ISROUTER;
3827 			else
3828 				nce->nce_flags &= ~NCE_F_ISROUTER;
3829 			mutex_exit(&nce->nce_lock);
3830 			NCE_REFRELE(nce);
3831 		}
3832 	}
3833 }
3834 
3835 /*
3836  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
3837  * for this ill.  Make sure the v6/v4 question has been answered about this
3838  * ill.  The creation of this ndd variable is only for backwards compatibility.
3839  * The preferred way to control per-interface IP forwarding is through the
3840  * ILLF_ROUTER interface flag.
3841  */
3842 static int
3843 ill_set_ndd_name(ill_t *ill)
3844 {
3845 	char *suffix;
3846 	ip_stack_t	*ipst = ill->ill_ipst;
3847 
3848 	ASSERT(IAM_WRITER_ILL(ill));
3849 
3850 	if (ill->ill_isv6)
3851 		suffix = ipv6_forward_suffix;
3852 	else
3853 		suffix = ipv4_forward_suffix;
3854 
3855 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
3856 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
3857 	/*
3858 	 * Copies over the '\0'.
3859 	 * Note that strlen(suffix) is always bounded.
3860 	 */
3861 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
3862 	    strlen(suffix) + 1);
3863 
3864 	/*
3865 	 * Use of the nd table requires holding the reader lock.
3866 	 * Modifying the nd table thru nd_load/nd_unload requires
3867 	 * the writer lock.
3868 	 */
3869 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
3870 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
3871 	    nd_ill_forward_set, (caddr_t)ill)) {
3872 		/*
3873 		 * If the nd_load failed, it only meant that it could not
3874 		 * allocate a new bunch of room for further NDD expansion.
3875 		 * Because of that, the ill_ndd_name will be set to 0, and
3876 		 * this interface is at the mercy of the global ip_forwarding
3877 		 * variable.
3878 		 */
3879 		rw_exit(&ipst->ips_ip_g_nd_lock);
3880 		ill->ill_ndd_name = NULL;
3881 		return (ENOMEM);
3882 	}
3883 	rw_exit(&ipst->ips_ip_g_nd_lock);
3884 	return (0);
3885 }
3886 
3887 /*
3888  * Intializes the context structure and returns the first ill in the list
3889  * cuurently start_list and end_list can have values:
3890  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
3891  * IP_V4_G_HEAD		Traverse IPV4 list only.
3892  * IP_V6_G_HEAD		Traverse IPV6 list only.
3893  */
3894 
3895 /*
3896  * We don't check for CONDEMNED ills here. Caller must do that if
3897  * necessary under the ill lock.
3898  */
3899 ill_t *
3900 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
3901     ip_stack_t *ipst)
3902 {
3903 	ill_if_t *ifp;
3904 	ill_t *ill;
3905 	avl_tree_t *avl_tree;
3906 
3907 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
3908 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
3909 
3910 	/*
3911 	 * setup the lists to search
3912 	 */
3913 	if (end_list != MAX_G_HEADS) {
3914 		ctx->ctx_current_list = start_list;
3915 		ctx->ctx_last_list = end_list;
3916 	} else {
3917 		ctx->ctx_last_list = MAX_G_HEADS - 1;
3918 		ctx->ctx_current_list = 0;
3919 	}
3920 
3921 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
3922 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
3923 		if (ifp != (ill_if_t *)
3924 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
3925 			avl_tree = &ifp->illif_avl_by_ppa;
3926 			ill = avl_first(avl_tree);
3927 			/*
3928 			 * ill is guaranteed to be non NULL or ifp should have
3929 			 * not existed.
3930 			 */
3931 			ASSERT(ill != NULL);
3932 			return (ill);
3933 		}
3934 		ctx->ctx_current_list++;
3935 	}
3936 
3937 	return (NULL);
3938 }
3939 
3940 /*
3941  * returns the next ill in the list. ill_first() must have been called
3942  * before calling ill_next() or bad things will happen.
3943  */
3944 
3945 /*
3946  * We don't check for CONDEMNED ills here. Caller must do that if
3947  * necessary under the ill lock.
3948  */
3949 ill_t *
3950 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
3951 {
3952 	ill_if_t *ifp;
3953 	ill_t *ill;
3954 	ip_stack_t	*ipst = lastill->ill_ipst;
3955 
3956 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
3957 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
3958 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
3959 	    AVL_AFTER)) != NULL) {
3960 		return (ill);
3961 	}
3962 
3963 	/* goto next ill_ifp in the list. */
3964 	ifp = lastill->ill_ifptr->illif_next;
3965 
3966 	/* make sure not at end of circular list */
3967 	while (ifp ==
3968 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
3969 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
3970 			return (NULL);
3971 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
3972 	}
3973 
3974 	return (avl_first(&ifp->illif_avl_by_ppa));
3975 }
3976 
3977 /*
3978  * Check interface name for correct format: [a-zA-Z]+[a-zA-Z0-9._]*[0-9]+
3979  * The final number (PPA) must not have any leading zeros.  Upon success, a
3980  * pointer to the start of the PPA is returned; otherwise NULL is returned.
3981  */
3982 static char *
3983 ill_get_ppa_ptr(char *name)
3984 {
3985 	int namelen = strlen(name);
3986 	int end_ndx = namelen - 1;
3987 	int ppa_ndx, i;
3988 
3989 	/*
3990 	 * Check that the first character is [a-zA-Z], and that the last
3991 	 * character is [0-9].
3992 	 */
3993 	if (namelen == 0 || !isalpha(name[0]) || !isdigit(name[end_ndx]))
3994 		return (NULL);
3995 
3996 	/*
3997 	 * Set `ppa_ndx' to the PPA start, and check for leading zeroes.
3998 	 */
3999 	for (ppa_ndx = end_ndx; ppa_ndx > 0; ppa_ndx--)
4000 		if (!isdigit(name[ppa_ndx - 1]))
4001 			break;
4002 
4003 	if (name[ppa_ndx] == '0' && ppa_ndx < end_ndx)
4004 		return (NULL);
4005 
4006 	/*
4007 	 * Check that the intermediate characters are [a-z0-9.]
4008 	 */
4009 	for (i = 1; i < ppa_ndx; i++) {
4010 		if (!isalpha(name[i]) && !isdigit(name[i]) &&
4011 		    name[i] != '.' && name[i] != '_') {
4012 			return (NULL);
4013 		}
4014 	}
4015 
4016 	return (name + ppa_ndx);
4017 }
4018 
4019 /*
4020  * use avl tree to locate the ill.
4021  */
4022 static ill_t *
4023 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
4024     ipsq_func_t func, int *error, ip_stack_t *ipst)
4025 {
4026 	char *ppa_ptr = NULL;
4027 	int len;
4028 	uint_t ppa;
4029 	ill_t *ill = NULL;
4030 	ill_if_t *ifp;
4031 	int list;
4032 	ipsq_t *ipsq;
4033 
4034 	if (error != NULL)
4035 		*error = 0;
4036 
4037 	/*
4038 	 * get ppa ptr
4039 	 */
4040 	if (isv6)
4041 		list = IP_V6_G_HEAD;
4042 	else
4043 		list = IP_V4_G_HEAD;
4044 
4045 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4046 		if (error != NULL)
4047 			*error = ENXIO;
4048 		return (NULL);
4049 	}
4050 
4051 	len = ppa_ptr - name + 1;
4052 
4053 	ppa = stoi(&ppa_ptr);
4054 
4055 	ifp = IP_VX_ILL_G_LIST(list, ipst);
4056 
4057 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4058 		/*
4059 		 * match is done on len - 1 as the name is not null
4060 		 * terminated it contains ppa in addition to the interface
4061 		 * name.
4062 		 */
4063 		if ((ifp->illif_name_len == len) &&
4064 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4065 			break;
4066 		} else {
4067 			ifp = ifp->illif_next;
4068 		}
4069 	}
4070 
4071 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4072 		/*
4073 		 * Even the interface type does not exist.
4074 		 */
4075 		if (error != NULL)
4076 			*error = ENXIO;
4077 		return (NULL);
4078 	}
4079 
4080 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4081 	if (ill != NULL) {
4082 		/*
4083 		 * The block comment at the start of ipif_down
4084 		 * explains the use of the macros used below
4085 		 */
4086 		GRAB_CONN_LOCK(q);
4087 		mutex_enter(&ill->ill_lock);
4088 		if (ILL_CAN_LOOKUP(ill)) {
4089 			ill_refhold_locked(ill);
4090 			mutex_exit(&ill->ill_lock);
4091 			RELEASE_CONN_LOCK(q);
4092 			return (ill);
4093 		} else if (ILL_CAN_WAIT(ill, q)) {
4094 			ipsq = ill->ill_phyint->phyint_ipsq;
4095 			mutex_enter(&ipsq->ipsq_lock);
4096 			mutex_enter(&ipsq->ipsq_xop->ipx_lock);
4097 			mutex_exit(&ill->ill_lock);
4098 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4099 			mutex_exit(&ipsq->ipsq_xop->ipx_lock);
4100 			mutex_exit(&ipsq->ipsq_lock);
4101 			RELEASE_CONN_LOCK(q);
4102 			if (error != NULL)
4103 				*error = EINPROGRESS;
4104 			return (NULL);
4105 		}
4106 		mutex_exit(&ill->ill_lock);
4107 		RELEASE_CONN_LOCK(q);
4108 	}
4109 	if (error != NULL)
4110 		*error = ENXIO;
4111 	return (NULL);
4112 }
4113 
4114 /*
4115  * comparison function for use with avl.
4116  */
4117 static int
4118 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4119 {
4120 	uint_t ppa;
4121 	uint_t ill_ppa;
4122 
4123 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4124 
4125 	ppa = *((uint_t *)ppa_ptr);
4126 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4127 	/*
4128 	 * We want the ill with the lowest ppa to be on the
4129 	 * top.
4130 	 */
4131 	if (ill_ppa < ppa)
4132 		return (1);
4133 	if (ill_ppa > ppa)
4134 		return (-1);
4135 	return (0);
4136 }
4137 
4138 /*
4139  * remove an interface type from the global list.
4140  */
4141 static void
4142 ill_delete_interface_type(ill_if_t *interface)
4143 {
4144 	ASSERT(interface != NULL);
4145 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4146 
4147 	avl_destroy(&interface->illif_avl_by_ppa);
4148 	if (interface->illif_ppa_arena != NULL)
4149 		vmem_destroy(interface->illif_ppa_arena);
4150 
4151 	remque(interface);
4152 
4153 	mi_free(interface);
4154 }
4155 
4156 /*
4157  * remove ill from the global list.
4158  */
4159 static void
4160 ill_glist_delete(ill_t *ill)
4161 {
4162 	ip_stack_t	*ipst;
4163 	phyint_t	*phyi;
4164 
4165 	if (ill == NULL)
4166 		return;
4167 	ipst = ill->ill_ipst;
4168 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4169 
4170 	/*
4171 	 * If the ill was never inserted into the AVL tree
4172 	 * we skip the if branch.
4173 	 */
4174 	if (ill->ill_ifptr != NULL) {
4175 		/*
4176 		 * remove from AVL tree and free ppa number
4177 		 */
4178 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4179 
4180 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4181 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4182 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4183 		}
4184 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4185 			ill_delete_interface_type(ill->ill_ifptr);
4186 		}
4187 
4188 		/*
4189 		 * Indicate ill is no longer in the list.
4190 		 */
4191 		ill->ill_ifptr = NULL;
4192 		ill->ill_name_length = 0;
4193 		ill->ill_name[0] = '\0';
4194 		ill->ill_ppa = UINT_MAX;
4195 	}
4196 
4197 	/* Generate one last event for this ill. */
4198 	ill_nic_event_dispatch(ill, 0, NE_UNPLUMB, ill->ill_name,
4199 	    ill->ill_name_length);
4200 
4201 	ASSERT(ill->ill_phyint != NULL);
4202 	phyi = ill->ill_phyint;
4203 	ill->ill_phyint = NULL;
4204 
4205 	/*
4206 	 * ill_init allocates a phyint always to store the copy
4207 	 * of flags relevant to phyint. At that point in time, we could
4208 	 * not assign the name and hence phyint_illv4/v6 could not be
4209 	 * initialized. Later in ipif_set_values, we assign the name to
4210 	 * the ill, at which point in time we assign phyint_illv4/v6.
4211 	 * Thus we don't rely on phyint_illv6 to be initialized always.
4212 	 */
4213 	if (ill->ill_flags & ILLF_IPV6)
4214 		phyi->phyint_illv6 = NULL;
4215 	else
4216 		phyi->phyint_illv4 = NULL;
4217 
4218 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL) {
4219 		rw_exit(&ipst->ips_ill_g_lock);
4220 		return;
4221 	}
4222 
4223 	/*
4224 	 * There are no ills left on this phyint; pull it out of the phyint
4225 	 * avl trees, and free it.
4226 	 */
4227 	if (phyi->phyint_ifindex > 0) {
4228 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4229 		    phyi);
4230 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
4231 		    phyi);
4232 	}
4233 	rw_exit(&ipst->ips_ill_g_lock);
4234 
4235 	phyint_free(phyi);
4236 }
4237 
4238 /*
4239  * allocate a ppa, if the number of plumbed interfaces of this type are
4240  * less than ill_no_arena do a linear search to find a unused ppa.
4241  * When the number goes beyond ill_no_arena switch to using an arena.
4242  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4243  * is the return value for an error condition, so allocation starts at one
4244  * and is decremented by one.
4245  */
4246 static int
4247 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4248 {
4249 	ill_t *tmp_ill;
4250 	uint_t start, end;
4251 	int ppa;
4252 
4253 	if (ifp->illif_ppa_arena == NULL &&
4254 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4255 		/*
4256 		 * Create an arena.
4257 		 */
4258 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4259 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4260 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4261 			/* allocate what has already been assigned */
4262 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4263 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4264 		    tmp_ill, AVL_AFTER)) {
4265 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4266 			    1,		/* size */
4267 			    1,		/* align/quantum */
4268 			    0,		/* phase */
4269 			    0,		/* nocross */
4270 			    /* minaddr */
4271 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
4272 			    /* maxaddr */
4273 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
4274 			    VM_NOSLEEP|VM_FIRSTFIT);
4275 			if (ppa == 0) {
4276 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4277 				    " failed while switching"));
4278 				vmem_destroy(ifp->illif_ppa_arena);
4279 				ifp->illif_ppa_arena = NULL;
4280 				break;
4281 			}
4282 		}
4283 	}
4284 
4285 	if (ifp->illif_ppa_arena != NULL) {
4286 		if (ill->ill_ppa == UINT_MAX) {
4287 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4288 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4289 			if (ppa == 0)
4290 				return (EAGAIN);
4291 			ill->ill_ppa = --ppa;
4292 		} else {
4293 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4294 			    1, 		/* size */
4295 			    1, 		/* align/quantum */
4296 			    0, 		/* phase */
4297 			    0, 		/* nocross */
4298 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4299 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4300 			    VM_NOSLEEP|VM_FIRSTFIT);
4301 			/*
4302 			 * Most likely the allocation failed because
4303 			 * the requested ppa was in use.
4304 			 */
4305 			if (ppa == 0)
4306 				return (EEXIST);
4307 		}
4308 		return (0);
4309 	}
4310 
4311 	/*
4312 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4313 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4314 	 */
4315 	if (ill->ill_ppa == UINT_MAX) {
4316 		end = UINT_MAX - 1;
4317 		start = 0;
4318 	} else {
4319 		end = start = ill->ill_ppa;
4320 	}
4321 
4322 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4323 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4324 		if (start++ >= end) {
4325 			if (ill->ill_ppa == UINT_MAX)
4326 				return (EAGAIN);
4327 			else
4328 				return (EEXIST);
4329 		}
4330 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4331 	}
4332 	ill->ill_ppa = start;
4333 	return (0);
4334 }
4335 
4336 /*
4337  * Insert ill into the list of configured ill's. Once this function completes,
4338  * the ill is globally visible and is available through lookups. More precisely
4339  * this happens after the caller drops the ill_g_lock.
4340  */
4341 static int
4342 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4343 {
4344 	ill_if_t *ill_interface;
4345 	avl_index_t where = 0;
4346 	int error;
4347 	int name_length;
4348 	int index;
4349 	boolean_t check_length = B_FALSE;
4350 	ip_stack_t	*ipst = ill->ill_ipst;
4351 
4352 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4353 
4354 	name_length = mi_strlen(name) + 1;
4355 
4356 	if (isv6)
4357 		index = IP_V6_G_HEAD;
4358 	else
4359 		index = IP_V4_G_HEAD;
4360 
4361 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4362 	/*
4363 	 * Search for interface type based on name
4364 	 */
4365 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4366 		if ((ill_interface->illif_name_len == name_length) &&
4367 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4368 			break;
4369 		}
4370 		ill_interface = ill_interface->illif_next;
4371 	}
4372 
4373 	/*
4374 	 * Interface type not found, create one.
4375 	 */
4376 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4377 		ill_g_head_t ghead;
4378 
4379 		/*
4380 		 * allocate ill_if_t structure
4381 		 */
4382 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4383 		if (ill_interface == NULL) {
4384 			return (ENOMEM);
4385 		}
4386 
4387 		(void) strcpy(ill_interface->illif_name, name);
4388 		ill_interface->illif_name_len = name_length;
4389 
4390 		avl_create(&ill_interface->illif_avl_by_ppa,
4391 		    ill_compare_ppa, sizeof (ill_t),
4392 		    offsetof(struct ill_s, ill_avl_byppa));
4393 
4394 		/*
4395 		 * link the structure in the back to maintain order
4396 		 * of configuration for ifconfig output.
4397 		 */
4398 		ghead = ipst->ips_ill_g_heads[index];
4399 		insque(ill_interface, ghead.ill_g_list_tail);
4400 	}
4401 
4402 	if (ill->ill_ppa == UINT_MAX)
4403 		check_length = B_TRUE;
4404 
4405 	error = ill_alloc_ppa(ill_interface, ill);
4406 	if (error != 0) {
4407 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4408 			ill_delete_interface_type(ill->ill_ifptr);
4409 		return (error);
4410 	}
4411 
4412 	/*
4413 	 * When the ppa is choosen by the system, check that there is
4414 	 * enough space to insert ppa. if a specific ppa was passed in this
4415 	 * check is not required as the interface name passed in will have
4416 	 * the right ppa in it.
4417 	 */
4418 	if (check_length) {
4419 		/*
4420 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4421 		 */
4422 		char buf[sizeof (uint_t) * 3];
4423 
4424 		/*
4425 		 * convert ppa to string to calculate the amount of space
4426 		 * required for it in the name.
4427 		 */
4428 		numtos(ill->ill_ppa, buf);
4429 
4430 		/* Do we have enough space to insert ppa ? */
4431 
4432 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4433 			/* Free ppa and interface type struct */
4434 			if (ill_interface->illif_ppa_arena != NULL) {
4435 				vmem_free(ill_interface->illif_ppa_arena,
4436 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4437 			}
4438 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4439 				ill_delete_interface_type(ill->ill_ifptr);
4440 
4441 			return (EINVAL);
4442 		}
4443 	}
4444 
4445 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4446 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4447 
4448 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4449 	    &where);
4450 	ill->ill_ifptr = ill_interface;
4451 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4452 
4453 	ill_phyint_reinit(ill);
4454 	return (0);
4455 }
4456 
4457 /* Initialize the per phyint ipsq used for serialization */
4458 static boolean_t
4459 ipsq_init(ill_t *ill, boolean_t enter)
4460 {
4461 	ipsq_t  *ipsq;
4462 	ipxop_t	*ipx;
4463 
4464 	if ((ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP)) == NULL)
4465 		return (B_FALSE);
4466 
4467 	ill->ill_phyint->phyint_ipsq = ipsq;
4468 	ipx = ipsq->ipsq_xop = &ipsq->ipsq_ownxop;
4469 	ipx->ipx_ipsq = ipsq;
4470 	ipsq->ipsq_next = ipsq;
4471 	ipsq->ipsq_phyint = ill->ill_phyint;
4472 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4473 	mutex_init(&ipx->ipx_lock, NULL, MUTEX_DEFAULT, 0);
4474 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4475 	if (enter) {
4476 		ipx->ipx_writer = curthread;
4477 		ipx->ipx_forced = B_FALSE;
4478 		ipx->ipx_reentry_cnt = 1;
4479 #ifdef DEBUG
4480 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
4481 #endif
4482 	}
4483 	return (B_TRUE);
4484 }
4485 
4486 /*
4487  * ill_init is called by ip_open when a device control stream is opened.
4488  * It does a few initializations, and shoots a DL_INFO_REQ message down
4489  * to the driver.  The response is later picked up in ip_rput_dlpi and
4490  * used to set up default mechanisms for talking to the driver.  (Always
4491  * called as writer.)
4492  *
4493  * If this function returns error, ip_open will call ip_close which in
4494  * turn will call ill_delete to clean up any memory allocated here that
4495  * is not yet freed.
4496  */
4497 int
4498 ill_init(queue_t *q, ill_t *ill)
4499 {
4500 	int	count;
4501 	dl_info_req_t	*dlir;
4502 	mblk_t	*info_mp;
4503 	uchar_t *frag_ptr;
4504 
4505 	/*
4506 	 * The ill is initialized to zero by mi_alloc*(). In addition
4507 	 * some fields already contain valid values, initialized in
4508 	 * ip_open(), before we reach here.
4509 	 */
4510 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4511 
4512 	ill->ill_rq = q;
4513 	ill->ill_wq = WR(q);
4514 
4515 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4516 	    BPRI_HI);
4517 	if (info_mp == NULL)
4518 		return (ENOMEM);
4519 
4520 	/*
4521 	 * Allocate sufficient space to contain our fragment hash table and
4522 	 * the device name.
4523 	 */
4524 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4525 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4526 	if (frag_ptr == NULL) {
4527 		freemsg(info_mp);
4528 		return (ENOMEM);
4529 	}
4530 	ill->ill_frag_ptr = frag_ptr;
4531 	ill->ill_frag_free_num_pkts = 0;
4532 	ill->ill_last_frag_clean_time = 0;
4533 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4534 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4535 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4536 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4537 		    NULL, MUTEX_DEFAULT, NULL);
4538 	}
4539 
4540 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4541 	if (ill->ill_phyint == NULL) {
4542 		freemsg(info_mp);
4543 		mi_free(frag_ptr);
4544 		return (ENOMEM);
4545 	}
4546 
4547 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4548 	/*
4549 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4550 	 * at this point because of the following reason. If we can't
4551 	 * enter the ipsq at some point and cv_wait, the writer that
4552 	 * wakes us up tries to locate us using the list of all phyints
4553 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4554 	 * If we don't set it now, we risk a missed wakeup.
4555 	 */
4556 	ill->ill_phyint->phyint_illv4 = ill;
4557 	ill->ill_ppa = UINT_MAX;
4558 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4559 
4560 	if (!ipsq_init(ill, B_TRUE)) {
4561 		freemsg(info_mp);
4562 		mi_free(frag_ptr);
4563 		mi_free(ill->ill_phyint);
4564 		return (ENOMEM);
4565 	}
4566 
4567 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4568 
4569 	/* Frag queue limit stuff */
4570 	ill->ill_frag_count = 0;
4571 	ill->ill_ipf_gen = 0;
4572 
4573 	ill->ill_global_timer = INFINITY;
4574 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4575 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4576 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4577 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4578 
4579 	/*
4580 	 * Initialize IPv6 configuration variables.  The IP module is always
4581 	 * opened as an IPv4 module.  Instead tracking down the cases where
4582 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4583 	 * here for convenience, this has no effect until the ill is set to do
4584 	 * IPv6.
4585 	 */
4586 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4587 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4588 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4589 	ill->ill_max_buf = ND_MAX_Q;
4590 	ill->ill_refcnt = 0;
4591 
4592 	/* Send down the Info Request to the driver. */
4593 	info_mp->b_datap->db_type = M_PCPROTO;
4594 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4595 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4596 	dlir->dl_primitive = DL_INFO_REQ;
4597 
4598 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4599 
4600 	qprocson(q);
4601 	ill_dlpi_send(ill, info_mp);
4602 
4603 	return (0);
4604 }
4605 
4606 /*
4607  * ill_dls_info
4608  * creates datalink socket info from the device.
4609  */
4610 int
4611 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4612 {
4613 	size_t	len;
4614 	ill_t	*ill = ipif->ipif_ill;
4615 
4616 	sdl->sdl_family = AF_LINK;
4617 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4618 	sdl->sdl_type = ill->ill_type;
4619 	ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4620 	len = strlen(sdl->sdl_data);
4621 	ASSERT(len < 256);
4622 	sdl->sdl_nlen = (uchar_t)len;
4623 	sdl->sdl_alen = ill->ill_phys_addr_length;
4624 	sdl->sdl_slen = 0;
4625 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4626 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4627 
4628 	return (sizeof (struct sockaddr_dl));
4629 }
4630 
4631 /*
4632  * ill_xarp_info
4633  * creates xarp info from the device.
4634  */
4635 static int
4636 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4637 {
4638 	sdl->sdl_family = AF_LINK;
4639 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4640 	sdl->sdl_type = ill->ill_type;
4641 	ipif_get_name(ill->ill_ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4642 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4643 	sdl->sdl_alen = ill->ill_phys_addr_length;
4644 	sdl->sdl_slen = 0;
4645 	return (sdl->sdl_nlen);
4646 }
4647 
4648 static int
4649 loopback_kstat_update(kstat_t *ksp, int rw)
4650 {
4651 	kstat_named_t *kn;
4652 	netstackid_t	stackid;
4653 	netstack_t	*ns;
4654 	ip_stack_t	*ipst;
4655 
4656 	if (ksp == NULL || ksp->ks_data == NULL)
4657 		return (EIO);
4658 
4659 	if (rw == KSTAT_WRITE)
4660 		return (EACCES);
4661 
4662 	kn = KSTAT_NAMED_PTR(ksp);
4663 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4664 
4665 	ns = netstack_find_by_stackid(stackid);
4666 	if (ns == NULL)
4667 		return (-1);
4668 
4669 	ipst = ns->netstack_ip;
4670 	if (ipst == NULL) {
4671 		netstack_rele(ns);
4672 		return (-1);
4673 	}
4674 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4675 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4676 	netstack_rele(ns);
4677 	return (0);
4678 }
4679 
4680 /*
4681  * Has ifindex been plumbed already?
4682  */
4683 boolean_t
4684 phyint_exists(uint_t index, ip_stack_t *ipst)
4685 {
4686 	ASSERT(index != 0);
4687 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4688 
4689 	return (avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4690 	    &index, NULL) != NULL);
4691 }
4692 
4693 /* Pick a unique ifindex */
4694 boolean_t
4695 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4696 {
4697 	uint_t starting_index;
4698 
4699 	if (!ipst->ips_ill_index_wrap) {
4700 		*indexp = ipst->ips_ill_index++;
4701 		if (ipst->ips_ill_index == 0) {
4702 			/* Reached the uint_t limit Next time wrap  */
4703 			ipst->ips_ill_index_wrap = B_TRUE;
4704 		}
4705 		return (B_TRUE);
4706 	}
4707 
4708 	/*
4709 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4710 	 * at this point and don't want to call any function that attempts
4711 	 * to get the lock again.
4712 	 */
4713 	starting_index = ipst->ips_ill_index++;
4714 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
4715 		if (ipst->ips_ill_index != 0 &&
4716 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
4717 			/* found unused index - use it */
4718 			*indexp = ipst->ips_ill_index;
4719 			return (B_TRUE);
4720 		}
4721 	}
4722 
4723 	/*
4724 	 * all interface indicies are inuse.
4725 	 */
4726 	return (B_FALSE);
4727 }
4728 
4729 /*
4730  * Assign a unique interface index for the phyint.
4731  */
4732 static boolean_t
4733 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
4734 {
4735 	ASSERT(phyi->phyint_ifindex == 0);
4736 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
4737 }
4738 
4739 /*
4740  * Return a pointer to the ill which matches the supplied name.  Note that
4741  * the ill name length includes the null termination character.  (May be
4742  * called as writer.)
4743  * If do_alloc and the interface is "lo0" it will be automatically created.
4744  * Cannot bump up reference on condemned ills. So dup detect can't be done
4745  * using this func.
4746  */
4747 ill_t *
4748 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4749     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
4750     ip_stack_t *ipst)
4751 {
4752 	ill_t	*ill;
4753 	ipif_t	*ipif;
4754 	ipsq_t	*ipsq;
4755 	kstat_named_t	*kn;
4756 	boolean_t isloopback;
4757 	in6_addr_t ov6addr;
4758 
4759 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4760 
4761 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4762 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4763 	rw_exit(&ipst->ips_ill_g_lock);
4764 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4765 		return (ill);
4766 
4767 	/*
4768 	 * Couldn't find it.  Does this happen to be a lookup for the
4769 	 * loopback device and are we allowed to allocate it?
4770 	 */
4771 	if (!isloopback || !do_alloc)
4772 		return (NULL);
4773 
4774 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4775 
4776 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4777 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4778 		rw_exit(&ipst->ips_ill_g_lock);
4779 		return (ill);
4780 	}
4781 
4782 	/* Create the loopback device on demand */
4783 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4784 	    sizeof (ipif_loopback_name), BPRI_MED));
4785 	if (ill == NULL)
4786 		goto done;
4787 
4788 	*ill = ill_null;
4789 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4790 	ill->ill_ipst = ipst;
4791 	netstack_hold(ipst->ips_netstack);
4792 	/*
4793 	 * For exclusive stacks we set the zoneid to zero
4794 	 * to make IP operate as if in the global zone.
4795 	 */
4796 	ill->ill_zoneid = GLOBAL_ZONEID;
4797 
4798 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4799 	if (ill->ill_phyint == NULL)
4800 		goto done;
4801 
4802 	if (isv6)
4803 		ill->ill_phyint->phyint_illv6 = ill;
4804 	else
4805 		ill->ill_phyint->phyint_illv4 = ill;
4806 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4807 	ill->ill_max_frag = IP_LOOPBACK_MTU;
4808 	/* Add room for tcp+ip headers */
4809 	if (isv6) {
4810 		ill->ill_isv6 = B_TRUE;
4811 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
4812 	} else {
4813 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
4814 	}
4815 	if (!ill_allocate_mibs(ill))
4816 		goto done;
4817 	ill->ill_max_mtu = ill->ill_max_frag;
4818 	/*
4819 	 * ipif_loopback_name can't be pointed at directly because its used
4820 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
4821 	 * from the glist, ill_glist_delete() sets the first character of
4822 	 * ill_name to '\0'.
4823 	 */
4824 	ill->ill_name = (char *)ill + sizeof (*ill);
4825 	(void) strcpy(ill->ill_name, ipif_loopback_name);
4826 	ill->ill_name_length = sizeof (ipif_loopback_name);
4827 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
4828 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4829 
4830 	ill->ill_global_timer = INFINITY;
4831 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4832 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4833 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4834 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4835 
4836 	/* No resolver here. */
4837 	ill->ill_net_type = IRE_LOOPBACK;
4838 
4839 	/* Initialize the ipsq */
4840 	if (!ipsq_init(ill, B_FALSE))
4841 		goto done;
4842 
4843 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE, B_TRUE);
4844 	if (ipif == NULL)
4845 		goto done;
4846 
4847 	ill->ill_flags = ILLF_MULTICAST;
4848 
4849 	ov6addr = ipif->ipif_v6lcl_addr;
4850 	/* Set up default loopback address and mask. */
4851 	if (!isv6) {
4852 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
4853 
4854 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
4855 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4856 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
4857 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4858 		    ipif->ipif_v6subnet);
4859 		ill->ill_flags |= ILLF_IPV4;
4860 	} else {
4861 		ipif->ipif_v6lcl_addr = ipv6_loopback;
4862 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4863 		ipif->ipif_v6net_mask = ipv6_all_ones;
4864 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4865 		    ipif->ipif_v6subnet);
4866 		ill->ill_flags |= ILLF_IPV6;
4867 	}
4868 
4869 	/*
4870 	 * Chain us in at the end of the ill list. hold the ill
4871 	 * before we make it globally visible. 1 for the lookup.
4872 	 */
4873 	ill->ill_refcnt = 0;
4874 	ill_refhold(ill);
4875 
4876 	ill->ill_frag_count = 0;
4877 	ill->ill_frag_free_num_pkts = 0;
4878 	ill->ill_last_frag_clean_time = 0;
4879 
4880 	ipsq = ill->ill_phyint->phyint_ipsq;
4881 
4882 	if (ill_glist_insert(ill, "lo", isv6) != 0)
4883 		cmn_err(CE_PANIC, "cannot insert loopback interface");
4884 
4885 	/* Let SCTP know so that it can add this to its list */
4886 	sctp_update_ill(ill, SCTP_ILL_INSERT);
4887 
4888 	/*
4889 	 * We have already assigned ipif_v6lcl_addr above, but we need to
4890 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
4891 	 * requires to be after ill_glist_insert() since we need the
4892 	 * ill_index set. Pass on ipv6_loopback as the old address.
4893 	 */
4894 	sctp_update_ipif_addr(ipif, ov6addr);
4895 
4896 	/*
4897 	 * ill_glist_insert() -> ill_phyint_reinit() may have merged IPSQs.
4898 	 * If so, free our original one.
4899 	 */
4900 	if (ipsq != ill->ill_phyint->phyint_ipsq)
4901 		ipsq_delete(ipsq);
4902 
4903 	/*
4904 	 * Delay this till the ipif is allocated as ipif_allocate
4905 	 * de-references ill_phyint for getting the ifindex. We
4906 	 * can't do this before ipif_allocate because ill_phyint_reinit
4907 	 * -> phyint_assign_ifindex expects ipif to be present.
4908 	 */
4909 	mutex_enter(&ill->ill_phyint->phyint_lock);
4910 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
4911 	mutex_exit(&ill->ill_phyint->phyint_lock);
4912 
4913 	if (ipst->ips_loopback_ksp == NULL) {
4914 		/* Export loopback interface statistics */
4915 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
4916 		    ipif_loopback_name, "net",
4917 		    KSTAT_TYPE_NAMED, 2, 0,
4918 		    ipst->ips_netstack->netstack_stackid);
4919 		if (ipst->ips_loopback_ksp != NULL) {
4920 			ipst->ips_loopback_ksp->ks_update =
4921 			    loopback_kstat_update;
4922 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
4923 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
4924 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
4925 			ipst->ips_loopback_ksp->ks_private =
4926 			    (void *)(uintptr_t)ipst->ips_netstack->
4927 			    netstack_stackid;
4928 			kstat_install(ipst->ips_loopback_ksp);
4929 		}
4930 	}
4931 
4932 	if (error != NULL)
4933 		*error = 0;
4934 	*did_alloc = B_TRUE;
4935 	rw_exit(&ipst->ips_ill_g_lock);
4936 	ill_nic_event_dispatch(ill, MAP_IPIF_ID(ill->ill_ipif->ipif_id),
4937 	    NE_PLUMB, ill->ill_name, ill->ill_name_length);
4938 	return (ill);
4939 done:
4940 	if (ill != NULL) {
4941 		if (ill->ill_phyint != NULL) {
4942 			ipsq = ill->ill_phyint->phyint_ipsq;
4943 			if (ipsq != NULL) {
4944 				ipsq->ipsq_phyint = NULL;
4945 				ipsq_delete(ipsq);
4946 			}
4947 			mi_free(ill->ill_phyint);
4948 		}
4949 		ill_free_mib(ill);
4950 		if (ill->ill_ipst != NULL)
4951 			netstack_rele(ill->ill_ipst->ips_netstack);
4952 		mi_free(ill);
4953 	}
4954 	rw_exit(&ipst->ips_ill_g_lock);
4955 	if (error != NULL)
4956 		*error = ENOMEM;
4957 	return (NULL);
4958 }
4959 
4960 /*
4961  * For IPP calls - use the ip_stack_t for global stack.
4962  */
4963 ill_t *
4964 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
4965     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
4966 {
4967 	ip_stack_t	*ipst;
4968 	ill_t		*ill;
4969 
4970 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
4971 	if (ipst == NULL) {
4972 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
4973 		return (NULL);
4974 	}
4975 
4976 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
4977 	netstack_rele(ipst->ips_netstack);
4978 	return (ill);
4979 }
4980 
4981 /*
4982  * Return a pointer to the ill which matches the index and IP version type.
4983  */
4984 ill_t *
4985 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
4986     ipsq_func_t func, int *err, ip_stack_t *ipst)
4987 {
4988 	ill_t	*ill;
4989 	ipsq_t  *ipsq;
4990 	phyint_t *phyi;
4991 
4992 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
4993 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
4994 
4995 	if (err != NULL)
4996 		*err = 0;
4997 
4998 	/*
4999 	 * Indexes are stored in the phyint - a common structure
5000 	 * to both IPv4 and IPv6.
5001 	 */
5002 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5003 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5004 	    (void *) &index, NULL);
5005 	if (phyi != NULL) {
5006 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5007 		if (ill != NULL) {
5008 			/*
5009 			 * The block comment at the start of ipif_down
5010 			 * explains the use of the macros used below
5011 			 */
5012 			GRAB_CONN_LOCK(q);
5013 			mutex_enter(&ill->ill_lock);
5014 			if (ILL_CAN_LOOKUP(ill)) {
5015 				ill_refhold_locked(ill);
5016 				mutex_exit(&ill->ill_lock);
5017 				RELEASE_CONN_LOCK(q);
5018 				rw_exit(&ipst->ips_ill_g_lock);
5019 				return (ill);
5020 			} else if (ILL_CAN_WAIT(ill, q)) {
5021 				ipsq = ill->ill_phyint->phyint_ipsq;
5022 				mutex_enter(&ipsq->ipsq_lock);
5023 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
5024 				rw_exit(&ipst->ips_ill_g_lock);
5025 				mutex_exit(&ill->ill_lock);
5026 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5027 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
5028 				mutex_exit(&ipsq->ipsq_lock);
5029 				RELEASE_CONN_LOCK(q);
5030 				if (err != NULL)
5031 					*err = EINPROGRESS;
5032 				return (NULL);
5033 			}
5034 			RELEASE_CONN_LOCK(q);
5035 			mutex_exit(&ill->ill_lock);
5036 		}
5037 	}
5038 	rw_exit(&ipst->ips_ill_g_lock);
5039 	if (err != NULL)
5040 		*err = ENXIO;
5041 	return (NULL);
5042 }
5043 
5044 /*
5045  * Return the ifindex next in sequence after the passed in ifindex.
5046  * If there is no next ifindex for the given protocol, return 0.
5047  */
5048 uint_t
5049 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5050 {
5051 	phyint_t *phyi;
5052 	phyint_t *phyi_initial;
5053 	uint_t   ifindex;
5054 
5055 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5056 
5057 	if (index == 0) {
5058 		phyi = avl_first(
5059 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5060 	} else {
5061 		phyi = phyi_initial = avl_find(
5062 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5063 		    (void *) &index, NULL);
5064 	}
5065 
5066 	for (; phyi != NULL;
5067 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5068 	    phyi, AVL_AFTER)) {
5069 		/*
5070 		 * If we're not returning the first interface in the tree
5071 		 * and we still haven't moved past the phyint_t that
5072 		 * corresponds to index, avl_walk needs to be called again
5073 		 */
5074 		if (!((index != 0) && (phyi == phyi_initial))) {
5075 			if (isv6) {
5076 				if ((phyi->phyint_illv6) &&
5077 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5078 				    (phyi->phyint_illv6->ill_isv6 == 1))
5079 					break;
5080 			} else {
5081 				if ((phyi->phyint_illv4) &&
5082 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5083 				    (phyi->phyint_illv4->ill_isv6 == 0))
5084 					break;
5085 			}
5086 		}
5087 	}
5088 
5089 	rw_exit(&ipst->ips_ill_g_lock);
5090 
5091 	if (phyi != NULL)
5092 		ifindex = phyi->phyint_ifindex;
5093 	else
5094 		ifindex = 0;
5095 
5096 	return (ifindex);
5097 }
5098 
5099 /*
5100  * Return the ifindex for the named interface.
5101  * If there is no next ifindex for the interface, return 0.
5102  */
5103 uint_t
5104 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5105 {
5106 	phyint_t	*phyi;
5107 	avl_index_t	where = 0;
5108 	uint_t		ifindex;
5109 
5110 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5111 
5112 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5113 	    name, &where)) == NULL) {
5114 		rw_exit(&ipst->ips_ill_g_lock);
5115 		return (0);
5116 	}
5117 
5118 	ifindex = phyi->phyint_ifindex;
5119 
5120 	rw_exit(&ipst->ips_ill_g_lock);
5121 
5122 	return (ifindex);
5123 }
5124 
5125 /*
5126  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5127  * that gives a running thread a reference to the ill. This reference must be
5128  * released by the thread when it is done accessing the ill and related
5129  * objects. ill_refcnt can not be used to account for static references
5130  * such as other structures pointing to an ill. Callers must generally
5131  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5132  * or be sure that the ill is not being deleted or changing state before
5133  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5134  * ill won't change any of its critical state such as address, netmask etc.
5135  */
5136 void
5137 ill_refhold(ill_t *ill)
5138 {
5139 	mutex_enter(&ill->ill_lock);
5140 	ill->ill_refcnt++;
5141 	ILL_TRACE_REF(ill);
5142 	mutex_exit(&ill->ill_lock);
5143 }
5144 
5145 void
5146 ill_refhold_locked(ill_t *ill)
5147 {
5148 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5149 	ill->ill_refcnt++;
5150 	ILL_TRACE_REF(ill);
5151 }
5152 
5153 int
5154 ill_check_and_refhold(ill_t *ill)
5155 {
5156 	mutex_enter(&ill->ill_lock);
5157 	if (ILL_CAN_LOOKUP(ill)) {
5158 		ill_refhold_locked(ill);
5159 		mutex_exit(&ill->ill_lock);
5160 		return (0);
5161 	}
5162 	mutex_exit(&ill->ill_lock);
5163 	return (ILL_LOOKUP_FAILED);
5164 }
5165 
5166 /*
5167  * Must not be called while holding any locks. Otherwise if this is
5168  * the last reference to be released, there is a chance of recursive mutex
5169  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5170  * to restart an ioctl.
5171  */
5172 void
5173 ill_refrele(ill_t *ill)
5174 {
5175 	mutex_enter(&ill->ill_lock);
5176 	ASSERT(ill->ill_refcnt != 0);
5177 	ill->ill_refcnt--;
5178 	ILL_UNTRACE_REF(ill);
5179 	if (ill->ill_refcnt != 0) {
5180 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5181 		mutex_exit(&ill->ill_lock);
5182 		return;
5183 	}
5184 
5185 	/* Drops the ill_lock */
5186 	ipif_ill_refrele_tail(ill);
5187 }
5188 
5189 /*
5190  * Obtain a weak reference count on the ill. This reference ensures the
5191  * ill won't be freed, but the ill may change any of its critical state
5192  * such as netmask, address etc. Returns an error if the ill has started
5193  * closing.
5194  */
5195 boolean_t
5196 ill_waiter_inc(ill_t *ill)
5197 {
5198 	mutex_enter(&ill->ill_lock);
5199 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5200 		mutex_exit(&ill->ill_lock);
5201 		return (B_FALSE);
5202 	}
5203 	ill->ill_waiters++;
5204 	mutex_exit(&ill->ill_lock);
5205 	return (B_TRUE);
5206 }
5207 
5208 void
5209 ill_waiter_dcr(ill_t *ill)
5210 {
5211 	mutex_enter(&ill->ill_lock);
5212 	ill->ill_waiters--;
5213 	if (ill->ill_waiters == 0)
5214 		cv_broadcast(&ill->ill_cv);
5215 	mutex_exit(&ill->ill_lock);
5216 }
5217 
5218 /*
5219  * Named Dispatch routine to produce a formatted report on all ILLs.
5220  * This report is accessed by using the ndd utility to "get" ND variable
5221  * "ip_ill_status".
5222  */
5223 /* ARGSUSED */
5224 int
5225 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5226 {
5227 	ill_t		*ill;
5228 	ill_walk_context_t ctx;
5229 	ip_stack_t	*ipst;
5230 
5231 	ipst = CONNQ_TO_IPST(q);
5232 
5233 	(void) mi_mpprintf(mp,
5234 	    "ILL      " MI_COL_HDRPAD_STR
5235 	/*   01234567[89ABCDEF] */
5236 	    "rq       " MI_COL_HDRPAD_STR
5237 	/*   01234567[89ABCDEF] */
5238 	    "wq       " MI_COL_HDRPAD_STR
5239 	/*   01234567[89ABCDEF] */
5240 	    "upcnt mxfrg err name");
5241 	/*   12345 12345 123 xxxxxxxx  */
5242 
5243 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5244 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5245 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5246 		(void) mi_mpprintf(mp,
5247 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5248 		    "%05u %05u %03d %s",
5249 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5250 		    ill->ill_ipif_up_count,
5251 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5252 	}
5253 	rw_exit(&ipst->ips_ill_g_lock);
5254 
5255 	return (0);
5256 }
5257 
5258 /*
5259  * Named Dispatch routine to produce a formatted report on all IPIFs.
5260  * This report is accessed by using the ndd utility to "get" ND variable
5261  * "ip_ipif_status".
5262  */
5263 /* ARGSUSED */
5264 int
5265 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5266 {
5267 	char	buf1[INET6_ADDRSTRLEN];
5268 	char	buf2[INET6_ADDRSTRLEN];
5269 	char	buf3[INET6_ADDRSTRLEN];
5270 	char	buf4[INET6_ADDRSTRLEN];
5271 	char	buf5[INET6_ADDRSTRLEN];
5272 	char	buf6[INET6_ADDRSTRLEN];
5273 	char	buf[LIFNAMSIZ];
5274 	ill_t	*ill;
5275 	ipif_t	*ipif;
5276 	nv_t	*nvp;
5277 	uint64_t flags;
5278 	zoneid_t zoneid;
5279 	ill_walk_context_t ctx;
5280 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
5281 
5282 	(void) mi_mpprintf(mp,
5283 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5284 	    "\tlocal address\n"
5285 	    "\tsrc address\n"
5286 	    "\tsubnet\n"
5287 	    "\tmask\n"
5288 	    "\tbroadcast\n"
5289 	    "\tp-p-dst");
5290 
5291 	ASSERT(q->q_next == NULL);
5292 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5293 
5294 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5295 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5296 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5297 		for (ipif = ill->ill_ipif; ipif != NULL;
5298 		    ipif = ipif->ipif_next) {
5299 			if (zoneid != GLOBAL_ZONEID &&
5300 			    zoneid != ipif->ipif_zoneid &&
5301 			    ipif->ipif_zoneid != ALL_ZONES)
5302 				continue;
5303 
5304 			ipif_get_name(ipif, buf, sizeof (buf));
5305 			(void) mi_mpprintf(mp,
5306 			    MI_COL_PTRFMT_STR
5307 			    "%04u %05u %u/%u/%u %s %d",
5308 			    (void *)ipif,
5309 			    ipif->ipif_metric, ipif->ipif_mtu,
5310 			    ipif->ipif_ib_pkt_count,
5311 			    ipif->ipif_ob_pkt_count,
5312 			    ipif->ipif_fo_pkt_count,
5313 			    buf,
5314 			    ipif->ipif_zoneid);
5315 
5316 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5317 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5318 
5319 		/* Tack on text strings for any flags. */
5320 		nvp = ipif_nv_tbl;
5321 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5322 			if (nvp->nv_value & flags)
5323 				(void) mi_mpprintf_nr(mp, " %s",
5324 				    nvp->nv_name);
5325 		}
5326 		(void) mi_mpprintf(mp,
5327 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5328 		    inet_ntop(AF_INET6,
5329 		    &ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5330 		    inet_ntop(AF_INET6,
5331 		    &ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5332 		    inet_ntop(AF_INET6,
5333 		    &ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5334 		    inet_ntop(AF_INET6,
5335 		    &ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5336 		    inet_ntop(AF_INET6,
5337 		    &ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5338 		    inet_ntop(AF_INET6,
5339 		    &ipif->ipif_v6pp_dst_addr, buf6, sizeof (buf6)));
5340 		}
5341 	}
5342 	rw_exit(&ipst->ips_ill_g_lock);
5343 	return (0);
5344 }
5345 
5346 /*
5347  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5348  * driver.  We construct best guess defaults for lower level information that
5349  * we need.  If an interface is brought up without injection of any overriding
5350  * information from outside, we have to be ready to go with these defaults.
5351  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5352  * we primarely want the dl_provider_style.
5353  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5354  * at which point we assume the other part of the information is valid.
5355  */
5356 void
5357 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5358 {
5359 	uchar_t		*brdcst_addr;
5360 	uint_t		brdcst_addr_length, phys_addr_length;
5361 	t_scalar_t	sap_length;
5362 	dl_info_ack_t	*dlia;
5363 	ip_m_t		*ipm;
5364 	dl_qos_cl_sel1_t *sel1;
5365 	int		min_mtu;
5366 
5367 	ASSERT(IAM_WRITER_ILL(ill));
5368 
5369 	/*
5370 	 * Till the ill is fully up ILL_CHANGING will be set and
5371 	 * the ill is not globally visible. So no need for a lock.
5372 	 */
5373 	dlia = (dl_info_ack_t *)mp->b_rptr;
5374 	ill->ill_mactype = dlia->dl_mac_type;
5375 
5376 	ipm = ip_m_lookup(dlia->dl_mac_type);
5377 	if (ipm == NULL) {
5378 		ipm = ip_m_lookup(DL_OTHER);
5379 		ASSERT(ipm != NULL);
5380 	}
5381 	ill->ill_media = ipm;
5382 
5383 	/*
5384 	 * When the new DLPI stuff is ready we'll pull lengths
5385 	 * from dlia.
5386 	 */
5387 	if (dlia->dl_version == DL_VERSION_2) {
5388 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5389 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5390 		    brdcst_addr_length);
5391 		if (brdcst_addr == NULL) {
5392 			brdcst_addr_length = 0;
5393 		}
5394 		sap_length = dlia->dl_sap_length;
5395 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5396 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5397 		    brdcst_addr_length, sap_length, phys_addr_length));
5398 	} else {
5399 		brdcst_addr_length = 6;
5400 		brdcst_addr = ip_six_byte_all_ones;
5401 		sap_length = -2;
5402 		phys_addr_length = brdcst_addr_length;
5403 	}
5404 
5405 	ill->ill_bcast_addr_length = brdcst_addr_length;
5406 	ill->ill_phys_addr_length = phys_addr_length;
5407 	ill->ill_sap_length = sap_length;
5408 
5409 	/*
5410 	 * Synthetic DLPI types such as SUNW_DL_IPMP specify a zero SDU,
5411 	 * but we must ensure a minimum IP MTU is used since other bits of
5412 	 * IP will fly apart otherwise.
5413 	 */
5414 	min_mtu = ill->ill_isv6 ? IPV6_MIN_MTU : IP_MIN_MTU;
5415 	ill->ill_max_frag  = MAX(min_mtu, dlia->dl_max_sdu);
5416 	ill->ill_max_mtu = ill->ill_max_frag;
5417 
5418 	ill->ill_type = ipm->ip_m_type;
5419 
5420 	if (!ill->ill_dlpi_style_set) {
5421 		if (dlia->dl_provider_style == DL_STYLE2)
5422 			ill->ill_needs_attach = 1;
5423 
5424 		/*
5425 		 * Allocate the first ipif on this ill. We don't delay it
5426 		 * further as ioctl handling assumes atleast one ipif to
5427 		 * be present.
5428 		 *
5429 		 * At this point we don't know whether the ill is v4 or v6.
5430 		 * We will know this whan the SIOCSLIFNAME happens and
5431 		 * the correct value for ill_isv6 will be assigned in
5432 		 * ipif_set_values(). We need to hold the ill lock and
5433 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5434 		 * the wakeup.
5435 		 */
5436 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5437 		    dlia->dl_provider_style != DL_STYLE2, B_TRUE);
5438 		mutex_enter(&ill->ill_lock);
5439 		ASSERT(ill->ill_dlpi_style_set == 0);
5440 		ill->ill_dlpi_style_set = 1;
5441 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5442 		cv_broadcast(&ill->ill_cv);
5443 		mutex_exit(&ill->ill_lock);
5444 		freemsg(mp);
5445 		return;
5446 	}
5447 	ASSERT(ill->ill_ipif != NULL);
5448 	/*
5449 	 * We know whether it is IPv4 or IPv6 now, as this is the
5450 	 * second DL_INFO_ACK we are recieving in response to the
5451 	 * DL_INFO_REQ sent in ipif_set_values.
5452 	 */
5453 	if (ill->ill_isv6)
5454 		ill->ill_sap = IP6_DL_SAP;
5455 	else
5456 		ill->ill_sap = IP_DL_SAP;
5457 	/*
5458 	 * Set ipif_mtu which is used to set the IRE's
5459 	 * ire_max_frag value. The driver could have sent
5460 	 * a different mtu from what it sent last time. No
5461 	 * need to call ipif_mtu_change because IREs have
5462 	 * not yet been created.
5463 	 */
5464 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5465 	/*
5466 	 * Clear all the flags that were set based on ill_bcast_addr_length
5467 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5468 	 * changed now and we need to re-evaluate.
5469 	 */
5470 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5471 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5472 
5473 	/*
5474 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5475 	 * changed now.
5476 	 *
5477 	 * NOTE: The IPMP meta-interface is special-cased because it starts
5478 	 * with no underlying interfaces (and thus an unknown broadcast
5479 	 * address length), but we enforce that an interface is broadcast-
5480 	 * capable as part of allowing it to join a group.
5481 	 */
5482 	if (ill->ill_bcast_addr_length == 0 && !IS_IPMP(ill)) {
5483 		if (ill->ill_resolver_mp != NULL)
5484 			freemsg(ill->ill_resolver_mp);
5485 		if (ill->ill_bcast_mp != NULL)
5486 			freemsg(ill->ill_bcast_mp);
5487 		if (ill->ill_flags & ILLF_XRESOLV)
5488 			ill->ill_net_type = IRE_IF_RESOLVER;
5489 		else
5490 			ill->ill_net_type = IRE_IF_NORESOLVER;
5491 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5492 		    ill->ill_phys_addr_length,
5493 		    ill->ill_sap,
5494 		    ill->ill_sap_length);
5495 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5496 
5497 		if (ill->ill_isv6)
5498 			/*
5499 			 * Note: xresolv interfaces will eventually need NOARP
5500 			 * set here as well, but that will require those
5501 			 * external resolvers to have some knowledge of
5502 			 * that flag and act appropriately. Not to be changed
5503 			 * at present.
5504 			 */
5505 			ill->ill_flags |= ILLF_NONUD;
5506 		else
5507 			ill->ill_flags |= ILLF_NOARP;
5508 
5509 		if (ill->ill_phys_addr_length == 0) {
5510 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5511 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5512 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5513 			} else {
5514 				/* pt-pt supports multicast. */
5515 				ill->ill_flags |= ILLF_MULTICAST;
5516 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5517 			}
5518 		}
5519 	} else {
5520 		ill->ill_net_type = IRE_IF_RESOLVER;
5521 		if (ill->ill_bcast_mp != NULL)
5522 			freemsg(ill->ill_bcast_mp);
5523 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5524 		    ill->ill_bcast_addr_length, ill->ill_sap,
5525 		    ill->ill_sap_length);
5526 		/*
5527 		 * Later detect lack of DLPI driver multicast
5528 		 * capability by catching DL_ENABMULTI errors in
5529 		 * ip_rput_dlpi.
5530 		 */
5531 		ill->ill_flags |= ILLF_MULTICAST;
5532 		if (!ill->ill_isv6)
5533 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5534 	}
5535 
5536 	/* For IPMP, PHYI_IPMP should already be set by ipif_allocate() */
5537 	if (ill->ill_mactype == SUNW_DL_IPMP)
5538 		ASSERT(ill->ill_phyint->phyint_flags & PHYI_IPMP);
5539 
5540 	/* By default an interface does not support any CoS marking */
5541 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5542 
5543 	/*
5544 	 * If we get QoS information in DL_INFO_ACK, the device supports
5545 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5546 	 */
5547 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5548 	    dlia->dl_qos_length);
5549 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5550 		ill->ill_flags |= ILLF_COS_ENABLED;
5551 	}
5552 
5553 	/* Clear any previous error indication. */
5554 	ill->ill_error = 0;
5555 	freemsg(mp);
5556 }
5557 
5558 /*
5559  * Perform various checks to verify that an address would make sense as a
5560  * local, remote, or subnet interface address.
5561  */
5562 static boolean_t
5563 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5564 {
5565 	ipaddr_t	net_mask;
5566 
5567 	/*
5568 	 * Don't allow all zeroes, or all ones, but allow
5569 	 * all ones netmask.
5570 	 */
5571 	if ((net_mask = ip_net_mask(addr)) == 0)
5572 		return (B_FALSE);
5573 	/* A given netmask overrides the "guess" netmask */
5574 	if (subnet_mask != 0)
5575 		net_mask = subnet_mask;
5576 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5577 	    (addr == (addr | ~net_mask)))) {
5578 		return (B_FALSE);
5579 	}
5580 
5581 	/*
5582 	 * Even if the netmask is all ones, we do not allow address to be
5583 	 * 255.255.255.255
5584 	 */
5585 	if (addr == INADDR_BROADCAST)
5586 		return (B_FALSE);
5587 
5588 	if (CLASSD(addr))
5589 		return (B_FALSE);
5590 
5591 	return (B_TRUE);
5592 }
5593 
5594 #define	V6_IPIF_LINKLOCAL(p)	\
5595 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
5596 
5597 /*
5598  * Compare two given ipifs and check if the second one is better than
5599  * the first one using the order of preference (not taking deprecated
5600  * into acount) specified in ipif_lookup_multicast().
5601  */
5602 static boolean_t
5603 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
5604 {
5605 	/* Check the least preferred first. */
5606 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
5607 		/* If both ipifs are the same, use the first one. */
5608 		if (IS_LOOPBACK(new_ipif->ipif_ill))
5609 			return (B_FALSE);
5610 		else
5611 			return (B_TRUE);
5612 	}
5613 
5614 	/* For IPv6, check for link local address. */
5615 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
5616 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5617 		    V6_IPIF_LINKLOCAL(new_ipif)) {
5618 			/* The second one is equal or less preferred. */
5619 			return (B_FALSE);
5620 		} else {
5621 			return (B_TRUE);
5622 		}
5623 	}
5624 
5625 	/* Then check for point to point interface. */
5626 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
5627 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5628 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
5629 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
5630 			return (B_FALSE);
5631 		} else {
5632 			return (B_TRUE);
5633 		}
5634 	}
5635 
5636 	/* old_ipif is a normal interface, so no need to use the new one. */
5637 	return (B_FALSE);
5638 }
5639 
5640 /*
5641  * Find a mulitcast-capable ipif given an IP instance and zoneid.
5642  * The ipif must be up, and its ill must multicast-capable, not
5643  * condemned, not an underlying interface in an IPMP group, and
5644  * not a VNI interface.  Order of preference:
5645  *
5646  * 	1a. normal
5647  * 	1b. normal, but deprecated
5648  * 	2a. point to point
5649  * 	2b. point to point, but deprecated
5650  * 	3a. link local
5651  * 	3b. link local, but deprecated
5652  * 	4. loopback.
5653  */
5654 ipif_t *
5655 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
5656 {
5657 	ill_t			*ill;
5658 	ill_walk_context_t	ctx;
5659 	ipif_t			*ipif;
5660 	ipif_t			*saved_ipif = NULL;
5661 	ipif_t			*dep_ipif = NULL;
5662 
5663 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5664 	if (isv6)
5665 		ill = ILL_START_WALK_V6(&ctx, ipst);
5666 	else
5667 		ill = ILL_START_WALK_V4(&ctx, ipst);
5668 
5669 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5670 		mutex_enter(&ill->ill_lock);
5671 		if (IS_VNI(ill) || IS_UNDER_IPMP(ill) || !ILL_CAN_LOOKUP(ill) ||
5672 		    !(ill->ill_flags & ILLF_MULTICAST)) {
5673 			mutex_exit(&ill->ill_lock);
5674 			continue;
5675 		}
5676 		for (ipif = ill->ill_ipif; ipif != NULL;
5677 		    ipif = ipif->ipif_next) {
5678 			if (zoneid != ipif->ipif_zoneid &&
5679 			    zoneid != ALL_ZONES &&
5680 			    ipif->ipif_zoneid != ALL_ZONES) {
5681 				continue;
5682 			}
5683 			if (!(ipif->ipif_flags & IPIF_UP) ||
5684 			    !IPIF_CAN_LOOKUP(ipif)) {
5685 				continue;
5686 			}
5687 
5688 			/*
5689 			 * Found one candidate.  If it is deprecated,
5690 			 * remember it in dep_ipif.  If it is not deprecated,
5691 			 * remember it in saved_ipif.
5692 			 */
5693 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
5694 				if (dep_ipif == NULL) {
5695 					dep_ipif = ipif;
5696 				} else if (ipif_comp_multi(dep_ipif, ipif,
5697 				    isv6)) {
5698 					/*
5699 					 * If the previous dep_ipif does not
5700 					 * belong to the same ill, we've done
5701 					 * a ipif_refhold() on it.  So we need
5702 					 * to release it.
5703 					 */
5704 					if (dep_ipif->ipif_ill != ill)
5705 						ipif_refrele(dep_ipif);
5706 					dep_ipif = ipif;
5707 				}
5708 				continue;
5709 			}
5710 			if (saved_ipif == NULL) {
5711 				saved_ipif = ipif;
5712 			} else {
5713 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
5714 					if (saved_ipif->ipif_ill != ill)
5715 						ipif_refrele(saved_ipif);
5716 					saved_ipif = ipif;
5717 				}
5718 			}
5719 		}
5720 		/*
5721 		 * Before going to the next ill, do a ipif_refhold() on the
5722 		 * saved ones.
5723 		 */
5724 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
5725 			ipif_refhold_locked(saved_ipif);
5726 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
5727 			ipif_refhold_locked(dep_ipif);
5728 		mutex_exit(&ill->ill_lock);
5729 	}
5730 	rw_exit(&ipst->ips_ill_g_lock);
5731 
5732 	/*
5733 	 * If we have only the saved_ipif, return it.  But if we have both
5734 	 * saved_ipif and dep_ipif, check to see which one is better.
5735 	 */
5736 	if (saved_ipif != NULL) {
5737 		if (dep_ipif != NULL) {
5738 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
5739 				ipif_refrele(saved_ipif);
5740 				return (dep_ipif);
5741 			} else {
5742 				ipif_refrele(dep_ipif);
5743 				return (saved_ipif);
5744 			}
5745 		}
5746 		return (saved_ipif);
5747 	} else {
5748 		return (dep_ipif);
5749 	}
5750 }
5751 
5752 /*
5753  * This function is called when an application does not specify an interface
5754  * to be used for multicast traffic (joining a group/sending data).  It
5755  * calls ire_lookup_multi() to look for an interface route for the
5756  * specified multicast group.  Doing this allows the administrator to add
5757  * prefix routes for multicast to indicate which interface to be used for
5758  * multicast traffic in the above scenario.  The route could be for all
5759  * multicast (224.0/4), for a single multicast group (a /32 route) or
5760  * anything in between.  If there is no such multicast route, we just find
5761  * any multicast capable interface and return it.  The returned ipif
5762  * is refhold'ed.
5763  */
5764 ipif_t *
5765 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5766 {
5767 	ire_t			*ire;
5768 	ipif_t			*ipif;
5769 
5770 	ire = ire_lookup_multi(group, zoneid, ipst);
5771 	if (ire != NULL) {
5772 		ipif = ire->ire_ipif;
5773 		ipif_refhold(ipif);
5774 		ire_refrele(ire);
5775 		return (ipif);
5776 	}
5777 
5778 	return (ipif_lookup_multicast(ipst, zoneid, B_FALSE));
5779 }
5780 
5781 /*
5782  * Look for an ipif with the specified interface address and destination.
5783  * The destination address is used only for matching point-to-point interfaces.
5784  */
5785 ipif_t *
5786 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5787     ipsq_func_t func, int *error, ip_stack_t *ipst)
5788 {
5789 	ipif_t	*ipif;
5790 	ill_t	*ill;
5791 	ill_walk_context_t ctx;
5792 	ipsq_t	*ipsq;
5793 
5794 	if (error != NULL)
5795 		*error = 0;
5796 
5797 	/*
5798 	 * First match all the point-to-point interfaces
5799 	 * before looking at non-point-to-point interfaces.
5800 	 * This is done to avoid returning non-point-to-point
5801 	 * ipif instead of unnumbered point-to-point ipif.
5802 	 */
5803 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5804 	ill = ILL_START_WALK_V4(&ctx, ipst);
5805 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5806 		GRAB_CONN_LOCK(q);
5807 		mutex_enter(&ill->ill_lock);
5808 		for (ipif = ill->ill_ipif; ipif != NULL;
5809 		    ipif = ipif->ipif_next) {
5810 			/* Allow the ipif to be down */
5811 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5812 			    (ipif->ipif_lcl_addr == if_addr) &&
5813 			    (ipif->ipif_pp_dst_addr == dst)) {
5814 				/*
5815 				 * The block comment at the start of ipif_down
5816 				 * explains the use of the macros used below
5817 				 */
5818 				if (IPIF_CAN_LOOKUP(ipif)) {
5819 					ipif_refhold_locked(ipif);
5820 					mutex_exit(&ill->ill_lock);
5821 					RELEASE_CONN_LOCK(q);
5822 					rw_exit(&ipst->ips_ill_g_lock);
5823 					return (ipif);
5824 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5825 					ipsq = ill->ill_phyint->phyint_ipsq;
5826 					mutex_enter(&ipsq->ipsq_lock);
5827 					mutex_enter(&ipsq->ipsq_xop->ipx_lock);
5828 					mutex_exit(&ill->ill_lock);
5829 					rw_exit(&ipst->ips_ill_g_lock);
5830 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5831 					    ill);
5832 					mutex_exit(&ipsq->ipsq_xop->ipx_lock);
5833 					mutex_exit(&ipsq->ipsq_lock);
5834 					RELEASE_CONN_LOCK(q);
5835 					if (error != NULL)
5836 						*error = EINPROGRESS;
5837 					return (NULL);
5838 				}
5839 			}
5840 		}
5841 		mutex_exit(&ill->ill_lock);
5842 		RELEASE_CONN_LOCK(q);
5843 	}
5844 	rw_exit(&ipst->ips_ill_g_lock);
5845 
5846 	/* lookup the ipif based on interface address */
5847 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
5848 	    ipst);
5849 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5850 	return (ipif);
5851 }
5852 
5853 /*
5854  * Common function for ipif_lookup_addr() and ipif_lookup_addr_exact().
5855  */
5856 static ipif_t *
5857 ipif_lookup_addr_common(ipaddr_t addr, ill_t *match_ill, boolean_t match_illgrp,
5858     zoneid_t zoneid, queue_t *q, mblk_t *mp, ipsq_func_t func, int *error,
5859     ip_stack_t *ipst)
5860 {
5861 	ipif_t  *ipif;
5862 	ill_t   *ill;
5863 	boolean_t ptp = B_FALSE;
5864 	ipsq_t	*ipsq;
5865 	ill_walk_context_t	ctx;
5866 
5867 	if (error != NULL)
5868 		*error = 0;
5869 
5870 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5871 	/*
5872 	 * Repeat twice, first based on local addresses and
5873 	 * next time for pointopoint.
5874 	 */
5875 repeat:
5876 	ill = ILL_START_WALK_V4(&ctx, ipst);
5877 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5878 		if (match_ill != NULL && ill != match_ill &&
5879 		    (!match_illgrp || !IS_IN_SAME_ILLGRP(ill, match_ill))) {
5880 			continue;
5881 		}
5882 		GRAB_CONN_LOCK(q);
5883 		mutex_enter(&ill->ill_lock);
5884 		for (ipif = ill->ill_ipif; ipif != NULL;
5885 		    ipif = ipif->ipif_next) {
5886 			if (zoneid != ALL_ZONES &&
5887 			    zoneid != ipif->ipif_zoneid &&
5888 			    ipif->ipif_zoneid != ALL_ZONES)
5889 				continue;
5890 			/* Allow the ipif to be down */
5891 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5892 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5893 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
5894 			    (ipif->ipif_pp_dst_addr == addr))) {
5895 				/*
5896 				 * The block comment at the start of ipif_down
5897 				 * explains the use of the macros used below
5898 				 */
5899 				if (IPIF_CAN_LOOKUP(ipif)) {
5900 					ipif_refhold_locked(ipif);
5901 					mutex_exit(&ill->ill_lock);
5902 					RELEASE_CONN_LOCK(q);
5903 					rw_exit(&ipst->ips_ill_g_lock);
5904 					return (ipif);
5905 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5906 					ipsq = ill->ill_phyint->phyint_ipsq;
5907 					mutex_enter(&ipsq->ipsq_lock);
5908 					mutex_enter(&ipsq->ipsq_xop->ipx_lock);
5909 					mutex_exit(&ill->ill_lock);
5910 					rw_exit(&ipst->ips_ill_g_lock);
5911 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5912 					    ill);
5913 					mutex_exit(&ipsq->ipsq_xop->ipx_lock);
5914 					mutex_exit(&ipsq->ipsq_lock);
5915 					RELEASE_CONN_LOCK(q);
5916 					if (error != NULL)
5917 						*error = EINPROGRESS;
5918 					return (NULL);
5919 				}
5920 			}
5921 		}
5922 		mutex_exit(&ill->ill_lock);
5923 		RELEASE_CONN_LOCK(q);
5924 	}
5925 
5926 	/* If we already did the ptp case, then we are done */
5927 	if (ptp) {
5928 		rw_exit(&ipst->ips_ill_g_lock);
5929 		if (error != NULL)
5930 			*error = ENXIO;
5931 		return (NULL);
5932 	}
5933 	ptp = B_TRUE;
5934 	goto repeat;
5935 }
5936 
5937 /*
5938  * Check if the address exists in the system.
5939  * We don't hold the conn_lock as we will not perform defered ipsqueue
5940  * operation.
5941  */
5942 boolean_t
5943 ip_addr_exists(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
5944 {
5945 	ipif_t  *ipif;
5946 	ill_t   *ill;
5947 	ill_walk_context_t	ctx;
5948 
5949 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5950 
5951 	ill = ILL_START_WALK_V4(&ctx, ipst);
5952 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5953 		mutex_enter(&ill->ill_lock);
5954 		for (ipif = ill->ill_ipif; ipif != NULL;
5955 		    ipif = ipif->ipif_next) {
5956 			if (zoneid != ALL_ZONES &&
5957 			    zoneid != ipif->ipif_zoneid &&
5958 			    ipif->ipif_zoneid != ALL_ZONES)
5959 				continue;
5960 			/* Allow the ipif to be down */
5961 			/*
5962 			 * XXX Different from ipif_lookup_addr(), we don't do
5963 			 * twice lookups. As from bind()'s point of view, we
5964 			 * may return once we find a match.
5965 			 */
5966 			if (((ipif->ipif_lcl_addr == addr) &&
5967 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5968 			    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5969 			    (ipif->ipif_pp_dst_addr == addr))) {
5970 				/*
5971 				 * Allow bind() to be successful even if the
5972 				 * ipif is with IPIF_CHANGING bit set.
5973 				 */
5974 				mutex_exit(&ill->ill_lock);
5975 				rw_exit(&ipst->ips_ill_g_lock);
5976 				return (B_TRUE);
5977 			}
5978 		}
5979 		mutex_exit(&ill->ill_lock);
5980 	}
5981 
5982 	rw_exit(&ipst->ips_ill_g_lock);
5983 	return (B_FALSE);
5984 }
5985 
5986 /*
5987  * Lookup an ipif with the specified address.  For point-to-point links we
5988  * look for matches on either the destination address or the local address,
5989  * but we skip the local address check if IPIF_UNNUMBERED is set.  If the
5990  * `match_ill' argument is non-NULL, the lookup is restricted to that ill
5991  * (or illgrp if `match_ill' is in an IPMP group).
5992  */
5993 ipif_t *
5994 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
5995     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
5996 {
5997 	return (ipif_lookup_addr_common(addr, match_ill, B_TRUE, zoneid, q, mp,
5998 	    func, error, ipst));
5999 }
6000 
6001 /*
6002  * Special abbreviated version of ipif_lookup_addr() that doesn't match
6003  * `match_ill' across the IPMP group.  This function is only needed in some
6004  * corner-cases; almost everything should use ipif_lookup_addr().
6005  */
6006 static ipif_t *
6007 ipif_lookup_addr_exact(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6008 {
6009 	ASSERT(match_ill != NULL);
6010 	return (ipif_lookup_addr_common(addr, match_ill, B_FALSE, ALL_ZONES,
6011 	    NULL, NULL, NULL, NULL, ipst));
6012 }
6013 
6014 /*
6015  * Look for an ipif with the specified address. For point-point links
6016  * we look for matches on either the destination address and the local
6017  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6018  * is set.
6019  * If the `match_ill' argument is non-NULL, the lookup is restricted to that
6020  * ill (or illgrp if `match_ill' is in an IPMP group).
6021  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
6022  */
6023 zoneid_t
6024 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6025 {
6026 	zoneid_t zoneid;
6027 	ipif_t  *ipif;
6028 	ill_t   *ill;
6029 	boolean_t ptp = B_FALSE;
6030 	ill_walk_context_t	ctx;
6031 
6032 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6033 	/*
6034 	 * Repeat twice, first based on local addresses and
6035 	 * next time for pointopoint.
6036 	 */
6037 repeat:
6038 	ill = ILL_START_WALK_V4(&ctx, ipst);
6039 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6040 		if (match_ill != NULL && ill != match_ill &&
6041 		    !IS_IN_SAME_ILLGRP(ill, match_ill)) {
6042 			continue;
6043 		}
6044 		mutex_enter(&ill->ill_lock);
6045 		for (ipif = ill->ill_ipif; ipif != NULL;
6046 		    ipif = ipif->ipif_next) {
6047 			/* Allow the ipif to be down */
6048 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6049 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6050 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6051 			    (ipif->ipif_pp_dst_addr == addr)) &&
6052 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
6053 				zoneid = ipif->ipif_zoneid;
6054 				mutex_exit(&ill->ill_lock);
6055 				rw_exit(&ipst->ips_ill_g_lock);
6056 				/*
6057 				 * If ipif_zoneid was ALL_ZONES then we have
6058 				 * a trusted extensions shared IP address.
6059 				 * In that case GLOBAL_ZONEID works to send.
6060 				 */
6061 				if (zoneid == ALL_ZONES)
6062 					zoneid = GLOBAL_ZONEID;
6063 				return (zoneid);
6064 			}
6065 		}
6066 		mutex_exit(&ill->ill_lock);
6067 	}
6068 
6069 	/* If we already did the ptp case, then we are done */
6070 	if (ptp) {
6071 		rw_exit(&ipst->ips_ill_g_lock);
6072 		return (ALL_ZONES);
6073 	}
6074 	ptp = B_TRUE;
6075 	goto repeat;
6076 }
6077 
6078 /*
6079  * Look for an ipif that matches the specified remote address i.e. the
6080  * ipif that would receive the specified packet.
6081  * First look for directly connected interfaces and then do a recursive
6082  * IRE lookup and pick the first ipif corresponding to the source address in the
6083  * ire.
6084  * Returns: held ipif
6085  */
6086 ipif_t *
6087 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6088 {
6089 	ipif_t	*ipif;
6090 	ire_t	*ire;
6091 	ip_stack_t	*ipst = ill->ill_ipst;
6092 
6093 	ASSERT(!ill->ill_isv6);
6094 
6095 	/*
6096 	 * Someone could be changing this ipif currently or change it
6097 	 * after we return this. Thus  a few packets could use the old
6098 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6099 	 * will atomically be updated or cleaned up with the new value
6100 	 * Thus we don't need a lock to check the flags or other attrs below.
6101 	 */
6102 	mutex_enter(&ill->ill_lock);
6103 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6104 		if (!IPIF_CAN_LOOKUP(ipif))
6105 			continue;
6106 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6107 		    ipif->ipif_zoneid != ALL_ZONES)
6108 			continue;
6109 		/* Allow the ipif to be down */
6110 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6111 			if ((ipif->ipif_pp_dst_addr == addr) ||
6112 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6113 			    ipif->ipif_lcl_addr == addr)) {
6114 				ipif_refhold_locked(ipif);
6115 				mutex_exit(&ill->ill_lock);
6116 				return (ipif);
6117 			}
6118 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6119 			ipif_refhold_locked(ipif);
6120 			mutex_exit(&ill->ill_lock);
6121 			return (ipif);
6122 		}
6123 	}
6124 	mutex_exit(&ill->ill_lock);
6125 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6126 	    NULL, MATCH_IRE_RECURSIVE, ipst);
6127 	if (ire != NULL) {
6128 		/*
6129 		 * The callers of this function wants to know the
6130 		 * interface on which they have to send the replies
6131 		 * back. For IREs that have ire_stq and ire_ipif
6132 		 * derived from different ills, we really don't care
6133 		 * what we return here.
6134 		 */
6135 		ipif = ire->ire_ipif;
6136 		if (ipif != NULL) {
6137 			ipif_refhold(ipif);
6138 			ire_refrele(ire);
6139 			return (ipif);
6140 		}
6141 		ire_refrele(ire);
6142 	}
6143 	/* Pick the first interface */
6144 	ipif = ipif_get_next_ipif(NULL, ill);
6145 	return (ipif);
6146 }
6147 
6148 /*
6149  * This func does not prevent refcnt from increasing. But if
6150  * the caller has taken steps to that effect, then this func
6151  * can be used to determine whether the ill has become quiescent
6152  */
6153 static boolean_t
6154 ill_is_quiescent(ill_t *ill)
6155 {
6156 	ipif_t	*ipif;
6157 
6158 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6159 
6160 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6161 		if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6162 			return (B_FALSE);
6163 		}
6164 	}
6165 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6166 		return (B_FALSE);
6167 	}
6168 	return (B_TRUE);
6169 }
6170 
6171 boolean_t
6172 ill_is_freeable(ill_t *ill)
6173 {
6174 	ipif_t	*ipif;
6175 
6176 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6177 
6178 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6179 		if (ipif->ipif_refcnt != 0 || !IPIF_FREE_OK(ipif)) {
6180 			return (B_FALSE);
6181 		}
6182 	}
6183 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
6184 		return (B_FALSE);
6185 	}
6186 	return (B_TRUE);
6187 }
6188 
6189 /*
6190  * This func does not prevent refcnt from increasing. But if
6191  * the caller has taken steps to that effect, then this func
6192  * can be used to determine whether the ipif has become quiescent
6193  */
6194 static boolean_t
6195 ipif_is_quiescent(ipif_t *ipif)
6196 {
6197 	ill_t *ill;
6198 
6199 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6200 
6201 	if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6202 		return (B_FALSE);
6203 	}
6204 
6205 	ill = ipif->ipif_ill;
6206 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6207 	    ill->ill_logical_down) {
6208 		return (B_TRUE);
6209 	}
6210 
6211 	/* This is the last ipif going down or being deleted on this ill */
6212 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6213 		return (B_FALSE);
6214 	}
6215 
6216 	return (B_TRUE);
6217 }
6218 
6219 /*
6220  * return true if the ipif can be destroyed: the ipif has to be quiescent
6221  * with zero references from ire/nce/ilm to it.
6222  */
6223 static boolean_t
6224 ipif_is_freeable(ipif_t *ipif)
6225 {
6226 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6227 	ASSERT(ipif->ipif_id != 0);
6228 	return (ipif->ipif_refcnt == 0 && IPIF_FREE_OK(ipif));
6229 }
6230 
6231 /*
6232  * The ipif/ill/ire has been refreled. Do the tail processing.
6233  * Determine if the ipif or ill in question has become quiescent and if so
6234  * wakeup close and/or restart any queued pending ioctl that is waiting
6235  * for the ipif_down (or ill_down)
6236  */
6237 void
6238 ipif_ill_refrele_tail(ill_t *ill)
6239 {
6240 	mblk_t	*mp;
6241 	conn_t	*connp;
6242 	ipsq_t	*ipsq;
6243 	ipxop_t	*ipx;
6244 	ipif_t	*ipif;
6245 	dl_notify_ind_t *dlindp;
6246 
6247 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6248 
6249 	if ((ill->ill_state_flags & ILL_CONDEMNED) && ill_is_freeable(ill)) {
6250 		/* ip_modclose() may be waiting */
6251 		cv_broadcast(&ill->ill_cv);
6252 	}
6253 
6254 	ipsq = ill->ill_phyint->phyint_ipsq;
6255 	mutex_enter(&ipsq->ipsq_lock);
6256 	ipx = ipsq->ipsq_xop;
6257 	mutex_enter(&ipx->ipx_lock);
6258 	if (ipx->ipx_waitfor == 0)	/* no one's waiting; bail */
6259 		goto unlock;
6260 
6261 	ASSERT(ipx->ipx_pending_mp != NULL && ipx->ipx_pending_ipif != NULL);
6262 
6263 	ipif = ipx->ipx_pending_ipif;
6264 	if (ipif->ipif_ill != ill) 	/* wait is for another ill; bail */
6265 		goto unlock;
6266 
6267 	switch (ipx->ipx_waitfor) {
6268 	case IPIF_DOWN:
6269 		if (!ipif_is_quiescent(ipif))
6270 			goto unlock;
6271 		break;
6272 	case IPIF_FREE:
6273 		if (!ipif_is_freeable(ipif))
6274 			goto unlock;
6275 		break;
6276 	case ILL_DOWN:
6277 		if (!ill_is_quiescent(ill))
6278 			goto unlock;
6279 		break;
6280 	case ILL_FREE:
6281 		/*
6282 		 * ILL_FREE is only for loopback; normal ill teardown waits
6283 		 * synchronously in ip_modclose() without using ipx_waitfor,
6284 		 * handled by the cv_broadcast() at the top of this function.
6285 		 */
6286 		if (!ill_is_freeable(ill))
6287 			goto unlock;
6288 		break;
6289 	default:
6290 		cmn_err(CE_PANIC, "ipsq: %p unknown ipx_waitfor %d\n",
6291 		    (void *)ipsq, ipx->ipx_waitfor);
6292 	}
6293 
6294 	ill_refhold_locked(ill);	/* for qwriter_ip() call below */
6295 	mutex_exit(&ipx->ipx_lock);
6296 	mp = ipsq_pending_mp_get(ipsq, &connp);
6297 	mutex_exit(&ipsq->ipsq_lock);
6298 	mutex_exit(&ill->ill_lock);
6299 
6300 	ASSERT(mp != NULL);
6301 	/*
6302 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6303 	 * we can only get here when the current operation decides it
6304 	 * it needs to quiesce via ipsq_pending_mp_add().
6305 	 */
6306 	switch (mp->b_datap->db_type) {
6307 	case M_PCPROTO:
6308 	case M_PROTO:
6309 		/*
6310 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6311 		 */
6312 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6313 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6314 
6315 		switch (dlindp->dl_notification) {
6316 		case DL_NOTE_PHYS_ADDR:
6317 			qwriter_ip(ill, ill->ill_rq, mp,
6318 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6319 			return;
6320 		default:
6321 			ASSERT(0);
6322 			ill_refrele(ill);
6323 		}
6324 		break;
6325 
6326 	case M_ERROR:
6327 	case M_HANGUP:
6328 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6329 		    B_TRUE);
6330 		return;
6331 
6332 	case M_IOCTL:
6333 	case M_IOCDATA:
6334 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6335 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6336 		return;
6337 
6338 	default:
6339 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6340 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6341 	}
6342 	return;
6343 unlock:
6344 	mutex_exit(&ipsq->ipsq_lock);
6345 	mutex_exit(&ipx->ipx_lock);
6346 	mutex_exit(&ill->ill_lock);
6347 }
6348 
6349 #ifdef DEBUG
6350 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6351 static void
6352 th_trace_rrecord(th_trace_t *th_trace)
6353 {
6354 	tr_buf_t *tr_buf;
6355 	uint_t lastref;
6356 
6357 	lastref = th_trace->th_trace_lastref;
6358 	lastref++;
6359 	if (lastref == TR_BUF_MAX)
6360 		lastref = 0;
6361 	th_trace->th_trace_lastref = lastref;
6362 	tr_buf = &th_trace->th_trbuf[lastref];
6363 	tr_buf->tr_time = lbolt;
6364 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
6365 }
6366 
6367 static void
6368 th_trace_free(void *value)
6369 {
6370 	th_trace_t *th_trace = value;
6371 
6372 	ASSERT(th_trace->th_refcnt == 0);
6373 	kmem_free(th_trace, sizeof (*th_trace));
6374 }
6375 
6376 /*
6377  * Find or create the per-thread hash table used to track object references.
6378  * The ipst argument is NULL if we shouldn't allocate.
6379  *
6380  * Accesses per-thread data, so there's no need to lock here.
6381  */
6382 static mod_hash_t *
6383 th_trace_gethash(ip_stack_t *ipst)
6384 {
6385 	th_hash_t *thh;
6386 
6387 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
6388 		mod_hash_t *mh;
6389 		char name[256];
6390 		size_t objsize, rshift;
6391 		int retv;
6392 
6393 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
6394 			return (NULL);
6395 		(void) snprintf(name, sizeof (name), "th_trace_%p",
6396 		    (void *)curthread);
6397 
6398 		/*
6399 		 * We use mod_hash_create_extended here rather than the more
6400 		 * obvious mod_hash_create_ptrhash because the latter has a
6401 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
6402 		 * block.
6403 		 */
6404 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
6405 		    MAX(sizeof (ire_t), sizeof (nce_t)));
6406 		rshift = highbit(objsize);
6407 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
6408 		    th_trace_free, mod_hash_byptr, (void *)rshift,
6409 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
6410 		if (mh == NULL) {
6411 			kmem_free(thh, sizeof (*thh));
6412 			return (NULL);
6413 		}
6414 		thh->thh_hash = mh;
6415 		thh->thh_ipst = ipst;
6416 		/*
6417 		 * We trace ills, ipifs, ires, and nces.  All of these are
6418 		 * per-IP-stack, so the lock on the thread list is as well.
6419 		 */
6420 		rw_enter(&ip_thread_rwlock, RW_WRITER);
6421 		list_insert_tail(&ip_thread_list, thh);
6422 		rw_exit(&ip_thread_rwlock);
6423 		retv = tsd_set(ip_thread_data, thh);
6424 		ASSERT(retv == 0);
6425 	}
6426 	return (thh != NULL ? thh->thh_hash : NULL);
6427 }
6428 
6429 boolean_t
6430 th_trace_ref(const void *obj, ip_stack_t *ipst)
6431 {
6432 	th_trace_t *th_trace;
6433 	mod_hash_t *mh;
6434 	mod_hash_val_t val;
6435 
6436 	if ((mh = th_trace_gethash(ipst)) == NULL)
6437 		return (B_FALSE);
6438 
6439 	/*
6440 	 * Attempt to locate the trace buffer for this obj and thread.
6441 	 * If it does not exist, then allocate a new trace buffer and
6442 	 * insert into the hash.
6443 	 */
6444 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
6445 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
6446 		if (th_trace == NULL)
6447 			return (B_FALSE);
6448 
6449 		th_trace->th_id = curthread;
6450 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
6451 		    (mod_hash_val_t)th_trace) != 0) {
6452 			kmem_free(th_trace, sizeof (th_trace_t));
6453 			return (B_FALSE);
6454 		}
6455 	} else {
6456 		th_trace = (th_trace_t *)val;
6457 	}
6458 
6459 	ASSERT(th_trace->th_refcnt >= 0 &&
6460 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
6461 
6462 	th_trace->th_refcnt++;
6463 	th_trace_rrecord(th_trace);
6464 	return (B_TRUE);
6465 }
6466 
6467 /*
6468  * For the purpose of tracing a reference release, we assume that global
6469  * tracing is always on and that the same thread initiated the reference hold
6470  * is releasing.
6471  */
6472 void
6473 th_trace_unref(const void *obj)
6474 {
6475 	int retv;
6476 	mod_hash_t *mh;
6477 	th_trace_t *th_trace;
6478 	mod_hash_val_t val;
6479 
6480 	mh = th_trace_gethash(NULL);
6481 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
6482 	ASSERT(retv == 0);
6483 	th_trace = (th_trace_t *)val;
6484 
6485 	ASSERT(th_trace->th_refcnt > 0);
6486 	th_trace->th_refcnt--;
6487 	th_trace_rrecord(th_trace);
6488 }
6489 
6490 /*
6491  * If tracing has been disabled, then we assume that the reference counts are
6492  * now useless, and we clear them out before destroying the entries.
6493  */
6494 void
6495 th_trace_cleanup(const void *obj, boolean_t trace_disable)
6496 {
6497 	th_hash_t	*thh;
6498 	mod_hash_t	*mh;
6499 	mod_hash_val_t	val;
6500 	th_trace_t	*th_trace;
6501 	int		retv;
6502 
6503 	rw_enter(&ip_thread_rwlock, RW_READER);
6504 	for (thh = list_head(&ip_thread_list); thh != NULL;
6505 	    thh = list_next(&ip_thread_list, thh)) {
6506 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
6507 		    &val) == 0) {
6508 			th_trace = (th_trace_t *)val;
6509 			if (trace_disable)
6510 				th_trace->th_refcnt = 0;
6511 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
6512 			ASSERT(retv == 0);
6513 		}
6514 	}
6515 	rw_exit(&ip_thread_rwlock);
6516 }
6517 
6518 void
6519 ipif_trace_ref(ipif_t *ipif)
6520 {
6521 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6522 
6523 	if (ipif->ipif_trace_disable)
6524 		return;
6525 
6526 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
6527 		ipif->ipif_trace_disable = B_TRUE;
6528 		ipif_trace_cleanup(ipif);
6529 	}
6530 }
6531 
6532 void
6533 ipif_untrace_ref(ipif_t *ipif)
6534 {
6535 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6536 
6537 	if (!ipif->ipif_trace_disable)
6538 		th_trace_unref(ipif);
6539 }
6540 
6541 void
6542 ill_trace_ref(ill_t *ill)
6543 {
6544 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6545 
6546 	if (ill->ill_trace_disable)
6547 		return;
6548 
6549 	if (!th_trace_ref(ill, ill->ill_ipst)) {
6550 		ill->ill_trace_disable = B_TRUE;
6551 		ill_trace_cleanup(ill);
6552 	}
6553 }
6554 
6555 void
6556 ill_untrace_ref(ill_t *ill)
6557 {
6558 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6559 
6560 	if (!ill->ill_trace_disable)
6561 		th_trace_unref(ill);
6562 }
6563 
6564 /*
6565  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
6566  * failure, ipif_trace_disable is set.
6567  */
6568 static void
6569 ipif_trace_cleanup(const ipif_t *ipif)
6570 {
6571 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
6572 }
6573 
6574 /*
6575  * Called when ill is unplumbed or when memory alloc fails.  Note that on
6576  * failure, ill_trace_disable is set.
6577  */
6578 static void
6579 ill_trace_cleanup(const ill_t *ill)
6580 {
6581 	th_trace_cleanup(ill, ill->ill_trace_disable);
6582 }
6583 #endif /* DEBUG */
6584 
6585 void
6586 ipif_refhold_locked(ipif_t *ipif)
6587 {
6588 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6589 	ipif->ipif_refcnt++;
6590 	IPIF_TRACE_REF(ipif);
6591 }
6592 
6593 void
6594 ipif_refhold(ipif_t *ipif)
6595 {
6596 	ill_t	*ill;
6597 
6598 	ill = ipif->ipif_ill;
6599 	mutex_enter(&ill->ill_lock);
6600 	ipif->ipif_refcnt++;
6601 	IPIF_TRACE_REF(ipif);
6602 	mutex_exit(&ill->ill_lock);
6603 }
6604 
6605 /*
6606  * Must not be called while holding any locks. Otherwise if this is
6607  * the last reference to be released there is a chance of recursive mutex
6608  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6609  * to restart an ioctl.
6610  */
6611 void
6612 ipif_refrele(ipif_t *ipif)
6613 {
6614 	ill_t	*ill;
6615 
6616 	ill = ipif->ipif_ill;
6617 
6618 	mutex_enter(&ill->ill_lock);
6619 	ASSERT(ipif->ipif_refcnt != 0);
6620 	ipif->ipif_refcnt--;
6621 	IPIF_UNTRACE_REF(ipif);
6622 	if (ipif->ipif_refcnt != 0) {
6623 		mutex_exit(&ill->ill_lock);
6624 		return;
6625 	}
6626 
6627 	/* Drops the ill_lock */
6628 	ipif_ill_refrele_tail(ill);
6629 }
6630 
6631 ipif_t *
6632 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6633 {
6634 	ipif_t	*ipif;
6635 
6636 	mutex_enter(&ill->ill_lock);
6637 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6638 	    ipif != NULL; ipif = ipif->ipif_next) {
6639 		if (!IPIF_CAN_LOOKUP(ipif))
6640 			continue;
6641 		ipif_refhold_locked(ipif);
6642 		mutex_exit(&ill->ill_lock);
6643 		return (ipif);
6644 	}
6645 	mutex_exit(&ill->ill_lock);
6646 	return (NULL);
6647 }
6648 
6649 /*
6650  * TODO: make this table extendible at run time
6651  * Return a pointer to the mac type info for 'mac_type'
6652  */
6653 static ip_m_t *
6654 ip_m_lookup(t_uscalar_t mac_type)
6655 {
6656 	ip_m_t	*ipm;
6657 
6658 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6659 		if (ipm->ip_m_mac_type == mac_type)
6660 			return (ipm);
6661 	return (NULL);
6662 }
6663 
6664 /*
6665  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6666  * ipif_arg is passed in to associate it with the correct interface.
6667  * We may need to restart this operation if the ipif cannot be looked up
6668  * due to an exclusive operation that is currently in progress. The restart
6669  * entry point is specified by 'func'
6670  */
6671 int
6672 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6673     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ire_t **ire_arg,
6674     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
6675     struct rtsa_s *sp, ip_stack_t *ipst)
6676 {
6677 	ire_t	*ire;
6678 	ire_t	*gw_ire = NULL;
6679 	ipif_t	*ipif = NULL;
6680 	boolean_t ipif_refheld = B_FALSE;
6681 	uint_t	type;
6682 	int	match_flags = MATCH_IRE_TYPE;
6683 	int	error;
6684 	tsol_gc_t *gc = NULL;
6685 	tsol_gcgrp_t *gcgrp = NULL;
6686 	boolean_t gcgrp_xtraref = B_FALSE;
6687 
6688 	ip1dbg(("ip_rt_add:"));
6689 
6690 	if (ire_arg != NULL)
6691 		*ire_arg = NULL;
6692 
6693 	/*
6694 	 * If this is the case of RTF_HOST being set, then we set the netmask
6695 	 * to all ones (regardless if one was supplied).
6696 	 */
6697 	if (flags & RTF_HOST)
6698 		mask = IP_HOST_MASK;
6699 
6700 	/*
6701 	 * Prevent routes with a zero gateway from being created (since
6702 	 * interfaces can currently be plumbed and brought up no assigned
6703 	 * address).
6704 	 */
6705 	if (gw_addr == 0)
6706 		return (ENETUNREACH);
6707 	/*
6708 	 * Get the ipif, if any, corresponding to the gw_addr
6709 	 */
6710 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &error,
6711 	    ipst);
6712 	if (ipif != NULL) {
6713 		if (IS_VNI(ipif->ipif_ill)) {
6714 			ipif_refrele(ipif);
6715 			return (EINVAL);
6716 		}
6717 		ipif_refheld = B_TRUE;
6718 	} else if (error == EINPROGRESS) {
6719 		ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6720 		return (EINPROGRESS);
6721 	} else {
6722 		error = 0;
6723 	}
6724 
6725 	if (ipif != NULL) {
6726 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6727 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6728 	} else {
6729 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6730 	}
6731 
6732 	/*
6733 	 * GateD will attempt to create routes with a loopback interface
6734 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6735 	 * these routes to be added, but create them as interface routes
6736 	 * since the gateway is an interface address.
6737 	 */
6738 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6739 		flags &= ~RTF_GATEWAY;
6740 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6741 		    mask == IP_HOST_MASK) {
6742 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6743 			    ALL_ZONES, NULL, match_flags, ipst);
6744 			if (ire != NULL) {
6745 				ire_refrele(ire);
6746 				if (ipif_refheld)
6747 					ipif_refrele(ipif);
6748 				return (EEXIST);
6749 			}
6750 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6751 			    "for 0x%x\n", (void *)ipif,
6752 			    ipif->ipif_ire_type,
6753 			    ntohl(ipif->ipif_lcl_addr)));
6754 			ire = ire_create(
6755 			    (uchar_t *)&dst_addr,	/* dest address */
6756 			    (uchar_t *)&mask,		/* mask */
6757 			    (uchar_t *)&ipif->ipif_src_addr,
6758 			    NULL,			/* no gateway */
6759 			    &ipif->ipif_mtu,
6760 			    NULL,
6761 			    ipif->ipif_rq,		/* recv-from queue */
6762 			    NULL,			/* no send-to queue */
6763 			    ipif->ipif_ire_type,	/* LOOPBACK */
6764 			    ipif,
6765 			    0,
6766 			    0,
6767 			    0,
6768 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6769 			    RTF_PRIVATE : 0,
6770 			    &ire_uinfo_null,
6771 			    NULL,
6772 			    NULL,
6773 			    ipst);
6774 
6775 			if (ire == NULL) {
6776 				if (ipif_refheld)
6777 					ipif_refrele(ipif);
6778 				return (ENOMEM);
6779 			}
6780 			error = ire_add(&ire, q, mp, func, B_FALSE);
6781 			if (error == 0)
6782 				goto save_ire;
6783 			if (ipif_refheld)
6784 				ipif_refrele(ipif);
6785 			return (error);
6786 
6787 		}
6788 	}
6789 
6790 	/*
6791 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6792 	 * and the gateway address provided is one of the system's interface
6793 	 * addresses.  By using the routing socket interface and supplying an
6794 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6795 	 * specifying an interface route to be created is available which uses
6796 	 * the interface index that specifies the outgoing interface rather than
6797 	 * the address of an outgoing interface (which may not be able to
6798 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6799 	 * flag, routes can be specified which not only specify the next-hop to
6800 	 * be used when routing to a certain prefix, but also which outgoing
6801 	 * interface should be used.
6802 	 *
6803 	 * Previously, interfaces would have unique addresses assigned to them
6804 	 * and so the address assigned to a particular interface could be used
6805 	 * to identify a particular interface.  One exception to this was the
6806 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6807 	 *
6808 	 * With the advent of IPv6 and its link-local addresses, this
6809 	 * restriction was relaxed and interfaces could share addresses between
6810 	 * themselves.  In fact, typically all of the link-local interfaces on
6811 	 * an IPv6 node or router will have the same link-local address.  In
6812 	 * order to differentiate between these interfaces, the use of an
6813 	 * interface index is necessary and this index can be carried inside a
6814 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6815 	 * of using the interface index, however, is that all of the ipif's that
6816 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6817 	 * cannot be used to differentiate between ipif's (or logical
6818 	 * interfaces) that belong to the same ill (physical interface).
6819 	 *
6820 	 * For example, in the following case involving IPv4 interfaces and
6821 	 * logical interfaces
6822 	 *
6823 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6824 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6825 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6826 	 *
6827 	 * the ipif's corresponding to each of these interface routes can be
6828 	 * uniquely identified by the "gateway" (actually interface address).
6829 	 *
6830 	 * In this case involving multiple IPv6 default routes to a particular
6831 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6832 	 * default route is of interest:
6833 	 *
6834 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6835 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6836 	 */
6837 
6838 	/* RTF_GATEWAY not set */
6839 	if (!(flags & RTF_GATEWAY)) {
6840 		queue_t	*stq;
6841 
6842 		if (sp != NULL) {
6843 			ip2dbg(("ip_rt_add: gateway security attributes "
6844 			    "cannot be set with interface route\n"));
6845 			if (ipif_refheld)
6846 				ipif_refrele(ipif);
6847 			return (EINVAL);
6848 		}
6849 
6850 		/*
6851 		 * As the interface index specified with the RTA_IFP sockaddr is
6852 		 * the same for all ipif's off of an ill, the matching logic
6853 		 * below uses MATCH_IRE_ILL if such an index was specified.
6854 		 * This means that routes sharing the same prefix when added
6855 		 * using a RTA_IFP sockaddr must have distinct interface
6856 		 * indices (namely, they must be on distinct ill's).
6857 		 *
6858 		 * On the other hand, since the gateway address will usually be
6859 		 * different for each ipif on the system, the matching logic
6860 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6861 		 * route.  This means that interface routes for the same prefix
6862 		 * can be created if they belong to distinct ipif's and if a
6863 		 * RTA_IFP sockaddr is not present.
6864 		 */
6865 		if (ipif_arg != NULL) {
6866 			if (ipif_refheld)  {
6867 				ipif_refrele(ipif);
6868 				ipif_refheld = B_FALSE;
6869 			}
6870 			ipif = ipif_arg;
6871 			match_flags |= MATCH_IRE_ILL;
6872 		} else {
6873 			/*
6874 			 * Check the ipif corresponding to the gw_addr
6875 			 */
6876 			if (ipif == NULL)
6877 				return (ENETUNREACH);
6878 			match_flags |= MATCH_IRE_IPIF;
6879 		}
6880 		ASSERT(ipif != NULL);
6881 
6882 		/*
6883 		 * We check for an existing entry at this point.
6884 		 *
6885 		 * Since a netmask isn't passed in via the ioctl interface
6886 		 * (SIOCADDRT), we don't check for a matching netmask in that
6887 		 * case.
6888 		 */
6889 		if (!ioctl_msg)
6890 			match_flags |= MATCH_IRE_MASK;
6891 		ire = ire_ftable_lookup(dst_addr, mask, 0, IRE_INTERFACE, ipif,
6892 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
6893 		if (ire != NULL) {
6894 			ire_refrele(ire);
6895 			if (ipif_refheld)
6896 				ipif_refrele(ipif);
6897 			return (EEXIST);
6898 		}
6899 
6900 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
6901 		    ? ipif->ipif_rq : ipif->ipif_wq;
6902 
6903 		/*
6904 		 * Create a copy of the IRE_LOOPBACK,
6905 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
6906 		 * the modified address and netmask.
6907 		 */
6908 		ire = ire_create(
6909 		    (uchar_t *)&dst_addr,
6910 		    (uint8_t *)&mask,
6911 		    (uint8_t *)&ipif->ipif_src_addr,
6912 		    NULL,
6913 		    &ipif->ipif_mtu,
6914 		    NULL,
6915 		    NULL,
6916 		    stq,
6917 		    ipif->ipif_net_type,
6918 		    ipif,
6919 		    0,
6920 		    0,
6921 		    0,
6922 		    flags,
6923 		    &ire_uinfo_null,
6924 		    NULL,
6925 		    NULL,
6926 		    ipst);
6927 		if (ire == NULL) {
6928 			if (ipif_refheld)
6929 				ipif_refrele(ipif);
6930 			return (ENOMEM);
6931 		}
6932 
6933 		/*
6934 		 * Some software (for example, GateD and Sun Cluster) attempts
6935 		 * to create (what amount to) IRE_PREFIX routes with the
6936 		 * loopback address as the gateway.  This is primarily done to
6937 		 * set up prefixes with the RTF_REJECT flag set (for example,
6938 		 * when generating aggregate routes.)
6939 		 *
6940 		 * If the IRE type (as defined by ipif->ipif_net_type) is
6941 		 * IRE_LOOPBACK, then we map the request into a
6942 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
6943 		 * these interface routes, by definition, can only be that.
6944 		 *
6945 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
6946 		 * routine, but rather using ire_create() directly.
6947 		 *
6948 		 */
6949 		if (ipif->ipif_net_type == IRE_LOOPBACK) {
6950 			ire->ire_type = IRE_IF_NORESOLVER;
6951 			ire->ire_flags |= RTF_BLACKHOLE;
6952 		}
6953 
6954 		error = ire_add(&ire, q, mp, func, B_FALSE);
6955 		if (error == 0)
6956 			goto save_ire;
6957 
6958 		/*
6959 		 * In the result of failure, ire_add() will have already
6960 		 * deleted the ire in question, so there is no need to
6961 		 * do that here.
6962 		 */
6963 		if (ipif_refheld)
6964 			ipif_refrele(ipif);
6965 		return (error);
6966 	}
6967 	if (ipif_refheld) {
6968 		ipif_refrele(ipif);
6969 		ipif_refheld = B_FALSE;
6970 	}
6971 
6972 	/*
6973 	 * Get an interface IRE for the specified gateway.
6974 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
6975 	 * gateway, it is currently unreachable and we fail the request
6976 	 * accordingly.
6977 	 */
6978 	ipif = ipif_arg;
6979 	if (ipif_arg != NULL)
6980 		match_flags |= MATCH_IRE_ILL;
6981 again:
6982 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
6983 	    ALL_ZONES, 0, NULL, match_flags, ipst);
6984 	if (gw_ire == NULL) {
6985 		/*
6986 		 * With IPMP, we allow host routes to influence in.mpathd's
6987 		 * target selection.  However, if the test addresses are on
6988 		 * their own network, the above lookup will fail since the
6989 		 * underlying IRE_INTERFACEs are marked hidden.  So allow
6990 		 * hidden test IREs to be found and try again.
6991 		 */
6992 		if (!(match_flags & MATCH_IRE_MARK_TESTHIDDEN))  {
6993 			match_flags |= MATCH_IRE_MARK_TESTHIDDEN;
6994 			goto again;
6995 		}
6996 		return (ENETUNREACH);
6997 	}
6998 
6999 	/*
7000 	 * We create one of three types of IREs as a result of this request
7001 	 * based on the netmask.  A netmask of all ones (which is automatically
7002 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7003 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7004 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7005 	 * destination prefix.
7006 	 */
7007 	if (mask == IP_HOST_MASK)
7008 		type = IRE_HOST;
7009 	else if (mask == 0)
7010 		type = IRE_DEFAULT;
7011 	else
7012 		type = IRE_PREFIX;
7013 
7014 	/* check for a duplicate entry */
7015 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7016 	    NULL, ALL_ZONES, 0, NULL,
7017 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7018 	if (ire != NULL) {
7019 		ire_refrele(gw_ire);
7020 		ire_refrele(ire);
7021 		return (EEXIST);
7022 	}
7023 
7024 	/* Security attribute exists */
7025 	if (sp != NULL) {
7026 		tsol_gcgrp_addr_t ga;
7027 
7028 		/* find or create the gateway credentials group */
7029 		ga.ga_af = AF_INET;
7030 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7031 
7032 		/* we hold reference to it upon success */
7033 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7034 		if (gcgrp == NULL) {
7035 			ire_refrele(gw_ire);
7036 			return (ENOMEM);
7037 		}
7038 
7039 		/*
7040 		 * Create and add the security attribute to the group; a
7041 		 * reference to the group is made upon allocating a new
7042 		 * entry successfully.  If it finds an already-existing
7043 		 * entry for the security attribute in the group, it simply
7044 		 * returns it and no new reference is made to the group.
7045 		 */
7046 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7047 		if (gc == NULL) {
7048 			/* release reference held by gcgrp_lookup */
7049 			GCGRP_REFRELE(gcgrp);
7050 			ire_refrele(gw_ire);
7051 			return (ENOMEM);
7052 		}
7053 	}
7054 
7055 	/* Create the IRE. */
7056 	ire = ire_create(
7057 	    (uchar_t *)&dst_addr,		/* dest address */
7058 	    (uchar_t *)&mask,			/* mask */
7059 	    /* src address assigned by the caller? */
7060 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7061 	    (flags & RTF_SETSRC)) ?  &src_addr : NULL),
7062 	    (uchar_t *)&gw_addr,		/* gateway address */
7063 	    &gw_ire->ire_max_frag,
7064 	    NULL,				/* no src nce */
7065 	    NULL,				/* no recv-from queue */
7066 	    NULL,				/* no send-to queue */
7067 	    (ushort_t)type,			/* IRE type */
7068 	    ipif_arg,
7069 	    0,
7070 	    0,
7071 	    0,
7072 	    flags,
7073 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7074 	    gc,					/* security attribute */
7075 	    NULL,
7076 	    ipst);
7077 
7078 	/*
7079 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7080 	 * reference to the 'gcgrp'. We can now release the extra reference
7081 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7082 	 */
7083 	if (gcgrp_xtraref)
7084 		GCGRP_REFRELE(gcgrp);
7085 	if (ire == NULL) {
7086 		if (gc != NULL)
7087 			GC_REFRELE(gc);
7088 		ire_refrele(gw_ire);
7089 		return (ENOMEM);
7090 	}
7091 
7092 	/*
7093 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7094 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7095 	 */
7096 
7097 	/* Add the new IRE. */
7098 	error = ire_add(&ire, q, mp, func, B_FALSE);
7099 	if (error != 0) {
7100 		/*
7101 		 * In the result of failure, ire_add() will have already
7102 		 * deleted the ire in question, so there is no need to
7103 		 * do that here.
7104 		 */
7105 		ire_refrele(gw_ire);
7106 		return (error);
7107 	}
7108 
7109 	if (flags & RTF_MULTIRT) {
7110 		/*
7111 		 * Invoke the CGTP (multirouting) filtering module
7112 		 * to add the dst address in the filtering database.
7113 		 * Replicated inbound packets coming from that address
7114 		 * will be filtered to discard the duplicates.
7115 		 * It is not necessary to call the CGTP filter hook
7116 		 * when the dst address is a broadcast or multicast,
7117 		 * because an IP source address cannot be a broadcast
7118 		 * or a multicast.
7119 		 */
7120 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7121 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7122 		if (ire_dst != NULL) {
7123 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
7124 			ire_refrele(ire_dst);
7125 			goto save_ire;
7126 		}
7127 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
7128 		    !CLASSD(ire->ire_addr)) {
7129 			int res = ipst->ips_ip_cgtp_filter_ops->cfo_add_dest_v4(
7130 			    ipst->ips_netstack->netstack_stackid,
7131 			    ire->ire_addr,
7132 			    ire->ire_gateway_addr,
7133 			    ire->ire_src_addr,
7134 			    gw_ire->ire_src_addr);
7135 			if (res != 0) {
7136 				ire_refrele(gw_ire);
7137 				ire_delete(ire);
7138 				return (res);
7139 			}
7140 		}
7141 	}
7142 
7143 	/*
7144 	 * Now that the prefix IRE entry has been created, delete any
7145 	 * existing gateway IRE cache entries as well as any IRE caches
7146 	 * using the gateway, and force them to be created through
7147 	 * ip_newroute.
7148 	 */
7149 	if (gc != NULL) {
7150 		ASSERT(gcgrp != NULL);
7151 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7152 	}
7153 
7154 save_ire:
7155 	if (gw_ire != NULL) {
7156 		ire_refrele(gw_ire);
7157 	}
7158 	if (ipif != NULL) {
7159 		/*
7160 		 * Save enough information so that we can recreate the IRE if
7161 		 * the interface goes down and then up.  The metrics associated
7162 		 * with the route will be saved as well when rts_setmetrics() is
7163 		 * called after the IRE has been created.  In the case where
7164 		 * memory cannot be allocated, none of this information will be
7165 		 * saved.
7166 		 */
7167 		ipif_save_ire(ipif, ire);
7168 	}
7169 	if (ioctl_msg)
7170 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7171 	if (ire_arg != NULL) {
7172 		/*
7173 		 * Store the ire that was successfully added into where ire_arg
7174 		 * points to so that callers don't have to look it up
7175 		 * themselves (but they are responsible for ire_refrele()ing
7176 		 * the ire when they are finished with it).
7177 		 */
7178 		*ire_arg = ire;
7179 	} else {
7180 		ire_refrele(ire);		/* Held in ire_add */
7181 	}
7182 	if (ipif_refheld)
7183 		ipif_refrele(ipif);
7184 	return (0);
7185 }
7186 
7187 /*
7188  * ip_rt_delete is called to delete an IPv4 route.
7189  * ipif_arg is passed in to associate it with the correct interface.
7190  * We may need to restart this operation if the ipif cannot be looked up
7191  * due to an exclusive operation that is currently in progress. The restart
7192  * entry point is specified by 'func'
7193  */
7194 /* ARGSUSED4 */
7195 int
7196 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7197     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, boolean_t ioctl_msg,
7198     queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst)
7199 {
7200 	ire_t	*ire = NULL;
7201 	ipif_t	*ipif;
7202 	boolean_t ipif_refheld = B_FALSE;
7203 	uint_t	type;
7204 	uint_t	match_flags = MATCH_IRE_TYPE;
7205 	int	err = 0;
7206 
7207 	ip1dbg(("ip_rt_delete:"));
7208 	/*
7209 	 * If this is the case of RTF_HOST being set, then we set the netmask
7210 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7211 	 */
7212 	if (flags & RTF_HOST) {
7213 		mask = IP_HOST_MASK;
7214 		match_flags |= MATCH_IRE_MASK;
7215 	} else if (rtm_addrs & RTA_NETMASK) {
7216 		match_flags |= MATCH_IRE_MASK;
7217 	}
7218 
7219 	/*
7220 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7221 	 * we check if the gateway address is one of our interfaces first,
7222 	 * and fall back on RTF_GATEWAY routes.
7223 	 *
7224 	 * This makes it possible to delete an original
7225 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7226 	 *
7227 	 * As the interface index specified with the RTA_IFP sockaddr is the
7228 	 * same for all ipif's off of an ill, the matching logic below uses
7229 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7230 	 * sharing the same prefix and interface index as the the route
7231 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7232 	 * is specified in the request.
7233 	 *
7234 	 * On the other hand, since the gateway address will usually be
7235 	 * different for each ipif on the system, the matching logic
7236 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7237 	 * route.  This means that interface routes for the same prefix can be
7238 	 * uniquely identified if they belong to distinct ipif's and if a
7239 	 * RTA_IFP sockaddr is not present.
7240 	 *
7241 	 * For more detail on specifying routes by gateway address and by
7242 	 * interface index, see the comments in ip_rt_add().
7243 	 */
7244 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &err,
7245 	    ipst);
7246 	if (ipif != NULL)
7247 		ipif_refheld = B_TRUE;
7248 	else if (err == EINPROGRESS)
7249 		return (err);
7250 	else
7251 		err = 0;
7252 	if (ipif != NULL) {
7253 		if (ipif_arg != NULL) {
7254 			if (ipif_refheld) {
7255 				ipif_refrele(ipif);
7256 				ipif_refheld = B_FALSE;
7257 			}
7258 			ipif = ipif_arg;
7259 			match_flags |= MATCH_IRE_ILL;
7260 		} else {
7261 			match_flags |= MATCH_IRE_IPIF;
7262 		}
7263 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7264 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
7265 			    ALL_ZONES, NULL, match_flags, ipst);
7266 		}
7267 		if (ire == NULL) {
7268 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7269 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
7270 			    match_flags, ipst);
7271 		}
7272 	}
7273 
7274 	if (ire == NULL) {
7275 		/*
7276 		 * At this point, the gateway address is not one of our own
7277 		 * addresses or a matching interface route was not found.  We
7278 		 * set the IRE type to lookup based on whether
7279 		 * this is a host route, a default route or just a prefix.
7280 		 *
7281 		 * If an ipif_arg was passed in, then the lookup is based on an
7282 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7283 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7284 		 * set as the route being looked up is not a traditional
7285 		 * interface route.
7286 		 */
7287 		match_flags &= ~MATCH_IRE_IPIF;
7288 		match_flags |= MATCH_IRE_GW;
7289 		if (ipif_arg != NULL)
7290 			match_flags |= MATCH_IRE_ILL;
7291 		if (mask == IP_HOST_MASK)
7292 			type = IRE_HOST;
7293 		else if (mask == 0)
7294 			type = IRE_DEFAULT;
7295 		else
7296 			type = IRE_PREFIX;
7297 		ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7298 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7299 	}
7300 
7301 	if (ipif_refheld)
7302 		ipif_refrele(ipif);
7303 
7304 	/* ipif is not refheld anymore */
7305 	if (ire == NULL)
7306 		return (ESRCH);
7307 
7308 	if (ire->ire_flags & RTF_MULTIRT) {
7309 		/*
7310 		 * Invoke the CGTP (multirouting) filtering module
7311 		 * to remove the dst address from the filtering database.
7312 		 * Packets coming from that address will no longer be
7313 		 * filtered to remove duplicates.
7314 		 */
7315 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
7316 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
7317 			    ipst->ips_netstack->netstack_stackid,
7318 			    ire->ire_addr, ire->ire_gateway_addr);
7319 		}
7320 		ip_cgtp_bcast_delete(ire, ipst);
7321 	}
7322 
7323 	ipif = ire->ire_ipif;
7324 	if (ipif != NULL)
7325 		ipif_remove_ire(ipif, ire);
7326 	if (ioctl_msg)
7327 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7328 	ire_delete(ire);
7329 	ire_refrele(ire);
7330 	return (err);
7331 }
7332 
7333 /*
7334  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7335  */
7336 /* ARGSUSED */
7337 int
7338 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7339     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7340 {
7341 	ipaddr_t dst_addr;
7342 	ipaddr_t gw_addr;
7343 	ipaddr_t mask;
7344 	int error = 0;
7345 	mblk_t *mp1;
7346 	struct rtentry *rt;
7347 	ipif_t *ipif = NULL;
7348 	ip_stack_t	*ipst;
7349 
7350 	ASSERT(q->q_next == NULL);
7351 	ipst = CONNQ_TO_IPST(q);
7352 
7353 	ip1dbg(("ip_siocaddrt:"));
7354 	/* Existence of mp1 verified in ip_wput_nondata */
7355 	mp1 = mp->b_cont->b_cont;
7356 	rt = (struct rtentry *)mp1->b_rptr;
7357 
7358 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7359 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7360 
7361 	/*
7362 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7363 	 * to a particular host address.  In this case, we set the netmask to
7364 	 * all ones for the particular destination address.  Otherwise,
7365 	 * determine the netmask to be used based on dst_addr and the interfaces
7366 	 * in use.
7367 	 */
7368 	if (rt->rt_flags & RTF_HOST) {
7369 		mask = IP_HOST_MASK;
7370 	} else {
7371 		/*
7372 		 * Note that ip_subnet_mask returns a zero mask in the case of
7373 		 * default (an all-zeroes address).
7374 		 */
7375 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7376 	}
7377 
7378 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7379 	    B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7380 	if (ipif != NULL)
7381 		ipif_refrele(ipif);
7382 	return (error);
7383 }
7384 
7385 /*
7386  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7387  */
7388 /* ARGSUSED */
7389 int
7390 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7391     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7392 {
7393 	ipaddr_t dst_addr;
7394 	ipaddr_t gw_addr;
7395 	ipaddr_t mask;
7396 	int error;
7397 	mblk_t *mp1;
7398 	struct rtentry *rt;
7399 	ipif_t *ipif = NULL;
7400 	ip_stack_t	*ipst;
7401 
7402 	ASSERT(q->q_next == NULL);
7403 	ipst = CONNQ_TO_IPST(q);
7404 
7405 	ip1dbg(("ip_siocdelrt:"));
7406 	/* Existence of mp1 verified in ip_wput_nondata */
7407 	mp1 = mp->b_cont->b_cont;
7408 	rt = (struct rtentry *)mp1->b_rptr;
7409 
7410 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7411 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7412 
7413 	/*
7414 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7415 	 * to a particular host address.  In this case, we set the netmask to
7416 	 * all ones for the particular destination address.  Otherwise,
7417 	 * determine the netmask to be used based on dst_addr and the interfaces
7418 	 * in use.
7419 	 */
7420 	if (rt->rt_flags & RTF_HOST) {
7421 		mask = IP_HOST_MASK;
7422 	} else {
7423 		/*
7424 		 * Note that ip_subnet_mask returns a zero mask in the case of
7425 		 * default (an all-zeroes address).
7426 		 */
7427 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7428 	}
7429 
7430 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7431 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE, q,
7432 	    mp, ip_process_ioctl, ipst);
7433 	if (ipif != NULL)
7434 		ipif_refrele(ipif);
7435 	return (error);
7436 }
7437 
7438 /*
7439  * Enqueue the mp onto the ipsq, chained by b_next.
7440  * b_prev stores the function to be executed later, and b_queue the queue
7441  * where this mp originated.
7442  */
7443 void
7444 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7445     ill_t *pending_ill)
7446 {
7447 	conn_t	*connp;
7448 	ipxop_t *ipx = ipsq->ipsq_xop;
7449 
7450 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7451 	ASSERT(MUTEX_HELD(&ipx->ipx_lock));
7452 	ASSERT(func != NULL);
7453 
7454 	mp->b_queue = q;
7455 	mp->b_prev = (void *)func;
7456 	mp->b_next = NULL;
7457 
7458 	switch (type) {
7459 	case CUR_OP:
7460 		if (ipx->ipx_mptail != NULL) {
7461 			ASSERT(ipx->ipx_mphead != NULL);
7462 			ipx->ipx_mptail->b_next = mp;
7463 		} else {
7464 			ASSERT(ipx->ipx_mphead == NULL);
7465 			ipx->ipx_mphead = mp;
7466 		}
7467 		ipx->ipx_mptail = mp;
7468 		break;
7469 
7470 	case NEW_OP:
7471 		if (ipsq->ipsq_xopq_mptail != NULL) {
7472 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7473 			ipsq->ipsq_xopq_mptail->b_next = mp;
7474 		} else {
7475 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7476 			ipsq->ipsq_xopq_mphead = mp;
7477 		}
7478 		ipsq->ipsq_xopq_mptail = mp;
7479 		ipx->ipx_ipsq_queued = B_TRUE;
7480 		break;
7481 
7482 	case SWITCH_OP:
7483 		ASSERT(ipsq->ipsq_swxop != NULL);
7484 		/* only one switch operation is currently allowed */
7485 		ASSERT(ipsq->ipsq_switch_mp == NULL);
7486 		ipsq->ipsq_switch_mp = mp;
7487 		ipx->ipx_ipsq_queued = B_TRUE;
7488 		break;
7489 	default:
7490 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7491 	}
7492 
7493 	if (CONN_Q(q) && pending_ill != NULL) {
7494 		connp = Q_TO_CONN(q);
7495 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7496 		connp->conn_oper_pending_ill = pending_ill;
7497 	}
7498 }
7499 
7500 /*
7501  * Dequeue the next message that requested exclusive access to this IPSQ's
7502  * xop.  Specifically:
7503  *
7504  *  1. If we're still processing the current operation on `ipsq', then
7505  *     dequeue the next message for the operation (from ipx_mphead), or
7506  *     return NULL if there are no queued messages for the operation.
7507  *     These messages are queued via CUR_OP to qwriter_ip() and friends.
7508  *
7509  *  2. If the current operation on `ipsq' has completed (ipx_current_ipif is
7510  *     not set) see if the ipsq has requested an xop switch.  If so, switch
7511  *     `ipsq' to a different xop.  Xop switches only happen when joining or
7512  *     leaving IPMP groups and require a careful dance -- see the comments
7513  *     in-line below for details.  If we're leaving a group xop or if we're
7514  *     joining a group xop and become writer on it, then we proceed to (3).
7515  *     Otherwise, we return NULL and exit the xop.
7516  *
7517  *  3. For each IPSQ in the xop, return any switch operation stored on
7518  *     ipsq_switch_mp (set via SWITCH_OP); these must be processed before
7519  *     any other messages queued on the IPSQ.  Otherwise, dequeue the next
7520  *     exclusive operation (queued via NEW_OP) stored on ipsq_xopq_mphead.
7521  *     Note that if the phyint tied to `ipsq' is not using IPMP there will
7522  *     only be one IPSQ in the xop.  Otherwise, there will be one IPSQ for
7523  *     each phyint in the group, including the IPMP meta-interface phyint.
7524  */
7525 static mblk_t *
7526 ipsq_dq(ipsq_t *ipsq)
7527 {
7528 	ill_t	*illv4, *illv6;
7529 	mblk_t	*mp;
7530 	ipsq_t	*xopipsq;
7531 	ipsq_t	*leftipsq = NULL;
7532 	ipxop_t *ipx;
7533 	phyint_t *phyi = ipsq->ipsq_phyint;
7534 	ip_stack_t *ipst = ipsq->ipsq_ipst;
7535 	boolean_t emptied = B_FALSE;
7536 
7537 	/*
7538 	 * Grab all the locks we need in the defined order (ill_g_lock ->
7539 	 * ipsq_lock -> ipx_lock); ill_g_lock is needed to use ipsq_next.
7540 	 */
7541 	rw_enter(&ipst->ips_ill_g_lock,
7542 	    ipsq->ipsq_swxop != NULL ? RW_WRITER : RW_READER);
7543 	mutex_enter(&ipsq->ipsq_lock);
7544 	ipx = ipsq->ipsq_xop;
7545 	mutex_enter(&ipx->ipx_lock);
7546 
7547 	/*
7548 	 * Dequeue the next message associated with the current exclusive
7549 	 * operation, if any.
7550 	 */
7551 	if ((mp = ipx->ipx_mphead) != NULL) {
7552 		ipx->ipx_mphead = mp->b_next;
7553 		if (ipx->ipx_mphead == NULL)
7554 			ipx->ipx_mptail = NULL;
7555 		mp->b_next = (void *)ipsq;
7556 		goto out;
7557 	}
7558 
7559 	if (ipx->ipx_current_ipif != NULL)
7560 		goto empty;
7561 
7562 	if (ipsq->ipsq_swxop != NULL) {
7563 		/*
7564 		 * The exclusive operation that is now being completed has
7565 		 * requested a switch to a different xop.  This happens
7566 		 * when an interface joins or leaves an IPMP group.  Joins
7567 		 * happen through SIOCSLIFGROUPNAME (ip_sioctl_groupname()).
7568 		 * Leaves happen via SIOCSLIFGROUPNAME, interface unplumb
7569 		 * (phyint_free()), or interface plumb for an ill type
7570 		 * not in the IPMP group (ip_rput_dlpi_writer()).
7571 		 *
7572 		 * Xop switches are not allowed on the IPMP meta-interface.
7573 		 */
7574 		ASSERT(phyi == NULL || !(phyi->phyint_flags & PHYI_IPMP));
7575 		ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
7576 		DTRACE_PROBE1(ipsq__switch, (ipsq_t *), ipsq);
7577 
7578 		if (ipsq->ipsq_swxop == &ipsq->ipsq_ownxop) {
7579 			/*
7580 			 * We're switching back to our own xop, so we have two
7581 			 * xop's to drain/exit: our own, and the group xop
7582 			 * that we are leaving.
7583 			 *
7584 			 * First, pull ourselves out of the group ipsq list.
7585 			 * This is safe since we're writer on ill_g_lock.
7586 			 */
7587 			ASSERT(ipsq->ipsq_xop != &ipsq->ipsq_ownxop);
7588 
7589 			xopipsq = ipx->ipx_ipsq;
7590 			while (xopipsq->ipsq_next != ipsq)
7591 				xopipsq = xopipsq->ipsq_next;
7592 
7593 			xopipsq->ipsq_next = ipsq->ipsq_next;
7594 			ipsq->ipsq_next = ipsq;
7595 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
7596 			ipsq->ipsq_swxop = NULL;
7597 
7598 			/*
7599 			 * Second, prepare to exit the group xop.  The actual
7600 			 * ipsq_exit() is done at the end of this function
7601 			 * since we cannot hold any locks across ipsq_exit().
7602 			 * Note that although we drop the group's ipx_lock, no
7603 			 * threads can proceed since we're still ipx_writer.
7604 			 */
7605 			leftipsq = xopipsq;
7606 			mutex_exit(&ipx->ipx_lock);
7607 
7608 			/*
7609 			 * Third, set ipx to point to our own xop (which was
7610 			 * inactive and therefore can be entered).
7611 			 */
7612 			ipx = ipsq->ipsq_xop;
7613 			mutex_enter(&ipx->ipx_lock);
7614 			ASSERT(ipx->ipx_writer == NULL);
7615 			ASSERT(ipx->ipx_current_ipif == NULL);
7616 		} else {
7617 			/*
7618 			 * We're switching from our own xop to a group xop.
7619 			 * The requestor of the switch must ensure that the
7620 			 * group xop cannot go away (e.g. by ensuring the
7621 			 * phyint associated with the xop cannot go away).
7622 			 *
7623 			 * If we can become writer on our new xop, then we'll
7624 			 * do the drain.  Otherwise, the current writer of our
7625 			 * new xop will do the drain when it exits.
7626 			 *
7627 			 * First, splice ourselves into the group IPSQ list.
7628 			 * This is safe since we're writer on ill_g_lock.
7629 			 */
7630 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
7631 
7632 			xopipsq = ipsq->ipsq_swxop->ipx_ipsq;
7633 			while (xopipsq->ipsq_next != ipsq->ipsq_swxop->ipx_ipsq)
7634 				xopipsq = xopipsq->ipsq_next;
7635 
7636 			xopipsq->ipsq_next = ipsq;
7637 			ipsq->ipsq_next = ipsq->ipsq_swxop->ipx_ipsq;
7638 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
7639 			ipsq->ipsq_swxop = NULL;
7640 
7641 			/*
7642 			 * Second, exit our own xop, since it's now unused.
7643 			 * This is safe since we've got the only reference.
7644 			 */
7645 			ASSERT(ipx->ipx_writer == curthread);
7646 			ipx->ipx_writer = NULL;
7647 			VERIFY(--ipx->ipx_reentry_cnt == 0);
7648 			ipx->ipx_ipsq_queued = B_FALSE;
7649 			mutex_exit(&ipx->ipx_lock);
7650 
7651 			/*
7652 			 * Third, set ipx to point to our new xop, and check
7653 			 * if we can become writer on it.  If we cannot, then
7654 			 * the current writer will drain the IPSQ group when
7655 			 * it exits.  Our ipsq_xop is guaranteed to be stable
7656 			 * because we're still holding ipsq_lock.
7657 			 */
7658 			ipx = ipsq->ipsq_xop;
7659 			mutex_enter(&ipx->ipx_lock);
7660 			if (ipx->ipx_writer != NULL ||
7661 			    ipx->ipx_current_ipif != NULL) {
7662 				goto out;
7663 			}
7664 		}
7665 
7666 		/*
7667 		 * Fourth, become writer on our new ipx before we continue
7668 		 * with the drain.  Note that we never dropped ipsq_lock
7669 		 * above, so no other thread could've raced with us to
7670 		 * become writer first.  Also, we're holding ipx_lock, so
7671 		 * no other thread can examine the ipx right now.
7672 		 */
7673 		ASSERT(ipx->ipx_current_ipif == NULL);
7674 		ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
7675 		VERIFY(ipx->ipx_reentry_cnt++ == 0);
7676 		ipx->ipx_writer = curthread;
7677 		ipx->ipx_forced = B_FALSE;
7678 #ifdef DEBUG
7679 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
7680 #endif
7681 	}
7682 
7683 	xopipsq = ipsq;
7684 	do {
7685 		/*
7686 		 * So that other operations operate on a consistent and
7687 		 * complete phyint, a switch message on an IPSQ must be
7688 		 * handled prior to any other operations on that IPSQ.
7689 		 */
7690 		if ((mp = xopipsq->ipsq_switch_mp) != NULL) {
7691 			xopipsq->ipsq_switch_mp = NULL;
7692 			ASSERT(mp->b_next == NULL);
7693 			mp->b_next = (void *)xopipsq;
7694 			goto out;
7695 		}
7696 
7697 		if ((mp = xopipsq->ipsq_xopq_mphead) != NULL) {
7698 			xopipsq->ipsq_xopq_mphead = mp->b_next;
7699 			if (xopipsq->ipsq_xopq_mphead == NULL)
7700 				xopipsq->ipsq_xopq_mptail = NULL;
7701 			mp->b_next = (void *)xopipsq;
7702 			goto out;
7703 		}
7704 	} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
7705 empty:
7706 	/*
7707 	 * There are no messages.  Further, we are holding ipx_lock, hence no
7708 	 * new messages can end up on any IPSQ in the xop.
7709 	 */
7710 	ipx->ipx_writer = NULL;
7711 	ipx->ipx_forced = B_FALSE;
7712 	VERIFY(--ipx->ipx_reentry_cnt == 0);
7713 	ipx->ipx_ipsq_queued = B_FALSE;
7714 	emptied = B_TRUE;
7715 #ifdef	DEBUG
7716 	ipx->ipx_depth = 0;
7717 #endif
7718 out:
7719 	mutex_exit(&ipx->ipx_lock);
7720 	mutex_exit(&ipsq->ipsq_lock);
7721 
7722 	/*
7723 	 * If we completely emptied the xop, then wake up any threads waiting
7724 	 * to enter any of the IPSQ's associated with it.
7725 	 */
7726 	if (emptied) {
7727 		xopipsq = ipsq;
7728 		do {
7729 			if ((phyi = xopipsq->ipsq_phyint) == NULL)
7730 				continue;
7731 
7732 			illv4 = phyi->phyint_illv4;
7733 			illv6 = phyi->phyint_illv6;
7734 
7735 			GRAB_ILL_LOCKS(illv4, illv6);
7736 			if (illv4 != NULL)
7737 				cv_broadcast(&illv4->ill_cv);
7738 			if (illv6 != NULL)
7739 				cv_broadcast(&illv6->ill_cv);
7740 			RELEASE_ILL_LOCKS(illv4, illv6);
7741 		} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
7742 	}
7743 	rw_exit(&ipst->ips_ill_g_lock);
7744 
7745 	/*
7746 	 * Now that all locks are dropped, exit the IPSQ we left.
7747 	 */
7748 	if (leftipsq != NULL)
7749 		ipsq_exit(leftipsq);
7750 
7751 	return (mp);
7752 }
7753 
7754 /*
7755  * Enter the ipsq corresponding to ill, by waiting synchronously till
7756  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7757  * will have to drain completely before ipsq_enter returns success.
7758  * ipx_current_ipif will be set if some exclusive op is in progress,
7759  * and the ipsq_exit logic will start the next enqueued op after
7760  * completion of the current op. If 'force' is used, we don't wait
7761  * for the enqueued ops. This is needed when a conn_close wants to
7762  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7763  * of an ill can also use this option. But we dont' use it currently.
7764  */
7765 #define	ENTER_SQ_WAIT_TICKS 100
7766 boolean_t
7767 ipsq_enter(ill_t *ill, boolean_t force, int type)
7768 {
7769 	ipsq_t	*ipsq;
7770 	ipxop_t *ipx;
7771 	boolean_t waited_enough = B_FALSE;
7772 
7773 	/*
7774 	 * Note that the relationship between ill and ipsq is fixed as long as
7775 	 * the ill is not ILL_CONDEMNED.  Holding ipsq_lock ensures the
7776 	 * relationship between the IPSQ and xop cannot change.  However,
7777 	 * since we cannot hold ipsq_lock across the cv_wait(), it may change
7778 	 * while we're waiting.  We wait on ill_cv and rely on ipsq_exit()
7779 	 * waking up all ills in the xop when it becomes available.
7780 	 */
7781 	mutex_enter(&ill->ill_lock);
7782 	for (;;) {
7783 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7784 			mutex_exit(&ill->ill_lock);
7785 			return (B_FALSE);
7786 		}
7787 
7788 		ipsq = ill->ill_phyint->phyint_ipsq;
7789 		mutex_enter(&ipsq->ipsq_lock);
7790 		ipx = ipsq->ipsq_xop;
7791 		mutex_enter(&ipx->ipx_lock);
7792 
7793 		if (ipx->ipx_writer == NULL && (type == CUR_OP ||
7794 		    ipx->ipx_current_ipif == NULL || waited_enough))
7795 			break;
7796 
7797 		if (!force || ipx->ipx_writer != NULL) {
7798 			mutex_exit(&ipx->ipx_lock);
7799 			mutex_exit(&ipsq->ipsq_lock);
7800 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7801 		} else {
7802 			mutex_exit(&ipx->ipx_lock);
7803 			mutex_exit(&ipsq->ipsq_lock);
7804 			(void) cv_timedwait(&ill->ill_cv,
7805 			    &ill->ill_lock, lbolt + ENTER_SQ_WAIT_TICKS);
7806 			waited_enough = B_TRUE;
7807 		}
7808 	}
7809 
7810 	ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
7811 	ASSERT(ipx->ipx_reentry_cnt == 0);
7812 	ipx->ipx_writer = curthread;
7813 	ipx->ipx_forced = (ipx->ipx_current_ipif != NULL);
7814 	ipx->ipx_reentry_cnt++;
7815 #ifdef DEBUG
7816 	ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
7817 #endif
7818 	mutex_exit(&ipx->ipx_lock);
7819 	mutex_exit(&ipsq->ipsq_lock);
7820 	mutex_exit(&ill->ill_lock);
7821 	return (B_TRUE);
7822 }
7823 
7824 boolean_t
7825 ill_perim_enter(ill_t *ill)
7826 {
7827 	return (ipsq_enter(ill, B_FALSE, CUR_OP));
7828 }
7829 
7830 void
7831 ill_perim_exit(ill_t *ill)
7832 {
7833 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
7834 }
7835 
7836 /*
7837  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7838  * certain critical operations like plumbing (i.e. most set ioctls), multicast
7839  * joins, igmp/mld timers, etc.  There is one ipsq per phyint. The ipsq
7840  * serializes exclusive ioctls issued by applications on a per ipsq basis in
7841  * ipsq_xopq_mphead. It also protects against multiple threads executing in
7842  * the ipsq. Responses from the driver pertain to the current ioctl (say a
7843  * DL_BIND_ACK in response to a DL_BIND_REQ initiated as part of bringing
7844  * up the interface) and are enqueued in ipx_mphead.
7845  *
7846  * If a thread does not want to reenter the ipsq when it is already writer,
7847  * it must make sure that the specified reentry point to be called later
7848  * when the ipsq is empty, nor any code path starting from the specified reentry
7849  * point must never ever try to enter the ipsq again. Otherwise it can lead
7850  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7851  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7852  * dequeues the requests waiting to become exclusive in ipx_mphead and calls
7853  * the reentry point. When the list at ipx_mphead becomes empty ipsq_exit
7854  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7855  * ioctl if the current ioctl has completed. If the current ioctl is still
7856  * in progress it simply returns. The current ioctl could be waiting for
7857  * a response from another module (arp or the driver or could be waiting for
7858  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipx_pending_mp
7859  * and ipx_pending_ipif are set. ipx_current_ipif is set throughout the
7860  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7861  * ipx_current_ipif is NULL which happens only once the ioctl is complete and
7862  * all associated DLPI operations have completed.
7863  */
7864 
7865 /*
7866  * Try to enter the IPSQ corresponding to `ipif' or `ill' exclusively (`ipif'
7867  * and `ill' cannot both be specified).  Returns a pointer to the entered IPSQ
7868  * on success, or NULL on failure.  The caller ensures ipif/ill is valid by
7869  * refholding it as necessary.  If the IPSQ cannot be entered and `func' is
7870  * non-NULL, then `func' will be called back with `q' and `mp' once the IPSQ
7871  * can be entered.  If `func' is NULL, then `q' and `mp' are ignored.
7872  */
7873 ipsq_t *
7874 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7875     ipsq_func_t func, int type, boolean_t reentry_ok)
7876 {
7877 	ipsq_t	*ipsq;
7878 	ipxop_t	*ipx;
7879 
7880 	/* Only 1 of ipif or ill can be specified */
7881 	ASSERT((ipif != NULL) ^ (ill != NULL));
7882 	if (ipif != NULL)
7883 		ill = ipif->ipif_ill;
7884 
7885 	/*
7886 	 * lock ordering: conn_lock -> ill_lock -> ipsq_lock -> ipx_lock.
7887 	 * ipx of an ipsq can't change when ipsq_lock is held.
7888 	 */
7889 	GRAB_CONN_LOCK(q);
7890 	mutex_enter(&ill->ill_lock);
7891 	ipsq = ill->ill_phyint->phyint_ipsq;
7892 	mutex_enter(&ipsq->ipsq_lock);
7893 	ipx = ipsq->ipsq_xop;
7894 	mutex_enter(&ipx->ipx_lock);
7895 
7896 	/*
7897 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7898 	 *    (Note: If the caller does not specify reentry_ok then neither
7899 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7900 	 *    again. Otherwise it can lead to an infinite loop
7901 	 * 2. Enter the ipsq if there is no current writer and this attempted
7902 	 *    entry is part of the current operation
7903 	 * 3. Enter the ipsq if there is no current writer and this is a new
7904 	 *    operation and the operation queue is empty and there is no
7905 	 *    operation currently in progress
7906 	 */
7907 	if ((ipx->ipx_writer == curthread && reentry_ok) ||
7908 	    (ipx->ipx_writer == NULL && (type == CUR_OP || (type == NEW_OP &&
7909 	    !ipx->ipx_ipsq_queued && ipx->ipx_current_ipif == NULL)))) {
7910 		/* Success. */
7911 		ipx->ipx_reentry_cnt++;
7912 		ipx->ipx_writer = curthread;
7913 		ipx->ipx_forced = B_FALSE;
7914 		mutex_exit(&ipx->ipx_lock);
7915 		mutex_exit(&ipsq->ipsq_lock);
7916 		mutex_exit(&ill->ill_lock);
7917 		RELEASE_CONN_LOCK(q);
7918 #ifdef DEBUG
7919 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
7920 #endif
7921 		return (ipsq);
7922 	}
7923 
7924 	if (func != NULL)
7925 		ipsq_enq(ipsq, q, mp, func, type, ill);
7926 
7927 	mutex_exit(&ipx->ipx_lock);
7928 	mutex_exit(&ipsq->ipsq_lock);
7929 	mutex_exit(&ill->ill_lock);
7930 	RELEASE_CONN_LOCK(q);
7931 	return (NULL);
7932 }
7933 
7934 /*
7935  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7936  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7937  * cannot be entered, the mp is queued for completion.
7938  */
7939 void
7940 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7941     boolean_t reentry_ok)
7942 {
7943 	ipsq_t	*ipsq;
7944 
7945 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
7946 
7947 	/*
7948 	 * Drop the caller's refhold on the ill.  This is safe since we either
7949 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
7950 	 * IPSQ, in which case we return without accessing ill anymore.  This
7951 	 * is needed because func needs to see the correct refcount.
7952 	 * e.g. removeif can work only then.
7953 	 */
7954 	ill_refrele(ill);
7955 	if (ipsq != NULL) {
7956 		(*func)(ipsq, q, mp, NULL);
7957 		ipsq_exit(ipsq);
7958 	}
7959 }
7960 
7961 /*
7962  * Exit the specified IPSQ.  If this is the final exit on it then drain it
7963  * prior to exiting.  Caller must be writer on the specified IPSQ.
7964  */
7965 void
7966 ipsq_exit(ipsq_t *ipsq)
7967 {
7968 	mblk_t *mp;
7969 	ipsq_t *mp_ipsq;
7970 	queue_t	*q;
7971 	phyint_t *phyi;
7972 	ipsq_func_t func;
7973 
7974 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7975 
7976 	ASSERT(ipsq->ipsq_xop->ipx_reentry_cnt >= 1);
7977 	if (ipsq->ipsq_xop->ipx_reentry_cnt != 1) {
7978 		ipsq->ipsq_xop->ipx_reentry_cnt--;
7979 		return;
7980 	}
7981 
7982 	for (;;) {
7983 		phyi = ipsq->ipsq_phyint;
7984 		mp = ipsq_dq(ipsq);
7985 		mp_ipsq = (mp == NULL) ? NULL : (ipsq_t *)mp->b_next;
7986 
7987 		/*
7988 		 * If we've changed to a new IPSQ, and the phyint associated
7989 		 * with the old one has gone away, free the old IPSQ.  Note
7990 		 * that this cannot happen while the IPSQ is in a group.
7991 		 */
7992 		if (mp_ipsq != ipsq && phyi == NULL) {
7993 			ASSERT(ipsq->ipsq_next == ipsq);
7994 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
7995 			ipsq_delete(ipsq);
7996 		}
7997 
7998 		if (mp == NULL)
7999 			break;
8000 
8001 		q = mp->b_queue;
8002 		func = (ipsq_func_t)mp->b_prev;
8003 		ipsq = mp_ipsq;
8004 		mp->b_next = mp->b_prev = NULL;
8005 		mp->b_queue = NULL;
8006 
8007 		/*
8008 		 * If 'q' is an conn queue, it is valid, since we did a
8009 		 * a refhold on the conn at the start of the ioctl.
8010 		 * If 'q' is an ill queue, it is valid, since close of an
8011 		 * ill will clean up its IPSQ.
8012 		 */
8013 		(*func)(ipsq, q, mp, NULL);
8014 	}
8015 }
8016 
8017 /*
8018  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
8019  * and `ioccmd'.
8020  */
8021 void
8022 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
8023 {
8024 	ipxop_t *ipx = ipsq->ipsq_xop;
8025 
8026 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8027 	ASSERT(ipx->ipx_current_ipif == NULL);
8028 	ASSERT(ipx->ipx_current_ioctl == 0);
8029 
8030 	ipx->ipx_current_done = B_FALSE;
8031 	ipx->ipx_current_ioctl = ioccmd;
8032 	mutex_enter(&ipx->ipx_lock);
8033 	ipx->ipx_current_ipif = ipif;
8034 	mutex_exit(&ipx->ipx_lock);
8035 }
8036 
8037 /*
8038  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
8039  * the next exclusive operation to begin once we ipsq_exit().  However, if
8040  * pending DLPI operations remain, then we will wait for the queue to drain
8041  * before allowing the next exclusive operation to begin.  This ensures that
8042  * DLPI operations from one exclusive operation are never improperly processed
8043  * as part of a subsequent exclusive operation.
8044  */
8045 void
8046 ipsq_current_finish(ipsq_t *ipsq)
8047 {
8048 	ipxop_t	*ipx = ipsq->ipsq_xop;
8049 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
8050 	ipif_t	*ipif = ipx->ipx_current_ipif;
8051 
8052 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8053 
8054 	/*
8055 	 * For SIOCLIFREMOVEIF, the ipif has been already been blown away
8056 	 * (but in that case, IPIF_CHANGING will already be clear and no
8057 	 * pending DLPI messages can remain).
8058 	 */
8059 	if (ipx->ipx_current_ioctl != SIOCLIFREMOVEIF) {
8060 		ill_t *ill = ipif->ipif_ill;
8061 
8062 		mutex_enter(&ill->ill_lock);
8063 		dlpi_pending = ill->ill_dlpi_pending;
8064 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
8065 		mutex_exit(&ill->ill_lock);
8066 	}
8067 
8068 	ASSERT(!ipx->ipx_current_done);
8069 	ipx->ipx_current_done = B_TRUE;
8070 	ipx->ipx_current_ioctl = 0;
8071 	if (dlpi_pending == DL_PRIM_INVAL) {
8072 		mutex_enter(&ipx->ipx_lock);
8073 		ipx->ipx_current_ipif = NULL;
8074 		mutex_exit(&ipx->ipx_lock);
8075 	}
8076 }
8077 
8078 /*
8079  * The ill is closing. Flush all messages on the ipsq that originated
8080  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
8081  * for this ill since ipsq_enter could not have entered until then.
8082  * New messages can't be queued since the CONDEMNED flag is set.
8083  */
8084 static void
8085 ipsq_flush(ill_t *ill)
8086 {
8087 	queue_t	*q;
8088 	mblk_t	*prev;
8089 	mblk_t	*mp;
8090 	mblk_t	*mp_next;
8091 	ipxop_t	*ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
8092 
8093 	ASSERT(IAM_WRITER_ILL(ill));
8094 
8095 	/*
8096 	 * Flush any messages sent up by the driver.
8097 	 */
8098 	mutex_enter(&ipx->ipx_lock);
8099 	for (prev = NULL, mp = ipx->ipx_mphead; mp != NULL; mp = mp_next) {
8100 		mp_next = mp->b_next;
8101 		q = mp->b_queue;
8102 		if (q == ill->ill_rq || q == ill->ill_wq) {
8103 			/* dequeue mp */
8104 			if (prev == NULL)
8105 				ipx->ipx_mphead = mp->b_next;
8106 			else
8107 				prev->b_next = mp->b_next;
8108 			if (ipx->ipx_mptail == mp) {
8109 				ASSERT(mp_next == NULL);
8110 				ipx->ipx_mptail = prev;
8111 			}
8112 			inet_freemsg(mp);
8113 		} else {
8114 			prev = mp;
8115 		}
8116 	}
8117 	mutex_exit(&ipx->ipx_lock);
8118 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8119 	ipsq_xopq_mp_cleanup(ill, NULL);
8120 	ill_pending_mp_cleanup(ill);
8121 }
8122 
8123 /*
8124  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8125  * refhold and return the associated ipif
8126  */
8127 /* ARGSUSED */
8128 int
8129 ip_extract_tunreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8130     cmd_info_t *ci, ipsq_func_t func)
8131 {
8132 	boolean_t exists;
8133 	struct iftun_req *ta;
8134 	ipif_t  *ipif;
8135 	ill_t   *ill;
8136 	boolean_t isv6;
8137 	mblk_t  *mp1;
8138 	int error;
8139 	conn_t  *connp;
8140 	ip_stack_t  *ipst;
8141 
8142 	/* Existence verified in ip_wput_nondata */
8143 	mp1 = mp->b_cont->b_cont;
8144 	ta = (struct iftun_req *)mp1->b_rptr;
8145 	/*
8146 	 * Null terminate the string to protect against buffer
8147 	 * overrun. String was generated by user code and may not
8148 	 * be trusted.
8149 	 */
8150 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8151 
8152 	connp = Q_TO_CONN(q);
8153 	isv6 = connp->conn_af_isv6;
8154 	ipst = connp->conn_netstack->netstack_ip;
8155 
8156 	/* Disallows implicit create */
8157 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8158 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8159 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8160 	if (ipif == NULL)
8161 		return (error);
8162 
8163 	if (ipif->ipif_id != 0) {
8164 		/*
8165 		 * We really don't want to set/get tunnel parameters
8166 		 * on virtual tunnel interfaces.  Only allow the
8167 		 * base tunnel to do these.
8168 		 */
8169 		ipif_refrele(ipif);
8170 		return (EINVAL);
8171 	}
8172 
8173 	/*
8174 	 * Send down to tunnel mod for ioctl processing.
8175 	 * Will finish ioctl in ip_rput_other().
8176 	 */
8177 	ill = ipif->ipif_ill;
8178 	if (ill->ill_net_type == IRE_LOOPBACK) {
8179 		ipif_refrele(ipif);
8180 		return (EOPNOTSUPP);
8181 	}
8182 
8183 	if (ill->ill_wq == NULL) {
8184 		ipif_refrele(ipif);
8185 		return (ENXIO);
8186 	}
8187 	/*
8188 	 * Mark the ioctl as coming from an IPv6 interface for
8189 	 * tun's convenience.
8190 	 */
8191 	if (ill->ill_isv6)
8192 		ta->ifta_flags |= 0x80000000;
8193 	ci->ci_ipif = ipif;
8194 	return (0);
8195 }
8196 
8197 /*
8198  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8199  * and return the associated ipif.
8200  * Return value:
8201  *	Non zero: An error has occurred. ci may not be filled out.
8202  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8203  *	a held ipif in ci.ci_ipif.
8204  */
8205 int
8206 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8207     cmd_info_t *ci, ipsq_func_t func)
8208 {
8209 	char		*name;
8210 	struct ifreq    *ifr;
8211 	struct lifreq    *lifr;
8212 	ipif_t		*ipif = NULL;
8213 	ill_t		*ill;
8214 	conn_t		*connp;
8215 	boolean_t	isv6;
8216 	boolean_t	exists;
8217 	int		err;
8218 	mblk_t		*mp1;
8219 	zoneid_t	zoneid;
8220 	ip_stack_t	*ipst;
8221 
8222 	if (q->q_next != NULL) {
8223 		ill = (ill_t *)q->q_ptr;
8224 		isv6 = ill->ill_isv6;
8225 		connp = NULL;
8226 		zoneid = ALL_ZONES;
8227 		ipst = ill->ill_ipst;
8228 	} else {
8229 		ill = NULL;
8230 		connp = Q_TO_CONN(q);
8231 		isv6 = connp->conn_af_isv6;
8232 		zoneid = connp->conn_zoneid;
8233 		if (zoneid == GLOBAL_ZONEID) {
8234 			/* global zone can access ipifs in all zones */
8235 			zoneid = ALL_ZONES;
8236 		}
8237 		ipst = connp->conn_netstack->netstack_ip;
8238 	}
8239 
8240 	/* Has been checked in ip_wput_nondata */
8241 	mp1 = mp->b_cont->b_cont;
8242 
8243 	if (ipip->ipi_cmd_type == IF_CMD) {
8244 		/* This a old style SIOC[GS]IF* command */
8245 		ifr = (struct ifreq *)mp1->b_rptr;
8246 		/*
8247 		 * Null terminate the string to protect against buffer
8248 		 * overrun. String was generated by user code and may not
8249 		 * be trusted.
8250 		 */
8251 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8252 		name = ifr->ifr_name;
8253 		ci->ci_sin = (sin_t *)&ifr->ifr_addr;
8254 		ci->ci_sin6 = NULL;
8255 		ci->ci_lifr = (struct lifreq *)ifr;
8256 	} else {
8257 		/* This a new style SIOC[GS]LIF* command */
8258 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
8259 		lifr = (struct lifreq *)mp1->b_rptr;
8260 		/*
8261 		 * Null terminate the string to protect against buffer
8262 		 * overrun. String was generated by user code and may not
8263 		 * be trusted.
8264 		 */
8265 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8266 		name = lifr->lifr_name;
8267 		ci->ci_sin = (sin_t *)&lifr->lifr_addr;
8268 		ci->ci_sin6 = (sin6_t *)&lifr->lifr_addr;
8269 		ci->ci_lifr = lifr;
8270 	}
8271 
8272 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
8273 		/*
8274 		 * The ioctl will be failed if the ioctl comes down
8275 		 * an conn stream
8276 		 */
8277 		if (ill == NULL) {
8278 			/*
8279 			 * Not an ill queue, return EINVAL same as the
8280 			 * old error code.
8281 			 */
8282 			return (ENXIO);
8283 		}
8284 		ipif = ill->ill_ipif;
8285 		ipif_refhold(ipif);
8286 	} else {
8287 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8288 		    &exists, isv6, zoneid,
8289 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8290 		    ipst);
8291 		if (ipif == NULL) {
8292 			if (err == EINPROGRESS)
8293 				return (err);
8294 			err = 0;	/* Ensure we don't use it below */
8295 		}
8296 	}
8297 
8298 	/*
8299 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8300 	 */
8301 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
8302 		ipif_refrele(ipif);
8303 		return (ENXIO);
8304 	}
8305 
8306 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8307 	    name[0] == '\0') {
8308 		/*
8309 		 * Handle a or a SIOC?IF* with a null name
8310 		 * during plumb (on the ill queue before the I_PLINK).
8311 		 */
8312 		ipif = ill->ill_ipif;
8313 		ipif_refhold(ipif);
8314 	}
8315 
8316 	if (ipif == NULL)
8317 		return (ENXIO);
8318 
8319 	ci->ci_ipif = ipif;
8320 	return (0);
8321 }
8322 
8323 /*
8324  * Return the total number of ipifs.
8325  */
8326 static uint_t
8327 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8328 {
8329 	uint_t numifs = 0;
8330 	ill_t	*ill;
8331 	ill_walk_context_t	ctx;
8332 	ipif_t	*ipif;
8333 
8334 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8335 	ill = ILL_START_WALK_V4(&ctx, ipst);
8336 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8337 		if (IS_UNDER_IPMP(ill))
8338 			continue;
8339 		for (ipif = ill->ill_ipif; ipif != NULL;
8340 		    ipif = ipif->ipif_next) {
8341 			if (ipif->ipif_zoneid == zoneid ||
8342 			    ipif->ipif_zoneid == ALL_ZONES)
8343 				numifs++;
8344 		}
8345 	}
8346 	rw_exit(&ipst->ips_ill_g_lock);
8347 	return (numifs);
8348 }
8349 
8350 /*
8351  * Return the total number of ipifs.
8352  */
8353 static uint_t
8354 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8355 {
8356 	uint_t numifs = 0;
8357 	ill_t	*ill;
8358 	ipif_t	*ipif;
8359 	ill_walk_context_t	ctx;
8360 
8361 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8362 
8363 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8364 	if (family == AF_INET)
8365 		ill = ILL_START_WALK_V4(&ctx, ipst);
8366 	else if (family == AF_INET6)
8367 		ill = ILL_START_WALK_V6(&ctx, ipst);
8368 	else
8369 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8370 
8371 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8372 		if (IS_UNDER_IPMP(ill) && !(lifn_flags & LIFC_UNDER_IPMP))
8373 			continue;
8374 
8375 		for (ipif = ill->ill_ipif; ipif != NULL;
8376 		    ipif = ipif->ipif_next) {
8377 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8378 			    !(lifn_flags & LIFC_NOXMIT))
8379 				continue;
8380 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8381 			    !(lifn_flags & LIFC_TEMPORARY))
8382 				continue;
8383 			if (((ipif->ipif_flags &
8384 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8385 			    IPIF_DEPRECATED)) ||
8386 			    IS_LOOPBACK(ill) ||
8387 			    !(ipif->ipif_flags & IPIF_UP)) &&
8388 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8389 				continue;
8390 
8391 			if (zoneid != ipif->ipif_zoneid &&
8392 			    ipif->ipif_zoneid != ALL_ZONES &&
8393 			    (zoneid != GLOBAL_ZONEID ||
8394 			    !(lifn_flags & LIFC_ALLZONES)))
8395 				continue;
8396 
8397 			numifs++;
8398 		}
8399 	}
8400 	rw_exit(&ipst->ips_ill_g_lock);
8401 	return (numifs);
8402 }
8403 
8404 uint_t
8405 ip_get_lifsrcofnum(ill_t *ill)
8406 {
8407 	uint_t numifs = 0;
8408 	ill_t	*ill_head = ill;
8409 	ip_stack_t	*ipst = ill->ill_ipst;
8410 
8411 	/*
8412 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8413 	 * other thread may be trying to relink the ILLs in this usesrc group
8414 	 * and adjusting the ill_usesrc_grp_next pointers
8415 	 */
8416 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8417 	if ((ill->ill_usesrc_ifindex == 0) &&
8418 	    (ill->ill_usesrc_grp_next != NULL)) {
8419 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8420 		    ill = ill->ill_usesrc_grp_next)
8421 			numifs++;
8422 	}
8423 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8424 
8425 	return (numifs);
8426 }
8427 
8428 /* Null values are passed in for ipif, sin, and ifreq */
8429 /* ARGSUSED */
8430 int
8431 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8432     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8433 {
8434 	int *nump;
8435 	conn_t *connp = Q_TO_CONN(q);
8436 
8437 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8438 
8439 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8440 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8441 
8442 	*nump = ip_get_numifs(connp->conn_zoneid,
8443 	    connp->conn_netstack->netstack_ip);
8444 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8445 	return (0);
8446 }
8447 
8448 /* Null values are passed in for ipif, sin, and ifreq */
8449 /* ARGSUSED */
8450 int
8451 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8452     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8453 {
8454 	struct lifnum *lifn;
8455 	mblk_t	*mp1;
8456 	conn_t *connp = Q_TO_CONN(q);
8457 
8458 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8459 
8460 	/* Existence checked in ip_wput_nondata */
8461 	mp1 = mp->b_cont->b_cont;
8462 
8463 	lifn = (struct lifnum *)mp1->b_rptr;
8464 	switch (lifn->lifn_family) {
8465 	case AF_UNSPEC:
8466 	case AF_INET:
8467 	case AF_INET6:
8468 		break;
8469 	default:
8470 		return (EAFNOSUPPORT);
8471 	}
8472 
8473 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8474 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8475 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8476 	return (0);
8477 }
8478 
8479 /* ARGSUSED */
8480 int
8481 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8482     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8483 {
8484 	STRUCT_HANDLE(ifconf, ifc);
8485 	mblk_t *mp1;
8486 	struct iocblk *iocp;
8487 	struct ifreq *ifr;
8488 	ill_walk_context_t	ctx;
8489 	ill_t	*ill;
8490 	ipif_t	*ipif;
8491 	struct sockaddr_in *sin;
8492 	int32_t	ifclen;
8493 	zoneid_t zoneid;
8494 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8495 
8496 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8497 
8498 	ip1dbg(("ip_sioctl_get_ifconf"));
8499 	/* Existence verified in ip_wput_nondata */
8500 	mp1 = mp->b_cont->b_cont;
8501 	iocp = (struct iocblk *)mp->b_rptr;
8502 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8503 
8504 	/*
8505 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8506 	 * the user buffer address and length into which the list of struct
8507 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8508 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8509 	 * the SIOCGIFCONF operation was redefined to simply provide
8510 	 * a large output buffer into which we are supposed to jam the ifreq
8511 	 * array.  The same ioctl command code was used, despite the fact that
8512 	 * both the applications and the kernel code had to change, thus making
8513 	 * it impossible to support both interfaces.
8514 	 *
8515 	 * For reasons not good enough to try to explain, the following
8516 	 * algorithm is used for deciding what to do with one of these:
8517 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8518 	 * form with the output buffer coming down as the continuation message.
8519 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8520 	 * and we have to copy in the ifconf structure to find out how big the
8521 	 * output buffer is and where to copy out to.  Sure no problem...
8522 	 *
8523 	 */
8524 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8525 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8526 		int numifs = 0;
8527 		size_t ifc_bufsize;
8528 
8529 		/*
8530 		 * Must be (better be!) continuation of a TRANSPARENT
8531 		 * IOCTL.  We just copied in the ifconf structure.
8532 		 */
8533 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8534 		    (struct ifconf *)mp1->b_rptr);
8535 
8536 		/*
8537 		 * Allocate a buffer to hold requested information.
8538 		 *
8539 		 * If ifc_len is larger than what is needed, we only
8540 		 * allocate what we will use.
8541 		 *
8542 		 * If ifc_len is smaller than what is needed, return
8543 		 * EINVAL.
8544 		 *
8545 		 * XXX: the ill_t structure can hava 2 counters, for
8546 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8547 		 * number of interfaces for a device, so we don't need
8548 		 * to count them here...
8549 		 */
8550 		numifs = ip_get_numifs(zoneid, ipst);
8551 
8552 		ifclen = STRUCT_FGET(ifc, ifc_len);
8553 		ifc_bufsize = numifs * sizeof (struct ifreq);
8554 		if (ifc_bufsize > ifclen) {
8555 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8556 				/* old behaviour */
8557 				return (EINVAL);
8558 			} else {
8559 				ifc_bufsize = ifclen;
8560 			}
8561 		}
8562 
8563 		mp1 = mi_copyout_alloc(q, mp,
8564 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8565 		if (mp1 == NULL)
8566 			return (ENOMEM);
8567 
8568 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8569 	}
8570 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8571 	/*
8572 	 * the SIOCGIFCONF ioctl only knows about
8573 	 * IPv4 addresses, so don't try to tell
8574 	 * it about interfaces with IPv6-only
8575 	 * addresses. (Last parm 'isv6' is B_FALSE)
8576 	 */
8577 
8578 	ifr = (struct ifreq *)mp1->b_rptr;
8579 
8580 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8581 	ill = ILL_START_WALK_V4(&ctx, ipst);
8582 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8583 		if (IS_UNDER_IPMP(ill))
8584 			continue;
8585 		for (ipif = ill->ill_ipif; ipif != NULL;
8586 		    ipif = ipif->ipif_next) {
8587 			if (zoneid != ipif->ipif_zoneid &&
8588 			    ipif->ipif_zoneid != ALL_ZONES)
8589 				continue;
8590 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8591 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8592 					/* old behaviour */
8593 					rw_exit(&ipst->ips_ill_g_lock);
8594 					return (EINVAL);
8595 				} else {
8596 					goto if_copydone;
8597 				}
8598 			}
8599 			ipif_get_name(ipif, ifr->ifr_name,
8600 			    sizeof (ifr->ifr_name));
8601 			sin = (sin_t *)&ifr->ifr_addr;
8602 			*sin = sin_null;
8603 			sin->sin_family = AF_INET;
8604 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8605 			ifr++;
8606 		}
8607 	}
8608 if_copydone:
8609 	rw_exit(&ipst->ips_ill_g_lock);
8610 	mp1->b_wptr = (uchar_t *)ifr;
8611 
8612 	if (STRUCT_BUF(ifc) != NULL) {
8613 		STRUCT_FSET(ifc, ifc_len,
8614 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8615 	}
8616 	return (0);
8617 }
8618 
8619 /*
8620  * Get the interfaces using the address hosted on the interface passed in,
8621  * as a source adddress
8622  */
8623 /* ARGSUSED */
8624 int
8625 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8626     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8627 {
8628 	mblk_t *mp1;
8629 	ill_t	*ill, *ill_head;
8630 	ipif_t	*ipif, *orig_ipif;
8631 	int	numlifs = 0;
8632 	size_t	lifs_bufsize, lifsmaxlen;
8633 	struct	lifreq *lifr;
8634 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8635 	uint_t	ifindex;
8636 	zoneid_t zoneid;
8637 	int err = 0;
8638 	boolean_t isv6 = B_FALSE;
8639 	struct	sockaddr_in	*sin;
8640 	struct	sockaddr_in6	*sin6;
8641 	STRUCT_HANDLE(lifsrcof, lifs);
8642 	ip_stack_t		*ipst;
8643 
8644 	ipst = CONNQ_TO_IPST(q);
8645 
8646 	ASSERT(q->q_next == NULL);
8647 
8648 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8649 
8650 	/* Existence verified in ip_wput_nondata */
8651 	mp1 = mp->b_cont->b_cont;
8652 
8653 	/*
8654 	 * Must be (better be!) continuation of a TRANSPARENT
8655 	 * IOCTL.  We just copied in the lifsrcof structure.
8656 	 */
8657 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8658 	    (struct lifsrcof *)mp1->b_rptr);
8659 
8660 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8661 		return (EINVAL);
8662 
8663 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8664 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8665 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8666 	    ip_process_ioctl, &err, ipst);
8667 	if (ipif == NULL) {
8668 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8669 		    ifindex));
8670 		return (err);
8671 	}
8672 
8673 	/* Allocate a buffer to hold requested information */
8674 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8675 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8676 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8677 	/* The actual size needed is always returned in lifs_len */
8678 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8679 
8680 	/* If the amount we need is more than what is passed in, abort */
8681 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8682 		ipif_refrele(ipif);
8683 		return (0);
8684 	}
8685 
8686 	mp1 = mi_copyout_alloc(q, mp,
8687 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8688 	if (mp1 == NULL) {
8689 		ipif_refrele(ipif);
8690 		return (ENOMEM);
8691 	}
8692 
8693 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8694 	bzero(mp1->b_rptr, lifs_bufsize);
8695 
8696 	lifr = (struct lifreq *)mp1->b_rptr;
8697 
8698 	ill = ill_head = ipif->ipif_ill;
8699 	orig_ipif = ipif;
8700 
8701 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8702 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8703 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8704 
8705 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8706 	for (; (ill != NULL) && (ill != ill_head);
8707 	    ill = ill->ill_usesrc_grp_next) {
8708 
8709 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8710 			break;
8711 
8712 		ipif = ill->ill_ipif;
8713 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
8714 		if (ipif->ipif_isv6) {
8715 			sin6 = (sin6_t *)&lifr->lifr_addr;
8716 			*sin6 = sin6_null;
8717 			sin6->sin6_family = AF_INET6;
8718 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8719 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8720 			    &ipif->ipif_v6net_mask);
8721 		} else {
8722 			sin = (sin_t *)&lifr->lifr_addr;
8723 			*sin = sin_null;
8724 			sin->sin_family = AF_INET;
8725 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8726 			lifr->lifr_addrlen = ip_mask_to_plen(
8727 			    ipif->ipif_net_mask);
8728 		}
8729 		lifr++;
8730 	}
8731 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8732 	rw_exit(&ipst->ips_ill_g_lock);
8733 	ipif_refrele(orig_ipif);
8734 	mp1->b_wptr = (uchar_t *)lifr;
8735 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8736 
8737 	return (0);
8738 }
8739 
8740 /* ARGSUSED */
8741 int
8742 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8743     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8744 {
8745 	mblk_t *mp1;
8746 	int	list;
8747 	ill_t	*ill;
8748 	ipif_t	*ipif;
8749 	int	flags;
8750 	int	numlifs = 0;
8751 	size_t	lifc_bufsize;
8752 	struct	lifreq *lifr;
8753 	sa_family_t	family;
8754 	struct	sockaddr_in	*sin;
8755 	struct	sockaddr_in6	*sin6;
8756 	ill_walk_context_t	ctx;
8757 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8758 	int32_t	lifclen;
8759 	zoneid_t zoneid;
8760 	STRUCT_HANDLE(lifconf, lifc);
8761 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8762 
8763 	ip1dbg(("ip_sioctl_get_lifconf"));
8764 
8765 	ASSERT(q->q_next == NULL);
8766 
8767 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8768 
8769 	/* Existence verified in ip_wput_nondata */
8770 	mp1 = mp->b_cont->b_cont;
8771 
8772 	/*
8773 	 * An extended version of SIOCGIFCONF that takes an
8774 	 * additional address family and flags field.
8775 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8776 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8777 	 * interfaces are omitted.
8778 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8779 	 * unless LIFC_TEMPORARY is specified.
8780 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8781 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8782 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8783 	 * has priority over LIFC_NOXMIT.
8784 	 */
8785 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8786 
8787 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8788 		return (EINVAL);
8789 
8790 	/*
8791 	 * Must be (better be!) continuation of a TRANSPARENT
8792 	 * IOCTL.  We just copied in the lifconf structure.
8793 	 */
8794 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8795 
8796 	family = STRUCT_FGET(lifc, lifc_family);
8797 	flags = STRUCT_FGET(lifc, lifc_flags);
8798 
8799 	switch (family) {
8800 	case AF_UNSPEC:
8801 		/*
8802 		 * walk all ILL's.
8803 		 */
8804 		list = MAX_G_HEADS;
8805 		break;
8806 	case AF_INET:
8807 		/*
8808 		 * walk only IPV4 ILL's.
8809 		 */
8810 		list = IP_V4_G_HEAD;
8811 		break;
8812 	case AF_INET6:
8813 		/*
8814 		 * walk only IPV6 ILL's.
8815 		 */
8816 		list = IP_V6_G_HEAD;
8817 		break;
8818 	default:
8819 		return (EAFNOSUPPORT);
8820 	}
8821 
8822 	/*
8823 	 * Allocate a buffer to hold requested information.
8824 	 *
8825 	 * If lifc_len is larger than what is needed, we only
8826 	 * allocate what we will use.
8827 	 *
8828 	 * If lifc_len is smaller than what is needed, return
8829 	 * EINVAL.
8830 	 */
8831 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
8832 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8833 	lifclen = STRUCT_FGET(lifc, lifc_len);
8834 	if (lifc_bufsize > lifclen) {
8835 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8836 			return (EINVAL);
8837 		else
8838 			lifc_bufsize = lifclen;
8839 	}
8840 
8841 	mp1 = mi_copyout_alloc(q, mp,
8842 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8843 	if (mp1 == NULL)
8844 		return (ENOMEM);
8845 
8846 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8847 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8848 
8849 	lifr = (struct lifreq *)mp1->b_rptr;
8850 
8851 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8852 	ill = ill_first(list, list, &ctx, ipst);
8853 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8854 		if (IS_UNDER_IPMP(ill) && !(flags & LIFC_UNDER_IPMP))
8855 			continue;
8856 
8857 		for (ipif = ill->ill_ipif; ipif != NULL;
8858 		    ipif = ipif->ipif_next) {
8859 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8860 			    !(flags & LIFC_NOXMIT))
8861 				continue;
8862 
8863 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8864 			    !(flags & LIFC_TEMPORARY))
8865 				continue;
8866 
8867 			if (((ipif->ipif_flags &
8868 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8869 			    IPIF_DEPRECATED)) ||
8870 			    IS_LOOPBACK(ill) ||
8871 			    !(ipif->ipif_flags & IPIF_UP)) &&
8872 			    (flags & LIFC_EXTERNAL_SOURCE))
8873 				continue;
8874 
8875 			if (zoneid != ipif->ipif_zoneid &&
8876 			    ipif->ipif_zoneid != ALL_ZONES &&
8877 			    (zoneid != GLOBAL_ZONEID ||
8878 			    !(flags & LIFC_ALLZONES)))
8879 				continue;
8880 
8881 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8882 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8883 					rw_exit(&ipst->ips_ill_g_lock);
8884 					return (EINVAL);
8885 				} else {
8886 					goto lif_copydone;
8887 				}
8888 			}
8889 
8890 			ipif_get_name(ipif, lifr->lifr_name,
8891 			    sizeof (lifr->lifr_name));
8892 			lifr->lifr_type = ill->ill_type;
8893 			if (ipif->ipif_isv6) {
8894 				sin6 = (sin6_t *)&lifr->lifr_addr;
8895 				*sin6 = sin6_null;
8896 				sin6->sin6_family = AF_INET6;
8897 				sin6->sin6_addr =
8898 				    ipif->ipif_v6lcl_addr;
8899 				lifr->lifr_addrlen =
8900 				    ip_mask_to_plen_v6(
8901 				    &ipif->ipif_v6net_mask);
8902 			} else {
8903 				sin = (sin_t *)&lifr->lifr_addr;
8904 				*sin = sin_null;
8905 				sin->sin_family = AF_INET;
8906 				sin->sin_addr.s_addr =
8907 				    ipif->ipif_lcl_addr;
8908 				lifr->lifr_addrlen =
8909 				    ip_mask_to_plen(
8910 				    ipif->ipif_net_mask);
8911 			}
8912 			lifr++;
8913 		}
8914 	}
8915 lif_copydone:
8916 	rw_exit(&ipst->ips_ill_g_lock);
8917 
8918 	mp1->b_wptr = (uchar_t *)lifr;
8919 	if (STRUCT_BUF(lifc) != NULL) {
8920 		STRUCT_FSET(lifc, lifc_len,
8921 		    (int)((uchar_t *)lifr - mp1->b_rptr));
8922 	}
8923 	return (0);
8924 }
8925 
8926 static void
8927 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
8928 {
8929 	ip6_asp_t *table;
8930 	size_t table_size;
8931 	mblk_t *data_mp;
8932 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8933 	ip_stack_t	*ipst;
8934 
8935 	if (q->q_next == NULL)
8936 		ipst = CONNQ_TO_IPST(q);
8937 	else
8938 		ipst = ILLQ_TO_IPST(q);
8939 
8940 	/* These two ioctls are I_STR only */
8941 	if (iocp->ioc_count == TRANSPARENT) {
8942 		miocnak(q, mp, 0, EINVAL);
8943 		return;
8944 	}
8945 
8946 	data_mp = mp->b_cont;
8947 	if (data_mp == NULL) {
8948 		/* The user passed us a NULL argument */
8949 		table = NULL;
8950 		table_size = iocp->ioc_count;
8951 	} else {
8952 		/*
8953 		 * The user provided a table.  The stream head
8954 		 * may have copied in the user data in chunks,
8955 		 * so make sure everything is pulled up
8956 		 * properly.
8957 		 */
8958 		if (MBLKL(data_mp) < iocp->ioc_count) {
8959 			mblk_t *new_data_mp;
8960 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
8961 			    NULL) {
8962 				miocnak(q, mp, 0, ENOMEM);
8963 				return;
8964 			}
8965 			freemsg(data_mp);
8966 			data_mp = new_data_mp;
8967 			mp->b_cont = data_mp;
8968 		}
8969 		table = (ip6_asp_t *)data_mp->b_rptr;
8970 		table_size = iocp->ioc_count;
8971 	}
8972 
8973 	switch (iocp->ioc_cmd) {
8974 	case SIOCGIP6ADDRPOLICY:
8975 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
8976 		if (iocp->ioc_rval == -1)
8977 			iocp->ioc_error = EINVAL;
8978 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8979 		else if (table != NULL &&
8980 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
8981 			ip6_asp_t *src = table;
8982 			ip6_asp32_t *dst = (void *)table;
8983 			int count = table_size / sizeof (ip6_asp_t);
8984 			int i;
8985 
8986 			/*
8987 			 * We need to do an in-place shrink of the array
8988 			 * to match the alignment attributes of the
8989 			 * 32-bit ABI looking at it.
8990 			 */
8991 			/* LINTED: logical expression always true: op "||" */
8992 			ASSERT(sizeof (*src) > sizeof (*dst));
8993 			for (i = 1; i < count; i++)
8994 				bcopy(src + i, dst + i, sizeof (*dst));
8995 		}
8996 #endif
8997 		break;
8998 
8999 	case SIOCSIP6ADDRPOLICY:
9000 		ASSERT(mp->b_prev == NULL);
9001 		mp->b_prev = (void *)q;
9002 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9003 		/*
9004 		 * We pass in the datamodel here so that the ip6_asp_replace()
9005 		 * routine can handle converting from 32-bit to native formats
9006 		 * where necessary.
9007 		 *
9008 		 * A better way to handle this might be to convert the inbound
9009 		 * data structure here, and hang it off a new 'mp'; thus the
9010 		 * ip6_asp_replace() logic would always be dealing with native
9011 		 * format data structures..
9012 		 *
9013 		 * (An even simpler way to handle these ioctls is to just
9014 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9015 		 * and just recompile everything that depends on it.)
9016 		 */
9017 #endif
9018 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
9019 		    iocp->ioc_flag & IOC_MODELS);
9020 		return;
9021 	}
9022 
9023 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9024 	qreply(q, mp);
9025 }
9026 
9027 static void
9028 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9029 {
9030 	mblk_t 		*data_mp;
9031 	struct dstinforeq	*dir;
9032 	uint8_t		*end, *cur;
9033 	in6_addr_t	*daddr, *saddr;
9034 	ipaddr_t	v4daddr;
9035 	ire_t		*ire;
9036 	char		*slabel, *dlabel;
9037 	boolean_t	isipv4;
9038 	int		match_ire;
9039 	ill_t		*dst_ill;
9040 	ipif_t		*src_ipif, *ire_ipif;
9041 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9042 	zoneid_t	zoneid;
9043 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
9044 
9045 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9046 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9047 
9048 	/*
9049 	 * This ioctl is I_STR only, and must have a
9050 	 * data mblk following the M_IOCTL mblk.
9051 	 */
9052 	data_mp = mp->b_cont;
9053 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9054 		miocnak(q, mp, 0, EINVAL);
9055 		return;
9056 	}
9057 
9058 	if (MBLKL(data_mp) < iocp->ioc_count) {
9059 		mblk_t *new_data_mp;
9060 
9061 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9062 			miocnak(q, mp, 0, ENOMEM);
9063 			return;
9064 		}
9065 		freemsg(data_mp);
9066 		data_mp = new_data_mp;
9067 		mp->b_cont = data_mp;
9068 	}
9069 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9070 
9071 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9072 	    end - cur >= sizeof (struct dstinforeq);
9073 	    cur += sizeof (struct dstinforeq)) {
9074 		dir = (struct dstinforeq *)cur;
9075 		daddr = &dir->dir_daddr;
9076 		saddr = &dir->dir_saddr;
9077 
9078 		/*
9079 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9080 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9081 		 * and ipif_select_source[_v6]() do not.
9082 		 */
9083 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9084 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9085 
9086 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9087 		if (isipv4) {
9088 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9089 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9090 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9091 		} else {
9092 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9093 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9094 		}
9095 		if (ire == NULL) {
9096 			dir->dir_dreachable = 0;
9097 
9098 			/* move on to next dst addr */
9099 			continue;
9100 		}
9101 		dir->dir_dreachable = 1;
9102 
9103 		ire_ipif = ire->ire_ipif;
9104 		if (ire_ipif == NULL)
9105 			goto next_dst;
9106 
9107 		/*
9108 		 * We expect to get back an interface ire or a
9109 		 * gateway ire cache entry.  For both types, the
9110 		 * output interface is ire_ipif->ipif_ill.
9111 		 */
9112 		dst_ill = ire_ipif->ipif_ill;
9113 		dir->dir_dmactype = dst_ill->ill_mactype;
9114 
9115 		if (isipv4) {
9116 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9117 		} else {
9118 			src_ipif = ipif_select_source_v6(dst_ill,
9119 			    daddr, B_FALSE, IPV6_PREFER_SRC_DEFAULT, zoneid);
9120 		}
9121 		if (src_ipif == NULL)
9122 			goto next_dst;
9123 
9124 		*saddr = src_ipif->ipif_v6lcl_addr;
9125 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9126 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9127 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9128 		dir->dir_sdeprecated =
9129 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9130 		ipif_refrele(src_ipif);
9131 next_dst:
9132 		ire_refrele(ire);
9133 	}
9134 	miocack(q, mp, iocp->ioc_count, 0);
9135 }
9136 
9137 /*
9138  * Check if this is an address assigned to this machine.
9139  * Skips interfaces that are down by using ire checks.
9140  * Translates mapped addresses to v4 addresses and then
9141  * treats them as such, returning true if the v4 address
9142  * associated with this mapped address is configured.
9143  * Note: Applications will have to be careful what they do
9144  * with the response; use of mapped addresses limits
9145  * what can be done with the socket, especially with
9146  * respect to socket options and ioctls - neither IPv4
9147  * options nor IPv6 sticky options/ancillary data options
9148  * may be used.
9149  */
9150 /* ARGSUSED */
9151 int
9152 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9153     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9154 {
9155 	struct sioc_addrreq *sia;
9156 	sin_t *sin;
9157 	ire_t *ire;
9158 	mblk_t *mp1;
9159 	zoneid_t zoneid;
9160 	ip_stack_t	*ipst;
9161 
9162 	ip1dbg(("ip_sioctl_tmyaddr"));
9163 
9164 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9165 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9166 	ipst = CONNQ_TO_IPST(q);
9167 
9168 	/* Existence verified in ip_wput_nondata */
9169 	mp1 = mp->b_cont->b_cont;
9170 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9171 	sin = (sin_t *)&sia->sa_addr;
9172 	switch (sin->sin_family) {
9173 	case AF_INET6: {
9174 		sin6_t *sin6 = (sin6_t *)sin;
9175 
9176 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9177 			ipaddr_t v4_addr;
9178 
9179 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9180 			    v4_addr);
9181 			ire = ire_ctable_lookup(v4_addr, 0,
9182 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9183 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9184 		} else {
9185 			in6_addr_t v6addr;
9186 
9187 			v6addr = sin6->sin6_addr;
9188 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9189 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9190 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9191 		}
9192 		break;
9193 	}
9194 	case AF_INET: {
9195 		ipaddr_t v4addr;
9196 
9197 		v4addr = sin->sin_addr.s_addr;
9198 		ire = ire_ctable_lookup(v4addr, 0,
9199 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9200 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9201 		break;
9202 	}
9203 	default:
9204 		return (EAFNOSUPPORT);
9205 	}
9206 	if (ire != NULL) {
9207 		sia->sa_res = 1;
9208 		ire_refrele(ire);
9209 	} else {
9210 		sia->sa_res = 0;
9211 	}
9212 	return (0);
9213 }
9214 
9215 /*
9216  * Check if this is an address assigned on-link i.e. neighbor,
9217  * and makes sure it's reachable from the current zone.
9218  * Returns true for my addresses as well.
9219  * Translates mapped addresses to v4 addresses and then
9220  * treats them as such, returning true if the v4 address
9221  * associated with this mapped address is configured.
9222  * Note: Applications will have to be careful what they do
9223  * with the response; use of mapped addresses limits
9224  * what can be done with the socket, especially with
9225  * respect to socket options and ioctls - neither IPv4
9226  * options nor IPv6 sticky options/ancillary data options
9227  * may be used.
9228  */
9229 /* ARGSUSED */
9230 int
9231 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9232     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9233 {
9234 	struct sioc_addrreq *sia;
9235 	sin_t *sin;
9236 	mblk_t	*mp1;
9237 	ire_t *ire = NULL;
9238 	zoneid_t zoneid;
9239 	ip_stack_t	*ipst;
9240 
9241 	ip1dbg(("ip_sioctl_tonlink"));
9242 
9243 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9244 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9245 	ipst = CONNQ_TO_IPST(q);
9246 
9247 	/* Existence verified in ip_wput_nondata */
9248 	mp1 = mp->b_cont->b_cont;
9249 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9250 	sin = (sin_t *)&sia->sa_addr;
9251 
9252 	/*
9253 	 * Match addresses with a zero gateway field to avoid
9254 	 * routes going through a router.
9255 	 * Exclude broadcast and multicast addresses.
9256 	 */
9257 	switch (sin->sin_family) {
9258 	case AF_INET6: {
9259 		sin6_t *sin6 = (sin6_t *)sin;
9260 
9261 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9262 			ipaddr_t v4_addr;
9263 
9264 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9265 			    v4_addr);
9266 			if (!CLASSD(v4_addr)) {
9267 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9268 				    NULL, NULL, zoneid, NULL,
9269 				    MATCH_IRE_GW, ipst);
9270 			}
9271 		} else {
9272 			in6_addr_t v6addr;
9273 			in6_addr_t v6gw;
9274 
9275 			v6addr = sin6->sin6_addr;
9276 			v6gw = ipv6_all_zeros;
9277 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9278 				ire = ire_route_lookup_v6(&v6addr, 0,
9279 				    &v6gw, 0, NULL, NULL, zoneid,
9280 				    NULL, MATCH_IRE_GW, ipst);
9281 			}
9282 		}
9283 		break;
9284 	}
9285 	case AF_INET: {
9286 		ipaddr_t v4addr;
9287 
9288 		v4addr = sin->sin_addr.s_addr;
9289 		if (!CLASSD(v4addr)) {
9290 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9291 			    NULL, NULL, zoneid, NULL,
9292 			    MATCH_IRE_GW, ipst);
9293 		}
9294 		break;
9295 	}
9296 	default:
9297 		return (EAFNOSUPPORT);
9298 	}
9299 	sia->sa_res = 0;
9300 	if (ire != NULL) {
9301 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9302 		    IRE_LOCAL|IRE_LOOPBACK)) {
9303 			sia->sa_res = 1;
9304 		}
9305 		ire_refrele(ire);
9306 	}
9307 	return (0);
9308 }
9309 
9310 /*
9311  * TBD: implement when kernel maintaines a list of site prefixes.
9312  */
9313 /* ARGSUSED */
9314 int
9315 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9316     ip_ioctl_cmd_t *ipip, void *ifreq)
9317 {
9318 	return (ENXIO);
9319 }
9320 
9321 /* ARGSUSED */
9322 int
9323 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9324     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9325 {
9326 	ill_t		*ill;
9327 	mblk_t		*mp1;
9328 	conn_t		*connp;
9329 	boolean_t	success;
9330 
9331 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9332 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9333 	/* ioctl comes down on an conn */
9334 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9335 	connp = Q_TO_CONN(q);
9336 
9337 	mp->b_datap->db_type = M_IOCTL;
9338 
9339 	/*
9340 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9341 	 * The original mp contains contaminated b_next values due to 'mi',
9342 	 * which is needed to do the mi_copy_done. Unfortunately if we
9343 	 * send down the original mblk itself and if we are popped due to an
9344 	 * an unplumb before the response comes back from tunnel,
9345 	 * the streamhead (which does a freemsg) will see this contaminated
9346 	 * message and the assertion in freemsg about non-null b_next/b_prev
9347 	 * will panic a DEBUG kernel.
9348 	 */
9349 	mp1 = copymsg(mp);
9350 	if (mp1 == NULL)
9351 		return (ENOMEM);
9352 
9353 	ill = ipif->ipif_ill;
9354 	mutex_enter(&connp->conn_lock);
9355 	mutex_enter(&ill->ill_lock);
9356 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9357 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9358 		    mp, 0);
9359 	} else {
9360 		success = ill_pending_mp_add(ill, connp, mp);
9361 	}
9362 	mutex_exit(&ill->ill_lock);
9363 	mutex_exit(&connp->conn_lock);
9364 
9365 	if (success) {
9366 		ip1dbg(("sending down tunparam request "));
9367 		putnext(ill->ill_wq, mp1);
9368 		return (EINPROGRESS);
9369 	} else {
9370 		/* The conn has started closing */
9371 		freemsg(mp1);
9372 		return (EINTR);
9373 	}
9374 }
9375 
9376 /*
9377  * ARP IOCTLs.
9378  * How does IP get in the business of fronting ARP configuration/queries?
9379  * Well it's like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9380  * are by tradition passed in through a datagram socket.  That lands in IP.
9381  * As it happens, this is just as well since the interface is quite crude in
9382  * that it passes in no information about protocol or hardware types, or
9383  * interface association.  After making the protocol assumption, IP is in
9384  * the position to look up the name of the ILL, which ARP will need, and
9385  * format a request that can be handled by ARP.  The request is passed up
9386  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9387  * back a response.  ARP supports its own set of more general IOCTLs, in
9388  * case anyone is interested.
9389  */
9390 /* ARGSUSED */
9391 int
9392 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9393     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9394 {
9395 	mblk_t *mp1;
9396 	mblk_t *mp2;
9397 	mblk_t *pending_mp;
9398 	ipaddr_t ipaddr;
9399 	area_t *area;
9400 	struct iocblk *iocp;
9401 	conn_t *connp;
9402 	struct arpreq *ar;
9403 	struct xarpreq *xar;
9404 	int flags, alength;
9405 	uchar_t *lladdr;
9406 	ire_t *ire;
9407 	ip_stack_t *ipst;
9408 	ill_t *ill = ipif->ipif_ill;
9409 	ill_t *proxy_ill = NULL;
9410 	ipmp_arpent_t *entp = NULL;
9411 	boolean_t if_arp_ioctl = B_FALSE;
9412 	boolean_t proxyarp = B_FALSE;
9413 
9414 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9415 	connp = Q_TO_CONN(q);
9416 	ipst = connp->conn_netstack->netstack_ip;
9417 
9418 	if (ipip->ipi_cmd_type == XARP_CMD) {
9419 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9420 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9421 		ar = NULL;
9422 
9423 		flags = xar->xarp_flags;
9424 		lladdr = (uchar_t *)LLADDR(&xar->xarp_ha);
9425 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
9426 		/*
9427 		 * Validate against user's link layer address length
9428 		 * input and name and addr length limits.
9429 		 */
9430 		alength = ill->ill_phys_addr_length;
9431 		if (ipip->ipi_cmd == SIOCSXARP) {
9432 			if (alength != xar->xarp_ha.sdl_alen ||
9433 			    (alength + xar->xarp_ha.sdl_nlen >
9434 			    sizeof (xar->xarp_ha.sdl_data)))
9435 				return (EINVAL);
9436 		}
9437 	} else {
9438 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9439 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9440 		xar = NULL;
9441 
9442 		flags = ar->arp_flags;
9443 		lladdr = (uchar_t *)ar->arp_ha.sa_data;
9444 		/*
9445 		 * Theoretically, the sa_family could tell us what link
9446 		 * layer type this operation is trying to deal with. By
9447 		 * common usage AF_UNSPEC means ethernet. We'll assume
9448 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9449 		 * for now. Our new SIOC*XARP ioctls can be used more
9450 		 * generally.
9451 		 *
9452 		 * If the underlying media happens to have a non 6 byte
9453 		 * address, arp module will fail set/get, but the del
9454 		 * operation will succeed.
9455 		 */
9456 		alength = 6;
9457 		if ((ipip->ipi_cmd != SIOCDARP) &&
9458 		    (alength != ill->ill_phys_addr_length)) {
9459 			return (EINVAL);
9460 		}
9461 	}
9462 
9463 	ipaddr = sin->sin_addr.s_addr;
9464 
9465 	/*
9466 	 * IPMP ARP special handling:
9467 	 *
9468 	 * 1. Since ARP mappings must appear consistent across the group,
9469 	 *    prohibit changing ARP mappings on the underlying interfaces.
9470 	 *
9471 	 * 2. Since ARP mappings for IPMP data addresses are maintained by
9472 	 *    IP itself, prohibit changing them.
9473 	 *
9474 	 * 3. For proxy ARP, use a functioning hardware address in the group,
9475 	 *    provided one exists.  If one doesn't, just add the entry as-is;
9476 	 *    ipmp_illgrp_refresh_arpent() will refresh it if things change.
9477 	 */
9478 	if (IS_UNDER_IPMP(ill)) {
9479 		if (ipip->ipi_cmd != SIOCGARP && ipip->ipi_cmd != SIOCGXARP)
9480 			return (EPERM);
9481 	}
9482 	if (IS_IPMP(ill)) {
9483 		ipmp_illgrp_t *illg = ill->ill_grp;
9484 
9485 		switch (ipip->ipi_cmd) {
9486 		case SIOCSARP:
9487 		case SIOCSXARP:
9488 			proxy_ill = ipmp_illgrp_find_ill(illg, lladdr, alength);
9489 			if (proxy_ill != NULL) {
9490 				proxyarp = B_TRUE;
9491 				if (!ipmp_ill_is_active(proxy_ill))
9492 					proxy_ill = ipmp_illgrp_next_ill(illg);
9493 				if (proxy_ill != NULL)
9494 					lladdr = proxy_ill->ill_phys_addr;
9495 			}
9496 			/* FALLTHRU */
9497 		case SIOCDARP:
9498 		case SIOCDXARP:
9499 			ire = ire_ctable_lookup(ipaddr, 0, IRE_LOCAL, NULL,
9500 			    ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
9501 			if (ire != NULL) {
9502 				ire_refrele(ire);
9503 				return (EPERM);
9504 			}
9505 		}
9506 	}
9507 
9508 	/*
9509 	 * We are going to pass up to ARP a packet chain that looks
9510 	 * like:
9511 	 *
9512 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9513 	 *
9514 	 * Get a copy of the original IOCTL mblk to head the chain,
9515 	 * to be sent up (in mp1). Also get another copy to store
9516 	 * in the ill_pending_mp list, for matching the response
9517 	 * when it comes back from ARP.
9518 	 */
9519 	mp1 = copyb(mp);
9520 	pending_mp = copymsg(mp);
9521 	if (mp1 == NULL || pending_mp == NULL) {
9522 		if (mp1 != NULL)
9523 			freeb(mp1);
9524 		if (pending_mp != NULL)
9525 			inet_freemsg(pending_mp);
9526 		return (ENOMEM);
9527 	}
9528 
9529 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9530 	    (caddr_t)&ipaddr);
9531 	if (mp2 == NULL) {
9532 		freeb(mp1);
9533 		inet_freemsg(pending_mp);
9534 		return (ENOMEM);
9535 	}
9536 	/* Put together the chain. */
9537 	mp1->b_cont = mp2;
9538 	mp1->b_datap->db_type = M_IOCTL;
9539 	mp2->b_cont = mp;
9540 	mp2->b_datap->db_type = M_DATA;
9541 
9542 	iocp = (struct iocblk *)mp1->b_rptr;
9543 
9544 	/*
9545 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9546 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9547 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9548 	 * ioc_count field; set ioc_count to be correct.
9549 	 */
9550 	iocp->ioc_count = MBLKL(mp1->b_cont);
9551 
9552 	/*
9553 	 * Set the proper command in the ARP message.
9554 	 * Convert the SIOC{G|S|D}ARP calls into our
9555 	 * AR_ENTRY_xxx calls.
9556 	 */
9557 	area = (area_t *)mp2->b_rptr;
9558 	switch (iocp->ioc_cmd) {
9559 	case SIOCDARP:
9560 	case SIOCDXARP:
9561 		/*
9562 		 * We defer deleting the corresponding IRE until
9563 		 * we return from arp.
9564 		 */
9565 		area->area_cmd = AR_ENTRY_DELETE;
9566 		area->area_proto_mask_offset = 0;
9567 		break;
9568 	case SIOCGARP:
9569 	case SIOCGXARP:
9570 		area->area_cmd = AR_ENTRY_SQUERY;
9571 		area->area_proto_mask_offset = 0;
9572 		break;
9573 	case SIOCSARP:
9574 	case SIOCSXARP:
9575 		/*
9576 		 * Delete the corresponding ire to make sure IP will
9577 		 * pick up any change from arp.
9578 		 */
9579 		if (!if_arp_ioctl) {
9580 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9581 		} else {
9582 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9583 			if (ipif != NULL) {
9584 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9585 				    ipst);
9586 				ipif_refrele(ipif);
9587 			}
9588 		}
9589 		break;
9590 	}
9591 	iocp->ioc_cmd = area->area_cmd;
9592 
9593 	/*
9594 	 * Fill in the rest of the ARP operation fields.
9595 	 */
9596 	area->area_hw_addr_length = alength;
9597 	bcopy(lladdr, (char *)area + area->area_hw_addr_offset, alength);
9598 
9599 	/* Translate the flags. */
9600 	if (flags & ATF_PERM)
9601 		area->area_flags |= ACE_F_PERMANENT;
9602 	if (flags & ATF_PUBL)
9603 		area->area_flags |= ACE_F_PUBLISH;
9604 	if (flags & ATF_AUTHORITY)
9605 		area->area_flags |= ACE_F_AUTHORITY;
9606 
9607 	/*
9608 	 * If this is a permanent AR_ENTRY_ADD on the IPMP interface, track it
9609 	 * so that IP can update ARP as the active ills in the group change.
9610 	 */
9611 	if (IS_IPMP(ill) && area->area_cmd == AR_ENTRY_ADD &&
9612 	    (area->area_flags & ACE_F_PERMANENT)) {
9613 		entp = ipmp_illgrp_create_arpent(ill->ill_grp, mp2, proxyarp);
9614 
9615 		/*
9616 		 * The second part of the conditional below handles a corner
9617 		 * case: if this is proxy ARP and the IPMP group has no active
9618 		 * interfaces, we can't send the request to ARP now since it
9619 		 * won't be able to build an ACE.  So we return success and
9620 		 * notify ARP about the proxy ARP entry once an interface
9621 		 * becomes active.
9622 		 */
9623 		if (entp == NULL || (proxyarp && proxy_ill == NULL)) {
9624 			mp2->b_cont = NULL;
9625 			inet_freemsg(mp1);
9626 			inet_freemsg(pending_mp);
9627 			return (entp == NULL ? ENOMEM : 0);
9628 		}
9629 	}
9630 
9631 	/*
9632 	 * Before sending 'mp' to ARP, we have to clear the b_next
9633 	 * and b_prev. Otherwise if STREAMS encounters such a message
9634 	 * in freemsg(), (because ARP can close any time) it can cause
9635 	 * a panic. But mi code needs the b_next and b_prev values of
9636 	 * mp->b_cont, to complete the ioctl. So we store it here
9637 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9638 	 * when the response comes down from ARP.
9639 	 */
9640 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9641 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9642 	mp->b_cont->b_next = NULL;
9643 	mp->b_cont->b_prev = NULL;
9644 
9645 	mutex_enter(&connp->conn_lock);
9646 	mutex_enter(&ill->ill_lock);
9647 	/* conn has not yet started closing, hence this can't fail */
9648 	if (ipip->ipi_flags & IPI_WR) {
9649 		VERIFY(ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9650 		    pending_mp, 0) != 0);
9651 	} else {
9652 		VERIFY(ill_pending_mp_add(ill, connp, pending_mp) != 0);
9653 	}
9654 	mutex_exit(&ill->ill_lock);
9655 	mutex_exit(&connp->conn_lock);
9656 
9657 	/*
9658 	 * Up to ARP it goes.  The response will come back in ip_wput() as an
9659 	 * M_IOCACK, and will be handed to ip_sioctl_iocack() for completion.
9660 	 */
9661 	putnext(ill->ill_rq, mp1);
9662 
9663 	/*
9664 	 * If we created an IPMP ARP entry, mark that we've notified ARP.
9665 	 */
9666 	if (entp != NULL)
9667 		ipmp_illgrp_mark_arpent(ill->ill_grp, entp);
9668 
9669 	return (EINPROGRESS);
9670 }
9671 
9672 /*
9673  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
9674  * the associated sin and refhold and return the associated ipif via `ci'.
9675  */
9676 int
9677 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
9678     cmd_info_t *ci, ipsq_func_t func)
9679 {
9680 	mblk_t	*mp1;
9681 	int	err;
9682 	sin_t	*sin;
9683 	conn_t	*connp;
9684 	ipif_t	*ipif;
9685 	ire_t	*ire = NULL;
9686 	ill_t	*ill = NULL;
9687 	boolean_t exists;
9688 	ip_stack_t *ipst;
9689 	struct arpreq *ar;
9690 	struct xarpreq *xar;
9691 	struct sockaddr_dl *sdl;
9692 
9693 	/* ioctl comes down on a conn */
9694 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9695 	connp = Q_TO_CONN(q);
9696 	if (connp->conn_af_isv6)
9697 		return (ENXIO);
9698 
9699 	ipst = connp->conn_netstack->netstack_ip;
9700 
9701 	/* Verified in ip_wput_nondata */
9702 	mp1 = mp->b_cont->b_cont;
9703 
9704 	if (ipip->ipi_cmd_type == XARP_CMD) {
9705 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
9706 		xar = (struct xarpreq *)mp1->b_rptr;
9707 		sin = (sin_t *)&xar->xarp_pa;
9708 		sdl = &xar->xarp_ha;
9709 
9710 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
9711 			return (ENXIO);
9712 		if (sdl->sdl_nlen >= LIFNAMSIZ)
9713 			return (EINVAL);
9714 	} else {
9715 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
9716 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
9717 		ar = (struct arpreq *)mp1->b_rptr;
9718 		sin = (sin_t *)&ar->arp_pa;
9719 	}
9720 
9721 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
9722 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
9723 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, CONNP_TO_WQ(connp),
9724 		    mp, func, &err, ipst);
9725 		if (ipif == NULL)
9726 			return (err);
9727 		if (ipif->ipif_id != 0) {
9728 			ipif_refrele(ipif);
9729 			return (ENXIO);
9730 		}
9731 	} else {
9732 		/*
9733 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with an sdl_nlen
9734 		 * of 0: use the IP address to find the ipif.  If the IP
9735 		 * address is an IPMP test address, ire_ftable_lookup() will
9736 		 * find the wrong ill, so we first do an ipif_lookup_addr().
9737 		 */
9738 		ipif = ipif_lookup_addr(sin->sin_addr.s_addr, NULL, ALL_ZONES,
9739 		    CONNP_TO_WQ(connp), mp, func, &err, ipst);
9740 		if (ipif == NULL) {
9741 			ire = ire_ftable_lookup(sin->sin_addr.s_addr, 0, 0,
9742 			    IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0, NULL,
9743 			    MATCH_IRE_TYPE, ipst);
9744 			if (ire == NULL || ((ill = ire_to_ill(ire)) == NULL)) {
9745 				if (ire != NULL)
9746 					ire_refrele(ire);
9747 				return (ENXIO);
9748 			}
9749 			ipif = ill->ill_ipif;
9750 			ipif_refhold(ipif);
9751 			ire_refrele(ire);
9752 		}
9753 	}
9754 
9755 	if (ipif->ipif_net_type != IRE_IF_RESOLVER) {
9756 		ipif_refrele(ipif);
9757 		return (ENXIO);
9758 	}
9759 
9760 	ci->ci_sin = sin;
9761 	ci->ci_ipif = ipif;
9762 	return (0);
9763 }
9764 
9765 /*
9766  * Link or unlink the illgrp on IPMP meta-interface `ill' depending on the
9767  * value of `ioccmd'.  While an illgrp is linked to an ipmp_grp_t, it is
9768  * accessible from that ipmp_grp_t, which means SIOCSLIFGROUPNAME can look it
9769  * up and thus an ill can join that illgrp.
9770  *
9771  * We use I_PLINK/I_PUNLINK to do the link/unlink operations rather than
9772  * open()/close() primarily because close() is not allowed to fail or block
9773  * forever.  On the other hand, I_PUNLINK *can* fail, and there's no reason
9774  * why anyone should ever need to I_PUNLINK an in-use IPMP stream.  To ensure
9775  * symmetric behavior (e.g., doing an I_PLINK after and I_PUNLINK undoes the
9776  * I_PUNLINK) we defer linking to I_PLINK.  Separately, we also fail attempts
9777  * to I_LINK since I_UNLINK is optional and we'd end up in an inconsistent
9778  * state if I_UNLINK didn't occur.
9779  *
9780  * Note that for each plumb/unplumb operation, we may end up here more than
9781  * once because of the way ifconfig works.  However, it's OK to link the same
9782  * illgrp more than once, or unlink an illgrp that's already unlinked.
9783  */
9784 static int
9785 ip_sioctl_plink_ipmp(ill_t *ill, int ioccmd)
9786 {
9787 	int err;
9788 	ip_stack_t *ipst = ill->ill_ipst;
9789 
9790 	ASSERT(IS_IPMP(ill));
9791 	ASSERT(IAM_WRITER_ILL(ill));
9792 
9793 	switch (ioccmd) {
9794 	case I_LINK:
9795 		return (ENOTSUP);
9796 
9797 	case I_PLINK:
9798 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
9799 		ipmp_illgrp_link_grp(ill->ill_grp, ill->ill_phyint->phyint_grp);
9800 		rw_exit(&ipst->ips_ipmp_lock);
9801 		break;
9802 
9803 	case I_PUNLINK:
9804 		/*
9805 		 * Require all UP ipifs be brought down prior to unlinking the
9806 		 * illgrp so any associated IREs (and other state) is torched.
9807 		 */
9808 		if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0)
9809 			return (EBUSY);
9810 
9811 		/*
9812 		 * NOTE: We hold ipmp_lock across the unlink to prevent a race
9813 		 * with an SIOCSLIFGROUPNAME request from an ill trying to
9814 		 * join this group.  Specifically: ills trying to join grab
9815 		 * ipmp_lock and bump a "pending join" counter checked by
9816 		 * ipmp_illgrp_unlink_grp().  During the unlink no new pending
9817 		 * joins can occur (since we have ipmp_lock).  Once we drop
9818 		 * ipmp_lock, subsequent SIOCSLIFGROUPNAME requests will not
9819 		 * find the illgrp (since we unlinked it) and will return
9820 		 * EAFNOSUPPORT.  This will then take them back through the
9821 		 * IPMP meta-interface plumbing logic in ifconfig, and thus
9822 		 * back through I_PLINK above.
9823 		 */
9824 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
9825 		err = ipmp_illgrp_unlink_grp(ill->ill_grp);
9826 		rw_exit(&ipst->ips_ipmp_lock);
9827 		return (err);
9828 	default:
9829 		break;
9830 	}
9831 	return (0);
9832 }
9833 
9834 /*
9835  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9836  * atomically set/clear the muxids. Also complete the ioctl by acking or
9837  * naking it.  Note that the code is structured such that the link type,
9838  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9839  * its clones use the persistent link, while pppd(1M) and perhaps many
9840  * other daemons may use non-persistent link.  When combined with some
9841  * ill_t states, linking and unlinking lower streams may be used as
9842  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9843  */
9844 /* ARGSUSED */
9845 void
9846 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9847 {
9848 	mblk_t		*mp1, *mp2;
9849 	struct linkblk	*li;
9850 	struct ipmx_s	*ipmxp;
9851 	ill_t		*ill;
9852 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
9853 	int		err = 0;
9854 	boolean_t	entered_ipsq = B_FALSE;
9855 	boolean_t	islink;
9856 	ip_stack_t	*ipst;
9857 
9858 	if (CONN_Q(q))
9859 		ipst = CONNQ_TO_IPST(q);
9860 	else
9861 		ipst = ILLQ_TO_IPST(q);
9862 
9863 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
9864 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
9865 
9866 	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9867 
9868 	mp1 = mp->b_cont;	/* This is the linkblk info */
9869 	li = (struct linkblk *)mp1->b_rptr;
9870 
9871 	/*
9872 	 * ARP has added this special mblk, and the utility is asking us
9873 	 * to perform consistency checks, and also atomically set the
9874 	 * muxid. Ifconfig is an example.  It achieves this by using
9875 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9876 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9877 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9878 	 * and other comments in this routine for more details.
9879 	 */
9880 	mp2 = mp1->b_cont;	/* This is added by ARP */
9881 
9882 	/*
9883 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9884 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9885 	 * get the special mblk above.  For backward compatibility, we
9886 	 * request ip_sioctl_plink_ipmod() to skip the consistency checks.
9887 	 * The utility will use SIOCSLIFMUXID to store the muxids.  This is
9888 	 * not atomic, and can leave the streams unplumbable if the utility
9889 	 * is interrupted before it does the SIOCSLIFMUXID.
9890 	 */
9891 	if (mp2 == NULL) {
9892 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_FALSE);
9893 		if (err == EINPROGRESS)
9894 			return;
9895 		goto done;
9896 	}
9897 
9898 	/*
9899 	 * This is an I_{P}LINK sent down by ifconfig through the ARP module;
9900 	 * ARP has appended this last mblk to tell us whether the lower stream
9901 	 * is an arp-dev stream or an IP module stream.
9902 	 */
9903 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9904 	if (ipmxp->ipmx_arpdev_stream) {
9905 		/*
9906 		 * The lower stream is the arp-dev stream.
9907 		 */
9908 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9909 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
9910 		if (ill == NULL) {
9911 			if (err == EINPROGRESS)
9912 				return;
9913 			err = EINVAL;
9914 			goto done;
9915 		}
9916 
9917 		if (ipsq == NULL) {
9918 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9919 			    NEW_OP, B_FALSE);
9920 			if (ipsq == NULL) {
9921 				ill_refrele(ill);
9922 				return;
9923 			}
9924 			entered_ipsq = B_TRUE;
9925 		}
9926 		ASSERT(IAM_WRITER_ILL(ill));
9927 		ill_refrele(ill);
9928 
9929 		/*
9930 		 * To ensure consistency between IP and ARP, the following
9931 		 * LIFO scheme is used in plink/punlink. (IP first, ARP last).
9932 		 * This is because the muxid's are stored in the IP stream on
9933 		 * the ill.
9934 		 *
9935 		 * I_{P}LINK: ifconfig plinks the IP stream before plinking
9936 		 * the ARP stream. On an arp-dev stream, IP checks that it is
9937 		 * not yet plinked, and it also checks that the corresponding
9938 		 * IP stream is already plinked.
9939 		 *
9940 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream before
9941 		 * punlinking the IP stream. IP does not allow punlink of the
9942 		 * IP stream unless the arp stream has been punlinked.
9943 		 */
9944 		if ((islink &&
9945 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9946 		    (!islink && ill->ill_arp_muxid != li->l_index)) {
9947 			err = EINVAL;
9948 			goto done;
9949 		}
9950 
9951 		if (IS_IPMP(ill) &&
9952 		    (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
9953 			goto done;
9954 
9955 		ill->ill_arp_muxid = islink ? li->l_index : 0;
9956 	} else {
9957 		/*
9958 		 * The lower stream is probably an IP module stream.  Do
9959 		 * consistency checking.
9960 		 */
9961 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_TRUE);
9962 		if (err == EINPROGRESS)
9963 			return;
9964 	}
9965 done:
9966 	if (err == 0)
9967 		miocack(q, mp, 0, 0);
9968 	else
9969 		miocnak(q, mp, 0, err);
9970 
9971 	/* Conn was refheld in ip_sioctl_copyin_setup */
9972 	if (CONN_Q(q))
9973 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9974 	if (entered_ipsq)
9975 		ipsq_exit(ipsq);
9976 }
9977 
9978 /*
9979  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
9980  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
9981  * module stream).  If `doconsist' is set, then do the extended consistency
9982  * checks requested by ifconfig(1M) and (atomically) set ill_ip_muxid here.
9983  * Returns zero on success, EINPROGRESS if the operation is still pending, or
9984  * an error code on failure.
9985  */
9986 static int
9987 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
9988     struct linkblk *li, boolean_t doconsist)
9989 {
9990 	int		err = 0;
9991 	ill_t  		*ill;
9992 	queue_t		*ipwq, *dwq;
9993 	const char	*name;
9994 	struct qinit	*qinfo;
9995 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9996 	boolean_t	entered_ipsq = B_FALSE;
9997 
9998 	/*
9999 	 * Walk the lower stream to verify it's the IP module stream.
10000 	 * The IP module is identified by its name, wput function,
10001 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
10002 	 * (li->l_qbot) will not vanish until this ioctl completes.
10003 	 */
10004 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
10005 		qinfo = ipwq->q_qinfo;
10006 		name = qinfo->qi_minfo->mi_idname;
10007 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
10008 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
10009 			break;
10010 		}
10011 	}
10012 
10013 	/*
10014 	 * If this isn't an IP module stream, bail.
10015 	 */
10016 	if (ipwq == NULL)
10017 		return (0);
10018 
10019 	ill = ipwq->q_ptr;
10020 	ASSERT(ill != NULL);
10021 
10022 	if (ipsq == NULL) {
10023 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
10024 		    NEW_OP, B_FALSE);
10025 		if (ipsq == NULL)
10026 			return (EINPROGRESS);
10027 		entered_ipsq = B_TRUE;
10028 	}
10029 	ASSERT(IAM_WRITER_ILL(ill));
10030 
10031 	if (doconsist) {
10032 		/*
10033 		 * Consistency checking requires that I_{P}LINK occurs
10034 		 * prior to setting ill_ip_muxid, and that I_{P}UNLINK
10035 		 * occurs prior to clearing ill_arp_muxid.
10036 		 */
10037 		if ((islink && ill->ill_ip_muxid != 0) ||
10038 		    (!islink && ill->ill_arp_muxid != 0)) {
10039 			err = EINVAL;
10040 			goto done;
10041 		}
10042 	}
10043 
10044 	if (IS_IPMP(ill) && (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
10045 		goto done;
10046 
10047 	/*
10048 	 * As part of I_{P}LINKing, stash the number of downstream modules and
10049 	 * the read queue of the module immediately below IP in the ill.
10050 	 * These are used during the capability negotiation below.
10051 	 */
10052 	ill->ill_lmod_rq = NULL;
10053 	ill->ill_lmod_cnt = 0;
10054 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
10055 		ill->ill_lmod_rq = RD(dwq);
10056 		for (; dwq != NULL; dwq = dwq->q_next)
10057 			ill->ill_lmod_cnt++;
10058 	}
10059 
10060 	if (doconsist)
10061 		ill->ill_ip_muxid = islink ? li->l_index : 0;
10062 
10063 	/*
10064 	 * Mark the ipsq busy until the capability operations initiated below
10065 	 * complete. The PLINK/UNLINK ioctl itself completes when our caller
10066 	 * returns, but the capability operation may complete asynchronously
10067 	 * much later.
10068 	 */
10069 	ipsq_current_start(ipsq, ill->ill_ipif, ioccmd);
10070 	/*
10071 	 * If there's at least one up ipif on this ill, then we're bound to
10072 	 * the underlying driver via DLPI.  In that case, renegotiate
10073 	 * capabilities to account for any possible change in modules
10074 	 * interposed between IP and the driver.
10075 	 */
10076 	if (ill->ill_ipif_up_count > 0) {
10077 		if (islink)
10078 			ill_capability_probe(ill);
10079 		else
10080 			ill_capability_reset(ill, B_FALSE);
10081 	}
10082 	ipsq_current_finish(ipsq);
10083 done:
10084 	if (entered_ipsq)
10085 		ipsq_exit(ipsq);
10086 
10087 	return (err);
10088 }
10089 
10090 /*
10091  * Search the ioctl command in the ioctl tables and return a pointer
10092  * to the ioctl command information. The ioctl command tables are
10093  * static and fully populated at compile time.
10094  */
10095 ip_ioctl_cmd_t *
10096 ip_sioctl_lookup(int ioc_cmd)
10097 {
10098 	int index;
10099 	ip_ioctl_cmd_t *ipip;
10100 	ip_ioctl_cmd_t *ipip_end;
10101 
10102 	if (ioc_cmd == IPI_DONTCARE)
10103 		return (NULL);
10104 
10105 	/*
10106 	 * Do a 2 step search. First search the indexed table
10107 	 * based on the least significant byte of the ioctl cmd.
10108 	 * If we don't find a match, then search the misc table
10109 	 * serially.
10110 	 */
10111 	index = ioc_cmd & 0xFF;
10112 	if (index < ip_ndx_ioctl_count) {
10113 		ipip = &ip_ndx_ioctl_table[index];
10114 		if (ipip->ipi_cmd == ioc_cmd) {
10115 			/* Found a match in the ndx table */
10116 			return (ipip);
10117 		}
10118 	}
10119 
10120 	/* Search the misc table */
10121 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10122 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10123 		if (ipip->ipi_cmd == ioc_cmd)
10124 			/* Found a match in the misc table */
10125 			return (ipip);
10126 	}
10127 
10128 	return (NULL);
10129 }
10130 
10131 /*
10132  * Wrapper function for resuming deferred ioctl processing
10133  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10134  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10135  */
10136 /* ARGSUSED */
10137 void
10138 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10139     void *dummy_arg)
10140 {
10141 	ip_sioctl_copyin_setup(q, mp);
10142 }
10143 
10144 /*
10145  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10146  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10147  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10148  * We establish here the size of the block to be copied in.  mi_copyin
10149  * arranges for this to happen, an processing continues in ip_wput with
10150  * an M_IOCDATA message.
10151  */
10152 void
10153 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10154 {
10155 	int	copyin_size;
10156 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10157 	ip_ioctl_cmd_t *ipip;
10158 	cred_t *cr;
10159 	ip_stack_t	*ipst;
10160 
10161 	if (CONN_Q(q))
10162 		ipst = CONNQ_TO_IPST(q);
10163 	else
10164 		ipst = ILLQ_TO_IPST(q);
10165 
10166 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10167 	if (ipip == NULL) {
10168 		/*
10169 		 * The ioctl is not one we understand or own.
10170 		 * Pass it along to be processed down stream,
10171 		 * if this is a module instance of IP, else nak
10172 		 * the ioctl.
10173 		 */
10174 		if (q->q_next == NULL) {
10175 			goto nak;
10176 		} else {
10177 			putnext(q, mp);
10178 			return;
10179 		}
10180 	}
10181 
10182 	/*
10183 	 * If this is deferred, then we will do all the checks when we
10184 	 * come back.
10185 	 */
10186 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10187 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10188 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10189 		return;
10190 	}
10191 
10192 	/*
10193 	 * Only allow a very small subset of IP ioctls on this stream if
10194 	 * IP is a module and not a driver. Allowing ioctls to be processed
10195 	 * in this case may cause assert failures or data corruption.
10196 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10197 	 * ioctls allowed on an IP module stream, after which this stream
10198 	 * normally becomes a multiplexor (at which time the stream head
10199 	 * will fail all ioctls).
10200 	 */
10201 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10202 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10203 			/*
10204 			 * Pass common Streams ioctls which the IP
10205 			 * module does not own or consume along to
10206 			 * be processed down stream.
10207 			 */
10208 			putnext(q, mp);
10209 			return;
10210 		} else {
10211 			goto nak;
10212 		}
10213 	}
10214 
10215 	/* Make sure we have ioctl data to process. */
10216 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10217 		goto nak;
10218 
10219 	/*
10220 	 * Prefer dblk credential over ioctl credential; some synthesized
10221 	 * ioctls have kcred set because there's no way to crhold()
10222 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10223 	 * the framework; the caller of ioctl needs to hold the reference
10224 	 * for the duration of the call).
10225 	 */
10226 	cr = msg_getcred(mp, NULL);
10227 	if (cr == NULL)
10228 		cr = iocp->ioc_cr;
10229 
10230 	/* Make sure normal users don't send down privileged ioctls */
10231 	if ((ipip->ipi_flags & IPI_PRIV) &&
10232 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10233 		/* We checked the privilege earlier but log it here */
10234 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10235 		return;
10236 	}
10237 
10238 	/*
10239 	 * The ioctl command tables can only encode fixed length
10240 	 * ioctl data. If the length is variable, the table will
10241 	 * encode the length as zero. Such special cases are handled
10242 	 * below in the switch.
10243 	 */
10244 	if (ipip->ipi_copyin_size != 0) {
10245 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10246 		return;
10247 	}
10248 
10249 	switch (iocp->ioc_cmd) {
10250 	case O_SIOCGIFCONF:
10251 	case SIOCGIFCONF:
10252 		/*
10253 		 * This IOCTL is hilarious.  See comments in
10254 		 * ip_sioctl_get_ifconf for the story.
10255 		 */
10256 		if (iocp->ioc_count == TRANSPARENT)
10257 			copyin_size = SIZEOF_STRUCT(ifconf,
10258 			    iocp->ioc_flag);
10259 		else
10260 			copyin_size = iocp->ioc_count;
10261 		mi_copyin(q, mp, NULL, copyin_size);
10262 		return;
10263 
10264 	case O_SIOCGLIFCONF:
10265 	case SIOCGLIFCONF:
10266 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10267 		mi_copyin(q, mp, NULL, copyin_size);
10268 		return;
10269 
10270 	case SIOCGLIFSRCOF:
10271 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10272 		mi_copyin(q, mp, NULL, copyin_size);
10273 		return;
10274 	case SIOCGIP6ADDRPOLICY:
10275 		ip_sioctl_ip6addrpolicy(q, mp);
10276 		ip6_asp_table_refrele(ipst);
10277 		return;
10278 
10279 	case SIOCSIP6ADDRPOLICY:
10280 		ip_sioctl_ip6addrpolicy(q, mp);
10281 		return;
10282 
10283 	case SIOCGDSTINFO:
10284 		ip_sioctl_dstinfo(q, mp);
10285 		ip6_asp_table_refrele(ipst);
10286 		return;
10287 
10288 	case I_PLINK:
10289 	case I_PUNLINK:
10290 	case I_LINK:
10291 	case I_UNLINK:
10292 		/*
10293 		 * We treat non-persistent link similarly as the persistent
10294 		 * link case, in terms of plumbing/unplumbing, as well as
10295 		 * dynamic re-plumbing events indicator.  See comments
10296 		 * in ip_sioctl_plink() for more.
10297 		 *
10298 		 * Request can be enqueued in the 'ipsq' while waiting
10299 		 * to become exclusive. So bump up the conn ref.
10300 		 */
10301 		if (CONN_Q(q))
10302 			CONN_INC_REF(Q_TO_CONN(q));
10303 		ip_sioctl_plink(NULL, q, mp, NULL);
10304 		return;
10305 
10306 	case ND_GET:
10307 	case ND_SET:
10308 		/*
10309 		 * Use of the nd table requires holding the reader lock.
10310 		 * Modifying the nd table thru nd_load/nd_unload requires
10311 		 * the writer lock.
10312 		 */
10313 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10314 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10315 			rw_exit(&ipst->ips_ip_g_nd_lock);
10316 
10317 			if (iocp->ioc_error)
10318 				iocp->ioc_count = 0;
10319 			mp->b_datap->db_type = M_IOCACK;
10320 			qreply(q, mp);
10321 			return;
10322 		}
10323 		rw_exit(&ipst->ips_ip_g_nd_lock);
10324 		/*
10325 		 * We don't understand this subioctl of ND_GET / ND_SET.
10326 		 * Maybe intended for some driver / module below us
10327 		 */
10328 		if (q->q_next) {
10329 			putnext(q, mp);
10330 		} else {
10331 			iocp->ioc_error = ENOENT;
10332 			mp->b_datap->db_type = M_IOCNAK;
10333 			iocp->ioc_count = 0;
10334 			qreply(q, mp);
10335 		}
10336 		return;
10337 
10338 	case IP_IOCTL:
10339 		ip_wput_ioctl(q, mp);
10340 		return;
10341 	default:
10342 		cmn_err(CE_PANIC, "should not happen ");
10343 	}
10344 nak:
10345 	if (mp->b_cont != NULL) {
10346 		freemsg(mp->b_cont);
10347 		mp->b_cont = NULL;
10348 	}
10349 	iocp->ioc_error = EINVAL;
10350 	mp->b_datap->db_type = M_IOCNAK;
10351 	iocp->ioc_count = 0;
10352 	qreply(q, mp);
10353 }
10354 
10355 /* ip_wput hands off ARP IOCTL responses to us */
10356 /* ARGSUSED3 */
10357 void
10358 ip_sioctl_iocack(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
10359 {
10360 	struct arpreq *ar;
10361 	struct xarpreq *xar;
10362 	area_t	*area;
10363 	mblk_t	*area_mp;
10364 	struct iocblk *iocp;
10365 	mblk_t	*orig_ioc_mp, *tmp;
10366 	struct iocblk	*orig_iocp;
10367 	ill_t *ill;
10368 	conn_t *connp = NULL;
10369 	mblk_t *pending_mp;
10370 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10371 	int *flagsp;
10372 	char *storage = NULL;
10373 	sin_t *sin;
10374 	ipaddr_t addr;
10375 	int err;
10376 	ip_stack_t *ipst;
10377 
10378 	ASSERT(ipsq == NULL || IAM_WRITER_IPSQ(ipsq));
10379 	ill = q->q_ptr;
10380 	ASSERT(ill != NULL);
10381 	ipst = ill->ill_ipst;
10382 
10383 	/*
10384 	 * We should get back from ARP a packet chain that looks like:
10385 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10386 	 */
10387 	if (!(area_mp = mp->b_cont) ||
10388 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10389 	    !(orig_ioc_mp = area_mp->b_cont) ||
10390 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10391 		freemsg(mp);
10392 		return;
10393 	}
10394 
10395 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10396 
10397 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10398 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10399 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10400 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10401 		x_arp_ioctl = B_TRUE;
10402 		xar = (struct xarpreq *)tmp->b_rptr;
10403 		sin = (sin_t *)&xar->xarp_pa;
10404 		flagsp = &xar->xarp_flags;
10405 		storage = xar->xarp_ha.sdl_data;
10406 		if (xar->xarp_ha.sdl_nlen != 0)
10407 			ifx_arp_ioctl = B_TRUE;
10408 	} else {
10409 		ar = (struct arpreq *)tmp->b_rptr;
10410 		sin = (sin_t *)&ar->arp_pa;
10411 		flagsp = &ar->arp_flags;
10412 		storage = ar->arp_ha.sa_data;
10413 	}
10414 
10415 	iocp = (struct iocblk *)mp->b_rptr;
10416 
10417 	/*
10418 	 * Find the pending message; if we're exclusive, it'll be on our IPSQ.
10419 	 * Otherwise, we can find it from our ioc_id.
10420 	 */
10421 	if (ipsq != NULL)
10422 		pending_mp = ipsq_pending_mp_get(ipsq, &connp);
10423 	else
10424 		pending_mp = ill_pending_mp_get(ill, &connp, iocp->ioc_id);
10425 
10426 	if (pending_mp == NULL) {
10427 		ASSERT(connp == NULL);
10428 		inet_freemsg(mp);
10429 		return;
10430 	}
10431 	ASSERT(connp != NULL);
10432 	q = CONNP_TO_WQ(connp);
10433 
10434 	/* Uncouple the internally generated IOCTL from the original one */
10435 	area = (area_t *)area_mp->b_rptr;
10436 	area_mp->b_cont = NULL;
10437 
10438 	/*
10439 	 * Restore the b_next and b_prev used by mi code. This is needed
10440 	 * to complete the ioctl using mi* functions. We stored them in
10441 	 * the pending mp prior to sending the request to ARP.
10442 	 */
10443 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10444 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10445 	inet_freemsg(pending_mp);
10446 
10447 	/*
10448 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10449 	 * Catch the case where there is an IRE_CACHE by no entry in the
10450 	 * arp table.
10451 	 */
10452 	addr = sin->sin_addr.s_addr;
10453 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10454 		ire_t			*ire;
10455 		dl_unitdata_req_t	*dlup;
10456 		mblk_t			*llmp;
10457 		int			addr_len;
10458 		ill_t			*ipsqill = NULL;
10459 
10460 		if (ifx_arp_ioctl) {
10461 			/*
10462 			 * There's no need to lookup the ill, since
10463 			 * we've already done that when we started
10464 			 * processing the ioctl and sent the message
10465 			 * to ARP on that ill.  So use the ill that
10466 			 * is stored in q->q_ptr.
10467 			 */
10468 			ipsqill = ill;
10469 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10470 			    ipsqill->ill_ipif, ALL_ZONES,
10471 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10472 		} else {
10473 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10474 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10475 			if (ire != NULL)
10476 				ipsqill = ire_to_ill(ire);
10477 		}
10478 
10479 		if ((x_arp_ioctl) && (ipsqill != NULL))
10480 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10481 
10482 		if (ire != NULL) {
10483 			/*
10484 			 * Since the ire obtained from cachetable is used for
10485 			 * mac addr copying below, treat an incomplete ire as if
10486 			 * as if we never found it.
10487 			 */
10488 			if (ire->ire_nce != NULL &&
10489 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10490 				ire_refrele(ire);
10491 				ire = NULL;
10492 				ipsqill = NULL;
10493 				goto errack;
10494 			}
10495 			*flagsp = ATF_INUSE;
10496 			llmp = (ire->ire_nce != NULL ?
10497 			    ire->ire_nce->nce_res_mp : NULL);
10498 			if (llmp != NULL && ipsqill != NULL) {
10499 				uchar_t *macaddr;
10500 
10501 				addr_len = ipsqill->ill_phys_addr_length;
10502 				if (x_arp_ioctl && ((addr_len +
10503 				    ipsqill->ill_name_length) >
10504 				    sizeof (xar->xarp_ha.sdl_data))) {
10505 					ire_refrele(ire);
10506 					freemsg(mp);
10507 					ip_ioctl_finish(q, orig_ioc_mp,
10508 					    EINVAL, NO_COPYOUT, ipsq);
10509 					return;
10510 				}
10511 				*flagsp |= ATF_COM;
10512 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10513 				if (ipsqill->ill_sap_length < 0)
10514 					macaddr = llmp->b_rptr +
10515 					    dlup->dl_dest_addr_offset;
10516 				else
10517 					macaddr = llmp->b_rptr +
10518 					    dlup->dl_dest_addr_offset +
10519 					    ipsqill->ill_sap_length;
10520 				/*
10521 				 * For SIOCGARP, MAC address length
10522 				 * validation has already been done
10523 				 * before the ioctl was issued to ARP to
10524 				 * allow it to progress only on 6 byte
10525 				 * addressable (ethernet like) media. Thus
10526 				 * the mac address copying can not overwrite
10527 				 * the sa_data area below.
10528 				 */
10529 				bcopy(macaddr, storage, addr_len);
10530 			}
10531 			/* Ditch the internal IOCTL. */
10532 			freemsg(mp);
10533 			ire_refrele(ire);
10534 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, ipsq);
10535 			return;
10536 		}
10537 	}
10538 
10539 	/*
10540 	 * If this was a failed AR_ENTRY_ADD or a successful AR_ENTRY_DELETE
10541 	 * on the IPMP meta-interface, ensure any ARP entries added in
10542 	 * ip_sioctl_arp() are deleted.
10543 	 */
10544 	if (IS_IPMP(ill) &&
10545 	    ((iocp->ioc_error != 0 && iocp->ioc_cmd == AR_ENTRY_ADD) ||
10546 	    ((iocp->ioc_error == 0 && iocp->ioc_cmd == AR_ENTRY_DELETE)))) {
10547 		ipmp_illgrp_t *illg = ill->ill_grp;
10548 		ipmp_arpent_t *entp;
10549 
10550 		if ((entp = ipmp_illgrp_lookup_arpent(illg, &addr)) != NULL)
10551 			ipmp_illgrp_destroy_arpent(illg, entp);
10552 	}
10553 
10554 	/*
10555 	 * Delete the coresponding IRE_CACHE if any.
10556 	 * Reset the error if there was one (in case there was no entry
10557 	 * in arp.)
10558 	 */
10559 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10560 		ipif_t *ipintf = NULL;
10561 
10562 		if (ifx_arp_ioctl) {
10563 			/*
10564 			 * There's no need to lookup the ill, since
10565 			 * we've already done that when we started
10566 			 * processing the ioctl and sent the message
10567 			 * to ARP on that ill.  So use the ill that
10568 			 * is stored in q->q_ptr.
10569 			 */
10570 			ipintf = ill->ill_ipif;
10571 		}
10572 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10573 			/*
10574 			 * The address in "addr" may be an entry for a
10575 			 * router. If that's true, then any off-net
10576 			 * IRE_CACHE entries that go through the router
10577 			 * with address "addr" must be clobbered. Use
10578 			 * ire_walk to achieve this goal.
10579 			 */
10580 			if (ifx_arp_ioctl)
10581 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10582 				    ire_delete_cache_gw, (char *)&addr, ill);
10583 			else
10584 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10585 				    ALL_ZONES, ipst);
10586 			iocp->ioc_error = 0;
10587 		}
10588 	}
10589 errack:
10590 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10591 		err = iocp->ioc_error;
10592 		freemsg(mp);
10593 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, ipsq);
10594 		return;
10595 	}
10596 
10597 	/*
10598 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10599 	 * the area_t into the struct {x}arpreq.
10600 	 */
10601 	if (x_arp_ioctl) {
10602 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10603 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10604 		    sizeof (xar->xarp_ha.sdl_data)) {
10605 			freemsg(mp);
10606 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10607 			    ipsq);
10608 			return;
10609 		}
10610 	}
10611 	*flagsp = ATF_INUSE;
10612 	if (area->area_flags & ACE_F_PERMANENT)
10613 		*flagsp |= ATF_PERM;
10614 	if (area->area_flags & ACE_F_PUBLISH)
10615 		*flagsp |= ATF_PUBL;
10616 	if (area->area_flags & ACE_F_AUTHORITY)
10617 		*flagsp |= ATF_AUTHORITY;
10618 	if (area->area_hw_addr_length != 0) {
10619 		*flagsp |= ATF_COM;
10620 		/*
10621 		 * For SIOCGARP, MAC address length validation has
10622 		 * already been done before the ioctl was issued to ARP
10623 		 * to allow it to progress only on 6 byte addressable
10624 		 * (ethernet like) media. Thus the mac address copying
10625 		 * can not overwrite the sa_data area below.
10626 		 */
10627 		bcopy((char *)area + area->area_hw_addr_offset,
10628 		    storage, area->area_hw_addr_length);
10629 	}
10630 
10631 	/* Ditch the internal IOCTL. */
10632 	freemsg(mp);
10633 	/* Complete the original. */
10634 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, ipsq);
10635 }
10636 
10637 /*
10638  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10639  * interface) create the next available logical interface for this
10640  * physical interface.
10641  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10642  * ipif with the specified name.
10643  *
10644  * If the address family is not AF_UNSPEC then set the address as well.
10645  *
10646  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10647  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10648  *
10649  * Executed as a writer on the ill.
10650  * So no lock is needed to traverse the ipif chain, or examine the
10651  * phyint flags.
10652  */
10653 /* ARGSUSED */
10654 int
10655 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10656     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10657 {
10658 	mblk_t	*mp1;
10659 	struct lifreq *lifr;
10660 	boolean_t	isv6;
10661 	boolean_t	exists;
10662 	char 	*name;
10663 	char	*endp;
10664 	char	*cp;
10665 	int	namelen;
10666 	ipif_t	*ipif;
10667 	long	id;
10668 	ipsq_t	*ipsq;
10669 	ill_t	*ill;
10670 	sin_t	*sin;
10671 	int	err = 0;
10672 	boolean_t found_sep = B_FALSE;
10673 	conn_t	*connp;
10674 	zoneid_t zoneid;
10675 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10676 
10677 	ASSERT(q->q_next == NULL);
10678 	ip1dbg(("ip_sioctl_addif\n"));
10679 	/* Existence of mp1 has been checked in ip_wput_nondata */
10680 	mp1 = mp->b_cont->b_cont;
10681 	/*
10682 	 * Null terminate the string to protect against buffer
10683 	 * overrun. String was generated by user code and may not
10684 	 * be trusted.
10685 	 */
10686 	lifr = (struct lifreq *)mp1->b_rptr;
10687 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10688 	name = lifr->lifr_name;
10689 	ASSERT(CONN_Q(q));
10690 	connp = Q_TO_CONN(q);
10691 	isv6 = connp->conn_af_isv6;
10692 	zoneid = connp->conn_zoneid;
10693 	namelen = mi_strlen(name);
10694 	if (namelen == 0)
10695 		return (EINVAL);
10696 
10697 	exists = B_FALSE;
10698 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10699 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10700 		/*
10701 		 * Allow creating lo0 using SIOCLIFADDIF.
10702 		 * can't be any other writer thread. So can pass null below
10703 		 * for the last 4 args to ipif_lookup_name.
10704 		 */
10705 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10706 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10707 		/* Prevent any further action */
10708 		if (ipif == NULL) {
10709 			return (ENOBUFS);
10710 		} else if (!exists) {
10711 			/* We created the ipif now and as writer */
10712 			ipif_refrele(ipif);
10713 			return (0);
10714 		} else {
10715 			ill = ipif->ipif_ill;
10716 			ill_refhold(ill);
10717 			ipif_refrele(ipif);
10718 		}
10719 	} else {
10720 		/* Look for a colon in the name. */
10721 		endp = &name[namelen];
10722 		for (cp = endp; --cp > name; ) {
10723 			if (*cp == IPIF_SEPARATOR_CHAR) {
10724 				found_sep = B_TRUE;
10725 				/*
10726 				 * Reject any non-decimal aliases for plumbing
10727 				 * of logical interfaces. Aliases with leading
10728 				 * zeroes are also rejected as they introduce
10729 				 * ambiguity in the naming of the interfaces.
10730 				 * Comparing with "0" takes care of all such
10731 				 * cases.
10732 				 */
10733 				if ((strncmp("0", cp+1, 1)) == 0)
10734 					return (EINVAL);
10735 
10736 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10737 				    id <= 0 || *endp != '\0') {
10738 					return (EINVAL);
10739 				}
10740 				*cp = '\0';
10741 				break;
10742 			}
10743 		}
10744 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10745 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10746 		if (found_sep)
10747 			*cp = IPIF_SEPARATOR_CHAR;
10748 		if (ill == NULL)
10749 			return (err);
10750 	}
10751 
10752 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10753 	    B_TRUE);
10754 
10755 	/*
10756 	 * Release the refhold due to the lookup, now that we are excl
10757 	 * or we are just returning
10758 	 */
10759 	ill_refrele(ill);
10760 
10761 	if (ipsq == NULL)
10762 		return (EINPROGRESS);
10763 
10764 	/* We are now exclusive on the IPSQ */
10765 	ASSERT(IAM_WRITER_ILL(ill));
10766 
10767 	if (found_sep) {
10768 		/* Now see if there is an IPIF with this unit number. */
10769 		for (ipif = ill->ill_ipif; ipif != NULL;
10770 		    ipif = ipif->ipif_next) {
10771 			if (ipif->ipif_id == id) {
10772 				err = EEXIST;
10773 				goto done;
10774 			}
10775 		}
10776 	}
10777 
10778 	/*
10779 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10780 	 * of lo0.  Plumbing for lo0:0 happens in ipif_lookup_on_name()
10781 	 * instead.
10782 	 */
10783 	if ((ipif = ipif_allocate(ill, found_sep ? id : -1, IRE_LOCAL,
10784 	    B_TRUE, B_TRUE)) == NULL) {
10785 		err = ENOBUFS;
10786 		goto done;
10787 	}
10788 
10789 	/* Return created name with ioctl */
10790 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10791 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10792 	ip1dbg(("created %s\n", lifr->lifr_name));
10793 
10794 	/* Set address */
10795 	sin = (sin_t *)&lifr->lifr_addr;
10796 	if (sin->sin_family != AF_UNSPEC) {
10797 		err = ip_sioctl_addr(ipif, sin, q, mp,
10798 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10799 	}
10800 
10801 done:
10802 	ipsq_exit(ipsq);
10803 	return (err);
10804 }
10805 
10806 /*
10807  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10808  * interface) delete it based on the IP address (on this physical interface).
10809  * Otherwise delete it based on the ipif_id.
10810  * Also, special handling to allow a removeif of lo0.
10811  */
10812 /* ARGSUSED */
10813 int
10814 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10815     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10816 {
10817 	conn_t		*connp;
10818 	ill_t		*ill = ipif->ipif_ill;
10819 	boolean_t	 success;
10820 	ip_stack_t	*ipst;
10821 
10822 	ipst = CONNQ_TO_IPST(q);
10823 
10824 	ASSERT(q->q_next == NULL);
10825 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10826 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10827 	ASSERT(IAM_WRITER_IPIF(ipif));
10828 
10829 	connp = Q_TO_CONN(q);
10830 	/*
10831 	 * Special case for unplumbing lo0 (the loopback physical interface).
10832 	 * If unplumbing lo0, the incoming address structure has been
10833 	 * initialized to all zeros. When unplumbing lo0, all its logical
10834 	 * interfaces must be removed too.
10835 	 *
10836 	 * Note that this interface may be called to remove a specific
10837 	 * loopback logical interface (eg, lo0:1). But in that case
10838 	 * ipif->ipif_id != 0 so that the code path for that case is the
10839 	 * same as any other interface (meaning it skips the code directly
10840 	 * below).
10841 	 */
10842 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10843 		if (sin->sin_family == AF_UNSPEC &&
10844 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10845 			/*
10846 			 * Mark it condemned. No new ref. will be made to ill.
10847 			 */
10848 			mutex_enter(&ill->ill_lock);
10849 			ill->ill_state_flags |= ILL_CONDEMNED;
10850 			for (ipif = ill->ill_ipif; ipif != NULL;
10851 			    ipif = ipif->ipif_next) {
10852 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10853 			}
10854 			mutex_exit(&ill->ill_lock);
10855 
10856 			ipif = ill->ill_ipif;
10857 			/* unplumb the loopback interface */
10858 			ill_delete(ill);
10859 			mutex_enter(&connp->conn_lock);
10860 			mutex_enter(&ill->ill_lock);
10861 
10862 			/* Are any references to this ill active */
10863 			if (ill_is_freeable(ill)) {
10864 				mutex_exit(&ill->ill_lock);
10865 				mutex_exit(&connp->conn_lock);
10866 				ill_delete_tail(ill);
10867 				mi_free(ill);
10868 				return (0);
10869 			}
10870 			success = ipsq_pending_mp_add(connp, ipif,
10871 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10872 			mutex_exit(&connp->conn_lock);
10873 			mutex_exit(&ill->ill_lock);
10874 			if (success)
10875 				return (EINPROGRESS);
10876 			else
10877 				return (EINTR);
10878 		}
10879 	}
10880 
10881 	if (ipif->ipif_id == 0) {
10882 		ipsq_t *ipsq;
10883 
10884 		/* Find based on address */
10885 		if (ipif->ipif_isv6) {
10886 			sin6_t *sin6;
10887 
10888 			if (sin->sin_family != AF_INET6)
10889 				return (EAFNOSUPPORT);
10890 
10891 			sin6 = (sin6_t *)sin;
10892 			/* We are a writer, so we should be able to lookup */
10893 			ipif = ipif_lookup_addr_exact_v6(&sin6->sin6_addr, ill,
10894 			    ipst);
10895 		} else {
10896 			if (sin->sin_family != AF_INET)
10897 				return (EAFNOSUPPORT);
10898 
10899 			/* We are a writer, so we should be able to lookup */
10900 			ipif = ipif_lookup_addr_exact(sin->sin_addr.s_addr, ill,
10901 			    ipst);
10902 		}
10903 		if (ipif == NULL) {
10904 			return (EADDRNOTAVAIL);
10905 		}
10906 
10907 		/*
10908 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
10909 		 * lifr_name of the physical interface but with an ip address
10910 		 * lifr_addr of a logical interface plumbed over it.
10911 		 * So update ipx_current_ipif now that ipif points to the
10912 		 * correct one.
10913 		 */
10914 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
10915 		ipsq->ipsq_xop->ipx_current_ipif = ipif;
10916 
10917 		/* This is a writer */
10918 		ipif_refrele(ipif);
10919 	}
10920 
10921 	/*
10922 	 * Can not delete instance zero since it is tied to the ill.
10923 	 */
10924 	if (ipif->ipif_id == 0)
10925 		return (EBUSY);
10926 
10927 	mutex_enter(&ill->ill_lock);
10928 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10929 	mutex_exit(&ill->ill_lock);
10930 
10931 	ipif_free(ipif);
10932 
10933 	mutex_enter(&connp->conn_lock);
10934 	mutex_enter(&ill->ill_lock);
10935 
10936 	/* Are any references to this ipif active */
10937 	if (ipif_is_freeable(ipif)) {
10938 		mutex_exit(&ill->ill_lock);
10939 		mutex_exit(&connp->conn_lock);
10940 		ipif_non_duplicate(ipif);
10941 		ipif_down_tail(ipif);
10942 		ipif_free_tail(ipif); /* frees ipif */
10943 		return (0);
10944 	}
10945 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10946 	    IPIF_FREE);
10947 	mutex_exit(&ill->ill_lock);
10948 	mutex_exit(&connp->conn_lock);
10949 	if (success)
10950 		return (EINPROGRESS);
10951 	else
10952 		return (EINTR);
10953 }
10954 
10955 /*
10956  * Restart the removeif ioctl. The refcnt has gone down to 0.
10957  * The ipif is already condemned. So can't find it thru lookups.
10958  */
10959 /* ARGSUSED */
10960 int
10961 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
10962     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10963 {
10964 	ill_t *ill = ipif->ipif_ill;
10965 
10966 	ASSERT(IAM_WRITER_IPIF(ipif));
10967 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
10968 
10969 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
10970 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
10971 
10972 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10973 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
10974 		ill_delete_tail(ill);
10975 		mi_free(ill);
10976 		return (0);
10977 	}
10978 
10979 	ipif_non_duplicate(ipif);
10980 	ipif_down_tail(ipif);
10981 	ipif_free_tail(ipif);
10982 
10983 	ILL_UNMARK_CHANGING(ill);
10984 	return (0);
10985 }
10986 
10987 /*
10988  * Set the local interface address.
10989  * Allow an address of all zero when the interface is down.
10990  */
10991 /* ARGSUSED */
10992 int
10993 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10994     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10995 {
10996 	int err = 0;
10997 	in6_addr_t v6addr;
10998 	boolean_t need_up = B_FALSE;
10999 
11000 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
11001 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11002 
11003 	ASSERT(IAM_WRITER_IPIF(ipif));
11004 
11005 	if (ipif->ipif_isv6) {
11006 		sin6_t *sin6;
11007 		ill_t *ill;
11008 		phyint_t *phyi;
11009 
11010 		if (sin->sin_family != AF_INET6)
11011 			return (EAFNOSUPPORT);
11012 
11013 		sin6 = (sin6_t *)sin;
11014 		v6addr = sin6->sin6_addr;
11015 		ill = ipif->ipif_ill;
11016 		phyi = ill->ill_phyint;
11017 
11018 		/*
11019 		 * Enforce that true multicast interfaces have a link-local
11020 		 * address for logical unit 0.
11021 		 */
11022 		if (ipif->ipif_id == 0 &&
11023 		    (ill->ill_flags & ILLF_MULTICAST) &&
11024 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11025 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11026 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11027 			return (EADDRNOTAVAIL);
11028 		}
11029 
11030 		/*
11031 		 * up interfaces shouldn't have the unspecified address
11032 		 * unless they also have the IPIF_NOLOCAL flags set and
11033 		 * have a subnet assigned.
11034 		 */
11035 		if ((ipif->ipif_flags & IPIF_UP) &&
11036 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11037 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11038 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11039 			return (EADDRNOTAVAIL);
11040 		}
11041 
11042 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11043 			return (EADDRNOTAVAIL);
11044 	} else {
11045 		ipaddr_t addr;
11046 
11047 		if (sin->sin_family != AF_INET)
11048 			return (EAFNOSUPPORT);
11049 
11050 		addr = sin->sin_addr.s_addr;
11051 
11052 		/* Allow 0 as the local address. */
11053 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11054 			return (EADDRNOTAVAIL);
11055 
11056 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11057 	}
11058 
11059 	/*
11060 	 * Even if there is no change we redo things just to rerun
11061 	 * ipif_set_default.
11062 	 */
11063 	if (ipif->ipif_flags & IPIF_UP) {
11064 		/*
11065 		 * Setting a new local address, make sure
11066 		 * we have net and subnet bcast ire's for
11067 		 * the old address if we need them.
11068 		 */
11069 		if (!ipif->ipif_isv6)
11070 			ipif_check_bcast_ires(ipif);
11071 		/*
11072 		 * If the interface is already marked up,
11073 		 * we call ipif_down which will take care
11074 		 * of ditching any IREs that have been set
11075 		 * up based on the old interface address.
11076 		 */
11077 		err = ipif_logical_down(ipif, q, mp);
11078 		if (err == EINPROGRESS)
11079 			return (err);
11080 		ipif_down_tail(ipif);
11081 		need_up = 1;
11082 	}
11083 
11084 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11085 	return (err);
11086 }
11087 
11088 int
11089 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11090     boolean_t need_up)
11091 {
11092 	in6_addr_t v6addr;
11093 	in6_addr_t ov6addr;
11094 	ipaddr_t addr;
11095 	sin6_t	*sin6;
11096 	int	sinlen;
11097 	int	err = 0;
11098 	ill_t	*ill = ipif->ipif_ill;
11099 	boolean_t need_dl_down;
11100 	boolean_t need_arp_down;
11101 	struct iocblk *iocp;
11102 
11103 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11104 
11105 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11106 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11107 	ASSERT(IAM_WRITER_IPIF(ipif));
11108 
11109 	/* Must cancel any pending timer before taking the ill_lock */
11110 	if (ipif->ipif_recovery_id != 0)
11111 		(void) untimeout(ipif->ipif_recovery_id);
11112 	ipif->ipif_recovery_id = 0;
11113 
11114 	if (ipif->ipif_isv6) {
11115 		sin6 = (sin6_t *)sin;
11116 		v6addr = sin6->sin6_addr;
11117 		sinlen = sizeof (struct sockaddr_in6);
11118 	} else {
11119 		addr = sin->sin_addr.s_addr;
11120 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11121 		sinlen = sizeof (struct sockaddr_in);
11122 	}
11123 	mutex_enter(&ill->ill_lock);
11124 	ov6addr = ipif->ipif_v6lcl_addr;
11125 	ipif->ipif_v6lcl_addr = v6addr;
11126 	sctp_update_ipif_addr(ipif, ov6addr);
11127 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11128 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11129 	} else {
11130 		ipif->ipif_v6src_addr = v6addr;
11131 	}
11132 	ipif->ipif_addr_ready = 0;
11133 
11134 	/*
11135 	 * If the interface was previously marked as a duplicate, then since
11136 	 * we've now got a "new" address, it should no longer be considered a
11137 	 * duplicate -- even if the "new" address is the same as the old one.
11138 	 * Note that if all ipifs are down, we may have a pending ARP down
11139 	 * event to handle.  This is because we want to recover from duplicates
11140 	 * and thus delay tearing down ARP until the duplicates have been
11141 	 * removed or disabled.
11142 	 */
11143 	need_dl_down = need_arp_down = B_FALSE;
11144 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11145 		need_arp_down = !need_up;
11146 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11147 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11148 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11149 			need_dl_down = B_TRUE;
11150 		}
11151 	}
11152 
11153 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11154 	    !ill->ill_is_6to4tun) {
11155 		queue_t *wqp = ill->ill_wq;
11156 
11157 		/*
11158 		 * The local address of this interface is a 6to4 address,
11159 		 * check if this interface is in fact a 6to4 tunnel or just
11160 		 * an interface configured with a 6to4 address.  We are only
11161 		 * interested in the former.
11162 		 */
11163 		if (wqp != NULL) {
11164 			while ((wqp->q_next != NULL) &&
11165 			    (wqp->q_next->q_qinfo != NULL) &&
11166 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11167 
11168 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11169 				    == TUN6TO4_MODID) {
11170 					/* set for use in IP */
11171 					ill->ill_is_6to4tun = 1;
11172 					break;
11173 				}
11174 				wqp = wqp->q_next;
11175 			}
11176 		}
11177 	}
11178 
11179 	ipif_set_default(ipif);
11180 
11181 	/*
11182 	 * When publishing an interface address change event, we only notify
11183 	 * the event listeners of the new address.  It is assumed that if they
11184 	 * actively care about the addresses assigned that they will have
11185 	 * already discovered the previous address assigned (if there was one.)
11186 	 *
11187 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11188 	 */
11189 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11190 		ill_nic_event_dispatch(ill, MAP_IPIF_ID(ipif->ipif_id),
11191 		    NE_ADDRESS_CHANGE, sin, sinlen);
11192 	}
11193 
11194 	mutex_exit(&ill->ill_lock);
11195 
11196 	if (need_up) {
11197 		/*
11198 		 * Now bring the interface back up.  If this
11199 		 * is the only IPIF for the ILL, ipif_up
11200 		 * will have to re-bind to the device, so
11201 		 * we may get back EINPROGRESS, in which
11202 		 * case, this IOCTL will get completed in
11203 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11204 		 */
11205 		err = ipif_up(ipif, q, mp);
11206 	}
11207 
11208 	if (need_dl_down)
11209 		ill_dl_down(ill);
11210 	if (need_arp_down)
11211 		ipif_resolver_down(ipif);
11212 
11213 	return (err);
11214 }
11215 
11216 /*
11217  * Restart entry point to restart the address set operation after the
11218  * refcounts have dropped to zero.
11219  */
11220 /* ARGSUSED */
11221 int
11222 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11223     ip_ioctl_cmd_t *ipip, void *ifreq)
11224 {
11225 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11226 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11227 	ASSERT(IAM_WRITER_IPIF(ipif));
11228 	ipif_down_tail(ipif);
11229 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11230 }
11231 
11232 /* ARGSUSED */
11233 int
11234 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11235     ip_ioctl_cmd_t *ipip, void *if_req)
11236 {
11237 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11238 	struct lifreq *lifr = (struct lifreq *)if_req;
11239 
11240 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11241 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11242 	/*
11243 	 * The net mask and address can't change since we have a
11244 	 * reference to the ipif. So no lock is necessary.
11245 	 */
11246 	if (ipif->ipif_isv6) {
11247 		*sin6 = sin6_null;
11248 		sin6->sin6_family = AF_INET6;
11249 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11250 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11251 		lifr->lifr_addrlen =
11252 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11253 	} else {
11254 		*sin = sin_null;
11255 		sin->sin_family = AF_INET;
11256 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11257 		if (ipip->ipi_cmd_type == LIF_CMD) {
11258 			lifr->lifr_addrlen =
11259 			    ip_mask_to_plen(ipif->ipif_net_mask);
11260 		}
11261 	}
11262 	return (0);
11263 }
11264 
11265 /*
11266  * Set the destination address for a pt-pt interface.
11267  */
11268 /* ARGSUSED */
11269 int
11270 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11271     ip_ioctl_cmd_t *ipip, void *if_req)
11272 {
11273 	int err = 0;
11274 	in6_addr_t v6addr;
11275 	boolean_t need_up = B_FALSE;
11276 
11277 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11278 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11279 	ASSERT(IAM_WRITER_IPIF(ipif));
11280 
11281 	if (ipif->ipif_isv6) {
11282 		sin6_t *sin6;
11283 
11284 		if (sin->sin_family != AF_INET6)
11285 			return (EAFNOSUPPORT);
11286 
11287 		sin6 = (sin6_t *)sin;
11288 		v6addr = sin6->sin6_addr;
11289 
11290 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11291 			return (EADDRNOTAVAIL);
11292 	} else {
11293 		ipaddr_t addr;
11294 
11295 		if (sin->sin_family != AF_INET)
11296 			return (EAFNOSUPPORT);
11297 
11298 		addr = sin->sin_addr.s_addr;
11299 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11300 			return (EADDRNOTAVAIL);
11301 
11302 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11303 	}
11304 
11305 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11306 		return (0);	/* No change */
11307 
11308 	if (ipif->ipif_flags & IPIF_UP) {
11309 		/*
11310 		 * If the interface is already marked up,
11311 		 * we call ipif_down which will take care
11312 		 * of ditching any IREs that have been set
11313 		 * up based on the old pp dst address.
11314 		 */
11315 		err = ipif_logical_down(ipif, q, mp);
11316 		if (err == EINPROGRESS)
11317 			return (err);
11318 		ipif_down_tail(ipif);
11319 		need_up = B_TRUE;
11320 	}
11321 	/*
11322 	 * could return EINPROGRESS. If so ioctl will complete in
11323 	 * ip_rput_dlpi_writer
11324 	 */
11325 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11326 	return (err);
11327 }
11328 
11329 static int
11330 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11331     boolean_t need_up)
11332 {
11333 	in6_addr_t v6addr;
11334 	ill_t	*ill = ipif->ipif_ill;
11335 	int	err = 0;
11336 	boolean_t need_dl_down;
11337 	boolean_t need_arp_down;
11338 
11339 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11340 	    ipif->ipif_id, (void *)ipif));
11341 
11342 	/* Must cancel any pending timer before taking the ill_lock */
11343 	if (ipif->ipif_recovery_id != 0)
11344 		(void) untimeout(ipif->ipif_recovery_id);
11345 	ipif->ipif_recovery_id = 0;
11346 
11347 	if (ipif->ipif_isv6) {
11348 		sin6_t *sin6;
11349 
11350 		sin6 = (sin6_t *)sin;
11351 		v6addr = sin6->sin6_addr;
11352 	} else {
11353 		ipaddr_t addr;
11354 
11355 		addr = sin->sin_addr.s_addr;
11356 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11357 	}
11358 	mutex_enter(&ill->ill_lock);
11359 	/* Set point to point destination address. */
11360 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11361 		/*
11362 		 * Allow this as a means of creating logical
11363 		 * pt-pt interfaces on top of e.g. an Ethernet.
11364 		 * XXX Undocumented HACK for testing.
11365 		 * pt-pt interfaces are created with NUD disabled.
11366 		 */
11367 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11368 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11369 		if (ipif->ipif_isv6)
11370 			ill->ill_flags |= ILLF_NONUD;
11371 	}
11372 
11373 	/*
11374 	 * If the interface was previously marked as a duplicate, then since
11375 	 * we've now got a "new" address, it should no longer be considered a
11376 	 * duplicate -- even if the "new" address is the same as the old one.
11377 	 * Note that if all ipifs are down, we may have a pending ARP down
11378 	 * event to handle.
11379 	 */
11380 	need_dl_down = need_arp_down = B_FALSE;
11381 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11382 		need_arp_down = !need_up;
11383 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11384 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11385 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11386 			need_dl_down = B_TRUE;
11387 		}
11388 	}
11389 
11390 	/* Set the new address. */
11391 	ipif->ipif_v6pp_dst_addr = v6addr;
11392 	/* Make sure subnet tracks pp_dst */
11393 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11394 	mutex_exit(&ill->ill_lock);
11395 
11396 	if (need_up) {
11397 		/*
11398 		 * Now bring the interface back up.  If this
11399 		 * is the only IPIF for the ILL, ipif_up
11400 		 * will have to re-bind to the device, so
11401 		 * we may get back EINPROGRESS, in which
11402 		 * case, this IOCTL will get completed in
11403 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11404 		 */
11405 		err = ipif_up(ipif, q, mp);
11406 	}
11407 
11408 	if (need_dl_down)
11409 		ill_dl_down(ill);
11410 	if (need_arp_down)
11411 		ipif_resolver_down(ipif);
11412 
11413 	return (err);
11414 }
11415 
11416 /*
11417  * Restart entry point to restart the dstaddress set operation after the
11418  * refcounts have dropped to zero.
11419  */
11420 /* ARGSUSED */
11421 int
11422 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11423     ip_ioctl_cmd_t *ipip, void *ifreq)
11424 {
11425 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11426 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11427 	ipif_down_tail(ipif);
11428 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11429 }
11430 
11431 /* ARGSUSED */
11432 int
11433 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11434     ip_ioctl_cmd_t *ipip, void *if_req)
11435 {
11436 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11437 
11438 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11439 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11440 	/*
11441 	 * Get point to point destination address. The addresses can't
11442 	 * change since we hold a reference to the ipif.
11443 	 */
11444 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11445 		return (EADDRNOTAVAIL);
11446 
11447 	if (ipif->ipif_isv6) {
11448 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11449 		*sin6 = sin6_null;
11450 		sin6->sin6_family = AF_INET6;
11451 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11452 	} else {
11453 		*sin = sin_null;
11454 		sin->sin_family = AF_INET;
11455 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11456 	}
11457 	return (0);
11458 }
11459 
11460 /*
11461  * Set interface flags.  Many flags require special handling (e.g.,
11462  * bringing the interface down); see below for details.
11463  *
11464  * NOTE : We really don't enforce that ipif_id zero should be used
11465  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11466  *	  is because applications generally does SICGLIFFLAGS and
11467  *	  ORs in the new flags (that affects the logical) and does a
11468  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11469  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11470  *	  flags that will be turned on is correct with respect to
11471  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11472  */
11473 /* ARGSUSED */
11474 int
11475 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11476     ip_ioctl_cmd_t *ipip, void *if_req)
11477 {
11478 	uint64_t turn_on;
11479 	uint64_t turn_off;
11480 	int	err = 0;
11481 	phyint_t *phyi;
11482 	ill_t *ill;
11483 	uint64_t intf_flags, cantchange_flags;
11484 	boolean_t phyint_flags_modified = B_FALSE;
11485 	uint64_t flags;
11486 	struct ifreq *ifr;
11487 	struct lifreq *lifr;
11488 	boolean_t set_linklocal = B_FALSE;
11489 	boolean_t zero_source = B_FALSE;
11490 
11491 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11492 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11493 
11494 	ASSERT(IAM_WRITER_IPIF(ipif));
11495 
11496 	ill = ipif->ipif_ill;
11497 	phyi = ill->ill_phyint;
11498 
11499 	if (ipip->ipi_cmd_type == IF_CMD) {
11500 		ifr = (struct ifreq *)if_req;
11501 		flags = (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11502 	} else {
11503 		lifr = (struct lifreq *)if_req;
11504 		flags = lifr->lifr_flags;
11505 	}
11506 
11507 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11508 
11509 	/*
11510 	 * Have the flags been set correctly until now?
11511 	 */
11512 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11513 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11514 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11515 	/*
11516 	 * Compare the new flags to the old, and partition
11517 	 * into those coming on and those going off.
11518 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11519 	 */
11520 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11521 		flags |= intf_flags & ~0xFFFF;
11522 
11523 	/*
11524 	 * Explicitly fail attempts to change flags that are always invalid on
11525 	 * an IPMP meta-interface.
11526 	 */
11527 	if (IS_IPMP(ill) && ((flags ^ intf_flags) & IFF_IPMP_INVALID))
11528 		return (EINVAL);
11529 
11530 	/*
11531 	 * Check which flags will change; silently ignore flags which userland
11532 	 * is not allowed to control.  (Because these flags may change between
11533 	 * SIOCGLIFFLAGS and SIOCSLIFFLAGS, and that's outside of userland's
11534 	 * control, we need to silently ignore them rather than fail.)
11535 	 */
11536 	cantchange_flags = IFF_CANTCHANGE;
11537 	if (IS_IPMP(ill))
11538 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
11539 
11540 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
11541 	if (turn_on == 0)
11542 		return (0);	/* No change */
11543 
11544 	turn_off = intf_flags & turn_on;
11545 	turn_on ^= turn_off;
11546 
11547 	/*
11548 	 * All test addresses must be IFF_DEPRECATED (to ensure source address
11549 	 * selection avoids them) -- so force IFF_DEPRECATED on, and do not
11550 	 * allow it to be turned off.
11551 	 */
11552 	if ((turn_off & (IFF_DEPRECATED|IFF_NOFAILOVER)) == IFF_DEPRECATED &&
11553 	    (turn_on|intf_flags) & IFF_NOFAILOVER)
11554 		return (EINVAL);
11555 
11556 	if (turn_on & IFF_NOFAILOVER) {
11557 		turn_on |= IFF_DEPRECATED;
11558 		flags |= IFF_DEPRECATED;
11559 	}
11560 
11561 	/*
11562 	 * On underlying interfaces, only allow applications to manage test
11563 	 * addresses -- otherwise, they may get confused when the address
11564 	 * moves as part of being brought up.  Likewise, prevent an
11565 	 * application-managed test address from being converted to a data
11566 	 * address.  To prevent migration of administratively up addresses in
11567 	 * the kernel, we don't allow them to be converted either.
11568 	 */
11569 	if (IS_UNDER_IPMP(ill)) {
11570 		const uint64_t appflags = IFF_DHCPRUNNING | IFF_ADDRCONF;
11571 
11572 		if ((turn_on & appflags) && !(flags & IFF_NOFAILOVER))
11573 			return (EINVAL);
11574 
11575 		if ((turn_off & IFF_NOFAILOVER) &&
11576 		    (flags & (appflags | IFF_UP | IFF_DUPLICATE)))
11577 			return (EINVAL);
11578 	}
11579 
11580 	/*
11581 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11582 	 * IPv6 interfaces.
11583 	 */
11584 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11585 		return (EINVAL);
11586 
11587 	/*
11588 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
11589 	 */
11590 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
11591 		return (EINVAL);
11592 
11593 	/*
11594 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11595 	 * interfaces.  It makes no sense in that context.
11596 	 */
11597 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11598 		return (EINVAL);
11599 
11600 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11601 		zero_source = B_TRUE;
11602 
11603 	/*
11604 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11605 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11606 	 * If the link local address isn't set, and can be set, it will get
11607 	 * set later on in this function.
11608 	 */
11609 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11610 	    (flags & IFF_UP) && !zero_source &&
11611 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11612 		if (ipif_cant_setlinklocal(ipif))
11613 			return (EINVAL);
11614 		set_linklocal = B_TRUE;
11615 	}
11616 
11617 	/*
11618 	 * If we modify physical interface flags, we'll potentially need to
11619 	 * send up two routing socket messages for the changes (one for the
11620 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11621 	 */
11622 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11623 		phyint_flags_modified = B_TRUE;
11624 
11625 	/*
11626 	 * All functioning PHYI_STANDBY interfaces start life PHYI_INACTIVE
11627 	 * (otherwise, we'd immediately use them, defeating standby).  Also,
11628 	 * since PHYI_INACTIVE has a separate meaning when PHYI_STANDBY is not
11629 	 * set, don't allow PHYI_STANDBY to be set if PHYI_INACTIVE is already
11630 	 * set, and clear PHYI_INACTIVE if PHYI_STANDBY is being cleared.  We
11631 	 * also don't allow PHYI_STANDBY if VNI is enabled since its semantics
11632 	 * will not be honored.
11633 	 */
11634 	if (turn_on & PHYI_STANDBY) {
11635 		/*
11636 		 * No need to grab ill_g_usesrc_lock here; see the
11637 		 * synchronization notes in ip.c.
11638 		 */
11639 		if (ill->ill_usesrc_grp_next != NULL ||
11640 		    intf_flags & PHYI_INACTIVE)
11641 			return (EINVAL);
11642 		if (!(flags & PHYI_FAILED)) {
11643 			flags |= PHYI_INACTIVE;
11644 			turn_on |= PHYI_INACTIVE;
11645 		}
11646 	}
11647 
11648 	if (turn_off & PHYI_STANDBY) {
11649 		flags &= ~PHYI_INACTIVE;
11650 		turn_off |= PHYI_INACTIVE;
11651 	}
11652 
11653 	/*
11654 	 * PHYI_FAILED and PHYI_INACTIVE are mutually exclusive; fail if both
11655 	 * would end up on.
11656 	 */
11657 	if ((flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
11658 	    (PHYI_FAILED | PHYI_INACTIVE))
11659 		return (EINVAL);
11660 
11661 	/*
11662 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11663 	 * status of the interface.
11664 	 */
11665 	if ((turn_on | turn_off) & ILLF_ROUTER)
11666 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
11667 
11668 	/*
11669 	 * If the interface is not UP and we are not going to
11670 	 * bring it UP, record the flags and return. When the
11671 	 * interface comes UP later, the right actions will be
11672 	 * taken.
11673 	 */
11674 	if (!(ipif->ipif_flags & IPIF_UP) &&
11675 	    !(turn_on & IPIF_UP)) {
11676 		/* Record new flags in their respective places. */
11677 		mutex_enter(&ill->ill_lock);
11678 		mutex_enter(&ill->ill_phyint->phyint_lock);
11679 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11680 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11681 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11682 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11683 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11684 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11685 		mutex_exit(&ill->ill_lock);
11686 		mutex_exit(&ill->ill_phyint->phyint_lock);
11687 
11688 		/*
11689 		 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the
11690 		 * same to the kernel: if any of them has been set by
11691 		 * userland, the interface cannot be used for data traffic.
11692 		 */
11693 		if ((turn_on|turn_off) &
11694 		    (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
11695 			ASSERT(!IS_IPMP(ill));
11696 			/*
11697 			 * It's possible the ill is part of an "anonymous"
11698 			 * IPMP group rather than a real group.  In that case,
11699 			 * there are no other interfaces in the group and thus
11700 			 * no need to call ipmp_phyint_refresh_active().
11701 			 */
11702 			if (IS_UNDER_IPMP(ill))
11703 				ipmp_phyint_refresh_active(phyi);
11704 		}
11705 
11706 		if (phyint_flags_modified) {
11707 			if (phyi->phyint_illv4 != NULL) {
11708 				ip_rts_ifmsg(phyi->phyint_illv4->
11709 				    ill_ipif, RTSQ_DEFAULT);
11710 			}
11711 			if (phyi->phyint_illv6 != NULL) {
11712 				ip_rts_ifmsg(phyi->phyint_illv6->
11713 				    ill_ipif, RTSQ_DEFAULT);
11714 			}
11715 		}
11716 		return (0);
11717 	} else if (set_linklocal || zero_source) {
11718 		mutex_enter(&ill->ill_lock);
11719 		if (set_linklocal)
11720 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11721 		if (zero_source)
11722 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11723 		mutex_exit(&ill->ill_lock);
11724 	}
11725 
11726 	/*
11727 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11728 	 * or point-to-point interfaces with an unspecified destination. We do
11729 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11730 	 * have a subnet assigned, which is how in.ndpd currently manages its
11731 	 * onlink prefix list when no addresses are configured with those
11732 	 * prefixes.
11733 	 */
11734 	if (ipif->ipif_isv6 &&
11735 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11736 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11737 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11738 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11739 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11740 		return (EINVAL);
11741 	}
11742 
11743 	/*
11744 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11745 	 * from being brought up.
11746 	 */
11747 	if (!ipif->ipif_isv6 &&
11748 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11749 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11750 		return (EINVAL);
11751 	}
11752 
11753 	/*
11754 	 * The only flag changes that we currently take specific action on are
11755 	 * IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL, ILLF_NOARP,
11756 	 * ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, IPIF_PREFERRED, and
11757 	 * IPIF_NOFAILOVER.  This is done by bring the ipif down, changing the
11758 	 * flags and bringing it back up again.  For IPIF_NOFAILOVER, the act
11759 	 * of bringing it back up will trigger the address to be moved.
11760 	 */
11761 	if ((turn_on|turn_off) &
11762 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11763 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED|
11764 	    IPIF_NOFAILOVER)) {
11765 		/*
11766 		 * Taking this ipif down, make sure we have
11767 		 * valid net and subnet bcast ire's for other
11768 		 * logical interfaces, if we need them.
11769 		 */
11770 		if (!ipif->ipif_isv6)
11771 			ipif_check_bcast_ires(ipif);
11772 
11773 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11774 		    !(turn_off & IPIF_UP)) {
11775 			if (ipif->ipif_flags & IPIF_UP)
11776 				ill->ill_logical_down = 1;
11777 			turn_on &= ~IPIF_UP;
11778 		}
11779 		err = ipif_down(ipif, q, mp);
11780 		ip1dbg(("ipif_down returns %d err ", err));
11781 		if (err == EINPROGRESS)
11782 			return (err);
11783 		ipif_down_tail(ipif);
11784 	}
11785 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
11786 }
11787 
11788 static int
11789 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
11790 {
11791 	ill_t	*ill;
11792 	phyint_t *phyi;
11793 	uint64_t turn_on, turn_off;
11794 	uint64_t intf_flags, cantchange_flags;
11795 	boolean_t phyint_flags_modified = B_FALSE;
11796 	int	err = 0;
11797 	boolean_t set_linklocal = B_FALSE;
11798 	boolean_t zero_source = B_FALSE;
11799 
11800 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
11801 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
11802 
11803 	ASSERT(IAM_WRITER_IPIF(ipif));
11804 
11805 	ill = ipif->ipif_ill;
11806 	phyi = ill->ill_phyint;
11807 
11808 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11809 	cantchange_flags = IFF_CANTCHANGE | IFF_UP;
11810 	if (IS_IPMP(ill))
11811 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
11812 
11813 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
11814 	turn_off = intf_flags & turn_on;
11815 	turn_on ^= turn_off;
11816 
11817 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11818 		phyint_flags_modified = B_TRUE;
11819 
11820 	/*
11821 	 * Now we change the flags. Track current value of
11822 	 * other flags in their respective places.
11823 	 */
11824 	mutex_enter(&ill->ill_lock);
11825 	mutex_enter(&phyi->phyint_lock);
11826 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11827 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11828 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11829 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11830 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11831 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11832 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
11833 		set_linklocal = B_TRUE;
11834 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
11835 	}
11836 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
11837 		zero_source = B_TRUE;
11838 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
11839 	}
11840 	mutex_exit(&ill->ill_lock);
11841 	mutex_exit(&phyi->phyint_lock);
11842 
11843 	if (set_linklocal)
11844 		(void) ipif_setlinklocal(ipif);
11845 
11846 	if (zero_source)
11847 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11848 	else
11849 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
11850 
11851 	/*
11852 	 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the same to
11853 	 * the kernel: if any of them has been set by userland, the interface
11854 	 * cannot be used for data traffic.
11855 	 */
11856 	if ((turn_on|turn_off) & (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
11857 		ASSERT(!IS_IPMP(ill));
11858 		/*
11859 		 * It's possible the ill is part of an "anonymous" IPMP group
11860 		 * rather than a real group.  In that case, there are no other
11861 		 * interfaces in the group and thus no need for us to call
11862 		 * ipmp_phyint_refresh_active().
11863 		 */
11864 		if (IS_UNDER_IPMP(ill))
11865 			ipmp_phyint_refresh_active(phyi);
11866 	}
11867 
11868 	if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
11869 		/*
11870 		 * XXX ipif_up really does not know whether a phyint flags
11871 		 * was modified or not. So, it sends up information on
11872 		 * only one routing sockets message. As we don't bring up
11873 		 * the interface and also set PHYI_ flags simultaneously
11874 		 * it should be okay.
11875 		 */
11876 		err = ipif_up(ipif, q, mp);
11877 	} else {
11878 		/*
11879 		 * Make sure routing socket sees all changes to the flags.
11880 		 * ipif_up_done* handles this when we use ipif_up.
11881 		 */
11882 		if (phyint_flags_modified) {
11883 			if (phyi->phyint_illv4 != NULL) {
11884 				ip_rts_ifmsg(phyi->phyint_illv4->
11885 				    ill_ipif, RTSQ_DEFAULT);
11886 			}
11887 			if (phyi->phyint_illv6 != NULL) {
11888 				ip_rts_ifmsg(phyi->phyint_illv6->
11889 				    ill_ipif, RTSQ_DEFAULT);
11890 			}
11891 		} else {
11892 			ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
11893 		}
11894 		/*
11895 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
11896 		 * this in need_up case.
11897 		 */
11898 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
11899 	}
11900 	return (err);
11901 }
11902 
11903 /*
11904  * Restart the flags operation now that the refcounts have dropped to zero.
11905  */
11906 /* ARGSUSED */
11907 int
11908 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11909     ip_ioctl_cmd_t *ipip, void *if_req)
11910 {
11911 	uint64_t flags;
11912 	struct ifreq *ifr = if_req;
11913 	struct lifreq *lifr = if_req;
11914 
11915 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
11916 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11917 
11918 	ipif_down_tail(ipif);
11919 	if (ipip->ipi_cmd_type == IF_CMD) {
11920 		/* cast to uint16_t prevents unwanted sign extension */
11921 		flags = (uint16_t)ifr->ifr_flags;
11922 	} else {
11923 		flags = lifr->lifr_flags;
11924 	}
11925 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
11926 }
11927 
11928 /*
11929  * Can operate on either a module or a driver queue.
11930  */
11931 /* ARGSUSED */
11932 int
11933 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11934     ip_ioctl_cmd_t *ipip, void *if_req)
11935 {
11936 	/*
11937 	 * Has the flags been set correctly till now ?
11938 	 */
11939 	ill_t *ill = ipif->ipif_ill;
11940 	phyint_t *phyi = ill->ill_phyint;
11941 
11942 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
11943 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11944 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11945 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11946 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11947 
11948 	/*
11949 	 * Need a lock since some flags can be set even when there are
11950 	 * references to the ipif.
11951 	 */
11952 	mutex_enter(&ill->ill_lock);
11953 	if (ipip->ipi_cmd_type == IF_CMD) {
11954 		struct ifreq *ifr = (struct ifreq *)if_req;
11955 
11956 		/* Get interface flags (low 16 only). */
11957 		ifr->ifr_flags = ((ipif->ipif_flags |
11958 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
11959 	} else {
11960 		struct lifreq *lifr = (struct lifreq *)if_req;
11961 
11962 		/* Get interface flags. */
11963 		lifr->lifr_flags = ipif->ipif_flags |
11964 		    ill->ill_flags | phyi->phyint_flags;
11965 	}
11966 	mutex_exit(&ill->ill_lock);
11967 	return (0);
11968 }
11969 
11970 /* ARGSUSED */
11971 int
11972 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11973     ip_ioctl_cmd_t *ipip, void *if_req)
11974 {
11975 	int mtu;
11976 	int ip_min_mtu;
11977 	struct ifreq	*ifr;
11978 	struct lifreq *lifr;
11979 	ire_t	*ire;
11980 	ip_stack_t *ipst;
11981 
11982 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
11983 	    ipif->ipif_id, (void *)ipif));
11984 	if (ipip->ipi_cmd_type == IF_CMD) {
11985 		ifr = (struct ifreq *)if_req;
11986 		mtu = ifr->ifr_metric;
11987 	} else {
11988 		lifr = (struct lifreq *)if_req;
11989 		mtu = lifr->lifr_mtu;
11990 	}
11991 
11992 	if (ipif->ipif_isv6)
11993 		ip_min_mtu = IPV6_MIN_MTU;
11994 	else
11995 		ip_min_mtu = IP_MIN_MTU;
11996 
11997 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
11998 		return (EINVAL);
11999 
12000 	/*
12001 	 * Change the MTU size in all relevant ire's.
12002 	 * Mtu change Vs. new ire creation - protocol below.
12003 	 * First change ipif_mtu and the ire_max_frag of the
12004 	 * interface ire. Then do an ire walk and change the
12005 	 * ire_max_frag of all affected ires. During ire_add
12006 	 * under the bucket lock, set the ire_max_frag of the
12007 	 * new ire being created from the ipif/ire from which
12008 	 * it is being derived. If an mtu change happens after
12009 	 * the ire is added, the new ire will be cleaned up.
12010 	 * Conversely if the mtu change happens before the ire
12011 	 * is added, ire_add will see the new value of the mtu.
12012 	 */
12013 	ipif->ipif_mtu = mtu;
12014 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12015 
12016 	if (ipif->ipif_isv6)
12017 		ire = ipif_to_ire_v6(ipif);
12018 	else
12019 		ire = ipif_to_ire(ipif);
12020 	if (ire != NULL) {
12021 		ire->ire_max_frag = ipif->ipif_mtu;
12022 		ire_refrele(ire);
12023 	}
12024 	ipst = ipif->ipif_ill->ill_ipst;
12025 	if (ipif->ipif_flags & IPIF_UP) {
12026 		if (ipif->ipif_isv6)
12027 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12028 			    ipst);
12029 		else
12030 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12031 			    ipst);
12032 	}
12033 	/* Update the MTU in SCTP's list */
12034 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12035 	return (0);
12036 }
12037 
12038 /* Get interface MTU. */
12039 /* ARGSUSED */
12040 int
12041 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12042 	ip_ioctl_cmd_t *ipip, void *if_req)
12043 {
12044 	struct ifreq	*ifr;
12045 	struct lifreq	*lifr;
12046 
12047 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12048 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12049 	if (ipip->ipi_cmd_type == IF_CMD) {
12050 		ifr = (struct ifreq *)if_req;
12051 		ifr->ifr_metric = ipif->ipif_mtu;
12052 	} else {
12053 		lifr = (struct lifreq *)if_req;
12054 		lifr->lifr_mtu = ipif->ipif_mtu;
12055 	}
12056 	return (0);
12057 }
12058 
12059 /* Set interface broadcast address. */
12060 /* ARGSUSED2 */
12061 int
12062 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12063 	ip_ioctl_cmd_t *ipip, void *if_req)
12064 {
12065 	ipaddr_t addr;
12066 	ire_t	*ire;
12067 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12068 
12069 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12070 	    ipif->ipif_id));
12071 
12072 	ASSERT(IAM_WRITER_IPIF(ipif));
12073 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12074 		return (EADDRNOTAVAIL);
12075 
12076 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12077 
12078 	if (sin->sin_family != AF_INET)
12079 		return (EAFNOSUPPORT);
12080 
12081 	addr = sin->sin_addr.s_addr;
12082 	if (ipif->ipif_flags & IPIF_UP) {
12083 		/*
12084 		 * If we are already up, make sure the new
12085 		 * broadcast address makes sense.  If it does,
12086 		 * there should be an IRE for it already.
12087 		 * Don't match on ipif, only on the ill
12088 		 * since we are sharing these now.
12089 		 */
12090 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12091 		    ipif, ALL_ZONES, NULL,
12092 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12093 		if (ire == NULL) {
12094 			return (EINVAL);
12095 		} else {
12096 			ire_refrele(ire);
12097 		}
12098 	}
12099 	/*
12100 	 * Changing the broadcast addr for this ipif.
12101 	 * Make sure we have valid net and subnet bcast
12102 	 * ire's for other logical interfaces, if needed.
12103 	 */
12104 	if (addr != ipif->ipif_brd_addr)
12105 		ipif_check_bcast_ires(ipif);
12106 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12107 	return (0);
12108 }
12109 
12110 /* Get interface broadcast address. */
12111 /* ARGSUSED */
12112 int
12113 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12114     ip_ioctl_cmd_t *ipip, void *if_req)
12115 {
12116 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12117 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12118 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12119 		return (EADDRNOTAVAIL);
12120 
12121 	/* IPIF_BROADCAST not possible with IPv6 */
12122 	ASSERT(!ipif->ipif_isv6);
12123 	*sin = sin_null;
12124 	sin->sin_family = AF_INET;
12125 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12126 	return (0);
12127 }
12128 
12129 /*
12130  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12131  */
12132 /* ARGSUSED */
12133 int
12134 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12135     ip_ioctl_cmd_t *ipip, void *if_req)
12136 {
12137 	int err = 0;
12138 	in6_addr_t v6mask;
12139 
12140 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12141 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12142 
12143 	ASSERT(IAM_WRITER_IPIF(ipif));
12144 
12145 	if (ipif->ipif_isv6) {
12146 		sin6_t *sin6;
12147 
12148 		if (sin->sin_family != AF_INET6)
12149 			return (EAFNOSUPPORT);
12150 
12151 		sin6 = (sin6_t *)sin;
12152 		v6mask = sin6->sin6_addr;
12153 	} else {
12154 		ipaddr_t mask;
12155 
12156 		if (sin->sin_family != AF_INET)
12157 			return (EAFNOSUPPORT);
12158 
12159 		mask = sin->sin_addr.s_addr;
12160 		V4MASK_TO_V6(mask, v6mask);
12161 	}
12162 
12163 	/*
12164 	 * No big deal if the interface isn't already up, or the mask
12165 	 * isn't really changing, or this is pt-pt.
12166 	 */
12167 	if (!(ipif->ipif_flags & IPIF_UP) ||
12168 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12169 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12170 		ipif->ipif_v6net_mask = v6mask;
12171 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12172 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12173 			    ipif->ipif_v6net_mask,
12174 			    ipif->ipif_v6subnet);
12175 		}
12176 		return (0);
12177 	}
12178 	/*
12179 	 * Make sure we have valid net and subnet broadcast ire's
12180 	 * for the old netmask, if needed by other logical interfaces.
12181 	 */
12182 	if (!ipif->ipif_isv6)
12183 		ipif_check_bcast_ires(ipif);
12184 
12185 	err = ipif_logical_down(ipif, q, mp);
12186 	if (err == EINPROGRESS)
12187 		return (err);
12188 	ipif_down_tail(ipif);
12189 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12190 	return (err);
12191 }
12192 
12193 static int
12194 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12195 {
12196 	in6_addr_t v6mask;
12197 	int err = 0;
12198 
12199 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12200 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12201 
12202 	if (ipif->ipif_isv6) {
12203 		sin6_t *sin6;
12204 
12205 		sin6 = (sin6_t *)sin;
12206 		v6mask = sin6->sin6_addr;
12207 	} else {
12208 		ipaddr_t mask;
12209 
12210 		mask = sin->sin_addr.s_addr;
12211 		V4MASK_TO_V6(mask, v6mask);
12212 	}
12213 
12214 	ipif->ipif_v6net_mask = v6mask;
12215 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12216 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12217 		    ipif->ipif_v6subnet);
12218 	}
12219 	err = ipif_up(ipif, q, mp);
12220 
12221 	if (err == 0 || err == EINPROGRESS) {
12222 		/*
12223 		 * The interface must be DL_BOUND if this packet has to
12224 		 * go out on the wire. Since we only go through a logical
12225 		 * down and are bound with the driver during an internal
12226 		 * down/up that is satisfied.
12227 		 */
12228 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12229 			/* Potentially broadcast an address mask reply. */
12230 			ipif_mask_reply(ipif);
12231 		}
12232 	}
12233 	return (err);
12234 }
12235 
12236 /* ARGSUSED */
12237 int
12238 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12239     ip_ioctl_cmd_t *ipip, void *if_req)
12240 {
12241 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12242 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12243 	ipif_down_tail(ipif);
12244 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12245 }
12246 
12247 /* Get interface net mask. */
12248 /* ARGSUSED */
12249 int
12250 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12251     ip_ioctl_cmd_t *ipip, void *if_req)
12252 {
12253 	struct lifreq *lifr = (struct lifreq *)if_req;
12254 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12255 
12256 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12257 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12258 
12259 	/*
12260 	 * net mask can't change since we have a reference to the ipif.
12261 	 */
12262 	if (ipif->ipif_isv6) {
12263 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12264 		*sin6 = sin6_null;
12265 		sin6->sin6_family = AF_INET6;
12266 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12267 		lifr->lifr_addrlen =
12268 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12269 	} else {
12270 		*sin = sin_null;
12271 		sin->sin_family = AF_INET;
12272 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12273 		if (ipip->ipi_cmd_type == LIF_CMD) {
12274 			lifr->lifr_addrlen =
12275 			    ip_mask_to_plen(ipif->ipif_net_mask);
12276 		}
12277 	}
12278 	return (0);
12279 }
12280 
12281 /* ARGSUSED */
12282 int
12283 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12284     ip_ioctl_cmd_t *ipip, void *if_req)
12285 {
12286 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12287 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12288 
12289 	/*
12290 	 * Since no applications should ever be setting metrics on underlying
12291 	 * interfaces, we explicitly fail to smoke 'em out.
12292 	 */
12293 	if (IS_UNDER_IPMP(ipif->ipif_ill))
12294 		return (EINVAL);
12295 
12296 	/*
12297 	 * Set interface metric.  We don't use this for
12298 	 * anything but we keep track of it in case it is
12299 	 * important to routing applications or such.
12300 	 */
12301 	if (ipip->ipi_cmd_type == IF_CMD) {
12302 		struct ifreq    *ifr;
12303 
12304 		ifr = (struct ifreq *)if_req;
12305 		ipif->ipif_metric = ifr->ifr_metric;
12306 	} else {
12307 		struct lifreq   *lifr;
12308 
12309 		lifr = (struct lifreq *)if_req;
12310 		ipif->ipif_metric = lifr->lifr_metric;
12311 	}
12312 	return (0);
12313 }
12314 
12315 /* ARGSUSED */
12316 int
12317 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12318     ip_ioctl_cmd_t *ipip, void *if_req)
12319 {
12320 	/* Get interface metric. */
12321 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12322 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12323 
12324 	if (ipip->ipi_cmd_type == IF_CMD) {
12325 		struct ifreq    *ifr;
12326 
12327 		ifr = (struct ifreq *)if_req;
12328 		ifr->ifr_metric = ipif->ipif_metric;
12329 	} else {
12330 		struct lifreq   *lifr;
12331 
12332 		lifr = (struct lifreq *)if_req;
12333 		lifr->lifr_metric = ipif->ipif_metric;
12334 	}
12335 
12336 	return (0);
12337 }
12338 
12339 /* ARGSUSED */
12340 int
12341 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12342     ip_ioctl_cmd_t *ipip, void *if_req)
12343 {
12344 
12345 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12346 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12347 	/*
12348 	 * Set the muxid returned from I_PLINK.
12349 	 */
12350 	if (ipip->ipi_cmd_type == IF_CMD) {
12351 		struct ifreq *ifr = (struct ifreq *)if_req;
12352 
12353 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12354 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12355 	} else {
12356 		struct lifreq *lifr = (struct lifreq *)if_req;
12357 
12358 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12359 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12360 	}
12361 	return (0);
12362 }
12363 
12364 /* ARGSUSED */
12365 int
12366 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12367     ip_ioctl_cmd_t *ipip, void *if_req)
12368 {
12369 
12370 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12371 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12372 	/*
12373 	 * Get the muxid saved in ill for I_PUNLINK.
12374 	 */
12375 	if (ipip->ipi_cmd_type == IF_CMD) {
12376 		struct ifreq *ifr = (struct ifreq *)if_req;
12377 
12378 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12379 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12380 	} else {
12381 		struct lifreq *lifr = (struct lifreq *)if_req;
12382 
12383 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12384 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12385 	}
12386 	return (0);
12387 }
12388 
12389 /*
12390  * Set the subnet prefix. Does not modify the broadcast address.
12391  */
12392 /* ARGSUSED */
12393 int
12394 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12395     ip_ioctl_cmd_t *ipip, void *if_req)
12396 {
12397 	int err = 0;
12398 	in6_addr_t v6addr;
12399 	in6_addr_t v6mask;
12400 	boolean_t need_up = B_FALSE;
12401 	int addrlen;
12402 
12403 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12404 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12405 
12406 	ASSERT(IAM_WRITER_IPIF(ipif));
12407 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12408 
12409 	if (ipif->ipif_isv6) {
12410 		sin6_t *sin6;
12411 
12412 		if (sin->sin_family != AF_INET6)
12413 			return (EAFNOSUPPORT);
12414 
12415 		sin6 = (sin6_t *)sin;
12416 		v6addr = sin6->sin6_addr;
12417 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12418 			return (EADDRNOTAVAIL);
12419 	} else {
12420 		ipaddr_t addr;
12421 
12422 		if (sin->sin_family != AF_INET)
12423 			return (EAFNOSUPPORT);
12424 
12425 		addr = sin->sin_addr.s_addr;
12426 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12427 			return (EADDRNOTAVAIL);
12428 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12429 		/* Add 96 bits */
12430 		addrlen += IPV6_ABITS - IP_ABITS;
12431 	}
12432 
12433 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12434 		return (EINVAL);
12435 
12436 	/* Check if bits in the address is set past the mask */
12437 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12438 		return (EINVAL);
12439 
12440 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12441 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12442 		return (0);	/* No change */
12443 
12444 	if (ipif->ipif_flags & IPIF_UP) {
12445 		/*
12446 		 * If the interface is already marked up,
12447 		 * we call ipif_down which will take care
12448 		 * of ditching any IREs that have been set
12449 		 * up based on the old interface address.
12450 		 */
12451 		err = ipif_logical_down(ipif, q, mp);
12452 		if (err == EINPROGRESS)
12453 			return (err);
12454 		ipif_down_tail(ipif);
12455 		need_up = B_TRUE;
12456 	}
12457 
12458 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12459 	return (err);
12460 }
12461 
12462 static int
12463 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12464     queue_t *q, mblk_t *mp, boolean_t need_up)
12465 {
12466 	ill_t	*ill = ipif->ipif_ill;
12467 	int	err = 0;
12468 
12469 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12470 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12471 
12472 	/* Set the new address. */
12473 	mutex_enter(&ill->ill_lock);
12474 	ipif->ipif_v6net_mask = v6mask;
12475 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12476 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12477 		    ipif->ipif_v6subnet);
12478 	}
12479 	mutex_exit(&ill->ill_lock);
12480 
12481 	if (need_up) {
12482 		/*
12483 		 * Now bring the interface back up.  If this
12484 		 * is the only IPIF for the ILL, ipif_up
12485 		 * will have to re-bind to the device, so
12486 		 * we may get back EINPROGRESS, in which
12487 		 * case, this IOCTL will get completed in
12488 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12489 		 */
12490 		err = ipif_up(ipif, q, mp);
12491 		if (err == EINPROGRESS)
12492 			return (err);
12493 	}
12494 	return (err);
12495 }
12496 
12497 /* ARGSUSED */
12498 int
12499 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12500     ip_ioctl_cmd_t *ipip, void *if_req)
12501 {
12502 	int	addrlen;
12503 	in6_addr_t v6addr;
12504 	in6_addr_t v6mask;
12505 	struct lifreq *lifr = (struct lifreq *)if_req;
12506 
12507 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12508 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12509 	ipif_down_tail(ipif);
12510 
12511 	addrlen = lifr->lifr_addrlen;
12512 	if (ipif->ipif_isv6) {
12513 		sin6_t *sin6;
12514 
12515 		sin6 = (sin6_t *)sin;
12516 		v6addr = sin6->sin6_addr;
12517 	} else {
12518 		ipaddr_t addr;
12519 
12520 		addr = sin->sin_addr.s_addr;
12521 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12522 		addrlen += IPV6_ABITS - IP_ABITS;
12523 	}
12524 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12525 
12526 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12527 }
12528 
12529 /* ARGSUSED */
12530 int
12531 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12532     ip_ioctl_cmd_t *ipip, void *if_req)
12533 {
12534 	struct lifreq *lifr = (struct lifreq *)if_req;
12535 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12536 
12537 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12538 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12539 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12540 
12541 	if (ipif->ipif_isv6) {
12542 		*sin6 = sin6_null;
12543 		sin6->sin6_family = AF_INET6;
12544 		sin6->sin6_addr = ipif->ipif_v6subnet;
12545 		lifr->lifr_addrlen =
12546 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12547 	} else {
12548 		*sin = sin_null;
12549 		sin->sin_family = AF_INET;
12550 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12551 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12552 	}
12553 	return (0);
12554 }
12555 
12556 /*
12557  * Set the IPv6 address token.
12558  */
12559 /* ARGSUSED */
12560 int
12561 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12562     ip_ioctl_cmd_t *ipi, void *if_req)
12563 {
12564 	ill_t *ill = ipif->ipif_ill;
12565 	int err;
12566 	in6_addr_t v6addr;
12567 	in6_addr_t v6mask;
12568 	boolean_t need_up = B_FALSE;
12569 	int i;
12570 	sin6_t *sin6 = (sin6_t *)sin;
12571 	struct lifreq *lifr = (struct lifreq *)if_req;
12572 	int addrlen;
12573 
12574 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12575 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12576 	ASSERT(IAM_WRITER_IPIF(ipif));
12577 
12578 	addrlen = lifr->lifr_addrlen;
12579 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12580 	if (ipif->ipif_id != 0)
12581 		return (EINVAL);
12582 
12583 	if (!ipif->ipif_isv6)
12584 		return (EINVAL);
12585 
12586 	if (addrlen > IPV6_ABITS)
12587 		return (EINVAL);
12588 
12589 	v6addr = sin6->sin6_addr;
12590 
12591 	/*
12592 	 * The length of the token is the length from the end.  To get
12593 	 * the proper mask for this, compute the mask of the bits not
12594 	 * in the token; ie. the prefix, and then xor to get the mask.
12595 	 */
12596 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12597 		return (EINVAL);
12598 	for (i = 0; i < 4; i++) {
12599 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12600 	}
12601 
12602 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12603 	    ill->ill_token_length == addrlen)
12604 		return (0);	/* No change */
12605 
12606 	if (ipif->ipif_flags & IPIF_UP) {
12607 		err = ipif_logical_down(ipif, q, mp);
12608 		if (err == EINPROGRESS)
12609 			return (err);
12610 		ipif_down_tail(ipif);
12611 		need_up = B_TRUE;
12612 	}
12613 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12614 	return (err);
12615 }
12616 
12617 static int
12618 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12619     mblk_t *mp, boolean_t need_up)
12620 {
12621 	in6_addr_t v6addr;
12622 	in6_addr_t v6mask;
12623 	ill_t	*ill = ipif->ipif_ill;
12624 	int	i;
12625 	int	err = 0;
12626 
12627 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12628 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12629 	v6addr = sin6->sin6_addr;
12630 	/*
12631 	 * The length of the token is the length from the end.  To get
12632 	 * the proper mask for this, compute the mask of the bits not
12633 	 * in the token; ie. the prefix, and then xor to get the mask.
12634 	 */
12635 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12636 	for (i = 0; i < 4; i++)
12637 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12638 
12639 	mutex_enter(&ill->ill_lock);
12640 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12641 	ill->ill_token_length = addrlen;
12642 	mutex_exit(&ill->ill_lock);
12643 
12644 	if (need_up) {
12645 		/*
12646 		 * Now bring the interface back up.  If this
12647 		 * is the only IPIF for the ILL, ipif_up
12648 		 * will have to re-bind to the device, so
12649 		 * we may get back EINPROGRESS, in which
12650 		 * case, this IOCTL will get completed in
12651 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12652 		 */
12653 		err = ipif_up(ipif, q, mp);
12654 		if (err == EINPROGRESS)
12655 			return (err);
12656 	}
12657 	return (err);
12658 }
12659 
12660 /* ARGSUSED */
12661 int
12662 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12663     ip_ioctl_cmd_t *ipi, void *if_req)
12664 {
12665 	ill_t *ill;
12666 	sin6_t *sin6 = (sin6_t *)sin;
12667 	struct lifreq *lifr = (struct lifreq *)if_req;
12668 
12669 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12670 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12671 	if (ipif->ipif_id != 0)
12672 		return (EINVAL);
12673 
12674 	ill = ipif->ipif_ill;
12675 	if (!ill->ill_isv6)
12676 		return (ENXIO);
12677 
12678 	*sin6 = sin6_null;
12679 	sin6->sin6_family = AF_INET6;
12680 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12681 	sin6->sin6_addr = ill->ill_token;
12682 	lifr->lifr_addrlen = ill->ill_token_length;
12683 	return (0);
12684 }
12685 
12686 /*
12687  * Set (hardware) link specific information that might override
12688  * what was acquired through the DL_INFO_ACK.
12689  * The logic is as follows.
12690  *
12691  * become exclusive
12692  * set CHANGING flag
12693  * change mtu on affected IREs
12694  * clear CHANGING flag
12695  *
12696  * An ire add that occurs before the CHANGING flag is set will have its mtu
12697  * changed by the ip_sioctl_lnkinfo.
12698  *
12699  * During the time the CHANGING flag is set, no new ires will be added to the
12700  * bucket, and ire add will fail (due the CHANGING flag).
12701  *
12702  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12703  * before it is added to the bucket.
12704  *
12705  * Obviously only 1 thread can set the CHANGING flag and we need to become
12706  * exclusive to set the flag.
12707  */
12708 /* ARGSUSED */
12709 int
12710 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12711     ip_ioctl_cmd_t *ipi, void *if_req)
12712 {
12713 	ill_t		*ill = ipif->ipif_ill;
12714 	ipif_t		*nipif;
12715 	int		ip_min_mtu;
12716 	boolean_t	mtu_walk = B_FALSE;
12717 	struct lifreq	*lifr = (struct lifreq *)if_req;
12718 	lif_ifinfo_req_t *lir;
12719 	ire_t		*ire;
12720 
12721 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12722 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12723 	lir = &lifr->lifr_ifinfo;
12724 	ASSERT(IAM_WRITER_IPIF(ipif));
12725 
12726 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12727 	if (ipif->ipif_id != 0)
12728 		return (EINVAL);
12729 
12730 	/* Set interface MTU. */
12731 	if (ipif->ipif_isv6)
12732 		ip_min_mtu = IPV6_MIN_MTU;
12733 	else
12734 		ip_min_mtu = IP_MIN_MTU;
12735 
12736 	/*
12737 	 * Verify values before we set anything. Allow zero to
12738 	 * mean unspecified.
12739 	 */
12740 	if (lir->lir_maxmtu != 0 &&
12741 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12742 	    lir->lir_maxmtu < ip_min_mtu))
12743 		return (EINVAL);
12744 	if (lir->lir_reachtime != 0 &&
12745 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12746 		return (EINVAL);
12747 	if (lir->lir_reachretrans != 0 &&
12748 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12749 		return (EINVAL);
12750 
12751 	mutex_enter(&ill->ill_lock);
12752 	ill->ill_state_flags |= ILL_CHANGING;
12753 	for (nipif = ill->ill_ipif; nipif != NULL;
12754 	    nipif = nipif->ipif_next) {
12755 		nipif->ipif_state_flags |= IPIF_CHANGING;
12756 	}
12757 
12758 	if (lir->lir_maxmtu != 0) {
12759 		ill->ill_max_mtu = lir->lir_maxmtu;
12760 		ill->ill_user_mtu = lir->lir_maxmtu;
12761 		mtu_walk = B_TRUE;
12762 	}
12763 	mutex_exit(&ill->ill_lock);
12764 
12765 	if (lir->lir_reachtime != 0)
12766 		ill->ill_reachable_time = lir->lir_reachtime;
12767 
12768 	if (lir->lir_reachretrans != 0)
12769 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12770 
12771 	ill->ill_max_hops = lir->lir_maxhops;
12772 
12773 	ill->ill_max_buf = ND_MAX_Q;
12774 
12775 	if (mtu_walk) {
12776 		/*
12777 		 * Set the MTU on all ipifs associated with this ill except
12778 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12779 		 */
12780 		for (nipif = ill->ill_ipif; nipif != NULL;
12781 		    nipif = nipif->ipif_next) {
12782 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12783 				continue;
12784 
12785 			nipif->ipif_mtu = ill->ill_max_mtu;
12786 
12787 			if (!(nipif->ipif_flags & IPIF_UP))
12788 				continue;
12789 
12790 			if (nipif->ipif_isv6)
12791 				ire = ipif_to_ire_v6(nipif);
12792 			else
12793 				ire = ipif_to_ire(nipif);
12794 			if (ire != NULL) {
12795 				ire->ire_max_frag = ipif->ipif_mtu;
12796 				ire_refrele(ire);
12797 			}
12798 
12799 			ire_walk_ill(MATCH_IRE_ILL, 0, ipif_mtu_change,
12800 			    nipif, ill);
12801 		}
12802 	}
12803 
12804 	mutex_enter(&ill->ill_lock);
12805 	for (nipif = ill->ill_ipif; nipif != NULL;
12806 	    nipif = nipif->ipif_next) {
12807 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
12808 	}
12809 	ILL_UNMARK_CHANGING(ill);
12810 	mutex_exit(&ill->ill_lock);
12811 
12812 	/*
12813 	 * Refresh IPMP meta-interface MTU if necessary.
12814 	 */
12815 	if (IS_UNDER_IPMP(ill))
12816 		ipmp_illgrp_refresh_mtu(ill->ill_grp);
12817 
12818 	return (0);
12819 }
12820 
12821 /* ARGSUSED */
12822 int
12823 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12824     ip_ioctl_cmd_t *ipi, void *if_req)
12825 {
12826 	struct lif_ifinfo_req *lir;
12827 	ill_t *ill = ipif->ipif_ill;
12828 
12829 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
12830 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12831 	if (ipif->ipif_id != 0)
12832 		return (EINVAL);
12833 
12834 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
12835 	lir->lir_maxhops = ill->ill_max_hops;
12836 	lir->lir_reachtime = ill->ill_reachable_time;
12837 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
12838 	lir->lir_maxmtu = ill->ill_max_mtu;
12839 
12840 	return (0);
12841 }
12842 
12843 /*
12844  * Return best guess as to the subnet mask for the specified address.
12845  * Based on the subnet masks for all the configured interfaces.
12846  *
12847  * We end up returning a zero mask in the case of default, multicast or
12848  * experimental.
12849  */
12850 static ipaddr_t
12851 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
12852 {
12853 	ipaddr_t net_mask;
12854 	ill_t	*ill;
12855 	ipif_t	*ipif;
12856 	ill_walk_context_t ctx;
12857 	ipif_t	*fallback_ipif = NULL;
12858 
12859 	net_mask = ip_net_mask(addr);
12860 	if (net_mask == 0) {
12861 		*ipifp = NULL;
12862 		return (0);
12863 	}
12864 
12865 	/* Let's check to see if this is maybe a local subnet route. */
12866 	/* this function only applies to IPv4 interfaces */
12867 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
12868 	ill = ILL_START_WALK_V4(&ctx, ipst);
12869 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
12870 		mutex_enter(&ill->ill_lock);
12871 		for (ipif = ill->ill_ipif; ipif != NULL;
12872 		    ipif = ipif->ipif_next) {
12873 			if (!IPIF_CAN_LOOKUP(ipif))
12874 				continue;
12875 			if (!(ipif->ipif_flags & IPIF_UP))
12876 				continue;
12877 			if ((ipif->ipif_subnet & net_mask) ==
12878 			    (addr & net_mask)) {
12879 				/*
12880 				 * Don't trust pt-pt interfaces if there are
12881 				 * other interfaces.
12882 				 */
12883 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
12884 					if (fallback_ipif == NULL) {
12885 						ipif_refhold_locked(ipif);
12886 						fallback_ipif = ipif;
12887 					}
12888 					continue;
12889 				}
12890 
12891 				/*
12892 				 * Fine. Just assume the same net mask as the
12893 				 * directly attached subnet interface is using.
12894 				 */
12895 				ipif_refhold_locked(ipif);
12896 				mutex_exit(&ill->ill_lock);
12897 				rw_exit(&ipst->ips_ill_g_lock);
12898 				if (fallback_ipif != NULL)
12899 					ipif_refrele(fallback_ipif);
12900 				*ipifp = ipif;
12901 				return (ipif->ipif_net_mask);
12902 			}
12903 		}
12904 		mutex_exit(&ill->ill_lock);
12905 	}
12906 	rw_exit(&ipst->ips_ill_g_lock);
12907 
12908 	*ipifp = fallback_ipif;
12909 	return ((fallback_ipif != NULL) ?
12910 	    fallback_ipif->ipif_net_mask : net_mask);
12911 }
12912 
12913 /*
12914  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
12915  */
12916 static void
12917 ip_wput_ioctl(queue_t *q, mblk_t *mp)
12918 {
12919 	IOCP	iocp;
12920 	ipft_t	*ipft;
12921 	ipllc_t	*ipllc;
12922 	mblk_t	*mp1;
12923 	cred_t	*cr;
12924 	int	error = 0;
12925 	conn_t	*connp;
12926 
12927 	ip1dbg(("ip_wput_ioctl"));
12928 	iocp = (IOCP)mp->b_rptr;
12929 	mp1 = mp->b_cont;
12930 	if (mp1 == NULL) {
12931 		iocp->ioc_error = EINVAL;
12932 		mp->b_datap->db_type = M_IOCNAK;
12933 		iocp->ioc_count = 0;
12934 		qreply(q, mp);
12935 		return;
12936 	}
12937 
12938 	/*
12939 	 * These IOCTLs provide various control capabilities to
12940 	 * upstream agents such as ULPs and processes.	There
12941 	 * are currently two such IOCTLs implemented.  They
12942 	 * are used by TCP to provide update information for
12943 	 * existing IREs and to forcibly delete an IRE for a
12944 	 * host that is not responding, thereby forcing an
12945 	 * attempt at a new route.
12946 	 */
12947 	iocp->ioc_error = EINVAL;
12948 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
12949 		goto done;
12950 
12951 	ipllc = (ipllc_t *)mp1->b_rptr;
12952 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
12953 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
12954 			break;
12955 	}
12956 	/*
12957 	 * prefer credential from mblk over ioctl;
12958 	 * see ip_sioctl_copyin_setup
12959 	 */
12960 	cr = msg_getcred(mp, NULL);
12961 	if (cr == NULL)
12962 		cr = iocp->ioc_cr;
12963 
12964 	/*
12965 	 * Refhold the conn in case the request gets queued up in some lookup
12966 	 */
12967 	ASSERT(CONN_Q(q));
12968 	connp = Q_TO_CONN(q);
12969 	CONN_INC_REF(connp);
12970 	if (ipft->ipft_pfi &&
12971 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
12972 	    pullupmsg(mp1, ipft->ipft_min_size))) {
12973 		error = (*ipft->ipft_pfi)(q,
12974 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
12975 	}
12976 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
12977 		/*
12978 		 * CONN_OPER_PENDING_DONE happens in the function called
12979 		 * through ipft_pfi above.
12980 		 */
12981 		return;
12982 	}
12983 
12984 	CONN_OPER_PENDING_DONE(connp);
12985 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
12986 		freemsg(mp);
12987 		return;
12988 	}
12989 	iocp->ioc_error = error;
12990 
12991 done:
12992 	mp->b_datap->db_type = M_IOCACK;
12993 	if (iocp->ioc_error)
12994 		iocp->ioc_count = 0;
12995 	qreply(q, mp);
12996 }
12997 
12998 /*
12999  * Lookup an ipif using the sequence id (ipif_seqid)
13000  */
13001 ipif_t *
13002 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13003 {
13004 	ipif_t *ipif;
13005 
13006 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13007 
13008 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13009 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13010 			return (ipif);
13011 	}
13012 	return (NULL);
13013 }
13014 
13015 /*
13016  * Assign a unique id for the ipif. This is used later when we send
13017  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13018  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13019  * IRE is added, we verify that ipif has not disappeared.
13020  */
13021 
13022 static void
13023 ipif_assign_seqid(ipif_t *ipif)
13024 {
13025 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13026 
13027 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
13028 }
13029 
13030 /*
13031  * Clone the contents of `sipif' to `dipif'.  Requires that both ipifs are
13032  * administratively down (i.e., no DAD), of the same type, and locked.  Note
13033  * that the clone is complete -- including the seqid -- and the expectation is
13034  * that the caller will either free or overwrite `sipif' before it's unlocked.
13035  */
13036 static void
13037 ipif_clone(const ipif_t *sipif, ipif_t *dipif)
13038 {
13039 	ASSERT(MUTEX_HELD(&sipif->ipif_ill->ill_lock));
13040 	ASSERT(MUTEX_HELD(&dipif->ipif_ill->ill_lock));
13041 	ASSERT(!(sipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
13042 	ASSERT(!(dipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
13043 	ASSERT(sipif->ipif_ire_type == dipif->ipif_ire_type);
13044 	ASSERT(sipif->ipif_arp_del_mp == NULL);
13045 	ASSERT(dipif->ipif_arp_del_mp == NULL);
13046 	ASSERT(sipif->ipif_igmp_rpt == NULL);
13047 	ASSERT(dipif->ipif_igmp_rpt == NULL);
13048 	ASSERT(sipif->ipif_multicast_up == 0);
13049 	ASSERT(dipif->ipif_multicast_up == 0);
13050 	ASSERT(sipif->ipif_joined_allhosts == 0);
13051 	ASSERT(dipif->ipif_joined_allhosts == 0);
13052 
13053 	dipif->ipif_mtu = sipif->ipif_mtu;
13054 	dipif->ipif_flags = sipif->ipif_flags;
13055 	dipif->ipif_metric = sipif->ipif_metric;
13056 	dipif->ipif_zoneid = sipif->ipif_zoneid;
13057 	dipif->ipif_v6subnet = sipif->ipif_v6subnet;
13058 	dipif->ipif_v6lcl_addr = sipif->ipif_v6lcl_addr;
13059 	dipif->ipif_v6src_addr = sipif->ipif_v6src_addr;
13060 	dipif->ipif_v6net_mask = sipif->ipif_v6net_mask;
13061 	dipif->ipif_v6brd_addr = sipif->ipif_v6brd_addr;
13062 	dipif->ipif_v6pp_dst_addr = sipif->ipif_v6pp_dst_addr;
13063 
13064 	/*
13065 	 * While dipif is down right now, it might've been up before.  Since
13066 	 * it's changing identity, its packet counters need to be reset.
13067 	 */
13068 	dipif->ipif_ib_pkt_count = 0;
13069 	dipif->ipif_ob_pkt_count = 0;
13070 	dipif->ipif_fo_pkt_count = 0;
13071 
13072 	/*
13073 	 * As per the comment atop the function, we assume that these sipif
13074 	 * fields will be changed before sipif is unlocked.
13075 	 */
13076 	dipif->ipif_seqid = sipif->ipif_seqid;
13077 	dipif->ipif_saved_ire_mp = sipif->ipif_saved_ire_mp;
13078 	dipif->ipif_saved_ire_cnt = sipif->ipif_saved_ire_cnt;
13079 	dipif->ipif_state_flags = sipif->ipif_state_flags;
13080 }
13081 
13082 /*
13083  * Transfer the contents of `sipif' to `dipif', and then free (if `virgipif'
13084  * is NULL) or overwrite `sipif' with `virgipif', which must be a virgin
13085  * (unreferenced) ipif.  Also, if `sipif' is used by the current xop, then
13086  * transfer the xop to `dipif'.  Requires that all ipifs are administratively
13087  * down (i.e., no DAD), of the same type, and unlocked.
13088  */
13089 static void
13090 ipif_transfer(ipif_t *sipif, ipif_t *dipif, ipif_t *virgipif)
13091 {
13092 	ipsq_t *ipsq = sipif->ipif_ill->ill_phyint->phyint_ipsq;
13093 	ipxop_t *ipx = ipsq->ipsq_xop;
13094 
13095 	ASSERT(sipif != dipif);
13096 	ASSERT(sipif != virgipif);
13097 
13098 	/*
13099 	 * Grab all of the locks that protect the ipif in a defined order.
13100 	 */
13101 	GRAB_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
13102 	if (sipif > dipif) {
13103 		mutex_enter(&sipif->ipif_saved_ire_lock);
13104 		mutex_enter(&dipif->ipif_saved_ire_lock);
13105 	} else {
13106 		mutex_enter(&dipif->ipif_saved_ire_lock);
13107 		mutex_enter(&sipif->ipif_saved_ire_lock);
13108 	}
13109 
13110 	ipif_clone(sipif, dipif);
13111 	if (virgipif != NULL) {
13112 		ipif_clone(virgipif, sipif);
13113 		mi_free(virgipif);
13114 	}
13115 
13116 	mutex_exit(&sipif->ipif_saved_ire_lock);
13117 	mutex_exit(&dipif->ipif_saved_ire_lock);
13118 	RELEASE_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
13119 
13120 	/*
13121 	 * Transfer ownership of the current xop, if necessary.
13122 	 */
13123 	if (ipx->ipx_current_ipif == sipif) {
13124 		ASSERT(ipx->ipx_pending_ipif == NULL);
13125 		mutex_enter(&ipx->ipx_lock);
13126 		ipx->ipx_current_ipif = dipif;
13127 		mutex_exit(&ipx->ipx_lock);
13128 	}
13129 
13130 	if (virgipif == NULL)
13131 		mi_free(sipif);
13132 }
13133 
13134 /*
13135  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13136  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13137  * be inserted into the first space available in the list. The value of
13138  * ipif_id will then be set to the appropriate value for its position.
13139  */
13140 static int
13141 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock)
13142 {
13143 	ill_t *ill;
13144 	ipif_t *tipif;
13145 	ipif_t **tipifp;
13146 	int id;
13147 	ip_stack_t	*ipst;
13148 
13149 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13150 	    IAM_WRITER_IPIF(ipif));
13151 
13152 	ill = ipif->ipif_ill;
13153 	ASSERT(ill != NULL);
13154 	ipst = ill->ill_ipst;
13155 
13156 	/*
13157 	 * In the case of lo0:0 we already hold the ill_g_lock.
13158 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13159 	 * ipif_insert.
13160 	 */
13161 	if (acquire_g_lock)
13162 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13163 	mutex_enter(&ill->ill_lock);
13164 	id = ipif->ipif_id;
13165 	tipifp = &(ill->ill_ipif);
13166 	if (id == -1) {	/* need to find a real id */
13167 		id = 0;
13168 		while ((tipif = *tipifp) != NULL) {
13169 			ASSERT(tipif->ipif_id >= id);
13170 			if (tipif->ipif_id != id)
13171 				break; /* non-consecutive id */
13172 			id++;
13173 			tipifp = &(tipif->ipif_next);
13174 		}
13175 		/* limit number of logical interfaces */
13176 		if (id >= ipst->ips_ip_addrs_per_if) {
13177 			mutex_exit(&ill->ill_lock);
13178 			if (acquire_g_lock)
13179 				rw_exit(&ipst->ips_ill_g_lock);
13180 			return (-1);
13181 		}
13182 		ipif->ipif_id = id; /* assign new id */
13183 	} else if (id < ipst->ips_ip_addrs_per_if) {
13184 		/* we have a real id; insert ipif in the right place */
13185 		while ((tipif = *tipifp) != NULL) {
13186 			ASSERT(tipif->ipif_id != id);
13187 			if (tipif->ipif_id > id)
13188 				break; /* found correct location */
13189 			tipifp = &(tipif->ipif_next);
13190 		}
13191 	} else {
13192 		mutex_exit(&ill->ill_lock);
13193 		if (acquire_g_lock)
13194 			rw_exit(&ipst->ips_ill_g_lock);
13195 		return (-1);
13196 	}
13197 
13198 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13199 
13200 	ipif->ipif_next = tipif;
13201 	*tipifp = ipif;
13202 	mutex_exit(&ill->ill_lock);
13203 	if (acquire_g_lock)
13204 		rw_exit(&ipst->ips_ill_g_lock);
13205 
13206 	return (0);
13207 }
13208 
13209 static void
13210 ipif_remove(ipif_t *ipif)
13211 {
13212 	ipif_t	**ipifp;
13213 	ill_t	*ill = ipif->ipif_ill;
13214 
13215 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13216 
13217 	mutex_enter(&ill->ill_lock);
13218 	ipifp = &ill->ill_ipif;
13219 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13220 		if (*ipifp == ipif) {
13221 			*ipifp = ipif->ipif_next;
13222 			break;
13223 		}
13224 	}
13225 	mutex_exit(&ill->ill_lock);
13226 }
13227 
13228 /*
13229  * Allocate and initialize a new interface control structure.  (Always
13230  * called as writer.)
13231  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13232  * is not part of the global linked list of ills. ipif_seqid is unique
13233  * in the system and to preserve the uniqueness, it is assigned only
13234  * when ill becomes part of the global list. At that point ill will
13235  * have a name. If it doesn't get assigned here, it will get assigned
13236  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13237  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13238  * the interface flags or any other information from the DL_INFO_ACK for
13239  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13240  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13241  * second DL_INFO_ACK comes in from the driver.
13242  */
13243 static ipif_t *
13244 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize,
13245     boolean_t insert)
13246 {
13247 	ipif_t	*ipif;
13248 	phyint_t *phyi = ill->ill_phyint;
13249 	ip_stack_t *ipst = ill->ill_ipst;
13250 
13251 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13252 	    ill->ill_name, id, (void *)ill));
13253 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13254 
13255 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13256 		return (NULL);
13257 	*ipif = ipif_zero;	/* start clean */
13258 
13259 	ipif->ipif_ill = ill;
13260 	ipif->ipif_id = id;	/* could be -1 */
13261 	/*
13262 	 * Inherit the zoneid from the ill; for the shared stack instance
13263 	 * this is always the global zone
13264 	 */
13265 	ipif->ipif_zoneid = ill->ill_zoneid;
13266 
13267 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13268 
13269 	ipif->ipif_refcnt = 0;
13270 	ipif->ipif_saved_ire_cnt = 0;
13271 
13272 	if (insert) {
13273 		if (ipif_insert(ipif, ire_type != IRE_LOOPBACK) != 0) {
13274 			mi_free(ipif);
13275 			return (NULL);
13276 		}
13277 		/* -1 id should have been replaced by real id */
13278 		id = ipif->ipif_id;
13279 		ASSERT(id >= 0);
13280 	}
13281 
13282 	if (ill->ill_name[0] != '\0')
13283 		ipif_assign_seqid(ipif);
13284 
13285 	/*
13286 	 * If this is ipif zero, configure ill/phyint-wide information.
13287 	 * Defer most configuration until we're guaranteed we're attached.
13288 	 */
13289 	if (id == 0) {
13290 		if (ill->ill_mactype == SUNW_DL_IPMP) {
13291 			/*
13292 			 * Set PHYI_IPMP and also set PHYI_FAILED since there
13293 			 * are no active interfaces.  Similarly, PHYI_RUNNING
13294 			 * isn't set until the group has an active interface.
13295 			 */
13296 			mutex_enter(&phyi->phyint_lock);
13297 			phyi->phyint_flags |= (PHYI_IPMP | PHYI_FAILED);
13298 			mutex_exit(&phyi->phyint_lock);
13299 
13300 			/*
13301 			 * Create the illgrp (which must not exist yet because
13302 			 * the zeroth ipif is created once per ill).  However,
13303 			 * do not not link it to the ipmp_grp_t until I_PLINK
13304 			 * is called; see ip_sioctl_plink_ipmp() for details.
13305 			 */
13306 			if (ipmp_illgrp_create(ill) == NULL) {
13307 				if (insert) {
13308 					rw_enter(&ipst->ips_ill_g_lock,
13309 					    RW_WRITER);
13310 					ipif_remove(ipif);
13311 					rw_exit(&ipst->ips_ill_g_lock);
13312 				}
13313 				mi_free(ipif);
13314 				return (NULL);
13315 			}
13316 		} else {
13317 			/*
13318 			 * By default, PHYI_RUNNING is set when the zeroth
13319 			 * ipif is created.  For other ipifs, we don't touch
13320 			 * it since DLPI notifications may have changed it.
13321 			 */
13322 			mutex_enter(&phyi->phyint_lock);
13323 			phyi->phyint_flags |= PHYI_RUNNING;
13324 			mutex_exit(&phyi->phyint_lock);
13325 		}
13326 	}
13327 
13328 	/*
13329 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13330 	 * The ipif is still not up and can't be looked up until the
13331 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13332 	 */
13333 	mutex_enter(&ill->ill_lock);
13334 	mutex_enter(&phyi->phyint_lock);
13335 
13336 	ipif->ipif_ire_type = ire_type;
13337 
13338 	if (ipif->ipif_isv6) {
13339 		ill->ill_flags |= ILLF_IPV6;
13340 	} else {
13341 		ipaddr_t inaddr_any = INADDR_ANY;
13342 
13343 		ill->ill_flags |= ILLF_IPV4;
13344 
13345 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13346 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13347 		    &ipif->ipif_v6lcl_addr);
13348 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13349 		    &ipif->ipif_v6src_addr);
13350 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13351 		    &ipif->ipif_v6subnet);
13352 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13353 		    &ipif->ipif_v6net_mask);
13354 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13355 		    &ipif->ipif_v6brd_addr);
13356 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13357 		    &ipif->ipif_v6pp_dst_addr);
13358 	}
13359 
13360 	/*
13361 	 * Don't set the interface flags etc. now, will do it in
13362 	 * ip_ll_subnet_defaults.
13363 	 */
13364 	if (!initialize)
13365 		goto out;
13366 
13367 	ipif->ipif_mtu = ill->ill_max_mtu;
13368 
13369 	/*
13370 	 * NOTE: The IPMP meta-interface is special-cased because it starts
13371 	 * with no underlying interfaces (and thus an unknown broadcast
13372 	 * address length), but all interfaces that can be placed into an IPMP
13373 	 * group are required to be broadcast-capable.
13374 	 */
13375 	if (ill->ill_bcast_addr_length != 0 || IS_IPMP(ill)) {
13376 		/*
13377 		 * Later detect lack of DLPI driver multicast
13378 		 * capability by catching DL_ENABMULTI errors in
13379 		 * ip_rput_dlpi.
13380 		 */
13381 		ill->ill_flags |= ILLF_MULTICAST;
13382 		if (!ipif->ipif_isv6)
13383 			ipif->ipif_flags |= IPIF_BROADCAST;
13384 	} else {
13385 		if (ill->ill_net_type != IRE_LOOPBACK) {
13386 			if (ipif->ipif_isv6)
13387 				/*
13388 				 * Note: xresolv interfaces will eventually need
13389 				 * NOARP set here as well, but that will require
13390 				 * those external resolvers to have some
13391 				 * knowledge of that flag and act appropriately.
13392 				 * Not to be changed at present.
13393 				 */
13394 				ill->ill_flags |= ILLF_NONUD;
13395 			else
13396 				ill->ill_flags |= ILLF_NOARP;
13397 		}
13398 		if (ill->ill_phys_addr_length == 0) {
13399 			if (ill->ill_mactype == SUNW_DL_VNI) {
13400 				ipif->ipif_flags |= IPIF_NOXMIT;
13401 				phyi->phyint_flags |= PHYI_VIRTUAL;
13402 			} else {
13403 				/* pt-pt supports multicast. */
13404 				ill->ill_flags |= ILLF_MULTICAST;
13405 				if (ill->ill_net_type == IRE_LOOPBACK) {
13406 					phyi->phyint_flags |=
13407 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13408 				} else {
13409 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13410 				}
13411 			}
13412 		}
13413 	}
13414 out:
13415 	mutex_exit(&phyi->phyint_lock);
13416 	mutex_exit(&ill->ill_lock);
13417 	return (ipif);
13418 }
13419 
13420 /*
13421  * If appropriate, send a message up to the resolver delete the entry
13422  * for the address of this interface which is going out of business.
13423  * (Always called as writer).
13424  *
13425  * NOTE : We need to check for NULL mps as some of the fields are
13426  *	  initialized only for some interface types. See ipif_resolver_up()
13427  *	  for details.
13428  */
13429 void
13430 ipif_resolver_down(ipif_t *ipif)
13431 {
13432 	mblk_t	*mp;
13433 	ill_t	*ill = ipif->ipif_ill;
13434 
13435 	ip1dbg(("ipif_resolver_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13436 	ASSERT(IAM_WRITER_IPIF(ipif));
13437 
13438 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13439 		return;
13440 
13441 	/* Delete the mapping for the local address */
13442 	mp = ipif->ipif_arp_del_mp;
13443 	if (mp != NULL) {
13444 		ip1dbg(("ipif_resolver_down: arp cmd %x for %s:%u\n",
13445 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13446 		putnext(ill->ill_rq, mp);
13447 		ipif->ipif_arp_del_mp = NULL;
13448 	}
13449 
13450 	/*
13451 	 * Make IPMP aware of the deleted data address.
13452 	 */
13453 	if (IS_IPMP(ill))
13454 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
13455 
13456 	/*
13457 	 * If this is the last ipif that is going down and there are no
13458 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13459 	 * clean up ARP completely.
13460 	 */
13461 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13462 		/*
13463 		 * If this was the last ipif on an IPMP interface, purge any
13464 		 * IPMP ARP entries associated with it.
13465 		 */
13466 		if (IS_IPMP(ill))
13467 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
13468 
13469 		/* Send up AR_INTERFACE_DOWN message */
13470 		mp = ill->ill_arp_down_mp;
13471 		if (mp != NULL) {
13472 			ip1dbg(("ipif_resolver_down: arp cmd %x for %s:%u\n",
13473 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13474 			    ipif->ipif_id));
13475 			putnext(ill->ill_rq, mp);
13476 			ill->ill_arp_down_mp = NULL;
13477 		}
13478 
13479 		/* Tell ARP to delete the multicast mappings */
13480 		mp = ill->ill_arp_del_mapping_mp;
13481 		if (mp != NULL) {
13482 			ip1dbg(("ipif_resolver_down: arp cmd %x for %s:%u\n",
13483 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13484 			    ipif->ipif_id));
13485 			putnext(ill->ill_rq, mp);
13486 			ill->ill_arp_del_mapping_mp = NULL;
13487 		}
13488 	}
13489 }
13490 
13491 /*
13492  * Set up the multicast mappings for `ipif' in ARP.  If `arp_add_mapping_mp'
13493  * is non-NULL, then upon success it will contain an mblk that can be passed
13494  * to ARP to create the mapping.  Otherwise, if it's NULL, upon success ARP
13495  * will have already been notified to create the mapping.  Returns zero on
13496  * success, -1 upon failure.
13497  */
13498 int
13499 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13500 {
13501 	mblk_t	*del_mp = NULL;
13502 	mblk_t *add_mp = NULL;
13503 	mblk_t *mp;
13504 	ill_t	*ill = ipif->ipif_ill;
13505 	phyint_t *phyi = ill->ill_phyint;
13506 	ipaddr_t addr, mask, extract_mask = 0;
13507 	arma_t	*arma;
13508 	uint8_t *maddr, *bphys_addr;
13509 	uint32_t hw_start;
13510 	dl_unitdata_req_t *dlur;
13511 
13512 	ASSERT(IAM_WRITER_IPIF(ipif));
13513 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13514 		return (0);
13515 
13516 	/*
13517 	 * IPMP meta-interfaces don't have any inherent multicast mappings,
13518 	 * and instead use the ones on the underlying interfaces.
13519 	 */
13520 	if (IS_IPMP(ill))
13521 		return (0);
13522 
13523 	/*
13524 	 * Delete the existing mapping from ARP.  Normally, ipif_down() ->
13525 	 * ipif_resolver_down() will send this up to ARP, but it may be that
13526 	 * we are enabling PHYI_MULTI_BCAST via ip_rput_dlpi_writer().
13527 	 */
13528 	mp = ill->ill_arp_del_mapping_mp;
13529 	if (mp != NULL) {
13530 		ip1dbg(("ipif_arp_setup_multicast: arp cmd %x for %s:%u\n",
13531 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13532 		putnext(ill->ill_rq, mp);
13533 		ill->ill_arp_del_mapping_mp = NULL;
13534 	}
13535 
13536 	if (arp_add_mapping_mp != NULL)
13537 		*arp_add_mapping_mp = NULL;
13538 
13539 	/*
13540 	 * Check that the address is not to long for the constant
13541 	 * length reserved in the template arma_t.
13542 	 */
13543 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13544 		return (-1);
13545 
13546 	/* Add mapping mblk */
13547 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13548 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13549 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13550 	    (caddr_t)&addr);
13551 	if (add_mp == NULL)
13552 		return (-1);
13553 	arma = (arma_t *)add_mp->b_rptr;
13554 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13555 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13556 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13557 
13558 	/*
13559 	 * Determine the broadcast address.
13560 	 */
13561 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13562 	if (ill->ill_sap_length < 0)
13563 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13564 	else
13565 		bphys_addr = (uchar_t *)dlur +
13566 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13567 	/*
13568 	 * Check PHYI_MULTI_BCAST and length of physical
13569 	 * address to determine if we use the mapping or the
13570 	 * broadcast address.
13571 	 */
13572 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13573 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13574 		    bphys_addr, maddr, &hw_start, &extract_mask))
13575 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13576 
13577 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13578 	    (ill->ill_flags & ILLF_MULTICAST)) {
13579 		/* Make sure this will not match the "exact" entry. */
13580 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13581 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13582 		    (caddr_t)&addr);
13583 		if (del_mp == NULL) {
13584 			freemsg(add_mp);
13585 			return (-1);
13586 		}
13587 		bcopy(&extract_mask, (char *)arma +
13588 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13589 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13590 			/* Use link-layer broadcast address for MULTI_BCAST */
13591 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13592 			ip2dbg(("ipif_arp_setup_multicast: adding"
13593 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13594 		} else {
13595 			arma->arma_hw_mapping_start = hw_start;
13596 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13597 			    " ARP setup for %s\n", ill->ill_name));
13598 		}
13599 	} else {
13600 		freemsg(add_mp);
13601 		ASSERT(del_mp == NULL);
13602 		/* It is neither MULTICAST nor MULTI_BCAST */
13603 		return (0);
13604 	}
13605 	ASSERT(add_mp != NULL && del_mp != NULL);
13606 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13607 	ill->ill_arp_del_mapping_mp = del_mp;
13608 	if (arp_add_mapping_mp != NULL) {
13609 		/* The caller just wants the mblks allocated */
13610 		*arp_add_mapping_mp = add_mp;
13611 	} else {
13612 		/* The caller wants us to send it to arp */
13613 		putnext(ill->ill_rq, add_mp);
13614 	}
13615 	return (0);
13616 }
13617 
13618 /*
13619  * Get the resolver set up for a new IP address.  (Always called as writer.)
13620  * Called both for IPv4 and IPv6 interfaces, though it only sets up the
13621  * resolver for v6 if it's an ILLF_XRESOLV interface.  Honors ILLF_NOARP.
13622  *
13623  * The enumerated value res_act tunes the behavior:
13624  * 	* Res_act_initial: set up all the resolver structures for a new
13625  *	  IP address.
13626  *	* Res_act_defend: tell ARP that it needs to send a single gratuitous
13627  *	  ARP message in defense of the address.
13628  *	* Res_act_rebind: tell ARP to change the hardware address for an IP
13629  *	  address (and issue gratuitous ARPs).  Used by ipmp_ill_bind_ipif().
13630  *
13631  * Returns zero on success, or an errno upon failure.
13632  */
13633 int
13634 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13635 {
13636 	mblk_t	*arp_up_mp = NULL;
13637 	mblk_t	*arp_down_mp = NULL;
13638 	mblk_t	*arp_add_mp = NULL;
13639 	mblk_t	*arp_del_mp = NULL;
13640 	mblk_t	*arp_add_mapping_mp = NULL;
13641 	mblk_t	*arp_del_mapping_mp = NULL;
13642 	ill_t	*ill = ipif->ipif_ill;
13643 	int	err = ENOMEM;
13644 	boolean_t added_ipif = B_FALSE;
13645 	boolean_t publish;
13646 	boolean_t was_dup;
13647 
13648 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13649 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13650 	ASSERT(IAM_WRITER_IPIF(ipif));
13651 
13652 	was_dup = B_FALSE;
13653 	if (res_act == Res_act_initial) {
13654 		ipif->ipif_addr_ready = 0;
13655 		/*
13656 		 * We're bringing an interface up here.  There's no way that we
13657 		 * should need to shut down ARP now.
13658 		 */
13659 		mutex_enter(&ill->ill_lock);
13660 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13661 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13662 			ill->ill_ipif_dup_count--;
13663 			was_dup = B_TRUE;
13664 		}
13665 		mutex_exit(&ill->ill_lock);
13666 	}
13667 	if (ipif->ipif_recovery_id != 0)
13668 		(void) untimeout(ipif->ipif_recovery_id);
13669 	ipif->ipif_recovery_id = 0;
13670 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13671 		ipif->ipif_addr_ready = 1;
13672 		return (0);
13673 	}
13674 	/* NDP will set the ipif_addr_ready flag when it's ready */
13675 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13676 		return (0);
13677 
13678 	if (ill->ill_isv6) {
13679 		/*
13680 		 * External resolver for IPv6
13681 		 */
13682 		ASSERT(res_act == Res_act_initial);
13683 		publish = !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr);
13684 	} else {
13685 		/*
13686 		 * IPv4 arp case. If the ARP stream has already started
13687 		 * closing, fail this request for ARP bringup. Else
13688 		 * record the fact that an ARP bringup is pending.
13689 		 */
13690 		mutex_enter(&ill->ill_lock);
13691 		if (ill->ill_arp_closing) {
13692 			mutex_exit(&ill->ill_lock);
13693 			err = EINVAL;
13694 			goto failed;
13695 		} else {
13696 			if (ill->ill_ipif_up_count == 0 &&
13697 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13698 				ill->ill_arp_bringup_pending = 1;
13699 			mutex_exit(&ill->ill_lock);
13700 		}
13701 		publish = (ipif->ipif_lcl_addr != INADDR_ANY);
13702 	}
13703 
13704 	if (IS_IPMP(ill) && publish) {
13705 		/*
13706 		 * If we're here via ipif_up(), then the ipif won't be bound
13707 		 * yet -- add it to the group, which will bind it if possible.
13708 		 * (We would add it in ipif_up(), but deleting on failure
13709 		 * there is gruesome.)  If we're here via ipmp_ill_bind_ipif(),
13710 		 * then the ipif has already been added to the group and we
13711 		 * just need to use the binding.
13712 		 */
13713 		if (ipmp_ipif_bound_ill(ipif) == NULL) {
13714 			if (ipmp_illgrp_add_ipif(ill->ill_grp, ipif) == NULL) {
13715 				/*
13716 				 * We couldn't bind the ipif to an ill yet,
13717 				 * so we have nothing to publish.
13718 				 */
13719 				publish = B_FALSE;
13720 			}
13721 			added_ipif = B_TRUE;
13722 		}
13723 	}
13724 
13725 	/*
13726 	 * Add an entry for the local address in ARP only if it
13727 	 * is not UNNUMBERED and it is suitable for publishing.
13728 	 */
13729 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && publish) {
13730 		if (res_act == Res_act_defend) {
13731 			arp_add_mp = ipif_area_alloc(ipif, ACE_F_DEFEND);
13732 			if (arp_add_mp == NULL)
13733 				goto failed;
13734 			/*
13735 			 * If we're just defending our address now, then
13736 			 * there's no need to set up ARP multicast mappings.
13737 			 * The publish command is enough.
13738 			 */
13739 			goto done;
13740 		}
13741 
13742 		/*
13743 		 * Allocate an ARP add message and an ARP delete message (the
13744 		 * latter is saved for use when the address goes down).
13745 		 */
13746 		if ((arp_add_mp = ipif_area_alloc(ipif, 0)) == NULL)
13747 			goto failed;
13748 
13749 		if ((arp_del_mp = ipif_ared_alloc(ipif)) == NULL)
13750 			goto failed;
13751 
13752 		if (res_act != Res_act_initial)
13753 			goto arp_setup_multicast;
13754 	} else {
13755 		if (res_act != Res_act_initial)
13756 			goto done;
13757 	}
13758 	/*
13759 	 * Need to bring up ARP or setup multicast mapping only
13760 	 * when the first interface is coming UP.
13761 	 */
13762 	if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0 || was_dup)
13763 		goto done;
13764 
13765 	/*
13766 	 * Allocate an ARP down message (to be saved) and an ARP up message.
13767 	 */
13768 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13769 	if (arp_down_mp == NULL)
13770 		goto failed;
13771 
13772 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13773 	if (arp_up_mp == NULL)
13774 		goto failed;
13775 
13776 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13777 		goto done;
13778 
13779 arp_setup_multicast:
13780 	/*
13781 	 * Setup the multicast mappings. This function initializes
13782 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13783 	 * IPv6, or for the IPMP interface (since it has no link-layer).
13784 	 */
13785 	if (!ill->ill_isv6 && !IS_IPMP(ill)) {
13786 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13787 		if (err != 0)
13788 			goto failed;
13789 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13790 		ASSERT(arp_add_mapping_mp != NULL);
13791 	}
13792 done:
13793 	if (arp_up_mp != NULL) {
13794 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13795 		    ill->ill_name, ipif->ipif_id));
13796 		putnext(ill->ill_rq, arp_up_mp);
13797 		arp_up_mp = NULL;
13798 	}
13799 	if (arp_add_mp != NULL) {
13800 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13801 		    ill->ill_name, ipif->ipif_id));
13802 		/*
13803 		 * If it's an extended ARP implementation, then we'll wait to
13804 		 * hear that DAD has finished before using the interface.
13805 		 */
13806 		if (!ill->ill_arp_extend)
13807 			ipif->ipif_addr_ready = 1;
13808 		putnext(ill->ill_rq, arp_add_mp);
13809 		arp_add_mp = NULL;
13810 	} else {
13811 		ipif->ipif_addr_ready = 1;
13812 	}
13813 	if (arp_add_mapping_mp != NULL) {
13814 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13815 		    ill->ill_name, ipif->ipif_id));
13816 		putnext(ill->ill_rq, arp_add_mapping_mp);
13817 		arp_add_mapping_mp = NULL;
13818 	}
13819 
13820 	if (res_act == Res_act_initial) {
13821 		if (ill->ill_flags & ILLF_NOARP)
13822 			err = ill_arp_off(ill);
13823 		else
13824 			err = ill_arp_on(ill);
13825 		if (err != 0) {
13826 			ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n",
13827 			    err));
13828 			goto failed;
13829 		}
13830 	}
13831 
13832 	if (arp_del_mp != NULL) {
13833 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13834 		ipif->ipif_arp_del_mp = arp_del_mp;
13835 	}
13836 	if (arp_down_mp != NULL) {
13837 		ASSERT(ill->ill_arp_down_mp == NULL);
13838 		ill->ill_arp_down_mp = arp_down_mp;
13839 	}
13840 	if (arp_del_mapping_mp != NULL) {
13841 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13842 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13843 	}
13844 
13845 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
13846 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
13847 failed:
13848 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13849 	if (added_ipif)
13850 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
13851 	freemsg(arp_add_mp);
13852 	freemsg(arp_del_mp);
13853 	freemsg(arp_add_mapping_mp);
13854 	freemsg(arp_up_mp);
13855 	freemsg(arp_down_mp);
13856 	ill->ill_arp_bringup_pending = 0;
13857 	return (err);
13858 }
13859 
13860 /*
13861  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
13862  * just gone back up.
13863  */
13864 static void
13865 ipif_arp_start_dad(ipif_t *ipif)
13866 {
13867 	ill_t *ill = ipif->ipif_ill;
13868 	mblk_t *arp_add_mp;
13869 
13870 	/* ACE_F_UNVERIFIED restarts DAD */
13871 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
13872 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13873 	    ipif->ipif_lcl_addr == INADDR_ANY ||
13874 	    (arp_add_mp = ipif_area_alloc(ipif, ACE_F_UNVERIFIED)) == NULL) {
13875 		/*
13876 		 * If we can't contact ARP for some reason, that's not really a
13877 		 * problem.  Just send out the routing socket notification that
13878 		 * DAD completion would have done, and continue.
13879 		 */
13880 		ipif_mask_reply(ipif);
13881 		ipif_up_notify(ipif);
13882 		ipif->ipif_addr_ready = 1;
13883 		return;
13884 	}
13885 
13886 	putnext(ill->ill_rq, arp_add_mp);
13887 }
13888 
13889 static void
13890 ipif_ndp_start_dad(ipif_t *ipif)
13891 {
13892 	nce_t *nce;
13893 
13894 	nce = ndp_lookup_v6(ipif->ipif_ill, B_TRUE, &ipif->ipif_v6lcl_addr,
13895 	    B_FALSE);
13896 	if (nce == NULL)
13897 		return;
13898 
13899 	if (!ndp_restart_dad(nce)) {
13900 		/*
13901 		 * If we can't restart DAD for some reason, that's not really a
13902 		 * problem.  Just send out the routing socket notification that
13903 		 * DAD completion would have done, and continue.
13904 		 */
13905 		ipif_up_notify(ipif);
13906 		ipif->ipif_addr_ready = 1;
13907 	}
13908 	NCE_REFRELE(nce);
13909 }
13910 
13911 /*
13912  * Restart duplicate address detection on all interfaces on the given ill.
13913  *
13914  * This is called when an interface transitions from down to up
13915  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
13916  *
13917  * Note that since the underlying physical link has transitioned, we must cause
13918  * at least one routing socket message to be sent here, either via DAD
13919  * completion or just by default on the first ipif.  (If we don't do this, then
13920  * in.mpathd will see long delays when doing link-based failure recovery.)
13921  */
13922 void
13923 ill_restart_dad(ill_t *ill, boolean_t went_up)
13924 {
13925 	ipif_t *ipif;
13926 
13927 	if (ill == NULL)
13928 		return;
13929 
13930 	/*
13931 	 * If layer two doesn't support duplicate address detection, then just
13932 	 * send the routing socket message now and be done with it.
13933 	 */
13934 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
13935 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
13936 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
13937 		return;
13938 	}
13939 
13940 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13941 		if (went_up) {
13942 			if (ipif->ipif_flags & IPIF_UP) {
13943 				if (ill->ill_isv6)
13944 					ipif_ndp_start_dad(ipif);
13945 				else
13946 					ipif_arp_start_dad(ipif);
13947 			} else if (ill->ill_isv6 &&
13948 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
13949 				/*
13950 				 * For IPv4, the ARP module itself will
13951 				 * automatically start the DAD process when it
13952 				 * sees DL_NOTE_LINK_UP.  We respond to the
13953 				 * AR_CN_READY at the completion of that task.
13954 				 * For IPv6, we must kick off the bring-up
13955 				 * process now.
13956 				 */
13957 				ndp_do_recovery(ipif);
13958 			} else {
13959 				/*
13960 				 * Unfortunately, the first ipif is "special"
13961 				 * and represents the underlying ill in the
13962 				 * routing socket messages.  Thus, when this
13963 				 * one ipif is down, we must still notify so
13964 				 * that the user knows the IFF_RUNNING status
13965 				 * change.  (If the first ipif is up, then
13966 				 * we'll handle eventual routing socket
13967 				 * notification via DAD completion.)
13968 				 */
13969 				if (ipif == ill->ill_ipif) {
13970 					ip_rts_ifmsg(ill->ill_ipif,
13971 					    RTSQ_DEFAULT);
13972 				}
13973 			}
13974 		} else {
13975 			/*
13976 			 * After link down, we'll need to send a new routing
13977 			 * message when the link comes back, so clear
13978 			 * ipif_addr_ready.
13979 			 */
13980 			ipif->ipif_addr_ready = 0;
13981 		}
13982 	}
13983 
13984 	/*
13985 	 * If we've torn down links, then notify the user right away.
13986 	 */
13987 	if (!went_up)
13988 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
13989 }
13990 
13991 static void
13992 ipsq_delete(ipsq_t *ipsq)
13993 {
13994 	ipxop_t *ipx = ipsq->ipsq_xop;
13995 
13996 	ipsq->ipsq_ipst = NULL;
13997 	ASSERT(ipsq->ipsq_phyint == NULL);
13998 	ASSERT(ipsq->ipsq_xop != NULL);
13999 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipx->ipx_mphead == NULL);
14000 	ASSERT(ipx->ipx_pending_mp == NULL);
14001 	kmem_free(ipsq, sizeof (ipsq_t));
14002 }
14003 
14004 static int
14005 ill_up_ipifs_on_ill(ill_t *ill, queue_t *q, mblk_t *mp)
14006 {
14007 	int err;
14008 	ipif_t *ipif;
14009 
14010 	if (ill == NULL)
14011 		return (0);
14012 
14013 	/*
14014 	 * Except for ipif_state_flags and ill_state_flags the other
14015 	 * fields of the ipif/ill that are modified below are protected
14016 	 * implicitly since we are a writer. We would have tried to down
14017 	 * even an ipif that was already down, in ill_down_ipifs. So we
14018 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14019 	 */
14020 	ASSERT(IAM_WRITER_ILL(ill));
14021 
14022 	ill->ill_up_ipifs = B_TRUE;
14023 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14024 		mutex_enter(&ill->ill_lock);
14025 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
14026 		mutex_exit(&ill->ill_lock);
14027 		if (ipif->ipif_was_up) {
14028 			if (!(ipif->ipif_flags & IPIF_UP))
14029 				err = ipif_up(ipif, q, mp);
14030 			ipif->ipif_was_up = B_FALSE;
14031 			if (err != 0) {
14032 				ASSERT(err == EINPROGRESS);
14033 				return (err);
14034 			}
14035 		}
14036 	}
14037 	mutex_enter(&ill->ill_lock);
14038 	ill->ill_state_flags &= ~ILL_CHANGING;
14039 	mutex_exit(&ill->ill_lock);
14040 	ill->ill_up_ipifs = B_FALSE;
14041 	return (0);
14042 }
14043 
14044 /*
14045  * This function is called to bring up all the ipifs that were up before
14046  * bringing the ill down via ill_down_ipifs().
14047  */
14048 int
14049 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14050 {
14051 	int err;
14052 
14053 	ASSERT(IAM_WRITER_ILL(ill));
14054 
14055 	err = ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv4, q, mp);
14056 	if (err != 0)
14057 		return (err);
14058 
14059 	return (ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv6, q, mp));
14060 }
14061 
14062 /*
14063  * Bring down any IPIF_UP ipifs on ill.
14064  */
14065 static void
14066 ill_down_ipifs(ill_t *ill)
14067 {
14068 	ipif_t *ipif;
14069 
14070 	ASSERT(IAM_WRITER_ILL(ill));
14071 
14072 	/*
14073 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14074 	 * are modified below are protected implicitly since we are a writer
14075 	 */
14076 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14077 		/*
14078 		 * We go through the ipif_down logic even if the ipif
14079 		 * is already down, since routes can be added based
14080 		 * on down ipifs. Going through ipif_down once again
14081 		 * will delete any IREs created based on these routes.
14082 		 */
14083 		if (ipif->ipif_flags & IPIF_UP)
14084 			ipif->ipif_was_up = B_TRUE;
14085 
14086 		mutex_enter(&ill->ill_lock);
14087 		ipif->ipif_state_flags |= IPIF_CHANGING;
14088 		mutex_exit(&ill->ill_lock);
14089 
14090 		/*
14091 		 * Need to re-create net/subnet bcast ires if
14092 		 * they are dependent on ipif.
14093 		 */
14094 		if (!ipif->ipif_isv6)
14095 			ipif_check_bcast_ires(ipif);
14096 		(void) ipif_logical_down(ipif, NULL, NULL);
14097 		ipif_non_duplicate(ipif);
14098 		ipif_down_tail(ipif);
14099 	}
14100 }
14101 
14102 /*
14103  * Redo source address selection.  This is called when a
14104  * non-NOLOCAL/DEPRECATED/ANYCAST ipif comes up.
14105  */
14106 void
14107 ill_update_source_selection(ill_t *ill)
14108 {
14109 	ipif_t *ipif;
14110 
14111 	ASSERT(IAM_WRITER_ILL(ill));
14112 
14113 	/*
14114 	 * Underlying interfaces are only used for test traffic and thus
14115 	 * should always send with their (deprecated) source addresses.
14116 	 */
14117 	if (IS_UNDER_IPMP(ill))
14118 		return;
14119 
14120 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14121 		if (ill->ill_isv6)
14122 			ipif_recreate_interface_routes_v6(NULL, ipif);
14123 		else
14124 			ipif_recreate_interface_routes(NULL, ipif);
14125 	}
14126 }
14127 
14128 /*
14129  * Finish the group join started in ip_sioctl_groupname().
14130  */
14131 /* ARGSUSED */
14132 static void
14133 ip_join_illgrps(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
14134 {
14135 	ill_t		*ill = q->q_ptr;
14136 	phyint_t	*phyi = ill->ill_phyint;
14137 	ipmp_grp_t	*grp = phyi->phyint_grp;
14138 	ip_stack_t	*ipst = ill->ill_ipst;
14139 
14140 	/* IS_UNDER_IPMP() won't work until ipmp_ill_join_illgrp() is called */
14141 	ASSERT(!IS_IPMP(ill) && grp != NULL);
14142 	ASSERT(IAM_WRITER_IPSQ(ipsq));
14143 
14144 	if (phyi->phyint_illv4 != NULL) {
14145 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
14146 		VERIFY(grp->gr_pendv4-- > 0);
14147 		rw_exit(&ipst->ips_ipmp_lock);
14148 		ipmp_ill_join_illgrp(phyi->phyint_illv4, grp->gr_v4);
14149 	}
14150 	if (phyi->phyint_illv6 != NULL) {
14151 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
14152 		VERIFY(grp->gr_pendv6-- > 0);
14153 		rw_exit(&ipst->ips_ipmp_lock);
14154 		ipmp_ill_join_illgrp(phyi->phyint_illv6, grp->gr_v6);
14155 	}
14156 	freemsg(mp);
14157 }
14158 
14159 /*
14160  * Process an SIOCSLIFGROUPNAME request.
14161  */
14162 /* ARGSUSED */
14163 int
14164 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14165     ip_ioctl_cmd_t *ipip, void *ifreq)
14166 {
14167 	struct lifreq	*lifr = ifreq;
14168 	ill_t		*ill = ipif->ipif_ill;
14169 	ip_stack_t	*ipst = ill->ill_ipst;
14170 	phyint_t	*phyi = ill->ill_phyint;
14171 	ipmp_grp_t	*grp = phyi->phyint_grp;
14172 	mblk_t		*ipsq_mp;
14173 	int		err = 0;
14174 
14175 	/*
14176 	 * Note that phyint_grp can only change here, where we're exclusive.
14177 	 */
14178 	ASSERT(IAM_WRITER_ILL(ill));
14179 
14180 	if (ipif->ipif_id != 0 || ill->ill_usesrc_grp_next != NULL ||
14181 	    (phyi->phyint_flags & PHYI_VIRTUAL))
14182 		return (EINVAL);
14183 
14184 	lifr->lifr_groupname[LIFGRNAMSIZ - 1] = '\0';
14185 
14186 	rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
14187 
14188 	/*
14189 	 * If the name hasn't changed, there's nothing to do.
14190 	 */
14191 	if (grp != NULL && strcmp(grp->gr_name, lifr->lifr_groupname) == 0)
14192 		goto unlock;
14193 
14194 	/*
14195 	 * Handle requests to rename an IPMP meta-interface.
14196 	 *
14197 	 * Note that creation of the IPMP meta-interface is handled in
14198 	 * userland through the standard plumbing sequence.  As part of the
14199 	 * plumbing the IPMP meta-interface, its initial groupname is set to
14200 	 * the name of the interface (see ipif_set_values_tail()).
14201 	 */
14202 	if (IS_IPMP(ill)) {
14203 		err = ipmp_grp_rename(grp, lifr->lifr_groupname);
14204 		goto unlock;
14205 	}
14206 
14207 	/*
14208 	 * Handle requests to add or remove an IP interface from a group.
14209 	 */
14210 	if (lifr->lifr_groupname[0] != '\0') {			/* add */
14211 		/*
14212 		 * Moves are handled by first removing the interface from
14213 		 * its existing group, and then adding it to another group.
14214 		 * So, fail if it's already in a group.
14215 		 */
14216 		if (IS_UNDER_IPMP(ill)) {
14217 			err = EALREADY;
14218 			goto unlock;
14219 		}
14220 
14221 		grp = ipmp_grp_lookup(lifr->lifr_groupname, ipst);
14222 		if (grp == NULL) {
14223 			err = ENOENT;
14224 			goto unlock;
14225 		}
14226 
14227 		/*
14228 		 * Check if the phyint and its ills are suitable for
14229 		 * inclusion into the group.
14230 		 */
14231 		if ((err = ipmp_grp_vet_phyint(grp, phyi)) != 0)
14232 			goto unlock;
14233 
14234 		/*
14235 		 * Checks pass; join the group, and enqueue the remaining
14236 		 * illgrp joins for when we've become part of the group xop
14237 		 * and are exclusive across its IPSQs.  Since qwriter_ip()
14238 		 * requires an mblk_t to scribble on, and since `mp' will be
14239 		 * freed as part of completing the ioctl, allocate another.
14240 		 */
14241 		if ((ipsq_mp = allocb(0, BPRI_MED)) == NULL) {
14242 			err = ENOMEM;
14243 			goto unlock;
14244 		}
14245 
14246 		/*
14247 		 * Before we drop ipmp_lock, bump gr_pend* to ensure that the
14248 		 * IPMP meta-interface ills needed by `phyi' cannot go away
14249 		 * before ip_join_illgrps() is called back.  See the comments
14250 		 * in ip_sioctl_plink_ipmp() for more.
14251 		 */
14252 		if (phyi->phyint_illv4 != NULL)
14253 			grp->gr_pendv4++;
14254 		if (phyi->phyint_illv6 != NULL)
14255 			grp->gr_pendv6++;
14256 
14257 		rw_exit(&ipst->ips_ipmp_lock);
14258 
14259 		ipmp_phyint_join_grp(phyi, grp);
14260 		ill_refhold(ill);
14261 		qwriter_ip(ill, ill->ill_rq, ipsq_mp, ip_join_illgrps,
14262 		    SWITCH_OP, B_FALSE);
14263 		return (0);
14264 	} else {
14265 		/*
14266 		 * Request to remove the interface from a group.  If the
14267 		 * interface is not in a group, this trivially succeeds.
14268 		 */
14269 		rw_exit(&ipst->ips_ipmp_lock);
14270 		if (IS_UNDER_IPMP(ill))
14271 			ipmp_phyint_leave_grp(phyi);
14272 		return (0);
14273 	}
14274 unlock:
14275 	rw_exit(&ipst->ips_ipmp_lock);
14276 	return (err);
14277 }
14278 
14279 /*
14280  * Process an SIOCGLIFBINDING request.
14281  */
14282 /* ARGSUSED */
14283 int
14284 ip_sioctl_get_binding(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14285     ip_ioctl_cmd_t *ipip, void *ifreq)
14286 {
14287 	ill_t		*ill;
14288 	struct lifreq	*lifr = ifreq;
14289 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
14290 
14291 	if (!IS_IPMP(ipif->ipif_ill))
14292 		return (EINVAL);
14293 
14294 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
14295 	if ((ill = ipif->ipif_bound_ill) == NULL)
14296 		lifr->lifr_binding[0] = '\0';
14297 	else
14298 		(void) strlcpy(lifr->lifr_binding, ill->ill_name, LIFNAMSIZ);
14299 	rw_exit(&ipst->ips_ipmp_lock);
14300 	return (0);
14301 }
14302 
14303 /*
14304  * Process an SIOCGLIFGROUPNAME request.
14305  */
14306 /* ARGSUSED */
14307 int
14308 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14309     ip_ioctl_cmd_t *ipip, void *ifreq)
14310 {
14311 	ipmp_grp_t	*grp;
14312 	struct lifreq	*lifr = ifreq;
14313 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
14314 
14315 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
14316 	if ((grp = ipif->ipif_ill->ill_phyint->phyint_grp) == NULL)
14317 		lifr->lifr_groupname[0] = '\0';
14318 	else
14319 		(void) strlcpy(lifr->lifr_groupname, grp->gr_name, LIFGRNAMSIZ);
14320 	rw_exit(&ipst->ips_ipmp_lock);
14321 	return (0);
14322 }
14323 
14324 /*
14325  * Process an SIOCGLIFGROUPINFO request.
14326  */
14327 /* ARGSUSED */
14328 int
14329 ip_sioctl_groupinfo(ipif_t *dummy_ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14330     ip_ioctl_cmd_t *ipip, void *dummy)
14331 {
14332 	ipmp_grp_t	*grp;
14333 	lifgroupinfo_t	*lifgr;
14334 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
14335 
14336 	/* ip_wput_nondata() verified mp->b_cont->b_cont */
14337 	lifgr = (lifgroupinfo_t *)mp->b_cont->b_cont->b_rptr;
14338 	lifgr->gi_grname[LIFGRNAMSIZ - 1] = '\0';
14339 
14340 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
14341 	if ((grp = ipmp_grp_lookup(lifgr->gi_grname, ipst)) == NULL) {
14342 		rw_exit(&ipst->ips_ipmp_lock);
14343 		return (ENOENT);
14344 	}
14345 	ipmp_grp_info(grp, lifgr);
14346 	rw_exit(&ipst->ips_ipmp_lock);
14347 	return (0);
14348 }
14349 
14350 static void
14351 ill_dl_down(ill_t *ill)
14352 {
14353 	/*
14354 	 * The ill is down; unbind but stay attached since we're still
14355 	 * associated with a PPA. If we have negotiated DLPI capabilites
14356 	 * with the data link service provider (IDS_OK) then reset them.
14357 	 * The interval between unbinding and rebinding is potentially
14358 	 * unbounded hence we cannot assume things will be the same.
14359 	 * The DLPI capabilities will be probed again when the data link
14360 	 * is brought up.
14361 	 */
14362 	mblk_t	*mp = ill->ill_unbind_mp;
14363 
14364 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
14365 
14366 	ill->ill_unbind_mp = NULL;
14367 	if (mp != NULL) {
14368 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
14369 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
14370 		    ill->ill_name));
14371 		mutex_enter(&ill->ill_lock);
14372 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
14373 		mutex_exit(&ill->ill_lock);
14374 		/*
14375 		 * ip_rput does not pass up normal (M_PROTO) DLPI messages
14376 		 * after ILL_CONDEMNED is set. So in the unplumb case, we call
14377 		 * ill_capability_dld_disable disable rightaway. If this is not
14378 		 * an unplumb operation then the disable happens on receipt of
14379 		 * the capab ack via ip_rput_dlpi_writer ->
14380 		 * ill_capability_ack_thr. In both cases the order of
14381 		 * the operations seen by DLD is capability disable followed
14382 		 * by DL_UNBIND. Also the DLD capability disable needs a
14383 		 * cv_wait'able context.
14384 		 */
14385 		if (ill->ill_state_flags & ILL_CONDEMNED)
14386 			ill_capability_dld_disable(ill);
14387 		ill_capability_reset(ill, B_FALSE);
14388 		ill_dlpi_send(ill, mp);
14389 	}
14390 
14391 	/*
14392 	 * Toss all of our multicast memberships.  We could keep them, but
14393 	 * then we'd have to do bookkeeping of any joins and leaves performed
14394 	 * by the application while the the interface is down (we can't just
14395 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
14396 	 * on a downed interface).
14397 	 */
14398 	ill_leave_multicast(ill);
14399 
14400 	mutex_enter(&ill->ill_lock);
14401 	ill->ill_dl_up = 0;
14402 	ill_nic_event_dispatch(ill, 0, NE_DOWN, NULL, 0);
14403 	mutex_exit(&ill->ill_lock);
14404 }
14405 
14406 static void
14407 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
14408 {
14409 	union DL_primitives *dlp;
14410 	t_uscalar_t prim;
14411 
14412 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
14413 
14414 	dlp = (union DL_primitives *)mp->b_rptr;
14415 	prim = dlp->dl_primitive;
14416 
14417 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
14418 	    dl_primstr(prim), prim, ill->ill_name));
14419 
14420 	switch (prim) {
14421 	case DL_PHYS_ADDR_REQ:
14422 	{
14423 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
14424 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
14425 		break;
14426 	}
14427 	case DL_BIND_REQ:
14428 		mutex_enter(&ill->ill_lock);
14429 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
14430 		mutex_exit(&ill->ill_lock);
14431 		break;
14432 	}
14433 
14434 	/*
14435 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
14436 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
14437 	 * we only wait for the ACK of the DL_UNBIND_REQ.
14438 	 */
14439 	mutex_enter(&ill->ill_lock);
14440 	if (!(ill->ill_state_flags & ILL_CONDEMNED) || (prim == DL_UNBIND_REQ))
14441 		ill->ill_dlpi_pending = prim;
14442 
14443 	mutex_exit(&ill->ill_lock);
14444 	putnext(ill->ill_wq, mp);
14445 }
14446 
14447 /*
14448  * Helper function for ill_dlpi_send().
14449  */
14450 /* ARGSUSED */
14451 static void
14452 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
14453 {
14454 	ill_dlpi_send(q->q_ptr, mp);
14455 }
14456 
14457 /*
14458  * Send a DLPI control message to the driver but make sure there
14459  * is only one outstanding message. Uses ill_dlpi_pending to tell
14460  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
14461  * when an ACK or a NAK is received to process the next queued message.
14462  */
14463 void
14464 ill_dlpi_send(ill_t *ill, mblk_t *mp)
14465 {
14466 	mblk_t **mpp;
14467 
14468 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
14469 
14470 	/*
14471 	 * To ensure that any DLPI requests for current exclusive operation
14472 	 * are always completely sent before any DLPI messages for other
14473 	 * operations, require writer access before enqueuing.
14474 	 */
14475 	if (!IAM_WRITER_ILL(ill)) {
14476 		ill_refhold(ill);
14477 		/* qwriter_ip() does the ill_refrele() */
14478 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
14479 		    NEW_OP, B_TRUE);
14480 		return;
14481 	}
14482 
14483 	mutex_enter(&ill->ill_lock);
14484 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
14485 		/* Must queue message. Tail insertion */
14486 		mpp = &ill->ill_dlpi_deferred;
14487 		while (*mpp != NULL)
14488 			mpp = &((*mpp)->b_next);
14489 
14490 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
14491 		    ill->ill_name));
14492 
14493 		*mpp = mp;
14494 		mutex_exit(&ill->ill_lock);
14495 		return;
14496 	}
14497 	mutex_exit(&ill->ill_lock);
14498 	ill_dlpi_dispatch(ill, mp);
14499 }
14500 
14501 static void
14502 ill_capability_send(ill_t *ill, mblk_t *mp)
14503 {
14504 	ill->ill_capab_pending_cnt++;
14505 	ill_dlpi_send(ill, mp);
14506 }
14507 
14508 void
14509 ill_capability_done(ill_t *ill)
14510 {
14511 	ASSERT(ill->ill_capab_pending_cnt != 0);
14512 
14513 	ill_dlpi_done(ill, DL_CAPABILITY_REQ);
14514 
14515 	ill->ill_capab_pending_cnt--;
14516 	if (ill->ill_capab_pending_cnt == 0 &&
14517 	    ill->ill_dlpi_capab_state == IDCS_OK)
14518 		ill_capability_reset_alloc(ill);
14519 }
14520 
14521 /*
14522  * Send all deferred DLPI messages without waiting for their ACKs.
14523  */
14524 void
14525 ill_dlpi_send_deferred(ill_t *ill)
14526 {
14527 	mblk_t *mp, *nextmp;
14528 
14529 	/*
14530 	 * Clear ill_dlpi_pending so that the message is not queued in
14531 	 * ill_dlpi_send().
14532 	 */
14533 	mutex_enter(&ill->ill_lock);
14534 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
14535 	mp = ill->ill_dlpi_deferred;
14536 	ill->ill_dlpi_deferred = NULL;
14537 	mutex_exit(&ill->ill_lock);
14538 
14539 	for (; mp != NULL; mp = nextmp) {
14540 		nextmp = mp->b_next;
14541 		mp->b_next = NULL;
14542 		ill_dlpi_send(ill, mp);
14543 	}
14544 }
14545 
14546 /*
14547  * Check if the DLPI primitive `prim' is pending; print a warning if not.
14548  */
14549 boolean_t
14550 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
14551 {
14552 	t_uscalar_t pending;
14553 
14554 	mutex_enter(&ill->ill_lock);
14555 	if (ill->ill_dlpi_pending == prim) {
14556 		mutex_exit(&ill->ill_lock);
14557 		return (B_TRUE);
14558 	}
14559 
14560 	/*
14561 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
14562 	 * without waiting, so don't print any warnings in that case.
14563 	 */
14564 	if (ill->ill_state_flags & ILL_CONDEMNED) {
14565 		mutex_exit(&ill->ill_lock);
14566 		return (B_FALSE);
14567 	}
14568 	pending = ill->ill_dlpi_pending;
14569 	mutex_exit(&ill->ill_lock);
14570 
14571 	if (pending == DL_PRIM_INVAL) {
14572 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
14573 		    "received unsolicited ack for %s on %s\n",
14574 		    dl_primstr(prim), ill->ill_name);
14575 	} else {
14576 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
14577 		    "received unexpected ack for %s on %s (expecting %s)\n",
14578 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
14579 	}
14580 	return (B_FALSE);
14581 }
14582 
14583 /*
14584  * Complete the current DLPI operation associated with `prim' on `ill' and
14585  * start the next queued DLPI operation (if any).  If there are no queued DLPI
14586  * operations and the ill's current exclusive IPSQ operation has finished
14587  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
14588  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
14589  * the comments above ipsq_current_finish() for details.
14590  */
14591 void
14592 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
14593 {
14594 	mblk_t *mp;
14595 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
14596 	ipxop_t *ipx = ipsq->ipsq_xop;
14597 
14598 	ASSERT(IAM_WRITER_IPSQ(ipsq));
14599 	mutex_enter(&ill->ill_lock);
14600 
14601 	ASSERT(prim != DL_PRIM_INVAL);
14602 	ASSERT(ill->ill_dlpi_pending == prim);
14603 
14604 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
14605 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
14606 
14607 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
14608 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
14609 		if (ipx->ipx_current_done) {
14610 			mutex_enter(&ipx->ipx_lock);
14611 			ipx->ipx_current_ipif = NULL;
14612 			mutex_exit(&ipx->ipx_lock);
14613 		}
14614 		cv_signal(&ill->ill_cv);
14615 		mutex_exit(&ill->ill_lock);
14616 		return;
14617 	}
14618 
14619 	ill->ill_dlpi_deferred = mp->b_next;
14620 	mp->b_next = NULL;
14621 	mutex_exit(&ill->ill_lock);
14622 
14623 	ill_dlpi_dispatch(ill, mp);
14624 }
14625 
14626 void
14627 conn_delete_ire(conn_t *connp, caddr_t arg)
14628 {
14629 	ipif_t	*ipif = (ipif_t *)arg;
14630 	ire_t	*ire;
14631 
14632 	/*
14633 	 * Look at the cached ires on conns which has pointers to ipifs.
14634 	 * We just call ire_refrele which clears up the reference
14635 	 * to ire. Called when a conn closes. Also called from ipif_free
14636 	 * to cleanup indirect references to the stale ipif via the cached ire.
14637 	 */
14638 	mutex_enter(&connp->conn_lock);
14639 	ire = connp->conn_ire_cache;
14640 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
14641 		connp->conn_ire_cache = NULL;
14642 		mutex_exit(&connp->conn_lock);
14643 		IRE_REFRELE_NOTR(ire);
14644 		return;
14645 	}
14646 	mutex_exit(&connp->conn_lock);
14647 
14648 }
14649 
14650 /*
14651  * Some operations (e.g., ipif_down()) conditionally delete a number
14652  * of IREs. Those IREs may have been previously cached in the conn structure.
14653  * This ipcl_walk() walker function releases all references to such IREs based
14654  * on the condemned flag.
14655  */
14656 /* ARGSUSED */
14657 void
14658 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
14659 {
14660 	ire_t	*ire;
14661 
14662 	mutex_enter(&connp->conn_lock);
14663 	ire = connp->conn_ire_cache;
14664 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
14665 		connp->conn_ire_cache = NULL;
14666 		mutex_exit(&connp->conn_lock);
14667 		IRE_REFRELE_NOTR(ire);
14668 		return;
14669 	}
14670 	mutex_exit(&connp->conn_lock);
14671 }
14672 
14673 /*
14674  * Take down a specific interface, but don't lose any information about it.
14675  * (Always called as writer.)
14676  * This function goes through the down sequence even if the interface is
14677  * already down. There are 2 reasons.
14678  * a. Currently we permit interface routes that depend on down interfaces
14679  *    to be added. This behaviour itself is questionable. However it appears
14680  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
14681  *    time. We go thru the cleanup in order to remove these routes.
14682  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
14683  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
14684  *    down, but we need to cleanup i.e. do ill_dl_down and
14685  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
14686  *
14687  * IP-MT notes:
14688  *
14689  * Model of reference to interfaces.
14690  *
14691  * The following members in ipif_t track references to the ipif.
14692  *	int     ipif_refcnt;    Active reference count
14693  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
14694  *	uint_t  ipif_ilm_cnt;   Number of ilms's references this ipif.
14695  *
14696  * The following members in ill_t track references to the ill.
14697  *	int             ill_refcnt;     active refcnt
14698  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
14699  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
14700  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
14701  *
14702  * Reference to an ipif or ill can be obtained in any of the following ways.
14703  *
14704  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
14705  * Pointers to ipif / ill from other data structures viz ire and conn.
14706  * Implicit reference to the ipif / ill by holding a reference to the ire.
14707  *
14708  * The ipif/ill lookup functions return a reference held ipif / ill.
14709  * ipif_refcnt and ill_refcnt track the reference counts respectively.
14710  * This is a purely dynamic reference count associated with threads holding
14711  * references to the ipif / ill. Pointers from other structures do not
14712  * count towards this reference count.
14713  *
14714  * ipif_ire_cnt/ill_ire_cnt is the number of ire's
14715  * associated with the ipif/ill. This is incremented whenever a new
14716  * ire is created referencing the ipif/ill. This is done atomically inside
14717  * ire_add_v[46] where the ire is actually added to the ire hash table.
14718  * The count is decremented in ire_inactive where the ire is destroyed.
14719  *
14720  * nce's reference ill's thru nce_ill and the count of nce's associated with
14721  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
14722  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
14723  * table. Similarly it is decremented in ndp_inactive() where the nce
14724  * is destroyed.
14725  *
14726  * ilm's reference to the ipif (for IPv4 ilm's) or the ill (for IPv6 ilm's)
14727  * is incremented in ilm_add_v6() and decremented before the ilm is freed
14728  * in ilm_walker_cleanup() or ilm_delete().
14729  *
14730  * Flow of ioctls involving interface down/up
14731  *
14732  * The following is the sequence of an attempt to set some critical flags on an
14733  * up interface.
14734  * ip_sioctl_flags
14735  * ipif_down
14736  * wait for ipif to be quiescent
14737  * ipif_down_tail
14738  * ip_sioctl_flags_tail
14739  *
14740  * All set ioctls that involve down/up sequence would have a skeleton similar
14741  * to the above. All the *tail functions are called after the refcounts have
14742  * dropped to the appropriate values.
14743  *
14744  * The mechanism to quiesce an ipif is as follows.
14745  *
14746  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
14747  * on the ipif. Callers either pass a flag requesting wait or the lookup
14748  *  functions will return NULL.
14749  *
14750  * Delete all ires referencing this ipif
14751  *
14752  * Any thread attempting to do an ipif_refhold on an ipif that has been
14753  * obtained thru a cached pointer will first make sure that
14754  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
14755  * increment the refcount.
14756  *
14757  * The above guarantees that the ipif refcount will eventually come down to
14758  * zero and the ipif will quiesce, once all threads that currently hold a
14759  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
14760  * ipif_refcount has dropped to zero and all ire's associated with this ipif
14761  * have also been ire_inactive'd. i.e. when ipif_{ire, ill}_cnt and
14762  * ipif_refcnt both drop to zero. See also: comments above IPIF_DOWN_OK()
14763  * in ip.h
14764  *
14765  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
14766  *
14767  * Threads trying to lookup an ipif or ill can pass a flag requesting
14768  * wait and restart if the ipif / ill cannot be looked up currently.
14769  * For eg. bind, and route operations (Eg. route add / delete) cannot return
14770  * failure if the ipif is currently undergoing an exclusive operation, and
14771  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
14772  * is restarted by ipsq_exit() when the current exclusive operation completes.
14773  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
14774  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
14775  * change while the ill_lock is held. Before dropping the ill_lock we acquire
14776  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
14777  * until we release the ipsq_lock, even though the the ill/ipif state flags
14778  * can change after we drop the ill_lock.
14779  *
14780  * An attempt to send out a packet using an ipif that is currently
14781  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
14782  * operation and restart it later when the exclusive condition on the ipif ends.
14783  * This is an example of not passing the wait flag to the lookup functions. For
14784  * example an attempt to refhold and use conn->conn_multicast_ipif and send
14785  * out a multicast packet on that ipif will fail while the ipif is
14786  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
14787  * currently IPIF_CHANGING will also fail.
14788  */
14789 int
14790 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
14791 {
14792 	ill_t		*ill = ipif->ipif_ill;
14793 	conn_t		*connp;
14794 	boolean_t	success;
14795 	boolean_t	ipif_was_up = B_FALSE;
14796 	ip_stack_t	*ipst = ill->ill_ipst;
14797 
14798 	ASSERT(IAM_WRITER_IPIF(ipif));
14799 
14800 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
14801 
14802 	if (ipif->ipif_flags & IPIF_UP) {
14803 		mutex_enter(&ill->ill_lock);
14804 		ipif->ipif_flags &= ~IPIF_UP;
14805 		ASSERT(ill->ill_ipif_up_count > 0);
14806 		--ill->ill_ipif_up_count;
14807 		mutex_exit(&ill->ill_lock);
14808 		ipif_was_up = B_TRUE;
14809 		/* Update status in SCTP's list */
14810 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
14811 		ill_nic_event_dispatch(ipif->ipif_ill,
14812 		    MAP_IPIF_ID(ipif->ipif_id), NE_LIF_DOWN, NULL, 0);
14813 	}
14814 
14815 	/*
14816 	 * Blow away memberships we established in ipif_multicast_up().
14817 	 */
14818 	ipif_multicast_down(ipif);
14819 
14820 	/*
14821 	 * Remove from the mapping for __sin6_src_id. We insert only
14822 	 * when the address is not INADDR_ANY. As IPv4 addresses are
14823 	 * stored as mapped addresses, we need to check for mapped
14824 	 * INADDR_ANY also.
14825 	 */
14826 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
14827 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
14828 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14829 		int err;
14830 
14831 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
14832 		    ipif->ipif_zoneid, ipst);
14833 		if (err != 0) {
14834 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
14835 		}
14836 	}
14837 
14838 	/*
14839 	 * Delete all IRE's pointing at this ipif or its source address.
14840 	 */
14841 	if (ipif->ipif_isv6) {
14842 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
14843 		    ipst);
14844 	} else {
14845 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
14846 		    ipst);
14847 	}
14848 
14849 	if (ipif_was_up && ill->ill_ipif_up_count == 0) {
14850 		/*
14851 		 * Since the interface is now down, it may have just become
14852 		 * inactive.  Note that this needs to be done even for a
14853 		 * lll_logical_down(), or ARP entries will not get correctly
14854 		 * restored when the interface comes back up.
14855 		 */
14856 		if (IS_UNDER_IPMP(ill))
14857 			ipmp_ill_refresh_active(ill);
14858 	}
14859 
14860 	/*
14861 	 * Cleaning up the conn_ire_cache or conns must be done only after the
14862 	 * ires have been deleted above. Otherwise a thread could end up
14863 	 * caching an ire in a conn after we have finished the cleanup of the
14864 	 * conn. The caching is done after making sure that the ire is not yet
14865 	 * condemned. Also documented in the block comment above ip_output
14866 	 */
14867 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
14868 	/* Also, delete the ires cached in SCTP */
14869 	sctp_ire_cache_flush(ipif);
14870 
14871 	/*
14872 	 * Update any other ipifs which have used "our" local address as
14873 	 * a source address. This entails removing and recreating IRE_INTERFACE
14874 	 * entries for such ipifs.
14875 	 */
14876 	if (ipif->ipif_isv6)
14877 		ipif_update_other_ipifs_v6(ipif);
14878 	else
14879 		ipif_update_other_ipifs(ipif);
14880 
14881 	/*
14882 	 * neighbor-discovery or arp entries for this interface.
14883 	 */
14884 	ipif_ndp_down(ipif);
14885 
14886 	/*
14887 	 * If mp is NULL the caller will wait for the appropriate refcnt.
14888 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
14889 	 * and ill_delete -> ipif_free -> ipif_down
14890 	 */
14891 	if (mp == NULL) {
14892 		ASSERT(q == NULL);
14893 		return (0);
14894 	}
14895 
14896 	if (CONN_Q(q)) {
14897 		connp = Q_TO_CONN(q);
14898 		mutex_enter(&connp->conn_lock);
14899 	} else {
14900 		connp = NULL;
14901 	}
14902 	mutex_enter(&ill->ill_lock);
14903 	/*
14904 	 * Are there any ire's pointing to this ipif that are still active ?
14905 	 * If this is the last ipif going down, are there any ire's pointing
14906 	 * to this ill that are still active ?
14907 	 */
14908 	if (ipif_is_quiescent(ipif)) {
14909 		mutex_exit(&ill->ill_lock);
14910 		if (connp != NULL)
14911 			mutex_exit(&connp->conn_lock);
14912 		return (0);
14913 	}
14914 
14915 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
14916 	    ill->ill_name, (void *)ill));
14917 	/*
14918 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
14919 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
14920 	 * which in turn is called by the last refrele on the ipif/ill/ire.
14921 	 */
14922 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
14923 	if (!success) {
14924 		/* The conn is closing. So just return */
14925 		ASSERT(connp != NULL);
14926 		mutex_exit(&ill->ill_lock);
14927 		mutex_exit(&connp->conn_lock);
14928 		return (EINTR);
14929 	}
14930 
14931 	mutex_exit(&ill->ill_lock);
14932 	if (connp != NULL)
14933 		mutex_exit(&connp->conn_lock);
14934 	return (EINPROGRESS);
14935 }
14936 
14937 void
14938 ipif_down_tail(ipif_t *ipif)
14939 {
14940 	ill_t	*ill = ipif->ipif_ill;
14941 
14942 	/*
14943 	 * Skip any loopback interface (null wq).
14944 	 * If this is the last logical interface on the ill
14945 	 * have ill_dl_down tell the driver we are gone (unbind)
14946 	 * Note that lun 0 can ipif_down even though
14947 	 * there are other logical units that are up.
14948 	 * This occurs e.g. when we change a "significant" IFF_ flag.
14949 	 */
14950 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
14951 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
14952 	    ill->ill_dl_up) {
14953 		ill_dl_down(ill);
14954 	}
14955 	ill->ill_logical_down = 0;
14956 
14957 	/*
14958 	 * Has to be after removing the routes in ipif_down_delete_ire.
14959 	 */
14960 	ipif_resolver_down(ipif);
14961 
14962 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
14963 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif, RTSQ_DEFAULT);
14964 }
14965 
14966 /*
14967  * Bring interface logically down without bringing the physical interface
14968  * down e.g. when the netmask is changed. This avoids long lasting link
14969  * negotiations between an ethernet interface and a certain switches.
14970  */
14971 static int
14972 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
14973 {
14974 	/*
14975 	 * The ill_logical_down flag is a transient flag. It is set here
14976 	 * and is cleared once the down has completed in ipif_down_tail.
14977 	 * This flag does not indicate whether the ill stream is in the
14978 	 * DL_BOUND state with the driver. Instead this flag is used by
14979 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
14980 	 * the driver. The state of the ill stream i.e. whether it is
14981 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
14982 	 */
14983 	ipif->ipif_ill->ill_logical_down = 1;
14984 	return (ipif_down(ipif, q, mp));
14985 }
14986 
14987 /*
14988  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
14989  * If the usesrc client ILL is already part of a usesrc group or not,
14990  * in either case a ire_stq with the matching usesrc client ILL will
14991  * locate the IRE's that need to be deleted. We want IREs to be created
14992  * with the new source address.
14993  */
14994 static void
14995 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
14996 {
14997 	ill_t	*ucill = (ill_t *)ill_arg;
14998 
14999 	ASSERT(IAM_WRITER_ILL(ucill));
15000 
15001 	if (ire->ire_stq == NULL)
15002 		return;
15003 
15004 	if ((ire->ire_type == IRE_CACHE) &&
15005 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
15006 		ire_delete(ire);
15007 }
15008 
15009 /*
15010  * ire_walk routine to delete every IRE dependent on the interface
15011  * address that is going down.	(Always called as writer.)
15012  * Works for both v4 and v6.
15013  * In addition for checking for ire_ipif matches it also checks for
15014  * IRE_CACHE entries which have the same source address as the
15015  * disappearing ipif since ipif_select_source might have picked
15016  * that source. Note that ipif_down/ipif_update_other_ipifs takes
15017  * care of any IRE_INTERFACE with the disappearing source address.
15018  */
15019 static void
15020 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
15021 {
15022 	ipif_t	*ipif = (ipif_t *)ipif_arg;
15023 
15024 	ASSERT(IAM_WRITER_IPIF(ipif));
15025 	if (ire->ire_ipif == NULL)
15026 		return;
15027 
15028 	if (ire->ire_ipif != ipif) {
15029 		/*
15030 		 * Look for a matching source address.
15031 		 */
15032 		if (ire->ire_type != IRE_CACHE)
15033 			return;
15034 		if (ipif->ipif_flags & IPIF_NOLOCAL)
15035 			return;
15036 
15037 		if (ire->ire_ipversion == IPV4_VERSION) {
15038 			if (ire->ire_src_addr != ipif->ipif_src_addr)
15039 				return;
15040 		} else {
15041 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
15042 			    &ipif->ipif_v6lcl_addr))
15043 				return;
15044 		}
15045 		ire_delete(ire);
15046 		return;
15047 	}
15048 	/*
15049 	 * ire_delete() will do an ire_flush_cache which will delete
15050 	 * all ire_ipif matches
15051 	 */
15052 	ire_delete(ire);
15053 }
15054 
15055 /*
15056  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
15057  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
15058  * 2) when an interface is brought up or down (on that ill).
15059  * This ensures that the IRE_CACHE entries don't retain stale source
15060  * address selection results.
15061  */
15062 void
15063 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
15064 {
15065 	ill_t	*ill = (ill_t *)ill_arg;
15066 
15067 	ASSERT(IAM_WRITER_ILL(ill));
15068 	ASSERT(ire->ire_type == IRE_CACHE);
15069 
15070 	/*
15071 	 * We are called for IRE_CACHEs whose ire_stq or ire_ipif matches
15072 	 * ill, but we only want to delete the IRE if ire_ipif matches.
15073 	 */
15074 	ASSERT(ire->ire_ipif != NULL);
15075 	if (ill == ire->ire_ipif->ipif_ill)
15076 		ire_delete(ire);
15077 }
15078 
15079 /*
15080  * Delete all the IREs whose ire_stq's reference `ill_arg'.  IPMP uses this
15081  * instead of ill_ipif_cache_delete() because ire_ipif->ipif_ill references
15082  * the IPMP ill.
15083  */
15084 void
15085 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
15086 {
15087 	ill_t	*ill = (ill_t *)ill_arg;
15088 
15089 	ASSERT(IAM_WRITER_ILL(ill));
15090 	ASSERT(ire->ire_type == IRE_CACHE);
15091 
15092 	/*
15093 	 * We are called for IRE_CACHEs whose ire_stq or ire_ipif matches
15094 	 * ill, but we only want to delete the IRE if ire_stq matches.
15095 	 */
15096 	if (ire->ire_stq->q_ptr == ill_arg)
15097 		ire_delete(ire);
15098 }
15099 
15100 /*
15101  * Delete all broadcast IREs with a source address on `ill_arg'.
15102  */
15103 static void
15104 ill_broadcast_delete(ire_t *ire, char *ill_arg)
15105 {
15106 	ill_t *ill = (ill_t *)ill_arg;
15107 
15108 	ASSERT(IAM_WRITER_ILL(ill));
15109 	ASSERT(ire->ire_type == IRE_BROADCAST);
15110 
15111 	if (ire->ire_ipif->ipif_ill == ill)
15112 		ire_delete(ire);
15113 }
15114 
15115 /*
15116  * Initiate deallocate of an IPIF. Always called as writer. Called by
15117  * ill_delete or ip_sioctl_removeif.
15118  */
15119 static void
15120 ipif_free(ipif_t *ipif)
15121 {
15122 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15123 
15124 	ASSERT(IAM_WRITER_IPIF(ipif));
15125 
15126 	if (ipif->ipif_recovery_id != 0)
15127 		(void) untimeout(ipif->ipif_recovery_id);
15128 	ipif->ipif_recovery_id = 0;
15129 
15130 	/* Remove conn references */
15131 	reset_conn_ipif(ipif);
15132 
15133 	/*
15134 	 * Make sure we have valid net and subnet broadcast ire's for the
15135 	 * other ipif's which share them with this ipif.
15136 	 */
15137 	if (!ipif->ipif_isv6)
15138 		ipif_check_bcast_ires(ipif);
15139 
15140 	/*
15141 	 * Take down the interface. We can be called either from ill_delete
15142 	 * or from ip_sioctl_removeif.
15143 	 */
15144 	(void) ipif_down(ipif, NULL, NULL);
15145 
15146 	/*
15147 	 * Now that the interface is down, there's no chance it can still
15148 	 * become a duplicate.  Cancel any timer that may have been set while
15149 	 * tearing down.
15150 	 */
15151 	if (ipif->ipif_recovery_id != 0)
15152 		(void) untimeout(ipif->ipif_recovery_id);
15153 	ipif->ipif_recovery_id = 0;
15154 
15155 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15156 	/* Remove pointers to this ill in the multicast routing tables */
15157 	reset_mrt_vif_ipif(ipif);
15158 	/* If necessary, clear the cached source ipif rotor. */
15159 	if (ipif->ipif_ill->ill_src_ipif == ipif)
15160 		ipif->ipif_ill->ill_src_ipif = NULL;
15161 	rw_exit(&ipst->ips_ill_g_lock);
15162 }
15163 
15164 static void
15165 ipif_free_tail(ipif_t *ipif)
15166 {
15167 	mblk_t	*mp;
15168 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
15169 
15170 	/*
15171 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
15172 	 */
15173 	mutex_enter(&ipif->ipif_saved_ire_lock);
15174 	mp = ipif->ipif_saved_ire_mp;
15175 	ipif->ipif_saved_ire_mp = NULL;
15176 	mutex_exit(&ipif->ipif_saved_ire_lock);
15177 	freemsg(mp);
15178 
15179 	/*
15180 	 * Need to hold both ill_g_lock and ill_lock while
15181 	 * inserting or removing an ipif from the linked list
15182 	 * of ipifs hanging off the ill.
15183 	 */
15184 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15185 
15186 	ASSERT(ilm_walk_ipif(ipif) == 0);
15187 
15188 #ifdef DEBUG
15189 	ipif_trace_cleanup(ipif);
15190 #endif
15191 
15192 	/* Ask SCTP to take it out of it list */
15193 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
15194 
15195 	/* Get it out of the ILL interface list. */
15196 	ipif_remove(ipif);
15197 	rw_exit(&ipst->ips_ill_g_lock);
15198 
15199 	mutex_destroy(&ipif->ipif_saved_ire_lock);
15200 
15201 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
15202 	ASSERT(ipif->ipif_recovery_id == 0);
15203 
15204 	/* Free the memory. */
15205 	mi_free(ipif);
15206 }
15207 
15208 /*
15209  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
15210  * is zero.
15211  */
15212 void
15213 ipif_get_name(const ipif_t *ipif, char *buf, int len)
15214 {
15215 	char	lbuf[LIFNAMSIZ];
15216 	char	*name;
15217 	size_t	name_len;
15218 
15219 	buf[0] = '\0';
15220 	name = ipif->ipif_ill->ill_name;
15221 	name_len = ipif->ipif_ill->ill_name_length;
15222 	if (ipif->ipif_id != 0) {
15223 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
15224 		    ipif->ipif_id);
15225 		name = lbuf;
15226 		name_len = mi_strlen(name) + 1;
15227 	}
15228 	len -= 1;
15229 	buf[len] = '\0';
15230 	len = MIN(len, name_len);
15231 	bcopy(name, buf, len);
15232 }
15233 
15234 /*
15235  * Find an IPIF based on the name passed in.  Names can be of the
15236  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
15237  * The <phys> string can have forms like <dev><#> (e.g., le0),
15238  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
15239  * When there is no colon, the implied unit id is zero. <phys> must
15240  * correspond to the name of an ILL.  (May be called as writer.)
15241  */
15242 static ipif_t *
15243 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
15244     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
15245     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
15246 {
15247 	char	*cp;
15248 	char	*endp;
15249 	long	id;
15250 	ill_t	*ill;
15251 	ipif_t	*ipif;
15252 	uint_t	ire_type;
15253 	boolean_t did_alloc = B_FALSE;
15254 	ipsq_t	*ipsq;
15255 
15256 	if (error != NULL)
15257 		*error = 0;
15258 
15259 	/*
15260 	 * If the caller wants to us to create the ipif, make sure we have a
15261 	 * valid zoneid
15262 	 */
15263 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
15264 
15265 	if (namelen == 0) {
15266 		if (error != NULL)
15267 			*error = ENXIO;
15268 		return (NULL);
15269 	}
15270 
15271 	*exists = B_FALSE;
15272 	/* Look for a colon in the name. */
15273 	endp = &name[namelen];
15274 	for (cp = endp; --cp > name; ) {
15275 		if (*cp == IPIF_SEPARATOR_CHAR)
15276 			break;
15277 	}
15278 
15279 	if (*cp == IPIF_SEPARATOR_CHAR) {
15280 		/*
15281 		 * Reject any non-decimal aliases for logical
15282 		 * interfaces. Aliases with leading zeroes
15283 		 * are also rejected as they introduce ambiguity
15284 		 * in the naming of the interfaces.
15285 		 * In order to confirm with existing semantics,
15286 		 * and to not break any programs/script relying
15287 		 * on that behaviour, if<0>:0 is considered to be
15288 		 * a valid interface.
15289 		 *
15290 		 * If alias has two or more digits and the first
15291 		 * is zero, fail.
15292 		 */
15293 		if (&cp[2] < endp && cp[1] == '0') {
15294 			if (error != NULL)
15295 				*error = EINVAL;
15296 			return (NULL);
15297 		}
15298 	}
15299 
15300 	if (cp <= name) {
15301 		cp = endp;
15302 	} else {
15303 		*cp = '\0';
15304 	}
15305 
15306 	/*
15307 	 * Look up the ILL, based on the portion of the name
15308 	 * before the slash. ill_lookup_on_name returns a held ill.
15309 	 * Temporary to check whether ill exists already. If so
15310 	 * ill_lookup_on_name will clear it.
15311 	 */
15312 	ill = ill_lookup_on_name(name, do_alloc, isv6,
15313 	    q, mp, func, error, &did_alloc, ipst);
15314 	if (cp != endp)
15315 		*cp = IPIF_SEPARATOR_CHAR;
15316 	if (ill == NULL)
15317 		return (NULL);
15318 
15319 	/* Establish the unit number in the name. */
15320 	id = 0;
15321 	if (cp < endp && *endp == '\0') {
15322 		/* If there was a colon, the unit number follows. */
15323 		cp++;
15324 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
15325 			ill_refrele(ill);
15326 			if (error != NULL)
15327 				*error = ENXIO;
15328 			return (NULL);
15329 		}
15330 	}
15331 
15332 	GRAB_CONN_LOCK(q);
15333 	mutex_enter(&ill->ill_lock);
15334 	/* Now see if there is an IPIF with this unit number. */
15335 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15336 		if (ipif->ipif_id == id) {
15337 			if (zoneid != ALL_ZONES &&
15338 			    zoneid != ipif->ipif_zoneid &&
15339 			    ipif->ipif_zoneid != ALL_ZONES) {
15340 				mutex_exit(&ill->ill_lock);
15341 				RELEASE_CONN_LOCK(q);
15342 				ill_refrele(ill);
15343 				if (error != NULL)
15344 					*error = ENXIO;
15345 				return (NULL);
15346 			}
15347 			/*
15348 			 * The block comment at the start of ipif_down
15349 			 * explains the use of the macros used below
15350 			 */
15351 			if (IPIF_CAN_LOOKUP(ipif)) {
15352 				ipif_refhold_locked(ipif);
15353 				mutex_exit(&ill->ill_lock);
15354 				if (!did_alloc)
15355 					*exists = B_TRUE;
15356 				/*
15357 				 * Drop locks before calling ill_refrele
15358 				 * since it can potentially call into
15359 				 * ipif_ill_refrele_tail which can end up
15360 				 * in trying to acquire any lock.
15361 				 */
15362 				RELEASE_CONN_LOCK(q);
15363 				ill_refrele(ill);
15364 				return (ipif);
15365 			} else if (IPIF_CAN_WAIT(ipif, q)) {
15366 				ipsq = ill->ill_phyint->phyint_ipsq;
15367 				mutex_enter(&ipsq->ipsq_lock);
15368 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
15369 				mutex_exit(&ill->ill_lock);
15370 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
15371 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
15372 				mutex_exit(&ipsq->ipsq_lock);
15373 				RELEASE_CONN_LOCK(q);
15374 				ill_refrele(ill);
15375 				if (error != NULL)
15376 					*error = EINPROGRESS;
15377 				return (NULL);
15378 			}
15379 		}
15380 	}
15381 	RELEASE_CONN_LOCK(q);
15382 
15383 	if (!do_alloc) {
15384 		mutex_exit(&ill->ill_lock);
15385 		ill_refrele(ill);
15386 		if (error != NULL)
15387 			*error = ENXIO;
15388 		return (NULL);
15389 	}
15390 
15391 	/*
15392 	 * If none found, atomically allocate and return a new one.
15393 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
15394 	 * to support "receive only" use of lo0:1 etc. as is still done
15395 	 * below as an initial guess.
15396 	 * However, this is now likely to be overriden later in ipif_up_done()
15397 	 * when we know for sure what address has been configured on the
15398 	 * interface, since we might have more than one loopback interface
15399 	 * with a loopback address, e.g. in the case of zones, and all the
15400 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
15401 	 */
15402 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
15403 		ire_type = IRE_LOOPBACK;
15404 	else
15405 		ire_type = IRE_LOCAL;
15406 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE, B_TRUE);
15407 	if (ipif != NULL)
15408 		ipif_refhold_locked(ipif);
15409 	else if (error != NULL)
15410 		*error = ENOMEM;
15411 	mutex_exit(&ill->ill_lock);
15412 	ill_refrele(ill);
15413 	return (ipif);
15414 }
15415 
15416 /*
15417  * This routine is called whenever a new address comes up on an ipif.  If
15418  * we are configured to respond to address mask requests, then we are supposed
15419  * to broadcast an address mask reply at this time.  This routine is also
15420  * called if we are already up, but a netmask change is made.  This is legal
15421  * but might not make the system manager very popular.	(May be called
15422  * as writer.)
15423  */
15424 void
15425 ipif_mask_reply(ipif_t *ipif)
15426 {
15427 	icmph_t	*icmph;
15428 	ipha_t	*ipha;
15429 	mblk_t	*mp;
15430 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15431 
15432 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
15433 
15434 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
15435 		return;
15436 
15437 	/* ICMP mask reply is IPv4 only */
15438 	ASSERT(!ipif->ipif_isv6);
15439 	/* ICMP mask reply is not for a loopback interface */
15440 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
15441 
15442 	mp = allocb(REPLY_LEN, BPRI_HI);
15443 	if (mp == NULL)
15444 		return;
15445 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
15446 
15447 	ipha = (ipha_t *)mp->b_rptr;
15448 	bzero(ipha, REPLY_LEN);
15449 	*ipha = icmp_ipha;
15450 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
15451 	ipha->ipha_src = ipif->ipif_src_addr;
15452 	ipha->ipha_dst = ipif->ipif_brd_addr;
15453 	ipha->ipha_length = htons(REPLY_LEN);
15454 	ipha->ipha_ident = 0;
15455 
15456 	icmph = (icmph_t *)&ipha[1];
15457 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
15458 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
15459 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
15460 
15461 	put(ipif->ipif_wq, mp);
15462 
15463 #undef	REPLY_LEN
15464 }
15465 
15466 /*
15467  * When the mtu in the ipif changes, we call this routine through ire_walk
15468  * to update all the relevant IREs.
15469  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
15470  */
15471 static void
15472 ipif_mtu_change(ire_t *ire, char *ipif_arg)
15473 {
15474 	ipif_t *ipif = (ipif_t *)ipif_arg;
15475 
15476 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
15477 		return;
15478 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
15479 }
15480 
15481 /*
15482  * When the mtu in the ill changes, we call this routine through ire_walk
15483  * to update all the relevant IREs.
15484  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
15485  */
15486 void
15487 ill_mtu_change(ire_t *ire, char *ill_arg)
15488 {
15489 	ill_t	*ill = (ill_t *)ill_arg;
15490 
15491 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
15492 		return;
15493 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
15494 }
15495 
15496 /*
15497  * Join the ipif specific multicast groups.
15498  * Must be called after a mapping has been set up in the resolver.  (Always
15499  * called as writer.)
15500  */
15501 void
15502 ipif_multicast_up(ipif_t *ipif)
15503 {
15504 	int err;
15505 	ill_t *ill;
15506 
15507 	ASSERT(IAM_WRITER_IPIF(ipif));
15508 
15509 	ill = ipif->ipif_ill;
15510 
15511 	ip1dbg(("ipif_multicast_up\n"));
15512 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
15513 		return;
15514 
15515 	if (ipif->ipif_isv6) {
15516 		in6_addr_t v6allmc = ipv6_all_hosts_mcast;
15517 		in6_addr_t v6solmc = ipv6_solicited_node_mcast;
15518 
15519 		v6solmc.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
15520 
15521 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
15522 			return;
15523 
15524 		ip1dbg(("ipif_multicast_up - addmulti\n"));
15525 
15526 		/*
15527 		 * Join the all hosts multicast address.  We skip this for
15528 		 * underlying IPMP interfaces since they should be invisible.
15529 		 */
15530 		if (!IS_UNDER_IPMP(ill)) {
15531 			err = ip_addmulti_v6(&v6allmc, ill, ipif->ipif_zoneid,
15532 			    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
15533 			if (err != 0) {
15534 				ip0dbg(("ipif_multicast_up: "
15535 				    "all_hosts_mcast failed %d\n", err));
15536 				return;
15537 			}
15538 			ipif->ipif_joined_allhosts = 1;
15539 		}
15540 
15541 		/*
15542 		 * Enable multicast for the solicited node multicast address
15543 		 */
15544 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
15545 			err = ip_addmulti_v6(&v6solmc, ill, ipif->ipif_zoneid,
15546 			    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
15547 			if (err != 0) {
15548 				ip0dbg(("ipif_multicast_up: solicited MC"
15549 				    " failed %d\n", err));
15550 				if (ipif->ipif_joined_allhosts) {
15551 					(void) ip_delmulti_v6(&v6allmc, ill,
15552 					    ipif->ipif_zoneid, B_TRUE, B_TRUE);
15553 					ipif->ipif_joined_allhosts = 0;
15554 				}
15555 				return;
15556 			}
15557 		}
15558 	} else {
15559 		if (ipif->ipif_lcl_addr == INADDR_ANY || IS_UNDER_IPMP(ill))
15560 			return;
15561 
15562 		/* Join the all hosts multicast address */
15563 		ip1dbg(("ipif_multicast_up - addmulti\n"));
15564 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
15565 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
15566 		if (err) {
15567 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
15568 			return;
15569 		}
15570 	}
15571 	ipif->ipif_multicast_up = 1;
15572 }
15573 
15574 /*
15575  * Blow away any multicast groups that we joined in ipif_multicast_up().
15576  * (Explicit memberships are blown away in ill_leave_multicast() when the
15577  * ill is brought down.)
15578  */
15579 void
15580 ipif_multicast_down(ipif_t *ipif)
15581 {
15582 	int err;
15583 
15584 	ASSERT(IAM_WRITER_IPIF(ipif));
15585 
15586 	ip1dbg(("ipif_multicast_down\n"));
15587 	if (!ipif->ipif_multicast_up)
15588 		return;
15589 
15590 	ip1dbg(("ipif_multicast_down - delmulti\n"));
15591 
15592 	if (!ipif->ipif_isv6) {
15593 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
15594 		    B_TRUE);
15595 		if (err != 0)
15596 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
15597 
15598 		ipif->ipif_multicast_up = 0;
15599 		return;
15600 	}
15601 
15602 	/*
15603 	 * Leave the all-hosts multicast address.
15604 	 */
15605 	if (ipif->ipif_joined_allhosts) {
15606 		err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
15607 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
15608 		if (err != 0) {
15609 			ip0dbg(("ipif_multicast_down: all_hosts_mcast "
15610 			    "failed %d\n", err));
15611 		}
15612 		ipif->ipif_joined_allhosts = 0;
15613 	}
15614 
15615 	/*
15616 	 * Disable multicast for the solicited node multicast address
15617 	 */
15618 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
15619 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
15620 
15621 		ipv6_multi.s6_addr32[3] |=
15622 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
15623 
15624 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
15625 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
15626 		if (err != 0) {
15627 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
15628 			    err));
15629 		}
15630 	}
15631 
15632 	ipif->ipif_multicast_up = 0;
15633 }
15634 
15635 /*
15636  * Used when an interface comes up to recreate any extra routes on this
15637  * interface.
15638  */
15639 static ire_t **
15640 ipif_recover_ire(ipif_t *ipif)
15641 {
15642 	mblk_t	*mp;
15643 	ire_t	**ipif_saved_irep;
15644 	ire_t	**irep;
15645 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15646 
15647 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
15648 	    ipif->ipif_id));
15649 
15650 	mutex_enter(&ipif->ipif_saved_ire_lock);
15651 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
15652 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
15653 	if (ipif_saved_irep == NULL) {
15654 		mutex_exit(&ipif->ipif_saved_ire_lock);
15655 		return (NULL);
15656 	}
15657 
15658 	irep = ipif_saved_irep;
15659 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
15660 		ire_t		*ire;
15661 		queue_t		*rfq;
15662 		queue_t		*stq;
15663 		ifrt_t		*ifrt;
15664 		uchar_t		*src_addr;
15665 		uchar_t		*gateway_addr;
15666 		ushort_t	type;
15667 
15668 		/*
15669 		 * When the ire was initially created and then added in
15670 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
15671 		 * in the case of a traditional interface route, or as one of
15672 		 * the IRE_OFFSUBNET types (with the exception of
15673 		 * IRE_HOST types ire which is created by icmp_redirect() and
15674 		 * which we don't need to save or recover).  In the case where
15675 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
15676 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
15677 		 * to satisfy software like GateD and Sun Cluster which creates
15678 		 * routes using the the loopback interface's address as a
15679 		 * gateway.
15680 		 *
15681 		 * As ifrt->ifrt_type reflects the already updated ire_type,
15682 		 * ire_create() will be called in the same way here as
15683 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
15684 		 * the route looks like a traditional interface route (where
15685 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
15686 		 * the saved ifrt->ifrt_type.  This means that in the case where
15687 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
15688 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
15689 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
15690 		 */
15691 		ifrt = (ifrt_t *)mp->b_rptr;
15692 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
15693 		if (ifrt->ifrt_type & IRE_INTERFACE) {
15694 			rfq = NULL;
15695 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
15696 			    ? ipif->ipif_rq : ipif->ipif_wq;
15697 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
15698 			    ? (uint8_t *)&ifrt->ifrt_src_addr
15699 			    : (uint8_t *)&ipif->ipif_src_addr;
15700 			gateway_addr = NULL;
15701 			type = ipif->ipif_net_type;
15702 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
15703 			/* Recover multiroute broadcast IRE. */
15704 			rfq = ipif->ipif_rq;
15705 			stq = ipif->ipif_wq;
15706 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
15707 			    ? (uint8_t *)&ifrt->ifrt_src_addr
15708 			    : (uint8_t *)&ipif->ipif_src_addr;
15709 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
15710 			type = ifrt->ifrt_type;
15711 		} else {
15712 			rfq = NULL;
15713 			stq = NULL;
15714 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
15715 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
15716 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
15717 			type = ifrt->ifrt_type;
15718 		}
15719 
15720 		/*
15721 		 * Create a copy of the IRE with the saved address and netmask.
15722 		 */
15723 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
15724 		    "0x%x/0x%x\n",
15725 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
15726 		    ntohl(ifrt->ifrt_addr),
15727 		    ntohl(ifrt->ifrt_mask)));
15728 		ire = ire_create(
15729 		    (uint8_t *)&ifrt->ifrt_addr,
15730 		    (uint8_t *)&ifrt->ifrt_mask,
15731 		    src_addr,
15732 		    gateway_addr,
15733 		    &ifrt->ifrt_max_frag,
15734 		    NULL,
15735 		    rfq,
15736 		    stq,
15737 		    type,
15738 		    ipif,
15739 		    0,
15740 		    0,
15741 		    0,
15742 		    ifrt->ifrt_flags,
15743 		    &ifrt->ifrt_iulp_info,
15744 		    NULL,
15745 		    NULL,
15746 		    ipst);
15747 
15748 		if (ire == NULL) {
15749 			mutex_exit(&ipif->ipif_saved_ire_lock);
15750 			kmem_free(ipif_saved_irep,
15751 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
15752 			return (NULL);
15753 		}
15754 
15755 		/*
15756 		 * Some software (for example, GateD and Sun Cluster) attempts
15757 		 * to create (what amount to) IRE_PREFIX routes with the
15758 		 * loopback address as the gateway.  This is primarily done to
15759 		 * set up prefixes with the RTF_REJECT flag set (for example,
15760 		 * when generating aggregate routes.)
15761 		 *
15762 		 * If the IRE type (as defined by ipif->ipif_net_type) is
15763 		 * IRE_LOOPBACK, then we map the request into a
15764 		 * IRE_IF_NORESOLVER.
15765 		 */
15766 		if (ipif->ipif_net_type == IRE_LOOPBACK)
15767 			ire->ire_type = IRE_IF_NORESOLVER;
15768 		/*
15769 		 * ire held by ire_add, will be refreled' towards the
15770 		 * the end of ipif_up_done
15771 		 */
15772 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
15773 		*irep = ire;
15774 		irep++;
15775 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
15776 	}
15777 	mutex_exit(&ipif->ipif_saved_ire_lock);
15778 	return (ipif_saved_irep);
15779 }
15780 
15781 /*
15782  * Used to set the netmask and broadcast address to default values when the
15783  * interface is brought up.  (Always called as writer.)
15784  */
15785 static void
15786 ipif_set_default(ipif_t *ipif)
15787 {
15788 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
15789 
15790 	if (!ipif->ipif_isv6) {
15791 		/*
15792 		 * Interface holds an IPv4 address. Default
15793 		 * mask is the natural netmask.
15794 		 */
15795 		if (!ipif->ipif_net_mask) {
15796 			ipaddr_t	v4mask;
15797 
15798 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
15799 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
15800 		}
15801 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
15802 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
15803 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
15804 		} else {
15805 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
15806 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
15807 		}
15808 		/*
15809 		 * NOTE: SunOS 4.X does this even if the broadcast address
15810 		 * has been already set thus we do the same here.
15811 		 */
15812 		if (ipif->ipif_flags & IPIF_BROADCAST) {
15813 			ipaddr_t	v4addr;
15814 
15815 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
15816 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
15817 		}
15818 	} else {
15819 		/*
15820 		 * Interface holds an IPv6-only address.  Default
15821 		 * mask is all-ones.
15822 		 */
15823 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
15824 			ipif->ipif_v6net_mask = ipv6_all_ones;
15825 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
15826 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
15827 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
15828 		} else {
15829 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
15830 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
15831 		}
15832 	}
15833 }
15834 
15835 /*
15836  * Return 0 if this address can be used as local address without causing
15837  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
15838  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
15839  * Note that the same IPv6 link-local address is allowed as long as the ills
15840  * are not on the same link.
15841  */
15842 int
15843 ip_addr_availability_check(ipif_t *new_ipif)
15844 {
15845 	in6_addr_t our_v6addr;
15846 	ill_t *ill;
15847 	ipif_t *ipif;
15848 	ill_walk_context_t ctx;
15849 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
15850 
15851 	ASSERT(IAM_WRITER_IPIF(new_ipif));
15852 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
15853 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
15854 
15855 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
15856 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
15857 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
15858 		return (0);
15859 
15860 	our_v6addr = new_ipif->ipif_v6lcl_addr;
15861 
15862 	if (new_ipif->ipif_isv6)
15863 		ill = ILL_START_WALK_V6(&ctx, ipst);
15864 	else
15865 		ill = ILL_START_WALK_V4(&ctx, ipst);
15866 
15867 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
15868 		for (ipif = ill->ill_ipif; ipif != NULL;
15869 		    ipif = ipif->ipif_next) {
15870 			if ((ipif == new_ipif) ||
15871 			    !(ipif->ipif_flags & IPIF_UP) ||
15872 			    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
15873 			    !IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
15874 			    &our_v6addr))
15875 				continue;
15876 
15877 			if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
15878 				new_ipif->ipif_flags |= IPIF_UNNUMBERED;
15879 			else if (ipif->ipif_flags & IPIF_POINTOPOINT)
15880 				ipif->ipif_flags |= IPIF_UNNUMBERED;
15881 			else if ((IN6_IS_ADDR_LINKLOCAL(&our_v6addr) ||
15882 			    IN6_IS_ADDR_SITELOCAL(&our_v6addr)) &&
15883 			    !IS_ON_SAME_LAN(ill, new_ipif->ipif_ill))
15884 				continue;
15885 			else if (new_ipif->ipif_zoneid != ipif->ipif_zoneid &&
15886 			    ipif->ipif_zoneid != ALL_ZONES && IS_LOOPBACK(ill))
15887 				continue;
15888 			else if (new_ipif->ipif_ill == ill)
15889 				return (EADDRINUSE);
15890 			else
15891 				return (EADDRNOTAVAIL);
15892 		}
15893 	}
15894 
15895 	return (0);
15896 }
15897 
15898 /*
15899  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
15900  * IREs for the ipif.
15901  * When the routine returns EINPROGRESS then mp has been consumed and
15902  * the ioctl will be acked from ip_rput_dlpi.
15903  */
15904 int
15905 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
15906 {
15907 	ill_t		*ill = ipif->ipif_ill;
15908 	boolean_t 	isv6 = ipif->ipif_isv6;
15909 	int		err = 0;
15910 	boolean_t	success;
15911 	uint_t		ipif_orig_id;
15912 	ip_stack_t	*ipst = ill->ill_ipst;
15913 
15914 	ASSERT(IAM_WRITER_IPIF(ipif));
15915 
15916 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
15917 
15918 	/* Shouldn't get here if it is already up. */
15919 	if (ipif->ipif_flags & IPIF_UP)
15920 		return (EALREADY);
15921 
15922 	/*
15923 	 * If this is a request to bring up a data address on an interface
15924 	 * under IPMP, then move the address to its IPMP meta-interface and
15925 	 * try to bring it up.  One complication is that the zeroth ipif for
15926 	 * an ill is special, in that every ill always has one, and that code
15927 	 * throughout IP deferences ill->ill_ipif without holding any locks.
15928 	 */
15929 	if (IS_UNDER_IPMP(ill) && ipmp_ipif_is_dataaddr(ipif) &&
15930 	    (!ipif->ipif_isv6 || !V6_IPIF_LINKLOCAL(ipif))) {
15931 		ipif_t	*stubipif = NULL, *moveipif = NULL;
15932 		ill_t	*ipmp_ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
15933 
15934 		/*
15935 		 * The ipif being brought up should be quiesced.  If it's not,
15936 		 * something has gone amiss and we need to bail out.  (If it's
15937 		 * quiesced, we know it will remain so via IPIF_CHANGING.)
15938 		 */
15939 		mutex_enter(&ill->ill_lock);
15940 		if (!ipif_is_quiescent(ipif)) {
15941 			mutex_exit(&ill->ill_lock);
15942 			return (EINVAL);
15943 		}
15944 		mutex_exit(&ill->ill_lock);
15945 
15946 		/*
15947 		 * If we're going to need to allocate ipifs, do it prior
15948 		 * to starting the move (and grabbing locks).
15949 		 */
15950 		if (ipif->ipif_id == 0) {
15951 			moveipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
15952 			    B_FALSE);
15953 			stubipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
15954 			    B_FALSE);
15955 			if (moveipif == NULL || stubipif == NULL) {
15956 				mi_free(moveipif);
15957 				mi_free(stubipif);
15958 				return (ENOMEM);
15959 			}
15960 		}
15961 
15962 		/*
15963 		 * Grab or transfer the ipif to move.  During the move, keep
15964 		 * ill_g_lock held to prevent any ill walker threads from
15965 		 * seeing things in an inconsistent state.
15966 		 */
15967 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15968 		if (ipif->ipif_id != 0) {
15969 			ipif_remove(ipif);
15970 		} else {
15971 			ipif_transfer(ipif, moveipif, stubipif);
15972 			ipif = moveipif;
15973 		}
15974 
15975 		/*
15976 		 * Place the ipif on the IPMP ill.  If the zeroth ipif on
15977 		 * the IPMP ill is a stub (0.0.0.0 down address) then we
15978 		 * replace that one.  Otherwise, pick the next available slot.
15979 		 */
15980 		ipif->ipif_ill = ipmp_ill;
15981 		ipif_orig_id = ipif->ipif_id;
15982 
15983 		if (ipmp_ipif_is_stubaddr(ipmp_ill->ill_ipif)) {
15984 			ipif_transfer(ipif, ipmp_ill->ill_ipif, NULL);
15985 			ipif = ipmp_ill->ill_ipif;
15986 		} else {
15987 			ipif->ipif_id = -1;
15988 			if (ipif_insert(ipif, B_FALSE) != 0) {
15989 				/*
15990 				 * No more available ipif_id's -- put it back
15991 				 * on the original ill and fail the operation.
15992 				 * Since we're writer on the ill, we can be
15993 				 * sure our old slot is still available.
15994 				 */
15995 				ipif->ipif_id = ipif_orig_id;
15996 				ipif->ipif_ill = ill;
15997 				if (ipif_orig_id == 0) {
15998 					ipif_transfer(ipif, ill->ill_ipif,
15999 					    NULL);
16000 				} else {
16001 					VERIFY(ipif_insert(ipif, B_FALSE) == 0);
16002 				}
16003 				rw_exit(&ipst->ips_ill_g_lock);
16004 				return (ENOMEM);
16005 			}
16006 		}
16007 		rw_exit(&ipst->ips_ill_g_lock);
16008 
16009 		/*
16010 		 * Tell SCTP that the ipif has moved.  Note that even if we
16011 		 * had to allocate a new ipif, the original sequence id was
16012 		 * preserved and therefore SCTP won't know.
16013 		 */
16014 		sctp_move_ipif(ipif, ill, ipmp_ill);
16015 
16016 		/*
16017 		 * If the ipif being brought up was on slot zero, then we
16018 		 * first need to bring up the placeholder we stuck there.  In
16019 		 * ip_rput_dlpi_writer(), ip_arp_done(), or the recursive call
16020 		 * to ipif_up() itself, if we successfully bring up the
16021 		 * placeholder, we'll check ill_move_ipif and bring it up too.
16022 		 */
16023 		if (ipif_orig_id == 0) {
16024 			ASSERT(ill->ill_move_ipif == NULL);
16025 			ill->ill_move_ipif = ipif;
16026 			if ((err = ipif_up(ill->ill_ipif, q, mp)) == 0)
16027 				ASSERT(ill->ill_move_ipif == NULL);
16028 			if (err != EINPROGRESS)
16029 				ill->ill_move_ipif = NULL;
16030 			return (err);
16031 		}
16032 
16033 		/*
16034 		 * Bring it up on the IPMP ill.
16035 		 */
16036 		return (ipif_up(ipif, q, mp));
16037 	}
16038 
16039 	/* Skip arp/ndp for any loopback interface. */
16040 	if (ill->ill_wq != NULL) {
16041 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
16042 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
16043 
16044 		if (!ill->ill_dl_up) {
16045 			/*
16046 			 * ill_dl_up is not yet set. i.e. we are yet to
16047 			 * DL_BIND with the driver and this is the first
16048 			 * logical interface on the ill to become "up".
16049 			 * Tell the driver to get going (via DL_BIND_REQ).
16050 			 * Note that changing "significant" IFF_ flags
16051 			 * address/netmask etc cause a down/up dance, but
16052 			 * does not cause an unbind (DL_UNBIND) with the driver
16053 			 */
16054 			return (ill_dl_up(ill, ipif, mp, q));
16055 		}
16056 
16057 		/*
16058 		 * ipif_resolver_up may end up sending an
16059 		 * AR_INTERFACE_UP message to ARP, which would, in
16060 		 * turn send a DLPI message to the driver. ioctls are
16061 		 * serialized and so we cannot send more than one
16062 		 * interface up message at a time. If ipif_resolver_up
16063 		 * does send an interface up message to ARP, we get
16064 		 * EINPROGRESS and we will complete in ip_arp_done.
16065 		 */
16066 
16067 		ASSERT(connp != NULL || !CONN_Q(q));
16068 		if (connp != NULL)
16069 			mutex_enter(&connp->conn_lock);
16070 		mutex_enter(&ill->ill_lock);
16071 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
16072 		mutex_exit(&ill->ill_lock);
16073 		if (connp != NULL)
16074 			mutex_exit(&connp->conn_lock);
16075 		if (!success)
16076 			return (EINTR);
16077 
16078 		/*
16079 		 * Crank up the resolver.  For IPv6, this cranks up the
16080 		 * external resolver if one is configured, but even if an
16081 		 * external resolver isn't configured, it must be called to
16082 		 * reset DAD state.  For IPv6, if an external resolver is not
16083 		 * being used, ipif_resolver_up() will never return
16084 		 * EINPROGRESS, so we can always call ipif_ndp_up() here.
16085 		 * Note that if an external resolver is being used, there's no
16086 		 * need to call ipif_ndp_up() since it will do nothing.
16087 		 */
16088 		err = ipif_resolver_up(ipif, Res_act_initial);
16089 		if (err == EINPROGRESS) {
16090 			/* We will complete it in ip_arp_done() */
16091 			return (err);
16092 		}
16093 
16094 		if (isv6 && err == 0)
16095 			err = ipif_ndp_up(ipif, B_TRUE);
16096 
16097 		ASSERT(err != EINPROGRESS);
16098 		mp = ipsq_pending_mp_get(ipsq, &connp);
16099 		ASSERT(mp != NULL);
16100 		if (err != 0)
16101 			return (err);
16102 	} else {
16103 		/*
16104 		 * Interfaces without underlying hardware don't do duplicate
16105 		 * address detection.
16106 		 */
16107 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
16108 		ipif->ipif_addr_ready = 1;
16109 	}
16110 
16111 	err = isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif);
16112 	if (err == 0 && ill->ill_move_ipif != NULL) {
16113 		ipif = ill->ill_move_ipif;
16114 		ill->ill_move_ipif = NULL;
16115 		return (ipif_up(ipif, q, mp));
16116 	}
16117 	return (err);
16118 }
16119 
16120 /*
16121  * Perform a bind for the physical device.
16122  * When the routine returns EINPROGRESS then mp has been consumed and
16123  * the ioctl will be acked from ip_rput_dlpi.
16124  * Allocate an unbind message and save it until ipif_down.
16125  */
16126 static int
16127 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
16128 {
16129 	areq_t	*areq;
16130 	mblk_t	*areq_mp = NULL;
16131 	mblk_t	*bind_mp = NULL;
16132 	mblk_t	*unbind_mp = NULL;
16133 	conn_t	*connp;
16134 	boolean_t success;
16135 	uint16_t sap_addr;
16136 
16137 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
16138 	ASSERT(IAM_WRITER_ILL(ill));
16139 	ASSERT(mp != NULL);
16140 
16141 	/* Create a resolver cookie for ARP */
16142 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
16143 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
16144 		if (areq_mp == NULL)
16145 			return (ENOMEM);
16146 
16147 		freemsg(ill->ill_resolver_mp);
16148 		ill->ill_resolver_mp = areq_mp;
16149 		areq = (areq_t *)areq_mp->b_rptr;
16150 		sap_addr = ill->ill_sap;
16151 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
16152 	}
16153 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
16154 	    DL_BIND_REQ);
16155 	if (bind_mp == NULL)
16156 		goto bad;
16157 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
16158 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
16159 
16160 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
16161 	if (unbind_mp == NULL)
16162 		goto bad;
16163 
16164 	/*
16165 	 * Record state needed to complete this operation when the
16166 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
16167 	 */
16168 	ASSERT(WR(q)->q_next == NULL);
16169 	connp = Q_TO_CONN(q);
16170 
16171 	mutex_enter(&connp->conn_lock);
16172 	mutex_enter(&ipif->ipif_ill->ill_lock);
16173 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
16174 	mutex_exit(&ipif->ipif_ill->ill_lock);
16175 	mutex_exit(&connp->conn_lock);
16176 	if (!success)
16177 		goto bad;
16178 
16179 	/*
16180 	 * Save the unbind message for ill_dl_down(); it will be consumed when
16181 	 * the interface goes down.
16182 	 */
16183 	ASSERT(ill->ill_unbind_mp == NULL);
16184 	ill->ill_unbind_mp = unbind_mp;
16185 
16186 	ill_dlpi_send(ill, bind_mp);
16187 	/* Send down link-layer capabilities probe if not already done. */
16188 	ill_capability_probe(ill);
16189 
16190 	/*
16191 	 * Sysid used to rely on the fact that netboots set domainname
16192 	 * and the like. Now that miniroot boots aren't strictly netboots
16193 	 * and miniroot network configuration is driven from userland
16194 	 * these things still need to be set. This situation can be detected
16195 	 * by comparing the interface being configured here to the one
16196 	 * dhcifname was set to reference by the boot loader. Once sysid is
16197 	 * converted to use dhcp_ipc_getinfo() this call can go away.
16198 	 */
16199 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
16200 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
16201 	    (strlen(srpc_domain) == 0)) {
16202 		if (dhcpinit() != 0)
16203 			cmn_err(CE_WARN, "no cached dhcp response");
16204 	}
16205 
16206 	/*
16207 	 * This operation will complete in ip_rput_dlpi with either
16208 	 * a DL_BIND_ACK or DL_ERROR_ACK.
16209 	 */
16210 	return (EINPROGRESS);
16211 bad:
16212 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
16213 
16214 	freemsg(bind_mp);
16215 	freemsg(unbind_mp);
16216 	return (ENOMEM);
16217 }
16218 
16219 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
16220 
16221 /*
16222  * DLPI and ARP is up.
16223  * Create all the IREs associated with an interface bring up multicast.
16224  * Set the interface flag and finish other initialization
16225  * that potentially had to be differed to after DL_BIND_ACK.
16226  */
16227 int
16228 ipif_up_done(ipif_t *ipif)
16229 {
16230 	ire_t	*ire_array[20];
16231 	ire_t	**irep = ire_array;
16232 	ire_t	**irep1;
16233 	ipaddr_t net_mask = 0;
16234 	ipaddr_t subnet_mask, route_mask;
16235 	ill_t	*ill = ipif->ipif_ill;
16236 	queue_t	*stq;
16237 	ipif_t	 *src_ipif;
16238 	ipif_t   *tmp_ipif;
16239 	boolean_t	flush_ire_cache = B_TRUE;
16240 	int	err = 0;
16241 	ire_t	**ipif_saved_irep = NULL;
16242 	int ipif_saved_ire_cnt;
16243 	int	cnt;
16244 	boolean_t	src_ipif_held = B_FALSE;
16245 	boolean_t	loopback = B_FALSE;
16246 	ip_stack_t	*ipst = ill->ill_ipst;
16247 
16248 	ip1dbg(("ipif_up_done(%s:%u)\n",
16249 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
16250 	/* Check if this is a loopback interface */
16251 	if (ipif->ipif_ill->ill_wq == NULL)
16252 		loopback = B_TRUE;
16253 
16254 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
16255 	/*
16256 	 * If all other interfaces for this ill are down or DEPRECATED,
16257 	 * or otherwise unsuitable for source address selection, remove
16258 	 * any IRE_CACHE entries for this ill to make sure source
16259 	 * address selection gets to take this new ipif into account.
16260 	 * No need to hold ill_lock while traversing the ipif list since
16261 	 * we are writer
16262 	 */
16263 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
16264 	    tmp_ipif = tmp_ipif->ipif_next) {
16265 		if (((tmp_ipif->ipif_flags &
16266 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
16267 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
16268 		    (tmp_ipif == ipif))
16269 			continue;
16270 		/* first useable pre-existing interface */
16271 		flush_ire_cache = B_FALSE;
16272 		break;
16273 	}
16274 	if (flush_ire_cache)
16275 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
16276 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
16277 
16278 	/*
16279 	 * Figure out which way the send-to queue should go.  Only
16280 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
16281 	 * should show up here.
16282 	 */
16283 	switch (ill->ill_net_type) {
16284 	case IRE_IF_RESOLVER:
16285 		stq = ill->ill_rq;
16286 		break;
16287 	case IRE_IF_NORESOLVER:
16288 	case IRE_LOOPBACK:
16289 		stq = ill->ill_wq;
16290 		break;
16291 	default:
16292 		return (EINVAL);
16293 	}
16294 
16295 	if (IS_LOOPBACK(ill)) {
16296 		/*
16297 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
16298 		 * ipif_lookup_on_name(), but in the case of zones we can have
16299 		 * several loopback addresses on lo0. So all the interfaces with
16300 		 * loopback addresses need to be marked IRE_LOOPBACK.
16301 		 */
16302 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
16303 		    htonl(INADDR_LOOPBACK))
16304 			ipif->ipif_ire_type = IRE_LOOPBACK;
16305 		else
16306 			ipif->ipif_ire_type = IRE_LOCAL;
16307 	}
16308 
16309 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST) ||
16310 	    ((ipif->ipif_flags & IPIF_DEPRECATED) &&
16311 	    !(ipif->ipif_flags & IPIF_NOFAILOVER))) {
16312 		/*
16313 		 * Can't use our source address. Select a different
16314 		 * source address for the IRE_INTERFACE and IRE_LOCAL
16315 		 */
16316 		src_ipif = ipif_select_source(ipif->ipif_ill,
16317 		    ipif->ipif_subnet, ipif->ipif_zoneid);
16318 		if (src_ipif == NULL)
16319 			src_ipif = ipif;	/* Last resort */
16320 		else
16321 			src_ipif_held = B_TRUE;
16322 	} else {
16323 		src_ipif = ipif;
16324 	}
16325 
16326 	/* Create all the IREs associated with this interface */
16327 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
16328 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
16329 
16330 		/*
16331 		 * If we're on a labeled system then make sure that zone-
16332 		 * private addresses have proper remote host database entries.
16333 		 */
16334 		if (is_system_labeled() &&
16335 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
16336 		    !tsol_check_interface_address(ipif))
16337 			return (EINVAL);
16338 
16339 		/* Register the source address for __sin6_src_id */
16340 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
16341 		    ipif->ipif_zoneid, ipst);
16342 		if (err != 0) {
16343 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
16344 			return (err);
16345 		}
16346 
16347 		/* If the interface address is set, create the local IRE. */
16348 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
16349 		    (void *)ipif,
16350 		    ipif->ipif_ire_type,
16351 		    ntohl(ipif->ipif_lcl_addr)));
16352 		*irep++ = ire_create(
16353 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
16354 		    (uchar_t *)&ip_g_all_ones,		/* mask */
16355 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
16356 		    NULL,				/* no gateway */
16357 		    &ip_loopback_mtuplus,		/* max frag size */
16358 		    NULL,
16359 		    ipif->ipif_rq,			/* recv-from queue */
16360 		    NULL,				/* no send-to queue */
16361 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
16362 		    ipif,
16363 		    0,
16364 		    0,
16365 		    0,
16366 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
16367 		    RTF_PRIVATE : 0,
16368 		    &ire_uinfo_null,
16369 		    NULL,
16370 		    NULL,
16371 		    ipst);
16372 	} else {
16373 		ip1dbg((
16374 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
16375 		    ipif->ipif_ire_type,
16376 		    ntohl(ipif->ipif_lcl_addr),
16377 		    (uint_t)ipif->ipif_flags));
16378 	}
16379 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
16380 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
16381 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
16382 	} else {
16383 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
16384 	}
16385 
16386 	subnet_mask = ipif->ipif_net_mask;
16387 
16388 	/*
16389 	 * If mask was not specified, use natural netmask of
16390 	 * interface address. Also, store this mask back into the
16391 	 * ipif struct.
16392 	 */
16393 	if (subnet_mask == 0) {
16394 		subnet_mask = net_mask;
16395 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
16396 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
16397 		    ipif->ipif_v6subnet);
16398 	}
16399 
16400 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
16401 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
16402 	    ipif->ipif_subnet != INADDR_ANY) {
16403 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
16404 
16405 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
16406 			route_mask = IP_HOST_MASK;
16407 		} else {
16408 			route_mask = subnet_mask;
16409 		}
16410 
16411 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
16412 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
16413 		    (void *)ipif, (void *)ill,
16414 		    ill->ill_net_type,
16415 		    ntohl(ipif->ipif_subnet)));
16416 		*irep++ = ire_create(
16417 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
16418 		    (uchar_t *)&route_mask,		/* mask */
16419 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
16420 		    NULL,				/* no gateway */
16421 		    &ipif->ipif_mtu,			/* max frag */
16422 		    NULL,
16423 		    NULL,				/* no recv queue */
16424 		    stq,				/* send-to queue */
16425 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
16426 		    ipif,
16427 		    0,
16428 		    0,
16429 		    0,
16430 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
16431 		    &ire_uinfo_null,
16432 		    NULL,
16433 		    NULL,
16434 		    ipst);
16435 	}
16436 
16437 	/*
16438 	 * Create any necessary broadcast IREs.
16439 	 */
16440 	if (ipif->ipif_flags & IPIF_BROADCAST)
16441 		irep = ipif_create_bcast_ires(ipif, irep);
16442 
16443 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
16444 
16445 	/* If an earlier ire_create failed, get out now */
16446 	for (irep1 = irep; irep1 > ire_array; ) {
16447 		irep1--;
16448 		if (*irep1 == NULL) {
16449 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
16450 			err = ENOMEM;
16451 			goto bad;
16452 		}
16453 	}
16454 
16455 	/*
16456 	 * Need to atomically check for IP address availability under
16457 	 * ip_addr_avail_lock.  ill_g_lock is held as reader to ensure no new
16458 	 * ills or new ipifs can be added while we are checking availability.
16459 	 */
16460 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16461 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
16462 	/* Mark it up, and increment counters. */
16463 	ipif->ipif_flags |= IPIF_UP;
16464 	ill->ill_ipif_up_count++;
16465 	err = ip_addr_availability_check(ipif);
16466 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
16467 	rw_exit(&ipst->ips_ill_g_lock);
16468 
16469 	if (err != 0) {
16470 		/*
16471 		 * Our address may already be up on the same ill. In this case,
16472 		 * the ARP entry for our ipif replaced the one for the other
16473 		 * ipif. So we don't want to delete it (otherwise the other ipif
16474 		 * would be unable to send packets).
16475 		 * ip_addr_availability_check() identifies this case for us and
16476 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
16477 		 * which is the expected error code.
16478 		 */
16479 		if (err == EADDRINUSE) {
16480 			freemsg(ipif->ipif_arp_del_mp);
16481 			ipif->ipif_arp_del_mp = NULL;
16482 			err = EADDRNOTAVAIL;
16483 		}
16484 		ill->ill_ipif_up_count--;
16485 		ipif->ipif_flags &= ~IPIF_UP;
16486 		goto bad;
16487 	}
16488 
16489 	/*
16490 	 * Add in all newly created IREs.  ire_create_bcast() has
16491 	 * already checked for duplicates of the IRE_BROADCAST type.
16492 	 */
16493 	for (irep1 = irep; irep1 > ire_array; ) {
16494 		irep1--;
16495 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
16496 		/*
16497 		 * refheld by ire_add. refele towards the end of the func
16498 		 */
16499 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
16500 	}
16501 
16502 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
16503 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
16504 	ipif_saved_irep = ipif_recover_ire(ipif);
16505 
16506 	if (!loopback) {
16507 		/*
16508 		 * If the broadcast address has been set, make sure it makes
16509 		 * sense based on the interface address.
16510 		 * Only match on ill since we are sharing broadcast addresses.
16511 		 */
16512 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
16513 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
16514 			ire_t	*ire;
16515 
16516 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
16517 			    IRE_BROADCAST, ipif, ALL_ZONES,
16518 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
16519 
16520 			if (ire == NULL) {
16521 				/*
16522 				 * If there isn't a matching broadcast IRE,
16523 				 * revert to the default for this netmask.
16524 				 */
16525 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
16526 				mutex_enter(&ipif->ipif_ill->ill_lock);
16527 				ipif_set_default(ipif);
16528 				mutex_exit(&ipif->ipif_ill->ill_lock);
16529 			} else {
16530 				ire_refrele(ire);
16531 			}
16532 		}
16533 
16534 	}
16535 
16536 	if (ill->ill_need_recover_multicast) {
16537 		/*
16538 		 * Need to recover all multicast memberships in the driver.
16539 		 * This had to be deferred until we had attached.  The same
16540 		 * code exists in ipif_up_done_v6() to recover IPv6
16541 		 * memberships.
16542 		 *
16543 		 * Note that it would be preferable to unconditionally do the
16544 		 * ill_recover_multicast() in ill_dl_up(), but we cannot do
16545 		 * that since ill_join_allmulti() depends on ill_dl_up being
16546 		 * set, and it is not set until we receive a DL_BIND_ACK after
16547 		 * having called ill_dl_up().
16548 		 */
16549 		ill_recover_multicast(ill);
16550 	}
16551 
16552 	if (ill->ill_ipif_up_count == 1) {
16553 		/*
16554 		 * Since the interface is now up, it may now be active.
16555 		 */
16556 		if (IS_UNDER_IPMP(ill))
16557 			ipmp_ill_refresh_active(ill);
16558 
16559 		/*
16560 		 * If this is an IPMP interface, we may now be able to
16561 		 * establish ARP entries.
16562 		 */
16563 		if (IS_IPMP(ill))
16564 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
16565 	}
16566 
16567 	/* Join the allhosts multicast address */
16568 	ipif_multicast_up(ipif);
16569 
16570 	/*
16571 	 * See if anybody else would benefit from our new ipif.
16572 	 */
16573 	if (!loopback &&
16574 	    !(ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
16575 		ill_update_source_selection(ill);
16576 	}
16577 
16578 	for (irep1 = irep; irep1 > ire_array; ) {
16579 		irep1--;
16580 		if (*irep1 != NULL) {
16581 			/* was held in ire_add */
16582 			ire_refrele(*irep1);
16583 		}
16584 	}
16585 
16586 	cnt = ipif_saved_ire_cnt;
16587 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
16588 		if (*irep1 != NULL) {
16589 			/* was held in ire_add */
16590 			ire_refrele(*irep1);
16591 		}
16592 	}
16593 
16594 	if (!loopback && ipif->ipif_addr_ready) {
16595 		/* Broadcast an address mask reply. */
16596 		ipif_mask_reply(ipif);
16597 	}
16598 	if (ipif_saved_irep != NULL) {
16599 		kmem_free(ipif_saved_irep,
16600 		    ipif_saved_ire_cnt * sizeof (ire_t *));
16601 	}
16602 	if (src_ipif_held)
16603 		ipif_refrele(src_ipif);
16604 
16605 	/*
16606 	 * This had to be deferred until we had bound.  Tell routing sockets and
16607 	 * others that this interface is up if it looks like the address has
16608 	 * been validated.  Otherwise, if it isn't ready yet, wait for
16609 	 * duplicate address detection to do its thing.
16610 	 */
16611 	if (ipif->ipif_addr_ready)
16612 		ipif_up_notify(ipif);
16613 	return (0);
16614 
16615 bad:
16616 	ip1dbg(("ipif_up_done: FAILED \n"));
16617 
16618 	while (irep > ire_array) {
16619 		irep--;
16620 		if (*irep != NULL)
16621 			ire_delete(*irep);
16622 	}
16623 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
16624 
16625 	if (ipif_saved_irep != NULL) {
16626 		kmem_free(ipif_saved_irep,
16627 		    ipif_saved_ire_cnt * sizeof (ire_t *));
16628 	}
16629 	if (src_ipif_held)
16630 		ipif_refrele(src_ipif);
16631 
16632 	ipif_resolver_down(ipif);
16633 	return (err);
16634 }
16635 
16636 /*
16637  * Turn off the ARP with the ILLF_NOARP flag.
16638  */
16639 static int
16640 ill_arp_off(ill_t *ill)
16641 {
16642 	mblk_t	*arp_off_mp = NULL;
16643 	mblk_t	*arp_on_mp = NULL;
16644 
16645 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
16646 
16647 	ASSERT(IAM_WRITER_ILL(ill));
16648 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
16649 
16650 	/*
16651 	 * If the on message is still around we've already done
16652 	 * an arp_off without doing an arp_on thus there is no
16653 	 * work needed.
16654 	 */
16655 	if (ill->ill_arp_on_mp != NULL)
16656 		return (0);
16657 
16658 	/*
16659 	 * Allocate an ARP on message (to be saved) and an ARP off message
16660 	 */
16661 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
16662 	if (!arp_off_mp)
16663 		return (ENOMEM);
16664 
16665 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
16666 	if (!arp_on_mp)
16667 		goto failed;
16668 
16669 	ASSERT(ill->ill_arp_on_mp == NULL);
16670 	ill->ill_arp_on_mp = arp_on_mp;
16671 
16672 	/* Send an AR_INTERFACE_OFF request */
16673 	putnext(ill->ill_rq, arp_off_mp);
16674 	return (0);
16675 failed:
16676 
16677 	if (arp_off_mp)
16678 		freemsg(arp_off_mp);
16679 	return (ENOMEM);
16680 }
16681 
16682 /*
16683  * Turn on ARP by turning off the ILLF_NOARP flag.
16684  */
16685 static int
16686 ill_arp_on(ill_t *ill)
16687 {
16688 	mblk_t	*mp;
16689 
16690 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
16691 
16692 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
16693 
16694 	ASSERT(IAM_WRITER_ILL(ill));
16695 	/*
16696 	 * Send an AR_INTERFACE_ON request if we have already done
16697 	 * an arp_off (which allocated the message).
16698 	 */
16699 	if (ill->ill_arp_on_mp != NULL) {
16700 		mp = ill->ill_arp_on_mp;
16701 		ill->ill_arp_on_mp = NULL;
16702 		putnext(ill->ill_rq, mp);
16703 	}
16704 	return (0);
16705 }
16706 
16707 /*
16708  * Checks for availbility of a usable source address (if there is one) when the
16709  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
16710  * this selection is done regardless of the destination.
16711  */
16712 boolean_t
16713 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
16714 {
16715 	uint_t	ifindex;
16716 	ipif_t	*ipif = NULL;
16717 	ill_t	*uill;
16718 	boolean_t isv6;
16719 	ip_stack_t	*ipst = ill->ill_ipst;
16720 
16721 	ASSERT(ill != NULL);
16722 
16723 	isv6 = ill->ill_isv6;
16724 	ifindex = ill->ill_usesrc_ifindex;
16725 	if (ifindex != 0) {
16726 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
16727 		    NULL, ipst);
16728 		if (uill == NULL)
16729 			return (NULL);
16730 		mutex_enter(&uill->ill_lock);
16731 		for (ipif = uill->ill_ipif; ipif != NULL;
16732 		    ipif = ipif->ipif_next) {
16733 			if (!IPIF_CAN_LOOKUP(ipif))
16734 				continue;
16735 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
16736 				continue;
16737 			if (!(ipif->ipif_flags & IPIF_UP))
16738 				continue;
16739 			if (ipif->ipif_zoneid != zoneid)
16740 				continue;
16741 			if ((isv6 &&
16742 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
16743 			    (ipif->ipif_lcl_addr == INADDR_ANY))
16744 				continue;
16745 			mutex_exit(&uill->ill_lock);
16746 			ill_refrele(uill);
16747 			return (B_TRUE);
16748 		}
16749 		mutex_exit(&uill->ill_lock);
16750 		ill_refrele(uill);
16751 	}
16752 	return (B_FALSE);
16753 }
16754 
16755 /*
16756  * IP source address type, sorted from worst to best.  For a given type,
16757  * always prefer IP addresses on the same subnet.  All-zones addresses are
16758  * suboptimal because they pose problems with unlabeled destinations.
16759  */
16760 typedef enum {
16761 	IPIF_NONE,
16762 	IPIF_DIFFNET_DEPRECATED, 	/* deprecated and different subnet */
16763 	IPIF_SAMENET_DEPRECATED, 	/* deprecated and same subnet */
16764 	IPIF_DIFFNET_ALLZONES,		/* allzones and different subnet */
16765 	IPIF_SAMENET_ALLZONES,		/* allzones and same subnet */
16766 	IPIF_DIFFNET,			/* normal and different subnet */
16767 	IPIF_SAMENET			/* normal and same subnet */
16768 } ipif_type_t;
16769 
16770 /*
16771  * Pick the optimal ipif on `ill' for sending to destination `dst' from zone
16772  * `zoneid'.  We rate usable ipifs from low -> high as per the ipif_type_t
16773  * enumeration, and return the highest-rated ipif.  If there's a tie, we pick
16774  * the first one, unless IPMP is used in which case we round-robin among them;
16775  * see below for more.
16776  *
16777  * Returns NULL if there is no suitable source address for the ill.
16778  * This only occurs when there is no valid source address for the ill.
16779  */
16780 ipif_t *
16781 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
16782 {
16783 	ill_t	*usill = NULL;
16784 	ill_t	*ipmp_ill = NULL;
16785 	ipif_t	*start_ipif, *next_ipif, *ipif, *best_ipif;
16786 	ipif_type_t type, best_type;
16787 	tsol_tpc_t *src_rhtp, *dst_rhtp;
16788 	ip_stack_t *ipst = ill->ill_ipst;
16789 	boolean_t samenet;
16790 
16791 	if (ill->ill_usesrc_ifindex != 0) {
16792 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
16793 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
16794 		if (usill != NULL)
16795 			ill = usill;	/* Select source from usesrc ILL */
16796 		else
16797 			return (NULL);
16798 	}
16799 
16800 	/*
16801 	 * Test addresses should never be used for source address selection,
16802 	 * so if we were passed one, switch to the IPMP meta-interface.
16803 	 */
16804 	if (IS_UNDER_IPMP(ill)) {
16805 		if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL)
16806 			ill = ipmp_ill;	/* Select source from IPMP ill */
16807 		else
16808 			return (NULL);
16809 	}
16810 
16811 	/*
16812 	 * If we're dealing with an unlabeled destination on a labeled system,
16813 	 * make sure that we ignore source addresses that are incompatible with
16814 	 * the destination's default label.  That destination's default label
16815 	 * must dominate the minimum label on the source address.
16816 	 */
16817 	dst_rhtp = NULL;
16818 	if (is_system_labeled()) {
16819 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
16820 		if (dst_rhtp == NULL)
16821 			return (NULL);
16822 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
16823 			TPC_RELE(dst_rhtp);
16824 			dst_rhtp = NULL;
16825 		}
16826 	}
16827 
16828 	/*
16829 	 * Hold the ill_g_lock as reader. This makes sure that no ipif/ill
16830 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
16831 	 * After selecting the right ipif, under ill_lock make sure ipif is
16832 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
16833 	 * we retry. Inside the loop we still need to check for CONDEMNED,
16834 	 * but not under a lock.
16835 	 */
16836 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16837 retry:
16838 	/*
16839 	 * For source address selection, we treat the ipif list as circular
16840 	 * and continue until we get back to where we started.  This allows
16841 	 * IPMP to vary source address selection (which improves inbound load
16842 	 * spreading) by caching its last ending point and starting from
16843 	 * there.  NOTE: we don't have to worry about ill_src_ipif changing
16844 	 * ills since that can't happen on the IPMP ill.
16845 	 */
16846 	start_ipif = ill->ill_ipif;
16847 	if (IS_IPMP(ill) && ill->ill_src_ipif != NULL)
16848 		start_ipif = ill->ill_src_ipif;
16849 
16850 	ipif = start_ipif;
16851 	best_ipif = NULL;
16852 	best_type = IPIF_NONE;
16853 	do {
16854 		if ((next_ipif = ipif->ipif_next) == NULL)
16855 			next_ipif = ill->ill_ipif;
16856 
16857 		if (!IPIF_CAN_LOOKUP(ipif))
16858 			continue;
16859 		/* Always skip NOLOCAL and ANYCAST interfaces */
16860 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
16861 			continue;
16862 		if (!(ipif->ipif_flags & IPIF_UP) || !ipif->ipif_addr_ready)
16863 			continue;
16864 		if (ipif->ipif_zoneid != zoneid &&
16865 		    ipif->ipif_zoneid != ALL_ZONES)
16866 			continue;
16867 
16868 		/*
16869 		 * Interfaces with 0.0.0.0 address are allowed to be UP, but
16870 		 * are not valid as source addresses.
16871 		 */
16872 		if (ipif->ipif_lcl_addr == INADDR_ANY)
16873 			continue;
16874 
16875 		/*
16876 		 * Check compatibility of local address for destination's
16877 		 * default label if we're on a labeled system.	Incompatible
16878 		 * addresses can't be used at all.
16879 		 */
16880 		if (dst_rhtp != NULL) {
16881 			boolean_t incompat;
16882 
16883 			src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
16884 			    IPV4_VERSION, B_FALSE);
16885 			if (src_rhtp == NULL)
16886 				continue;
16887 			incompat = src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
16888 			    src_rhtp->tpc_tp.tp_doi !=
16889 			    dst_rhtp->tpc_tp.tp_doi ||
16890 			    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
16891 			    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
16892 			    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
16893 			    src_rhtp->tpc_tp.tp_sl_set_cipso));
16894 			TPC_RELE(src_rhtp);
16895 			if (incompat)
16896 				continue;
16897 		}
16898 
16899 		samenet = ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet);
16900 
16901 		if (ipif->ipif_flags & IPIF_DEPRECATED) {
16902 			type = samenet ? IPIF_SAMENET_DEPRECATED :
16903 			    IPIF_DIFFNET_DEPRECATED;
16904 		} else if (ipif->ipif_zoneid == ALL_ZONES) {
16905 			type = samenet ? IPIF_SAMENET_ALLZONES :
16906 			    IPIF_DIFFNET_ALLZONES;
16907 		} else {
16908 			type = samenet ? IPIF_SAMENET : IPIF_DIFFNET;
16909 		}
16910 
16911 		if (type > best_type) {
16912 			best_type = type;
16913 			best_ipif = ipif;
16914 			if (best_type == IPIF_SAMENET)
16915 				break; /* can't get better */
16916 		}
16917 	} while ((ipif = next_ipif) != start_ipif);
16918 
16919 	if ((ipif = best_ipif) != NULL) {
16920 		mutex_enter(&ipif->ipif_ill->ill_lock);
16921 		if (!IPIF_CAN_LOOKUP(ipif)) {
16922 			mutex_exit(&ipif->ipif_ill->ill_lock);
16923 			goto retry;
16924 		}
16925 		ipif_refhold_locked(ipif);
16926 
16927 		/*
16928 		 * For IPMP, update the source ipif rotor to the next ipif,
16929 		 * provided we can look it up.  (We must not use it if it's
16930 		 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after
16931 		 * ipif_free() checked ill_src_ipif.)
16932 		 */
16933 		if (IS_IPMP(ill) && ipif != NULL) {
16934 			next_ipif = ipif->ipif_next;
16935 			if (next_ipif != NULL && IPIF_CAN_LOOKUP(next_ipif))
16936 				ill->ill_src_ipif = next_ipif;
16937 			else
16938 				ill->ill_src_ipif = NULL;
16939 		}
16940 		mutex_exit(&ipif->ipif_ill->ill_lock);
16941 	}
16942 
16943 	rw_exit(&ipst->ips_ill_g_lock);
16944 	if (usill != NULL)
16945 		ill_refrele(usill);
16946 	if (ipmp_ill != NULL)
16947 		ill_refrele(ipmp_ill);
16948 	if (dst_rhtp != NULL)
16949 		TPC_RELE(dst_rhtp);
16950 
16951 #ifdef DEBUG
16952 	if (ipif == NULL) {
16953 		char buf1[INET6_ADDRSTRLEN];
16954 
16955 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
16956 		    ill->ill_name,
16957 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
16958 	} else {
16959 		char buf1[INET6_ADDRSTRLEN];
16960 		char buf2[INET6_ADDRSTRLEN];
16961 
16962 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
16963 		    ipif->ipif_ill->ill_name,
16964 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
16965 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
16966 		    buf2, sizeof (buf2))));
16967 	}
16968 #endif /* DEBUG */
16969 	return (ipif);
16970 }
16971 
16972 /*
16973  * If old_ipif is not NULL, see if ipif was derived from old
16974  * ipif and if so, recreate the interface route by re-doing
16975  * source address selection. This happens when ipif_down ->
16976  * ipif_update_other_ipifs calls us.
16977  *
16978  * If old_ipif is NULL, just redo the source address selection
16979  * if needed. This happens when ipif_up_done calls us.
16980  */
16981 static void
16982 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
16983 {
16984 	ire_t *ire;
16985 	ire_t *ipif_ire;
16986 	queue_t *stq;
16987 	ipif_t *nipif;
16988 	ill_t *ill;
16989 	boolean_t need_rele = B_FALSE;
16990 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
16991 
16992 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
16993 	ASSERT(IAM_WRITER_IPIF(ipif));
16994 
16995 	ill = ipif->ipif_ill;
16996 	if (!(ipif->ipif_flags &
16997 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
16998 		/*
16999 		 * Can't possibly have borrowed the source
17000 		 * from old_ipif.
17001 		 */
17002 		return;
17003 	}
17004 
17005 	/*
17006 	 * Is there any work to be done? No work if the address
17007 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
17008 	 * ipif_select_source() does not borrow addresses from
17009 	 * NOLOCAL and ANYCAST interfaces).
17010 	 */
17011 	if ((old_ipif != NULL) &&
17012 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
17013 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
17014 	    (old_ipif->ipif_flags &
17015 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
17016 		return;
17017 	}
17018 
17019 	/*
17020 	 * Perform the same checks as when creating the
17021 	 * IRE_INTERFACE in ipif_up_done.
17022 	 */
17023 	if (!(ipif->ipif_flags & IPIF_UP))
17024 		return;
17025 
17026 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
17027 	    (ipif->ipif_subnet == INADDR_ANY))
17028 		return;
17029 
17030 	ipif_ire = ipif_to_ire(ipif);
17031 	if (ipif_ire == NULL)
17032 		return;
17033 
17034 	/*
17035 	 * We know that ipif uses some other source for its
17036 	 * IRE_INTERFACE. Is it using the source of this
17037 	 * old_ipif?
17038 	 */
17039 	if (old_ipif != NULL &&
17040 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
17041 		ire_refrele(ipif_ire);
17042 		return;
17043 	}
17044 	if (ip_debug > 2) {
17045 		/* ip1dbg */
17046 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
17047 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
17048 	}
17049 
17050 	stq = ipif_ire->ire_stq;
17051 
17052 	/*
17053 	 * Can't use our source address. Select a different
17054 	 * source address for the IRE_INTERFACE.
17055 	 */
17056 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
17057 	if (nipif == NULL) {
17058 		/* Last resort - all ipif's have IPIF_NOLOCAL */
17059 		nipif = ipif;
17060 	} else {
17061 		need_rele = B_TRUE;
17062 	}
17063 
17064 	ire = ire_create(
17065 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
17066 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
17067 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
17068 	    NULL,				/* no gateway */
17069 	    &ipif->ipif_mtu,			/* max frag */
17070 	    NULL,				/* no src nce */
17071 	    NULL,				/* no recv from queue */
17072 	    stq,				/* send-to queue */
17073 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
17074 	    ipif,
17075 	    0,
17076 	    0,
17077 	    0,
17078 	    0,
17079 	    &ire_uinfo_null,
17080 	    NULL,
17081 	    NULL,
17082 	    ipst);
17083 
17084 	if (ire != NULL) {
17085 		ire_t *ret_ire;
17086 		int error;
17087 
17088 		/*
17089 		 * We don't need ipif_ire anymore. We need to delete
17090 		 * before we add so that ire_add does not detect
17091 		 * duplicates.
17092 		 */
17093 		ire_delete(ipif_ire);
17094 		ret_ire = ire;
17095 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
17096 		ASSERT(error == 0);
17097 		ASSERT(ire == ret_ire);
17098 		/* Held in ire_add */
17099 		ire_refrele(ret_ire);
17100 	}
17101 	/*
17102 	 * Either we are falling through from above or could not
17103 	 * allocate a replacement.
17104 	 */
17105 	ire_refrele(ipif_ire);
17106 	if (need_rele)
17107 		ipif_refrele(nipif);
17108 }
17109 
17110 /*
17111  * This old_ipif is going away.
17112  *
17113  * Determine if any other ipif's are using our address as
17114  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
17115  * IPIF_DEPRECATED).
17116  * Find the IRE_INTERFACE for such ipifs and recreate them
17117  * to use an different source address following the rules in
17118  * ipif_up_done.
17119  */
17120 static void
17121 ipif_update_other_ipifs(ipif_t *old_ipif)
17122 {
17123 	ipif_t	*ipif;
17124 	ill_t	*ill;
17125 	char	buf[INET6_ADDRSTRLEN];
17126 
17127 	ASSERT(IAM_WRITER_IPIF(old_ipif));
17128 
17129 	ill = old_ipif->ipif_ill;
17130 
17131 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n", ill->ill_name,
17132 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr, buf, sizeof (buf))));
17133 
17134 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17135 		if (ipif == old_ipif)
17136 			continue;
17137 		ipif_recreate_interface_routes(old_ipif, ipif);
17138 	}
17139 }
17140 
17141 /* ARGSUSED */
17142 int
17143 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
17144 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
17145 {
17146 	/*
17147 	 * ill_phyint_reinit merged the v4 and v6 into a single
17148 	 * ipsq.  We might not have been able to complete the
17149 	 * operation in ipif_set_values, if we could not become
17150 	 * exclusive.  If so restart it here.
17151 	 */
17152 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
17153 }
17154 
17155 /*
17156  * Can operate on either a module or a driver queue.
17157  * Returns an error if not a module queue.
17158  */
17159 /* ARGSUSED */
17160 int
17161 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
17162     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
17163 {
17164 	queue_t		*q1 = q;
17165 	char 		*cp;
17166 	char		interf_name[LIFNAMSIZ];
17167 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
17168 
17169 	if (q->q_next == NULL) {
17170 		ip1dbg((
17171 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
17172 		return (EINVAL);
17173 	}
17174 
17175 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
17176 		return (EALREADY);
17177 
17178 	do {
17179 		q1 = q1->q_next;
17180 	} while (q1->q_next);
17181 	cp = q1->q_qinfo->qi_minfo->mi_idname;
17182 	(void) sprintf(interf_name, "%s%d", cp, ppa);
17183 
17184 	/*
17185 	 * Here we are not going to delay the ioack until after
17186 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
17187 	 * original ioctl message before sending the requests.
17188 	 */
17189 	return (ipif_set_values(q, mp, interf_name, &ppa));
17190 }
17191 
17192 /* ARGSUSED */
17193 int
17194 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
17195     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
17196 {
17197 	return (ENXIO);
17198 }
17199 
17200 /*
17201  * Refresh all IRE_BROADCAST entries associated with `ill' to ensure the
17202  * minimum (but complete) set exist.  This is necessary when adding or
17203  * removing an interface to/from an IPMP group, since interfaces in an
17204  * IPMP group use the IRE_BROADCAST entries for the IPMP group (whenever
17205  * its test address subnets overlap with IPMP data addresses).	It's also
17206  * used to refresh the IRE_BROADCAST entries associated with the IPMP
17207  * interface when the nominated broadcast interface changes.
17208  */
17209 void
17210 ill_refresh_bcast(ill_t *ill)
17211 {
17212 	ire_t *ire_array[12];	/* max ipif_create_bcast_ires() can create */
17213 	ire_t **irep;
17214 	ipif_t *ipif;
17215 
17216 	ASSERT(!ill->ill_isv6);
17217 	ASSERT(IAM_WRITER_ILL(ill));
17218 
17219 	/*
17220 	 * Remove any old broadcast IREs.
17221 	 */
17222 	ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE, IRE_BROADCAST,
17223 	    ill_broadcast_delete, ill, ill);
17224 
17225 	/*
17226 	 * Create new ones for any ipifs that are up and broadcast-capable.
17227 	 */
17228 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17229 		if ((ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST)) !=
17230 		    (IPIF_UP|IPIF_BROADCAST))
17231 			continue;
17232 
17233 		irep = ipif_create_bcast_ires(ipif, ire_array);
17234 		while (irep-- > ire_array) {
17235 			(void) ire_add(irep, NULL, NULL, NULL, B_FALSE);
17236 			if (*irep != NULL)
17237 				ire_refrele(*irep);
17238 		}
17239 	}
17240 }
17241 
17242 /*
17243  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
17244  * `irep'.  Returns a pointer to the next free `irep' entry (just like
17245  * ire_check_and_create_bcast()).
17246  */
17247 static ire_t **
17248 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
17249 {
17250 	ipaddr_t addr;
17251 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
17252 	ipaddr_t subnetmask = ipif->ipif_net_mask;
17253 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
17254 
17255 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
17256 
17257 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
17258 
17259 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
17260 	    (ipif->ipif_flags & IPIF_NOLOCAL))
17261 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
17262 
17263 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
17264 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
17265 
17266 	/*
17267 	 * For backward compatibility, we create net broadcast IREs based on
17268 	 * the old "IP address class system", since some old machines only
17269 	 * respond to these class derived net broadcast.  However, we must not
17270 	 * create these net broadcast IREs if the subnetmask is shorter than
17271 	 * the IP address class based derived netmask.  Otherwise, we may
17272 	 * create a net broadcast address which is the same as an IP address
17273 	 * on the subnet -- and then TCP will refuse to talk to that address.
17274 	 */
17275 	if (netmask < subnetmask) {
17276 		addr = netmask & ipif->ipif_subnet;
17277 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
17278 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
17279 		    flags);
17280 	}
17281 
17282 	/*
17283 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
17284 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
17285 	 * created.  Creating these broadcast IREs will only create confusion
17286 	 * as `addr' will be the same as the IP address.
17287 	 */
17288 	if (subnetmask != 0xFFFFFFFF) {
17289 		addr = ipif->ipif_subnet;
17290 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
17291 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
17292 		    irep, flags);
17293 	}
17294 
17295 	return (irep);
17296 }
17297 
17298 /*
17299  * Broadcast IRE info structure used in the functions below.  Since we
17300  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
17301  */
17302 typedef struct bcast_ireinfo {
17303 	uchar_t		bi_type;	/* BCAST_* value from below */
17304 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
17305 			bi_needrep:1,	/* do we need to replace it? */
17306 			bi_haverep:1,	/* have we replaced it? */
17307 			bi_pad:5;
17308 	ipaddr_t	bi_addr;	/* IRE address */
17309 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
17310 } bcast_ireinfo_t;
17311 
17312 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
17313 
17314 /*
17315  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
17316  * return B_TRUE if it should immediately be used to recreate the IRE.
17317  */
17318 static boolean_t
17319 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
17320 {
17321 	ipaddr_t addr;
17322 
17323 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
17324 
17325 	switch (bireinfop->bi_type) {
17326 	case BCAST_NET:
17327 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
17328 		if (addr != bireinfop->bi_addr)
17329 			return (B_FALSE);
17330 		break;
17331 	case BCAST_SUBNET:
17332 		if (ipif->ipif_subnet != bireinfop->bi_addr)
17333 			return (B_FALSE);
17334 		break;
17335 	}
17336 
17337 	bireinfop->bi_needrep = 1;
17338 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
17339 		if (bireinfop->bi_backup == NULL)
17340 			bireinfop->bi_backup = ipif;
17341 		return (B_FALSE);
17342 	}
17343 	return (B_TRUE);
17344 }
17345 
17346 /*
17347  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
17348  * them ala ire_check_and_create_bcast().
17349  */
17350 static ire_t **
17351 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
17352 {
17353 	ipaddr_t mask, addr;
17354 
17355 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
17356 
17357 	addr = bireinfop->bi_addr;
17358 	irep = ire_create_bcast(ipif, addr, irep);
17359 
17360 	switch (bireinfop->bi_type) {
17361 	case BCAST_NET:
17362 		mask = ip_net_mask(ipif->ipif_subnet);
17363 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
17364 		break;
17365 	case BCAST_SUBNET:
17366 		mask = ipif->ipif_net_mask;
17367 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
17368 		break;
17369 	}
17370 
17371 	bireinfop->bi_haverep = 1;
17372 	return (irep);
17373 }
17374 
17375 /*
17376  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
17377  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
17378  * that are going away are still needed.  If so, have ipif_create_bcast()
17379  * recreate them (except for the deprecated case, as explained below).
17380  */
17381 static ire_t **
17382 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
17383     ire_t **irep)
17384 {
17385 	int i;
17386 	ipif_t *ipif;
17387 
17388 	ASSERT(!ill->ill_isv6);
17389 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17390 		/*
17391 		 * Skip this ipif if it's (a) the one being taken down, (b)
17392 		 * not in the same zone, or (c) has no valid local address.
17393 		 */
17394 		if (ipif == test_ipif ||
17395 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
17396 		    ipif->ipif_subnet == 0 ||
17397 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
17398 		    (IPIF_UP|IPIF_BROADCAST))
17399 			continue;
17400 
17401 		/*
17402 		 * For each dying IRE that hasn't yet been replaced, see if
17403 		 * `ipif' needs it and whether the IRE should be recreated on
17404 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
17405 		 * will return B_FALSE even if `ipif' needs the IRE on the
17406 		 * hopes that we'll later find a needy non-deprecated ipif.
17407 		 * However, the ipif is recorded in bi_backup for possible
17408 		 * subsequent use by ipif_check_bcast_ires().
17409 		 */
17410 		for (i = 0; i < BCAST_COUNT; i++) {
17411 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
17412 				continue;
17413 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
17414 				continue;
17415 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
17416 		}
17417 
17418 		/*
17419 		 * If we've replaced all of the broadcast IREs that are going
17420 		 * to be taken down, we know we're done.
17421 		 */
17422 		for (i = 0; i < BCAST_COUNT; i++) {
17423 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
17424 				break;
17425 		}
17426 		if (i == BCAST_COUNT)
17427 			break;
17428 	}
17429 	return (irep);
17430 }
17431 
17432 /*
17433  * Check if `test_ipif' (which is going away) is associated with any existing
17434  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
17435  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
17436  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
17437  *
17438  * This is necessary because broadcast IREs are shared.  In particular, a
17439  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
17440  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
17441  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
17442  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
17443  * same zone, they will share the same set of broadcast IREs.
17444  *
17445  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
17446  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
17447  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
17448  */
17449 static void
17450 ipif_check_bcast_ires(ipif_t *test_ipif)
17451 {
17452 	ill_t		*ill = test_ipif->ipif_ill;
17453 	ire_t		*ire, *ire_array[12]; 		/* see note above */
17454 	ire_t		**irep1, **irep = &ire_array[0];
17455 	uint_t 		i, willdie;
17456 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
17457 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
17458 
17459 	ASSERT(!test_ipif->ipif_isv6);
17460 	ASSERT(IAM_WRITER_IPIF(test_ipif));
17461 
17462 	/*
17463 	 * No broadcast IREs for the LOOPBACK interface
17464 	 * or others such as point to point and IPIF_NOXMIT.
17465 	 */
17466 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
17467 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
17468 		return;
17469 
17470 	bzero(bireinfo, sizeof (bireinfo));
17471 	bireinfo[0].bi_type = BCAST_ALLZEROES;
17472 	bireinfo[0].bi_addr = 0;
17473 
17474 	bireinfo[1].bi_type = BCAST_ALLONES;
17475 	bireinfo[1].bi_addr = INADDR_BROADCAST;
17476 
17477 	bireinfo[2].bi_type = BCAST_NET;
17478 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
17479 
17480 	if (test_ipif->ipif_net_mask != 0)
17481 		mask = test_ipif->ipif_net_mask;
17482 	bireinfo[3].bi_type = BCAST_SUBNET;
17483 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
17484 
17485 	/*
17486 	 * Figure out what (if any) broadcast IREs will die as a result of
17487 	 * `test_ipif' going away.  If none will die, we're done.
17488 	 */
17489 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
17490 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
17491 		    test_ipif, ALL_ZONES, NULL,
17492 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
17493 		if (ire != NULL) {
17494 			willdie++;
17495 			bireinfo[i].bi_willdie = 1;
17496 			ire_refrele(ire);
17497 		}
17498 	}
17499 
17500 	if (willdie == 0)
17501 		return;
17502 
17503 	/*
17504 	 * Walk through all the ipifs that will be affected by the dying IREs,
17505 	 * and recreate the IREs as necessary. Note that all interfaces in an
17506 	 * IPMP illgrp share the same broadcast IREs, and thus the entire
17507 	 * illgrp must be walked, starting with the IPMP meta-interface (so
17508 	 * that broadcast IREs end up on it whenever possible).
17509 	 */
17510 	if (IS_UNDER_IPMP(ill))
17511 		ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
17512 
17513 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
17514 
17515 	if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
17516 		ipmp_illgrp_t *illg = ill->ill_grp;
17517 
17518 		ill = list_head(&illg->ig_if);
17519 		for (; ill != NULL; ill = list_next(&illg->ig_if, ill)) {
17520 			for (i = 0; i < BCAST_COUNT; i++) {
17521 				if (bireinfo[i].bi_willdie &&
17522 				    !bireinfo[i].bi_haverep)
17523 					break;
17524 			}
17525 			if (i == BCAST_COUNT)
17526 				break;
17527 
17528 			irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
17529 		}
17530 	}
17531 
17532 	/*
17533 	 * Scan through the set of broadcast IREs and see if there are any
17534 	 * that we need to replace that have not yet been replaced.  If so,
17535 	 * replace them using the appropriate backup ipif.
17536 	 */
17537 	for (i = 0; i < BCAST_COUNT; i++) {
17538 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
17539 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
17540 			    &bireinfo[i], irep);
17541 	}
17542 
17543 	/*
17544 	 * If we can't create all of them, don't add any of them.  (Code in
17545 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
17546 	 * non-loopback copy and loopback copy for a given address.)
17547 	 */
17548 	for (irep1 = irep; irep1 > ire_array; ) {
17549 		irep1--;
17550 		if (*irep1 == NULL) {
17551 			ip0dbg(("ipif_check_bcast_ires: can't create "
17552 			    "IRE_BROADCAST, memory allocation failure\n"));
17553 			while (irep > ire_array) {
17554 				irep--;
17555 				if (*irep != NULL)
17556 					ire_delete(*irep);
17557 			}
17558 			return;
17559 		}
17560 	}
17561 
17562 	for (irep1 = irep; irep1 > ire_array; ) {
17563 		irep1--;
17564 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
17565 			ire_refrele(*irep1);		/* Held in ire_add */
17566 	}
17567 }
17568 
17569 /*
17570  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
17571  * from lifr_flags and the name from lifr_name.
17572  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
17573  * since ipif_lookup_on_name uses the _isv6 flags when matching.
17574  * Returns EINPROGRESS when mp has been consumed by queueing it on
17575  * ill_pending_mp and the ioctl will complete in ip_rput.
17576  *
17577  * Can operate on either a module or a driver queue.
17578  * Returns an error if not a module queue.
17579  */
17580 /* ARGSUSED */
17581 int
17582 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17583     ip_ioctl_cmd_t *ipip, void *if_req)
17584 {
17585 	ill_t	*ill = q->q_ptr;
17586 	phyint_t *phyi;
17587 	ip_stack_t *ipst;
17588 	struct lifreq *lifr = if_req;
17589 
17590 	ASSERT(ipif != NULL);
17591 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
17592 
17593 	if (q->q_next == NULL) {
17594 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
17595 		return (EINVAL);
17596 	}
17597 
17598 	/*
17599 	 * If we are not writer on 'q' then this interface exists already
17600 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
17601 	 * so return EALREADY.
17602 	 */
17603 	if (ill != ipif->ipif_ill)
17604 		return (EALREADY);
17605 
17606 	if (ill->ill_name[0] != '\0')
17607 		return (EALREADY);
17608 
17609 	/*
17610 	 * Set all the flags. Allows all kinds of override. Provide some
17611 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
17612 	 * unless there is either multicast/broadcast support in the driver
17613 	 * or it is a pt-pt link.
17614 	 */
17615 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
17616 		/* Meaningless to IP thus don't allow them to be set. */
17617 		ip1dbg(("ip_setname: EINVAL 1\n"));
17618 		return (EINVAL);
17619 	}
17620 
17621 	/*
17622 	 * If there's another ill already with the requested name, ensure
17623 	 * that it's of the same type.  Otherwise, ill_phyint_reinit() will
17624 	 * fuse together two unrelated ills, which will cause chaos.
17625 	 */
17626 	ipst = ill->ill_ipst;
17627 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
17628 	    lifr->lifr_name, NULL);
17629 	if (phyi != NULL) {
17630 		ill_t *ill_mate = phyi->phyint_illv4;
17631 
17632 		if (ill_mate == NULL)
17633 			ill_mate = phyi->phyint_illv6;
17634 		ASSERT(ill_mate != NULL);
17635 
17636 		if (ill_mate->ill_media->ip_m_mac_type !=
17637 		    ill->ill_media->ip_m_mac_type) {
17638 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
17639 			    "use the same ill name on differing media\n"));
17640 			return (EINVAL);
17641 		}
17642 	}
17643 
17644 	/*
17645 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
17646 	 * ill_bcast_addr_length info.
17647 	 */
17648 	if (!ill->ill_needs_attach &&
17649 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
17650 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
17651 	    ill->ill_bcast_addr_length == 0)) {
17652 		/* Link not broadcast/pt-pt capable i.e. no multicast */
17653 		ip1dbg(("ip_setname: EINVAL 2\n"));
17654 		return (EINVAL);
17655 	}
17656 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
17657 	    ((lifr->lifr_flags & IFF_IPV6) ||
17658 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
17659 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
17660 		ip1dbg(("ip_setname: EINVAL 3\n"));
17661 		return (EINVAL);
17662 	}
17663 	if (lifr->lifr_flags & IFF_UP) {
17664 		/* Can only be set with SIOCSLIFFLAGS */
17665 		ip1dbg(("ip_setname: EINVAL 4\n"));
17666 		return (EINVAL);
17667 	}
17668 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
17669 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
17670 		ip1dbg(("ip_setname: EINVAL 5\n"));
17671 		return (EINVAL);
17672 	}
17673 	/*
17674 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
17675 	 */
17676 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
17677 	    !(lifr->lifr_flags & IFF_IPV6) &&
17678 	    !(ipif->ipif_isv6)) {
17679 		ip1dbg(("ip_setname: EINVAL 6\n"));
17680 		return (EINVAL);
17681 	}
17682 
17683 	/*
17684 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
17685 	 * we have all the flags here. So, we assign rather than we OR.
17686 	 * We can't OR the flags here because we don't want to set
17687 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
17688 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
17689 	 * on lifr_flags value here.
17690 	 */
17691 	/*
17692 	 * This ill has not been inserted into the global list.
17693 	 * So we are still single threaded and don't need any lock
17694 	 */
17695 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS & ~IFF_DUPLICATE;
17696 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
17697 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
17698 
17699 	/* We started off as V4. */
17700 	if (ill->ill_flags & ILLF_IPV6) {
17701 		ill->ill_phyint->phyint_illv6 = ill;
17702 		ill->ill_phyint->phyint_illv4 = NULL;
17703 	}
17704 
17705 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
17706 }
17707 
17708 /* ARGSUSED */
17709 int
17710 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17711     ip_ioctl_cmd_t *ipip, void *if_req)
17712 {
17713 	/*
17714 	 * ill_phyint_reinit merged the v4 and v6 into a single
17715 	 * ipsq.  We might not have been able to complete the
17716 	 * slifname in ipif_set_values, if we could not become
17717 	 * exclusive.  If so restart it here
17718 	 */
17719 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
17720 }
17721 
17722 /*
17723  * Return a pointer to the ipif which matches the index, IP version type and
17724  * zoneid.
17725  */
17726 ipif_t *
17727 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
17728     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
17729 {
17730 	ill_t	*ill;
17731 	ipif_t	*ipif = NULL;
17732 
17733 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
17734 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
17735 
17736 	if (err != NULL)
17737 		*err = 0;
17738 
17739 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
17740 	if (ill != NULL) {
17741 		mutex_enter(&ill->ill_lock);
17742 		for (ipif = ill->ill_ipif; ipif != NULL;
17743 		    ipif = ipif->ipif_next) {
17744 			if (IPIF_CAN_LOOKUP(ipif) && (zoneid == ALL_ZONES ||
17745 			    zoneid == ipif->ipif_zoneid ||
17746 			    ipif->ipif_zoneid == ALL_ZONES)) {
17747 				ipif_refhold_locked(ipif);
17748 				break;
17749 			}
17750 		}
17751 		mutex_exit(&ill->ill_lock);
17752 		ill_refrele(ill);
17753 		if (ipif == NULL && err != NULL)
17754 			*err = ENXIO;
17755 	}
17756 	return (ipif);
17757 }
17758 
17759 /*
17760  * Change an existing physical interface's index. If the new index
17761  * is acceptable we update the index and the phyint_list_avl_by_index tree.
17762  * Finally, we update other systems which may have a dependence on the
17763  * index value.
17764  */
17765 /* ARGSUSED */
17766 int
17767 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17768     ip_ioctl_cmd_t *ipip, void *ifreq)
17769 {
17770 	ill_t		*ill;
17771 	phyint_t	*phyi;
17772 	struct ifreq	*ifr = (struct ifreq *)ifreq;
17773 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17774 	uint_t	old_index, index;
17775 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
17776 	avl_index_t	where;
17777 
17778 	if (ipip->ipi_cmd_type == IF_CMD)
17779 		index = ifr->ifr_index;
17780 	else
17781 		index = lifr->lifr_index;
17782 
17783 	/*
17784 	 * Only allow on physical interface. Also, index zero is illegal.
17785 	 */
17786 	ill = ipif->ipif_ill;
17787 	phyi = ill->ill_phyint;
17788 	if (ipif->ipif_id != 0 || index == 0) {
17789 		return (EINVAL);
17790 	}
17791 
17792 	/* If the index is not changing, no work to do */
17793 	if (phyi->phyint_ifindex == index)
17794 		return (0);
17795 
17796 	/*
17797 	 * Use phyint_exists() to determine if the new interface index
17798 	 * is already in use. If the index is unused then we need to
17799 	 * change the phyint's position in the phyint_list_avl_by_index
17800 	 * tree. If we do not do this, subsequent lookups (using the new
17801 	 * index value) will not find the phyint.
17802 	 */
17803 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17804 	if (phyint_exists(index, ipst)) {
17805 		rw_exit(&ipst->ips_ill_g_lock);
17806 		return (EEXIST);
17807 	}
17808 
17809 	/* The new index is unused. Set it in the phyint. */
17810 	old_index = phyi->phyint_ifindex;
17811 	phyi->phyint_ifindex = index;
17812 
17813 	avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, phyi);
17814 	(void) avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
17815 	    &index, &where);
17816 	avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
17817 	    phyi, where);
17818 	rw_exit(&ipst->ips_ill_g_lock);
17819 
17820 	/* Update SCTP's ILL list */
17821 	sctp_ill_reindex(ill, old_index);
17822 
17823 	/* Send the routing sockets message */
17824 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
17825 	if (ILL_OTHER(ill))
17826 		ip_rts_ifmsg(ILL_OTHER(ill)->ill_ipif, RTSQ_DEFAULT);
17827 
17828 	return (0);
17829 }
17830 
17831 /* ARGSUSED */
17832 int
17833 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17834     ip_ioctl_cmd_t *ipip, void *ifreq)
17835 {
17836 	struct ifreq	*ifr = (struct ifreq *)ifreq;
17837 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17838 
17839 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
17840 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17841 	/* Get the interface index */
17842 	if (ipip->ipi_cmd_type == IF_CMD) {
17843 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
17844 	} else {
17845 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
17846 	}
17847 	return (0);
17848 }
17849 
17850 /* ARGSUSED */
17851 int
17852 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17853     ip_ioctl_cmd_t *ipip, void *ifreq)
17854 {
17855 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17856 
17857 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
17858 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17859 	/* Get the interface zone */
17860 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
17861 	lifr->lifr_zoneid = ipif->ipif_zoneid;
17862 	return (0);
17863 }
17864 
17865 /*
17866  * Set the zoneid of an interface.
17867  */
17868 /* ARGSUSED */
17869 int
17870 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17871     ip_ioctl_cmd_t *ipip, void *ifreq)
17872 {
17873 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17874 	int err = 0;
17875 	boolean_t need_up = B_FALSE;
17876 	zone_t *zptr;
17877 	zone_status_t status;
17878 	zoneid_t zoneid;
17879 
17880 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
17881 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
17882 		if (!is_system_labeled())
17883 			return (ENOTSUP);
17884 		zoneid = GLOBAL_ZONEID;
17885 	}
17886 
17887 	/* cannot assign instance zero to a non-global zone */
17888 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
17889 		return (ENOTSUP);
17890 
17891 	/*
17892 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
17893 	 * the event of a race with the zone shutdown processing, since IP
17894 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
17895 	 * interface will be cleaned up even if the zone is shut down
17896 	 * immediately after the status check. If the interface can't be brought
17897 	 * down right away, and the zone is shut down before the restart
17898 	 * function is called, we resolve the possible races by rechecking the
17899 	 * zone status in the restart function.
17900 	 */
17901 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
17902 		return (EINVAL);
17903 	status = zone_status_get(zptr);
17904 	zone_rele(zptr);
17905 
17906 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
17907 		return (EINVAL);
17908 
17909 	if (ipif->ipif_flags & IPIF_UP) {
17910 		/*
17911 		 * If the interface is already marked up,
17912 		 * we call ipif_down which will take care
17913 		 * of ditching any IREs that have been set
17914 		 * up based on the old interface address.
17915 		 */
17916 		err = ipif_logical_down(ipif, q, mp);
17917 		if (err == EINPROGRESS)
17918 			return (err);
17919 		ipif_down_tail(ipif);
17920 		need_up = B_TRUE;
17921 	}
17922 
17923 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
17924 	return (err);
17925 }
17926 
17927 static int
17928 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
17929     queue_t *q, mblk_t *mp, boolean_t need_up)
17930 {
17931 	int	err = 0;
17932 	ip_stack_t	*ipst;
17933 
17934 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
17935 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17936 
17937 	if (CONN_Q(q))
17938 		ipst = CONNQ_TO_IPST(q);
17939 	else
17940 		ipst = ILLQ_TO_IPST(q);
17941 
17942 	/*
17943 	 * For exclusive stacks we don't allow a different zoneid than
17944 	 * global.
17945 	 */
17946 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
17947 	    zoneid != GLOBAL_ZONEID)
17948 		return (EINVAL);
17949 
17950 	/* Set the new zone id. */
17951 	ipif->ipif_zoneid = zoneid;
17952 
17953 	/* Update sctp list */
17954 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
17955 
17956 	if (need_up) {
17957 		/*
17958 		 * Now bring the interface back up.  If this
17959 		 * is the only IPIF for the ILL, ipif_up
17960 		 * will have to re-bind to the device, so
17961 		 * we may get back EINPROGRESS, in which
17962 		 * case, this IOCTL will get completed in
17963 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
17964 		 */
17965 		err = ipif_up(ipif, q, mp);
17966 	}
17967 	return (err);
17968 }
17969 
17970 /* ARGSUSED */
17971 int
17972 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17973     ip_ioctl_cmd_t *ipip, void *if_req)
17974 {
17975 	struct lifreq *lifr = (struct lifreq *)if_req;
17976 	zoneid_t zoneid;
17977 	zone_t *zptr;
17978 	zone_status_t status;
17979 
17980 	ASSERT(ipif->ipif_id != 0);
17981 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
17982 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
17983 		zoneid = GLOBAL_ZONEID;
17984 
17985 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
17986 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17987 
17988 	/*
17989 	 * We recheck the zone status to resolve the following race condition:
17990 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
17991 	 * 2) hme0:1 is up and can't be brought down right away;
17992 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
17993 	 * 3) zone "myzone" is halted; the zone status switches to
17994 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
17995 	 * the interfaces to remove - hme0:1 is not returned because it's not
17996 	 * yet in "myzone", so it won't be removed;
17997 	 * 4) the restart function for SIOCSLIFZONE is called; without the
17998 	 * status check here, we would have hme0:1 in "myzone" after it's been
17999 	 * destroyed.
18000 	 * Note that if the status check fails, we need to bring the interface
18001 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
18002 	 * ipif_up_done[_v6]().
18003 	 */
18004 	status = ZONE_IS_UNINITIALIZED;
18005 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
18006 		status = zone_status_get(zptr);
18007 		zone_rele(zptr);
18008 	}
18009 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
18010 		if (ipif->ipif_isv6) {
18011 			(void) ipif_up_done_v6(ipif);
18012 		} else {
18013 			(void) ipif_up_done(ipif);
18014 		}
18015 		return (EINVAL);
18016 	}
18017 
18018 	ipif_down_tail(ipif);
18019 
18020 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
18021 	    B_TRUE));
18022 }
18023 
18024 /*
18025  * Return the number of addresses on `ill' with one or more of the values
18026  * in `set' set and all of the values in `clear' clear.
18027  */
18028 static uint_t
18029 ill_flagaddr_cnt(const ill_t *ill, uint64_t set, uint64_t clear)
18030 {
18031 	ipif_t	*ipif;
18032 	uint_t	cnt = 0;
18033 
18034 	ASSERT(IAM_WRITER_ILL(ill));
18035 
18036 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
18037 		if ((ipif->ipif_flags & set) && !(ipif->ipif_flags & clear))
18038 			cnt++;
18039 
18040 	return (cnt);
18041 }
18042 
18043 /*
18044  * Return the number of migratable addresses on `ill' that are under
18045  * application control.
18046  */
18047 uint_t
18048 ill_appaddr_cnt(const ill_t *ill)
18049 {
18050 	return (ill_flagaddr_cnt(ill, IPIF_DHCPRUNNING | IPIF_ADDRCONF,
18051 	    IPIF_NOFAILOVER));
18052 }
18053 
18054 /*
18055  * Return the number of point-to-point addresses on `ill'.
18056  */
18057 uint_t
18058 ill_ptpaddr_cnt(const ill_t *ill)
18059 {
18060 	return (ill_flagaddr_cnt(ill, IPIF_POINTOPOINT, 0));
18061 }
18062 
18063 /* ARGSUSED */
18064 int
18065 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18066 	ip_ioctl_cmd_t *ipip, void *ifreq)
18067 {
18068 	struct lifreq	*lifr = ifreq;
18069 
18070 	ASSERT(q->q_next == NULL);
18071 	ASSERT(CONN_Q(q));
18072 
18073 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
18074 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
18075 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
18076 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
18077 
18078 	return (0);
18079 }
18080 
18081 /* Find the previous ILL in this usesrc group */
18082 static ill_t *
18083 ill_prev_usesrc(ill_t *uill)
18084 {
18085 	ill_t *ill;
18086 
18087 	for (ill = uill->ill_usesrc_grp_next;
18088 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
18089 	    ill = ill->ill_usesrc_grp_next)
18090 		/* do nothing */;
18091 	return (ill);
18092 }
18093 
18094 /*
18095  * Release all members of the usesrc group. This routine is called
18096  * from ill_delete when the interface being unplumbed is the
18097  * group head.
18098  */
18099 static void
18100 ill_disband_usesrc_group(ill_t *uill)
18101 {
18102 	ill_t *next_ill, *tmp_ill;
18103 	ip_stack_t	*ipst = uill->ill_ipst;
18104 
18105 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
18106 	next_ill = uill->ill_usesrc_grp_next;
18107 
18108 	do {
18109 		ASSERT(next_ill != NULL);
18110 		tmp_ill = next_ill->ill_usesrc_grp_next;
18111 		ASSERT(tmp_ill != NULL);
18112 		next_ill->ill_usesrc_grp_next = NULL;
18113 		next_ill->ill_usesrc_ifindex = 0;
18114 		next_ill = tmp_ill;
18115 	} while (next_ill->ill_usesrc_ifindex != 0);
18116 	uill->ill_usesrc_grp_next = NULL;
18117 }
18118 
18119 /*
18120  * Remove the client usesrc ILL from the list and relink to a new list
18121  */
18122 int
18123 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
18124 {
18125 	ill_t *ill, *tmp_ill;
18126 	ip_stack_t	*ipst = ucill->ill_ipst;
18127 
18128 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
18129 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
18130 
18131 	/*
18132 	 * Check if the usesrc client ILL passed in is not already
18133 	 * in use as a usesrc ILL i.e one whose source address is
18134 	 * in use OR a usesrc ILL is not already in use as a usesrc
18135 	 * client ILL
18136 	 */
18137 	if ((ucill->ill_usesrc_ifindex == 0) ||
18138 	    (uill->ill_usesrc_ifindex != 0)) {
18139 		return (-1);
18140 	}
18141 
18142 	ill = ill_prev_usesrc(ucill);
18143 	ASSERT(ill->ill_usesrc_grp_next != NULL);
18144 
18145 	/* Remove from the current list */
18146 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
18147 		/* Only two elements in the list */
18148 		ASSERT(ill->ill_usesrc_ifindex == 0);
18149 		ill->ill_usesrc_grp_next = NULL;
18150 	} else {
18151 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
18152 	}
18153 
18154 	if (ifindex == 0) {
18155 		ucill->ill_usesrc_ifindex = 0;
18156 		ucill->ill_usesrc_grp_next = NULL;
18157 		return (0);
18158 	}
18159 
18160 	ucill->ill_usesrc_ifindex = ifindex;
18161 	tmp_ill = uill->ill_usesrc_grp_next;
18162 	uill->ill_usesrc_grp_next = ucill;
18163 	ucill->ill_usesrc_grp_next =
18164 	    (tmp_ill != NULL) ? tmp_ill : uill;
18165 	return (0);
18166 }
18167 
18168 /*
18169  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
18170  * ip.c for locking details.
18171  */
18172 /* ARGSUSED */
18173 int
18174 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18175     ip_ioctl_cmd_t *ipip, void *ifreq)
18176 {
18177 	struct lifreq *lifr = (struct lifreq *)ifreq;
18178 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
18179 	    ill_flag_changed = B_FALSE;
18180 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
18181 	int err = 0, ret;
18182 	uint_t ifindex;
18183 	ipsq_t *ipsq = NULL;
18184 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
18185 
18186 	ASSERT(IAM_WRITER_IPIF(ipif));
18187 	ASSERT(q->q_next == NULL);
18188 	ASSERT(CONN_Q(q));
18189 
18190 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
18191 
18192 	ifindex = lifr->lifr_index;
18193 	if (ifindex == 0) {
18194 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
18195 			/* non usesrc group interface, nothing to reset */
18196 			return (0);
18197 		}
18198 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
18199 		/* valid reset request */
18200 		reset_flg = B_TRUE;
18201 	}
18202 
18203 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
18204 	    ip_process_ioctl, &err, ipst);
18205 	if (usesrc_ill == NULL) {
18206 		return (err);
18207 	}
18208 
18209 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
18210 	    NEW_OP, B_TRUE);
18211 	if (ipsq == NULL) {
18212 		err = EINPROGRESS;
18213 		/* Operation enqueued on the ipsq of the usesrc ILL */
18214 		goto done;
18215 	}
18216 
18217 	/* USESRC isn't currently supported with IPMP */
18218 	if (IS_IPMP(usesrc_ill) || IS_UNDER_IPMP(usesrc_ill)) {
18219 		err = ENOTSUP;
18220 		goto done;
18221 	}
18222 
18223 	/*
18224 	 * USESRC isn't compatible with the STANDBY flag.  (STANDBY is only
18225 	 * used by IPMP underlying interfaces, but someone might think it's
18226 	 * more general and try to use it independently with VNI.)
18227 	 */
18228 	if (usesrc_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
18229 		err = ENOTSUP;
18230 		goto done;
18231 	}
18232 
18233 	/*
18234 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
18235 	 * already a client then return EINVAL
18236 	 */
18237 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
18238 		err = EINVAL;
18239 		goto done;
18240 	}
18241 
18242 	/*
18243 	 * If the ill_usesrc_ifindex field is already set to what it needs to
18244 	 * be then this is a duplicate operation.
18245 	 */
18246 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
18247 		err = 0;
18248 		goto done;
18249 	}
18250 
18251 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
18252 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
18253 	    usesrc_ill->ill_isv6));
18254 
18255 	/*
18256 	 * The next step ensures that no new ires will be created referencing
18257 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
18258 	 * we go through an ire walk deleting all ire caches that reference
18259 	 * the client ill. New ires referencing the client ill that are added
18260 	 * to the ire table before the ILL_CHANGING flag is set, will be
18261 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
18262 	 * the client ill while the ILL_CHANGING flag is set will be failed
18263 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
18264 	 * checks (under the ill_g_usesrc_lock) that the ire being added
18265 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
18266 	 * belong to the same usesrc group.
18267 	 */
18268 	mutex_enter(&usesrc_cli_ill->ill_lock);
18269 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
18270 	mutex_exit(&usesrc_cli_ill->ill_lock);
18271 	ill_flag_changed = B_TRUE;
18272 
18273 	if (ipif->ipif_isv6)
18274 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
18275 		    ALL_ZONES, ipst);
18276 	else
18277 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
18278 		    ALL_ZONES, ipst);
18279 
18280 	/*
18281 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
18282 	 * and the ill_usesrc_ifindex fields
18283 	 */
18284 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
18285 
18286 	if (reset_flg) {
18287 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
18288 		if (ret != 0) {
18289 			err = EINVAL;
18290 		}
18291 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
18292 		goto done;
18293 	}
18294 
18295 	/*
18296 	 * Four possibilities to consider:
18297 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
18298 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
18299 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
18300 	 * 4. Both are part of their respective usesrc groups
18301 	 */
18302 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
18303 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
18304 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
18305 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
18306 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
18307 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
18308 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
18309 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
18310 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
18311 		/* Insert at head of list */
18312 		usesrc_cli_ill->ill_usesrc_grp_next =
18313 		    usesrc_ill->ill_usesrc_grp_next;
18314 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
18315 	} else {
18316 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
18317 		    ifindex);
18318 		if (ret != 0)
18319 			err = EINVAL;
18320 	}
18321 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
18322 
18323 done:
18324 	if (ill_flag_changed) {
18325 		mutex_enter(&usesrc_cli_ill->ill_lock);
18326 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
18327 		mutex_exit(&usesrc_cli_ill->ill_lock);
18328 	}
18329 	if (ipsq != NULL)
18330 		ipsq_exit(ipsq);
18331 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
18332 	ill_refrele(usesrc_ill);
18333 	return (err);
18334 }
18335 
18336 /*
18337  * comparison function used by avl.
18338  */
18339 static int
18340 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
18341 {
18342 
18343 	uint_t index;
18344 
18345 	ASSERT(phyip != NULL && index_ptr != NULL);
18346 
18347 	index = *((uint_t *)index_ptr);
18348 	/*
18349 	 * let the phyint with the lowest index be on top.
18350 	 */
18351 	if (((phyint_t *)phyip)->phyint_ifindex < index)
18352 		return (1);
18353 	if (((phyint_t *)phyip)->phyint_ifindex > index)
18354 		return (-1);
18355 	return (0);
18356 }
18357 
18358 /*
18359  * comparison function used by avl.
18360  */
18361 static int
18362 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
18363 {
18364 	ill_t *ill;
18365 	int res = 0;
18366 
18367 	ASSERT(phyip != NULL && name_ptr != NULL);
18368 
18369 	if (((phyint_t *)phyip)->phyint_illv4)
18370 		ill = ((phyint_t *)phyip)->phyint_illv4;
18371 	else
18372 		ill = ((phyint_t *)phyip)->phyint_illv6;
18373 	ASSERT(ill != NULL);
18374 
18375 	res = strcmp(ill->ill_name, (char *)name_ptr);
18376 	if (res > 0)
18377 		return (1);
18378 	else if (res < 0)
18379 		return (-1);
18380 	return (0);
18381 }
18382 
18383 /*
18384  * This function is called on the unplumb path via ill_glist_delete() when
18385  * there are no ills left on the phyint and thus the phyint can be freed.
18386  */
18387 static void
18388 phyint_free(phyint_t *phyi)
18389 {
18390 	ip_stack_t *ipst = PHYINT_TO_IPST(phyi);
18391 
18392 	ASSERT(phyi->phyint_illv4 == NULL && phyi->phyint_illv6 == NULL);
18393 
18394 	/*
18395 	 * If this phyint was an IPMP meta-interface, blow away the group.
18396 	 * This is safe to do because all of the illgrps have already been
18397 	 * removed by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find us.
18398 	 * If we're cleaning up as a result of failed initialization,
18399 	 * phyint_grp may be NULL.
18400 	 */
18401 	if ((phyi->phyint_flags & PHYI_IPMP) && (phyi->phyint_grp != NULL)) {
18402 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
18403 		ipmp_grp_destroy(phyi->phyint_grp);
18404 		phyi->phyint_grp = NULL;
18405 		rw_exit(&ipst->ips_ipmp_lock);
18406 	}
18407 
18408 	/*
18409 	 * If this interface was under IPMP, take it out of the group.
18410 	 */
18411 	if (phyi->phyint_grp != NULL)
18412 		ipmp_phyint_leave_grp(phyi);
18413 
18414 	/*
18415 	 * Delete the phyint and disassociate its ipsq.  The ipsq itself
18416 	 * will be freed in ipsq_exit().
18417 	 */
18418 	phyi->phyint_ipsq->ipsq_phyint = NULL;
18419 	phyi->phyint_name[0] = '\0';
18420 
18421 	mi_free(phyi);
18422 }
18423 
18424 /*
18425  * Attach the ill to the phyint structure which can be shared by both
18426  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
18427  * function is called from ipif_set_values and ill_lookup_on_name (for
18428  * loopback) where we know the name of the ill. We lookup the ill and if
18429  * there is one present already with the name use that phyint. Otherwise
18430  * reuse the one allocated by ill_init.
18431  */
18432 static void
18433 ill_phyint_reinit(ill_t *ill)
18434 {
18435 	boolean_t isv6 = ill->ill_isv6;
18436 	phyint_t *phyi_old;
18437 	phyint_t *phyi;
18438 	avl_index_t where = 0;
18439 	ill_t	*ill_other = NULL;
18440 	ip_stack_t	*ipst = ill->ill_ipst;
18441 
18442 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
18443 
18444 	phyi_old = ill->ill_phyint;
18445 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
18446 	    phyi_old->phyint_illv6 == NULL));
18447 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
18448 	    phyi_old->phyint_illv4 == NULL));
18449 	ASSERT(phyi_old->phyint_ifindex == 0);
18450 
18451 	/*
18452 	 * Now that our ill has a name, set it in the phyint.
18453 	 */
18454 	(void) strlcpy(ill->ill_phyint->phyint_name, ill->ill_name, LIFNAMSIZ);
18455 
18456 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
18457 	    ill->ill_name, &where);
18458 
18459 	/*
18460 	 * 1. We grabbed the ill_g_lock before inserting this ill into
18461 	 *    the global list of ills. So no other thread could have located
18462 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
18463 	 * 2. Now locate the other protocol instance of this ill.
18464 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
18465 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
18466 	 *    of neither ill can change.
18467 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
18468 	 *    other ill.
18469 	 * 5. Release all locks.
18470 	 */
18471 
18472 	/*
18473 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
18474 	 * we are initializing IPv4.
18475 	 */
18476 	if (phyi != NULL) {
18477 		ill_other = (isv6) ? phyi->phyint_illv4 : phyi->phyint_illv6;
18478 		ASSERT(ill_other->ill_phyint != NULL);
18479 		ASSERT((isv6 && !ill_other->ill_isv6) ||
18480 		    (!isv6 && ill_other->ill_isv6));
18481 		GRAB_ILL_LOCKS(ill, ill_other);
18482 		/*
18483 		 * We are potentially throwing away phyint_flags which
18484 		 * could be different from the one that we obtain from
18485 		 * ill_other->ill_phyint. But it is okay as we are assuming
18486 		 * that the state maintained within IP is correct.
18487 		 */
18488 		mutex_enter(&phyi->phyint_lock);
18489 		if (isv6) {
18490 			ASSERT(phyi->phyint_illv6 == NULL);
18491 			phyi->phyint_illv6 = ill;
18492 		} else {
18493 			ASSERT(phyi->phyint_illv4 == NULL);
18494 			phyi->phyint_illv4 = ill;
18495 		}
18496 
18497 		/*
18498 		 * Delete the old phyint and make its ipsq eligible
18499 		 * to be freed in ipsq_exit().
18500 		 */
18501 		phyi_old->phyint_illv4 = NULL;
18502 		phyi_old->phyint_illv6 = NULL;
18503 		phyi_old->phyint_ipsq->ipsq_phyint = NULL;
18504 		phyi_old->phyint_name[0] = '\0';
18505 		mi_free(phyi_old);
18506 	} else {
18507 		mutex_enter(&ill->ill_lock);
18508 		/*
18509 		 * We don't need to acquire any lock, since
18510 		 * the ill is not yet visible globally  and we
18511 		 * have not yet released the ill_g_lock.
18512 		 */
18513 		phyi = phyi_old;
18514 		mutex_enter(&phyi->phyint_lock);
18515 		/* XXX We need a recovery strategy here. */
18516 		if (!phyint_assign_ifindex(phyi, ipst))
18517 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
18518 
18519 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
18520 		    (void *)phyi, where);
18521 
18522 		(void) avl_find(&ipst->ips_phyint_g_list->
18523 		    phyint_list_avl_by_index,
18524 		    &phyi->phyint_ifindex, &where);
18525 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
18526 		    (void *)phyi, where);
18527 	}
18528 
18529 	/*
18530 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
18531 	 * pending mp is not affected because that is per ill basis.
18532 	 */
18533 	ill->ill_phyint = phyi;
18534 
18535 	/*
18536 	 * Now that the phyint's ifindex has been assigned, complete the
18537 	 * remaining
18538 	 */
18539 
18540 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
18541 	if (ill->ill_isv6) {
18542 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
18543 		    ill->ill_phyint->phyint_ifindex;
18544 		ill->ill_mcast_type = ipst->ips_mld_max_version;
18545 	} else {
18546 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
18547 	}
18548 
18549 	/*
18550 	 * Generate an event within the hooks framework to indicate that
18551 	 * a new interface has just been added to IP.  For this event to
18552 	 * be generated, the network interface must, at least, have an
18553 	 * ifindex assigned to it.
18554 	 *
18555 	 * This needs to be run inside the ill_g_lock perimeter to ensure
18556 	 * that the ordering of delivered events to listeners matches the
18557 	 * order of them in the kernel.
18558 	 *
18559 	 * This function could be called from ill_lookup_on_name. In that case
18560 	 * the interface is loopback "lo", which will not generate a NIC event.
18561 	 */
18562 	if (ill->ill_name_length <= 2 ||
18563 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
18564 		ill_nic_event_dispatch(ill, 0, NE_PLUMB, ill->ill_name,
18565 		    ill->ill_name_length);
18566 	}
18567 	RELEASE_ILL_LOCKS(ill, ill_other);
18568 	mutex_exit(&phyi->phyint_lock);
18569 }
18570 
18571 /*
18572  * Notify any downstream modules of the name of this interface.
18573  * An M_IOCTL is used even though we don't expect a successful reply.
18574  * Any reply message from the driver (presumably an M_IOCNAK) will
18575  * eventually get discarded somewhere upstream.  The message format is
18576  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
18577  * to IP.
18578  */
18579 static void
18580 ip_ifname_notify(ill_t *ill, queue_t *q)
18581 {
18582 	mblk_t *mp1, *mp2;
18583 	struct iocblk *iocp;
18584 	struct lifreq *lifr;
18585 
18586 	mp1 = mkiocb(SIOCSLIFNAME);
18587 	if (mp1 == NULL)
18588 		return;
18589 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
18590 	if (mp2 == NULL) {
18591 		freeb(mp1);
18592 		return;
18593 	}
18594 
18595 	mp1->b_cont = mp2;
18596 	iocp = (struct iocblk *)mp1->b_rptr;
18597 	iocp->ioc_count = sizeof (struct lifreq);
18598 
18599 	lifr = (struct lifreq *)mp2->b_rptr;
18600 	mp2->b_wptr += sizeof (struct lifreq);
18601 	bzero(lifr, sizeof (struct lifreq));
18602 
18603 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
18604 	lifr->lifr_ppa = ill->ill_ppa;
18605 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
18606 
18607 	putnext(q, mp1);
18608 }
18609 
18610 static int
18611 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
18612 {
18613 	int		err;
18614 	ip_stack_t	*ipst = ill->ill_ipst;
18615 	phyint_t	*phyi = ill->ill_phyint;
18616 
18617 	/* Set the obsolete NDD per-interface forwarding name. */
18618 	err = ill_set_ndd_name(ill);
18619 	if (err != 0) {
18620 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
18621 		    err);
18622 	}
18623 
18624 	/*
18625 	 * Now that ill_name is set, the configuration for the IPMP
18626 	 * meta-interface can be performed.
18627 	 */
18628 	if (IS_IPMP(ill)) {
18629 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
18630 		/*
18631 		 * If phyi->phyint_grp is NULL, then this is the first IPMP
18632 		 * meta-interface and we need to create the IPMP group.
18633 		 */
18634 		if (phyi->phyint_grp == NULL) {
18635 			/*
18636 			 * If someone has renamed another IPMP group to have
18637 			 * the same name as our interface, bail.
18638 			 */
18639 			if (ipmp_grp_lookup(ill->ill_name, ipst) != NULL) {
18640 				rw_exit(&ipst->ips_ipmp_lock);
18641 				return (EEXIST);
18642 			}
18643 			phyi->phyint_grp = ipmp_grp_create(ill->ill_name, phyi);
18644 			if (phyi->phyint_grp == NULL) {
18645 				rw_exit(&ipst->ips_ipmp_lock);
18646 				return (ENOMEM);
18647 			}
18648 		}
18649 		rw_exit(&ipst->ips_ipmp_lock);
18650 	}
18651 
18652 	/* Tell downstream modules where they are. */
18653 	ip_ifname_notify(ill, q);
18654 
18655 	/*
18656 	 * ill_dl_phys returns EINPROGRESS in the usual case.
18657 	 * Error cases are ENOMEM ...
18658 	 */
18659 	err = ill_dl_phys(ill, ipif, mp, q);
18660 
18661 	/*
18662 	 * If there is no IRE expiration timer running, get one started.
18663 	 * igmp and mld timers will be triggered by the first multicast
18664 	 */
18665 	if (ipst->ips_ip_ire_expire_id == 0) {
18666 		/*
18667 		 * acquire the lock and check again.
18668 		 */
18669 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
18670 		if (ipst->ips_ip_ire_expire_id == 0) {
18671 			ipst->ips_ip_ire_expire_id = timeout(
18672 			    ip_trash_timer_expire, ipst,
18673 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
18674 		}
18675 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
18676 	}
18677 
18678 	if (ill->ill_isv6) {
18679 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
18680 		if (ipst->ips_mld_slowtimeout_id == 0) {
18681 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
18682 			    (void *)ipst,
18683 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
18684 		}
18685 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
18686 	} else {
18687 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
18688 		if (ipst->ips_igmp_slowtimeout_id == 0) {
18689 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
18690 			    (void *)ipst,
18691 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
18692 		}
18693 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
18694 	}
18695 
18696 	return (err);
18697 }
18698 
18699 /*
18700  * Common routine for ppa and ifname setting. Should be called exclusive.
18701  *
18702  * Returns EINPROGRESS when mp has been consumed by queueing it on
18703  * ill_pending_mp and the ioctl will complete in ip_rput.
18704  *
18705  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
18706  * the new name and new ppa in lifr_name and lifr_ppa respectively.
18707  * For SLIFNAME, we pass these values back to the userland.
18708  */
18709 static int
18710 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
18711 {
18712 	ill_t	*ill;
18713 	ipif_t	*ipif;
18714 	ipsq_t	*ipsq;
18715 	char	*ppa_ptr;
18716 	char	*old_ptr;
18717 	char	old_char;
18718 	int	error;
18719 	ip_stack_t	*ipst;
18720 
18721 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
18722 	ASSERT(q->q_next != NULL);
18723 	ASSERT(interf_name != NULL);
18724 
18725 	ill = (ill_t *)q->q_ptr;
18726 	ipst = ill->ill_ipst;
18727 
18728 	ASSERT(ill->ill_ipst != NULL);
18729 	ASSERT(ill->ill_name[0] == '\0');
18730 	ASSERT(IAM_WRITER_ILL(ill));
18731 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
18732 	ASSERT(ill->ill_ppa == UINT_MAX);
18733 
18734 	/* The ppa is sent down by ifconfig or is chosen */
18735 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
18736 		return (EINVAL);
18737 	}
18738 
18739 	/*
18740 	 * make sure ppa passed in is same as ppa in the name.
18741 	 * This check is not made when ppa == UINT_MAX in that case ppa
18742 	 * in the name could be anything. System will choose a ppa and
18743 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
18744 	 */
18745 	if (*new_ppa_ptr != UINT_MAX) {
18746 		/* stoi changes the pointer */
18747 		old_ptr = ppa_ptr;
18748 		/*
18749 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
18750 		 * (they don't have an externally visible ppa).  We assign one
18751 		 * here so that we can manage the interface.  Note that in
18752 		 * the past this value was always 0 for DLPI 1 drivers.
18753 		 */
18754 		if (*new_ppa_ptr == 0)
18755 			*new_ppa_ptr = stoi(&old_ptr);
18756 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
18757 			return (EINVAL);
18758 	}
18759 	/*
18760 	 * terminate string before ppa
18761 	 * save char at that location.
18762 	 */
18763 	old_char = ppa_ptr[0];
18764 	ppa_ptr[0] = '\0';
18765 
18766 	ill->ill_ppa = *new_ppa_ptr;
18767 	/*
18768 	 * Finish as much work now as possible before calling ill_glist_insert
18769 	 * which makes the ill globally visible and also merges it with the
18770 	 * other protocol instance of this phyint. The remaining work is
18771 	 * done after entering the ipsq which may happen sometime later.
18772 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
18773 	 */
18774 	ipif = ill->ill_ipif;
18775 
18776 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
18777 	ipif_assign_seqid(ipif);
18778 
18779 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
18780 		ill->ill_flags |= ILLF_IPV4;
18781 
18782 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
18783 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
18784 
18785 	if (ill->ill_flags & ILLF_IPV6) {
18786 
18787 		ill->ill_isv6 = B_TRUE;
18788 		if (ill->ill_rq != NULL) {
18789 			ill->ill_rq->q_qinfo = &iprinitv6;
18790 			ill->ill_wq->q_qinfo = &ipwinitv6;
18791 		}
18792 
18793 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
18794 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
18795 		ipif->ipif_v6src_addr = ipv6_all_zeros;
18796 		ipif->ipif_v6subnet = ipv6_all_zeros;
18797 		ipif->ipif_v6net_mask = ipv6_all_zeros;
18798 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
18799 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
18800 		/*
18801 		 * point-to-point or Non-mulicast capable
18802 		 * interfaces won't do NUD unless explicitly
18803 		 * configured to do so.
18804 		 */
18805 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
18806 		    !(ill->ill_flags & ILLF_MULTICAST)) {
18807 			ill->ill_flags |= ILLF_NONUD;
18808 		}
18809 		/* Make sure IPv4 specific flag is not set on IPv6 if */
18810 		if (ill->ill_flags & ILLF_NOARP) {
18811 			/*
18812 			 * Note: xresolv interfaces will eventually need
18813 			 * NOARP set here as well, but that will require
18814 			 * those external resolvers to have some
18815 			 * knowledge of that flag and act appropriately.
18816 			 * Not to be changed at present.
18817 			 */
18818 			ill->ill_flags &= ~ILLF_NOARP;
18819 		}
18820 		/*
18821 		 * Set the ILLF_ROUTER flag according to the global
18822 		 * IPv6 forwarding policy.
18823 		 */
18824 		if (ipst->ips_ipv6_forward != 0)
18825 			ill->ill_flags |= ILLF_ROUTER;
18826 	} else if (ill->ill_flags & ILLF_IPV4) {
18827 		ill->ill_isv6 = B_FALSE;
18828 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
18829 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
18830 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
18831 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
18832 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
18833 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
18834 		/*
18835 		 * Set the ILLF_ROUTER flag according to the global
18836 		 * IPv4 forwarding policy.
18837 		 */
18838 		if (ipst->ips_ip_g_forward != 0)
18839 			ill->ill_flags |= ILLF_ROUTER;
18840 	}
18841 
18842 	ASSERT(ill->ill_phyint != NULL);
18843 
18844 	/*
18845 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
18846 	 * be completed in ill_glist_insert -> ill_phyint_reinit
18847 	 */
18848 	if (!ill_allocate_mibs(ill))
18849 		return (ENOMEM);
18850 
18851 	/*
18852 	 * Pick a default sap until we get the DL_INFO_ACK back from
18853 	 * the driver.
18854 	 */
18855 	if (ill->ill_sap == 0) {
18856 		if (ill->ill_isv6)
18857 			ill->ill_sap = IP6_DL_SAP;
18858 		else
18859 			ill->ill_sap = IP_DL_SAP;
18860 	}
18861 
18862 	ill->ill_ifname_pending = 1;
18863 	ill->ill_ifname_pending_err = 0;
18864 
18865 	/*
18866 	 * When the first ipif comes up in ipif_up_done(), multicast groups
18867 	 * that were joined while this ill was not bound to the DLPI link need
18868 	 * to be recovered by ill_recover_multicast().
18869 	 */
18870 	ill->ill_need_recover_multicast = 1;
18871 
18872 	ill_refhold(ill);
18873 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
18874 	if ((error = ill_glist_insert(ill, interf_name,
18875 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
18876 		ill->ill_ppa = UINT_MAX;
18877 		ill->ill_name[0] = '\0';
18878 		/*
18879 		 * undo null termination done above.
18880 		 */
18881 		ppa_ptr[0] = old_char;
18882 		rw_exit(&ipst->ips_ill_g_lock);
18883 		ill_refrele(ill);
18884 		return (error);
18885 	}
18886 
18887 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
18888 
18889 	/*
18890 	 * When we return the buffer pointed to by interf_name should contain
18891 	 * the same name as in ill_name.
18892 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
18893 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
18894 	 * so copy full name and update the ppa ptr.
18895 	 * When ppa passed in != UINT_MAX all values are correct just undo
18896 	 * null termination, this saves a bcopy.
18897 	 */
18898 	if (*new_ppa_ptr == UINT_MAX) {
18899 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
18900 		*new_ppa_ptr = ill->ill_ppa;
18901 	} else {
18902 		/*
18903 		 * undo null termination done above.
18904 		 */
18905 		ppa_ptr[0] = old_char;
18906 	}
18907 
18908 	/* Let SCTP know about this ILL */
18909 	sctp_update_ill(ill, SCTP_ILL_INSERT);
18910 
18911 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
18912 	    B_TRUE);
18913 
18914 	rw_exit(&ipst->ips_ill_g_lock);
18915 	ill_refrele(ill);
18916 	if (ipsq == NULL)
18917 		return (EINPROGRESS);
18918 
18919 	/*
18920 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
18921 	 */
18922 	if (ipsq->ipsq_xop->ipx_current_ipif == NULL)
18923 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
18924 	else
18925 		ASSERT(ipsq->ipsq_xop->ipx_current_ipif == ipif);
18926 
18927 	error = ipif_set_values_tail(ill, ipif, mp, q);
18928 	ipsq_exit(ipsq);
18929 	if (error != 0 && error != EINPROGRESS) {
18930 		/*
18931 		 * restore previous values
18932 		 */
18933 		ill->ill_isv6 = B_FALSE;
18934 	}
18935 	return (error);
18936 }
18937 
18938 void
18939 ipif_init(ip_stack_t *ipst)
18940 {
18941 	int i;
18942 
18943 	for (i = 0; i < MAX_G_HEADS; i++) {
18944 		ipst->ips_ill_g_heads[i].ill_g_list_head =
18945 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
18946 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
18947 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
18948 	}
18949 
18950 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
18951 	    ill_phyint_compare_index,
18952 	    sizeof (phyint_t),
18953 	    offsetof(struct phyint, phyint_avl_by_index));
18954 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
18955 	    ill_phyint_compare_name,
18956 	    sizeof (phyint_t),
18957 	    offsetof(struct phyint, phyint_avl_by_name));
18958 }
18959 
18960 /*
18961  * Lookup the ipif corresponding to the onlink destination address. For
18962  * point-to-point interfaces, it matches with remote endpoint destination
18963  * address. For point-to-multipoint interfaces it only tries to match the
18964  * destination with the interface's subnet address. The longest, most specific
18965  * match is found to take care of such rare network configurations like -
18966  * le0: 129.146.1.1/16
18967  * le1: 129.146.2.2/24
18968  *
18969  * This is used by SO_DONTROUTE and IP_NEXTHOP.  Since neither of those are
18970  * supported on underlying interfaces in an IPMP group, underlying interfaces
18971  * are ignored when looking up a match.  (If we didn't ignore them, we'd
18972  * risk using a test address as a source for outgoing traffic.)
18973  */
18974 ipif_t *
18975 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
18976 {
18977 	ipif_t	*ipif, *best_ipif;
18978 	ill_t	*ill;
18979 	ill_walk_context_t ctx;
18980 
18981 	ASSERT(zoneid != ALL_ZONES);
18982 	best_ipif = NULL;
18983 
18984 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
18985 	ill = ILL_START_WALK_V4(&ctx, ipst);
18986 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
18987 		if (IS_UNDER_IPMP(ill))
18988 			continue;
18989 		mutex_enter(&ill->ill_lock);
18990 		for (ipif = ill->ill_ipif; ipif != NULL;
18991 		    ipif = ipif->ipif_next) {
18992 			if (!IPIF_CAN_LOOKUP(ipif))
18993 				continue;
18994 			if (ipif->ipif_zoneid != zoneid &&
18995 			    ipif->ipif_zoneid != ALL_ZONES)
18996 				continue;
18997 			/*
18998 			 * Point-to-point case. Look for exact match with
18999 			 * destination address.
19000 			 */
19001 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19002 				if (ipif->ipif_pp_dst_addr == addr) {
19003 					ipif_refhold_locked(ipif);
19004 					mutex_exit(&ill->ill_lock);
19005 					rw_exit(&ipst->ips_ill_g_lock);
19006 					if (best_ipif != NULL)
19007 						ipif_refrele(best_ipif);
19008 					return (ipif);
19009 				}
19010 			} else if (ipif->ipif_subnet == (addr &
19011 			    ipif->ipif_net_mask)) {
19012 				/*
19013 				 * Point-to-multipoint case. Looping through to
19014 				 * find the most specific match. If there are
19015 				 * multiple best match ipif's then prefer ipif's
19016 				 * that are UP. If there is only one best match
19017 				 * ipif and it is DOWN we must still return it.
19018 				 */
19019 				if ((best_ipif == NULL) ||
19020 				    (ipif->ipif_net_mask >
19021 				    best_ipif->ipif_net_mask) ||
19022 				    ((ipif->ipif_net_mask ==
19023 				    best_ipif->ipif_net_mask) &&
19024 				    ((ipif->ipif_flags & IPIF_UP) &&
19025 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
19026 					ipif_refhold_locked(ipif);
19027 					mutex_exit(&ill->ill_lock);
19028 					rw_exit(&ipst->ips_ill_g_lock);
19029 					if (best_ipif != NULL)
19030 						ipif_refrele(best_ipif);
19031 					best_ipif = ipif;
19032 					rw_enter(&ipst->ips_ill_g_lock,
19033 					    RW_READER);
19034 					mutex_enter(&ill->ill_lock);
19035 				}
19036 			}
19037 		}
19038 		mutex_exit(&ill->ill_lock);
19039 	}
19040 	rw_exit(&ipst->ips_ill_g_lock);
19041 	return (best_ipif);
19042 }
19043 
19044 /*
19045  * Save enough information so that we can recreate the IRE if
19046  * the interface goes down and then up.
19047  */
19048 static void
19049 ipif_save_ire(ipif_t *ipif, ire_t *ire)
19050 {
19051 	mblk_t	*save_mp;
19052 
19053 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
19054 	if (save_mp != NULL) {
19055 		ifrt_t	*ifrt;
19056 
19057 		save_mp->b_wptr += sizeof (ifrt_t);
19058 		ifrt = (ifrt_t *)save_mp->b_rptr;
19059 		bzero(ifrt, sizeof (ifrt_t));
19060 		ifrt->ifrt_type = ire->ire_type;
19061 		ifrt->ifrt_addr = ire->ire_addr;
19062 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
19063 		ifrt->ifrt_src_addr = ire->ire_src_addr;
19064 		ifrt->ifrt_mask = ire->ire_mask;
19065 		ifrt->ifrt_flags = ire->ire_flags;
19066 		ifrt->ifrt_max_frag = ire->ire_max_frag;
19067 		mutex_enter(&ipif->ipif_saved_ire_lock);
19068 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
19069 		ipif->ipif_saved_ire_mp = save_mp;
19070 		ipif->ipif_saved_ire_cnt++;
19071 		mutex_exit(&ipif->ipif_saved_ire_lock);
19072 	}
19073 }
19074 
19075 static void
19076 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
19077 {
19078 	mblk_t	**mpp;
19079 	mblk_t	*mp;
19080 	ifrt_t	*ifrt;
19081 
19082 	/* Remove from ipif_saved_ire_mp list if it is there */
19083 	mutex_enter(&ipif->ipif_saved_ire_lock);
19084 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
19085 	    mpp = &(*mpp)->b_cont) {
19086 		/*
19087 		 * On a given ipif, the triple of address, gateway and
19088 		 * mask is unique for each saved IRE (in the case of
19089 		 * ordinary interface routes, the gateway address is
19090 		 * all-zeroes).
19091 		 */
19092 		mp = *mpp;
19093 		ifrt = (ifrt_t *)mp->b_rptr;
19094 		if (ifrt->ifrt_addr == ire->ire_addr &&
19095 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
19096 		    ifrt->ifrt_mask == ire->ire_mask) {
19097 			*mpp = mp->b_cont;
19098 			ipif->ipif_saved_ire_cnt--;
19099 			freeb(mp);
19100 			break;
19101 		}
19102 	}
19103 	mutex_exit(&ipif->ipif_saved_ire_lock);
19104 }
19105 
19106 /*
19107  * IP multirouting broadcast routes handling
19108  * Append CGTP broadcast IREs to regular ones created
19109  * at ifconfig time.
19110  */
19111 static void
19112 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
19113 {
19114 	ire_t *ire_prim;
19115 
19116 	ASSERT(ire != NULL);
19117 	ASSERT(ire_dst != NULL);
19118 
19119 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
19120 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
19121 	if (ire_prim != NULL) {
19122 		/*
19123 		 * We are in the special case of broadcasts for
19124 		 * CGTP. We add an IRE_BROADCAST that holds
19125 		 * the RTF_MULTIRT flag, the destination
19126 		 * address of ire_dst and the low level
19127 		 * info of ire_prim. In other words, CGTP
19128 		 * broadcast is added to the redundant ipif.
19129 		 */
19130 		ipif_t *ipif_prim;
19131 		ire_t  *bcast_ire;
19132 
19133 		ipif_prim = ire_prim->ire_ipif;
19134 
19135 		ip2dbg(("ip_cgtp_filter_bcast_add: "
19136 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
19137 		    (void *)ire_dst, (void *)ire_prim,
19138 		    (void *)ipif_prim));
19139 
19140 		bcast_ire = ire_create(
19141 		    (uchar_t *)&ire->ire_addr,
19142 		    (uchar_t *)&ip_g_all_ones,
19143 		    (uchar_t *)&ire_dst->ire_src_addr,
19144 		    (uchar_t *)&ire->ire_gateway_addr,
19145 		    &ipif_prim->ipif_mtu,
19146 		    NULL,
19147 		    ipif_prim->ipif_rq,
19148 		    ipif_prim->ipif_wq,
19149 		    IRE_BROADCAST,
19150 		    ipif_prim,
19151 		    0,
19152 		    0,
19153 		    0,
19154 		    ire->ire_flags,
19155 		    &ire_uinfo_null,
19156 		    NULL,
19157 		    NULL,
19158 		    ipst);
19159 
19160 		if (bcast_ire != NULL) {
19161 
19162 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
19163 			    B_FALSE) == 0) {
19164 				ip2dbg(("ip_cgtp_filter_bcast_add: "
19165 				    "added bcast_ire %p\n",
19166 				    (void *)bcast_ire));
19167 
19168 				ipif_save_ire(bcast_ire->ire_ipif,
19169 				    bcast_ire);
19170 				ire_refrele(bcast_ire);
19171 			}
19172 		}
19173 		ire_refrele(ire_prim);
19174 	}
19175 }
19176 
19177 /*
19178  * IP multirouting broadcast routes handling
19179  * Remove the broadcast ire
19180  */
19181 static void
19182 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
19183 {
19184 	ire_t *ire_dst;
19185 
19186 	ASSERT(ire != NULL);
19187 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
19188 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
19189 	if (ire_dst != NULL) {
19190 		ire_t *ire_prim;
19191 
19192 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
19193 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
19194 		if (ire_prim != NULL) {
19195 			ipif_t *ipif_prim;
19196 			ire_t  *bcast_ire;
19197 
19198 			ipif_prim = ire_prim->ire_ipif;
19199 
19200 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
19201 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
19202 			    (void *)ire_dst, (void *)ire_prim,
19203 			    (void *)ipif_prim));
19204 
19205 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
19206 			    ire->ire_gateway_addr,
19207 			    IRE_BROADCAST,
19208 			    ipif_prim, ALL_ZONES,
19209 			    NULL,
19210 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
19211 			    MATCH_IRE_MASK, ipst);
19212 
19213 			if (bcast_ire != NULL) {
19214 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
19215 				    "looked up bcast_ire %p\n",
19216 				    (void *)bcast_ire));
19217 				ipif_remove_ire(bcast_ire->ire_ipif,
19218 				    bcast_ire);
19219 				ire_delete(bcast_ire);
19220 				ire_refrele(bcast_ire);
19221 			}
19222 			ire_refrele(ire_prim);
19223 		}
19224 		ire_refrele(ire_dst);
19225 	}
19226 }
19227 
19228 /*
19229  * IPsec hardware acceleration capabilities related functions.
19230  */
19231 
19232 /*
19233  * Free a per-ill IPsec capabilities structure.
19234  */
19235 static void
19236 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
19237 {
19238 	if (capab->auth_hw_algs != NULL)
19239 		kmem_free(capab->auth_hw_algs, capab->algs_size);
19240 	if (capab->encr_hw_algs != NULL)
19241 		kmem_free(capab->encr_hw_algs, capab->algs_size);
19242 	if (capab->encr_algparm != NULL)
19243 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
19244 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
19245 }
19246 
19247 /*
19248  * Allocate a new per-ill IPsec capabilities structure. This structure
19249  * is specific to an IPsec protocol (AH or ESP). It is implemented as
19250  * an array which specifies, for each algorithm, whether this algorithm
19251  * is supported by the ill or not.
19252  */
19253 static ill_ipsec_capab_t *
19254 ill_ipsec_capab_alloc(void)
19255 {
19256 	ill_ipsec_capab_t *capab;
19257 	uint_t nelems;
19258 
19259 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
19260 	if (capab == NULL)
19261 		return (NULL);
19262 
19263 	/* we need one bit per algorithm */
19264 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
19265 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
19266 
19267 	/* allocate memory to store algorithm flags */
19268 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
19269 	if (capab->encr_hw_algs == NULL)
19270 		goto nomem;
19271 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
19272 	if (capab->auth_hw_algs == NULL)
19273 		goto nomem;
19274 	/*
19275 	 * Leave encr_algparm NULL for now since we won't need it half
19276 	 * the time
19277 	 */
19278 	return (capab);
19279 
19280 nomem:
19281 	ill_ipsec_capab_free(capab);
19282 	return (NULL);
19283 }
19284 
19285 /*
19286  * Resize capability array.  Since we're exclusive, this is OK.
19287  */
19288 static boolean_t
19289 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
19290 {
19291 	ipsec_capab_algparm_t *nalp, *oalp;
19292 	uint32_t olen, nlen;
19293 
19294 	oalp = capab->encr_algparm;
19295 	olen = capab->encr_algparm_size;
19296 
19297 	if (oalp != NULL) {
19298 		if (algid < capab->encr_algparm_end)
19299 			return (B_TRUE);
19300 	}
19301 
19302 	nlen = (algid + 1) * sizeof (*nalp);
19303 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
19304 	if (nalp == NULL)
19305 		return (B_FALSE);
19306 
19307 	if (oalp != NULL) {
19308 		bcopy(oalp, nalp, olen);
19309 		kmem_free(oalp, olen);
19310 	}
19311 	capab->encr_algparm = nalp;
19312 	capab->encr_algparm_size = nlen;
19313 	capab->encr_algparm_end = algid + 1;
19314 
19315 	return (B_TRUE);
19316 }
19317 
19318 /*
19319  * Compare the capabilities of the specified ill with the protocol
19320  * and algorithms specified by the SA passed as argument.
19321  * If they match, returns B_TRUE, B_FALSE if they do not match.
19322  *
19323  * The ill can be passed as a pointer to it, or by specifying its index
19324  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
19325  *
19326  * Called by ipsec_out_is_accelerated() do decide whether an outbound
19327  * packet is eligible for hardware acceleration, and by
19328  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
19329  * to a particular ill.
19330  */
19331 boolean_t
19332 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
19333     ipsa_t *sa, netstack_t *ns)
19334 {
19335 	boolean_t sa_isv6;
19336 	uint_t algid;
19337 	struct ill_ipsec_capab_s *cpp;
19338 	boolean_t need_refrele = B_FALSE;
19339 	ip_stack_t	*ipst = ns->netstack_ip;
19340 
19341 	if (ill == NULL) {
19342 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
19343 		    NULL, NULL, NULL, ipst);
19344 		if (ill == NULL) {
19345 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
19346 			return (B_FALSE);
19347 		}
19348 		need_refrele = B_TRUE;
19349 	}
19350 
19351 	/*
19352 	 * Use the address length specified by the SA to determine
19353 	 * if it corresponds to a IPv6 address, and fail the matching
19354 	 * if the isv6 flag passed as argument does not match.
19355 	 * Note: this check is used for SADB capability checking before
19356 	 * sending SA information to an ill.
19357 	 */
19358 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
19359 	if (sa_isv6 != ill_isv6)
19360 		/* protocol mismatch */
19361 		goto done;
19362 
19363 	/*
19364 	 * Check if the ill supports the protocol, algorithm(s) and
19365 	 * key size(s) specified by the SA, and get the pointers to
19366 	 * the algorithms supported by the ill.
19367 	 */
19368 	switch (sa->ipsa_type) {
19369 
19370 	case SADB_SATYPE_ESP:
19371 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
19372 			/* ill does not support ESP acceleration */
19373 			goto done;
19374 		cpp = ill->ill_ipsec_capab_esp;
19375 		algid = sa->ipsa_auth_alg;
19376 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
19377 			goto done;
19378 		algid = sa->ipsa_encr_alg;
19379 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
19380 			goto done;
19381 		if (algid < cpp->encr_algparm_end) {
19382 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
19383 			if (sa->ipsa_encrkeybits < alp->minkeylen)
19384 				goto done;
19385 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
19386 				goto done;
19387 		}
19388 		break;
19389 
19390 	case SADB_SATYPE_AH:
19391 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
19392 			/* ill does not support AH acceleration */
19393 			goto done;
19394 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
19395 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
19396 			goto done;
19397 		break;
19398 	}
19399 
19400 	if (need_refrele)
19401 		ill_refrele(ill);
19402 	return (B_TRUE);
19403 done:
19404 	if (need_refrele)
19405 		ill_refrele(ill);
19406 	return (B_FALSE);
19407 }
19408 
19409 /*
19410  * Add a new ill to the list of IPsec capable ills.
19411  * Called from ill_capability_ipsec_ack() when an ACK was received
19412  * indicating that IPsec hardware processing was enabled for an ill.
19413  *
19414  * ill must point to the ill for which acceleration was enabled.
19415  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
19416  */
19417 static void
19418 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
19419 {
19420 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
19421 	uint_t sa_type;
19422 	uint_t ipproto;
19423 	ip_stack_t	*ipst = ill->ill_ipst;
19424 
19425 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
19426 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
19427 
19428 	switch (dl_cap) {
19429 	case DL_CAPAB_IPSEC_AH:
19430 		sa_type = SADB_SATYPE_AH;
19431 		ills = &ipst->ips_ipsec_capab_ills_ah;
19432 		ipproto = IPPROTO_AH;
19433 		break;
19434 	case DL_CAPAB_IPSEC_ESP:
19435 		sa_type = SADB_SATYPE_ESP;
19436 		ills = &ipst->ips_ipsec_capab_ills_esp;
19437 		ipproto = IPPROTO_ESP;
19438 		break;
19439 	}
19440 
19441 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
19442 
19443 	/*
19444 	 * Add ill index to list of hardware accelerators. If
19445 	 * already in list, do nothing.
19446 	 */
19447 	for (cur_ill = *ills; cur_ill != NULL &&
19448 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
19449 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
19450 		;
19451 
19452 	if (cur_ill == NULL) {
19453 		/* if this is a new entry for this ill */
19454 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
19455 		if (new_ill == NULL) {
19456 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19457 			return;
19458 		}
19459 
19460 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
19461 		new_ill->ill_isv6 = ill->ill_isv6;
19462 		new_ill->next = *ills;
19463 		*ills = new_ill;
19464 	} else if (!sadb_resync) {
19465 		/* not resync'ing SADB and an entry exists for this ill */
19466 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19467 		return;
19468 	}
19469 
19470 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19471 
19472 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
19473 		/*
19474 		 * IPsec module for protocol loaded, initiate dump
19475 		 * of the SADB to this ill.
19476 		 */
19477 		sadb_ill_download(ill, sa_type);
19478 }
19479 
19480 /*
19481  * Remove an ill from the list of IPsec capable ills.
19482  */
19483 static void
19484 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
19485 {
19486 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
19487 	ip_stack_t	*ipst = ill->ill_ipst;
19488 
19489 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
19490 	    dl_cap == DL_CAPAB_IPSEC_ESP);
19491 
19492 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
19493 	    &ipst->ips_ipsec_capab_ills_esp;
19494 
19495 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
19496 
19497 	prev_ill = NULL;
19498 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
19499 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
19500 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
19501 		;
19502 	if (cur_ill == NULL) {
19503 		/* entry not found */
19504 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19505 		return;
19506 	}
19507 	if (prev_ill == NULL) {
19508 		/* entry at front of list */
19509 		*ills = NULL;
19510 	} else {
19511 		prev_ill->next = cur_ill->next;
19512 	}
19513 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
19514 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19515 }
19516 
19517 /*
19518  * Called by SADB to send a DL_CONTROL_REQ message to every ill
19519  * supporting the specified IPsec protocol acceleration.
19520  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
19521  * We free the mblk and, if sa is non-null, release the held referece.
19522  */
19523 void
19524 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
19525     netstack_t *ns)
19526 {
19527 	ipsec_capab_ill_t *ici, *cur_ici;
19528 	ill_t *ill;
19529 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
19530 	ip_stack_t	*ipst = ns->netstack_ip;
19531 
19532 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
19533 	    ipst->ips_ipsec_capab_ills_esp;
19534 
19535 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
19536 
19537 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
19538 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
19539 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
19540 
19541 		/*
19542 		 * Handle the case where the ill goes away while the SADB is
19543 		 * attempting to send messages.  If it's going away, it's
19544 		 * nuking its shadow SADB, so we don't care..
19545 		 */
19546 
19547 		if (ill == NULL)
19548 			continue;
19549 
19550 		if (sa != NULL) {
19551 			/*
19552 			 * Make sure capabilities match before
19553 			 * sending SA to ill.
19554 			 */
19555 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
19556 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
19557 				ill_refrele(ill);
19558 				continue;
19559 			}
19560 
19561 			mutex_enter(&sa->ipsa_lock);
19562 			sa->ipsa_flags |= IPSA_F_HW;
19563 			mutex_exit(&sa->ipsa_lock);
19564 		}
19565 
19566 		/*
19567 		 * Copy template message, and add it to the front
19568 		 * of the mblk ship list. We want to avoid holding
19569 		 * the ipsec_capab_ills_lock while sending the
19570 		 * message to the ills.
19571 		 *
19572 		 * The b_next and b_prev are temporarily used
19573 		 * to build a list of mblks to be sent down, and to
19574 		 * save the ill to which they must be sent.
19575 		 */
19576 		nmp = copymsg(mp);
19577 		if (nmp == NULL) {
19578 			ill_refrele(ill);
19579 			continue;
19580 		}
19581 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
19582 		nmp->b_next = mp_ship_list;
19583 		mp_ship_list = nmp;
19584 		nmp->b_prev = (mblk_t *)ill;
19585 	}
19586 
19587 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19588 
19589 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
19590 		/* restore the mblk to a sane state */
19591 		next_mp = nmp->b_next;
19592 		nmp->b_next = NULL;
19593 		ill = (ill_t *)nmp->b_prev;
19594 		nmp->b_prev = NULL;
19595 
19596 		ill_dlpi_send(ill, nmp);
19597 		ill_refrele(ill);
19598 	}
19599 
19600 	if (sa != NULL)
19601 		IPSA_REFRELE(sa);
19602 	freemsg(mp);
19603 }
19604 
19605 /*
19606  * Derive an interface id from the link layer address.
19607  * Knows about IEEE 802 and IEEE EUI-64 mappings.
19608  */
19609 static boolean_t
19610 ip_ether_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19611 {
19612 	char		*addr;
19613 
19614 	if (ill->ill_phys_addr_length != ETHERADDRL)
19615 		return (B_FALSE);
19616 
19617 	/* Form EUI-64 like address */
19618 	addr = (char *)&v6addr->s6_addr32[2];
19619 	bcopy(ill->ill_phys_addr, addr, 3);
19620 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
19621 	addr[3] = (char)0xff;
19622 	addr[4] = (char)0xfe;
19623 	bcopy(ill->ill_phys_addr + 3, addr + 5, 3);
19624 	return (B_TRUE);
19625 }
19626 
19627 /* ARGSUSED */
19628 static boolean_t
19629 ip_nodef_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19630 {
19631 	return (B_FALSE);
19632 }
19633 
19634 typedef struct ipmp_ifcookie {
19635 	uint32_t	ic_hostid;
19636 	char		ic_ifname[LIFNAMSIZ];
19637 	char		ic_zonename[ZONENAME_MAX];
19638 } ipmp_ifcookie_t;
19639 
19640 /*
19641  * Construct a pseudo-random interface ID for the IPMP interface that's both
19642  * predictable and (almost) guaranteed to be unique.
19643  */
19644 static boolean_t
19645 ip_ipmp_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19646 {
19647 	zone_t		*zp;
19648 	uint8_t		*addr;
19649 	uchar_t		hash[16];
19650 	ulong_t 	hostid;
19651 	MD5_CTX		ctx;
19652 	ipmp_ifcookie_t	ic = { 0 };
19653 
19654 	ASSERT(IS_IPMP(ill));
19655 
19656 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
19657 	ic.ic_hostid = htonl((uint32_t)hostid);
19658 
19659 	(void) strlcpy(ic.ic_ifname, ill->ill_name, LIFNAMSIZ);
19660 
19661 	if ((zp = zone_find_by_id(ill->ill_zoneid)) != NULL) {
19662 		(void) strlcpy(ic.ic_zonename, zp->zone_name, ZONENAME_MAX);
19663 		zone_rele(zp);
19664 	}
19665 
19666 	MD5Init(&ctx);
19667 	MD5Update(&ctx, &ic, sizeof (ic));
19668 	MD5Final(hash, &ctx);
19669 
19670 	/*
19671 	 * Map the hash to an interface ID per the basic approach in RFC3041.
19672 	 */
19673 	addr = &v6addr->s6_addr8[8];
19674 	bcopy(hash + 8, addr, sizeof (uint64_t));
19675 	addr[0] &= ~0x2;				/* set local bit */
19676 
19677 	return (B_TRUE);
19678 }
19679 
19680 /* ARGSUSED */
19681 static boolean_t
19682 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
19683     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
19684 {
19685 	/*
19686 	 * Multicast address mappings used over Ethernet/802.X.
19687 	 * This address is used as a base for mappings.
19688 	 */
19689 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
19690 	    0x00, 0x00, 0x00};
19691 
19692 	/*
19693 	 * Extract low order 32 bits from IPv6 multicast address.
19694 	 * Or that into the link layer address, starting from the
19695 	 * second byte.
19696 	 */
19697 	*hw_start = 2;
19698 	v6_extract_mask->s6_addr32[0] = 0;
19699 	v6_extract_mask->s6_addr32[1] = 0;
19700 	v6_extract_mask->s6_addr32[2] = 0;
19701 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
19702 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
19703 	return (B_TRUE);
19704 }
19705 
19706 /*
19707  * Indicate by return value whether multicast is supported. If not,
19708  * this code should not touch/change any parameters.
19709  */
19710 /* ARGSUSED */
19711 static boolean_t
19712 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
19713     uint32_t *hw_start, ipaddr_t *extract_mask)
19714 {
19715 	/*
19716 	 * Multicast address mappings used over Ethernet/802.X.
19717 	 * This address is used as a base for mappings.
19718 	 */
19719 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
19720 	    0x00, 0x00, 0x00 };
19721 
19722 	if (phys_length != ETHERADDRL)
19723 		return (B_FALSE);
19724 
19725 	*extract_mask = htonl(0x007fffff);
19726 	*hw_start = 2;
19727 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
19728 	return (B_TRUE);
19729 }
19730 
19731 /*
19732  * Derive IPoIB interface id from the link layer address.
19733  */
19734 static boolean_t
19735 ip_ib_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19736 {
19737 	char		*addr;
19738 
19739 	if (ill->ill_phys_addr_length != 20)
19740 		return (B_FALSE);
19741 	addr = (char *)&v6addr->s6_addr32[2];
19742 	bcopy(ill->ill_phys_addr + 12, addr, 8);
19743 	/*
19744 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
19745 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
19746 	 * rules. In these cases, the IBA considers these GUIDs to be in
19747 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
19748 	 * required; vendors are required not to assign global EUI-64's
19749 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
19750 	 * of the interface identifier. Whether the GUID is in modified
19751 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
19752 	 * bit set to 1.
19753 	 */
19754 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
19755 	return (B_TRUE);
19756 }
19757 
19758 /*
19759  * Note on mapping from multicast IP addresses to IPoIB multicast link
19760  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
19761  * The format of an IPoIB multicast address is:
19762  *
19763  *  4 byte QPN      Scope Sign.  Pkey
19764  * +--------------------------------------------+
19765  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
19766  * +--------------------------------------------+
19767  *
19768  * The Scope and Pkey components are properties of the IBA port and
19769  * network interface. They can be ascertained from the broadcast address.
19770  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
19771  */
19772 
19773 static boolean_t
19774 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
19775     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
19776 {
19777 	/*
19778 	 * Base IPoIB IPv6 multicast address used for mappings.
19779 	 * Does not contain the IBA scope/Pkey values.
19780 	 */
19781 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
19782 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
19783 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
19784 
19785 	/*
19786 	 * Extract low order 80 bits from IPv6 multicast address.
19787 	 * Or that into the link layer address, starting from the
19788 	 * sixth byte.
19789 	 */
19790 	*hw_start = 6;
19791 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
19792 
19793 	/*
19794 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
19795 	 */
19796 	*(maddr + 5) = *(bphys_addr + 5);
19797 	*(maddr + 8) = *(bphys_addr + 8);
19798 	*(maddr + 9) = *(bphys_addr + 9);
19799 
19800 	v6_extract_mask->s6_addr32[0] = 0;
19801 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
19802 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
19803 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
19804 	return (B_TRUE);
19805 }
19806 
19807 static boolean_t
19808 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
19809     uint32_t *hw_start, ipaddr_t *extract_mask)
19810 {
19811 	/*
19812 	 * Base IPoIB IPv4 multicast address used for mappings.
19813 	 * Does not contain the IBA scope/Pkey values.
19814 	 */
19815 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
19816 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
19817 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
19818 
19819 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
19820 		return (B_FALSE);
19821 
19822 	/*
19823 	 * Extract low order 28 bits from IPv4 multicast address.
19824 	 * Or that into the link layer address, starting from the
19825 	 * sixteenth byte.
19826 	 */
19827 	*extract_mask = htonl(0x0fffffff);
19828 	*hw_start = 16;
19829 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
19830 
19831 	/*
19832 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
19833 	 */
19834 	*(maddr + 5) = *(bphys_addr + 5);
19835 	*(maddr + 8) = *(bphys_addr + 8);
19836 	*(maddr + 9) = *(bphys_addr + 9);
19837 	return (B_TRUE);
19838 }
19839 
19840 /*
19841  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
19842  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
19843  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
19844  * the link-local address is preferred.
19845  */
19846 boolean_t
19847 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
19848 {
19849 	ipif_t	*ipif;
19850 	ipif_t	*maybe_ipif = NULL;
19851 
19852 	mutex_enter(&ill->ill_lock);
19853 	if (ill->ill_state_flags & ILL_CONDEMNED) {
19854 		mutex_exit(&ill->ill_lock);
19855 		if (ipifp != NULL)
19856 			*ipifp = NULL;
19857 		return (B_FALSE);
19858 	}
19859 
19860 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19861 		if (!IPIF_CAN_LOOKUP(ipif))
19862 			continue;
19863 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
19864 		    ipif->ipif_zoneid != ALL_ZONES)
19865 			continue;
19866 		if ((ipif->ipif_flags & flags) != flags)
19867 			continue;
19868 
19869 		if (ipifp == NULL) {
19870 			mutex_exit(&ill->ill_lock);
19871 			ASSERT(maybe_ipif == NULL);
19872 			return (B_TRUE);
19873 		}
19874 		if (!ill->ill_isv6 ||
19875 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
19876 			ipif_refhold_locked(ipif);
19877 			mutex_exit(&ill->ill_lock);
19878 			*ipifp = ipif;
19879 			return (B_TRUE);
19880 		}
19881 		if (maybe_ipif == NULL)
19882 			maybe_ipif = ipif;
19883 	}
19884 	if (ipifp != NULL) {
19885 		if (maybe_ipif != NULL)
19886 			ipif_refhold_locked(maybe_ipif);
19887 		*ipifp = maybe_ipif;
19888 	}
19889 	mutex_exit(&ill->ill_lock);
19890 	return (maybe_ipif != NULL);
19891 }
19892 
19893 /*
19894  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
19895  * If a pointer to an ipif_t is returned then the caller will need to do
19896  * an ill_refrele().
19897  */
19898 ipif_t *
19899 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
19900     ip_stack_t *ipst)
19901 {
19902 	ipif_t *ipif;
19903 	ill_t *ill;
19904 
19905 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
19906 	    ipst);
19907 	if (ill == NULL)
19908 		return (NULL);
19909 
19910 	mutex_enter(&ill->ill_lock);
19911 	if (ill->ill_state_flags & ILL_CONDEMNED) {
19912 		mutex_exit(&ill->ill_lock);
19913 		ill_refrele(ill);
19914 		return (NULL);
19915 	}
19916 
19917 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19918 		if (!IPIF_CAN_LOOKUP(ipif))
19919 			continue;
19920 		if (lifidx == ipif->ipif_id) {
19921 			ipif_refhold_locked(ipif);
19922 			break;
19923 		}
19924 	}
19925 
19926 	mutex_exit(&ill->ill_lock);
19927 	ill_refrele(ill);
19928 	return (ipif);
19929 }
19930 
19931 /*
19932  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
19933  * There is one exceptions IRE_BROADCAST are difficult to recreate,
19934  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
19935  * for details.
19936  */
19937 void
19938 ill_fastpath_flush(ill_t *ill)
19939 {
19940 	ip_stack_t *ipst = ill->ill_ipst;
19941 
19942 	nce_fastpath_list_dispatch(ill, NULL, NULL);
19943 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
19944 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
19945 }
19946 
19947 /*
19948  * Set the physical address information for `ill' to the contents of the
19949  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
19950  * asynchronous if `ill' cannot immediately be quiesced -- in which case
19951  * EINPROGRESS will be returned.
19952  */
19953 int
19954 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
19955 {
19956 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
19957 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
19958 
19959 	ASSERT(IAM_WRITER_IPSQ(ipsq));
19960 
19961 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
19962 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
19963 		/* Changing DL_IPV6_TOKEN is not yet supported */
19964 		return (0);
19965 	}
19966 
19967 	/*
19968 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
19969 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
19970 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
19971 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
19972 	 */
19973 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
19974 		freemsg(mp);
19975 		return (ENOMEM);
19976 	}
19977 
19978 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
19979 
19980 	/*
19981 	 * If we can quiesce the ill, then set the address.  If not, then
19982 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
19983 	 */
19984 	ill_down_ipifs(ill);
19985 	mutex_enter(&ill->ill_lock);
19986 	if (!ill_is_quiescent(ill)) {
19987 		/* call cannot fail since `conn_t *' argument is NULL */
19988 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
19989 		    mp, ILL_DOWN);
19990 		mutex_exit(&ill->ill_lock);
19991 		return (EINPROGRESS);
19992 	}
19993 	mutex_exit(&ill->ill_lock);
19994 
19995 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
19996 	return (0);
19997 }
19998 
19999 /*
20000  * Once the ill associated with `q' has quiesced, set its physical address
20001  * information to the values in `addrmp'.  Note that two copies of `addrmp'
20002  * are passed (linked by b_cont), since we sometimes need to save two distinct
20003  * copies in the ill_t, and our context doesn't permit sleeping or allocation
20004  * failure (we'll free the other copy if it's not needed).  Since the ill_t
20005  * is quiesced, we know any stale IREs with the old address information have
20006  * already been removed, so we don't need to call ill_fastpath_flush().
20007  */
20008 /* ARGSUSED */
20009 static void
20010 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
20011 {
20012 	ill_t		*ill = q->q_ptr;
20013 	mblk_t		*addrmp2 = unlinkb(addrmp);
20014 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
20015 	uint_t		addrlen, addroff;
20016 
20017 	ASSERT(IAM_WRITER_IPSQ(ipsq));
20018 
20019 	addroff	= dlindp->dl_addr_offset;
20020 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
20021 
20022 	switch (dlindp->dl_data) {
20023 	case DL_IPV6_LINK_LAYER_ADDR:
20024 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
20025 		freemsg(addrmp2);
20026 		break;
20027 
20028 	case DL_CURR_PHYS_ADDR:
20029 		freemsg(ill->ill_phys_addr_mp);
20030 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
20031 		ill->ill_phys_addr_mp = addrmp;
20032 		ill->ill_phys_addr_length = addrlen;
20033 
20034 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
20035 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
20036 		else
20037 			freemsg(addrmp2);
20038 		break;
20039 	default:
20040 		ASSERT(0);
20041 	}
20042 
20043 	/*
20044 	 * If there are ipifs to bring up, ill_up_ipifs() will return
20045 	 * EINPROGRESS, and ipsq_current_finish() will be called by
20046 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
20047 	 * brought up.
20048 	 */
20049 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
20050 		ipsq_current_finish(ipsq);
20051 }
20052 
20053 /*
20054  * Helper routine for setting the ill_nd_lla fields.
20055  */
20056 void
20057 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
20058 {
20059 	freemsg(ill->ill_nd_lla_mp);
20060 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
20061 	ill->ill_nd_lla_mp = ndmp;
20062 	ill->ill_nd_lla_len = addrlen;
20063 }
20064 
20065 major_t IP_MAJ;
20066 #define	IP	"ip"
20067 
20068 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
20069 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
20070 
20071 /*
20072  * Issue REMOVEIF ioctls to have the loopback interfaces
20073  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
20074  * the former going away when the user-level processes in the zone
20075  * are killed  * and the latter are cleaned up by the stream head
20076  * str_stack_shutdown callback that undoes all I_PLINKs.
20077  */
20078 void
20079 ip_loopback_cleanup(ip_stack_t *ipst)
20080 {
20081 	int error;
20082 	ldi_handle_t	lh = NULL;
20083 	ldi_ident_t	li = NULL;
20084 	int		rval;
20085 	cred_t		*cr;
20086 	struct strioctl iocb;
20087 	struct lifreq	lifreq;
20088 
20089 	IP_MAJ = ddi_name_to_major(IP);
20090 
20091 #ifdef NS_DEBUG
20092 	(void) printf("ip_loopback_cleanup() stackid %d\n",
20093 	    ipst->ips_netstack->netstack_stackid);
20094 #endif
20095 
20096 	bzero(&lifreq, sizeof (lifreq));
20097 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
20098 
20099 	error = ldi_ident_from_major(IP_MAJ, &li);
20100 	if (error) {
20101 #ifdef DEBUG
20102 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
20103 		    error);
20104 #endif
20105 		return;
20106 	}
20107 
20108 	cr = zone_get_kcred(netstackid_to_zoneid(
20109 	    ipst->ips_netstack->netstack_stackid));
20110 	ASSERT(cr != NULL);
20111 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
20112 	if (error) {
20113 #ifdef DEBUG
20114 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
20115 		    error);
20116 #endif
20117 		goto out;
20118 	}
20119 	iocb.ic_cmd = SIOCLIFREMOVEIF;
20120 	iocb.ic_timout = 15;
20121 	iocb.ic_len = sizeof (lifreq);
20122 	iocb.ic_dp = (char *)&lifreq;
20123 
20124 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
20125 	/* LINTED - statement has no consequent */
20126 	if (error) {
20127 #ifdef NS_DEBUG
20128 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
20129 		    "UDP6 error %d\n", error);
20130 #endif
20131 	}
20132 	(void) ldi_close(lh, FREAD|FWRITE, cr);
20133 	lh = NULL;
20134 
20135 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
20136 	if (error) {
20137 #ifdef NS_DEBUG
20138 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
20139 		    error);
20140 #endif
20141 		goto out;
20142 	}
20143 
20144 	iocb.ic_cmd = SIOCLIFREMOVEIF;
20145 	iocb.ic_timout = 15;
20146 	iocb.ic_len = sizeof (lifreq);
20147 	iocb.ic_dp = (char *)&lifreq;
20148 
20149 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
20150 	/* LINTED - statement has no consequent */
20151 	if (error) {
20152 #ifdef NS_DEBUG
20153 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
20154 		    "UDP error %d\n", error);
20155 #endif
20156 	}
20157 	(void) ldi_close(lh, FREAD|FWRITE, cr);
20158 	lh = NULL;
20159 
20160 out:
20161 	/* Close layered handles */
20162 	if (lh)
20163 		(void) ldi_close(lh, FREAD|FWRITE, cr);
20164 	if (li)
20165 		ldi_ident_release(li);
20166 
20167 	crfree(cr);
20168 }
20169 
20170 /*
20171  * This needs to be in-sync with nic_event_t definition
20172  */
20173 static const char *
20174 ill_hook_event2str(nic_event_t event)
20175 {
20176 	switch (event) {
20177 	case NE_PLUMB:
20178 		return ("PLUMB");
20179 	case NE_UNPLUMB:
20180 		return ("UNPLUMB");
20181 	case NE_UP:
20182 		return ("UP");
20183 	case NE_DOWN:
20184 		return ("DOWN");
20185 	case NE_ADDRESS_CHANGE:
20186 		return ("ADDRESS_CHANGE");
20187 	case NE_LIF_UP:
20188 		return ("LIF_UP");
20189 	case NE_LIF_DOWN:
20190 		return ("LIF_DOWN");
20191 	default:
20192 		return ("UNKNOWN");
20193 	}
20194 }
20195 
20196 void
20197 ill_nic_event_dispatch(ill_t *ill, lif_if_t lif, nic_event_t event,
20198     nic_event_data_t data, size_t datalen)
20199 {
20200 	ip_stack_t		*ipst = ill->ill_ipst;
20201 	hook_nic_event_int_t	*info;
20202 	const char		*str = NULL;
20203 
20204 	/* create a new nic event info */
20205 	if ((info = kmem_alloc(sizeof (*info), KM_NOSLEEP)) == NULL)
20206 		goto fail;
20207 
20208 	info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
20209 	info->hnei_event.hne_lif = lif;
20210 	info->hnei_event.hne_event = event;
20211 	info->hnei_event.hne_protocol = ill->ill_isv6 ?
20212 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
20213 	info->hnei_event.hne_data = NULL;
20214 	info->hnei_event.hne_datalen = 0;
20215 	info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
20216 
20217 	if (data != NULL && datalen != 0) {
20218 		info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
20219 		if (info->hnei_event.hne_data == NULL)
20220 			goto fail;
20221 		bcopy(data, info->hnei_event.hne_data, datalen);
20222 		info->hnei_event.hne_datalen = datalen;
20223 	}
20224 
20225 	if (ddi_taskq_dispatch(eventq_queue_nic, ip_ne_queue_func, info,
20226 	    DDI_NOSLEEP) == DDI_SUCCESS)
20227 		return;
20228 
20229 fail:
20230 	if (info != NULL) {
20231 		if (info->hnei_event.hne_data != NULL) {
20232 			kmem_free(info->hnei_event.hne_data,
20233 			    info->hnei_event.hne_datalen);
20234 		}
20235 		kmem_free(info, sizeof (hook_nic_event_t));
20236 	}
20237 	str = ill_hook_event2str(event);
20238 	ip2dbg(("ill_nic_event_dispatch: could not dispatch %s nic event "
20239 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
20240 }
20241 
20242 void
20243 ipif_up_notify(ipif_t *ipif)
20244 {
20245 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
20246 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif, RTSQ_DEFAULT);
20247 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
20248 	ill_nic_event_dispatch(ipif->ipif_ill, MAP_IPIF_ID(ipif->ipif_id),
20249 	    NE_LIF_UP, NULL, 0);
20250 }
20251