xref: /titanic_51/usr/src/uts/common/inet/ip/ip_if.c (revision 25540de2e9a31b620c68d87ce2f3bbe8a196ad93)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 /*
28  * This file contains the interface control functions for IP.
29  */
30 
31 #include <sys/types.h>
32 #include <sys/stream.h>
33 #include <sys/dlpi.h>
34 #include <sys/stropts.h>
35 #include <sys/strsun.h>
36 #include <sys/sysmacros.h>
37 #include <sys/strlog.h>
38 #include <sys/ddi.h>
39 #include <sys/sunddi.h>
40 #include <sys/cmn_err.h>
41 #include <sys/kstat.h>
42 #include <sys/debug.h>
43 #include <sys/zone.h>
44 #include <sys/sunldi.h>
45 #include <sys/file.h>
46 #include <sys/bitmap.h>
47 #include <sys/cpuvar.h>
48 #include <sys/time.h>
49 #include <sys/ctype.h>
50 #include <sys/kmem.h>
51 #include <sys/systm.h>
52 #include <sys/param.h>
53 #include <sys/socket.h>
54 #include <sys/isa_defs.h>
55 #include <net/if.h>
56 #include <net/if_arp.h>
57 #include <net/if_types.h>
58 #include <net/if_dl.h>
59 #include <net/route.h>
60 #include <sys/sockio.h>
61 #include <netinet/in.h>
62 #include <netinet/ip6.h>
63 #include <netinet/icmp6.h>
64 #include <netinet/igmp_var.h>
65 #include <sys/policy.h>
66 #include <sys/ethernet.h>
67 #include <sys/callb.h>
68 #include <sys/md5.h>
69 
70 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
71 #include <inet/mi.h>
72 #include <inet/nd.h>
73 #include <inet/arp.h>
74 #include <inet/mib2.h>
75 #include <inet/ip.h>
76 #include <inet/ip6.h>
77 #include <inet/ip6_asp.h>
78 #include <inet/tcp.h>
79 #include <inet/ip_multi.h>
80 #include <inet/ip_ire.h>
81 #include <inet/ip_ftable.h>
82 #include <inet/ip_rts.h>
83 #include <inet/ip_ndp.h>
84 #include <inet/ip_if.h>
85 #include <inet/ip_impl.h>
86 #include <inet/tun.h>
87 #include <inet/sctp_ip.h>
88 #include <inet/ip_netinfo.h>
89 
90 #include <net/pfkeyv2.h>
91 #include <inet/ipsec_info.h>
92 #include <inet/sadb.h>
93 #include <inet/ipsec_impl.h>
94 #include <sys/iphada.h>
95 
96 #include <netinet/igmp.h>
97 #include <inet/ip_listutils.h>
98 #include <inet/ipclassifier.h>
99 #include <sys/mac_client.h>
100 #include <sys/dld.h>
101 
102 #include <sys/systeminfo.h>
103 #include <sys/bootconf.h>
104 
105 #include <sys/tsol/tndb.h>
106 #include <sys/tsol/tnet.h>
107 
108 /* The character which tells where the ill_name ends */
109 #define	IPIF_SEPARATOR_CHAR	':'
110 
111 /* IP ioctl function table entry */
112 typedef struct ipft_s {
113 	int	ipft_cmd;
114 	pfi_t	ipft_pfi;
115 	int	ipft_min_size;
116 	int	ipft_flags;
117 } ipft_t;
118 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
119 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
120 
121 typedef struct ip_sock_ar_s {
122 	union {
123 		area_t	ip_sock_area;
124 		ared_t	ip_sock_ared;
125 		areq_t	ip_sock_areq;
126 	} ip_sock_ar_u;
127 	queue_t	*ip_sock_ar_q;
128 } ip_sock_ar_t;
129 
130 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
131 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
132 		    char *value, caddr_t cp, cred_t *ioc_cr);
133 
134 static boolean_t ill_is_quiescent(ill_t *);
135 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
136 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
137 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
138     mblk_t *mp, boolean_t need_up);
139 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
140     mblk_t *mp, boolean_t need_up);
141 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
142     queue_t *q, mblk_t *mp, boolean_t need_up);
143 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
144     mblk_t *mp);
145 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
146     mblk_t *mp);
147 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
148     queue_t *q, mblk_t *mp, boolean_t need_up);
149 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
150     int ioccmd, struct linkblk *li, boolean_t doconsist);
151 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
152 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
153 static void	ipsq_flush(ill_t *ill);
154 
155 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
156     queue_t *q, mblk_t *mp, boolean_t need_up);
157 static void	ipsq_delete(ipsq_t *);
158 
159 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
160     boolean_t initialize, boolean_t insert);
161 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
162 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
163 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
164 		    boolean_t isv6);
165 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
166 static void	ipif_delete_cache_ire(ire_t *, char *);
167 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
168 static void	ipif_free(ipif_t *ipif);
169 static void	ipif_free_tail(ipif_t *ipif);
170 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
171 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
172 static void	ipif_set_default(ipif_t *ipif);
173 static int	ipif_set_values(queue_t *q, mblk_t *mp,
174     char *interf_name, uint_t *ppa);
175 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
176     queue_t *q);
177 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
178     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
179     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *);
180 static void	ipif_update_other_ipifs(ipif_t *old_ipif);
181 
182 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
183 static int	ill_arp_off(ill_t *ill);
184 static int	ill_arp_on(ill_t *ill);
185 static void	ill_delete_interface_type(ill_if_t *);
186 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
187 static void	ill_dl_down(ill_t *ill);
188 static void	ill_down(ill_t *ill);
189 static void	ill_downi(ire_t *ire, char *ill_arg);
190 static void	ill_free_mib(ill_t *ill);
191 static void	ill_glist_delete(ill_t *);
192 static void	ill_phyint_reinit(ill_t *ill);
193 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
194 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
195 static ip_v6intfid_func_t ip_ether_v6intfid, ip_ib_v6intfid;
196 static ip_v6intfid_func_t ip_ipmp_v6intfid, ip_nodef_v6intfid;
197 static ip_v6mapinfo_func_t ip_ether_v6mapinfo, ip_ib_v6mapinfo;
198 static ip_v4mapinfo_func_t ip_ether_v4mapinfo, ip_ib_v4mapinfo;
199 static void	ipif_save_ire(ipif_t *, ire_t *);
200 static void	ipif_remove_ire(ipif_t *, ire_t *);
201 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *, ip_stack_t *);
202 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
203 static void	phyint_free(phyint_t *);
204 
205 /*
206  * Per-ill IPsec capabilities management.
207  */
208 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
209 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
210 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
211 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
212 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
213 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
214     boolean_t);
215 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
216 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
217 static void ill_capability_mdt_reset_fill(ill_t *, mblk_t *);
218 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
219 static void ill_capability_ipsec_reset_fill(ill_t *, mblk_t *);
220 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
221 static void ill_capability_hcksum_reset_fill(ill_t *, mblk_t *);
222 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
223     dl_capability_sub_t *);
224 static void ill_capability_zerocopy_reset_fill(ill_t *, mblk_t *);
225 static int  ill_capability_ipsec_reset_size(ill_t *, int *, int *, int *,
226     int *);
227 static void	ill_capability_dld_reset_fill(ill_t *, mblk_t *);
228 static void	ill_capability_dld_ack(ill_t *, mblk_t *,
229 		    dl_capability_sub_t *);
230 static void	ill_capability_dld_enable(ill_t *);
231 static void	ill_capability_ack_thr(void *);
232 static void	ill_capability_lso_enable(ill_t *);
233 static void	ill_capability_send(ill_t *, mblk_t *);
234 
235 static ill_t	*ill_prev_usesrc(ill_t *);
236 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
237 static void	ill_disband_usesrc_group(ill_t *);
238 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
239 
240 #ifdef DEBUG
241 static  void    ill_trace_cleanup(const ill_t *);
242 static  void    ipif_trace_cleanup(const ipif_t *);
243 #endif
244 
245 /*
246  * if we go over the memory footprint limit more than once in this msec
247  * interval, we'll start pruning aggressively.
248  */
249 int ip_min_frag_prune_time = 0;
250 
251 /*
252  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
253  * and the IPsec DOI
254  */
255 #define	MAX_IPSEC_ALGS	256
256 
257 #define	BITSPERBYTE	8
258 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
259 
260 #define	IPSEC_ALG_ENABLE(algs, algid) \
261 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
262 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
263 
264 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
265 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
266 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
267 
268 typedef uint8_t ipsec_capab_elem_t;
269 
270 /*
271  * Per-algorithm parameters.  Note that at present, only encryption
272  * algorithms have variable keysize (IKE does not provide a way to negotiate
273  * auth algorithm keysize).
274  *
275  * All sizes here are in bits.
276  */
277 typedef struct
278 {
279 	uint16_t	minkeylen;
280 	uint16_t	maxkeylen;
281 } ipsec_capab_algparm_t;
282 
283 /*
284  * Per-ill capabilities.
285  */
286 struct ill_ipsec_capab_s {
287 	ipsec_capab_elem_t *encr_hw_algs;
288 	ipsec_capab_elem_t *auth_hw_algs;
289 	uint32_t algs_size;	/* size of _hw_algs in bytes */
290 	/* algorithm key lengths */
291 	ipsec_capab_algparm_t *encr_algparm;
292 	uint32_t encr_algparm_size;
293 	uint32_t encr_algparm_end;
294 };
295 
296 /*
297  * The field values are larger than strictly necessary for simple
298  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
299  */
300 static area_t	ip_area_template = {
301 	AR_ENTRY_ADD,			/* area_cmd */
302 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
303 					/* area_name_offset */
304 	/* area_name_length temporarily holds this structure length */
305 	sizeof (area_t),			/* area_name_length */
306 	IP_ARP_PROTO_TYPE,		/* area_proto */
307 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
308 	IP_ADDR_LEN,			/* area_proto_addr_length */
309 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
310 					/* area_proto_mask_offset */
311 	0,				/* area_flags */
312 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
313 					/* area_hw_addr_offset */
314 	/* Zero length hw_addr_length means 'use your idea of the address' */
315 	0				/* area_hw_addr_length */
316 };
317 
318 /*
319  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
320  * support
321  */
322 static area_t	ip6_area_template = {
323 	AR_ENTRY_ADD,			/* area_cmd */
324 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
325 					/* area_name_offset */
326 	/* area_name_length temporarily holds this structure length */
327 	sizeof (area_t),			/* area_name_length */
328 	IP_ARP_PROTO_TYPE,		/* area_proto */
329 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
330 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
331 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
332 					/* area_proto_mask_offset */
333 	0,				/* area_flags */
334 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
335 					/* area_hw_addr_offset */
336 	/* Zero length hw_addr_length means 'use your idea of the address' */
337 	0				/* area_hw_addr_length */
338 };
339 
340 static ared_t	ip_ared_template = {
341 	AR_ENTRY_DELETE,
342 	sizeof (ared_t) + IP_ADDR_LEN,
343 	sizeof (ared_t),
344 	IP_ARP_PROTO_TYPE,
345 	sizeof (ared_t),
346 	IP_ADDR_LEN,
347 	0
348 };
349 
350 static ared_t	ip6_ared_template = {
351 	AR_ENTRY_DELETE,
352 	sizeof (ared_t) + IPV6_ADDR_LEN,
353 	sizeof (ared_t),
354 	IP_ARP_PROTO_TYPE,
355 	sizeof (ared_t),
356 	IPV6_ADDR_LEN,
357 	0
358 };
359 
360 /*
361  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
362  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
363  * areq is used).
364  */
365 static areq_t	ip_areq_template = {
366 	AR_ENTRY_QUERY,			/* cmd */
367 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
368 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
369 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
370 	sizeof (areq_t),			/* target addr offset */
371 	IP_ADDR_LEN,			/* target addr_length */
372 	0,				/* flags */
373 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
374 	IP_ADDR_LEN,			/* sender addr length */
375 	AR_EQ_DEFAULT_XMIT_COUNT,	/* xmit_count */
376 	AR_EQ_DEFAULT_XMIT_INTERVAL,	/* (re)xmit_interval in milliseconds */
377 	AR_EQ_DEFAULT_MAX_BUFFERED	/* max # of requests to buffer */
378 	/* anything else filled in by the code */
379 };
380 
381 static arc_t	ip_aru_template = {
382 	AR_INTERFACE_UP,
383 	sizeof (arc_t),		/* Name offset */
384 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
385 };
386 
387 static arc_t	ip_ard_template = {
388 	AR_INTERFACE_DOWN,
389 	sizeof (arc_t),		/* Name offset */
390 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
391 };
392 
393 static arc_t	ip_aron_template = {
394 	AR_INTERFACE_ON,
395 	sizeof (arc_t),		/* Name offset */
396 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
397 };
398 
399 static arc_t	ip_aroff_template = {
400 	AR_INTERFACE_OFF,
401 	sizeof (arc_t),		/* Name offset */
402 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
403 };
404 
405 static arma_t	ip_arma_multi_template = {
406 	AR_MAPPING_ADD,
407 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
408 				/* Name offset */
409 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
410 	IP_ARP_PROTO_TYPE,
411 	sizeof (arma_t),			/* proto_addr_offset */
412 	IP_ADDR_LEN,				/* proto_addr_length */
413 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
414 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
415 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
416 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
417 	IP_MAX_HW_LEN,				/* hw_addr_length */
418 	0,					/* hw_mapping_start */
419 };
420 
421 static ipft_t	ip_ioctl_ftbl[] = {
422 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
423 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
424 		IPFT_F_NO_REPLY },
425 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
426 		IPFT_F_NO_REPLY },
427 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
428 	{ 0 }
429 };
430 
431 /* Simple ICMP IP Header Template */
432 static ipha_t icmp_ipha = {
433 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
434 };
435 
436 /* Flag descriptors for ip_ipif_report */
437 static nv_t	ipif_nv_tbl[] = {
438 	{ IPIF_UP,		"UP" },
439 	{ IPIF_BROADCAST,	"BROADCAST" },
440 	{ ILLF_DEBUG,		"DEBUG" },
441 	{ PHYI_LOOPBACK,	"LOOPBACK" },
442 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
443 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
444 	{ PHYI_RUNNING,		"RUNNING" },
445 	{ ILLF_NOARP,		"NOARP" },
446 	{ PHYI_PROMISC,		"PROMISC" },
447 	{ PHYI_ALLMULTI,	"ALLMULTI" },
448 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
449 	{ ILLF_MULTICAST,	"MULTICAST" },
450 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
451 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
452 	{ IPIF_DHCPRUNNING,	"DHCP" },
453 	{ IPIF_PRIVATE,		"PRIVATE" },
454 	{ IPIF_NOXMIT,		"NOXMIT" },
455 	{ IPIF_NOLOCAL,		"NOLOCAL" },
456 	{ IPIF_DEPRECATED,	"DEPRECATED" },
457 	{ IPIF_PREFERRED,	"PREFERRED" },
458 	{ IPIF_TEMPORARY,	"TEMPORARY" },
459 	{ IPIF_ADDRCONF,	"ADDRCONF" },
460 	{ PHYI_VIRTUAL,		"VIRTUAL" },
461 	{ ILLF_ROUTER,		"ROUTER" },
462 	{ ILLF_NONUD,		"NONUD" },
463 	{ IPIF_ANYCAST,		"ANYCAST" },
464 	{ ILLF_NORTEXCH,	"NORTEXCH" },
465 	{ ILLF_IPV4,		"IPV4" },
466 	{ ILLF_IPV6,		"IPV6" },
467 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
468 	{ PHYI_FAILED,		"FAILED" },
469 	{ PHYI_STANDBY,		"STANDBY" },
470 	{ PHYI_INACTIVE,	"INACTIVE" },
471 	{ PHYI_OFFLINE,		"OFFLINE" },
472 	{ PHYI_IPMP,		"IPMP" }
473 };
474 
475 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
476 
477 static ip_m_t   ip_m_tbl[] = {
478 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
479 	    ip_ether_v6intfid },
480 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
481 	    ip_nodef_v6intfid },
482 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
483 	    ip_nodef_v6intfid },
484 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
485 	    ip_nodef_v6intfid },
486 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
487 	    ip_ether_v6intfid },
488 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
489 	    ip_ib_v6intfid },
490 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL },
491 	{ SUNW_DL_IPMP, IFT_OTHER, NULL, NULL, ip_ipmp_v6intfid },
492 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
493 	    ip_nodef_v6intfid }
494 };
495 
496 static ill_t	ill_null;		/* Empty ILL for init. */
497 char	ipif_loopback_name[] = "lo0";
498 static char *ipv4_forward_suffix = ":ip_forwarding";
499 static char *ipv6_forward_suffix = ":ip6_forwarding";
500 static	sin6_t	sin6_null;	/* Zero address for quick clears */
501 static	sin_t	sin_null;	/* Zero address for quick clears */
502 
503 /* When set search for unused ipif_seqid */
504 static ipif_t	ipif_zero;
505 
506 /*
507  * ppa arena is created after these many
508  * interfaces have been plumbed.
509  */
510 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
511 
512 /*
513  * Allocate per-interface mibs.
514  * Returns true if ok. False otherwise.
515  *  ipsq  may not yet be allocated (loopback case ).
516  */
517 static boolean_t
518 ill_allocate_mibs(ill_t *ill)
519 {
520 	/* Already allocated? */
521 	if (ill->ill_ip_mib != NULL) {
522 		if (ill->ill_isv6)
523 			ASSERT(ill->ill_icmp6_mib != NULL);
524 		return (B_TRUE);
525 	}
526 
527 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
528 	    KM_NOSLEEP);
529 	if (ill->ill_ip_mib == NULL) {
530 		return (B_FALSE);
531 	}
532 
533 	/* Setup static information */
534 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
535 	    sizeof (mib2_ipIfStatsEntry_t));
536 	if (ill->ill_isv6) {
537 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
538 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
539 		    sizeof (mib2_ipv6AddrEntry_t));
540 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
541 		    sizeof (mib2_ipv6RouteEntry_t));
542 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
543 		    sizeof (mib2_ipv6NetToMediaEntry_t));
544 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
545 		    sizeof (ipv6_member_t));
546 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
547 		    sizeof (ipv6_grpsrc_t));
548 	} else {
549 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
550 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
551 		    sizeof (mib2_ipAddrEntry_t));
552 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
553 		    sizeof (mib2_ipRouteEntry_t));
554 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
555 		    sizeof (mib2_ipNetToMediaEntry_t));
556 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
557 		    sizeof (ip_member_t));
558 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
559 		    sizeof (ip_grpsrc_t));
560 
561 		/*
562 		 * For a v4 ill, we are done at this point, because per ill
563 		 * icmp mibs are only used for v6.
564 		 */
565 		return (B_TRUE);
566 	}
567 
568 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
569 	    KM_NOSLEEP);
570 	if (ill->ill_icmp6_mib == NULL) {
571 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
572 		ill->ill_ip_mib = NULL;
573 		return (B_FALSE);
574 	}
575 	/* static icmp info */
576 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
577 	    sizeof (mib2_ipv6IfIcmpEntry_t);
578 	/*
579 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
580 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
581 	 * -> ill_phyint_reinit
582 	 */
583 	return (B_TRUE);
584 }
585 
586 /*
587  * Common code for preparation of ARP commands.  Two points to remember:
588  * 	1) The ill_name is tacked on at the end of the allocated space so
589  *	   the templates name_offset field must contain the total space
590  *	   to allocate less the name length.
591  *
592  *	2) The templates name_length field should contain the *template*
593  *	   length.  We use it as a parameter to bcopy() and then write
594  *	   the real ill_name_length into the name_length field of the copy.
595  * (Always called as writer.)
596  */
597 mblk_t *
598 ill_arp_alloc(ill_t *ill, const uchar_t *template, caddr_t addr)
599 {
600 	arc_t	*arc = (arc_t *)template;
601 	char	*cp;
602 	int	len;
603 	mblk_t	*mp;
604 	uint_t	name_length = ill->ill_name_length;
605 	uint_t	template_len = arc->arc_name_length;
606 
607 	len = arc->arc_name_offset + name_length;
608 	mp = allocb(len, BPRI_HI);
609 	if (mp == NULL)
610 		return (NULL);
611 	cp = (char *)mp->b_rptr;
612 	mp->b_wptr = (uchar_t *)&cp[len];
613 	if (template_len)
614 		bcopy(template, cp, template_len);
615 	if (len > template_len)
616 		bzero(&cp[template_len], len - template_len);
617 	mp->b_datap->db_type = M_PROTO;
618 
619 	arc = (arc_t *)cp;
620 	arc->arc_name_length = name_length;
621 	cp = (char *)arc + arc->arc_name_offset;
622 	bcopy(ill->ill_name, cp, name_length);
623 
624 	if (addr) {
625 		area_t	*area = (area_t *)mp->b_rptr;
626 
627 		cp = (char *)area + area->area_proto_addr_offset;
628 		bcopy(addr, cp, area->area_proto_addr_length);
629 		if (area->area_cmd == AR_ENTRY_ADD) {
630 			cp = (char *)area;
631 			len = area->area_proto_addr_length;
632 			if (area->area_proto_mask_offset)
633 				cp += area->area_proto_mask_offset;
634 			else
635 				cp += area->area_proto_addr_offset + len;
636 			while (len-- > 0)
637 				*cp++ = (char)~0;
638 		}
639 	}
640 	return (mp);
641 }
642 
643 mblk_t *
644 ipif_area_alloc(ipif_t *ipif, uint_t optflags)
645 {
646 	caddr_t	addr;
647 	mblk_t 	*mp;
648 	area_t	*area;
649 	uchar_t	*areap;
650 	ill_t	*ill = ipif->ipif_ill;
651 
652 	if (ill->ill_isv6) {
653 		ASSERT(ill->ill_flags & ILLF_XRESOLV);
654 		addr = (caddr_t)&ipif->ipif_v6lcl_addr;
655 		areap = (uchar_t *)&ip6_area_template;
656 	} else {
657 		addr = (caddr_t)&ipif->ipif_lcl_addr;
658 		areap = (uchar_t *)&ip_area_template;
659 	}
660 
661 	if ((mp = ill_arp_alloc(ill, areap, addr)) == NULL)
662 		return (NULL);
663 
664 	/*
665 	 * IPMP requires that the hardware address be included in all
666 	 * AR_ENTRY_ADD requests so that ARP can deduce the arl to send on.
667 	 * If there are no active underlying ills in the group (and thus no
668 	 * hardware address, DAD will be deferred until an underlying ill
669 	 * becomes active.
670 	 */
671 	if (IS_IPMP(ill)) {
672 		if ((ill = ipmp_ipif_hold_bound_ill(ipif)) == NULL) {
673 			freemsg(mp);
674 			return (NULL);
675 		}
676 	} else {
677 		ill_refhold(ill);
678 	}
679 
680 	area = (area_t *)mp->b_rptr;
681 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR;
682 	area->area_flags |= optflags;
683 	area->area_hw_addr_length = ill->ill_phys_addr_length;
684 	bcopy(ill->ill_phys_addr, mp->b_rptr + area->area_hw_addr_offset,
685 	    area->area_hw_addr_length);
686 
687 	ill_refrele(ill);
688 	return (mp);
689 }
690 
691 mblk_t *
692 ipif_ared_alloc(ipif_t *ipif)
693 {
694 	caddr_t	addr;
695 	uchar_t	*aredp;
696 
697 	if (ipif->ipif_ill->ill_isv6) {
698 		ASSERT(ipif->ipif_ill->ill_flags & ILLF_XRESOLV);
699 		addr = (caddr_t)&ipif->ipif_v6lcl_addr;
700 		aredp = (uchar_t *)&ip6_ared_template;
701 	} else {
702 		addr = (caddr_t)&ipif->ipif_lcl_addr;
703 		aredp = (uchar_t *)&ip_ared_template;
704 	}
705 
706 	return (ill_arp_alloc(ipif->ipif_ill, aredp, addr));
707 }
708 
709 mblk_t *
710 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
711 {
712 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
713 	    (char *)&addr));
714 }
715 
716 mblk_t *
717 ill_arie_alloc(ill_t *ill, const char *grifname, const void *template)
718 {
719 	mblk_t	*mp = ill_arp_alloc(ill, template, 0);
720 	arie_t	*arie;
721 
722 	if (mp != NULL) {
723 		arie = (arie_t *)mp->b_rptr;
724 		(void) strlcpy(arie->arie_grifname, grifname, LIFNAMSIZ);
725 	}
726 	return (mp);
727 }
728 
729 /*
730  * Completely vaporize a lower level tap and all associated interfaces.
731  * ill_delete is called only out of ip_close when the device control
732  * stream is being closed.
733  */
734 void
735 ill_delete(ill_t *ill)
736 {
737 	ipif_t	*ipif;
738 	ill_t	*prev_ill;
739 	ip_stack_t	*ipst = ill->ill_ipst;
740 
741 	/*
742 	 * ill_delete may be forcibly entering the ipsq. The previous
743 	 * ioctl may not have completed and may need to be aborted.
744 	 * ipsq_flush takes care of it. If we don't need to enter the
745 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
746 	 * ill_delete_tail is sufficient.
747 	 */
748 	ipsq_flush(ill);
749 
750 	/*
751 	 * Nuke all interfaces.  ipif_free will take down the interface,
752 	 * remove it from the list, and free the data structure.
753 	 * Walk down the ipif list and remove the logical interfaces
754 	 * first before removing the main ipif. We can't unplumb
755 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
756 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
757 	 * POINTOPOINT.
758 	 *
759 	 * If ill_ipif was not properly initialized (i.e low on memory),
760 	 * then no interfaces to clean up. In this case just clean up the
761 	 * ill.
762 	 */
763 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
764 		ipif_free(ipif);
765 
766 	/*
767 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
768 	 * So nobody can be using this mp now. Free the mp allocated for
769 	 * honoring ILLF_NOARP
770 	 */
771 	freemsg(ill->ill_arp_on_mp);
772 	ill->ill_arp_on_mp = NULL;
773 
774 	/* Clean up msgs on pending upcalls for mrouted */
775 	reset_mrt_ill(ill);
776 
777 	/*
778 	 * ipif_free -> reset_conn_ipif will remove all multicast
779 	 * references for IPv4. For IPv6, we need to do it here as
780 	 * it points only at ills.
781 	 */
782 	reset_conn_ill(ill);
783 
784 	/*
785 	 * Remove multicast references added as a result of calls to
786 	 * ip_join_allmulti().
787 	 */
788 	ip_purge_allmulti(ill);
789 
790 	/*
791 	 * If the ill being deleted is under IPMP, boot it out of the illgrp.
792 	 */
793 	if (IS_UNDER_IPMP(ill))
794 		ipmp_ill_leave_illgrp(ill);
795 
796 	/*
797 	 * ill_down will arrange to blow off any IRE's dependent on this
798 	 * ILL, and shut down fragmentation reassembly.
799 	 */
800 	ill_down(ill);
801 
802 	/* Let SCTP know, so that it can remove this from its list. */
803 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
804 
805 	/*
806 	 * If an address on this ILL is being used as a source address then
807 	 * clear out the pointers in other ILLs that point to this ILL.
808 	 */
809 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
810 	if (ill->ill_usesrc_grp_next != NULL) {
811 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
812 			ill_disband_usesrc_group(ill);
813 		} else {	/* consumer of the usesrc ILL */
814 			prev_ill = ill_prev_usesrc(ill);
815 			prev_ill->ill_usesrc_grp_next =
816 			    ill->ill_usesrc_grp_next;
817 		}
818 	}
819 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
820 }
821 
822 static void
823 ipif_non_duplicate(ipif_t *ipif)
824 {
825 	ill_t *ill = ipif->ipif_ill;
826 	mutex_enter(&ill->ill_lock);
827 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
828 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
829 		ASSERT(ill->ill_ipif_dup_count > 0);
830 		ill->ill_ipif_dup_count--;
831 	}
832 	mutex_exit(&ill->ill_lock);
833 }
834 
835 /*
836  * ill_delete_tail is called from ip_modclose after all references
837  * to the closing ill are gone. The wait is done in ip_modclose
838  */
839 void
840 ill_delete_tail(ill_t *ill)
841 {
842 	mblk_t	**mpp;
843 	ipif_t	*ipif;
844 	ip_stack_t	*ipst = ill->ill_ipst;
845 
846 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
847 		ipif_non_duplicate(ipif);
848 		ipif_down_tail(ipif);
849 	}
850 
851 	ASSERT(ill->ill_ipif_dup_count == 0 &&
852 	    ill->ill_arp_down_mp == NULL &&
853 	    ill->ill_arp_del_mapping_mp == NULL);
854 
855 	/*
856 	 * If polling capability is enabled (which signifies direct
857 	 * upcall into IP and driver has ill saved as a handle),
858 	 * we need to make sure that unbind has completed before we
859 	 * let the ill disappear and driver no longer has any reference
860 	 * to this ill.
861 	 */
862 	mutex_enter(&ill->ill_lock);
863 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
864 		cv_wait(&ill->ill_cv, &ill->ill_lock);
865 	mutex_exit(&ill->ill_lock);
866 	ASSERT(!(ill->ill_capabilities &
867 	    (ILL_CAPAB_DLD | ILL_CAPAB_DLD_POLL | ILL_CAPAB_DLD_DIRECT)));
868 
869 	if (ill->ill_net_type != IRE_LOOPBACK)
870 		qprocsoff(ill->ill_rq);
871 
872 	/*
873 	 * We do an ipsq_flush once again now. New messages could have
874 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
875 	 * could also have landed up if an ioctl thread had looked up
876 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
877 	 * enqueued the ioctl when we did the ipsq_flush last time.
878 	 */
879 	ipsq_flush(ill);
880 
881 	/*
882 	 * Free capabilities.
883 	 */
884 	if (ill->ill_ipsec_capab_ah != NULL) {
885 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
886 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
887 		ill->ill_ipsec_capab_ah = NULL;
888 	}
889 
890 	if (ill->ill_ipsec_capab_esp != NULL) {
891 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
892 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
893 		ill->ill_ipsec_capab_esp = NULL;
894 	}
895 
896 	if (ill->ill_mdt_capab != NULL) {
897 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
898 		ill->ill_mdt_capab = NULL;
899 	}
900 
901 	if (ill->ill_hcksum_capab != NULL) {
902 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
903 		ill->ill_hcksum_capab = NULL;
904 	}
905 
906 	if (ill->ill_zerocopy_capab != NULL) {
907 		kmem_free(ill->ill_zerocopy_capab,
908 		    sizeof (ill_zerocopy_capab_t));
909 		ill->ill_zerocopy_capab = NULL;
910 	}
911 
912 	if (ill->ill_lso_capab != NULL) {
913 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
914 		ill->ill_lso_capab = NULL;
915 	}
916 
917 	if (ill->ill_dld_capab != NULL) {
918 		kmem_free(ill->ill_dld_capab, sizeof (ill_dld_capab_t));
919 		ill->ill_dld_capab = NULL;
920 	}
921 
922 	while (ill->ill_ipif != NULL)
923 		ipif_free_tail(ill->ill_ipif);
924 
925 	/*
926 	 * We have removed all references to ilm from conn and the ones joined
927 	 * within the kernel.
928 	 *
929 	 * We don't walk conns, mrts and ires because
930 	 *
931 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
932 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
933 	 *    ill references.
934 	 */
935 	ASSERT(ilm_walk_ill(ill) == 0);
936 
937 	/*
938 	 * If this ill is an IPMP meta-interface, blow away the illgrp.  This
939 	 * is safe to do because the illgrp has already been unlinked from the
940 	 * group by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find it.
941 	 */
942 	if (IS_IPMP(ill)) {
943 		ipmp_illgrp_destroy(ill->ill_grp);
944 		ill->ill_grp = NULL;
945 	}
946 
947 	/*
948 	 * Take us out of the list of ILLs. ill_glist_delete -> phyint_free
949 	 * could free the phyint. No more reference to the phyint after this
950 	 * point.
951 	 */
952 	(void) ill_glist_delete(ill);
953 
954 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
955 	if (ill->ill_ndd_name != NULL)
956 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
957 	rw_exit(&ipst->ips_ip_g_nd_lock);
958 
959 	if (ill->ill_frag_ptr != NULL) {
960 		uint_t count;
961 
962 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
963 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
964 		}
965 		mi_free(ill->ill_frag_ptr);
966 		ill->ill_frag_ptr = NULL;
967 		ill->ill_frag_hash_tbl = NULL;
968 	}
969 
970 	freemsg(ill->ill_nd_lla_mp);
971 	/* Free all retained control messages. */
972 	mpp = &ill->ill_first_mp_to_free;
973 	do {
974 		while (mpp[0]) {
975 			mblk_t  *mp;
976 			mblk_t  *mp1;
977 
978 			mp = mpp[0];
979 			mpp[0] = mp->b_next;
980 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
981 				mp1->b_next = NULL;
982 				mp1->b_prev = NULL;
983 			}
984 			freemsg(mp);
985 		}
986 	} while (mpp++ != &ill->ill_last_mp_to_free);
987 
988 	ill_free_mib(ill);
989 
990 #ifdef DEBUG
991 	ill_trace_cleanup(ill);
992 #endif
993 
994 	/* Drop refcnt here */
995 	netstack_rele(ill->ill_ipst->ips_netstack);
996 	ill->ill_ipst = NULL;
997 }
998 
999 static void
1000 ill_free_mib(ill_t *ill)
1001 {
1002 	ip_stack_t *ipst = ill->ill_ipst;
1003 
1004 	/*
1005 	 * MIB statistics must not be lost, so when an interface
1006 	 * goes away the counter values will be added to the global
1007 	 * MIBs.
1008 	 */
1009 	if (ill->ill_ip_mib != NULL) {
1010 		if (ill->ill_isv6) {
1011 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
1012 			    ill->ill_ip_mib);
1013 		} else {
1014 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
1015 			    ill->ill_ip_mib);
1016 		}
1017 
1018 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
1019 		ill->ill_ip_mib = NULL;
1020 	}
1021 	if (ill->ill_icmp6_mib != NULL) {
1022 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
1023 		    ill->ill_icmp6_mib);
1024 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
1025 		ill->ill_icmp6_mib = NULL;
1026 	}
1027 }
1028 
1029 /*
1030  * Concatenate together a physical address and a sap.
1031  *
1032  * Sap_lengths are interpreted as follows:
1033  *   sap_length == 0	==>	no sap
1034  *   sap_length > 0	==>	sap is at the head of the dlpi address
1035  *   sap_length < 0	==>	sap is at the tail of the dlpi address
1036  */
1037 static void
1038 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
1039     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
1040 {
1041 	uint16_t sap_addr = (uint16_t)sap_src;
1042 
1043 	if (sap_length == 0) {
1044 		if (phys_src == NULL)
1045 			bzero(dst, phys_length);
1046 		else
1047 			bcopy(phys_src, dst, phys_length);
1048 	} else if (sap_length < 0) {
1049 		if (phys_src == NULL)
1050 			bzero(dst, phys_length);
1051 		else
1052 			bcopy(phys_src, dst, phys_length);
1053 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
1054 	} else {
1055 		bcopy(&sap_addr, dst, sizeof (sap_addr));
1056 		if (phys_src == NULL)
1057 			bzero((char *)dst + sap_length, phys_length);
1058 		else
1059 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
1060 	}
1061 }
1062 
1063 /*
1064  * Generate a dl_unitdata_req mblk for the device and address given.
1065  * addr_length is the length of the physical portion of the address.
1066  * If addr is NULL include an all zero address of the specified length.
1067  * TRUE? In any case, addr_length is taken to be the entire length of the
1068  * dlpi address, including the absolute value of sap_length.
1069  */
1070 mblk_t *
1071 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1072 		t_scalar_t sap_length)
1073 {
1074 	dl_unitdata_req_t *dlur;
1075 	mblk_t	*mp;
1076 	t_scalar_t	abs_sap_length;		/* absolute value */
1077 
1078 	abs_sap_length = ABS(sap_length);
1079 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1080 	    DL_UNITDATA_REQ);
1081 	if (mp == NULL)
1082 		return (NULL);
1083 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1084 	/* HACK: accomodate incompatible DLPI drivers */
1085 	if (addr_length == 8)
1086 		addr_length = 6;
1087 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1088 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1089 	dlur->dl_priority.dl_min = 0;
1090 	dlur->dl_priority.dl_max = 0;
1091 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1092 	    (uchar_t *)&dlur[1]);
1093 	return (mp);
1094 }
1095 
1096 /*
1097  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1098  * Return an error if we already have 1 or more ioctls in progress.
1099  * This is used only for non-exclusive ioctls. Currently this is used
1100  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1101  * and thus need to use ipsq_pending_mp_add.
1102  */
1103 boolean_t
1104 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1105 {
1106 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1107 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1108 	/*
1109 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1110 	 */
1111 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1112 	    (add_mp->b_datap->db_type == M_IOCTL));
1113 
1114 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1115 	/*
1116 	 * Return error if the conn has started closing. The conn
1117 	 * could have finished cleaning up the pending mp list,
1118 	 * If so we should not add another mp to the list negating
1119 	 * the cleanup.
1120 	 */
1121 	if (connp->conn_state_flags & CONN_CLOSING)
1122 		return (B_FALSE);
1123 	/*
1124 	 * Add the pending mp to the head of the list, chained by b_next.
1125 	 * Note down the conn on which the ioctl request came, in b_prev.
1126 	 * This will be used to later get the conn, when we get a response
1127 	 * on the ill queue, from some other module (typically arp)
1128 	 */
1129 	add_mp->b_next = (void *)ill->ill_pending_mp;
1130 	add_mp->b_queue = CONNP_TO_WQ(connp);
1131 	ill->ill_pending_mp = add_mp;
1132 	if (connp != NULL)
1133 		connp->conn_oper_pending_ill = ill;
1134 	return (B_TRUE);
1135 }
1136 
1137 /*
1138  * Retrieve the ill_pending_mp and return it. We have to walk the list
1139  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1140  */
1141 mblk_t *
1142 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1143 {
1144 	mblk_t	*prev = NULL;
1145 	mblk_t	*curr = NULL;
1146 	uint_t	id;
1147 	conn_t	*connp;
1148 
1149 	/*
1150 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1151 	 * up the pending mp, but it does not know the ioc_id and
1152 	 * passes in a zero for it.
1153 	 */
1154 	mutex_enter(&ill->ill_lock);
1155 	if (ioc_id != 0)
1156 		*connpp = NULL;
1157 
1158 	/* Search the list for the appropriate ioctl based on ioc_id */
1159 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1160 	    prev = curr, curr = curr->b_next) {
1161 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1162 		connp = Q_TO_CONN(curr->b_queue);
1163 		/* Match based on the ioc_id or based on the conn */
1164 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1165 			break;
1166 	}
1167 
1168 	if (curr != NULL) {
1169 		/* Unlink the mblk from the pending mp list */
1170 		if (prev != NULL) {
1171 			prev->b_next = curr->b_next;
1172 		} else {
1173 			ASSERT(ill->ill_pending_mp == curr);
1174 			ill->ill_pending_mp = curr->b_next;
1175 		}
1176 
1177 		/*
1178 		 * conn refcnt must have been bumped up at the start of
1179 		 * the ioctl. So we can safely access the conn.
1180 		 */
1181 		ASSERT(CONN_Q(curr->b_queue));
1182 		*connpp = Q_TO_CONN(curr->b_queue);
1183 		curr->b_next = NULL;
1184 		curr->b_queue = NULL;
1185 	}
1186 
1187 	mutex_exit(&ill->ill_lock);
1188 
1189 	return (curr);
1190 }
1191 
1192 /*
1193  * Add the pending mp to the list. There can be only 1 pending mp
1194  * in the list. Any exclusive ioctl that needs to wait for a response
1195  * from another module or driver needs to use this function to set
1196  * the ipx_pending_mp to the ioctl mblk and wait for the response from
1197  * the other module/driver. This is also used while waiting for the
1198  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1199  */
1200 boolean_t
1201 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1202     int waitfor)
1203 {
1204 	ipxop_t	*ipx = ipif->ipif_ill->ill_phyint->phyint_ipsq->ipsq_xop;
1205 
1206 	ASSERT(IAM_WRITER_IPIF(ipif));
1207 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1208 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1209 	ASSERT(ipx->ipx_pending_mp == NULL);
1210 	/*
1211 	 * The caller may be using a different ipif than the one passed into
1212 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
1213 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
1214 	 * that `ipx_current_ipif == ipif'.
1215 	 */
1216 	ASSERT(ipx->ipx_current_ipif != NULL);
1217 
1218 	/*
1219 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1220 	 * M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the driver.
1221 	 */
1222 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1223 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP) ||
1224 	    (DB_TYPE(add_mp) == M_PROTO) || (DB_TYPE(add_mp) == M_PCPROTO));
1225 
1226 	if (connp != NULL) {
1227 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1228 		/*
1229 		 * Return error if the conn has started closing. The conn
1230 		 * could have finished cleaning up the pending mp list,
1231 		 * If so we should not add another mp to the list negating
1232 		 * the cleanup.
1233 		 */
1234 		if (connp->conn_state_flags & CONN_CLOSING)
1235 			return (B_FALSE);
1236 	}
1237 	mutex_enter(&ipx->ipx_lock);
1238 	ipx->ipx_pending_ipif = ipif;
1239 	/*
1240 	 * Note down the queue in b_queue. This will be returned by
1241 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1242 	 * the processing
1243 	 */
1244 	add_mp->b_next = NULL;
1245 	add_mp->b_queue = q;
1246 	ipx->ipx_pending_mp = add_mp;
1247 	ipx->ipx_waitfor = waitfor;
1248 	mutex_exit(&ipx->ipx_lock);
1249 
1250 	if (connp != NULL)
1251 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1252 
1253 	return (B_TRUE);
1254 }
1255 
1256 /*
1257  * Retrieve the ipx_pending_mp and return it. There can be only 1 mp
1258  * queued in the list.
1259  */
1260 mblk_t *
1261 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1262 {
1263 	mblk_t	*curr = NULL;
1264 	ipxop_t	*ipx = ipsq->ipsq_xop;
1265 
1266 	*connpp = NULL;
1267 	mutex_enter(&ipx->ipx_lock);
1268 	if (ipx->ipx_pending_mp == NULL) {
1269 		mutex_exit(&ipx->ipx_lock);
1270 		return (NULL);
1271 	}
1272 
1273 	/* There can be only 1 such excl message */
1274 	curr = ipx->ipx_pending_mp;
1275 	ASSERT(curr->b_next == NULL);
1276 	ipx->ipx_pending_ipif = NULL;
1277 	ipx->ipx_pending_mp = NULL;
1278 	ipx->ipx_waitfor = 0;
1279 	mutex_exit(&ipx->ipx_lock);
1280 
1281 	if (CONN_Q(curr->b_queue)) {
1282 		/*
1283 		 * This mp did a refhold on the conn, at the start of the ioctl.
1284 		 * So we can safely return a pointer to the conn to the caller.
1285 		 */
1286 		*connpp = Q_TO_CONN(curr->b_queue);
1287 	} else {
1288 		*connpp = NULL;
1289 	}
1290 	curr->b_next = NULL;
1291 	curr->b_prev = NULL;
1292 	return (curr);
1293 }
1294 
1295 /*
1296  * Cleanup the ioctl mp queued in ipx_pending_mp
1297  * - Called in the ill_delete path
1298  * - Called in the M_ERROR or M_HANGUP path on the ill.
1299  * - Called in the conn close path.
1300  */
1301 boolean_t
1302 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1303 {
1304 	mblk_t	*mp;
1305 	ipxop_t	*ipx;
1306 	queue_t	*q;
1307 	ipif_t	*ipif;
1308 
1309 	ASSERT(IAM_WRITER_ILL(ill));
1310 	ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
1311 
1312 	/*
1313 	 * If connp is null, unconditionally clean up the ipx_pending_mp.
1314 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1315 	 * even if it is meant for another ill, since we have to enqueue
1316 	 * a new mp now in ipx_pending_mp to complete the ipif_down.
1317 	 * If connp is non-null we are called from the conn close path.
1318 	 */
1319 	mutex_enter(&ipx->ipx_lock);
1320 	mp = ipx->ipx_pending_mp;
1321 	if (mp == NULL || (connp != NULL &&
1322 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1323 		mutex_exit(&ipx->ipx_lock);
1324 		return (B_FALSE);
1325 	}
1326 	/* Now remove from the ipx_pending_mp */
1327 	ipx->ipx_pending_mp = NULL;
1328 	q = mp->b_queue;
1329 	mp->b_next = NULL;
1330 	mp->b_prev = NULL;
1331 	mp->b_queue = NULL;
1332 
1333 	ipif = ipx->ipx_pending_ipif;
1334 	ipx->ipx_pending_ipif = NULL;
1335 	ipx->ipx_waitfor = 0;
1336 	ipx->ipx_current_ipif = NULL;
1337 	ipx->ipx_current_ioctl = 0;
1338 	ipx->ipx_current_done = B_TRUE;
1339 	mutex_exit(&ipx->ipx_lock);
1340 
1341 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1342 		if (connp == NULL) {
1343 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1344 		} else {
1345 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
1346 			mutex_enter(&ipif->ipif_ill->ill_lock);
1347 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
1348 			mutex_exit(&ipif->ipif_ill->ill_lock);
1349 		}
1350 	} else {
1351 		/*
1352 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1353 		 * be just inet_freemsg. we have to restart it
1354 		 * otherwise the thread will be stuck.
1355 		 */
1356 		inet_freemsg(mp);
1357 	}
1358 	return (B_TRUE);
1359 }
1360 
1361 /*
1362  * The ill is closing. Cleanup all the pending mps. Called exclusively
1363  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1364  * knows this ill, and hence nobody can add an mp to this list
1365  */
1366 static void
1367 ill_pending_mp_cleanup(ill_t *ill)
1368 {
1369 	mblk_t	*mp;
1370 	queue_t	*q;
1371 
1372 	ASSERT(IAM_WRITER_ILL(ill));
1373 
1374 	mutex_enter(&ill->ill_lock);
1375 	/*
1376 	 * Every mp on the pending mp list originating from an ioctl
1377 	 * added 1 to the conn refcnt, at the start of the ioctl.
1378 	 * So bump it down now.  See comments in ip_wput_nondata()
1379 	 */
1380 	while (ill->ill_pending_mp != NULL) {
1381 		mp = ill->ill_pending_mp;
1382 		ill->ill_pending_mp = mp->b_next;
1383 		mutex_exit(&ill->ill_lock);
1384 
1385 		q = mp->b_queue;
1386 		ASSERT(CONN_Q(q));
1387 		mp->b_next = NULL;
1388 		mp->b_prev = NULL;
1389 		mp->b_queue = NULL;
1390 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1391 		mutex_enter(&ill->ill_lock);
1392 	}
1393 	ill->ill_pending_ipif = NULL;
1394 
1395 	mutex_exit(&ill->ill_lock);
1396 }
1397 
1398 /*
1399  * Called in the conn close path and ill delete path
1400  */
1401 static void
1402 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1403 {
1404 	ipsq_t	*ipsq;
1405 	mblk_t	*prev;
1406 	mblk_t	*curr;
1407 	mblk_t	*next;
1408 	queue_t	*q;
1409 	mblk_t	*tmp_list = NULL;
1410 
1411 	ASSERT(IAM_WRITER_ILL(ill));
1412 	if (connp != NULL)
1413 		q = CONNP_TO_WQ(connp);
1414 	else
1415 		q = ill->ill_wq;
1416 
1417 	ipsq = ill->ill_phyint->phyint_ipsq;
1418 	/*
1419 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1420 	 * In the case of ioctl from a conn, there can be only 1 mp
1421 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1422 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1423 	 * ioctls meant for this ill form conn's are not flushed. They will
1424 	 * be processed during ipsq_exit and will not find the ill and will
1425 	 * return error.
1426 	 */
1427 	mutex_enter(&ipsq->ipsq_lock);
1428 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1429 	    curr = next) {
1430 		next = curr->b_next;
1431 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1432 			/* Unlink the mblk from the pending mp list */
1433 			if (prev != NULL) {
1434 				prev->b_next = curr->b_next;
1435 			} else {
1436 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1437 				ipsq->ipsq_xopq_mphead = curr->b_next;
1438 			}
1439 			if (ipsq->ipsq_xopq_mptail == curr)
1440 				ipsq->ipsq_xopq_mptail = prev;
1441 			/*
1442 			 * Create a temporary list and release the ipsq lock
1443 			 * New elements are added to the head of the tmp_list
1444 			 */
1445 			curr->b_next = tmp_list;
1446 			tmp_list = curr;
1447 		} else {
1448 			prev = curr;
1449 		}
1450 	}
1451 	mutex_exit(&ipsq->ipsq_lock);
1452 
1453 	while (tmp_list != NULL) {
1454 		curr = tmp_list;
1455 		tmp_list = curr->b_next;
1456 		curr->b_next = NULL;
1457 		curr->b_prev = NULL;
1458 		curr->b_queue = NULL;
1459 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1460 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1461 			    CONN_CLOSE : NO_COPYOUT, NULL);
1462 		} else {
1463 			/*
1464 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1465 			 * this can't be just inet_freemsg. we have to
1466 			 * restart it otherwise the thread will be stuck.
1467 			 */
1468 			inet_freemsg(curr);
1469 		}
1470 	}
1471 }
1472 
1473 /*
1474  * This conn has started closing. Cleanup any pending ioctl from this conn.
1475  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1476  */
1477 void
1478 conn_ioctl_cleanup(conn_t *connp)
1479 {
1480 	mblk_t *curr;
1481 	ipsq_t	*ipsq;
1482 	ill_t	*ill;
1483 	boolean_t refheld;
1484 
1485 	/*
1486 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1487 	 * ioctl has not yet started, the mp is pending in the list headed by
1488 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1489 	 * ipx_pending_mp. If the ioctl timed out in the streamhead but
1490 	 * is currently executing now the mp is not queued anywhere but
1491 	 * conn_oper_pending_ill is null. The conn close will wait
1492 	 * till the conn_ref drops to zero.
1493 	 */
1494 	mutex_enter(&connp->conn_lock);
1495 	ill = connp->conn_oper_pending_ill;
1496 	if (ill == NULL) {
1497 		mutex_exit(&connp->conn_lock);
1498 		return;
1499 	}
1500 
1501 	curr = ill_pending_mp_get(ill, &connp, 0);
1502 	if (curr != NULL) {
1503 		mutex_exit(&connp->conn_lock);
1504 		CONN_DEC_REF(connp);
1505 		inet_freemsg(curr);
1506 		return;
1507 	}
1508 	/*
1509 	 * We may not be able to refhold the ill if the ill/ipif
1510 	 * is changing. But we need to make sure that the ill will
1511 	 * not vanish. So we just bump up the ill_waiter count.
1512 	 */
1513 	refheld = ill_waiter_inc(ill);
1514 	mutex_exit(&connp->conn_lock);
1515 	if (refheld) {
1516 		if (ipsq_enter(ill, B_TRUE, NEW_OP)) {
1517 			ill_waiter_dcr(ill);
1518 			/*
1519 			 * Check whether this ioctl has started and is
1520 			 * pending. If it is not found there then check
1521 			 * whether this ioctl has not even started and is in
1522 			 * the ipsq_xopq list.
1523 			 */
1524 			if (!ipsq_pending_mp_cleanup(ill, connp))
1525 				ipsq_xopq_mp_cleanup(ill, connp);
1526 			ipsq = ill->ill_phyint->phyint_ipsq;
1527 			ipsq_exit(ipsq);
1528 			return;
1529 		}
1530 	}
1531 
1532 	/*
1533 	 * The ill is also closing and we could not bump up the
1534 	 * ill_waiter_count or we could not enter the ipsq. Leave
1535 	 * the cleanup to ill_delete
1536 	 */
1537 	mutex_enter(&connp->conn_lock);
1538 	while (connp->conn_oper_pending_ill != NULL)
1539 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1540 	mutex_exit(&connp->conn_lock);
1541 	if (refheld)
1542 		ill_waiter_dcr(ill);
1543 }
1544 
1545 /*
1546  * ipcl_walk function for cleaning up conn_*_ill fields.
1547  */
1548 static void
1549 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1550 {
1551 	ill_t	*ill = (ill_t *)arg;
1552 	ire_t	*ire;
1553 
1554 	mutex_enter(&connp->conn_lock);
1555 	if (connp->conn_multicast_ill == ill) {
1556 		/* Revert to late binding */
1557 		connp->conn_multicast_ill = NULL;
1558 	}
1559 	if (connp->conn_incoming_ill == ill)
1560 		connp->conn_incoming_ill = NULL;
1561 	if (connp->conn_outgoing_ill == ill)
1562 		connp->conn_outgoing_ill = NULL;
1563 	if (connp->conn_dhcpinit_ill == ill) {
1564 		connp->conn_dhcpinit_ill = NULL;
1565 		ASSERT(ill->ill_dhcpinit != 0);
1566 		atomic_dec_32(&ill->ill_dhcpinit);
1567 	}
1568 	if (connp->conn_ire_cache != NULL) {
1569 		ire = connp->conn_ire_cache;
1570 		/*
1571 		 * Source address selection makes it possible for IRE_CACHE
1572 		 * entries to be created with ire_stq coming from interface X
1573 		 * and ipif coming from interface Y.  Thus whenever interface
1574 		 * X goes down, remove all references to it by checking both
1575 		 * on ire_ipif and ire_stq.
1576 		 */
1577 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1578 		    (ire->ire_type == IRE_CACHE &&
1579 		    ire->ire_stq == ill->ill_wq)) {
1580 			connp->conn_ire_cache = NULL;
1581 			mutex_exit(&connp->conn_lock);
1582 			ire_refrele_notr(ire);
1583 			return;
1584 		}
1585 	}
1586 	mutex_exit(&connp->conn_lock);
1587 }
1588 
1589 /* ARGSUSED */
1590 void
1591 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1592 {
1593 	ill_t	*ill = q->q_ptr;
1594 	ipif_t	*ipif;
1595 
1596 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1597 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1598 		ipif_non_duplicate(ipif);
1599 		ipif_down_tail(ipif);
1600 	}
1601 	freemsg(mp);
1602 	ipsq_current_finish(ipsq);
1603 }
1604 
1605 /*
1606  * ill_down_start is called when we want to down this ill and bring it up again
1607  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1608  * all interfaces, but don't tear down any plumbing.
1609  */
1610 boolean_t
1611 ill_down_start(queue_t *q, mblk_t *mp)
1612 {
1613 	ill_t	*ill = q->q_ptr;
1614 	ipif_t	*ipif;
1615 
1616 	ASSERT(IAM_WRITER_ILL(ill));
1617 
1618 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1619 		(void) ipif_down(ipif, NULL, NULL);
1620 
1621 	ill_down(ill);
1622 
1623 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1624 
1625 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1626 
1627 	/*
1628 	 * Atomically test and add the pending mp if references are active.
1629 	 */
1630 	mutex_enter(&ill->ill_lock);
1631 	if (!ill_is_quiescent(ill)) {
1632 		/* call cannot fail since `conn_t *' argument is NULL */
1633 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1634 		    mp, ILL_DOWN);
1635 		mutex_exit(&ill->ill_lock);
1636 		return (B_FALSE);
1637 	}
1638 	mutex_exit(&ill->ill_lock);
1639 	return (B_TRUE);
1640 }
1641 
1642 static void
1643 ill_down(ill_t *ill)
1644 {
1645 	ip_stack_t	*ipst = ill->ill_ipst;
1646 
1647 	/* Blow off any IREs dependent on this ILL. */
1648 	ire_walk(ill_downi, ill, ipst);
1649 
1650 	/* Remove any conn_*_ill depending on this ill */
1651 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1652 }
1653 
1654 /*
1655  * ire_walk routine used to delete every IRE that depends on queues
1656  * associated with 'ill'.  (Always called as writer.)
1657  */
1658 static void
1659 ill_downi(ire_t *ire, char *ill_arg)
1660 {
1661 	ill_t	*ill = (ill_t *)ill_arg;
1662 
1663 	/*
1664 	 * Source address selection makes it possible for IRE_CACHE
1665 	 * entries to be created with ire_stq coming from interface X
1666 	 * and ipif coming from interface Y.  Thus whenever interface
1667 	 * X goes down, remove all references to it by checking both
1668 	 * on ire_ipif and ire_stq.
1669 	 */
1670 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1671 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1672 		ire_delete(ire);
1673 	}
1674 }
1675 
1676 /*
1677  * Remove ire/nce from the fastpath list.
1678  */
1679 void
1680 ill_fastpath_nack(ill_t *ill)
1681 {
1682 	nce_fastpath_list_dispatch(ill, NULL, NULL);
1683 }
1684 
1685 /* Consume an M_IOCACK of the fastpath probe. */
1686 void
1687 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1688 {
1689 	mblk_t	*mp1 = mp;
1690 
1691 	/*
1692 	 * If this was the first attempt turn on the fastpath probing.
1693 	 */
1694 	mutex_enter(&ill->ill_lock);
1695 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1696 		ill->ill_dlpi_fastpath_state = IDS_OK;
1697 	mutex_exit(&ill->ill_lock);
1698 
1699 	/* Free the M_IOCACK mblk, hold on to the data */
1700 	mp = mp->b_cont;
1701 	freeb(mp1);
1702 	if (mp == NULL)
1703 		return;
1704 	if (mp->b_cont != NULL) {
1705 		/*
1706 		 * Update all IRE's or NCE's that are waiting for
1707 		 * fastpath update.
1708 		 */
1709 		nce_fastpath_list_dispatch(ill, ndp_fastpath_update, mp);
1710 		mp1 = mp->b_cont;
1711 		freeb(mp);
1712 		mp = mp1;
1713 	} else {
1714 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1715 	}
1716 
1717 	freeb(mp);
1718 }
1719 
1720 /*
1721  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1722  * The data portion of the request is a dl_unitdata_req_t template for
1723  * what we would send downstream in the absence of a fastpath confirmation.
1724  */
1725 int
1726 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1727 {
1728 	struct iocblk	*ioc;
1729 	mblk_t	*mp;
1730 
1731 	if (dlur_mp == NULL)
1732 		return (EINVAL);
1733 
1734 	mutex_enter(&ill->ill_lock);
1735 	switch (ill->ill_dlpi_fastpath_state) {
1736 	case IDS_FAILED:
1737 		/*
1738 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1739 		 * support it.
1740 		 */
1741 		mutex_exit(&ill->ill_lock);
1742 		return (ENOTSUP);
1743 	case IDS_UNKNOWN:
1744 		/* This is the first probe */
1745 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1746 		break;
1747 	default:
1748 		break;
1749 	}
1750 	mutex_exit(&ill->ill_lock);
1751 
1752 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1753 		return (EAGAIN);
1754 
1755 	mp->b_cont = copyb(dlur_mp);
1756 	if (mp->b_cont == NULL) {
1757 		freeb(mp);
1758 		return (EAGAIN);
1759 	}
1760 
1761 	ioc = (struct iocblk *)mp->b_rptr;
1762 	ioc->ioc_count = msgdsize(mp->b_cont);
1763 
1764 	putnext(ill->ill_wq, mp);
1765 	return (0);
1766 }
1767 
1768 void
1769 ill_capability_probe(ill_t *ill)
1770 {
1771 	mblk_t	*mp;
1772 
1773 	ASSERT(IAM_WRITER_ILL(ill));
1774 
1775 	if (ill->ill_dlpi_capab_state != IDCS_UNKNOWN &&
1776 	    ill->ill_dlpi_capab_state != IDCS_FAILED)
1777 		return;
1778 
1779 	/*
1780 	 * We are starting a new cycle of capability negotiation.
1781 	 * Free up the capab reset messages of any previous incarnation.
1782 	 * We will do a fresh allocation when we get the response to our probe
1783 	 */
1784 	if (ill->ill_capab_reset_mp != NULL) {
1785 		freemsg(ill->ill_capab_reset_mp);
1786 		ill->ill_capab_reset_mp = NULL;
1787 	}
1788 
1789 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1790 
1791 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t), DL_CAPABILITY_REQ);
1792 	if (mp == NULL)
1793 		return;
1794 
1795 	ill_capability_send(ill, mp);
1796 	ill->ill_dlpi_capab_state = IDCS_PROBE_SENT;
1797 }
1798 
1799 void
1800 ill_capability_reset(ill_t *ill, boolean_t reneg)
1801 {
1802 	ASSERT(IAM_WRITER_ILL(ill));
1803 
1804 	if (ill->ill_dlpi_capab_state != IDCS_OK)
1805 		return;
1806 
1807 	ill->ill_dlpi_capab_state = reneg ? IDCS_RENEG : IDCS_RESET_SENT;
1808 
1809 	ill_capability_send(ill, ill->ill_capab_reset_mp);
1810 	ill->ill_capab_reset_mp = NULL;
1811 	/*
1812 	 * We turn off all capabilities except those pertaining to
1813 	 * direct function call capabilities viz. ILL_CAPAB_DLD*
1814 	 * which will be turned off by the corresponding reset functions.
1815 	 */
1816 	ill->ill_capabilities &= ~(ILL_CAPAB_MDT | ILL_CAPAB_HCKSUM  |
1817 	    ILL_CAPAB_ZEROCOPY | ILL_CAPAB_AH | ILL_CAPAB_ESP);
1818 }
1819 
1820 static void
1821 ill_capability_reset_alloc(ill_t *ill)
1822 {
1823 	mblk_t *mp;
1824 	size_t	size = 0;
1825 	int	err;
1826 	dl_capability_req_t	*capb;
1827 
1828 	ASSERT(IAM_WRITER_ILL(ill));
1829 	ASSERT(ill->ill_capab_reset_mp == NULL);
1830 
1831 	if (ILL_MDT_CAPABLE(ill))
1832 		size += sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
1833 
1834 	if (ILL_HCKSUM_CAPABLE(ill)) {
1835 		size += sizeof (dl_capability_sub_t) +
1836 		    sizeof (dl_capab_hcksum_t);
1837 	}
1838 
1839 	if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) {
1840 		size += sizeof (dl_capability_sub_t) +
1841 		    sizeof (dl_capab_zerocopy_t);
1842 	}
1843 
1844 	if (ill->ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)) {
1845 		size += sizeof (dl_capability_sub_t);
1846 		size += ill_capability_ipsec_reset_size(ill, NULL, NULL,
1847 		    NULL, NULL);
1848 	}
1849 
1850 	if (ill->ill_capabilities & ILL_CAPAB_DLD) {
1851 		size += sizeof (dl_capability_sub_t) +
1852 		    sizeof (dl_capab_dld_t);
1853 	}
1854 
1855 	mp = allocb_wait(size + sizeof (dl_capability_req_t), BPRI_MED,
1856 	    STR_NOSIG, &err);
1857 
1858 	mp->b_datap->db_type = M_PROTO;
1859 	bzero(mp->b_rptr, size + sizeof (dl_capability_req_t));
1860 
1861 	capb = (dl_capability_req_t *)mp->b_rptr;
1862 	capb->dl_primitive = DL_CAPABILITY_REQ;
1863 	capb->dl_sub_offset = sizeof (dl_capability_req_t);
1864 	capb->dl_sub_length = size;
1865 
1866 	mp->b_wptr += sizeof (dl_capability_req_t);
1867 
1868 	/*
1869 	 * Each handler fills in the corresponding dl_capability_sub_t
1870 	 * inside the mblk,
1871 	 */
1872 	ill_capability_mdt_reset_fill(ill, mp);
1873 	ill_capability_hcksum_reset_fill(ill, mp);
1874 	ill_capability_zerocopy_reset_fill(ill, mp);
1875 	ill_capability_ipsec_reset_fill(ill, mp);
1876 	ill_capability_dld_reset_fill(ill, mp);
1877 
1878 	ill->ill_capab_reset_mp = mp;
1879 }
1880 
1881 static void
1882 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1883 {
1884 	dl_capab_id_t *id_ic;
1885 	uint_t sub_dl_cap = outers->dl_cap;
1886 	dl_capability_sub_t *inners;
1887 	uint8_t *capend;
1888 
1889 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1890 
1891 	/*
1892 	 * Note: range checks here are not absolutely sufficient to
1893 	 * make us robust against malformed messages sent by drivers;
1894 	 * this is in keeping with the rest of IP's dlpi handling.
1895 	 * (Remember, it's coming from something else in the kernel
1896 	 * address space)
1897 	 */
1898 
1899 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1900 	if (capend > mp->b_wptr) {
1901 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1902 		    "malformed sub-capability too long for mblk");
1903 		return;
1904 	}
1905 
1906 	id_ic = (dl_capab_id_t *)(outers + 1);
1907 
1908 	if (outers->dl_length < sizeof (*id_ic) ||
1909 	    (inners = &id_ic->id_subcap,
1910 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1911 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1912 		    "encapsulated capab type %d too long for mblk",
1913 		    inners->dl_cap);
1914 		return;
1915 	}
1916 
1917 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1918 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1919 		    "isn't as expected; pass-thru module(s) detected, "
1920 		    "discarding capability\n", inners->dl_cap));
1921 		return;
1922 	}
1923 
1924 	/* Process the encapsulated sub-capability */
1925 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1926 }
1927 
1928 /*
1929  * Process Multidata Transmit capability negotiation ack received from a
1930  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1931  * DL_CAPABILITY_ACK message.
1932  */
1933 static void
1934 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1935 {
1936 	mblk_t *nmp = NULL;
1937 	dl_capability_req_t *oc;
1938 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1939 	ill_mdt_capab_t **ill_mdt_capab;
1940 	uint_t sub_dl_cap = isub->dl_cap;
1941 	uint8_t *capend;
1942 
1943 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1944 
1945 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1946 
1947 	/*
1948 	 * Note: range checks here are not absolutely sufficient to
1949 	 * make us robust against malformed messages sent by drivers;
1950 	 * this is in keeping with the rest of IP's dlpi handling.
1951 	 * (Remember, it's coming from something else in the kernel
1952 	 * address space)
1953 	 */
1954 
1955 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1956 	if (capend > mp->b_wptr) {
1957 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1958 		    "malformed sub-capability too long for mblk");
1959 		return;
1960 	}
1961 
1962 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1963 
1964 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1965 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1966 		    "unsupported MDT sub-capability (version %d, expected %d)",
1967 		    mdt_ic->mdt_version, MDT_VERSION_2);
1968 		return;
1969 	}
1970 
1971 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1972 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1973 		    "capability isn't as expected; pass-thru module(s) "
1974 		    "detected, discarding capability\n"));
1975 		return;
1976 	}
1977 
1978 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
1979 
1980 		if (*ill_mdt_capab == NULL) {
1981 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
1982 			    KM_NOSLEEP);
1983 			if (*ill_mdt_capab == NULL) {
1984 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1985 				    "could not enable MDT version %d "
1986 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
1987 				    ill->ill_name);
1988 				return;
1989 			}
1990 		}
1991 
1992 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
1993 		    "MDT version %d (%d bytes leading, %d bytes trailing "
1994 		    "header spaces, %d max pld bufs, %d span limit)\n",
1995 		    ill->ill_name, MDT_VERSION_2,
1996 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
1997 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
1998 
1999 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
2000 		(*ill_mdt_capab)->ill_mdt_on = 1;
2001 		/*
2002 		 * Round the following values to the nearest 32-bit; ULP
2003 		 * may further adjust them to accomodate for additional
2004 		 * protocol headers.  We pass these values to ULP during
2005 		 * bind time.
2006 		 */
2007 		(*ill_mdt_capab)->ill_mdt_hdr_head =
2008 		    roundup(mdt_ic->mdt_hdr_head, 4);
2009 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
2010 		    roundup(mdt_ic->mdt_hdr_tail, 4);
2011 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
2012 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
2013 
2014 		ill->ill_capabilities |= ILL_CAPAB_MDT;
2015 	} else {
2016 		uint_t size;
2017 		uchar_t *rptr;
2018 
2019 		size = sizeof (dl_capability_req_t) +
2020 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
2021 
2022 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2023 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
2024 			    "could not enable MDT for %s (ENOMEM)\n",
2025 			    ill->ill_name);
2026 			return;
2027 		}
2028 
2029 		rptr = nmp->b_rptr;
2030 		/* initialize dl_capability_req_t */
2031 		oc = (dl_capability_req_t *)nmp->b_rptr;
2032 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
2033 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
2034 		    sizeof (dl_capab_mdt_t);
2035 		nmp->b_rptr += sizeof (dl_capability_req_t);
2036 
2037 		/* initialize dl_capability_sub_t */
2038 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2039 		nmp->b_rptr += sizeof (*isub);
2040 
2041 		/* initialize dl_capab_mdt_t */
2042 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2043 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2044 
2045 		nmp->b_rptr = rptr;
2046 
2047 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2048 		    "to enable MDT version %d\n", ill->ill_name,
2049 		    MDT_VERSION_2));
2050 
2051 		/* set ENABLE flag */
2052 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2053 
2054 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2055 		ill_capability_send(ill, nmp);
2056 	}
2057 }
2058 
2059 static void
2060 ill_capability_mdt_reset_fill(ill_t *ill, mblk_t *mp)
2061 {
2062 	dl_capab_mdt_t *mdt_subcap;
2063 	dl_capability_sub_t *dl_subcap;
2064 
2065 	if (!ILL_MDT_CAPABLE(ill))
2066 		return;
2067 
2068 	ASSERT(ill->ill_mdt_capab != NULL);
2069 
2070 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2071 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2072 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2073 
2074 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2075 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2076 	mdt_subcap->mdt_flags = 0;
2077 	mdt_subcap->mdt_hdr_head = 0;
2078 	mdt_subcap->mdt_hdr_tail = 0;
2079 
2080 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2081 }
2082 
2083 static void
2084 ill_capability_dld_reset_fill(ill_t *ill, mblk_t *mp)
2085 {
2086 	dl_capability_sub_t *dl_subcap;
2087 
2088 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
2089 		return;
2090 
2091 	/*
2092 	 * The dl_capab_dld_t that follows the dl_capability_sub_t is not
2093 	 * initialized below since it is not used by DLD.
2094 	 */
2095 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2096 	dl_subcap->dl_cap = DL_CAPAB_DLD;
2097 	dl_subcap->dl_length = sizeof (dl_capab_dld_t);
2098 
2099 	mp->b_wptr += sizeof (dl_capability_sub_t) + sizeof (dl_capab_dld_t);
2100 }
2101 
2102 /*
2103  * Send a DL_NOTIFY_REQ to the specified ill to enable
2104  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2105  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2106  * acceleration.
2107  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2108  */
2109 static boolean_t
2110 ill_enable_promisc_notify(ill_t *ill)
2111 {
2112 	mblk_t *mp;
2113 	dl_notify_req_t *req;
2114 
2115 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2116 
2117 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2118 	if (mp == NULL)
2119 		return (B_FALSE);
2120 
2121 	req = (dl_notify_req_t *)mp->b_rptr;
2122 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2123 	    DL_NOTE_PROMISC_OFF_PHYS;
2124 
2125 	ill_dlpi_send(ill, mp);
2126 
2127 	return (B_TRUE);
2128 }
2129 
2130 /*
2131  * Allocate an IPsec capability request which will be filled by our
2132  * caller to turn on support for one or more algorithms.
2133  */
2134 static mblk_t *
2135 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2136 {
2137 	mblk_t *nmp;
2138 	dl_capability_req_t	*ocap;
2139 	dl_capab_ipsec_t	*ocip;
2140 	dl_capab_ipsec_t	*icip;
2141 	uint8_t			*ptr;
2142 	icip = (dl_capab_ipsec_t *)(isub + 1);
2143 
2144 	/*
2145 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2146 	 * PROMISC_ON/OFF notification from the provider. We need to
2147 	 * do this before enabling the algorithms to avoid leakage of
2148 	 * cleartext packets.
2149 	 */
2150 
2151 	if (!ill_enable_promisc_notify(ill))
2152 		return (NULL);
2153 
2154 	/*
2155 	 * Allocate new mblk which will contain a new capability
2156 	 * request to enable the capabilities.
2157 	 */
2158 
2159 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2160 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2161 	if (nmp == NULL)
2162 		return (NULL);
2163 
2164 	ptr = nmp->b_rptr;
2165 
2166 	/* initialize dl_capability_req_t */
2167 	ocap = (dl_capability_req_t *)ptr;
2168 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2169 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2170 	ptr += sizeof (dl_capability_req_t);
2171 
2172 	/* initialize dl_capability_sub_t */
2173 	bcopy(isub, ptr, sizeof (*isub));
2174 	ptr += sizeof (*isub);
2175 
2176 	/* initialize dl_capab_ipsec_t */
2177 	ocip = (dl_capab_ipsec_t *)ptr;
2178 	bcopy(icip, ocip, sizeof (*icip));
2179 
2180 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2181 	return (nmp);
2182 }
2183 
2184 /*
2185  * Process an IPsec capability negotiation ack received from a DLS Provider.
2186  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2187  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2188  */
2189 static void
2190 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2191 {
2192 	dl_capab_ipsec_t	*icip;
2193 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2194 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2195 	uint_t cipher, nciphers;
2196 	mblk_t *nmp;
2197 	uint_t alg_len;
2198 	boolean_t need_sadb_dump;
2199 	uint_t sub_dl_cap = isub->dl_cap;
2200 	ill_ipsec_capab_t **ill_capab;
2201 	uint64_t ill_capab_flag;
2202 	uint8_t *capend, *ciphend;
2203 	boolean_t sadb_resync;
2204 
2205 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2206 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2207 
2208 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2209 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2210 		ill_capab_flag = ILL_CAPAB_AH;
2211 	} else {
2212 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2213 		ill_capab_flag = ILL_CAPAB_ESP;
2214 	}
2215 
2216 	/*
2217 	 * If the ill capability structure exists, then this incoming
2218 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2219 	 * If this is so, then we'd need to resynchronize the SADB
2220 	 * after re-enabling the offloaded ciphers.
2221 	 */
2222 	sadb_resync = (*ill_capab != NULL);
2223 
2224 	/*
2225 	 * Note: range checks here are not absolutely sufficient to
2226 	 * make us robust against malformed messages sent by drivers;
2227 	 * this is in keeping with the rest of IP's dlpi handling.
2228 	 * (Remember, it's coming from something else in the kernel
2229 	 * address space)
2230 	 */
2231 
2232 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2233 	if (capend > mp->b_wptr) {
2234 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2235 		    "malformed sub-capability too long for mblk");
2236 		return;
2237 	}
2238 
2239 	/*
2240 	 * There are two types of acks we process here:
2241 	 * 1. acks in reply to a (first form) generic capability req
2242 	 *    (no ENABLE flag set)
2243 	 * 2. acks in reply to a ENABLE capability req.
2244 	 *    (ENABLE flag set)
2245 	 *
2246 	 * We process the subcapability passed as argument as follows:
2247 	 * 1 do initializations
2248 	 *   1.1 initialize nmp = NULL
2249 	 *   1.2 set need_sadb_dump to B_FALSE
2250 	 * 2 for each cipher in subcapability:
2251 	 *   2.1 if ENABLE flag is set:
2252 	 *	2.1.1 update per-ill ipsec capabilities info
2253 	 *	2.1.2 set need_sadb_dump to B_TRUE
2254 	 *   2.2 if ENABLE flag is not set:
2255 	 *	2.2.1 if nmp is NULL:
2256 	 *		2.2.1.1 allocate and initialize nmp
2257 	 *		2.2.1.2 init current pos in nmp
2258 	 *	2.2.2 copy current cipher to current pos in nmp
2259 	 *	2.2.3 set ENABLE flag in nmp
2260 	 *	2.2.4 update current pos
2261 	 * 3 if nmp is not equal to NULL, send enable request
2262 	 *   3.1 send capability request
2263 	 * 4 if need_sadb_dump is B_TRUE
2264 	 *   4.1 enable promiscuous on/off notifications
2265 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2266 	 *	AH or ESP SA's to interface.
2267 	 */
2268 
2269 	nmp = NULL;
2270 	oalg = NULL;
2271 	need_sadb_dump = B_FALSE;
2272 	icip = (dl_capab_ipsec_t *)(isub + 1);
2273 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2274 
2275 	nciphers = icip->cip_nciphers;
2276 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2277 
2278 	if (ciphend > capend) {
2279 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2280 		    "too many ciphers for sub-capability len");
2281 		return;
2282 	}
2283 
2284 	for (cipher = 0; cipher < nciphers; cipher++) {
2285 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2286 
2287 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2288 			/*
2289 			 * TBD: when we provide a way to disable capabilities
2290 			 * from above, need to manage the request-pending state
2291 			 * and fail if we were not expecting this ACK.
2292 			 */
2293 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2294 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2295 
2296 			/*
2297 			 * Update IPsec capabilities for this ill
2298 			 */
2299 
2300 			if (*ill_capab == NULL) {
2301 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2302 				    ("ill_capability_ipsec_ack: "
2303 				    "allocating ipsec_capab for ill\n"));
2304 				*ill_capab = ill_ipsec_capab_alloc();
2305 
2306 				if (*ill_capab == NULL) {
2307 					cmn_err(CE_WARN,
2308 					    "ill_capability_ipsec_ack: "
2309 					    "could not enable IPsec Hardware "
2310 					    "acceleration for %s (ENOMEM)\n",
2311 					    ill->ill_name);
2312 					return;
2313 				}
2314 			}
2315 
2316 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2317 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2318 
2319 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2320 				cmn_err(CE_WARN,
2321 				    "ill_capability_ipsec_ack: "
2322 				    "malformed IPsec algorithm id %d",
2323 				    ialg->alg_prim);
2324 				continue;
2325 			}
2326 
2327 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2328 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2329 				    ialg->alg_prim);
2330 			} else {
2331 				ipsec_capab_algparm_t *alp;
2332 
2333 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2334 				    ialg->alg_prim);
2335 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2336 				    ialg->alg_prim)) {
2337 					cmn_err(CE_WARN,
2338 					    "ill_capability_ipsec_ack: "
2339 					    "no space for IPsec alg id %d",
2340 					    ialg->alg_prim);
2341 					continue;
2342 				}
2343 				alp = &((*ill_capab)->encr_algparm[
2344 				    ialg->alg_prim]);
2345 				alp->minkeylen = ialg->alg_minbits;
2346 				alp->maxkeylen = ialg->alg_maxbits;
2347 			}
2348 			ill->ill_capabilities |= ill_capab_flag;
2349 			/*
2350 			 * indicate that a capability was enabled, which
2351 			 * will be used below to kick off a SADB dump
2352 			 * to the ill.
2353 			 */
2354 			need_sadb_dump = B_TRUE;
2355 		} else {
2356 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2357 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2358 			    ialg->alg_prim));
2359 
2360 			if (nmp == NULL) {
2361 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2362 				if (nmp == NULL) {
2363 					/*
2364 					 * Sending the PROMISC_ON/OFF
2365 					 * notification request failed.
2366 					 * We cannot enable the algorithms
2367 					 * since the Provider will not
2368 					 * notify IP of promiscous mode
2369 					 * changes, which could lead
2370 					 * to leakage of packets.
2371 					 */
2372 					cmn_err(CE_WARN,
2373 					    "ill_capability_ipsec_ack: "
2374 					    "could not enable IPsec Hardware "
2375 					    "acceleration for %s (ENOMEM)\n",
2376 					    ill->ill_name);
2377 					return;
2378 				}
2379 				/* ptr to current output alg specifier */
2380 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2381 			}
2382 
2383 			/*
2384 			 * Copy current alg specifier, set ENABLE
2385 			 * flag, and advance to next output alg.
2386 			 * For now we enable all IPsec capabilities.
2387 			 */
2388 			ASSERT(oalg != NULL);
2389 			bcopy(ialg, oalg, alg_len);
2390 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2391 			nmp->b_wptr += alg_len;
2392 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2393 		}
2394 
2395 		/* move to next input algorithm specifier */
2396 		ialg = (dl_capab_ipsec_alg_t *)
2397 		    ((char *)ialg + alg_len);
2398 	}
2399 
2400 	if (nmp != NULL)
2401 		/*
2402 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2403 		 * IPsec hardware acceleration.
2404 		 */
2405 		ill_capability_send(ill, nmp);
2406 
2407 	if (need_sadb_dump)
2408 		/*
2409 		 * An acknowledgement corresponding to a request to
2410 		 * enable acceleration was received, notify SADB.
2411 		 */
2412 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2413 }
2414 
2415 /*
2416  * Given an mblk with enough space in it, create sub-capability entries for
2417  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2418  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2419  * in preparation for the reset the DL_CAPABILITY_REQ message.
2420  */
2421 static void
2422 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2423     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2424 {
2425 	dl_capab_ipsec_t *oipsec;
2426 	dl_capab_ipsec_alg_t *oalg;
2427 	dl_capability_sub_t *dl_subcap;
2428 	int i, k;
2429 
2430 	ASSERT(nciphers > 0);
2431 	ASSERT(ill_cap != NULL);
2432 	ASSERT(mp != NULL);
2433 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2434 
2435 	/* dl_capability_sub_t for "stype" */
2436 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2437 	dl_subcap->dl_cap = stype;
2438 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2439 	mp->b_wptr += sizeof (dl_capability_sub_t);
2440 
2441 	/* dl_capab_ipsec_t for "stype" */
2442 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2443 	oipsec->cip_version = 1;
2444 	oipsec->cip_nciphers = nciphers;
2445 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2446 
2447 	/* create entries for "stype" AUTH ciphers */
2448 	for (i = 0; i < ill_cap->algs_size; i++) {
2449 		for (k = 0; k < BITSPERBYTE; k++) {
2450 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2451 				continue;
2452 
2453 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2454 			bzero((void *)oalg, sizeof (*oalg));
2455 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2456 			oalg->alg_prim = k + (BITSPERBYTE * i);
2457 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2458 		}
2459 	}
2460 	/* create entries for "stype" ENCR ciphers */
2461 	for (i = 0; i < ill_cap->algs_size; i++) {
2462 		for (k = 0; k < BITSPERBYTE; k++) {
2463 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2464 				continue;
2465 
2466 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2467 			bzero((void *)oalg, sizeof (*oalg));
2468 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2469 			oalg->alg_prim = k + (BITSPERBYTE * i);
2470 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2471 		}
2472 	}
2473 }
2474 
2475 /*
2476  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2477  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2478  * POPC instruction, but our macro is more flexible for an arbitrary length
2479  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2480  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2481  * stays that way, we can reduce the number of iterations required.
2482  */
2483 #define	COUNT_1S(val, sum) {					\
2484 	uint8_t x = val & 0xff;					\
2485 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2486 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2487 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2488 }
2489 
2490 /* ARGSUSED */
2491 static int
2492 ill_capability_ipsec_reset_size(ill_t *ill, int *ah_cntp, int *ah_lenp,
2493     int *esp_cntp, int *esp_lenp)
2494 {
2495 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2496 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2497 	uint64_t ill_capabilities = ill->ill_capabilities;
2498 	int ah_cnt = 0, esp_cnt = 0;
2499 	int ah_len = 0, esp_len = 0;
2500 	int i, size = 0;
2501 
2502 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2503 		return (0);
2504 
2505 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2506 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2507 
2508 	/* Find out the number of ciphers for AH */
2509 	if (cap_ah != NULL) {
2510 		for (i = 0; i < cap_ah->algs_size; i++) {
2511 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2512 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2513 		}
2514 		if (ah_cnt > 0) {
2515 			size += sizeof (dl_capability_sub_t) +
2516 			    sizeof (dl_capab_ipsec_t);
2517 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2518 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2519 			size += ah_len;
2520 		}
2521 	}
2522 
2523 	/* Find out the number of ciphers for ESP */
2524 	if (cap_esp != NULL) {
2525 		for (i = 0; i < cap_esp->algs_size; i++) {
2526 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2527 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2528 		}
2529 		if (esp_cnt > 0) {
2530 			size += sizeof (dl_capability_sub_t) +
2531 			    sizeof (dl_capab_ipsec_t);
2532 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2533 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2534 			size += esp_len;
2535 		}
2536 	}
2537 
2538 	if (ah_cntp != NULL)
2539 		*ah_cntp = ah_cnt;
2540 	if (ah_lenp != NULL)
2541 		*ah_lenp = ah_len;
2542 	if (esp_cntp != NULL)
2543 		*esp_cntp = esp_cnt;
2544 	if (esp_lenp != NULL)
2545 		*esp_lenp = esp_len;
2546 
2547 	return (size);
2548 }
2549 
2550 /* ARGSUSED */
2551 static void
2552 ill_capability_ipsec_reset_fill(ill_t *ill, mblk_t *mp)
2553 {
2554 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2555 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2556 	int ah_cnt = 0, esp_cnt = 0;
2557 	int ah_len = 0, esp_len = 0;
2558 	int size;
2559 
2560 	size = ill_capability_ipsec_reset_size(ill, &ah_cnt, &ah_len,
2561 	    &esp_cnt, &esp_len);
2562 	if (size == 0)
2563 		return;
2564 
2565 	/*
2566 	 * Clear the capability flags for IPsec HA but retain the ill
2567 	 * capability structures since it's possible that another thread
2568 	 * is still referring to them.  The structures only get deallocated
2569 	 * when we destroy the ill.
2570 	 *
2571 	 * Various places check the flags to see if the ill is capable of
2572 	 * hardware acceleration, and by clearing them we ensure that new
2573 	 * outbound IPsec packets are sent down encrypted.
2574 	 */
2575 
2576 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2577 	if (ah_cnt > 0) {
2578 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2579 		    cap_ah, mp);
2580 	}
2581 
2582 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2583 	if (esp_cnt > 0) {
2584 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2585 		    cap_esp, mp);
2586 	}
2587 
2588 	/*
2589 	 * At this point we've composed a bunch of sub-capabilities to be
2590 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2591 	 * by the caller.  Upon receiving this reset message, the driver
2592 	 * must stop inbound decryption (by destroying all inbound SAs)
2593 	 * and let the corresponding packets come in encrypted.
2594 	 */
2595 }
2596 
2597 static void
2598 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2599     boolean_t encapsulated)
2600 {
2601 	boolean_t legacy = B_FALSE;
2602 
2603 	/*
2604 	 * Note that only the following two sub-capabilities may be
2605 	 * considered as "legacy", since their original definitions
2606 	 * do not incorporate the dl_mid_t module ID token, and hence
2607 	 * may require the use of the wrapper sub-capability.
2608 	 */
2609 	switch (subp->dl_cap) {
2610 	case DL_CAPAB_IPSEC_AH:
2611 	case DL_CAPAB_IPSEC_ESP:
2612 		legacy = B_TRUE;
2613 		break;
2614 	}
2615 
2616 	/*
2617 	 * For legacy sub-capabilities which don't incorporate a queue_t
2618 	 * pointer in their structures, discard them if we detect that
2619 	 * there are intermediate modules in between IP and the driver.
2620 	 */
2621 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2622 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2623 		    "%d discarded; %d module(s) present below IP\n",
2624 		    subp->dl_cap, ill->ill_lmod_cnt));
2625 		return;
2626 	}
2627 
2628 	switch (subp->dl_cap) {
2629 	case DL_CAPAB_IPSEC_AH:
2630 	case DL_CAPAB_IPSEC_ESP:
2631 		ill_capability_ipsec_ack(ill, mp, subp);
2632 		break;
2633 	case DL_CAPAB_MDT:
2634 		ill_capability_mdt_ack(ill, mp, subp);
2635 		break;
2636 	case DL_CAPAB_HCKSUM:
2637 		ill_capability_hcksum_ack(ill, mp, subp);
2638 		break;
2639 	case DL_CAPAB_ZEROCOPY:
2640 		ill_capability_zerocopy_ack(ill, mp, subp);
2641 		break;
2642 	case DL_CAPAB_DLD:
2643 		ill_capability_dld_ack(ill, mp, subp);
2644 		break;
2645 	default:
2646 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2647 		    subp->dl_cap));
2648 	}
2649 }
2650 
2651 /*
2652  * Process a hardware checksum offload capability negotiation ack received
2653  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
2654  * of a DL_CAPABILITY_ACK message.
2655  */
2656 static void
2657 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2658 {
2659 	dl_capability_req_t	*ocap;
2660 	dl_capab_hcksum_t	*ihck, *ohck;
2661 	ill_hcksum_capab_t	**ill_hcksum;
2662 	mblk_t			*nmp = NULL;
2663 	uint_t			sub_dl_cap = isub->dl_cap;
2664 	uint8_t			*capend;
2665 
2666 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
2667 
2668 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
2669 
2670 	/*
2671 	 * Note: range checks here are not absolutely sufficient to
2672 	 * make us robust against malformed messages sent by drivers;
2673 	 * this is in keeping with the rest of IP's dlpi handling.
2674 	 * (Remember, it's coming from something else in the kernel
2675 	 * address space)
2676 	 */
2677 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2678 	if (capend > mp->b_wptr) {
2679 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
2680 		    "malformed sub-capability too long for mblk");
2681 		return;
2682 	}
2683 
2684 	/*
2685 	 * There are two types of acks we process here:
2686 	 * 1. acks in reply to a (first form) generic capability req
2687 	 *    (no ENABLE flag set)
2688 	 * 2. acks in reply to a ENABLE capability req.
2689 	 *    (ENABLE flag set)
2690 	 */
2691 	ihck = (dl_capab_hcksum_t *)(isub + 1);
2692 
2693 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
2694 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
2695 		    "unsupported hardware checksum "
2696 		    "sub-capability (version %d, expected %d)",
2697 		    ihck->hcksum_version, HCKSUM_VERSION_1);
2698 		return;
2699 	}
2700 
2701 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
2702 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
2703 		    "checksum capability isn't as expected; pass-thru "
2704 		    "module(s) detected, discarding capability\n"));
2705 		return;
2706 	}
2707 
2708 #define	CURR_HCKSUM_CAPAB				\
2709 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
2710 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
2711 
2712 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
2713 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
2714 		/* do ENABLE processing */
2715 		if (*ill_hcksum == NULL) {
2716 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
2717 			    KM_NOSLEEP);
2718 
2719 			if (*ill_hcksum == NULL) {
2720 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
2721 				    "could not enable hcksum version %d "
2722 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
2723 				    ill->ill_name);
2724 				return;
2725 			}
2726 		}
2727 
2728 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
2729 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
2730 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
2731 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
2732 		    "has enabled hardware checksumming\n ",
2733 		    ill->ill_name));
2734 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
2735 		/*
2736 		 * Enabling hardware checksum offload
2737 		 * Currently IP supports {TCP,UDP}/IPv4
2738 		 * partial and full cksum offload and
2739 		 * IPv4 header checksum offload.
2740 		 * Allocate new mblk which will
2741 		 * contain a new capability request
2742 		 * to enable hardware checksum offload.
2743 		 */
2744 		uint_t	size;
2745 		uchar_t	*rptr;
2746 
2747 		size = sizeof (dl_capability_req_t) +
2748 		    sizeof (dl_capability_sub_t) + isub->dl_length;
2749 
2750 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2751 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
2752 			    "could not enable hardware cksum for %s (ENOMEM)\n",
2753 			    ill->ill_name);
2754 			return;
2755 		}
2756 
2757 		rptr = nmp->b_rptr;
2758 		/* initialize dl_capability_req_t */
2759 		ocap = (dl_capability_req_t *)nmp->b_rptr;
2760 		ocap->dl_sub_offset =
2761 		    sizeof (dl_capability_req_t);
2762 		ocap->dl_sub_length =
2763 		    sizeof (dl_capability_sub_t) +
2764 		    isub->dl_length;
2765 		nmp->b_rptr += sizeof (dl_capability_req_t);
2766 
2767 		/* initialize dl_capability_sub_t */
2768 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2769 		nmp->b_rptr += sizeof (*isub);
2770 
2771 		/* initialize dl_capab_hcksum_t */
2772 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
2773 		bcopy(ihck, ohck, sizeof (*ihck));
2774 
2775 		nmp->b_rptr = rptr;
2776 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2777 
2778 		/* Set ENABLE flag */
2779 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
2780 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
2781 
2782 		/*
2783 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2784 		 * hardware checksum acceleration.
2785 		 */
2786 		ill_capability_send(ill, nmp);
2787 	} else {
2788 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
2789 		    "advertised %x hardware checksum capability flags\n",
2790 		    ill->ill_name, ihck->hcksum_txflags));
2791 	}
2792 }
2793 
2794 static void
2795 ill_capability_hcksum_reset_fill(ill_t *ill, mblk_t *mp)
2796 {
2797 	dl_capab_hcksum_t *hck_subcap;
2798 	dl_capability_sub_t *dl_subcap;
2799 
2800 	if (!ILL_HCKSUM_CAPABLE(ill))
2801 		return;
2802 
2803 	ASSERT(ill->ill_hcksum_capab != NULL);
2804 
2805 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2806 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
2807 	dl_subcap->dl_length = sizeof (*hck_subcap);
2808 
2809 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
2810 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
2811 	hck_subcap->hcksum_txflags = 0;
2812 
2813 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*hck_subcap);
2814 }
2815 
2816 static void
2817 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2818 {
2819 	mblk_t *nmp = NULL;
2820 	dl_capability_req_t *oc;
2821 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
2822 	ill_zerocopy_capab_t **ill_zerocopy_capab;
2823 	uint_t sub_dl_cap = isub->dl_cap;
2824 	uint8_t *capend;
2825 
2826 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
2827 
2828 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
2829 
2830 	/*
2831 	 * Note: range checks here are not absolutely sufficient to
2832 	 * make us robust against malformed messages sent by drivers;
2833 	 * this is in keeping with the rest of IP's dlpi handling.
2834 	 * (Remember, it's coming from something else in the kernel
2835 	 * address space)
2836 	 */
2837 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2838 	if (capend > mp->b_wptr) {
2839 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
2840 		    "malformed sub-capability too long for mblk");
2841 		return;
2842 	}
2843 
2844 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
2845 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
2846 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
2847 		    "unsupported ZEROCOPY sub-capability (version %d, "
2848 		    "expected %d)", zc_ic->zerocopy_version,
2849 		    ZEROCOPY_VERSION_1);
2850 		return;
2851 	}
2852 
2853 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
2854 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
2855 		    "capability isn't as expected; pass-thru module(s) "
2856 		    "detected, discarding capability\n"));
2857 		return;
2858 	}
2859 
2860 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
2861 		if (*ill_zerocopy_capab == NULL) {
2862 			*ill_zerocopy_capab =
2863 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
2864 			    KM_NOSLEEP);
2865 
2866 			if (*ill_zerocopy_capab == NULL) {
2867 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
2868 				    "could not enable Zero-copy version %d "
2869 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
2870 				    ill->ill_name);
2871 				return;
2872 			}
2873 		}
2874 
2875 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
2876 		    "supports Zero-copy version %d\n", ill->ill_name,
2877 		    ZEROCOPY_VERSION_1));
2878 
2879 		(*ill_zerocopy_capab)->ill_zerocopy_version =
2880 		    zc_ic->zerocopy_version;
2881 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
2882 		    zc_ic->zerocopy_flags;
2883 
2884 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
2885 	} else {
2886 		uint_t size;
2887 		uchar_t *rptr;
2888 
2889 		size = sizeof (dl_capability_req_t) +
2890 		    sizeof (dl_capability_sub_t) +
2891 		    sizeof (dl_capab_zerocopy_t);
2892 
2893 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2894 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
2895 			    "could not enable zerocopy for %s (ENOMEM)\n",
2896 			    ill->ill_name);
2897 			return;
2898 		}
2899 
2900 		rptr = nmp->b_rptr;
2901 		/* initialize dl_capability_req_t */
2902 		oc = (dl_capability_req_t *)rptr;
2903 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
2904 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
2905 		    sizeof (dl_capab_zerocopy_t);
2906 		rptr += sizeof (dl_capability_req_t);
2907 
2908 		/* initialize dl_capability_sub_t */
2909 		bcopy(isub, rptr, sizeof (*isub));
2910 		rptr += sizeof (*isub);
2911 
2912 		/* initialize dl_capab_zerocopy_t */
2913 		zc_oc = (dl_capab_zerocopy_t *)rptr;
2914 		*zc_oc = *zc_ic;
2915 
2916 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
2917 		    "to enable zero-copy version %d\n", ill->ill_name,
2918 		    ZEROCOPY_VERSION_1));
2919 
2920 		/* set VMSAFE_MEM flag */
2921 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
2922 
2923 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
2924 		ill_capability_send(ill, nmp);
2925 	}
2926 }
2927 
2928 static void
2929 ill_capability_zerocopy_reset_fill(ill_t *ill, mblk_t *mp)
2930 {
2931 	dl_capab_zerocopy_t *zerocopy_subcap;
2932 	dl_capability_sub_t *dl_subcap;
2933 
2934 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
2935 		return;
2936 
2937 	ASSERT(ill->ill_zerocopy_capab != NULL);
2938 
2939 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2940 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
2941 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
2942 
2943 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
2944 	zerocopy_subcap->zerocopy_version =
2945 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
2946 	zerocopy_subcap->zerocopy_flags = 0;
2947 
2948 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
2949 }
2950 
2951 /*
2952  * DLD capability
2953  * Refer to dld.h for more information regarding the purpose and usage
2954  * of this capability.
2955  */
2956 static void
2957 ill_capability_dld_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2958 {
2959 	dl_capab_dld_t		*dld_ic, dld;
2960 	uint_t			sub_dl_cap = isub->dl_cap;
2961 	uint8_t			*capend;
2962 	ill_dld_capab_t		*idc;
2963 
2964 	ASSERT(IAM_WRITER_ILL(ill));
2965 	ASSERT(sub_dl_cap == DL_CAPAB_DLD);
2966 
2967 	/*
2968 	 * Note: range checks here are not absolutely sufficient to
2969 	 * make us robust against malformed messages sent by drivers;
2970 	 * this is in keeping with the rest of IP's dlpi handling.
2971 	 * (Remember, it's coming from something else in the kernel
2972 	 * address space)
2973 	 */
2974 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2975 	if (capend > mp->b_wptr) {
2976 		cmn_err(CE_WARN, "ill_capability_dld_ack: "
2977 		    "malformed sub-capability too long for mblk");
2978 		return;
2979 	}
2980 	dld_ic = (dl_capab_dld_t *)(isub + 1);
2981 	if (dld_ic->dld_version != DLD_CURRENT_VERSION) {
2982 		cmn_err(CE_CONT, "ill_capability_dld_ack: "
2983 		    "unsupported DLD sub-capability (version %d, "
2984 		    "expected %d)", dld_ic->dld_version,
2985 		    DLD_CURRENT_VERSION);
2986 		return;
2987 	}
2988 	if (!dlcapabcheckqid(&dld_ic->dld_mid, ill->ill_lmod_rq)) {
2989 		ip1dbg(("ill_capability_dld_ack: mid token for dld "
2990 		    "capability isn't as expected; pass-thru module(s) "
2991 		    "detected, discarding capability\n"));
2992 		return;
2993 	}
2994 
2995 	/*
2996 	 * Copy locally to ensure alignment.
2997 	 */
2998 	bcopy(dld_ic, &dld, sizeof (dl_capab_dld_t));
2999 
3000 	if ((idc = ill->ill_dld_capab) == NULL) {
3001 		idc = kmem_zalloc(sizeof (ill_dld_capab_t), KM_NOSLEEP);
3002 		if (idc == NULL) {
3003 			cmn_err(CE_WARN, "ill_capability_dld_ack: "
3004 			    "could not enable DLD version %d "
3005 			    "for %s (ENOMEM)\n", DLD_CURRENT_VERSION,
3006 			    ill->ill_name);
3007 			return;
3008 		}
3009 		idc->idc_capab_df = (ip_capab_func_t)dld.dld_capab;
3010 		idc->idc_capab_dh = (void *)dld.dld_capab_handle;
3011 		ill->ill_dld_capab = idc;
3012 	}
3013 	ip1dbg(("ill_capability_dld_ack: interface %s "
3014 	    "supports DLD version %d\n", ill->ill_name, DLD_CURRENT_VERSION));
3015 
3016 	ill_capability_dld_enable(ill);
3017 }
3018 
3019 /*
3020  * Typically capability negotiation between IP and the driver happens via
3021  * DLPI message exchange. However GLD also offers a direct function call
3022  * mechanism to exchange the DLD_DIRECT_CAPAB and DLD_POLL_CAPAB capabilities,
3023  * But arbitrary function calls into IP or GLD are not permitted, since both
3024  * of them are protected by their own perimeter mechanism. The perimeter can
3025  * be viewed as a coarse lock or serialization mechanism. The hierarchy of
3026  * these perimeters is IP -> MAC. Thus for example to enable the squeue
3027  * polling, IP needs to enter its perimeter, then call ill_mac_perim_enter
3028  * to enter the mac perimeter and then do the direct function calls into
3029  * GLD to enable squeue polling. The ring related callbacks from the mac into
3030  * the stack to add, bind, quiesce, restart or cleanup a ring are all
3031  * protected by the mac perimeter.
3032  */
3033 static void
3034 ill_mac_perim_enter(ill_t *ill, mac_perim_handle_t *mphp)
3035 {
3036 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
3037 	int			err;
3038 
3039 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mphp,
3040 	    DLD_ENABLE);
3041 	ASSERT(err == 0);
3042 }
3043 
3044 static void
3045 ill_mac_perim_exit(ill_t *ill, mac_perim_handle_t mph)
3046 {
3047 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
3048 	int			err;
3049 
3050 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mph,
3051 	    DLD_DISABLE);
3052 	ASSERT(err == 0);
3053 }
3054 
3055 boolean_t
3056 ill_mac_perim_held(ill_t *ill)
3057 {
3058 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
3059 
3060 	return (idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, NULL,
3061 	    DLD_QUERY));
3062 }
3063 
3064 static void
3065 ill_capability_direct_enable(ill_t *ill)
3066 {
3067 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
3068 	ill_dld_direct_t	*idd = &idc->idc_direct;
3069 	dld_capab_direct_t	direct;
3070 	int			rc;
3071 
3072 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
3073 
3074 	bzero(&direct, sizeof (direct));
3075 	direct.di_rx_cf = (uintptr_t)ip_input;
3076 	direct.di_rx_ch = ill;
3077 
3078 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT, &direct,
3079 	    DLD_ENABLE);
3080 	if (rc == 0) {
3081 		idd->idd_tx_df = (ip_dld_tx_t)direct.di_tx_df;
3082 		idd->idd_tx_dh = direct.di_tx_dh;
3083 		idd->idd_tx_cb_df = (ip_dld_callb_t)direct.di_tx_cb_df;
3084 		idd->idd_tx_cb_dh = direct.di_tx_cb_dh;
3085 		/*
3086 		 * One time registration of flow enable callback function
3087 		 */
3088 		ill->ill_flownotify_mh = idd->idd_tx_cb_df(idd->idd_tx_cb_dh,
3089 		    ill_flow_enable, ill);
3090 		ill->ill_capabilities |= ILL_CAPAB_DLD_DIRECT;
3091 		DTRACE_PROBE1(direct_on, (ill_t *), ill);
3092 	} else {
3093 		cmn_err(CE_WARN, "warning: could not enable DIRECT "
3094 		    "capability, rc = %d\n", rc);
3095 		DTRACE_PROBE2(direct_off, (ill_t *), ill, (int), rc);
3096 	}
3097 }
3098 
3099 static void
3100 ill_capability_poll_enable(ill_t *ill)
3101 {
3102 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
3103 	dld_capab_poll_t	poll;
3104 	int			rc;
3105 
3106 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
3107 
3108 	bzero(&poll, sizeof (poll));
3109 	poll.poll_ring_add_cf = (uintptr_t)ip_squeue_add_ring;
3110 	poll.poll_ring_remove_cf = (uintptr_t)ip_squeue_clean_ring;
3111 	poll.poll_ring_quiesce_cf = (uintptr_t)ip_squeue_quiesce_ring;
3112 	poll.poll_ring_restart_cf = (uintptr_t)ip_squeue_restart_ring;
3113 	poll.poll_ring_bind_cf = (uintptr_t)ip_squeue_bind_ring;
3114 	poll.poll_ring_ch = ill;
3115 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL, &poll,
3116 	    DLD_ENABLE);
3117 	if (rc == 0) {
3118 		ill->ill_capabilities |= ILL_CAPAB_DLD_POLL;
3119 		DTRACE_PROBE1(poll_on, (ill_t *), ill);
3120 	} else {
3121 		ip1dbg(("warning: could not enable POLL "
3122 		    "capability, rc = %d\n", rc));
3123 		DTRACE_PROBE2(poll_off, (ill_t *), ill, (int), rc);
3124 	}
3125 }
3126 
3127 /*
3128  * Enable the LSO capability.
3129  */
3130 static void
3131 ill_capability_lso_enable(ill_t *ill)
3132 {
3133 	ill_dld_capab_t	*idc = ill->ill_dld_capab;
3134 	dld_capab_lso_t	lso;
3135 	int rc;
3136 
3137 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
3138 
3139 	if (ill->ill_lso_capab == NULL) {
3140 		ill->ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3141 		    KM_NOSLEEP);
3142 		if (ill->ill_lso_capab == NULL) {
3143 			cmn_err(CE_WARN, "ill_capability_lso_enable: "
3144 			    "could not enable LSO for %s (ENOMEM)\n",
3145 			    ill->ill_name);
3146 			return;
3147 		}
3148 	}
3149 
3150 	bzero(&lso, sizeof (lso));
3151 	if ((rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO, &lso,
3152 	    DLD_ENABLE)) == 0) {
3153 		ill->ill_lso_capab->ill_lso_flags = lso.lso_flags;
3154 		ill->ill_lso_capab->ill_lso_max = lso.lso_max;
3155 		ill->ill_capabilities |= ILL_CAPAB_DLD_LSO;
3156 		ip1dbg(("ill_capability_lso_enable: interface %s "
3157 		    "has enabled LSO\n ", ill->ill_name));
3158 	} else {
3159 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
3160 		ill->ill_lso_capab = NULL;
3161 		DTRACE_PROBE2(lso_off, (ill_t *), ill, (int), rc);
3162 	}
3163 }
3164 
3165 static void
3166 ill_capability_dld_enable(ill_t *ill)
3167 {
3168 	mac_perim_handle_t mph;
3169 
3170 	ASSERT(IAM_WRITER_ILL(ill));
3171 
3172 	if (ill->ill_isv6)
3173 		return;
3174 
3175 	ill_mac_perim_enter(ill, &mph);
3176 	if (!ill->ill_isv6) {
3177 		ill_capability_direct_enable(ill);
3178 		ill_capability_poll_enable(ill);
3179 		ill_capability_lso_enable(ill);
3180 	}
3181 	ill->ill_capabilities |= ILL_CAPAB_DLD;
3182 	ill_mac_perim_exit(ill, mph);
3183 }
3184 
3185 static void
3186 ill_capability_dld_disable(ill_t *ill)
3187 {
3188 	ill_dld_capab_t	*idc;
3189 	ill_dld_direct_t *idd;
3190 	mac_perim_handle_t	mph;
3191 
3192 	ASSERT(IAM_WRITER_ILL(ill));
3193 
3194 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
3195 		return;
3196 
3197 	ill_mac_perim_enter(ill, &mph);
3198 
3199 	idc = ill->ill_dld_capab;
3200 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT) != 0) {
3201 		/*
3202 		 * For performance we avoid locks in the transmit data path
3203 		 * and don't maintain a count of the number of threads using
3204 		 * direct calls. Thus some threads could be using direct
3205 		 * transmit calls to GLD, even after the capability mechanism
3206 		 * turns it off. This is still safe since the handles used in
3207 		 * the direct calls continue to be valid until the unplumb is
3208 		 * completed. Remove the callback that was added (1-time) at
3209 		 * capab enable time.
3210 		 */
3211 		mutex_enter(&ill->ill_lock);
3212 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_DIRECT;
3213 		mutex_exit(&ill->ill_lock);
3214 		if (ill->ill_flownotify_mh != NULL) {
3215 			idd = &idc->idc_direct;
3216 			idd->idd_tx_cb_df(idd->idd_tx_cb_dh, NULL,
3217 			    ill->ill_flownotify_mh);
3218 			ill->ill_flownotify_mh = NULL;
3219 		}
3220 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT,
3221 		    NULL, DLD_DISABLE);
3222 	}
3223 
3224 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_POLL) != 0) {
3225 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_POLL;
3226 		ip_squeue_clean_all(ill);
3227 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL,
3228 		    NULL, DLD_DISABLE);
3229 	}
3230 
3231 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_LSO) != 0) {
3232 		ASSERT(ill->ill_lso_capab != NULL);
3233 		/*
3234 		 * Clear the capability flag for LSO but retain the
3235 		 * ill_lso_capab structure since it's possible that another
3236 		 * thread is still referring to it.  The structure only gets
3237 		 * deallocated when we destroy the ill.
3238 		 */
3239 
3240 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_LSO;
3241 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO,
3242 		    NULL, DLD_DISABLE);
3243 	}
3244 
3245 	ill->ill_capabilities &= ~ILL_CAPAB_DLD;
3246 	ill_mac_perim_exit(ill, mph);
3247 }
3248 
3249 /*
3250  * Capability Negotiation protocol
3251  *
3252  * We don't wait for DLPI capability operations to finish during interface
3253  * bringup or teardown. Doing so would introduce more asynchrony and the
3254  * interface up/down operations will need multiple return and restarts.
3255  * Instead the 'ipsq_current_ipif' of the ipsq is not cleared as long as
3256  * the 'ill_dlpi_deferred' chain is non-empty. This ensures that the next
3257  * exclusive operation won't start until the DLPI operations of the previous
3258  * exclusive operation complete.
3259  *
3260  * The capability state machine is shown below.
3261  *
3262  * state		next state		event, action
3263  *
3264  * IDCS_UNKNOWN 	IDCS_PROBE_SENT		ill_capability_probe
3265  * IDCS_PROBE_SENT	IDCS_OK			ill_capability_ack
3266  * IDCS_PROBE_SENT	IDCS_FAILED		ip_rput_dlpi_writer (nack)
3267  * IDCS_OK		IDCS_RENEG		Receipt of DL_NOTE_CAPAB_RENEG
3268  * IDCS_OK		IDCS_RESET_SENT		ill_capability_reset
3269  * IDCS_RESET_SENT	IDCS_UNKNOWN		ill_capability_ack_thr
3270  * IDCS_RENEG		IDCS_PROBE_SENT		ill_capability_ack_thr ->
3271  *						    ill_capability_probe.
3272  */
3273 
3274 /*
3275  * Dedicated thread started from ip_stack_init that handles capability
3276  * disable. This thread ensures the taskq dispatch does not fail by waiting
3277  * for resources using TQ_SLEEP. The taskq mechanism is used to ensure
3278  * that direct calls to DLD are done in a cv_waitable context.
3279  */
3280 void
3281 ill_taskq_dispatch(ip_stack_t *ipst)
3282 {
3283 	callb_cpr_t cprinfo;
3284 	char 	name[64];
3285 	mblk_t	*mp;
3286 
3287 	(void) snprintf(name, sizeof (name), "ill_taskq_dispatch_%d",
3288 	    ipst->ips_netstack->netstack_stackid);
3289 	CALLB_CPR_INIT(&cprinfo, &ipst->ips_capab_taskq_lock, callb_generic_cpr,
3290 	    name);
3291 	mutex_enter(&ipst->ips_capab_taskq_lock);
3292 
3293 	for (;;) {
3294 		mp = list_head(&ipst->ips_capab_taskq_list);
3295 		while (mp != NULL) {
3296 			list_remove(&ipst->ips_capab_taskq_list, mp);
3297 			mutex_exit(&ipst->ips_capab_taskq_lock);
3298 			VERIFY(taskq_dispatch(system_taskq,
3299 			    ill_capability_ack_thr, mp, TQ_SLEEP) != 0);
3300 			mutex_enter(&ipst->ips_capab_taskq_lock);
3301 			mp = list_head(&ipst->ips_capab_taskq_list);
3302 		}
3303 
3304 		if (ipst->ips_capab_taskq_quit)
3305 			break;
3306 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
3307 		cv_wait(&ipst->ips_capab_taskq_cv, &ipst->ips_capab_taskq_lock);
3308 		CALLB_CPR_SAFE_END(&cprinfo, &ipst->ips_capab_taskq_lock);
3309 	}
3310 	VERIFY(list_head(&ipst->ips_capab_taskq_list) == NULL);
3311 	CALLB_CPR_EXIT(&cprinfo);
3312 	thread_exit();
3313 }
3314 
3315 /*
3316  * Consume a new-style hardware capabilities negotiation ack.
3317  * Called via taskq on receipt of DL_CAPABBILITY_ACK.
3318  */
3319 static void
3320 ill_capability_ack_thr(void *arg)
3321 {
3322 	mblk_t	*mp = arg;
3323 	dl_capability_ack_t *capp;
3324 	dl_capability_sub_t *subp, *endp;
3325 	ill_t	*ill;
3326 	boolean_t reneg;
3327 
3328 	ill = (ill_t *)mp->b_prev;
3329 	VERIFY(ipsq_enter(ill, B_FALSE, CUR_OP) == B_TRUE);
3330 
3331 	if (ill->ill_dlpi_capab_state == IDCS_RESET_SENT ||
3332 	    ill->ill_dlpi_capab_state == IDCS_RENEG) {
3333 		/*
3334 		 * We have received the ack for our DL_CAPAB reset request.
3335 		 * There isnt' anything in the message that needs processing.
3336 		 * All message based capabilities have been disabled, now
3337 		 * do the function call based capability disable.
3338 		 */
3339 		reneg = ill->ill_dlpi_capab_state == IDCS_RENEG;
3340 		ill_capability_dld_disable(ill);
3341 		ill->ill_dlpi_capab_state = IDCS_UNKNOWN;
3342 		if (reneg)
3343 			ill_capability_probe(ill);
3344 		goto done;
3345 	}
3346 
3347 	if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
3348 		ill->ill_dlpi_capab_state = IDCS_OK;
3349 
3350 	capp = (dl_capability_ack_t *)mp->b_rptr;
3351 
3352 	if (capp->dl_sub_length == 0) {
3353 		/* no new-style capabilities */
3354 		goto done;
3355 	}
3356 
3357 	/* make sure the driver supplied correct dl_sub_length */
3358 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3359 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3360 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3361 		goto done;
3362 	}
3363 
3364 
3365 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3366 	/*
3367 	 * There are sub-capabilities. Process the ones we know about.
3368 	 * Loop until we don't have room for another sub-cap header..
3369 	 */
3370 	for (subp = SC(capp, capp->dl_sub_offset),
3371 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3372 	    subp <= endp;
3373 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3374 
3375 		switch (subp->dl_cap) {
3376 		case DL_CAPAB_ID_WRAPPER:
3377 			ill_capability_id_ack(ill, mp, subp);
3378 			break;
3379 		default:
3380 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3381 			break;
3382 		}
3383 	}
3384 #undef SC
3385 done:
3386 	inet_freemsg(mp);
3387 	ill_capability_done(ill);
3388 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
3389 }
3390 
3391 /*
3392  * This needs to be started in a taskq thread to provide a cv_waitable
3393  * context.
3394  */
3395 void
3396 ill_capability_ack(ill_t *ill, mblk_t *mp)
3397 {
3398 	ip_stack_t	*ipst = ill->ill_ipst;
3399 
3400 	mp->b_prev = (mblk_t *)ill;
3401 	if (taskq_dispatch(system_taskq, ill_capability_ack_thr, mp,
3402 	    TQ_NOSLEEP) != 0)
3403 		return;
3404 
3405 	/*
3406 	 * The taskq dispatch failed. Signal the ill_taskq_dispatch thread
3407 	 * which will do the dispatch using TQ_SLEEP to guarantee success.
3408 	 */
3409 	mutex_enter(&ipst->ips_capab_taskq_lock);
3410 	list_insert_tail(&ipst->ips_capab_taskq_list, mp);
3411 	cv_signal(&ipst->ips_capab_taskq_cv);
3412 	mutex_exit(&ipst->ips_capab_taskq_lock);
3413 }
3414 
3415 /*
3416  * This routine is called to scan the fragmentation reassembly table for
3417  * the specified ILL for any packets that are starting to smell.
3418  * dead_interval is the maximum time in seconds that will be tolerated.  It
3419  * will either be the value specified in ip_g_frag_timeout, or zero if the
3420  * ILL is shutting down and it is time to blow everything off.
3421  *
3422  * It returns the number of seconds (as a time_t) that the next frag timer
3423  * should be scheduled for, 0 meaning that the timer doesn't need to be
3424  * re-started.  Note that the method of calculating next_timeout isn't
3425  * entirely accurate since time will flow between the time we grab
3426  * current_time and the time we schedule the next timeout.  This isn't a
3427  * big problem since this is the timer for sending an ICMP reassembly time
3428  * exceeded messages, and it doesn't have to be exactly accurate.
3429  *
3430  * This function is
3431  * sometimes called as writer, although this is not required.
3432  */
3433 time_t
3434 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3435 {
3436 	ipfb_t	*ipfb;
3437 	ipfb_t	*endp;
3438 	ipf_t	*ipf;
3439 	ipf_t	*ipfnext;
3440 	mblk_t	*mp;
3441 	time_t	current_time = gethrestime_sec();
3442 	time_t	next_timeout = 0;
3443 	uint32_t	hdr_length;
3444 	mblk_t	*send_icmp_head;
3445 	mblk_t	*send_icmp_head_v6;
3446 	zoneid_t zoneid;
3447 	ip_stack_t *ipst = ill->ill_ipst;
3448 
3449 	ipfb = ill->ill_frag_hash_tbl;
3450 	if (ipfb == NULL)
3451 		return (B_FALSE);
3452 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3453 	/* Walk the frag hash table. */
3454 	for (; ipfb < endp; ipfb++) {
3455 		send_icmp_head = NULL;
3456 		send_icmp_head_v6 = NULL;
3457 		mutex_enter(&ipfb->ipfb_lock);
3458 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3459 			time_t frag_time = current_time - ipf->ipf_timestamp;
3460 			time_t frag_timeout;
3461 
3462 			if (frag_time < dead_interval) {
3463 				/*
3464 				 * There are some outstanding fragments
3465 				 * that will timeout later.  Make note of
3466 				 * the time so that we can reschedule the
3467 				 * next timeout appropriately.
3468 				 */
3469 				frag_timeout = dead_interval - frag_time;
3470 				if (next_timeout == 0 ||
3471 				    frag_timeout < next_timeout) {
3472 					next_timeout = frag_timeout;
3473 				}
3474 				break;
3475 			}
3476 			/* Time's up.  Get it out of here. */
3477 			hdr_length = ipf->ipf_nf_hdr_len;
3478 			ipfnext = ipf->ipf_hash_next;
3479 			if (ipfnext)
3480 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3481 			*ipf->ipf_ptphn = ipfnext;
3482 			mp = ipf->ipf_mp->b_cont;
3483 			for (; mp; mp = mp->b_cont) {
3484 				/* Extra points for neatness. */
3485 				IP_REASS_SET_START(mp, 0);
3486 				IP_REASS_SET_END(mp, 0);
3487 			}
3488 			mp = ipf->ipf_mp->b_cont;
3489 			atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
3490 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3491 			ipfb->ipfb_count -= ipf->ipf_count;
3492 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3493 			ipfb->ipfb_frag_pkts--;
3494 			/*
3495 			 * We do not send any icmp message from here because
3496 			 * we currently are holding the ipfb_lock for this
3497 			 * hash chain. If we try and send any icmp messages
3498 			 * from here we may end up via a put back into ip
3499 			 * trying to get the same lock, causing a recursive
3500 			 * mutex panic. Instead we build a list and send all
3501 			 * the icmp messages after we have dropped the lock.
3502 			 */
3503 			if (ill->ill_isv6) {
3504 				if (hdr_length != 0) {
3505 					mp->b_next = send_icmp_head_v6;
3506 					send_icmp_head_v6 = mp;
3507 				} else {
3508 					freemsg(mp);
3509 				}
3510 			} else {
3511 				if (hdr_length != 0) {
3512 					mp->b_next = send_icmp_head;
3513 					send_icmp_head = mp;
3514 				} else {
3515 					freemsg(mp);
3516 				}
3517 			}
3518 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3519 			freeb(ipf->ipf_mp);
3520 		}
3521 		mutex_exit(&ipfb->ipfb_lock);
3522 		/*
3523 		 * Now need to send any icmp messages that we delayed from
3524 		 * above.
3525 		 */
3526 		while (send_icmp_head_v6 != NULL) {
3527 			ip6_t *ip6h;
3528 
3529 			mp = send_icmp_head_v6;
3530 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3531 			mp->b_next = NULL;
3532 			if (mp->b_datap->db_type == M_CTL)
3533 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3534 			else
3535 				ip6h = (ip6_t *)mp->b_rptr;
3536 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3537 			    ill, ipst);
3538 			if (zoneid == ALL_ZONES) {
3539 				freemsg(mp);
3540 			} else {
3541 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3542 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3543 				    B_FALSE, zoneid, ipst);
3544 			}
3545 		}
3546 		while (send_icmp_head != NULL) {
3547 			ipaddr_t dst;
3548 
3549 			mp = send_icmp_head;
3550 			send_icmp_head = send_icmp_head->b_next;
3551 			mp->b_next = NULL;
3552 
3553 			if (mp->b_datap->db_type == M_CTL)
3554 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3555 			else
3556 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3557 
3558 			zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst);
3559 			if (zoneid == ALL_ZONES) {
3560 				freemsg(mp);
3561 			} else {
3562 				icmp_time_exceeded(ill->ill_wq, mp,
3563 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid,
3564 				    ipst);
3565 			}
3566 		}
3567 	}
3568 	/*
3569 	 * A non-dying ILL will use the return value to decide whether to
3570 	 * restart the frag timer, and for how long.
3571 	 */
3572 	return (next_timeout);
3573 }
3574 
3575 /*
3576  * This routine is called when the approximate count of mblk memory used
3577  * for the specified ILL has exceeded max_count.
3578  */
3579 void
3580 ill_frag_prune(ill_t *ill, uint_t max_count)
3581 {
3582 	ipfb_t	*ipfb;
3583 	ipf_t	*ipf;
3584 	size_t	count;
3585 
3586 	/*
3587 	 * If we are here within ip_min_frag_prune_time msecs remove
3588 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3589 	 * ill_frag_free_num_pkts.
3590 	 */
3591 	mutex_enter(&ill->ill_lock);
3592 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3593 	    (ip_min_frag_prune_time != 0 ?
3594 	    ip_min_frag_prune_time : msec_per_tick)) {
3595 
3596 		ill->ill_frag_free_num_pkts++;
3597 
3598 	} else {
3599 		ill->ill_frag_free_num_pkts = 0;
3600 	}
3601 	ill->ill_last_frag_clean_time = lbolt;
3602 	mutex_exit(&ill->ill_lock);
3603 
3604 	/*
3605 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3606 	 */
3607 	if (ill->ill_frag_free_num_pkts != 0) {
3608 		int ix;
3609 
3610 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3611 			ipfb = &ill->ill_frag_hash_tbl[ix];
3612 			mutex_enter(&ipfb->ipfb_lock);
3613 			if (ipfb->ipfb_ipf != NULL) {
3614 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3615 				    ill->ill_frag_free_num_pkts);
3616 			}
3617 			mutex_exit(&ipfb->ipfb_lock);
3618 		}
3619 	}
3620 	/*
3621 	 * While the reassembly list for this ILL is too big, prune a fragment
3622 	 * queue by age, oldest first.
3623 	 */
3624 	while (ill->ill_frag_count > max_count) {
3625 		int	ix;
3626 		ipfb_t	*oipfb = NULL;
3627 		uint_t	oldest = UINT_MAX;
3628 
3629 		count = 0;
3630 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3631 			ipfb = &ill->ill_frag_hash_tbl[ix];
3632 			mutex_enter(&ipfb->ipfb_lock);
3633 			ipf = ipfb->ipfb_ipf;
3634 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3635 				oldest = ipf->ipf_gen;
3636 				oipfb = ipfb;
3637 			}
3638 			count += ipfb->ipfb_count;
3639 			mutex_exit(&ipfb->ipfb_lock);
3640 		}
3641 		if (oipfb == NULL)
3642 			break;
3643 
3644 		if (count <= max_count)
3645 			return;	/* Somebody beat us to it, nothing to do */
3646 		mutex_enter(&oipfb->ipfb_lock);
3647 		ipf = oipfb->ipfb_ipf;
3648 		if (ipf != NULL) {
3649 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3650 		}
3651 		mutex_exit(&oipfb->ipfb_lock);
3652 	}
3653 }
3654 
3655 /*
3656  * free 'free_cnt' fragmented packets starting at ipf.
3657  */
3658 void
3659 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3660 {
3661 	size_t	count;
3662 	mblk_t	*mp;
3663 	mblk_t	*tmp;
3664 	ipf_t **ipfp = ipf->ipf_ptphn;
3665 
3666 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3667 	ASSERT(ipfp != NULL);
3668 	ASSERT(ipf != NULL);
3669 
3670 	while (ipf != NULL && free_cnt-- > 0) {
3671 		count = ipf->ipf_count;
3672 		mp = ipf->ipf_mp;
3673 		ipf = ipf->ipf_hash_next;
3674 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3675 			IP_REASS_SET_START(tmp, 0);
3676 			IP_REASS_SET_END(tmp, 0);
3677 		}
3678 		atomic_add_32(&ill->ill_frag_count, -count);
3679 		ASSERT(ipfb->ipfb_count >= count);
3680 		ipfb->ipfb_count -= count;
3681 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3682 		ipfb->ipfb_frag_pkts--;
3683 		freemsg(mp);
3684 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3685 	}
3686 
3687 	if (ipf)
3688 		ipf->ipf_ptphn = ipfp;
3689 	ipfp[0] = ipf;
3690 }
3691 
3692 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3693 	"obsolete and may be removed in a future release of Solaris.  Use " \
3694 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3695 
3696 /*
3697  * For obsolete per-interface forwarding configuration;
3698  * called in response to ND_GET.
3699  */
3700 /* ARGSUSED */
3701 static int
3702 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3703 {
3704 	ill_t *ill = (ill_t *)cp;
3705 
3706 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3707 
3708 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3709 	return (0);
3710 }
3711 
3712 /*
3713  * For obsolete per-interface forwarding configuration;
3714  * called in response to ND_SET.
3715  */
3716 /* ARGSUSED */
3717 static int
3718 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3719     cred_t *ioc_cr)
3720 {
3721 	long value;
3722 	int retval;
3723 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
3724 
3725 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3726 
3727 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3728 	    value < 0 || value > 1) {
3729 		return (EINVAL);
3730 	}
3731 
3732 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3733 	retval = ill_forward_set((ill_t *)cp, (value != 0));
3734 	rw_exit(&ipst->ips_ill_g_lock);
3735 	return (retval);
3736 }
3737 
3738 /*
3739  * Helper function for ill_forward_set().
3740  */
3741 static void
3742 ill_forward_set_on_ill(ill_t *ill, boolean_t enable)
3743 {
3744 	ip_stack_t	*ipst = ill->ill_ipst;
3745 
3746 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3747 
3748 	ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3749 	    (enable ? "Enabling" : "Disabling"),
3750 	    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3751 	mutex_enter(&ill->ill_lock);
3752 	if (enable)
3753 		ill->ill_flags |= ILLF_ROUTER;
3754 	else
3755 		ill->ill_flags &= ~ILLF_ROUTER;
3756 	mutex_exit(&ill->ill_lock);
3757 	if (ill->ill_isv6)
3758 		ill_set_nce_router_flags(ill, enable);
3759 	/* Notify routing socket listeners of this change. */
3760 	ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
3761 }
3762 
3763 /*
3764  * Set an ill's ILLF_ROUTER flag appropriately.  Send up RTS_IFINFO routing
3765  * socket messages for each interface whose flags we change.
3766  */
3767 int
3768 ill_forward_set(ill_t *ill, boolean_t enable)
3769 {
3770 	ipmp_illgrp_t *illg;
3771 	ip_stack_t *ipst = ill->ill_ipst;
3772 
3773 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3774 
3775 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3776 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
3777 		return (0);
3778 
3779 	if (IS_LOOPBACK(ill))
3780 		return (EINVAL);
3781 
3782 	if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
3783 		/*
3784 		 * Update all of the interfaces in the group.
3785 		 */
3786 		illg = ill->ill_grp;
3787 		ill = list_head(&illg->ig_if);
3788 		for (; ill != NULL; ill = list_next(&illg->ig_if, ill))
3789 			ill_forward_set_on_ill(ill, enable);
3790 
3791 		/*
3792 		 * Update the IPMP meta-interface.
3793 		 */
3794 		ill_forward_set_on_ill(ipmp_illgrp_ipmp_ill(illg), enable);
3795 		return (0);
3796 	}
3797 
3798 	ill_forward_set_on_ill(ill, enable);
3799 	return (0);
3800 }
3801 
3802 /*
3803  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3804  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3805  * set or clear.
3806  */
3807 static void
3808 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3809 {
3810 	ipif_t *ipif;
3811 	nce_t *nce;
3812 
3813 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3814 		/*
3815 		 * NOTE: we match across the illgrp because nce's for
3816 		 * addresses on IPMP interfaces have an nce_ill that points to
3817 		 * the bound underlying ill.
3818 		 */
3819 		nce = ndp_lookup_v6(ill, B_TRUE, &ipif->ipif_v6lcl_addr,
3820 		    B_FALSE);
3821 		if (nce != NULL) {
3822 			mutex_enter(&nce->nce_lock);
3823 			if (enable)
3824 				nce->nce_flags |= NCE_F_ISROUTER;
3825 			else
3826 				nce->nce_flags &= ~NCE_F_ISROUTER;
3827 			mutex_exit(&nce->nce_lock);
3828 			NCE_REFRELE(nce);
3829 		}
3830 	}
3831 }
3832 
3833 /*
3834  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
3835  * for this ill.  Make sure the v6/v4 question has been answered about this
3836  * ill.  The creation of this ndd variable is only for backwards compatibility.
3837  * The preferred way to control per-interface IP forwarding is through the
3838  * ILLF_ROUTER interface flag.
3839  */
3840 static int
3841 ill_set_ndd_name(ill_t *ill)
3842 {
3843 	char *suffix;
3844 	ip_stack_t	*ipst = ill->ill_ipst;
3845 
3846 	ASSERT(IAM_WRITER_ILL(ill));
3847 
3848 	if (ill->ill_isv6)
3849 		suffix = ipv6_forward_suffix;
3850 	else
3851 		suffix = ipv4_forward_suffix;
3852 
3853 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
3854 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
3855 	/*
3856 	 * Copies over the '\0'.
3857 	 * Note that strlen(suffix) is always bounded.
3858 	 */
3859 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
3860 	    strlen(suffix) + 1);
3861 
3862 	/*
3863 	 * Use of the nd table requires holding the reader lock.
3864 	 * Modifying the nd table thru nd_load/nd_unload requires
3865 	 * the writer lock.
3866 	 */
3867 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
3868 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
3869 	    nd_ill_forward_set, (caddr_t)ill)) {
3870 		/*
3871 		 * If the nd_load failed, it only meant that it could not
3872 		 * allocate a new bunch of room for further NDD expansion.
3873 		 * Because of that, the ill_ndd_name will be set to 0, and
3874 		 * this interface is at the mercy of the global ip_forwarding
3875 		 * variable.
3876 		 */
3877 		rw_exit(&ipst->ips_ip_g_nd_lock);
3878 		ill->ill_ndd_name = NULL;
3879 		return (ENOMEM);
3880 	}
3881 	rw_exit(&ipst->ips_ip_g_nd_lock);
3882 	return (0);
3883 }
3884 
3885 /*
3886  * Intializes the context structure and returns the first ill in the list
3887  * cuurently start_list and end_list can have values:
3888  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
3889  * IP_V4_G_HEAD		Traverse IPV4 list only.
3890  * IP_V6_G_HEAD		Traverse IPV6 list only.
3891  */
3892 
3893 /*
3894  * We don't check for CONDEMNED ills here. Caller must do that if
3895  * necessary under the ill lock.
3896  */
3897 ill_t *
3898 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
3899     ip_stack_t *ipst)
3900 {
3901 	ill_if_t *ifp;
3902 	ill_t *ill;
3903 	avl_tree_t *avl_tree;
3904 
3905 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
3906 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
3907 
3908 	/*
3909 	 * setup the lists to search
3910 	 */
3911 	if (end_list != MAX_G_HEADS) {
3912 		ctx->ctx_current_list = start_list;
3913 		ctx->ctx_last_list = end_list;
3914 	} else {
3915 		ctx->ctx_last_list = MAX_G_HEADS - 1;
3916 		ctx->ctx_current_list = 0;
3917 	}
3918 
3919 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
3920 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
3921 		if (ifp != (ill_if_t *)
3922 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
3923 			avl_tree = &ifp->illif_avl_by_ppa;
3924 			ill = avl_first(avl_tree);
3925 			/*
3926 			 * ill is guaranteed to be non NULL or ifp should have
3927 			 * not existed.
3928 			 */
3929 			ASSERT(ill != NULL);
3930 			return (ill);
3931 		}
3932 		ctx->ctx_current_list++;
3933 	}
3934 
3935 	return (NULL);
3936 }
3937 
3938 /*
3939  * returns the next ill in the list. ill_first() must have been called
3940  * before calling ill_next() or bad things will happen.
3941  */
3942 
3943 /*
3944  * We don't check for CONDEMNED ills here. Caller must do that if
3945  * necessary under the ill lock.
3946  */
3947 ill_t *
3948 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
3949 {
3950 	ill_if_t *ifp;
3951 	ill_t *ill;
3952 	ip_stack_t	*ipst = lastill->ill_ipst;
3953 
3954 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
3955 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
3956 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
3957 	    AVL_AFTER)) != NULL) {
3958 		return (ill);
3959 	}
3960 
3961 	/* goto next ill_ifp in the list. */
3962 	ifp = lastill->ill_ifptr->illif_next;
3963 
3964 	/* make sure not at end of circular list */
3965 	while (ifp ==
3966 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
3967 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
3968 			return (NULL);
3969 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
3970 	}
3971 
3972 	return (avl_first(&ifp->illif_avl_by_ppa));
3973 }
3974 
3975 /*
3976  * Check interface name for correct format: [a-zA-Z]+[a-zA-Z0-9._]*[0-9]+
3977  * The final number (PPA) must not have any leading zeros.  Upon success, a
3978  * pointer to the start of the PPA is returned; otherwise NULL is returned.
3979  */
3980 static char *
3981 ill_get_ppa_ptr(char *name)
3982 {
3983 	int namelen = strlen(name);
3984 	int end_ndx = namelen - 1;
3985 	int ppa_ndx, i;
3986 
3987 	/*
3988 	 * Check that the first character is [a-zA-Z], and that the last
3989 	 * character is [0-9].
3990 	 */
3991 	if (namelen == 0 || !isalpha(name[0]) || !isdigit(name[end_ndx]))
3992 		return (NULL);
3993 
3994 	/*
3995 	 * Set `ppa_ndx' to the PPA start, and check for leading zeroes.
3996 	 */
3997 	for (ppa_ndx = end_ndx; ppa_ndx > 0; ppa_ndx--)
3998 		if (!isdigit(name[ppa_ndx - 1]))
3999 			break;
4000 
4001 	if (name[ppa_ndx] == '0' && ppa_ndx < end_ndx)
4002 		return (NULL);
4003 
4004 	/*
4005 	 * Check that the intermediate characters are [a-z0-9.]
4006 	 */
4007 	for (i = 1; i < ppa_ndx; i++) {
4008 		if (!isalpha(name[i]) && !isdigit(name[i]) &&
4009 		    name[i] != '.' && name[i] != '_') {
4010 			return (NULL);
4011 		}
4012 	}
4013 
4014 	return (name + ppa_ndx);
4015 }
4016 
4017 /*
4018  * use avl tree to locate the ill.
4019  */
4020 static ill_t *
4021 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
4022     ipsq_func_t func, int *error, ip_stack_t *ipst)
4023 {
4024 	char *ppa_ptr = NULL;
4025 	int len;
4026 	uint_t ppa;
4027 	ill_t *ill = NULL;
4028 	ill_if_t *ifp;
4029 	int list;
4030 	ipsq_t *ipsq;
4031 
4032 	if (error != NULL)
4033 		*error = 0;
4034 
4035 	/*
4036 	 * get ppa ptr
4037 	 */
4038 	if (isv6)
4039 		list = IP_V6_G_HEAD;
4040 	else
4041 		list = IP_V4_G_HEAD;
4042 
4043 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4044 		if (error != NULL)
4045 			*error = ENXIO;
4046 		return (NULL);
4047 	}
4048 
4049 	len = ppa_ptr - name + 1;
4050 
4051 	ppa = stoi(&ppa_ptr);
4052 
4053 	ifp = IP_VX_ILL_G_LIST(list, ipst);
4054 
4055 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4056 		/*
4057 		 * match is done on len - 1 as the name is not null
4058 		 * terminated it contains ppa in addition to the interface
4059 		 * name.
4060 		 */
4061 		if ((ifp->illif_name_len == len) &&
4062 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4063 			break;
4064 		} else {
4065 			ifp = ifp->illif_next;
4066 		}
4067 	}
4068 
4069 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4070 		/*
4071 		 * Even the interface type does not exist.
4072 		 */
4073 		if (error != NULL)
4074 			*error = ENXIO;
4075 		return (NULL);
4076 	}
4077 
4078 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4079 	if (ill != NULL) {
4080 		/*
4081 		 * The block comment at the start of ipif_down
4082 		 * explains the use of the macros used below
4083 		 */
4084 		GRAB_CONN_LOCK(q);
4085 		mutex_enter(&ill->ill_lock);
4086 		if (ILL_CAN_LOOKUP(ill)) {
4087 			ill_refhold_locked(ill);
4088 			mutex_exit(&ill->ill_lock);
4089 			RELEASE_CONN_LOCK(q);
4090 			return (ill);
4091 		} else if (ILL_CAN_WAIT(ill, q)) {
4092 			ipsq = ill->ill_phyint->phyint_ipsq;
4093 			mutex_enter(&ipsq->ipsq_lock);
4094 			mutex_enter(&ipsq->ipsq_xop->ipx_lock);
4095 			mutex_exit(&ill->ill_lock);
4096 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4097 			mutex_exit(&ipsq->ipsq_xop->ipx_lock);
4098 			mutex_exit(&ipsq->ipsq_lock);
4099 			RELEASE_CONN_LOCK(q);
4100 			if (error != NULL)
4101 				*error = EINPROGRESS;
4102 			return (NULL);
4103 		}
4104 		mutex_exit(&ill->ill_lock);
4105 		RELEASE_CONN_LOCK(q);
4106 	}
4107 	if (error != NULL)
4108 		*error = ENXIO;
4109 	return (NULL);
4110 }
4111 
4112 /*
4113  * comparison function for use with avl.
4114  */
4115 static int
4116 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4117 {
4118 	uint_t ppa;
4119 	uint_t ill_ppa;
4120 
4121 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4122 
4123 	ppa = *((uint_t *)ppa_ptr);
4124 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4125 	/*
4126 	 * We want the ill with the lowest ppa to be on the
4127 	 * top.
4128 	 */
4129 	if (ill_ppa < ppa)
4130 		return (1);
4131 	if (ill_ppa > ppa)
4132 		return (-1);
4133 	return (0);
4134 }
4135 
4136 /*
4137  * remove an interface type from the global list.
4138  */
4139 static void
4140 ill_delete_interface_type(ill_if_t *interface)
4141 {
4142 	ASSERT(interface != NULL);
4143 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4144 
4145 	avl_destroy(&interface->illif_avl_by_ppa);
4146 	if (interface->illif_ppa_arena != NULL)
4147 		vmem_destroy(interface->illif_ppa_arena);
4148 
4149 	remque(interface);
4150 
4151 	mi_free(interface);
4152 }
4153 
4154 /*
4155  * remove ill from the global list.
4156  */
4157 static void
4158 ill_glist_delete(ill_t *ill)
4159 {
4160 	ip_stack_t	*ipst;
4161 	phyint_t	*phyi;
4162 
4163 	if (ill == NULL)
4164 		return;
4165 	ipst = ill->ill_ipst;
4166 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4167 
4168 	/*
4169 	 * If the ill was never inserted into the AVL tree
4170 	 * we skip the if branch.
4171 	 */
4172 	if (ill->ill_ifptr != NULL) {
4173 		/*
4174 		 * remove from AVL tree and free ppa number
4175 		 */
4176 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4177 
4178 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4179 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4180 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4181 		}
4182 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4183 			ill_delete_interface_type(ill->ill_ifptr);
4184 		}
4185 
4186 		/*
4187 		 * Indicate ill is no longer in the list.
4188 		 */
4189 		ill->ill_ifptr = NULL;
4190 		ill->ill_name_length = 0;
4191 		ill->ill_name[0] = '\0';
4192 		ill->ill_ppa = UINT_MAX;
4193 	}
4194 
4195 	/* Generate one last event for this ill. */
4196 	ill_nic_event_dispatch(ill, 0, NE_UNPLUMB, ill->ill_name,
4197 	    ill->ill_name_length);
4198 
4199 	ASSERT(ill->ill_phyint != NULL);
4200 	phyi = ill->ill_phyint;
4201 	ill->ill_phyint = NULL;
4202 
4203 	/*
4204 	 * ill_init allocates a phyint always to store the copy
4205 	 * of flags relevant to phyint. At that point in time, we could
4206 	 * not assign the name and hence phyint_illv4/v6 could not be
4207 	 * initialized. Later in ipif_set_values, we assign the name to
4208 	 * the ill, at which point in time we assign phyint_illv4/v6.
4209 	 * Thus we don't rely on phyint_illv6 to be initialized always.
4210 	 */
4211 	if (ill->ill_flags & ILLF_IPV6)
4212 		phyi->phyint_illv6 = NULL;
4213 	else
4214 		phyi->phyint_illv4 = NULL;
4215 
4216 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL) {
4217 		rw_exit(&ipst->ips_ill_g_lock);
4218 		return;
4219 	}
4220 
4221 	/*
4222 	 * There are no ills left on this phyint; pull it out of the phyint
4223 	 * avl trees, and free it.
4224 	 */
4225 	if (phyi->phyint_ifindex > 0) {
4226 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4227 		    phyi);
4228 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
4229 		    phyi);
4230 	}
4231 	rw_exit(&ipst->ips_ill_g_lock);
4232 
4233 	phyint_free(phyi);
4234 }
4235 
4236 /*
4237  * allocate a ppa, if the number of plumbed interfaces of this type are
4238  * less than ill_no_arena do a linear search to find a unused ppa.
4239  * When the number goes beyond ill_no_arena switch to using an arena.
4240  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4241  * is the return value for an error condition, so allocation starts at one
4242  * and is decremented by one.
4243  */
4244 static int
4245 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4246 {
4247 	ill_t *tmp_ill;
4248 	uint_t start, end;
4249 	int ppa;
4250 
4251 	if (ifp->illif_ppa_arena == NULL &&
4252 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4253 		/*
4254 		 * Create an arena.
4255 		 */
4256 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4257 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4258 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4259 			/* allocate what has already been assigned */
4260 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4261 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4262 		    tmp_ill, AVL_AFTER)) {
4263 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4264 			    1,		/* size */
4265 			    1,		/* align/quantum */
4266 			    0,		/* phase */
4267 			    0,		/* nocross */
4268 			    /* minaddr */
4269 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
4270 			    /* maxaddr */
4271 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
4272 			    VM_NOSLEEP|VM_FIRSTFIT);
4273 			if (ppa == 0) {
4274 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4275 				    " failed while switching"));
4276 				vmem_destroy(ifp->illif_ppa_arena);
4277 				ifp->illif_ppa_arena = NULL;
4278 				break;
4279 			}
4280 		}
4281 	}
4282 
4283 	if (ifp->illif_ppa_arena != NULL) {
4284 		if (ill->ill_ppa == UINT_MAX) {
4285 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4286 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4287 			if (ppa == 0)
4288 				return (EAGAIN);
4289 			ill->ill_ppa = --ppa;
4290 		} else {
4291 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4292 			    1, 		/* size */
4293 			    1, 		/* align/quantum */
4294 			    0, 		/* phase */
4295 			    0, 		/* nocross */
4296 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4297 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4298 			    VM_NOSLEEP|VM_FIRSTFIT);
4299 			/*
4300 			 * Most likely the allocation failed because
4301 			 * the requested ppa was in use.
4302 			 */
4303 			if (ppa == 0)
4304 				return (EEXIST);
4305 		}
4306 		return (0);
4307 	}
4308 
4309 	/*
4310 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4311 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4312 	 */
4313 	if (ill->ill_ppa == UINT_MAX) {
4314 		end = UINT_MAX - 1;
4315 		start = 0;
4316 	} else {
4317 		end = start = ill->ill_ppa;
4318 	}
4319 
4320 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4321 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4322 		if (start++ >= end) {
4323 			if (ill->ill_ppa == UINT_MAX)
4324 				return (EAGAIN);
4325 			else
4326 				return (EEXIST);
4327 		}
4328 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4329 	}
4330 	ill->ill_ppa = start;
4331 	return (0);
4332 }
4333 
4334 /*
4335  * Insert ill into the list of configured ill's. Once this function completes,
4336  * the ill is globally visible and is available through lookups. More precisely
4337  * this happens after the caller drops the ill_g_lock.
4338  */
4339 static int
4340 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4341 {
4342 	ill_if_t *ill_interface;
4343 	avl_index_t where = 0;
4344 	int error;
4345 	int name_length;
4346 	int index;
4347 	boolean_t check_length = B_FALSE;
4348 	ip_stack_t	*ipst = ill->ill_ipst;
4349 
4350 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4351 
4352 	name_length = mi_strlen(name) + 1;
4353 
4354 	if (isv6)
4355 		index = IP_V6_G_HEAD;
4356 	else
4357 		index = IP_V4_G_HEAD;
4358 
4359 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4360 	/*
4361 	 * Search for interface type based on name
4362 	 */
4363 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4364 		if ((ill_interface->illif_name_len == name_length) &&
4365 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4366 			break;
4367 		}
4368 		ill_interface = ill_interface->illif_next;
4369 	}
4370 
4371 	/*
4372 	 * Interface type not found, create one.
4373 	 */
4374 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4375 
4376 		ill_g_head_t ghead;
4377 
4378 		/*
4379 		 * allocate ill_if_t structure
4380 		 */
4381 
4382 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4383 		if (ill_interface == NULL) {
4384 			return (ENOMEM);
4385 		}
4386 
4387 
4388 
4389 		(void) strcpy(ill_interface->illif_name, name);
4390 		ill_interface->illif_name_len = name_length;
4391 
4392 		avl_create(&ill_interface->illif_avl_by_ppa,
4393 		    ill_compare_ppa, sizeof (ill_t),
4394 		    offsetof(struct ill_s, ill_avl_byppa));
4395 
4396 		/*
4397 		 * link the structure in the back to maintain order
4398 		 * of configuration for ifconfig output.
4399 		 */
4400 		ghead = ipst->ips_ill_g_heads[index];
4401 		insque(ill_interface, ghead.ill_g_list_tail);
4402 
4403 	}
4404 
4405 	if (ill->ill_ppa == UINT_MAX)
4406 		check_length = B_TRUE;
4407 
4408 	error = ill_alloc_ppa(ill_interface, ill);
4409 	if (error != 0) {
4410 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4411 			ill_delete_interface_type(ill->ill_ifptr);
4412 		return (error);
4413 	}
4414 
4415 	/*
4416 	 * When the ppa is choosen by the system, check that there is
4417 	 * enough space to insert ppa. if a specific ppa was passed in this
4418 	 * check is not required as the interface name passed in will have
4419 	 * the right ppa in it.
4420 	 */
4421 	if (check_length) {
4422 		/*
4423 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4424 		 */
4425 		char buf[sizeof (uint_t) * 3];
4426 
4427 		/*
4428 		 * convert ppa to string to calculate the amount of space
4429 		 * required for it in the name.
4430 		 */
4431 		numtos(ill->ill_ppa, buf);
4432 
4433 		/* Do we have enough space to insert ppa ? */
4434 
4435 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4436 			/* Free ppa and interface type struct */
4437 			if (ill_interface->illif_ppa_arena != NULL) {
4438 				vmem_free(ill_interface->illif_ppa_arena,
4439 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4440 			}
4441 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4442 				ill_delete_interface_type(ill->ill_ifptr);
4443 
4444 			return (EINVAL);
4445 		}
4446 	}
4447 
4448 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4449 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4450 
4451 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4452 	    &where);
4453 	ill->ill_ifptr = ill_interface;
4454 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4455 
4456 	ill_phyint_reinit(ill);
4457 	return (0);
4458 }
4459 
4460 /* Initialize the per phyint ipsq used for serialization */
4461 static boolean_t
4462 ipsq_init(ill_t *ill, boolean_t enter)
4463 {
4464 	ipsq_t  *ipsq;
4465 	ipxop_t	*ipx;
4466 
4467 	if ((ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP)) == NULL)
4468 		return (B_FALSE);
4469 
4470 	ill->ill_phyint->phyint_ipsq = ipsq;
4471 	ipx = ipsq->ipsq_xop = &ipsq->ipsq_ownxop;
4472 	ipx->ipx_ipsq = ipsq;
4473 	ipsq->ipsq_next = ipsq;
4474 	ipsq->ipsq_phyint = ill->ill_phyint;
4475 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4476 	mutex_init(&ipx->ipx_lock, NULL, MUTEX_DEFAULT, 0);
4477 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4478 	if (enter) {
4479 		ipx->ipx_writer = curthread;
4480 		ipx->ipx_forced = B_FALSE;
4481 		ipx->ipx_reentry_cnt = 1;
4482 #ifdef DEBUG
4483 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
4484 #endif
4485 	}
4486 	return (B_TRUE);
4487 }
4488 
4489 /*
4490  * ill_init is called by ip_open when a device control stream is opened.
4491  * It does a few initializations, and shoots a DL_INFO_REQ message down
4492  * to the driver.  The response is later picked up in ip_rput_dlpi and
4493  * used to set up default mechanisms for talking to the driver.  (Always
4494  * called as writer.)
4495  *
4496  * If this function returns error, ip_open will call ip_close which in
4497  * turn will call ill_delete to clean up any memory allocated here that
4498  * is not yet freed.
4499  */
4500 int
4501 ill_init(queue_t *q, ill_t *ill)
4502 {
4503 	int	count;
4504 	dl_info_req_t	*dlir;
4505 	mblk_t	*info_mp;
4506 	uchar_t *frag_ptr;
4507 
4508 	/*
4509 	 * The ill is initialized to zero by mi_alloc*(). In addition
4510 	 * some fields already contain valid values, initialized in
4511 	 * ip_open(), before we reach here.
4512 	 */
4513 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4514 
4515 	ill->ill_rq = q;
4516 	ill->ill_wq = WR(q);
4517 
4518 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4519 	    BPRI_HI);
4520 	if (info_mp == NULL)
4521 		return (ENOMEM);
4522 
4523 	/*
4524 	 * Allocate sufficient space to contain our fragment hash table and
4525 	 * the device name.
4526 	 */
4527 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4528 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4529 	if (frag_ptr == NULL) {
4530 		freemsg(info_mp);
4531 		return (ENOMEM);
4532 	}
4533 	ill->ill_frag_ptr = frag_ptr;
4534 	ill->ill_frag_free_num_pkts = 0;
4535 	ill->ill_last_frag_clean_time = 0;
4536 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4537 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4538 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4539 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4540 		    NULL, MUTEX_DEFAULT, NULL);
4541 	}
4542 
4543 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4544 	if (ill->ill_phyint == NULL) {
4545 		freemsg(info_mp);
4546 		mi_free(frag_ptr);
4547 		return (ENOMEM);
4548 	}
4549 
4550 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4551 	/*
4552 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4553 	 * at this point because of the following reason. If we can't
4554 	 * enter the ipsq at some point and cv_wait, the writer that
4555 	 * wakes us up tries to locate us using the list of all phyints
4556 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4557 	 * If we don't set it now, we risk a missed wakeup.
4558 	 */
4559 	ill->ill_phyint->phyint_illv4 = ill;
4560 	ill->ill_ppa = UINT_MAX;
4561 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4562 
4563 	if (!ipsq_init(ill, B_TRUE)) {
4564 		freemsg(info_mp);
4565 		mi_free(frag_ptr);
4566 		mi_free(ill->ill_phyint);
4567 		return (ENOMEM);
4568 	}
4569 
4570 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4571 
4572 	/* Frag queue limit stuff */
4573 	ill->ill_frag_count = 0;
4574 	ill->ill_ipf_gen = 0;
4575 
4576 	ill->ill_global_timer = INFINITY;
4577 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4578 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4579 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4580 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4581 
4582 	/*
4583 	 * Initialize IPv6 configuration variables.  The IP module is always
4584 	 * opened as an IPv4 module.  Instead tracking down the cases where
4585 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4586 	 * here for convenience, this has no effect until the ill is set to do
4587 	 * IPv6.
4588 	 */
4589 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4590 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4591 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4592 	ill->ill_max_buf = ND_MAX_Q;
4593 	ill->ill_refcnt = 0;
4594 
4595 	/* Send down the Info Request to the driver. */
4596 	info_mp->b_datap->db_type = M_PCPROTO;
4597 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4598 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4599 	dlir->dl_primitive = DL_INFO_REQ;
4600 
4601 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4602 
4603 	qprocson(q);
4604 	ill_dlpi_send(ill, info_mp);
4605 
4606 	return (0);
4607 }
4608 
4609 /*
4610  * ill_dls_info
4611  * creates datalink socket info from the device.
4612  */
4613 int
4614 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4615 {
4616 	size_t	len;
4617 	ill_t	*ill = ipif->ipif_ill;
4618 
4619 	sdl->sdl_family = AF_LINK;
4620 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4621 	sdl->sdl_type = ill->ill_type;
4622 	ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4623 	len = strlen(sdl->sdl_data);
4624 	ASSERT(len < 256);
4625 	sdl->sdl_nlen = (uchar_t)len;
4626 	sdl->sdl_alen = ill->ill_phys_addr_length;
4627 	sdl->sdl_slen = 0;
4628 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4629 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4630 
4631 	return (sizeof (struct sockaddr_dl));
4632 }
4633 
4634 /*
4635  * ill_xarp_info
4636  * creates xarp info from the device.
4637  */
4638 static int
4639 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4640 {
4641 	sdl->sdl_family = AF_LINK;
4642 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4643 	sdl->sdl_type = ill->ill_type;
4644 	ipif_get_name(ill->ill_ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4645 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4646 	sdl->sdl_alen = ill->ill_phys_addr_length;
4647 	sdl->sdl_slen = 0;
4648 	return (sdl->sdl_nlen);
4649 }
4650 
4651 static int
4652 loopback_kstat_update(kstat_t *ksp, int rw)
4653 {
4654 	kstat_named_t *kn;
4655 	netstackid_t	stackid;
4656 	netstack_t	*ns;
4657 	ip_stack_t	*ipst;
4658 
4659 	if (ksp == NULL || ksp->ks_data == NULL)
4660 		return (EIO);
4661 
4662 	if (rw == KSTAT_WRITE)
4663 		return (EACCES);
4664 
4665 	kn = KSTAT_NAMED_PTR(ksp);
4666 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4667 
4668 	ns = netstack_find_by_stackid(stackid);
4669 	if (ns == NULL)
4670 		return (-1);
4671 
4672 	ipst = ns->netstack_ip;
4673 	if (ipst == NULL) {
4674 		netstack_rele(ns);
4675 		return (-1);
4676 	}
4677 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4678 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4679 	netstack_rele(ns);
4680 	return (0);
4681 }
4682 
4683 /*
4684  * Has ifindex been plumbed already?
4685  */
4686 static boolean_t
4687 phyint_exists(uint_t index, ip_stack_t *ipst)
4688 {
4689 	ASSERT(index != 0);
4690 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4691 
4692 	return (avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4693 	    &index, NULL) != NULL);
4694 }
4695 
4696 /* Pick a unique ifindex */
4697 boolean_t
4698 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4699 {
4700 	uint_t starting_index;
4701 
4702 	if (!ipst->ips_ill_index_wrap) {
4703 		*indexp = ipst->ips_ill_index++;
4704 		if (ipst->ips_ill_index == 0) {
4705 			/* Reached the uint_t limit Next time wrap  */
4706 			ipst->ips_ill_index_wrap = B_TRUE;
4707 		}
4708 		return (B_TRUE);
4709 	}
4710 
4711 	/*
4712 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4713 	 * at this point and don't want to call any function that attempts
4714 	 * to get the lock again.
4715 	 */
4716 	starting_index = ipst->ips_ill_index++;
4717 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
4718 		if (ipst->ips_ill_index != 0 &&
4719 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
4720 			/* found unused index - use it */
4721 			*indexp = ipst->ips_ill_index;
4722 			return (B_TRUE);
4723 		}
4724 	}
4725 
4726 	/*
4727 	 * all interface indicies are inuse.
4728 	 */
4729 	return (B_FALSE);
4730 }
4731 
4732 /*
4733  * Assign a unique interface index for the phyint.
4734  */
4735 static boolean_t
4736 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
4737 {
4738 	ASSERT(phyi->phyint_ifindex == 0);
4739 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
4740 }
4741 
4742 /*
4743  * Return a pointer to the ill which matches the supplied name.  Note that
4744  * the ill name length includes the null termination character.  (May be
4745  * called as writer.)
4746  * If do_alloc and the interface is "lo0" it will be automatically created.
4747  * Cannot bump up reference on condemned ills. So dup detect can't be done
4748  * using this func.
4749  */
4750 ill_t *
4751 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4752     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
4753     ip_stack_t *ipst)
4754 {
4755 	ill_t	*ill;
4756 	ipif_t	*ipif;
4757 	ipsq_t	*ipsq;
4758 	kstat_named_t	*kn;
4759 	boolean_t isloopback;
4760 	in6_addr_t ov6addr;
4761 
4762 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4763 
4764 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4765 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4766 	rw_exit(&ipst->ips_ill_g_lock);
4767 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4768 		return (ill);
4769 
4770 	/*
4771 	 * Couldn't find it.  Does this happen to be a lookup for the
4772 	 * loopback device and are we allowed to allocate it?
4773 	 */
4774 	if (!isloopback || !do_alloc)
4775 		return (NULL);
4776 
4777 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4778 
4779 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4780 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4781 		rw_exit(&ipst->ips_ill_g_lock);
4782 		return (ill);
4783 	}
4784 
4785 	/* Create the loopback device on demand */
4786 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4787 	    sizeof (ipif_loopback_name), BPRI_MED));
4788 	if (ill == NULL)
4789 		goto done;
4790 
4791 	*ill = ill_null;
4792 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4793 	ill->ill_ipst = ipst;
4794 	netstack_hold(ipst->ips_netstack);
4795 	/*
4796 	 * For exclusive stacks we set the zoneid to zero
4797 	 * to make IP operate as if in the global zone.
4798 	 */
4799 	ill->ill_zoneid = GLOBAL_ZONEID;
4800 
4801 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4802 	if (ill->ill_phyint == NULL)
4803 		goto done;
4804 
4805 	if (isv6)
4806 		ill->ill_phyint->phyint_illv6 = ill;
4807 	else
4808 		ill->ill_phyint->phyint_illv4 = ill;
4809 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4810 	ill->ill_max_frag = IP_LOOPBACK_MTU;
4811 	/* Add room for tcp+ip headers */
4812 	if (isv6) {
4813 		ill->ill_isv6 = B_TRUE;
4814 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
4815 	} else {
4816 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
4817 	}
4818 	if (!ill_allocate_mibs(ill))
4819 		goto done;
4820 	ill->ill_max_mtu = ill->ill_max_frag;
4821 	/*
4822 	 * ipif_loopback_name can't be pointed at directly because its used
4823 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
4824 	 * from the glist, ill_glist_delete() sets the first character of
4825 	 * ill_name to '\0'.
4826 	 */
4827 	ill->ill_name = (char *)ill + sizeof (*ill);
4828 	(void) strcpy(ill->ill_name, ipif_loopback_name);
4829 	ill->ill_name_length = sizeof (ipif_loopback_name);
4830 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
4831 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4832 
4833 	ill->ill_global_timer = INFINITY;
4834 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4835 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4836 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4837 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4838 
4839 	/* No resolver here. */
4840 	ill->ill_net_type = IRE_LOOPBACK;
4841 
4842 	/* Initialize the ipsq */
4843 	if (!ipsq_init(ill, B_FALSE))
4844 		goto done;
4845 
4846 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE, B_TRUE);
4847 	if (ipif == NULL)
4848 		goto done;
4849 
4850 	ill->ill_flags = ILLF_MULTICAST;
4851 
4852 	ov6addr = ipif->ipif_v6lcl_addr;
4853 	/* Set up default loopback address and mask. */
4854 	if (!isv6) {
4855 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
4856 
4857 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
4858 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4859 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
4860 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4861 		    ipif->ipif_v6subnet);
4862 		ill->ill_flags |= ILLF_IPV4;
4863 	} else {
4864 		ipif->ipif_v6lcl_addr = ipv6_loopback;
4865 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4866 		ipif->ipif_v6net_mask = ipv6_all_ones;
4867 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4868 		    ipif->ipif_v6subnet);
4869 		ill->ill_flags |= ILLF_IPV6;
4870 	}
4871 
4872 	/*
4873 	 * Chain us in at the end of the ill list. hold the ill
4874 	 * before we make it globally visible. 1 for the lookup.
4875 	 */
4876 	ill->ill_refcnt = 0;
4877 	ill_refhold(ill);
4878 
4879 	ill->ill_frag_count = 0;
4880 	ill->ill_frag_free_num_pkts = 0;
4881 	ill->ill_last_frag_clean_time = 0;
4882 
4883 	ipsq = ill->ill_phyint->phyint_ipsq;
4884 
4885 	if (ill_glist_insert(ill, "lo", isv6) != 0)
4886 		cmn_err(CE_PANIC, "cannot insert loopback interface");
4887 
4888 	/* Let SCTP know so that it can add this to its list */
4889 	sctp_update_ill(ill, SCTP_ILL_INSERT);
4890 
4891 	/*
4892 	 * We have already assigned ipif_v6lcl_addr above, but we need to
4893 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
4894 	 * requires to be after ill_glist_insert() since we need the
4895 	 * ill_index set. Pass on ipv6_loopback as the old address.
4896 	 */
4897 	sctp_update_ipif_addr(ipif, ov6addr);
4898 
4899 	/*
4900 	 * ill_glist_insert() -> ill_phyint_reinit() may have merged IPSQs.
4901 	 * If so, free our original one.
4902 	 */
4903 	if (ipsq != ill->ill_phyint->phyint_ipsq)
4904 		ipsq_delete(ipsq);
4905 
4906 	/*
4907 	 * Delay this till the ipif is allocated as ipif_allocate
4908 	 * de-references ill_phyint for getting the ifindex. We
4909 	 * can't do this before ipif_allocate because ill_phyint_reinit
4910 	 * -> phyint_assign_ifindex expects ipif to be present.
4911 	 */
4912 	mutex_enter(&ill->ill_phyint->phyint_lock);
4913 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
4914 	mutex_exit(&ill->ill_phyint->phyint_lock);
4915 
4916 	if (ipst->ips_loopback_ksp == NULL) {
4917 		/* Export loopback interface statistics */
4918 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
4919 		    ipif_loopback_name, "net",
4920 		    KSTAT_TYPE_NAMED, 2, 0,
4921 		    ipst->ips_netstack->netstack_stackid);
4922 		if (ipst->ips_loopback_ksp != NULL) {
4923 			ipst->ips_loopback_ksp->ks_update =
4924 			    loopback_kstat_update;
4925 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
4926 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
4927 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
4928 			ipst->ips_loopback_ksp->ks_private =
4929 			    (void *)(uintptr_t)ipst->ips_netstack->
4930 			    netstack_stackid;
4931 			kstat_install(ipst->ips_loopback_ksp);
4932 		}
4933 	}
4934 
4935 	if (error != NULL)
4936 		*error = 0;
4937 	*did_alloc = B_TRUE;
4938 	rw_exit(&ipst->ips_ill_g_lock);
4939 	ill_nic_event_dispatch(ill, MAP_IPIF_ID(ill->ill_ipif->ipif_id),
4940 	    NE_PLUMB, ill->ill_name, ill->ill_name_length);
4941 	return (ill);
4942 done:
4943 	if (ill != NULL) {
4944 		if (ill->ill_phyint != NULL) {
4945 			ipsq = ill->ill_phyint->phyint_ipsq;
4946 			if (ipsq != NULL) {
4947 				ipsq->ipsq_phyint = NULL;
4948 				ipsq_delete(ipsq);
4949 			}
4950 			mi_free(ill->ill_phyint);
4951 		}
4952 		ill_free_mib(ill);
4953 		if (ill->ill_ipst != NULL)
4954 			netstack_rele(ill->ill_ipst->ips_netstack);
4955 		mi_free(ill);
4956 	}
4957 	rw_exit(&ipst->ips_ill_g_lock);
4958 	if (error != NULL)
4959 		*error = ENOMEM;
4960 	return (NULL);
4961 }
4962 
4963 /*
4964  * For IPP calls - use the ip_stack_t for global stack.
4965  */
4966 ill_t *
4967 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
4968     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
4969 {
4970 	ip_stack_t	*ipst;
4971 	ill_t		*ill;
4972 
4973 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
4974 	if (ipst == NULL) {
4975 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
4976 		return (NULL);
4977 	}
4978 
4979 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
4980 	netstack_rele(ipst->ips_netstack);
4981 	return (ill);
4982 }
4983 
4984 /*
4985  * Return a pointer to the ill which matches the index and IP version type.
4986  */
4987 ill_t *
4988 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
4989     ipsq_func_t func, int *err, ip_stack_t *ipst)
4990 {
4991 	ill_t	*ill;
4992 	ipsq_t  *ipsq;
4993 	phyint_t *phyi;
4994 
4995 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
4996 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
4997 
4998 	if (err != NULL)
4999 		*err = 0;
5000 
5001 	/*
5002 	 * Indexes are stored in the phyint - a common structure
5003 	 * to both IPv4 and IPv6.
5004 	 */
5005 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5006 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5007 	    (void *) &index, NULL);
5008 	if (phyi != NULL) {
5009 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5010 		if (ill != NULL) {
5011 			/*
5012 			 * The block comment at the start of ipif_down
5013 			 * explains the use of the macros used below
5014 			 */
5015 			GRAB_CONN_LOCK(q);
5016 			mutex_enter(&ill->ill_lock);
5017 			if (ILL_CAN_LOOKUP(ill)) {
5018 				ill_refhold_locked(ill);
5019 				mutex_exit(&ill->ill_lock);
5020 				RELEASE_CONN_LOCK(q);
5021 				rw_exit(&ipst->ips_ill_g_lock);
5022 				return (ill);
5023 			} else if (ILL_CAN_WAIT(ill, q)) {
5024 				ipsq = ill->ill_phyint->phyint_ipsq;
5025 				mutex_enter(&ipsq->ipsq_lock);
5026 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
5027 				rw_exit(&ipst->ips_ill_g_lock);
5028 				mutex_exit(&ill->ill_lock);
5029 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5030 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
5031 				mutex_exit(&ipsq->ipsq_lock);
5032 				RELEASE_CONN_LOCK(q);
5033 				if (err != NULL)
5034 					*err = EINPROGRESS;
5035 				return (NULL);
5036 			}
5037 			RELEASE_CONN_LOCK(q);
5038 			mutex_exit(&ill->ill_lock);
5039 		}
5040 	}
5041 	rw_exit(&ipst->ips_ill_g_lock);
5042 	if (err != NULL)
5043 		*err = ENXIO;
5044 	return (NULL);
5045 }
5046 
5047 /*
5048  * Return the ifindex next in sequence after the passed in ifindex.
5049  * If there is no next ifindex for the given protocol, return 0.
5050  */
5051 uint_t
5052 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5053 {
5054 	phyint_t *phyi;
5055 	phyint_t *phyi_initial;
5056 	uint_t   ifindex;
5057 
5058 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5059 
5060 	if (index == 0) {
5061 		phyi = avl_first(
5062 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5063 	} else {
5064 		phyi = phyi_initial = avl_find(
5065 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5066 		    (void *) &index, NULL);
5067 	}
5068 
5069 	for (; phyi != NULL;
5070 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5071 	    phyi, AVL_AFTER)) {
5072 		/*
5073 		 * If we're not returning the first interface in the tree
5074 		 * and we still haven't moved past the phyint_t that
5075 		 * corresponds to index, avl_walk needs to be called again
5076 		 */
5077 		if (!((index != 0) && (phyi == phyi_initial))) {
5078 			if (isv6) {
5079 				if ((phyi->phyint_illv6) &&
5080 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5081 				    (phyi->phyint_illv6->ill_isv6 == 1))
5082 					break;
5083 			} else {
5084 				if ((phyi->phyint_illv4) &&
5085 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5086 				    (phyi->phyint_illv4->ill_isv6 == 0))
5087 					break;
5088 			}
5089 		}
5090 	}
5091 
5092 	rw_exit(&ipst->ips_ill_g_lock);
5093 
5094 	if (phyi != NULL)
5095 		ifindex = phyi->phyint_ifindex;
5096 	else
5097 		ifindex = 0;
5098 
5099 	return (ifindex);
5100 }
5101 
5102 /*
5103  * Return the ifindex for the named interface.
5104  * If there is no next ifindex for the interface, return 0.
5105  */
5106 uint_t
5107 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5108 {
5109 	phyint_t	*phyi;
5110 	avl_index_t	where = 0;
5111 	uint_t		ifindex;
5112 
5113 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5114 
5115 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5116 	    name, &where)) == NULL) {
5117 		rw_exit(&ipst->ips_ill_g_lock);
5118 		return (0);
5119 	}
5120 
5121 	ifindex = phyi->phyint_ifindex;
5122 
5123 	rw_exit(&ipst->ips_ill_g_lock);
5124 
5125 	return (ifindex);
5126 }
5127 
5128 /*
5129  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5130  * that gives a running thread a reference to the ill. This reference must be
5131  * released by the thread when it is done accessing the ill and related
5132  * objects. ill_refcnt can not be used to account for static references
5133  * such as other structures pointing to an ill. Callers must generally
5134  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5135  * or be sure that the ill is not being deleted or changing state before
5136  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5137  * ill won't change any of its critical state such as address, netmask etc.
5138  */
5139 void
5140 ill_refhold(ill_t *ill)
5141 {
5142 	mutex_enter(&ill->ill_lock);
5143 	ill->ill_refcnt++;
5144 	ILL_TRACE_REF(ill);
5145 	mutex_exit(&ill->ill_lock);
5146 }
5147 
5148 void
5149 ill_refhold_locked(ill_t *ill)
5150 {
5151 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5152 	ill->ill_refcnt++;
5153 	ILL_TRACE_REF(ill);
5154 }
5155 
5156 int
5157 ill_check_and_refhold(ill_t *ill)
5158 {
5159 	mutex_enter(&ill->ill_lock);
5160 	if (ILL_CAN_LOOKUP(ill)) {
5161 		ill_refhold_locked(ill);
5162 		mutex_exit(&ill->ill_lock);
5163 		return (0);
5164 	}
5165 	mutex_exit(&ill->ill_lock);
5166 	return (ILL_LOOKUP_FAILED);
5167 }
5168 
5169 /*
5170  * Must not be called while holding any locks. Otherwise if this is
5171  * the last reference to be released, there is a chance of recursive mutex
5172  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5173  * to restart an ioctl.
5174  */
5175 void
5176 ill_refrele(ill_t *ill)
5177 {
5178 	mutex_enter(&ill->ill_lock);
5179 	ASSERT(ill->ill_refcnt != 0);
5180 	ill->ill_refcnt--;
5181 	ILL_UNTRACE_REF(ill);
5182 	if (ill->ill_refcnt != 0) {
5183 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5184 		mutex_exit(&ill->ill_lock);
5185 		return;
5186 	}
5187 
5188 	/* Drops the ill_lock */
5189 	ipif_ill_refrele_tail(ill);
5190 }
5191 
5192 /*
5193  * Obtain a weak reference count on the ill. This reference ensures the
5194  * ill won't be freed, but the ill may change any of its critical state
5195  * such as netmask, address etc. Returns an error if the ill has started
5196  * closing.
5197  */
5198 boolean_t
5199 ill_waiter_inc(ill_t *ill)
5200 {
5201 	mutex_enter(&ill->ill_lock);
5202 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5203 		mutex_exit(&ill->ill_lock);
5204 		return (B_FALSE);
5205 	}
5206 	ill->ill_waiters++;
5207 	mutex_exit(&ill->ill_lock);
5208 	return (B_TRUE);
5209 }
5210 
5211 void
5212 ill_waiter_dcr(ill_t *ill)
5213 {
5214 	mutex_enter(&ill->ill_lock);
5215 	ill->ill_waiters--;
5216 	if (ill->ill_waiters == 0)
5217 		cv_broadcast(&ill->ill_cv);
5218 	mutex_exit(&ill->ill_lock);
5219 }
5220 
5221 /*
5222  * Named Dispatch routine to produce a formatted report on all ILLs.
5223  * This report is accessed by using the ndd utility to "get" ND variable
5224  * "ip_ill_status".
5225  */
5226 /* ARGSUSED */
5227 int
5228 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5229 {
5230 	ill_t		*ill;
5231 	ill_walk_context_t ctx;
5232 	ip_stack_t	*ipst;
5233 
5234 	ipst = CONNQ_TO_IPST(q);
5235 
5236 	(void) mi_mpprintf(mp,
5237 	    "ILL      " MI_COL_HDRPAD_STR
5238 	/*   01234567[89ABCDEF] */
5239 	    "rq       " MI_COL_HDRPAD_STR
5240 	/*   01234567[89ABCDEF] */
5241 	    "wq       " MI_COL_HDRPAD_STR
5242 	/*   01234567[89ABCDEF] */
5243 	    "upcnt mxfrg err name");
5244 	/*   12345 12345 123 xxxxxxxx  */
5245 
5246 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5247 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5248 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5249 		(void) mi_mpprintf(mp,
5250 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5251 		    "%05u %05u %03d %s",
5252 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5253 		    ill->ill_ipif_up_count,
5254 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5255 	}
5256 	rw_exit(&ipst->ips_ill_g_lock);
5257 
5258 	return (0);
5259 }
5260 
5261 /*
5262  * Named Dispatch routine to produce a formatted report on all IPIFs.
5263  * This report is accessed by using the ndd utility to "get" ND variable
5264  * "ip_ipif_status".
5265  */
5266 /* ARGSUSED */
5267 int
5268 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5269 {
5270 	char	buf1[INET6_ADDRSTRLEN];
5271 	char	buf2[INET6_ADDRSTRLEN];
5272 	char	buf3[INET6_ADDRSTRLEN];
5273 	char	buf4[INET6_ADDRSTRLEN];
5274 	char	buf5[INET6_ADDRSTRLEN];
5275 	char	buf6[INET6_ADDRSTRLEN];
5276 	char	buf[LIFNAMSIZ];
5277 	ill_t	*ill;
5278 	ipif_t	*ipif;
5279 	nv_t	*nvp;
5280 	uint64_t flags;
5281 	zoneid_t zoneid;
5282 	ill_walk_context_t ctx;
5283 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
5284 
5285 	(void) mi_mpprintf(mp,
5286 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5287 	    "\tlocal address\n"
5288 	    "\tsrc address\n"
5289 	    "\tsubnet\n"
5290 	    "\tmask\n"
5291 	    "\tbroadcast\n"
5292 	    "\tp-p-dst");
5293 
5294 	ASSERT(q->q_next == NULL);
5295 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5296 
5297 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5298 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5299 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5300 		for (ipif = ill->ill_ipif; ipif != NULL;
5301 		    ipif = ipif->ipif_next) {
5302 			if (zoneid != GLOBAL_ZONEID &&
5303 			    zoneid != ipif->ipif_zoneid &&
5304 			    ipif->ipif_zoneid != ALL_ZONES)
5305 				continue;
5306 
5307 			ipif_get_name(ipif, buf, sizeof (buf));
5308 			(void) mi_mpprintf(mp,
5309 			    MI_COL_PTRFMT_STR
5310 			    "%04u %05u %u/%u/%u %s %d",
5311 			    (void *)ipif,
5312 			    ipif->ipif_metric, ipif->ipif_mtu,
5313 			    ipif->ipif_ib_pkt_count,
5314 			    ipif->ipif_ob_pkt_count,
5315 			    ipif->ipif_fo_pkt_count,
5316 			    buf,
5317 			    ipif->ipif_zoneid);
5318 
5319 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5320 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5321 
5322 		/* Tack on text strings for any flags. */
5323 		nvp = ipif_nv_tbl;
5324 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5325 			if (nvp->nv_value & flags)
5326 				(void) mi_mpprintf_nr(mp, " %s",
5327 				    nvp->nv_name);
5328 		}
5329 		(void) mi_mpprintf(mp,
5330 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5331 		    inet_ntop(AF_INET6,
5332 		    &ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5333 		    inet_ntop(AF_INET6,
5334 		    &ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5335 		    inet_ntop(AF_INET6,
5336 		    &ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5337 		    inet_ntop(AF_INET6,
5338 		    &ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5339 		    inet_ntop(AF_INET6,
5340 		    &ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5341 		    inet_ntop(AF_INET6,
5342 		    &ipif->ipif_v6pp_dst_addr, buf6, sizeof (buf6)));
5343 		}
5344 	}
5345 	rw_exit(&ipst->ips_ill_g_lock);
5346 	return (0);
5347 }
5348 
5349 /*
5350  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5351  * driver.  We construct best guess defaults for lower level information that
5352  * we need.  If an interface is brought up without injection of any overriding
5353  * information from outside, we have to be ready to go with these defaults.
5354  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5355  * we primarely want the dl_provider_style.
5356  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5357  * at which point we assume the other part of the information is valid.
5358  */
5359 void
5360 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5361 {
5362 	uchar_t		*brdcst_addr;
5363 	uint_t		brdcst_addr_length, phys_addr_length;
5364 	t_scalar_t	sap_length;
5365 	dl_info_ack_t	*dlia;
5366 	ip_m_t		*ipm;
5367 	dl_qos_cl_sel1_t *sel1;
5368 	int		min_mtu;
5369 
5370 	ASSERT(IAM_WRITER_ILL(ill));
5371 
5372 	/*
5373 	 * Till the ill is fully up ILL_CHANGING will be set and
5374 	 * the ill is not globally visible. So no need for a lock.
5375 	 */
5376 	dlia = (dl_info_ack_t *)mp->b_rptr;
5377 	ill->ill_mactype = dlia->dl_mac_type;
5378 
5379 	ipm = ip_m_lookup(dlia->dl_mac_type);
5380 	if (ipm == NULL) {
5381 		ipm = ip_m_lookup(DL_OTHER);
5382 		ASSERT(ipm != NULL);
5383 	}
5384 	ill->ill_media = ipm;
5385 
5386 	/*
5387 	 * When the new DLPI stuff is ready we'll pull lengths
5388 	 * from dlia.
5389 	 */
5390 	if (dlia->dl_version == DL_VERSION_2) {
5391 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5392 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5393 		    brdcst_addr_length);
5394 		if (brdcst_addr == NULL) {
5395 			brdcst_addr_length = 0;
5396 		}
5397 		sap_length = dlia->dl_sap_length;
5398 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5399 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5400 		    brdcst_addr_length, sap_length, phys_addr_length));
5401 	} else {
5402 		brdcst_addr_length = 6;
5403 		brdcst_addr = ip_six_byte_all_ones;
5404 		sap_length = -2;
5405 		phys_addr_length = brdcst_addr_length;
5406 	}
5407 
5408 	ill->ill_bcast_addr_length = brdcst_addr_length;
5409 	ill->ill_phys_addr_length = phys_addr_length;
5410 	ill->ill_sap_length = sap_length;
5411 
5412 	/*
5413 	 * Synthetic DLPI types such as SUNW_DL_IPMP specify a zero SDU,
5414 	 * but we must ensure a minimum IP MTU is used since other bits of
5415 	 * IP will fly apart otherwise.
5416 	 */
5417 	min_mtu = ill->ill_isv6 ? IPV6_MIN_MTU : IP_MIN_MTU;
5418 	ill->ill_max_frag  = MAX(min_mtu, dlia->dl_max_sdu);
5419 	ill->ill_max_mtu = ill->ill_max_frag;
5420 
5421 	ill->ill_type = ipm->ip_m_type;
5422 
5423 	if (!ill->ill_dlpi_style_set) {
5424 		if (dlia->dl_provider_style == DL_STYLE2)
5425 			ill->ill_needs_attach = 1;
5426 
5427 		/*
5428 		 * Allocate the first ipif on this ill. We don't delay it
5429 		 * further as ioctl handling assumes atleast one ipif to
5430 		 * be present.
5431 		 *
5432 		 * At this point we don't know whether the ill is v4 or v6.
5433 		 * We will know this whan the SIOCSLIFNAME happens and
5434 		 * the correct value for ill_isv6 will be assigned in
5435 		 * ipif_set_values(). We need to hold the ill lock and
5436 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5437 		 * the wakeup.
5438 		 */
5439 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5440 		    dlia->dl_provider_style != DL_STYLE2, B_TRUE);
5441 		mutex_enter(&ill->ill_lock);
5442 		ASSERT(ill->ill_dlpi_style_set == 0);
5443 		ill->ill_dlpi_style_set = 1;
5444 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5445 		cv_broadcast(&ill->ill_cv);
5446 		mutex_exit(&ill->ill_lock);
5447 		freemsg(mp);
5448 		return;
5449 	}
5450 	ASSERT(ill->ill_ipif != NULL);
5451 	/*
5452 	 * We know whether it is IPv4 or IPv6 now, as this is the
5453 	 * second DL_INFO_ACK we are recieving in response to the
5454 	 * DL_INFO_REQ sent in ipif_set_values.
5455 	 */
5456 	if (ill->ill_isv6)
5457 		ill->ill_sap = IP6_DL_SAP;
5458 	else
5459 		ill->ill_sap = IP_DL_SAP;
5460 	/*
5461 	 * Set ipif_mtu which is used to set the IRE's
5462 	 * ire_max_frag value. The driver could have sent
5463 	 * a different mtu from what it sent last time. No
5464 	 * need to call ipif_mtu_change because IREs have
5465 	 * not yet been created.
5466 	 */
5467 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5468 	/*
5469 	 * Clear all the flags that were set based on ill_bcast_addr_length
5470 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5471 	 * changed now and we need to re-evaluate.
5472 	 */
5473 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5474 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5475 
5476 	/*
5477 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5478 	 * changed now.
5479 	 *
5480 	 * NOTE: The IPMP meta-interface is special-cased because it starts
5481 	 * with no underlying interfaces (and thus an unknown broadcast
5482 	 * address length), but we enforce that an interface is broadcast-
5483 	 * capable as part of allowing it to join a group.
5484 	 */
5485 	if (ill->ill_bcast_addr_length == 0 && !IS_IPMP(ill)) {
5486 		if (ill->ill_resolver_mp != NULL)
5487 			freemsg(ill->ill_resolver_mp);
5488 		if (ill->ill_bcast_mp != NULL)
5489 			freemsg(ill->ill_bcast_mp);
5490 		if (ill->ill_flags & ILLF_XRESOLV)
5491 			ill->ill_net_type = IRE_IF_RESOLVER;
5492 		else
5493 			ill->ill_net_type = IRE_IF_NORESOLVER;
5494 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5495 		    ill->ill_phys_addr_length,
5496 		    ill->ill_sap,
5497 		    ill->ill_sap_length);
5498 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5499 
5500 		if (ill->ill_isv6)
5501 			/*
5502 			 * Note: xresolv interfaces will eventually need NOARP
5503 			 * set here as well, but that will require those
5504 			 * external resolvers to have some knowledge of
5505 			 * that flag and act appropriately. Not to be changed
5506 			 * at present.
5507 			 */
5508 			ill->ill_flags |= ILLF_NONUD;
5509 		else
5510 			ill->ill_flags |= ILLF_NOARP;
5511 
5512 		if (ill->ill_phys_addr_length == 0) {
5513 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5514 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5515 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5516 			} else {
5517 				/* pt-pt supports multicast. */
5518 				ill->ill_flags |= ILLF_MULTICAST;
5519 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5520 			}
5521 		}
5522 	} else {
5523 		ill->ill_net_type = IRE_IF_RESOLVER;
5524 		if (ill->ill_bcast_mp != NULL)
5525 			freemsg(ill->ill_bcast_mp);
5526 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5527 		    ill->ill_bcast_addr_length, ill->ill_sap,
5528 		    ill->ill_sap_length);
5529 		/*
5530 		 * Later detect lack of DLPI driver multicast
5531 		 * capability by catching DL_ENABMULTI errors in
5532 		 * ip_rput_dlpi.
5533 		 */
5534 		ill->ill_flags |= ILLF_MULTICAST;
5535 		if (!ill->ill_isv6)
5536 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5537 	}
5538 
5539 	/* For IPMP, PHYI_IPMP should already be set by ipif_allocate() */
5540 	if (ill->ill_mactype == SUNW_DL_IPMP)
5541 		ASSERT(ill->ill_phyint->phyint_flags & PHYI_IPMP);
5542 
5543 	/* By default an interface does not support any CoS marking */
5544 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5545 
5546 	/*
5547 	 * If we get QoS information in DL_INFO_ACK, the device supports
5548 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5549 	 */
5550 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5551 	    dlia->dl_qos_length);
5552 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5553 		ill->ill_flags |= ILLF_COS_ENABLED;
5554 	}
5555 
5556 	/* Clear any previous error indication. */
5557 	ill->ill_error = 0;
5558 	freemsg(mp);
5559 }
5560 
5561 /*
5562  * Perform various checks to verify that an address would make sense as a
5563  * local, remote, or subnet interface address.
5564  */
5565 static boolean_t
5566 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5567 {
5568 	ipaddr_t	net_mask;
5569 
5570 	/*
5571 	 * Don't allow all zeroes, or all ones, but allow
5572 	 * all ones netmask.
5573 	 */
5574 	if ((net_mask = ip_net_mask(addr)) == 0)
5575 		return (B_FALSE);
5576 	/* A given netmask overrides the "guess" netmask */
5577 	if (subnet_mask != 0)
5578 		net_mask = subnet_mask;
5579 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5580 	    (addr == (addr | ~net_mask)))) {
5581 		return (B_FALSE);
5582 	}
5583 
5584 	/*
5585 	 * Even if the netmask is all ones, we do not allow address to be
5586 	 * 255.255.255.255
5587 	 */
5588 	if (addr == INADDR_BROADCAST)
5589 		return (B_FALSE);
5590 
5591 	if (CLASSD(addr))
5592 		return (B_FALSE);
5593 
5594 	return (B_TRUE);
5595 }
5596 
5597 #define	V6_IPIF_LINKLOCAL(p)	\
5598 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
5599 
5600 /*
5601  * Compare two given ipifs and check if the second one is better than
5602  * the first one using the order of preference (not taking deprecated
5603  * into acount) specified in ipif_lookup_multicast().
5604  */
5605 static boolean_t
5606 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
5607 {
5608 	/* Check the least preferred first. */
5609 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
5610 		/* If both ipifs are the same, use the first one. */
5611 		if (IS_LOOPBACK(new_ipif->ipif_ill))
5612 			return (B_FALSE);
5613 		else
5614 			return (B_TRUE);
5615 	}
5616 
5617 	/* For IPv6, check for link local address. */
5618 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
5619 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5620 		    V6_IPIF_LINKLOCAL(new_ipif)) {
5621 			/* The second one is equal or less preferred. */
5622 			return (B_FALSE);
5623 		} else {
5624 			return (B_TRUE);
5625 		}
5626 	}
5627 
5628 	/* Then check for point to point interface. */
5629 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
5630 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5631 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
5632 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
5633 			return (B_FALSE);
5634 		} else {
5635 			return (B_TRUE);
5636 		}
5637 	}
5638 
5639 	/* old_ipif is a normal interface, so no need to use the new one. */
5640 	return (B_FALSE);
5641 }
5642 
5643 /*
5644  * Find a mulitcast-capable ipif given an IP instance and zoneid.
5645  * The ipif must be up, and its ill must multicast-capable, not
5646  * condemned, not an underlying interface in an IPMP group, and
5647  * not a VNI interface.  Order of preference:
5648  *
5649  * 	1a. normal
5650  * 	1b. normal, but deprecated
5651  * 	2a. point to point
5652  * 	2b. point to point, but deprecated
5653  * 	3a. link local
5654  * 	3b. link local, but deprecated
5655  * 	4. loopback.
5656  */
5657 ipif_t *
5658 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
5659 {
5660 	ill_t			*ill;
5661 	ill_walk_context_t	ctx;
5662 	ipif_t			*ipif;
5663 	ipif_t			*saved_ipif = NULL;
5664 	ipif_t			*dep_ipif = NULL;
5665 
5666 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5667 	if (isv6)
5668 		ill = ILL_START_WALK_V6(&ctx, ipst);
5669 	else
5670 		ill = ILL_START_WALK_V4(&ctx, ipst);
5671 
5672 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5673 		mutex_enter(&ill->ill_lock);
5674 		if (IS_VNI(ill) || IS_UNDER_IPMP(ill) || !ILL_CAN_LOOKUP(ill) ||
5675 		    !(ill->ill_flags & ILLF_MULTICAST)) {
5676 			mutex_exit(&ill->ill_lock);
5677 			continue;
5678 		}
5679 		for (ipif = ill->ill_ipif; ipif != NULL;
5680 		    ipif = ipif->ipif_next) {
5681 			if (zoneid != ipif->ipif_zoneid &&
5682 			    zoneid != ALL_ZONES &&
5683 			    ipif->ipif_zoneid != ALL_ZONES) {
5684 				continue;
5685 			}
5686 			if (!(ipif->ipif_flags & IPIF_UP) ||
5687 			    !IPIF_CAN_LOOKUP(ipif)) {
5688 				continue;
5689 			}
5690 
5691 			/*
5692 			 * Found one candidate.  If it is deprecated,
5693 			 * remember it in dep_ipif.  If it is not deprecated,
5694 			 * remember it in saved_ipif.
5695 			 */
5696 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
5697 				if (dep_ipif == NULL) {
5698 					dep_ipif = ipif;
5699 				} else if (ipif_comp_multi(dep_ipif, ipif,
5700 				    isv6)) {
5701 					/*
5702 					 * If the previous dep_ipif does not
5703 					 * belong to the same ill, we've done
5704 					 * a ipif_refhold() on it.  So we need
5705 					 * to release it.
5706 					 */
5707 					if (dep_ipif->ipif_ill != ill)
5708 						ipif_refrele(dep_ipif);
5709 					dep_ipif = ipif;
5710 				}
5711 				continue;
5712 			}
5713 			if (saved_ipif == NULL) {
5714 				saved_ipif = ipif;
5715 			} else {
5716 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
5717 					if (saved_ipif->ipif_ill != ill)
5718 						ipif_refrele(saved_ipif);
5719 					saved_ipif = ipif;
5720 				}
5721 			}
5722 		}
5723 		/*
5724 		 * Before going to the next ill, do a ipif_refhold() on the
5725 		 * saved ones.
5726 		 */
5727 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
5728 			ipif_refhold_locked(saved_ipif);
5729 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
5730 			ipif_refhold_locked(dep_ipif);
5731 		mutex_exit(&ill->ill_lock);
5732 	}
5733 	rw_exit(&ipst->ips_ill_g_lock);
5734 
5735 	/*
5736 	 * If we have only the saved_ipif, return it.  But if we have both
5737 	 * saved_ipif and dep_ipif, check to see which one is better.
5738 	 */
5739 	if (saved_ipif != NULL) {
5740 		if (dep_ipif != NULL) {
5741 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
5742 				ipif_refrele(saved_ipif);
5743 				return (dep_ipif);
5744 			} else {
5745 				ipif_refrele(dep_ipif);
5746 				return (saved_ipif);
5747 			}
5748 		}
5749 		return (saved_ipif);
5750 	} else {
5751 		return (dep_ipif);
5752 	}
5753 }
5754 
5755 /*
5756  * This function is called when an application does not specify an interface
5757  * to be used for multicast traffic (joining a group/sending data).  It
5758  * calls ire_lookup_multi() to look for an interface route for the
5759  * specified multicast group.  Doing this allows the administrator to add
5760  * prefix routes for multicast to indicate which interface to be used for
5761  * multicast traffic in the above scenario.  The route could be for all
5762  * multicast (224.0/4), for a single multicast group (a /32 route) or
5763  * anything in between.  If there is no such multicast route, we just find
5764  * any multicast capable interface and return it.  The returned ipif
5765  * is refhold'ed.
5766  */
5767 ipif_t *
5768 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5769 {
5770 	ire_t			*ire;
5771 	ipif_t			*ipif;
5772 
5773 	ire = ire_lookup_multi(group, zoneid, ipst);
5774 	if (ire != NULL) {
5775 		ipif = ire->ire_ipif;
5776 		ipif_refhold(ipif);
5777 		ire_refrele(ire);
5778 		return (ipif);
5779 	}
5780 
5781 	return (ipif_lookup_multicast(ipst, zoneid, B_FALSE));
5782 }
5783 
5784 /*
5785  * Look for an ipif with the specified interface address and destination.
5786  * The destination address is used only for matching point-to-point interfaces.
5787  */
5788 ipif_t *
5789 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5790     ipsq_func_t func, int *error, ip_stack_t *ipst)
5791 {
5792 	ipif_t	*ipif;
5793 	ill_t	*ill;
5794 	ill_walk_context_t ctx;
5795 	ipsq_t	*ipsq;
5796 
5797 	if (error != NULL)
5798 		*error = 0;
5799 
5800 	/*
5801 	 * First match all the point-to-point interfaces
5802 	 * before looking at non-point-to-point interfaces.
5803 	 * This is done to avoid returning non-point-to-point
5804 	 * ipif instead of unnumbered point-to-point ipif.
5805 	 */
5806 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5807 	ill = ILL_START_WALK_V4(&ctx, ipst);
5808 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5809 		GRAB_CONN_LOCK(q);
5810 		mutex_enter(&ill->ill_lock);
5811 		for (ipif = ill->ill_ipif; ipif != NULL;
5812 		    ipif = ipif->ipif_next) {
5813 			/* Allow the ipif to be down */
5814 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5815 			    (ipif->ipif_lcl_addr == if_addr) &&
5816 			    (ipif->ipif_pp_dst_addr == dst)) {
5817 				/*
5818 				 * The block comment at the start of ipif_down
5819 				 * explains the use of the macros used below
5820 				 */
5821 				if (IPIF_CAN_LOOKUP(ipif)) {
5822 					ipif_refhold_locked(ipif);
5823 					mutex_exit(&ill->ill_lock);
5824 					RELEASE_CONN_LOCK(q);
5825 					rw_exit(&ipst->ips_ill_g_lock);
5826 					return (ipif);
5827 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5828 					ipsq = ill->ill_phyint->phyint_ipsq;
5829 					mutex_enter(&ipsq->ipsq_lock);
5830 					mutex_enter(&ipsq->ipsq_xop->ipx_lock);
5831 					mutex_exit(&ill->ill_lock);
5832 					rw_exit(&ipst->ips_ill_g_lock);
5833 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5834 					    ill);
5835 					mutex_exit(&ipsq->ipsq_xop->ipx_lock);
5836 					mutex_exit(&ipsq->ipsq_lock);
5837 					RELEASE_CONN_LOCK(q);
5838 					if (error != NULL)
5839 						*error = EINPROGRESS;
5840 					return (NULL);
5841 				}
5842 			}
5843 		}
5844 		mutex_exit(&ill->ill_lock);
5845 		RELEASE_CONN_LOCK(q);
5846 	}
5847 	rw_exit(&ipst->ips_ill_g_lock);
5848 
5849 	/* lookup the ipif based on interface address */
5850 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
5851 	    ipst);
5852 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5853 	return (ipif);
5854 }
5855 
5856 /*
5857  * Common function for ipif_lookup_addr() and ipif_lookup_addr_exact().
5858  */
5859 static ipif_t *
5860 ipif_lookup_addr_common(ipaddr_t addr, ill_t *match_ill, boolean_t match_illgrp,
5861     zoneid_t zoneid, queue_t *q, mblk_t *mp, ipsq_func_t func, int *error,
5862     ip_stack_t *ipst)
5863 {
5864 	ipif_t  *ipif;
5865 	ill_t   *ill;
5866 	boolean_t ptp = B_FALSE;
5867 	ipsq_t	*ipsq;
5868 	ill_walk_context_t	ctx;
5869 
5870 	if (error != NULL)
5871 		*error = 0;
5872 
5873 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5874 	/*
5875 	 * Repeat twice, first based on local addresses and
5876 	 * next time for pointopoint.
5877 	 */
5878 repeat:
5879 	ill = ILL_START_WALK_V4(&ctx, ipst);
5880 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5881 		if (match_ill != NULL && ill != match_ill &&
5882 		    (!match_illgrp || !IS_IN_SAME_ILLGRP(ill, match_ill))) {
5883 			continue;
5884 		}
5885 		GRAB_CONN_LOCK(q);
5886 		mutex_enter(&ill->ill_lock);
5887 		for (ipif = ill->ill_ipif; ipif != NULL;
5888 		    ipif = ipif->ipif_next) {
5889 			if (zoneid != ALL_ZONES &&
5890 			    zoneid != ipif->ipif_zoneid &&
5891 			    ipif->ipif_zoneid != ALL_ZONES)
5892 				continue;
5893 			/* Allow the ipif to be down */
5894 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5895 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5896 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
5897 			    (ipif->ipif_pp_dst_addr == addr))) {
5898 				/*
5899 				 * The block comment at the start of ipif_down
5900 				 * explains the use of the macros used below
5901 				 */
5902 				if (IPIF_CAN_LOOKUP(ipif)) {
5903 					ipif_refhold_locked(ipif);
5904 					mutex_exit(&ill->ill_lock);
5905 					RELEASE_CONN_LOCK(q);
5906 					rw_exit(&ipst->ips_ill_g_lock);
5907 					return (ipif);
5908 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5909 					ipsq = ill->ill_phyint->phyint_ipsq;
5910 					mutex_enter(&ipsq->ipsq_lock);
5911 					mutex_enter(&ipsq->ipsq_xop->ipx_lock);
5912 					mutex_exit(&ill->ill_lock);
5913 					rw_exit(&ipst->ips_ill_g_lock);
5914 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5915 					    ill);
5916 					mutex_exit(&ipsq->ipsq_xop->ipx_lock);
5917 					mutex_exit(&ipsq->ipsq_lock);
5918 					RELEASE_CONN_LOCK(q);
5919 					if (error != NULL)
5920 						*error = EINPROGRESS;
5921 					return (NULL);
5922 				}
5923 			}
5924 		}
5925 		mutex_exit(&ill->ill_lock);
5926 		RELEASE_CONN_LOCK(q);
5927 	}
5928 
5929 	/* If we already did the ptp case, then we are done */
5930 	if (ptp) {
5931 		rw_exit(&ipst->ips_ill_g_lock);
5932 		if (error != NULL)
5933 			*error = ENXIO;
5934 		return (NULL);
5935 	}
5936 	ptp = B_TRUE;
5937 	goto repeat;
5938 }
5939 
5940 /*
5941  * Check if the address exists in the system.
5942  * We don't hold the conn_lock as we will not perform defered ipsqueue
5943  * operation.
5944  */
5945 boolean_t
5946 ip_addr_exists(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
5947 {
5948 	ipif_t  *ipif;
5949 	ill_t   *ill;
5950 	ill_walk_context_t	ctx;
5951 
5952 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5953 
5954 	ill = ILL_START_WALK_V4(&ctx, ipst);
5955 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5956 		mutex_enter(&ill->ill_lock);
5957 		for (ipif = ill->ill_ipif; ipif != NULL;
5958 		    ipif = ipif->ipif_next) {
5959 			if (zoneid != ALL_ZONES &&
5960 			    zoneid != ipif->ipif_zoneid &&
5961 			    ipif->ipif_zoneid != ALL_ZONES)
5962 				continue;
5963 			/* Allow the ipif to be down */
5964 			/*
5965 			 * XXX Different from ipif_lookup_addr(), we don't do
5966 			 * twice lookups. As from bind()'s point of view, we
5967 			 * may return once we find a match.
5968 			 */
5969 			if (((ipif->ipif_lcl_addr == addr) &&
5970 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5971 			    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5972 			    (ipif->ipif_pp_dst_addr == addr))) {
5973 				/*
5974 				 * Allow bind() to be successful even if the
5975 				 * ipif is with IPIF_CHANGING bit set.
5976 				 */
5977 				mutex_exit(&ill->ill_lock);
5978 				rw_exit(&ipst->ips_ill_g_lock);
5979 				return (B_TRUE);
5980 			}
5981 		}
5982 		mutex_exit(&ill->ill_lock);
5983 	}
5984 
5985 	rw_exit(&ipst->ips_ill_g_lock);
5986 	return (B_FALSE);
5987 }
5988 
5989 /*
5990  * Lookup an ipif with the specified address.  For point-to-point links we
5991  * look for matches on either the destination address or the local address,
5992  * but we skip the local address check if IPIF_UNNUMBERED is set.  If the
5993  * `match_ill' argument is non-NULL, the lookup is restricted to that ill
5994  * (or illgrp if `match_ill' is in an IPMP group).
5995  */
5996 ipif_t *
5997 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
5998     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
5999 {
6000 	return (ipif_lookup_addr_common(addr, match_ill, B_TRUE, zoneid, q, mp,
6001 	    func, error, ipst));
6002 }
6003 
6004 /*
6005  * Special abbreviated version of ipif_lookup_addr() that doesn't match
6006  * `match_ill' across the IPMP group.  This function is only needed in some
6007  * corner-cases; almost everything should use ipif_lookup_addr().
6008  */
6009 static ipif_t *
6010 ipif_lookup_addr_exact(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6011 {
6012 	ASSERT(match_ill != NULL);
6013 	return (ipif_lookup_addr_common(addr, match_ill, B_FALSE, ALL_ZONES,
6014 	    NULL, NULL, NULL, NULL, ipst));
6015 }
6016 
6017 /*
6018  * Look for an ipif with the specified address. For point-point links
6019  * we look for matches on either the destination address and the local
6020  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6021  * is set.
6022  * If the `match_ill' argument is non-NULL, the lookup is restricted to that
6023  * ill (or illgrp if `match_ill' is in an IPMP group).
6024  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
6025  */
6026 zoneid_t
6027 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6028 {
6029 	zoneid_t zoneid;
6030 	ipif_t  *ipif;
6031 	ill_t   *ill;
6032 	boolean_t ptp = B_FALSE;
6033 	ill_walk_context_t	ctx;
6034 
6035 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6036 	/*
6037 	 * Repeat twice, first based on local addresses and
6038 	 * next time for pointopoint.
6039 	 */
6040 repeat:
6041 	ill = ILL_START_WALK_V4(&ctx, ipst);
6042 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6043 		if (match_ill != NULL && ill != match_ill &&
6044 		    !IS_IN_SAME_ILLGRP(ill, match_ill)) {
6045 			continue;
6046 		}
6047 		mutex_enter(&ill->ill_lock);
6048 		for (ipif = ill->ill_ipif; ipif != NULL;
6049 		    ipif = ipif->ipif_next) {
6050 			/* Allow the ipif to be down */
6051 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6052 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6053 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6054 			    (ipif->ipif_pp_dst_addr == addr)) &&
6055 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
6056 				zoneid = ipif->ipif_zoneid;
6057 				mutex_exit(&ill->ill_lock);
6058 				rw_exit(&ipst->ips_ill_g_lock);
6059 				/*
6060 				 * If ipif_zoneid was ALL_ZONES then we have
6061 				 * a trusted extensions shared IP address.
6062 				 * In that case GLOBAL_ZONEID works to send.
6063 				 */
6064 				if (zoneid == ALL_ZONES)
6065 					zoneid = GLOBAL_ZONEID;
6066 				return (zoneid);
6067 			}
6068 		}
6069 		mutex_exit(&ill->ill_lock);
6070 	}
6071 
6072 	/* If we already did the ptp case, then we are done */
6073 	if (ptp) {
6074 		rw_exit(&ipst->ips_ill_g_lock);
6075 		return (ALL_ZONES);
6076 	}
6077 	ptp = B_TRUE;
6078 	goto repeat;
6079 }
6080 
6081 /*
6082  * Look for an ipif that matches the specified remote address i.e. the
6083  * ipif that would receive the specified packet.
6084  * First look for directly connected interfaces and then do a recursive
6085  * IRE lookup and pick the first ipif corresponding to the source address in the
6086  * ire.
6087  * Returns: held ipif
6088  */
6089 ipif_t *
6090 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6091 {
6092 	ipif_t	*ipif;
6093 	ire_t	*ire;
6094 	ip_stack_t	*ipst = ill->ill_ipst;
6095 
6096 	ASSERT(!ill->ill_isv6);
6097 
6098 	/*
6099 	 * Someone could be changing this ipif currently or change it
6100 	 * after we return this. Thus  a few packets could use the old
6101 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6102 	 * will atomically be updated or cleaned up with the new value
6103 	 * Thus we don't need a lock to check the flags or other attrs below.
6104 	 */
6105 	mutex_enter(&ill->ill_lock);
6106 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6107 		if (!IPIF_CAN_LOOKUP(ipif))
6108 			continue;
6109 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6110 		    ipif->ipif_zoneid != ALL_ZONES)
6111 			continue;
6112 		/* Allow the ipif to be down */
6113 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6114 			if ((ipif->ipif_pp_dst_addr == addr) ||
6115 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6116 			    ipif->ipif_lcl_addr == addr)) {
6117 				ipif_refhold_locked(ipif);
6118 				mutex_exit(&ill->ill_lock);
6119 				return (ipif);
6120 			}
6121 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6122 			ipif_refhold_locked(ipif);
6123 			mutex_exit(&ill->ill_lock);
6124 			return (ipif);
6125 		}
6126 	}
6127 	mutex_exit(&ill->ill_lock);
6128 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6129 	    NULL, MATCH_IRE_RECURSIVE, ipst);
6130 	if (ire != NULL) {
6131 		/*
6132 		 * The callers of this function wants to know the
6133 		 * interface on which they have to send the replies
6134 		 * back. For IREs that have ire_stq and ire_ipif
6135 		 * derived from different ills, we really don't care
6136 		 * what we return here.
6137 		 */
6138 		ipif = ire->ire_ipif;
6139 		if (ipif != NULL) {
6140 			ipif_refhold(ipif);
6141 			ire_refrele(ire);
6142 			return (ipif);
6143 		}
6144 		ire_refrele(ire);
6145 	}
6146 	/* Pick the first interface */
6147 	ipif = ipif_get_next_ipif(NULL, ill);
6148 	return (ipif);
6149 }
6150 
6151 /*
6152  * This func does not prevent refcnt from increasing. But if
6153  * the caller has taken steps to that effect, then this func
6154  * can be used to determine whether the ill has become quiescent
6155  */
6156 static boolean_t
6157 ill_is_quiescent(ill_t *ill)
6158 {
6159 	ipif_t	*ipif;
6160 
6161 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6162 
6163 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6164 		if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6165 			return (B_FALSE);
6166 		}
6167 	}
6168 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6169 		return (B_FALSE);
6170 	}
6171 	return (B_TRUE);
6172 }
6173 
6174 boolean_t
6175 ill_is_freeable(ill_t *ill)
6176 {
6177 	ipif_t	*ipif;
6178 
6179 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6180 
6181 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6182 		if (ipif->ipif_refcnt != 0 || !IPIF_FREE_OK(ipif)) {
6183 			return (B_FALSE);
6184 		}
6185 	}
6186 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
6187 		return (B_FALSE);
6188 	}
6189 	return (B_TRUE);
6190 }
6191 
6192 /*
6193  * This func does not prevent refcnt from increasing. But if
6194  * the caller has taken steps to that effect, then this func
6195  * can be used to determine whether the ipif has become quiescent
6196  */
6197 static boolean_t
6198 ipif_is_quiescent(ipif_t *ipif)
6199 {
6200 	ill_t *ill;
6201 
6202 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6203 
6204 	if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6205 		return (B_FALSE);
6206 	}
6207 
6208 	ill = ipif->ipif_ill;
6209 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6210 	    ill->ill_logical_down) {
6211 		return (B_TRUE);
6212 	}
6213 
6214 	/* This is the last ipif going down or being deleted on this ill */
6215 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6216 		return (B_FALSE);
6217 	}
6218 
6219 	return (B_TRUE);
6220 }
6221 
6222 /*
6223  * return true if the ipif can be destroyed: the ipif has to be quiescent
6224  * with zero references from ire/nce/ilm to it.
6225  */
6226 static boolean_t
6227 ipif_is_freeable(ipif_t *ipif)
6228 {
6229 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6230 	ASSERT(ipif->ipif_id != 0);
6231 	return (ipif->ipif_refcnt == 0 && IPIF_FREE_OK(ipif));
6232 }
6233 
6234 /*
6235  * The ipif/ill/ire has been refreled. Do the tail processing.
6236  * Determine if the ipif or ill in question has become quiescent and if so
6237  * wakeup close and/or restart any queued pending ioctl that is waiting
6238  * for the ipif_down (or ill_down)
6239  */
6240 void
6241 ipif_ill_refrele_tail(ill_t *ill)
6242 {
6243 	mblk_t	*mp;
6244 	conn_t	*connp;
6245 	ipsq_t	*ipsq;
6246 	ipxop_t	*ipx;
6247 	ipif_t	*ipif;
6248 	dl_notify_ind_t *dlindp;
6249 
6250 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6251 
6252 	if ((ill->ill_state_flags & ILL_CONDEMNED) && ill_is_freeable(ill)) {
6253 		/* ip_modclose() may be waiting */
6254 		cv_broadcast(&ill->ill_cv);
6255 	}
6256 
6257 	ipsq = ill->ill_phyint->phyint_ipsq;
6258 	mutex_enter(&ipsq->ipsq_lock);
6259 	ipx = ipsq->ipsq_xop;
6260 	mutex_enter(&ipx->ipx_lock);
6261 	if (ipx->ipx_waitfor == 0)	/* no one's waiting; bail */
6262 		goto unlock;
6263 
6264 	ASSERT(ipx->ipx_pending_mp != NULL && ipx->ipx_pending_ipif != NULL);
6265 
6266 	ipif = ipx->ipx_pending_ipif;
6267 	if (ipif->ipif_ill != ill) 	/* wait is for another ill; bail */
6268 		goto unlock;
6269 
6270 	switch (ipx->ipx_waitfor) {
6271 	case IPIF_DOWN:
6272 		if (!ipif_is_quiescent(ipif))
6273 			goto unlock;
6274 		break;
6275 	case IPIF_FREE:
6276 		if (!ipif_is_freeable(ipif))
6277 			goto unlock;
6278 		break;
6279 	case ILL_DOWN:
6280 		if (!ill_is_quiescent(ill))
6281 			goto unlock;
6282 		break;
6283 	case ILL_FREE:
6284 		/*
6285 		 * ILL_FREE is only for loopback; normal ill teardown waits
6286 		 * synchronously in ip_modclose() without using ipx_waitfor,
6287 		 * handled by the cv_broadcast() at the top of this function.
6288 		 */
6289 		if (!ill_is_freeable(ill))
6290 			goto unlock;
6291 		break;
6292 	default:
6293 		cmn_err(CE_PANIC, "ipsq: %p unknown ipx_waitfor %d\n",
6294 		    (void *)ipsq, ipx->ipx_waitfor);
6295 	}
6296 
6297 	ill_refhold_locked(ill);	/* for qwriter_ip() call below */
6298 	mutex_exit(&ipx->ipx_lock);
6299 	mp = ipsq_pending_mp_get(ipsq, &connp);
6300 	mutex_exit(&ipsq->ipsq_lock);
6301 	mutex_exit(&ill->ill_lock);
6302 
6303 	ASSERT(mp != NULL);
6304 	/*
6305 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6306 	 * we can only get here when the current operation decides it
6307 	 * it needs to quiesce via ipsq_pending_mp_add().
6308 	 */
6309 	switch (mp->b_datap->db_type) {
6310 	case M_PCPROTO:
6311 	case M_PROTO:
6312 		/*
6313 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6314 		 */
6315 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6316 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6317 
6318 		switch (dlindp->dl_notification) {
6319 		case DL_NOTE_PHYS_ADDR:
6320 			qwriter_ip(ill, ill->ill_rq, mp,
6321 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6322 			return;
6323 		default:
6324 			ASSERT(0);
6325 			ill_refrele(ill);
6326 		}
6327 		break;
6328 
6329 	case M_ERROR:
6330 	case M_HANGUP:
6331 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6332 		    B_TRUE);
6333 		return;
6334 
6335 	case M_IOCTL:
6336 	case M_IOCDATA:
6337 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6338 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6339 		return;
6340 
6341 	default:
6342 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6343 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6344 	}
6345 	return;
6346 unlock:
6347 	mutex_exit(&ipsq->ipsq_lock);
6348 	mutex_exit(&ipx->ipx_lock);
6349 	mutex_exit(&ill->ill_lock);
6350 }
6351 
6352 #ifdef DEBUG
6353 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6354 static void
6355 th_trace_rrecord(th_trace_t *th_trace)
6356 {
6357 	tr_buf_t *tr_buf;
6358 	uint_t lastref;
6359 
6360 	lastref = th_trace->th_trace_lastref;
6361 	lastref++;
6362 	if (lastref == TR_BUF_MAX)
6363 		lastref = 0;
6364 	th_trace->th_trace_lastref = lastref;
6365 	tr_buf = &th_trace->th_trbuf[lastref];
6366 	tr_buf->tr_time = lbolt;
6367 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
6368 }
6369 
6370 static void
6371 th_trace_free(void *value)
6372 {
6373 	th_trace_t *th_trace = value;
6374 
6375 	ASSERT(th_trace->th_refcnt == 0);
6376 	kmem_free(th_trace, sizeof (*th_trace));
6377 }
6378 
6379 /*
6380  * Find or create the per-thread hash table used to track object references.
6381  * The ipst argument is NULL if we shouldn't allocate.
6382  *
6383  * Accesses per-thread data, so there's no need to lock here.
6384  */
6385 static mod_hash_t *
6386 th_trace_gethash(ip_stack_t *ipst)
6387 {
6388 	th_hash_t *thh;
6389 
6390 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
6391 		mod_hash_t *mh;
6392 		char name[256];
6393 		size_t objsize, rshift;
6394 		int retv;
6395 
6396 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
6397 			return (NULL);
6398 		(void) snprintf(name, sizeof (name), "th_trace_%p",
6399 		    (void *)curthread);
6400 
6401 		/*
6402 		 * We use mod_hash_create_extended here rather than the more
6403 		 * obvious mod_hash_create_ptrhash because the latter has a
6404 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
6405 		 * block.
6406 		 */
6407 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
6408 		    MAX(sizeof (ire_t), sizeof (nce_t)));
6409 		rshift = highbit(objsize);
6410 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
6411 		    th_trace_free, mod_hash_byptr, (void *)rshift,
6412 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
6413 		if (mh == NULL) {
6414 			kmem_free(thh, sizeof (*thh));
6415 			return (NULL);
6416 		}
6417 		thh->thh_hash = mh;
6418 		thh->thh_ipst = ipst;
6419 		/*
6420 		 * We trace ills, ipifs, ires, and nces.  All of these are
6421 		 * per-IP-stack, so the lock on the thread list is as well.
6422 		 */
6423 		rw_enter(&ip_thread_rwlock, RW_WRITER);
6424 		list_insert_tail(&ip_thread_list, thh);
6425 		rw_exit(&ip_thread_rwlock);
6426 		retv = tsd_set(ip_thread_data, thh);
6427 		ASSERT(retv == 0);
6428 	}
6429 	return (thh != NULL ? thh->thh_hash : NULL);
6430 }
6431 
6432 boolean_t
6433 th_trace_ref(const void *obj, ip_stack_t *ipst)
6434 {
6435 	th_trace_t *th_trace;
6436 	mod_hash_t *mh;
6437 	mod_hash_val_t val;
6438 
6439 	if ((mh = th_trace_gethash(ipst)) == NULL)
6440 		return (B_FALSE);
6441 
6442 	/*
6443 	 * Attempt to locate the trace buffer for this obj and thread.
6444 	 * If it does not exist, then allocate a new trace buffer and
6445 	 * insert into the hash.
6446 	 */
6447 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
6448 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
6449 		if (th_trace == NULL)
6450 			return (B_FALSE);
6451 
6452 		th_trace->th_id = curthread;
6453 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
6454 		    (mod_hash_val_t)th_trace) != 0) {
6455 			kmem_free(th_trace, sizeof (th_trace_t));
6456 			return (B_FALSE);
6457 		}
6458 	} else {
6459 		th_trace = (th_trace_t *)val;
6460 	}
6461 
6462 	ASSERT(th_trace->th_refcnt >= 0 &&
6463 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
6464 
6465 	th_trace->th_refcnt++;
6466 	th_trace_rrecord(th_trace);
6467 	return (B_TRUE);
6468 }
6469 
6470 /*
6471  * For the purpose of tracing a reference release, we assume that global
6472  * tracing is always on and that the same thread initiated the reference hold
6473  * is releasing.
6474  */
6475 void
6476 th_trace_unref(const void *obj)
6477 {
6478 	int retv;
6479 	mod_hash_t *mh;
6480 	th_trace_t *th_trace;
6481 	mod_hash_val_t val;
6482 
6483 	mh = th_trace_gethash(NULL);
6484 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
6485 	ASSERT(retv == 0);
6486 	th_trace = (th_trace_t *)val;
6487 
6488 	ASSERT(th_trace->th_refcnt > 0);
6489 	th_trace->th_refcnt--;
6490 	th_trace_rrecord(th_trace);
6491 }
6492 
6493 /*
6494  * If tracing has been disabled, then we assume that the reference counts are
6495  * now useless, and we clear them out before destroying the entries.
6496  */
6497 void
6498 th_trace_cleanup(const void *obj, boolean_t trace_disable)
6499 {
6500 	th_hash_t	*thh;
6501 	mod_hash_t	*mh;
6502 	mod_hash_val_t	val;
6503 	th_trace_t	*th_trace;
6504 	int		retv;
6505 
6506 	rw_enter(&ip_thread_rwlock, RW_READER);
6507 	for (thh = list_head(&ip_thread_list); thh != NULL;
6508 	    thh = list_next(&ip_thread_list, thh)) {
6509 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
6510 		    &val) == 0) {
6511 			th_trace = (th_trace_t *)val;
6512 			if (trace_disable)
6513 				th_trace->th_refcnt = 0;
6514 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
6515 			ASSERT(retv == 0);
6516 		}
6517 	}
6518 	rw_exit(&ip_thread_rwlock);
6519 }
6520 
6521 void
6522 ipif_trace_ref(ipif_t *ipif)
6523 {
6524 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6525 
6526 	if (ipif->ipif_trace_disable)
6527 		return;
6528 
6529 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
6530 		ipif->ipif_trace_disable = B_TRUE;
6531 		ipif_trace_cleanup(ipif);
6532 	}
6533 }
6534 
6535 void
6536 ipif_untrace_ref(ipif_t *ipif)
6537 {
6538 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6539 
6540 	if (!ipif->ipif_trace_disable)
6541 		th_trace_unref(ipif);
6542 }
6543 
6544 void
6545 ill_trace_ref(ill_t *ill)
6546 {
6547 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6548 
6549 	if (ill->ill_trace_disable)
6550 		return;
6551 
6552 	if (!th_trace_ref(ill, ill->ill_ipst)) {
6553 		ill->ill_trace_disable = B_TRUE;
6554 		ill_trace_cleanup(ill);
6555 	}
6556 }
6557 
6558 void
6559 ill_untrace_ref(ill_t *ill)
6560 {
6561 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6562 
6563 	if (!ill->ill_trace_disable)
6564 		th_trace_unref(ill);
6565 }
6566 
6567 /*
6568  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
6569  * failure, ipif_trace_disable is set.
6570  */
6571 static void
6572 ipif_trace_cleanup(const ipif_t *ipif)
6573 {
6574 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
6575 }
6576 
6577 /*
6578  * Called when ill is unplumbed or when memory alloc fails.  Note that on
6579  * failure, ill_trace_disable is set.
6580  */
6581 static void
6582 ill_trace_cleanup(const ill_t *ill)
6583 {
6584 	th_trace_cleanup(ill, ill->ill_trace_disable);
6585 }
6586 #endif /* DEBUG */
6587 
6588 void
6589 ipif_refhold_locked(ipif_t *ipif)
6590 {
6591 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6592 	ipif->ipif_refcnt++;
6593 	IPIF_TRACE_REF(ipif);
6594 }
6595 
6596 void
6597 ipif_refhold(ipif_t *ipif)
6598 {
6599 	ill_t	*ill;
6600 
6601 	ill = ipif->ipif_ill;
6602 	mutex_enter(&ill->ill_lock);
6603 	ipif->ipif_refcnt++;
6604 	IPIF_TRACE_REF(ipif);
6605 	mutex_exit(&ill->ill_lock);
6606 }
6607 
6608 /*
6609  * Must not be called while holding any locks. Otherwise if this is
6610  * the last reference to be released there is a chance of recursive mutex
6611  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6612  * to restart an ioctl.
6613  */
6614 void
6615 ipif_refrele(ipif_t *ipif)
6616 {
6617 	ill_t	*ill;
6618 
6619 	ill = ipif->ipif_ill;
6620 
6621 	mutex_enter(&ill->ill_lock);
6622 	ASSERT(ipif->ipif_refcnt != 0);
6623 	ipif->ipif_refcnt--;
6624 	IPIF_UNTRACE_REF(ipif);
6625 	if (ipif->ipif_refcnt != 0) {
6626 		mutex_exit(&ill->ill_lock);
6627 		return;
6628 	}
6629 
6630 	/* Drops the ill_lock */
6631 	ipif_ill_refrele_tail(ill);
6632 }
6633 
6634 ipif_t *
6635 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6636 {
6637 	ipif_t	*ipif;
6638 
6639 	mutex_enter(&ill->ill_lock);
6640 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6641 	    ipif != NULL; ipif = ipif->ipif_next) {
6642 		if (!IPIF_CAN_LOOKUP(ipif))
6643 			continue;
6644 		ipif_refhold_locked(ipif);
6645 		mutex_exit(&ill->ill_lock);
6646 		return (ipif);
6647 	}
6648 	mutex_exit(&ill->ill_lock);
6649 	return (NULL);
6650 }
6651 
6652 /*
6653  * TODO: make this table extendible at run time
6654  * Return a pointer to the mac type info for 'mac_type'
6655  */
6656 static ip_m_t *
6657 ip_m_lookup(t_uscalar_t mac_type)
6658 {
6659 	ip_m_t	*ipm;
6660 
6661 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6662 		if (ipm->ip_m_mac_type == mac_type)
6663 			return (ipm);
6664 	return (NULL);
6665 }
6666 
6667 /*
6668  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6669  * ipif_arg is passed in to associate it with the correct interface.
6670  * We may need to restart this operation if the ipif cannot be looked up
6671  * due to an exclusive operation that is currently in progress. The restart
6672  * entry point is specified by 'func'
6673  */
6674 int
6675 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6676     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ire_t **ire_arg,
6677     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
6678     struct rtsa_s *sp, ip_stack_t *ipst)
6679 {
6680 	ire_t	*ire;
6681 	ire_t	*gw_ire = NULL;
6682 	ipif_t	*ipif = NULL;
6683 	boolean_t ipif_refheld = B_FALSE;
6684 	uint_t	type;
6685 	int	match_flags = MATCH_IRE_TYPE;
6686 	int	error;
6687 	tsol_gc_t *gc = NULL;
6688 	tsol_gcgrp_t *gcgrp = NULL;
6689 	boolean_t gcgrp_xtraref = B_FALSE;
6690 
6691 	ip1dbg(("ip_rt_add:"));
6692 
6693 	if (ire_arg != NULL)
6694 		*ire_arg = NULL;
6695 
6696 	/*
6697 	 * If this is the case of RTF_HOST being set, then we set the netmask
6698 	 * to all ones (regardless if one was supplied).
6699 	 */
6700 	if (flags & RTF_HOST)
6701 		mask = IP_HOST_MASK;
6702 
6703 	/*
6704 	 * Prevent routes with a zero gateway from being created (since
6705 	 * interfaces can currently be plumbed and brought up no assigned
6706 	 * address).
6707 	 */
6708 	if (gw_addr == 0)
6709 		return (ENETUNREACH);
6710 	/*
6711 	 * Get the ipif, if any, corresponding to the gw_addr
6712 	 */
6713 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &error,
6714 	    ipst);
6715 	if (ipif != NULL) {
6716 		if (IS_VNI(ipif->ipif_ill)) {
6717 			ipif_refrele(ipif);
6718 			return (EINVAL);
6719 		}
6720 		ipif_refheld = B_TRUE;
6721 	} else if (error == EINPROGRESS) {
6722 		ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6723 		return (EINPROGRESS);
6724 	} else {
6725 		error = 0;
6726 	}
6727 
6728 	if (ipif != NULL) {
6729 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6730 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6731 	} else {
6732 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6733 	}
6734 
6735 	/*
6736 	 * GateD will attempt to create routes with a loopback interface
6737 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6738 	 * these routes to be added, but create them as interface routes
6739 	 * since the gateway is an interface address.
6740 	 */
6741 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6742 		flags &= ~RTF_GATEWAY;
6743 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6744 		    mask == IP_HOST_MASK) {
6745 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6746 			    ALL_ZONES, NULL, match_flags, ipst);
6747 			if (ire != NULL) {
6748 				ire_refrele(ire);
6749 				if (ipif_refheld)
6750 					ipif_refrele(ipif);
6751 				return (EEXIST);
6752 			}
6753 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6754 			    "for 0x%x\n", (void *)ipif,
6755 			    ipif->ipif_ire_type,
6756 			    ntohl(ipif->ipif_lcl_addr)));
6757 			ire = ire_create(
6758 			    (uchar_t *)&dst_addr,	/* dest address */
6759 			    (uchar_t *)&mask,		/* mask */
6760 			    (uchar_t *)&ipif->ipif_src_addr,
6761 			    NULL,			/* no gateway */
6762 			    &ipif->ipif_mtu,
6763 			    NULL,
6764 			    ipif->ipif_rq,		/* recv-from queue */
6765 			    NULL,			/* no send-to queue */
6766 			    ipif->ipif_ire_type,	/* LOOPBACK */
6767 			    ipif,
6768 			    0,
6769 			    0,
6770 			    0,
6771 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6772 			    RTF_PRIVATE : 0,
6773 			    &ire_uinfo_null,
6774 			    NULL,
6775 			    NULL,
6776 			    ipst);
6777 
6778 			if (ire == NULL) {
6779 				if (ipif_refheld)
6780 					ipif_refrele(ipif);
6781 				return (ENOMEM);
6782 			}
6783 			error = ire_add(&ire, q, mp, func, B_FALSE);
6784 			if (error == 0)
6785 				goto save_ire;
6786 			if (ipif_refheld)
6787 				ipif_refrele(ipif);
6788 			return (error);
6789 
6790 		}
6791 	}
6792 
6793 	/*
6794 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6795 	 * and the gateway address provided is one of the system's interface
6796 	 * addresses.  By using the routing socket interface and supplying an
6797 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6798 	 * specifying an interface route to be created is available which uses
6799 	 * the interface index that specifies the outgoing interface rather than
6800 	 * the address of an outgoing interface (which may not be able to
6801 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6802 	 * flag, routes can be specified which not only specify the next-hop to
6803 	 * be used when routing to a certain prefix, but also which outgoing
6804 	 * interface should be used.
6805 	 *
6806 	 * Previously, interfaces would have unique addresses assigned to them
6807 	 * and so the address assigned to a particular interface could be used
6808 	 * to identify a particular interface.  One exception to this was the
6809 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6810 	 *
6811 	 * With the advent of IPv6 and its link-local addresses, this
6812 	 * restriction was relaxed and interfaces could share addresses between
6813 	 * themselves.  In fact, typically all of the link-local interfaces on
6814 	 * an IPv6 node or router will have the same link-local address.  In
6815 	 * order to differentiate between these interfaces, the use of an
6816 	 * interface index is necessary and this index can be carried inside a
6817 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6818 	 * of using the interface index, however, is that all of the ipif's that
6819 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6820 	 * cannot be used to differentiate between ipif's (or logical
6821 	 * interfaces) that belong to the same ill (physical interface).
6822 	 *
6823 	 * For example, in the following case involving IPv4 interfaces and
6824 	 * logical interfaces
6825 	 *
6826 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6827 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6828 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6829 	 *
6830 	 * the ipif's corresponding to each of these interface routes can be
6831 	 * uniquely identified by the "gateway" (actually interface address).
6832 	 *
6833 	 * In this case involving multiple IPv6 default routes to a particular
6834 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6835 	 * default route is of interest:
6836 	 *
6837 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6838 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6839 	 */
6840 
6841 	/* RTF_GATEWAY not set */
6842 	if (!(flags & RTF_GATEWAY)) {
6843 		queue_t	*stq;
6844 
6845 		if (sp != NULL) {
6846 			ip2dbg(("ip_rt_add: gateway security attributes "
6847 			    "cannot be set with interface route\n"));
6848 			if (ipif_refheld)
6849 				ipif_refrele(ipif);
6850 			return (EINVAL);
6851 		}
6852 
6853 		/*
6854 		 * As the interface index specified with the RTA_IFP sockaddr is
6855 		 * the same for all ipif's off of an ill, the matching logic
6856 		 * below uses MATCH_IRE_ILL if such an index was specified.
6857 		 * This means that routes sharing the same prefix when added
6858 		 * using a RTA_IFP sockaddr must have distinct interface
6859 		 * indices (namely, they must be on distinct ill's).
6860 		 *
6861 		 * On the other hand, since the gateway address will usually be
6862 		 * different for each ipif on the system, the matching logic
6863 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6864 		 * route.  This means that interface routes for the same prefix
6865 		 * can be created if they belong to distinct ipif's and if a
6866 		 * RTA_IFP sockaddr is not present.
6867 		 */
6868 		if (ipif_arg != NULL) {
6869 			if (ipif_refheld)  {
6870 				ipif_refrele(ipif);
6871 				ipif_refheld = B_FALSE;
6872 			}
6873 			ipif = ipif_arg;
6874 			match_flags |= MATCH_IRE_ILL;
6875 		} else {
6876 			/*
6877 			 * Check the ipif corresponding to the gw_addr
6878 			 */
6879 			if (ipif == NULL)
6880 				return (ENETUNREACH);
6881 			match_flags |= MATCH_IRE_IPIF;
6882 		}
6883 		ASSERT(ipif != NULL);
6884 
6885 		/*
6886 		 * We check for an existing entry at this point.
6887 		 *
6888 		 * Since a netmask isn't passed in via the ioctl interface
6889 		 * (SIOCADDRT), we don't check for a matching netmask in that
6890 		 * case.
6891 		 */
6892 		if (!ioctl_msg)
6893 			match_flags |= MATCH_IRE_MASK;
6894 		ire = ire_ftable_lookup(dst_addr, mask, 0, IRE_INTERFACE, ipif,
6895 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
6896 		if (ire != NULL) {
6897 			ire_refrele(ire);
6898 			if (ipif_refheld)
6899 				ipif_refrele(ipif);
6900 			return (EEXIST);
6901 		}
6902 
6903 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
6904 		    ? ipif->ipif_rq : ipif->ipif_wq;
6905 
6906 		/*
6907 		 * Create a copy of the IRE_LOOPBACK,
6908 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
6909 		 * the modified address and netmask.
6910 		 */
6911 		ire = ire_create(
6912 		    (uchar_t *)&dst_addr,
6913 		    (uint8_t *)&mask,
6914 		    (uint8_t *)&ipif->ipif_src_addr,
6915 		    NULL,
6916 		    &ipif->ipif_mtu,
6917 		    NULL,
6918 		    NULL,
6919 		    stq,
6920 		    ipif->ipif_net_type,
6921 		    ipif,
6922 		    0,
6923 		    0,
6924 		    0,
6925 		    flags,
6926 		    &ire_uinfo_null,
6927 		    NULL,
6928 		    NULL,
6929 		    ipst);
6930 		if (ire == NULL) {
6931 			if (ipif_refheld)
6932 				ipif_refrele(ipif);
6933 			return (ENOMEM);
6934 		}
6935 
6936 		/*
6937 		 * Some software (for example, GateD and Sun Cluster) attempts
6938 		 * to create (what amount to) IRE_PREFIX routes with the
6939 		 * loopback address as the gateway.  This is primarily done to
6940 		 * set up prefixes with the RTF_REJECT flag set (for example,
6941 		 * when generating aggregate routes.)
6942 		 *
6943 		 * If the IRE type (as defined by ipif->ipif_net_type) is
6944 		 * IRE_LOOPBACK, then we map the request into a
6945 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
6946 		 * these interface routes, by definition, can only be that.
6947 		 *
6948 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
6949 		 * routine, but rather using ire_create() directly.
6950 		 *
6951 		 */
6952 		if (ipif->ipif_net_type == IRE_LOOPBACK) {
6953 			ire->ire_type = IRE_IF_NORESOLVER;
6954 			ire->ire_flags |= RTF_BLACKHOLE;
6955 		}
6956 
6957 		error = ire_add(&ire, q, mp, func, B_FALSE);
6958 		if (error == 0)
6959 			goto save_ire;
6960 
6961 		/*
6962 		 * In the result of failure, ire_add() will have already
6963 		 * deleted the ire in question, so there is no need to
6964 		 * do that here.
6965 		 */
6966 		if (ipif_refheld)
6967 			ipif_refrele(ipif);
6968 		return (error);
6969 	}
6970 	if (ipif_refheld) {
6971 		ipif_refrele(ipif);
6972 		ipif_refheld = B_FALSE;
6973 	}
6974 
6975 	/*
6976 	 * Get an interface IRE for the specified gateway.
6977 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
6978 	 * gateway, it is currently unreachable and we fail the request
6979 	 * accordingly.
6980 	 */
6981 	ipif = ipif_arg;
6982 	if (ipif_arg != NULL)
6983 		match_flags |= MATCH_IRE_ILL;
6984 again:
6985 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
6986 	    ALL_ZONES, 0, NULL, match_flags, ipst);
6987 	if (gw_ire == NULL) {
6988 		/*
6989 		 * With IPMP, we allow host routes to influence in.mpathd's
6990 		 * target selection.  However, if the test addresses are on
6991 		 * their own network, the above lookup will fail since the
6992 		 * underlying IRE_INTERFACEs are marked hidden.  So allow
6993 		 * hidden test IREs to be found and try again.
6994 		 */
6995 		if (!(match_flags & MATCH_IRE_MARK_TESTHIDDEN))  {
6996 			match_flags |= MATCH_IRE_MARK_TESTHIDDEN;
6997 			goto again;
6998 		}
6999 		return (ENETUNREACH);
7000 	}
7001 
7002 	/*
7003 	 * We create one of three types of IREs as a result of this request
7004 	 * based on the netmask.  A netmask of all ones (which is automatically
7005 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7006 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7007 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7008 	 * destination prefix.
7009 	 */
7010 	if (mask == IP_HOST_MASK)
7011 		type = IRE_HOST;
7012 	else if (mask == 0)
7013 		type = IRE_DEFAULT;
7014 	else
7015 		type = IRE_PREFIX;
7016 
7017 	/* check for a duplicate entry */
7018 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7019 	    NULL, ALL_ZONES, 0, NULL,
7020 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7021 	if (ire != NULL) {
7022 		ire_refrele(gw_ire);
7023 		ire_refrele(ire);
7024 		return (EEXIST);
7025 	}
7026 
7027 	/* Security attribute exists */
7028 	if (sp != NULL) {
7029 		tsol_gcgrp_addr_t ga;
7030 
7031 		/* find or create the gateway credentials group */
7032 		ga.ga_af = AF_INET;
7033 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7034 
7035 		/* we hold reference to it upon success */
7036 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7037 		if (gcgrp == NULL) {
7038 			ire_refrele(gw_ire);
7039 			return (ENOMEM);
7040 		}
7041 
7042 		/*
7043 		 * Create and add the security attribute to the group; a
7044 		 * reference to the group is made upon allocating a new
7045 		 * entry successfully.  If it finds an already-existing
7046 		 * entry for the security attribute in the group, it simply
7047 		 * returns it and no new reference is made to the group.
7048 		 */
7049 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7050 		if (gc == NULL) {
7051 			/* release reference held by gcgrp_lookup */
7052 			GCGRP_REFRELE(gcgrp);
7053 			ire_refrele(gw_ire);
7054 			return (ENOMEM);
7055 		}
7056 	}
7057 
7058 	/* Create the IRE. */
7059 	ire = ire_create(
7060 	    (uchar_t *)&dst_addr,		/* dest address */
7061 	    (uchar_t *)&mask,			/* mask */
7062 	    /* src address assigned by the caller? */
7063 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7064 	    (flags & RTF_SETSRC)) ?  &src_addr : NULL),
7065 	    (uchar_t *)&gw_addr,		/* gateway address */
7066 	    &gw_ire->ire_max_frag,
7067 	    NULL,				/* no src nce */
7068 	    NULL,				/* no recv-from queue */
7069 	    NULL,				/* no send-to queue */
7070 	    (ushort_t)type,			/* IRE type */
7071 	    ipif_arg,
7072 	    0,
7073 	    0,
7074 	    0,
7075 	    flags,
7076 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7077 	    gc,					/* security attribute */
7078 	    NULL,
7079 	    ipst);
7080 
7081 	/*
7082 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7083 	 * reference to the 'gcgrp'. We can now release the extra reference
7084 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7085 	 */
7086 	if (gcgrp_xtraref)
7087 		GCGRP_REFRELE(gcgrp);
7088 	if (ire == NULL) {
7089 		if (gc != NULL)
7090 			GC_REFRELE(gc);
7091 		ire_refrele(gw_ire);
7092 		return (ENOMEM);
7093 	}
7094 
7095 	/*
7096 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7097 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7098 	 */
7099 
7100 	/* Add the new IRE. */
7101 	error = ire_add(&ire, q, mp, func, B_FALSE);
7102 	if (error != 0) {
7103 		/*
7104 		 * In the result of failure, ire_add() will have already
7105 		 * deleted the ire in question, so there is no need to
7106 		 * do that here.
7107 		 */
7108 		ire_refrele(gw_ire);
7109 		return (error);
7110 	}
7111 
7112 	if (flags & RTF_MULTIRT) {
7113 		/*
7114 		 * Invoke the CGTP (multirouting) filtering module
7115 		 * to add the dst address in the filtering database.
7116 		 * Replicated inbound packets coming from that address
7117 		 * will be filtered to discard the duplicates.
7118 		 * It is not necessary to call the CGTP filter hook
7119 		 * when the dst address is a broadcast or multicast,
7120 		 * because an IP source address cannot be a broadcast
7121 		 * or a multicast.
7122 		 */
7123 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7124 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7125 		if (ire_dst != NULL) {
7126 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
7127 			ire_refrele(ire_dst);
7128 			goto save_ire;
7129 		}
7130 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
7131 		    !CLASSD(ire->ire_addr)) {
7132 			int res = ipst->ips_ip_cgtp_filter_ops->cfo_add_dest_v4(
7133 			    ipst->ips_netstack->netstack_stackid,
7134 			    ire->ire_addr,
7135 			    ire->ire_gateway_addr,
7136 			    ire->ire_src_addr,
7137 			    gw_ire->ire_src_addr);
7138 			if (res != 0) {
7139 				ire_refrele(gw_ire);
7140 				ire_delete(ire);
7141 				return (res);
7142 			}
7143 		}
7144 	}
7145 
7146 	/*
7147 	 * Now that the prefix IRE entry has been created, delete any
7148 	 * existing gateway IRE cache entries as well as any IRE caches
7149 	 * using the gateway, and force them to be created through
7150 	 * ip_newroute.
7151 	 */
7152 	if (gc != NULL) {
7153 		ASSERT(gcgrp != NULL);
7154 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7155 	}
7156 
7157 save_ire:
7158 	if (gw_ire != NULL) {
7159 		ire_refrele(gw_ire);
7160 	}
7161 	if (ipif != NULL) {
7162 		/*
7163 		 * Save enough information so that we can recreate the IRE if
7164 		 * the interface goes down and then up.  The metrics associated
7165 		 * with the route will be saved as well when rts_setmetrics() is
7166 		 * called after the IRE has been created.  In the case where
7167 		 * memory cannot be allocated, none of this information will be
7168 		 * saved.
7169 		 */
7170 		ipif_save_ire(ipif, ire);
7171 	}
7172 	if (ioctl_msg)
7173 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7174 	if (ire_arg != NULL) {
7175 		/*
7176 		 * Store the ire that was successfully added into where ire_arg
7177 		 * points to so that callers don't have to look it up
7178 		 * themselves (but they are responsible for ire_refrele()ing
7179 		 * the ire when they are finished with it).
7180 		 */
7181 		*ire_arg = ire;
7182 	} else {
7183 		ire_refrele(ire);		/* Held in ire_add */
7184 	}
7185 	if (ipif_refheld)
7186 		ipif_refrele(ipif);
7187 	return (0);
7188 }
7189 
7190 /*
7191  * ip_rt_delete is called to delete an IPv4 route.
7192  * ipif_arg is passed in to associate it with the correct interface.
7193  * We may need to restart this operation if the ipif cannot be looked up
7194  * due to an exclusive operation that is currently in progress. The restart
7195  * entry point is specified by 'func'
7196  */
7197 /* ARGSUSED4 */
7198 int
7199 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7200     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, boolean_t ioctl_msg,
7201     queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst)
7202 {
7203 	ire_t	*ire = NULL;
7204 	ipif_t	*ipif;
7205 	boolean_t ipif_refheld = B_FALSE;
7206 	uint_t	type;
7207 	uint_t	match_flags = MATCH_IRE_TYPE;
7208 	int	err = 0;
7209 
7210 	ip1dbg(("ip_rt_delete:"));
7211 	/*
7212 	 * If this is the case of RTF_HOST being set, then we set the netmask
7213 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7214 	 */
7215 	if (flags & RTF_HOST) {
7216 		mask = IP_HOST_MASK;
7217 		match_flags |= MATCH_IRE_MASK;
7218 	} else if (rtm_addrs & RTA_NETMASK) {
7219 		match_flags |= MATCH_IRE_MASK;
7220 	}
7221 
7222 	/*
7223 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7224 	 * we check if the gateway address is one of our interfaces first,
7225 	 * and fall back on RTF_GATEWAY routes.
7226 	 *
7227 	 * This makes it possible to delete an original
7228 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7229 	 *
7230 	 * As the interface index specified with the RTA_IFP sockaddr is the
7231 	 * same for all ipif's off of an ill, the matching logic below uses
7232 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7233 	 * sharing the same prefix and interface index as the the route
7234 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7235 	 * is specified in the request.
7236 	 *
7237 	 * On the other hand, since the gateway address will usually be
7238 	 * different for each ipif on the system, the matching logic
7239 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7240 	 * route.  This means that interface routes for the same prefix can be
7241 	 * uniquely identified if they belong to distinct ipif's and if a
7242 	 * RTA_IFP sockaddr is not present.
7243 	 *
7244 	 * For more detail on specifying routes by gateway address and by
7245 	 * interface index, see the comments in ip_rt_add().
7246 	 */
7247 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &err,
7248 	    ipst);
7249 	if (ipif != NULL)
7250 		ipif_refheld = B_TRUE;
7251 	else if (err == EINPROGRESS)
7252 		return (err);
7253 	else
7254 		err = 0;
7255 	if (ipif != NULL) {
7256 		if (ipif_arg != NULL) {
7257 			if (ipif_refheld) {
7258 				ipif_refrele(ipif);
7259 				ipif_refheld = B_FALSE;
7260 			}
7261 			ipif = ipif_arg;
7262 			match_flags |= MATCH_IRE_ILL;
7263 		} else {
7264 			match_flags |= MATCH_IRE_IPIF;
7265 		}
7266 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7267 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
7268 			    ALL_ZONES, NULL, match_flags, ipst);
7269 		}
7270 		if (ire == NULL) {
7271 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7272 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
7273 			    match_flags, ipst);
7274 		}
7275 	}
7276 
7277 	if (ire == NULL) {
7278 		/*
7279 		 * At this point, the gateway address is not one of our own
7280 		 * addresses or a matching interface route was not found.  We
7281 		 * set the IRE type to lookup based on whether
7282 		 * this is a host route, a default route or just a prefix.
7283 		 *
7284 		 * If an ipif_arg was passed in, then the lookup is based on an
7285 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7286 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7287 		 * set as the route being looked up is not a traditional
7288 		 * interface route.
7289 		 */
7290 		match_flags &= ~MATCH_IRE_IPIF;
7291 		match_flags |= MATCH_IRE_GW;
7292 		if (ipif_arg != NULL)
7293 			match_flags |= MATCH_IRE_ILL;
7294 		if (mask == IP_HOST_MASK)
7295 			type = IRE_HOST;
7296 		else if (mask == 0)
7297 			type = IRE_DEFAULT;
7298 		else
7299 			type = IRE_PREFIX;
7300 		ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7301 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7302 	}
7303 
7304 	if (ipif_refheld)
7305 		ipif_refrele(ipif);
7306 
7307 	/* ipif is not refheld anymore */
7308 	if (ire == NULL)
7309 		return (ESRCH);
7310 
7311 	if (ire->ire_flags & RTF_MULTIRT) {
7312 		/*
7313 		 * Invoke the CGTP (multirouting) filtering module
7314 		 * to remove the dst address from the filtering database.
7315 		 * Packets coming from that address will no longer be
7316 		 * filtered to remove duplicates.
7317 		 */
7318 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
7319 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
7320 			    ipst->ips_netstack->netstack_stackid,
7321 			    ire->ire_addr, ire->ire_gateway_addr);
7322 		}
7323 		ip_cgtp_bcast_delete(ire, ipst);
7324 	}
7325 
7326 	ipif = ire->ire_ipif;
7327 	if (ipif != NULL)
7328 		ipif_remove_ire(ipif, ire);
7329 	if (ioctl_msg)
7330 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7331 	ire_delete(ire);
7332 	ire_refrele(ire);
7333 	return (err);
7334 }
7335 
7336 /*
7337  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7338  */
7339 /* ARGSUSED */
7340 int
7341 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7342     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7343 {
7344 	ipaddr_t dst_addr;
7345 	ipaddr_t gw_addr;
7346 	ipaddr_t mask;
7347 	int error = 0;
7348 	mblk_t *mp1;
7349 	struct rtentry *rt;
7350 	ipif_t *ipif = NULL;
7351 	ip_stack_t	*ipst;
7352 
7353 	ASSERT(q->q_next == NULL);
7354 	ipst = CONNQ_TO_IPST(q);
7355 
7356 	ip1dbg(("ip_siocaddrt:"));
7357 	/* Existence of mp1 verified in ip_wput_nondata */
7358 	mp1 = mp->b_cont->b_cont;
7359 	rt = (struct rtentry *)mp1->b_rptr;
7360 
7361 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7362 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7363 
7364 	/*
7365 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7366 	 * to a particular host address.  In this case, we set the netmask to
7367 	 * all ones for the particular destination address.  Otherwise,
7368 	 * determine the netmask to be used based on dst_addr and the interfaces
7369 	 * in use.
7370 	 */
7371 	if (rt->rt_flags & RTF_HOST) {
7372 		mask = IP_HOST_MASK;
7373 	} else {
7374 		/*
7375 		 * Note that ip_subnet_mask returns a zero mask in the case of
7376 		 * default (an all-zeroes address).
7377 		 */
7378 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7379 	}
7380 
7381 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7382 	    B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7383 	if (ipif != NULL)
7384 		ipif_refrele(ipif);
7385 	return (error);
7386 }
7387 
7388 /*
7389  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7390  */
7391 /* ARGSUSED */
7392 int
7393 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7394     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7395 {
7396 	ipaddr_t dst_addr;
7397 	ipaddr_t gw_addr;
7398 	ipaddr_t mask;
7399 	int error;
7400 	mblk_t *mp1;
7401 	struct rtentry *rt;
7402 	ipif_t *ipif = NULL;
7403 	ip_stack_t	*ipst;
7404 
7405 	ASSERT(q->q_next == NULL);
7406 	ipst = CONNQ_TO_IPST(q);
7407 
7408 	ip1dbg(("ip_siocdelrt:"));
7409 	/* Existence of mp1 verified in ip_wput_nondata */
7410 	mp1 = mp->b_cont->b_cont;
7411 	rt = (struct rtentry *)mp1->b_rptr;
7412 
7413 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7414 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7415 
7416 	/*
7417 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7418 	 * to a particular host address.  In this case, we set the netmask to
7419 	 * all ones for the particular destination address.  Otherwise,
7420 	 * determine the netmask to be used based on dst_addr and the interfaces
7421 	 * in use.
7422 	 */
7423 	if (rt->rt_flags & RTF_HOST) {
7424 		mask = IP_HOST_MASK;
7425 	} else {
7426 		/*
7427 		 * Note that ip_subnet_mask returns a zero mask in the case of
7428 		 * default (an all-zeroes address).
7429 		 */
7430 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7431 	}
7432 
7433 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7434 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE, q,
7435 	    mp, ip_process_ioctl, ipst);
7436 	if (ipif != NULL)
7437 		ipif_refrele(ipif);
7438 	return (error);
7439 }
7440 
7441 /*
7442  * Enqueue the mp onto the ipsq, chained by b_next.
7443  * b_prev stores the function to be executed later, and b_queue the queue
7444  * where this mp originated.
7445  */
7446 void
7447 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7448     ill_t *pending_ill)
7449 {
7450 	conn_t	*connp;
7451 	ipxop_t *ipx = ipsq->ipsq_xop;
7452 
7453 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7454 	ASSERT(MUTEX_HELD(&ipx->ipx_lock));
7455 	ASSERT(func != NULL);
7456 
7457 	mp->b_queue = q;
7458 	mp->b_prev = (void *)func;
7459 	mp->b_next = NULL;
7460 
7461 	switch (type) {
7462 	case CUR_OP:
7463 		if (ipx->ipx_mptail != NULL) {
7464 			ASSERT(ipx->ipx_mphead != NULL);
7465 			ipx->ipx_mptail->b_next = mp;
7466 		} else {
7467 			ASSERT(ipx->ipx_mphead == NULL);
7468 			ipx->ipx_mphead = mp;
7469 		}
7470 		ipx->ipx_mptail = mp;
7471 		break;
7472 
7473 	case NEW_OP:
7474 		if (ipsq->ipsq_xopq_mptail != NULL) {
7475 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7476 			ipsq->ipsq_xopq_mptail->b_next = mp;
7477 		} else {
7478 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7479 			ipsq->ipsq_xopq_mphead = mp;
7480 		}
7481 		ipsq->ipsq_xopq_mptail = mp;
7482 		ipx->ipx_ipsq_queued = B_TRUE;
7483 		break;
7484 
7485 	case SWITCH_OP:
7486 		ASSERT(ipsq->ipsq_swxop != NULL);
7487 		/* only one switch operation is currently allowed */
7488 		ASSERT(ipsq->ipsq_switch_mp == NULL);
7489 		ipsq->ipsq_switch_mp = mp;
7490 		ipx->ipx_ipsq_queued = B_TRUE;
7491 		break;
7492 	default:
7493 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7494 	}
7495 
7496 	if (CONN_Q(q) && pending_ill != NULL) {
7497 		connp = Q_TO_CONN(q);
7498 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7499 		connp->conn_oper_pending_ill = pending_ill;
7500 	}
7501 }
7502 
7503 /*
7504  * Dequeue the next message that requested exclusive access to this IPSQ's
7505  * xop.  Specifically:
7506  *
7507  *  1. If we're still processing the current operation on `ipsq', then
7508  *     dequeue the next message for the operation (from ipx_mphead), or
7509  *     return NULL if there are no queued messages for the operation.
7510  *     These messages are queued via CUR_OP to qwriter_ip() and friends.
7511  *
7512  *  2. If the current operation on `ipsq' has completed (ipx_current_ipif is
7513  *     not set) see if the ipsq has requested an xop switch.  If so, switch
7514  *     `ipsq' to a different xop.  Xop switches only happen when joining or
7515  *     leaving IPMP groups and require a careful dance -- see the comments
7516  *     in-line below for details.  If we're leaving a group xop or if we're
7517  *     joining a group xop and become writer on it, then we proceed to (3).
7518  *     Otherwise, we return NULL and exit the xop.
7519  *
7520  *  3. For each IPSQ in the xop, return any switch operation stored on
7521  *     ipsq_switch_mp (set via SWITCH_OP); these must be processed before
7522  *     any other messages queued on the IPSQ.  Otherwise, dequeue the next
7523  *     exclusive operation (queued via NEW_OP) stored on ipsq_xopq_mphead.
7524  *     Note that if the phyint tied to `ipsq' is not using IPMP there will
7525  *     only be one IPSQ in the xop.  Otherwise, there will be one IPSQ for
7526  *     each phyint in the group, including the IPMP meta-interface phyint.
7527  */
7528 static mblk_t *
7529 ipsq_dq(ipsq_t *ipsq)
7530 {
7531 	ill_t	*illv4, *illv6;
7532 	mblk_t	*mp;
7533 	ipsq_t	*xopipsq;
7534 	ipsq_t	*leftipsq = NULL;
7535 	ipxop_t *ipx;
7536 	phyint_t *phyi = ipsq->ipsq_phyint;
7537 	ip_stack_t *ipst = ipsq->ipsq_ipst;
7538 	boolean_t emptied = B_FALSE;
7539 
7540 	/*
7541 	 * Grab all the locks we need in the defined order (ill_g_lock ->
7542 	 * ipsq_lock -> ipx_lock); ill_g_lock is needed to use ipsq_next.
7543 	 */
7544 	rw_enter(&ipst->ips_ill_g_lock,
7545 	    ipsq->ipsq_swxop != NULL ? RW_WRITER : RW_READER);
7546 	mutex_enter(&ipsq->ipsq_lock);
7547 	ipx = ipsq->ipsq_xop;
7548 	mutex_enter(&ipx->ipx_lock);
7549 
7550 	/*
7551 	 * Dequeue the next message associated with the current exclusive
7552 	 * operation, if any.
7553 	 */
7554 	if ((mp = ipx->ipx_mphead) != NULL) {
7555 		ipx->ipx_mphead = mp->b_next;
7556 		if (ipx->ipx_mphead == NULL)
7557 			ipx->ipx_mptail = NULL;
7558 		mp->b_next = (void *)ipsq;
7559 		goto out;
7560 	}
7561 
7562 	if (ipx->ipx_current_ipif != NULL)
7563 		goto empty;
7564 
7565 	if (ipsq->ipsq_swxop != NULL) {
7566 		/*
7567 		 * The exclusive operation that is now being completed has
7568 		 * requested a switch to a different xop.  This happens
7569 		 * when an interface joins or leaves an IPMP group.  Joins
7570 		 * happen through SIOCSLIFGROUPNAME (ip_sioctl_groupname()).
7571 		 * Leaves happen via SIOCSLIFGROUPNAME, interface unplumb
7572 		 * (phyint_free()), or interface plumb for an ill type
7573 		 * not in the IPMP group (ip_rput_dlpi_writer()).
7574 		 *
7575 		 * Xop switches are not allowed on the IPMP meta-interface.
7576 		 */
7577 		ASSERT(phyi == NULL || !(phyi->phyint_flags & PHYI_IPMP));
7578 		ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
7579 		DTRACE_PROBE1(ipsq__switch, (ipsq_t *), ipsq);
7580 
7581 		if (ipsq->ipsq_swxop == &ipsq->ipsq_ownxop) {
7582 			/*
7583 			 * We're switching back to our own xop, so we have two
7584 			 * xop's to drain/exit: our own, and the group xop
7585 			 * that we are leaving.
7586 			 *
7587 			 * First, pull ourselves out of the group ipsq list.
7588 			 * This is safe since we're writer on ill_g_lock.
7589 			 */
7590 			ASSERT(ipsq->ipsq_xop != &ipsq->ipsq_ownxop);
7591 
7592 			xopipsq = ipx->ipx_ipsq;
7593 			while (xopipsq->ipsq_next != ipsq)
7594 				xopipsq = xopipsq->ipsq_next;
7595 
7596 			xopipsq->ipsq_next = ipsq->ipsq_next;
7597 			ipsq->ipsq_next = ipsq;
7598 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
7599 			ipsq->ipsq_swxop = NULL;
7600 
7601 			/*
7602 			 * Second, prepare to exit the group xop.  The actual
7603 			 * ipsq_exit() is done at the end of this function
7604 			 * since we cannot hold any locks across ipsq_exit().
7605 			 * Note that although we drop the group's ipx_lock, no
7606 			 * threads can proceed since we're still ipx_writer.
7607 			 */
7608 			leftipsq = xopipsq;
7609 			mutex_exit(&ipx->ipx_lock);
7610 
7611 			/*
7612 			 * Third, set ipx to point to our own xop (which was
7613 			 * inactive and therefore can be entered).
7614 			 */
7615 			ipx = ipsq->ipsq_xop;
7616 			mutex_enter(&ipx->ipx_lock);
7617 			ASSERT(ipx->ipx_writer == NULL);
7618 			ASSERT(ipx->ipx_current_ipif == NULL);
7619 		} else {
7620 			/*
7621 			 * We're switching from our own xop to a group xop.
7622 			 * The requestor of the switch must ensure that the
7623 			 * group xop cannot go away (e.g. by ensuring the
7624 			 * phyint associated with the xop cannot go away).
7625 			 *
7626 			 * If we can become writer on our new xop, then we'll
7627 			 * do the drain.  Otherwise, the current writer of our
7628 			 * new xop will do the drain when it exits.
7629 			 *
7630 			 * First, splice ourselves into the group IPSQ list.
7631 			 * This is safe since we're writer on ill_g_lock.
7632 			 */
7633 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
7634 
7635 			xopipsq = ipsq->ipsq_swxop->ipx_ipsq;
7636 			while (xopipsq->ipsq_next != ipsq->ipsq_swxop->ipx_ipsq)
7637 				xopipsq = xopipsq->ipsq_next;
7638 
7639 			xopipsq->ipsq_next = ipsq;
7640 			ipsq->ipsq_next = ipsq->ipsq_swxop->ipx_ipsq;
7641 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
7642 			ipsq->ipsq_swxop = NULL;
7643 
7644 			/*
7645 			 * Second, exit our own xop, since it's now unused.
7646 			 * This is safe since we've got the only reference.
7647 			 */
7648 			ASSERT(ipx->ipx_writer == curthread);
7649 			ipx->ipx_writer = NULL;
7650 			VERIFY(--ipx->ipx_reentry_cnt == 0);
7651 			ipx->ipx_ipsq_queued = B_FALSE;
7652 			mutex_exit(&ipx->ipx_lock);
7653 
7654 			/*
7655 			 * Third, set ipx to point to our new xop, and check
7656 			 * if we can become writer on it.  If we cannot, then
7657 			 * the current writer will drain the IPSQ group when
7658 			 * it exits.  Our ipsq_xop is guaranteed to be stable
7659 			 * because we're still holding ipsq_lock.
7660 			 */
7661 			ipx = ipsq->ipsq_xop;
7662 			mutex_enter(&ipx->ipx_lock);
7663 			if (ipx->ipx_writer != NULL ||
7664 			    ipx->ipx_current_ipif != NULL) {
7665 				goto out;
7666 			}
7667 		}
7668 
7669 		/*
7670 		 * Fourth, become writer on our new ipx before we continue
7671 		 * with the drain.  Note that we never dropped ipsq_lock
7672 		 * above, so no other thread could've raced with us to
7673 		 * become writer first.  Also, we're holding ipx_lock, so
7674 		 * no other thread can examine the ipx right now.
7675 		 */
7676 		ASSERT(ipx->ipx_current_ipif == NULL);
7677 		ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
7678 		VERIFY(ipx->ipx_reentry_cnt++ == 0);
7679 		ipx->ipx_writer = curthread;
7680 		ipx->ipx_forced = B_FALSE;
7681 #ifdef DEBUG
7682 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
7683 #endif
7684 	}
7685 
7686 	xopipsq = ipsq;
7687 	do {
7688 		/*
7689 		 * So that other operations operate on a consistent and
7690 		 * complete phyint, a switch message on an IPSQ must be
7691 		 * handled prior to any other operations on that IPSQ.
7692 		 */
7693 		if ((mp = xopipsq->ipsq_switch_mp) != NULL) {
7694 			xopipsq->ipsq_switch_mp = NULL;
7695 			ASSERT(mp->b_next == NULL);
7696 			mp->b_next = (void *)xopipsq;
7697 			goto out;
7698 		}
7699 
7700 		if ((mp = xopipsq->ipsq_xopq_mphead) != NULL) {
7701 			xopipsq->ipsq_xopq_mphead = mp->b_next;
7702 			if (xopipsq->ipsq_xopq_mphead == NULL)
7703 				xopipsq->ipsq_xopq_mptail = NULL;
7704 			mp->b_next = (void *)xopipsq;
7705 			goto out;
7706 		}
7707 	} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
7708 empty:
7709 	/*
7710 	 * There are no messages.  Further, we are holding ipx_lock, hence no
7711 	 * new messages can end up on any IPSQ in the xop.
7712 	 */
7713 	ipx->ipx_writer = NULL;
7714 	ipx->ipx_forced = B_FALSE;
7715 	VERIFY(--ipx->ipx_reentry_cnt == 0);
7716 	ipx->ipx_ipsq_queued = B_FALSE;
7717 	emptied = B_TRUE;
7718 #ifdef	DEBUG
7719 	ipx->ipx_depth = 0;
7720 #endif
7721 out:
7722 	mutex_exit(&ipx->ipx_lock);
7723 	mutex_exit(&ipsq->ipsq_lock);
7724 
7725 	/*
7726 	 * If we completely emptied the xop, then wake up any threads waiting
7727 	 * to enter any of the IPSQ's associated with it.
7728 	 */
7729 	if (emptied) {
7730 		xopipsq = ipsq;
7731 		do {
7732 			if ((phyi = xopipsq->ipsq_phyint) == NULL)
7733 				continue;
7734 
7735 			illv4 = phyi->phyint_illv4;
7736 			illv6 = phyi->phyint_illv6;
7737 
7738 			GRAB_ILL_LOCKS(illv4, illv6);
7739 			if (illv4 != NULL)
7740 				cv_broadcast(&illv4->ill_cv);
7741 			if (illv6 != NULL)
7742 				cv_broadcast(&illv6->ill_cv);
7743 			RELEASE_ILL_LOCKS(illv4, illv6);
7744 		} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
7745 	}
7746 	rw_exit(&ipst->ips_ill_g_lock);
7747 
7748 	/*
7749 	 * Now that all locks are dropped, exit the IPSQ we left.
7750 	 */
7751 	if (leftipsq != NULL)
7752 		ipsq_exit(leftipsq);
7753 
7754 	return (mp);
7755 }
7756 
7757 /*
7758  * Enter the ipsq corresponding to ill, by waiting synchronously till
7759  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7760  * will have to drain completely before ipsq_enter returns success.
7761  * ipx_current_ipif will be set if some exclusive op is in progress,
7762  * and the ipsq_exit logic will start the next enqueued op after
7763  * completion of the current op. If 'force' is used, we don't wait
7764  * for the enqueued ops. This is needed when a conn_close wants to
7765  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7766  * of an ill can also use this option. But we dont' use it currently.
7767  */
7768 #define	ENTER_SQ_WAIT_TICKS 100
7769 boolean_t
7770 ipsq_enter(ill_t *ill, boolean_t force, int type)
7771 {
7772 	ipsq_t	*ipsq;
7773 	ipxop_t *ipx;
7774 	boolean_t waited_enough = B_FALSE;
7775 
7776 	/*
7777 	 * Note that the relationship between ill and ipsq is fixed as long as
7778 	 * the ill is not ILL_CONDEMNED.  Holding ipsq_lock ensures the
7779 	 * relationship between the IPSQ and xop cannot change.  However,
7780 	 * since we cannot hold ipsq_lock across the cv_wait(), it may change
7781 	 * while we're waiting.  We wait on ill_cv and rely on ipsq_exit()
7782 	 * waking up all ills in the xop when it becomes available.
7783 	 */
7784 	mutex_enter(&ill->ill_lock);
7785 	for (;;) {
7786 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7787 			mutex_exit(&ill->ill_lock);
7788 			return (B_FALSE);
7789 		}
7790 
7791 		ipsq = ill->ill_phyint->phyint_ipsq;
7792 		mutex_enter(&ipsq->ipsq_lock);
7793 		ipx = ipsq->ipsq_xop;
7794 		mutex_enter(&ipx->ipx_lock);
7795 
7796 		if (ipx->ipx_writer == NULL && (type == CUR_OP ||
7797 		    ipx->ipx_current_ipif == NULL || waited_enough))
7798 			break;
7799 
7800 		if (!force || ipx->ipx_writer != NULL) {
7801 			mutex_exit(&ipx->ipx_lock);
7802 			mutex_exit(&ipsq->ipsq_lock);
7803 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7804 		} else {
7805 			mutex_exit(&ipx->ipx_lock);
7806 			mutex_exit(&ipsq->ipsq_lock);
7807 			(void) cv_timedwait(&ill->ill_cv,
7808 			    &ill->ill_lock, lbolt + ENTER_SQ_WAIT_TICKS);
7809 			waited_enough = B_TRUE;
7810 		}
7811 	}
7812 
7813 	ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
7814 	ASSERT(ipx->ipx_reentry_cnt == 0);
7815 	ipx->ipx_writer = curthread;
7816 	ipx->ipx_forced = (ipx->ipx_current_ipif != NULL);
7817 	ipx->ipx_reentry_cnt++;
7818 #ifdef DEBUG
7819 	ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
7820 #endif
7821 	mutex_exit(&ipx->ipx_lock);
7822 	mutex_exit(&ipsq->ipsq_lock);
7823 	mutex_exit(&ill->ill_lock);
7824 	return (B_TRUE);
7825 }
7826 
7827 boolean_t
7828 ill_perim_enter(ill_t *ill)
7829 {
7830 	return (ipsq_enter(ill, B_FALSE, CUR_OP));
7831 }
7832 
7833 void
7834 ill_perim_exit(ill_t *ill)
7835 {
7836 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
7837 }
7838 
7839 /*
7840  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7841  * certain critical operations like plumbing (i.e. most set ioctls), multicast
7842  * joins, igmp/mld timers, etc.  There is one ipsq per phyint. The ipsq
7843  * serializes exclusive ioctls issued by applications on a per ipsq basis in
7844  * ipsq_xopq_mphead. It also protects against multiple threads executing in
7845  * the ipsq. Responses from the driver pertain to the current ioctl (say a
7846  * DL_BIND_ACK in response to a DL_BIND_REQ initiated as part of bringing
7847  * up the interface) and are enqueued in ipx_mphead.
7848  *
7849  * If a thread does not want to reenter the ipsq when it is already writer,
7850  * it must make sure that the specified reentry point to be called later
7851  * when the ipsq is empty, nor any code path starting from the specified reentry
7852  * point must never ever try to enter the ipsq again. Otherwise it can lead
7853  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7854  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7855  * dequeues the requests waiting to become exclusive in ipx_mphead and calls
7856  * the reentry point. When the list at ipx_mphead becomes empty ipsq_exit
7857  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7858  * ioctl if the current ioctl has completed. If the current ioctl is still
7859  * in progress it simply returns. The current ioctl could be waiting for
7860  * a response from another module (arp or the driver or could be waiting for
7861  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipx_pending_mp
7862  * and ipx_pending_ipif are set. ipx_current_ipif is set throughout the
7863  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7864  * ipx_current_ipif is NULL which happens only once the ioctl is complete and
7865  * all associated DLPI operations have completed.
7866  */
7867 
7868 /*
7869  * Try to enter the IPSQ corresponding to `ipif' or `ill' exclusively (`ipif'
7870  * and `ill' cannot both be specified).  Returns a pointer to the entered IPSQ
7871  * on success, or NULL on failure.  The caller ensures ipif/ill is valid by
7872  * refholding it as necessary.  If the IPSQ cannot be entered and `func' is
7873  * non-NULL, then `func' will be called back with `q' and `mp' once the IPSQ
7874  * can be entered.  If `func' is NULL, then `q' and `mp' are ignored.
7875  */
7876 ipsq_t *
7877 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7878     ipsq_func_t func, int type, boolean_t reentry_ok)
7879 {
7880 	ipsq_t	*ipsq;
7881 	ipxop_t	*ipx;
7882 
7883 	/* Only 1 of ipif or ill can be specified */
7884 	ASSERT((ipif != NULL) ^ (ill != NULL));
7885 	if (ipif != NULL)
7886 		ill = ipif->ipif_ill;
7887 
7888 	/*
7889 	 * lock ordering: conn_lock -> ill_lock -> ipsq_lock -> ipx_lock.
7890 	 * ipx of an ipsq can't change when ipsq_lock is held.
7891 	 */
7892 	GRAB_CONN_LOCK(q);
7893 	mutex_enter(&ill->ill_lock);
7894 	ipsq = ill->ill_phyint->phyint_ipsq;
7895 	mutex_enter(&ipsq->ipsq_lock);
7896 	ipx = ipsq->ipsq_xop;
7897 	mutex_enter(&ipx->ipx_lock);
7898 
7899 	/*
7900 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7901 	 *    (Note: If the caller does not specify reentry_ok then neither
7902 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7903 	 *    again. Otherwise it can lead to an infinite loop
7904 	 * 2. Enter the ipsq if there is no current writer and this attempted
7905 	 *    entry is part of the current operation
7906 	 * 3. Enter the ipsq if there is no current writer and this is a new
7907 	 *    operation and the operation queue is empty and there is no
7908 	 *    operation currently in progress
7909 	 */
7910 	if ((ipx->ipx_writer == curthread && reentry_ok) ||
7911 	    (ipx->ipx_writer == NULL && (type == CUR_OP || (type == NEW_OP &&
7912 	    !ipx->ipx_ipsq_queued && ipx->ipx_current_ipif == NULL)))) {
7913 		/* Success. */
7914 		ipx->ipx_reentry_cnt++;
7915 		ipx->ipx_writer = curthread;
7916 		ipx->ipx_forced = B_FALSE;
7917 		mutex_exit(&ipx->ipx_lock);
7918 		mutex_exit(&ipsq->ipsq_lock);
7919 		mutex_exit(&ill->ill_lock);
7920 		RELEASE_CONN_LOCK(q);
7921 #ifdef DEBUG
7922 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
7923 #endif
7924 		return (ipsq);
7925 	}
7926 
7927 	if (func != NULL)
7928 		ipsq_enq(ipsq, q, mp, func, type, ill);
7929 
7930 	mutex_exit(&ipx->ipx_lock);
7931 	mutex_exit(&ipsq->ipsq_lock);
7932 	mutex_exit(&ill->ill_lock);
7933 	RELEASE_CONN_LOCK(q);
7934 	return (NULL);
7935 }
7936 
7937 /*
7938  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7939  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7940  * cannot be entered, the mp is queued for completion.
7941  */
7942 void
7943 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7944     boolean_t reentry_ok)
7945 {
7946 	ipsq_t	*ipsq;
7947 
7948 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
7949 
7950 	/*
7951 	 * Drop the caller's refhold on the ill.  This is safe since we either
7952 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
7953 	 * IPSQ, in which case we return without accessing ill anymore.  This
7954 	 * is needed because func needs to see the correct refcount.
7955 	 * e.g. removeif can work only then.
7956 	 */
7957 	ill_refrele(ill);
7958 	if (ipsq != NULL) {
7959 		(*func)(ipsq, q, mp, NULL);
7960 		ipsq_exit(ipsq);
7961 	}
7962 }
7963 
7964 /*
7965  * Exit the specified IPSQ.  If this is the final exit on it then drain it
7966  * prior to exiting.  Caller must be writer on the specified IPSQ.
7967  */
7968 void
7969 ipsq_exit(ipsq_t *ipsq)
7970 {
7971 	mblk_t *mp;
7972 	ipsq_t *mp_ipsq;
7973 	queue_t	*q;
7974 	phyint_t *phyi;
7975 	ipsq_func_t func;
7976 
7977 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7978 
7979 	ASSERT(ipsq->ipsq_xop->ipx_reentry_cnt >= 1);
7980 	if (ipsq->ipsq_xop->ipx_reentry_cnt != 1) {
7981 		ipsq->ipsq_xop->ipx_reentry_cnt--;
7982 		return;
7983 	}
7984 
7985 	for (;;) {
7986 		phyi = ipsq->ipsq_phyint;
7987 		mp = ipsq_dq(ipsq);
7988 		mp_ipsq = (mp == NULL) ? NULL : (ipsq_t *)mp->b_next;
7989 
7990 		/*
7991 		 * If we've changed to a new IPSQ, and the phyint associated
7992 		 * with the old one has gone away, free the old IPSQ.  Note
7993 		 * that this cannot happen while the IPSQ is in a group.
7994 		 */
7995 		if (mp_ipsq != ipsq && phyi == NULL) {
7996 			ASSERT(ipsq->ipsq_next == ipsq);
7997 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
7998 			ipsq_delete(ipsq);
7999 		}
8000 
8001 		if (mp == NULL)
8002 			break;
8003 
8004 		q = mp->b_queue;
8005 		func = (ipsq_func_t)mp->b_prev;
8006 		ipsq = mp_ipsq;
8007 		mp->b_next = mp->b_prev = NULL;
8008 		mp->b_queue = NULL;
8009 
8010 		/*
8011 		 * If 'q' is an conn queue, it is valid, since we did a
8012 		 * a refhold on the conn at the start of the ioctl.
8013 		 * If 'q' is an ill queue, it is valid, since close of an
8014 		 * ill will clean up its IPSQ.
8015 		 */
8016 		(*func)(ipsq, q, mp, NULL);
8017 	}
8018 }
8019 
8020 /*
8021  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
8022  * and `ioccmd'.
8023  */
8024 void
8025 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
8026 {
8027 	ipxop_t *ipx = ipsq->ipsq_xop;
8028 
8029 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8030 	ASSERT(ipx->ipx_current_ipif == NULL);
8031 	ASSERT(ipx->ipx_current_ioctl == 0);
8032 
8033 	ipx->ipx_current_done = B_FALSE;
8034 	ipx->ipx_current_ioctl = ioccmd;
8035 	mutex_enter(&ipx->ipx_lock);
8036 	ipx->ipx_current_ipif = ipif;
8037 	mutex_exit(&ipx->ipx_lock);
8038 }
8039 
8040 /*
8041  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
8042  * the next exclusive operation to begin once we ipsq_exit().  However, if
8043  * pending DLPI operations remain, then we will wait for the queue to drain
8044  * before allowing the next exclusive operation to begin.  This ensures that
8045  * DLPI operations from one exclusive operation are never improperly processed
8046  * as part of a subsequent exclusive operation.
8047  */
8048 void
8049 ipsq_current_finish(ipsq_t *ipsq)
8050 {
8051 	ipxop_t	*ipx = ipsq->ipsq_xop;
8052 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
8053 	ipif_t	*ipif = ipx->ipx_current_ipif;
8054 
8055 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8056 
8057 	/*
8058 	 * For SIOCLIFREMOVEIF, the ipif has been already been blown away
8059 	 * (but in that case, IPIF_CHANGING will already be clear and no
8060 	 * pending DLPI messages can remain).
8061 	 */
8062 	if (ipx->ipx_current_ioctl != SIOCLIFREMOVEIF) {
8063 		ill_t *ill = ipif->ipif_ill;
8064 
8065 		mutex_enter(&ill->ill_lock);
8066 		dlpi_pending = ill->ill_dlpi_pending;
8067 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
8068 		mutex_exit(&ill->ill_lock);
8069 	}
8070 
8071 	ASSERT(!ipx->ipx_current_done);
8072 	ipx->ipx_current_done = B_TRUE;
8073 	ipx->ipx_current_ioctl = 0;
8074 	if (dlpi_pending == DL_PRIM_INVAL) {
8075 		mutex_enter(&ipx->ipx_lock);
8076 		ipx->ipx_current_ipif = NULL;
8077 		mutex_exit(&ipx->ipx_lock);
8078 	}
8079 }
8080 
8081 /*
8082  * The ill is closing. Flush all messages on the ipsq that originated
8083  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
8084  * for this ill since ipsq_enter could not have entered until then.
8085  * New messages can't be queued since the CONDEMNED flag is set.
8086  */
8087 static void
8088 ipsq_flush(ill_t *ill)
8089 {
8090 	queue_t	*q;
8091 	mblk_t	*prev;
8092 	mblk_t	*mp;
8093 	mblk_t	*mp_next;
8094 	ipxop_t	*ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
8095 
8096 	ASSERT(IAM_WRITER_ILL(ill));
8097 
8098 	/*
8099 	 * Flush any messages sent up by the driver.
8100 	 */
8101 	mutex_enter(&ipx->ipx_lock);
8102 	for (prev = NULL, mp = ipx->ipx_mphead; mp != NULL; mp = mp_next) {
8103 		mp_next = mp->b_next;
8104 		q = mp->b_queue;
8105 		if (q == ill->ill_rq || q == ill->ill_wq) {
8106 			/* dequeue mp */
8107 			if (prev == NULL)
8108 				ipx->ipx_mphead = mp->b_next;
8109 			else
8110 				prev->b_next = mp->b_next;
8111 			if (ipx->ipx_mptail == mp) {
8112 				ASSERT(mp_next == NULL);
8113 				ipx->ipx_mptail = prev;
8114 			}
8115 			inet_freemsg(mp);
8116 		} else {
8117 			prev = mp;
8118 		}
8119 	}
8120 	mutex_exit(&ipx->ipx_lock);
8121 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8122 	ipsq_xopq_mp_cleanup(ill, NULL);
8123 	ill_pending_mp_cleanup(ill);
8124 }
8125 
8126 /*
8127  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8128  * refhold and return the associated ipif
8129  */
8130 /* ARGSUSED */
8131 int
8132 ip_extract_tunreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8133     cmd_info_t *ci, ipsq_func_t func)
8134 {
8135 	boolean_t exists;
8136 	struct iftun_req *ta;
8137 	ipif_t  *ipif;
8138 	ill_t   *ill;
8139 	boolean_t isv6;
8140 	mblk_t  *mp1;
8141 	int error;
8142 	conn_t  *connp;
8143 	ip_stack_t  *ipst;
8144 
8145 	/* Existence verified in ip_wput_nondata */
8146 	mp1 = mp->b_cont->b_cont;
8147 	ta = (struct iftun_req *)mp1->b_rptr;
8148 	/*
8149 	 * Null terminate the string to protect against buffer
8150 	 * overrun. String was generated by user code and may not
8151 	 * be trusted.
8152 	 */
8153 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8154 
8155 	connp = Q_TO_CONN(q);
8156 	isv6 = connp->conn_af_isv6;
8157 	ipst = connp->conn_netstack->netstack_ip;
8158 
8159 	/* Disallows implicit create */
8160 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8161 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8162 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8163 	if (ipif == NULL)
8164 		return (error);
8165 
8166 	if (ipif->ipif_id != 0) {
8167 		/*
8168 		 * We really don't want to set/get tunnel parameters
8169 		 * on virtual tunnel interfaces.  Only allow the
8170 		 * base tunnel to do these.
8171 		 */
8172 		ipif_refrele(ipif);
8173 		return (EINVAL);
8174 	}
8175 
8176 	/*
8177 	 * Send down to tunnel mod for ioctl processing.
8178 	 * Will finish ioctl in ip_rput_other().
8179 	 */
8180 	ill = ipif->ipif_ill;
8181 	if (ill->ill_net_type == IRE_LOOPBACK) {
8182 		ipif_refrele(ipif);
8183 		return (EOPNOTSUPP);
8184 	}
8185 
8186 	if (ill->ill_wq == NULL) {
8187 		ipif_refrele(ipif);
8188 		return (ENXIO);
8189 	}
8190 	/*
8191 	 * Mark the ioctl as coming from an IPv6 interface for
8192 	 * tun's convenience.
8193 	 */
8194 	if (ill->ill_isv6)
8195 		ta->ifta_flags |= 0x80000000;
8196 	ci->ci_ipif = ipif;
8197 	return (0);
8198 }
8199 
8200 /*
8201  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8202  * and return the associated ipif.
8203  * Return value:
8204  *	Non zero: An error has occurred. ci may not be filled out.
8205  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8206  *	a held ipif in ci.ci_ipif.
8207  */
8208 int
8209 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8210     cmd_info_t *ci, ipsq_func_t func)
8211 {
8212 	char		*name;
8213 	struct ifreq    *ifr;
8214 	struct lifreq    *lifr;
8215 	ipif_t		*ipif = NULL;
8216 	ill_t		*ill;
8217 	conn_t		*connp;
8218 	boolean_t	isv6;
8219 	boolean_t	exists;
8220 	int		err;
8221 	mblk_t		*mp1;
8222 	zoneid_t	zoneid;
8223 	ip_stack_t	*ipst;
8224 
8225 	if (q->q_next != NULL) {
8226 		ill = (ill_t *)q->q_ptr;
8227 		isv6 = ill->ill_isv6;
8228 		connp = NULL;
8229 		zoneid = ALL_ZONES;
8230 		ipst = ill->ill_ipst;
8231 	} else {
8232 		ill = NULL;
8233 		connp = Q_TO_CONN(q);
8234 		isv6 = connp->conn_af_isv6;
8235 		zoneid = connp->conn_zoneid;
8236 		if (zoneid == GLOBAL_ZONEID) {
8237 			/* global zone can access ipifs in all zones */
8238 			zoneid = ALL_ZONES;
8239 		}
8240 		ipst = connp->conn_netstack->netstack_ip;
8241 	}
8242 
8243 	/* Has been checked in ip_wput_nondata */
8244 	mp1 = mp->b_cont->b_cont;
8245 
8246 	if (ipip->ipi_cmd_type == IF_CMD) {
8247 		/* This a old style SIOC[GS]IF* command */
8248 		ifr = (struct ifreq *)mp1->b_rptr;
8249 		/*
8250 		 * Null terminate the string to protect against buffer
8251 		 * overrun. String was generated by user code and may not
8252 		 * be trusted.
8253 		 */
8254 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8255 		name = ifr->ifr_name;
8256 		ci->ci_sin = (sin_t *)&ifr->ifr_addr;
8257 		ci->ci_sin6 = NULL;
8258 		ci->ci_lifr = (struct lifreq *)ifr;
8259 	} else {
8260 		/* This a new style SIOC[GS]LIF* command */
8261 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
8262 		lifr = (struct lifreq *)mp1->b_rptr;
8263 		/*
8264 		 * Null terminate the string to protect against buffer
8265 		 * overrun. String was generated by user code and may not
8266 		 * be trusted.
8267 		 */
8268 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8269 		name = lifr->lifr_name;
8270 		ci->ci_sin = (sin_t *)&lifr->lifr_addr;
8271 		ci->ci_sin6 = (sin6_t *)&lifr->lifr_addr;
8272 		ci->ci_lifr = lifr;
8273 	}
8274 
8275 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
8276 		/*
8277 		 * The ioctl will be failed if the ioctl comes down
8278 		 * an conn stream
8279 		 */
8280 		if (ill == NULL) {
8281 			/*
8282 			 * Not an ill queue, return EINVAL same as the
8283 			 * old error code.
8284 			 */
8285 			return (ENXIO);
8286 		}
8287 		ipif = ill->ill_ipif;
8288 		ipif_refhold(ipif);
8289 	} else {
8290 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8291 		    &exists, isv6, zoneid,
8292 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8293 		    ipst);
8294 		if (ipif == NULL) {
8295 			if (err == EINPROGRESS)
8296 				return (err);
8297 			err = 0;	/* Ensure we don't use it below */
8298 		}
8299 	}
8300 
8301 	/*
8302 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8303 	 */
8304 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
8305 		ipif_refrele(ipif);
8306 		return (ENXIO);
8307 	}
8308 
8309 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8310 	    name[0] == '\0') {
8311 		/*
8312 		 * Handle a or a SIOC?IF* with a null name
8313 		 * during plumb (on the ill queue before the I_PLINK).
8314 		 */
8315 		ipif = ill->ill_ipif;
8316 		ipif_refhold(ipif);
8317 	}
8318 
8319 	if (ipif == NULL)
8320 		return (ENXIO);
8321 
8322 	ci->ci_ipif = ipif;
8323 	return (0);
8324 }
8325 
8326 /*
8327  * Return the total number of ipifs.
8328  */
8329 static uint_t
8330 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8331 {
8332 	uint_t numifs = 0;
8333 	ill_t	*ill;
8334 	ill_walk_context_t	ctx;
8335 	ipif_t	*ipif;
8336 
8337 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8338 	ill = ILL_START_WALK_V4(&ctx, ipst);
8339 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8340 		if (IS_UNDER_IPMP(ill))
8341 			continue;
8342 		for (ipif = ill->ill_ipif; ipif != NULL;
8343 		    ipif = ipif->ipif_next) {
8344 			if (ipif->ipif_zoneid == zoneid ||
8345 			    ipif->ipif_zoneid == ALL_ZONES)
8346 				numifs++;
8347 		}
8348 	}
8349 	rw_exit(&ipst->ips_ill_g_lock);
8350 	return (numifs);
8351 }
8352 
8353 /*
8354  * Return the total number of ipifs.
8355  */
8356 static uint_t
8357 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8358 {
8359 	uint_t numifs = 0;
8360 	ill_t	*ill;
8361 	ipif_t	*ipif;
8362 	ill_walk_context_t	ctx;
8363 
8364 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8365 
8366 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8367 	if (family == AF_INET)
8368 		ill = ILL_START_WALK_V4(&ctx, ipst);
8369 	else if (family == AF_INET6)
8370 		ill = ILL_START_WALK_V6(&ctx, ipst);
8371 	else
8372 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8373 
8374 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8375 		if (IS_UNDER_IPMP(ill) && !(lifn_flags & LIFC_UNDER_IPMP))
8376 			continue;
8377 
8378 		for (ipif = ill->ill_ipif; ipif != NULL;
8379 		    ipif = ipif->ipif_next) {
8380 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8381 			    !(lifn_flags & LIFC_NOXMIT))
8382 				continue;
8383 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8384 			    !(lifn_flags & LIFC_TEMPORARY))
8385 				continue;
8386 			if (((ipif->ipif_flags &
8387 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8388 			    IPIF_DEPRECATED)) ||
8389 			    IS_LOOPBACK(ill) ||
8390 			    !(ipif->ipif_flags & IPIF_UP)) &&
8391 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8392 				continue;
8393 
8394 			if (zoneid != ipif->ipif_zoneid &&
8395 			    ipif->ipif_zoneid != ALL_ZONES &&
8396 			    (zoneid != GLOBAL_ZONEID ||
8397 			    !(lifn_flags & LIFC_ALLZONES)))
8398 				continue;
8399 
8400 			numifs++;
8401 		}
8402 	}
8403 	rw_exit(&ipst->ips_ill_g_lock);
8404 	return (numifs);
8405 }
8406 
8407 uint_t
8408 ip_get_lifsrcofnum(ill_t *ill)
8409 {
8410 	uint_t numifs = 0;
8411 	ill_t	*ill_head = ill;
8412 	ip_stack_t	*ipst = ill->ill_ipst;
8413 
8414 	/*
8415 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8416 	 * other thread may be trying to relink the ILLs in this usesrc group
8417 	 * and adjusting the ill_usesrc_grp_next pointers
8418 	 */
8419 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8420 	if ((ill->ill_usesrc_ifindex == 0) &&
8421 	    (ill->ill_usesrc_grp_next != NULL)) {
8422 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8423 		    ill = ill->ill_usesrc_grp_next)
8424 			numifs++;
8425 	}
8426 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8427 
8428 	return (numifs);
8429 }
8430 
8431 /* Null values are passed in for ipif, sin, and ifreq */
8432 /* ARGSUSED */
8433 int
8434 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8435     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8436 {
8437 	int *nump;
8438 	conn_t *connp = Q_TO_CONN(q);
8439 
8440 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8441 
8442 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8443 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8444 
8445 	*nump = ip_get_numifs(connp->conn_zoneid,
8446 	    connp->conn_netstack->netstack_ip);
8447 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8448 	return (0);
8449 }
8450 
8451 /* Null values are passed in for ipif, sin, and ifreq */
8452 /* ARGSUSED */
8453 int
8454 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8455     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8456 {
8457 	struct lifnum *lifn;
8458 	mblk_t	*mp1;
8459 	conn_t *connp = Q_TO_CONN(q);
8460 
8461 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8462 
8463 	/* Existence checked in ip_wput_nondata */
8464 	mp1 = mp->b_cont->b_cont;
8465 
8466 	lifn = (struct lifnum *)mp1->b_rptr;
8467 	switch (lifn->lifn_family) {
8468 	case AF_UNSPEC:
8469 	case AF_INET:
8470 	case AF_INET6:
8471 		break;
8472 	default:
8473 		return (EAFNOSUPPORT);
8474 	}
8475 
8476 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8477 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8478 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8479 	return (0);
8480 }
8481 
8482 /* ARGSUSED */
8483 int
8484 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8485     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8486 {
8487 	STRUCT_HANDLE(ifconf, ifc);
8488 	mblk_t *mp1;
8489 	struct iocblk *iocp;
8490 	struct ifreq *ifr;
8491 	ill_walk_context_t	ctx;
8492 	ill_t	*ill;
8493 	ipif_t	*ipif;
8494 	struct sockaddr_in *sin;
8495 	int32_t	ifclen;
8496 	zoneid_t zoneid;
8497 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8498 
8499 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8500 
8501 	ip1dbg(("ip_sioctl_get_ifconf"));
8502 	/* Existence verified in ip_wput_nondata */
8503 	mp1 = mp->b_cont->b_cont;
8504 	iocp = (struct iocblk *)mp->b_rptr;
8505 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8506 
8507 	/*
8508 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8509 	 * the user buffer address and length into which the list of struct
8510 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8511 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8512 	 * the SIOCGIFCONF operation was redefined to simply provide
8513 	 * a large output buffer into which we are supposed to jam the ifreq
8514 	 * array.  The same ioctl command code was used, despite the fact that
8515 	 * both the applications and the kernel code had to change, thus making
8516 	 * it impossible to support both interfaces.
8517 	 *
8518 	 * For reasons not good enough to try to explain, the following
8519 	 * algorithm is used for deciding what to do with one of these:
8520 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8521 	 * form with the output buffer coming down as the continuation message.
8522 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8523 	 * and we have to copy in the ifconf structure to find out how big the
8524 	 * output buffer is and where to copy out to.  Sure no problem...
8525 	 *
8526 	 */
8527 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8528 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8529 		int numifs = 0;
8530 		size_t ifc_bufsize;
8531 
8532 		/*
8533 		 * Must be (better be!) continuation of a TRANSPARENT
8534 		 * IOCTL.  We just copied in the ifconf structure.
8535 		 */
8536 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8537 		    (struct ifconf *)mp1->b_rptr);
8538 
8539 		/*
8540 		 * Allocate a buffer to hold requested information.
8541 		 *
8542 		 * If ifc_len is larger than what is needed, we only
8543 		 * allocate what we will use.
8544 		 *
8545 		 * If ifc_len is smaller than what is needed, return
8546 		 * EINVAL.
8547 		 *
8548 		 * XXX: the ill_t structure can hava 2 counters, for
8549 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8550 		 * number of interfaces for a device, so we don't need
8551 		 * to count them here...
8552 		 */
8553 		numifs = ip_get_numifs(zoneid, ipst);
8554 
8555 		ifclen = STRUCT_FGET(ifc, ifc_len);
8556 		ifc_bufsize = numifs * sizeof (struct ifreq);
8557 		if (ifc_bufsize > ifclen) {
8558 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8559 				/* old behaviour */
8560 				return (EINVAL);
8561 			} else {
8562 				ifc_bufsize = ifclen;
8563 			}
8564 		}
8565 
8566 		mp1 = mi_copyout_alloc(q, mp,
8567 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8568 		if (mp1 == NULL)
8569 			return (ENOMEM);
8570 
8571 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8572 	}
8573 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8574 	/*
8575 	 * the SIOCGIFCONF ioctl only knows about
8576 	 * IPv4 addresses, so don't try to tell
8577 	 * it about interfaces with IPv6-only
8578 	 * addresses. (Last parm 'isv6' is B_FALSE)
8579 	 */
8580 
8581 	ifr = (struct ifreq *)mp1->b_rptr;
8582 
8583 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8584 	ill = ILL_START_WALK_V4(&ctx, ipst);
8585 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8586 		if (IS_UNDER_IPMP(ill))
8587 			continue;
8588 		for (ipif = ill->ill_ipif; ipif != NULL;
8589 		    ipif = ipif->ipif_next) {
8590 			if (zoneid != ipif->ipif_zoneid &&
8591 			    ipif->ipif_zoneid != ALL_ZONES)
8592 				continue;
8593 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8594 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8595 					/* old behaviour */
8596 					rw_exit(&ipst->ips_ill_g_lock);
8597 					return (EINVAL);
8598 				} else {
8599 					goto if_copydone;
8600 				}
8601 			}
8602 			ipif_get_name(ipif, ifr->ifr_name,
8603 			    sizeof (ifr->ifr_name));
8604 			sin = (sin_t *)&ifr->ifr_addr;
8605 			*sin = sin_null;
8606 			sin->sin_family = AF_INET;
8607 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8608 			ifr++;
8609 		}
8610 	}
8611 if_copydone:
8612 	rw_exit(&ipst->ips_ill_g_lock);
8613 	mp1->b_wptr = (uchar_t *)ifr;
8614 
8615 	if (STRUCT_BUF(ifc) != NULL) {
8616 		STRUCT_FSET(ifc, ifc_len,
8617 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8618 	}
8619 	return (0);
8620 }
8621 
8622 /*
8623  * Get the interfaces using the address hosted on the interface passed in,
8624  * as a source adddress
8625  */
8626 /* ARGSUSED */
8627 int
8628 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8629     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8630 {
8631 	mblk_t *mp1;
8632 	ill_t	*ill, *ill_head;
8633 	ipif_t	*ipif, *orig_ipif;
8634 	int	numlifs = 0;
8635 	size_t	lifs_bufsize, lifsmaxlen;
8636 	struct	lifreq *lifr;
8637 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8638 	uint_t	ifindex;
8639 	zoneid_t zoneid;
8640 	int err = 0;
8641 	boolean_t isv6 = B_FALSE;
8642 	struct	sockaddr_in	*sin;
8643 	struct	sockaddr_in6	*sin6;
8644 	STRUCT_HANDLE(lifsrcof, lifs);
8645 	ip_stack_t		*ipst;
8646 
8647 	ipst = CONNQ_TO_IPST(q);
8648 
8649 	ASSERT(q->q_next == NULL);
8650 
8651 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8652 
8653 	/* Existence verified in ip_wput_nondata */
8654 	mp1 = mp->b_cont->b_cont;
8655 
8656 	/*
8657 	 * Must be (better be!) continuation of a TRANSPARENT
8658 	 * IOCTL.  We just copied in the lifsrcof structure.
8659 	 */
8660 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8661 	    (struct lifsrcof *)mp1->b_rptr);
8662 
8663 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8664 		return (EINVAL);
8665 
8666 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8667 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8668 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8669 	    ip_process_ioctl, &err, ipst);
8670 	if (ipif == NULL) {
8671 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8672 		    ifindex));
8673 		return (err);
8674 	}
8675 
8676 	/* Allocate a buffer to hold requested information */
8677 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8678 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8679 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8680 	/* The actual size needed is always returned in lifs_len */
8681 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8682 
8683 	/* If the amount we need is more than what is passed in, abort */
8684 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8685 		ipif_refrele(ipif);
8686 		return (0);
8687 	}
8688 
8689 	mp1 = mi_copyout_alloc(q, mp,
8690 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8691 	if (mp1 == NULL) {
8692 		ipif_refrele(ipif);
8693 		return (ENOMEM);
8694 	}
8695 
8696 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8697 	bzero(mp1->b_rptr, lifs_bufsize);
8698 
8699 	lifr = (struct lifreq *)mp1->b_rptr;
8700 
8701 	ill = ill_head = ipif->ipif_ill;
8702 	orig_ipif = ipif;
8703 
8704 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8705 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8706 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8707 
8708 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8709 	for (; (ill != NULL) && (ill != ill_head);
8710 	    ill = ill->ill_usesrc_grp_next) {
8711 
8712 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8713 			break;
8714 
8715 		ipif = ill->ill_ipif;
8716 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
8717 		if (ipif->ipif_isv6) {
8718 			sin6 = (sin6_t *)&lifr->lifr_addr;
8719 			*sin6 = sin6_null;
8720 			sin6->sin6_family = AF_INET6;
8721 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8722 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8723 			    &ipif->ipif_v6net_mask);
8724 		} else {
8725 			sin = (sin_t *)&lifr->lifr_addr;
8726 			*sin = sin_null;
8727 			sin->sin_family = AF_INET;
8728 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8729 			lifr->lifr_addrlen = ip_mask_to_plen(
8730 			    ipif->ipif_net_mask);
8731 		}
8732 		lifr++;
8733 	}
8734 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8735 	rw_exit(&ipst->ips_ill_g_lock);
8736 	ipif_refrele(orig_ipif);
8737 	mp1->b_wptr = (uchar_t *)lifr;
8738 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8739 
8740 	return (0);
8741 }
8742 
8743 /* ARGSUSED */
8744 int
8745 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8746     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8747 {
8748 	mblk_t *mp1;
8749 	int	list;
8750 	ill_t	*ill;
8751 	ipif_t	*ipif;
8752 	int	flags;
8753 	int	numlifs = 0;
8754 	size_t	lifc_bufsize;
8755 	struct	lifreq *lifr;
8756 	sa_family_t	family;
8757 	struct	sockaddr_in	*sin;
8758 	struct	sockaddr_in6	*sin6;
8759 	ill_walk_context_t	ctx;
8760 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8761 	int32_t	lifclen;
8762 	zoneid_t zoneid;
8763 	STRUCT_HANDLE(lifconf, lifc);
8764 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8765 
8766 	ip1dbg(("ip_sioctl_get_lifconf"));
8767 
8768 	ASSERT(q->q_next == NULL);
8769 
8770 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8771 
8772 	/* Existence verified in ip_wput_nondata */
8773 	mp1 = mp->b_cont->b_cont;
8774 
8775 	/*
8776 	 * An extended version of SIOCGIFCONF that takes an
8777 	 * additional address family and flags field.
8778 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8779 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8780 	 * interfaces are omitted.
8781 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8782 	 * unless LIFC_TEMPORARY is specified.
8783 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8784 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8785 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8786 	 * has priority over LIFC_NOXMIT.
8787 	 */
8788 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8789 
8790 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8791 		return (EINVAL);
8792 
8793 	/*
8794 	 * Must be (better be!) continuation of a TRANSPARENT
8795 	 * IOCTL.  We just copied in the lifconf structure.
8796 	 */
8797 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8798 
8799 	family = STRUCT_FGET(lifc, lifc_family);
8800 	flags = STRUCT_FGET(lifc, lifc_flags);
8801 
8802 	switch (family) {
8803 	case AF_UNSPEC:
8804 		/*
8805 		 * walk all ILL's.
8806 		 */
8807 		list = MAX_G_HEADS;
8808 		break;
8809 	case AF_INET:
8810 		/*
8811 		 * walk only IPV4 ILL's.
8812 		 */
8813 		list = IP_V4_G_HEAD;
8814 		break;
8815 	case AF_INET6:
8816 		/*
8817 		 * walk only IPV6 ILL's.
8818 		 */
8819 		list = IP_V6_G_HEAD;
8820 		break;
8821 	default:
8822 		return (EAFNOSUPPORT);
8823 	}
8824 
8825 	/*
8826 	 * Allocate a buffer to hold requested information.
8827 	 *
8828 	 * If lifc_len is larger than what is needed, we only
8829 	 * allocate what we will use.
8830 	 *
8831 	 * If lifc_len is smaller than what is needed, return
8832 	 * EINVAL.
8833 	 */
8834 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
8835 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8836 	lifclen = STRUCT_FGET(lifc, lifc_len);
8837 	if (lifc_bufsize > lifclen) {
8838 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8839 			return (EINVAL);
8840 		else
8841 			lifc_bufsize = lifclen;
8842 	}
8843 
8844 	mp1 = mi_copyout_alloc(q, mp,
8845 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8846 	if (mp1 == NULL)
8847 		return (ENOMEM);
8848 
8849 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8850 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8851 
8852 	lifr = (struct lifreq *)mp1->b_rptr;
8853 
8854 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8855 	ill = ill_first(list, list, &ctx, ipst);
8856 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8857 		if (IS_UNDER_IPMP(ill) && !(flags & LIFC_UNDER_IPMP))
8858 			continue;
8859 
8860 		for (ipif = ill->ill_ipif; ipif != NULL;
8861 		    ipif = ipif->ipif_next) {
8862 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8863 			    !(flags & LIFC_NOXMIT))
8864 				continue;
8865 
8866 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8867 			    !(flags & LIFC_TEMPORARY))
8868 				continue;
8869 
8870 			if (((ipif->ipif_flags &
8871 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8872 			    IPIF_DEPRECATED)) ||
8873 			    IS_LOOPBACK(ill) ||
8874 			    !(ipif->ipif_flags & IPIF_UP)) &&
8875 			    (flags & LIFC_EXTERNAL_SOURCE))
8876 				continue;
8877 
8878 			if (zoneid != ipif->ipif_zoneid &&
8879 			    ipif->ipif_zoneid != ALL_ZONES &&
8880 			    (zoneid != GLOBAL_ZONEID ||
8881 			    !(flags & LIFC_ALLZONES)))
8882 				continue;
8883 
8884 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8885 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8886 					rw_exit(&ipst->ips_ill_g_lock);
8887 					return (EINVAL);
8888 				} else {
8889 					goto lif_copydone;
8890 				}
8891 			}
8892 
8893 			ipif_get_name(ipif, lifr->lifr_name,
8894 			    sizeof (lifr->lifr_name));
8895 			lifr->lifr_type = ill->ill_type;
8896 			if (ipif->ipif_isv6) {
8897 				sin6 = (sin6_t *)&lifr->lifr_addr;
8898 				*sin6 = sin6_null;
8899 				sin6->sin6_family = AF_INET6;
8900 				sin6->sin6_addr =
8901 				    ipif->ipif_v6lcl_addr;
8902 				lifr->lifr_addrlen =
8903 				    ip_mask_to_plen_v6(
8904 				    &ipif->ipif_v6net_mask);
8905 			} else {
8906 				sin = (sin_t *)&lifr->lifr_addr;
8907 				*sin = sin_null;
8908 				sin->sin_family = AF_INET;
8909 				sin->sin_addr.s_addr =
8910 				    ipif->ipif_lcl_addr;
8911 				lifr->lifr_addrlen =
8912 				    ip_mask_to_plen(
8913 				    ipif->ipif_net_mask);
8914 			}
8915 			lifr++;
8916 		}
8917 	}
8918 lif_copydone:
8919 	rw_exit(&ipst->ips_ill_g_lock);
8920 
8921 	mp1->b_wptr = (uchar_t *)lifr;
8922 	if (STRUCT_BUF(lifc) != NULL) {
8923 		STRUCT_FSET(lifc, lifc_len,
8924 		    (int)((uchar_t *)lifr - mp1->b_rptr));
8925 	}
8926 	return (0);
8927 }
8928 
8929 static void
8930 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
8931 {
8932 	ip6_asp_t *table;
8933 	size_t table_size;
8934 	mblk_t *data_mp;
8935 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8936 	ip_stack_t	*ipst;
8937 
8938 	if (q->q_next == NULL)
8939 		ipst = CONNQ_TO_IPST(q);
8940 	else
8941 		ipst = ILLQ_TO_IPST(q);
8942 
8943 	/* These two ioctls are I_STR only */
8944 	if (iocp->ioc_count == TRANSPARENT) {
8945 		miocnak(q, mp, 0, EINVAL);
8946 		return;
8947 	}
8948 
8949 	data_mp = mp->b_cont;
8950 	if (data_mp == NULL) {
8951 		/* The user passed us a NULL argument */
8952 		table = NULL;
8953 		table_size = iocp->ioc_count;
8954 	} else {
8955 		/*
8956 		 * The user provided a table.  The stream head
8957 		 * may have copied in the user data in chunks,
8958 		 * so make sure everything is pulled up
8959 		 * properly.
8960 		 */
8961 		if (MBLKL(data_mp) < iocp->ioc_count) {
8962 			mblk_t *new_data_mp;
8963 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
8964 			    NULL) {
8965 				miocnak(q, mp, 0, ENOMEM);
8966 				return;
8967 			}
8968 			freemsg(data_mp);
8969 			data_mp = new_data_mp;
8970 			mp->b_cont = data_mp;
8971 		}
8972 		table = (ip6_asp_t *)data_mp->b_rptr;
8973 		table_size = iocp->ioc_count;
8974 	}
8975 
8976 	switch (iocp->ioc_cmd) {
8977 	case SIOCGIP6ADDRPOLICY:
8978 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
8979 		if (iocp->ioc_rval == -1)
8980 			iocp->ioc_error = EINVAL;
8981 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8982 		else if (table != NULL &&
8983 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
8984 			ip6_asp_t *src = table;
8985 			ip6_asp32_t *dst = (void *)table;
8986 			int count = table_size / sizeof (ip6_asp_t);
8987 			int i;
8988 
8989 			/*
8990 			 * We need to do an in-place shrink of the array
8991 			 * to match the alignment attributes of the
8992 			 * 32-bit ABI looking at it.
8993 			 */
8994 			/* LINTED: logical expression always true: op "||" */
8995 			ASSERT(sizeof (*src) > sizeof (*dst));
8996 			for (i = 1; i < count; i++)
8997 				bcopy(src + i, dst + i, sizeof (*dst));
8998 		}
8999 #endif
9000 		break;
9001 
9002 	case SIOCSIP6ADDRPOLICY:
9003 		ASSERT(mp->b_prev == NULL);
9004 		mp->b_prev = (void *)q;
9005 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9006 		/*
9007 		 * We pass in the datamodel here so that the ip6_asp_replace()
9008 		 * routine can handle converting from 32-bit to native formats
9009 		 * where necessary.
9010 		 *
9011 		 * A better way to handle this might be to convert the inbound
9012 		 * data structure here, and hang it off a new 'mp'; thus the
9013 		 * ip6_asp_replace() logic would always be dealing with native
9014 		 * format data structures..
9015 		 *
9016 		 * (An even simpler way to handle these ioctls is to just
9017 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9018 		 * and just recompile everything that depends on it.)
9019 		 */
9020 #endif
9021 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
9022 		    iocp->ioc_flag & IOC_MODELS);
9023 		return;
9024 	}
9025 
9026 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9027 	qreply(q, mp);
9028 }
9029 
9030 static void
9031 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9032 {
9033 	mblk_t 		*data_mp;
9034 	struct dstinforeq	*dir;
9035 	uint8_t		*end, *cur;
9036 	in6_addr_t	*daddr, *saddr;
9037 	ipaddr_t	v4daddr;
9038 	ire_t		*ire;
9039 	char		*slabel, *dlabel;
9040 	boolean_t	isipv4;
9041 	int		match_ire;
9042 	ill_t		*dst_ill;
9043 	ipif_t		*src_ipif, *ire_ipif;
9044 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9045 	zoneid_t	zoneid;
9046 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
9047 
9048 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9049 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9050 
9051 	/*
9052 	 * This ioctl is I_STR only, and must have a
9053 	 * data mblk following the M_IOCTL mblk.
9054 	 */
9055 	data_mp = mp->b_cont;
9056 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9057 		miocnak(q, mp, 0, EINVAL);
9058 		return;
9059 	}
9060 
9061 	if (MBLKL(data_mp) < iocp->ioc_count) {
9062 		mblk_t *new_data_mp;
9063 
9064 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9065 			miocnak(q, mp, 0, ENOMEM);
9066 			return;
9067 		}
9068 		freemsg(data_mp);
9069 		data_mp = new_data_mp;
9070 		mp->b_cont = data_mp;
9071 	}
9072 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9073 
9074 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9075 	    end - cur >= sizeof (struct dstinforeq);
9076 	    cur += sizeof (struct dstinforeq)) {
9077 		dir = (struct dstinforeq *)cur;
9078 		daddr = &dir->dir_daddr;
9079 		saddr = &dir->dir_saddr;
9080 
9081 		/*
9082 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9083 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9084 		 * and ipif_select_source[_v6]() do not.
9085 		 */
9086 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9087 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9088 
9089 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9090 		if (isipv4) {
9091 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9092 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9093 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9094 		} else {
9095 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9096 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9097 		}
9098 		if (ire == NULL) {
9099 			dir->dir_dreachable = 0;
9100 
9101 			/* move on to next dst addr */
9102 			continue;
9103 		}
9104 		dir->dir_dreachable = 1;
9105 
9106 		ire_ipif = ire->ire_ipif;
9107 		if (ire_ipif == NULL)
9108 			goto next_dst;
9109 
9110 		/*
9111 		 * We expect to get back an interface ire or a
9112 		 * gateway ire cache entry.  For both types, the
9113 		 * output interface is ire_ipif->ipif_ill.
9114 		 */
9115 		dst_ill = ire_ipif->ipif_ill;
9116 		dir->dir_dmactype = dst_ill->ill_mactype;
9117 
9118 		if (isipv4) {
9119 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9120 		} else {
9121 			src_ipif = ipif_select_source_v6(dst_ill,
9122 			    daddr, B_FALSE, IPV6_PREFER_SRC_DEFAULT, zoneid);
9123 		}
9124 		if (src_ipif == NULL)
9125 			goto next_dst;
9126 
9127 		*saddr = src_ipif->ipif_v6lcl_addr;
9128 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9129 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9130 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9131 		dir->dir_sdeprecated =
9132 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9133 		ipif_refrele(src_ipif);
9134 next_dst:
9135 		ire_refrele(ire);
9136 	}
9137 	miocack(q, mp, iocp->ioc_count, 0);
9138 }
9139 
9140 /*
9141  * Check if this is an address assigned to this machine.
9142  * Skips interfaces that are down by using ire checks.
9143  * Translates mapped addresses to v4 addresses and then
9144  * treats them as such, returning true if the v4 address
9145  * associated with this mapped address is configured.
9146  * Note: Applications will have to be careful what they do
9147  * with the response; use of mapped addresses limits
9148  * what can be done with the socket, especially with
9149  * respect to socket options and ioctls - neither IPv4
9150  * options nor IPv6 sticky options/ancillary data options
9151  * may be used.
9152  */
9153 /* ARGSUSED */
9154 int
9155 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9156     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9157 {
9158 	struct sioc_addrreq *sia;
9159 	sin_t *sin;
9160 	ire_t *ire;
9161 	mblk_t *mp1;
9162 	zoneid_t zoneid;
9163 	ip_stack_t	*ipst;
9164 
9165 	ip1dbg(("ip_sioctl_tmyaddr"));
9166 
9167 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9168 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9169 	ipst = CONNQ_TO_IPST(q);
9170 
9171 	/* Existence verified in ip_wput_nondata */
9172 	mp1 = mp->b_cont->b_cont;
9173 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9174 	sin = (sin_t *)&sia->sa_addr;
9175 	switch (sin->sin_family) {
9176 	case AF_INET6: {
9177 		sin6_t *sin6 = (sin6_t *)sin;
9178 
9179 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9180 			ipaddr_t v4_addr;
9181 
9182 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9183 			    v4_addr);
9184 			ire = ire_ctable_lookup(v4_addr, 0,
9185 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9186 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9187 		} else {
9188 			in6_addr_t v6addr;
9189 
9190 			v6addr = sin6->sin6_addr;
9191 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9192 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9193 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9194 		}
9195 		break;
9196 	}
9197 	case AF_INET: {
9198 		ipaddr_t v4addr;
9199 
9200 		v4addr = sin->sin_addr.s_addr;
9201 		ire = ire_ctable_lookup(v4addr, 0,
9202 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9203 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9204 		break;
9205 	}
9206 	default:
9207 		return (EAFNOSUPPORT);
9208 	}
9209 	if (ire != NULL) {
9210 		sia->sa_res = 1;
9211 		ire_refrele(ire);
9212 	} else {
9213 		sia->sa_res = 0;
9214 	}
9215 	return (0);
9216 }
9217 
9218 /*
9219  * Check if this is an address assigned on-link i.e. neighbor,
9220  * and makes sure it's reachable from the current zone.
9221  * Returns true for my addresses as well.
9222  * Translates mapped addresses to v4 addresses and then
9223  * treats them as such, returning true if the v4 address
9224  * associated with this mapped address is configured.
9225  * Note: Applications will have to be careful what they do
9226  * with the response; use of mapped addresses limits
9227  * what can be done with the socket, especially with
9228  * respect to socket options and ioctls - neither IPv4
9229  * options nor IPv6 sticky options/ancillary data options
9230  * may be used.
9231  */
9232 /* ARGSUSED */
9233 int
9234 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9235     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9236 {
9237 	struct sioc_addrreq *sia;
9238 	sin_t *sin;
9239 	mblk_t	*mp1;
9240 	ire_t *ire = NULL;
9241 	zoneid_t zoneid;
9242 	ip_stack_t	*ipst;
9243 
9244 	ip1dbg(("ip_sioctl_tonlink"));
9245 
9246 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9247 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9248 	ipst = CONNQ_TO_IPST(q);
9249 
9250 	/* Existence verified in ip_wput_nondata */
9251 	mp1 = mp->b_cont->b_cont;
9252 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9253 	sin = (sin_t *)&sia->sa_addr;
9254 
9255 	/*
9256 	 * Match addresses with a zero gateway field to avoid
9257 	 * routes going through a router.
9258 	 * Exclude broadcast and multicast addresses.
9259 	 */
9260 	switch (sin->sin_family) {
9261 	case AF_INET6: {
9262 		sin6_t *sin6 = (sin6_t *)sin;
9263 
9264 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9265 			ipaddr_t v4_addr;
9266 
9267 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9268 			    v4_addr);
9269 			if (!CLASSD(v4_addr)) {
9270 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9271 				    NULL, NULL, zoneid, NULL,
9272 				    MATCH_IRE_GW, ipst);
9273 			}
9274 		} else {
9275 			in6_addr_t v6addr;
9276 			in6_addr_t v6gw;
9277 
9278 			v6addr = sin6->sin6_addr;
9279 			v6gw = ipv6_all_zeros;
9280 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9281 				ire = ire_route_lookup_v6(&v6addr, 0,
9282 				    &v6gw, 0, NULL, NULL, zoneid,
9283 				    NULL, MATCH_IRE_GW, ipst);
9284 			}
9285 		}
9286 		break;
9287 	}
9288 	case AF_INET: {
9289 		ipaddr_t v4addr;
9290 
9291 		v4addr = sin->sin_addr.s_addr;
9292 		if (!CLASSD(v4addr)) {
9293 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9294 			    NULL, NULL, zoneid, NULL,
9295 			    MATCH_IRE_GW, ipst);
9296 		}
9297 		break;
9298 	}
9299 	default:
9300 		return (EAFNOSUPPORT);
9301 	}
9302 	sia->sa_res = 0;
9303 	if (ire != NULL) {
9304 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9305 		    IRE_LOCAL|IRE_LOOPBACK)) {
9306 			sia->sa_res = 1;
9307 		}
9308 		ire_refrele(ire);
9309 	}
9310 	return (0);
9311 }
9312 
9313 /*
9314  * TBD: implement when kernel maintaines a list of site prefixes.
9315  */
9316 /* ARGSUSED */
9317 int
9318 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9319     ip_ioctl_cmd_t *ipip, void *ifreq)
9320 {
9321 	return (ENXIO);
9322 }
9323 
9324 /* ARGSUSED */
9325 int
9326 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9327     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9328 {
9329 	ill_t		*ill;
9330 	mblk_t		*mp1;
9331 	conn_t		*connp;
9332 	boolean_t	success;
9333 
9334 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9335 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9336 	/* ioctl comes down on an conn */
9337 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9338 	connp = Q_TO_CONN(q);
9339 
9340 	mp->b_datap->db_type = M_IOCTL;
9341 
9342 	/*
9343 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9344 	 * The original mp contains contaminated b_next values due to 'mi',
9345 	 * which is needed to do the mi_copy_done. Unfortunately if we
9346 	 * send down the original mblk itself and if we are popped due to an
9347 	 * an unplumb before the response comes back from tunnel,
9348 	 * the streamhead (which does a freemsg) will see this contaminated
9349 	 * message and the assertion in freemsg about non-null b_next/b_prev
9350 	 * will panic a DEBUG kernel.
9351 	 */
9352 	mp1 = copymsg(mp);
9353 	if (mp1 == NULL)
9354 		return (ENOMEM);
9355 
9356 	ill = ipif->ipif_ill;
9357 	mutex_enter(&connp->conn_lock);
9358 	mutex_enter(&ill->ill_lock);
9359 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9360 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9361 		    mp, 0);
9362 	} else {
9363 		success = ill_pending_mp_add(ill, connp, mp);
9364 	}
9365 	mutex_exit(&ill->ill_lock);
9366 	mutex_exit(&connp->conn_lock);
9367 
9368 	if (success) {
9369 		ip1dbg(("sending down tunparam request "));
9370 		putnext(ill->ill_wq, mp1);
9371 		return (EINPROGRESS);
9372 	} else {
9373 		/* The conn has started closing */
9374 		freemsg(mp1);
9375 		return (EINTR);
9376 	}
9377 }
9378 
9379 /*
9380  * ARP IOCTLs.
9381  * How does IP get in the business of fronting ARP configuration/queries?
9382  * Well it's like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9383  * are by tradition passed in through a datagram socket.  That lands in IP.
9384  * As it happens, this is just as well since the interface is quite crude in
9385  * that it passes in no information about protocol or hardware types, or
9386  * interface association.  After making the protocol assumption, IP is in
9387  * the position to look up the name of the ILL, which ARP will need, and
9388  * format a request that can be handled by ARP.  The request is passed up
9389  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9390  * back a response.  ARP supports its own set of more general IOCTLs, in
9391  * case anyone is interested.
9392  */
9393 /* ARGSUSED */
9394 int
9395 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9396     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9397 {
9398 	mblk_t *mp1;
9399 	mblk_t *mp2;
9400 	mblk_t *pending_mp;
9401 	ipaddr_t ipaddr;
9402 	area_t *area;
9403 	struct iocblk *iocp;
9404 	conn_t *connp;
9405 	struct arpreq *ar;
9406 	struct xarpreq *xar;
9407 	int flags, alength;
9408 	uchar_t *lladdr;
9409 	ire_t *ire;
9410 	ip_stack_t *ipst;
9411 	ill_t *ill = ipif->ipif_ill;
9412 	ill_t *proxy_ill = NULL;
9413 	ipmp_arpent_t *entp = NULL;
9414 	boolean_t if_arp_ioctl = B_FALSE;
9415 	boolean_t proxyarp = B_FALSE;
9416 
9417 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9418 	connp = Q_TO_CONN(q);
9419 	ipst = connp->conn_netstack->netstack_ip;
9420 
9421 	if (ipip->ipi_cmd_type == XARP_CMD) {
9422 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9423 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9424 		ar = NULL;
9425 
9426 		flags = xar->xarp_flags;
9427 		lladdr = (uchar_t *)LLADDR(&xar->xarp_ha);
9428 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
9429 		/*
9430 		 * Validate against user's link layer address length
9431 		 * input and name and addr length limits.
9432 		 */
9433 		alength = ill->ill_phys_addr_length;
9434 		if (ipip->ipi_cmd == SIOCSXARP) {
9435 			if (alength != xar->xarp_ha.sdl_alen ||
9436 			    (alength + xar->xarp_ha.sdl_nlen >
9437 			    sizeof (xar->xarp_ha.sdl_data)))
9438 				return (EINVAL);
9439 		}
9440 	} else {
9441 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9442 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9443 		xar = NULL;
9444 
9445 		flags = ar->arp_flags;
9446 		lladdr = (uchar_t *)ar->arp_ha.sa_data;
9447 		/*
9448 		 * Theoretically, the sa_family could tell us what link
9449 		 * layer type this operation is trying to deal with. By
9450 		 * common usage AF_UNSPEC means ethernet. We'll assume
9451 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9452 		 * for now. Our new SIOC*XARP ioctls can be used more
9453 		 * generally.
9454 		 *
9455 		 * If the underlying media happens to have a non 6 byte
9456 		 * address, arp module will fail set/get, but the del
9457 		 * operation will succeed.
9458 		 */
9459 		alength = 6;
9460 		if ((ipip->ipi_cmd != SIOCDARP) &&
9461 		    (alength != ill->ill_phys_addr_length)) {
9462 			return (EINVAL);
9463 		}
9464 	}
9465 
9466 	ipaddr = sin->sin_addr.s_addr;
9467 
9468 	/*
9469 	 * IPMP ARP special handling:
9470 	 *
9471 	 * 1. Since ARP mappings must appear consistent across the group,
9472 	 *    prohibit changing ARP mappings on the underlying interfaces.
9473 	 *
9474 	 * 2. Since ARP mappings for IPMP data addresses are maintained by
9475 	 *    IP itself, prohibit changing them.
9476 	 *
9477 	 * 3. For proxy ARP, use a functioning hardware address in the group,
9478 	 *    provided one exists.  If one doesn't, just add the entry as-is;
9479 	 *    ipmp_illgrp_refresh_arpent() will refresh it if things change.
9480 	 */
9481 	if (IS_UNDER_IPMP(ill)) {
9482 		if (ipip->ipi_cmd != SIOCGARP && ipip->ipi_cmd != SIOCGXARP)
9483 			return (EPERM);
9484 	}
9485 	if (IS_IPMP(ill)) {
9486 		ipmp_illgrp_t *illg = ill->ill_grp;
9487 
9488 		switch (ipip->ipi_cmd) {
9489 		case SIOCSARP:
9490 		case SIOCSXARP:
9491 			proxy_ill = ipmp_illgrp_find_ill(illg, lladdr, alength);
9492 			if (proxy_ill != NULL) {
9493 				proxyarp = B_TRUE;
9494 				if (!ipmp_ill_is_active(proxy_ill))
9495 					proxy_ill = ipmp_illgrp_next_ill(illg);
9496 				if (proxy_ill != NULL)
9497 					lladdr = proxy_ill->ill_phys_addr;
9498 			}
9499 			/* FALLTHRU */
9500 		case SIOCDARP:
9501 		case SIOCDXARP:
9502 			ire = ire_ctable_lookup(ipaddr, 0, IRE_LOCAL, NULL,
9503 			    ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
9504 			if (ire != NULL) {
9505 				ire_refrele(ire);
9506 				return (EPERM);
9507 			}
9508 		}
9509 	}
9510 
9511 	/*
9512 	 * We are going to pass up to ARP a packet chain that looks
9513 	 * like:
9514 	 *
9515 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9516 	 *
9517 	 * Get a copy of the original IOCTL mblk to head the chain,
9518 	 * to be sent up (in mp1). Also get another copy to store
9519 	 * in the ill_pending_mp list, for matching the response
9520 	 * when it comes back from ARP.
9521 	 */
9522 	mp1 = copyb(mp);
9523 	pending_mp = copymsg(mp);
9524 	if (mp1 == NULL || pending_mp == NULL) {
9525 		if (mp1 != NULL)
9526 			freeb(mp1);
9527 		if (pending_mp != NULL)
9528 			inet_freemsg(pending_mp);
9529 		return (ENOMEM);
9530 	}
9531 
9532 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9533 	    (caddr_t)&ipaddr);
9534 	if (mp2 == NULL) {
9535 		freeb(mp1);
9536 		inet_freemsg(pending_mp);
9537 		return (ENOMEM);
9538 	}
9539 	/* Put together the chain. */
9540 	mp1->b_cont = mp2;
9541 	mp1->b_datap->db_type = M_IOCTL;
9542 	mp2->b_cont = mp;
9543 	mp2->b_datap->db_type = M_DATA;
9544 
9545 	iocp = (struct iocblk *)mp1->b_rptr;
9546 
9547 	/*
9548 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9549 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9550 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9551 	 * ioc_count field; set ioc_count to be correct.
9552 	 */
9553 	iocp->ioc_count = MBLKL(mp1->b_cont);
9554 
9555 	/*
9556 	 * Set the proper command in the ARP message.
9557 	 * Convert the SIOC{G|S|D}ARP calls into our
9558 	 * AR_ENTRY_xxx calls.
9559 	 */
9560 	area = (area_t *)mp2->b_rptr;
9561 	switch (iocp->ioc_cmd) {
9562 	case SIOCDARP:
9563 	case SIOCDXARP:
9564 		/*
9565 		 * We defer deleting the corresponding IRE until
9566 		 * we return from arp.
9567 		 */
9568 		area->area_cmd = AR_ENTRY_DELETE;
9569 		area->area_proto_mask_offset = 0;
9570 		break;
9571 	case SIOCGARP:
9572 	case SIOCGXARP:
9573 		area->area_cmd = AR_ENTRY_SQUERY;
9574 		area->area_proto_mask_offset = 0;
9575 		break;
9576 	case SIOCSARP:
9577 	case SIOCSXARP:
9578 		/*
9579 		 * Delete the corresponding ire to make sure IP will
9580 		 * pick up any change from arp.
9581 		 */
9582 		if (!if_arp_ioctl) {
9583 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9584 		} else {
9585 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9586 			if (ipif != NULL) {
9587 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9588 				    ipst);
9589 				ipif_refrele(ipif);
9590 			}
9591 		}
9592 		break;
9593 	}
9594 	iocp->ioc_cmd = area->area_cmd;
9595 
9596 	/*
9597 	 * Fill in the rest of the ARP operation fields.
9598 	 */
9599 	area->area_hw_addr_length = alength;
9600 	bcopy(lladdr, (char *)area + area->area_hw_addr_offset, alength);
9601 
9602 	/* Translate the flags. */
9603 	if (flags & ATF_PERM)
9604 		area->area_flags |= ACE_F_PERMANENT;
9605 	if (flags & ATF_PUBL)
9606 		area->area_flags |= ACE_F_PUBLISH;
9607 	if (flags & ATF_AUTHORITY)
9608 		area->area_flags |= ACE_F_AUTHORITY;
9609 
9610 	/*
9611 	 * If this is a permanent AR_ENTRY_ADD on the IPMP interface, track it
9612 	 * so that IP can update ARP as the active ills in the group change.
9613 	 */
9614 	if (IS_IPMP(ill) && area->area_cmd == AR_ENTRY_ADD &&
9615 	    (area->area_flags & ACE_F_PERMANENT)) {
9616 		entp = ipmp_illgrp_create_arpent(ill->ill_grp, mp2, proxyarp);
9617 
9618 		/*
9619 		 * The second part of the conditional below handles a corner
9620 		 * case: if this is proxy ARP and the IPMP group has no active
9621 		 * interfaces, we can't send the request to ARP now since it
9622 		 * won't be able to build an ACE.  So we return success and
9623 		 * notify ARP about the proxy ARP entry once an interface
9624 		 * becomes active.
9625 		 */
9626 		if (entp == NULL || (proxyarp && proxy_ill == NULL)) {
9627 			mp2->b_cont = NULL;
9628 			inet_freemsg(mp1);
9629 			inet_freemsg(pending_mp);
9630 			return (entp == NULL ? ENOMEM : 0);
9631 		}
9632 	}
9633 
9634 	/*
9635 	 * Before sending 'mp' to ARP, we have to clear the b_next
9636 	 * and b_prev. Otherwise if STREAMS encounters such a message
9637 	 * in freemsg(), (because ARP can close any time) it can cause
9638 	 * a panic. But mi code needs the b_next and b_prev values of
9639 	 * mp->b_cont, to complete the ioctl. So we store it here
9640 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9641 	 * when the response comes down from ARP.
9642 	 */
9643 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9644 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9645 	mp->b_cont->b_next = NULL;
9646 	mp->b_cont->b_prev = NULL;
9647 
9648 	mutex_enter(&connp->conn_lock);
9649 	mutex_enter(&ill->ill_lock);
9650 	/* conn has not yet started closing, hence this can't fail */
9651 	if (ipip->ipi_flags & IPI_WR) {
9652 		VERIFY(ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9653 		    pending_mp, 0) != 0);
9654 	} else {
9655 		VERIFY(ill_pending_mp_add(ill, connp, pending_mp) != 0);
9656 	}
9657 	mutex_exit(&ill->ill_lock);
9658 	mutex_exit(&connp->conn_lock);
9659 
9660 	/*
9661 	 * Up to ARP it goes.  The response will come back in ip_wput() as an
9662 	 * M_IOCACK, and will be handed to ip_sioctl_iocack() for completion.
9663 	 */
9664 	putnext(ill->ill_rq, mp1);
9665 
9666 	/*
9667 	 * If we created an IPMP ARP entry, mark that we've notified ARP.
9668 	 */
9669 	if (entp != NULL)
9670 		ipmp_illgrp_mark_arpent(ill->ill_grp, entp);
9671 
9672 	return (EINPROGRESS);
9673 }
9674 
9675 /*
9676  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
9677  * the associated sin and refhold and return the associated ipif via `ci'.
9678  */
9679 int
9680 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
9681     cmd_info_t *ci, ipsq_func_t func)
9682 {
9683 	mblk_t	*mp1;
9684 	int	err;
9685 	sin_t	*sin;
9686 	conn_t	*connp;
9687 	ipif_t	*ipif;
9688 	ire_t	*ire = NULL;
9689 	ill_t	*ill = NULL;
9690 	boolean_t exists;
9691 	ip_stack_t *ipst;
9692 	struct arpreq *ar;
9693 	struct xarpreq *xar;
9694 	struct sockaddr_dl *sdl;
9695 
9696 	/* ioctl comes down on a conn */
9697 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9698 	connp = Q_TO_CONN(q);
9699 	if (connp->conn_af_isv6)
9700 		return (ENXIO);
9701 
9702 	ipst = connp->conn_netstack->netstack_ip;
9703 
9704 	/* Verified in ip_wput_nondata */
9705 	mp1 = mp->b_cont->b_cont;
9706 
9707 	if (ipip->ipi_cmd_type == XARP_CMD) {
9708 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
9709 		xar = (struct xarpreq *)mp1->b_rptr;
9710 		sin = (sin_t *)&xar->xarp_pa;
9711 		sdl = &xar->xarp_ha;
9712 
9713 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
9714 			return (ENXIO);
9715 		if (sdl->sdl_nlen >= LIFNAMSIZ)
9716 			return (EINVAL);
9717 	} else {
9718 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
9719 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
9720 		ar = (struct arpreq *)mp1->b_rptr;
9721 		sin = (sin_t *)&ar->arp_pa;
9722 	}
9723 
9724 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
9725 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
9726 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, CONNP_TO_WQ(connp),
9727 		    mp, func, &err, ipst);
9728 		if (ipif == NULL)
9729 			return (err);
9730 		if (ipif->ipif_id != 0) {
9731 			ipif_refrele(ipif);
9732 			return (ENXIO);
9733 		}
9734 	} else {
9735 		/*
9736 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with an sdl_nlen
9737 		 * of 0: use the IP address to find the ipif.  If the IP
9738 		 * address is an IPMP test address, ire_ftable_lookup() will
9739 		 * find the wrong ill, so we first do an ipif_lookup_addr().
9740 		 */
9741 		ipif = ipif_lookup_addr(sin->sin_addr.s_addr, NULL, ALL_ZONES,
9742 		    CONNP_TO_WQ(connp), mp, func, &err, ipst);
9743 		if (ipif == NULL) {
9744 			ire = ire_ftable_lookup(sin->sin_addr.s_addr, 0, 0,
9745 			    IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0, NULL,
9746 			    MATCH_IRE_TYPE, ipst);
9747 			if (ire == NULL || ((ill = ire_to_ill(ire)) == NULL)) {
9748 				if (ire != NULL)
9749 					ire_refrele(ire);
9750 				return (ENXIO);
9751 			}
9752 			ipif = ill->ill_ipif;
9753 			ipif_refhold(ipif);
9754 			ire_refrele(ire);
9755 		}
9756 	}
9757 
9758 	if (ipif->ipif_net_type != IRE_IF_RESOLVER) {
9759 		ipif_refrele(ipif);
9760 		return (ENXIO);
9761 	}
9762 
9763 	ci->ci_sin = sin;
9764 	ci->ci_ipif = ipif;
9765 	return (0);
9766 }
9767 
9768 /*
9769  * Link or unlink the illgrp on IPMP meta-interface `ill' depending on the
9770  * value of `ioccmd'.  While an illgrp is linked to an ipmp_grp_t, it is
9771  * accessible from that ipmp_grp_t, which means SIOCSLIFGROUPNAME can look it
9772  * up and thus an ill can join that illgrp.
9773  *
9774  * We use I_PLINK/I_PUNLINK to do the link/unlink operations rather than
9775  * open()/close() primarily because close() is not allowed to fail or block
9776  * forever.  On the other hand, I_PUNLINK *can* fail, and there's no reason
9777  * why anyone should ever need to I_PUNLINK an in-use IPMP stream.  To ensure
9778  * symmetric behavior (e.g., doing an I_PLINK after and I_PUNLINK undoes the
9779  * I_PUNLINK) we defer linking to I_PLINK.  Separately, we also fail attempts
9780  * to I_LINK since I_UNLINK is optional and we'd end up in an inconsistent
9781  * state if I_UNLINK didn't occur.
9782  *
9783  * Note that for each plumb/unplumb operation, we may end up here more than
9784  * once because of the way ifconfig works.  However, it's OK to link the same
9785  * illgrp more than once, or unlink an illgrp that's already unlinked.
9786  */
9787 static int
9788 ip_sioctl_plink_ipmp(ill_t *ill, int ioccmd)
9789 {
9790 	int err;
9791 	ip_stack_t *ipst = ill->ill_ipst;
9792 
9793 	ASSERT(IS_IPMP(ill));
9794 	ASSERT(IAM_WRITER_ILL(ill));
9795 
9796 	switch (ioccmd) {
9797 	case I_LINK:
9798 		return (ENOTSUP);
9799 
9800 	case I_PLINK:
9801 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
9802 		ipmp_illgrp_link_grp(ill->ill_grp, ill->ill_phyint->phyint_grp);
9803 		rw_exit(&ipst->ips_ipmp_lock);
9804 		break;
9805 
9806 	case I_PUNLINK:
9807 		/*
9808 		 * Require all UP ipifs be brought down prior to unlinking the
9809 		 * illgrp so any associated IREs (and other state) is torched.
9810 		 */
9811 		if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0)
9812 			return (EBUSY);
9813 
9814 		/*
9815 		 * NOTE: We hold ipmp_lock across the unlink to prevent a race
9816 		 * with an SIOCSLIFGROUPNAME request from an ill trying to
9817 		 * join this group.  Specifically: ills trying to join grab
9818 		 * ipmp_lock and bump a "pending join" counter checked by
9819 		 * ipmp_illgrp_unlink_grp().  During the unlink no new pending
9820 		 * joins can occur (since we have ipmp_lock).  Once we drop
9821 		 * ipmp_lock, subsequent SIOCSLIFGROUPNAME requests will not
9822 		 * find the illgrp (since we unlinked it) and will return
9823 		 * EAFNOSUPPORT.  This will then take them back through the
9824 		 * IPMP meta-interface plumbing logic in ifconfig, and thus
9825 		 * back through I_PLINK above.
9826 		 */
9827 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
9828 		err = ipmp_illgrp_unlink_grp(ill->ill_grp);
9829 		rw_exit(&ipst->ips_ipmp_lock);
9830 		return (err);
9831 	default:
9832 		break;
9833 	}
9834 	return (0);
9835 }
9836 
9837 /*
9838  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9839  * atomically set/clear the muxids. Also complete the ioctl by acking or
9840  * naking it.  Note that the code is structured such that the link type,
9841  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9842  * its clones use the persistent link, while pppd(1M) and perhaps many
9843  * other daemons may use non-persistent link.  When combined with some
9844  * ill_t states, linking and unlinking lower streams may be used as
9845  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9846  */
9847 /* ARGSUSED */
9848 void
9849 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9850 {
9851 	mblk_t		*mp1, *mp2;
9852 	struct linkblk	*li;
9853 	struct ipmx_s	*ipmxp;
9854 	ill_t		*ill;
9855 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
9856 	int		err = 0;
9857 	boolean_t	entered_ipsq = B_FALSE;
9858 	boolean_t	islink;
9859 	ip_stack_t	*ipst;
9860 
9861 	if (CONN_Q(q))
9862 		ipst = CONNQ_TO_IPST(q);
9863 	else
9864 		ipst = ILLQ_TO_IPST(q);
9865 
9866 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
9867 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
9868 
9869 	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9870 
9871 	mp1 = mp->b_cont;	/* This is the linkblk info */
9872 	li = (struct linkblk *)mp1->b_rptr;
9873 
9874 	/*
9875 	 * ARP has added this special mblk, and the utility is asking us
9876 	 * to perform consistency checks, and also atomically set the
9877 	 * muxid. Ifconfig is an example.  It achieves this by using
9878 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9879 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9880 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9881 	 * and other comments in this routine for more details.
9882 	 */
9883 	mp2 = mp1->b_cont;	/* This is added by ARP */
9884 
9885 	/*
9886 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9887 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9888 	 * get the special mblk above.  For backward compatibility, we
9889 	 * request ip_sioctl_plink_ipmod() to skip the consistency checks.
9890 	 * The utility will use SIOCSLIFMUXID to store the muxids.  This is
9891 	 * not atomic, and can leave the streams unplumbable if the utility
9892 	 * is interrupted before it does the SIOCSLIFMUXID.
9893 	 */
9894 	if (mp2 == NULL) {
9895 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_FALSE);
9896 		if (err == EINPROGRESS)
9897 			return;
9898 		goto done;
9899 	}
9900 
9901 	/*
9902 	 * This is an I_{P}LINK sent down by ifconfig through the ARP module;
9903 	 * ARP has appended this last mblk to tell us whether the lower stream
9904 	 * is an arp-dev stream or an IP module stream.
9905 	 */
9906 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9907 	if (ipmxp->ipmx_arpdev_stream) {
9908 		/*
9909 		 * The lower stream is the arp-dev stream.
9910 		 */
9911 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9912 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
9913 		if (ill == NULL) {
9914 			if (err == EINPROGRESS)
9915 				return;
9916 			err = EINVAL;
9917 			goto done;
9918 		}
9919 
9920 		if (ipsq == NULL) {
9921 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9922 			    NEW_OP, B_FALSE);
9923 			if (ipsq == NULL) {
9924 				ill_refrele(ill);
9925 				return;
9926 			}
9927 			entered_ipsq = B_TRUE;
9928 		}
9929 		ASSERT(IAM_WRITER_ILL(ill));
9930 		ill_refrele(ill);
9931 
9932 		/*
9933 		 * To ensure consistency between IP and ARP, the following
9934 		 * LIFO scheme is used in plink/punlink. (IP first, ARP last).
9935 		 * This is because the muxid's are stored in the IP stream on
9936 		 * the ill.
9937 		 *
9938 		 * I_{P}LINK: ifconfig plinks the IP stream before plinking
9939 		 * the ARP stream. On an arp-dev stream, IP checks that it is
9940 		 * not yet plinked, and it also checks that the corresponding
9941 		 * IP stream is already plinked.
9942 		 *
9943 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream before
9944 		 * punlinking the IP stream. IP does not allow punlink of the
9945 		 * IP stream unless the arp stream has been punlinked.
9946 		 */
9947 		if ((islink &&
9948 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9949 		    (!islink && ill->ill_arp_muxid != li->l_index)) {
9950 			err = EINVAL;
9951 			goto done;
9952 		}
9953 
9954 		if (IS_IPMP(ill) &&
9955 		    (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
9956 			goto done;
9957 
9958 		ill->ill_arp_muxid = islink ? li->l_index : 0;
9959 	} else {
9960 		/*
9961 		 * The lower stream is probably an IP module stream.  Do
9962 		 * consistency checking.
9963 		 */
9964 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_TRUE);
9965 		if (err == EINPROGRESS)
9966 			return;
9967 	}
9968 done:
9969 	if (err == 0)
9970 		miocack(q, mp, 0, 0);
9971 	else
9972 		miocnak(q, mp, 0, err);
9973 
9974 	/* Conn was refheld in ip_sioctl_copyin_setup */
9975 	if (CONN_Q(q))
9976 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9977 	if (entered_ipsq)
9978 		ipsq_exit(ipsq);
9979 }
9980 
9981 /*
9982  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
9983  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
9984  * module stream).  If `doconsist' is set, then do the extended consistency
9985  * checks requested by ifconfig(1M) and (atomically) set ill_ip_muxid here.
9986  * Returns zero on success, EINPROGRESS if the operation is still pending, or
9987  * an error code on failure.
9988  */
9989 static int
9990 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
9991     struct linkblk *li, boolean_t doconsist)
9992 {
9993 	int		err = 0;
9994 	ill_t  		*ill;
9995 	queue_t		*ipwq, *dwq;
9996 	const char	*name;
9997 	struct qinit	*qinfo;
9998 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9999 	boolean_t	entered_ipsq = B_FALSE;
10000 
10001 	/*
10002 	 * Walk the lower stream to verify it's the IP module stream.
10003 	 * The IP module is identified by its name, wput function,
10004 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
10005 	 * (li->l_qbot) will not vanish until this ioctl completes.
10006 	 */
10007 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
10008 		qinfo = ipwq->q_qinfo;
10009 		name = qinfo->qi_minfo->mi_idname;
10010 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
10011 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
10012 			break;
10013 		}
10014 	}
10015 
10016 	/*
10017 	 * If this isn't an IP module stream, bail.
10018 	 */
10019 	if (ipwq == NULL)
10020 		return (0);
10021 
10022 	ill = ipwq->q_ptr;
10023 	ASSERT(ill != NULL);
10024 
10025 	if (ipsq == NULL) {
10026 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
10027 		    NEW_OP, B_FALSE);
10028 		if (ipsq == NULL)
10029 			return (EINPROGRESS);
10030 		entered_ipsq = B_TRUE;
10031 	}
10032 	ASSERT(IAM_WRITER_ILL(ill));
10033 
10034 	if (doconsist) {
10035 		/*
10036 		 * Consistency checking requires that I_{P}LINK occurs
10037 		 * prior to setting ill_ip_muxid, and that I_{P}UNLINK
10038 		 * occurs prior to clearing ill_arp_muxid.
10039 		 */
10040 		if ((islink && ill->ill_ip_muxid != 0) ||
10041 		    (!islink && ill->ill_arp_muxid != 0)) {
10042 			err = EINVAL;
10043 			goto done;
10044 		}
10045 	}
10046 
10047 	if (IS_IPMP(ill) && (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
10048 		goto done;
10049 
10050 	/*
10051 	 * As part of I_{P}LINKing, stash the number of downstream modules and
10052 	 * the read queue of the module immediately below IP in the ill.
10053 	 * These are used during the capability negotiation below.
10054 	 */
10055 	ill->ill_lmod_rq = NULL;
10056 	ill->ill_lmod_cnt = 0;
10057 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
10058 		ill->ill_lmod_rq = RD(dwq);
10059 		for (; dwq != NULL; dwq = dwq->q_next)
10060 			ill->ill_lmod_cnt++;
10061 	}
10062 
10063 	if (doconsist)
10064 		ill->ill_ip_muxid = islink ? li->l_index : 0;
10065 
10066 	/*
10067 	 * Mark the ipsq busy until the capability operations initiated below
10068 	 * complete. The PLINK/UNLINK ioctl itself completes when our caller
10069 	 * returns, but the capability operation may complete asynchronously
10070 	 * much later.
10071 	 */
10072 	ipsq_current_start(ipsq, ill->ill_ipif, ioccmd);
10073 	/*
10074 	 * If there's at least one up ipif on this ill, then we're bound to
10075 	 * the underlying driver via DLPI.  In that case, renegotiate
10076 	 * capabilities to account for any possible change in modules
10077 	 * interposed between IP and the driver.
10078 	 */
10079 	if (ill->ill_ipif_up_count > 0) {
10080 		if (islink)
10081 			ill_capability_probe(ill);
10082 		else
10083 			ill_capability_reset(ill, B_FALSE);
10084 	}
10085 	ipsq_current_finish(ipsq);
10086 done:
10087 	if (entered_ipsq)
10088 		ipsq_exit(ipsq);
10089 
10090 	return (err);
10091 }
10092 
10093 /*
10094  * Search the ioctl command in the ioctl tables and return a pointer
10095  * to the ioctl command information. The ioctl command tables are
10096  * static and fully populated at compile time.
10097  */
10098 ip_ioctl_cmd_t *
10099 ip_sioctl_lookup(int ioc_cmd)
10100 {
10101 	int index;
10102 	ip_ioctl_cmd_t *ipip;
10103 	ip_ioctl_cmd_t *ipip_end;
10104 
10105 	if (ioc_cmd == IPI_DONTCARE)
10106 		return (NULL);
10107 
10108 	/*
10109 	 * Do a 2 step search. First search the indexed table
10110 	 * based on the least significant byte of the ioctl cmd.
10111 	 * If we don't find a match, then search the misc table
10112 	 * serially.
10113 	 */
10114 	index = ioc_cmd & 0xFF;
10115 	if (index < ip_ndx_ioctl_count) {
10116 		ipip = &ip_ndx_ioctl_table[index];
10117 		if (ipip->ipi_cmd == ioc_cmd) {
10118 			/* Found a match in the ndx table */
10119 			return (ipip);
10120 		}
10121 	}
10122 
10123 	/* Search the misc table */
10124 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10125 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10126 		if (ipip->ipi_cmd == ioc_cmd)
10127 			/* Found a match in the misc table */
10128 			return (ipip);
10129 	}
10130 
10131 	return (NULL);
10132 }
10133 
10134 /*
10135  * Wrapper function for resuming deferred ioctl processing
10136  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10137  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10138  */
10139 /* ARGSUSED */
10140 void
10141 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10142     void *dummy_arg)
10143 {
10144 	ip_sioctl_copyin_setup(q, mp);
10145 }
10146 
10147 /*
10148  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10149  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10150  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10151  * We establish here the size of the block to be copied in.  mi_copyin
10152  * arranges for this to happen, an processing continues in ip_wput with
10153  * an M_IOCDATA message.
10154  */
10155 void
10156 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10157 {
10158 	int	copyin_size;
10159 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10160 	ip_ioctl_cmd_t *ipip;
10161 	cred_t *cr;
10162 	ip_stack_t	*ipst;
10163 
10164 	if (CONN_Q(q))
10165 		ipst = CONNQ_TO_IPST(q);
10166 	else
10167 		ipst = ILLQ_TO_IPST(q);
10168 
10169 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10170 	if (ipip == NULL) {
10171 		/*
10172 		 * The ioctl is not one we understand or own.
10173 		 * Pass it along to be processed down stream,
10174 		 * if this is a module instance of IP, else nak
10175 		 * the ioctl.
10176 		 */
10177 		if (q->q_next == NULL) {
10178 			goto nak;
10179 		} else {
10180 			putnext(q, mp);
10181 			return;
10182 		}
10183 	}
10184 
10185 	/*
10186 	 * If this is deferred, then we will do all the checks when we
10187 	 * come back.
10188 	 */
10189 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10190 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10191 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10192 		return;
10193 	}
10194 
10195 	/*
10196 	 * Only allow a very small subset of IP ioctls on this stream if
10197 	 * IP is a module and not a driver. Allowing ioctls to be processed
10198 	 * in this case may cause assert failures or data corruption.
10199 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10200 	 * ioctls allowed on an IP module stream, after which this stream
10201 	 * normally becomes a multiplexor (at which time the stream head
10202 	 * will fail all ioctls).
10203 	 */
10204 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10205 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10206 			/*
10207 			 * Pass common Streams ioctls which the IP
10208 			 * module does not own or consume along to
10209 			 * be processed down stream.
10210 			 */
10211 			putnext(q, mp);
10212 			return;
10213 		} else {
10214 			goto nak;
10215 		}
10216 	}
10217 
10218 	/* Make sure we have ioctl data to process. */
10219 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10220 		goto nak;
10221 
10222 	/*
10223 	 * Prefer dblk credential over ioctl credential; some synthesized
10224 	 * ioctls have kcred set because there's no way to crhold()
10225 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10226 	 * the framework; the caller of ioctl needs to hold the reference
10227 	 * for the duration of the call).
10228 	 */
10229 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10230 
10231 	/* Make sure normal users don't send down privileged ioctls */
10232 	if ((ipip->ipi_flags & IPI_PRIV) &&
10233 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10234 		/* We checked the privilege earlier but log it here */
10235 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10236 		return;
10237 	}
10238 
10239 	/*
10240 	 * The ioctl command tables can only encode fixed length
10241 	 * ioctl data. If the length is variable, the table will
10242 	 * encode the length as zero. Such special cases are handled
10243 	 * below in the switch.
10244 	 */
10245 	if (ipip->ipi_copyin_size != 0) {
10246 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10247 		return;
10248 	}
10249 
10250 	switch (iocp->ioc_cmd) {
10251 	case O_SIOCGIFCONF:
10252 	case SIOCGIFCONF:
10253 		/*
10254 		 * This IOCTL is hilarious.  See comments in
10255 		 * ip_sioctl_get_ifconf for the story.
10256 		 */
10257 		if (iocp->ioc_count == TRANSPARENT)
10258 			copyin_size = SIZEOF_STRUCT(ifconf,
10259 			    iocp->ioc_flag);
10260 		else
10261 			copyin_size = iocp->ioc_count;
10262 		mi_copyin(q, mp, NULL, copyin_size);
10263 		return;
10264 
10265 	case O_SIOCGLIFCONF:
10266 	case SIOCGLIFCONF:
10267 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10268 		mi_copyin(q, mp, NULL, copyin_size);
10269 		return;
10270 
10271 	case SIOCGLIFSRCOF:
10272 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10273 		mi_copyin(q, mp, NULL, copyin_size);
10274 		return;
10275 	case SIOCGIP6ADDRPOLICY:
10276 		ip_sioctl_ip6addrpolicy(q, mp);
10277 		ip6_asp_table_refrele(ipst);
10278 		return;
10279 
10280 	case SIOCSIP6ADDRPOLICY:
10281 		ip_sioctl_ip6addrpolicy(q, mp);
10282 		return;
10283 
10284 	case SIOCGDSTINFO:
10285 		ip_sioctl_dstinfo(q, mp);
10286 		ip6_asp_table_refrele(ipst);
10287 		return;
10288 
10289 	case I_PLINK:
10290 	case I_PUNLINK:
10291 	case I_LINK:
10292 	case I_UNLINK:
10293 		/*
10294 		 * We treat non-persistent link similarly as the persistent
10295 		 * link case, in terms of plumbing/unplumbing, as well as
10296 		 * dynamic re-plumbing events indicator.  See comments
10297 		 * in ip_sioctl_plink() for more.
10298 		 *
10299 		 * Request can be enqueued in the 'ipsq' while waiting
10300 		 * to become exclusive. So bump up the conn ref.
10301 		 */
10302 		if (CONN_Q(q))
10303 			CONN_INC_REF(Q_TO_CONN(q));
10304 		ip_sioctl_plink(NULL, q, mp, NULL);
10305 		return;
10306 
10307 	case ND_GET:
10308 	case ND_SET:
10309 		/*
10310 		 * Use of the nd table requires holding the reader lock.
10311 		 * Modifying the nd table thru nd_load/nd_unload requires
10312 		 * the writer lock.
10313 		 */
10314 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10315 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10316 			rw_exit(&ipst->ips_ip_g_nd_lock);
10317 
10318 			if (iocp->ioc_error)
10319 				iocp->ioc_count = 0;
10320 			mp->b_datap->db_type = M_IOCACK;
10321 			qreply(q, mp);
10322 			return;
10323 		}
10324 		rw_exit(&ipst->ips_ip_g_nd_lock);
10325 		/*
10326 		 * We don't understand this subioctl of ND_GET / ND_SET.
10327 		 * Maybe intended for some driver / module below us
10328 		 */
10329 		if (q->q_next) {
10330 			putnext(q, mp);
10331 		} else {
10332 			iocp->ioc_error = ENOENT;
10333 			mp->b_datap->db_type = M_IOCNAK;
10334 			iocp->ioc_count = 0;
10335 			qreply(q, mp);
10336 		}
10337 		return;
10338 
10339 	case IP_IOCTL:
10340 		ip_wput_ioctl(q, mp);
10341 		return;
10342 	default:
10343 		cmn_err(CE_PANIC, "should not happen ");
10344 	}
10345 nak:
10346 	if (mp->b_cont != NULL) {
10347 		freemsg(mp->b_cont);
10348 		mp->b_cont = NULL;
10349 	}
10350 	iocp->ioc_error = EINVAL;
10351 	mp->b_datap->db_type = M_IOCNAK;
10352 	iocp->ioc_count = 0;
10353 	qreply(q, mp);
10354 }
10355 
10356 /* ip_wput hands off ARP IOCTL responses to us */
10357 /* ARGSUSED3 */
10358 void
10359 ip_sioctl_iocack(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
10360 {
10361 	struct arpreq *ar;
10362 	struct xarpreq *xar;
10363 	area_t	*area;
10364 	mblk_t	*area_mp;
10365 	struct iocblk *iocp;
10366 	mblk_t	*orig_ioc_mp, *tmp;
10367 	struct iocblk	*orig_iocp;
10368 	ill_t *ill;
10369 	conn_t *connp = NULL;
10370 	mblk_t *pending_mp;
10371 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10372 	int *flagsp;
10373 	char *storage = NULL;
10374 	sin_t *sin;
10375 	ipaddr_t addr;
10376 	int err;
10377 	ip_stack_t *ipst;
10378 
10379 	ASSERT(ipsq == NULL || IAM_WRITER_IPSQ(ipsq));
10380 	ill = q->q_ptr;
10381 	ASSERT(ill != NULL);
10382 	ipst = ill->ill_ipst;
10383 
10384 	/*
10385 	 * We should get back from ARP a packet chain that looks like:
10386 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10387 	 */
10388 	if (!(area_mp = mp->b_cont) ||
10389 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10390 	    !(orig_ioc_mp = area_mp->b_cont) ||
10391 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10392 		freemsg(mp);
10393 		return;
10394 	}
10395 
10396 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10397 
10398 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10399 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10400 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10401 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10402 		x_arp_ioctl = B_TRUE;
10403 		xar = (struct xarpreq *)tmp->b_rptr;
10404 		sin = (sin_t *)&xar->xarp_pa;
10405 		flagsp = &xar->xarp_flags;
10406 		storage = xar->xarp_ha.sdl_data;
10407 		if (xar->xarp_ha.sdl_nlen != 0)
10408 			ifx_arp_ioctl = B_TRUE;
10409 	} else {
10410 		ar = (struct arpreq *)tmp->b_rptr;
10411 		sin = (sin_t *)&ar->arp_pa;
10412 		flagsp = &ar->arp_flags;
10413 		storage = ar->arp_ha.sa_data;
10414 	}
10415 
10416 	iocp = (struct iocblk *)mp->b_rptr;
10417 
10418 	/*
10419 	 * Find the pending message; if we're exclusive, it'll be on our IPSQ.
10420 	 * Otherwise, we can find it from our ioc_id.
10421 	 */
10422 	if (ipsq != NULL)
10423 		pending_mp = ipsq_pending_mp_get(ipsq, &connp);
10424 	else
10425 		pending_mp = ill_pending_mp_get(ill, &connp, iocp->ioc_id);
10426 
10427 	if (pending_mp == NULL) {
10428 		ASSERT(connp == NULL);
10429 		inet_freemsg(mp);
10430 		return;
10431 	}
10432 	ASSERT(connp != NULL);
10433 	q = CONNP_TO_WQ(connp);
10434 
10435 	/* Uncouple the internally generated IOCTL from the original one */
10436 	area = (area_t *)area_mp->b_rptr;
10437 	area_mp->b_cont = NULL;
10438 
10439 	/*
10440 	 * Restore the b_next and b_prev used by mi code. This is needed
10441 	 * to complete the ioctl using mi* functions. We stored them in
10442 	 * the pending mp prior to sending the request to ARP.
10443 	 */
10444 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10445 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10446 	inet_freemsg(pending_mp);
10447 
10448 	/*
10449 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10450 	 * Catch the case where there is an IRE_CACHE by no entry in the
10451 	 * arp table.
10452 	 */
10453 	addr = sin->sin_addr.s_addr;
10454 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10455 		ire_t			*ire;
10456 		dl_unitdata_req_t	*dlup;
10457 		mblk_t			*llmp;
10458 		int			addr_len;
10459 		ill_t			*ipsqill = NULL;
10460 
10461 		if (ifx_arp_ioctl) {
10462 			/*
10463 			 * There's no need to lookup the ill, since
10464 			 * we've already done that when we started
10465 			 * processing the ioctl and sent the message
10466 			 * to ARP on that ill.  So use the ill that
10467 			 * is stored in q->q_ptr.
10468 			 */
10469 			ipsqill = ill;
10470 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10471 			    ipsqill->ill_ipif, ALL_ZONES,
10472 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10473 		} else {
10474 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10475 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10476 			if (ire != NULL)
10477 				ipsqill = ire_to_ill(ire);
10478 		}
10479 
10480 		if ((x_arp_ioctl) && (ipsqill != NULL))
10481 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10482 
10483 		if (ire != NULL) {
10484 			/*
10485 			 * Since the ire obtained from cachetable is used for
10486 			 * mac addr copying below, treat an incomplete ire as if
10487 			 * as if we never found it.
10488 			 */
10489 			if (ire->ire_nce != NULL &&
10490 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10491 				ire_refrele(ire);
10492 				ire = NULL;
10493 				ipsqill = NULL;
10494 				goto errack;
10495 			}
10496 			*flagsp = ATF_INUSE;
10497 			llmp = (ire->ire_nce != NULL ?
10498 			    ire->ire_nce->nce_res_mp : NULL);
10499 			if (llmp != NULL && ipsqill != NULL) {
10500 				uchar_t *macaddr;
10501 
10502 				addr_len = ipsqill->ill_phys_addr_length;
10503 				if (x_arp_ioctl && ((addr_len +
10504 				    ipsqill->ill_name_length) >
10505 				    sizeof (xar->xarp_ha.sdl_data))) {
10506 					ire_refrele(ire);
10507 					freemsg(mp);
10508 					ip_ioctl_finish(q, orig_ioc_mp,
10509 					    EINVAL, NO_COPYOUT, ipsq);
10510 					return;
10511 				}
10512 				*flagsp |= ATF_COM;
10513 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10514 				if (ipsqill->ill_sap_length < 0)
10515 					macaddr = llmp->b_rptr +
10516 					    dlup->dl_dest_addr_offset;
10517 				else
10518 					macaddr = llmp->b_rptr +
10519 					    dlup->dl_dest_addr_offset +
10520 					    ipsqill->ill_sap_length;
10521 				/*
10522 				 * For SIOCGARP, MAC address length
10523 				 * validation has already been done
10524 				 * before the ioctl was issued to ARP to
10525 				 * allow it to progress only on 6 byte
10526 				 * addressable (ethernet like) media. Thus
10527 				 * the mac address copying can not overwrite
10528 				 * the sa_data area below.
10529 				 */
10530 				bcopy(macaddr, storage, addr_len);
10531 			}
10532 			/* Ditch the internal IOCTL. */
10533 			freemsg(mp);
10534 			ire_refrele(ire);
10535 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, ipsq);
10536 			return;
10537 		}
10538 	}
10539 
10540 	/*
10541 	 * If this was a failed AR_ENTRY_ADD or a successful AR_ENTRY_DELETE
10542 	 * on the IPMP meta-interface, ensure any ARP entries added in
10543 	 * ip_sioctl_arp() are deleted.
10544 	 */
10545 	if (IS_IPMP(ill) &&
10546 	    ((iocp->ioc_error != 0 && iocp->ioc_cmd == AR_ENTRY_ADD) ||
10547 	    ((iocp->ioc_error == 0 && iocp->ioc_cmd == AR_ENTRY_DELETE)))) {
10548 		ipmp_illgrp_t *illg = ill->ill_grp;
10549 		ipmp_arpent_t *entp;
10550 
10551 		if ((entp = ipmp_illgrp_lookup_arpent(illg, &addr)) != NULL)
10552 			ipmp_illgrp_destroy_arpent(illg, entp);
10553 	}
10554 
10555 	/*
10556 	 * Delete the coresponding IRE_CACHE if any.
10557 	 * Reset the error if there was one (in case there was no entry
10558 	 * in arp.)
10559 	 */
10560 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10561 		ipif_t *ipintf = NULL;
10562 
10563 		if (ifx_arp_ioctl) {
10564 			/*
10565 			 * There's no need to lookup the ill, since
10566 			 * we've already done that when we started
10567 			 * processing the ioctl and sent the message
10568 			 * to ARP on that ill.  So use the ill that
10569 			 * is stored in q->q_ptr.
10570 			 */
10571 			ipintf = ill->ill_ipif;
10572 		}
10573 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10574 			/*
10575 			 * The address in "addr" may be an entry for a
10576 			 * router. If that's true, then any off-net
10577 			 * IRE_CACHE entries that go through the router
10578 			 * with address "addr" must be clobbered. Use
10579 			 * ire_walk to achieve this goal.
10580 			 */
10581 			if (ifx_arp_ioctl)
10582 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10583 				    ire_delete_cache_gw, (char *)&addr, ill);
10584 			else
10585 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10586 				    ALL_ZONES, ipst);
10587 			iocp->ioc_error = 0;
10588 		}
10589 	}
10590 errack:
10591 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10592 		err = iocp->ioc_error;
10593 		freemsg(mp);
10594 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, ipsq);
10595 		return;
10596 	}
10597 
10598 	/*
10599 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10600 	 * the area_t into the struct {x}arpreq.
10601 	 */
10602 	if (x_arp_ioctl) {
10603 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10604 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10605 		    sizeof (xar->xarp_ha.sdl_data)) {
10606 			freemsg(mp);
10607 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10608 			    ipsq);
10609 			return;
10610 		}
10611 	}
10612 	*flagsp = ATF_INUSE;
10613 	if (area->area_flags & ACE_F_PERMANENT)
10614 		*flagsp |= ATF_PERM;
10615 	if (area->area_flags & ACE_F_PUBLISH)
10616 		*flagsp |= ATF_PUBL;
10617 	if (area->area_flags & ACE_F_AUTHORITY)
10618 		*flagsp |= ATF_AUTHORITY;
10619 	if (area->area_hw_addr_length != 0) {
10620 		*flagsp |= ATF_COM;
10621 		/*
10622 		 * For SIOCGARP, MAC address length validation has
10623 		 * already been done before the ioctl was issued to ARP
10624 		 * to allow it to progress only on 6 byte addressable
10625 		 * (ethernet like) media. Thus the mac address copying
10626 		 * can not overwrite the sa_data area below.
10627 		 */
10628 		bcopy((char *)area + area->area_hw_addr_offset,
10629 		    storage, area->area_hw_addr_length);
10630 	}
10631 
10632 	/* Ditch the internal IOCTL. */
10633 	freemsg(mp);
10634 	/* Complete the original. */
10635 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, ipsq);
10636 }
10637 
10638 /*
10639  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10640  * interface) create the next available logical interface for this
10641  * physical interface.
10642  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10643  * ipif with the specified name.
10644  *
10645  * If the address family is not AF_UNSPEC then set the address as well.
10646  *
10647  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10648  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10649  *
10650  * Executed as a writer on the ill.
10651  * So no lock is needed to traverse the ipif chain, or examine the
10652  * phyint flags.
10653  */
10654 /* ARGSUSED */
10655 int
10656 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10657     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10658 {
10659 	mblk_t	*mp1;
10660 	struct lifreq *lifr;
10661 	boolean_t	isv6;
10662 	boolean_t	exists;
10663 	char 	*name;
10664 	char	*endp;
10665 	char	*cp;
10666 	int	namelen;
10667 	ipif_t	*ipif;
10668 	long	id;
10669 	ipsq_t	*ipsq;
10670 	ill_t	*ill;
10671 	sin_t	*sin;
10672 	int	err = 0;
10673 	boolean_t found_sep = B_FALSE;
10674 	conn_t	*connp;
10675 	zoneid_t zoneid;
10676 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10677 
10678 	ASSERT(q->q_next == NULL);
10679 	ip1dbg(("ip_sioctl_addif\n"));
10680 	/* Existence of mp1 has been checked in ip_wput_nondata */
10681 	mp1 = mp->b_cont->b_cont;
10682 	/*
10683 	 * Null terminate the string to protect against buffer
10684 	 * overrun. String was generated by user code and may not
10685 	 * be trusted.
10686 	 */
10687 	lifr = (struct lifreq *)mp1->b_rptr;
10688 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10689 	name = lifr->lifr_name;
10690 	ASSERT(CONN_Q(q));
10691 	connp = Q_TO_CONN(q);
10692 	isv6 = connp->conn_af_isv6;
10693 	zoneid = connp->conn_zoneid;
10694 	namelen = mi_strlen(name);
10695 	if (namelen == 0)
10696 		return (EINVAL);
10697 
10698 	exists = B_FALSE;
10699 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10700 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10701 		/*
10702 		 * Allow creating lo0 using SIOCLIFADDIF.
10703 		 * can't be any other writer thread. So can pass null below
10704 		 * for the last 4 args to ipif_lookup_name.
10705 		 */
10706 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10707 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10708 		/* Prevent any further action */
10709 		if (ipif == NULL) {
10710 			return (ENOBUFS);
10711 		} else if (!exists) {
10712 			/* We created the ipif now and as writer */
10713 			ipif_refrele(ipif);
10714 			return (0);
10715 		} else {
10716 			ill = ipif->ipif_ill;
10717 			ill_refhold(ill);
10718 			ipif_refrele(ipif);
10719 		}
10720 	} else {
10721 		/* Look for a colon in the name. */
10722 		endp = &name[namelen];
10723 		for (cp = endp; --cp > name; ) {
10724 			if (*cp == IPIF_SEPARATOR_CHAR) {
10725 				found_sep = B_TRUE;
10726 				/*
10727 				 * Reject any non-decimal aliases for plumbing
10728 				 * of logical interfaces. Aliases with leading
10729 				 * zeroes are also rejected as they introduce
10730 				 * ambiguity in the naming of the interfaces.
10731 				 * Comparing with "0" takes care of all such
10732 				 * cases.
10733 				 */
10734 				if ((strncmp("0", cp+1, 1)) == 0)
10735 					return (EINVAL);
10736 
10737 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10738 				    id <= 0 || *endp != '\0') {
10739 					return (EINVAL);
10740 				}
10741 				*cp = '\0';
10742 				break;
10743 			}
10744 		}
10745 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10746 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10747 		if (found_sep)
10748 			*cp = IPIF_SEPARATOR_CHAR;
10749 		if (ill == NULL)
10750 			return (err);
10751 	}
10752 
10753 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10754 	    B_TRUE);
10755 
10756 	/*
10757 	 * Release the refhold due to the lookup, now that we are excl
10758 	 * or we are just returning
10759 	 */
10760 	ill_refrele(ill);
10761 
10762 	if (ipsq == NULL)
10763 		return (EINPROGRESS);
10764 
10765 	/* We are now exclusive on the IPSQ */
10766 	ASSERT(IAM_WRITER_ILL(ill));
10767 
10768 	if (found_sep) {
10769 		/* Now see if there is an IPIF with this unit number. */
10770 		for (ipif = ill->ill_ipif; ipif != NULL;
10771 		    ipif = ipif->ipif_next) {
10772 			if (ipif->ipif_id == id) {
10773 				err = EEXIST;
10774 				goto done;
10775 			}
10776 		}
10777 	}
10778 
10779 	/*
10780 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10781 	 * of lo0.  Plumbing for lo0:0 happens in ipif_lookup_on_name()
10782 	 * instead.
10783 	 */
10784 	if ((ipif = ipif_allocate(ill, found_sep ? id : -1, IRE_LOCAL,
10785 	    B_TRUE, B_TRUE)) == NULL) {
10786 		err = ENOBUFS;
10787 		goto done;
10788 	}
10789 
10790 	/* Return created name with ioctl */
10791 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10792 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10793 	ip1dbg(("created %s\n", lifr->lifr_name));
10794 
10795 	/* Set address */
10796 	sin = (sin_t *)&lifr->lifr_addr;
10797 	if (sin->sin_family != AF_UNSPEC) {
10798 		err = ip_sioctl_addr(ipif, sin, q, mp,
10799 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10800 	}
10801 
10802 done:
10803 	ipsq_exit(ipsq);
10804 	return (err);
10805 }
10806 
10807 /*
10808  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10809  * interface) delete it based on the IP address (on this physical interface).
10810  * Otherwise delete it based on the ipif_id.
10811  * Also, special handling to allow a removeif of lo0.
10812  */
10813 /* ARGSUSED */
10814 int
10815 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10816     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10817 {
10818 	conn_t		*connp;
10819 	ill_t		*ill = ipif->ipif_ill;
10820 	boolean_t	 success;
10821 	ip_stack_t	*ipst;
10822 
10823 	ipst = CONNQ_TO_IPST(q);
10824 
10825 	ASSERT(q->q_next == NULL);
10826 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10827 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10828 	ASSERT(IAM_WRITER_IPIF(ipif));
10829 
10830 	connp = Q_TO_CONN(q);
10831 	/*
10832 	 * Special case for unplumbing lo0 (the loopback physical interface).
10833 	 * If unplumbing lo0, the incoming address structure has been
10834 	 * initialized to all zeros. When unplumbing lo0, all its logical
10835 	 * interfaces must be removed too.
10836 	 *
10837 	 * Note that this interface may be called to remove a specific
10838 	 * loopback logical interface (eg, lo0:1). But in that case
10839 	 * ipif->ipif_id != 0 so that the code path for that case is the
10840 	 * same as any other interface (meaning it skips the code directly
10841 	 * below).
10842 	 */
10843 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10844 		if (sin->sin_family == AF_UNSPEC &&
10845 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10846 			/*
10847 			 * Mark it condemned. No new ref. will be made to ill.
10848 			 */
10849 			mutex_enter(&ill->ill_lock);
10850 			ill->ill_state_flags |= ILL_CONDEMNED;
10851 			for (ipif = ill->ill_ipif; ipif != NULL;
10852 			    ipif = ipif->ipif_next) {
10853 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10854 			}
10855 			mutex_exit(&ill->ill_lock);
10856 
10857 			ipif = ill->ill_ipif;
10858 			/* unplumb the loopback interface */
10859 			ill_delete(ill);
10860 			mutex_enter(&connp->conn_lock);
10861 			mutex_enter(&ill->ill_lock);
10862 
10863 			/* Are any references to this ill active */
10864 			if (ill_is_freeable(ill)) {
10865 				mutex_exit(&ill->ill_lock);
10866 				mutex_exit(&connp->conn_lock);
10867 				ill_delete_tail(ill);
10868 				mi_free(ill);
10869 				return (0);
10870 			}
10871 			success = ipsq_pending_mp_add(connp, ipif,
10872 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10873 			mutex_exit(&connp->conn_lock);
10874 			mutex_exit(&ill->ill_lock);
10875 			if (success)
10876 				return (EINPROGRESS);
10877 			else
10878 				return (EINTR);
10879 		}
10880 	}
10881 
10882 	if (ipif->ipif_id == 0) {
10883 		ipsq_t *ipsq;
10884 
10885 		/* Find based on address */
10886 		if (ipif->ipif_isv6) {
10887 			sin6_t *sin6;
10888 
10889 			if (sin->sin_family != AF_INET6)
10890 				return (EAFNOSUPPORT);
10891 
10892 			sin6 = (sin6_t *)sin;
10893 			/* We are a writer, so we should be able to lookup */
10894 			ipif = ipif_lookup_addr_exact_v6(&sin6->sin6_addr, ill,
10895 			    ipst);
10896 		} else {
10897 			if (sin->sin_family != AF_INET)
10898 				return (EAFNOSUPPORT);
10899 
10900 			/* We are a writer, so we should be able to lookup */
10901 			ipif = ipif_lookup_addr_exact(sin->sin_addr.s_addr, ill,
10902 			    ipst);
10903 		}
10904 		if (ipif == NULL) {
10905 			return (EADDRNOTAVAIL);
10906 		}
10907 
10908 		/*
10909 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
10910 		 * lifr_name of the physical interface but with an ip address
10911 		 * lifr_addr of a logical interface plumbed over it.
10912 		 * So update ipx_current_ipif now that ipif points to the
10913 		 * correct one.
10914 		 */
10915 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
10916 		ipsq->ipsq_xop->ipx_current_ipif = ipif;
10917 
10918 		/* This is a writer */
10919 		ipif_refrele(ipif);
10920 	}
10921 
10922 	/*
10923 	 * Can not delete instance zero since it is tied to the ill.
10924 	 */
10925 	if (ipif->ipif_id == 0)
10926 		return (EBUSY);
10927 
10928 	mutex_enter(&ill->ill_lock);
10929 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10930 	mutex_exit(&ill->ill_lock);
10931 
10932 	ipif_free(ipif);
10933 
10934 	mutex_enter(&connp->conn_lock);
10935 	mutex_enter(&ill->ill_lock);
10936 
10937 
10938 	/* Are any references to this ipif active */
10939 	if (ipif_is_freeable(ipif)) {
10940 		mutex_exit(&ill->ill_lock);
10941 		mutex_exit(&connp->conn_lock);
10942 		ipif_non_duplicate(ipif);
10943 		ipif_down_tail(ipif);
10944 		ipif_free_tail(ipif); /* frees ipif */
10945 		return (0);
10946 	}
10947 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10948 	    IPIF_FREE);
10949 	mutex_exit(&ill->ill_lock);
10950 	mutex_exit(&connp->conn_lock);
10951 	if (success)
10952 		return (EINPROGRESS);
10953 	else
10954 		return (EINTR);
10955 }
10956 
10957 /*
10958  * Restart the removeif ioctl. The refcnt has gone down to 0.
10959  * The ipif is already condemned. So can't find it thru lookups.
10960  */
10961 /* ARGSUSED */
10962 int
10963 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
10964     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10965 {
10966 	ill_t *ill = ipif->ipif_ill;
10967 
10968 	ASSERT(IAM_WRITER_IPIF(ipif));
10969 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
10970 
10971 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
10972 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
10973 
10974 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10975 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
10976 		ill_delete_tail(ill);
10977 		mi_free(ill);
10978 		return (0);
10979 	}
10980 
10981 	ipif_non_duplicate(ipif);
10982 	ipif_down_tail(ipif);
10983 	ipif_free_tail(ipif);
10984 
10985 	ILL_UNMARK_CHANGING(ill);
10986 	return (0);
10987 }
10988 
10989 /*
10990  * Set the local interface address.
10991  * Allow an address of all zero when the interface is down.
10992  */
10993 /* ARGSUSED */
10994 int
10995 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10996     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10997 {
10998 	int err = 0;
10999 	in6_addr_t v6addr;
11000 	boolean_t need_up = B_FALSE;
11001 
11002 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
11003 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11004 
11005 	ASSERT(IAM_WRITER_IPIF(ipif));
11006 
11007 	if (ipif->ipif_isv6) {
11008 		sin6_t *sin6;
11009 		ill_t *ill;
11010 		phyint_t *phyi;
11011 
11012 		if (sin->sin_family != AF_INET6)
11013 			return (EAFNOSUPPORT);
11014 
11015 		sin6 = (sin6_t *)sin;
11016 		v6addr = sin6->sin6_addr;
11017 		ill = ipif->ipif_ill;
11018 		phyi = ill->ill_phyint;
11019 
11020 		/*
11021 		 * Enforce that true multicast interfaces have a link-local
11022 		 * address for logical unit 0.
11023 		 */
11024 		if (ipif->ipif_id == 0 &&
11025 		    (ill->ill_flags & ILLF_MULTICAST) &&
11026 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11027 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11028 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11029 			return (EADDRNOTAVAIL);
11030 		}
11031 
11032 		/*
11033 		 * up interfaces shouldn't have the unspecified address
11034 		 * unless they also have the IPIF_NOLOCAL flags set and
11035 		 * have a subnet assigned.
11036 		 */
11037 		if ((ipif->ipif_flags & IPIF_UP) &&
11038 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11039 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11040 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11041 			return (EADDRNOTAVAIL);
11042 		}
11043 
11044 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11045 			return (EADDRNOTAVAIL);
11046 	} else {
11047 		ipaddr_t addr;
11048 
11049 		if (sin->sin_family != AF_INET)
11050 			return (EAFNOSUPPORT);
11051 
11052 		addr = sin->sin_addr.s_addr;
11053 
11054 		/* Allow 0 as the local address. */
11055 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11056 			return (EADDRNOTAVAIL);
11057 
11058 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11059 	}
11060 
11061 	/*
11062 	 * Even if there is no change we redo things just to rerun
11063 	 * ipif_set_default.
11064 	 */
11065 	if (ipif->ipif_flags & IPIF_UP) {
11066 		/*
11067 		 * Setting a new local address, make sure
11068 		 * we have net and subnet bcast ire's for
11069 		 * the old address if we need them.
11070 		 */
11071 		if (!ipif->ipif_isv6)
11072 			ipif_check_bcast_ires(ipif);
11073 		/*
11074 		 * If the interface is already marked up,
11075 		 * we call ipif_down which will take care
11076 		 * of ditching any IREs that have been set
11077 		 * up based on the old interface address.
11078 		 */
11079 		err = ipif_logical_down(ipif, q, mp);
11080 		if (err == EINPROGRESS)
11081 			return (err);
11082 		ipif_down_tail(ipif);
11083 		need_up = 1;
11084 	}
11085 
11086 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11087 	return (err);
11088 }
11089 
11090 int
11091 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11092     boolean_t need_up)
11093 {
11094 	in6_addr_t v6addr;
11095 	in6_addr_t ov6addr;
11096 	ipaddr_t addr;
11097 	sin6_t	*sin6;
11098 	int	sinlen;
11099 	int	err = 0;
11100 	ill_t	*ill = ipif->ipif_ill;
11101 	boolean_t need_dl_down;
11102 	boolean_t need_arp_down;
11103 	struct iocblk *iocp;
11104 
11105 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11106 
11107 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11108 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11109 	ASSERT(IAM_WRITER_IPIF(ipif));
11110 
11111 	/* Must cancel any pending timer before taking the ill_lock */
11112 	if (ipif->ipif_recovery_id != 0)
11113 		(void) untimeout(ipif->ipif_recovery_id);
11114 	ipif->ipif_recovery_id = 0;
11115 
11116 	if (ipif->ipif_isv6) {
11117 		sin6 = (sin6_t *)sin;
11118 		v6addr = sin6->sin6_addr;
11119 		sinlen = sizeof (struct sockaddr_in6);
11120 	} else {
11121 		addr = sin->sin_addr.s_addr;
11122 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11123 		sinlen = sizeof (struct sockaddr_in);
11124 	}
11125 	mutex_enter(&ill->ill_lock);
11126 	ov6addr = ipif->ipif_v6lcl_addr;
11127 	ipif->ipif_v6lcl_addr = v6addr;
11128 	sctp_update_ipif_addr(ipif, ov6addr);
11129 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11130 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11131 	} else {
11132 		ipif->ipif_v6src_addr = v6addr;
11133 	}
11134 	ipif->ipif_addr_ready = 0;
11135 
11136 	/*
11137 	 * If the interface was previously marked as a duplicate, then since
11138 	 * we've now got a "new" address, it should no longer be considered a
11139 	 * duplicate -- even if the "new" address is the same as the old one.
11140 	 * Note that if all ipifs are down, we may have a pending ARP down
11141 	 * event to handle.  This is because we want to recover from duplicates
11142 	 * and thus delay tearing down ARP until the duplicates have been
11143 	 * removed or disabled.
11144 	 */
11145 	need_dl_down = need_arp_down = B_FALSE;
11146 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11147 		need_arp_down = !need_up;
11148 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11149 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11150 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11151 			need_dl_down = B_TRUE;
11152 		}
11153 	}
11154 
11155 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11156 	    !ill->ill_is_6to4tun) {
11157 		queue_t *wqp = ill->ill_wq;
11158 
11159 		/*
11160 		 * The local address of this interface is a 6to4 address,
11161 		 * check if this interface is in fact a 6to4 tunnel or just
11162 		 * an interface configured with a 6to4 address.  We are only
11163 		 * interested in the former.
11164 		 */
11165 		if (wqp != NULL) {
11166 			while ((wqp->q_next != NULL) &&
11167 			    (wqp->q_next->q_qinfo != NULL) &&
11168 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11169 
11170 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11171 				    == TUN6TO4_MODID) {
11172 					/* set for use in IP */
11173 					ill->ill_is_6to4tun = 1;
11174 					break;
11175 				}
11176 				wqp = wqp->q_next;
11177 			}
11178 		}
11179 	}
11180 
11181 	ipif_set_default(ipif);
11182 
11183 	/*
11184 	 * When publishing an interface address change event, we only notify
11185 	 * the event listeners of the new address.  It is assumed that if they
11186 	 * actively care about the addresses assigned that they will have
11187 	 * already discovered the previous address assigned (if there was one.)
11188 	 *
11189 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11190 	 */
11191 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11192 		ill_nic_event_dispatch(ill, MAP_IPIF_ID(ipif->ipif_id),
11193 		    NE_ADDRESS_CHANGE, sin, sinlen);
11194 	}
11195 
11196 	mutex_exit(&ill->ill_lock);
11197 
11198 	if (need_up) {
11199 		/*
11200 		 * Now bring the interface back up.  If this
11201 		 * is the only IPIF for the ILL, ipif_up
11202 		 * will have to re-bind to the device, so
11203 		 * we may get back EINPROGRESS, in which
11204 		 * case, this IOCTL will get completed in
11205 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11206 		 */
11207 		err = ipif_up(ipif, q, mp);
11208 	}
11209 
11210 	if (need_dl_down)
11211 		ill_dl_down(ill);
11212 	if (need_arp_down)
11213 		ipif_resolver_down(ipif);
11214 
11215 	return (err);
11216 }
11217 
11218 
11219 /*
11220  * Restart entry point to restart the address set operation after the
11221  * refcounts have dropped to zero.
11222  */
11223 /* ARGSUSED */
11224 int
11225 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11226     ip_ioctl_cmd_t *ipip, void *ifreq)
11227 {
11228 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11229 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11230 	ASSERT(IAM_WRITER_IPIF(ipif));
11231 	ipif_down_tail(ipif);
11232 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11233 }
11234 
11235 /* ARGSUSED */
11236 int
11237 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11238     ip_ioctl_cmd_t *ipip, void *if_req)
11239 {
11240 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11241 	struct lifreq *lifr = (struct lifreq *)if_req;
11242 
11243 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11244 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11245 	/*
11246 	 * The net mask and address can't change since we have a
11247 	 * reference to the ipif. So no lock is necessary.
11248 	 */
11249 	if (ipif->ipif_isv6) {
11250 		*sin6 = sin6_null;
11251 		sin6->sin6_family = AF_INET6;
11252 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11253 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11254 		lifr->lifr_addrlen =
11255 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11256 	} else {
11257 		*sin = sin_null;
11258 		sin->sin_family = AF_INET;
11259 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11260 		if (ipip->ipi_cmd_type == LIF_CMD) {
11261 			lifr->lifr_addrlen =
11262 			    ip_mask_to_plen(ipif->ipif_net_mask);
11263 		}
11264 	}
11265 	return (0);
11266 }
11267 
11268 /*
11269  * Set the destination address for a pt-pt interface.
11270  */
11271 /* ARGSUSED */
11272 int
11273 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11274     ip_ioctl_cmd_t *ipip, void *if_req)
11275 {
11276 	int err = 0;
11277 	in6_addr_t v6addr;
11278 	boolean_t need_up = B_FALSE;
11279 
11280 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11281 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11282 	ASSERT(IAM_WRITER_IPIF(ipif));
11283 
11284 	if (ipif->ipif_isv6) {
11285 		sin6_t *sin6;
11286 
11287 		if (sin->sin_family != AF_INET6)
11288 			return (EAFNOSUPPORT);
11289 
11290 		sin6 = (sin6_t *)sin;
11291 		v6addr = sin6->sin6_addr;
11292 
11293 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11294 			return (EADDRNOTAVAIL);
11295 	} else {
11296 		ipaddr_t addr;
11297 
11298 		if (sin->sin_family != AF_INET)
11299 			return (EAFNOSUPPORT);
11300 
11301 		addr = sin->sin_addr.s_addr;
11302 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11303 			return (EADDRNOTAVAIL);
11304 
11305 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11306 	}
11307 
11308 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11309 		return (0);	/* No change */
11310 
11311 	if (ipif->ipif_flags & IPIF_UP) {
11312 		/*
11313 		 * If the interface is already marked up,
11314 		 * we call ipif_down which will take care
11315 		 * of ditching any IREs that have been set
11316 		 * up based on the old pp dst address.
11317 		 */
11318 		err = ipif_logical_down(ipif, q, mp);
11319 		if (err == EINPROGRESS)
11320 			return (err);
11321 		ipif_down_tail(ipif);
11322 		need_up = B_TRUE;
11323 	}
11324 	/*
11325 	 * could return EINPROGRESS. If so ioctl will complete in
11326 	 * ip_rput_dlpi_writer
11327 	 */
11328 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11329 	return (err);
11330 }
11331 
11332 static int
11333 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11334     boolean_t need_up)
11335 {
11336 	in6_addr_t v6addr;
11337 	ill_t	*ill = ipif->ipif_ill;
11338 	int	err = 0;
11339 	boolean_t need_dl_down;
11340 	boolean_t need_arp_down;
11341 
11342 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11343 	    ipif->ipif_id, (void *)ipif));
11344 
11345 	/* Must cancel any pending timer before taking the ill_lock */
11346 	if (ipif->ipif_recovery_id != 0)
11347 		(void) untimeout(ipif->ipif_recovery_id);
11348 	ipif->ipif_recovery_id = 0;
11349 
11350 	if (ipif->ipif_isv6) {
11351 		sin6_t *sin6;
11352 
11353 		sin6 = (sin6_t *)sin;
11354 		v6addr = sin6->sin6_addr;
11355 	} else {
11356 		ipaddr_t addr;
11357 
11358 		addr = sin->sin_addr.s_addr;
11359 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11360 	}
11361 	mutex_enter(&ill->ill_lock);
11362 	/* Set point to point destination address. */
11363 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11364 		/*
11365 		 * Allow this as a means of creating logical
11366 		 * pt-pt interfaces on top of e.g. an Ethernet.
11367 		 * XXX Undocumented HACK for testing.
11368 		 * pt-pt interfaces are created with NUD disabled.
11369 		 */
11370 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11371 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11372 		if (ipif->ipif_isv6)
11373 			ill->ill_flags |= ILLF_NONUD;
11374 	}
11375 
11376 	/*
11377 	 * If the interface was previously marked as a duplicate, then since
11378 	 * we've now got a "new" address, it should no longer be considered a
11379 	 * duplicate -- even if the "new" address is the same as the old one.
11380 	 * Note that if all ipifs are down, we may have a pending ARP down
11381 	 * event to handle.
11382 	 */
11383 	need_dl_down = need_arp_down = B_FALSE;
11384 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11385 		need_arp_down = !need_up;
11386 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11387 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11388 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11389 			need_dl_down = B_TRUE;
11390 		}
11391 	}
11392 
11393 	/* Set the new address. */
11394 	ipif->ipif_v6pp_dst_addr = v6addr;
11395 	/* Make sure subnet tracks pp_dst */
11396 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11397 	mutex_exit(&ill->ill_lock);
11398 
11399 	if (need_up) {
11400 		/*
11401 		 * Now bring the interface back up.  If this
11402 		 * is the only IPIF for the ILL, ipif_up
11403 		 * will have to re-bind to the device, so
11404 		 * we may get back EINPROGRESS, in which
11405 		 * case, this IOCTL will get completed in
11406 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11407 		 */
11408 		err = ipif_up(ipif, q, mp);
11409 	}
11410 
11411 	if (need_dl_down)
11412 		ill_dl_down(ill);
11413 	if (need_arp_down)
11414 		ipif_resolver_down(ipif);
11415 
11416 	return (err);
11417 }
11418 
11419 /*
11420  * Restart entry point to restart the dstaddress set operation after the
11421  * refcounts have dropped to zero.
11422  */
11423 /* ARGSUSED */
11424 int
11425 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11426     ip_ioctl_cmd_t *ipip, void *ifreq)
11427 {
11428 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11429 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11430 	ipif_down_tail(ipif);
11431 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11432 }
11433 
11434 /* ARGSUSED */
11435 int
11436 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11437     ip_ioctl_cmd_t *ipip, void *if_req)
11438 {
11439 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11440 
11441 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11442 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11443 	/*
11444 	 * Get point to point destination address. The addresses can't
11445 	 * change since we hold a reference to the ipif.
11446 	 */
11447 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11448 		return (EADDRNOTAVAIL);
11449 
11450 	if (ipif->ipif_isv6) {
11451 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11452 		*sin6 = sin6_null;
11453 		sin6->sin6_family = AF_INET6;
11454 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11455 	} else {
11456 		*sin = sin_null;
11457 		sin->sin_family = AF_INET;
11458 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11459 	}
11460 	return (0);
11461 }
11462 
11463 /*
11464  * Set interface flags.  Many flags require special handling (e.g.,
11465  * bringing the interface down); see below for details.
11466  *
11467  * NOTE : We really don't enforce that ipif_id zero should be used
11468  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11469  *	  is because applications generally does SICGLIFFLAGS and
11470  *	  ORs in the new flags (that affects the logical) and does a
11471  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11472  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11473  *	  flags that will be turned on is correct with respect to
11474  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11475  */
11476 /* ARGSUSED */
11477 int
11478 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11479     ip_ioctl_cmd_t *ipip, void *if_req)
11480 {
11481 	uint64_t turn_on;
11482 	uint64_t turn_off;
11483 	int	err = 0;
11484 	phyint_t *phyi;
11485 	ill_t *ill;
11486 	uint64_t intf_flags, cantchange_flags;
11487 	boolean_t phyint_flags_modified = B_FALSE;
11488 	uint64_t flags;
11489 	struct ifreq *ifr;
11490 	struct lifreq *lifr;
11491 	boolean_t set_linklocal = B_FALSE;
11492 	boolean_t zero_source = B_FALSE;
11493 
11494 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11495 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11496 
11497 	ASSERT(IAM_WRITER_IPIF(ipif));
11498 
11499 	ill = ipif->ipif_ill;
11500 	phyi = ill->ill_phyint;
11501 
11502 	if (ipip->ipi_cmd_type == IF_CMD) {
11503 		ifr = (struct ifreq *)if_req;
11504 		flags = (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11505 	} else {
11506 		lifr = (struct lifreq *)if_req;
11507 		flags = lifr->lifr_flags;
11508 	}
11509 
11510 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11511 
11512 	/*
11513 	 * Have the flags been set correctly until now?
11514 	 */
11515 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11516 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11517 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11518 	/*
11519 	 * Compare the new flags to the old, and partition
11520 	 * into those coming on and those going off.
11521 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11522 	 */
11523 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11524 		flags |= intf_flags & ~0xFFFF;
11525 
11526 	/*
11527 	 * Explicitly fail attempts to change flags that are always invalid on
11528 	 * an IPMP meta-interface.
11529 	 */
11530 	if (IS_IPMP(ill) && ((flags ^ intf_flags) & IFF_IPMP_INVALID))
11531 		return (EINVAL);
11532 
11533 	/*
11534 	 * Check which flags will change; silently ignore flags which userland
11535 	 * is not allowed to control.  (Because these flags may change between
11536 	 * SIOCGLIFFLAGS and SIOCSLIFFLAGS, and that's outside of userland's
11537 	 * control, we need to silently ignore them rather than fail.)
11538 	 */
11539 	cantchange_flags = IFF_CANTCHANGE;
11540 	if (IS_IPMP(ill))
11541 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
11542 
11543 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
11544 	if (turn_on == 0)
11545 		return (0);	/* No change */
11546 
11547 	turn_off = intf_flags & turn_on;
11548 	turn_on ^= turn_off;
11549 
11550 	/*
11551 	 * All test addresses must be IFF_DEPRECATED (to ensure source address
11552 	 * selection avoids them) -- so force IFF_DEPRECATED on, and do not
11553 	 * allow it to be turned off.
11554 	 */
11555 	if ((turn_off & (IFF_DEPRECATED|IFF_NOFAILOVER)) == IFF_DEPRECATED &&
11556 	    (turn_on|intf_flags) & IFF_NOFAILOVER)
11557 		return (EINVAL);
11558 
11559 	if (turn_on & IFF_NOFAILOVER) {
11560 		turn_on |= IFF_DEPRECATED;
11561 		flags |= IFF_DEPRECATED;
11562 	}
11563 
11564 	/*
11565 	 * On underlying interfaces, only allow applications to manage test
11566 	 * addresses -- otherwise, they may get confused when the address
11567 	 * moves as part of being brought up.  Likewise, prevent an
11568 	 * application-managed test address from being converted to a data
11569 	 * address.  To prevent migration of administratively up addresses in
11570 	 * the kernel, we don't allow them to be converted either.
11571 	 */
11572 	if (IS_UNDER_IPMP(ill)) {
11573 		const uint64_t appflags = IFF_DHCPRUNNING | IFF_ADDRCONF;
11574 
11575 		if ((turn_on & appflags) && !(flags & IFF_NOFAILOVER))
11576 			return (EINVAL);
11577 
11578 		if ((turn_off & IFF_NOFAILOVER) &&
11579 		    (flags & (appflags | IFF_UP | IFF_DUPLICATE)))
11580 			return (EINVAL);
11581 	}
11582 
11583 	/*
11584 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11585 	 * IPv6 interfaces.
11586 	 */
11587 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11588 		return (EINVAL);
11589 
11590 	/*
11591 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
11592 	 */
11593 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
11594 		return (EINVAL);
11595 
11596 	/*
11597 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11598 	 * interfaces.  It makes no sense in that context.
11599 	 */
11600 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11601 		return (EINVAL);
11602 
11603 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11604 		zero_source = B_TRUE;
11605 
11606 	/*
11607 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11608 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11609 	 * If the link local address isn't set, and can be set, it will get
11610 	 * set later on in this function.
11611 	 */
11612 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11613 	    (flags & IFF_UP) && !zero_source &&
11614 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11615 		if (ipif_cant_setlinklocal(ipif))
11616 			return (EINVAL);
11617 		set_linklocal = B_TRUE;
11618 	}
11619 
11620 	/*
11621 	 * If we modify physical interface flags, we'll potentially need to
11622 	 * send up two routing socket messages for the changes (one for the
11623 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11624 	 */
11625 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11626 		phyint_flags_modified = B_TRUE;
11627 
11628 	/*
11629 	 * All functioning PHYI_STANDBY interfaces start life PHYI_INACTIVE
11630 	 * (otherwise, we'd immediately use them, defeating standby).  Also,
11631 	 * since PHYI_INACTIVE has a separate meaning when PHYI_STANDBY is not
11632 	 * set, don't allow PHYI_STANDBY to be set if PHYI_INACTIVE is already
11633 	 * set, and clear PHYI_INACTIVE if PHYI_STANDBY is being cleared.  We
11634 	 * also don't allow PHYI_STANDBY if VNI is enabled since its semantics
11635 	 * will not be honored.
11636 	 */
11637 	if (turn_on & PHYI_STANDBY) {
11638 		/*
11639 		 * No need to grab ill_g_usesrc_lock here; see the
11640 		 * synchronization notes in ip.c.
11641 		 */
11642 		if (ill->ill_usesrc_grp_next != NULL ||
11643 		    intf_flags & PHYI_INACTIVE)
11644 			return (EINVAL);
11645 		if (!(flags & PHYI_FAILED)) {
11646 			flags |= PHYI_INACTIVE;
11647 			turn_on |= PHYI_INACTIVE;
11648 		}
11649 	}
11650 
11651 	if (turn_off & PHYI_STANDBY) {
11652 		flags &= ~PHYI_INACTIVE;
11653 		turn_off |= PHYI_INACTIVE;
11654 	}
11655 
11656 	/*
11657 	 * PHYI_FAILED and PHYI_INACTIVE are mutually exclusive; fail if both
11658 	 * would end up on.
11659 	 */
11660 	if ((flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
11661 	    (PHYI_FAILED | PHYI_INACTIVE))
11662 		return (EINVAL);
11663 
11664 	/*
11665 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11666 	 * status of the interface.
11667 	 */
11668 	if ((turn_on | turn_off) & ILLF_ROUTER)
11669 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
11670 
11671 	/*
11672 	 * If the interface is not UP and we are not going to
11673 	 * bring it UP, record the flags and return. When the
11674 	 * interface comes UP later, the right actions will be
11675 	 * taken.
11676 	 */
11677 	if (!(ipif->ipif_flags & IPIF_UP) &&
11678 	    !(turn_on & IPIF_UP)) {
11679 		/* Record new flags in their respective places. */
11680 		mutex_enter(&ill->ill_lock);
11681 		mutex_enter(&ill->ill_phyint->phyint_lock);
11682 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11683 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11684 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11685 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11686 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11687 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11688 		mutex_exit(&ill->ill_lock);
11689 		mutex_exit(&ill->ill_phyint->phyint_lock);
11690 
11691 		/*
11692 		 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the
11693 		 * same to the kernel: if any of them has been set by
11694 		 * userland, the interface cannot be used for data traffic.
11695 		 */
11696 		if ((turn_on|turn_off) &
11697 		    (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
11698 			ASSERT(!IS_IPMP(ill));
11699 			/*
11700 			 * It's possible the ill is part of an "anonymous"
11701 			 * IPMP group rather than a real group.  In that case,
11702 			 * there are no other interfaces in the group and thus
11703 			 * no need to call ipmp_phyint_refresh_active().
11704 			 */
11705 			if (IS_UNDER_IPMP(ill))
11706 				ipmp_phyint_refresh_active(phyi);
11707 		}
11708 
11709 		if (phyint_flags_modified) {
11710 			if (phyi->phyint_illv4 != NULL) {
11711 				ip_rts_ifmsg(phyi->phyint_illv4->
11712 				    ill_ipif, RTSQ_DEFAULT);
11713 			}
11714 			if (phyi->phyint_illv6 != NULL) {
11715 				ip_rts_ifmsg(phyi->phyint_illv6->
11716 				    ill_ipif, RTSQ_DEFAULT);
11717 			}
11718 		}
11719 		return (0);
11720 	} else if (set_linklocal || zero_source) {
11721 		mutex_enter(&ill->ill_lock);
11722 		if (set_linklocal)
11723 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11724 		if (zero_source)
11725 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11726 		mutex_exit(&ill->ill_lock);
11727 	}
11728 
11729 	/*
11730 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11731 	 * or point-to-point interfaces with an unspecified destination. We do
11732 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11733 	 * have a subnet assigned, which is how in.ndpd currently manages its
11734 	 * onlink prefix list when no addresses are configured with those
11735 	 * prefixes.
11736 	 */
11737 	if (ipif->ipif_isv6 &&
11738 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11739 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11740 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11741 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11742 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11743 		return (EINVAL);
11744 	}
11745 
11746 	/*
11747 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11748 	 * from being brought up.
11749 	 */
11750 	if (!ipif->ipif_isv6 &&
11751 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11752 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11753 		return (EINVAL);
11754 	}
11755 
11756 	/*
11757 	 * The only flag changes that we currently take specific action on are
11758 	 * IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL, ILLF_NOARP,
11759 	 * ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, IPIF_PREFERRED, and
11760 	 * IPIF_NOFAILOVER.  This is done by bring the ipif down, changing the
11761 	 * flags and bringing it back up again.  For IPIF_NOFAILOVER, the act
11762 	 * of bringing it back up will trigger the address to be moved.
11763 	 */
11764 	if ((turn_on|turn_off) &
11765 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11766 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED|
11767 	    IPIF_NOFAILOVER)) {
11768 		/*
11769 		 * Taking this ipif down, make sure we have
11770 		 * valid net and subnet bcast ire's for other
11771 		 * logical interfaces, if we need them.
11772 		 */
11773 		if (!ipif->ipif_isv6)
11774 			ipif_check_bcast_ires(ipif);
11775 
11776 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11777 		    !(turn_off & IPIF_UP)) {
11778 			if (ipif->ipif_flags & IPIF_UP)
11779 				ill->ill_logical_down = 1;
11780 			turn_on &= ~IPIF_UP;
11781 		}
11782 		err = ipif_down(ipif, q, mp);
11783 		ip1dbg(("ipif_down returns %d err ", err));
11784 		if (err == EINPROGRESS)
11785 			return (err);
11786 		ipif_down_tail(ipif);
11787 	}
11788 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
11789 }
11790 
11791 static int
11792 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
11793 {
11794 	ill_t	*ill;
11795 	phyint_t *phyi;
11796 	uint64_t turn_on, turn_off;
11797 	uint64_t intf_flags, cantchange_flags;
11798 	boolean_t phyint_flags_modified = B_FALSE;
11799 	int	err = 0;
11800 	boolean_t set_linklocal = B_FALSE;
11801 	boolean_t zero_source = B_FALSE;
11802 
11803 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
11804 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
11805 
11806 	ASSERT(IAM_WRITER_IPIF(ipif));
11807 
11808 	ill = ipif->ipif_ill;
11809 	phyi = ill->ill_phyint;
11810 
11811 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11812 	cantchange_flags = IFF_CANTCHANGE | IFF_UP;
11813 	if (IS_IPMP(ill))
11814 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
11815 
11816 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
11817 	turn_off = intf_flags & turn_on;
11818 	turn_on ^= turn_off;
11819 
11820 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11821 		phyint_flags_modified = B_TRUE;
11822 
11823 	/*
11824 	 * Now we change the flags. Track current value of
11825 	 * other flags in their respective places.
11826 	 */
11827 	mutex_enter(&ill->ill_lock);
11828 	mutex_enter(&phyi->phyint_lock);
11829 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11830 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11831 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11832 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11833 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11834 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11835 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
11836 		set_linklocal = B_TRUE;
11837 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
11838 	}
11839 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
11840 		zero_source = B_TRUE;
11841 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
11842 	}
11843 	mutex_exit(&ill->ill_lock);
11844 	mutex_exit(&phyi->phyint_lock);
11845 
11846 	if (set_linklocal)
11847 		(void) ipif_setlinklocal(ipif);
11848 
11849 	if (zero_source)
11850 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11851 	else
11852 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
11853 
11854 	/*
11855 	 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the same to
11856 	 * the kernel: if any of them has been set by userland, the interface
11857 	 * cannot be used for data traffic.
11858 	 */
11859 	if ((turn_on|turn_off) & (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
11860 		ASSERT(!IS_IPMP(ill));
11861 		/*
11862 		 * It's possible the ill is part of an "anonymous" IPMP group
11863 		 * rather than a real group.  In that case, there are no other
11864 		 * interfaces in the group and thus no need for us to call
11865 		 * ipmp_phyint_refresh_active().
11866 		 */
11867 		if (IS_UNDER_IPMP(ill))
11868 			ipmp_phyint_refresh_active(phyi);
11869 	}
11870 
11871 	if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
11872 		/*
11873 		 * XXX ipif_up really does not know whether a phyint flags
11874 		 * was modified or not. So, it sends up information on
11875 		 * only one routing sockets message. As we don't bring up
11876 		 * the interface and also set PHYI_ flags simultaneously
11877 		 * it should be okay.
11878 		 */
11879 		err = ipif_up(ipif, q, mp);
11880 	} else {
11881 		/*
11882 		 * Make sure routing socket sees all changes to the flags.
11883 		 * ipif_up_done* handles this when we use ipif_up.
11884 		 */
11885 		if (phyint_flags_modified) {
11886 			if (phyi->phyint_illv4 != NULL) {
11887 				ip_rts_ifmsg(phyi->phyint_illv4->
11888 				    ill_ipif, RTSQ_DEFAULT);
11889 			}
11890 			if (phyi->phyint_illv6 != NULL) {
11891 				ip_rts_ifmsg(phyi->phyint_illv6->
11892 				    ill_ipif, RTSQ_DEFAULT);
11893 			}
11894 		} else {
11895 			ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
11896 		}
11897 		/*
11898 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
11899 		 * this in need_up case.
11900 		 */
11901 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
11902 	}
11903 	return (err);
11904 }
11905 
11906 /*
11907  * Restart the flags operation now that the refcounts have dropped to zero.
11908  */
11909 /* ARGSUSED */
11910 int
11911 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11912     ip_ioctl_cmd_t *ipip, void *if_req)
11913 {
11914 	uint64_t flags;
11915 	struct ifreq *ifr = if_req;
11916 	struct lifreq *lifr = if_req;
11917 
11918 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
11919 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11920 
11921 	ipif_down_tail(ipif);
11922 	if (ipip->ipi_cmd_type == IF_CMD) {
11923 		/* cast to uint16_t prevents unwanted sign extension */
11924 		flags = (uint16_t)ifr->ifr_flags;
11925 	} else {
11926 		flags = lifr->lifr_flags;
11927 	}
11928 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
11929 }
11930 
11931 /*
11932  * Can operate on either a module or a driver queue.
11933  */
11934 /* ARGSUSED */
11935 int
11936 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11937     ip_ioctl_cmd_t *ipip, void *if_req)
11938 {
11939 	/*
11940 	 * Has the flags been set correctly till now ?
11941 	 */
11942 	ill_t *ill = ipif->ipif_ill;
11943 	phyint_t *phyi = ill->ill_phyint;
11944 
11945 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
11946 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11947 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11948 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11949 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11950 
11951 	/*
11952 	 * Need a lock since some flags can be set even when there are
11953 	 * references to the ipif.
11954 	 */
11955 	mutex_enter(&ill->ill_lock);
11956 	if (ipip->ipi_cmd_type == IF_CMD) {
11957 		struct ifreq *ifr = (struct ifreq *)if_req;
11958 
11959 		/* Get interface flags (low 16 only). */
11960 		ifr->ifr_flags = ((ipif->ipif_flags |
11961 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
11962 	} else {
11963 		struct lifreq *lifr = (struct lifreq *)if_req;
11964 
11965 		/* Get interface flags. */
11966 		lifr->lifr_flags = ipif->ipif_flags |
11967 		    ill->ill_flags | phyi->phyint_flags;
11968 	}
11969 	mutex_exit(&ill->ill_lock);
11970 	return (0);
11971 }
11972 
11973 /* ARGSUSED */
11974 int
11975 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11976     ip_ioctl_cmd_t *ipip, void *if_req)
11977 {
11978 	int mtu;
11979 	int ip_min_mtu;
11980 	struct ifreq	*ifr;
11981 	struct lifreq *lifr;
11982 	ire_t	*ire;
11983 	ip_stack_t *ipst;
11984 
11985 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
11986 	    ipif->ipif_id, (void *)ipif));
11987 	if (ipip->ipi_cmd_type == IF_CMD) {
11988 		ifr = (struct ifreq *)if_req;
11989 		mtu = ifr->ifr_metric;
11990 	} else {
11991 		lifr = (struct lifreq *)if_req;
11992 		mtu = lifr->lifr_mtu;
11993 	}
11994 
11995 	if (ipif->ipif_isv6)
11996 		ip_min_mtu = IPV6_MIN_MTU;
11997 	else
11998 		ip_min_mtu = IP_MIN_MTU;
11999 
12000 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12001 		return (EINVAL);
12002 
12003 	/*
12004 	 * Change the MTU size in all relevant ire's.
12005 	 * Mtu change Vs. new ire creation - protocol below.
12006 	 * First change ipif_mtu and the ire_max_frag of the
12007 	 * interface ire. Then do an ire walk and change the
12008 	 * ire_max_frag of all affected ires. During ire_add
12009 	 * under the bucket lock, set the ire_max_frag of the
12010 	 * new ire being created from the ipif/ire from which
12011 	 * it is being derived. If an mtu change happens after
12012 	 * the ire is added, the new ire will be cleaned up.
12013 	 * Conversely if the mtu change happens before the ire
12014 	 * is added, ire_add will see the new value of the mtu.
12015 	 */
12016 	ipif->ipif_mtu = mtu;
12017 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12018 
12019 	if (ipif->ipif_isv6)
12020 		ire = ipif_to_ire_v6(ipif);
12021 	else
12022 		ire = ipif_to_ire(ipif);
12023 	if (ire != NULL) {
12024 		ire->ire_max_frag = ipif->ipif_mtu;
12025 		ire_refrele(ire);
12026 	}
12027 	ipst = ipif->ipif_ill->ill_ipst;
12028 	if (ipif->ipif_flags & IPIF_UP) {
12029 		if (ipif->ipif_isv6)
12030 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12031 			    ipst);
12032 		else
12033 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12034 			    ipst);
12035 	}
12036 	/* Update the MTU in SCTP's list */
12037 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12038 	return (0);
12039 }
12040 
12041 /* Get interface MTU. */
12042 /* ARGSUSED */
12043 int
12044 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12045 	ip_ioctl_cmd_t *ipip, void *if_req)
12046 {
12047 	struct ifreq	*ifr;
12048 	struct lifreq	*lifr;
12049 
12050 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12051 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12052 	if (ipip->ipi_cmd_type == IF_CMD) {
12053 		ifr = (struct ifreq *)if_req;
12054 		ifr->ifr_metric = ipif->ipif_mtu;
12055 	} else {
12056 		lifr = (struct lifreq *)if_req;
12057 		lifr->lifr_mtu = ipif->ipif_mtu;
12058 	}
12059 	return (0);
12060 }
12061 
12062 /* Set interface broadcast address. */
12063 /* ARGSUSED2 */
12064 int
12065 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12066 	ip_ioctl_cmd_t *ipip, void *if_req)
12067 {
12068 	ipaddr_t addr;
12069 	ire_t	*ire;
12070 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12071 
12072 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12073 	    ipif->ipif_id));
12074 
12075 	ASSERT(IAM_WRITER_IPIF(ipif));
12076 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12077 		return (EADDRNOTAVAIL);
12078 
12079 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12080 
12081 	if (sin->sin_family != AF_INET)
12082 		return (EAFNOSUPPORT);
12083 
12084 	addr = sin->sin_addr.s_addr;
12085 	if (ipif->ipif_flags & IPIF_UP) {
12086 		/*
12087 		 * If we are already up, make sure the new
12088 		 * broadcast address makes sense.  If it does,
12089 		 * there should be an IRE for it already.
12090 		 * Don't match on ipif, only on the ill
12091 		 * since we are sharing these now.
12092 		 */
12093 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12094 		    ipif, ALL_ZONES, NULL,
12095 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12096 		if (ire == NULL) {
12097 			return (EINVAL);
12098 		} else {
12099 			ire_refrele(ire);
12100 		}
12101 	}
12102 	/*
12103 	 * Changing the broadcast addr for this ipif.
12104 	 * Make sure we have valid net and subnet bcast
12105 	 * ire's for other logical interfaces, if needed.
12106 	 */
12107 	if (addr != ipif->ipif_brd_addr)
12108 		ipif_check_bcast_ires(ipif);
12109 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12110 	return (0);
12111 }
12112 
12113 /* Get interface broadcast address. */
12114 /* ARGSUSED */
12115 int
12116 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12117     ip_ioctl_cmd_t *ipip, void *if_req)
12118 {
12119 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12120 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12121 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12122 		return (EADDRNOTAVAIL);
12123 
12124 	/* IPIF_BROADCAST not possible with IPv6 */
12125 	ASSERT(!ipif->ipif_isv6);
12126 	*sin = sin_null;
12127 	sin->sin_family = AF_INET;
12128 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12129 	return (0);
12130 }
12131 
12132 /*
12133  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12134  */
12135 /* ARGSUSED */
12136 int
12137 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12138     ip_ioctl_cmd_t *ipip, void *if_req)
12139 {
12140 	int err = 0;
12141 	in6_addr_t v6mask;
12142 
12143 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12144 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12145 
12146 	ASSERT(IAM_WRITER_IPIF(ipif));
12147 
12148 	if (ipif->ipif_isv6) {
12149 		sin6_t *sin6;
12150 
12151 		if (sin->sin_family != AF_INET6)
12152 			return (EAFNOSUPPORT);
12153 
12154 		sin6 = (sin6_t *)sin;
12155 		v6mask = sin6->sin6_addr;
12156 	} else {
12157 		ipaddr_t mask;
12158 
12159 		if (sin->sin_family != AF_INET)
12160 			return (EAFNOSUPPORT);
12161 
12162 		mask = sin->sin_addr.s_addr;
12163 		V4MASK_TO_V6(mask, v6mask);
12164 	}
12165 
12166 	/*
12167 	 * No big deal if the interface isn't already up, or the mask
12168 	 * isn't really changing, or this is pt-pt.
12169 	 */
12170 	if (!(ipif->ipif_flags & IPIF_UP) ||
12171 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12172 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12173 		ipif->ipif_v6net_mask = v6mask;
12174 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12175 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12176 			    ipif->ipif_v6net_mask,
12177 			    ipif->ipif_v6subnet);
12178 		}
12179 		return (0);
12180 	}
12181 	/*
12182 	 * Make sure we have valid net and subnet broadcast ire's
12183 	 * for the old netmask, if needed by other logical interfaces.
12184 	 */
12185 	if (!ipif->ipif_isv6)
12186 		ipif_check_bcast_ires(ipif);
12187 
12188 	err = ipif_logical_down(ipif, q, mp);
12189 	if (err == EINPROGRESS)
12190 		return (err);
12191 	ipif_down_tail(ipif);
12192 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12193 	return (err);
12194 }
12195 
12196 static int
12197 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12198 {
12199 	in6_addr_t v6mask;
12200 	int err = 0;
12201 
12202 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12203 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12204 
12205 	if (ipif->ipif_isv6) {
12206 		sin6_t *sin6;
12207 
12208 		sin6 = (sin6_t *)sin;
12209 		v6mask = sin6->sin6_addr;
12210 	} else {
12211 		ipaddr_t mask;
12212 
12213 		mask = sin->sin_addr.s_addr;
12214 		V4MASK_TO_V6(mask, v6mask);
12215 	}
12216 
12217 	ipif->ipif_v6net_mask = v6mask;
12218 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12219 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12220 		    ipif->ipif_v6subnet);
12221 	}
12222 	err = ipif_up(ipif, q, mp);
12223 
12224 	if (err == 0 || err == EINPROGRESS) {
12225 		/*
12226 		 * The interface must be DL_BOUND if this packet has to
12227 		 * go out on the wire. Since we only go through a logical
12228 		 * down and are bound with the driver during an internal
12229 		 * down/up that is satisfied.
12230 		 */
12231 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12232 			/* Potentially broadcast an address mask reply. */
12233 			ipif_mask_reply(ipif);
12234 		}
12235 	}
12236 	return (err);
12237 }
12238 
12239 /* ARGSUSED */
12240 int
12241 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12242     ip_ioctl_cmd_t *ipip, void *if_req)
12243 {
12244 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12245 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12246 	ipif_down_tail(ipif);
12247 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12248 }
12249 
12250 /* Get interface net mask. */
12251 /* ARGSUSED */
12252 int
12253 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12254     ip_ioctl_cmd_t *ipip, void *if_req)
12255 {
12256 	struct lifreq *lifr = (struct lifreq *)if_req;
12257 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12258 
12259 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12260 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12261 
12262 	/*
12263 	 * net mask can't change since we have a reference to the ipif.
12264 	 */
12265 	if (ipif->ipif_isv6) {
12266 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12267 		*sin6 = sin6_null;
12268 		sin6->sin6_family = AF_INET6;
12269 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12270 		lifr->lifr_addrlen =
12271 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12272 	} else {
12273 		*sin = sin_null;
12274 		sin->sin_family = AF_INET;
12275 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12276 		if (ipip->ipi_cmd_type == LIF_CMD) {
12277 			lifr->lifr_addrlen =
12278 			    ip_mask_to_plen(ipif->ipif_net_mask);
12279 		}
12280 	}
12281 	return (0);
12282 }
12283 
12284 /* ARGSUSED */
12285 int
12286 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12287     ip_ioctl_cmd_t *ipip, void *if_req)
12288 {
12289 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12290 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12291 
12292 	/*
12293 	 * Since no applications should ever be setting metrics on underlying
12294 	 * interfaces, we explicitly fail to smoke 'em out.
12295 	 */
12296 	if (IS_UNDER_IPMP(ipif->ipif_ill))
12297 		return (EINVAL);
12298 
12299 	/*
12300 	 * Set interface metric.  We don't use this for
12301 	 * anything but we keep track of it in case it is
12302 	 * important to routing applications or such.
12303 	 */
12304 	if (ipip->ipi_cmd_type == IF_CMD) {
12305 		struct ifreq    *ifr;
12306 
12307 		ifr = (struct ifreq *)if_req;
12308 		ipif->ipif_metric = ifr->ifr_metric;
12309 	} else {
12310 		struct lifreq   *lifr;
12311 
12312 		lifr = (struct lifreq *)if_req;
12313 		ipif->ipif_metric = lifr->lifr_metric;
12314 	}
12315 	return (0);
12316 }
12317 
12318 /* ARGSUSED */
12319 int
12320 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12321     ip_ioctl_cmd_t *ipip, void *if_req)
12322 {
12323 	/* Get interface metric. */
12324 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12325 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12326 
12327 	if (ipip->ipi_cmd_type == IF_CMD) {
12328 		struct ifreq    *ifr;
12329 
12330 		ifr = (struct ifreq *)if_req;
12331 		ifr->ifr_metric = ipif->ipif_metric;
12332 	} else {
12333 		struct lifreq   *lifr;
12334 
12335 		lifr = (struct lifreq *)if_req;
12336 		lifr->lifr_metric = ipif->ipif_metric;
12337 	}
12338 
12339 	return (0);
12340 }
12341 
12342 /* ARGSUSED */
12343 int
12344 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12345     ip_ioctl_cmd_t *ipip, void *if_req)
12346 {
12347 
12348 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12349 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12350 	/*
12351 	 * Set the muxid returned from I_PLINK.
12352 	 */
12353 	if (ipip->ipi_cmd_type == IF_CMD) {
12354 		struct ifreq *ifr = (struct ifreq *)if_req;
12355 
12356 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12357 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12358 	} else {
12359 		struct lifreq *lifr = (struct lifreq *)if_req;
12360 
12361 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12362 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12363 	}
12364 	return (0);
12365 }
12366 
12367 /* ARGSUSED */
12368 int
12369 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12370     ip_ioctl_cmd_t *ipip, void *if_req)
12371 {
12372 
12373 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12374 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12375 	/*
12376 	 * Get the muxid saved in ill for I_PUNLINK.
12377 	 */
12378 	if (ipip->ipi_cmd_type == IF_CMD) {
12379 		struct ifreq *ifr = (struct ifreq *)if_req;
12380 
12381 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12382 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12383 	} else {
12384 		struct lifreq *lifr = (struct lifreq *)if_req;
12385 
12386 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12387 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12388 	}
12389 	return (0);
12390 }
12391 
12392 /*
12393  * Set the subnet prefix. Does not modify the broadcast address.
12394  */
12395 /* ARGSUSED */
12396 int
12397 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12398     ip_ioctl_cmd_t *ipip, void *if_req)
12399 {
12400 	int err = 0;
12401 	in6_addr_t v6addr;
12402 	in6_addr_t v6mask;
12403 	boolean_t need_up = B_FALSE;
12404 	int addrlen;
12405 
12406 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12407 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12408 
12409 	ASSERT(IAM_WRITER_IPIF(ipif));
12410 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12411 
12412 	if (ipif->ipif_isv6) {
12413 		sin6_t *sin6;
12414 
12415 		if (sin->sin_family != AF_INET6)
12416 			return (EAFNOSUPPORT);
12417 
12418 		sin6 = (sin6_t *)sin;
12419 		v6addr = sin6->sin6_addr;
12420 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12421 			return (EADDRNOTAVAIL);
12422 	} else {
12423 		ipaddr_t addr;
12424 
12425 		if (sin->sin_family != AF_INET)
12426 			return (EAFNOSUPPORT);
12427 
12428 		addr = sin->sin_addr.s_addr;
12429 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12430 			return (EADDRNOTAVAIL);
12431 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12432 		/* Add 96 bits */
12433 		addrlen += IPV6_ABITS - IP_ABITS;
12434 	}
12435 
12436 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12437 		return (EINVAL);
12438 
12439 	/* Check if bits in the address is set past the mask */
12440 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12441 		return (EINVAL);
12442 
12443 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12444 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12445 		return (0);	/* No change */
12446 
12447 	if (ipif->ipif_flags & IPIF_UP) {
12448 		/*
12449 		 * If the interface is already marked up,
12450 		 * we call ipif_down which will take care
12451 		 * of ditching any IREs that have been set
12452 		 * up based on the old interface address.
12453 		 */
12454 		err = ipif_logical_down(ipif, q, mp);
12455 		if (err == EINPROGRESS)
12456 			return (err);
12457 		ipif_down_tail(ipif);
12458 		need_up = B_TRUE;
12459 	}
12460 
12461 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12462 	return (err);
12463 }
12464 
12465 static int
12466 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12467     queue_t *q, mblk_t *mp, boolean_t need_up)
12468 {
12469 	ill_t	*ill = ipif->ipif_ill;
12470 	int	err = 0;
12471 
12472 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12473 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12474 
12475 	/* Set the new address. */
12476 	mutex_enter(&ill->ill_lock);
12477 	ipif->ipif_v6net_mask = v6mask;
12478 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12479 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12480 		    ipif->ipif_v6subnet);
12481 	}
12482 	mutex_exit(&ill->ill_lock);
12483 
12484 	if (need_up) {
12485 		/*
12486 		 * Now bring the interface back up.  If this
12487 		 * is the only IPIF for the ILL, ipif_up
12488 		 * will have to re-bind to the device, so
12489 		 * we may get back EINPROGRESS, in which
12490 		 * case, this IOCTL will get completed in
12491 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12492 		 */
12493 		err = ipif_up(ipif, q, mp);
12494 		if (err == EINPROGRESS)
12495 			return (err);
12496 	}
12497 	return (err);
12498 }
12499 
12500 /* ARGSUSED */
12501 int
12502 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12503     ip_ioctl_cmd_t *ipip, void *if_req)
12504 {
12505 	int	addrlen;
12506 	in6_addr_t v6addr;
12507 	in6_addr_t v6mask;
12508 	struct lifreq *lifr = (struct lifreq *)if_req;
12509 
12510 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12511 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12512 	ipif_down_tail(ipif);
12513 
12514 	addrlen = lifr->lifr_addrlen;
12515 	if (ipif->ipif_isv6) {
12516 		sin6_t *sin6;
12517 
12518 		sin6 = (sin6_t *)sin;
12519 		v6addr = sin6->sin6_addr;
12520 	} else {
12521 		ipaddr_t addr;
12522 
12523 		addr = sin->sin_addr.s_addr;
12524 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12525 		addrlen += IPV6_ABITS - IP_ABITS;
12526 	}
12527 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12528 
12529 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12530 }
12531 
12532 /* ARGSUSED */
12533 int
12534 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12535     ip_ioctl_cmd_t *ipip, void *if_req)
12536 {
12537 	struct lifreq *lifr = (struct lifreq *)if_req;
12538 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12539 
12540 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12541 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12542 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12543 
12544 	if (ipif->ipif_isv6) {
12545 		*sin6 = sin6_null;
12546 		sin6->sin6_family = AF_INET6;
12547 		sin6->sin6_addr = ipif->ipif_v6subnet;
12548 		lifr->lifr_addrlen =
12549 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12550 	} else {
12551 		*sin = sin_null;
12552 		sin->sin_family = AF_INET;
12553 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12554 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12555 	}
12556 	return (0);
12557 }
12558 
12559 /*
12560  * Set the IPv6 address token.
12561  */
12562 /* ARGSUSED */
12563 int
12564 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12565     ip_ioctl_cmd_t *ipi, void *if_req)
12566 {
12567 	ill_t *ill = ipif->ipif_ill;
12568 	int err;
12569 	in6_addr_t v6addr;
12570 	in6_addr_t v6mask;
12571 	boolean_t need_up = B_FALSE;
12572 	int i;
12573 	sin6_t *sin6 = (sin6_t *)sin;
12574 	struct lifreq *lifr = (struct lifreq *)if_req;
12575 	int addrlen;
12576 
12577 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12578 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12579 	ASSERT(IAM_WRITER_IPIF(ipif));
12580 
12581 	addrlen = lifr->lifr_addrlen;
12582 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12583 	if (ipif->ipif_id != 0)
12584 		return (EINVAL);
12585 
12586 	if (!ipif->ipif_isv6)
12587 		return (EINVAL);
12588 
12589 	if (addrlen > IPV6_ABITS)
12590 		return (EINVAL);
12591 
12592 	v6addr = sin6->sin6_addr;
12593 
12594 	/*
12595 	 * The length of the token is the length from the end.  To get
12596 	 * the proper mask for this, compute the mask of the bits not
12597 	 * in the token; ie. the prefix, and then xor to get the mask.
12598 	 */
12599 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12600 		return (EINVAL);
12601 	for (i = 0; i < 4; i++) {
12602 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12603 	}
12604 
12605 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12606 	    ill->ill_token_length == addrlen)
12607 		return (0);	/* No change */
12608 
12609 	if (ipif->ipif_flags & IPIF_UP) {
12610 		err = ipif_logical_down(ipif, q, mp);
12611 		if (err == EINPROGRESS)
12612 			return (err);
12613 		ipif_down_tail(ipif);
12614 		need_up = B_TRUE;
12615 	}
12616 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12617 	return (err);
12618 }
12619 
12620 static int
12621 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12622     mblk_t *mp, boolean_t need_up)
12623 {
12624 	in6_addr_t v6addr;
12625 	in6_addr_t v6mask;
12626 	ill_t	*ill = ipif->ipif_ill;
12627 	int	i;
12628 	int	err = 0;
12629 
12630 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12631 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12632 	v6addr = sin6->sin6_addr;
12633 	/*
12634 	 * The length of the token is the length from the end.  To get
12635 	 * the proper mask for this, compute the mask of the bits not
12636 	 * in the token; ie. the prefix, and then xor to get the mask.
12637 	 */
12638 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12639 	for (i = 0; i < 4; i++)
12640 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12641 
12642 	mutex_enter(&ill->ill_lock);
12643 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12644 	ill->ill_token_length = addrlen;
12645 	mutex_exit(&ill->ill_lock);
12646 
12647 	if (need_up) {
12648 		/*
12649 		 * Now bring the interface back up.  If this
12650 		 * is the only IPIF for the ILL, ipif_up
12651 		 * will have to re-bind to the device, so
12652 		 * we may get back EINPROGRESS, in which
12653 		 * case, this IOCTL will get completed in
12654 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12655 		 */
12656 		err = ipif_up(ipif, q, mp);
12657 		if (err == EINPROGRESS)
12658 			return (err);
12659 	}
12660 	return (err);
12661 }
12662 
12663 /* ARGSUSED */
12664 int
12665 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12666     ip_ioctl_cmd_t *ipi, void *if_req)
12667 {
12668 	ill_t *ill;
12669 	sin6_t *sin6 = (sin6_t *)sin;
12670 	struct lifreq *lifr = (struct lifreq *)if_req;
12671 
12672 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12673 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12674 	if (ipif->ipif_id != 0)
12675 		return (EINVAL);
12676 
12677 	ill = ipif->ipif_ill;
12678 	if (!ill->ill_isv6)
12679 		return (ENXIO);
12680 
12681 	*sin6 = sin6_null;
12682 	sin6->sin6_family = AF_INET6;
12683 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12684 	sin6->sin6_addr = ill->ill_token;
12685 	lifr->lifr_addrlen = ill->ill_token_length;
12686 	return (0);
12687 }
12688 
12689 /*
12690  * Set (hardware) link specific information that might override
12691  * what was acquired through the DL_INFO_ACK.
12692  * The logic is as follows.
12693  *
12694  * become exclusive
12695  * set CHANGING flag
12696  * change mtu on affected IREs
12697  * clear CHANGING flag
12698  *
12699  * An ire add that occurs before the CHANGING flag is set will have its mtu
12700  * changed by the ip_sioctl_lnkinfo.
12701  *
12702  * During the time the CHANGING flag is set, no new ires will be added to the
12703  * bucket, and ire add will fail (due the CHANGING flag).
12704  *
12705  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12706  * before it is added to the bucket.
12707  *
12708  * Obviously only 1 thread can set the CHANGING flag and we need to become
12709  * exclusive to set the flag.
12710  */
12711 /* ARGSUSED */
12712 int
12713 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12714     ip_ioctl_cmd_t *ipi, void *if_req)
12715 {
12716 	ill_t		*ill = ipif->ipif_ill;
12717 	ipif_t		*nipif;
12718 	int		ip_min_mtu;
12719 	boolean_t	mtu_walk = B_FALSE;
12720 	struct lifreq	*lifr = (struct lifreq *)if_req;
12721 	lif_ifinfo_req_t *lir;
12722 	ire_t		*ire;
12723 
12724 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12725 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12726 	lir = &lifr->lifr_ifinfo;
12727 	ASSERT(IAM_WRITER_IPIF(ipif));
12728 
12729 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12730 	if (ipif->ipif_id != 0)
12731 		return (EINVAL);
12732 
12733 	/* Set interface MTU. */
12734 	if (ipif->ipif_isv6)
12735 		ip_min_mtu = IPV6_MIN_MTU;
12736 	else
12737 		ip_min_mtu = IP_MIN_MTU;
12738 
12739 	/*
12740 	 * Verify values before we set anything. Allow zero to
12741 	 * mean unspecified.
12742 	 */
12743 	if (lir->lir_maxmtu != 0 &&
12744 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12745 	    lir->lir_maxmtu < ip_min_mtu))
12746 		return (EINVAL);
12747 	if (lir->lir_reachtime != 0 &&
12748 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12749 		return (EINVAL);
12750 	if (lir->lir_reachretrans != 0 &&
12751 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12752 		return (EINVAL);
12753 
12754 	mutex_enter(&ill->ill_lock);
12755 	ill->ill_state_flags |= ILL_CHANGING;
12756 	for (nipif = ill->ill_ipif; nipif != NULL;
12757 	    nipif = nipif->ipif_next) {
12758 		nipif->ipif_state_flags |= IPIF_CHANGING;
12759 	}
12760 
12761 	if (lir->lir_maxmtu != 0) {
12762 		ill->ill_max_mtu = lir->lir_maxmtu;
12763 		ill->ill_user_mtu = lir->lir_maxmtu;
12764 		mtu_walk = B_TRUE;
12765 	}
12766 	mutex_exit(&ill->ill_lock);
12767 
12768 	if (lir->lir_reachtime != 0)
12769 		ill->ill_reachable_time = lir->lir_reachtime;
12770 
12771 	if (lir->lir_reachretrans != 0)
12772 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12773 
12774 	ill->ill_max_hops = lir->lir_maxhops;
12775 
12776 	ill->ill_max_buf = ND_MAX_Q;
12777 
12778 	if (mtu_walk) {
12779 		/*
12780 		 * Set the MTU on all ipifs associated with this ill except
12781 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12782 		 */
12783 		for (nipif = ill->ill_ipif; nipif != NULL;
12784 		    nipif = nipif->ipif_next) {
12785 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12786 				continue;
12787 
12788 			nipif->ipif_mtu = ill->ill_max_mtu;
12789 
12790 			if (!(nipif->ipif_flags & IPIF_UP))
12791 				continue;
12792 
12793 			if (nipif->ipif_isv6)
12794 				ire = ipif_to_ire_v6(nipif);
12795 			else
12796 				ire = ipif_to_ire(nipif);
12797 			if (ire != NULL) {
12798 				ire->ire_max_frag = ipif->ipif_mtu;
12799 				ire_refrele(ire);
12800 			}
12801 
12802 			ire_walk_ill(MATCH_IRE_ILL, 0, ipif_mtu_change,
12803 			    nipif, ill);
12804 		}
12805 	}
12806 
12807 	mutex_enter(&ill->ill_lock);
12808 	for (nipif = ill->ill_ipif; nipif != NULL;
12809 	    nipif = nipif->ipif_next) {
12810 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
12811 	}
12812 	ILL_UNMARK_CHANGING(ill);
12813 	mutex_exit(&ill->ill_lock);
12814 
12815 	/*
12816 	 * Refresh IPMP meta-interface MTU if necessary.
12817 	 */
12818 	if (IS_UNDER_IPMP(ill))
12819 		ipmp_illgrp_refresh_mtu(ill->ill_grp);
12820 
12821 	return (0);
12822 }
12823 
12824 /* ARGSUSED */
12825 int
12826 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12827     ip_ioctl_cmd_t *ipi, void *if_req)
12828 {
12829 	struct lif_ifinfo_req *lir;
12830 	ill_t *ill = ipif->ipif_ill;
12831 
12832 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
12833 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12834 	if (ipif->ipif_id != 0)
12835 		return (EINVAL);
12836 
12837 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
12838 	lir->lir_maxhops = ill->ill_max_hops;
12839 	lir->lir_reachtime = ill->ill_reachable_time;
12840 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
12841 	lir->lir_maxmtu = ill->ill_max_mtu;
12842 
12843 	return (0);
12844 }
12845 
12846 /*
12847  * Return best guess as to the subnet mask for the specified address.
12848  * Based on the subnet masks for all the configured interfaces.
12849  *
12850  * We end up returning a zero mask in the case of default, multicast or
12851  * experimental.
12852  */
12853 static ipaddr_t
12854 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
12855 {
12856 	ipaddr_t net_mask;
12857 	ill_t	*ill;
12858 	ipif_t	*ipif;
12859 	ill_walk_context_t ctx;
12860 	ipif_t	*fallback_ipif = NULL;
12861 
12862 	net_mask = ip_net_mask(addr);
12863 	if (net_mask == 0) {
12864 		*ipifp = NULL;
12865 		return (0);
12866 	}
12867 
12868 	/* Let's check to see if this is maybe a local subnet route. */
12869 	/* this function only applies to IPv4 interfaces */
12870 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
12871 	ill = ILL_START_WALK_V4(&ctx, ipst);
12872 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
12873 		mutex_enter(&ill->ill_lock);
12874 		for (ipif = ill->ill_ipif; ipif != NULL;
12875 		    ipif = ipif->ipif_next) {
12876 			if (!IPIF_CAN_LOOKUP(ipif))
12877 				continue;
12878 			if (!(ipif->ipif_flags & IPIF_UP))
12879 				continue;
12880 			if ((ipif->ipif_subnet & net_mask) ==
12881 			    (addr & net_mask)) {
12882 				/*
12883 				 * Don't trust pt-pt interfaces if there are
12884 				 * other interfaces.
12885 				 */
12886 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
12887 					if (fallback_ipif == NULL) {
12888 						ipif_refhold_locked(ipif);
12889 						fallback_ipif = ipif;
12890 					}
12891 					continue;
12892 				}
12893 
12894 				/*
12895 				 * Fine. Just assume the same net mask as the
12896 				 * directly attached subnet interface is using.
12897 				 */
12898 				ipif_refhold_locked(ipif);
12899 				mutex_exit(&ill->ill_lock);
12900 				rw_exit(&ipst->ips_ill_g_lock);
12901 				if (fallback_ipif != NULL)
12902 					ipif_refrele(fallback_ipif);
12903 				*ipifp = ipif;
12904 				return (ipif->ipif_net_mask);
12905 			}
12906 		}
12907 		mutex_exit(&ill->ill_lock);
12908 	}
12909 	rw_exit(&ipst->ips_ill_g_lock);
12910 
12911 	*ipifp = fallback_ipif;
12912 	return ((fallback_ipif != NULL) ?
12913 	    fallback_ipif->ipif_net_mask : net_mask);
12914 }
12915 
12916 /*
12917  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
12918  */
12919 static void
12920 ip_wput_ioctl(queue_t *q, mblk_t *mp)
12921 {
12922 	IOCP	iocp;
12923 	ipft_t	*ipft;
12924 	ipllc_t	*ipllc;
12925 	mblk_t	*mp1;
12926 	cred_t	*cr;
12927 	int	error = 0;
12928 	conn_t	*connp;
12929 
12930 	ip1dbg(("ip_wput_ioctl"));
12931 	iocp = (IOCP)mp->b_rptr;
12932 	mp1 = mp->b_cont;
12933 	if (mp1 == NULL) {
12934 		iocp->ioc_error = EINVAL;
12935 		mp->b_datap->db_type = M_IOCNAK;
12936 		iocp->ioc_count = 0;
12937 		qreply(q, mp);
12938 		return;
12939 	}
12940 
12941 	/*
12942 	 * These IOCTLs provide various control capabilities to
12943 	 * upstream agents such as ULPs and processes.	There
12944 	 * are currently two such IOCTLs implemented.  They
12945 	 * are used by TCP to provide update information for
12946 	 * existing IREs and to forcibly delete an IRE for a
12947 	 * host that is not responding, thereby forcing an
12948 	 * attempt at a new route.
12949 	 */
12950 	iocp->ioc_error = EINVAL;
12951 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
12952 		goto done;
12953 
12954 	ipllc = (ipllc_t *)mp1->b_rptr;
12955 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
12956 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
12957 			break;
12958 	}
12959 	/*
12960 	 * prefer credential from mblk over ioctl;
12961 	 * see ip_sioctl_copyin_setup
12962 	 */
12963 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
12964 
12965 	/*
12966 	 * Refhold the conn in case the request gets queued up in some lookup
12967 	 */
12968 	ASSERT(CONN_Q(q));
12969 	connp = Q_TO_CONN(q);
12970 	CONN_INC_REF(connp);
12971 	if (ipft->ipft_pfi &&
12972 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
12973 	    pullupmsg(mp1, ipft->ipft_min_size))) {
12974 		error = (*ipft->ipft_pfi)(q,
12975 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
12976 	}
12977 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
12978 		/*
12979 		 * CONN_OPER_PENDING_DONE happens in the function called
12980 		 * through ipft_pfi above.
12981 		 */
12982 		return;
12983 	}
12984 
12985 	CONN_OPER_PENDING_DONE(connp);
12986 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
12987 		freemsg(mp);
12988 		return;
12989 	}
12990 	iocp->ioc_error = error;
12991 
12992 done:
12993 	mp->b_datap->db_type = M_IOCACK;
12994 	if (iocp->ioc_error)
12995 		iocp->ioc_count = 0;
12996 	qreply(q, mp);
12997 }
12998 
12999 /*
13000  * Lookup an ipif using the sequence id (ipif_seqid)
13001  */
13002 ipif_t *
13003 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13004 {
13005 	ipif_t *ipif;
13006 
13007 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13008 
13009 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13010 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13011 			return (ipif);
13012 	}
13013 	return (NULL);
13014 }
13015 
13016 /*
13017  * Assign a unique id for the ipif. This is used later when we send
13018  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13019  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13020  * IRE is added, we verify that ipif has not disappeared.
13021  */
13022 
13023 static void
13024 ipif_assign_seqid(ipif_t *ipif)
13025 {
13026 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13027 
13028 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
13029 }
13030 
13031 /*
13032  * Clone the contents of `sipif' to `dipif'.  Requires that both ipifs are
13033  * administratively down (i.e., no DAD), of the same type, and locked.  Note
13034  * that the clone is complete -- including the seqid -- and the expectation is
13035  * that the caller will either free or overwrite `sipif' before it's unlocked.
13036  */
13037 static void
13038 ipif_clone(const ipif_t *sipif, ipif_t *dipif)
13039 {
13040 	ASSERT(MUTEX_HELD(&sipif->ipif_ill->ill_lock));
13041 	ASSERT(MUTEX_HELD(&dipif->ipif_ill->ill_lock));
13042 	ASSERT(!(sipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
13043 	ASSERT(!(dipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
13044 	ASSERT(sipif->ipif_ire_type == dipif->ipif_ire_type);
13045 	ASSERT(sipif->ipif_arp_del_mp == NULL);
13046 	ASSERT(dipif->ipif_arp_del_mp == NULL);
13047 	ASSERT(sipif->ipif_igmp_rpt == NULL);
13048 	ASSERT(dipif->ipif_igmp_rpt == NULL);
13049 	ASSERT(sipif->ipif_multicast_up == 0);
13050 	ASSERT(dipif->ipif_multicast_up == 0);
13051 	ASSERT(sipif->ipif_joined_allhosts == 0);
13052 	ASSERT(dipif->ipif_joined_allhosts == 0);
13053 
13054 	dipif->ipif_mtu = sipif->ipif_mtu;
13055 	dipif->ipif_flags = sipif->ipif_flags;
13056 	dipif->ipif_metric = sipif->ipif_metric;
13057 	dipif->ipif_zoneid = sipif->ipif_zoneid;
13058 	dipif->ipif_v6subnet = sipif->ipif_v6subnet;
13059 	dipif->ipif_v6lcl_addr = sipif->ipif_v6lcl_addr;
13060 	dipif->ipif_v6src_addr = sipif->ipif_v6src_addr;
13061 	dipif->ipif_v6net_mask = sipif->ipif_v6net_mask;
13062 	dipif->ipif_v6brd_addr = sipif->ipif_v6brd_addr;
13063 	dipif->ipif_v6pp_dst_addr = sipif->ipif_v6pp_dst_addr;
13064 
13065 	/*
13066 	 * While dipif is down right now, it might've been up before.  Since
13067 	 * it's changing identity, its packet counters need to be reset.
13068 	 */
13069 	dipif->ipif_ib_pkt_count = 0;
13070 	dipif->ipif_ob_pkt_count = 0;
13071 	dipif->ipif_fo_pkt_count = 0;
13072 
13073 	/*
13074 	 * As per the comment atop the function, we assume that these sipif
13075 	 * fields will be changed before sipif is unlocked.
13076 	 */
13077 	dipif->ipif_seqid = sipif->ipif_seqid;
13078 	dipif->ipif_saved_ire_mp = sipif->ipif_saved_ire_mp;
13079 	dipif->ipif_saved_ire_cnt = sipif->ipif_saved_ire_cnt;
13080 	dipif->ipif_state_flags = sipif->ipif_state_flags;
13081 }
13082 
13083 /*
13084  * Transfer the contents of `sipif' to `dipif', and then free (if `virgipif'
13085  * is NULL) or overwrite `sipif' with `virgipif', which must be a virgin
13086  * (unreferenced) ipif.  Also, if `sipif' is used by the current xop, then
13087  * transfer the xop to `dipif'.  Requires that all ipifs are administratively
13088  * down (i.e., no DAD), of the same type, and unlocked.
13089  */
13090 static void
13091 ipif_transfer(ipif_t *sipif, ipif_t *dipif, ipif_t *virgipif)
13092 {
13093 	ipsq_t *ipsq = sipif->ipif_ill->ill_phyint->phyint_ipsq;
13094 	int ipx_current_ioctl;
13095 
13096 	ASSERT(sipif != dipif);
13097 	ASSERT(sipif != virgipif);
13098 
13099 	/*
13100 	 * Grab all of the locks that protect the ipif in a defined order.
13101 	 */
13102 	GRAB_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
13103 	if (sipif > dipif) {
13104 		mutex_enter(&sipif->ipif_saved_ire_lock);
13105 		mutex_enter(&dipif->ipif_saved_ire_lock);
13106 	} else {
13107 		mutex_enter(&dipif->ipif_saved_ire_lock);
13108 		mutex_enter(&sipif->ipif_saved_ire_lock);
13109 	}
13110 
13111 	ipif_clone(sipif, dipif);
13112 	if (virgipif != NULL) {
13113 		ipif_clone(virgipif, sipif);
13114 		mi_free(virgipif);
13115 	}
13116 
13117 	mutex_exit(&sipif->ipif_saved_ire_lock);
13118 	mutex_exit(&dipif->ipif_saved_ire_lock);
13119 	RELEASE_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
13120 
13121 	/*
13122 	 * Transfer ownership of the current xop, if necessary.
13123 	 */
13124 	if (ipsq->ipsq_xop->ipx_current_ipif == sipif) {
13125 		ASSERT(ipsq->ipsq_xop->ipx_pending_ipif == NULL);
13126 		ipx_current_ioctl = ipsq->ipsq_xop->ipx_current_ioctl;
13127 		ipsq_current_finish(ipsq);
13128 		ipsq_current_start(ipsq, dipif, ipx_current_ioctl);
13129 	}
13130 
13131 	if (virgipif == NULL)
13132 		mi_free(sipif);
13133 }
13134 
13135 /*
13136  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13137  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13138  * be inserted into the first space available in the list. The value of
13139  * ipif_id will then be set to the appropriate value for its position.
13140  */
13141 static int
13142 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock)
13143 {
13144 	ill_t *ill;
13145 	ipif_t *tipif;
13146 	ipif_t **tipifp;
13147 	int id;
13148 	ip_stack_t	*ipst;
13149 
13150 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13151 	    IAM_WRITER_IPIF(ipif));
13152 
13153 	ill = ipif->ipif_ill;
13154 	ASSERT(ill != NULL);
13155 	ipst = ill->ill_ipst;
13156 
13157 	/*
13158 	 * In the case of lo0:0 we already hold the ill_g_lock.
13159 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13160 	 * ipif_insert.
13161 	 */
13162 	if (acquire_g_lock)
13163 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13164 	mutex_enter(&ill->ill_lock);
13165 	id = ipif->ipif_id;
13166 	tipifp = &(ill->ill_ipif);
13167 	if (id == -1) {	/* need to find a real id */
13168 		id = 0;
13169 		while ((tipif = *tipifp) != NULL) {
13170 			ASSERT(tipif->ipif_id >= id);
13171 			if (tipif->ipif_id != id)
13172 				break; /* non-consecutive id */
13173 			id++;
13174 			tipifp = &(tipif->ipif_next);
13175 		}
13176 		/* limit number of logical interfaces */
13177 		if (id >= ipst->ips_ip_addrs_per_if) {
13178 			mutex_exit(&ill->ill_lock);
13179 			if (acquire_g_lock)
13180 				rw_exit(&ipst->ips_ill_g_lock);
13181 			return (-1);
13182 		}
13183 		ipif->ipif_id = id; /* assign new id */
13184 	} else if (id < ipst->ips_ip_addrs_per_if) {
13185 		/* we have a real id; insert ipif in the right place */
13186 		while ((tipif = *tipifp) != NULL) {
13187 			ASSERT(tipif->ipif_id != id);
13188 			if (tipif->ipif_id > id)
13189 				break; /* found correct location */
13190 			tipifp = &(tipif->ipif_next);
13191 		}
13192 	} else {
13193 		mutex_exit(&ill->ill_lock);
13194 		if (acquire_g_lock)
13195 			rw_exit(&ipst->ips_ill_g_lock);
13196 		return (-1);
13197 	}
13198 
13199 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13200 
13201 	ipif->ipif_next = tipif;
13202 	*tipifp = ipif;
13203 	mutex_exit(&ill->ill_lock);
13204 	if (acquire_g_lock)
13205 		rw_exit(&ipst->ips_ill_g_lock);
13206 
13207 	return (0);
13208 }
13209 
13210 static void
13211 ipif_remove(ipif_t *ipif)
13212 {
13213 	ipif_t	**ipifp;
13214 	ill_t	*ill = ipif->ipif_ill;
13215 
13216 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13217 
13218 	mutex_enter(&ill->ill_lock);
13219 	ipifp = &ill->ill_ipif;
13220 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13221 		if (*ipifp == ipif) {
13222 			*ipifp = ipif->ipif_next;
13223 			break;
13224 		}
13225 	}
13226 	mutex_exit(&ill->ill_lock);
13227 }
13228 
13229 /*
13230  * Allocate and initialize a new interface control structure.  (Always
13231  * called as writer.)
13232  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13233  * is not part of the global linked list of ills. ipif_seqid is unique
13234  * in the system and to preserve the uniqueness, it is assigned only
13235  * when ill becomes part of the global list. At that point ill will
13236  * have a name. If it doesn't get assigned here, it will get assigned
13237  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13238  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13239  * the interface flags or any other information from the DL_INFO_ACK for
13240  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13241  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13242  * second DL_INFO_ACK comes in from the driver.
13243  */
13244 static ipif_t *
13245 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize,
13246     boolean_t insert)
13247 {
13248 	ipif_t	*ipif;
13249 	phyint_t *phyi = ill->ill_phyint;
13250 	ip_stack_t *ipst = ill->ill_ipst;
13251 
13252 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13253 	    ill->ill_name, id, (void *)ill));
13254 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13255 
13256 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13257 		return (NULL);
13258 	*ipif = ipif_zero;	/* start clean */
13259 
13260 	ipif->ipif_ill = ill;
13261 	ipif->ipif_id = id;	/* could be -1 */
13262 	/*
13263 	 * Inherit the zoneid from the ill; for the shared stack instance
13264 	 * this is always the global zone
13265 	 */
13266 	ipif->ipif_zoneid = ill->ill_zoneid;
13267 
13268 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13269 
13270 	ipif->ipif_refcnt = 0;
13271 	ipif->ipif_saved_ire_cnt = 0;
13272 
13273 	if (insert) {
13274 		if (ipif_insert(ipif, ire_type != IRE_LOOPBACK) != 0) {
13275 			mi_free(ipif);
13276 			return (NULL);
13277 		}
13278 		/* -1 id should have been replaced by real id */
13279 		id = ipif->ipif_id;
13280 		ASSERT(id >= 0);
13281 	}
13282 
13283 	if (ill->ill_name[0] != '\0')
13284 		ipif_assign_seqid(ipif);
13285 
13286 	/*
13287 	 * If this is ipif zero, configure ill/phyint-wide information.
13288 	 * Defer most configuration until we're guaranteed we're attached.
13289 	 */
13290 	if (id == 0) {
13291 		if (ill->ill_mactype == SUNW_DL_IPMP) {
13292 			/*
13293 			 * Set PHYI_IPMP and also set PHYI_FAILED since there
13294 			 * are no active interfaces.  Similarly, PHYI_RUNNING
13295 			 * isn't set until the group has an active interface.
13296 			 */
13297 			mutex_enter(&phyi->phyint_lock);
13298 			phyi->phyint_flags |= (PHYI_IPMP | PHYI_FAILED);
13299 			mutex_exit(&phyi->phyint_lock);
13300 
13301 			/*
13302 			 * Create the illgrp (which must not exist yet because
13303 			 * the zeroth ipif is created once per ill).  However,
13304 			 * do not not link it to the ipmp_grp_t until I_PLINK
13305 			 * is called; see ip_sioctl_plink_ipmp() for details.
13306 			 */
13307 			if (ipmp_illgrp_create(ill) == NULL) {
13308 				if (insert) {
13309 					rw_enter(&ipst->ips_ill_g_lock,
13310 					    RW_WRITER);
13311 					ipif_remove(ipif);
13312 					rw_exit(&ipst->ips_ill_g_lock);
13313 				}
13314 				mi_free(ipif);
13315 				return (NULL);
13316 			}
13317 		} else {
13318 			/*
13319 			 * By default, PHYI_RUNNING is set when the zeroth
13320 			 * ipif is created.  For other ipifs, we don't touch
13321 			 * it since DLPI notifications may have changed it.
13322 			 */
13323 			mutex_enter(&phyi->phyint_lock);
13324 			phyi->phyint_flags |= PHYI_RUNNING;
13325 			mutex_exit(&phyi->phyint_lock);
13326 		}
13327 	}
13328 
13329 	/*
13330 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13331 	 * The ipif is still not up and can't be looked up until the
13332 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13333 	 */
13334 	mutex_enter(&ill->ill_lock);
13335 	mutex_enter(&phyi->phyint_lock);
13336 
13337 	ipif->ipif_ire_type = ire_type;
13338 
13339 	if (ipif->ipif_isv6) {
13340 		ill->ill_flags |= ILLF_IPV6;
13341 	} else {
13342 		ipaddr_t inaddr_any = INADDR_ANY;
13343 
13344 		ill->ill_flags |= ILLF_IPV4;
13345 
13346 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13347 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13348 		    &ipif->ipif_v6lcl_addr);
13349 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13350 		    &ipif->ipif_v6src_addr);
13351 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13352 		    &ipif->ipif_v6subnet);
13353 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13354 		    &ipif->ipif_v6net_mask);
13355 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13356 		    &ipif->ipif_v6brd_addr);
13357 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13358 		    &ipif->ipif_v6pp_dst_addr);
13359 	}
13360 
13361 	/*
13362 	 * Don't set the interface flags etc. now, will do it in
13363 	 * ip_ll_subnet_defaults.
13364 	 */
13365 	if (!initialize)
13366 		goto out;
13367 
13368 	ipif->ipif_mtu = ill->ill_max_mtu;
13369 
13370 	/*
13371 	 * NOTE: The IPMP meta-interface is special-cased because it starts
13372 	 * with no underlying interfaces (and thus an unknown broadcast
13373 	 * address length), but all interfaces that can be placed into an IPMP
13374 	 * group are required to be broadcast-capable.
13375 	 */
13376 	if (ill->ill_bcast_addr_length != 0 || IS_IPMP(ill)) {
13377 		/*
13378 		 * Later detect lack of DLPI driver multicast
13379 		 * capability by catching DL_ENABMULTI errors in
13380 		 * ip_rput_dlpi.
13381 		 */
13382 		ill->ill_flags |= ILLF_MULTICAST;
13383 		if (!ipif->ipif_isv6)
13384 			ipif->ipif_flags |= IPIF_BROADCAST;
13385 	} else {
13386 		if (ill->ill_net_type != IRE_LOOPBACK) {
13387 			if (ipif->ipif_isv6)
13388 				/*
13389 				 * Note: xresolv interfaces will eventually need
13390 				 * NOARP set here as well, but that will require
13391 				 * those external resolvers to have some
13392 				 * knowledge of that flag and act appropriately.
13393 				 * Not to be changed at present.
13394 				 */
13395 				ill->ill_flags |= ILLF_NONUD;
13396 			else
13397 				ill->ill_flags |= ILLF_NOARP;
13398 		}
13399 		if (ill->ill_phys_addr_length == 0) {
13400 			if (ill->ill_mactype == SUNW_DL_VNI) {
13401 				ipif->ipif_flags |= IPIF_NOXMIT;
13402 				phyi->phyint_flags |= PHYI_VIRTUAL;
13403 			} else {
13404 				/* pt-pt supports multicast. */
13405 				ill->ill_flags |= ILLF_MULTICAST;
13406 				if (ill->ill_net_type == IRE_LOOPBACK) {
13407 					phyi->phyint_flags |=
13408 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13409 				} else {
13410 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13411 				}
13412 			}
13413 		}
13414 	}
13415 out:
13416 	mutex_exit(&phyi->phyint_lock);
13417 	mutex_exit(&ill->ill_lock);
13418 	return (ipif);
13419 }
13420 
13421 /*
13422  * If appropriate, send a message up to the resolver delete the entry
13423  * for the address of this interface which is going out of business.
13424  * (Always called as writer).
13425  *
13426  * NOTE : We need to check for NULL mps as some of the fields are
13427  *	  initialized only for some interface types. See ipif_resolver_up()
13428  *	  for details.
13429  */
13430 void
13431 ipif_resolver_down(ipif_t *ipif)
13432 {
13433 	mblk_t	*mp;
13434 	ill_t	*ill = ipif->ipif_ill;
13435 
13436 	ip1dbg(("ipif_resolver_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13437 	ASSERT(IAM_WRITER_IPIF(ipif));
13438 
13439 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13440 		return;
13441 
13442 	/* Delete the mapping for the local address */
13443 	mp = ipif->ipif_arp_del_mp;
13444 	if (mp != NULL) {
13445 		ip1dbg(("ipif_resolver_down: arp cmd %x for %s:%u\n",
13446 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13447 		putnext(ill->ill_rq, mp);
13448 		ipif->ipif_arp_del_mp = NULL;
13449 	}
13450 
13451 	/*
13452 	 * Make IPMP aware of the deleted data address.
13453 	 */
13454 	if (IS_IPMP(ill))
13455 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
13456 
13457 	/*
13458 	 * If this is the last ipif that is going down and there are no
13459 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13460 	 * clean up ARP completely.
13461 	 */
13462 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13463 		/*
13464 		 * If this was the last ipif on an IPMP interface, purge any
13465 		 * IPMP ARP entries associated with it.
13466 		 */
13467 		if (IS_IPMP(ill))
13468 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
13469 
13470 		/* Send up AR_INTERFACE_DOWN message */
13471 		mp = ill->ill_arp_down_mp;
13472 		if (mp != NULL) {
13473 			ip1dbg(("ipif_resolver_down: arp cmd %x for %s:%u\n",
13474 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13475 			    ipif->ipif_id));
13476 			putnext(ill->ill_rq, mp);
13477 			ill->ill_arp_down_mp = NULL;
13478 		}
13479 
13480 		/* Tell ARP to delete the multicast mappings */
13481 		mp = ill->ill_arp_del_mapping_mp;
13482 		if (mp != NULL) {
13483 			ip1dbg(("ipif_resolver_down: arp cmd %x for %s:%u\n",
13484 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13485 			    ipif->ipif_id));
13486 			putnext(ill->ill_rq, mp);
13487 			ill->ill_arp_del_mapping_mp = NULL;
13488 		}
13489 	}
13490 }
13491 
13492 /*
13493  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13494  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13495  * that it wants the add_mp allocated in this function to be returned
13496  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13497  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13498  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13499  * as it does a ipif_arp_down after calling this function - which will
13500  * remove what we add here.
13501  *
13502  * Returns -1 on failures and 0 on success.
13503  */
13504 int
13505 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13506 {
13507 	mblk_t	*del_mp = NULL;
13508 	mblk_t *add_mp = NULL;
13509 	mblk_t *mp;
13510 	ill_t	*ill = ipif->ipif_ill;
13511 	phyint_t *phyi = ill->ill_phyint;
13512 	ipaddr_t addr, mask, extract_mask = 0;
13513 	arma_t	*arma;
13514 	uint8_t *maddr, *bphys_addr;
13515 	uint32_t hw_start;
13516 	dl_unitdata_req_t *dlur;
13517 
13518 	ASSERT(IAM_WRITER_IPIF(ipif));
13519 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13520 		return (0);
13521 
13522 	/*
13523 	 * IPMP meta-interfaces don't have any inherent multicast mappings,
13524 	 * and instead use the ones on the underlying interfaces.
13525 	 */
13526 	if (IS_IPMP(ill))
13527 		return (0);
13528 
13529 	/*
13530 	 * Delete the existing mapping from ARP. Normally ipif_down
13531 	 * -> ipif_arp_down should send this up to ARP. The only
13532 	 * reason we would find this when we are switching from
13533 	 * Multicast to Broadcast where we did not do a down.
13534 	 */
13535 	mp = ill->ill_arp_del_mapping_mp;
13536 	if (mp != NULL) {
13537 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13538 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13539 		putnext(ill->ill_rq, mp);
13540 		ill->ill_arp_del_mapping_mp = NULL;
13541 	}
13542 
13543 	if (arp_add_mapping_mp != NULL)
13544 		*arp_add_mapping_mp = NULL;
13545 
13546 	/*
13547 	 * Check that the address is not to long for the constant
13548 	 * length reserved in the template arma_t.
13549 	 */
13550 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13551 		return (-1);
13552 
13553 	/* Add mapping mblk */
13554 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13555 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13556 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13557 	    (caddr_t)&addr);
13558 	if (add_mp == NULL)
13559 		return (-1);
13560 	arma = (arma_t *)add_mp->b_rptr;
13561 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13562 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13563 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13564 
13565 	/*
13566 	 * Determine the broadcast address.
13567 	 */
13568 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13569 	if (ill->ill_sap_length < 0)
13570 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13571 	else
13572 		bphys_addr = (uchar_t *)dlur +
13573 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13574 	/*
13575 	 * Check PHYI_MULTI_BCAST and length of physical
13576 	 * address to determine if we use the mapping or the
13577 	 * broadcast address.
13578 	 */
13579 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13580 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13581 		    bphys_addr, maddr, &hw_start, &extract_mask))
13582 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13583 
13584 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13585 	    (ill->ill_flags & ILLF_MULTICAST)) {
13586 		/* Make sure this will not match the "exact" entry. */
13587 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13588 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13589 		    (caddr_t)&addr);
13590 		if (del_mp == NULL) {
13591 			freemsg(add_mp);
13592 			return (-1);
13593 		}
13594 		bcopy(&extract_mask, (char *)arma +
13595 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13596 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13597 			/* Use link-layer broadcast address for MULTI_BCAST */
13598 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13599 			ip2dbg(("ipif_arp_setup_multicast: adding"
13600 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13601 		} else {
13602 			arma->arma_hw_mapping_start = hw_start;
13603 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13604 			    " ARP setup for %s\n", ill->ill_name));
13605 		}
13606 	} else {
13607 		freemsg(add_mp);
13608 		ASSERT(del_mp == NULL);
13609 		/* It is neither MULTICAST nor MULTI_BCAST */
13610 		return (0);
13611 	}
13612 	ASSERT(add_mp != NULL && del_mp != NULL);
13613 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13614 	ill->ill_arp_del_mapping_mp = del_mp;
13615 	if (arp_add_mapping_mp != NULL) {
13616 		/* The caller just wants the mblks allocated */
13617 		*arp_add_mapping_mp = add_mp;
13618 	} else {
13619 		/* The caller wants us to send it to arp */
13620 		putnext(ill->ill_rq, add_mp);
13621 	}
13622 	return (0);
13623 }
13624 
13625 /*
13626  * Get the resolver set up for a new IP address.  (Always called as writer.)
13627  * Called both for IPv4 and IPv6 interfaces, though it only sets up the
13628  * resolver for v6 if it's an ILLF_XRESOLV interface.  Honors ILLF_NOARP.
13629  *
13630  * The enumerated value res_act tunes the behavior:
13631  * 	* Res_act_initial: set up all the resolver structures for a new
13632  *	  IP address.
13633  *	* Res_act_defend: tell ARP that it needs to send a single gratuitous
13634  *	  ARP message in defense of the address.
13635  *	* Res_act_rebind: tell ARP to change the hardware address for an IP
13636  *	  address (and issue gratuitous ARPs).  Used by ipmp_ill_bind_ipif().
13637  *
13638  * Returns error on failure.
13639  */
13640 int
13641 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13642 {
13643 	mblk_t	*arp_up_mp = NULL;
13644 	mblk_t	*arp_down_mp = NULL;
13645 	mblk_t	*arp_add_mp = NULL;
13646 	mblk_t	*arp_del_mp = NULL;
13647 	mblk_t	*arp_add_mapping_mp = NULL;
13648 	mblk_t	*arp_del_mapping_mp = NULL;
13649 	ill_t	*ill = ipif->ipif_ill;
13650 	int	err = ENOMEM;
13651 	boolean_t added_ipif = B_FALSE;
13652 	boolean_t publish;
13653 	boolean_t was_dup;
13654 
13655 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13656 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13657 	ASSERT(IAM_WRITER_IPIF(ipif));
13658 
13659 	was_dup = B_FALSE;
13660 	if (res_act == Res_act_initial) {
13661 		ipif->ipif_addr_ready = 0;
13662 		/*
13663 		 * We're bringing an interface up here.  There's no way that we
13664 		 * should need to shut down ARP now.
13665 		 */
13666 		mutex_enter(&ill->ill_lock);
13667 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13668 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13669 			ill->ill_ipif_dup_count--;
13670 			was_dup = B_TRUE;
13671 		}
13672 		mutex_exit(&ill->ill_lock);
13673 	}
13674 	if (ipif->ipif_recovery_id != 0)
13675 		(void) untimeout(ipif->ipif_recovery_id);
13676 	ipif->ipif_recovery_id = 0;
13677 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13678 		ipif->ipif_addr_ready = 1;
13679 		return (0);
13680 	}
13681 	/* NDP will set the ipif_addr_ready flag when it's ready */
13682 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13683 		return (0);
13684 
13685 	if (ill->ill_isv6) {
13686 		/*
13687 		 * External resolver for IPv6
13688 		 */
13689 		ASSERT(res_act == Res_act_initial);
13690 		publish = !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr);
13691 	} else {
13692 		/*
13693 		 * IPv4 arp case. If the ARP stream has already started
13694 		 * closing, fail this request for ARP bringup. Else
13695 		 * record the fact that an ARP bringup is pending.
13696 		 */
13697 		mutex_enter(&ill->ill_lock);
13698 		if (ill->ill_arp_closing) {
13699 			mutex_exit(&ill->ill_lock);
13700 			err = EINVAL;
13701 			goto failed;
13702 		} else {
13703 			if (ill->ill_ipif_up_count == 0 &&
13704 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13705 				ill->ill_arp_bringup_pending = 1;
13706 			mutex_exit(&ill->ill_lock);
13707 		}
13708 		publish = (ipif->ipif_lcl_addr != INADDR_ANY);
13709 	}
13710 
13711 	if (IS_IPMP(ill) && publish) {
13712 		/*
13713 		 * If we're here via ipif_up(), then the ipif won't be bound
13714 		 * yet -- add it to the group, which will bind it if possible.
13715 		 * (We would add it in ipif_up(), but deleting on failure
13716 		 * there is gruesome.)  If we're here via ipmp_ill_bind_ipif(),
13717 		 * then the ipif has already been added to the group and we
13718 		 * just need to use the binding.
13719 		 */
13720 		if (ipmp_ipif_bound_ill(ipif) == NULL) {
13721 			if (ipmp_illgrp_add_ipif(ill->ill_grp, ipif) == NULL) {
13722 				/*
13723 				 * We couldn't bind the ipif to an ill yet,
13724 				 * so we have nothing to publish.
13725 				 */
13726 				publish = B_FALSE;
13727 			}
13728 			added_ipif = B_TRUE;
13729 		}
13730 	}
13731 
13732 	/*
13733 	 * Add an entry for the local address in ARP only if it
13734 	 * is not UNNUMBERED and it is suitable for publishing.
13735 	 */
13736 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && publish) {
13737 		if (res_act == Res_act_defend) {
13738 			arp_add_mp = ipif_area_alloc(ipif, ACE_F_DEFEND);
13739 			if (arp_add_mp == NULL)
13740 				goto failed;
13741 			/*
13742 			 * If we're just defending our address now, then
13743 			 * there's no need to set up ARP multicast mappings.
13744 			 * The publish command is enough.
13745 			 */
13746 			goto done;
13747 		}
13748 
13749 		/*
13750 		 * Allocate an ARP add message and an ARP delete message (the
13751 		 * latter is saved for use when the address goes down).
13752 		 */
13753 		if ((arp_add_mp = ipif_area_alloc(ipif, 0)) == NULL)
13754 			goto failed;
13755 
13756 		if ((arp_del_mp = ipif_ared_alloc(ipif)) == NULL)
13757 			goto failed;
13758 
13759 		if (res_act != Res_act_initial)
13760 			goto arp_setup_multicast;
13761 	} else {
13762 		if (res_act != Res_act_initial)
13763 			goto done;
13764 	}
13765 	/*
13766 	 * Need to bring up ARP or setup multicast mapping only
13767 	 * when the first interface is coming UP.
13768 	 */
13769 	if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0 || was_dup)
13770 		goto done;
13771 
13772 	/*
13773 	 * Allocate an ARP down message (to be saved) and an ARP up message.
13774 	 */
13775 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13776 	if (arp_down_mp == NULL)
13777 		goto failed;
13778 
13779 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13780 	if (arp_up_mp == NULL)
13781 		goto failed;
13782 
13783 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13784 		goto done;
13785 
13786 arp_setup_multicast:
13787 	/*
13788 	 * Setup the multicast mappings. This function initializes
13789 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13790 	 * IPv6, or for the IPMP interface (since it has no link-layer).
13791 	 */
13792 	if (!ill->ill_isv6 && !IS_IPMP(ill)) {
13793 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13794 		if (err != 0)
13795 			goto failed;
13796 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13797 		ASSERT(arp_add_mapping_mp != NULL);
13798 	}
13799 done:
13800 	if (arp_up_mp != NULL) {
13801 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13802 		    ill->ill_name, ipif->ipif_id));
13803 		putnext(ill->ill_rq, arp_up_mp);
13804 		arp_up_mp = NULL;
13805 	}
13806 	if (arp_add_mp != NULL) {
13807 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13808 		    ill->ill_name, ipif->ipif_id));
13809 		/*
13810 		 * If it's an extended ARP implementation, then we'll wait to
13811 		 * hear that DAD has finished before using the interface.
13812 		 */
13813 		if (!ill->ill_arp_extend)
13814 			ipif->ipif_addr_ready = 1;
13815 		putnext(ill->ill_rq, arp_add_mp);
13816 		arp_add_mp = NULL;
13817 	} else {
13818 		ipif->ipif_addr_ready = 1;
13819 	}
13820 	if (arp_add_mapping_mp != NULL) {
13821 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13822 		    ill->ill_name, ipif->ipif_id));
13823 		putnext(ill->ill_rq, arp_add_mapping_mp);
13824 		arp_add_mapping_mp = NULL;
13825 	}
13826 
13827 	if (res_act == Res_act_initial) {
13828 		if (ill->ill_flags & ILLF_NOARP)
13829 			err = ill_arp_off(ill);
13830 		else
13831 			err = ill_arp_on(ill);
13832 		if (err != 0) {
13833 			ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n",
13834 			    err));
13835 			goto failed;
13836 		}
13837 	}
13838 
13839 	if (arp_del_mp != NULL) {
13840 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13841 		ipif->ipif_arp_del_mp = arp_del_mp;
13842 	}
13843 	if (arp_down_mp != NULL) {
13844 		ASSERT(ill->ill_arp_down_mp == NULL);
13845 		ill->ill_arp_down_mp = arp_down_mp;
13846 	}
13847 	if (arp_del_mapping_mp != NULL) {
13848 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13849 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13850 	}
13851 
13852 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
13853 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
13854 failed:
13855 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13856 	if (added_ipif)
13857 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
13858 	freemsg(arp_add_mp);
13859 	freemsg(arp_del_mp);
13860 	freemsg(arp_add_mapping_mp);
13861 	freemsg(arp_up_mp);
13862 	freemsg(arp_down_mp);
13863 	ill->ill_arp_bringup_pending = 0;
13864 	return (err);
13865 }
13866 
13867 /*
13868  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
13869  * just gone back up.
13870  */
13871 static void
13872 ipif_arp_start_dad(ipif_t *ipif)
13873 {
13874 	ill_t *ill = ipif->ipif_ill;
13875 	mblk_t *arp_add_mp;
13876 
13877 	/* ACE_F_UNVERIFIED restarts DAD */
13878 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
13879 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13880 	    ipif->ipif_lcl_addr == INADDR_ANY ||
13881 	    (arp_add_mp = ipif_area_alloc(ipif, ACE_F_UNVERIFIED)) == NULL) {
13882 		/*
13883 		 * If we can't contact ARP for some reason, that's not really a
13884 		 * problem.  Just send out the routing socket notification that
13885 		 * DAD completion would have done, and continue.
13886 		 */
13887 		ipif_mask_reply(ipif);
13888 		ipif_up_notify(ipif);
13889 		ipif->ipif_addr_ready = 1;
13890 		return;
13891 	}
13892 
13893 	putnext(ill->ill_rq, arp_add_mp);
13894 }
13895 
13896 static void
13897 ipif_ndp_start_dad(ipif_t *ipif)
13898 {
13899 	nce_t *nce;
13900 
13901 	nce = ndp_lookup_v6(ipif->ipif_ill, B_TRUE, &ipif->ipif_v6lcl_addr,
13902 	    B_FALSE);
13903 	if (nce == NULL)
13904 		return;
13905 
13906 	if (!ndp_restart_dad(nce)) {
13907 		/*
13908 		 * If we can't restart DAD for some reason, that's not really a
13909 		 * problem.  Just send out the routing socket notification that
13910 		 * DAD completion would have done, and continue.
13911 		 */
13912 		ipif_up_notify(ipif);
13913 		ipif->ipif_addr_ready = 1;
13914 	}
13915 	NCE_REFRELE(nce);
13916 }
13917 
13918 /*
13919  * Restart duplicate address detection on all interfaces on the given ill.
13920  *
13921  * This is called when an interface transitions from down to up
13922  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
13923  *
13924  * Note that since the underlying physical link has transitioned, we must cause
13925  * at least one routing socket message to be sent here, either via DAD
13926  * completion or just by default on the first ipif.  (If we don't do this, then
13927  * in.mpathd will see long delays when doing link-based failure recovery.)
13928  */
13929 void
13930 ill_restart_dad(ill_t *ill, boolean_t went_up)
13931 {
13932 	ipif_t *ipif;
13933 
13934 	if (ill == NULL)
13935 		return;
13936 
13937 	/*
13938 	 * If layer two doesn't support duplicate address detection, then just
13939 	 * send the routing socket message now and be done with it.
13940 	 */
13941 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
13942 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
13943 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
13944 		return;
13945 	}
13946 
13947 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13948 		if (went_up) {
13949 			if (ipif->ipif_flags & IPIF_UP) {
13950 				if (ill->ill_isv6)
13951 					ipif_ndp_start_dad(ipif);
13952 				else
13953 					ipif_arp_start_dad(ipif);
13954 			} else if (ill->ill_isv6 &&
13955 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
13956 				/*
13957 				 * For IPv4, the ARP module itself will
13958 				 * automatically start the DAD process when it
13959 				 * sees DL_NOTE_LINK_UP.  We respond to the
13960 				 * AR_CN_READY at the completion of that task.
13961 				 * For IPv6, we must kick off the bring-up
13962 				 * process now.
13963 				 */
13964 				ndp_do_recovery(ipif);
13965 			} else {
13966 				/*
13967 				 * Unfortunately, the first ipif is "special"
13968 				 * and represents the underlying ill in the
13969 				 * routing socket messages.  Thus, when this
13970 				 * one ipif is down, we must still notify so
13971 				 * that the user knows the IFF_RUNNING status
13972 				 * change.  (If the first ipif is up, then
13973 				 * we'll handle eventual routing socket
13974 				 * notification via DAD completion.)
13975 				 */
13976 				if (ipif == ill->ill_ipif) {
13977 					ip_rts_ifmsg(ill->ill_ipif,
13978 					    RTSQ_DEFAULT);
13979 				}
13980 			}
13981 		} else {
13982 			/*
13983 			 * After link down, we'll need to send a new routing
13984 			 * message when the link comes back, so clear
13985 			 * ipif_addr_ready.
13986 			 */
13987 			ipif->ipif_addr_ready = 0;
13988 		}
13989 	}
13990 
13991 	/*
13992 	 * If we've torn down links, then notify the user right away.
13993 	 */
13994 	if (!went_up)
13995 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
13996 }
13997 
13998 static void
13999 ipsq_delete(ipsq_t *ipsq)
14000 {
14001 	ipxop_t *ipx = ipsq->ipsq_xop;
14002 
14003 	ipsq->ipsq_ipst = NULL;
14004 	ASSERT(ipsq->ipsq_phyint == NULL);
14005 	ASSERT(ipsq->ipsq_xop != NULL);
14006 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipx->ipx_mphead == NULL);
14007 	ASSERT(ipx->ipx_pending_mp == NULL);
14008 	kmem_free(ipsq, sizeof (ipsq_t));
14009 }
14010 
14011 static int
14012 ill_up_ipifs_on_ill(ill_t *ill, queue_t *q, mblk_t *mp)
14013 {
14014 	int err;
14015 	ipif_t *ipif;
14016 
14017 	if (ill == NULL)
14018 		return (0);
14019 
14020 	/*
14021 	 * Except for ipif_state_flags and ill_state_flags the other
14022 	 * fields of the ipif/ill that are modified below are protected
14023 	 * implicitly since we are a writer. We would have tried to down
14024 	 * even an ipif that was already down, in ill_down_ipifs. So we
14025 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14026 	 */
14027 	ASSERT(IAM_WRITER_ILL(ill));
14028 
14029 	ill->ill_up_ipifs = B_TRUE;
14030 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14031 		mutex_enter(&ill->ill_lock);
14032 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
14033 		mutex_exit(&ill->ill_lock);
14034 		if (ipif->ipif_was_up) {
14035 			if (!(ipif->ipif_flags & IPIF_UP))
14036 				err = ipif_up(ipif, q, mp);
14037 			ipif->ipif_was_up = B_FALSE;
14038 			if (err != 0) {
14039 				ASSERT(err == EINPROGRESS);
14040 				return (err);
14041 			}
14042 		}
14043 	}
14044 	mutex_enter(&ill->ill_lock);
14045 	ill->ill_state_flags &= ~ILL_CHANGING;
14046 	mutex_exit(&ill->ill_lock);
14047 	ill->ill_up_ipifs = B_FALSE;
14048 	return (0);
14049 }
14050 
14051 /*
14052  * This function is called to bring up all the ipifs that were up before
14053  * bringing the ill down via ill_down_ipifs().
14054  */
14055 int
14056 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14057 {
14058 	int err;
14059 
14060 	ASSERT(IAM_WRITER_ILL(ill));
14061 
14062 	err = ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv4, q, mp);
14063 	if (err != 0)
14064 		return (err);
14065 
14066 	return (ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv6, q, mp));
14067 }
14068 
14069 /*
14070  * Bring down any IPIF_UP ipifs on ill.
14071  */
14072 static void
14073 ill_down_ipifs(ill_t *ill)
14074 {
14075 	ipif_t *ipif;
14076 
14077 	ASSERT(IAM_WRITER_ILL(ill));
14078 
14079 	/*
14080 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14081 	 * are modified below are protected implicitly since we are a writer
14082 	 */
14083 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14084 		/*
14085 		 * We go through the ipif_down logic even if the ipif
14086 		 * is already down, since routes can be added based
14087 		 * on down ipifs. Going through ipif_down once again
14088 		 * will delete any IREs created based on these routes.
14089 		 */
14090 		if (ipif->ipif_flags & IPIF_UP)
14091 			ipif->ipif_was_up = B_TRUE;
14092 
14093 		mutex_enter(&ill->ill_lock);
14094 		ipif->ipif_state_flags |= IPIF_CHANGING;
14095 		mutex_exit(&ill->ill_lock);
14096 
14097 		/*
14098 		 * Need to re-create net/subnet bcast ires if
14099 		 * they are dependent on ipif.
14100 		 */
14101 		if (!ipif->ipif_isv6)
14102 			ipif_check_bcast_ires(ipif);
14103 		(void) ipif_logical_down(ipif, NULL, NULL);
14104 		ipif_non_duplicate(ipif);
14105 		ipif_down_tail(ipif);
14106 	}
14107 }
14108 
14109 void
14110 ill_lock_ills(ill_t **list, int cnt)
14111 {
14112 	int	i;
14113 
14114 	if (cnt > 1) {
14115 		boolean_t try_again;
14116 		do {
14117 			try_again = B_FALSE;
14118 			for (i = 0; i < cnt - 1; i++) {
14119 				if (list[i] < list[i + 1]) {
14120 					ill_t	*tmp;
14121 
14122 					/* swap the elements */
14123 					tmp = list[i];
14124 					list[i] = list[i + 1];
14125 					list[i + 1] = tmp;
14126 					try_again = B_TRUE;
14127 				}
14128 			}
14129 		} while (try_again);
14130 	}
14131 
14132 	for (i = 0; i < cnt; i++) {
14133 		if (i == 0) {
14134 			if (list[i] != NULL)
14135 				mutex_enter(&list[i]->ill_lock);
14136 			else
14137 				return;
14138 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14139 			mutex_enter(&list[i]->ill_lock);
14140 		}
14141 	}
14142 }
14143 
14144 void
14145 ill_unlock_ills(ill_t **list, int cnt)
14146 {
14147 	int	i;
14148 
14149 	for (i = 0; i < cnt; i++) {
14150 		if ((i == 0) && (list[i] != NULL)) {
14151 			mutex_exit(&list[i]->ill_lock);
14152 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14153 			mutex_exit(&list[i]->ill_lock);
14154 		}
14155 	}
14156 }
14157 
14158 /*
14159  * Redo source address selection.  This is called when a
14160  * non-NOLOCAL/DEPRECATED/ANYCAST ipif comes up.
14161  */
14162 void
14163 ill_update_source_selection(ill_t *ill)
14164 {
14165 	ipif_t *ipif;
14166 
14167 	ASSERT(IAM_WRITER_ILL(ill));
14168 
14169 	/*
14170 	 * Underlying interfaces are only used for test traffic and thus
14171 	 * should always send with their (deprecated) source addresses.
14172 	 */
14173 	if (IS_UNDER_IPMP(ill))
14174 		return;
14175 
14176 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14177 		if (ill->ill_isv6)
14178 			ipif_recreate_interface_routes_v6(NULL, ipif);
14179 		else
14180 			ipif_recreate_interface_routes(NULL, ipif);
14181 	}
14182 }
14183 
14184 /*
14185  * Finish the group join started in ip_sioctl_groupname().
14186  */
14187 /* ARGSUSED */
14188 static void
14189 ip_join_illgrps(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
14190 {
14191 	ill_t		*ill = q->q_ptr;
14192 	phyint_t	*phyi = ill->ill_phyint;
14193 	ipmp_grp_t	*grp = phyi->phyint_grp;
14194 	ip_stack_t	*ipst = ill->ill_ipst;
14195 
14196 	/* IS_UNDER_IPMP() won't work until ipmp_ill_join_illgrp() is called */
14197 	ASSERT(!IS_IPMP(ill) && grp != NULL);
14198 	ASSERT(IAM_WRITER_IPSQ(ipsq));
14199 
14200 	if (phyi->phyint_illv4 != NULL) {
14201 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
14202 		VERIFY(grp->gr_pendv4-- > 0);
14203 		rw_exit(&ipst->ips_ipmp_lock);
14204 		ipmp_ill_join_illgrp(phyi->phyint_illv4, grp->gr_v4);
14205 	}
14206 	if (phyi->phyint_illv6 != NULL) {
14207 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
14208 		VERIFY(grp->gr_pendv6-- > 0);
14209 		rw_exit(&ipst->ips_ipmp_lock);
14210 		ipmp_ill_join_illgrp(phyi->phyint_illv6, grp->gr_v6);
14211 	}
14212 	freemsg(mp);
14213 }
14214 
14215 /*
14216  * Process an SIOCSLIFGROUPNAME request.
14217  */
14218 /* ARGSUSED */
14219 int
14220 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14221     ip_ioctl_cmd_t *ipip, void *ifreq)
14222 {
14223 	struct lifreq	*lifr = ifreq;
14224 	ill_t		*ill = ipif->ipif_ill;
14225 	ip_stack_t	*ipst = ill->ill_ipst;
14226 	phyint_t	*phyi = ill->ill_phyint;
14227 	ipmp_grp_t	*grp = phyi->phyint_grp;
14228 	mblk_t		*ipsq_mp;
14229 	int		err = 0;
14230 
14231 	/*
14232 	 * Note that phyint_grp can only change here, where we're exclusive.
14233 	 */
14234 	ASSERT(IAM_WRITER_ILL(ill));
14235 
14236 	if (ipif->ipif_id != 0 || ill->ill_usesrc_grp_next != NULL ||
14237 	    (phyi->phyint_flags & PHYI_VIRTUAL))
14238 		return (EINVAL);
14239 
14240 	lifr->lifr_groupname[LIFGRNAMSIZ - 1] = '\0';
14241 
14242 	rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
14243 
14244 	/*
14245 	 * If the name hasn't changed, there's nothing to do.
14246 	 */
14247 	if (grp != NULL && strcmp(grp->gr_name, lifr->lifr_groupname) == 0)
14248 		goto unlock;
14249 
14250 	/*
14251 	 * Handle requests to rename an IPMP meta-interface.
14252 	 *
14253 	 * Note that creation of the IPMP meta-interface is handled in
14254 	 * userland through the standard plumbing sequence.  As part of the
14255 	 * plumbing the IPMP meta-interface, its initial groupname is set to
14256 	 * the name of the interface (see ipif_set_values_tail()).
14257 	 */
14258 	if (IS_IPMP(ill)) {
14259 		err = ipmp_grp_rename(grp, lifr->lifr_groupname);
14260 		goto unlock;
14261 	}
14262 
14263 	/*
14264 	 * Handle requests to add or remove an IP interface from a group.
14265 	 */
14266 	if (lifr->lifr_groupname[0] != '\0') {			/* add */
14267 		/*
14268 		 * Moves are handled by first removing the interface from
14269 		 * its existing group, and then adding it to another group.
14270 		 * So, fail if it's already in a group.
14271 		 */
14272 		if (IS_UNDER_IPMP(ill)) {
14273 			err = EALREADY;
14274 			goto unlock;
14275 		}
14276 
14277 		grp = ipmp_grp_lookup(lifr->lifr_groupname, ipst);
14278 		if (grp == NULL) {
14279 			err = ENOENT;
14280 			goto unlock;
14281 		}
14282 
14283 		/*
14284 		 * Check if the phyint and its ills are suitable for
14285 		 * inclusion into the group.
14286 		 */
14287 		if ((err = ipmp_grp_vet_phyint(grp, phyi)) != 0)
14288 			goto unlock;
14289 
14290 		/*
14291 		 * Checks pass; join the group, and enqueue the remaining
14292 		 * illgrp joins for when we've become part of the group xop
14293 		 * and are exclusive across its IPSQs.  Since qwriter_ip()
14294 		 * requires an mblk_t to scribble on, and since `mp' will be
14295 		 * freed as part of completing the ioctl, allocate another.
14296 		 */
14297 		if ((ipsq_mp = allocb(0, BPRI_MED)) == NULL) {
14298 			err = ENOMEM;
14299 			goto unlock;
14300 		}
14301 
14302 		/*
14303 		 * Before we drop ipmp_lock, bump gr_pend* to ensure that the
14304 		 * IPMP meta-interface ills needed by `phyi' cannot go away
14305 		 * before ip_join_illgrps() is called back.  See the comments
14306 		 * in ip_sioctl_plink_ipmp() for more.
14307 		 */
14308 		if (phyi->phyint_illv4 != NULL)
14309 			grp->gr_pendv4++;
14310 		if (phyi->phyint_illv6 != NULL)
14311 			grp->gr_pendv6++;
14312 
14313 		rw_exit(&ipst->ips_ipmp_lock);
14314 
14315 		ipmp_phyint_join_grp(phyi, grp);
14316 		ill_refhold(ill);
14317 		qwriter_ip(ill, ill->ill_rq, ipsq_mp, ip_join_illgrps,
14318 		    SWITCH_OP, B_FALSE);
14319 		return (0);
14320 	} else {
14321 		/*
14322 		 * Request to remove the interface from a group.  If the
14323 		 * interface is not in a group, this trivially succeeds.
14324 		 */
14325 		rw_exit(&ipst->ips_ipmp_lock);
14326 		if (IS_UNDER_IPMP(ill))
14327 			ipmp_phyint_leave_grp(phyi);
14328 		return (0);
14329 	}
14330 unlock:
14331 	rw_exit(&ipst->ips_ipmp_lock);
14332 	return (err);
14333 }
14334 
14335 /*
14336  * Process an SIOCGLIFBINDING request.
14337  */
14338 /* ARGSUSED */
14339 int
14340 ip_sioctl_get_binding(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14341     ip_ioctl_cmd_t *ipip, void *ifreq)
14342 {
14343 	ill_t		*bound_ill;
14344 	struct lifreq	*lifr = ifreq;
14345 
14346 	if (!IS_IPMP(ipif->ipif_ill))
14347 		return (EINVAL);
14348 
14349 	if ((bound_ill = ipmp_ipif_hold_bound_ill(ipif)) == NULL) {
14350 		lifr->lifr_binding[0] = '\0';
14351 		return (0);
14352 	}
14353 
14354 	(void) strlcpy(lifr->lifr_binding, bound_ill->ill_name, LIFNAMSIZ);
14355 	ill_refrele(bound_ill);
14356 	return (0);
14357 }
14358 
14359 /*
14360  * Process an SIOCGLIFGROUPNAME request.
14361  */
14362 /* ARGSUSED */
14363 int
14364 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14365     ip_ioctl_cmd_t *ipip, void *ifreq)
14366 {
14367 	ipmp_grp_t	*grp;
14368 	struct lifreq	*lifr = ifreq;
14369 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
14370 
14371 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
14372 	if ((grp = ipif->ipif_ill->ill_phyint->phyint_grp) == NULL)
14373 		lifr->lifr_groupname[0] = '\0';
14374 	else
14375 		(void) strlcpy(lifr->lifr_groupname, grp->gr_name, LIFGRNAMSIZ);
14376 	rw_exit(&ipst->ips_ipmp_lock);
14377 	return (0);
14378 }
14379 
14380 /*
14381  * Process an SIOCGLIFGROUPINFO request.
14382  */
14383 /* ARGSUSED */
14384 int
14385 ip_sioctl_groupinfo(ipif_t *dummy_ipif, sin_t *sin, queue_t *q, mblk_t *mp,
14386     ip_ioctl_cmd_t *ipip, void *dummy)
14387 {
14388 	lifgroupinfo_t	*lifgr;
14389 	ipmp_grp_t	*grp;
14390 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
14391 
14392 	/* ip_wput_nondata() verified mp->b_cont->b_cont */
14393 	lifgr = (lifgroupinfo_t *)mp->b_cont->b_cont->b_rptr;
14394 	lifgr->gi_grname[LIFGRNAMSIZ - 1] = '\0';
14395 
14396 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
14397 	if ((grp = ipmp_grp_lookup(lifgr->gi_grname, ipst)) == NULL) {
14398 		rw_exit(&ipst->ips_ipmp_lock);
14399 		return (ENOENT);
14400 	}
14401 	ipmp_grp_info(grp, lifgr);
14402 	rw_exit(&ipst->ips_ipmp_lock);
14403 	return (0);
14404 }
14405 
14406 static void
14407 ill_dl_down(ill_t *ill)
14408 {
14409 	/*
14410 	 * The ill is down; unbind but stay attached since we're still
14411 	 * associated with a PPA. If we have negotiated DLPI capabilites
14412 	 * with the data link service provider (IDS_OK) then reset them.
14413 	 * The interval between unbinding and rebinding is potentially
14414 	 * unbounded hence we cannot assume things will be the same.
14415 	 * The DLPI capabilities will be probed again when the data link
14416 	 * is brought up.
14417 	 */
14418 	mblk_t	*mp = ill->ill_unbind_mp;
14419 
14420 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
14421 
14422 	ill->ill_unbind_mp = NULL;
14423 	if (mp != NULL) {
14424 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
14425 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
14426 		    ill->ill_name));
14427 		mutex_enter(&ill->ill_lock);
14428 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
14429 		mutex_exit(&ill->ill_lock);
14430 		/*
14431 		 * ip_rput does not pass up normal (M_PROTO) DLPI messages
14432 		 * after ILL_CONDEMNED is set. So in the unplumb case, we call
14433 		 * ill_capability_dld_disable disable rightaway. If this is not
14434 		 * an unplumb operation then the disable happens on receipt of
14435 		 * the capab ack via ip_rput_dlpi_writer ->
14436 		 * ill_capability_ack_thr. In both cases the order of
14437 		 * the operations seen by DLD is capability disable followed
14438 		 * by DL_UNBIND. Also the DLD capability disable needs a
14439 		 * cv_wait'able context.
14440 		 */
14441 		if (ill->ill_state_flags & ILL_CONDEMNED)
14442 			ill_capability_dld_disable(ill);
14443 		ill_capability_reset(ill, B_FALSE);
14444 		ill_dlpi_send(ill, mp);
14445 	}
14446 
14447 	/*
14448 	 * Toss all of our multicast memberships.  We could keep them, but
14449 	 * then we'd have to do bookkeeping of any joins and leaves performed
14450 	 * by the application while the the interface is down (we can't just
14451 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
14452 	 * on a downed interface).
14453 	 */
14454 	ill_leave_multicast(ill);
14455 
14456 	mutex_enter(&ill->ill_lock);
14457 	ill->ill_dl_up = 0;
14458 	ill_nic_event_dispatch(ill, 0, NE_DOWN, NULL, 0);
14459 	mutex_exit(&ill->ill_lock);
14460 }
14461 
14462 static void
14463 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
14464 {
14465 	union DL_primitives *dlp;
14466 	t_uscalar_t prim;
14467 
14468 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
14469 
14470 	dlp = (union DL_primitives *)mp->b_rptr;
14471 	prim = dlp->dl_primitive;
14472 
14473 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
14474 	    dl_primstr(prim), prim, ill->ill_name));
14475 
14476 	switch (prim) {
14477 	case DL_PHYS_ADDR_REQ:
14478 	{
14479 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
14480 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
14481 		break;
14482 	}
14483 	case DL_BIND_REQ:
14484 		mutex_enter(&ill->ill_lock);
14485 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
14486 		mutex_exit(&ill->ill_lock);
14487 		break;
14488 	}
14489 
14490 	/*
14491 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
14492 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
14493 	 * we only wait for the ACK of the DL_UNBIND_REQ.
14494 	 */
14495 	mutex_enter(&ill->ill_lock);
14496 	if (!(ill->ill_state_flags & ILL_CONDEMNED) || (prim == DL_UNBIND_REQ))
14497 		ill->ill_dlpi_pending = prim;
14498 
14499 	mutex_exit(&ill->ill_lock);
14500 	putnext(ill->ill_wq, mp);
14501 }
14502 
14503 /*
14504  * Helper function for ill_dlpi_send().
14505  */
14506 /* ARGSUSED */
14507 static void
14508 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
14509 {
14510 	ill_dlpi_send(q->q_ptr, mp);
14511 }
14512 
14513 /*
14514  * Send a DLPI control message to the driver but make sure there
14515  * is only one outstanding message. Uses ill_dlpi_pending to tell
14516  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
14517  * when an ACK or a NAK is received to process the next queued message.
14518  */
14519 void
14520 ill_dlpi_send(ill_t *ill, mblk_t *mp)
14521 {
14522 	mblk_t **mpp;
14523 
14524 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
14525 
14526 	/*
14527 	 * To ensure that any DLPI requests for current exclusive operation
14528 	 * are always completely sent before any DLPI messages for other
14529 	 * operations, require writer access before enqueuing.
14530 	 */
14531 	if (!IAM_WRITER_ILL(ill)) {
14532 		ill_refhold(ill);
14533 		/* qwriter_ip() does the ill_refrele() */
14534 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
14535 		    NEW_OP, B_TRUE);
14536 		return;
14537 	}
14538 
14539 	mutex_enter(&ill->ill_lock);
14540 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
14541 		/* Must queue message. Tail insertion */
14542 		mpp = &ill->ill_dlpi_deferred;
14543 		while (*mpp != NULL)
14544 			mpp = &((*mpp)->b_next);
14545 
14546 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
14547 		    ill->ill_name));
14548 
14549 		*mpp = mp;
14550 		mutex_exit(&ill->ill_lock);
14551 		return;
14552 	}
14553 	mutex_exit(&ill->ill_lock);
14554 	ill_dlpi_dispatch(ill, mp);
14555 }
14556 
14557 static void
14558 ill_capability_send(ill_t *ill, mblk_t *mp)
14559 {
14560 	ill->ill_capab_pending_cnt++;
14561 	ill_dlpi_send(ill, mp);
14562 }
14563 
14564 void
14565 ill_capability_done(ill_t *ill)
14566 {
14567 	ASSERT(ill->ill_capab_pending_cnt != 0);
14568 
14569 	ill_dlpi_done(ill, DL_CAPABILITY_REQ);
14570 
14571 	ill->ill_capab_pending_cnt--;
14572 	if (ill->ill_capab_pending_cnt == 0 &&
14573 	    ill->ill_dlpi_capab_state == IDCS_OK)
14574 		ill_capability_reset_alloc(ill);
14575 }
14576 
14577 /*
14578  * Send all deferred DLPI messages without waiting for their ACKs.
14579  */
14580 void
14581 ill_dlpi_send_deferred(ill_t *ill)
14582 {
14583 	mblk_t *mp, *nextmp;
14584 
14585 	/*
14586 	 * Clear ill_dlpi_pending so that the message is not queued in
14587 	 * ill_dlpi_send().
14588 	 */
14589 	mutex_enter(&ill->ill_lock);
14590 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
14591 	mp = ill->ill_dlpi_deferred;
14592 	ill->ill_dlpi_deferred = NULL;
14593 	mutex_exit(&ill->ill_lock);
14594 
14595 	for (; mp != NULL; mp = nextmp) {
14596 		nextmp = mp->b_next;
14597 		mp->b_next = NULL;
14598 		ill_dlpi_send(ill, mp);
14599 	}
14600 }
14601 
14602 /*
14603  * Check if the DLPI primitive `prim' is pending; print a warning if not.
14604  */
14605 boolean_t
14606 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
14607 {
14608 	t_uscalar_t pending;
14609 
14610 	mutex_enter(&ill->ill_lock);
14611 	if (ill->ill_dlpi_pending == prim) {
14612 		mutex_exit(&ill->ill_lock);
14613 		return (B_TRUE);
14614 	}
14615 
14616 	/*
14617 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
14618 	 * without waiting, so don't print any warnings in that case.
14619 	 */
14620 	if (ill->ill_state_flags & ILL_CONDEMNED) {
14621 		mutex_exit(&ill->ill_lock);
14622 		return (B_FALSE);
14623 	}
14624 	pending = ill->ill_dlpi_pending;
14625 	mutex_exit(&ill->ill_lock);
14626 
14627 	if (pending == DL_PRIM_INVAL) {
14628 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
14629 		    "received unsolicited ack for %s on %s\n",
14630 		    dl_primstr(prim), ill->ill_name);
14631 	} else {
14632 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
14633 		    "received unexpected ack for %s on %s (expecting %s)\n",
14634 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
14635 	}
14636 	return (B_FALSE);
14637 }
14638 
14639 /*
14640  * Complete the current DLPI operation associated with `prim' on `ill' and
14641  * start the next queued DLPI operation (if any).  If there are no queued DLPI
14642  * operations and the ill's current exclusive IPSQ operation has finished
14643  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
14644  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
14645  * the comments above ipsq_current_finish() for details.
14646  */
14647 void
14648 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
14649 {
14650 	mblk_t *mp;
14651 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
14652 	ipxop_t *ipx = ipsq->ipsq_xop;
14653 
14654 	ASSERT(IAM_WRITER_IPSQ(ipsq));
14655 	mutex_enter(&ill->ill_lock);
14656 
14657 	ASSERT(prim != DL_PRIM_INVAL);
14658 	ASSERT(ill->ill_dlpi_pending == prim);
14659 
14660 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
14661 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
14662 
14663 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
14664 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
14665 		if (ipx->ipx_current_done) {
14666 			mutex_enter(&ipx->ipx_lock);
14667 			ipx->ipx_current_ipif = NULL;
14668 			mutex_exit(&ipx->ipx_lock);
14669 		}
14670 		cv_signal(&ill->ill_cv);
14671 		mutex_exit(&ill->ill_lock);
14672 		return;
14673 	}
14674 
14675 	ill->ill_dlpi_deferred = mp->b_next;
14676 	mp->b_next = NULL;
14677 	mutex_exit(&ill->ill_lock);
14678 
14679 	ill_dlpi_dispatch(ill, mp);
14680 }
14681 
14682 void
14683 conn_delete_ire(conn_t *connp, caddr_t arg)
14684 {
14685 	ipif_t	*ipif = (ipif_t *)arg;
14686 	ire_t	*ire;
14687 
14688 	/*
14689 	 * Look at the cached ires on conns which has pointers to ipifs.
14690 	 * We just call ire_refrele which clears up the reference
14691 	 * to ire. Called when a conn closes. Also called from ipif_free
14692 	 * to cleanup indirect references to the stale ipif via the cached ire.
14693 	 */
14694 	mutex_enter(&connp->conn_lock);
14695 	ire = connp->conn_ire_cache;
14696 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
14697 		connp->conn_ire_cache = NULL;
14698 		mutex_exit(&connp->conn_lock);
14699 		IRE_REFRELE_NOTR(ire);
14700 		return;
14701 	}
14702 	mutex_exit(&connp->conn_lock);
14703 
14704 }
14705 
14706 /*
14707  * Some operations (e.g., ipif_down()) conditionally delete a number
14708  * of IREs. Those IREs may have been previously cached in the conn structure.
14709  * This ipcl_walk() walker function releases all references to such IREs based
14710  * on the condemned flag.
14711  */
14712 /* ARGSUSED */
14713 void
14714 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
14715 {
14716 	ire_t	*ire;
14717 
14718 	mutex_enter(&connp->conn_lock);
14719 	ire = connp->conn_ire_cache;
14720 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
14721 		connp->conn_ire_cache = NULL;
14722 		mutex_exit(&connp->conn_lock);
14723 		IRE_REFRELE_NOTR(ire);
14724 		return;
14725 	}
14726 	mutex_exit(&connp->conn_lock);
14727 }
14728 
14729 /*
14730  * Take down a specific interface, but don't lose any information about it.
14731  * (Always called as writer.)
14732  * This function goes through the down sequence even if the interface is
14733  * already down. There are 2 reasons.
14734  * a. Currently we permit interface routes that depend on down interfaces
14735  *    to be added. This behaviour itself is questionable. However it appears
14736  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
14737  *    time. We go thru the cleanup in order to remove these routes.
14738  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
14739  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
14740  *    down, but we need to cleanup i.e. do ill_dl_down and
14741  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
14742  *
14743  * IP-MT notes:
14744  *
14745  * Model of reference to interfaces.
14746  *
14747  * The following members in ipif_t track references to the ipif.
14748  *	int     ipif_refcnt;    Active reference count
14749  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
14750  *	uint_t  ipif_ilm_cnt;   Number of ilms's references this ipif.
14751  *
14752  * The following members in ill_t track references to the ill.
14753  *	int             ill_refcnt;     active refcnt
14754  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
14755  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
14756  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
14757  *
14758  * Reference to an ipif or ill can be obtained in any of the following ways.
14759  *
14760  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
14761  * Pointers to ipif / ill from other data structures viz ire and conn.
14762  * Implicit reference to the ipif / ill by holding a reference to the ire.
14763  *
14764  * The ipif/ill lookup functions return a reference held ipif / ill.
14765  * ipif_refcnt and ill_refcnt track the reference counts respectively.
14766  * This is a purely dynamic reference count associated with threads holding
14767  * references to the ipif / ill. Pointers from other structures do not
14768  * count towards this reference count.
14769  *
14770  * ipif_ire_cnt/ill_ire_cnt is the number of ire's
14771  * associated with the ipif/ill. This is incremented whenever a new
14772  * ire is created referencing the ipif/ill. This is done atomically inside
14773  * ire_add_v[46] where the ire is actually added to the ire hash table.
14774  * The count is decremented in ire_inactive where the ire is destroyed.
14775  *
14776  * nce's reference ill's thru nce_ill and the count of nce's associated with
14777  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
14778  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
14779  * table. Similarly it is decremented in ndp_inactive() where the nce
14780  * is destroyed.
14781  *
14782  * ilm's reference to the ipif (for IPv4 ilm's) or the ill (for IPv6 ilm's)
14783  * is incremented in ilm_add_v6() and decremented before the ilm is freed
14784  * in ilm_walker_cleanup() or ilm_delete().
14785  *
14786  * Flow of ioctls involving interface down/up
14787  *
14788  * The following is the sequence of an attempt to set some critical flags on an
14789  * up interface.
14790  * ip_sioctl_flags
14791  * ipif_down
14792  * wait for ipif to be quiescent
14793  * ipif_down_tail
14794  * ip_sioctl_flags_tail
14795  *
14796  * All set ioctls that involve down/up sequence would have a skeleton similar
14797  * to the above. All the *tail functions are called after the refcounts have
14798  * dropped to the appropriate values.
14799  *
14800  * The mechanism to quiesce an ipif is as follows.
14801  *
14802  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
14803  * on the ipif. Callers either pass a flag requesting wait or the lookup
14804  *  functions will return NULL.
14805  *
14806  * Delete all ires referencing this ipif
14807  *
14808  * Any thread attempting to do an ipif_refhold on an ipif that has been
14809  * obtained thru a cached pointer will first make sure that
14810  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
14811  * increment the refcount.
14812  *
14813  * The above guarantees that the ipif refcount will eventually come down to
14814  * zero and the ipif will quiesce, once all threads that currently hold a
14815  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
14816  * ipif_refcount has dropped to zero and all ire's associated with this ipif
14817  * have also been ire_inactive'd. i.e. when ipif_{ire, ill}_cnt and
14818  * ipif_refcnt both drop to zero. See also: comments above IPIF_DOWN_OK()
14819  * in ip.h
14820  *
14821  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
14822  *
14823  * Threads trying to lookup an ipif or ill can pass a flag requesting
14824  * wait and restart if the ipif / ill cannot be looked up currently.
14825  * For eg. bind, and route operations (Eg. route add / delete) cannot return
14826  * failure if the ipif is currently undergoing an exclusive operation, and
14827  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
14828  * is restarted by ipsq_exit() when the current exclusive operation completes.
14829  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
14830  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
14831  * change while the ill_lock is held. Before dropping the ill_lock we acquire
14832  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
14833  * until we release the ipsq_lock, even though the the ill/ipif state flags
14834  * can change after we drop the ill_lock.
14835  *
14836  * An attempt to send out a packet using an ipif that is currently
14837  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
14838  * operation and restart it later when the exclusive condition on the ipif ends.
14839  * This is an example of not passing the wait flag to the lookup functions. For
14840  * example an attempt to refhold and use conn->conn_multicast_ipif and send
14841  * out a multicast packet on that ipif will fail while the ipif is
14842  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
14843  * currently IPIF_CHANGING will also fail.
14844  */
14845 int
14846 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
14847 {
14848 	ill_t		*ill = ipif->ipif_ill;
14849 	conn_t		*connp;
14850 	boolean_t	success;
14851 	boolean_t	ipif_was_up = B_FALSE;
14852 	ip_stack_t	*ipst = ill->ill_ipst;
14853 
14854 	ASSERT(IAM_WRITER_IPIF(ipif));
14855 
14856 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
14857 
14858 	if (ipif->ipif_flags & IPIF_UP) {
14859 		mutex_enter(&ill->ill_lock);
14860 		ipif->ipif_flags &= ~IPIF_UP;
14861 		ASSERT(ill->ill_ipif_up_count > 0);
14862 		--ill->ill_ipif_up_count;
14863 		mutex_exit(&ill->ill_lock);
14864 		ipif_was_up = B_TRUE;
14865 		/* Update status in SCTP's list */
14866 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
14867 		ill_nic_event_dispatch(ipif->ipif_ill,
14868 		    MAP_IPIF_ID(ipif->ipif_id), NE_LIF_DOWN, NULL, 0);
14869 	}
14870 
14871 	/*
14872 	 * Blow away memberships we established in ipif_multicast_up().
14873 	 */
14874 	ipif_multicast_down(ipif);
14875 
14876 	/*
14877 	 * Remove from the mapping for __sin6_src_id. We insert only
14878 	 * when the address is not INADDR_ANY. As IPv4 addresses are
14879 	 * stored as mapped addresses, we need to check for mapped
14880 	 * INADDR_ANY also.
14881 	 */
14882 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
14883 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
14884 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14885 		int err;
14886 
14887 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
14888 		    ipif->ipif_zoneid, ipst);
14889 		if (err != 0) {
14890 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
14891 		}
14892 	}
14893 
14894 	/*
14895 	 * Delete all IRE's pointing at this ipif or its source address.
14896 	 */
14897 	if (ipif->ipif_isv6) {
14898 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
14899 		    ipst);
14900 	} else {
14901 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
14902 		    ipst);
14903 	}
14904 
14905 	if (ipif_was_up && ill->ill_ipif_up_count == 0) {
14906 		/*
14907 		 * Since the interface is now down, it may have just become
14908 		 * inactive.  Note that this needs to be done even for a
14909 		 * lll_logical_down(), or ARP entries will not get correctly
14910 		 * restored when the interface comes back up.
14911 		 */
14912 		if (IS_UNDER_IPMP(ill))
14913 			ipmp_ill_refresh_active(ill);
14914 	}
14915 
14916 	/*
14917 	 * Cleaning up the conn_ire_cache or conns must be done only after the
14918 	 * ires have been deleted above. Otherwise a thread could end up
14919 	 * caching an ire in a conn after we have finished the cleanup of the
14920 	 * conn. The caching is done after making sure that the ire is not yet
14921 	 * condemned. Also documented in the block comment above ip_output
14922 	 */
14923 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
14924 	/* Also, delete the ires cached in SCTP */
14925 	sctp_ire_cache_flush(ipif);
14926 
14927 	/*
14928 	 * Update any other ipifs which have used "our" local address as
14929 	 * a source address. This entails removing and recreating IRE_INTERFACE
14930 	 * entries for such ipifs.
14931 	 */
14932 	if (ipif->ipif_isv6)
14933 		ipif_update_other_ipifs_v6(ipif);
14934 	else
14935 		ipif_update_other_ipifs(ipif);
14936 
14937 	/*
14938 	 * neighbor-discovery or arp entries for this interface.
14939 	 */
14940 	ipif_ndp_down(ipif);
14941 
14942 	/*
14943 	 * If mp is NULL the caller will wait for the appropriate refcnt.
14944 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
14945 	 * and ill_delete -> ipif_free -> ipif_down
14946 	 */
14947 	if (mp == NULL) {
14948 		ASSERT(q == NULL);
14949 		return (0);
14950 	}
14951 
14952 	if (CONN_Q(q)) {
14953 		connp = Q_TO_CONN(q);
14954 		mutex_enter(&connp->conn_lock);
14955 	} else {
14956 		connp = NULL;
14957 	}
14958 	mutex_enter(&ill->ill_lock);
14959 	/*
14960 	 * Are there any ire's pointing to this ipif that are still active ?
14961 	 * If this is the last ipif going down, are there any ire's pointing
14962 	 * to this ill that are still active ?
14963 	 */
14964 	if (ipif_is_quiescent(ipif)) {
14965 		mutex_exit(&ill->ill_lock);
14966 		if (connp != NULL)
14967 			mutex_exit(&connp->conn_lock);
14968 		return (0);
14969 	}
14970 
14971 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
14972 	    ill->ill_name, (void *)ill));
14973 	/*
14974 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
14975 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
14976 	 * which in turn is called by the last refrele on the ipif/ill/ire.
14977 	 */
14978 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
14979 	if (!success) {
14980 		/* The conn is closing. So just return */
14981 		ASSERT(connp != NULL);
14982 		mutex_exit(&ill->ill_lock);
14983 		mutex_exit(&connp->conn_lock);
14984 		return (EINTR);
14985 	}
14986 
14987 	mutex_exit(&ill->ill_lock);
14988 	if (connp != NULL)
14989 		mutex_exit(&connp->conn_lock);
14990 	return (EINPROGRESS);
14991 }
14992 
14993 void
14994 ipif_down_tail(ipif_t *ipif)
14995 {
14996 	ill_t	*ill = ipif->ipif_ill;
14997 
14998 	/*
14999 	 * Skip any loopback interface (null wq).
15000 	 * If this is the last logical interface on the ill
15001 	 * have ill_dl_down tell the driver we are gone (unbind)
15002 	 * Note that lun 0 can ipif_down even though
15003 	 * there are other logical units that are up.
15004 	 * This occurs e.g. when we change a "significant" IFF_ flag.
15005 	 */
15006 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
15007 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
15008 	    ill->ill_dl_up) {
15009 		ill_dl_down(ill);
15010 	}
15011 	ill->ill_logical_down = 0;
15012 
15013 	/*
15014 	 * Has to be after removing the routes in ipif_down_delete_ire.
15015 	 */
15016 	ipif_resolver_down(ipif);
15017 
15018 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
15019 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif, RTSQ_DEFAULT);
15020 }
15021 
15022 /*
15023  * Bring interface logically down without bringing the physical interface
15024  * down e.g. when the netmask is changed. This avoids long lasting link
15025  * negotiations between an ethernet interface and a certain switches.
15026  */
15027 static int
15028 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
15029 {
15030 	/*
15031 	 * The ill_logical_down flag is a transient flag. It is set here
15032 	 * and is cleared once the down has completed in ipif_down_tail.
15033 	 * This flag does not indicate whether the ill stream is in the
15034 	 * DL_BOUND state with the driver. Instead this flag is used by
15035 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
15036 	 * the driver. The state of the ill stream i.e. whether it is
15037 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
15038 	 */
15039 	ipif->ipif_ill->ill_logical_down = 1;
15040 	return (ipif_down(ipif, q, mp));
15041 }
15042 
15043 /*
15044  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
15045  * If the usesrc client ILL is already part of a usesrc group or not,
15046  * in either case a ire_stq with the matching usesrc client ILL will
15047  * locate the IRE's that need to be deleted. We want IREs to be created
15048  * with the new source address.
15049  */
15050 static void
15051 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
15052 {
15053 	ill_t	*ucill = (ill_t *)ill_arg;
15054 
15055 	ASSERT(IAM_WRITER_ILL(ucill));
15056 
15057 	if (ire->ire_stq == NULL)
15058 		return;
15059 
15060 	if ((ire->ire_type == IRE_CACHE) &&
15061 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
15062 		ire_delete(ire);
15063 }
15064 
15065 /*
15066  * ire_walk routine to delete every IRE dependent on the interface
15067  * address that is going down.	(Always called as writer.)
15068  * Works for both v4 and v6.
15069  * In addition for checking for ire_ipif matches it also checks for
15070  * IRE_CACHE entries which have the same source address as the
15071  * disappearing ipif since ipif_select_source might have picked
15072  * that source. Note that ipif_down/ipif_update_other_ipifs takes
15073  * care of any IRE_INTERFACE with the disappearing source address.
15074  */
15075 static void
15076 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
15077 {
15078 	ipif_t	*ipif = (ipif_t *)ipif_arg;
15079 
15080 	ASSERT(IAM_WRITER_IPIF(ipif));
15081 	if (ire->ire_ipif == NULL)
15082 		return;
15083 
15084 	if (ire->ire_ipif != ipif) {
15085 		/*
15086 		 * Look for a matching source address.
15087 		 */
15088 		if (ire->ire_type != IRE_CACHE)
15089 			return;
15090 		if (ipif->ipif_flags & IPIF_NOLOCAL)
15091 			return;
15092 
15093 		if (ire->ire_ipversion == IPV4_VERSION) {
15094 			if (ire->ire_src_addr != ipif->ipif_src_addr)
15095 				return;
15096 		} else {
15097 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
15098 			    &ipif->ipif_v6lcl_addr))
15099 				return;
15100 		}
15101 		ire_delete(ire);
15102 		return;
15103 	}
15104 	/*
15105 	 * ire_delete() will do an ire_flush_cache which will delete
15106 	 * all ire_ipif matches
15107 	 */
15108 	ire_delete(ire);
15109 }
15110 
15111 /*
15112  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
15113  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
15114  * 2) when an interface is brought up or down (on that ill).
15115  * This ensures that the IRE_CACHE entries don't retain stale source
15116  * address selection results.
15117  */
15118 void
15119 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
15120 {
15121 	ill_t	*ill = (ill_t *)ill_arg;
15122 
15123 	ASSERT(IAM_WRITER_ILL(ill));
15124 	ASSERT(ire->ire_type == IRE_CACHE);
15125 
15126 	/*
15127 	 * We are called for IRE_CACHEs whose ire_stq or ire_ipif matches
15128 	 * ill, but we only want to delete the IRE if ire_ipif matches.
15129 	 */
15130 	ASSERT(ire->ire_ipif != NULL);
15131 	if (ill == ire->ire_ipif->ipif_ill)
15132 		ire_delete(ire);
15133 }
15134 
15135 /*
15136  * Delete all the IREs whose ire_stq's reference `ill_arg'.  IPMP uses this
15137  * instead of ill_ipif_cache_delete() because ire_ipif->ipif_ill references
15138  * the IPMP ill.
15139  */
15140 void
15141 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
15142 {
15143 	ill_t	*ill = (ill_t *)ill_arg;
15144 
15145 	ASSERT(IAM_WRITER_ILL(ill));
15146 	ASSERT(ire->ire_type == IRE_CACHE);
15147 
15148 	/*
15149 	 * We are called for IRE_CACHEs whose ire_stq or ire_ipif matches
15150 	 * ill, but we only want to delete the IRE if ire_stq matches.
15151 	 */
15152 	if (ire->ire_stq->q_ptr == ill_arg)
15153 		ire_delete(ire);
15154 }
15155 
15156 /*
15157  * Delete all broadcast IREs with a source address on `ill_arg'.
15158  */
15159 static void
15160 ill_broadcast_delete(ire_t *ire, char *ill_arg)
15161 {
15162 	ill_t *ill = (ill_t *)ill_arg;
15163 
15164 	ASSERT(IAM_WRITER_ILL(ill));
15165 	ASSERT(ire->ire_type == IRE_BROADCAST);
15166 
15167 	if (ire->ire_ipif->ipif_ill == ill)
15168 		ire_delete(ire);
15169 }
15170 
15171 /*
15172  * Initiate deallocate of an IPIF. Always called as writer. Called by
15173  * ill_delete or ip_sioctl_removeif.
15174  */
15175 static void
15176 ipif_free(ipif_t *ipif)
15177 {
15178 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15179 
15180 	ASSERT(IAM_WRITER_IPIF(ipif));
15181 
15182 	if (ipif->ipif_recovery_id != 0)
15183 		(void) untimeout(ipif->ipif_recovery_id);
15184 	ipif->ipif_recovery_id = 0;
15185 
15186 	/* Remove conn references */
15187 	reset_conn_ipif(ipif);
15188 
15189 	/*
15190 	 * Make sure we have valid net and subnet broadcast ire's for the
15191 	 * other ipif's which share them with this ipif.
15192 	 */
15193 	if (!ipif->ipif_isv6)
15194 		ipif_check_bcast_ires(ipif);
15195 
15196 	/*
15197 	 * Take down the interface. We can be called either from ill_delete
15198 	 * or from ip_sioctl_removeif.
15199 	 */
15200 	(void) ipif_down(ipif, NULL, NULL);
15201 
15202 	/*
15203 	 * Now that the interface is down, there's no chance it can still
15204 	 * become a duplicate.  Cancel any timer that may have been set while
15205 	 * tearing down.
15206 	 */
15207 	if (ipif->ipif_recovery_id != 0)
15208 		(void) untimeout(ipif->ipif_recovery_id);
15209 	ipif->ipif_recovery_id = 0;
15210 
15211 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15212 	/* Remove pointers to this ill in the multicast routing tables */
15213 	reset_mrt_vif_ipif(ipif);
15214 	/* If necessary, clear the cached source ipif rotor. */
15215 	if (ipif->ipif_ill->ill_src_ipif == ipif)
15216 		ipif->ipif_ill->ill_src_ipif = NULL;
15217 	rw_exit(&ipst->ips_ill_g_lock);
15218 }
15219 
15220 static void
15221 ipif_free_tail(ipif_t *ipif)
15222 {
15223 	mblk_t	*mp;
15224 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
15225 
15226 	/*
15227 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
15228 	 */
15229 	mutex_enter(&ipif->ipif_saved_ire_lock);
15230 	mp = ipif->ipif_saved_ire_mp;
15231 	ipif->ipif_saved_ire_mp = NULL;
15232 	mutex_exit(&ipif->ipif_saved_ire_lock);
15233 	freemsg(mp);
15234 
15235 	/*
15236 	 * Need to hold both ill_g_lock and ill_lock while
15237 	 * inserting or removing an ipif from the linked list
15238 	 * of ipifs hanging off the ill.
15239 	 */
15240 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15241 
15242 	ASSERT(ilm_walk_ipif(ipif) == 0);
15243 
15244 #ifdef DEBUG
15245 	ipif_trace_cleanup(ipif);
15246 #endif
15247 
15248 	/* Ask SCTP to take it out of it list */
15249 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
15250 
15251 	/* Get it out of the ILL interface list. */
15252 	ipif_remove(ipif);
15253 	rw_exit(&ipst->ips_ill_g_lock);
15254 
15255 	mutex_destroy(&ipif->ipif_saved_ire_lock);
15256 
15257 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
15258 	ASSERT(ipif->ipif_recovery_id == 0);
15259 
15260 	/* Free the memory. */
15261 	mi_free(ipif);
15262 }
15263 
15264 /*
15265  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
15266  * is zero.
15267  */
15268 void
15269 ipif_get_name(const ipif_t *ipif, char *buf, int len)
15270 {
15271 	char	lbuf[LIFNAMSIZ];
15272 	char	*name;
15273 	size_t	name_len;
15274 
15275 	buf[0] = '\0';
15276 	name = ipif->ipif_ill->ill_name;
15277 	name_len = ipif->ipif_ill->ill_name_length;
15278 	if (ipif->ipif_id != 0) {
15279 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
15280 		    ipif->ipif_id);
15281 		name = lbuf;
15282 		name_len = mi_strlen(name) + 1;
15283 	}
15284 	len -= 1;
15285 	buf[len] = '\0';
15286 	len = MIN(len, name_len);
15287 	bcopy(name, buf, len);
15288 }
15289 
15290 /*
15291  * Find an IPIF based on the name passed in.  Names can be of the
15292  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
15293  * The <phys> string can have forms like <dev><#> (e.g., le0),
15294  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
15295  * When there is no colon, the implied unit id is zero. <phys> must
15296  * correspond to the name of an ILL.  (May be called as writer.)
15297  */
15298 static ipif_t *
15299 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
15300     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
15301     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
15302 {
15303 	char	*cp;
15304 	char	*endp;
15305 	long	id;
15306 	ill_t	*ill;
15307 	ipif_t	*ipif;
15308 	uint_t	ire_type;
15309 	boolean_t did_alloc = B_FALSE;
15310 	ipsq_t	*ipsq;
15311 
15312 	if (error != NULL)
15313 		*error = 0;
15314 
15315 	/*
15316 	 * If the caller wants to us to create the ipif, make sure we have a
15317 	 * valid zoneid
15318 	 */
15319 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
15320 
15321 	if (namelen == 0) {
15322 		if (error != NULL)
15323 			*error = ENXIO;
15324 		return (NULL);
15325 	}
15326 
15327 	*exists = B_FALSE;
15328 	/* Look for a colon in the name. */
15329 	endp = &name[namelen];
15330 	for (cp = endp; --cp > name; ) {
15331 		if (*cp == IPIF_SEPARATOR_CHAR)
15332 			break;
15333 	}
15334 
15335 	if (*cp == IPIF_SEPARATOR_CHAR) {
15336 		/*
15337 		 * Reject any non-decimal aliases for logical
15338 		 * interfaces. Aliases with leading zeroes
15339 		 * are also rejected as they introduce ambiguity
15340 		 * in the naming of the interfaces.
15341 		 * In order to confirm with existing semantics,
15342 		 * and to not break any programs/script relying
15343 		 * on that behaviour, if<0>:0 is considered to be
15344 		 * a valid interface.
15345 		 *
15346 		 * If alias has two or more digits and the first
15347 		 * is zero, fail.
15348 		 */
15349 		if (&cp[2] < endp && cp[1] == '0') {
15350 			if (error != NULL)
15351 				*error = EINVAL;
15352 			return (NULL);
15353 		}
15354 	}
15355 
15356 	if (cp <= name) {
15357 		cp = endp;
15358 	} else {
15359 		*cp = '\0';
15360 	}
15361 
15362 	/*
15363 	 * Look up the ILL, based on the portion of the name
15364 	 * before the slash. ill_lookup_on_name returns a held ill.
15365 	 * Temporary to check whether ill exists already. If so
15366 	 * ill_lookup_on_name will clear it.
15367 	 */
15368 	ill = ill_lookup_on_name(name, do_alloc, isv6,
15369 	    q, mp, func, error, &did_alloc, ipst);
15370 	if (cp != endp)
15371 		*cp = IPIF_SEPARATOR_CHAR;
15372 	if (ill == NULL)
15373 		return (NULL);
15374 
15375 	/* Establish the unit number in the name. */
15376 	id = 0;
15377 	if (cp < endp && *endp == '\0') {
15378 		/* If there was a colon, the unit number follows. */
15379 		cp++;
15380 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
15381 			ill_refrele(ill);
15382 			if (error != NULL)
15383 				*error = ENXIO;
15384 			return (NULL);
15385 		}
15386 	}
15387 
15388 	GRAB_CONN_LOCK(q);
15389 	mutex_enter(&ill->ill_lock);
15390 	/* Now see if there is an IPIF with this unit number. */
15391 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15392 		if (ipif->ipif_id == id) {
15393 			if (zoneid != ALL_ZONES &&
15394 			    zoneid != ipif->ipif_zoneid &&
15395 			    ipif->ipif_zoneid != ALL_ZONES) {
15396 				mutex_exit(&ill->ill_lock);
15397 				RELEASE_CONN_LOCK(q);
15398 				ill_refrele(ill);
15399 				if (error != NULL)
15400 					*error = ENXIO;
15401 				return (NULL);
15402 			}
15403 			/*
15404 			 * The block comment at the start of ipif_down
15405 			 * explains the use of the macros used below
15406 			 */
15407 			if (IPIF_CAN_LOOKUP(ipif)) {
15408 				ipif_refhold_locked(ipif);
15409 				mutex_exit(&ill->ill_lock);
15410 				if (!did_alloc)
15411 					*exists = B_TRUE;
15412 				/*
15413 				 * Drop locks before calling ill_refrele
15414 				 * since it can potentially call into
15415 				 * ipif_ill_refrele_tail which can end up
15416 				 * in trying to acquire any lock.
15417 				 */
15418 				RELEASE_CONN_LOCK(q);
15419 				ill_refrele(ill);
15420 				return (ipif);
15421 			} else if (IPIF_CAN_WAIT(ipif, q)) {
15422 				ipsq = ill->ill_phyint->phyint_ipsq;
15423 				mutex_enter(&ipsq->ipsq_lock);
15424 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
15425 				mutex_exit(&ill->ill_lock);
15426 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
15427 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
15428 				mutex_exit(&ipsq->ipsq_lock);
15429 				RELEASE_CONN_LOCK(q);
15430 				ill_refrele(ill);
15431 				if (error != NULL)
15432 					*error = EINPROGRESS;
15433 				return (NULL);
15434 			}
15435 		}
15436 	}
15437 	RELEASE_CONN_LOCK(q);
15438 
15439 	if (!do_alloc) {
15440 		mutex_exit(&ill->ill_lock);
15441 		ill_refrele(ill);
15442 		if (error != NULL)
15443 			*error = ENXIO;
15444 		return (NULL);
15445 	}
15446 
15447 	/*
15448 	 * If none found, atomically allocate and return a new one.
15449 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
15450 	 * to support "receive only" use of lo0:1 etc. as is still done
15451 	 * below as an initial guess.
15452 	 * However, this is now likely to be overriden later in ipif_up_done()
15453 	 * when we know for sure what address has been configured on the
15454 	 * interface, since we might have more than one loopback interface
15455 	 * with a loopback address, e.g. in the case of zones, and all the
15456 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
15457 	 */
15458 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
15459 		ire_type = IRE_LOOPBACK;
15460 	else
15461 		ire_type = IRE_LOCAL;
15462 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE, B_TRUE);
15463 	if (ipif != NULL)
15464 		ipif_refhold_locked(ipif);
15465 	else if (error != NULL)
15466 		*error = ENOMEM;
15467 	mutex_exit(&ill->ill_lock);
15468 	ill_refrele(ill);
15469 	return (ipif);
15470 }
15471 
15472 /*
15473  * This routine is called whenever a new address comes up on an ipif.  If
15474  * we are configured to respond to address mask requests, then we are supposed
15475  * to broadcast an address mask reply at this time.  This routine is also
15476  * called if we are already up, but a netmask change is made.  This is legal
15477  * but might not make the system manager very popular.	(May be called
15478  * as writer.)
15479  */
15480 void
15481 ipif_mask_reply(ipif_t *ipif)
15482 {
15483 	icmph_t	*icmph;
15484 	ipha_t	*ipha;
15485 	mblk_t	*mp;
15486 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15487 
15488 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
15489 
15490 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
15491 		return;
15492 
15493 	/* ICMP mask reply is IPv4 only */
15494 	ASSERT(!ipif->ipif_isv6);
15495 	/* ICMP mask reply is not for a loopback interface */
15496 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
15497 
15498 	mp = allocb(REPLY_LEN, BPRI_HI);
15499 	if (mp == NULL)
15500 		return;
15501 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
15502 
15503 	ipha = (ipha_t *)mp->b_rptr;
15504 	bzero(ipha, REPLY_LEN);
15505 	*ipha = icmp_ipha;
15506 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
15507 	ipha->ipha_src = ipif->ipif_src_addr;
15508 	ipha->ipha_dst = ipif->ipif_brd_addr;
15509 	ipha->ipha_length = htons(REPLY_LEN);
15510 	ipha->ipha_ident = 0;
15511 
15512 	icmph = (icmph_t *)&ipha[1];
15513 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
15514 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
15515 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
15516 
15517 	put(ipif->ipif_wq, mp);
15518 
15519 #undef	REPLY_LEN
15520 }
15521 
15522 /*
15523  * When the mtu in the ipif changes, we call this routine through ire_walk
15524  * to update all the relevant IREs.
15525  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
15526  */
15527 static void
15528 ipif_mtu_change(ire_t *ire, char *ipif_arg)
15529 {
15530 	ipif_t *ipif = (ipif_t *)ipif_arg;
15531 
15532 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
15533 		return;
15534 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
15535 }
15536 
15537 /*
15538  * When the mtu in the ill changes, we call this routine through ire_walk
15539  * to update all the relevant IREs.
15540  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
15541  */
15542 void
15543 ill_mtu_change(ire_t *ire, char *ill_arg)
15544 {
15545 	ill_t	*ill = (ill_t *)ill_arg;
15546 
15547 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
15548 		return;
15549 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
15550 }
15551 
15552 /*
15553  * Join the ipif specific multicast groups.
15554  * Must be called after a mapping has been set up in the resolver.  (Always
15555  * called as writer.)
15556  */
15557 void
15558 ipif_multicast_up(ipif_t *ipif)
15559 {
15560 	int err;
15561 	ill_t *ill;
15562 
15563 	ASSERT(IAM_WRITER_IPIF(ipif));
15564 
15565 	ill = ipif->ipif_ill;
15566 
15567 	ip1dbg(("ipif_multicast_up\n"));
15568 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
15569 		return;
15570 
15571 	if (ipif->ipif_isv6) {
15572 		in6_addr_t v6allmc = ipv6_all_hosts_mcast;
15573 		in6_addr_t v6solmc = ipv6_solicited_node_mcast;
15574 
15575 		v6solmc.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
15576 
15577 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
15578 			return;
15579 
15580 		ip1dbg(("ipif_multicast_up - addmulti\n"));
15581 
15582 		/*
15583 		 * Join the all hosts multicast address.  We skip this for
15584 		 * underlying IPMP interfaces since they should be invisible.
15585 		 */
15586 		if (!IS_UNDER_IPMP(ill)) {
15587 			err = ip_addmulti_v6(&v6allmc, ill, ipif->ipif_zoneid,
15588 			    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
15589 			if (err != 0) {
15590 				ip0dbg(("ipif_multicast_up: "
15591 				    "all_hosts_mcast failed %d\n", err));
15592 				return;
15593 			}
15594 			ipif->ipif_joined_allhosts = 1;
15595 		}
15596 
15597 		/*
15598 		 * Enable multicast for the solicited node multicast address
15599 		 */
15600 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
15601 			err = ip_addmulti_v6(&v6solmc, ill, ipif->ipif_zoneid,
15602 			    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
15603 			if (err != 0) {
15604 				ip0dbg(("ipif_multicast_up: solicited MC"
15605 				    " failed %d\n", err));
15606 				if (ipif->ipif_joined_allhosts) {
15607 					(void) ip_delmulti_v6(&v6allmc, ill,
15608 					    ipif->ipif_zoneid, B_TRUE, B_TRUE);
15609 					ipif->ipif_joined_allhosts = 0;
15610 				}
15611 				return;
15612 			}
15613 		}
15614 	} else {
15615 		if (ipif->ipif_lcl_addr == INADDR_ANY || IS_UNDER_IPMP(ill))
15616 			return;
15617 
15618 		/* Join the all hosts multicast address */
15619 		ip1dbg(("ipif_multicast_up - addmulti\n"));
15620 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
15621 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
15622 		if (err) {
15623 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
15624 			return;
15625 		}
15626 	}
15627 	ipif->ipif_multicast_up = 1;
15628 }
15629 
15630 /*
15631  * Blow away any multicast groups that we joined in ipif_multicast_up().
15632  * (Explicit memberships are blown away in ill_leave_multicast() when the
15633  * ill is brought down.)
15634  */
15635 void
15636 ipif_multicast_down(ipif_t *ipif)
15637 {
15638 	int err;
15639 
15640 	ASSERT(IAM_WRITER_IPIF(ipif));
15641 
15642 	ip1dbg(("ipif_multicast_down\n"));
15643 	if (!ipif->ipif_multicast_up)
15644 		return;
15645 
15646 	ip1dbg(("ipif_multicast_down - delmulti\n"));
15647 
15648 	if (!ipif->ipif_isv6) {
15649 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
15650 		    B_TRUE);
15651 		if (err != 0)
15652 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
15653 
15654 		ipif->ipif_multicast_up = 0;
15655 		return;
15656 	}
15657 
15658 	/*
15659 	 * Leave the all-hosts multicast address.
15660 	 */
15661 	if (ipif->ipif_joined_allhosts) {
15662 		err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
15663 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
15664 		if (err != 0) {
15665 			ip0dbg(("ipif_multicast_down: all_hosts_mcast "
15666 			    "failed %d\n", err));
15667 		}
15668 		ipif->ipif_joined_allhosts = 0;
15669 	}
15670 
15671 	/*
15672 	 * Disable multicast for the solicited node multicast address
15673 	 */
15674 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
15675 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
15676 
15677 		ipv6_multi.s6_addr32[3] |=
15678 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
15679 
15680 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
15681 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
15682 		if (err != 0) {
15683 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
15684 			    err));
15685 		}
15686 	}
15687 
15688 	ipif->ipif_multicast_up = 0;
15689 }
15690 
15691 /*
15692  * Used when an interface comes up to recreate any extra routes on this
15693  * interface.
15694  */
15695 static ire_t **
15696 ipif_recover_ire(ipif_t *ipif)
15697 {
15698 	mblk_t	*mp;
15699 	ire_t	**ipif_saved_irep;
15700 	ire_t	**irep;
15701 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15702 
15703 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
15704 	    ipif->ipif_id));
15705 
15706 	mutex_enter(&ipif->ipif_saved_ire_lock);
15707 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
15708 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
15709 	if (ipif_saved_irep == NULL) {
15710 		mutex_exit(&ipif->ipif_saved_ire_lock);
15711 		return (NULL);
15712 	}
15713 
15714 	irep = ipif_saved_irep;
15715 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
15716 		ire_t		*ire;
15717 		queue_t		*rfq;
15718 		queue_t		*stq;
15719 		ifrt_t		*ifrt;
15720 		uchar_t		*src_addr;
15721 		uchar_t		*gateway_addr;
15722 		ushort_t	type;
15723 
15724 		/*
15725 		 * When the ire was initially created and then added in
15726 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
15727 		 * in the case of a traditional interface route, or as one of
15728 		 * the IRE_OFFSUBNET types (with the exception of
15729 		 * IRE_HOST types ire which is created by icmp_redirect() and
15730 		 * which we don't need to save or recover).  In the case where
15731 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
15732 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
15733 		 * to satisfy software like GateD and Sun Cluster which creates
15734 		 * routes using the the loopback interface's address as a
15735 		 * gateway.
15736 		 *
15737 		 * As ifrt->ifrt_type reflects the already updated ire_type,
15738 		 * ire_create() will be called in the same way here as
15739 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
15740 		 * the route looks like a traditional interface route (where
15741 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
15742 		 * the saved ifrt->ifrt_type.  This means that in the case where
15743 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
15744 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
15745 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
15746 		 */
15747 		ifrt = (ifrt_t *)mp->b_rptr;
15748 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
15749 		if (ifrt->ifrt_type & IRE_INTERFACE) {
15750 			rfq = NULL;
15751 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
15752 			    ? ipif->ipif_rq : ipif->ipif_wq;
15753 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
15754 			    ? (uint8_t *)&ifrt->ifrt_src_addr
15755 			    : (uint8_t *)&ipif->ipif_src_addr;
15756 			gateway_addr = NULL;
15757 			type = ipif->ipif_net_type;
15758 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
15759 			/* Recover multiroute broadcast IRE. */
15760 			rfq = ipif->ipif_rq;
15761 			stq = ipif->ipif_wq;
15762 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
15763 			    ? (uint8_t *)&ifrt->ifrt_src_addr
15764 			    : (uint8_t *)&ipif->ipif_src_addr;
15765 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
15766 			type = ifrt->ifrt_type;
15767 		} else {
15768 			rfq = NULL;
15769 			stq = NULL;
15770 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
15771 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
15772 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
15773 			type = ifrt->ifrt_type;
15774 		}
15775 
15776 		/*
15777 		 * Create a copy of the IRE with the saved address and netmask.
15778 		 */
15779 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
15780 		    "0x%x/0x%x\n",
15781 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
15782 		    ntohl(ifrt->ifrt_addr),
15783 		    ntohl(ifrt->ifrt_mask)));
15784 		ire = ire_create(
15785 		    (uint8_t *)&ifrt->ifrt_addr,
15786 		    (uint8_t *)&ifrt->ifrt_mask,
15787 		    src_addr,
15788 		    gateway_addr,
15789 		    &ifrt->ifrt_max_frag,
15790 		    NULL,
15791 		    rfq,
15792 		    stq,
15793 		    type,
15794 		    ipif,
15795 		    0,
15796 		    0,
15797 		    0,
15798 		    ifrt->ifrt_flags,
15799 		    &ifrt->ifrt_iulp_info,
15800 		    NULL,
15801 		    NULL,
15802 		    ipst);
15803 
15804 		if (ire == NULL) {
15805 			mutex_exit(&ipif->ipif_saved_ire_lock);
15806 			kmem_free(ipif_saved_irep,
15807 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
15808 			return (NULL);
15809 		}
15810 
15811 		/*
15812 		 * Some software (for example, GateD and Sun Cluster) attempts
15813 		 * to create (what amount to) IRE_PREFIX routes with the
15814 		 * loopback address as the gateway.  This is primarily done to
15815 		 * set up prefixes with the RTF_REJECT flag set (for example,
15816 		 * when generating aggregate routes.)
15817 		 *
15818 		 * If the IRE type (as defined by ipif->ipif_net_type) is
15819 		 * IRE_LOOPBACK, then we map the request into a
15820 		 * IRE_IF_NORESOLVER.
15821 		 */
15822 		if (ipif->ipif_net_type == IRE_LOOPBACK)
15823 			ire->ire_type = IRE_IF_NORESOLVER;
15824 		/*
15825 		 * ire held by ire_add, will be refreled' towards the
15826 		 * the end of ipif_up_done
15827 		 */
15828 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
15829 		*irep = ire;
15830 		irep++;
15831 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
15832 	}
15833 	mutex_exit(&ipif->ipif_saved_ire_lock);
15834 	return (ipif_saved_irep);
15835 }
15836 
15837 /*
15838  * Used to set the netmask and broadcast address to default values when the
15839  * interface is brought up.  (Always called as writer.)
15840  */
15841 static void
15842 ipif_set_default(ipif_t *ipif)
15843 {
15844 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
15845 
15846 	if (!ipif->ipif_isv6) {
15847 		/*
15848 		 * Interface holds an IPv4 address. Default
15849 		 * mask is the natural netmask.
15850 		 */
15851 		if (!ipif->ipif_net_mask) {
15852 			ipaddr_t	v4mask;
15853 
15854 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
15855 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
15856 		}
15857 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
15858 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
15859 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
15860 		} else {
15861 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
15862 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
15863 		}
15864 		/*
15865 		 * NOTE: SunOS 4.X does this even if the broadcast address
15866 		 * has been already set thus we do the same here.
15867 		 */
15868 		if (ipif->ipif_flags & IPIF_BROADCAST) {
15869 			ipaddr_t	v4addr;
15870 
15871 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
15872 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
15873 		}
15874 	} else {
15875 		/*
15876 		 * Interface holds an IPv6-only address.  Default
15877 		 * mask is all-ones.
15878 		 */
15879 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
15880 			ipif->ipif_v6net_mask = ipv6_all_ones;
15881 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
15882 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
15883 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
15884 		} else {
15885 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
15886 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
15887 		}
15888 	}
15889 }
15890 
15891 /*
15892  * Return 0 if this address can be used as local address without causing
15893  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
15894  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
15895  * Note that the same IPv6 link-local address is allowed as long as the ills
15896  * are not on the same link.
15897  */
15898 int
15899 ip_addr_availability_check(ipif_t *new_ipif)
15900 {
15901 	in6_addr_t our_v6addr;
15902 	ill_t *ill;
15903 	ipif_t *ipif;
15904 	ill_walk_context_t ctx;
15905 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
15906 
15907 	ASSERT(IAM_WRITER_IPIF(new_ipif));
15908 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
15909 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
15910 
15911 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
15912 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
15913 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
15914 		return (0);
15915 
15916 	our_v6addr = new_ipif->ipif_v6lcl_addr;
15917 
15918 	if (new_ipif->ipif_isv6)
15919 		ill = ILL_START_WALK_V6(&ctx, ipst);
15920 	else
15921 		ill = ILL_START_WALK_V4(&ctx, ipst);
15922 
15923 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
15924 		for (ipif = ill->ill_ipif; ipif != NULL;
15925 		    ipif = ipif->ipif_next) {
15926 			if ((ipif == new_ipif) ||
15927 			    !(ipif->ipif_flags & IPIF_UP) ||
15928 			    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
15929 			    !IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
15930 			    &our_v6addr))
15931 				continue;
15932 
15933 			if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
15934 				new_ipif->ipif_flags |= IPIF_UNNUMBERED;
15935 			else if (ipif->ipif_flags & IPIF_POINTOPOINT)
15936 				ipif->ipif_flags |= IPIF_UNNUMBERED;
15937 			else if ((IN6_IS_ADDR_LINKLOCAL(&our_v6addr) ||
15938 			    IN6_IS_ADDR_SITELOCAL(&our_v6addr)) &&
15939 			    !IS_ON_SAME_LAN(ill, new_ipif->ipif_ill))
15940 				continue;
15941 			else if (new_ipif->ipif_zoneid != ipif->ipif_zoneid &&
15942 			    ipif->ipif_zoneid != ALL_ZONES && IS_LOOPBACK(ill))
15943 				continue;
15944 			else if (new_ipif->ipif_ill == ill)
15945 				return (EADDRINUSE);
15946 			else
15947 				return (EADDRNOTAVAIL);
15948 		}
15949 	}
15950 
15951 	return (0);
15952 }
15953 
15954 /*
15955  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
15956  * IREs for the ipif.
15957  * When the routine returns EINPROGRESS then mp has been consumed and
15958  * the ioctl will be acked from ip_rput_dlpi.
15959  */
15960 int
15961 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
15962 {
15963 	ill_t		*ill = ipif->ipif_ill;
15964 	boolean_t 	isv6 = ipif->ipif_isv6;
15965 	int		err = 0;
15966 	boolean_t	success;
15967 	uint_t		ipif_orig_id;
15968 	ip_stack_t	*ipst = ill->ill_ipst;
15969 
15970 	ASSERT(IAM_WRITER_IPIF(ipif));
15971 
15972 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
15973 
15974 	/* Shouldn't get here if it is already up. */
15975 	if (ipif->ipif_flags & IPIF_UP)
15976 		return (EALREADY);
15977 
15978 	/*
15979 	 * If this is a request to bring up a data address on an interface
15980 	 * under IPMP, then move the address to its IPMP meta-interface and
15981 	 * try to bring it up.  One complication is that the zeroth ipif for
15982 	 * an ill is special, in that every ill always has one, and that code
15983 	 * throughout IP deferences ill->ill_ipif without holding any locks.
15984 	 */
15985 	if (IS_UNDER_IPMP(ill) && ipmp_ipif_is_dataaddr(ipif) &&
15986 	    (!ipif->ipif_isv6 || !V6_IPIF_LINKLOCAL(ipif))) {
15987 		ipif_t	*stubipif = NULL, *moveipif = NULL;
15988 		ill_t	*ipmp_ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
15989 
15990 		/*
15991 		 * The ipif being brought up should be quiesced.  If it's not,
15992 		 * something has gone amiss and we need to bail out.  (If it's
15993 		 * quiesced, we know it will remain so via IPIF_CHANGING.)
15994 		 */
15995 		mutex_enter(&ill->ill_lock);
15996 		if (!ipif_is_quiescent(ipif)) {
15997 			mutex_exit(&ill->ill_lock);
15998 			return (EINVAL);
15999 		}
16000 		mutex_exit(&ill->ill_lock);
16001 
16002 		/*
16003 		 * If we're going to need to allocate ipifs, do it prior
16004 		 * to starting the move (and grabbing locks).
16005 		 */
16006 		if (ipif->ipif_id == 0) {
16007 			moveipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
16008 			    B_FALSE);
16009 			stubipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
16010 			    B_FALSE);
16011 			if (moveipif == NULL || stubipif == NULL) {
16012 				mi_free(moveipif);
16013 				mi_free(stubipif);
16014 				return (ENOMEM);
16015 			}
16016 		}
16017 
16018 		/*
16019 		 * Grab or transfer the ipif to move.  During the move, keep
16020 		 * ill_g_lock held to prevent any ill walker threads from
16021 		 * seeing things in an inconsistent state.
16022 		 */
16023 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16024 		if (ipif->ipif_id != 0) {
16025 			ipif_remove(ipif);
16026 		} else {
16027 			ipif_transfer(ipif, moveipif, stubipif);
16028 			ipif = moveipif;
16029 		}
16030 
16031 		/*
16032 		 * Place the ipif on the IPMP ill.  If the zeroth ipif on
16033 		 * the IPMP ill is a stub (0.0.0.0 down address) then we
16034 		 * replace that one.  Otherwise, pick the next available slot.
16035 		 */
16036 		ipif->ipif_ill = ipmp_ill;
16037 		ipif_orig_id = ipif->ipif_id;
16038 
16039 		if (ipmp_ipif_is_stubaddr(ipmp_ill->ill_ipif)) {
16040 			ipif_transfer(ipif, ipmp_ill->ill_ipif, NULL);
16041 			ipif = ipmp_ill->ill_ipif;
16042 		} else {
16043 			ipif->ipif_id = -1;
16044 			if (ipif_insert(ipif, B_FALSE) != 0) {
16045 				/*
16046 				 * No more available ipif_id's -- put it back
16047 				 * on the original ill and fail the operation.
16048 				 * Since we're writer on the ill, we can be
16049 				 * sure our old slot is still available.
16050 				 */
16051 				ipif->ipif_id = ipif_orig_id;
16052 				ipif->ipif_ill = ill;
16053 				if (ipif_orig_id == 0) {
16054 					ipif_transfer(ipif, ill->ill_ipif,
16055 					    NULL);
16056 				} else {
16057 					VERIFY(ipif_insert(ipif, B_FALSE) == 0);
16058 				}
16059 				rw_exit(&ipst->ips_ill_g_lock);
16060 				return (ENOMEM);
16061 			}
16062 		}
16063 		rw_exit(&ipst->ips_ill_g_lock);
16064 
16065 		/*
16066 		 * Tell SCTP that the ipif has moved.  Note that even if we
16067 		 * had to allocate a new ipif, the original sequence id was
16068 		 * preserved and therefore SCTP won't know.
16069 		 */
16070 		sctp_move_ipif(ipif, ill, ipmp_ill);
16071 
16072 		/*
16073 		 * If the ipif being brought up was on slot zero, then we
16074 		 * first need to bring up the placeholder we stuck there.  In
16075 		 * ip_rput_dlpi_writer(), ip_arp_done(), or the recursive call
16076 		 * to ipif_up() itself, if we successfully bring up the
16077 		 * placeholder, we'll check ill_move_ipif and bring it up too.
16078 		 */
16079 		if (ipif_orig_id == 0) {
16080 			ASSERT(ill->ill_move_ipif == NULL);
16081 			ill->ill_move_ipif = ipif;
16082 			if ((err = ipif_up(ill->ill_ipif, q, mp)) == 0)
16083 				ASSERT(ill->ill_move_ipif == NULL);
16084 			if (err != EINPROGRESS)
16085 				ill->ill_move_ipif = NULL;
16086 			return (err);
16087 		}
16088 
16089 		/*
16090 		 * Bring it up on the IPMP ill.
16091 		 */
16092 		return (ipif_up(ipif, q, mp));
16093 	}
16094 
16095 	/* Skip arp/ndp for any loopback interface. */
16096 	if (ill->ill_wq != NULL) {
16097 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
16098 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
16099 
16100 		if (!ill->ill_dl_up) {
16101 			/*
16102 			 * ill_dl_up is not yet set. i.e. we are yet to
16103 			 * DL_BIND with the driver and this is the first
16104 			 * logical interface on the ill to become "up".
16105 			 * Tell the driver to get going (via DL_BIND_REQ).
16106 			 * Note that changing "significant" IFF_ flags
16107 			 * address/netmask etc cause a down/up dance, but
16108 			 * does not cause an unbind (DL_UNBIND) with the driver
16109 			 */
16110 			return (ill_dl_up(ill, ipif, mp, q));
16111 		}
16112 
16113 		/*
16114 		 * ipif_resolver_up may end up sending an
16115 		 * AR_INTERFACE_UP message to ARP, which would, in
16116 		 * turn send a DLPI message to the driver. ioctls are
16117 		 * serialized and so we cannot send more than one
16118 		 * interface up message at a time. If ipif_resolver_up
16119 		 * does send an interface up message to ARP, we get
16120 		 * EINPROGRESS and we will complete in ip_arp_done.
16121 		 */
16122 
16123 		ASSERT(connp != NULL || !CONN_Q(q));
16124 		if (connp != NULL)
16125 			mutex_enter(&connp->conn_lock);
16126 		mutex_enter(&ill->ill_lock);
16127 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
16128 		mutex_exit(&ill->ill_lock);
16129 		if (connp != NULL)
16130 			mutex_exit(&connp->conn_lock);
16131 		if (!success)
16132 			return (EINTR);
16133 
16134 		/*
16135 		 * Crank up the resolver.  For IPv6, this cranks up the
16136 		 * external resolver if one is configured, but even if an
16137 		 * external resolver isn't configured, it must be called to
16138 		 * reset DAD state.  For IPv6, if an external resolver is not
16139 		 * being used, ipif_resolver_up() will never return
16140 		 * EINPROGRESS, so we can always call ipif_ndp_up() here.
16141 		 * Note that if an external resolver is being used, there's no
16142 		 * need to call ipif_ndp_up() since it will do nothing.
16143 		 */
16144 		err = ipif_resolver_up(ipif, Res_act_initial);
16145 		if (err == EINPROGRESS) {
16146 			/* We will complete it in ip_arp_done() */
16147 			return (err);
16148 		}
16149 
16150 		if (isv6 && err == 0)
16151 			err = ipif_ndp_up(ipif, B_TRUE);
16152 
16153 		ASSERT(err != EINPROGRESS);
16154 		mp = ipsq_pending_mp_get(ipsq, &connp);
16155 		ASSERT(mp != NULL);
16156 		if (err != 0)
16157 			return (err);
16158 	} else {
16159 		/*
16160 		 * Interfaces without underlying hardware don't do duplicate
16161 		 * address detection.
16162 		 */
16163 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
16164 		ipif->ipif_addr_ready = 1;
16165 	}
16166 
16167 	err = isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif);
16168 	if (err == 0 && ill->ill_move_ipif != NULL) {
16169 		ipif = ill->ill_move_ipif;
16170 		ill->ill_move_ipif = NULL;
16171 		return (ipif_up(ipif, q, mp));
16172 	}
16173 	return (err);
16174 }
16175 
16176 /*
16177  * Perform a bind for the physical device.
16178  * When the routine returns EINPROGRESS then mp has been consumed and
16179  * the ioctl will be acked from ip_rput_dlpi.
16180  * Allocate an unbind message and save it until ipif_down.
16181  */
16182 static int
16183 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
16184 {
16185 	areq_t	*areq;
16186 	mblk_t	*areq_mp = NULL;
16187 	mblk_t	*bind_mp = NULL;
16188 	mblk_t	*unbind_mp = NULL;
16189 	conn_t	*connp;
16190 	boolean_t success;
16191 	uint16_t sap_addr;
16192 
16193 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
16194 	ASSERT(IAM_WRITER_ILL(ill));
16195 	ASSERT(mp != NULL);
16196 
16197 	/* Create a resolver cookie for ARP */
16198 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
16199 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
16200 		if (areq_mp == NULL)
16201 			return (ENOMEM);
16202 
16203 		freemsg(ill->ill_resolver_mp);
16204 		ill->ill_resolver_mp = areq_mp;
16205 		areq = (areq_t *)areq_mp->b_rptr;
16206 		sap_addr = ill->ill_sap;
16207 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
16208 	}
16209 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
16210 	    DL_BIND_REQ);
16211 	if (bind_mp == NULL)
16212 		goto bad;
16213 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
16214 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
16215 
16216 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
16217 	if (unbind_mp == NULL)
16218 		goto bad;
16219 
16220 	/*
16221 	 * Record state needed to complete this operation when the
16222 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
16223 	 */
16224 	ASSERT(WR(q)->q_next == NULL);
16225 	connp = Q_TO_CONN(q);
16226 
16227 	mutex_enter(&connp->conn_lock);
16228 	mutex_enter(&ipif->ipif_ill->ill_lock);
16229 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
16230 	mutex_exit(&ipif->ipif_ill->ill_lock);
16231 	mutex_exit(&connp->conn_lock);
16232 	if (!success)
16233 		goto bad;
16234 
16235 	/*
16236 	 * Save the unbind message for ill_dl_down(); it will be consumed when
16237 	 * the interface goes down.
16238 	 */
16239 	ASSERT(ill->ill_unbind_mp == NULL);
16240 	ill->ill_unbind_mp = unbind_mp;
16241 
16242 	ill_dlpi_send(ill, bind_mp);
16243 	/* Send down link-layer capabilities probe if not already done. */
16244 	ill_capability_probe(ill);
16245 
16246 	/*
16247 	 * Sysid used to rely on the fact that netboots set domainname
16248 	 * and the like. Now that miniroot boots aren't strictly netboots
16249 	 * and miniroot network configuration is driven from userland
16250 	 * these things still need to be set. This situation can be detected
16251 	 * by comparing the interface being configured here to the one
16252 	 * dhcifname was set to reference by the boot loader. Once sysid is
16253 	 * converted to use dhcp_ipc_getinfo() this call can go away.
16254 	 */
16255 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
16256 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
16257 	    (strlen(srpc_domain) == 0)) {
16258 		if (dhcpinit() != 0)
16259 			cmn_err(CE_WARN, "no cached dhcp response");
16260 	}
16261 
16262 	/*
16263 	 * This operation will complete in ip_rput_dlpi with either
16264 	 * a DL_BIND_ACK or DL_ERROR_ACK.
16265 	 */
16266 	return (EINPROGRESS);
16267 bad:
16268 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
16269 
16270 	freemsg(bind_mp);
16271 	freemsg(unbind_mp);
16272 	return (ENOMEM);
16273 }
16274 
16275 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
16276 
16277 /*
16278  * DLPI and ARP is up.
16279  * Create all the IREs associated with an interface bring up multicast.
16280  * Set the interface flag and finish other initialization
16281  * that potentially had to be differed to after DL_BIND_ACK.
16282  */
16283 int
16284 ipif_up_done(ipif_t *ipif)
16285 {
16286 	ire_t	*ire_array[20];
16287 	ire_t	**irep = ire_array;
16288 	ire_t	**irep1;
16289 	ipaddr_t net_mask = 0;
16290 	ipaddr_t subnet_mask, route_mask;
16291 	ill_t	*ill = ipif->ipif_ill;
16292 	queue_t	*stq;
16293 	ipif_t	 *src_ipif;
16294 	ipif_t   *tmp_ipif;
16295 	boolean_t	flush_ire_cache = B_TRUE;
16296 	int	err = 0;
16297 	ire_t	**ipif_saved_irep = NULL;
16298 	int ipif_saved_ire_cnt;
16299 	int	cnt;
16300 	boolean_t	src_ipif_held = B_FALSE;
16301 	boolean_t	loopback = B_FALSE;
16302 	ip_stack_t	*ipst = ill->ill_ipst;
16303 
16304 	ip1dbg(("ipif_up_done(%s:%u)\n",
16305 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
16306 	/* Check if this is a loopback interface */
16307 	if (ipif->ipif_ill->ill_wq == NULL)
16308 		loopback = B_TRUE;
16309 
16310 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
16311 	/*
16312 	 * If all other interfaces for this ill are down or DEPRECATED,
16313 	 * or otherwise unsuitable for source address selection, remove
16314 	 * any IRE_CACHE entries for this ill to make sure source
16315 	 * address selection gets to take this new ipif into account.
16316 	 * No need to hold ill_lock while traversing the ipif list since
16317 	 * we are writer
16318 	 */
16319 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
16320 	    tmp_ipif = tmp_ipif->ipif_next) {
16321 		if (((tmp_ipif->ipif_flags &
16322 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
16323 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
16324 		    (tmp_ipif == ipif))
16325 			continue;
16326 		/* first useable pre-existing interface */
16327 		flush_ire_cache = B_FALSE;
16328 		break;
16329 	}
16330 	if (flush_ire_cache)
16331 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
16332 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
16333 
16334 	/*
16335 	 * Figure out which way the send-to queue should go.  Only
16336 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
16337 	 * should show up here.
16338 	 */
16339 	switch (ill->ill_net_type) {
16340 	case IRE_IF_RESOLVER:
16341 		stq = ill->ill_rq;
16342 		break;
16343 	case IRE_IF_NORESOLVER:
16344 	case IRE_LOOPBACK:
16345 		stq = ill->ill_wq;
16346 		break;
16347 	default:
16348 		return (EINVAL);
16349 	}
16350 
16351 	if (IS_LOOPBACK(ill)) {
16352 		/*
16353 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
16354 		 * ipif_lookup_on_name(), but in the case of zones we can have
16355 		 * several loopback addresses on lo0. So all the interfaces with
16356 		 * loopback addresses need to be marked IRE_LOOPBACK.
16357 		 */
16358 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
16359 		    htonl(INADDR_LOOPBACK))
16360 			ipif->ipif_ire_type = IRE_LOOPBACK;
16361 		else
16362 			ipif->ipif_ire_type = IRE_LOCAL;
16363 	}
16364 
16365 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST) ||
16366 	    ((ipif->ipif_flags & IPIF_DEPRECATED) &&
16367 	    !(ipif->ipif_flags & IPIF_NOFAILOVER))) {
16368 		/*
16369 		 * Can't use our source address. Select a different
16370 		 * source address for the IRE_INTERFACE and IRE_LOCAL
16371 		 */
16372 		src_ipif = ipif_select_source(ipif->ipif_ill,
16373 		    ipif->ipif_subnet, ipif->ipif_zoneid);
16374 		if (src_ipif == NULL)
16375 			src_ipif = ipif;	/* Last resort */
16376 		else
16377 			src_ipif_held = B_TRUE;
16378 	} else {
16379 		src_ipif = ipif;
16380 	}
16381 
16382 	/* Create all the IREs associated with this interface */
16383 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
16384 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
16385 
16386 		/*
16387 		 * If we're on a labeled system then make sure that zone-
16388 		 * private addresses have proper remote host database entries.
16389 		 */
16390 		if (is_system_labeled() &&
16391 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
16392 		    !tsol_check_interface_address(ipif))
16393 			return (EINVAL);
16394 
16395 		/* Register the source address for __sin6_src_id */
16396 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
16397 		    ipif->ipif_zoneid, ipst);
16398 		if (err != 0) {
16399 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
16400 			return (err);
16401 		}
16402 
16403 		/* If the interface address is set, create the local IRE. */
16404 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
16405 		    (void *)ipif,
16406 		    ipif->ipif_ire_type,
16407 		    ntohl(ipif->ipif_lcl_addr)));
16408 		*irep++ = ire_create(
16409 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
16410 		    (uchar_t *)&ip_g_all_ones,		/* mask */
16411 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
16412 		    NULL,				/* no gateway */
16413 		    &ip_loopback_mtuplus,		/* max frag size */
16414 		    NULL,
16415 		    ipif->ipif_rq,			/* recv-from queue */
16416 		    NULL,				/* no send-to queue */
16417 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
16418 		    ipif,
16419 		    0,
16420 		    0,
16421 		    0,
16422 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
16423 		    RTF_PRIVATE : 0,
16424 		    &ire_uinfo_null,
16425 		    NULL,
16426 		    NULL,
16427 		    ipst);
16428 	} else {
16429 		ip1dbg((
16430 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
16431 		    ipif->ipif_ire_type,
16432 		    ntohl(ipif->ipif_lcl_addr),
16433 		    (uint_t)ipif->ipif_flags));
16434 	}
16435 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
16436 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
16437 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
16438 	} else {
16439 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
16440 	}
16441 
16442 	subnet_mask = ipif->ipif_net_mask;
16443 
16444 	/*
16445 	 * If mask was not specified, use natural netmask of
16446 	 * interface address. Also, store this mask back into the
16447 	 * ipif struct.
16448 	 */
16449 	if (subnet_mask == 0) {
16450 		subnet_mask = net_mask;
16451 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
16452 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
16453 		    ipif->ipif_v6subnet);
16454 	}
16455 
16456 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
16457 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
16458 	    ipif->ipif_subnet != INADDR_ANY) {
16459 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
16460 
16461 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
16462 			route_mask = IP_HOST_MASK;
16463 		} else {
16464 			route_mask = subnet_mask;
16465 		}
16466 
16467 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
16468 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
16469 		    (void *)ipif, (void *)ill,
16470 		    ill->ill_net_type,
16471 		    ntohl(ipif->ipif_subnet)));
16472 		*irep++ = ire_create(
16473 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
16474 		    (uchar_t *)&route_mask,		/* mask */
16475 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
16476 		    NULL,				/* no gateway */
16477 		    &ipif->ipif_mtu,			/* max frag */
16478 		    NULL,
16479 		    NULL,				/* no recv queue */
16480 		    stq,				/* send-to queue */
16481 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
16482 		    ipif,
16483 		    0,
16484 		    0,
16485 		    0,
16486 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
16487 		    &ire_uinfo_null,
16488 		    NULL,
16489 		    NULL,
16490 		    ipst);
16491 	}
16492 
16493 	/*
16494 	 * Create any necessary broadcast IREs.
16495 	 */
16496 	if (ipif->ipif_flags & IPIF_BROADCAST)
16497 		irep = ipif_create_bcast_ires(ipif, irep);
16498 
16499 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
16500 
16501 	/* If an earlier ire_create failed, get out now */
16502 	for (irep1 = irep; irep1 > ire_array; ) {
16503 		irep1--;
16504 		if (*irep1 == NULL) {
16505 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
16506 			err = ENOMEM;
16507 			goto bad;
16508 		}
16509 	}
16510 
16511 	/*
16512 	 * Need to atomically check for IP address availability under
16513 	 * ip_addr_avail_lock.  ill_g_lock is held as reader to ensure no new
16514 	 * ills or new ipifs can be added while we are checking availability.
16515 	 */
16516 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16517 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
16518 	/* Mark it up, and increment counters. */
16519 	ipif->ipif_flags |= IPIF_UP;
16520 	ill->ill_ipif_up_count++;
16521 	err = ip_addr_availability_check(ipif);
16522 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
16523 	rw_exit(&ipst->ips_ill_g_lock);
16524 
16525 	if (err != 0) {
16526 		/*
16527 		 * Our address may already be up on the same ill. In this case,
16528 		 * the ARP entry for our ipif replaced the one for the other
16529 		 * ipif. So we don't want to delete it (otherwise the other ipif
16530 		 * would be unable to send packets).
16531 		 * ip_addr_availability_check() identifies this case for us and
16532 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
16533 		 * which is the expected error code.
16534 		 */
16535 		if (err == EADDRINUSE) {
16536 			freemsg(ipif->ipif_arp_del_mp);
16537 			ipif->ipif_arp_del_mp = NULL;
16538 			err = EADDRNOTAVAIL;
16539 		}
16540 		ill->ill_ipif_up_count--;
16541 		ipif->ipif_flags &= ~IPIF_UP;
16542 		goto bad;
16543 	}
16544 
16545 	/*
16546 	 * Add in all newly created IREs.  ire_create_bcast() has
16547 	 * already checked for duplicates of the IRE_BROADCAST type.
16548 	 */
16549 	for (irep1 = irep; irep1 > ire_array; ) {
16550 		irep1--;
16551 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
16552 		/*
16553 		 * refheld by ire_add. refele towards the end of the func
16554 		 */
16555 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
16556 	}
16557 
16558 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
16559 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
16560 	ipif_saved_irep = ipif_recover_ire(ipif);
16561 
16562 	if (!loopback) {
16563 		/*
16564 		 * If the broadcast address has been set, make sure it makes
16565 		 * sense based on the interface address.
16566 		 * Only match on ill since we are sharing broadcast addresses.
16567 		 */
16568 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
16569 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
16570 			ire_t	*ire;
16571 
16572 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
16573 			    IRE_BROADCAST, ipif, ALL_ZONES,
16574 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
16575 
16576 			if (ire == NULL) {
16577 				/*
16578 				 * If there isn't a matching broadcast IRE,
16579 				 * revert to the default for this netmask.
16580 				 */
16581 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
16582 				mutex_enter(&ipif->ipif_ill->ill_lock);
16583 				ipif_set_default(ipif);
16584 				mutex_exit(&ipif->ipif_ill->ill_lock);
16585 			} else {
16586 				ire_refrele(ire);
16587 			}
16588 		}
16589 
16590 	}
16591 
16592 	if (ill->ill_need_recover_multicast) {
16593 		/*
16594 		 * Need to recover all multicast memberships in the driver.
16595 		 * This had to be deferred until we had attached.  The same
16596 		 * code exists in ipif_up_done_v6() to recover IPv6
16597 		 * memberships.
16598 		 *
16599 		 * Note that it would be preferable to unconditionally do the
16600 		 * ill_recover_multicast() in ill_dl_up(), but we cannot do
16601 		 * that since ill_join_allmulti() depends on ill_dl_up being
16602 		 * set, and it is not set until we receive a DL_BIND_ACK after
16603 		 * having called ill_dl_up().
16604 		 */
16605 		ill_recover_multicast(ill);
16606 	}
16607 
16608 	if (ill->ill_ipif_up_count == 1) {
16609 		/*
16610 		 * Since the interface is now up, it may now be active.
16611 		 */
16612 		if (IS_UNDER_IPMP(ill))
16613 			ipmp_ill_refresh_active(ill);
16614 
16615 		/*
16616 		 * If this is an IPMP interface, we may now be able to
16617 		 * establish ARP entries.
16618 		 */
16619 		if (IS_IPMP(ill))
16620 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
16621 	}
16622 
16623 	/* Join the allhosts multicast address */
16624 	ipif_multicast_up(ipif);
16625 
16626 	/*
16627 	 * See if anybody else would benefit from our new ipif.
16628 	 */
16629 	if (!loopback &&
16630 	    !(ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
16631 		ill_update_source_selection(ill);
16632 	}
16633 
16634 	for (irep1 = irep; irep1 > ire_array; ) {
16635 		irep1--;
16636 		if (*irep1 != NULL) {
16637 			/* was held in ire_add */
16638 			ire_refrele(*irep1);
16639 		}
16640 	}
16641 
16642 	cnt = ipif_saved_ire_cnt;
16643 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
16644 		if (*irep1 != NULL) {
16645 			/* was held in ire_add */
16646 			ire_refrele(*irep1);
16647 		}
16648 	}
16649 
16650 	if (!loopback && ipif->ipif_addr_ready) {
16651 		/* Broadcast an address mask reply. */
16652 		ipif_mask_reply(ipif);
16653 	}
16654 	if (ipif_saved_irep != NULL) {
16655 		kmem_free(ipif_saved_irep,
16656 		    ipif_saved_ire_cnt * sizeof (ire_t *));
16657 	}
16658 	if (src_ipif_held)
16659 		ipif_refrele(src_ipif);
16660 
16661 	/*
16662 	 * This had to be deferred until we had bound.  Tell routing sockets and
16663 	 * others that this interface is up if it looks like the address has
16664 	 * been validated.  Otherwise, if it isn't ready yet, wait for
16665 	 * duplicate address detection to do its thing.
16666 	 */
16667 	if (ipif->ipif_addr_ready)
16668 		ipif_up_notify(ipif);
16669 	return (0);
16670 
16671 bad:
16672 	ip1dbg(("ipif_up_done: FAILED \n"));
16673 
16674 	while (irep > ire_array) {
16675 		irep--;
16676 		if (*irep != NULL)
16677 			ire_delete(*irep);
16678 	}
16679 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
16680 
16681 	if (ipif_saved_irep != NULL) {
16682 		kmem_free(ipif_saved_irep,
16683 		    ipif_saved_ire_cnt * sizeof (ire_t *));
16684 	}
16685 	if (src_ipif_held)
16686 		ipif_refrele(src_ipif);
16687 
16688 	ipif_resolver_down(ipif);
16689 	return (err);
16690 }
16691 
16692 /*
16693  * Turn off the ARP with the ILLF_NOARP flag.
16694  */
16695 static int
16696 ill_arp_off(ill_t *ill)
16697 {
16698 	mblk_t	*arp_off_mp = NULL;
16699 	mblk_t	*arp_on_mp = NULL;
16700 
16701 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
16702 
16703 	ASSERT(IAM_WRITER_ILL(ill));
16704 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
16705 
16706 	/*
16707 	 * If the on message is still around we've already done
16708 	 * an arp_off without doing an arp_on thus there is no
16709 	 * work needed.
16710 	 */
16711 	if (ill->ill_arp_on_mp != NULL)
16712 		return (0);
16713 
16714 	/*
16715 	 * Allocate an ARP on message (to be saved) and an ARP off message
16716 	 */
16717 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
16718 	if (!arp_off_mp)
16719 		return (ENOMEM);
16720 
16721 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
16722 	if (!arp_on_mp)
16723 		goto failed;
16724 
16725 	ASSERT(ill->ill_arp_on_mp == NULL);
16726 	ill->ill_arp_on_mp = arp_on_mp;
16727 
16728 	/* Send an AR_INTERFACE_OFF request */
16729 	putnext(ill->ill_rq, arp_off_mp);
16730 	return (0);
16731 failed:
16732 
16733 	if (arp_off_mp)
16734 		freemsg(arp_off_mp);
16735 	return (ENOMEM);
16736 }
16737 
16738 /*
16739  * Turn on ARP by turning off the ILLF_NOARP flag.
16740  */
16741 static int
16742 ill_arp_on(ill_t *ill)
16743 {
16744 	mblk_t	*mp;
16745 
16746 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
16747 
16748 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
16749 
16750 	ASSERT(IAM_WRITER_ILL(ill));
16751 	/*
16752 	 * Send an AR_INTERFACE_ON request if we have already done
16753 	 * an arp_off (which allocated the message).
16754 	 */
16755 	if (ill->ill_arp_on_mp != NULL) {
16756 		mp = ill->ill_arp_on_mp;
16757 		ill->ill_arp_on_mp = NULL;
16758 		putnext(ill->ill_rq, mp);
16759 	}
16760 	return (0);
16761 }
16762 
16763 /*
16764  * Checks for availbility of a usable source address (if there is one) when the
16765  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
16766  * this selection is done regardless of the destination.
16767  */
16768 boolean_t
16769 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
16770 {
16771 	uint_t	ifindex;
16772 	ipif_t	*ipif = NULL;
16773 	ill_t	*uill;
16774 	boolean_t isv6;
16775 	ip_stack_t	*ipst = ill->ill_ipst;
16776 
16777 	ASSERT(ill != NULL);
16778 
16779 	isv6 = ill->ill_isv6;
16780 	ifindex = ill->ill_usesrc_ifindex;
16781 	if (ifindex != 0) {
16782 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
16783 		    NULL, ipst);
16784 		if (uill == NULL)
16785 			return (NULL);
16786 		mutex_enter(&uill->ill_lock);
16787 		for (ipif = uill->ill_ipif; ipif != NULL;
16788 		    ipif = ipif->ipif_next) {
16789 			if (!IPIF_CAN_LOOKUP(ipif))
16790 				continue;
16791 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
16792 				continue;
16793 			if (!(ipif->ipif_flags & IPIF_UP))
16794 				continue;
16795 			if (ipif->ipif_zoneid != zoneid)
16796 				continue;
16797 			if ((isv6 &&
16798 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
16799 			    (ipif->ipif_lcl_addr == INADDR_ANY))
16800 				continue;
16801 			mutex_exit(&uill->ill_lock);
16802 			ill_refrele(uill);
16803 			return (B_TRUE);
16804 		}
16805 		mutex_exit(&uill->ill_lock);
16806 		ill_refrele(uill);
16807 	}
16808 	return (B_FALSE);
16809 }
16810 
16811 /*
16812  * IP source address type, sorted from worst to best.  For a given type,
16813  * always prefer IP addresses on the same subnet.  All-zones addresses are
16814  * suboptimal because they pose problems with unlabeled destinations.
16815  */
16816 typedef enum {
16817 	IPIF_NONE,
16818 	IPIF_DIFFNET_DEPRECATED, 	/* deprecated and different subnet */
16819 	IPIF_SAMENET_DEPRECATED, 	/* deprecated and same subnet */
16820 	IPIF_DIFFNET_ALLZONES,		/* allzones and different subnet */
16821 	IPIF_SAMENET_ALLZONES,		/* allzones and same subnet */
16822 	IPIF_DIFFNET,			/* normal and different subnet */
16823 	IPIF_SAMENET			/* normal and same subnet */
16824 } ipif_type_t;
16825 
16826 /*
16827  * Pick the optimal ipif on `ill' for sending to destination `dst' from zone
16828  * `zoneid'.  We rate usable ipifs from low -> high as per the ipif_type_t
16829  * enumeration, and return the highest-rated ipif.  If there's a tie, we pick
16830  * the first one, unless IPMP is used in which case we round-robin among them;
16831  * see below for more.
16832  *
16833  * Returns NULL if there is no suitable source address for the ill.
16834  * This only occurs when there is no valid source address for the ill.
16835  */
16836 ipif_t *
16837 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
16838 {
16839 	ill_t	*usill = NULL;
16840 	ill_t	*ipmp_ill = NULL;
16841 	ipif_t	*start_ipif, *next_ipif, *ipif, *best_ipif;
16842 	ipif_type_t type, best_type;
16843 	tsol_tpc_t *src_rhtp, *dst_rhtp;
16844 	ip_stack_t *ipst = ill->ill_ipst;
16845 	boolean_t samenet;
16846 
16847 	if (ill->ill_usesrc_ifindex != 0) {
16848 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
16849 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
16850 		if (usill != NULL)
16851 			ill = usill;	/* Select source from usesrc ILL */
16852 		else
16853 			return (NULL);
16854 	}
16855 
16856 	/*
16857 	 * Test addresses should never be used for source address selection,
16858 	 * so if we were passed one, switch to the IPMP meta-interface.
16859 	 */
16860 	if (IS_UNDER_IPMP(ill)) {
16861 		if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL)
16862 			ill = ipmp_ill;	/* Select source from IPMP ill */
16863 		else
16864 			return (NULL);
16865 	}
16866 
16867 	/*
16868 	 * If we're dealing with an unlabeled destination on a labeled system,
16869 	 * make sure that we ignore source addresses that are incompatible with
16870 	 * the destination's default label.  That destination's default label
16871 	 * must dominate the minimum label on the source address.
16872 	 */
16873 	dst_rhtp = NULL;
16874 	if (is_system_labeled()) {
16875 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
16876 		if (dst_rhtp == NULL)
16877 			return (NULL);
16878 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
16879 			TPC_RELE(dst_rhtp);
16880 			dst_rhtp = NULL;
16881 		}
16882 	}
16883 
16884 	/*
16885 	 * Hold the ill_g_lock as reader. This makes sure that no ipif/ill
16886 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
16887 	 * After selecting the right ipif, under ill_lock make sure ipif is
16888 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
16889 	 * we retry. Inside the loop we still need to check for CONDEMNED,
16890 	 * but not under a lock.
16891 	 */
16892 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16893 retry:
16894 	/*
16895 	 * For source address selection, we treat the ipif list as circular
16896 	 * and continue until we get back to where we started.  This allows
16897 	 * IPMP to vary source address selection (which improves inbound load
16898 	 * spreading) by caching its last ending point and starting from
16899 	 * there.  NOTE: we don't have to worry about ill_src_ipif changing
16900 	 * ills since that can't happen on the IPMP ill.
16901 	 */
16902 	start_ipif = ill->ill_ipif;
16903 	if (IS_IPMP(ill) && ill->ill_src_ipif != NULL)
16904 		start_ipif = ill->ill_src_ipif;
16905 
16906 	ipif = start_ipif;
16907 	best_ipif = NULL;
16908 	best_type = IPIF_NONE;
16909 	do {
16910 		if ((next_ipif = ipif->ipif_next) == NULL)
16911 			next_ipif = ill->ill_ipif;
16912 
16913 		if (!IPIF_CAN_LOOKUP(ipif))
16914 			continue;
16915 		/* Always skip NOLOCAL and ANYCAST interfaces */
16916 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
16917 			continue;
16918 		if (!(ipif->ipif_flags & IPIF_UP) || !ipif->ipif_addr_ready)
16919 			continue;
16920 		if (ipif->ipif_zoneid != zoneid &&
16921 		    ipif->ipif_zoneid != ALL_ZONES)
16922 			continue;
16923 
16924 		/*
16925 		 * Interfaces with 0.0.0.0 address are allowed to be UP, but
16926 		 * are not valid as source addresses.
16927 		 */
16928 		if (ipif->ipif_lcl_addr == INADDR_ANY)
16929 			continue;
16930 
16931 		/*
16932 		 * Check compatibility of local address for destination's
16933 		 * default label if we're on a labeled system.	Incompatible
16934 		 * addresses can't be used at all.
16935 		 */
16936 		if (dst_rhtp != NULL) {
16937 			boolean_t incompat;
16938 
16939 			src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
16940 			    IPV4_VERSION, B_FALSE);
16941 			if (src_rhtp == NULL)
16942 				continue;
16943 			incompat = src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
16944 			    src_rhtp->tpc_tp.tp_doi !=
16945 			    dst_rhtp->tpc_tp.tp_doi ||
16946 			    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
16947 			    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
16948 			    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
16949 			    src_rhtp->tpc_tp.tp_sl_set_cipso));
16950 			TPC_RELE(src_rhtp);
16951 			if (incompat)
16952 				continue;
16953 		}
16954 
16955 		samenet = ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet);
16956 
16957 		if (ipif->ipif_flags & IPIF_DEPRECATED) {
16958 			type = samenet ? IPIF_SAMENET_DEPRECATED :
16959 			    IPIF_DIFFNET_DEPRECATED;
16960 		} else if (ipif->ipif_zoneid == ALL_ZONES) {
16961 			type = samenet ? IPIF_SAMENET_ALLZONES :
16962 			    IPIF_DIFFNET_ALLZONES;
16963 		} else {
16964 			type = samenet ? IPIF_SAMENET : IPIF_DIFFNET;
16965 		}
16966 
16967 		if (type > best_type) {
16968 			best_type = type;
16969 			best_ipif = ipif;
16970 			if (best_type == IPIF_SAMENET)
16971 				break; /* can't get better */
16972 		}
16973 	} while ((ipif = next_ipif) != start_ipif);
16974 
16975 	if ((ipif = best_ipif) != NULL) {
16976 		mutex_enter(&ipif->ipif_ill->ill_lock);
16977 		if (!IPIF_CAN_LOOKUP(ipif)) {
16978 			mutex_exit(&ipif->ipif_ill->ill_lock);
16979 			goto retry;
16980 		}
16981 		ipif_refhold_locked(ipif);
16982 
16983 		/*
16984 		 * For IPMP, update the source ipif rotor to the next ipif,
16985 		 * provided we can look it up.  (We must not use it if it's
16986 		 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after
16987 		 * ipif_free() checked ill_src_ipif.)
16988 		 */
16989 		if (IS_IPMP(ill) && ipif != NULL) {
16990 			next_ipif = ipif->ipif_next;
16991 			if (next_ipif != NULL && IPIF_CAN_LOOKUP(next_ipif))
16992 				ill->ill_src_ipif = next_ipif;
16993 			else
16994 				ill->ill_src_ipif = NULL;
16995 		}
16996 		mutex_exit(&ipif->ipif_ill->ill_lock);
16997 	}
16998 
16999 	rw_exit(&ipst->ips_ill_g_lock);
17000 	if (usill != NULL)
17001 		ill_refrele(usill);
17002 	if (ipmp_ill != NULL)
17003 		ill_refrele(ipmp_ill);
17004 	if (dst_rhtp != NULL)
17005 		TPC_RELE(dst_rhtp);
17006 
17007 #ifdef DEBUG
17008 	if (ipif == NULL) {
17009 		char buf1[INET6_ADDRSTRLEN];
17010 
17011 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
17012 		    ill->ill_name,
17013 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
17014 	} else {
17015 		char buf1[INET6_ADDRSTRLEN];
17016 		char buf2[INET6_ADDRSTRLEN];
17017 
17018 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
17019 		    ipif->ipif_ill->ill_name,
17020 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
17021 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
17022 		    buf2, sizeof (buf2))));
17023 	}
17024 #endif /* DEBUG */
17025 	return (ipif);
17026 }
17027 
17028 
17029 /*
17030  * If old_ipif is not NULL, see if ipif was derived from old
17031  * ipif and if so, recreate the interface route by re-doing
17032  * source address selection. This happens when ipif_down ->
17033  * ipif_update_other_ipifs calls us.
17034  *
17035  * If old_ipif is NULL, just redo the source address selection
17036  * if needed. This happens when ipif_up_done calls us.
17037  */
17038 static void
17039 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
17040 {
17041 	ire_t *ire;
17042 	ire_t *ipif_ire;
17043 	queue_t *stq;
17044 	ipif_t *nipif;
17045 	ill_t *ill;
17046 	boolean_t need_rele = B_FALSE;
17047 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
17048 
17049 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
17050 	ASSERT(IAM_WRITER_IPIF(ipif));
17051 
17052 	ill = ipif->ipif_ill;
17053 	if (!(ipif->ipif_flags &
17054 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
17055 		/*
17056 		 * Can't possibly have borrowed the source
17057 		 * from old_ipif.
17058 		 */
17059 		return;
17060 	}
17061 
17062 	/*
17063 	 * Is there any work to be done? No work if the address
17064 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
17065 	 * ipif_select_source() does not borrow addresses from
17066 	 * NOLOCAL and ANYCAST interfaces).
17067 	 */
17068 	if ((old_ipif != NULL) &&
17069 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
17070 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
17071 	    (old_ipif->ipif_flags &
17072 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
17073 		return;
17074 	}
17075 
17076 	/*
17077 	 * Perform the same checks as when creating the
17078 	 * IRE_INTERFACE in ipif_up_done.
17079 	 */
17080 	if (!(ipif->ipif_flags & IPIF_UP))
17081 		return;
17082 
17083 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
17084 	    (ipif->ipif_subnet == INADDR_ANY))
17085 		return;
17086 
17087 	ipif_ire = ipif_to_ire(ipif);
17088 	if (ipif_ire == NULL)
17089 		return;
17090 
17091 	/*
17092 	 * We know that ipif uses some other source for its
17093 	 * IRE_INTERFACE. Is it using the source of this
17094 	 * old_ipif?
17095 	 */
17096 	if (old_ipif != NULL &&
17097 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
17098 		ire_refrele(ipif_ire);
17099 		return;
17100 	}
17101 	if (ip_debug > 2) {
17102 		/* ip1dbg */
17103 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
17104 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
17105 	}
17106 
17107 	stq = ipif_ire->ire_stq;
17108 
17109 	/*
17110 	 * Can't use our source address. Select a different
17111 	 * source address for the IRE_INTERFACE.
17112 	 */
17113 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
17114 	if (nipif == NULL) {
17115 		/* Last resort - all ipif's have IPIF_NOLOCAL */
17116 		nipif = ipif;
17117 	} else {
17118 		need_rele = B_TRUE;
17119 	}
17120 
17121 	ire = ire_create(
17122 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
17123 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
17124 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
17125 	    NULL,				/* no gateway */
17126 	    &ipif->ipif_mtu,			/* max frag */
17127 	    NULL,				/* no src nce */
17128 	    NULL,				/* no recv from queue */
17129 	    stq,				/* send-to queue */
17130 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
17131 	    ipif,
17132 	    0,
17133 	    0,
17134 	    0,
17135 	    0,
17136 	    &ire_uinfo_null,
17137 	    NULL,
17138 	    NULL,
17139 	    ipst);
17140 
17141 	if (ire != NULL) {
17142 		ire_t *ret_ire;
17143 		int error;
17144 
17145 		/*
17146 		 * We don't need ipif_ire anymore. We need to delete
17147 		 * before we add so that ire_add does not detect
17148 		 * duplicates.
17149 		 */
17150 		ire_delete(ipif_ire);
17151 		ret_ire = ire;
17152 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
17153 		ASSERT(error == 0);
17154 		ASSERT(ire == ret_ire);
17155 		/* Held in ire_add */
17156 		ire_refrele(ret_ire);
17157 	}
17158 	/*
17159 	 * Either we are falling through from above or could not
17160 	 * allocate a replacement.
17161 	 */
17162 	ire_refrele(ipif_ire);
17163 	if (need_rele)
17164 		ipif_refrele(nipif);
17165 }
17166 
17167 /*
17168  * This old_ipif is going away.
17169  *
17170  * Determine if any other ipif's are using our address as
17171  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
17172  * IPIF_DEPRECATED).
17173  * Find the IRE_INTERFACE for such ipifs and recreate them
17174  * to use an different source address following the rules in
17175  * ipif_up_done.
17176  */
17177 static void
17178 ipif_update_other_ipifs(ipif_t *old_ipif)
17179 {
17180 	ipif_t	*ipif;
17181 	ill_t	*ill;
17182 	char	buf[INET6_ADDRSTRLEN];
17183 
17184 	ASSERT(IAM_WRITER_IPIF(old_ipif));
17185 
17186 	ill = old_ipif->ipif_ill;
17187 
17188 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n", ill->ill_name,
17189 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr, buf, sizeof (buf))));
17190 
17191 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17192 		if (ipif == old_ipif)
17193 			continue;
17194 		ipif_recreate_interface_routes(old_ipif, ipif);
17195 	}
17196 }
17197 
17198 /* ARGSUSED */
17199 int
17200 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
17201 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
17202 {
17203 	/*
17204 	 * ill_phyint_reinit merged the v4 and v6 into a single
17205 	 * ipsq.  We might not have been able to complete the
17206 	 * operation in ipif_set_values, if we could not become
17207 	 * exclusive.  If so restart it here.
17208 	 */
17209 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
17210 }
17211 
17212 /*
17213  * Can operate on either a module or a driver queue.
17214  * Returns an error if not a module queue.
17215  */
17216 /* ARGSUSED */
17217 int
17218 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
17219     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
17220 {
17221 	queue_t		*q1 = q;
17222 	char 		*cp;
17223 	char		interf_name[LIFNAMSIZ];
17224 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
17225 
17226 	if (q->q_next == NULL) {
17227 		ip1dbg((
17228 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
17229 		return (EINVAL);
17230 	}
17231 
17232 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
17233 		return (EALREADY);
17234 
17235 	do {
17236 		q1 = q1->q_next;
17237 	} while (q1->q_next);
17238 	cp = q1->q_qinfo->qi_minfo->mi_idname;
17239 	(void) sprintf(interf_name, "%s%d", cp, ppa);
17240 
17241 	/*
17242 	 * Here we are not going to delay the ioack until after
17243 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
17244 	 * original ioctl message before sending the requests.
17245 	 */
17246 	return (ipif_set_values(q, mp, interf_name, &ppa));
17247 }
17248 
17249 /* ARGSUSED */
17250 int
17251 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
17252     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
17253 {
17254 	return (ENXIO);
17255 }
17256 
17257 /*
17258  * Refresh all IRE_BROADCAST entries associated with `ill' to ensure the
17259  * minimum (but complete) set exist.  This is necessary when adding or
17260  * removing an interface to/from an IPMP group, since interfaces in an
17261  * IPMP group use the IRE_BROADCAST entries for the IPMP group (whenever
17262  * its test address subnets overlap with IPMP data addresses).	It's also
17263  * used to refresh the IRE_BROADCAST entries associated with the IPMP
17264  * interface when the nominated broadcast interface changes.
17265  */
17266 void
17267 ill_refresh_bcast(ill_t *ill)
17268 {
17269 	ire_t *ire_array[12];	/* max ipif_create_bcast_ires() can create */
17270 	ire_t **irep;
17271 	ipif_t *ipif;
17272 
17273 	ASSERT(!ill->ill_isv6);
17274 	ASSERT(IAM_WRITER_ILL(ill));
17275 
17276 	/*
17277 	 * Remove any old broadcast IREs.
17278 	 */
17279 	ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE, IRE_BROADCAST,
17280 	    ill_broadcast_delete, ill, ill);
17281 
17282 	/*
17283 	 * Create new ones for any ipifs that are up and broadcast-capable.
17284 	 */
17285 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17286 		if ((ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST)) !=
17287 		    (IPIF_UP|IPIF_BROADCAST))
17288 			continue;
17289 
17290 		irep = ipif_create_bcast_ires(ipif, ire_array);
17291 		while (irep-- > ire_array) {
17292 			(void) ire_add(irep, NULL, NULL, NULL, B_FALSE);
17293 			if (*irep != NULL)
17294 				ire_refrele(*irep);
17295 		}
17296 	}
17297 }
17298 
17299 /*
17300  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
17301  * `irep'.  Returns a pointer to the next free `irep' entry (just like
17302  * ire_check_and_create_bcast()).
17303  */
17304 static ire_t **
17305 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
17306 {
17307 	ipaddr_t addr;
17308 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
17309 	ipaddr_t subnetmask = ipif->ipif_net_mask;
17310 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
17311 
17312 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
17313 
17314 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
17315 
17316 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
17317 	    (ipif->ipif_flags & IPIF_NOLOCAL))
17318 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
17319 
17320 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
17321 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
17322 
17323 	/*
17324 	 * For backward compatibility, we create net broadcast IREs based on
17325 	 * the old "IP address class system", since some old machines only
17326 	 * respond to these class derived net broadcast.  However, we must not
17327 	 * create these net broadcast IREs if the subnetmask is shorter than
17328 	 * the IP address class based derived netmask.  Otherwise, we may
17329 	 * create a net broadcast address which is the same as an IP address
17330 	 * on the subnet -- and then TCP will refuse to talk to that address.
17331 	 */
17332 	if (netmask < subnetmask) {
17333 		addr = netmask & ipif->ipif_subnet;
17334 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
17335 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
17336 		    flags);
17337 	}
17338 
17339 	/*
17340 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
17341 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
17342 	 * created.  Creating these broadcast IREs will only create confusion
17343 	 * as `addr' will be the same as the IP address.
17344 	 */
17345 	if (subnetmask != 0xFFFFFFFF) {
17346 		addr = ipif->ipif_subnet;
17347 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
17348 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
17349 		    irep, flags);
17350 	}
17351 
17352 	return (irep);
17353 }
17354 
17355 /*
17356  * Broadcast IRE info structure used in the functions below.  Since we
17357  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
17358  */
17359 typedef struct bcast_ireinfo {
17360 	uchar_t		bi_type;	/* BCAST_* value from below */
17361 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
17362 			bi_needrep:1,	/* do we need to replace it? */
17363 			bi_haverep:1,	/* have we replaced it? */
17364 			bi_pad:5;
17365 	ipaddr_t	bi_addr;	/* IRE address */
17366 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
17367 } bcast_ireinfo_t;
17368 
17369 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
17370 
17371 /*
17372  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
17373  * return B_TRUE if it should immediately be used to recreate the IRE.
17374  */
17375 static boolean_t
17376 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
17377 {
17378 	ipaddr_t addr;
17379 
17380 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
17381 
17382 	switch (bireinfop->bi_type) {
17383 	case BCAST_NET:
17384 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
17385 		if (addr != bireinfop->bi_addr)
17386 			return (B_FALSE);
17387 		break;
17388 	case BCAST_SUBNET:
17389 		if (ipif->ipif_subnet != bireinfop->bi_addr)
17390 			return (B_FALSE);
17391 		break;
17392 	}
17393 
17394 	bireinfop->bi_needrep = 1;
17395 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
17396 		if (bireinfop->bi_backup == NULL)
17397 			bireinfop->bi_backup = ipif;
17398 		return (B_FALSE);
17399 	}
17400 	return (B_TRUE);
17401 }
17402 
17403 /*
17404  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
17405  * them ala ire_check_and_create_bcast().
17406  */
17407 static ire_t **
17408 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
17409 {
17410 	ipaddr_t mask, addr;
17411 
17412 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
17413 
17414 	addr = bireinfop->bi_addr;
17415 	irep = ire_create_bcast(ipif, addr, irep);
17416 
17417 	switch (bireinfop->bi_type) {
17418 	case BCAST_NET:
17419 		mask = ip_net_mask(ipif->ipif_subnet);
17420 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
17421 		break;
17422 	case BCAST_SUBNET:
17423 		mask = ipif->ipif_net_mask;
17424 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
17425 		break;
17426 	}
17427 
17428 	bireinfop->bi_haverep = 1;
17429 	return (irep);
17430 }
17431 
17432 /*
17433  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
17434  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
17435  * that are going away are still needed.  If so, have ipif_create_bcast()
17436  * recreate them (except for the deprecated case, as explained below).
17437  */
17438 static ire_t **
17439 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
17440     ire_t **irep)
17441 {
17442 	int i;
17443 	ipif_t *ipif;
17444 
17445 	ASSERT(!ill->ill_isv6);
17446 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17447 		/*
17448 		 * Skip this ipif if it's (a) the one being taken down, (b)
17449 		 * not in the same zone, or (c) has no valid local address.
17450 		 */
17451 		if (ipif == test_ipif ||
17452 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
17453 		    ipif->ipif_subnet == 0 ||
17454 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
17455 		    (IPIF_UP|IPIF_BROADCAST))
17456 			continue;
17457 
17458 		/*
17459 		 * For each dying IRE that hasn't yet been replaced, see if
17460 		 * `ipif' needs it and whether the IRE should be recreated on
17461 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
17462 		 * will return B_FALSE even if `ipif' needs the IRE on the
17463 		 * hopes that we'll later find a needy non-deprecated ipif.
17464 		 * However, the ipif is recorded in bi_backup for possible
17465 		 * subsequent use by ipif_check_bcast_ires().
17466 		 */
17467 		for (i = 0; i < BCAST_COUNT; i++) {
17468 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
17469 				continue;
17470 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
17471 				continue;
17472 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
17473 		}
17474 
17475 		/*
17476 		 * If we've replaced all of the broadcast IREs that are going
17477 		 * to be taken down, we know we're done.
17478 		 */
17479 		for (i = 0; i < BCAST_COUNT; i++) {
17480 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
17481 				break;
17482 		}
17483 		if (i == BCAST_COUNT)
17484 			break;
17485 	}
17486 	return (irep);
17487 }
17488 
17489 /*
17490  * Check if `test_ipif' (which is going away) is associated with any existing
17491  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
17492  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
17493  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
17494  *
17495  * This is necessary because broadcast IREs are shared.  In particular, a
17496  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
17497  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
17498  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
17499  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
17500  * same zone, they will share the same set of broadcast IREs.
17501  *
17502  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
17503  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
17504  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
17505  */
17506 static void
17507 ipif_check_bcast_ires(ipif_t *test_ipif)
17508 {
17509 	ill_t		*ill = test_ipif->ipif_ill;
17510 	ire_t		*ire, *ire_array[12]; 		/* see note above */
17511 	ire_t		**irep1, **irep = &ire_array[0];
17512 	uint_t 		i, willdie;
17513 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
17514 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
17515 
17516 	ASSERT(!test_ipif->ipif_isv6);
17517 	ASSERT(IAM_WRITER_IPIF(test_ipif));
17518 
17519 	/*
17520 	 * No broadcast IREs for the LOOPBACK interface
17521 	 * or others such as point to point and IPIF_NOXMIT.
17522 	 */
17523 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
17524 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
17525 		return;
17526 
17527 	bzero(bireinfo, sizeof (bireinfo));
17528 	bireinfo[0].bi_type = BCAST_ALLZEROES;
17529 	bireinfo[0].bi_addr = 0;
17530 
17531 	bireinfo[1].bi_type = BCAST_ALLONES;
17532 	bireinfo[1].bi_addr = INADDR_BROADCAST;
17533 
17534 	bireinfo[2].bi_type = BCAST_NET;
17535 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
17536 
17537 	if (test_ipif->ipif_net_mask != 0)
17538 		mask = test_ipif->ipif_net_mask;
17539 	bireinfo[3].bi_type = BCAST_SUBNET;
17540 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
17541 
17542 	/*
17543 	 * Figure out what (if any) broadcast IREs will die as a result of
17544 	 * `test_ipif' going away.  If none will die, we're done.
17545 	 */
17546 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
17547 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
17548 		    test_ipif, ALL_ZONES, NULL,
17549 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
17550 		if (ire != NULL) {
17551 			willdie++;
17552 			bireinfo[i].bi_willdie = 1;
17553 			ire_refrele(ire);
17554 		}
17555 	}
17556 
17557 	if (willdie == 0)
17558 		return;
17559 
17560 	/*
17561 	 * Walk through all the ipifs that will be affected by the dying IREs,
17562 	 * and recreate the IREs as necessary. Note that all interfaces in an
17563 	 * IPMP illgrp share the same broadcast IREs, and thus the entire
17564 	 * illgrp must be walked, starting with the IPMP meta-interface (so
17565 	 * that broadcast IREs end up on it whenever possible).
17566 	 */
17567 	if (IS_UNDER_IPMP(ill))
17568 		ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
17569 
17570 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
17571 
17572 	if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
17573 		ipmp_illgrp_t *illg = ill->ill_grp;
17574 
17575 		ill = list_head(&illg->ig_if);
17576 		for (; ill != NULL; ill = list_next(&illg->ig_if, ill)) {
17577 			for (i = 0; i < BCAST_COUNT; i++) {
17578 				if (bireinfo[i].bi_willdie &&
17579 				    !bireinfo[i].bi_haverep)
17580 					break;
17581 			}
17582 			if (i == BCAST_COUNT)
17583 				break;
17584 
17585 			irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
17586 		}
17587 	}
17588 
17589 	/*
17590 	 * Scan through the set of broadcast IREs and see if there are any
17591 	 * that we need to replace that have not yet been replaced.  If so,
17592 	 * replace them using the appropriate backup ipif.
17593 	 */
17594 	for (i = 0; i < BCAST_COUNT; i++) {
17595 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
17596 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
17597 			    &bireinfo[i], irep);
17598 	}
17599 
17600 	/*
17601 	 * If we can't create all of them, don't add any of them.  (Code in
17602 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
17603 	 * non-loopback copy and loopback copy for a given address.)
17604 	 */
17605 	for (irep1 = irep; irep1 > ire_array; ) {
17606 		irep1--;
17607 		if (*irep1 == NULL) {
17608 			ip0dbg(("ipif_check_bcast_ires: can't create "
17609 			    "IRE_BROADCAST, memory allocation failure\n"));
17610 			while (irep > ire_array) {
17611 				irep--;
17612 				if (*irep != NULL)
17613 					ire_delete(*irep);
17614 			}
17615 			return;
17616 		}
17617 	}
17618 
17619 	for (irep1 = irep; irep1 > ire_array; ) {
17620 		irep1--;
17621 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
17622 			ire_refrele(*irep1);		/* Held in ire_add */
17623 	}
17624 }
17625 
17626 /*
17627  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
17628  * from lifr_flags and the name from lifr_name.
17629  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
17630  * since ipif_lookup_on_name uses the _isv6 flags when matching.
17631  * Returns EINPROGRESS when mp has been consumed by queueing it on
17632  * ill_pending_mp and the ioctl will complete in ip_rput.
17633  *
17634  * Can operate on either a module or a driver queue.
17635  * Returns an error if not a module queue.
17636  */
17637 /* ARGSUSED */
17638 int
17639 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17640     ip_ioctl_cmd_t *ipip, void *if_req)
17641 {
17642 	ill_t	*ill = q->q_ptr;
17643 	phyint_t *phyi;
17644 	ip_stack_t *ipst;
17645 	struct lifreq *lifr = if_req;
17646 
17647 	ASSERT(ipif != NULL);
17648 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
17649 
17650 	if (q->q_next == NULL) {
17651 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
17652 		return (EINVAL);
17653 	}
17654 
17655 	/*
17656 	 * If we are not writer on 'q' then this interface exists already
17657 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
17658 	 * so return EALREADY.
17659 	 */
17660 	if (ill != ipif->ipif_ill)
17661 		return (EALREADY);
17662 
17663 	if (ill->ill_name[0] != '\0')
17664 		return (EALREADY);
17665 
17666 	/*
17667 	 * Set all the flags. Allows all kinds of override. Provide some
17668 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
17669 	 * unless there is either multicast/broadcast support in the driver
17670 	 * or it is a pt-pt link.
17671 	 */
17672 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
17673 		/* Meaningless to IP thus don't allow them to be set. */
17674 		ip1dbg(("ip_setname: EINVAL 1\n"));
17675 		return (EINVAL);
17676 	}
17677 
17678 	/*
17679 	 * If there's another ill already with the requested name, ensure
17680 	 * that it's of the same type.  Otherwise, ill_phyint_reinit() will
17681 	 * fuse together two unrelated ills, which will cause chaos.
17682 	 */
17683 	ipst = ill->ill_ipst;
17684 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
17685 	    lifr->lifr_name, NULL);
17686 	if (phyi != NULL) {
17687 		ill_t *ill_mate = phyi->phyint_illv4;
17688 
17689 		if (ill_mate == NULL)
17690 			ill_mate = phyi->phyint_illv6;
17691 		ASSERT(ill_mate != NULL);
17692 
17693 		if (ill_mate->ill_media->ip_m_mac_type !=
17694 		    ill->ill_media->ip_m_mac_type) {
17695 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
17696 			    "use the same ill name on differing media\n"));
17697 			return (EINVAL);
17698 		}
17699 	}
17700 
17701 	/*
17702 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
17703 	 * ill_bcast_addr_length info.
17704 	 */
17705 	if (!ill->ill_needs_attach &&
17706 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
17707 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
17708 	    ill->ill_bcast_addr_length == 0)) {
17709 		/* Link not broadcast/pt-pt capable i.e. no multicast */
17710 		ip1dbg(("ip_setname: EINVAL 2\n"));
17711 		return (EINVAL);
17712 	}
17713 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
17714 	    ((lifr->lifr_flags & IFF_IPV6) ||
17715 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
17716 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
17717 		ip1dbg(("ip_setname: EINVAL 3\n"));
17718 		return (EINVAL);
17719 	}
17720 	if (lifr->lifr_flags & IFF_UP) {
17721 		/* Can only be set with SIOCSLIFFLAGS */
17722 		ip1dbg(("ip_setname: EINVAL 4\n"));
17723 		return (EINVAL);
17724 	}
17725 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
17726 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
17727 		ip1dbg(("ip_setname: EINVAL 5\n"));
17728 		return (EINVAL);
17729 	}
17730 	/*
17731 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
17732 	 */
17733 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
17734 	    !(lifr->lifr_flags & IFF_IPV6) &&
17735 	    !(ipif->ipif_isv6)) {
17736 		ip1dbg(("ip_setname: EINVAL 6\n"));
17737 		return (EINVAL);
17738 	}
17739 
17740 	/*
17741 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
17742 	 * we have all the flags here. So, we assign rather than we OR.
17743 	 * We can't OR the flags here because we don't want to set
17744 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
17745 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
17746 	 * on lifr_flags value here.
17747 	 */
17748 	/*
17749 	 * This ill has not been inserted into the global list.
17750 	 * So we are still single threaded and don't need any lock
17751 	 */
17752 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS & ~IFF_DUPLICATE;
17753 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
17754 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
17755 
17756 	/* We started off as V4. */
17757 	if (ill->ill_flags & ILLF_IPV6) {
17758 		ill->ill_phyint->phyint_illv6 = ill;
17759 		ill->ill_phyint->phyint_illv4 = NULL;
17760 	}
17761 
17762 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
17763 }
17764 
17765 /* ARGSUSED */
17766 int
17767 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17768     ip_ioctl_cmd_t *ipip, void *if_req)
17769 {
17770 	/*
17771 	 * ill_phyint_reinit merged the v4 and v6 into a single
17772 	 * ipsq.  We might not have been able to complete the
17773 	 * slifname in ipif_set_values, if we could not become
17774 	 * exclusive.  If so restart it here
17775 	 */
17776 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
17777 }
17778 
17779 /*
17780  * Return a pointer to the ipif which matches the index, IP version type and
17781  * zoneid.
17782  */
17783 ipif_t *
17784 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
17785     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
17786 {
17787 	ill_t	*ill;
17788 	ipif_t	*ipif = NULL;
17789 
17790 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
17791 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
17792 
17793 	if (err != NULL)
17794 		*err = 0;
17795 
17796 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
17797 	if (ill != NULL) {
17798 		mutex_enter(&ill->ill_lock);
17799 		for (ipif = ill->ill_ipif; ipif != NULL;
17800 		    ipif = ipif->ipif_next) {
17801 			if (IPIF_CAN_LOOKUP(ipif) && (zoneid == ALL_ZONES ||
17802 			    zoneid == ipif->ipif_zoneid ||
17803 			    ipif->ipif_zoneid == ALL_ZONES)) {
17804 				ipif_refhold_locked(ipif);
17805 				break;
17806 			}
17807 		}
17808 		mutex_exit(&ill->ill_lock);
17809 		ill_refrele(ill);
17810 		if (ipif == NULL && err != NULL)
17811 			*err = ENXIO;
17812 	}
17813 	return (ipif);
17814 }
17815 
17816 /*
17817  * We first need to ensure that the new index is unique, and
17818  * then carry the change across both v4 and v6 ill representation
17819  * of the physical interface.
17820  */
17821 /* ARGSUSED */
17822 int
17823 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17824     ip_ioctl_cmd_t *ipip, void *ifreq)
17825 {
17826 	ill_t		*ill;
17827 	phyint_t	*phyi;
17828 	struct ifreq	*ifr = (struct ifreq *)ifreq;
17829 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17830 	uint_t	old_index, index;
17831 	ill_t	*ill_v4;
17832 	ill_t	*ill_v6;
17833 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
17834 
17835 	if (ipip->ipi_cmd_type == IF_CMD)
17836 		index = ifr->ifr_index;
17837 	else
17838 		index = lifr->lifr_index;
17839 
17840 	/*
17841 	 * Only allow on physical interface. Also, index zero is illegal.
17842 	 */
17843 	ill = ipif->ipif_ill;
17844 	phyi = ill->ill_phyint;
17845 	if (ipif->ipif_id != 0 || index == 0) {
17846 		return (EINVAL);
17847 	}
17848 
17849 	/* If the index is not changing, no work to do */
17850 	if (phyi->phyint_ifindex == index)
17851 		return (0);
17852 
17853 	/*
17854 	 * Use ill_lookup_on_ifindex to determine if the
17855 	 * new index is unused and if so allow the change.
17856 	 */
17857 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
17858 	    ipst);
17859 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
17860 	    ipst);
17861 	if (ill_v6 != NULL || ill_v4 != NULL) {
17862 		if (ill_v4 != NULL)
17863 			ill_refrele(ill_v4);
17864 		if (ill_v6 != NULL)
17865 			ill_refrele(ill_v6);
17866 		return (EBUSY);
17867 	}
17868 
17869 	/* The new index is unused. Set it in the phyint. */
17870 	old_index = phyi->phyint_ifindex;
17871 	phyi->phyint_ifindex = index;
17872 
17873 	/* Update SCTP's ILL list */
17874 	sctp_ill_reindex(ill, old_index);
17875 
17876 	/* Send the routing sockets message */
17877 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
17878 	if (ILL_OTHER(ill))
17879 		ip_rts_ifmsg(ILL_OTHER(ill)->ill_ipif, RTSQ_DEFAULT);
17880 
17881 	return (0);
17882 }
17883 
17884 /* ARGSUSED */
17885 int
17886 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17887     ip_ioctl_cmd_t *ipip, void *ifreq)
17888 {
17889 	struct ifreq	*ifr = (struct ifreq *)ifreq;
17890 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17891 
17892 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
17893 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17894 	/* Get the interface index */
17895 	if (ipip->ipi_cmd_type == IF_CMD) {
17896 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
17897 	} else {
17898 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
17899 	}
17900 	return (0);
17901 }
17902 
17903 /* ARGSUSED */
17904 int
17905 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17906     ip_ioctl_cmd_t *ipip, void *ifreq)
17907 {
17908 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17909 
17910 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
17911 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17912 	/* Get the interface zone */
17913 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
17914 	lifr->lifr_zoneid = ipif->ipif_zoneid;
17915 	return (0);
17916 }
17917 
17918 /*
17919  * Set the zoneid of an interface.
17920  */
17921 /* ARGSUSED */
17922 int
17923 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17924     ip_ioctl_cmd_t *ipip, void *ifreq)
17925 {
17926 	struct lifreq	*lifr = (struct lifreq *)ifreq;
17927 	int err = 0;
17928 	boolean_t need_up = B_FALSE;
17929 	zone_t *zptr;
17930 	zone_status_t status;
17931 	zoneid_t zoneid;
17932 
17933 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
17934 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
17935 		if (!is_system_labeled())
17936 			return (ENOTSUP);
17937 		zoneid = GLOBAL_ZONEID;
17938 	}
17939 
17940 	/* cannot assign instance zero to a non-global zone */
17941 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
17942 		return (ENOTSUP);
17943 
17944 	/*
17945 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
17946 	 * the event of a race with the zone shutdown processing, since IP
17947 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
17948 	 * interface will be cleaned up even if the zone is shut down
17949 	 * immediately after the status check. If the interface can't be brought
17950 	 * down right away, and the zone is shut down before the restart
17951 	 * function is called, we resolve the possible races by rechecking the
17952 	 * zone status in the restart function.
17953 	 */
17954 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
17955 		return (EINVAL);
17956 	status = zone_status_get(zptr);
17957 	zone_rele(zptr);
17958 
17959 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
17960 		return (EINVAL);
17961 
17962 	if (ipif->ipif_flags & IPIF_UP) {
17963 		/*
17964 		 * If the interface is already marked up,
17965 		 * we call ipif_down which will take care
17966 		 * of ditching any IREs that have been set
17967 		 * up based on the old interface address.
17968 		 */
17969 		err = ipif_logical_down(ipif, q, mp);
17970 		if (err == EINPROGRESS)
17971 			return (err);
17972 		ipif_down_tail(ipif);
17973 		need_up = B_TRUE;
17974 	}
17975 
17976 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
17977 	return (err);
17978 }
17979 
17980 static int
17981 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
17982     queue_t *q, mblk_t *mp, boolean_t need_up)
17983 {
17984 	int	err = 0;
17985 	ip_stack_t	*ipst;
17986 
17987 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
17988 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
17989 
17990 	if (CONN_Q(q))
17991 		ipst = CONNQ_TO_IPST(q);
17992 	else
17993 		ipst = ILLQ_TO_IPST(q);
17994 
17995 	/*
17996 	 * For exclusive stacks we don't allow a different zoneid than
17997 	 * global.
17998 	 */
17999 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
18000 	    zoneid != GLOBAL_ZONEID)
18001 		return (EINVAL);
18002 
18003 	/* Set the new zone id. */
18004 	ipif->ipif_zoneid = zoneid;
18005 
18006 	/* Update sctp list */
18007 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
18008 
18009 	if (need_up) {
18010 		/*
18011 		 * Now bring the interface back up.  If this
18012 		 * is the only IPIF for the ILL, ipif_up
18013 		 * will have to re-bind to the device, so
18014 		 * we may get back EINPROGRESS, in which
18015 		 * case, this IOCTL will get completed in
18016 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
18017 		 */
18018 		err = ipif_up(ipif, q, mp);
18019 	}
18020 	return (err);
18021 }
18022 
18023 /* ARGSUSED */
18024 int
18025 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18026     ip_ioctl_cmd_t *ipip, void *if_req)
18027 {
18028 	struct lifreq *lifr = (struct lifreq *)if_req;
18029 	zoneid_t zoneid;
18030 	zone_t *zptr;
18031 	zone_status_t status;
18032 
18033 	ASSERT(ipif->ipif_id != 0);
18034 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
18035 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
18036 		zoneid = GLOBAL_ZONEID;
18037 
18038 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
18039 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
18040 
18041 	/*
18042 	 * We recheck the zone status to resolve the following race condition:
18043 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
18044 	 * 2) hme0:1 is up and can't be brought down right away;
18045 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
18046 	 * 3) zone "myzone" is halted; the zone status switches to
18047 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
18048 	 * the interfaces to remove - hme0:1 is not returned because it's not
18049 	 * yet in "myzone", so it won't be removed;
18050 	 * 4) the restart function for SIOCSLIFZONE is called; without the
18051 	 * status check here, we would have hme0:1 in "myzone" after it's been
18052 	 * destroyed.
18053 	 * Note that if the status check fails, we need to bring the interface
18054 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
18055 	 * ipif_up_done[_v6]().
18056 	 */
18057 	status = ZONE_IS_UNINITIALIZED;
18058 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
18059 		status = zone_status_get(zptr);
18060 		zone_rele(zptr);
18061 	}
18062 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
18063 		if (ipif->ipif_isv6) {
18064 			(void) ipif_up_done_v6(ipif);
18065 		} else {
18066 			(void) ipif_up_done(ipif);
18067 		}
18068 		return (EINVAL);
18069 	}
18070 
18071 	ipif_down_tail(ipif);
18072 
18073 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
18074 	    B_TRUE));
18075 }
18076 
18077 /*
18078  * Return the number of addresses on `ill' with one or more of the values
18079  * in `set' set and all of the values in `clear' clear.
18080  */
18081 static uint_t
18082 ill_flagaddr_cnt(const ill_t *ill, uint64_t set, uint64_t clear)
18083 {
18084 	ipif_t	*ipif;
18085 	uint_t	cnt = 0;
18086 
18087 	ASSERT(IAM_WRITER_ILL(ill));
18088 
18089 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
18090 		if ((ipif->ipif_flags & set) && !(ipif->ipif_flags & clear))
18091 			cnt++;
18092 
18093 	return (cnt);
18094 }
18095 
18096 /*
18097  * Return the number of migratable addresses on `ill' that are under
18098  * application control.
18099  */
18100 uint_t
18101 ill_appaddr_cnt(const ill_t *ill)
18102 {
18103 	return (ill_flagaddr_cnt(ill, IPIF_DHCPRUNNING | IPIF_ADDRCONF,
18104 	    IPIF_NOFAILOVER));
18105 }
18106 
18107 /*
18108  * Return the number of point-to-point addresses on `ill'.
18109  */
18110 uint_t
18111 ill_ptpaddr_cnt(const ill_t *ill)
18112 {
18113 	return (ill_flagaddr_cnt(ill, IPIF_POINTOPOINT, 0));
18114 }
18115 
18116 /* ARGSUSED */
18117 int
18118 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18119 	ip_ioctl_cmd_t *ipip, void *ifreq)
18120 {
18121 	struct lifreq	*lifr = ifreq;
18122 
18123 	ASSERT(q->q_next == NULL);
18124 	ASSERT(CONN_Q(q));
18125 
18126 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
18127 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
18128 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
18129 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
18130 
18131 	return (0);
18132 }
18133 
18134 /* Find the previous ILL in this usesrc group */
18135 static ill_t *
18136 ill_prev_usesrc(ill_t *uill)
18137 {
18138 	ill_t *ill;
18139 
18140 	for (ill = uill->ill_usesrc_grp_next;
18141 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
18142 	    ill = ill->ill_usesrc_grp_next)
18143 		/* do nothing */;
18144 	return (ill);
18145 }
18146 
18147 /*
18148  * Release all members of the usesrc group. This routine is called
18149  * from ill_delete when the interface being unplumbed is the
18150  * group head.
18151  */
18152 static void
18153 ill_disband_usesrc_group(ill_t *uill)
18154 {
18155 	ill_t *next_ill, *tmp_ill;
18156 	ip_stack_t	*ipst = uill->ill_ipst;
18157 
18158 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
18159 	next_ill = uill->ill_usesrc_grp_next;
18160 
18161 	do {
18162 		ASSERT(next_ill != NULL);
18163 		tmp_ill = next_ill->ill_usesrc_grp_next;
18164 		ASSERT(tmp_ill != NULL);
18165 		next_ill->ill_usesrc_grp_next = NULL;
18166 		next_ill->ill_usesrc_ifindex = 0;
18167 		next_ill = tmp_ill;
18168 	} while (next_ill->ill_usesrc_ifindex != 0);
18169 	uill->ill_usesrc_grp_next = NULL;
18170 }
18171 
18172 /*
18173  * Remove the client usesrc ILL from the list and relink to a new list
18174  */
18175 int
18176 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
18177 {
18178 	ill_t *ill, *tmp_ill;
18179 	ip_stack_t	*ipst = ucill->ill_ipst;
18180 
18181 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
18182 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
18183 
18184 	/*
18185 	 * Check if the usesrc client ILL passed in is not already
18186 	 * in use as a usesrc ILL i.e one whose source address is
18187 	 * in use OR a usesrc ILL is not already in use as a usesrc
18188 	 * client ILL
18189 	 */
18190 	if ((ucill->ill_usesrc_ifindex == 0) ||
18191 	    (uill->ill_usesrc_ifindex != 0)) {
18192 		return (-1);
18193 	}
18194 
18195 	ill = ill_prev_usesrc(ucill);
18196 	ASSERT(ill->ill_usesrc_grp_next != NULL);
18197 
18198 	/* Remove from the current list */
18199 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
18200 		/* Only two elements in the list */
18201 		ASSERT(ill->ill_usesrc_ifindex == 0);
18202 		ill->ill_usesrc_grp_next = NULL;
18203 	} else {
18204 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
18205 	}
18206 
18207 	if (ifindex == 0) {
18208 		ucill->ill_usesrc_ifindex = 0;
18209 		ucill->ill_usesrc_grp_next = NULL;
18210 		return (0);
18211 	}
18212 
18213 	ucill->ill_usesrc_ifindex = ifindex;
18214 	tmp_ill = uill->ill_usesrc_grp_next;
18215 	uill->ill_usesrc_grp_next = ucill;
18216 	ucill->ill_usesrc_grp_next =
18217 	    (tmp_ill != NULL) ? tmp_ill : uill;
18218 	return (0);
18219 }
18220 
18221 /*
18222  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
18223  * ip.c for locking details.
18224  */
18225 /* ARGSUSED */
18226 int
18227 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18228     ip_ioctl_cmd_t *ipip, void *ifreq)
18229 {
18230 	struct lifreq *lifr = (struct lifreq *)ifreq;
18231 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
18232 	    ill_flag_changed = B_FALSE;
18233 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
18234 	int err = 0, ret;
18235 	uint_t ifindex;
18236 	ipsq_t *ipsq = NULL;
18237 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
18238 
18239 	ASSERT(IAM_WRITER_IPIF(ipif));
18240 	ASSERT(q->q_next == NULL);
18241 	ASSERT(CONN_Q(q));
18242 
18243 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
18244 
18245 	ifindex = lifr->lifr_index;
18246 	if (ifindex == 0) {
18247 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
18248 			/* non usesrc group interface, nothing to reset */
18249 			return (0);
18250 		}
18251 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
18252 		/* valid reset request */
18253 		reset_flg = B_TRUE;
18254 	}
18255 
18256 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
18257 	    ip_process_ioctl, &err, ipst);
18258 	if (usesrc_ill == NULL) {
18259 		return (err);
18260 	}
18261 
18262 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
18263 	    NEW_OP, B_TRUE);
18264 	if (ipsq == NULL) {
18265 		err = EINPROGRESS;
18266 		/* Operation enqueued on the ipsq of the usesrc ILL */
18267 		goto done;
18268 	}
18269 
18270 	/* USESRC isn't currently supported with IPMP */
18271 	if (IS_IPMP(usesrc_ill) || IS_UNDER_IPMP(usesrc_ill)) {
18272 		err = ENOTSUP;
18273 		goto done;
18274 	}
18275 
18276 	/*
18277 	 * USESRC isn't compatible with the STANDBY flag.  (STANDBY is only
18278 	 * used by IPMP underlying interfaces, but someone might think it's
18279 	 * more general and try to use it independently with VNI.)
18280 	 */
18281 	if (usesrc_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
18282 		err = ENOTSUP;
18283 		goto done;
18284 	}
18285 
18286 	/*
18287 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
18288 	 * already a client then return EINVAL
18289 	 */
18290 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
18291 		err = EINVAL;
18292 		goto done;
18293 	}
18294 
18295 	/*
18296 	 * If the ill_usesrc_ifindex field is already set to what it needs to
18297 	 * be then this is a duplicate operation.
18298 	 */
18299 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
18300 		err = 0;
18301 		goto done;
18302 	}
18303 
18304 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
18305 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
18306 	    usesrc_ill->ill_isv6));
18307 
18308 	/*
18309 	 * The next step ensures that no new ires will be created referencing
18310 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
18311 	 * we go through an ire walk deleting all ire caches that reference
18312 	 * the client ill. New ires referencing the client ill that are added
18313 	 * to the ire table before the ILL_CHANGING flag is set, will be
18314 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
18315 	 * the client ill while the ILL_CHANGING flag is set will be failed
18316 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
18317 	 * checks (under the ill_g_usesrc_lock) that the ire being added
18318 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
18319 	 * belong to the same usesrc group.
18320 	 */
18321 	mutex_enter(&usesrc_cli_ill->ill_lock);
18322 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
18323 	mutex_exit(&usesrc_cli_ill->ill_lock);
18324 	ill_flag_changed = B_TRUE;
18325 
18326 	if (ipif->ipif_isv6)
18327 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
18328 		    ALL_ZONES, ipst);
18329 	else
18330 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
18331 		    ALL_ZONES, ipst);
18332 
18333 	/*
18334 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
18335 	 * and the ill_usesrc_ifindex fields
18336 	 */
18337 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
18338 
18339 	if (reset_flg) {
18340 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
18341 		if (ret != 0) {
18342 			err = EINVAL;
18343 		}
18344 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
18345 		goto done;
18346 	}
18347 
18348 	/*
18349 	 * Four possibilities to consider:
18350 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
18351 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
18352 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
18353 	 * 4. Both are part of their respective usesrc groups
18354 	 */
18355 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
18356 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
18357 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
18358 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
18359 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
18360 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
18361 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
18362 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
18363 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
18364 		/* Insert at head of list */
18365 		usesrc_cli_ill->ill_usesrc_grp_next =
18366 		    usesrc_ill->ill_usesrc_grp_next;
18367 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
18368 	} else {
18369 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
18370 		    ifindex);
18371 		if (ret != 0)
18372 			err = EINVAL;
18373 	}
18374 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
18375 
18376 done:
18377 	if (ill_flag_changed) {
18378 		mutex_enter(&usesrc_cli_ill->ill_lock);
18379 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
18380 		mutex_exit(&usesrc_cli_ill->ill_lock);
18381 	}
18382 	if (ipsq != NULL)
18383 		ipsq_exit(ipsq);
18384 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
18385 	ill_refrele(usesrc_ill);
18386 	return (err);
18387 }
18388 
18389 /*
18390  * comparison function used by avl.
18391  */
18392 static int
18393 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
18394 {
18395 
18396 	uint_t index;
18397 
18398 	ASSERT(phyip != NULL && index_ptr != NULL);
18399 
18400 	index = *((uint_t *)index_ptr);
18401 	/*
18402 	 * let the phyint with the lowest index be on top.
18403 	 */
18404 	if (((phyint_t *)phyip)->phyint_ifindex < index)
18405 		return (1);
18406 	if (((phyint_t *)phyip)->phyint_ifindex > index)
18407 		return (-1);
18408 	return (0);
18409 }
18410 
18411 /*
18412  * comparison function used by avl.
18413  */
18414 static int
18415 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
18416 {
18417 	ill_t *ill;
18418 	int res = 0;
18419 
18420 	ASSERT(phyip != NULL && name_ptr != NULL);
18421 
18422 	if (((phyint_t *)phyip)->phyint_illv4)
18423 		ill = ((phyint_t *)phyip)->phyint_illv4;
18424 	else
18425 		ill = ((phyint_t *)phyip)->phyint_illv6;
18426 	ASSERT(ill != NULL);
18427 
18428 	res = strcmp(ill->ill_name, (char *)name_ptr);
18429 	if (res > 0)
18430 		return (1);
18431 	else if (res < 0)
18432 		return (-1);
18433 	return (0);
18434 }
18435 
18436 /*
18437  * This function is called on the unplumb path via ill_glist_delete() when
18438  * there are no ills left on the phyint and thus the phyint can be freed.
18439  */
18440 static void
18441 phyint_free(phyint_t *phyi)
18442 {
18443 	ip_stack_t *ipst = PHYINT_TO_IPST(phyi);
18444 
18445 	ASSERT(phyi->phyint_illv4 == NULL && phyi->phyint_illv6 == NULL);
18446 
18447 	/*
18448 	 * If this phyint was an IPMP meta-interface, blow away the group.
18449 	 * This is safe to do because all of the illgrps have already been
18450 	 * removed by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find us.
18451 	 * If we're cleaning up as a result of failed initialization,
18452 	 * phyint_grp may be NULL.
18453 	 */
18454 	if ((phyi->phyint_flags & PHYI_IPMP) && (phyi->phyint_grp != NULL)) {
18455 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
18456 		ipmp_grp_destroy(phyi->phyint_grp);
18457 		phyi->phyint_grp = NULL;
18458 		rw_exit(&ipst->ips_ipmp_lock);
18459 	}
18460 
18461 	/*
18462 	 * If this interface was under IPMP, take it out of the group.
18463 	 */
18464 	if (phyi->phyint_grp != NULL)
18465 		ipmp_phyint_leave_grp(phyi);
18466 
18467 	/*
18468 	 * Delete the phyint and disassociate its ipsq.  The ipsq itself
18469 	 * will be freed in ipsq_exit().
18470 	 */
18471 	phyi->phyint_ipsq->ipsq_phyint = NULL;
18472 	phyi->phyint_name[0] = '\0';
18473 
18474 	mi_free(phyi);
18475 }
18476 
18477 /*
18478  * Attach the ill to the phyint structure which can be shared by both
18479  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
18480  * function is called from ipif_set_values and ill_lookup_on_name (for
18481  * loopback) where we know the name of the ill. We lookup the ill and if
18482  * there is one present already with the name use that phyint. Otherwise
18483  * reuse the one allocated by ill_init.
18484  */
18485 static void
18486 ill_phyint_reinit(ill_t *ill)
18487 {
18488 	boolean_t isv6 = ill->ill_isv6;
18489 	phyint_t *phyi_old;
18490 	phyint_t *phyi;
18491 	avl_index_t where = 0;
18492 	ill_t	*ill_other = NULL;
18493 	ip_stack_t	*ipst = ill->ill_ipst;
18494 
18495 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
18496 
18497 	phyi_old = ill->ill_phyint;
18498 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
18499 	    phyi_old->phyint_illv6 == NULL));
18500 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
18501 	    phyi_old->phyint_illv4 == NULL));
18502 	ASSERT(phyi_old->phyint_ifindex == 0);
18503 
18504 	/*
18505 	 * Now that our ill has a name, set it in the phyint.
18506 	 */
18507 	(void) strlcpy(ill->ill_phyint->phyint_name, ill->ill_name, LIFNAMSIZ);
18508 
18509 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
18510 	    ill->ill_name, &where);
18511 
18512 	/*
18513 	 * 1. We grabbed the ill_g_lock before inserting this ill into
18514 	 *    the global list of ills. So no other thread could have located
18515 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
18516 	 * 2. Now locate the other protocol instance of this ill.
18517 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
18518 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
18519 	 *    of neither ill can change.
18520 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
18521 	 *    other ill.
18522 	 * 5. Release all locks.
18523 	 */
18524 
18525 	/*
18526 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
18527 	 * we are initializing IPv4.
18528 	 */
18529 	if (phyi != NULL) {
18530 		ill_other = (isv6) ? phyi->phyint_illv4 : phyi->phyint_illv6;
18531 		ASSERT(ill_other->ill_phyint != NULL);
18532 		ASSERT((isv6 && !ill_other->ill_isv6) ||
18533 		    (!isv6 && ill_other->ill_isv6));
18534 		GRAB_ILL_LOCKS(ill, ill_other);
18535 		/*
18536 		 * We are potentially throwing away phyint_flags which
18537 		 * could be different from the one that we obtain from
18538 		 * ill_other->ill_phyint. But it is okay as we are assuming
18539 		 * that the state maintained within IP is correct.
18540 		 */
18541 		mutex_enter(&phyi->phyint_lock);
18542 		if (isv6) {
18543 			ASSERT(phyi->phyint_illv6 == NULL);
18544 			phyi->phyint_illv6 = ill;
18545 		} else {
18546 			ASSERT(phyi->phyint_illv4 == NULL);
18547 			phyi->phyint_illv4 = ill;
18548 		}
18549 
18550 		/*
18551 		 * Delete the old phyint and make its ipsq eligible
18552 		 * to be freed in ipsq_exit().
18553 		 */
18554 		phyi_old->phyint_illv4 = NULL;
18555 		phyi_old->phyint_illv6 = NULL;
18556 		phyi_old->phyint_ipsq->ipsq_phyint = NULL;
18557 		phyi_old->phyint_name[0] = '\0';
18558 		mi_free(phyi_old);
18559 	} else {
18560 		mutex_enter(&ill->ill_lock);
18561 		/*
18562 		 * We don't need to acquire any lock, since
18563 		 * the ill is not yet visible globally  and we
18564 		 * have not yet released the ill_g_lock.
18565 		 */
18566 		phyi = phyi_old;
18567 		mutex_enter(&phyi->phyint_lock);
18568 		/* XXX We need a recovery strategy here. */
18569 		if (!phyint_assign_ifindex(phyi, ipst))
18570 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
18571 
18572 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
18573 		    (void *)phyi, where);
18574 
18575 		(void) avl_find(&ipst->ips_phyint_g_list->
18576 		    phyint_list_avl_by_index,
18577 		    &phyi->phyint_ifindex, &where);
18578 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
18579 		    (void *)phyi, where);
18580 	}
18581 
18582 	/*
18583 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
18584 	 * pending mp is not affected because that is per ill basis.
18585 	 */
18586 	ill->ill_phyint = phyi;
18587 
18588 	/*
18589 	 * Now that the phyint's ifindex has been assigned, complete the
18590 	 * remaining
18591 	 */
18592 
18593 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
18594 	if (ill->ill_isv6) {
18595 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
18596 		    ill->ill_phyint->phyint_ifindex;
18597 		ill->ill_mcast_type = ipst->ips_mld_max_version;
18598 	} else {
18599 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
18600 	}
18601 
18602 	/*
18603 	 * Generate an event within the hooks framework to indicate that
18604 	 * a new interface has just been added to IP.  For this event to
18605 	 * be generated, the network interface must, at least, have an
18606 	 * ifindex assigned to it.
18607 	 *
18608 	 * This needs to be run inside the ill_g_lock perimeter to ensure
18609 	 * that the ordering of delivered events to listeners matches the
18610 	 * order of them in the kernel.
18611 	 *
18612 	 * This function could be called from ill_lookup_on_name. In that case
18613 	 * the interface is loopback "lo", which will not generate a NIC event.
18614 	 */
18615 	if (ill->ill_name_length <= 2 ||
18616 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
18617 		ill_nic_event_dispatch(ill, 0, NE_PLUMB, ill->ill_name,
18618 		    ill->ill_name_length);
18619 	}
18620 	RELEASE_ILL_LOCKS(ill, ill_other);
18621 	mutex_exit(&phyi->phyint_lock);
18622 }
18623 
18624 /*
18625  * Notify any downstream modules of the name of this interface.
18626  * An M_IOCTL is used even though we don't expect a successful reply.
18627  * Any reply message from the driver (presumably an M_IOCNAK) will
18628  * eventually get discarded somewhere upstream.  The message format is
18629  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
18630  * to IP.
18631  */
18632 static void
18633 ip_ifname_notify(ill_t *ill, queue_t *q)
18634 {
18635 	mblk_t *mp1, *mp2;
18636 	struct iocblk *iocp;
18637 	struct lifreq *lifr;
18638 
18639 	mp1 = mkiocb(SIOCSLIFNAME);
18640 	if (mp1 == NULL)
18641 		return;
18642 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
18643 	if (mp2 == NULL) {
18644 		freeb(mp1);
18645 		return;
18646 	}
18647 
18648 	mp1->b_cont = mp2;
18649 	iocp = (struct iocblk *)mp1->b_rptr;
18650 	iocp->ioc_count = sizeof (struct lifreq);
18651 
18652 	lifr = (struct lifreq *)mp2->b_rptr;
18653 	mp2->b_wptr += sizeof (struct lifreq);
18654 	bzero(lifr, sizeof (struct lifreq));
18655 
18656 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
18657 	lifr->lifr_ppa = ill->ill_ppa;
18658 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
18659 
18660 	putnext(q, mp1);
18661 }
18662 
18663 static int
18664 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
18665 {
18666 	int		err;
18667 	ip_stack_t	*ipst = ill->ill_ipst;
18668 	phyint_t	*phyi = ill->ill_phyint;
18669 
18670 	/* Set the obsolete NDD per-interface forwarding name. */
18671 	err = ill_set_ndd_name(ill);
18672 	if (err != 0) {
18673 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
18674 		    err);
18675 	}
18676 
18677 	/*
18678 	 * Now that ill_name is set, the configuration for the IPMP
18679 	 * meta-interface can be performed.
18680 	 */
18681 	if (IS_IPMP(ill)) {
18682 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
18683 		/*
18684 		 * If phyi->phyint_grp is NULL, then this is the first IPMP
18685 		 * meta-interface and we need to create the IPMP group.
18686 		 */
18687 		if (phyi->phyint_grp == NULL) {
18688 			/*
18689 			 * If someone has renamed another IPMP group to have
18690 			 * the same name as our interface, bail.
18691 			 */
18692 			if (ipmp_grp_lookup(ill->ill_name, ipst) != NULL) {
18693 				rw_exit(&ipst->ips_ipmp_lock);
18694 				return (EEXIST);
18695 			}
18696 			phyi->phyint_grp = ipmp_grp_create(ill->ill_name, phyi);
18697 			if (phyi->phyint_grp == NULL) {
18698 				rw_exit(&ipst->ips_ipmp_lock);
18699 				return (ENOMEM);
18700 			}
18701 		}
18702 		rw_exit(&ipst->ips_ipmp_lock);
18703 	}
18704 
18705 	/* Tell downstream modules where they are. */
18706 	ip_ifname_notify(ill, q);
18707 
18708 	/*
18709 	 * ill_dl_phys returns EINPROGRESS in the usual case.
18710 	 * Error cases are ENOMEM ...
18711 	 */
18712 	err = ill_dl_phys(ill, ipif, mp, q);
18713 
18714 	/*
18715 	 * If there is no IRE expiration timer running, get one started.
18716 	 * igmp and mld timers will be triggered by the first multicast
18717 	 */
18718 	if (ipst->ips_ip_ire_expire_id == 0) {
18719 		/*
18720 		 * acquire the lock and check again.
18721 		 */
18722 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
18723 		if (ipst->ips_ip_ire_expire_id == 0) {
18724 			ipst->ips_ip_ire_expire_id = timeout(
18725 			    ip_trash_timer_expire, ipst,
18726 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
18727 		}
18728 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
18729 	}
18730 
18731 	if (ill->ill_isv6) {
18732 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
18733 		if (ipst->ips_mld_slowtimeout_id == 0) {
18734 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
18735 			    (void *)ipst,
18736 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
18737 		}
18738 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
18739 	} else {
18740 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
18741 		if (ipst->ips_igmp_slowtimeout_id == 0) {
18742 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
18743 			    (void *)ipst,
18744 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
18745 		}
18746 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
18747 	}
18748 
18749 	return (err);
18750 }
18751 
18752 /*
18753  * Common routine for ppa and ifname setting. Should be called exclusive.
18754  *
18755  * Returns EINPROGRESS when mp has been consumed by queueing it on
18756  * ill_pending_mp and the ioctl will complete in ip_rput.
18757  *
18758  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
18759  * the new name and new ppa in lifr_name and lifr_ppa respectively.
18760  * For SLIFNAME, we pass these values back to the userland.
18761  */
18762 static int
18763 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
18764 {
18765 	ill_t	*ill;
18766 	ipif_t	*ipif;
18767 	ipsq_t	*ipsq;
18768 	char	*ppa_ptr;
18769 	char	*old_ptr;
18770 	char	old_char;
18771 	int	error;
18772 	ip_stack_t	*ipst;
18773 
18774 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
18775 	ASSERT(q->q_next != NULL);
18776 	ASSERT(interf_name != NULL);
18777 
18778 	ill = (ill_t *)q->q_ptr;
18779 	ipst = ill->ill_ipst;
18780 
18781 	ASSERT(ill->ill_ipst != NULL);
18782 	ASSERT(ill->ill_name[0] == '\0');
18783 	ASSERT(IAM_WRITER_ILL(ill));
18784 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
18785 	ASSERT(ill->ill_ppa == UINT_MAX);
18786 
18787 	/* The ppa is sent down by ifconfig or is chosen */
18788 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
18789 		return (EINVAL);
18790 	}
18791 
18792 	/*
18793 	 * make sure ppa passed in is same as ppa in the name.
18794 	 * This check is not made when ppa == UINT_MAX in that case ppa
18795 	 * in the name could be anything. System will choose a ppa and
18796 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
18797 	 */
18798 	if (*new_ppa_ptr != UINT_MAX) {
18799 		/* stoi changes the pointer */
18800 		old_ptr = ppa_ptr;
18801 		/*
18802 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
18803 		 * (they don't have an externally visible ppa).  We assign one
18804 		 * here so that we can manage the interface.  Note that in
18805 		 * the past this value was always 0 for DLPI 1 drivers.
18806 		 */
18807 		if (*new_ppa_ptr == 0)
18808 			*new_ppa_ptr = stoi(&old_ptr);
18809 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
18810 			return (EINVAL);
18811 	}
18812 	/*
18813 	 * terminate string before ppa
18814 	 * save char at that location.
18815 	 */
18816 	old_char = ppa_ptr[0];
18817 	ppa_ptr[0] = '\0';
18818 
18819 	ill->ill_ppa = *new_ppa_ptr;
18820 	/*
18821 	 * Finish as much work now as possible before calling ill_glist_insert
18822 	 * which makes the ill globally visible and also merges it with the
18823 	 * other protocol instance of this phyint. The remaining work is
18824 	 * done after entering the ipsq which may happen sometime later.
18825 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
18826 	 */
18827 	ipif = ill->ill_ipif;
18828 
18829 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
18830 	ipif_assign_seqid(ipif);
18831 
18832 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
18833 		ill->ill_flags |= ILLF_IPV4;
18834 
18835 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
18836 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
18837 
18838 	if (ill->ill_flags & ILLF_IPV6) {
18839 
18840 		ill->ill_isv6 = B_TRUE;
18841 		if (ill->ill_rq != NULL) {
18842 			ill->ill_rq->q_qinfo = &iprinitv6;
18843 			ill->ill_wq->q_qinfo = &ipwinitv6;
18844 		}
18845 
18846 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
18847 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
18848 		ipif->ipif_v6src_addr = ipv6_all_zeros;
18849 		ipif->ipif_v6subnet = ipv6_all_zeros;
18850 		ipif->ipif_v6net_mask = ipv6_all_zeros;
18851 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
18852 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
18853 		/*
18854 		 * point-to-point or Non-mulicast capable
18855 		 * interfaces won't do NUD unless explicitly
18856 		 * configured to do so.
18857 		 */
18858 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
18859 		    !(ill->ill_flags & ILLF_MULTICAST)) {
18860 			ill->ill_flags |= ILLF_NONUD;
18861 		}
18862 		/* Make sure IPv4 specific flag is not set on IPv6 if */
18863 		if (ill->ill_flags & ILLF_NOARP) {
18864 			/*
18865 			 * Note: xresolv interfaces will eventually need
18866 			 * NOARP set here as well, but that will require
18867 			 * those external resolvers to have some
18868 			 * knowledge of that flag and act appropriately.
18869 			 * Not to be changed at present.
18870 			 */
18871 			ill->ill_flags &= ~ILLF_NOARP;
18872 		}
18873 		/*
18874 		 * Set the ILLF_ROUTER flag according to the global
18875 		 * IPv6 forwarding policy.
18876 		 */
18877 		if (ipst->ips_ipv6_forward != 0)
18878 			ill->ill_flags |= ILLF_ROUTER;
18879 	} else if (ill->ill_flags & ILLF_IPV4) {
18880 		ill->ill_isv6 = B_FALSE;
18881 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
18882 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
18883 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
18884 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
18885 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
18886 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
18887 		/*
18888 		 * Set the ILLF_ROUTER flag according to the global
18889 		 * IPv4 forwarding policy.
18890 		 */
18891 		if (ipst->ips_ip_g_forward != 0)
18892 			ill->ill_flags |= ILLF_ROUTER;
18893 	}
18894 
18895 	ASSERT(ill->ill_phyint != NULL);
18896 
18897 	/*
18898 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
18899 	 * be completed in ill_glist_insert -> ill_phyint_reinit
18900 	 */
18901 	if (!ill_allocate_mibs(ill))
18902 		return (ENOMEM);
18903 
18904 	/*
18905 	 * Pick a default sap until we get the DL_INFO_ACK back from
18906 	 * the driver.
18907 	 */
18908 	if (ill->ill_sap == 0) {
18909 		if (ill->ill_isv6)
18910 			ill->ill_sap = IP6_DL_SAP;
18911 		else
18912 			ill->ill_sap = IP_DL_SAP;
18913 	}
18914 
18915 	ill->ill_ifname_pending = 1;
18916 	ill->ill_ifname_pending_err = 0;
18917 
18918 	/*
18919 	 * When the first ipif comes up in ipif_up_done(), multicast groups
18920 	 * that were joined while this ill was not bound to the DLPI link need
18921 	 * to be recovered by ill_recover_multicast().
18922 	 */
18923 	ill->ill_need_recover_multicast = 1;
18924 
18925 	ill_refhold(ill);
18926 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
18927 	if ((error = ill_glist_insert(ill, interf_name,
18928 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
18929 		ill->ill_ppa = UINT_MAX;
18930 		ill->ill_name[0] = '\0';
18931 		/*
18932 		 * undo null termination done above.
18933 		 */
18934 		ppa_ptr[0] = old_char;
18935 		rw_exit(&ipst->ips_ill_g_lock);
18936 		ill_refrele(ill);
18937 		return (error);
18938 	}
18939 
18940 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
18941 
18942 	/*
18943 	 * When we return the buffer pointed to by interf_name should contain
18944 	 * the same name as in ill_name.
18945 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
18946 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
18947 	 * so copy full name and update the ppa ptr.
18948 	 * When ppa passed in != UINT_MAX all values are correct just undo
18949 	 * null termination, this saves a bcopy.
18950 	 */
18951 	if (*new_ppa_ptr == UINT_MAX) {
18952 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
18953 		*new_ppa_ptr = ill->ill_ppa;
18954 	} else {
18955 		/*
18956 		 * undo null termination done above.
18957 		 */
18958 		ppa_ptr[0] = old_char;
18959 	}
18960 
18961 	/* Let SCTP know about this ILL */
18962 	sctp_update_ill(ill, SCTP_ILL_INSERT);
18963 
18964 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
18965 	    B_TRUE);
18966 
18967 	rw_exit(&ipst->ips_ill_g_lock);
18968 	ill_refrele(ill);
18969 	if (ipsq == NULL)
18970 		return (EINPROGRESS);
18971 
18972 	/*
18973 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
18974 	 */
18975 	if (ipsq->ipsq_xop->ipx_current_ipif == NULL)
18976 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
18977 	else
18978 		ASSERT(ipsq->ipsq_xop->ipx_current_ipif == ipif);
18979 
18980 	error = ipif_set_values_tail(ill, ipif, mp, q);
18981 	ipsq_exit(ipsq);
18982 	if (error != 0 && error != EINPROGRESS) {
18983 		/*
18984 		 * restore previous values
18985 		 */
18986 		ill->ill_isv6 = B_FALSE;
18987 	}
18988 	return (error);
18989 }
18990 
18991 
18992 void
18993 ipif_init(ip_stack_t *ipst)
18994 {
18995 	int i;
18996 
18997 	for (i = 0; i < MAX_G_HEADS; i++) {
18998 		ipst->ips_ill_g_heads[i].ill_g_list_head =
18999 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
19000 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
19001 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
19002 	}
19003 
19004 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
19005 	    ill_phyint_compare_index,
19006 	    sizeof (phyint_t),
19007 	    offsetof(struct phyint, phyint_avl_by_index));
19008 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
19009 	    ill_phyint_compare_name,
19010 	    sizeof (phyint_t),
19011 	    offsetof(struct phyint, phyint_avl_by_name));
19012 }
19013 
19014 /*
19015  * Lookup the ipif corresponding to the onlink destination address. For
19016  * point-to-point interfaces, it matches with remote endpoint destination
19017  * address. For point-to-multipoint interfaces it only tries to match the
19018  * destination with the interface's subnet address. The longest, most specific
19019  * match is found to take care of such rare network configurations like -
19020  * le0: 129.146.1.1/16
19021  * le1: 129.146.2.2/24
19022  *
19023  * This is used by SO_DONTROUTE and IP_NEXTHOP.  Since neither of those are
19024  * supported on underlying interfaces in an IPMP group, underlying interfaces
19025  * are ignored when looking up a match.  (If we didn't ignore them, we'd
19026  * risk using a test address as a source for outgoing traffic.)
19027  */
19028 ipif_t *
19029 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
19030 {
19031 	ipif_t	*ipif, *best_ipif;
19032 	ill_t	*ill;
19033 	ill_walk_context_t ctx;
19034 
19035 	ASSERT(zoneid != ALL_ZONES);
19036 	best_ipif = NULL;
19037 
19038 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
19039 	ill = ILL_START_WALK_V4(&ctx, ipst);
19040 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
19041 		if (IS_UNDER_IPMP(ill))
19042 			continue;
19043 		mutex_enter(&ill->ill_lock);
19044 		for (ipif = ill->ill_ipif; ipif != NULL;
19045 		    ipif = ipif->ipif_next) {
19046 			if (!IPIF_CAN_LOOKUP(ipif))
19047 				continue;
19048 			if (ipif->ipif_zoneid != zoneid &&
19049 			    ipif->ipif_zoneid != ALL_ZONES)
19050 				continue;
19051 			/*
19052 			 * Point-to-point case. Look for exact match with
19053 			 * destination address.
19054 			 */
19055 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19056 				if (ipif->ipif_pp_dst_addr == addr) {
19057 					ipif_refhold_locked(ipif);
19058 					mutex_exit(&ill->ill_lock);
19059 					rw_exit(&ipst->ips_ill_g_lock);
19060 					if (best_ipif != NULL)
19061 						ipif_refrele(best_ipif);
19062 					return (ipif);
19063 				}
19064 			} else if (ipif->ipif_subnet == (addr &
19065 			    ipif->ipif_net_mask)) {
19066 				/*
19067 				 * Point-to-multipoint case. Looping through to
19068 				 * find the most specific match. If there are
19069 				 * multiple best match ipif's then prefer ipif's
19070 				 * that are UP. If there is only one best match
19071 				 * ipif and it is DOWN we must still return it.
19072 				 */
19073 				if ((best_ipif == NULL) ||
19074 				    (ipif->ipif_net_mask >
19075 				    best_ipif->ipif_net_mask) ||
19076 				    ((ipif->ipif_net_mask ==
19077 				    best_ipif->ipif_net_mask) &&
19078 				    ((ipif->ipif_flags & IPIF_UP) &&
19079 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
19080 					ipif_refhold_locked(ipif);
19081 					mutex_exit(&ill->ill_lock);
19082 					rw_exit(&ipst->ips_ill_g_lock);
19083 					if (best_ipif != NULL)
19084 						ipif_refrele(best_ipif);
19085 					best_ipif = ipif;
19086 					rw_enter(&ipst->ips_ill_g_lock,
19087 					    RW_READER);
19088 					mutex_enter(&ill->ill_lock);
19089 				}
19090 			}
19091 		}
19092 		mutex_exit(&ill->ill_lock);
19093 	}
19094 	rw_exit(&ipst->ips_ill_g_lock);
19095 	return (best_ipif);
19096 }
19097 
19098 /*
19099  * Save enough information so that we can recreate the IRE if
19100  * the interface goes down and then up.
19101  */
19102 static void
19103 ipif_save_ire(ipif_t *ipif, ire_t *ire)
19104 {
19105 	mblk_t	*save_mp;
19106 
19107 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
19108 	if (save_mp != NULL) {
19109 		ifrt_t	*ifrt;
19110 
19111 		save_mp->b_wptr += sizeof (ifrt_t);
19112 		ifrt = (ifrt_t *)save_mp->b_rptr;
19113 		bzero(ifrt, sizeof (ifrt_t));
19114 		ifrt->ifrt_type = ire->ire_type;
19115 		ifrt->ifrt_addr = ire->ire_addr;
19116 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
19117 		ifrt->ifrt_src_addr = ire->ire_src_addr;
19118 		ifrt->ifrt_mask = ire->ire_mask;
19119 		ifrt->ifrt_flags = ire->ire_flags;
19120 		ifrt->ifrt_max_frag = ire->ire_max_frag;
19121 		mutex_enter(&ipif->ipif_saved_ire_lock);
19122 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
19123 		ipif->ipif_saved_ire_mp = save_mp;
19124 		ipif->ipif_saved_ire_cnt++;
19125 		mutex_exit(&ipif->ipif_saved_ire_lock);
19126 	}
19127 }
19128 
19129 static void
19130 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
19131 {
19132 	mblk_t	**mpp;
19133 	mblk_t	*mp;
19134 	ifrt_t	*ifrt;
19135 
19136 	/* Remove from ipif_saved_ire_mp list if it is there */
19137 	mutex_enter(&ipif->ipif_saved_ire_lock);
19138 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
19139 	    mpp = &(*mpp)->b_cont) {
19140 		/*
19141 		 * On a given ipif, the triple of address, gateway and
19142 		 * mask is unique for each saved IRE (in the case of
19143 		 * ordinary interface routes, the gateway address is
19144 		 * all-zeroes).
19145 		 */
19146 		mp = *mpp;
19147 		ifrt = (ifrt_t *)mp->b_rptr;
19148 		if (ifrt->ifrt_addr == ire->ire_addr &&
19149 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
19150 		    ifrt->ifrt_mask == ire->ire_mask) {
19151 			*mpp = mp->b_cont;
19152 			ipif->ipif_saved_ire_cnt--;
19153 			freeb(mp);
19154 			break;
19155 		}
19156 	}
19157 	mutex_exit(&ipif->ipif_saved_ire_lock);
19158 }
19159 
19160 /*
19161  * IP multirouting broadcast routes handling
19162  * Append CGTP broadcast IREs to regular ones created
19163  * at ifconfig time.
19164  */
19165 static void
19166 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
19167 {
19168 	ire_t *ire_prim;
19169 
19170 	ASSERT(ire != NULL);
19171 	ASSERT(ire_dst != NULL);
19172 
19173 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
19174 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
19175 	if (ire_prim != NULL) {
19176 		/*
19177 		 * We are in the special case of broadcasts for
19178 		 * CGTP. We add an IRE_BROADCAST that holds
19179 		 * the RTF_MULTIRT flag, the destination
19180 		 * address of ire_dst and the low level
19181 		 * info of ire_prim. In other words, CGTP
19182 		 * broadcast is added to the redundant ipif.
19183 		 */
19184 		ipif_t *ipif_prim;
19185 		ire_t  *bcast_ire;
19186 
19187 		ipif_prim = ire_prim->ire_ipif;
19188 
19189 		ip2dbg(("ip_cgtp_filter_bcast_add: "
19190 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
19191 		    (void *)ire_dst, (void *)ire_prim,
19192 		    (void *)ipif_prim));
19193 
19194 		bcast_ire = ire_create(
19195 		    (uchar_t *)&ire->ire_addr,
19196 		    (uchar_t *)&ip_g_all_ones,
19197 		    (uchar_t *)&ire_dst->ire_src_addr,
19198 		    (uchar_t *)&ire->ire_gateway_addr,
19199 		    &ipif_prim->ipif_mtu,
19200 		    NULL,
19201 		    ipif_prim->ipif_rq,
19202 		    ipif_prim->ipif_wq,
19203 		    IRE_BROADCAST,
19204 		    ipif_prim,
19205 		    0,
19206 		    0,
19207 		    0,
19208 		    ire->ire_flags,
19209 		    &ire_uinfo_null,
19210 		    NULL,
19211 		    NULL,
19212 		    ipst);
19213 
19214 		if (bcast_ire != NULL) {
19215 
19216 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
19217 			    B_FALSE) == 0) {
19218 				ip2dbg(("ip_cgtp_filter_bcast_add: "
19219 				    "added bcast_ire %p\n",
19220 				    (void *)bcast_ire));
19221 
19222 				ipif_save_ire(bcast_ire->ire_ipif,
19223 				    bcast_ire);
19224 				ire_refrele(bcast_ire);
19225 			}
19226 		}
19227 		ire_refrele(ire_prim);
19228 	}
19229 }
19230 
19231 
19232 /*
19233  * IP multirouting broadcast routes handling
19234  * Remove the broadcast ire
19235  */
19236 static void
19237 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
19238 {
19239 	ire_t *ire_dst;
19240 
19241 	ASSERT(ire != NULL);
19242 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
19243 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
19244 	if (ire_dst != NULL) {
19245 		ire_t *ire_prim;
19246 
19247 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
19248 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
19249 		if (ire_prim != NULL) {
19250 			ipif_t *ipif_prim;
19251 			ire_t  *bcast_ire;
19252 
19253 			ipif_prim = ire_prim->ire_ipif;
19254 
19255 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
19256 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
19257 			    (void *)ire_dst, (void *)ire_prim,
19258 			    (void *)ipif_prim));
19259 
19260 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
19261 			    ire->ire_gateway_addr,
19262 			    IRE_BROADCAST,
19263 			    ipif_prim, ALL_ZONES,
19264 			    NULL,
19265 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
19266 			    MATCH_IRE_MASK, ipst);
19267 
19268 			if (bcast_ire != NULL) {
19269 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
19270 				    "looked up bcast_ire %p\n",
19271 				    (void *)bcast_ire));
19272 				ipif_remove_ire(bcast_ire->ire_ipif,
19273 				    bcast_ire);
19274 				ire_delete(bcast_ire);
19275 				ire_refrele(bcast_ire);
19276 			}
19277 			ire_refrele(ire_prim);
19278 		}
19279 		ire_refrele(ire_dst);
19280 	}
19281 }
19282 
19283 /*
19284  * IPsec hardware acceleration capabilities related functions.
19285  */
19286 
19287 /*
19288  * Free a per-ill IPsec capabilities structure.
19289  */
19290 static void
19291 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
19292 {
19293 	if (capab->auth_hw_algs != NULL)
19294 		kmem_free(capab->auth_hw_algs, capab->algs_size);
19295 	if (capab->encr_hw_algs != NULL)
19296 		kmem_free(capab->encr_hw_algs, capab->algs_size);
19297 	if (capab->encr_algparm != NULL)
19298 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
19299 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
19300 }
19301 
19302 /*
19303  * Allocate a new per-ill IPsec capabilities structure. This structure
19304  * is specific to an IPsec protocol (AH or ESP). It is implemented as
19305  * an array which specifies, for each algorithm, whether this algorithm
19306  * is supported by the ill or not.
19307  */
19308 static ill_ipsec_capab_t *
19309 ill_ipsec_capab_alloc(void)
19310 {
19311 	ill_ipsec_capab_t *capab;
19312 	uint_t nelems;
19313 
19314 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
19315 	if (capab == NULL)
19316 		return (NULL);
19317 
19318 	/* we need one bit per algorithm */
19319 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
19320 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
19321 
19322 	/* allocate memory to store algorithm flags */
19323 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
19324 	if (capab->encr_hw_algs == NULL)
19325 		goto nomem;
19326 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
19327 	if (capab->auth_hw_algs == NULL)
19328 		goto nomem;
19329 	/*
19330 	 * Leave encr_algparm NULL for now since we won't need it half
19331 	 * the time
19332 	 */
19333 	return (capab);
19334 
19335 nomem:
19336 	ill_ipsec_capab_free(capab);
19337 	return (NULL);
19338 }
19339 
19340 /*
19341  * Resize capability array.  Since we're exclusive, this is OK.
19342  */
19343 static boolean_t
19344 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
19345 {
19346 	ipsec_capab_algparm_t *nalp, *oalp;
19347 	uint32_t olen, nlen;
19348 
19349 	oalp = capab->encr_algparm;
19350 	olen = capab->encr_algparm_size;
19351 
19352 	if (oalp != NULL) {
19353 		if (algid < capab->encr_algparm_end)
19354 			return (B_TRUE);
19355 	}
19356 
19357 	nlen = (algid + 1) * sizeof (*nalp);
19358 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
19359 	if (nalp == NULL)
19360 		return (B_FALSE);
19361 
19362 	if (oalp != NULL) {
19363 		bcopy(oalp, nalp, olen);
19364 		kmem_free(oalp, olen);
19365 	}
19366 	capab->encr_algparm = nalp;
19367 	capab->encr_algparm_size = nlen;
19368 	capab->encr_algparm_end = algid + 1;
19369 
19370 	return (B_TRUE);
19371 }
19372 
19373 /*
19374  * Compare the capabilities of the specified ill with the protocol
19375  * and algorithms specified by the SA passed as argument.
19376  * If they match, returns B_TRUE, B_FALSE if they do not match.
19377  *
19378  * The ill can be passed as a pointer to it, or by specifying its index
19379  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
19380  *
19381  * Called by ipsec_out_is_accelerated() do decide whether an outbound
19382  * packet is eligible for hardware acceleration, and by
19383  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
19384  * to a particular ill.
19385  */
19386 boolean_t
19387 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
19388     ipsa_t *sa, netstack_t *ns)
19389 {
19390 	boolean_t sa_isv6;
19391 	uint_t algid;
19392 	struct ill_ipsec_capab_s *cpp;
19393 	boolean_t need_refrele = B_FALSE;
19394 	ip_stack_t	*ipst = ns->netstack_ip;
19395 
19396 	if (ill == NULL) {
19397 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
19398 		    NULL, NULL, NULL, ipst);
19399 		if (ill == NULL) {
19400 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
19401 			return (B_FALSE);
19402 		}
19403 		need_refrele = B_TRUE;
19404 	}
19405 
19406 	/*
19407 	 * Use the address length specified by the SA to determine
19408 	 * if it corresponds to a IPv6 address, and fail the matching
19409 	 * if the isv6 flag passed as argument does not match.
19410 	 * Note: this check is used for SADB capability checking before
19411 	 * sending SA information to an ill.
19412 	 */
19413 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
19414 	if (sa_isv6 != ill_isv6)
19415 		/* protocol mismatch */
19416 		goto done;
19417 
19418 	/*
19419 	 * Check if the ill supports the protocol, algorithm(s) and
19420 	 * key size(s) specified by the SA, and get the pointers to
19421 	 * the algorithms supported by the ill.
19422 	 */
19423 	switch (sa->ipsa_type) {
19424 
19425 	case SADB_SATYPE_ESP:
19426 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
19427 			/* ill does not support ESP acceleration */
19428 			goto done;
19429 		cpp = ill->ill_ipsec_capab_esp;
19430 		algid = sa->ipsa_auth_alg;
19431 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
19432 			goto done;
19433 		algid = sa->ipsa_encr_alg;
19434 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
19435 			goto done;
19436 		if (algid < cpp->encr_algparm_end) {
19437 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
19438 			if (sa->ipsa_encrkeybits < alp->minkeylen)
19439 				goto done;
19440 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
19441 				goto done;
19442 		}
19443 		break;
19444 
19445 	case SADB_SATYPE_AH:
19446 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
19447 			/* ill does not support AH acceleration */
19448 			goto done;
19449 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
19450 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
19451 			goto done;
19452 		break;
19453 	}
19454 
19455 	if (need_refrele)
19456 		ill_refrele(ill);
19457 	return (B_TRUE);
19458 done:
19459 	if (need_refrele)
19460 		ill_refrele(ill);
19461 	return (B_FALSE);
19462 }
19463 
19464 /*
19465  * Add a new ill to the list of IPsec capable ills.
19466  * Called from ill_capability_ipsec_ack() when an ACK was received
19467  * indicating that IPsec hardware processing was enabled for an ill.
19468  *
19469  * ill must point to the ill for which acceleration was enabled.
19470  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
19471  */
19472 static void
19473 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
19474 {
19475 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
19476 	uint_t sa_type;
19477 	uint_t ipproto;
19478 	ip_stack_t	*ipst = ill->ill_ipst;
19479 
19480 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
19481 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
19482 
19483 	switch (dl_cap) {
19484 	case DL_CAPAB_IPSEC_AH:
19485 		sa_type = SADB_SATYPE_AH;
19486 		ills = &ipst->ips_ipsec_capab_ills_ah;
19487 		ipproto = IPPROTO_AH;
19488 		break;
19489 	case DL_CAPAB_IPSEC_ESP:
19490 		sa_type = SADB_SATYPE_ESP;
19491 		ills = &ipst->ips_ipsec_capab_ills_esp;
19492 		ipproto = IPPROTO_ESP;
19493 		break;
19494 	}
19495 
19496 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
19497 
19498 	/*
19499 	 * Add ill index to list of hardware accelerators. If
19500 	 * already in list, do nothing.
19501 	 */
19502 	for (cur_ill = *ills; cur_ill != NULL &&
19503 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
19504 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
19505 		;
19506 
19507 	if (cur_ill == NULL) {
19508 		/* if this is a new entry for this ill */
19509 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
19510 		if (new_ill == NULL) {
19511 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19512 			return;
19513 		}
19514 
19515 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
19516 		new_ill->ill_isv6 = ill->ill_isv6;
19517 		new_ill->next = *ills;
19518 		*ills = new_ill;
19519 	} else if (!sadb_resync) {
19520 		/* not resync'ing SADB and an entry exists for this ill */
19521 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19522 		return;
19523 	}
19524 
19525 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19526 
19527 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
19528 		/*
19529 		 * IPsec module for protocol loaded, initiate dump
19530 		 * of the SADB to this ill.
19531 		 */
19532 		sadb_ill_download(ill, sa_type);
19533 }
19534 
19535 /*
19536  * Remove an ill from the list of IPsec capable ills.
19537  */
19538 static void
19539 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
19540 {
19541 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
19542 	ip_stack_t	*ipst = ill->ill_ipst;
19543 
19544 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
19545 	    dl_cap == DL_CAPAB_IPSEC_ESP);
19546 
19547 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
19548 	    &ipst->ips_ipsec_capab_ills_esp;
19549 
19550 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
19551 
19552 	prev_ill = NULL;
19553 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
19554 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
19555 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
19556 		;
19557 	if (cur_ill == NULL) {
19558 		/* entry not found */
19559 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19560 		return;
19561 	}
19562 	if (prev_ill == NULL) {
19563 		/* entry at front of list */
19564 		*ills = NULL;
19565 	} else {
19566 		prev_ill->next = cur_ill->next;
19567 	}
19568 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
19569 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19570 }
19571 
19572 /*
19573  * Called by SADB to send a DL_CONTROL_REQ message to every ill
19574  * supporting the specified IPsec protocol acceleration.
19575  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
19576  * We free the mblk and, if sa is non-null, release the held referece.
19577  */
19578 void
19579 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
19580     netstack_t *ns)
19581 {
19582 	ipsec_capab_ill_t *ici, *cur_ici;
19583 	ill_t *ill;
19584 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
19585 	ip_stack_t	*ipst = ns->netstack_ip;
19586 
19587 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
19588 	    ipst->ips_ipsec_capab_ills_esp;
19589 
19590 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
19591 
19592 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
19593 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
19594 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
19595 
19596 		/*
19597 		 * Handle the case where the ill goes away while the SADB is
19598 		 * attempting to send messages.  If it's going away, it's
19599 		 * nuking its shadow SADB, so we don't care..
19600 		 */
19601 
19602 		if (ill == NULL)
19603 			continue;
19604 
19605 		if (sa != NULL) {
19606 			/*
19607 			 * Make sure capabilities match before
19608 			 * sending SA to ill.
19609 			 */
19610 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
19611 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
19612 				ill_refrele(ill);
19613 				continue;
19614 			}
19615 
19616 			mutex_enter(&sa->ipsa_lock);
19617 			sa->ipsa_flags |= IPSA_F_HW;
19618 			mutex_exit(&sa->ipsa_lock);
19619 		}
19620 
19621 		/*
19622 		 * Copy template message, and add it to the front
19623 		 * of the mblk ship list. We want to avoid holding
19624 		 * the ipsec_capab_ills_lock while sending the
19625 		 * message to the ills.
19626 		 *
19627 		 * The b_next and b_prev are temporarily used
19628 		 * to build a list of mblks to be sent down, and to
19629 		 * save the ill to which they must be sent.
19630 		 */
19631 		nmp = copymsg(mp);
19632 		if (nmp == NULL) {
19633 			ill_refrele(ill);
19634 			continue;
19635 		}
19636 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
19637 		nmp->b_next = mp_ship_list;
19638 		mp_ship_list = nmp;
19639 		nmp->b_prev = (mblk_t *)ill;
19640 	}
19641 
19642 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
19643 
19644 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
19645 		/* restore the mblk to a sane state */
19646 		next_mp = nmp->b_next;
19647 		nmp->b_next = NULL;
19648 		ill = (ill_t *)nmp->b_prev;
19649 		nmp->b_prev = NULL;
19650 
19651 		ill_dlpi_send(ill, nmp);
19652 		ill_refrele(ill);
19653 	}
19654 
19655 	if (sa != NULL)
19656 		IPSA_REFRELE(sa);
19657 	freemsg(mp);
19658 }
19659 
19660 /*
19661  * Derive an interface id from the link layer address.
19662  * Knows about IEEE 802 and IEEE EUI-64 mappings.
19663  */
19664 static boolean_t
19665 ip_ether_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19666 {
19667 	char		*addr;
19668 
19669 	if (ill->ill_phys_addr_length != ETHERADDRL)
19670 		return (B_FALSE);
19671 
19672 	/* Form EUI-64 like address */
19673 	addr = (char *)&v6addr->s6_addr32[2];
19674 	bcopy(ill->ill_phys_addr, addr, 3);
19675 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
19676 	addr[3] = (char)0xff;
19677 	addr[4] = (char)0xfe;
19678 	bcopy(ill->ill_phys_addr + 3, addr + 5, 3);
19679 	return (B_TRUE);
19680 }
19681 
19682 /* ARGSUSED */
19683 static boolean_t
19684 ip_nodef_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19685 {
19686 	return (B_FALSE);
19687 }
19688 
19689 typedef struct ipmp_ifcookie {
19690 	uint32_t	ic_hostid;
19691 	char		ic_ifname[LIFNAMSIZ];
19692 	char		ic_zonename[ZONENAME_MAX];
19693 } ipmp_ifcookie_t;
19694 
19695 /*
19696  * Construct a pseudo-random interface ID for the IPMP interface that's both
19697  * predictable and (almost) guaranteed to be unique.
19698  */
19699 static boolean_t
19700 ip_ipmp_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19701 {
19702 	zone_t		*zp;
19703 	uint8_t		*addr;
19704 	uchar_t		hash[16];
19705 	ulong_t 	hostid;
19706 	MD5_CTX		ctx;
19707 	ipmp_ifcookie_t	ic = { 0 };
19708 
19709 	ASSERT(IS_IPMP(ill));
19710 
19711 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
19712 	ic.ic_hostid = htonl((uint32_t)hostid);
19713 
19714 	(void) strlcpy(ic.ic_ifname, ill->ill_name, LIFNAMSIZ);
19715 
19716 	if ((zp = zone_find_by_id(ill->ill_zoneid)) != NULL) {
19717 		(void) strlcpy(ic.ic_zonename, zp->zone_name, ZONENAME_MAX);
19718 		zone_rele(zp);
19719 	}
19720 
19721 	MD5Init(&ctx);
19722 	MD5Update(&ctx, &ic, sizeof (ic));
19723 	MD5Final(hash, &ctx);
19724 
19725 	/*
19726 	 * Map the hash to an interface ID per the basic approach in RFC3041.
19727 	 */
19728 	addr = &v6addr->s6_addr8[8];
19729 	bcopy(hash + 8, addr, sizeof (uint64_t));
19730 	addr[0] &= ~0x2;				/* set local bit */
19731 
19732 	return (B_TRUE);
19733 }
19734 
19735 /* ARGSUSED */
19736 static boolean_t
19737 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
19738     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
19739 {
19740 	/*
19741 	 * Multicast address mappings used over Ethernet/802.X.
19742 	 * This address is used as a base for mappings.
19743 	 */
19744 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
19745 	    0x00, 0x00, 0x00};
19746 
19747 	/*
19748 	 * Extract low order 32 bits from IPv6 multicast address.
19749 	 * Or that into the link layer address, starting from the
19750 	 * second byte.
19751 	 */
19752 	*hw_start = 2;
19753 	v6_extract_mask->s6_addr32[0] = 0;
19754 	v6_extract_mask->s6_addr32[1] = 0;
19755 	v6_extract_mask->s6_addr32[2] = 0;
19756 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
19757 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
19758 	return (B_TRUE);
19759 }
19760 
19761 /*
19762  * Indicate by return value whether multicast is supported. If not,
19763  * this code should not touch/change any parameters.
19764  */
19765 /* ARGSUSED */
19766 static boolean_t
19767 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
19768     uint32_t *hw_start, ipaddr_t *extract_mask)
19769 {
19770 	/*
19771 	 * Multicast address mappings used over Ethernet/802.X.
19772 	 * This address is used as a base for mappings.
19773 	 */
19774 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
19775 	    0x00, 0x00, 0x00 };
19776 
19777 	if (phys_length != ETHERADDRL)
19778 		return (B_FALSE);
19779 
19780 	*extract_mask = htonl(0x007fffff);
19781 	*hw_start = 2;
19782 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
19783 	return (B_TRUE);
19784 }
19785 
19786 /*
19787  * Derive IPoIB interface id from the link layer address.
19788  */
19789 static boolean_t
19790 ip_ib_v6intfid(ill_t *ill, in6_addr_t *v6addr)
19791 {
19792 	char		*addr;
19793 
19794 	if (ill->ill_phys_addr_length != 20)
19795 		return (B_FALSE);
19796 	addr = (char *)&v6addr->s6_addr32[2];
19797 	bcopy(ill->ill_phys_addr + 12, addr, 8);
19798 	/*
19799 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
19800 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
19801 	 * rules. In these cases, the IBA considers these GUIDs to be in
19802 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
19803 	 * required; vendors are required not to assign global EUI-64's
19804 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
19805 	 * of the interface identifier. Whether the GUID is in modified
19806 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
19807 	 * bit set to 1.
19808 	 */
19809 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
19810 	return (B_TRUE);
19811 }
19812 
19813 /*
19814  * Note on mapping from multicast IP addresses to IPoIB multicast link
19815  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
19816  * The format of an IPoIB multicast address is:
19817  *
19818  *  4 byte QPN      Scope Sign.  Pkey
19819  * +--------------------------------------------+
19820  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
19821  * +--------------------------------------------+
19822  *
19823  * The Scope and Pkey components are properties of the IBA port and
19824  * network interface. They can be ascertained from the broadcast address.
19825  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
19826  */
19827 
19828 static boolean_t
19829 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
19830     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
19831 {
19832 	/*
19833 	 * Base IPoIB IPv6 multicast address used for mappings.
19834 	 * Does not contain the IBA scope/Pkey values.
19835 	 */
19836 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
19837 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
19838 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
19839 
19840 	/*
19841 	 * Extract low order 80 bits from IPv6 multicast address.
19842 	 * Or that into the link layer address, starting from the
19843 	 * sixth byte.
19844 	 */
19845 	*hw_start = 6;
19846 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
19847 
19848 	/*
19849 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
19850 	 */
19851 	*(maddr + 5) = *(bphys_addr + 5);
19852 	*(maddr + 8) = *(bphys_addr + 8);
19853 	*(maddr + 9) = *(bphys_addr + 9);
19854 
19855 	v6_extract_mask->s6_addr32[0] = 0;
19856 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
19857 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
19858 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
19859 	return (B_TRUE);
19860 }
19861 
19862 static boolean_t
19863 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
19864     uint32_t *hw_start, ipaddr_t *extract_mask)
19865 {
19866 	/*
19867 	 * Base IPoIB IPv4 multicast address used for mappings.
19868 	 * Does not contain the IBA scope/Pkey values.
19869 	 */
19870 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
19871 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
19872 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
19873 
19874 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
19875 		return (B_FALSE);
19876 
19877 	/*
19878 	 * Extract low order 28 bits from IPv4 multicast address.
19879 	 * Or that into the link layer address, starting from the
19880 	 * sixteenth byte.
19881 	 */
19882 	*extract_mask = htonl(0x0fffffff);
19883 	*hw_start = 16;
19884 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
19885 
19886 	/*
19887 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
19888 	 */
19889 	*(maddr + 5) = *(bphys_addr + 5);
19890 	*(maddr + 8) = *(bphys_addr + 8);
19891 	*(maddr + 9) = *(bphys_addr + 9);
19892 	return (B_TRUE);
19893 }
19894 
19895 /*
19896  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
19897  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
19898  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
19899  * the link-local address is preferred.
19900  */
19901 boolean_t
19902 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
19903 {
19904 	ipif_t	*ipif;
19905 	ipif_t	*maybe_ipif = NULL;
19906 
19907 	mutex_enter(&ill->ill_lock);
19908 	if (ill->ill_state_flags & ILL_CONDEMNED) {
19909 		mutex_exit(&ill->ill_lock);
19910 		if (ipifp != NULL)
19911 			*ipifp = NULL;
19912 		return (B_FALSE);
19913 	}
19914 
19915 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19916 		if (!IPIF_CAN_LOOKUP(ipif))
19917 			continue;
19918 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
19919 		    ipif->ipif_zoneid != ALL_ZONES)
19920 			continue;
19921 		if ((ipif->ipif_flags & flags) != flags)
19922 			continue;
19923 
19924 		if (ipifp == NULL) {
19925 			mutex_exit(&ill->ill_lock);
19926 			ASSERT(maybe_ipif == NULL);
19927 			return (B_TRUE);
19928 		}
19929 		if (!ill->ill_isv6 ||
19930 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
19931 			ipif_refhold_locked(ipif);
19932 			mutex_exit(&ill->ill_lock);
19933 			*ipifp = ipif;
19934 			return (B_TRUE);
19935 		}
19936 		if (maybe_ipif == NULL)
19937 			maybe_ipif = ipif;
19938 	}
19939 	if (ipifp != NULL) {
19940 		if (maybe_ipif != NULL)
19941 			ipif_refhold_locked(maybe_ipif);
19942 		*ipifp = maybe_ipif;
19943 	}
19944 	mutex_exit(&ill->ill_lock);
19945 	return (maybe_ipif != NULL);
19946 }
19947 
19948 /*
19949  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
19950  * If a pointer to an ipif_t is returned then the caller will need to do
19951  * an ill_refrele().
19952  */
19953 ipif_t *
19954 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
19955     ip_stack_t *ipst)
19956 {
19957 	ipif_t *ipif;
19958 	ill_t *ill;
19959 
19960 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
19961 	    ipst);
19962 	if (ill == NULL)
19963 		return (NULL);
19964 
19965 	mutex_enter(&ill->ill_lock);
19966 	if (ill->ill_state_flags & ILL_CONDEMNED) {
19967 		mutex_exit(&ill->ill_lock);
19968 		ill_refrele(ill);
19969 		return (NULL);
19970 	}
19971 
19972 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19973 		if (!IPIF_CAN_LOOKUP(ipif))
19974 			continue;
19975 		if (lifidx == ipif->ipif_id) {
19976 			ipif_refhold_locked(ipif);
19977 			break;
19978 		}
19979 	}
19980 
19981 	mutex_exit(&ill->ill_lock);
19982 	ill_refrele(ill);
19983 	return (ipif);
19984 }
19985 
19986 /*
19987  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
19988  * There is one exceptions IRE_BROADCAST are difficult to recreate,
19989  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
19990  * for details.
19991  */
19992 void
19993 ill_fastpath_flush(ill_t *ill)
19994 {
19995 	ip_stack_t *ipst = ill->ill_ipst;
19996 
19997 	nce_fastpath_list_dispatch(ill, NULL, NULL);
19998 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
19999 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
20000 }
20001 
20002 /*
20003  * Set the physical address information for `ill' to the contents of the
20004  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
20005  * asynchronous if `ill' cannot immediately be quiesced -- in which case
20006  * EINPROGRESS will be returned.
20007  */
20008 int
20009 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
20010 {
20011 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
20012 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
20013 
20014 	ASSERT(IAM_WRITER_IPSQ(ipsq));
20015 
20016 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
20017 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
20018 		/* Changing DL_IPV6_TOKEN is not yet supported */
20019 		return (0);
20020 	}
20021 
20022 	/*
20023 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
20024 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
20025 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
20026 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
20027 	 */
20028 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
20029 		freemsg(mp);
20030 		return (ENOMEM);
20031 	}
20032 
20033 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
20034 
20035 	/*
20036 	 * If we can quiesce the ill, then set the address.  If not, then
20037 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
20038 	 */
20039 	ill_down_ipifs(ill);
20040 	mutex_enter(&ill->ill_lock);
20041 	if (!ill_is_quiescent(ill)) {
20042 		/* call cannot fail since `conn_t *' argument is NULL */
20043 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
20044 		    mp, ILL_DOWN);
20045 		mutex_exit(&ill->ill_lock);
20046 		return (EINPROGRESS);
20047 	}
20048 	mutex_exit(&ill->ill_lock);
20049 
20050 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
20051 	return (0);
20052 }
20053 
20054 /*
20055  * Once the ill associated with `q' has quiesced, set its physical address
20056  * information to the values in `addrmp'.  Note that two copies of `addrmp'
20057  * are passed (linked by b_cont), since we sometimes need to save two distinct
20058  * copies in the ill_t, and our context doesn't permit sleeping or allocation
20059  * failure (we'll free the other copy if it's not needed).  Since the ill_t
20060  * is quiesced, we know any stale IREs with the old address information have
20061  * already been removed, so we don't need to call ill_fastpath_flush().
20062  */
20063 /* ARGSUSED */
20064 static void
20065 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
20066 {
20067 	ill_t		*ill = q->q_ptr;
20068 	mblk_t		*addrmp2 = unlinkb(addrmp);
20069 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
20070 	uint_t		addrlen, addroff;
20071 
20072 	ASSERT(IAM_WRITER_IPSQ(ipsq));
20073 
20074 	addroff	= dlindp->dl_addr_offset;
20075 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
20076 
20077 	switch (dlindp->dl_data) {
20078 	case DL_IPV6_LINK_LAYER_ADDR:
20079 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
20080 		freemsg(addrmp2);
20081 		break;
20082 
20083 	case DL_CURR_PHYS_ADDR:
20084 		freemsg(ill->ill_phys_addr_mp);
20085 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
20086 		ill->ill_phys_addr_mp = addrmp;
20087 		ill->ill_phys_addr_length = addrlen;
20088 
20089 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
20090 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
20091 		else
20092 			freemsg(addrmp2);
20093 		break;
20094 	default:
20095 		ASSERT(0);
20096 	}
20097 
20098 	/*
20099 	 * If there are ipifs to bring up, ill_up_ipifs() will return
20100 	 * EINPROGRESS, and ipsq_current_finish() will be called by
20101 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
20102 	 * brought up.
20103 	 */
20104 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
20105 		ipsq_current_finish(ipsq);
20106 }
20107 
20108 /*
20109  * Helper routine for setting the ill_nd_lla fields.
20110  */
20111 void
20112 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
20113 {
20114 	freemsg(ill->ill_nd_lla_mp);
20115 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
20116 	ill->ill_nd_lla_mp = ndmp;
20117 	ill->ill_nd_lla_len = addrlen;
20118 }
20119 
20120 major_t IP_MAJ;
20121 #define	IP	"ip"
20122 
20123 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
20124 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
20125 
20126 /*
20127  * Issue REMOVEIF ioctls to have the loopback interfaces
20128  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
20129  * the former going away when the user-level processes in the zone
20130  * are killed  * and the latter are cleaned up by the stream head
20131  * str_stack_shutdown callback that undoes all I_PLINKs.
20132  */
20133 void
20134 ip_loopback_cleanup(ip_stack_t *ipst)
20135 {
20136 	int error;
20137 	ldi_handle_t	lh = NULL;
20138 	ldi_ident_t	li = NULL;
20139 	int		rval;
20140 	cred_t		*cr;
20141 	struct strioctl iocb;
20142 	struct lifreq	lifreq;
20143 
20144 	IP_MAJ = ddi_name_to_major(IP);
20145 
20146 #ifdef NS_DEBUG
20147 	(void) printf("ip_loopback_cleanup() stackid %d\n",
20148 	    ipst->ips_netstack->netstack_stackid);
20149 #endif
20150 
20151 	bzero(&lifreq, sizeof (lifreq));
20152 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
20153 
20154 	error = ldi_ident_from_major(IP_MAJ, &li);
20155 	if (error) {
20156 #ifdef DEBUG
20157 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
20158 		    error);
20159 #endif
20160 		return;
20161 	}
20162 
20163 	cr = zone_get_kcred(netstackid_to_zoneid(
20164 	    ipst->ips_netstack->netstack_stackid));
20165 	ASSERT(cr != NULL);
20166 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
20167 	if (error) {
20168 #ifdef DEBUG
20169 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
20170 		    error);
20171 #endif
20172 		goto out;
20173 	}
20174 	iocb.ic_cmd = SIOCLIFREMOVEIF;
20175 	iocb.ic_timout = 15;
20176 	iocb.ic_len = sizeof (lifreq);
20177 	iocb.ic_dp = (char *)&lifreq;
20178 
20179 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
20180 	/* LINTED - statement has no consequent */
20181 	if (error) {
20182 #ifdef NS_DEBUG
20183 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
20184 		    "UDP6 error %d\n", error);
20185 #endif
20186 	}
20187 	(void) ldi_close(lh, FREAD|FWRITE, cr);
20188 	lh = NULL;
20189 
20190 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
20191 	if (error) {
20192 #ifdef NS_DEBUG
20193 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
20194 		    error);
20195 #endif
20196 		goto out;
20197 	}
20198 
20199 	iocb.ic_cmd = SIOCLIFREMOVEIF;
20200 	iocb.ic_timout = 15;
20201 	iocb.ic_len = sizeof (lifreq);
20202 	iocb.ic_dp = (char *)&lifreq;
20203 
20204 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
20205 	/* LINTED - statement has no consequent */
20206 	if (error) {
20207 #ifdef NS_DEBUG
20208 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
20209 		    "UDP error %d\n", error);
20210 #endif
20211 	}
20212 	(void) ldi_close(lh, FREAD|FWRITE, cr);
20213 	lh = NULL;
20214 
20215 out:
20216 	/* Close layered handles */
20217 	if (lh)
20218 		(void) ldi_close(lh, FREAD|FWRITE, cr);
20219 	if (li)
20220 		ldi_ident_release(li);
20221 
20222 	crfree(cr);
20223 }
20224 
20225 /*
20226  * This needs to be in-sync with nic_event_t definition
20227  */
20228 static const char *
20229 ill_hook_event2str(nic_event_t event)
20230 {
20231 	switch (event) {
20232 	case NE_PLUMB:
20233 		return ("PLUMB");
20234 	case NE_UNPLUMB:
20235 		return ("UNPLUMB");
20236 	case NE_UP:
20237 		return ("UP");
20238 	case NE_DOWN:
20239 		return ("DOWN");
20240 	case NE_ADDRESS_CHANGE:
20241 		return ("ADDRESS_CHANGE");
20242 	case NE_LIF_UP:
20243 		return ("LIF_UP");
20244 	case NE_LIF_DOWN:
20245 		return ("LIF_DOWN");
20246 	default:
20247 		return ("UNKNOWN");
20248 	}
20249 }
20250 
20251 void
20252 ill_nic_event_dispatch(ill_t *ill, lif_if_t lif, nic_event_t event,
20253     nic_event_data_t data, size_t datalen)
20254 {
20255 	ip_stack_t		*ipst = ill->ill_ipst;
20256 	hook_nic_event_int_t	*info;
20257 	const char		*str = NULL;
20258 
20259 	/* create a new nic event info */
20260 	if ((info = kmem_alloc(sizeof (*info), KM_NOSLEEP)) == NULL)
20261 		goto fail;
20262 
20263 	info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
20264 	info->hnei_event.hne_lif = lif;
20265 	info->hnei_event.hne_event = event;
20266 	info->hnei_event.hne_protocol = ill->ill_isv6 ?
20267 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
20268 	info->hnei_event.hne_data = NULL;
20269 	info->hnei_event.hne_datalen = 0;
20270 	info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
20271 
20272 	if (data != NULL && datalen != 0) {
20273 		info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
20274 		if (info->hnei_event.hne_data == NULL)
20275 			goto fail;
20276 		bcopy(data, info->hnei_event.hne_data, datalen);
20277 		info->hnei_event.hne_datalen = datalen;
20278 	}
20279 
20280 	if (ddi_taskq_dispatch(eventq_queue_nic, ip_ne_queue_func, info,
20281 	    DDI_NOSLEEP) == DDI_SUCCESS)
20282 		return;
20283 
20284 fail:
20285 	if (info != NULL) {
20286 		if (info->hnei_event.hne_data != NULL) {
20287 			kmem_free(info->hnei_event.hne_data,
20288 			    info->hnei_event.hne_datalen);
20289 		}
20290 		kmem_free(info, sizeof (hook_nic_event_t));
20291 	}
20292 	str = ill_hook_event2str(event);
20293 	ip2dbg(("ill_nic_event_dispatch: could not dispatch %s nic event "
20294 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
20295 }
20296 
20297 void
20298 ipif_up_notify(ipif_t *ipif)
20299 {
20300 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
20301 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif, RTSQ_DEFAULT);
20302 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
20303 	ill_nic_event_dispatch(ipif->ipif_ill, MAP_IPIF_ID(ipif->ipif_id),
20304 	    NE_LIF_UP, NULL, 0);
20305 }
20306