xref: /illumos-gate/usr/src/uts/common/inet/ip/ip_if.c (revision 10597944279b73141546abca67a8e947810e5bb2)
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 (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 1990 Mentat Inc.
24  * Copyright (c) 2013 by Delphix. All rights reserved.
25  * Copyright (c) 2016, Joyent, Inc. All rights reserved.
26  * Copyright (c) 2014, OmniTI Computer Consulting, Inc. All rights reserved.
27  * Copyright 2025 Oxide Computer Company
28  */
29 
30 /*
31  * This file contains the interface control functions for IP.
32  */
33 
34 #include <sys/types.h>
35 #include <sys/stream.h>
36 #include <sys/dlpi.h>
37 #include <sys/stropts.h>
38 #include <sys/strsun.h>
39 #include <sys/sysmacros.h>
40 #include <sys/strsubr.h>
41 #include <sys/strlog.h>
42 #include <sys/ddi.h>
43 #include <sys/sunddi.h>
44 #include <sys/cmn_err.h>
45 #include <sys/kstat.h>
46 #include <sys/debug.h>
47 #include <sys/zone.h>
48 #include <sys/sunldi.h>
49 #include <sys/file.h>
50 #include <sys/bitmap.h>
51 #include <sys/cpuvar.h>
52 #include <sys/time.h>
53 #include <sys/ctype.h>
54 #include <sys/kmem.h>
55 #include <sys/systm.h>
56 #include <sys/param.h>
57 #include <sys/socket.h>
58 #include <sys/isa_defs.h>
59 #include <net/if.h>
60 #include <net/if_arp.h>
61 #include <net/if_types.h>
62 #include <net/if_dl.h>
63 #include <net/route.h>
64 #include <sys/sockio.h>
65 #include <netinet/in.h>
66 #include <netinet/ip6.h>
67 #include <netinet/icmp6.h>
68 #include <netinet/igmp_var.h>
69 #include <sys/policy.h>
70 #include <sys/ethernet.h>
71 #include <sys/callb.h>
72 #include <sys/md5.h>
73 
74 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
75 #include <inet/mi.h>
76 #include <inet/nd.h>
77 #include <inet/tunables.h>
78 #include <inet/arp.h>
79 #include <inet/ip_arp.h>
80 #include <inet/mib2.h>
81 #include <inet/ip.h>
82 #include <inet/ip6.h>
83 #include <inet/ip6_asp.h>
84 #include <inet/tcp.h>
85 #include <inet/ip_multi.h>
86 #include <inet/ip_ire.h>
87 #include <inet/ip_ftable.h>
88 #include <inet/ip_rts.h>
89 #include <inet/ip_ndp.h>
90 #include <inet/ip_if.h>
91 #include <inet/ip_impl.h>
92 #include <inet/sctp_ip.h>
93 #include <inet/ip_netinfo.h>
94 #include <inet/ilb_ip.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 #include <sys/mac_flow.h>
102 
103 #include <sys/systeminfo.h>
104 #include <sys/bootconf.h>
105 
106 #include <sys/tsol/tndb.h>
107 #include <sys/tsol/tnet.h>
108 
109 #include <inet/rawip_impl.h> /* needed for icmp_stack_t */
110 #include <inet/udp_impl.h> /* needed for udp_stack_t */
111 
112 /* The character which tells where the ill_name ends */
113 #define	IPIF_SEPARATOR_CHAR	':'
114 
115 /* IP ioctl function table entry */
116 typedef struct ipft_s {
117 	int	ipft_cmd;
118 	pfi_t	ipft_pfi;
119 	int	ipft_min_size;
120 	int	ipft_flags;
121 } ipft_t;
122 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
123 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
124 
125 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
126 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
127 		    char *value, caddr_t cp, cred_t *ioc_cr);
128 
129 static boolean_t ill_is_quiescent(ill_t *);
130 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
131 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
132 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
133     mblk_t *mp, boolean_t need_up);
134 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
135     mblk_t *mp, boolean_t need_up);
136 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
137     queue_t *q, mblk_t *mp, boolean_t need_up);
138 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
139     mblk_t *mp);
140 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
141     mblk_t *mp);
142 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
143     queue_t *q, mblk_t *mp, boolean_t need_up);
144 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
145     int ioccmd, struct linkblk *li);
146 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
147 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
148 static void	ipsq_flush(ill_t *ill);
149 
150 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
151     queue_t *q, mblk_t *mp, boolean_t need_up);
152 static void	ipsq_delete(ipsq_t *);
153 
154 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
155     boolean_t initialize, boolean_t insert, int *errorp);
156 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
157 static void	ipif_delete_bcast_ires(ipif_t *ipif);
158 static int	ipif_add_ires_v4(ipif_t *, boolean_t);
159 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
160 		    boolean_t isv6);
161 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
162 static void	ipif_free(ipif_t *ipif);
163 static void	ipif_free_tail(ipif_t *ipif);
164 static void	ipif_set_default(ipif_t *ipif);
165 static int	ipif_set_values(queue_t *q, mblk_t *mp,
166     char *interf_name, uint_t *ppa);
167 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
168     queue_t *q);
169 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
170     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
171     ip_stack_t *);
172 static ipif_t	*ipif_lookup_on_name_async(char *name, size_t namelen,
173     boolean_t isv6, zoneid_t zoneid, queue_t *q, mblk_t *mp, ipsq_func_t func,
174     int *error, ip_stack_t *);
175 
176 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
177 static void	ill_delete_interface_type(ill_if_t *);
178 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
179 static void	ill_dl_down(ill_t *ill);
180 static void	ill_down(ill_t *ill);
181 static void	ill_down_ipifs(ill_t *, boolean_t);
182 static void	ill_free_mib(ill_t *ill);
183 static void	ill_glist_delete(ill_t *);
184 static void	ill_phyint_reinit(ill_t *ill);
185 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
186 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
187 static void	ill_replumb_tail(ipsq_t *, queue_t *, mblk_t *, void *);
188 
189 static ip_v6intfid_func_t ip_ether_v6intfid, ip_ib_v6intfid;
190 static ip_v6intfid_func_t ip_ipv4_v6intfid, ip_ipv6_v6intfid;
191 static ip_v6intfid_func_t ip_ipmp_v6intfid, ip_nodef_v6intfid;
192 static ip_v6intfid_func_t ip_ipv4_v6destintfid, ip_ipv6_v6destintfid;
193 static ip_v4mapinfo_func_t ip_ether_v4_mapping;
194 static ip_v6mapinfo_func_t ip_ether_v6_mapping;
195 static ip_v4mapinfo_func_t ip_ib_v4_mapping;
196 static ip_v6mapinfo_func_t ip_ib_v6_mapping;
197 static ip_v4mapinfo_func_t ip_mbcast_mapping;
198 static void	ip_cgtp_bcast_add(ire_t *, ip_stack_t *);
199 static void	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
200 static void	phyint_free(phyint_t *);
201 
202 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *);
203 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
204 static void ill_capability_vrrp_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
205 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
206 static void ill_capability_hcksum_reset_fill(ill_t *, mblk_t *);
207 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
208     dl_capability_sub_t *);
209 static void ill_capability_zerocopy_reset_fill(ill_t *, mblk_t *);
210 static void	ill_capability_dld_reset_fill(ill_t *, mblk_t *);
211 static void	ill_capability_dld_ack(ill_t *, mblk_t *,
212 		    dl_capability_sub_t *);
213 static void	ill_capability_dld_enable(ill_t *);
214 static void	ill_capability_ack_thr(void *);
215 static void	ill_capability_lso_enable(ill_t *);
216 
217 static ill_t	*ill_prev_usesrc(ill_t *);
218 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
219 static void	ill_disband_usesrc_group(ill_t *);
220 static void	ip_sioctl_garp_reply(mblk_t *, ill_t *, void *, int);
221 
222 #ifdef DEBUG
223 static	void	ill_trace_cleanup(const ill_t *);
224 static	void	ipif_trace_cleanup(const ipif_t *);
225 #endif
226 
227 static	void	ill_dlpi_clear_deferred(ill_t *ill);
228 
229 static	void	phyint_flags_init(phyint_t *, t_uscalar_t);
230 
231 /*
232  * if we go over the memory footprint limit more than once in this msec
233  * interval, we'll start pruning aggressively.
234  */
235 int ip_min_frag_prune_time = 0;
236 
237 static ipft_t	ip_ioctl_ftbl[] = {
238 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
239 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
240 		IPFT_F_NO_REPLY },
241 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
242 	{ 0 }
243 };
244 
245 /* Simple ICMP IP Header Template */
246 static ipha_t icmp_ipha = {
247 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
248 };
249 
250 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
251 
252 static ip_m_t   ip_m_tbl[] = {
253 	{ DL_ETHER, IFT_ETHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
254 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_ether_v6intfid,
255 	    ip_nodef_v6intfid },
256 	{ DL_CSMACD, IFT_ISO88023, ETHERTYPE_IP, ETHERTYPE_IPV6,
257 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
258 	    ip_nodef_v6intfid },
259 	{ DL_TPB, IFT_ISO88024, ETHERTYPE_IP, ETHERTYPE_IPV6,
260 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
261 	    ip_nodef_v6intfid },
262 	{ DL_TPR, IFT_ISO88025, ETHERTYPE_IP, ETHERTYPE_IPV6,
263 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
264 	    ip_nodef_v6intfid },
265 	{ DL_FDDI, IFT_FDDI, ETHERTYPE_IP, ETHERTYPE_IPV6,
266 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_ether_v6intfid,
267 	    ip_nodef_v6intfid },
268 	{ DL_IB, IFT_IB, ETHERTYPE_IP, ETHERTYPE_IPV6,
269 	    ip_ib_v4_mapping, ip_ib_v6_mapping, ip_ib_v6intfid,
270 	    ip_nodef_v6intfid },
271 	{ DL_IPV4, IFT_IPV4, IPPROTO_ENCAP, IPPROTO_IPV6,
272 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv4_v6intfid,
273 	    ip_ipv4_v6destintfid },
274 	{ DL_IPV6, IFT_IPV6, IPPROTO_ENCAP, IPPROTO_IPV6,
275 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv6_v6intfid,
276 	    ip_ipv6_v6destintfid },
277 	{ DL_6TO4, IFT_6TO4, IPPROTO_ENCAP, IPPROTO_IPV6,
278 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv4_v6intfid,
279 	    ip_nodef_v6intfid },
280 	{ SUNW_DL_VNI, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
281 	    NULL, NULL, ip_nodef_v6intfid, ip_nodef_v6intfid },
282 	{ SUNW_DL_IPMP, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
283 	    NULL, NULL, ip_ipmp_v6intfid, ip_nodef_v6intfid },
284 	{ DL_OTHER, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
285 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
286 	    ip_nodef_v6intfid }
287 };
288 
289 char	ipif_loopback_name[] = "lo0";
290 
291 /* These are used by all IP network modules. */
292 sin6_t	sin6_null;	/* Zero address for quick clears */
293 sin_t	sin_null;	/* Zero address for quick clears */
294 
295 /* When set search for unused ipif_seqid */
296 static ipif_t	ipif_zero;
297 
298 /*
299  * ppa arena is created after these many
300  * interfaces have been plumbed.
301  */
302 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
303 
304 /*
305  * Allocate per-interface mibs.
306  * Returns true if ok. False otherwise.
307  *  ipsq  may not yet be allocated (loopback case ).
308  */
309 static boolean_t
ill_allocate_mibs(ill_t * ill)310 ill_allocate_mibs(ill_t *ill)
311 {
312 	/* Already allocated? */
313 	if (ill->ill_ip_mib != NULL) {
314 		if (ill->ill_isv6)
315 			ASSERT(ill->ill_icmp6_mib != NULL);
316 		return (B_TRUE);
317 	}
318 
319 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
320 	    KM_NOSLEEP);
321 	if (ill->ill_ip_mib == NULL) {
322 		return (B_FALSE);
323 	}
324 
325 	/* Setup static information */
326 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
327 	    sizeof (mib2_ipIfStatsEntry_t));
328 	if (ill->ill_isv6) {
329 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
330 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
331 		    sizeof (mib2_ipv6AddrEntry_t));
332 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
333 		    sizeof (mib2_ipv6RouteEntry_t));
334 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
335 		    sizeof (mib2_ipv6NetToMediaEntry_t));
336 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
337 		    sizeof (ipv6_member_t));
338 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
339 		    sizeof (ipv6_grpsrc_t));
340 	} else {
341 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
342 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
343 		    sizeof (mib2_ipAddrEntry_t));
344 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
345 		    sizeof (mib2_ipRouteEntry_t));
346 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
347 		    sizeof (mib2_ipNetToMediaEntry_t));
348 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
349 		    sizeof (ip_member_t));
350 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
351 		    sizeof (ip_grpsrc_t));
352 
353 		/*
354 		 * For a v4 ill, we are done at this point, because per ill
355 		 * icmp mibs are only used for v6.
356 		 */
357 		return (B_TRUE);
358 	}
359 
360 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
361 	    KM_NOSLEEP);
362 	if (ill->ill_icmp6_mib == NULL) {
363 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
364 		ill->ill_ip_mib = NULL;
365 		return (B_FALSE);
366 	}
367 	/* static icmp info */
368 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
369 	    sizeof (mib2_ipv6IfIcmpEntry_t);
370 	/*
371 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
372 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
373 	 * -> ill_phyint_reinit
374 	 */
375 	return (B_TRUE);
376 }
377 
378 /*
379  * Completely vaporize a lower level tap and all associated interfaces.
380  * ill_delete is called only out of ip_close when the device control
381  * stream is being closed.
382  */
383 void
ill_delete(ill_t * ill)384 ill_delete(ill_t *ill)
385 {
386 	ipif_t	*ipif;
387 	ill_t	*prev_ill;
388 	ip_stack_t	*ipst = ill->ill_ipst;
389 
390 	/*
391 	 * ill_delete may be forcibly entering the ipsq. The previous
392 	 * ioctl may not have completed and may need to be aborted.
393 	 * ipsq_flush takes care of it. If we don't need to enter the
394 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
395 	 * ill_delete_tail is sufficient.
396 	 */
397 	ipsq_flush(ill);
398 
399 	/*
400 	 * Nuke all interfaces.  ipif_free will take down the interface,
401 	 * remove it from the list, and free the data structure.
402 	 * Walk down the ipif list and remove the logical interfaces
403 	 * first before removing the main ipif. We can't unplumb
404 	 * zeroth interface first in the case of IPv6 as update_conn_ill
405 	 * -> ip_ll_multireq de-references ill_ipif for checking
406 	 * POINTOPOINT.
407 	 *
408 	 * If ill_ipif was not properly initialized (i.e low on memory),
409 	 * then no interfaces to clean up. In this case just clean up the
410 	 * ill.
411 	 */
412 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
413 		ipif_free(ipif);
414 
415 	/*
416 	 * clean out all the nce_t entries that depend on this
417 	 * ill for the ill_phys_addr.
418 	 */
419 	nce_flush(ill, B_TRUE);
420 
421 	/* Clean up msgs on pending upcalls for mrouted */
422 	reset_mrt_ill(ill);
423 
424 	update_conn_ill(ill, ipst);
425 
426 	/*
427 	 * Remove multicast references added as a result of calls to
428 	 * ip_join_allmulti().
429 	 */
430 	ip_purge_allmulti(ill);
431 
432 	/*
433 	 * If the ill being deleted is under IPMP, boot it out of the illgrp.
434 	 */
435 	if (IS_UNDER_IPMP(ill))
436 		ipmp_ill_leave_illgrp(ill);
437 
438 	/*
439 	 * ill_down will arrange to blow off any IRE's dependent on this
440 	 * ILL, and shut down fragmentation reassembly.
441 	 */
442 	ill_down(ill);
443 
444 	/* Let SCTP know, so that it can remove this from its list. */
445 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
446 
447 	/*
448 	 * Walk all CONNs that can have a reference on an ire or nce for this
449 	 * ill (we actually walk all that now have stale references).
450 	 */
451 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
452 
453 	/* With IPv6 we have dce_ifindex. Cleanup for neatness */
454 	if (ill->ill_isv6)
455 		dce_cleanup(ill->ill_phyint->phyint_ifindex, ipst);
456 
457 	/*
458 	 * If an address on this ILL is being used as a source address then
459 	 * clear out the pointers in other ILLs that point to this ILL.
460 	 */
461 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
462 	if (ill->ill_usesrc_grp_next != NULL) {
463 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
464 			ill_disband_usesrc_group(ill);
465 		} else {	/* consumer of the usesrc ILL */
466 			prev_ill = ill_prev_usesrc(ill);
467 			prev_ill->ill_usesrc_grp_next =
468 			    ill->ill_usesrc_grp_next;
469 		}
470 	}
471 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
472 }
473 
474 static void
ipif_non_duplicate(ipif_t * ipif)475 ipif_non_duplicate(ipif_t *ipif)
476 {
477 	ill_t *ill = ipif->ipif_ill;
478 	mutex_enter(&ill->ill_lock);
479 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
480 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
481 		ASSERT(ill->ill_ipif_dup_count > 0);
482 		ill->ill_ipif_dup_count--;
483 	}
484 	mutex_exit(&ill->ill_lock);
485 }
486 
487 /*
488  * ill_delete_tail is called from ip_modclose after all references
489  * to the closing ill are gone. The wait is done in ip_modclose
490  */
491 void
ill_delete_tail(ill_t * ill)492 ill_delete_tail(ill_t *ill)
493 {
494 	mblk_t	**mpp;
495 	ipif_t	*ipif;
496 	ip_stack_t *ipst = ill->ill_ipst;
497 
498 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
499 		ipif_non_duplicate(ipif);
500 		(void) ipif_down_tail(ipif);
501 	}
502 
503 	ASSERT(ill->ill_ipif_dup_count == 0);
504 
505 	/*
506 	 * If polling capability is enabled (which signifies direct
507 	 * upcall into IP and driver has ill saved as a handle),
508 	 * we need to make sure that unbind has completed before we
509 	 * let the ill disappear and driver no longer has any reference
510 	 * to this ill.
511 	 */
512 	mutex_enter(&ill->ill_lock);
513 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
514 		cv_wait(&ill->ill_cv, &ill->ill_lock);
515 	mutex_exit(&ill->ill_lock);
516 	ASSERT(!(ill->ill_capabilities &
517 	    (ILL_CAPAB_DLD | ILL_CAPAB_DLD_POLL | ILL_CAPAB_DLD_DIRECT)));
518 
519 	if (ill->ill_net_type != IRE_LOOPBACK)
520 		qprocsoff(ill->ill_rq);
521 
522 	/*
523 	 * We do an ipsq_flush once again now. New messages could have
524 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
525 	 * could also have landed up if an ioctl thread had looked up
526 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
527 	 * enqueued the ioctl when we did the ipsq_flush last time.
528 	 */
529 	ipsq_flush(ill);
530 
531 	/*
532 	 * Free capabilities.
533 	 */
534 	if (ill->ill_hcksum_capab != NULL) {
535 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
536 		ill->ill_hcksum_capab = NULL;
537 	}
538 
539 	if (ill->ill_zerocopy_capab != NULL) {
540 		kmem_free(ill->ill_zerocopy_capab,
541 		    sizeof (ill_zerocopy_capab_t));
542 		ill->ill_zerocopy_capab = NULL;
543 	}
544 
545 	if (ill->ill_lso_capab != NULL) {
546 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
547 		ill->ill_lso_capab = NULL;
548 	}
549 
550 	if (ill->ill_dld_capab != NULL) {
551 		kmem_free(ill->ill_dld_capab, sizeof (ill_dld_capab_t));
552 		ill->ill_dld_capab = NULL;
553 	}
554 
555 	/* Clean up ill_allowed_ips* related state */
556 	if (ill->ill_allowed_ips != NULL) {
557 		ASSERT(ill->ill_allowed_ips_cnt > 0);
558 		kmem_free(ill->ill_allowed_ips,
559 		    ill->ill_allowed_ips_cnt * sizeof (in6_addr_t));
560 		ill->ill_allowed_ips = NULL;
561 		ill->ill_allowed_ips_cnt = 0;
562 	}
563 
564 	while (ill->ill_ipif != NULL)
565 		ipif_free_tail(ill->ill_ipif);
566 
567 	/*
568 	 * We have removed all references to ilm from conn and the ones joined
569 	 * within the kernel.
570 	 *
571 	 * We don't walk conns, mrts and ires because
572 	 *
573 	 * 1) update_conn_ill and reset_mrt_ill cleans up conns and mrts.
574 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
575 	 *    ill references.
576 	 */
577 
578 	/*
579 	 * If this ill is an IPMP meta-interface, blow away the illgrp.  This
580 	 * is safe to do because the illgrp has already been unlinked from the
581 	 * group by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find it.
582 	 */
583 	if (IS_IPMP(ill)) {
584 		ipmp_illgrp_destroy(ill->ill_grp);
585 		ill->ill_grp = NULL;
586 	}
587 
588 	if (ill->ill_mphysaddr_list != NULL) {
589 		multiphysaddr_t *mpa, *tmpa;
590 
591 		mpa = ill->ill_mphysaddr_list;
592 		ill->ill_mphysaddr_list = NULL;
593 		while (mpa) {
594 			tmpa = mpa->mpa_next;
595 			kmem_free(mpa, sizeof (*mpa));
596 			mpa = tmpa;
597 		}
598 	}
599 	/*
600 	 * Take us out of the list of ILLs. ill_glist_delete -> phyint_free
601 	 * could free the phyint. No more reference to the phyint after this
602 	 * point.
603 	 */
604 	(void) ill_glist_delete(ill);
605 
606 	if (ill->ill_frag_ptr != NULL) {
607 		uint_t count;
608 
609 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
610 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
611 		}
612 		mi_free(ill->ill_frag_ptr);
613 		ill->ill_frag_ptr = NULL;
614 		ill->ill_frag_hash_tbl = NULL;
615 	}
616 
617 	freemsg(ill->ill_nd_lla_mp);
618 	/* Free all retained control messages. */
619 	mpp = &ill->ill_first_mp_to_free;
620 	do {
621 		while (mpp[0]) {
622 			mblk_t  *mp;
623 			mblk_t  *mp1;
624 
625 			mp = mpp[0];
626 			mpp[0] = mp->b_next;
627 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
628 				mp1->b_next = NULL;
629 				mp1->b_prev = NULL;
630 			}
631 			freemsg(mp);
632 		}
633 	} while (mpp++ != &ill->ill_last_mp_to_free);
634 
635 	ill_free_mib(ill);
636 
637 #ifdef DEBUG
638 	ill_trace_cleanup(ill);
639 #endif
640 
641 	/* The default multicast interface might have changed */
642 	ire_increment_multicast_generation(ipst, ill->ill_isv6);
643 
644 	/* Drop refcnt here */
645 	netstack_rele(ill->ill_ipst->ips_netstack);
646 	ill->ill_ipst = NULL;
647 }
648 
649 static void
ill_free_mib(ill_t * ill)650 ill_free_mib(ill_t *ill)
651 {
652 	ip_stack_t *ipst = ill->ill_ipst;
653 
654 	/*
655 	 * MIB statistics must not be lost, so when an interface
656 	 * goes away the counter values will be added to the global
657 	 * MIBs.
658 	 */
659 	if (ill->ill_ip_mib != NULL) {
660 		if (ill->ill_isv6) {
661 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
662 			    ill->ill_ip_mib);
663 		} else {
664 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
665 			    ill->ill_ip_mib);
666 		}
667 
668 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
669 		ill->ill_ip_mib = NULL;
670 	}
671 	if (ill->ill_icmp6_mib != NULL) {
672 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
673 		    ill->ill_icmp6_mib);
674 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
675 		ill->ill_icmp6_mib = NULL;
676 	}
677 }
678 
679 /*
680  * Concatenate together a physical address and a sap.
681  *
682  * Sap_lengths are interpreted as follows:
683  *   sap_length == 0	==>	no sap
684  *   sap_length > 0	==>	sap is at the head of the dlpi address
685  *   sap_length < 0	==>	sap is at the tail of the dlpi address
686  */
687 static void
ill_dlur_copy_address(uchar_t * phys_src,uint_t phys_length,t_scalar_t sap_src,t_scalar_t sap_length,uchar_t * dst)688 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
689     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
690 {
691 	uint16_t sap_addr = (uint16_t)sap_src;
692 
693 	if (sap_length == 0) {
694 		if (phys_src == NULL)
695 			bzero(dst, phys_length);
696 		else
697 			bcopy(phys_src, dst, phys_length);
698 	} else if (sap_length < 0) {
699 		if (phys_src == NULL)
700 			bzero(dst, phys_length);
701 		else
702 			bcopy(phys_src, dst, phys_length);
703 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
704 	} else {
705 		bcopy(&sap_addr, dst, sizeof (sap_addr));
706 		if (phys_src == NULL)
707 			bzero((char *)dst + sap_length, phys_length);
708 		else
709 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
710 	}
711 }
712 
713 /*
714  * Generate a dl_unitdata_req mblk for the device and address given.
715  * addr_length is the length of the physical portion of the address.
716  * If addr is NULL include an all zero address of the specified length.
717  * TRUE? In any case, addr_length is taken to be the entire length of the
718  * dlpi address, including the absolute value of sap_length.
719  */
720 mblk_t *
ill_dlur_gen(uchar_t * addr,uint_t addr_length,t_uscalar_t sap,t_scalar_t sap_length)721 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
722     t_scalar_t sap_length)
723 {
724 	dl_unitdata_req_t *dlur;
725 	mblk_t	*mp;
726 	t_scalar_t	abs_sap_length;		/* absolute value */
727 
728 	abs_sap_length = ABS(sap_length);
729 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
730 	    DL_UNITDATA_REQ);
731 	if (mp == NULL)
732 		return (NULL);
733 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
734 	/* HACK: accomodate incompatible DLPI drivers */
735 	if (addr_length == 8)
736 		addr_length = 6;
737 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
738 	dlur->dl_dest_addr_offset = sizeof (*dlur);
739 	dlur->dl_priority.dl_min = 0;
740 	dlur->dl_priority.dl_max = 0;
741 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
742 	    (uchar_t *)&dlur[1]);
743 	return (mp);
744 }
745 
746 /*
747  * Add the pending mp to the list. There can be only 1 pending mp
748  * in the list. Any exclusive ioctl that needs to wait for a response
749  * from another module or driver needs to use this function to set
750  * the ipx_pending_mp to the ioctl mblk and wait for the response from
751  * the other module/driver. This is also used while waiting for the
752  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
753  */
754 boolean_t
ipsq_pending_mp_add(conn_t * connp,ipif_t * ipif,queue_t * q,mblk_t * add_mp,int waitfor)755 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
756     int waitfor)
757 {
758 	ipxop_t	*ipx = ipif->ipif_ill->ill_phyint->phyint_ipsq->ipsq_xop;
759 
760 	ASSERT(IAM_WRITER_IPIF(ipif));
761 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
762 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
763 	ASSERT(ipx->ipx_pending_mp == NULL);
764 	/*
765 	 * The caller may be using a different ipif than the one passed into
766 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
767 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
768 	 * that `ipx_current_ipif == ipif'.
769 	 */
770 	ASSERT(ipx->ipx_current_ipif != NULL);
771 
772 	/*
773 	 * M_IOCDATA from ioctls, M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the
774 	 * driver.
775 	 */
776 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_ERROR) ||
777 	    (DB_TYPE(add_mp) == M_HANGUP) || (DB_TYPE(add_mp) == M_PROTO) ||
778 	    (DB_TYPE(add_mp) == M_PCPROTO));
779 
780 	if (connp != NULL) {
781 		ASSERT(MUTEX_HELD(&connp->conn_lock));
782 		/*
783 		 * Return error if the conn has started closing. The conn
784 		 * could have finished cleaning up the pending mp list,
785 		 * If so we should not add another mp to the list negating
786 		 * the cleanup.
787 		 */
788 		if (connp->conn_state_flags & CONN_CLOSING)
789 			return (B_FALSE);
790 	}
791 	mutex_enter(&ipx->ipx_lock);
792 	ipx->ipx_pending_ipif = ipif;
793 	/*
794 	 * Note down the queue in b_queue. This will be returned by
795 	 * ipsq_pending_mp_get. Caller will then use these values to restart
796 	 * the processing
797 	 */
798 	add_mp->b_next = NULL;
799 	add_mp->b_queue = q;
800 	ipx->ipx_pending_mp = add_mp;
801 	ipx->ipx_waitfor = waitfor;
802 	mutex_exit(&ipx->ipx_lock);
803 
804 	if (connp != NULL)
805 		connp->conn_oper_pending_ill = ipif->ipif_ill;
806 
807 	return (B_TRUE);
808 }
809 
810 /*
811  * Retrieve the ipx_pending_mp and return it. There can be only 1 mp
812  * queued in the list.
813  */
814 mblk_t *
ipsq_pending_mp_get(ipsq_t * ipsq,conn_t ** connpp)815 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
816 {
817 	mblk_t	*curr = NULL;
818 	ipxop_t	*ipx = ipsq->ipsq_xop;
819 
820 	*connpp = NULL;
821 	mutex_enter(&ipx->ipx_lock);
822 	if (ipx->ipx_pending_mp == NULL) {
823 		mutex_exit(&ipx->ipx_lock);
824 		return (NULL);
825 	}
826 
827 	/* There can be only 1 such excl message */
828 	curr = ipx->ipx_pending_mp;
829 	ASSERT(curr->b_next == NULL);
830 	ipx->ipx_pending_ipif = NULL;
831 	ipx->ipx_pending_mp = NULL;
832 	ipx->ipx_waitfor = 0;
833 	mutex_exit(&ipx->ipx_lock);
834 
835 	if (CONN_Q(curr->b_queue)) {
836 		/*
837 		 * This mp did a refhold on the conn, at the start of the ioctl.
838 		 * So we can safely return a pointer to the conn to the caller.
839 		 */
840 		*connpp = Q_TO_CONN(curr->b_queue);
841 	} else {
842 		*connpp = NULL;
843 	}
844 	curr->b_next = NULL;
845 	curr->b_prev = NULL;
846 	return (curr);
847 }
848 
849 /*
850  * Cleanup the ioctl mp queued in ipx_pending_mp
851  * - Called in the ill_delete path
852  * - Called in the M_ERROR or M_HANGUP path on the ill.
853  * - Called in the conn close path.
854  *
855  * Returns success on finding the pending mblk associated with the ioctl or
856  * exclusive operation in progress, failure otherwise.
857  */
858 boolean_t
ipsq_pending_mp_cleanup(ill_t * ill,conn_t * connp)859 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
860 {
861 	mblk_t	*mp;
862 	ipxop_t	*ipx;
863 	queue_t	*q;
864 	ipif_t	*ipif;
865 	int	cmd;
866 
867 	ASSERT(IAM_WRITER_ILL(ill));
868 	ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
869 
870 	mutex_enter(&ipx->ipx_lock);
871 	mp = ipx->ipx_pending_mp;
872 	if (connp != NULL) {
873 		if (mp == NULL || mp->b_queue != CONNP_TO_WQ(connp)) {
874 			/*
875 			 * Nothing to clean since the conn that is closing
876 			 * does not have a matching pending mblk in
877 			 * ipx_pending_mp.
878 			 */
879 			mutex_exit(&ipx->ipx_lock);
880 			return (B_FALSE);
881 		}
882 	} else {
883 		/*
884 		 * A non-zero ill_error signifies we are called in the
885 		 * M_ERROR or M_HANGUP path and we need to unconditionally
886 		 * abort any current ioctl and do the corresponding cleanup.
887 		 * A zero ill_error means we are in the ill_delete path and
888 		 * we do the cleanup only if there is a pending mp.
889 		 */
890 		if (mp == NULL && ill->ill_error == 0) {
891 			mutex_exit(&ipx->ipx_lock);
892 			return (B_FALSE);
893 		}
894 	}
895 
896 	/* Now remove from the ipx_pending_mp */
897 	ipx->ipx_pending_mp = NULL;
898 	ipif = ipx->ipx_pending_ipif;
899 	ipx->ipx_pending_ipif = NULL;
900 	ipx->ipx_waitfor = 0;
901 	ipx->ipx_current_ipif = NULL;
902 	cmd = ipx->ipx_current_ioctl;
903 	ipx->ipx_current_ioctl = 0;
904 	ipx->ipx_current_done = B_TRUE;
905 	mutex_exit(&ipx->ipx_lock);
906 
907 	if (mp == NULL)
908 		return (B_FALSE);
909 
910 	q = mp->b_queue;
911 	mp->b_next = NULL;
912 	mp->b_prev = NULL;
913 	mp->b_queue = NULL;
914 
915 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
916 		DTRACE_PROBE4(ipif__ioctl,
917 		    char *, "ipsq_pending_mp_cleanup",
918 		    int, cmd, ill_t *, ipif == NULL ? NULL : ipif->ipif_ill,
919 		    ipif_t *, ipif);
920 		if (connp == NULL) {
921 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
922 		} else {
923 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
924 			mutex_enter(&ipif->ipif_ill->ill_lock);
925 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
926 			mutex_exit(&ipif->ipif_ill->ill_lock);
927 		}
928 	} else {
929 		inet_freemsg(mp);
930 	}
931 	return (B_TRUE);
932 }
933 
934 /*
935  * Called in the conn close path and ill delete path
936  */
937 static void
ipsq_xopq_mp_cleanup(ill_t * ill,conn_t * connp)938 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
939 {
940 	ipsq_t	*ipsq;
941 	mblk_t	*prev;
942 	mblk_t	*curr;
943 	mblk_t	*next;
944 	queue_t	*wq, *rq = NULL;
945 	mblk_t	*tmp_list = NULL;
946 
947 	ASSERT(IAM_WRITER_ILL(ill));
948 	if (connp != NULL)
949 		wq = CONNP_TO_WQ(connp);
950 	else
951 		wq = ill->ill_wq;
952 
953 	/*
954 	 * In the case of lo0 being unplumbed, ill_wq will be NULL. Guard
955 	 * against this here.
956 	 */
957 	if (wq != NULL)
958 		rq = RD(wq);
959 
960 	ipsq = ill->ill_phyint->phyint_ipsq;
961 	/*
962 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
963 	 * In the case of ioctl from a conn, there can be only 1 mp
964 	 * queued on the ipsq. If an ill is being unplumbed flush all
965 	 * the messages.
966 	 */
967 	mutex_enter(&ipsq->ipsq_lock);
968 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
969 	    curr = next) {
970 		next = curr->b_next;
971 		if (connp == NULL ||
972 		    (curr->b_queue == wq || curr->b_queue == rq)) {
973 			/* Unlink the mblk from the pending mp list */
974 			if (prev != NULL) {
975 				prev->b_next = curr->b_next;
976 			} else {
977 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
978 				ipsq->ipsq_xopq_mphead = curr->b_next;
979 			}
980 			if (ipsq->ipsq_xopq_mptail == curr)
981 				ipsq->ipsq_xopq_mptail = prev;
982 			/*
983 			 * Create a temporary list and release the ipsq lock
984 			 * New elements are added to the head of the tmp_list
985 			 */
986 			curr->b_next = tmp_list;
987 			tmp_list = curr;
988 		} else {
989 			prev = curr;
990 		}
991 	}
992 	mutex_exit(&ipsq->ipsq_lock);
993 
994 	while (tmp_list != NULL) {
995 		curr = tmp_list;
996 		tmp_list = curr->b_next;
997 		curr->b_next = NULL;
998 		curr->b_prev = NULL;
999 		wq = curr->b_queue;
1000 		curr->b_queue = NULL;
1001 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1002 			DTRACE_PROBE4(ipif__ioctl,
1003 			    char *, "ipsq_xopq_mp_cleanup",
1004 			    int, 0, ill_t *, NULL, ipif_t *, NULL);
1005 			ip_ioctl_finish(wq, curr, ENXIO, connp != NULL ?
1006 			    CONN_CLOSE : NO_COPYOUT, NULL);
1007 		} else {
1008 			/*
1009 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1010 			 * this can't be just inet_freemsg. we have to
1011 			 * restart it otherwise the thread will be stuck.
1012 			 */
1013 			inet_freemsg(curr);
1014 		}
1015 	}
1016 }
1017 
1018 /*
1019  * This conn has started closing. Cleanup any pending ioctl from this conn.
1020  * STREAMS ensures that there can be at most 1 active ioctl on a stream.
1021  */
1022 void
conn_ioctl_cleanup(conn_t * connp)1023 conn_ioctl_cleanup(conn_t *connp)
1024 {
1025 	ipsq_t	*ipsq;
1026 	ill_t	*ill;
1027 	boolean_t refheld;
1028 
1029 	/*
1030 	 * Check for a queued ioctl. If the ioctl has not yet started, the mp
1031 	 * is pending in the list headed by ipsq_xopq_head. If the ioctl has
1032 	 * started the mp could be present in ipx_pending_mp. Note that if
1033 	 * conn_oper_pending_ill is NULL, the ioctl may still be in flight and
1034 	 * not yet queued anywhere. In this case, the conn close code will wait
1035 	 * until the conn_ref is dropped. If the stream was a tcp stream, then
1036 	 * tcp_close will wait first until all ioctls have completed for this
1037 	 * conn.
1038 	 */
1039 	mutex_enter(&connp->conn_lock);
1040 	ill = connp->conn_oper_pending_ill;
1041 	if (ill == NULL) {
1042 		mutex_exit(&connp->conn_lock);
1043 		return;
1044 	}
1045 
1046 	/*
1047 	 * We may not be able to refhold the ill if the ill/ipif
1048 	 * is changing. But we need to make sure that the ill will
1049 	 * not vanish. So we just bump up the ill_waiter count.
1050 	 */
1051 	refheld = ill_waiter_inc(ill);
1052 	mutex_exit(&connp->conn_lock);
1053 	if (refheld) {
1054 		if (ipsq_enter(ill, B_TRUE, NEW_OP)) {
1055 			ill_waiter_dcr(ill);
1056 			/*
1057 			 * Check whether this ioctl has started and is
1058 			 * pending. If it is not found there then check
1059 			 * whether this ioctl has not even started and is in
1060 			 * the ipsq_xopq list.
1061 			 */
1062 			if (!ipsq_pending_mp_cleanup(ill, connp))
1063 				ipsq_xopq_mp_cleanup(ill, connp);
1064 			ipsq = ill->ill_phyint->phyint_ipsq;
1065 			ipsq_exit(ipsq);
1066 			return;
1067 		}
1068 	}
1069 
1070 	/*
1071 	 * The ill is also closing and we could not bump up the
1072 	 * ill_waiter_count or we could not enter the ipsq. Leave
1073 	 * the cleanup to ill_delete
1074 	 */
1075 	mutex_enter(&connp->conn_lock);
1076 	while (connp->conn_oper_pending_ill != NULL)
1077 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1078 	mutex_exit(&connp->conn_lock);
1079 	if (refheld)
1080 		ill_waiter_dcr(ill);
1081 }
1082 
1083 /*
1084  * ipcl_walk function for cleaning up conn_*_ill fields.
1085  * Note that we leave ixa_multicast_ifindex, conn_incoming_ifindex, and
1086  * conn_bound_if in place. We prefer dropping
1087  * packets instead of sending them out the wrong interface, or accepting
1088  * packets from the wrong ifindex.
1089  */
1090 static void
conn_cleanup_ill(conn_t * connp,caddr_t arg)1091 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1092 {
1093 	ill_t	*ill = (ill_t *)arg;
1094 
1095 	mutex_enter(&connp->conn_lock);
1096 	if (connp->conn_dhcpinit_ill == ill) {
1097 		connp->conn_dhcpinit_ill = NULL;
1098 		ASSERT(ill->ill_dhcpinit != 0);
1099 		atomic_dec_32(&ill->ill_dhcpinit);
1100 		ill_set_inputfn(ill);
1101 	}
1102 	mutex_exit(&connp->conn_lock);
1103 }
1104 
1105 static int
ill_down_ipifs_tail(ill_t * ill)1106 ill_down_ipifs_tail(ill_t *ill)
1107 {
1108 	ipif_t	*ipif;
1109 	int err;
1110 
1111 	ASSERT(IAM_WRITER_ILL(ill));
1112 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1113 		ipif_non_duplicate(ipif);
1114 		/*
1115 		 * ipif_down_tail will call arp_ll_down on the last ipif
1116 		 * and typically return EINPROGRESS when the DL_UNBIND is sent.
1117 		 */
1118 		if ((err = ipif_down_tail(ipif)) != 0)
1119 			return (err);
1120 	}
1121 	return (0);
1122 }
1123 
1124 /* ARGSUSED */
1125 void
ipif_all_down_tail(ipsq_t * ipsq,queue_t * q,mblk_t * mp,void * dummy_arg)1126 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1127 {
1128 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1129 	(void) ill_down_ipifs_tail(q->q_ptr);
1130 	freemsg(mp);
1131 	ipsq_current_finish(ipsq);
1132 }
1133 
1134 /*
1135  * ill_down_start is called when we want to down this ill and bring it up again
1136  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1137  * all interfaces, but don't tear down any plumbing.
1138  */
1139 boolean_t
ill_down_start(queue_t * q,mblk_t * mp)1140 ill_down_start(queue_t *q, mblk_t *mp)
1141 {
1142 	ill_t	*ill = q->q_ptr;
1143 	ipif_t	*ipif;
1144 
1145 	ASSERT(IAM_WRITER_ILL(ill));
1146 	/*
1147 	 * It is possible that some ioctl is already in progress while we
1148 	 * received the M_ERROR / M_HANGUP in which case, we need to abort
1149 	 * the ioctl. ill_down_start() is being processed as CUR_OP rather
1150 	 * than as NEW_OP since the cause of the M_ERROR / M_HANGUP may prevent
1151 	 * the in progress ioctl from ever completing.
1152 	 *
1153 	 * The thread that started the ioctl (if any) must have returned,
1154 	 * since we are now executing as writer. After the 2 calls below,
1155 	 * the state of the ipsq and the ill would reflect no trace of any
1156 	 * pending operation. Subsequently if there is any response to the
1157 	 * original ioctl from the driver, it would be discarded as an
1158 	 * unsolicited message from the driver.
1159 	 */
1160 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1161 	ill_dlpi_clear_deferred(ill);
1162 
1163 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1164 		(void) ipif_down(ipif, NULL, NULL);
1165 
1166 	ill_down(ill);
1167 
1168 	/*
1169 	 * Walk all CONNs that can have a reference on an ire or nce for this
1170 	 * ill (we actually walk all that now have stale references).
1171 	 */
1172 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ill->ill_ipst);
1173 
1174 	/* With IPv6 we have dce_ifindex. Cleanup for neatness */
1175 	if (ill->ill_isv6)
1176 		dce_cleanup(ill->ill_phyint->phyint_ifindex, ill->ill_ipst);
1177 
1178 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1179 
1180 	/*
1181 	 * Atomically test and add the pending mp if references are active.
1182 	 */
1183 	mutex_enter(&ill->ill_lock);
1184 	if (!ill_is_quiescent(ill)) {
1185 		/* call cannot fail since `conn_t *' argument is NULL */
1186 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1187 		    mp, ILL_DOWN);
1188 		mutex_exit(&ill->ill_lock);
1189 		return (B_FALSE);
1190 	}
1191 	mutex_exit(&ill->ill_lock);
1192 	return (B_TRUE);
1193 }
1194 
1195 static void
ill_down(ill_t * ill)1196 ill_down(ill_t *ill)
1197 {
1198 	mblk_t	*mp;
1199 	ip_stack_t	*ipst = ill->ill_ipst;
1200 
1201 	/*
1202 	 * Blow off any IREs dependent on this ILL.
1203 	 * The caller needs to handle conn_ixa_cleanup
1204 	 */
1205 	ill_delete_ires(ill);
1206 
1207 	ire_walk_ill(0, 0, ill_downi, ill, ill);
1208 
1209 	/* Remove any conn_*_ill depending on this ill */
1210 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1211 
1212 	/*
1213 	 * Free state for additional IREs.
1214 	 */
1215 	mutex_enter(&ill->ill_saved_ire_lock);
1216 	mp = ill->ill_saved_ire_mp;
1217 	ill->ill_saved_ire_mp = NULL;
1218 	ill->ill_saved_ire_cnt = 0;
1219 	mutex_exit(&ill->ill_saved_ire_lock);
1220 	freemsg(mp);
1221 }
1222 
1223 /*
1224  * ire_walk routine used to delete every IRE that depends on
1225  * 'ill'.  (Always called as writer, and may only be called from ire_walk.)
1226  *
1227  * Note: since the routes added by the kernel are deleted separately,
1228  * this will only be 1) IRE_IF_CLONE and 2) manually added IRE_INTERFACE.
1229  *
1230  * We also remove references on ire_nce_cache entries that refer to the ill.
1231  */
1232 void
ill_downi(ire_t * ire,char * ill_arg)1233 ill_downi(ire_t *ire, char *ill_arg)
1234 {
1235 	ill_t	*ill = (ill_t *)ill_arg;
1236 	nce_t	*nce;
1237 
1238 	mutex_enter(&ire->ire_lock);
1239 	nce = ire->ire_nce_cache;
1240 	if (nce != NULL && nce->nce_ill == ill)
1241 		ire->ire_nce_cache = NULL;
1242 	else
1243 		nce = NULL;
1244 	mutex_exit(&ire->ire_lock);
1245 	if (nce != NULL)
1246 		nce_refrele(nce);
1247 	if (ire->ire_ill == ill) {
1248 		/*
1249 		 * The existing interface binding for ire must be
1250 		 * deleted before trying to bind the route to another
1251 		 * interface. However, since we are using the contents of the
1252 		 * ire after ire_delete, the caller has to ensure that
1253 		 * CONDEMNED (deleted) ire's are not removed from the list
1254 		 * when ire_delete() returns. Currently ill_downi() is
1255 		 * only called as part of ire_walk*() routines, so that
1256 		 * the irb_refhold() done by ire_walk*() will ensure that
1257 		 * ire_delete() does not lead to ire_inactive().
1258 		 */
1259 		ASSERT(ire->ire_bucket->irb_refcnt > 0);
1260 		ire_delete(ire);
1261 		if (ire->ire_unbound)
1262 			ire_rebind(ire);
1263 	}
1264 }
1265 
1266 /* Remove IRE_IF_CLONE on this ill */
1267 void
ill_downi_if_clone(ire_t * ire,char * ill_arg)1268 ill_downi_if_clone(ire_t *ire, char *ill_arg)
1269 {
1270 	ill_t	*ill = (ill_t *)ill_arg;
1271 
1272 	ASSERT(ire->ire_type & IRE_IF_CLONE);
1273 	if (ire->ire_ill == ill)
1274 		ire_delete(ire);
1275 }
1276 
1277 /* Consume an M_IOCACK of the fastpath probe. */
1278 void
ill_fastpath_ack(ill_t * ill,mblk_t * mp)1279 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1280 {
1281 	mblk_t	*mp1 = mp;
1282 
1283 	/*
1284 	 * If this was the first attempt turn on the fastpath probing.
1285 	 */
1286 	mutex_enter(&ill->ill_lock);
1287 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1288 		ill->ill_dlpi_fastpath_state = IDS_OK;
1289 	mutex_exit(&ill->ill_lock);
1290 
1291 	/* Free the M_IOCACK mblk, hold on to the data */
1292 	mp = mp->b_cont;
1293 	freeb(mp1);
1294 	if (mp == NULL)
1295 		return;
1296 	if (mp->b_cont != NULL)
1297 		nce_fastpath_update(ill, mp);
1298 	else
1299 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1300 	freemsg(mp);
1301 }
1302 
1303 /*
1304  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1305  * The data portion of the request is a dl_unitdata_req_t template for
1306  * what we would send downstream in the absence of a fastpath confirmation.
1307  */
1308 int
ill_fastpath_probe(ill_t * ill,mblk_t * dlur_mp)1309 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1310 {
1311 	struct iocblk	*ioc;
1312 	mblk_t	*mp;
1313 
1314 	if (dlur_mp == NULL)
1315 		return (EINVAL);
1316 
1317 	mutex_enter(&ill->ill_lock);
1318 	switch (ill->ill_dlpi_fastpath_state) {
1319 	case IDS_FAILED:
1320 		/*
1321 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1322 		 * support it.
1323 		 */
1324 		mutex_exit(&ill->ill_lock);
1325 		return (ENOTSUP);
1326 	case IDS_UNKNOWN:
1327 		/* This is the first probe */
1328 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1329 		break;
1330 	default:
1331 		break;
1332 	}
1333 	mutex_exit(&ill->ill_lock);
1334 
1335 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1336 		return (EAGAIN);
1337 
1338 	mp->b_cont = copyb(dlur_mp);
1339 	if (mp->b_cont == NULL) {
1340 		freeb(mp);
1341 		return (EAGAIN);
1342 	}
1343 
1344 	ioc = (struct iocblk *)mp->b_rptr;
1345 	ioc->ioc_count = msgdsize(mp->b_cont);
1346 
1347 	DTRACE_PROBE3(ill__dlpi, char *, "ill_fastpath_probe",
1348 	    char *, "DL_IOC_HDR_INFO", ill_t *, ill);
1349 	putnext(ill->ill_wq, mp);
1350 	return (0);
1351 }
1352 
1353 void
ill_capability_probe(ill_t * ill)1354 ill_capability_probe(ill_t *ill)
1355 {
1356 	mblk_t	*mp;
1357 
1358 	ASSERT(IAM_WRITER_ILL(ill));
1359 
1360 	if (ill->ill_dlpi_capab_state != IDCS_UNKNOWN &&
1361 	    ill->ill_dlpi_capab_state != IDCS_FAILED)
1362 		return;
1363 
1364 	/*
1365 	 * We are starting a new cycle of capability negotiation.
1366 	 * Free up the capab reset messages of any previous incarnation.
1367 	 * We will do a fresh allocation when we get the response to our probe
1368 	 */
1369 	if (ill->ill_capab_reset_mp != NULL) {
1370 		freemsg(ill->ill_capab_reset_mp);
1371 		ill->ill_capab_reset_mp = NULL;
1372 	}
1373 
1374 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1375 
1376 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t), DL_CAPABILITY_REQ);
1377 	if (mp == NULL)
1378 		return;
1379 
1380 	ill_capability_send(ill, mp);
1381 	ill->ill_dlpi_capab_state = IDCS_PROBE_SENT;
1382 }
1383 
1384 void
ill_capability_reset(ill_t * ill,boolean_t reneg)1385 ill_capability_reset(ill_t *ill, boolean_t reneg)
1386 {
1387 	ASSERT(IAM_WRITER_ILL(ill));
1388 
1389 	if (ill->ill_dlpi_capab_state != IDCS_OK)
1390 		return;
1391 
1392 	ill->ill_dlpi_capab_state = reneg ? IDCS_RENEG : IDCS_RESET_SENT;
1393 
1394 	ill_capability_send(ill, ill->ill_capab_reset_mp);
1395 	ill->ill_capab_reset_mp = NULL;
1396 	/*
1397 	 * We turn off all capabilities except those pertaining to
1398 	 * direct function call capabilities viz. ILL_CAPAB_DLD*
1399 	 * which will be turned off by the corresponding reset functions.
1400 	 */
1401 	ill->ill_capabilities &= ~(ILL_CAPAB_HCKSUM  | ILL_CAPAB_ZEROCOPY);
1402 }
1403 
1404 static void
ill_capability_reset_alloc(ill_t * ill)1405 ill_capability_reset_alloc(ill_t *ill)
1406 {
1407 	mblk_t *mp;
1408 	size_t	size = 0;
1409 	int	err;
1410 	dl_capability_req_t	*capb;
1411 
1412 	ASSERT(IAM_WRITER_ILL(ill));
1413 	ASSERT(ill->ill_capab_reset_mp == NULL);
1414 
1415 	if (ILL_HCKSUM_CAPABLE(ill)) {
1416 		size += sizeof (dl_capability_sub_t) +
1417 		    sizeof (dl_capab_hcksum_t);
1418 	}
1419 
1420 	if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) {
1421 		size += sizeof (dl_capability_sub_t) +
1422 		    sizeof (dl_capab_zerocopy_t);
1423 	}
1424 
1425 	if (ill->ill_capabilities & ILL_CAPAB_DLD) {
1426 		size += sizeof (dl_capability_sub_t) +
1427 		    sizeof (dl_capab_dld_t);
1428 	}
1429 
1430 	mp = allocb_wait(size + sizeof (dl_capability_req_t), BPRI_MED,
1431 	    STR_NOSIG, &err);
1432 
1433 	mp->b_datap->db_type = M_PROTO;
1434 	bzero(mp->b_rptr, size + sizeof (dl_capability_req_t));
1435 
1436 	capb = (dl_capability_req_t *)mp->b_rptr;
1437 	capb->dl_primitive = DL_CAPABILITY_REQ;
1438 	capb->dl_sub_offset = sizeof (dl_capability_req_t);
1439 	capb->dl_sub_length = size;
1440 
1441 	mp->b_wptr += sizeof (dl_capability_req_t);
1442 
1443 	/*
1444 	 * Each handler fills in the corresponding dl_capability_sub_t
1445 	 * inside the mblk,
1446 	 */
1447 	ill_capability_hcksum_reset_fill(ill, mp);
1448 	ill_capability_zerocopy_reset_fill(ill, mp);
1449 	ill_capability_dld_reset_fill(ill, mp);
1450 
1451 	ill->ill_capab_reset_mp = mp;
1452 }
1453 
1454 static void
ill_capability_id_ack(ill_t * ill,mblk_t * mp,dl_capability_sub_t * outers)1455 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1456 {
1457 	dl_capab_id_t *id_ic;
1458 	uint_t sub_dl_cap = outers->dl_cap;
1459 	dl_capability_sub_t *inners;
1460 	uint8_t *capend;
1461 
1462 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1463 
1464 	/*
1465 	 * Note: range checks here are not absolutely sufficient to
1466 	 * make us robust against malformed messages sent by drivers;
1467 	 * this is in keeping with the rest of IP's dlpi handling.
1468 	 * (Remember, it's coming from something else in the kernel
1469 	 * address space)
1470 	 */
1471 
1472 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1473 	if (capend > mp->b_wptr) {
1474 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1475 		    "malformed sub-capability too long for mblk");
1476 		return;
1477 	}
1478 
1479 	id_ic = (dl_capab_id_t *)(outers + 1);
1480 
1481 	inners = &id_ic->id_subcap;
1482 	if (outers->dl_length < sizeof (*id_ic) ||
1483 	    inners->dl_length > (outers->dl_length - sizeof (*inners))) {
1484 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1485 		    "encapsulated capab type %d too long for mblk",
1486 		    inners->dl_cap);
1487 		return;
1488 	}
1489 
1490 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1491 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1492 		    "isn't as expected; pass-thru module(s) detected, "
1493 		    "discarding capability\n", inners->dl_cap));
1494 		return;
1495 	}
1496 
1497 	/* Process the encapsulated sub-capability */
1498 	ill_capability_dispatch(ill, mp, inners);
1499 }
1500 
1501 static void
ill_capability_dld_reset_fill(ill_t * ill,mblk_t * mp)1502 ill_capability_dld_reset_fill(ill_t *ill, mblk_t *mp)
1503 {
1504 	dl_capability_sub_t *dl_subcap;
1505 
1506 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
1507 		return;
1508 
1509 	/*
1510 	 * The dl_capab_dld_t that follows the dl_capability_sub_t is not
1511 	 * initialized below since it is not used by DLD.
1512 	 */
1513 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1514 	dl_subcap->dl_cap = DL_CAPAB_DLD;
1515 	dl_subcap->dl_length = sizeof (dl_capab_dld_t);
1516 
1517 	mp->b_wptr += sizeof (dl_capability_sub_t) + sizeof (dl_capab_dld_t);
1518 }
1519 
1520 static void
ill_capability_dispatch(ill_t * ill,mblk_t * mp,dl_capability_sub_t * subp)1521 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp)
1522 {
1523 	/*
1524 	 * If no ipif was brought up over this ill, this DL_CAPABILITY_REQ/ACK
1525 	 * is only to get the VRRP capability.
1526 	 *
1527 	 * Note that we cannot check ill_ipif_up_count here since
1528 	 * ill_ipif_up_count is only incremented when the resolver is setup.
1529 	 * That is done asynchronously, and can race with this function.
1530 	 */
1531 	if (!ill->ill_dl_up) {
1532 		if (subp->dl_cap == DL_CAPAB_VRRP)
1533 			ill_capability_vrrp_ack(ill, mp, subp);
1534 		return;
1535 	}
1536 
1537 	switch (subp->dl_cap) {
1538 	case DL_CAPAB_HCKSUM:
1539 		ill_capability_hcksum_ack(ill, mp, subp);
1540 		break;
1541 	case DL_CAPAB_ZEROCOPY:
1542 		ill_capability_zerocopy_ack(ill, mp, subp);
1543 		break;
1544 	case DL_CAPAB_DLD:
1545 		ill_capability_dld_ack(ill, mp, subp);
1546 		break;
1547 	case DL_CAPAB_VRRP:
1548 		break;
1549 	default:
1550 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
1551 		    subp->dl_cap));
1552 	}
1553 }
1554 
1555 /*
1556  * Process the vrrp capability received from a DLS Provider. isub must point
1557  * to the sub-capability (DL_CAPAB_VRRP) of a DL_CAPABILITY_ACK message.
1558  */
1559 static void
ill_capability_vrrp_ack(ill_t * ill,mblk_t * mp,dl_capability_sub_t * isub)1560 ill_capability_vrrp_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1561 {
1562 	dl_capab_vrrp_t	*vrrp;
1563 	uint_t		sub_dl_cap = isub->dl_cap;
1564 	uint8_t		*capend;
1565 
1566 	ASSERT(IAM_WRITER_ILL(ill));
1567 	ASSERT(sub_dl_cap == DL_CAPAB_VRRP);
1568 
1569 	/*
1570 	 * Note: range checks here are not absolutely sufficient to
1571 	 * make us robust against malformed messages sent by drivers;
1572 	 * this is in keeping with the rest of IP's dlpi handling.
1573 	 * (Remember, it's coming from something else in the kernel
1574 	 * address space)
1575 	 */
1576 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1577 	if (capend > mp->b_wptr) {
1578 		cmn_err(CE_WARN, "ill_capability_vrrp_ack: "
1579 		    "malformed sub-capability too long for mblk");
1580 		return;
1581 	}
1582 	vrrp = (dl_capab_vrrp_t *)(isub + 1);
1583 
1584 	/*
1585 	 * Compare the IP address family and set ILLF_VRRP for the right ill.
1586 	 */
1587 	if ((vrrp->vrrp_af == AF_INET6 && ill->ill_isv6) ||
1588 	    (vrrp->vrrp_af == AF_INET && !ill->ill_isv6)) {
1589 		ill->ill_flags |= ILLF_VRRP;
1590 	}
1591 }
1592 
1593 /*
1594  * Process a hardware checksum offload capability negotiation ack received
1595  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
1596  * of a DL_CAPABILITY_ACK message.
1597  */
1598 static void
ill_capability_hcksum_ack(ill_t * ill,mblk_t * mp,dl_capability_sub_t * isub)1599 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1600 {
1601 	dl_capability_req_t	*ocap;
1602 	dl_capab_hcksum_t	*ihck, *ohck;
1603 	ill_hcksum_capab_t	**ill_hcksum;
1604 	mblk_t			*nmp = NULL;
1605 	uint_t			sub_dl_cap = isub->dl_cap;
1606 	uint8_t			*capend;
1607 
1608 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
1609 
1610 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
1611 
1612 	/*
1613 	 * Note: range checks here are not absolutely sufficient to
1614 	 * make us robust against malformed messages sent by drivers;
1615 	 * this is in keeping with the rest of IP's dlpi handling.
1616 	 * (Remember, it's coming from something else in the kernel
1617 	 * address space)
1618 	 */
1619 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1620 	if (capend > mp->b_wptr) {
1621 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1622 		    "malformed sub-capability too long for mblk");
1623 		return;
1624 	}
1625 
1626 	/*
1627 	 * There are two types of acks we process here:
1628 	 * 1. acks in reply to a (first form) generic capability req
1629 	 *    (no ENABLE flag set)
1630 	 * 2. acks in reply to a ENABLE capability req.
1631 	 *    (ENABLE flag set)
1632 	 */
1633 	ihck = (dl_capab_hcksum_t *)(isub + 1);
1634 
1635 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
1636 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
1637 		    "unsupported hardware checksum "
1638 		    "sub-capability (version %d, expected %d)",
1639 		    ihck->hcksum_version, HCKSUM_VERSION_1);
1640 		return;
1641 	}
1642 
1643 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
1644 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
1645 		    "checksum capability isn't as expected; pass-thru "
1646 		    "module(s) detected, discarding capability\n"));
1647 		return;
1648 	}
1649 
1650 #define	CURR_HCKSUM_CAPAB				\
1651 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
1652 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
1653 
1654 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
1655 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
1656 		/* do ENABLE processing */
1657 		if (*ill_hcksum == NULL) {
1658 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
1659 			    KM_NOSLEEP);
1660 
1661 			if (*ill_hcksum == NULL) {
1662 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1663 				    "could not enable hcksum version %d "
1664 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
1665 				    ill->ill_name);
1666 				return;
1667 			}
1668 		}
1669 
1670 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
1671 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
1672 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
1673 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
1674 		    "has enabled hardware checksumming\n ",
1675 		    ill->ill_name));
1676 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
1677 		/*
1678 		 * Enabling hardware checksum offload
1679 		 * Currently IP supports {TCP,UDP}/IPv4
1680 		 * partial and full cksum offload and
1681 		 * IPv4 header checksum offload.
1682 		 * Allocate new mblk which will
1683 		 * contain a new capability request
1684 		 * to enable hardware checksum offload.
1685 		 */
1686 		uint_t	size;
1687 		uchar_t	*rptr;
1688 
1689 		size = sizeof (dl_capability_req_t) +
1690 		    sizeof (dl_capability_sub_t) + isub->dl_length;
1691 
1692 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1693 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1694 			    "could not enable hardware cksum for %s (ENOMEM)\n",
1695 			    ill->ill_name);
1696 			return;
1697 		}
1698 
1699 		rptr = nmp->b_rptr;
1700 		/* initialize dl_capability_req_t */
1701 		ocap = (dl_capability_req_t *)nmp->b_rptr;
1702 		ocap->dl_sub_offset =
1703 		    sizeof (dl_capability_req_t);
1704 		ocap->dl_sub_length =
1705 		    sizeof (dl_capability_sub_t) +
1706 		    isub->dl_length;
1707 		nmp->b_rptr += sizeof (dl_capability_req_t);
1708 
1709 		/* initialize dl_capability_sub_t */
1710 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
1711 		nmp->b_rptr += sizeof (*isub);
1712 
1713 		/* initialize dl_capab_hcksum_t */
1714 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
1715 		bcopy(ihck, ohck, sizeof (*ihck));
1716 
1717 		nmp->b_rptr = rptr;
1718 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
1719 
1720 		/* Set ENABLE flag */
1721 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
1722 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
1723 
1724 		/*
1725 		 * nmp points to a DL_CAPABILITY_REQ message to enable
1726 		 * hardware checksum acceleration.
1727 		 */
1728 		ill_capability_send(ill, nmp);
1729 	} else {
1730 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
1731 		    "advertised %x hardware checksum capability flags\n",
1732 		    ill->ill_name, ihck->hcksum_txflags));
1733 	}
1734 }
1735 
1736 static void
ill_capability_hcksum_reset_fill(ill_t * ill,mblk_t * mp)1737 ill_capability_hcksum_reset_fill(ill_t *ill, mblk_t *mp)
1738 {
1739 	dl_capab_hcksum_t *hck_subcap;
1740 	dl_capability_sub_t *dl_subcap;
1741 
1742 	if (!ILL_HCKSUM_CAPABLE(ill))
1743 		return;
1744 
1745 	ASSERT(ill->ill_hcksum_capab != NULL);
1746 
1747 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1748 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
1749 	dl_subcap->dl_length = sizeof (*hck_subcap);
1750 
1751 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
1752 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
1753 	hck_subcap->hcksum_txflags = 0;
1754 
1755 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*hck_subcap);
1756 }
1757 
1758 static void
ill_capability_zerocopy_ack(ill_t * ill,mblk_t * mp,dl_capability_sub_t * isub)1759 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1760 {
1761 	mblk_t *nmp = NULL;
1762 	dl_capability_req_t *oc;
1763 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
1764 	ill_zerocopy_capab_t **ill_zerocopy_capab;
1765 	uint_t sub_dl_cap = isub->dl_cap;
1766 	uint8_t *capend;
1767 
1768 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
1769 
1770 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
1771 
1772 	/*
1773 	 * Note: range checks here are not absolutely sufficient to
1774 	 * make us robust against malformed messages sent by drivers;
1775 	 * this is in keeping with the rest of IP's dlpi handling.
1776 	 * (Remember, it's coming from something else in the kernel
1777 	 * address space)
1778 	 */
1779 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1780 	if (capend > mp->b_wptr) {
1781 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1782 		    "malformed sub-capability too long for mblk");
1783 		return;
1784 	}
1785 
1786 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
1787 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
1788 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
1789 		    "unsupported ZEROCOPY sub-capability (version %d, "
1790 		    "expected %d)", zc_ic->zerocopy_version,
1791 		    ZEROCOPY_VERSION_1);
1792 		return;
1793 	}
1794 
1795 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
1796 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
1797 		    "capability isn't as expected; pass-thru module(s) "
1798 		    "detected, discarding capability\n"));
1799 		return;
1800 	}
1801 
1802 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
1803 		if (*ill_zerocopy_capab == NULL) {
1804 			*ill_zerocopy_capab =
1805 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
1806 			    KM_NOSLEEP);
1807 
1808 			if (*ill_zerocopy_capab == NULL) {
1809 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1810 				    "could not enable Zero-copy version %d "
1811 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
1812 				    ill->ill_name);
1813 				return;
1814 			}
1815 		}
1816 
1817 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
1818 		    "supports Zero-copy version %d\n", ill->ill_name,
1819 		    ZEROCOPY_VERSION_1));
1820 
1821 		(*ill_zerocopy_capab)->ill_zerocopy_version =
1822 		    zc_ic->zerocopy_version;
1823 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
1824 		    zc_ic->zerocopy_flags;
1825 
1826 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
1827 	} else {
1828 		uint_t size;
1829 		uchar_t *rptr;
1830 
1831 		size = sizeof (dl_capability_req_t) +
1832 		    sizeof (dl_capability_sub_t) +
1833 		    sizeof (dl_capab_zerocopy_t);
1834 
1835 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1836 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1837 			    "could not enable zerocopy for %s (ENOMEM)\n",
1838 			    ill->ill_name);
1839 			return;
1840 		}
1841 
1842 		rptr = nmp->b_rptr;
1843 		/* initialize dl_capability_req_t */
1844 		oc = (dl_capability_req_t *)rptr;
1845 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1846 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1847 		    sizeof (dl_capab_zerocopy_t);
1848 		rptr += sizeof (dl_capability_req_t);
1849 
1850 		/* initialize dl_capability_sub_t */
1851 		bcopy(isub, rptr, sizeof (*isub));
1852 		rptr += sizeof (*isub);
1853 
1854 		/* initialize dl_capab_zerocopy_t */
1855 		zc_oc = (dl_capab_zerocopy_t *)rptr;
1856 		*zc_oc = *zc_ic;
1857 
1858 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
1859 		    "to enable zero-copy version %d\n", ill->ill_name,
1860 		    ZEROCOPY_VERSION_1));
1861 
1862 		/* set VMSAFE_MEM flag */
1863 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
1864 
1865 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
1866 		ill_capability_send(ill, nmp);
1867 	}
1868 }
1869 
1870 static void
ill_capability_zerocopy_reset_fill(ill_t * ill,mblk_t * mp)1871 ill_capability_zerocopy_reset_fill(ill_t *ill, mblk_t *mp)
1872 {
1873 	dl_capab_zerocopy_t *zerocopy_subcap;
1874 	dl_capability_sub_t *dl_subcap;
1875 
1876 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
1877 		return;
1878 
1879 	ASSERT(ill->ill_zerocopy_capab != NULL);
1880 
1881 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1882 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
1883 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
1884 
1885 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
1886 	zerocopy_subcap->zerocopy_version =
1887 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
1888 	zerocopy_subcap->zerocopy_flags = 0;
1889 
1890 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
1891 }
1892 
1893 /*
1894  * DLD capability
1895  * Refer to dld.h for more information regarding the purpose and usage
1896  * of this capability.
1897  */
1898 static void
ill_capability_dld_ack(ill_t * ill,mblk_t * mp,dl_capability_sub_t * isub)1899 ill_capability_dld_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1900 {
1901 	dl_capab_dld_t		*dld_ic, dld;
1902 	uint_t			sub_dl_cap = isub->dl_cap;
1903 	uint8_t			*capend;
1904 	ill_dld_capab_t		*idc;
1905 
1906 	ASSERT(IAM_WRITER_ILL(ill));
1907 	ASSERT(sub_dl_cap == DL_CAPAB_DLD);
1908 
1909 	/*
1910 	 * Note: range checks here are not absolutely sufficient to
1911 	 * make us robust against malformed messages sent by drivers;
1912 	 * this is in keeping with the rest of IP's dlpi handling.
1913 	 * (Remember, it's coming from something else in the kernel
1914 	 * address space)
1915 	 */
1916 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1917 	if (capend > mp->b_wptr) {
1918 		cmn_err(CE_WARN, "ill_capability_dld_ack: "
1919 		    "malformed sub-capability too long for mblk");
1920 		return;
1921 	}
1922 	dld_ic = (dl_capab_dld_t *)(isub + 1);
1923 	if (dld_ic->dld_version != DLD_CURRENT_VERSION) {
1924 		cmn_err(CE_CONT, "ill_capability_dld_ack: "
1925 		    "unsupported DLD sub-capability (version %d, "
1926 		    "expected %d)", dld_ic->dld_version,
1927 		    DLD_CURRENT_VERSION);
1928 		return;
1929 	}
1930 	if (!dlcapabcheckqid(&dld_ic->dld_mid, ill->ill_lmod_rq)) {
1931 		ip1dbg(("ill_capability_dld_ack: mid token for dld "
1932 		    "capability isn't as expected; pass-thru module(s) "
1933 		    "detected, discarding capability\n"));
1934 		return;
1935 	}
1936 
1937 	/*
1938 	 * Copy locally to ensure alignment.
1939 	 */
1940 	bcopy(dld_ic, &dld, sizeof (dl_capab_dld_t));
1941 
1942 	if ((idc = ill->ill_dld_capab) == NULL) {
1943 		idc = kmem_zalloc(sizeof (ill_dld_capab_t), KM_NOSLEEP);
1944 		if (idc == NULL) {
1945 			cmn_err(CE_WARN, "ill_capability_dld_ack: "
1946 			    "could not enable DLD version %d "
1947 			    "for %s (ENOMEM)\n", DLD_CURRENT_VERSION,
1948 			    ill->ill_name);
1949 			return;
1950 		}
1951 		ill->ill_dld_capab = idc;
1952 	}
1953 	idc->idc_capab_df = (ip_capab_func_t)dld.dld_capab;
1954 	idc->idc_capab_dh = (void *)dld.dld_capab_handle;
1955 	ip1dbg(("ill_capability_dld_ack: interface %s "
1956 	    "supports DLD version %d\n", ill->ill_name, DLD_CURRENT_VERSION));
1957 
1958 	ill_capability_dld_enable(ill);
1959 }
1960 
1961 /*
1962  * Typically capability negotiation between IP and the driver happens via
1963  * DLPI message exchange. However GLD also offers a direct function call
1964  * mechanism to exchange the DLD_DIRECT_CAPAB and DLD_POLL_CAPAB capabilities,
1965  * But arbitrary function calls into IP or GLD are not permitted, since both
1966  * of them are protected by their own perimeter mechanism. The perimeter can
1967  * be viewed as a coarse lock or serialization mechanism. The hierarchy of
1968  * these perimeters is IP -> MAC. Thus for example to enable the squeue
1969  * polling, IP needs to enter its perimeter, then call ill_mac_perim_enter
1970  * to enter the mac perimeter and then do the direct function calls into
1971  * GLD to enable squeue polling. The ring related callbacks from the mac into
1972  * the stack to add, bind, quiesce, restart or cleanup a ring are all
1973  * protected by the mac perimeter.
1974  */
1975 static void
ill_mac_perim_enter(ill_t * ill,mac_perim_handle_t * mphp)1976 ill_mac_perim_enter(ill_t *ill, mac_perim_handle_t *mphp)
1977 {
1978 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1979 	int			err;
1980 
1981 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mphp,
1982 	    DLD_ENABLE);
1983 	ASSERT(err == 0);
1984 }
1985 
1986 static void
ill_mac_perim_exit(ill_t * ill,mac_perim_handle_t mph)1987 ill_mac_perim_exit(ill_t *ill, mac_perim_handle_t mph)
1988 {
1989 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1990 	int			err;
1991 
1992 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mph,
1993 	    DLD_DISABLE);
1994 	ASSERT(err == 0);
1995 }
1996 
1997 boolean_t
ill_mac_perim_held(ill_t * ill)1998 ill_mac_perim_held(ill_t *ill)
1999 {
2000 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
2001 
2002 	return (idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, NULL,
2003 	    DLD_QUERY));
2004 }
2005 
2006 static void
ill_capability_direct_enable(ill_t * ill)2007 ill_capability_direct_enable(ill_t *ill)
2008 {
2009 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
2010 	ill_dld_direct_t	*idd = &idc->idc_direct;
2011 	dld_capab_direct_t	direct;
2012 	int			rc;
2013 
2014 	ASSERT(IAM_WRITER_ILL(ill));
2015 
2016 	bzero(&direct, sizeof (direct));
2017 	if (ill->ill_isv6) {
2018 		direct.di_rx_cf = (uintptr_t)ip_input_v6;
2019 	} else {
2020 		direct.di_rx_cf = (uintptr_t)ip_input;
2021 	}
2022 	direct.di_rx_ch = ill;
2023 
2024 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT, &direct,
2025 	    DLD_ENABLE);
2026 	if (rc == 0) {
2027 		idd->idd_tx_df = (ip_dld_tx_t)direct.di_tx_df;
2028 		idd->idd_tx_dh = direct.di_tx_dh;
2029 		idd->idd_tx_cb_df = (ip_dld_callb_t)direct.di_tx_cb_df;
2030 		idd->idd_tx_cb_dh = direct.di_tx_cb_dh;
2031 		idd->idd_tx_fctl_df = (ip_dld_fctl_t)direct.di_tx_fctl_df;
2032 		idd->idd_tx_fctl_dh = direct.di_tx_fctl_dh;
2033 		ASSERT(idd->idd_tx_cb_df != NULL);
2034 		ASSERT(idd->idd_tx_fctl_df != NULL);
2035 		ASSERT(idd->idd_tx_df != NULL);
2036 		/*
2037 		 * One time registration of flow enable callback function
2038 		 */
2039 		ill->ill_flownotify_mh = idd->idd_tx_cb_df(idd->idd_tx_cb_dh,
2040 		    ill_flow_enable, ill);
2041 		ill->ill_capabilities |= ILL_CAPAB_DLD_DIRECT;
2042 		DTRACE_PROBE1(direct_on, (ill_t *), ill);
2043 	} else {
2044 		cmn_err(CE_WARN, "warning: could not enable DIRECT "
2045 		    "capability, rc = %d\n", rc);
2046 		DTRACE_PROBE2(direct_off, (ill_t *), ill, (int), rc);
2047 	}
2048 }
2049 
2050 static void
ill_capability_poll_enable(ill_t * ill)2051 ill_capability_poll_enable(ill_t *ill)
2052 {
2053 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
2054 	dld_capab_poll_t	poll;
2055 	int			rc;
2056 
2057 	ASSERT(IAM_WRITER_ILL(ill));
2058 
2059 	bzero(&poll, sizeof (poll));
2060 	poll.poll_ring_add_cf = (uintptr_t)ip_squeue_add_ring;
2061 	poll.poll_ring_remove_cf = (uintptr_t)ip_squeue_clean_ring;
2062 	poll.poll_ring_quiesce_cf = (uintptr_t)ip_squeue_quiesce_ring;
2063 	poll.poll_ring_restart_cf = (uintptr_t)ip_squeue_restart_ring;
2064 	poll.poll_ring_bind_cf = (uintptr_t)ip_squeue_bind_ring;
2065 	poll.poll_ring_ch = ill;
2066 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL, &poll,
2067 	    DLD_ENABLE);
2068 	if (rc == 0) {
2069 		ill->ill_capabilities |= ILL_CAPAB_DLD_POLL;
2070 		DTRACE_PROBE1(poll_on, (ill_t *), ill);
2071 	} else {
2072 		ip1dbg(("warning: could not enable POLL "
2073 		    "capability, rc = %d\n", rc));
2074 		DTRACE_PROBE2(poll_off, (ill_t *), ill, (int), rc);
2075 	}
2076 }
2077 
2078 /*
2079  * Enable the LSO capability.
2080  */
2081 static void
ill_capability_lso_enable(ill_t * ill)2082 ill_capability_lso_enable(ill_t *ill)
2083 {
2084 	ill_dld_capab_t	*idc = ill->ill_dld_capab;
2085 	dld_capab_lso_t	lso;
2086 	int rc;
2087 
2088 	ASSERT(IAM_WRITER_ILL(ill));
2089 
2090 	if (ill->ill_lso_capab == NULL) {
2091 		ill->ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
2092 		    KM_NOSLEEP);
2093 		if (ill->ill_lso_capab == NULL) {
2094 			cmn_err(CE_WARN, "ill_capability_lso_enable: "
2095 			    "could not enable LSO for %s (ENOMEM)\n",
2096 			    ill->ill_name);
2097 			return;
2098 		}
2099 	}
2100 
2101 	bzero(&lso, sizeof (lso));
2102 	if ((rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO, &lso,
2103 	    DLD_ENABLE)) == 0) {
2104 		ill->ill_lso_capab->ill_lso_flags = lso.lso_flags;
2105 		ill->ill_lso_capab->ill_lso_max_tcpv4 = lso.lso_max_tcpv4;
2106 		ill->ill_lso_capab->ill_lso_max_tcpv6 = lso.lso_max_tcpv6;
2107 		ill->ill_capabilities |= ILL_CAPAB_LSO;
2108 		ip1dbg(("ill_capability_lso_enable: interface %s "
2109 		    "has enabled LSO\n ", ill->ill_name));
2110 	} else {
2111 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
2112 		ill->ill_lso_capab = NULL;
2113 		DTRACE_PROBE2(lso_off, (ill_t *), ill, (int), rc);
2114 	}
2115 }
2116 
2117 static void
ill_capability_dld_enable(ill_t * ill)2118 ill_capability_dld_enable(ill_t *ill)
2119 {
2120 	mac_perim_handle_t mph;
2121 
2122 	ASSERT(IAM_WRITER_ILL(ill));
2123 
2124 	ill_mac_perim_enter(ill, &mph);
2125 	ill_capability_direct_enable(ill);
2126 	ill_capability_poll_enable(ill);
2127 	ill_capability_lso_enable(ill);
2128 	ill->ill_capabilities |= ILL_CAPAB_DLD;
2129 	ill_mac_perim_exit(ill, mph);
2130 }
2131 
2132 static void
ill_capability_dld_disable(ill_t * ill)2133 ill_capability_dld_disable(ill_t *ill)
2134 {
2135 	ill_dld_capab_t	*idc;
2136 	ill_dld_direct_t *idd;
2137 	mac_perim_handle_t	mph;
2138 
2139 	ASSERT(IAM_WRITER_ILL(ill));
2140 
2141 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
2142 		return;
2143 
2144 	ill_mac_perim_enter(ill, &mph);
2145 
2146 	idc = ill->ill_dld_capab;
2147 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT) != 0) {
2148 		/*
2149 		 * For performance we avoid locks in the transmit data path
2150 		 * and don't maintain a count of the number of threads using
2151 		 * direct calls. Thus some threads could be using direct
2152 		 * transmit calls to GLD, even after the capability mechanism
2153 		 * turns it off. This is still safe since the handles used in
2154 		 * the direct calls continue to be valid until the unplumb is
2155 		 * completed. Remove the callback that was added (1-time) at
2156 		 * capab enable time.
2157 		 */
2158 		mutex_enter(&ill->ill_lock);
2159 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_DIRECT;
2160 		mutex_exit(&ill->ill_lock);
2161 		if (ill->ill_flownotify_mh != NULL) {
2162 			idd = &idc->idc_direct;
2163 			idd->idd_tx_cb_df(idd->idd_tx_cb_dh, NULL,
2164 			    ill->ill_flownotify_mh);
2165 			ill->ill_flownotify_mh = NULL;
2166 		}
2167 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT,
2168 		    NULL, DLD_DISABLE);
2169 	}
2170 
2171 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_POLL) != 0) {
2172 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_POLL;
2173 		ip_squeue_clean_all(ill);
2174 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL,
2175 		    NULL, DLD_DISABLE);
2176 	}
2177 
2178 	if ((ill->ill_capabilities & ILL_CAPAB_LSO) != 0) {
2179 		ASSERT(ill->ill_lso_capab != NULL);
2180 		/*
2181 		 * Clear the capability flag for LSO but retain the
2182 		 * ill_lso_capab structure since it's possible that another
2183 		 * thread is still referring to it.  The structure only gets
2184 		 * deallocated when we destroy the ill.
2185 		 */
2186 
2187 		ill->ill_capabilities &= ~ILL_CAPAB_LSO;
2188 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO,
2189 		    NULL, DLD_DISABLE);
2190 	}
2191 
2192 	ill->ill_capabilities &= ~ILL_CAPAB_DLD;
2193 	ill_mac_perim_exit(ill, mph);
2194 }
2195 
2196 /*
2197  * Capability Negotiation protocol
2198  *
2199  * We don't wait for DLPI capability operations to finish during interface
2200  * bringup or teardown. Doing so would introduce more asynchrony and the
2201  * interface up/down operations will need multiple return and restarts.
2202  * Instead the 'ipsq_current_ipif' of the ipsq is not cleared as long as
2203  * the 'ill_dlpi_deferred' chain is non-empty. This ensures that the next
2204  * exclusive operation won't start until the DLPI operations of the previous
2205  * exclusive operation complete.
2206  *
2207  * The capability state machine is shown below.
2208  *
2209  * state		next state		event, action
2210  *
2211  * IDCS_UNKNOWN		IDCS_PROBE_SENT		ill_capability_probe
2212  * IDCS_PROBE_SENT	IDCS_OK			ill_capability_ack
2213  * IDCS_PROBE_SENT	IDCS_FAILED		ip_rput_dlpi_writer (nack)
2214  * IDCS_OK		IDCS_RENEG		Receipt of DL_NOTE_CAPAB_RENEG
2215  * IDCS_OK		IDCS_RESET_SENT		ill_capability_reset
2216  * IDCS_RESET_SENT	IDCS_UNKNOWN		ill_capability_ack_thr
2217  * IDCS_RENEG		IDCS_PROBE_SENT		ill_capability_ack_thr ->
2218  *						    ill_capability_probe.
2219  */
2220 
2221 /*
2222  * Dedicated thread started from ip_stack_init that handles capability
2223  * disable. This thread ensures the taskq dispatch does not fail by waiting
2224  * for resources using TQ_SLEEP. The taskq mechanism is used to ensure
2225  * that direct calls to DLD are done in a cv_waitable context.
2226  */
2227 void
ill_taskq_dispatch(ip_stack_t * ipst)2228 ill_taskq_dispatch(ip_stack_t *ipst)
2229 {
2230 	callb_cpr_t cprinfo;
2231 	char	name[64];
2232 	mblk_t	*mp;
2233 
2234 	(void) snprintf(name, sizeof (name), "ill_taskq_dispatch_%d",
2235 	    ipst->ips_netstack->netstack_stackid);
2236 	CALLB_CPR_INIT(&cprinfo, &ipst->ips_capab_taskq_lock, callb_generic_cpr,
2237 	    name);
2238 	mutex_enter(&ipst->ips_capab_taskq_lock);
2239 
2240 	for (;;) {
2241 		mp = ipst->ips_capab_taskq_head;
2242 		while (mp != NULL) {
2243 			ipst->ips_capab_taskq_head = mp->b_next;
2244 			if (ipst->ips_capab_taskq_head == NULL)
2245 				ipst->ips_capab_taskq_tail = NULL;
2246 			mutex_exit(&ipst->ips_capab_taskq_lock);
2247 			mp->b_next = NULL;
2248 
2249 			VERIFY(taskq_dispatch(system_taskq,
2250 			    ill_capability_ack_thr, mp, TQ_SLEEP) !=
2251 			    TASKQID_INVALID);
2252 			mutex_enter(&ipst->ips_capab_taskq_lock);
2253 			mp = ipst->ips_capab_taskq_head;
2254 		}
2255 
2256 		if (ipst->ips_capab_taskq_quit)
2257 			break;
2258 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
2259 		cv_wait(&ipst->ips_capab_taskq_cv, &ipst->ips_capab_taskq_lock);
2260 		CALLB_CPR_SAFE_END(&cprinfo, &ipst->ips_capab_taskq_lock);
2261 	}
2262 	VERIFY(ipst->ips_capab_taskq_head == NULL);
2263 	VERIFY(ipst->ips_capab_taskq_tail == NULL);
2264 	CALLB_CPR_EXIT(&cprinfo);
2265 	thread_exit();
2266 }
2267 
2268 /*
2269  * Consume a new-style hardware capabilities negotiation ack.
2270  * Called via taskq on receipt of DL_CAPABILITY_ACK.
2271  */
2272 static void
ill_capability_ack_thr(void * arg)2273 ill_capability_ack_thr(void *arg)
2274 {
2275 	mblk_t	*mp = arg;
2276 	dl_capability_ack_t *capp;
2277 	dl_capability_sub_t *subp, *endp;
2278 	ill_t	*ill;
2279 	boolean_t reneg;
2280 
2281 	ill = (ill_t *)mp->b_prev;
2282 	mp->b_prev = NULL;
2283 
2284 	VERIFY(ipsq_enter(ill, B_FALSE, CUR_OP) == B_TRUE);
2285 
2286 	if (ill->ill_dlpi_capab_state == IDCS_RESET_SENT ||
2287 	    ill->ill_dlpi_capab_state == IDCS_RENEG) {
2288 		/*
2289 		 * We have received the ack for our DL_CAPAB reset request.
2290 		 * There isnt' anything in the message that needs processing.
2291 		 * All message based capabilities have been disabled, now
2292 		 * do the function call based capability disable.
2293 		 */
2294 		reneg = ill->ill_dlpi_capab_state == IDCS_RENEG;
2295 		ill_capability_dld_disable(ill);
2296 		ill->ill_dlpi_capab_state = IDCS_UNKNOWN;
2297 		if (reneg)
2298 			ill_capability_probe(ill);
2299 		goto done;
2300 	}
2301 
2302 	if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
2303 		ill->ill_dlpi_capab_state = IDCS_OK;
2304 
2305 	capp = (dl_capability_ack_t *)mp->b_rptr;
2306 
2307 	if (capp->dl_sub_length == 0) {
2308 		/* no new-style capabilities */
2309 		goto done;
2310 	}
2311 
2312 	/* make sure the driver supplied correct dl_sub_length */
2313 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
2314 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
2315 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
2316 		goto done;
2317 	}
2318 
2319 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
2320 	/*
2321 	 * There are sub-capabilities. Process the ones we know about.
2322 	 * Loop until we don't have room for another sub-cap header..
2323 	 */
2324 	for (subp = SC(capp, capp->dl_sub_offset),
2325 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
2326 	    subp <= endp;
2327 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
2328 
2329 		switch (subp->dl_cap) {
2330 		case DL_CAPAB_ID_WRAPPER:
2331 			ill_capability_id_ack(ill, mp, subp);
2332 			break;
2333 		default:
2334 			ill_capability_dispatch(ill, mp, subp);
2335 			break;
2336 		}
2337 	}
2338 #undef SC
2339 done:
2340 	inet_freemsg(mp);
2341 	ill_capability_done(ill);
2342 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
2343 }
2344 
2345 /*
2346  * This needs to be started in a taskq thread to provide a cv_waitable
2347  * context.
2348  */
2349 void
ill_capability_ack(ill_t * ill,mblk_t * mp)2350 ill_capability_ack(ill_t *ill, mblk_t *mp)
2351 {
2352 	ip_stack_t	*ipst = ill->ill_ipst;
2353 
2354 	mp->b_prev = (mblk_t *)ill;
2355 	ASSERT(mp->b_next == NULL);
2356 
2357 	if (taskq_dispatch(system_taskq, ill_capability_ack_thr, mp,
2358 	    TQ_NOSLEEP) != TASKQID_INVALID)
2359 		return;
2360 
2361 	/*
2362 	 * The taskq dispatch failed. Signal the ill_taskq_dispatch thread
2363 	 * which will do the dispatch using TQ_SLEEP to guarantee success.
2364 	 */
2365 	mutex_enter(&ipst->ips_capab_taskq_lock);
2366 	if (ipst->ips_capab_taskq_head == NULL) {
2367 		ASSERT(ipst->ips_capab_taskq_tail == NULL);
2368 		ipst->ips_capab_taskq_head = mp;
2369 	} else {
2370 		ipst->ips_capab_taskq_tail->b_next = mp;
2371 	}
2372 	ipst->ips_capab_taskq_tail = mp;
2373 
2374 	cv_signal(&ipst->ips_capab_taskq_cv);
2375 	mutex_exit(&ipst->ips_capab_taskq_lock);
2376 }
2377 
2378 /*
2379  * This routine is called to scan the fragmentation reassembly table for
2380  * the specified ILL for any packets that are starting to smell.
2381  * dead_interval is the maximum time in seconds that will be tolerated.  It
2382  * will either be the value specified in ip_g_frag_timeout, or zero if the
2383  * ILL is shutting down and it is time to blow everything off.
2384  *
2385  * It returns the number of seconds (as a time_t) that the next frag timer
2386  * should be scheduled for, 0 meaning that the timer doesn't need to be
2387  * re-started.  Note that the method of calculating next_timeout isn't
2388  * entirely accurate since time will flow between the time we grab
2389  * current_time and the time we schedule the next timeout.  This isn't a
2390  * big problem since this is the timer for sending an ICMP reassembly time
2391  * exceeded messages, and it doesn't have to be exactly accurate.
2392  *
2393  * This function is
2394  * sometimes called as writer, although this is not required.
2395  */
2396 time_t
ill_frag_timeout(ill_t * ill,time_t dead_interval)2397 ill_frag_timeout(ill_t *ill, time_t dead_interval)
2398 {
2399 	ipfb_t	*ipfb;
2400 	ipfb_t	*endp;
2401 	ipf_t	*ipf;
2402 	ipf_t	*ipfnext;
2403 	mblk_t	*mp;
2404 	time_t	current_time = gethrestime_sec();
2405 	time_t	next_timeout = 0;
2406 	uint32_t	hdr_length;
2407 	mblk_t	*send_icmp_head;
2408 	mblk_t	*send_icmp_head_v6;
2409 	ip_stack_t *ipst = ill->ill_ipst;
2410 	ip_recv_attr_t iras;
2411 
2412 	bzero(&iras, sizeof (iras));
2413 	iras.ira_flags = 0;
2414 	iras.ira_ill = iras.ira_rill = ill;
2415 	iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
2416 	iras.ira_rifindex = iras.ira_ruifindex;
2417 
2418 	ipfb = ill->ill_frag_hash_tbl;
2419 	if (ipfb == NULL)
2420 		return (B_FALSE);
2421 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
2422 	/* Walk the frag hash table. */
2423 	for (; ipfb < endp; ipfb++) {
2424 		send_icmp_head = NULL;
2425 		send_icmp_head_v6 = NULL;
2426 		mutex_enter(&ipfb->ipfb_lock);
2427 		while ((ipf = ipfb->ipfb_ipf) != 0) {
2428 			time_t frag_time = current_time - ipf->ipf_timestamp;
2429 			time_t frag_timeout;
2430 
2431 			if (frag_time < dead_interval) {
2432 				/*
2433 				 * There are some outstanding fragments
2434 				 * that will timeout later.  Make note of
2435 				 * the time so that we can reschedule the
2436 				 * next timeout appropriately.
2437 				 */
2438 				frag_timeout = dead_interval - frag_time;
2439 				if (next_timeout == 0 ||
2440 				    frag_timeout < next_timeout) {
2441 					next_timeout = frag_timeout;
2442 				}
2443 				break;
2444 			}
2445 			/* Time's up.  Get it out of here. */
2446 			hdr_length = ipf->ipf_nf_hdr_len;
2447 			ipfnext = ipf->ipf_hash_next;
2448 			if (ipfnext)
2449 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
2450 			*ipf->ipf_ptphn = ipfnext;
2451 			mp = ipf->ipf_mp->b_cont;
2452 			for (; mp; mp = mp->b_cont) {
2453 				/* Extra points for neatness. */
2454 				IP_REASS_SET_START(mp, 0);
2455 				IP_REASS_SET_END(mp, 0);
2456 			}
2457 			mp = ipf->ipf_mp->b_cont;
2458 			atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
2459 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
2460 			ipfb->ipfb_count -= ipf->ipf_count;
2461 			ASSERT(ipfb->ipfb_frag_pkts > 0);
2462 			ipfb->ipfb_frag_pkts--;
2463 			/*
2464 			 * We do not send any icmp message from here because
2465 			 * we currently are holding the ipfb_lock for this
2466 			 * hash chain. If we try and send any icmp messages
2467 			 * from here we may end up via a put back into ip
2468 			 * trying to get the same lock, causing a recursive
2469 			 * mutex panic. Instead we build a list and send all
2470 			 * the icmp messages after we have dropped the lock.
2471 			 */
2472 			if (ill->ill_isv6) {
2473 				if (hdr_length != 0) {
2474 					mp->b_next = send_icmp_head_v6;
2475 					send_icmp_head_v6 = mp;
2476 				} else {
2477 					freemsg(mp);
2478 				}
2479 			} else {
2480 				if (hdr_length != 0) {
2481 					mp->b_next = send_icmp_head;
2482 					send_icmp_head = mp;
2483 				} else {
2484 					freemsg(mp);
2485 				}
2486 			}
2487 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2488 			ip_drop_input("ipIfStatsReasmFails", ipf->ipf_mp, ill);
2489 			freeb(ipf->ipf_mp);
2490 		}
2491 		mutex_exit(&ipfb->ipfb_lock);
2492 		/*
2493 		 * Now need to send any icmp messages that we delayed from
2494 		 * above.
2495 		 */
2496 		while (send_icmp_head_v6 != NULL) {
2497 			ip6_t *ip6h;
2498 
2499 			mp = send_icmp_head_v6;
2500 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
2501 			mp->b_next = NULL;
2502 			ip6h = (ip6_t *)mp->b_rptr;
2503 			iras.ira_flags = 0;
2504 			/*
2505 			 * This will result in an incorrect ALL_ZONES zoneid
2506 			 * for multicast packets, but we
2507 			 * don't send ICMP errors for those in any case.
2508 			 */
2509 			iras.ira_zoneid =
2510 			    ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
2511 			    ill, ipst);
2512 			ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2513 			icmp_time_exceeded_v6(mp,
2514 			    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
2515 			    &iras);
2516 			ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2517 		}
2518 		while (send_icmp_head != NULL) {
2519 			ipaddr_t dst;
2520 
2521 			mp = send_icmp_head;
2522 			send_icmp_head = send_icmp_head->b_next;
2523 			mp->b_next = NULL;
2524 
2525 			dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
2526 
2527 			iras.ira_flags = IRAF_IS_IPV4;
2528 			/*
2529 			 * This will result in an incorrect ALL_ZONES zoneid
2530 			 * for broadcast and multicast packets, but we
2531 			 * don't send ICMP errors for those in any case.
2532 			 */
2533 			iras.ira_zoneid = ipif_lookup_addr_zoneid(dst,
2534 			    ill, ipst);
2535 			ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2536 			icmp_time_exceeded(mp,
2537 			    ICMP_REASSEMBLY_TIME_EXCEEDED, &iras);
2538 			ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2539 		}
2540 	}
2541 	/*
2542 	 * A non-dying ILL will use the return value to decide whether to
2543 	 * restart the frag timer, and for how long.
2544 	 */
2545 	return (next_timeout);
2546 }
2547 
2548 /*
2549  * This routine is called when the approximate count of mblk memory used
2550  * for the specified ILL has exceeded max_count.
2551  */
2552 void
ill_frag_prune(ill_t * ill,uint_t max_count)2553 ill_frag_prune(ill_t *ill, uint_t max_count)
2554 {
2555 	ipfb_t	*ipfb;
2556 	ipf_t	*ipf;
2557 	size_t	count;
2558 	clock_t now;
2559 
2560 	/*
2561 	 * If we are here within ip_min_frag_prune_time msecs remove
2562 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
2563 	 * ill_frag_free_num_pkts.
2564 	 */
2565 	mutex_enter(&ill->ill_lock);
2566 	now = ddi_get_lbolt();
2567 	if (TICK_TO_MSEC(now - ill->ill_last_frag_clean_time) <=
2568 	    (ip_min_frag_prune_time != 0 ?
2569 	    ip_min_frag_prune_time : msec_per_tick)) {
2570 
2571 		ill->ill_frag_free_num_pkts++;
2572 
2573 	} else {
2574 		ill->ill_frag_free_num_pkts = 0;
2575 	}
2576 	ill->ill_last_frag_clean_time = now;
2577 	mutex_exit(&ill->ill_lock);
2578 
2579 	/*
2580 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
2581 	 */
2582 	if (ill->ill_frag_free_num_pkts != 0) {
2583 		int ix;
2584 
2585 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2586 			ipfb = &ill->ill_frag_hash_tbl[ix];
2587 			mutex_enter(&ipfb->ipfb_lock);
2588 			if (ipfb->ipfb_ipf != NULL) {
2589 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
2590 				    ill->ill_frag_free_num_pkts);
2591 			}
2592 			mutex_exit(&ipfb->ipfb_lock);
2593 		}
2594 	}
2595 	/*
2596 	 * While the reassembly list for this ILL is too big, prune a fragment
2597 	 * queue by age, oldest first.
2598 	 */
2599 	while (ill->ill_frag_count > max_count) {
2600 		int	ix;
2601 		ipfb_t	*oipfb = NULL;
2602 		uint_t	oldest = UINT_MAX;
2603 
2604 		count = 0;
2605 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2606 			ipfb = &ill->ill_frag_hash_tbl[ix];
2607 			mutex_enter(&ipfb->ipfb_lock);
2608 			ipf = ipfb->ipfb_ipf;
2609 			if (ipf != NULL && ipf->ipf_gen < oldest) {
2610 				oldest = ipf->ipf_gen;
2611 				oipfb = ipfb;
2612 			}
2613 			count += ipfb->ipfb_count;
2614 			mutex_exit(&ipfb->ipfb_lock);
2615 		}
2616 		if (oipfb == NULL)
2617 			break;
2618 
2619 		if (count <= max_count)
2620 			return;	/* Somebody beat us to it, nothing to do */
2621 		mutex_enter(&oipfb->ipfb_lock);
2622 		ipf = oipfb->ipfb_ipf;
2623 		if (ipf != NULL) {
2624 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
2625 		}
2626 		mutex_exit(&oipfb->ipfb_lock);
2627 	}
2628 }
2629 
2630 /*
2631  * free 'free_cnt' fragmented packets starting at ipf.
2632  */
2633 void
ill_frag_free_pkts(ill_t * ill,ipfb_t * ipfb,ipf_t * ipf,int free_cnt)2634 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
2635 {
2636 	size_t	count;
2637 	mblk_t	*mp;
2638 	mblk_t	*tmp;
2639 	ipf_t **ipfp = ipf->ipf_ptphn;
2640 
2641 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
2642 	ASSERT(ipfp != NULL);
2643 	ASSERT(ipf != NULL);
2644 
2645 	while (ipf != NULL && free_cnt-- > 0) {
2646 		count = ipf->ipf_count;
2647 		mp = ipf->ipf_mp;
2648 		ipf = ipf->ipf_hash_next;
2649 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
2650 			IP_REASS_SET_START(tmp, 0);
2651 			IP_REASS_SET_END(tmp, 0);
2652 		}
2653 		atomic_add_32(&ill->ill_frag_count, -count);
2654 		ASSERT(ipfb->ipfb_count >= count);
2655 		ipfb->ipfb_count -= count;
2656 		ASSERT(ipfb->ipfb_frag_pkts > 0);
2657 		ipfb->ipfb_frag_pkts--;
2658 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2659 		ip_drop_input("ipIfStatsReasmFails", mp, ill);
2660 		freemsg(mp);
2661 	}
2662 
2663 	if (ipf)
2664 		ipf->ipf_ptphn = ipfp;
2665 	ipfp[0] = ipf;
2666 }
2667 
2668 /*
2669  * Helper function for ill_forward_set().
2670  */
2671 static void
ill_forward_set_on_ill(ill_t * ill,boolean_t enable)2672 ill_forward_set_on_ill(ill_t *ill, boolean_t enable)
2673 {
2674 	ip_stack_t	*ipst = ill->ill_ipst;
2675 
2676 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2677 
2678 	ip1dbg(("ill_forward_set: %s %s forwarding on %s",
2679 	    (enable ? "Enabling" : "Disabling"),
2680 	    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
2681 	mutex_enter(&ill->ill_lock);
2682 	if (enable)
2683 		ill->ill_flags |= ILLF_ROUTER;
2684 	else
2685 		ill->ill_flags &= ~ILLF_ROUTER;
2686 	mutex_exit(&ill->ill_lock);
2687 	if (ill->ill_isv6)
2688 		ill_set_nce_router_flags(ill, enable);
2689 	/* Notify routing socket listeners of this change. */
2690 	if (ill->ill_ipif != NULL)
2691 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
2692 }
2693 
2694 /*
2695  * Set an ill's ILLF_ROUTER flag appropriately.  Send up RTS_IFINFO routing
2696  * socket messages for each interface whose flags we change.
2697  */
2698 int
ill_forward_set(ill_t * ill,boolean_t enable)2699 ill_forward_set(ill_t *ill, boolean_t enable)
2700 {
2701 	ipmp_illgrp_t *illg;
2702 	ip_stack_t *ipst = ill->ill_ipst;
2703 
2704 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2705 
2706 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
2707 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
2708 		return (0);
2709 
2710 	if (IS_LOOPBACK(ill))
2711 		return (EINVAL);
2712 
2713 	if (enable && ill->ill_allowed_ips_cnt > 0)
2714 		return (EPERM);
2715 
2716 	if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
2717 		/*
2718 		 * Update all of the interfaces in the group.
2719 		 */
2720 		illg = ill->ill_grp;
2721 		ill = list_head(&illg->ig_if);
2722 		for (; ill != NULL; ill = list_next(&illg->ig_if, ill))
2723 			ill_forward_set_on_ill(ill, enable);
2724 
2725 		/*
2726 		 * Update the IPMP meta-interface.
2727 		 */
2728 		ill_forward_set_on_ill(ipmp_illgrp_ipmp_ill(illg), enable);
2729 		return (0);
2730 	}
2731 
2732 	ill_forward_set_on_ill(ill, enable);
2733 	return (0);
2734 }
2735 
2736 /*
2737  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
2738  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
2739  * set or clear.
2740  */
2741 static void
ill_set_nce_router_flags(ill_t * ill,boolean_t enable)2742 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
2743 {
2744 	ipif_t *ipif;
2745 	ncec_t *ncec;
2746 	nce_t *nce;
2747 
2748 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
2749 		/*
2750 		 * NOTE: we match across the illgrp because nce's for
2751 		 * addresses on IPMP interfaces have an nce_ill that points to
2752 		 * the bound underlying ill.
2753 		 */
2754 		nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
2755 		if (nce != NULL) {
2756 			ncec = nce->nce_common;
2757 			mutex_enter(&ncec->ncec_lock);
2758 			if (enable)
2759 				ncec->ncec_flags |= NCE_F_ISROUTER;
2760 			else
2761 				ncec->ncec_flags &= ~NCE_F_ISROUTER;
2762 			mutex_exit(&ncec->ncec_lock);
2763 			nce_refrele(nce);
2764 		}
2765 	}
2766 }
2767 
2768 /*
2769  * Intializes the context structure and returns the first ill in the list
2770  * cuurently start_list and end_list can have values:
2771  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
2772  * IP_V4_G_HEAD		Traverse IPV4 list only.
2773  * IP_V6_G_HEAD		Traverse IPV6 list only.
2774  */
2775 
2776 /*
2777  * We don't check for CONDEMNED ills here. Caller must do that if
2778  * necessary under the ill lock.
2779  */
2780 ill_t *
ill_first(int start_list,int end_list,ill_walk_context_t * ctx,ip_stack_t * ipst)2781 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
2782     ip_stack_t *ipst)
2783 {
2784 	ill_if_t *ifp;
2785 	ill_t *ill;
2786 	avl_tree_t *avl_tree;
2787 
2788 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
2789 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
2790 
2791 	/*
2792 	 * setup the lists to search
2793 	 */
2794 	if (end_list != MAX_G_HEADS) {
2795 		ctx->ctx_current_list = start_list;
2796 		ctx->ctx_last_list = end_list;
2797 	} else {
2798 		ctx->ctx_last_list = MAX_G_HEADS - 1;
2799 		ctx->ctx_current_list = 0;
2800 	}
2801 
2802 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
2803 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2804 		if (ifp != (ill_if_t *)
2805 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2806 			avl_tree = &ifp->illif_avl_by_ppa;
2807 			ill = avl_first(avl_tree);
2808 			/*
2809 			 * ill is guaranteed to be non NULL or ifp should have
2810 			 * not existed.
2811 			 */
2812 			ASSERT(ill != NULL);
2813 			return (ill);
2814 		}
2815 		ctx->ctx_current_list++;
2816 	}
2817 
2818 	return (NULL);
2819 }
2820 
2821 /*
2822  * returns the next ill in the list. ill_first() must have been called
2823  * before calling ill_next() or bad things will happen.
2824  */
2825 
2826 /*
2827  * We don't check for CONDEMNED ills here. Caller must do that if
2828  * necessary under the ill lock.
2829  */
2830 ill_t *
ill_next(ill_walk_context_t * ctx,ill_t * lastill)2831 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
2832 {
2833 	ill_if_t *ifp;
2834 	ill_t *ill;
2835 	ip_stack_t	*ipst = lastill->ill_ipst;
2836 
2837 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
2838 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
2839 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
2840 	    AVL_AFTER)) != NULL) {
2841 		return (ill);
2842 	}
2843 
2844 	/* goto next ill_ifp in the list. */
2845 	ifp = lastill->ill_ifptr->illif_next;
2846 
2847 	/* make sure not at end of circular list */
2848 	while (ifp ==
2849 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2850 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
2851 			return (NULL);
2852 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2853 	}
2854 
2855 	return (avl_first(&ifp->illif_avl_by_ppa));
2856 }
2857 
2858 /*
2859  * Check interface name for correct format: [a-zA-Z]+[a-zA-Z0-9._]*[0-9]+
2860  * The final number (PPA) must not have any leading zeros.  Upon success, a
2861  * pointer to the start of the PPA is returned; otherwise NULL is returned.
2862  */
2863 static char *
ill_get_ppa_ptr(char * name)2864 ill_get_ppa_ptr(char *name)
2865 {
2866 	int namelen = strlen(name);
2867 	int end_ndx = namelen - 1;
2868 	int ppa_ndx, i;
2869 
2870 	/*
2871 	 * Check that the first character is [a-zA-Z], and that the last
2872 	 * character is [0-9].
2873 	 */
2874 	if (namelen == 0 || !isalpha(name[0]) || !isdigit(name[end_ndx]))
2875 		return (NULL);
2876 
2877 	/*
2878 	 * Set `ppa_ndx' to the PPA start, and check for leading zeroes.
2879 	 */
2880 	for (ppa_ndx = end_ndx; ppa_ndx > 0; ppa_ndx--)
2881 		if (!isdigit(name[ppa_ndx - 1]))
2882 			break;
2883 
2884 	if (name[ppa_ndx] == '0' && ppa_ndx < end_ndx)
2885 		return (NULL);
2886 
2887 	/*
2888 	 * Check that the intermediate characters are [a-z0-9.]
2889 	 */
2890 	for (i = 1; i < ppa_ndx; i++) {
2891 		if (!isalpha(name[i]) && !isdigit(name[i]) &&
2892 		    name[i] != '.' && name[i] != '_') {
2893 			return (NULL);
2894 		}
2895 	}
2896 
2897 	return (name + ppa_ndx);
2898 }
2899 
2900 /*
2901  * use avl tree to locate the ill.
2902  */
2903 static ill_t *
ill_find_by_name(char * name,boolean_t isv6,ip_stack_t * ipst)2904 ill_find_by_name(char *name, boolean_t isv6, ip_stack_t *ipst)
2905 {
2906 	char *ppa_ptr = NULL;
2907 	int len;
2908 	uint_t ppa;
2909 	ill_t *ill = NULL;
2910 	ill_if_t *ifp;
2911 	int list;
2912 
2913 	/*
2914 	 * get ppa ptr
2915 	 */
2916 	if (isv6)
2917 		list = IP_V6_G_HEAD;
2918 	else
2919 		list = IP_V4_G_HEAD;
2920 
2921 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
2922 		return (NULL);
2923 	}
2924 
2925 	len = ppa_ptr - name + 1;
2926 
2927 	ppa = stoi(&ppa_ptr);
2928 
2929 	ifp = IP_VX_ILL_G_LIST(list, ipst);
2930 
2931 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
2932 		/*
2933 		 * match is done on len - 1 as the name is not null
2934 		 * terminated it contains ppa in addition to the interface
2935 		 * name.
2936 		 */
2937 		if ((ifp->illif_name_len == len) &&
2938 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
2939 			break;
2940 		} else {
2941 			ifp = ifp->illif_next;
2942 		}
2943 	}
2944 
2945 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
2946 		/*
2947 		 * Even the interface type does not exist.
2948 		 */
2949 		return (NULL);
2950 	}
2951 
2952 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
2953 	if (ill != NULL) {
2954 		mutex_enter(&ill->ill_lock);
2955 		if (ILL_CAN_LOOKUP(ill)) {
2956 			ill_refhold_locked(ill);
2957 			mutex_exit(&ill->ill_lock);
2958 			return (ill);
2959 		}
2960 		mutex_exit(&ill->ill_lock);
2961 	}
2962 	return (NULL);
2963 }
2964 
2965 /*
2966  * comparison function for use with avl.
2967  */
2968 static int
ill_compare_ppa(const void * ppa_ptr,const void * ill_ptr)2969 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
2970 {
2971 	uint_t ppa;
2972 	uint_t ill_ppa;
2973 
2974 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
2975 
2976 	ppa = *((uint_t *)ppa_ptr);
2977 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
2978 	/*
2979 	 * We want the ill with the lowest ppa to be on the
2980 	 * top.
2981 	 */
2982 	if (ill_ppa < ppa)
2983 		return (1);
2984 	if (ill_ppa > ppa)
2985 		return (-1);
2986 	return (0);
2987 }
2988 
2989 /*
2990  * remove an interface type from the global list.
2991  */
2992 static void
ill_delete_interface_type(ill_if_t * interface)2993 ill_delete_interface_type(ill_if_t *interface)
2994 {
2995 	ASSERT(interface != NULL);
2996 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
2997 
2998 	avl_destroy(&interface->illif_avl_by_ppa);
2999 	if (interface->illif_ppa_arena != NULL)
3000 		vmem_destroy(interface->illif_ppa_arena);
3001 
3002 	remque(interface);
3003 
3004 	mi_free(interface);
3005 }
3006 
3007 /*
3008  * remove ill from the global list.
3009  */
3010 static void
ill_glist_delete(ill_t * ill)3011 ill_glist_delete(ill_t *ill)
3012 {
3013 	ip_stack_t	*ipst;
3014 	phyint_t	*phyi;
3015 
3016 	if (ill == NULL)
3017 		return;
3018 	ipst = ill->ill_ipst;
3019 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
3020 
3021 	/*
3022 	 * If the ill was never inserted into the AVL tree
3023 	 * we skip the if branch.
3024 	 */
3025 	if (ill->ill_ifptr != NULL) {
3026 		/*
3027 		 * remove from AVL tree and free ppa number
3028 		 */
3029 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
3030 
3031 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
3032 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
3033 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3034 		}
3035 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
3036 			ill_delete_interface_type(ill->ill_ifptr);
3037 		}
3038 
3039 		/*
3040 		 * Indicate ill is no longer in the list.
3041 		 */
3042 		ill->ill_ifptr = NULL;
3043 		ill->ill_name_length = 0;
3044 		ill->ill_name[0] = '\0';
3045 		ill->ill_ppa = UINT_MAX;
3046 	}
3047 
3048 	/* Generate one last event for this ill. */
3049 	ill_nic_event_dispatch(ill, 0, NE_UNPLUMB, ill->ill_name,
3050 	    ill->ill_name_length);
3051 
3052 	ASSERT(ill->ill_phyint != NULL);
3053 	phyi = ill->ill_phyint;
3054 	ill->ill_phyint = NULL;
3055 
3056 	/*
3057 	 * ill_init allocates a phyint always to store the copy
3058 	 * of flags relevant to phyint. At that point in time, we could
3059 	 * not assign the name and hence phyint_illv4/v6 could not be
3060 	 * initialized. Later in ipif_set_values, we assign the name to
3061 	 * the ill, at which point in time we assign phyint_illv4/v6.
3062 	 * Thus we don't rely on phyint_illv6 to be initialized always.
3063 	 */
3064 	if (ill->ill_flags & ILLF_IPV6)
3065 		phyi->phyint_illv6 = NULL;
3066 	else
3067 		phyi->phyint_illv4 = NULL;
3068 
3069 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL) {
3070 		rw_exit(&ipst->ips_ill_g_lock);
3071 		return;
3072 	}
3073 
3074 	/*
3075 	 * There are no ills left on this phyint; pull it out of the phyint
3076 	 * avl trees, and free it.
3077 	 */
3078 	if (phyi->phyint_ifindex > 0) {
3079 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3080 		    phyi);
3081 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
3082 		    phyi);
3083 	}
3084 	rw_exit(&ipst->ips_ill_g_lock);
3085 
3086 	phyint_free(phyi);
3087 }
3088 
3089 /*
3090  * allocate a ppa, if the number of plumbed interfaces of this type are
3091  * less than ill_no_arena do a linear search to find a unused ppa.
3092  * When the number goes beyond ill_no_arena switch to using an arena.
3093  * Note: ppa value of zero cannot be allocated from vmem_arena as it
3094  * is the return value for an error condition, so allocation starts at one
3095  * and is decremented by one.
3096  */
3097 static int
ill_alloc_ppa(ill_if_t * ifp,ill_t * ill)3098 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
3099 {
3100 	ill_t *tmp_ill;
3101 	uint_t start, end;
3102 	int ppa;
3103 
3104 	if (ifp->illif_ppa_arena == NULL &&
3105 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
3106 		/*
3107 		 * Create an arena.
3108 		 */
3109 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
3110 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
3111 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
3112 			/* allocate what has already been assigned */
3113 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
3114 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
3115 		    tmp_ill, AVL_AFTER)) {
3116 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3117 			    1,		/* size */
3118 			    1,		/* align/quantum */
3119 			    0,		/* phase */
3120 			    0,		/* nocross */
3121 			    /* minaddr */
3122 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
3123 			    /* maxaddr */
3124 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
3125 			    VM_NOSLEEP|VM_FIRSTFIT);
3126 			if (ppa == 0) {
3127 				ip1dbg(("ill_alloc_ppa: ppa allocation"
3128 				    " failed while switching"));
3129 				vmem_destroy(ifp->illif_ppa_arena);
3130 				ifp->illif_ppa_arena = NULL;
3131 				break;
3132 			}
3133 		}
3134 	}
3135 
3136 	if (ifp->illif_ppa_arena != NULL) {
3137 		if (ill->ill_ppa == UINT_MAX) {
3138 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
3139 			    1, VM_NOSLEEP|VM_FIRSTFIT);
3140 			if (ppa == 0)
3141 				return (EAGAIN);
3142 			ill->ill_ppa = --ppa;
3143 		} else {
3144 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3145 			    1,		/* size */
3146 			    1,		/* align/quantum */
3147 			    0,		/* phase */
3148 			    0,		/* nocross */
3149 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
3150 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
3151 			    VM_NOSLEEP|VM_FIRSTFIT);
3152 			/*
3153 			 * Most likely the allocation failed because
3154 			 * the requested ppa was in use.
3155 			 */
3156 			if (ppa == 0)
3157 				return (EEXIST);
3158 		}
3159 		return (0);
3160 	}
3161 
3162 	/*
3163 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
3164 	 * been plumbed to create one. Do a linear search to get a unused ppa.
3165 	 */
3166 	if (ill->ill_ppa == UINT_MAX) {
3167 		end = UINT_MAX - 1;
3168 		start = 0;
3169 	} else {
3170 		end = start = ill->ill_ppa;
3171 	}
3172 
3173 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
3174 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
3175 		if (start++ >= end) {
3176 			if (ill->ill_ppa == UINT_MAX)
3177 				return (EAGAIN);
3178 			else
3179 				return (EEXIST);
3180 		}
3181 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
3182 	}
3183 	ill->ill_ppa = start;
3184 	return (0);
3185 }
3186 
3187 /*
3188  * Insert ill into the list of configured ill's. Once this function completes,
3189  * the ill is globally visible and is available through lookups. More precisely
3190  * this happens after the caller drops the ill_g_lock.
3191  */
3192 static int
ill_glist_insert(ill_t * ill,char * name,boolean_t isv6)3193 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
3194 {
3195 	ill_if_t *ill_interface;
3196 	avl_index_t where = 0;
3197 	int error;
3198 	int name_length;
3199 	int index;
3200 	boolean_t check_length = B_FALSE;
3201 	ip_stack_t	*ipst = ill->ill_ipst;
3202 
3203 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
3204 
3205 	name_length = mi_strlen(name) + 1;
3206 
3207 	if (isv6)
3208 		index = IP_V6_G_HEAD;
3209 	else
3210 		index = IP_V4_G_HEAD;
3211 
3212 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
3213 	/*
3214 	 * Search for interface type based on name
3215 	 */
3216 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3217 		if ((ill_interface->illif_name_len == name_length) &&
3218 		    (strcmp(ill_interface->illif_name, name) == 0)) {
3219 			break;
3220 		}
3221 		ill_interface = ill_interface->illif_next;
3222 	}
3223 
3224 	/*
3225 	 * Interface type not found, create one.
3226 	 */
3227 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3228 		ill_g_head_t ghead;
3229 
3230 		/*
3231 		 * allocate ill_if_t structure
3232 		 */
3233 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
3234 		if (ill_interface == NULL) {
3235 			return (ENOMEM);
3236 		}
3237 
3238 		(void) strcpy(ill_interface->illif_name, name);
3239 		ill_interface->illif_name_len = name_length;
3240 
3241 		avl_create(&ill_interface->illif_avl_by_ppa,
3242 		    ill_compare_ppa, sizeof (ill_t),
3243 		    offsetof(struct ill_s, ill_avl_byppa));
3244 
3245 		/*
3246 		 * link the structure in the back to maintain order
3247 		 * of configuration for ifconfig output.
3248 		 */
3249 		ghead = ipst->ips_ill_g_heads[index];
3250 		insque(ill_interface, ghead.ill_g_list_tail);
3251 	}
3252 
3253 	if (ill->ill_ppa == UINT_MAX)
3254 		check_length = B_TRUE;
3255 
3256 	error = ill_alloc_ppa(ill_interface, ill);
3257 	if (error != 0) {
3258 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3259 			ill_delete_interface_type(ill->ill_ifptr);
3260 		return (error);
3261 	}
3262 
3263 	/*
3264 	 * When the ppa is choosen by the system, check that there is
3265 	 * enough space to insert ppa. if a specific ppa was passed in this
3266 	 * check is not required as the interface name passed in will have
3267 	 * the right ppa in it.
3268 	 */
3269 	if (check_length) {
3270 		/*
3271 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
3272 		 */
3273 		char buf[sizeof (uint_t) * 3];
3274 
3275 		/*
3276 		 * convert ppa to string to calculate the amount of space
3277 		 * required for it in the name.
3278 		 */
3279 		numtos(ill->ill_ppa, buf);
3280 
3281 		/* Do we have enough space to insert ppa ? */
3282 
3283 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
3284 			/* Free ppa and interface type struct */
3285 			if (ill_interface->illif_ppa_arena != NULL) {
3286 				vmem_free(ill_interface->illif_ppa_arena,
3287 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3288 			}
3289 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3290 				ill_delete_interface_type(ill->ill_ifptr);
3291 
3292 			return (EINVAL);
3293 		}
3294 	}
3295 
3296 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
3297 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
3298 
3299 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
3300 	    &where);
3301 	ill->ill_ifptr = ill_interface;
3302 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
3303 
3304 	ill_phyint_reinit(ill);
3305 	return (0);
3306 }
3307 
3308 /* Initialize the per phyint ipsq used for serialization */
3309 static boolean_t
ipsq_init(ill_t * ill,boolean_t enter)3310 ipsq_init(ill_t *ill, boolean_t enter)
3311 {
3312 	ipsq_t  *ipsq;
3313 	ipxop_t	*ipx;
3314 
3315 	if ((ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP)) == NULL)
3316 		return (B_FALSE);
3317 
3318 	ill->ill_phyint->phyint_ipsq = ipsq;
3319 	ipx = ipsq->ipsq_xop = &ipsq->ipsq_ownxop;
3320 	ipx->ipx_ipsq = ipsq;
3321 	ipsq->ipsq_next = ipsq;
3322 	ipsq->ipsq_phyint = ill->ill_phyint;
3323 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
3324 	mutex_init(&ipx->ipx_lock, NULL, MUTEX_DEFAULT, 0);
3325 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
3326 	if (enter) {
3327 		ipx->ipx_writer = curthread;
3328 		ipx->ipx_forced = B_FALSE;
3329 		ipx->ipx_reentry_cnt = 1;
3330 #ifdef DEBUG
3331 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
3332 #endif
3333 	}
3334 	return (B_TRUE);
3335 }
3336 
3337 /*
3338  * Here we perform initialisation of the ill_t common to both regular
3339  * interface ILLs and the special loopback ILL created by ill_lookup_on_name.
3340  */
3341 static int
ill_init_common(ill_t * ill,queue_t * q,boolean_t isv6,boolean_t is_loopback,boolean_t ipsq_enter)3342 ill_init_common(ill_t *ill, queue_t *q, boolean_t isv6, boolean_t is_loopback,
3343     boolean_t ipsq_enter)
3344 {
3345 	int count;
3346 	uchar_t *frag_ptr;
3347 
3348 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
3349 	mutex_init(&ill->ill_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
3350 	ill->ill_saved_ire_cnt = 0;
3351 
3352 	if (is_loopback) {
3353 		ill->ill_max_frag = isv6 ? ip_loopback_mtu_v6plus :
3354 		    ip_loopback_mtuplus;
3355 		/*
3356 		 * No resolver here.
3357 		 */
3358 		ill->ill_net_type = IRE_LOOPBACK;
3359 	} else {
3360 		ill->ill_rq = q;
3361 		ill->ill_wq = WR(q);
3362 		ill->ill_ppa = UINT_MAX;
3363 	}
3364 
3365 	ill->ill_isv6 = isv6;
3366 
3367 	/*
3368 	 * Allocate sufficient space to contain our fragment hash table and
3369 	 * the device name.
3370 	 */
3371 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE + 2 * LIFNAMSIZ);
3372 	if (frag_ptr == NULL)
3373 		return (ENOMEM);
3374 	ill->ill_frag_ptr = frag_ptr;
3375 	ill->ill_frag_free_num_pkts = 0;
3376 	ill->ill_last_frag_clean_time = 0;
3377 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
3378 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
3379 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
3380 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
3381 		    NULL, MUTEX_DEFAULT, NULL);
3382 	}
3383 
3384 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3385 	if (ill->ill_phyint == NULL) {
3386 		mi_free(frag_ptr);
3387 		return (ENOMEM);
3388 	}
3389 
3390 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3391 	if (isv6) {
3392 		ill->ill_phyint->phyint_illv6 = ill;
3393 	} else {
3394 		ill->ill_phyint->phyint_illv4 = ill;
3395 	}
3396 	if (is_loopback) {
3397 		phyint_flags_init(ill->ill_phyint, DL_LOOP);
3398 	}
3399 
3400 	list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3401 
3402 	ill_set_inputfn(ill);
3403 
3404 	if (!ipsq_init(ill, ipsq_enter)) {
3405 		mi_free(frag_ptr);
3406 		mi_free(ill->ill_phyint);
3407 		return (ENOMEM);
3408 	}
3409 
3410 	/* Frag queue limit stuff */
3411 	ill->ill_frag_count = 0;
3412 	ill->ill_ipf_gen = 0;
3413 
3414 	rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3415 	mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3416 	ill->ill_global_timer = INFINITY;
3417 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3418 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3419 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3420 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3421 
3422 	/*
3423 	 * Initialize IPv6 configuration variables.  The IP module is always
3424 	 * opened as an IPv4 module.  Instead tracking down the cases where
3425 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
3426 	 * here for convenience, this has no effect until the ill is set to do
3427 	 * IPv6.
3428 	 */
3429 	ill->ill_reachable_time = ND_REACHABLE_TIME;
3430 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
3431 	ill->ill_max_buf = ND_MAX_Q;
3432 	ill->ill_refcnt = 0;
3433 
3434 	return (0);
3435 }
3436 
3437 /*
3438  * ill_init is called by ip_open when a device control stream is opened.
3439  * It does a few initializations, and shoots a DL_INFO_REQ message down
3440  * to the driver.  The response is later picked up in ip_rput_dlpi and
3441  * used to set up default mechanisms for talking to the driver.  (Always
3442  * called as writer.)
3443  *
3444  * If this function returns error, ip_open will call ip_close which in
3445  * turn will call ill_delete to clean up any memory allocated here that
3446  * is not yet freed.
3447  *
3448  * Note: ill_ipst and ill_zoneid must be set before calling ill_init.
3449  */
3450 int
ill_init(queue_t * q,ill_t * ill)3451 ill_init(queue_t *q, ill_t *ill)
3452 {
3453 	int ret;
3454 	dl_info_req_t	*dlir;
3455 	mblk_t	*info_mp;
3456 
3457 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
3458 	    BPRI_HI);
3459 	if (info_mp == NULL)
3460 		return (ENOMEM);
3461 
3462 	/*
3463 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
3464 	 * at this point because of the following reason. If we can't
3465 	 * enter the ipsq at some point and cv_wait, the writer that
3466 	 * wakes us up tries to locate us using the list of all phyints
3467 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
3468 	 * If we don't set it now, we risk a missed wakeup.
3469 	 */
3470 	if ((ret = ill_init_common(ill, q, B_FALSE, B_FALSE, B_TRUE)) != 0) {
3471 		freemsg(info_mp);
3472 		return (ret);
3473 	}
3474 
3475 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
3476 
3477 	/* Send down the Info Request to the driver. */
3478 	info_mp->b_datap->db_type = M_PCPROTO;
3479 	dlir = (dl_info_req_t *)info_mp->b_rptr;
3480 	info_mp->b_wptr = (uchar_t *)&dlir[1];
3481 	dlir->dl_primitive = DL_INFO_REQ;
3482 
3483 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3484 
3485 	qprocson(q);
3486 	ill_dlpi_send(ill, info_mp);
3487 
3488 	return (0);
3489 }
3490 
3491 /*
3492  * ill_dls_info
3493  * creates datalink socket info from the device.
3494  */
3495 int
ill_dls_info(struct sockaddr_dl * sdl,const ill_t * ill)3496 ill_dls_info(struct sockaddr_dl *sdl, const ill_t *ill)
3497 {
3498 	size_t	len;
3499 
3500 	sdl->sdl_family = AF_LINK;
3501 	sdl->sdl_index = ill_get_upper_ifindex(ill);
3502 	sdl->sdl_type = ill->ill_type;
3503 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3504 	len = strlen(sdl->sdl_data);
3505 	ASSERT(len < 256);
3506 	sdl->sdl_nlen = (uchar_t)len;
3507 	sdl->sdl_alen = ill->ill_phys_addr_length;
3508 	sdl->sdl_slen = 0;
3509 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
3510 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
3511 
3512 	return (sizeof (struct sockaddr_dl));
3513 }
3514 
3515 /*
3516  * ill_xarp_info
3517  * creates xarp info from the device.
3518  */
3519 static int
ill_xarp_info(struct sockaddr_dl * sdl,ill_t * ill)3520 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
3521 {
3522 	sdl->sdl_family = AF_LINK;
3523 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
3524 	sdl->sdl_type = ill->ill_type;
3525 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3526 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
3527 	sdl->sdl_alen = ill->ill_phys_addr_length;
3528 	sdl->sdl_slen = 0;
3529 	return (sdl->sdl_nlen);
3530 }
3531 
3532 static int
loopback_kstat_update(kstat_t * ksp,int rw)3533 loopback_kstat_update(kstat_t *ksp, int rw)
3534 {
3535 	kstat_named_t *kn;
3536 	netstackid_t	stackid;
3537 	netstack_t	*ns;
3538 	ip_stack_t	*ipst;
3539 
3540 	if (ksp == NULL || ksp->ks_data == NULL)
3541 		return (EIO);
3542 
3543 	if (rw == KSTAT_WRITE)
3544 		return (EACCES);
3545 
3546 	kn = KSTAT_NAMED_PTR(ksp);
3547 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
3548 
3549 	ns = netstack_find_by_stackid(stackid);
3550 	if (ns == NULL)
3551 		return (-1);
3552 
3553 	ipst = ns->netstack_ip;
3554 	if (ipst == NULL) {
3555 		netstack_rele(ns);
3556 		return (-1);
3557 	}
3558 	kn[0].value.ui32 = ipst->ips_loopback_packets;
3559 	kn[1].value.ui32 = ipst->ips_loopback_packets;
3560 	netstack_rele(ns);
3561 	return (0);
3562 }
3563 
3564 /*
3565  * Has ifindex been plumbed already?
3566  */
3567 static boolean_t
phyint_exists(uint_t index,ip_stack_t * ipst)3568 phyint_exists(uint_t index, ip_stack_t *ipst)
3569 {
3570 	ASSERT(index != 0);
3571 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
3572 
3573 	return (avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3574 	    &index, NULL) != NULL);
3575 }
3576 
3577 /*
3578  * Pick a unique ifindex.
3579  * When the index counter passes IF_INDEX_MAX for the first time, the wrap
3580  * flag is set so that next time time ip_assign_ifindex() is called, it
3581  * falls through and resets the index counter back to 1, the minimum value
3582  * for the interface index. The logic below assumes that ips_ill_index
3583  * can hold a value of IF_INDEX_MAX+1 without there being any loss
3584  * (i.e. reset back to 0.)
3585  */
3586 boolean_t
ip_assign_ifindex(uint_t * indexp,ip_stack_t * ipst)3587 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
3588 {
3589 	uint_t loops;
3590 
3591 	if (!ipst->ips_ill_index_wrap) {
3592 		*indexp = ipst->ips_ill_index++;
3593 		if (ipst->ips_ill_index > IF_INDEX_MAX) {
3594 			/*
3595 			 * Reached the maximum ifindex value, set the wrap
3596 			 * flag to indicate that it is no longer possible
3597 			 * to assume that a given index is unallocated.
3598 			 */
3599 			ipst->ips_ill_index_wrap = B_TRUE;
3600 		}
3601 		return (B_TRUE);
3602 	}
3603 
3604 	if (ipst->ips_ill_index > IF_INDEX_MAX)
3605 		ipst->ips_ill_index = 1;
3606 
3607 	/*
3608 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
3609 	 * at this point and don't want to call any function that attempts
3610 	 * to get the lock again.
3611 	 */
3612 	for (loops = IF_INDEX_MAX; loops > 0; loops--) {
3613 		if (!phyint_exists(ipst->ips_ill_index, ipst)) {
3614 			/* found unused index - use it */
3615 			*indexp = ipst->ips_ill_index;
3616 			return (B_TRUE);
3617 		}
3618 
3619 		ipst->ips_ill_index++;
3620 		if (ipst->ips_ill_index > IF_INDEX_MAX)
3621 			ipst->ips_ill_index = 1;
3622 	}
3623 
3624 	/*
3625 	 * all interface indicies are inuse.
3626 	 */
3627 	return (B_FALSE);
3628 }
3629 
3630 /*
3631  * Assign a unique interface index for the phyint.
3632  */
3633 static boolean_t
phyint_assign_ifindex(phyint_t * phyi,ip_stack_t * ipst)3634 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
3635 {
3636 	ASSERT(phyi->phyint_ifindex == 0);
3637 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
3638 }
3639 
3640 /*
3641  * Initialize the flags on `phyi' as per the provided mactype.
3642  */
3643 static void
phyint_flags_init(phyint_t * phyi,t_uscalar_t mactype)3644 phyint_flags_init(phyint_t *phyi, t_uscalar_t mactype)
3645 {
3646 	uint64_t flags = 0;
3647 
3648 	/*
3649 	 * Initialize PHYI_RUNNING and PHYI_FAILED.  For non-IPMP interfaces,
3650 	 * we always presume the underlying hardware is working and set
3651 	 * PHYI_RUNNING (if it's not, the driver will subsequently send a
3652 	 * DL_NOTE_LINK_DOWN message).  For IPMP interfaces, at initialization
3653 	 * there are no active interfaces in the group so we set PHYI_FAILED.
3654 	 */
3655 	if (mactype == SUNW_DL_IPMP)
3656 		flags |= PHYI_FAILED;
3657 	else
3658 		flags |= PHYI_RUNNING;
3659 
3660 	switch (mactype) {
3661 	case SUNW_DL_VNI:
3662 		flags |= PHYI_VIRTUAL;
3663 		break;
3664 	case SUNW_DL_IPMP:
3665 		flags |= PHYI_IPMP;
3666 		break;
3667 	case DL_LOOP:
3668 		flags |= (PHYI_LOOPBACK | PHYI_VIRTUAL);
3669 		break;
3670 	}
3671 
3672 	mutex_enter(&phyi->phyint_lock);
3673 	phyi->phyint_flags |= flags;
3674 	mutex_exit(&phyi->phyint_lock);
3675 }
3676 
3677 /*
3678  * Return a pointer to the ill which matches the supplied name.  Note that
3679  * the ill name length includes the null termination character.  (May be
3680  * called as writer.)
3681  * If do_alloc and the interface is "lo0" it will be automatically created.
3682  * Cannot bump up reference on condemned ills. So dup detect can't be done
3683  * using this func.
3684  */
3685 ill_t *
ill_lookup_on_name(char * name,boolean_t do_alloc,boolean_t isv6,boolean_t * did_alloc,ip_stack_t * ipst)3686 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
3687     boolean_t *did_alloc, ip_stack_t *ipst)
3688 {
3689 	ill_t	*ill;
3690 	ipif_t	*ipif;
3691 	ipsq_t	*ipsq;
3692 	kstat_named_t	*kn;
3693 	boolean_t isloopback;
3694 	in6_addr_t ov6addr;
3695 
3696 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
3697 
3698 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3699 	ill = ill_find_by_name(name, isv6, ipst);
3700 	rw_exit(&ipst->ips_ill_g_lock);
3701 	if (ill != NULL)
3702 		return (ill);
3703 
3704 	/*
3705 	 * Couldn't find it.  Does this happen to be a lookup for the
3706 	 * loopback device and are we allowed to allocate it?
3707 	 */
3708 	if (!isloopback || !do_alloc)
3709 		return (NULL);
3710 
3711 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
3712 	ill = ill_find_by_name(name, isv6, ipst);
3713 	if (ill != NULL) {
3714 		rw_exit(&ipst->ips_ill_g_lock);
3715 		return (ill);
3716 	}
3717 
3718 	/* Create the loopback device on demand */
3719 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
3720 	    sizeof (ipif_loopback_name), BPRI_MED));
3721 	if (ill == NULL)
3722 		goto done;
3723 
3724 	bzero(ill, sizeof (*ill));
3725 	ill->ill_ipst = ipst;
3726 	netstack_hold(ipst->ips_netstack);
3727 	/*
3728 	 * For exclusive stacks we set the zoneid to zero
3729 	 * to make IP operate as if in the global zone.
3730 	 */
3731 	ill->ill_zoneid = GLOBAL_ZONEID;
3732 
3733 	if (ill_init_common(ill, NULL, isv6, B_TRUE, B_FALSE) != 0)
3734 		goto done;
3735 
3736 	if (!ill_allocate_mibs(ill))
3737 		goto done;
3738 
3739 	ill->ill_current_frag = ill->ill_max_frag;
3740 	ill->ill_mtu = ill->ill_max_frag;	/* Initial value */
3741 	ill->ill_mc_mtu = ill->ill_mtu;
3742 	/*
3743 	 * ipif_loopback_name can't be pointed at directly because its used
3744 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
3745 	 * from the glist, ill_glist_delete() sets the first character of
3746 	 * ill_name to '\0'.
3747 	 */
3748 	ill->ill_name = (char *)ill + sizeof (*ill);
3749 	(void) strcpy(ill->ill_name, ipif_loopback_name);
3750 	ill->ill_name_length = sizeof (ipif_loopback_name);
3751 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
3752 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3753 
3754 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE, B_TRUE, NULL);
3755 	if (ipif == NULL)
3756 		goto done;
3757 
3758 	ill->ill_flags = ILLF_MULTICAST;
3759 
3760 	ov6addr = ipif->ipif_v6lcl_addr;
3761 	/* Set up default loopback address and mask. */
3762 	if (!isv6) {
3763 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
3764 
3765 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
3766 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
3767 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3768 		    ipif->ipif_v6subnet);
3769 		ill->ill_flags |= ILLF_IPV4;
3770 	} else {
3771 		ipif->ipif_v6lcl_addr = ipv6_loopback;
3772 		ipif->ipif_v6net_mask = ipv6_all_ones;
3773 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3774 		    ipif->ipif_v6subnet);
3775 		ill->ill_flags |= ILLF_IPV6;
3776 	}
3777 
3778 	/*
3779 	 * Chain us in at the end of the ill list. hold the ill
3780 	 * before we make it globally visible. 1 for the lookup.
3781 	 */
3782 	ill_refhold(ill);
3783 
3784 	ipsq = ill->ill_phyint->phyint_ipsq;
3785 
3786 	if (ill_glist_insert(ill, "lo", isv6) != 0)
3787 		cmn_err(CE_PANIC, "cannot insert loopback interface");
3788 
3789 	/* Let SCTP know so that it can add this to its list */
3790 	sctp_update_ill(ill, SCTP_ILL_INSERT);
3791 
3792 	/*
3793 	 * We have already assigned ipif_v6lcl_addr above, but we need to
3794 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
3795 	 * requires to be after ill_glist_insert() since we need the
3796 	 * ill_index set. Pass on ipv6_loopback as the old address.
3797 	 */
3798 	sctp_update_ipif_addr(ipif, ov6addr);
3799 
3800 	ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
3801 
3802 	/*
3803 	 * ill_glist_insert() -> ill_phyint_reinit() may have merged IPSQs.
3804 	 * If so, free our original one.
3805 	 */
3806 	if (ipsq != ill->ill_phyint->phyint_ipsq)
3807 		ipsq_delete(ipsq);
3808 
3809 	if (ipst->ips_loopback_ksp == NULL) {
3810 		/* Export loopback interface statistics */
3811 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
3812 		    ipif_loopback_name, "net",
3813 		    KSTAT_TYPE_NAMED, 2, 0,
3814 		    ipst->ips_netstack->netstack_stackid);
3815 		if (ipst->ips_loopback_ksp != NULL) {
3816 			ipst->ips_loopback_ksp->ks_update =
3817 			    loopback_kstat_update;
3818 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
3819 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
3820 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
3821 			ipst->ips_loopback_ksp->ks_private =
3822 			    (void *)(uintptr_t)ipst->ips_netstack->
3823 			    netstack_stackid;
3824 			kstat_install(ipst->ips_loopback_ksp);
3825 		}
3826 	}
3827 
3828 	*did_alloc = B_TRUE;
3829 	rw_exit(&ipst->ips_ill_g_lock);
3830 	ill_nic_event_dispatch(ill, MAP_IPIF_ID(ill->ill_ipif->ipif_id),
3831 	    NE_PLUMB, ill->ill_name, ill->ill_name_length);
3832 	return (ill);
3833 done:
3834 	if (ill != NULL) {
3835 		if (ill->ill_phyint != NULL) {
3836 			ipsq = ill->ill_phyint->phyint_ipsq;
3837 			if (ipsq != NULL) {
3838 				ipsq->ipsq_phyint = NULL;
3839 				ipsq_delete(ipsq);
3840 			}
3841 			mi_free(ill->ill_phyint);
3842 		}
3843 		ill_free_mib(ill);
3844 		if (ill->ill_ipst != NULL)
3845 			netstack_rele(ill->ill_ipst->ips_netstack);
3846 		mi_free(ill);
3847 	}
3848 	rw_exit(&ipst->ips_ill_g_lock);
3849 	return (NULL);
3850 }
3851 
3852 /*
3853  * For IPP calls - use the ip_stack_t for global stack.
3854  */
3855 ill_t *
ill_lookup_on_ifindex_global_instance(uint_t index,boolean_t isv6)3856 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6)
3857 {
3858 	ip_stack_t	*ipst;
3859 	ill_t		*ill;
3860 	netstack_t	*ns;
3861 
3862 	ns = netstack_find_by_stackid(GLOBAL_NETSTACKID);
3863 
3864 	if ((ipst = ns->netstack_ip) == NULL) {
3865 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
3866 		netstack_rele(ns);
3867 		return (NULL);
3868 	}
3869 
3870 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
3871 	netstack_rele(ns);
3872 	return (ill);
3873 }
3874 
3875 /*
3876  * Return a pointer to the ill which matches the index and IP version type.
3877  */
3878 ill_t *
ill_lookup_on_ifindex(uint_t index,boolean_t isv6,ip_stack_t * ipst)3879 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3880 {
3881 	ill_t	*ill;
3882 	phyint_t *phyi;
3883 
3884 	/*
3885 	 * Indexes are stored in the phyint - a common structure
3886 	 * to both IPv4 and IPv6.
3887 	 */
3888 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3889 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3890 	    (void *) &index, NULL);
3891 	if (phyi != NULL) {
3892 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
3893 		if (ill != NULL) {
3894 			mutex_enter(&ill->ill_lock);
3895 			if (!ILL_IS_CONDEMNED(ill)) {
3896 				ill_refhold_locked(ill);
3897 				mutex_exit(&ill->ill_lock);
3898 				rw_exit(&ipst->ips_ill_g_lock);
3899 				return (ill);
3900 			}
3901 			mutex_exit(&ill->ill_lock);
3902 		}
3903 	}
3904 	rw_exit(&ipst->ips_ill_g_lock);
3905 	return (NULL);
3906 }
3907 
3908 /*
3909  * Verify whether or not an interface index is valid for the specified zoneid
3910  * to transmit packets.
3911  * It can be zero (meaning "reset") or an interface index assigned
3912  * to a non-VNI interface. (We don't use VNI interface to send packets.)
3913  */
3914 boolean_t
ip_xmit_ifindex_valid(uint_t ifindex,zoneid_t zoneid,boolean_t isv6,ip_stack_t * ipst)3915 ip_xmit_ifindex_valid(uint_t ifindex, zoneid_t zoneid, boolean_t isv6,
3916     ip_stack_t *ipst)
3917 {
3918 	ill_t		*ill;
3919 
3920 	if (ifindex == 0)
3921 		return (B_TRUE);
3922 
3923 	ill = ill_lookup_on_ifindex_zoneid(ifindex, zoneid, isv6, ipst);
3924 	if (ill == NULL)
3925 		return (B_FALSE);
3926 	if (IS_VNI(ill)) {
3927 		ill_refrele(ill);
3928 		return (B_FALSE);
3929 	}
3930 	ill_refrele(ill);
3931 	return (B_TRUE);
3932 }
3933 
3934 /*
3935  * Return the ifindex next in sequence after the passed in ifindex.
3936  * If there is no next ifindex for the given protocol, return 0.
3937  */
3938 uint_t
ill_get_next_ifindex(uint_t index,boolean_t isv6,ip_stack_t * ipst)3939 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3940 {
3941 	phyint_t *phyi;
3942 	phyint_t *phyi_initial;
3943 	uint_t   ifindex;
3944 
3945 	phyi_initial = NULL;
3946 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3947 
3948 	if (index == 0) {
3949 		phyi = avl_first(
3950 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
3951 	} else {
3952 		phyi = phyi_initial = avl_find(
3953 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3954 		    (void *) &index, NULL);
3955 	}
3956 
3957 	for (; phyi != NULL;
3958 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3959 	    phyi, AVL_AFTER)) {
3960 		/*
3961 		 * If we're not returning the first interface in the tree
3962 		 * and we still haven't moved past the phyint_t that
3963 		 * corresponds to index, avl_walk needs to be called again
3964 		 */
3965 		if (!((index != 0) && (phyi == phyi_initial))) {
3966 			if (isv6) {
3967 				if ((phyi->phyint_illv6) &&
3968 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
3969 				    (phyi->phyint_illv6->ill_isv6 == 1))
3970 					break;
3971 			} else {
3972 				if ((phyi->phyint_illv4) &&
3973 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
3974 				    (phyi->phyint_illv4->ill_isv6 == 0))
3975 					break;
3976 			}
3977 		}
3978 	}
3979 
3980 	rw_exit(&ipst->ips_ill_g_lock);
3981 
3982 	if (phyi != NULL)
3983 		ifindex = phyi->phyint_ifindex;
3984 	else
3985 		ifindex = 0;
3986 
3987 	return (ifindex);
3988 }
3989 
3990 /*
3991  * Return the ifindex for the named interface.
3992  * If there is no next ifindex for the interface, return 0.
3993  */
3994 uint_t
ill_get_ifindex_by_name(char * name,ip_stack_t * ipst)3995 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
3996 {
3997 	phyint_t	*phyi;
3998 	avl_index_t	where = 0;
3999 	uint_t		ifindex;
4000 
4001 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4002 
4003 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
4004 	    name, &where)) == NULL) {
4005 		rw_exit(&ipst->ips_ill_g_lock);
4006 		return (0);
4007 	}
4008 
4009 	ifindex = phyi->phyint_ifindex;
4010 
4011 	rw_exit(&ipst->ips_ill_g_lock);
4012 
4013 	return (ifindex);
4014 }
4015 
4016 /*
4017  * Return the ifindex to be used by upper layer protocols for instance
4018  * for IPV6_RECVPKTINFO. If IPMP this is the one for the upper ill.
4019  */
4020 uint_t
ill_get_upper_ifindex(const ill_t * ill)4021 ill_get_upper_ifindex(const ill_t *ill)
4022 {
4023 	if (IS_UNDER_IPMP(ill))
4024 		return (ipmp_ill_get_ipmp_ifindex(ill));
4025 	else
4026 		return (ill->ill_phyint->phyint_ifindex);
4027 }
4028 
4029 
4030 /*
4031  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
4032  * that gives a running thread a reference to the ill. This reference must be
4033  * released by the thread when it is done accessing the ill and related
4034  * objects. ill_refcnt can not be used to account for static references
4035  * such as other structures pointing to an ill. Callers must generally
4036  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
4037  * or be sure that the ill is not being deleted or changing state before
4038  * calling the refhold functions. A non-zero ill_refcnt ensures that the
4039  * ill won't change any of its critical state such as address, netmask etc.
4040  */
4041 void
ill_refhold(ill_t * ill)4042 ill_refhold(ill_t *ill)
4043 {
4044 	mutex_enter(&ill->ill_lock);
4045 	ill->ill_refcnt++;
4046 	ILL_TRACE_REF(ill);
4047 	mutex_exit(&ill->ill_lock);
4048 }
4049 
4050 void
ill_refhold_locked(ill_t * ill)4051 ill_refhold_locked(ill_t *ill)
4052 {
4053 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4054 	ill->ill_refcnt++;
4055 	ILL_TRACE_REF(ill);
4056 }
4057 
4058 /* Returns true if we managed to get a refhold */
4059 boolean_t
ill_check_and_refhold(ill_t * ill)4060 ill_check_and_refhold(ill_t *ill)
4061 {
4062 	mutex_enter(&ill->ill_lock);
4063 	if (!ILL_IS_CONDEMNED(ill)) {
4064 		ill_refhold_locked(ill);
4065 		mutex_exit(&ill->ill_lock);
4066 		return (B_TRUE);
4067 	}
4068 	mutex_exit(&ill->ill_lock);
4069 	return (B_FALSE);
4070 }
4071 
4072 /*
4073  * Must not be called while holding any locks. Otherwise if this is
4074  * the last reference to be released, there is a chance of recursive mutex
4075  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
4076  * to restart an ioctl.
4077  */
4078 void
ill_refrele(ill_t * ill)4079 ill_refrele(ill_t *ill)
4080 {
4081 	mutex_enter(&ill->ill_lock);
4082 	ASSERT(ill->ill_refcnt != 0);
4083 	ill->ill_refcnt--;
4084 	ILL_UNTRACE_REF(ill);
4085 	if (ill->ill_refcnt != 0) {
4086 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
4087 		mutex_exit(&ill->ill_lock);
4088 		return;
4089 	}
4090 
4091 	/* Drops the ill_lock */
4092 	ipif_ill_refrele_tail(ill);
4093 }
4094 
4095 /*
4096  * Obtain a weak reference count on the ill. This reference ensures the
4097  * ill won't be freed, but the ill may change any of its critical state
4098  * such as netmask, address etc. Returns an error if the ill has started
4099  * closing.
4100  */
4101 boolean_t
ill_waiter_inc(ill_t * ill)4102 ill_waiter_inc(ill_t *ill)
4103 {
4104 	mutex_enter(&ill->ill_lock);
4105 	if (ill->ill_state_flags & ILL_CONDEMNED) {
4106 		mutex_exit(&ill->ill_lock);
4107 		return (B_FALSE);
4108 	}
4109 	ill->ill_waiters++;
4110 	mutex_exit(&ill->ill_lock);
4111 	return (B_TRUE);
4112 }
4113 
4114 void
ill_waiter_dcr(ill_t * ill)4115 ill_waiter_dcr(ill_t *ill)
4116 {
4117 	mutex_enter(&ill->ill_lock);
4118 	ill->ill_waiters--;
4119 	if (ill->ill_waiters == 0)
4120 		cv_broadcast(&ill->ill_cv);
4121 	mutex_exit(&ill->ill_lock);
4122 }
4123 
4124 /*
4125  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
4126  * driver.  We construct best guess defaults for lower level information that
4127  * we need.  If an interface is brought up without injection of any overriding
4128  * information from outside, we have to be ready to go with these defaults.
4129  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
4130  * we primarely want the dl_provider_style.
4131  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
4132  * at which point we assume the other part of the information is valid.
4133  */
4134 void
ip_ll_subnet_defaults(ill_t * ill,mblk_t * mp)4135 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
4136 {
4137 	uchar_t		*brdcst_addr;
4138 	uint_t		brdcst_addr_length, phys_addr_length;
4139 	t_scalar_t	sap_length;
4140 	dl_info_ack_t	*dlia;
4141 	ip_m_t		*ipm;
4142 	dl_qos_cl_sel1_t *sel1;
4143 	int		min_mtu;
4144 
4145 	ASSERT(IAM_WRITER_ILL(ill));
4146 
4147 	/*
4148 	 * Till the ill is fully up  the ill is not globally visible.
4149 	 * So no need for a lock.
4150 	 */
4151 	dlia = (dl_info_ack_t *)mp->b_rptr;
4152 	ill->ill_mactype = dlia->dl_mac_type;
4153 
4154 	ipm = ip_m_lookup(dlia->dl_mac_type);
4155 	if (ipm == NULL) {
4156 		ipm = ip_m_lookup(DL_OTHER);
4157 		ASSERT(ipm != NULL);
4158 	}
4159 	ill->ill_media = ipm;
4160 
4161 	/*
4162 	 * When the new DLPI stuff is ready we'll pull lengths
4163 	 * from dlia.
4164 	 */
4165 	if (dlia->dl_version == DL_VERSION_2) {
4166 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
4167 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
4168 		    brdcst_addr_length);
4169 		if (brdcst_addr == NULL) {
4170 			brdcst_addr_length = 0;
4171 		}
4172 		sap_length = dlia->dl_sap_length;
4173 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
4174 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
4175 		    brdcst_addr_length, sap_length, phys_addr_length));
4176 	} else {
4177 		brdcst_addr_length = 6;
4178 		brdcst_addr = ip_six_byte_all_ones;
4179 		sap_length = -2;
4180 		phys_addr_length = brdcst_addr_length;
4181 	}
4182 
4183 	ill->ill_bcast_addr_length = brdcst_addr_length;
4184 	ill->ill_phys_addr_length = phys_addr_length;
4185 	ill->ill_sap_length = sap_length;
4186 
4187 	/*
4188 	 * Synthetic DLPI types such as SUNW_DL_IPMP specify a zero SDU,
4189 	 * but we must ensure a minimum IP MTU is used since other bits of
4190 	 * IP will fly apart otherwise.
4191 	 */
4192 	min_mtu = ill->ill_isv6 ? IPV6_MIN_MTU : IP_MIN_MTU;
4193 	ill->ill_max_frag = MAX(min_mtu, dlia->dl_max_sdu);
4194 	ill->ill_current_frag = ill->ill_max_frag;
4195 	ill->ill_mtu = ill->ill_max_frag;
4196 	ill->ill_mc_mtu = ill->ill_mtu;	/* Overridden by DL_NOTE_SDU_SIZE2 */
4197 
4198 	ill->ill_type = ipm->ip_m_type;
4199 
4200 	if (!ill->ill_dlpi_style_set) {
4201 		if (dlia->dl_provider_style == DL_STYLE2)
4202 			ill->ill_needs_attach = 1;
4203 
4204 		phyint_flags_init(ill->ill_phyint, ill->ill_mactype);
4205 
4206 		/*
4207 		 * Allocate the first ipif on this ill.  We don't delay it
4208 		 * further as ioctl handling assumes at least one ipif exists.
4209 		 *
4210 		 * At this point we don't know whether the ill is v4 or v6.
4211 		 * We will know this whan the SIOCSLIFNAME happens and
4212 		 * the correct value for ill_isv6 will be assigned in
4213 		 * ipif_set_values(). We need to hold the ill lock and
4214 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
4215 		 * the wakeup.
4216 		 */
4217 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
4218 		    dlia->dl_provider_style != DL_STYLE2, B_TRUE, NULL);
4219 		mutex_enter(&ill->ill_lock);
4220 		ASSERT(ill->ill_dlpi_style_set == 0);
4221 		ill->ill_dlpi_style_set = 1;
4222 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
4223 		cv_broadcast(&ill->ill_cv);
4224 		mutex_exit(&ill->ill_lock);
4225 		freemsg(mp);
4226 		return;
4227 	}
4228 	ASSERT(ill->ill_ipif != NULL);
4229 	/*
4230 	 * We know whether it is IPv4 or IPv6 now, as this is the
4231 	 * second DL_INFO_ACK we are recieving in response to the
4232 	 * DL_INFO_REQ sent in ipif_set_values.
4233 	 */
4234 	ill->ill_sap = (ill->ill_isv6) ? ipm->ip_m_ipv6sap : ipm->ip_m_ipv4sap;
4235 	/*
4236 	 * Clear all the flags that were set based on ill_bcast_addr_length
4237 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
4238 	 * changed now and we need to re-evaluate.
4239 	 */
4240 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
4241 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
4242 
4243 	/*
4244 	 * Free ill_bcast_mp as things could have changed now.
4245 	 *
4246 	 * NOTE: The IPMP meta-interface is special-cased because it starts
4247 	 * with no underlying interfaces (and thus an unknown broadcast
4248 	 * address length), but we enforce that an interface is broadcast-
4249 	 * capable as part of allowing it to join a group.
4250 	 */
4251 	if (ill->ill_bcast_addr_length == 0 && !IS_IPMP(ill)) {
4252 		if (ill->ill_bcast_mp != NULL)
4253 			freemsg(ill->ill_bcast_mp);
4254 		ill->ill_net_type = IRE_IF_NORESOLVER;
4255 
4256 		ill->ill_bcast_mp = ill_dlur_gen(NULL,
4257 		    ill->ill_phys_addr_length,
4258 		    ill->ill_sap,
4259 		    ill->ill_sap_length);
4260 
4261 		if (ill->ill_isv6)
4262 			/*
4263 			 * Note: xresolv interfaces will eventually need NOARP
4264 			 * set here as well, but that will require those
4265 			 * external resolvers to have some knowledge of
4266 			 * that flag and act appropriately. Not to be changed
4267 			 * at present.
4268 			 */
4269 			ill->ill_flags |= ILLF_NONUD;
4270 		else
4271 			ill->ill_flags |= ILLF_NOARP;
4272 
4273 		if (ill->ill_mactype == SUNW_DL_VNI) {
4274 			ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
4275 		} else if (ill->ill_phys_addr_length == 0 ||
4276 		    ill->ill_mactype == DL_IPV4 ||
4277 		    ill->ill_mactype == DL_IPV6) {
4278 			/*
4279 			 * The underying link is point-to-point, so mark the
4280 			 * interface as such.  We can do IP multicast over
4281 			 * such a link since it transmits all network-layer
4282 			 * packets to the remote side the same way.
4283 			 */
4284 			ill->ill_flags |= ILLF_MULTICAST;
4285 			ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
4286 		}
4287 	} else {
4288 		ill->ill_net_type = IRE_IF_RESOLVER;
4289 		if (ill->ill_bcast_mp != NULL)
4290 			freemsg(ill->ill_bcast_mp);
4291 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
4292 		    ill->ill_bcast_addr_length, ill->ill_sap,
4293 		    ill->ill_sap_length);
4294 		/*
4295 		 * Later detect lack of DLPI driver multicast
4296 		 * capability by catching DL_ENABMULTI errors in
4297 		 * ip_rput_dlpi.
4298 		 */
4299 		ill->ill_flags |= ILLF_MULTICAST;
4300 		if (!ill->ill_isv6)
4301 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
4302 	}
4303 
4304 	/* For IPMP, PHYI_IPMP should already be set by phyint_flags_init() */
4305 	if (ill->ill_mactype == SUNW_DL_IPMP)
4306 		ASSERT(ill->ill_phyint->phyint_flags & PHYI_IPMP);
4307 
4308 	/* By default an interface does not support any CoS marking */
4309 	ill->ill_flags &= ~ILLF_COS_ENABLED;
4310 
4311 	/*
4312 	 * If we get QoS information in DL_INFO_ACK, the device supports
4313 	 * some form of CoS marking, set ILLF_COS_ENABLED.
4314 	 */
4315 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
4316 	    dlia->dl_qos_length);
4317 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
4318 		ill->ill_flags |= ILLF_COS_ENABLED;
4319 	}
4320 
4321 	/* Clear any previous error indication. */
4322 	ill->ill_error = 0;
4323 	freemsg(mp);
4324 }
4325 
4326 /*
4327  * Perform various checks to verify that an address would make sense as a
4328  * local, remote, or subnet interface address.
4329  */
4330 static boolean_t
ip_addr_ok_v4(ipaddr_t addr,ipaddr_t subnet_mask)4331 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
4332 {
4333 	ipaddr_t	net_mask;
4334 
4335 	/*
4336 	 * Don't allow all zeroes, or all ones, but allow
4337 	 * all ones netmask.
4338 	 */
4339 	if ((net_mask = ip_net_mask(addr)) == 0)
4340 		return (B_FALSE);
4341 	/* A given netmask overrides the "guess" netmask */
4342 	if (subnet_mask != 0)
4343 		net_mask = subnet_mask;
4344 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
4345 	    (addr == (addr | ~net_mask)))) {
4346 		return (B_FALSE);
4347 	}
4348 
4349 	/*
4350 	 * Even if the netmask is all ones, we do not allow address to be
4351 	 * 255.255.255.255
4352 	 */
4353 	if (addr == INADDR_BROADCAST)
4354 		return (B_FALSE);
4355 
4356 	if (CLASSD(addr))
4357 		return (B_FALSE);
4358 
4359 	return (B_TRUE);
4360 }
4361 
4362 #define	V6_IPIF_LINKLOCAL(p)	\
4363 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
4364 
4365 /*
4366  * Compare two given ipifs and check if the second one is better than
4367  * the first one using the order of preference (not taking deprecated
4368  * into acount) specified in ipif_lookup_multicast().
4369  */
4370 static boolean_t
ipif_comp_multi(ipif_t * old_ipif,ipif_t * new_ipif,boolean_t isv6)4371 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
4372 {
4373 	/* Check the least preferred first. */
4374 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
4375 		/* If both ipifs are the same, use the first one. */
4376 		if (IS_LOOPBACK(new_ipif->ipif_ill))
4377 			return (B_FALSE);
4378 		else
4379 			return (B_TRUE);
4380 	}
4381 
4382 	/* For IPv6, check for link local address. */
4383 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
4384 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4385 		    V6_IPIF_LINKLOCAL(new_ipif)) {
4386 			/* The second one is equal or less preferred. */
4387 			return (B_FALSE);
4388 		} else {
4389 			return (B_TRUE);
4390 		}
4391 	}
4392 
4393 	/* Then check for point to point interface. */
4394 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
4395 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4396 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
4397 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
4398 			return (B_FALSE);
4399 		} else {
4400 			return (B_TRUE);
4401 		}
4402 	}
4403 
4404 	/* old_ipif is a normal interface, so no need to use the new one. */
4405 	return (B_FALSE);
4406 }
4407 
4408 /*
4409  * Find a mulitcast-capable ipif given an IP instance and zoneid.
4410  * The ipif must be up, and its ill must multicast-capable, not
4411  * condemned, not an underlying interface in an IPMP group, and
4412  * not a VNI interface.  Order of preference:
4413  *
4414  *	1a. normal
4415  *	1b. normal, but deprecated
4416  *	2a. point to point
4417  *	2b. point to point, but deprecated
4418  *	3a. link local
4419  *	3b. link local, but deprecated
4420  *	4. loopback.
4421  */
4422 static ipif_t *
ipif_lookup_multicast(ip_stack_t * ipst,zoneid_t zoneid,boolean_t isv6)4423 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4424 {
4425 	ill_t			*ill;
4426 	ill_walk_context_t	ctx;
4427 	ipif_t			*ipif;
4428 	ipif_t			*saved_ipif = NULL;
4429 	ipif_t			*dep_ipif = NULL;
4430 
4431 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4432 	if (isv6)
4433 		ill = ILL_START_WALK_V6(&ctx, ipst);
4434 	else
4435 		ill = ILL_START_WALK_V4(&ctx, ipst);
4436 
4437 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4438 		mutex_enter(&ill->ill_lock);
4439 		if (IS_VNI(ill) || IS_UNDER_IPMP(ill) ||
4440 		    ILL_IS_CONDEMNED(ill) ||
4441 		    !(ill->ill_flags & ILLF_MULTICAST)) {
4442 			mutex_exit(&ill->ill_lock);
4443 			continue;
4444 		}
4445 		for (ipif = ill->ill_ipif; ipif != NULL;
4446 		    ipif = ipif->ipif_next) {
4447 			if (zoneid != ipif->ipif_zoneid &&
4448 			    zoneid != ALL_ZONES &&
4449 			    ipif->ipif_zoneid != ALL_ZONES) {
4450 				continue;
4451 			}
4452 			if (!(ipif->ipif_flags & IPIF_UP) ||
4453 			    IPIF_IS_CONDEMNED(ipif)) {
4454 				continue;
4455 			}
4456 
4457 			/*
4458 			 * Found one candidate.  If it is deprecated,
4459 			 * remember it in dep_ipif.  If it is not deprecated,
4460 			 * remember it in saved_ipif.
4461 			 */
4462 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
4463 				if (dep_ipif == NULL) {
4464 					dep_ipif = ipif;
4465 				} else if (ipif_comp_multi(dep_ipif, ipif,
4466 				    isv6)) {
4467 					/*
4468 					 * If the previous dep_ipif does not
4469 					 * belong to the same ill, we've done
4470 					 * a ipif_refhold() on it.  So we need
4471 					 * to release it.
4472 					 */
4473 					if (dep_ipif->ipif_ill != ill)
4474 						ipif_refrele(dep_ipif);
4475 					dep_ipif = ipif;
4476 				}
4477 				continue;
4478 			}
4479 			if (saved_ipif == NULL) {
4480 				saved_ipif = ipif;
4481 			} else {
4482 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
4483 					if (saved_ipif->ipif_ill != ill)
4484 						ipif_refrele(saved_ipif);
4485 					saved_ipif = ipif;
4486 				}
4487 			}
4488 		}
4489 		/*
4490 		 * Before going to the next ill, do a ipif_refhold() on the
4491 		 * saved ones.
4492 		 */
4493 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
4494 			ipif_refhold_locked(saved_ipif);
4495 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
4496 			ipif_refhold_locked(dep_ipif);
4497 		mutex_exit(&ill->ill_lock);
4498 	}
4499 	rw_exit(&ipst->ips_ill_g_lock);
4500 
4501 	/*
4502 	 * If we have only the saved_ipif, return it.  But if we have both
4503 	 * saved_ipif and dep_ipif, check to see which one is better.
4504 	 */
4505 	if (saved_ipif != NULL) {
4506 		if (dep_ipif != NULL) {
4507 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
4508 				ipif_refrele(saved_ipif);
4509 				return (dep_ipif);
4510 			} else {
4511 				ipif_refrele(dep_ipif);
4512 				return (saved_ipif);
4513 			}
4514 		}
4515 		return (saved_ipif);
4516 	} else {
4517 		return (dep_ipif);
4518 	}
4519 }
4520 
4521 ill_t *
ill_lookup_multicast(ip_stack_t * ipst,zoneid_t zoneid,boolean_t isv6)4522 ill_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4523 {
4524 	ipif_t *ipif;
4525 	ill_t *ill;
4526 
4527 	ipif = ipif_lookup_multicast(ipst, zoneid, isv6);
4528 	if (ipif == NULL)
4529 		return (NULL);
4530 
4531 	ill = ipif->ipif_ill;
4532 	ill_refhold(ill);
4533 	ipif_refrele(ipif);
4534 	return (ill);
4535 }
4536 
4537 /*
4538  * This function is called when an application does not specify an interface
4539  * to be used for multicast traffic (joining a group/sending data).  It
4540  * calls ire_lookup_multi() to look for an interface route for the
4541  * specified multicast group.  Doing this allows the administrator to add
4542  * prefix routes for multicast to indicate which interface to be used for
4543  * multicast traffic in the above scenario.  The route could be for all
4544  * multicast (224.0/4), for a single multicast group (a /32 route) or
4545  * anything in between.  If there is no such multicast route, we just find
4546  * any multicast capable interface and return it.  The returned ipif
4547  * is refhold'ed.
4548  *
4549  * We support MULTIRT and RTF_SETSRC on the multicast routes added to the
4550  * unicast table. This is used by CGTP.
4551  */
4552 ill_t *
ill_lookup_group_v4(ipaddr_t group,zoneid_t zoneid,ip_stack_t * ipst,boolean_t * multirtp,ipaddr_t * setsrcp)4553 ill_lookup_group_v4(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst,
4554     boolean_t *multirtp, ipaddr_t *setsrcp)
4555 {
4556 	ill_t			*ill;
4557 
4558 	ill = ire_lookup_multi_ill_v4(group, zoneid, ipst, multirtp, setsrcp);
4559 	if (ill != NULL)
4560 		return (ill);
4561 
4562 	return (ill_lookup_multicast(ipst, zoneid, B_FALSE));
4563 }
4564 
4565 /*
4566  * Look for an ipif with the specified interface address and destination.
4567  * The destination address is used only for matching point-to-point interfaces.
4568  */
4569 ipif_t *
ipif_lookup_interface(ipaddr_t if_addr,ipaddr_t dst,ip_stack_t * ipst)4570 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, ip_stack_t *ipst)
4571 {
4572 	ipif_t	*ipif;
4573 	ill_t	*ill;
4574 	ill_walk_context_t ctx;
4575 
4576 	/*
4577 	 * First match all the point-to-point interfaces
4578 	 * before looking at non-point-to-point interfaces.
4579 	 * This is done to avoid returning non-point-to-point
4580 	 * ipif instead of unnumbered point-to-point ipif.
4581 	 */
4582 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4583 	ill = ILL_START_WALK_V4(&ctx, ipst);
4584 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4585 		mutex_enter(&ill->ill_lock);
4586 		for (ipif = ill->ill_ipif; ipif != NULL;
4587 		    ipif = ipif->ipif_next) {
4588 			/* Allow the ipif to be down */
4589 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
4590 			    (ipif->ipif_lcl_addr == if_addr) &&
4591 			    (ipif->ipif_pp_dst_addr == dst)) {
4592 				if (!IPIF_IS_CONDEMNED(ipif)) {
4593 					ipif_refhold_locked(ipif);
4594 					mutex_exit(&ill->ill_lock);
4595 					rw_exit(&ipst->ips_ill_g_lock);
4596 					return (ipif);
4597 				}
4598 			}
4599 		}
4600 		mutex_exit(&ill->ill_lock);
4601 	}
4602 	rw_exit(&ipst->ips_ill_g_lock);
4603 
4604 	/* lookup the ipif based on interface address */
4605 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, ipst);
4606 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
4607 	return (ipif);
4608 }
4609 
4610 /*
4611  * Common function for ipif_lookup_addr() and ipif_lookup_addr_exact().
4612  */
4613 static ipif_t *
ipif_lookup_addr_common(ipaddr_t addr,ill_t * match_ill,uint32_t match_flags,zoneid_t zoneid,ip_stack_t * ipst)4614 ipif_lookup_addr_common(ipaddr_t addr, ill_t *match_ill, uint32_t match_flags,
4615     zoneid_t zoneid, ip_stack_t *ipst)
4616 {
4617 	ipif_t  *ipif;
4618 	ill_t   *ill;
4619 	boolean_t ptp = B_FALSE;
4620 	ill_walk_context_t	ctx;
4621 	boolean_t match_illgrp = (match_flags & IPIF_MATCH_ILLGRP);
4622 	boolean_t no_duplicate = (match_flags & IPIF_MATCH_NONDUP);
4623 
4624 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4625 	/*
4626 	 * Repeat twice, first based on local addresses and
4627 	 * next time for pointopoint.
4628 	 */
4629 repeat:
4630 	ill = ILL_START_WALK_V4(&ctx, ipst);
4631 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4632 		if (match_ill != NULL && ill != match_ill &&
4633 		    (!match_illgrp || !IS_IN_SAME_ILLGRP(ill, match_ill))) {
4634 			continue;
4635 		}
4636 		mutex_enter(&ill->ill_lock);
4637 		for (ipif = ill->ill_ipif; ipif != NULL;
4638 		    ipif = ipif->ipif_next) {
4639 			if (zoneid != ALL_ZONES &&
4640 			    zoneid != ipif->ipif_zoneid &&
4641 			    ipif->ipif_zoneid != ALL_ZONES)
4642 				continue;
4643 
4644 			if (no_duplicate && !(ipif->ipif_flags & IPIF_UP))
4645 				continue;
4646 
4647 			/* Allow the ipif to be down */
4648 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4649 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4650 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4651 			    (ipif->ipif_pp_dst_addr == addr))) {
4652 				if (!IPIF_IS_CONDEMNED(ipif)) {
4653 					ipif_refhold_locked(ipif);
4654 					mutex_exit(&ill->ill_lock);
4655 					rw_exit(&ipst->ips_ill_g_lock);
4656 					return (ipif);
4657 				}
4658 			}
4659 		}
4660 		mutex_exit(&ill->ill_lock);
4661 	}
4662 
4663 	/* If we already did the ptp case, then we are done */
4664 	if (ptp) {
4665 		rw_exit(&ipst->ips_ill_g_lock);
4666 		return (NULL);
4667 	}
4668 	ptp = B_TRUE;
4669 	goto repeat;
4670 }
4671 
4672 /*
4673  * Lookup an ipif with the specified address.  For point-to-point links we
4674  * look for matches on either the destination address or the local address,
4675  * but we skip the local address check if IPIF_UNNUMBERED is set.  If the
4676  * `match_ill' argument is non-NULL, the lookup is restricted to that ill
4677  * (or illgrp if `match_ill' is in an IPMP group).
4678  */
4679 ipif_t *
ipif_lookup_addr(ipaddr_t addr,ill_t * match_ill,zoneid_t zoneid,ip_stack_t * ipst)4680 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4681     ip_stack_t *ipst)
4682 {
4683 	return (ipif_lookup_addr_common(addr, match_ill, IPIF_MATCH_ILLGRP,
4684 	    zoneid, ipst));
4685 }
4686 
4687 /*
4688  * Lookup an ipif with the specified address. Similar to ipif_lookup_addr,
4689  * except that we will only return an address if it is not marked as
4690  * IPIF_DUPLICATE
4691  */
4692 ipif_t *
ipif_lookup_addr_nondup(ipaddr_t addr,ill_t * match_ill,zoneid_t zoneid,ip_stack_t * ipst)4693 ipif_lookup_addr_nondup(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4694     ip_stack_t *ipst)
4695 {
4696 	return (ipif_lookup_addr_common(addr, match_ill,
4697 	    (IPIF_MATCH_ILLGRP | IPIF_MATCH_NONDUP),
4698 	    zoneid, ipst));
4699 }
4700 
4701 /*
4702  * Special abbreviated version of ipif_lookup_addr() that doesn't match
4703  * `match_ill' across the IPMP group.  This function is only needed in some
4704  * corner-cases; almost everything should use ipif_lookup_addr().
4705  */
4706 ipif_t *
ipif_lookup_addr_exact(ipaddr_t addr,ill_t * match_ill,ip_stack_t * ipst)4707 ipif_lookup_addr_exact(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4708 {
4709 	ASSERT(match_ill != NULL);
4710 	return (ipif_lookup_addr_common(addr, match_ill, 0, ALL_ZONES,
4711 	    ipst));
4712 }
4713 
4714 /*
4715  * Look for an ipif with the specified address. For point-point links
4716  * we look for matches on either the destination address and the local
4717  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
4718  * is set.
4719  * If the `match_ill' argument is non-NULL, the lookup is restricted to that
4720  * ill (or illgrp if `match_ill' is in an IPMP group).
4721  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
4722  */
4723 zoneid_t
ipif_lookup_addr_zoneid(ipaddr_t addr,ill_t * match_ill,ip_stack_t * ipst)4724 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4725 {
4726 	zoneid_t zoneid;
4727 	ipif_t  *ipif;
4728 	ill_t   *ill;
4729 	boolean_t ptp = B_FALSE;
4730 	ill_walk_context_t	ctx;
4731 
4732 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4733 	/*
4734 	 * Repeat twice, first based on local addresses and
4735 	 * next time for pointopoint.
4736 	 */
4737 repeat:
4738 	ill = ILL_START_WALK_V4(&ctx, ipst);
4739 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4740 		if (match_ill != NULL && ill != match_ill &&
4741 		    !IS_IN_SAME_ILLGRP(ill, match_ill)) {
4742 			continue;
4743 		}
4744 		mutex_enter(&ill->ill_lock);
4745 		for (ipif = ill->ill_ipif; ipif != NULL;
4746 		    ipif = ipif->ipif_next) {
4747 			/* Allow the ipif to be down */
4748 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4749 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4750 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4751 			    (ipif->ipif_pp_dst_addr == addr)) &&
4752 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
4753 				zoneid = ipif->ipif_zoneid;
4754 				mutex_exit(&ill->ill_lock);
4755 				rw_exit(&ipst->ips_ill_g_lock);
4756 				/*
4757 				 * If ipif_zoneid was ALL_ZONES then we have
4758 				 * a trusted extensions shared IP address.
4759 				 * In that case GLOBAL_ZONEID works to send.
4760 				 */
4761 				if (zoneid == ALL_ZONES)
4762 					zoneid = GLOBAL_ZONEID;
4763 				return (zoneid);
4764 			}
4765 		}
4766 		mutex_exit(&ill->ill_lock);
4767 	}
4768 
4769 	/* If we already did the ptp case, then we are done */
4770 	if (ptp) {
4771 		rw_exit(&ipst->ips_ill_g_lock);
4772 		return (ALL_ZONES);
4773 	}
4774 	ptp = B_TRUE;
4775 	goto repeat;
4776 }
4777 
4778 /*
4779  * Look for an ipif that matches the specified remote address i.e. the
4780  * ipif that would receive the specified packet.
4781  * First look for directly connected interfaces and then do a recursive
4782  * IRE lookup and pick the first ipif corresponding to the source address in the
4783  * ire.
4784  * Returns: held ipif
4785  *
4786  * This is only used for ICMP_ADDRESS_MASK_REQUESTs
4787  */
4788 ipif_t *
ipif_lookup_remote(ill_t * ill,ipaddr_t addr,zoneid_t zoneid)4789 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
4790 {
4791 	ipif_t	*ipif;
4792 
4793 	ASSERT(!ill->ill_isv6);
4794 
4795 	/*
4796 	 * Someone could be changing this ipif currently or change it
4797 	 * after we return this. Thus  a few packets could use the old
4798 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
4799 	 * will atomically be updated or cleaned up with the new value
4800 	 * Thus we don't need a lock to check the flags or other attrs below.
4801 	 */
4802 	mutex_enter(&ill->ill_lock);
4803 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4804 		if (IPIF_IS_CONDEMNED(ipif))
4805 			continue;
4806 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
4807 		    ipif->ipif_zoneid != ALL_ZONES)
4808 			continue;
4809 		/* Allow the ipif to be down */
4810 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
4811 			if ((ipif->ipif_pp_dst_addr == addr) ||
4812 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
4813 			    ipif->ipif_lcl_addr == addr)) {
4814 				ipif_refhold_locked(ipif);
4815 				mutex_exit(&ill->ill_lock);
4816 				return (ipif);
4817 			}
4818 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
4819 			ipif_refhold_locked(ipif);
4820 			mutex_exit(&ill->ill_lock);
4821 			return (ipif);
4822 		}
4823 	}
4824 	mutex_exit(&ill->ill_lock);
4825 	/*
4826 	 * For a remote destination it isn't possible to nail down a particular
4827 	 * ipif.
4828 	 */
4829 
4830 	/* Pick the first interface */
4831 	ipif = ipif_get_next_ipif(NULL, ill);
4832 	return (ipif);
4833 }
4834 
4835 /*
4836  * This func does not prevent refcnt from increasing. But if
4837  * the caller has taken steps to that effect, then this func
4838  * can be used to determine whether the ill has become quiescent
4839  */
4840 static boolean_t
ill_is_quiescent(ill_t * ill)4841 ill_is_quiescent(ill_t *ill)
4842 {
4843 	ipif_t	*ipif;
4844 
4845 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4846 
4847 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4848 		if (ipif->ipif_refcnt != 0)
4849 			return (B_FALSE);
4850 	}
4851 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
4852 		return (B_FALSE);
4853 	}
4854 	return (B_TRUE);
4855 }
4856 
4857 boolean_t
ill_is_freeable(ill_t * ill)4858 ill_is_freeable(ill_t *ill)
4859 {
4860 	ipif_t	*ipif;
4861 
4862 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4863 
4864 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4865 		if (ipif->ipif_refcnt != 0) {
4866 			return (B_FALSE);
4867 		}
4868 	}
4869 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
4870 		return (B_FALSE);
4871 	}
4872 	return (B_TRUE);
4873 }
4874 
4875 /*
4876  * This func does not prevent refcnt from increasing. But if
4877  * the caller has taken steps to that effect, then this func
4878  * can be used to determine whether the ipif has become quiescent
4879  */
4880 static boolean_t
ipif_is_quiescent(ipif_t * ipif)4881 ipif_is_quiescent(ipif_t *ipif)
4882 {
4883 	ill_t *ill;
4884 
4885 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4886 
4887 	if (ipif->ipif_refcnt != 0)
4888 		return (B_FALSE);
4889 
4890 	ill = ipif->ipif_ill;
4891 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
4892 	    ill->ill_logical_down) {
4893 		return (B_TRUE);
4894 	}
4895 
4896 	/* This is the last ipif going down or being deleted on this ill */
4897 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
4898 		return (B_FALSE);
4899 	}
4900 
4901 	return (B_TRUE);
4902 }
4903 
4904 /*
4905  * return true if the ipif can be destroyed: the ipif has to be quiescent
4906  * with zero references from ire/ilm to it.
4907  */
4908 static boolean_t
ipif_is_freeable(ipif_t * ipif)4909 ipif_is_freeable(ipif_t *ipif)
4910 {
4911 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4912 	ASSERT(ipif->ipif_id != 0);
4913 	return (ipif->ipif_refcnt == 0);
4914 }
4915 
4916 /*
4917  * The ipif/ill/ire has been refreled. Do the tail processing.
4918  * Determine if the ipif or ill in question has become quiescent and if so
4919  * wakeup close and/or restart any queued pending ioctl that is waiting
4920  * for the ipif_down (or ill_down)
4921  */
4922 void
ipif_ill_refrele_tail(ill_t * ill)4923 ipif_ill_refrele_tail(ill_t *ill)
4924 {
4925 	mblk_t	*mp;
4926 	conn_t	*connp;
4927 	ipsq_t	*ipsq;
4928 	ipxop_t	*ipx;
4929 	ipif_t	*ipif;
4930 	dl_notify_ind_t *dlindp;
4931 
4932 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4933 
4934 	if ((ill->ill_state_flags & ILL_CONDEMNED) && ill_is_freeable(ill)) {
4935 		/* ip_modclose() may be waiting */
4936 		cv_broadcast(&ill->ill_cv);
4937 	}
4938 
4939 	ipsq = ill->ill_phyint->phyint_ipsq;
4940 	mutex_enter(&ipsq->ipsq_lock);
4941 	ipx = ipsq->ipsq_xop;
4942 	mutex_enter(&ipx->ipx_lock);
4943 	if (ipx->ipx_waitfor == 0)	/* no one's waiting; bail */
4944 		goto unlock;
4945 
4946 	ASSERT(ipx->ipx_pending_mp != NULL && ipx->ipx_pending_ipif != NULL);
4947 
4948 	ipif = ipx->ipx_pending_ipif;
4949 	if (ipif->ipif_ill != ill)	/* wait is for another ill; bail */
4950 		goto unlock;
4951 
4952 	switch (ipx->ipx_waitfor) {
4953 	case IPIF_DOWN:
4954 		if (!ipif_is_quiescent(ipif))
4955 			goto unlock;
4956 		break;
4957 	case IPIF_FREE:
4958 		if (!ipif_is_freeable(ipif))
4959 			goto unlock;
4960 		break;
4961 	case ILL_DOWN:
4962 		if (!ill_is_quiescent(ill))
4963 			goto unlock;
4964 		break;
4965 	case ILL_FREE:
4966 		/*
4967 		 * ILL_FREE is only for loopback; normal ill teardown waits
4968 		 * synchronously in ip_modclose() without using ipx_waitfor,
4969 		 * handled by the cv_broadcast() at the top of this function.
4970 		 */
4971 		if (!ill_is_freeable(ill))
4972 			goto unlock;
4973 		break;
4974 	default:
4975 		cmn_err(CE_PANIC, "ipsq: %p unknown ipx_waitfor %d\n",
4976 		    (void *)ipsq, ipx->ipx_waitfor);
4977 	}
4978 
4979 	ill_refhold_locked(ill);	/* for qwriter_ip() call below */
4980 	mutex_exit(&ipx->ipx_lock);
4981 	mp = ipsq_pending_mp_get(ipsq, &connp);
4982 	mutex_exit(&ipsq->ipsq_lock);
4983 	mutex_exit(&ill->ill_lock);
4984 
4985 	ASSERT(mp != NULL);
4986 	/*
4987 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
4988 	 * we can only get here when the current operation decides it
4989 	 * it needs to quiesce via ipsq_pending_mp_add().
4990 	 */
4991 	switch (mp->b_datap->db_type) {
4992 	case M_PCPROTO:
4993 	case M_PROTO:
4994 		/*
4995 		 * For now, only DL_NOTIFY_IND messages can use this facility.
4996 		 */
4997 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
4998 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
4999 
5000 		switch (dlindp->dl_notification) {
5001 		case DL_NOTE_PHYS_ADDR:
5002 			qwriter_ip(ill, ill->ill_rq, mp,
5003 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
5004 			return;
5005 		case DL_NOTE_REPLUMB:
5006 			qwriter_ip(ill, ill->ill_rq, mp,
5007 			    ill_replumb_tail, CUR_OP, B_TRUE);
5008 			return;
5009 		default:
5010 			ASSERT(0);
5011 			ill_refrele(ill);
5012 		}
5013 		break;
5014 
5015 	case M_ERROR:
5016 	case M_HANGUP:
5017 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
5018 		    B_TRUE);
5019 		return;
5020 
5021 	case M_IOCTL:
5022 	case M_IOCDATA:
5023 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
5024 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
5025 		return;
5026 
5027 	default:
5028 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
5029 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
5030 	}
5031 	return;
5032 unlock:
5033 	mutex_exit(&ipsq->ipsq_lock);
5034 	mutex_exit(&ipx->ipx_lock);
5035 	mutex_exit(&ill->ill_lock);
5036 }
5037 
5038 #ifdef DEBUG
5039 /* Reuse trace buffer from beginning (if reached the end) and record trace */
5040 static void
th_trace_rrecord(th_trace_t * th_trace)5041 th_trace_rrecord(th_trace_t *th_trace)
5042 {
5043 	tr_buf_t *tr_buf;
5044 	uint_t lastref;
5045 
5046 	lastref = th_trace->th_trace_lastref;
5047 	lastref++;
5048 	if (lastref == TR_BUF_MAX)
5049 		lastref = 0;
5050 	th_trace->th_trace_lastref = lastref;
5051 	tr_buf = &th_trace->th_trbuf[lastref];
5052 	tr_buf->tr_time = ddi_get_lbolt();
5053 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
5054 }
5055 
5056 static void
th_trace_free(void * value)5057 th_trace_free(void *value)
5058 {
5059 	th_trace_t *th_trace = value;
5060 
5061 	ASSERT(th_trace->th_refcnt == 0);
5062 	kmem_free(th_trace, sizeof (*th_trace));
5063 }
5064 
5065 /*
5066  * Find or create the per-thread hash table used to track object references.
5067  * The ipst argument is NULL if we shouldn't allocate.
5068  *
5069  * Accesses per-thread data, so there's no need to lock here.
5070  */
5071 static mod_hash_t *
th_trace_gethash(ip_stack_t * ipst)5072 th_trace_gethash(ip_stack_t *ipst)
5073 {
5074 	th_hash_t *thh;
5075 
5076 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
5077 		mod_hash_t *mh;
5078 		char name[256];
5079 		size_t objsize, rshift;
5080 		int retv;
5081 
5082 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
5083 			return (NULL);
5084 		(void) snprintf(name, sizeof (name), "th_trace_%p",
5085 		    (void *)curthread);
5086 
5087 		/*
5088 		 * We use mod_hash_create_extended here rather than the more
5089 		 * obvious mod_hash_create_ptrhash because the latter has a
5090 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
5091 		 * block.
5092 		 */
5093 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
5094 		    MAX(sizeof (ire_t), sizeof (ncec_t)));
5095 		rshift = highbit(objsize);
5096 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
5097 		    th_trace_free, mod_hash_byptr, (void *)rshift,
5098 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
5099 		if (mh == NULL) {
5100 			kmem_free(thh, sizeof (*thh));
5101 			return (NULL);
5102 		}
5103 		thh->thh_hash = mh;
5104 		thh->thh_ipst = ipst;
5105 		/*
5106 		 * We trace ills, ipifs, ires, and nces.  All of these are
5107 		 * per-IP-stack, so the lock on the thread list is as well.
5108 		 */
5109 		rw_enter(&ip_thread_rwlock, RW_WRITER);
5110 		list_insert_tail(&ip_thread_list, thh);
5111 		rw_exit(&ip_thread_rwlock);
5112 		retv = tsd_set(ip_thread_data, thh);
5113 		ASSERT(retv == 0);
5114 	}
5115 	return (thh != NULL ? thh->thh_hash : NULL);
5116 }
5117 
5118 boolean_t
th_trace_ref(const void * obj,ip_stack_t * ipst)5119 th_trace_ref(const void *obj, ip_stack_t *ipst)
5120 {
5121 	th_trace_t *th_trace;
5122 	mod_hash_t *mh;
5123 	mod_hash_val_t val;
5124 
5125 	if ((mh = th_trace_gethash(ipst)) == NULL)
5126 		return (B_FALSE);
5127 
5128 	/*
5129 	 * Attempt to locate the trace buffer for this obj and thread.
5130 	 * If it does not exist, then allocate a new trace buffer and
5131 	 * insert into the hash.
5132 	 */
5133 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
5134 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
5135 		if (th_trace == NULL)
5136 			return (B_FALSE);
5137 
5138 		th_trace->th_id = curthread;
5139 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
5140 		    (mod_hash_val_t)th_trace) != 0) {
5141 			kmem_free(th_trace, sizeof (th_trace_t));
5142 			return (B_FALSE);
5143 		}
5144 	} else {
5145 		th_trace = (th_trace_t *)val;
5146 	}
5147 
5148 	ASSERT(th_trace->th_refcnt >= 0 &&
5149 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
5150 
5151 	th_trace->th_refcnt++;
5152 	th_trace_rrecord(th_trace);
5153 	return (B_TRUE);
5154 }
5155 
5156 /*
5157  * For the purpose of tracing a reference release, we assume that global
5158  * tracing is always on and that the same thread initiated the reference hold
5159  * is releasing.
5160  */
5161 void
th_trace_unref(const void * obj)5162 th_trace_unref(const void *obj)
5163 {
5164 	int retv;
5165 	mod_hash_t *mh;
5166 	th_trace_t *th_trace;
5167 	mod_hash_val_t val;
5168 
5169 	mh = th_trace_gethash(NULL);
5170 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
5171 	ASSERT(retv == 0);
5172 	th_trace = (th_trace_t *)val;
5173 
5174 	ASSERT(th_trace->th_refcnt > 0);
5175 	th_trace->th_refcnt--;
5176 	th_trace_rrecord(th_trace);
5177 }
5178 
5179 /*
5180  * If tracing has been disabled, then we assume that the reference counts are
5181  * now useless, and we clear them out before destroying the entries.
5182  */
5183 void
th_trace_cleanup(const void * obj,boolean_t trace_disable)5184 th_trace_cleanup(const void *obj, boolean_t trace_disable)
5185 {
5186 	th_hash_t	*thh;
5187 	mod_hash_t	*mh;
5188 	mod_hash_val_t	val;
5189 	th_trace_t	*th_trace;
5190 	int		retv;
5191 
5192 	rw_enter(&ip_thread_rwlock, RW_READER);
5193 	for (thh = list_head(&ip_thread_list); thh != NULL;
5194 	    thh = list_next(&ip_thread_list, thh)) {
5195 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
5196 		    &val) == 0) {
5197 			th_trace = (th_trace_t *)val;
5198 			if (trace_disable)
5199 				th_trace->th_refcnt = 0;
5200 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
5201 			ASSERT(retv == 0);
5202 		}
5203 	}
5204 	rw_exit(&ip_thread_rwlock);
5205 }
5206 
5207 void
ipif_trace_ref(ipif_t * ipif)5208 ipif_trace_ref(ipif_t *ipif)
5209 {
5210 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5211 
5212 	if (ipif->ipif_trace_disable)
5213 		return;
5214 
5215 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
5216 		ipif->ipif_trace_disable = B_TRUE;
5217 		ipif_trace_cleanup(ipif);
5218 	}
5219 }
5220 
5221 void
ipif_untrace_ref(ipif_t * ipif)5222 ipif_untrace_ref(ipif_t *ipif)
5223 {
5224 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5225 
5226 	if (!ipif->ipif_trace_disable)
5227 		th_trace_unref(ipif);
5228 }
5229 
5230 void
ill_trace_ref(ill_t * ill)5231 ill_trace_ref(ill_t *ill)
5232 {
5233 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5234 
5235 	if (ill->ill_trace_disable)
5236 		return;
5237 
5238 	if (!th_trace_ref(ill, ill->ill_ipst)) {
5239 		ill->ill_trace_disable = B_TRUE;
5240 		ill_trace_cleanup(ill);
5241 	}
5242 }
5243 
5244 void
ill_untrace_ref(ill_t * ill)5245 ill_untrace_ref(ill_t *ill)
5246 {
5247 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5248 
5249 	if (!ill->ill_trace_disable)
5250 		th_trace_unref(ill);
5251 }
5252 
5253 /*
5254  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
5255  * failure, ipif_trace_disable is set.
5256  */
5257 static void
ipif_trace_cleanup(const ipif_t * ipif)5258 ipif_trace_cleanup(const ipif_t *ipif)
5259 {
5260 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
5261 }
5262 
5263 /*
5264  * Called when ill is unplumbed or when memory alloc fails.  Note that on
5265  * failure, ill_trace_disable is set.
5266  */
5267 static void
ill_trace_cleanup(const ill_t * ill)5268 ill_trace_cleanup(const ill_t *ill)
5269 {
5270 	th_trace_cleanup(ill, ill->ill_trace_disable);
5271 }
5272 #endif /* DEBUG */
5273 
5274 void
ipif_refhold_locked(ipif_t * ipif)5275 ipif_refhold_locked(ipif_t *ipif)
5276 {
5277 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5278 	ipif->ipif_refcnt++;
5279 	IPIF_TRACE_REF(ipif);
5280 }
5281 
5282 void
ipif_refhold(ipif_t * ipif)5283 ipif_refhold(ipif_t *ipif)
5284 {
5285 	ill_t	*ill;
5286 
5287 	ill = ipif->ipif_ill;
5288 	mutex_enter(&ill->ill_lock);
5289 	ipif->ipif_refcnt++;
5290 	IPIF_TRACE_REF(ipif);
5291 	mutex_exit(&ill->ill_lock);
5292 }
5293 
5294 /*
5295  * Must not be called while holding any locks. Otherwise if this is
5296  * the last reference to be released there is a chance of recursive mutex
5297  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5298  * to restart an ioctl.
5299  */
5300 void
ipif_refrele(ipif_t * ipif)5301 ipif_refrele(ipif_t *ipif)
5302 {
5303 	ill_t	*ill;
5304 
5305 	ill = ipif->ipif_ill;
5306 
5307 	mutex_enter(&ill->ill_lock);
5308 	ASSERT(ipif->ipif_refcnt != 0);
5309 	ipif->ipif_refcnt--;
5310 	IPIF_UNTRACE_REF(ipif);
5311 	if (ipif->ipif_refcnt != 0) {
5312 		mutex_exit(&ill->ill_lock);
5313 		return;
5314 	}
5315 
5316 	/* Drops the ill_lock */
5317 	ipif_ill_refrele_tail(ill);
5318 }
5319 
5320 ipif_t *
ipif_get_next_ipif(ipif_t * curr,ill_t * ill)5321 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
5322 {
5323 	ipif_t	*ipif;
5324 
5325 	mutex_enter(&ill->ill_lock);
5326 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
5327 	    ipif != NULL; ipif = ipif->ipif_next) {
5328 		if (IPIF_IS_CONDEMNED(ipif))
5329 			continue;
5330 		ipif_refhold_locked(ipif);
5331 		mutex_exit(&ill->ill_lock);
5332 		return (ipif);
5333 	}
5334 	mutex_exit(&ill->ill_lock);
5335 	return (NULL);
5336 }
5337 
5338 /*
5339  * TODO: make this table extendible at run time
5340  * Return a pointer to the mac type info for 'mac_type'
5341  */
5342 static ip_m_t *
ip_m_lookup(t_uscalar_t mac_type)5343 ip_m_lookup(t_uscalar_t mac_type)
5344 {
5345 	ip_m_t	*ipm;
5346 
5347 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
5348 		if (ipm->ip_m_mac_type == mac_type)
5349 			return (ipm);
5350 	return (NULL);
5351 }
5352 
5353 /*
5354  * Make a link layer address from the multicast IP address *addr.
5355  * To form the link layer address, invoke the ip_m_v*mapping function
5356  * associated with the link-layer type.
5357  */
5358 void
ip_mcast_mapping(ill_t * ill,uchar_t * addr,uchar_t * hwaddr)5359 ip_mcast_mapping(ill_t *ill, uchar_t *addr, uchar_t *hwaddr)
5360 {
5361 	ip_m_t *ipm;
5362 
5363 	if (ill->ill_net_type == IRE_IF_NORESOLVER)
5364 		return;
5365 
5366 	ASSERT(addr != NULL);
5367 
5368 	ipm = ip_m_lookup(ill->ill_mactype);
5369 	if (ipm == NULL ||
5370 	    (ill->ill_isv6 && ipm->ip_m_v6mapping == NULL) ||
5371 	    (!ill->ill_isv6 && ipm->ip_m_v4mapping == NULL)) {
5372 		ip0dbg(("no mapping for ill %s mactype 0x%x\n",
5373 		    ill->ill_name, ill->ill_mactype));
5374 		return;
5375 	}
5376 	if (ill->ill_isv6)
5377 		(*ipm->ip_m_v6mapping)(ill, addr, hwaddr);
5378 	else
5379 		(*ipm->ip_m_v4mapping)(ill, addr, hwaddr);
5380 }
5381 
5382 /*
5383  * Returns B_FALSE if the IPv4 netmask pointed by `mask' is non-contiguous.
5384  * Otherwise returns B_TRUE.
5385  *
5386  * The netmask can be verified to be contiguous with 32 shifts and or
5387  * operations. Take the contiguous mask (in host byte order) and compute
5388  *	mask | mask << 1 | mask << 2 | ... | mask << 31
5389  * the result will be the same as the 'mask' for contiguous mask.
5390  */
5391 static boolean_t
ip_contiguous_mask(uint32_t mask)5392 ip_contiguous_mask(uint32_t mask)
5393 {
5394 	uint32_t	m = mask;
5395 	int		i;
5396 
5397 	for (i = 1; i < 32; i++)
5398 		m |= (mask << i);
5399 
5400 	return (m == mask);
5401 }
5402 
5403 /*
5404  * ip_rt_add is called to add an IPv4 route to the forwarding table.
5405  * ill is passed in to associate it with the correct interface.
5406  * If ire_arg is set, then we return the held IRE in that location.
5407  */
5408 int
ip_rt_add(ipaddr_t dst_addr,ipaddr_t mask,ipaddr_t gw_addr,ipaddr_t src_addr,int flags,ill_t * ill,ire_t ** ire_arg,boolean_t ioctl_msg,struct rtsa_s * sp,ip_stack_t * ipst,zoneid_t zoneid)5409 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5410     ipaddr_t src_addr, int flags, ill_t *ill, ire_t **ire_arg,
5411     boolean_t ioctl_msg, struct rtsa_s *sp, ip_stack_t *ipst, zoneid_t zoneid)
5412 {
5413 	ire_t	*ire, *nire;
5414 	ire_t	*gw_ire = NULL;
5415 	ipif_t	*ipif = NULL;
5416 	uint_t	type;
5417 	int	match_flags = MATCH_IRE_TYPE;
5418 	tsol_gc_t *gc = NULL;
5419 	tsol_gcgrp_t *gcgrp = NULL;
5420 	boolean_t gcgrp_xtraref = B_FALSE;
5421 	boolean_t cgtp_broadcast;
5422 	boolean_t unbound = B_FALSE;
5423 
5424 	ip1dbg(("ip_rt_add:"));
5425 
5426 	if (ire_arg != NULL)
5427 		*ire_arg = NULL;
5428 
5429 	/* disallow non-contiguous netmasks */
5430 	if (!ip_contiguous_mask(ntohl(mask)))
5431 		return (ENOTSUP);
5432 
5433 	/*
5434 	 * If this is the case of RTF_HOST being set, then we set the netmask
5435 	 * to all ones (regardless if one was supplied).
5436 	 */
5437 	if (flags & RTF_HOST)
5438 		mask = IP_HOST_MASK;
5439 
5440 	/*
5441 	 * Prevent routes with a zero gateway from being created (since
5442 	 * interfaces can currently be plumbed and brought up no assigned
5443 	 * address).
5444 	 */
5445 	if (gw_addr == 0)
5446 		return (ENETUNREACH);
5447 	/*
5448 	 * Get the ipif, if any, corresponding to the gw_addr
5449 	 * If -ifp was specified we restrict ourselves to the ill, otherwise
5450 	 * we match on the gatway and destination to handle unnumbered pt-pt
5451 	 * interfaces.
5452 	 */
5453 	if (ill != NULL)
5454 		ipif = ipif_lookup_addr(gw_addr, ill, ALL_ZONES, ipst);
5455 	else
5456 		ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
5457 	if (ipif != NULL) {
5458 		if (IS_VNI(ipif->ipif_ill)) {
5459 			ipif_refrele(ipif);
5460 			return (EINVAL);
5461 		}
5462 	}
5463 
5464 	/*
5465 	 * GateD will attempt to create routes with a loopback interface
5466 	 * address as the gateway and with RTF_GATEWAY set.  We allow
5467 	 * these routes to be added, but create them as interface routes
5468 	 * since the gateway is an interface address.
5469 	 */
5470 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
5471 		flags &= ~RTF_GATEWAY;
5472 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
5473 		    mask == IP_HOST_MASK) {
5474 			ire = ire_ftable_lookup_v4(dst_addr, 0, 0, IRE_LOOPBACK,
5475 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
5476 			    NULL);
5477 			if (ire != NULL) {
5478 				ire_refrele(ire);
5479 				ipif_refrele(ipif);
5480 				return (EEXIST);
5481 			}
5482 			ip1dbg(("ip_rt_add: 0x%p creating IRE 0x%x"
5483 			    "for 0x%x\n", (void *)ipif,
5484 			    ipif->ipif_ire_type,
5485 			    ntohl(ipif->ipif_lcl_addr)));
5486 			ire = ire_create(
5487 			    (uchar_t *)&dst_addr,	/* dest address */
5488 			    (uchar_t *)&mask,		/* mask */
5489 			    NULL,			/* no gateway */
5490 			    ipif->ipif_ire_type,	/* LOOPBACK */
5491 			    ipif->ipif_ill,
5492 			    zoneid,
5493 			    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0,
5494 			    NULL,
5495 			    ipst);
5496 
5497 			if (ire == NULL) {
5498 				ipif_refrele(ipif);
5499 				return (ENOMEM);
5500 			}
5501 			/* src address assigned by the caller? */
5502 			if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5503 				ire->ire_setsrc_addr = src_addr;
5504 
5505 			nire = ire_add(ire);
5506 			if (nire == NULL) {
5507 				/*
5508 				 * In the result of failure, ire_add() will have
5509 				 * already deleted the ire in question, so there
5510 				 * is no need to do that here.
5511 				 */
5512 				ipif_refrele(ipif);
5513 				return (ENOMEM);
5514 			}
5515 			/*
5516 			 * Check if it was a duplicate entry. This handles
5517 			 * the case of two racing route adds for the same route
5518 			 */
5519 			if (nire != ire) {
5520 				ASSERT(nire->ire_identical_ref > 1);
5521 				ire_delete(nire);
5522 				ire_refrele(nire);
5523 				ipif_refrele(ipif);
5524 				return (EEXIST);
5525 			}
5526 			ire = nire;
5527 			goto save_ire;
5528 		}
5529 	}
5530 
5531 	/*
5532 	 * The routes for multicast with CGTP are quite special in that
5533 	 * the gateway is the local interface address, yet RTF_GATEWAY
5534 	 * is set. We turn off RTF_GATEWAY to provide compatibility with
5535 	 * this undocumented and unusual use of multicast routes.
5536 	 */
5537 	if ((flags & RTF_MULTIRT) && ipif != NULL)
5538 		flags &= ~RTF_GATEWAY;
5539 
5540 	/*
5541 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
5542 	 * and the gateway address provided is one of the system's interface
5543 	 * addresses.  By using the routing socket interface and supplying an
5544 	 * RTA_IFP sockaddr with an interface index, an alternate method of
5545 	 * specifying an interface route to be created is available which uses
5546 	 * the interface index that specifies the outgoing interface rather than
5547 	 * the address of an outgoing interface (which may not be able to
5548 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
5549 	 * flag, routes can be specified which not only specify the next-hop to
5550 	 * be used when routing to a certain prefix, but also which outgoing
5551 	 * interface should be used.
5552 	 *
5553 	 * Previously, interfaces would have unique addresses assigned to them
5554 	 * and so the address assigned to a particular interface could be used
5555 	 * to identify a particular interface.  One exception to this was the
5556 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
5557 	 *
5558 	 * With the advent of IPv6 and its link-local addresses, this
5559 	 * restriction was relaxed and interfaces could share addresses between
5560 	 * themselves.  In fact, typically all of the link-local interfaces on
5561 	 * an IPv6 node or router will have the same link-local address.  In
5562 	 * order to differentiate between these interfaces, the use of an
5563 	 * interface index is necessary and this index can be carried inside a
5564 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
5565 	 * of using the interface index, however, is that all of the ipif's that
5566 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
5567 	 * cannot be used to differentiate between ipif's (or logical
5568 	 * interfaces) that belong to the same ill (physical interface).
5569 	 *
5570 	 * For example, in the following case involving IPv4 interfaces and
5571 	 * logical interfaces
5572 	 *
5573 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
5574 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0
5575 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0
5576 	 *
5577 	 * the ipif's corresponding to each of these interface routes can be
5578 	 * uniquely identified by the "gateway" (actually interface address).
5579 	 *
5580 	 * In this case involving multiple IPv6 default routes to a particular
5581 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
5582 	 * default route is of interest:
5583 	 *
5584 	 *	default		fe80::123:4567:89ab:cdef	U	if0
5585 	 *	default		fe80::123:4567:89ab:cdef	U	if1
5586 	 */
5587 
5588 	/* RTF_GATEWAY not set */
5589 	if (!(flags & RTF_GATEWAY)) {
5590 		if (sp != NULL) {
5591 			ip2dbg(("ip_rt_add: gateway security attributes "
5592 			    "cannot be set with interface route\n"));
5593 			if (ipif != NULL)
5594 				ipif_refrele(ipif);
5595 			return (EINVAL);
5596 		}
5597 
5598 		/*
5599 		 * Whether or not ill (RTA_IFP) is set, we require that
5600 		 * the gateway is one of our local addresses.
5601 		 */
5602 		if (ipif == NULL)
5603 			return (ENETUNREACH);
5604 
5605 		/*
5606 		 * We use MATCH_IRE_ILL here. If the caller specified an
5607 		 * interface (from the RTA_IFP sockaddr) we use it, otherwise
5608 		 * we use the ill derived from the gateway address.
5609 		 * We can always match the gateway address since we record it
5610 		 * in ire_gateway_addr.
5611 		 * We don't allow RTA_IFP to specify a different ill than the
5612 		 * one matching the ipif to make sure we can delete the route.
5613 		 */
5614 		match_flags |= MATCH_IRE_GW | MATCH_IRE_ILL;
5615 		if (ill == NULL) {
5616 			ill = ipif->ipif_ill;
5617 		} else if (ill != ipif->ipif_ill) {
5618 			ipif_refrele(ipif);
5619 			return (EINVAL);
5620 		}
5621 
5622 		/*
5623 		 * We check for an existing entry at this point.
5624 		 *
5625 		 * Since a netmask isn't passed in via the ioctl interface
5626 		 * (SIOCADDRT), we don't check for a matching netmask in that
5627 		 * case.
5628 		 */
5629 		if (!ioctl_msg)
5630 			match_flags |= MATCH_IRE_MASK;
5631 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
5632 		    IRE_INTERFACE, ill, ALL_ZONES, NULL, match_flags, 0, ipst,
5633 		    NULL);
5634 		if (ire != NULL) {
5635 			ire_refrele(ire);
5636 			ipif_refrele(ipif);
5637 			return (EEXIST);
5638 		}
5639 
5640 		/*
5641 		 * Some software (for example, GateD and Sun Cluster) attempts
5642 		 * to create (what amount to) IRE_PREFIX routes with the
5643 		 * loopback address as the gateway.  This is primarily done to
5644 		 * set up prefixes with the RTF_REJECT flag set (for example,
5645 		 * when generating aggregate routes.)
5646 		 *
5647 		 * If the IRE type (as defined by ill->ill_net_type) would be
5648 		 * IRE_LOOPBACK, then we map the request into a
5649 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
5650 		 * these interface routes, by definition, can only be that.
5651 		 *
5652 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
5653 		 * routine, but rather using ire_create() directly.
5654 		 *
5655 		 */
5656 		type = ill->ill_net_type;
5657 		if (type == IRE_LOOPBACK) {
5658 			type = IRE_IF_NORESOLVER;
5659 			flags |= RTF_BLACKHOLE;
5660 		}
5661 
5662 		/*
5663 		 * Create a copy of the IRE_IF_NORESOLVER or
5664 		 * IRE_IF_RESOLVER with the modified address, netmask, and
5665 		 * gateway.
5666 		 */
5667 		ire = ire_create(
5668 		    (uchar_t *)&dst_addr,
5669 		    (uint8_t *)&mask,
5670 		    (uint8_t *)&gw_addr,
5671 		    type,
5672 		    ill,
5673 		    zoneid,
5674 		    flags,
5675 		    NULL,
5676 		    ipst);
5677 		if (ire == NULL) {
5678 			ipif_refrele(ipif);
5679 			return (ENOMEM);
5680 		}
5681 
5682 		/* src address assigned by the caller? */
5683 		if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5684 			ire->ire_setsrc_addr = src_addr;
5685 
5686 		nire = ire_add(ire);
5687 		if (nire == NULL) {
5688 			/*
5689 			 * In the result of failure, ire_add() will have
5690 			 * already deleted the ire in question, so there
5691 			 * is no need to do that here.
5692 			 */
5693 			ipif_refrele(ipif);
5694 			return (ENOMEM);
5695 		}
5696 		/*
5697 		 * Check if it was a duplicate entry. This handles
5698 		 * the case of two racing route adds for the same route
5699 		 */
5700 		if (nire != ire) {
5701 			ire_delete(nire);
5702 			ire_refrele(nire);
5703 			ipif_refrele(ipif);
5704 			return (EEXIST);
5705 		}
5706 		ire = nire;
5707 		goto save_ire;
5708 	}
5709 
5710 	/*
5711 	 * Get an interface IRE for the specified gateway.
5712 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
5713 	 * gateway, it is currently unreachable and we fail the request
5714 	 * accordingly. We reject any RTF_GATEWAY routes where the gateway
5715 	 * is an IRE_LOCAL or IRE_LOOPBACK.
5716 	 * If RTA_IFP was specified we look on that particular ill.
5717 	 */
5718 	if (ill != NULL)
5719 		match_flags |= MATCH_IRE_ILL;
5720 
5721 	/* Check whether the gateway is reachable. */
5722 again:
5723 	type = IRE_INTERFACE | IRE_LOCAL | IRE_LOOPBACK;
5724 	if (flags & RTF_INDIRECT)
5725 		type |= IRE_OFFLINK;
5726 
5727 	gw_ire = ire_ftable_lookup_v4(gw_addr, 0, 0, type, ill,
5728 	    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
5729 	if (gw_ire == NULL) {
5730 		/*
5731 		 * With IPMP, we allow host routes to influence in.mpathd's
5732 		 * target selection.  However, if the test addresses are on
5733 		 * their own network, the above lookup will fail since the
5734 		 * underlying IRE_INTERFACEs are marked hidden.  So allow
5735 		 * hidden test IREs to be found and try again.
5736 		 */
5737 		if (!(match_flags & MATCH_IRE_TESTHIDDEN))  {
5738 			match_flags |= MATCH_IRE_TESTHIDDEN;
5739 			goto again;
5740 		}
5741 		if (ipif != NULL)
5742 			ipif_refrele(ipif);
5743 		return (ENETUNREACH);
5744 	}
5745 	if (gw_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) {
5746 		ire_refrele(gw_ire);
5747 		if (ipif != NULL)
5748 			ipif_refrele(ipif);
5749 		return (ENETUNREACH);
5750 	}
5751 
5752 	if (ill == NULL && !(flags & RTF_INDIRECT)) {
5753 		unbound = B_TRUE;
5754 		if (ipst->ips_ip_strict_src_multihoming > 0)
5755 			ill = gw_ire->ire_ill;
5756 	}
5757 
5758 	/*
5759 	 * We create one of three types of IREs as a result of this request
5760 	 * based on the netmask.  A netmask of all ones (which is automatically
5761 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
5762 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
5763 	 * created.  Otherwise, an IRE_PREFIX route is created for the
5764 	 * destination prefix.
5765 	 */
5766 	if (mask == IP_HOST_MASK)
5767 		type = IRE_HOST;
5768 	else if (mask == 0)
5769 		type = IRE_DEFAULT;
5770 	else
5771 		type = IRE_PREFIX;
5772 
5773 	/* check for a duplicate entry */
5774 	ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
5775 	    ALL_ZONES, NULL, match_flags | MATCH_IRE_MASK | MATCH_IRE_GW,
5776 	    0, ipst, NULL);
5777 	if (ire != NULL) {
5778 		if (ipif != NULL)
5779 			ipif_refrele(ipif);
5780 		ire_refrele(gw_ire);
5781 		ire_refrele(ire);
5782 		return (EEXIST);
5783 	}
5784 
5785 	/* Security attribute exists */
5786 	if (sp != NULL) {
5787 		tsol_gcgrp_addr_t ga;
5788 
5789 		/* find or create the gateway credentials group */
5790 		ga.ga_af = AF_INET;
5791 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
5792 
5793 		/* we hold reference to it upon success */
5794 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
5795 		if (gcgrp == NULL) {
5796 			if (ipif != NULL)
5797 				ipif_refrele(ipif);
5798 			ire_refrele(gw_ire);
5799 			return (ENOMEM);
5800 		}
5801 
5802 		/*
5803 		 * Create and add the security attribute to the group; a
5804 		 * reference to the group is made upon allocating a new
5805 		 * entry successfully.  If it finds an already-existing
5806 		 * entry for the security attribute in the group, it simply
5807 		 * returns it and no new reference is made to the group.
5808 		 */
5809 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
5810 		if (gc == NULL) {
5811 			if (ipif != NULL)
5812 				ipif_refrele(ipif);
5813 			/* release reference held by gcgrp_lookup */
5814 			GCGRP_REFRELE(gcgrp);
5815 			ire_refrele(gw_ire);
5816 			return (ENOMEM);
5817 		}
5818 	}
5819 
5820 	/* Create the IRE. */
5821 	ire = ire_create(
5822 	    (uchar_t *)&dst_addr,		/* dest address */
5823 	    (uchar_t *)&mask,			/* mask */
5824 	    (uchar_t *)&gw_addr,		/* gateway address */
5825 	    (ushort_t)type,			/* IRE type */
5826 	    ill,
5827 	    zoneid,
5828 	    flags,
5829 	    gc,					/* security attribute */
5830 	    ipst);
5831 
5832 	/*
5833 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
5834 	 * reference to the 'gcgrp'. We can now release the extra reference
5835 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
5836 	 */
5837 	if (gcgrp_xtraref)
5838 		GCGRP_REFRELE(gcgrp);
5839 	if (ire == NULL) {
5840 		if (gc != NULL)
5841 			GC_REFRELE(gc);
5842 		if (ipif != NULL)
5843 			ipif_refrele(ipif);
5844 		ire_refrele(gw_ire);
5845 		return (ENOMEM);
5846 	}
5847 
5848 	/* Before we add, check if an extra CGTP broadcast is needed */
5849 	cgtp_broadcast = ((flags & RTF_MULTIRT) &&
5850 	    ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST);
5851 
5852 	/* src address assigned by the caller? */
5853 	if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5854 		ire->ire_setsrc_addr = src_addr;
5855 
5856 	ire->ire_unbound = unbound;
5857 
5858 	/*
5859 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
5860 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
5861 	 */
5862 
5863 	/* Add the new IRE. */
5864 	nire = ire_add(ire);
5865 	if (nire == NULL) {
5866 		/*
5867 		 * In the result of failure, ire_add() will have
5868 		 * already deleted the ire in question, so there
5869 		 * is no need to do that here.
5870 		 */
5871 		if (ipif != NULL)
5872 			ipif_refrele(ipif);
5873 		ire_refrele(gw_ire);
5874 		return (ENOMEM);
5875 	}
5876 	/*
5877 	 * Check if it was a duplicate entry. This handles
5878 	 * the case of two racing route adds for the same route
5879 	 */
5880 	if (nire != ire) {
5881 		ire_delete(nire);
5882 		ire_refrele(nire);
5883 		if (ipif != NULL)
5884 			ipif_refrele(ipif);
5885 		ire_refrele(gw_ire);
5886 		return (EEXIST);
5887 	}
5888 	ire = nire;
5889 
5890 	if (flags & RTF_MULTIRT) {
5891 		/*
5892 		 * Invoke the CGTP (multirouting) filtering module
5893 		 * to add the dst address in the filtering database.
5894 		 * Replicated inbound packets coming from that address
5895 		 * will be filtered to discard the duplicates.
5896 		 * It is not necessary to call the CGTP filter hook
5897 		 * when the dst address is a broadcast or multicast,
5898 		 * because an IP source address cannot be a broadcast
5899 		 * or a multicast.
5900 		 */
5901 		if (cgtp_broadcast) {
5902 			ip_cgtp_bcast_add(ire, ipst);
5903 			goto save_ire;
5904 		}
5905 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
5906 		    !CLASSD(ire->ire_addr)) {
5907 			int res;
5908 			ipif_t *src_ipif;
5909 
5910 			/* Find the source address corresponding to gw_ire */
5911 			src_ipif = ipif_lookup_addr(gw_ire->ire_gateway_addr,
5912 			    NULL, zoneid, ipst);
5913 			if (src_ipif != NULL) {
5914 				res = ipst->ips_ip_cgtp_filter_ops->
5915 				    cfo_add_dest_v4(
5916 				    ipst->ips_netstack->netstack_stackid,
5917 				    ire->ire_addr,
5918 				    ire->ire_gateway_addr,
5919 				    ire->ire_setsrc_addr,
5920 				    src_ipif->ipif_lcl_addr);
5921 				ipif_refrele(src_ipif);
5922 			} else {
5923 				res = EADDRNOTAVAIL;
5924 			}
5925 			if (res != 0) {
5926 				if (ipif != NULL)
5927 					ipif_refrele(ipif);
5928 				ire_refrele(gw_ire);
5929 				ire_delete(ire);
5930 				ire_refrele(ire);	/* Held in ire_add */
5931 				return (res);
5932 			}
5933 		}
5934 	}
5935 
5936 save_ire:
5937 	if (gw_ire != NULL) {
5938 		ire_refrele(gw_ire);
5939 		gw_ire = NULL;
5940 	}
5941 	if (ill != NULL) {
5942 		/*
5943 		 * Save enough information so that we can recreate the IRE if
5944 		 * the interface goes down and then up.  The metrics associated
5945 		 * with the route will be saved as well when rts_setmetrics() is
5946 		 * called after the IRE has been created.  In the case where
5947 		 * memory cannot be allocated, none of this information will be
5948 		 * saved.
5949 		 */
5950 		ill_save_ire(ill, ire);
5951 	}
5952 	if (ioctl_msg)
5953 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
5954 	if (ire_arg != NULL) {
5955 		/*
5956 		 * Store the ire that was successfully added into where ire_arg
5957 		 * points to so that callers don't have to look it up
5958 		 * themselves (but they are responsible for ire_refrele()ing
5959 		 * the ire when they are finished with it).
5960 		 */
5961 		*ire_arg = ire;
5962 	} else {
5963 		ire_refrele(ire);		/* Held in ire_add */
5964 	}
5965 	if (ipif != NULL)
5966 		ipif_refrele(ipif);
5967 	return (0);
5968 }
5969 
5970 /*
5971  * ip_rt_delete is called to delete an IPv4 route.
5972  * ill is passed in to associate it with the correct interface.
5973  */
5974 /* ARGSUSED4 */
5975 int
ip_rt_delete(ipaddr_t dst_addr,ipaddr_t mask,ipaddr_t gw_addr,uint_t rtm_addrs,int flags,ill_t * ill,boolean_t ioctl_msg,ip_stack_t * ipst,zoneid_t zoneid)5976 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5977     uint_t rtm_addrs, int flags, ill_t *ill, boolean_t ioctl_msg,
5978     ip_stack_t *ipst, zoneid_t zoneid)
5979 {
5980 	ire_t	*ire = NULL;
5981 	ipif_t	*ipif;
5982 	uint_t	type;
5983 	uint_t	match_flags = MATCH_IRE_TYPE;
5984 	int	err = 0;
5985 
5986 	ip1dbg(("ip_rt_delete:"));
5987 	/*
5988 	 * If this is the case of RTF_HOST being set, then we set the netmask
5989 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
5990 	 */
5991 	if (flags & RTF_HOST) {
5992 		mask = IP_HOST_MASK;
5993 		match_flags |= MATCH_IRE_MASK;
5994 	} else if (rtm_addrs & RTA_NETMASK) {
5995 		match_flags |= MATCH_IRE_MASK;
5996 	}
5997 
5998 	/*
5999 	 * Note that RTF_GATEWAY is never set on a delete, therefore
6000 	 * we check if the gateway address is one of our interfaces first,
6001 	 * and fall back on RTF_GATEWAY routes.
6002 	 *
6003 	 * This makes it possible to delete an original
6004 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
6005 	 * However, we have RTF_KERNEL set on the ones created by ipif_up
6006 	 * and those can not be deleted here.
6007 	 *
6008 	 * We use MATCH_IRE_ILL if we know the interface. If the caller
6009 	 * specified an interface (from the RTA_IFP sockaddr) we use it,
6010 	 * otherwise we use the ill derived from the gateway address.
6011 	 * We can always match the gateway address since we record it
6012 	 * in ire_gateway_addr.
6013 	 *
6014 	 * For more detail on specifying routes by gateway address and by
6015 	 * interface index, see the comments in ip_rt_add().
6016 	 */
6017 	ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
6018 	if (ipif != NULL) {
6019 		ill_t	*ill_match;
6020 
6021 		if (ill != NULL)
6022 			ill_match = ill;
6023 		else
6024 			ill_match = ipif->ipif_ill;
6025 
6026 		match_flags |= MATCH_IRE_ILL;
6027 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
6028 			ire = ire_ftable_lookup_v4(dst_addr, mask, 0,
6029 			    IRE_LOOPBACK, ill_match, ALL_ZONES, NULL,
6030 			    match_flags, 0, ipst, NULL);
6031 		}
6032 		if (ire == NULL) {
6033 			match_flags |= MATCH_IRE_GW;
6034 			ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
6035 			    IRE_INTERFACE, ill_match, ALL_ZONES, NULL,
6036 			    match_flags, 0, ipst, NULL);
6037 		}
6038 		/* Avoid deleting routes created by kernel from an ipif */
6039 		if (ire != NULL && (ire->ire_flags & RTF_KERNEL)) {
6040 			ire_refrele(ire);
6041 			ire = NULL;
6042 		}
6043 
6044 		/* Restore in case we didn't find a match */
6045 		match_flags &= ~(MATCH_IRE_GW|MATCH_IRE_ILL);
6046 	}
6047 
6048 	if (ire == NULL) {
6049 		/*
6050 		 * At this point, the gateway address is not one of our own
6051 		 * addresses or a matching interface route was not found.  We
6052 		 * set the IRE type to lookup based on whether
6053 		 * this is a host route, a default route or just a prefix.
6054 		 *
6055 		 * If an ill was passed in, then the lookup is based on an
6056 		 * interface index so MATCH_IRE_ILL is added to match_flags.
6057 		 */
6058 		match_flags |= MATCH_IRE_GW;
6059 		if (ill != NULL)
6060 			match_flags |= MATCH_IRE_ILL;
6061 		if (mask == IP_HOST_MASK)
6062 			type = IRE_HOST;
6063 		else if (mask == 0)
6064 			type = IRE_DEFAULT;
6065 		else
6066 			type = IRE_PREFIX;
6067 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
6068 		    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
6069 	}
6070 
6071 	if (ipif != NULL) {
6072 		ipif_refrele(ipif);
6073 		ipif = NULL;
6074 	}
6075 
6076 	if (ire == NULL)
6077 		return (ESRCH);
6078 
6079 	if (ire->ire_flags & RTF_MULTIRT) {
6080 		/*
6081 		 * Invoke the CGTP (multirouting) filtering module
6082 		 * to remove the dst address from the filtering database.
6083 		 * Packets coming from that address will no longer be
6084 		 * filtered to remove duplicates.
6085 		 */
6086 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
6087 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
6088 			    ipst->ips_netstack->netstack_stackid,
6089 			    ire->ire_addr, ire->ire_gateway_addr);
6090 		}
6091 		ip_cgtp_bcast_delete(ire, ipst);
6092 	}
6093 
6094 	ill = ire->ire_ill;
6095 	if (ill != NULL)
6096 		ill_remove_saved_ire(ill, ire);
6097 	if (ioctl_msg)
6098 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
6099 	ire_delete(ire);
6100 	ire_refrele(ire);
6101 	return (err);
6102 }
6103 
6104 /*
6105  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
6106  */
6107 /* ARGSUSED */
6108 int
ip_siocaddrt(ipif_t * dummy_ipif,sin_t * dummy_sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * dummy_if_req)6109 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6110     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6111 {
6112 	ipaddr_t dst_addr;
6113 	ipaddr_t gw_addr;
6114 	ipaddr_t mask;
6115 	int error = 0;
6116 	mblk_t *mp1;
6117 	struct rtentry *rt;
6118 	ipif_t *ipif = NULL;
6119 	ip_stack_t	*ipst;
6120 
6121 	ASSERT(q->q_next == NULL);
6122 	ipst = CONNQ_TO_IPST(q);
6123 
6124 	ip1dbg(("ip_siocaddrt:"));
6125 	/* Existence of mp1 verified in ip_wput_nondata */
6126 	mp1 = mp->b_cont->b_cont;
6127 	rt = (struct rtentry *)mp1->b_rptr;
6128 
6129 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6130 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6131 
6132 	/*
6133 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
6134 	 * to a particular host address.  In this case, we set the netmask to
6135 	 * all ones for the particular destination address.  Otherwise,
6136 	 * determine the netmask to be used based on dst_addr and the interfaces
6137 	 * in use.
6138 	 */
6139 	if (rt->rt_flags & RTF_HOST) {
6140 		mask = IP_HOST_MASK;
6141 	} else {
6142 		/*
6143 		 * Note that ip_subnet_mask returns a zero mask in the case of
6144 		 * default (an all-zeroes address).
6145 		 */
6146 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6147 	}
6148 
6149 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
6150 	    B_TRUE, NULL, ipst, ALL_ZONES);
6151 	if (ipif != NULL)
6152 		ipif_refrele(ipif);
6153 	return (error);
6154 }
6155 
6156 /*
6157  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
6158  */
6159 /* ARGSUSED */
6160 int
ip_siocdelrt(ipif_t * dummy_ipif,sin_t * dummy_sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * dummy_if_req)6161 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6162     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6163 {
6164 	ipaddr_t dst_addr;
6165 	ipaddr_t gw_addr;
6166 	ipaddr_t mask;
6167 	int error;
6168 	mblk_t *mp1;
6169 	struct rtentry *rt;
6170 	ipif_t *ipif = NULL;
6171 	ip_stack_t	*ipst;
6172 
6173 	ASSERT(q->q_next == NULL);
6174 	ipst = CONNQ_TO_IPST(q);
6175 
6176 	ip1dbg(("ip_siocdelrt:"));
6177 	/* Existence of mp1 verified in ip_wput_nondata */
6178 	mp1 = mp->b_cont->b_cont;
6179 	rt = (struct rtentry *)mp1->b_rptr;
6180 
6181 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6182 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6183 
6184 	/*
6185 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
6186 	 * to a particular host address.  In this case, we set the netmask to
6187 	 * all ones for the particular destination address.  Otherwise,
6188 	 * determine the netmask to be used based on dst_addr and the interfaces
6189 	 * in use.
6190 	 */
6191 	if (rt->rt_flags & RTF_HOST) {
6192 		mask = IP_HOST_MASK;
6193 	} else {
6194 		/*
6195 		 * Note that ip_subnet_mask returns a zero mask in the case of
6196 		 * default (an all-zeroes address).
6197 		 */
6198 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6199 	}
6200 
6201 	error = ip_rt_delete(dst_addr, mask, gw_addr,
6202 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE,
6203 	    ipst, ALL_ZONES);
6204 	if (ipif != NULL)
6205 		ipif_refrele(ipif);
6206 	return (error);
6207 }
6208 
6209 /*
6210  * Enqueue the mp onto the ipsq, chained by b_next.
6211  * b_prev stores the function to be executed later, and b_queue the queue
6212  * where this mp originated.
6213  */
6214 void
ipsq_enq(ipsq_t * ipsq,queue_t * q,mblk_t * mp,ipsq_func_t func,int type,ill_t * pending_ill)6215 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6216     ill_t *pending_ill)
6217 {
6218 	conn_t	*connp;
6219 	ipxop_t *ipx = ipsq->ipsq_xop;
6220 
6221 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
6222 	ASSERT(MUTEX_HELD(&ipx->ipx_lock));
6223 	ASSERT(func != NULL);
6224 
6225 	mp->b_queue = q;
6226 	mp->b_prev = (void *)func;
6227 	mp->b_next = NULL;
6228 
6229 	switch (type) {
6230 	case CUR_OP:
6231 		if (ipx->ipx_mptail != NULL) {
6232 			ASSERT(ipx->ipx_mphead != NULL);
6233 			ipx->ipx_mptail->b_next = mp;
6234 		} else {
6235 			ASSERT(ipx->ipx_mphead == NULL);
6236 			ipx->ipx_mphead = mp;
6237 		}
6238 		ipx->ipx_mptail = mp;
6239 		break;
6240 
6241 	case NEW_OP:
6242 		if (ipsq->ipsq_xopq_mptail != NULL) {
6243 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
6244 			ipsq->ipsq_xopq_mptail->b_next = mp;
6245 		} else {
6246 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
6247 			ipsq->ipsq_xopq_mphead = mp;
6248 		}
6249 		ipsq->ipsq_xopq_mptail = mp;
6250 		ipx->ipx_ipsq_queued = B_TRUE;
6251 		break;
6252 
6253 	case SWITCH_OP:
6254 		ASSERT(ipsq->ipsq_swxop != NULL);
6255 		/* only one switch operation is currently allowed */
6256 		ASSERT(ipsq->ipsq_switch_mp == NULL);
6257 		ipsq->ipsq_switch_mp = mp;
6258 		ipx->ipx_ipsq_queued = B_TRUE;
6259 		break;
6260 	default:
6261 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
6262 	}
6263 
6264 	if (CONN_Q(q) && pending_ill != NULL) {
6265 		connp = Q_TO_CONN(q);
6266 		ASSERT(MUTEX_HELD(&connp->conn_lock));
6267 		connp->conn_oper_pending_ill = pending_ill;
6268 	}
6269 }
6270 
6271 /*
6272  * Dequeue the next message that requested exclusive access to this IPSQ's
6273  * xop.  Specifically:
6274  *
6275  *  1. If we're still processing the current operation on `ipsq', then
6276  *     dequeue the next message for the operation (from ipx_mphead), or
6277  *     return NULL if there are no queued messages for the operation.
6278  *     These messages are queued via CUR_OP to qwriter_ip() and friends.
6279  *
6280  *  2. If the current operation on `ipsq' has completed (ipx_current_ipif is
6281  *     not set) see if the ipsq has requested an xop switch.  If so, switch
6282  *     `ipsq' to a different xop.  Xop switches only happen when joining or
6283  *     leaving IPMP groups and require a careful dance -- see the comments
6284  *     in-line below for details.  If we're leaving a group xop or if we're
6285  *     joining a group xop and become writer on it, then we proceed to (3).
6286  *     Otherwise, we return NULL and exit the xop.
6287  *
6288  *  3. For each IPSQ in the xop, return any switch operation stored on
6289  *     ipsq_switch_mp (set via SWITCH_OP); these must be processed before
6290  *     any other messages queued on the IPSQ.  Otherwise, dequeue the next
6291  *     exclusive operation (queued via NEW_OP) stored on ipsq_xopq_mphead.
6292  *     Note that if the phyint tied to `ipsq' is not using IPMP there will
6293  *     only be one IPSQ in the xop.  Otherwise, there will be one IPSQ for
6294  *     each phyint in the group, including the IPMP meta-interface phyint.
6295  */
6296 static mblk_t *
ipsq_dq(ipsq_t * ipsq)6297 ipsq_dq(ipsq_t *ipsq)
6298 {
6299 	ill_t	*illv4, *illv6;
6300 	mblk_t	*mp;
6301 	ipsq_t	*xopipsq;
6302 	ipsq_t	*leftipsq = NULL;
6303 	ipxop_t *ipx;
6304 	phyint_t *phyi = ipsq->ipsq_phyint;
6305 	ip_stack_t *ipst = ipsq->ipsq_ipst;
6306 	boolean_t emptied = B_FALSE;
6307 
6308 	/*
6309 	 * Grab all the locks we need in the defined order (ill_g_lock ->
6310 	 * ipsq_lock -> ipx_lock); ill_g_lock is needed to use ipsq_next.
6311 	 */
6312 	rw_enter(&ipst->ips_ill_g_lock,
6313 	    ipsq->ipsq_swxop != NULL ? RW_WRITER : RW_READER);
6314 	mutex_enter(&ipsq->ipsq_lock);
6315 	ipx = ipsq->ipsq_xop;
6316 	mutex_enter(&ipx->ipx_lock);
6317 
6318 	/*
6319 	 * Dequeue the next message associated with the current exclusive
6320 	 * operation, if any.
6321 	 */
6322 	if ((mp = ipx->ipx_mphead) != NULL) {
6323 		ipx->ipx_mphead = mp->b_next;
6324 		if (ipx->ipx_mphead == NULL)
6325 			ipx->ipx_mptail = NULL;
6326 		mp->b_next = (void *)ipsq;
6327 		goto out;
6328 	}
6329 
6330 	if (ipx->ipx_current_ipif != NULL)
6331 		goto empty;
6332 
6333 	if (ipsq->ipsq_swxop != NULL) {
6334 		/*
6335 		 * The exclusive operation that is now being completed has
6336 		 * requested a switch to a different xop.  This happens
6337 		 * when an interface joins or leaves an IPMP group.  Joins
6338 		 * happen through SIOCSLIFGROUPNAME (ip_sioctl_groupname()).
6339 		 * Leaves happen via SIOCSLIFGROUPNAME, interface unplumb
6340 		 * (phyint_free()), or interface plumb for an ill type
6341 		 * not in the IPMP group (ip_rput_dlpi_writer()).
6342 		 *
6343 		 * Xop switches are not allowed on the IPMP meta-interface.
6344 		 */
6345 		ASSERT(phyi == NULL || !(phyi->phyint_flags & PHYI_IPMP));
6346 		ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
6347 		DTRACE_PROBE1(ipsq__switch, (ipsq_t *), ipsq);
6348 
6349 		if (ipsq->ipsq_swxop == &ipsq->ipsq_ownxop) {
6350 			/*
6351 			 * We're switching back to our own xop, so we have two
6352 			 * xop's to drain/exit: our own, and the group xop
6353 			 * that we are leaving.
6354 			 *
6355 			 * First, pull ourselves out of the group ipsq list.
6356 			 * This is safe since we're writer on ill_g_lock.
6357 			 */
6358 			ASSERT(ipsq->ipsq_xop != &ipsq->ipsq_ownxop);
6359 
6360 			xopipsq = ipx->ipx_ipsq;
6361 			while (xopipsq->ipsq_next != ipsq)
6362 				xopipsq = xopipsq->ipsq_next;
6363 
6364 			xopipsq->ipsq_next = ipsq->ipsq_next;
6365 			ipsq->ipsq_next = ipsq;
6366 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6367 			ipsq->ipsq_swxop = NULL;
6368 
6369 			/*
6370 			 * Second, prepare to exit the group xop.  The actual
6371 			 * ipsq_exit() is done at the end of this function
6372 			 * since we cannot hold any locks across ipsq_exit().
6373 			 * Note that although we drop the group's ipx_lock, no
6374 			 * threads can proceed since we're still ipx_writer.
6375 			 */
6376 			leftipsq = xopipsq;
6377 			mutex_exit(&ipx->ipx_lock);
6378 
6379 			/*
6380 			 * Third, set ipx to point to our own xop (which was
6381 			 * inactive and therefore can be entered).
6382 			 */
6383 			ipx = ipsq->ipsq_xop;
6384 			mutex_enter(&ipx->ipx_lock);
6385 			ASSERT(ipx->ipx_writer == NULL);
6386 			ASSERT(ipx->ipx_current_ipif == NULL);
6387 		} else {
6388 			/*
6389 			 * We're switching from our own xop to a group xop.
6390 			 * The requestor of the switch must ensure that the
6391 			 * group xop cannot go away (e.g. by ensuring the
6392 			 * phyint associated with the xop cannot go away).
6393 			 *
6394 			 * If we can become writer on our new xop, then we'll
6395 			 * do the drain.  Otherwise, the current writer of our
6396 			 * new xop will do the drain when it exits.
6397 			 *
6398 			 * First, splice ourselves into the group IPSQ list.
6399 			 * This is safe since we're writer on ill_g_lock.
6400 			 */
6401 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6402 
6403 			xopipsq = ipsq->ipsq_swxop->ipx_ipsq;
6404 			while (xopipsq->ipsq_next != ipsq->ipsq_swxop->ipx_ipsq)
6405 				xopipsq = xopipsq->ipsq_next;
6406 
6407 			xopipsq->ipsq_next = ipsq;
6408 			ipsq->ipsq_next = ipsq->ipsq_swxop->ipx_ipsq;
6409 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6410 			ipsq->ipsq_swxop = NULL;
6411 
6412 			/*
6413 			 * Second, exit our own xop, since it's now unused.
6414 			 * This is safe since we've got the only reference.
6415 			 */
6416 			ASSERT(ipx->ipx_writer == curthread);
6417 			ipx->ipx_writer = NULL;
6418 			VERIFY(--ipx->ipx_reentry_cnt == 0);
6419 			ipx->ipx_ipsq_queued = B_FALSE;
6420 			mutex_exit(&ipx->ipx_lock);
6421 
6422 			/*
6423 			 * Third, set ipx to point to our new xop, and check
6424 			 * if we can become writer on it.  If we cannot, then
6425 			 * the current writer will drain the IPSQ group when
6426 			 * it exits.  Our ipsq_xop is guaranteed to be stable
6427 			 * because we're still holding ipsq_lock.
6428 			 */
6429 			ipx = ipsq->ipsq_xop;
6430 			mutex_enter(&ipx->ipx_lock);
6431 			if (ipx->ipx_writer != NULL ||
6432 			    ipx->ipx_current_ipif != NULL) {
6433 				goto out;
6434 			}
6435 		}
6436 
6437 		/*
6438 		 * Fourth, become writer on our new ipx before we continue
6439 		 * with the drain.  Note that we never dropped ipsq_lock
6440 		 * above, so no other thread could've raced with us to
6441 		 * become writer first.  Also, we're holding ipx_lock, so
6442 		 * no other thread can examine the ipx right now.
6443 		 */
6444 		ASSERT(ipx->ipx_current_ipif == NULL);
6445 		ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6446 		VERIFY(ipx->ipx_reentry_cnt++ == 0);
6447 		ipx->ipx_writer = curthread;
6448 		ipx->ipx_forced = B_FALSE;
6449 #ifdef DEBUG
6450 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6451 #endif
6452 	}
6453 
6454 	xopipsq = ipsq;
6455 	do {
6456 		/*
6457 		 * So that other operations operate on a consistent and
6458 		 * complete phyint, a switch message on an IPSQ must be
6459 		 * handled prior to any other operations on that IPSQ.
6460 		 */
6461 		if ((mp = xopipsq->ipsq_switch_mp) != NULL) {
6462 			xopipsq->ipsq_switch_mp = NULL;
6463 			ASSERT(mp->b_next == NULL);
6464 			mp->b_next = (void *)xopipsq;
6465 			goto out;
6466 		}
6467 
6468 		if ((mp = xopipsq->ipsq_xopq_mphead) != NULL) {
6469 			xopipsq->ipsq_xopq_mphead = mp->b_next;
6470 			if (xopipsq->ipsq_xopq_mphead == NULL)
6471 				xopipsq->ipsq_xopq_mptail = NULL;
6472 			mp->b_next = (void *)xopipsq;
6473 			goto out;
6474 		}
6475 	} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6476 empty:
6477 	/*
6478 	 * There are no messages.  Further, we are holding ipx_lock, hence no
6479 	 * new messages can end up on any IPSQ in the xop.
6480 	 */
6481 	ipx->ipx_writer = NULL;
6482 	ipx->ipx_forced = B_FALSE;
6483 	VERIFY(--ipx->ipx_reentry_cnt == 0);
6484 	ipx->ipx_ipsq_queued = B_FALSE;
6485 	emptied = B_TRUE;
6486 #ifdef	DEBUG
6487 	ipx->ipx_depth = 0;
6488 #endif
6489 out:
6490 	mutex_exit(&ipx->ipx_lock);
6491 	mutex_exit(&ipsq->ipsq_lock);
6492 
6493 	/*
6494 	 * If we completely emptied the xop, then wake up any threads waiting
6495 	 * to enter any of the IPSQ's associated with it.
6496 	 */
6497 	if (emptied) {
6498 		xopipsq = ipsq;
6499 		do {
6500 			if ((phyi = xopipsq->ipsq_phyint) == NULL)
6501 				continue;
6502 
6503 			illv4 = phyi->phyint_illv4;
6504 			illv6 = phyi->phyint_illv6;
6505 
6506 			GRAB_ILL_LOCKS(illv4, illv6);
6507 			if (illv4 != NULL)
6508 				cv_broadcast(&illv4->ill_cv);
6509 			if (illv6 != NULL)
6510 				cv_broadcast(&illv6->ill_cv);
6511 			RELEASE_ILL_LOCKS(illv4, illv6);
6512 		} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6513 	}
6514 	rw_exit(&ipst->ips_ill_g_lock);
6515 
6516 	/*
6517 	 * Now that all locks are dropped, exit the IPSQ we left.
6518 	 */
6519 	if (leftipsq != NULL)
6520 		ipsq_exit(leftipsq);
6521 
6522 	return (mp);
6523 }
6524 
6525 /*
6526  * Return completion status of previously initiated DLPI operations on
6527  * ills in the purview of an ipsq.
6528  */
6529 static boolean_t
ipsq_dlpi_done(ipsq_t * ipsq)6530 ipsq_dlpi_done(ipsq_t *ipsq)
6531 {
6532 	ipsq_t		*ipsq_start;
6533 	phyint_t	*phyi;
6534 	ill_t		*ill;
6535 
6536 	ASSERT(RW_LOCK_HELD(&ipsq->ipsq_ipst->ips_ill_g_lock));
6537 	ipsq_start = ipsq;
6538 
6539 	do {
6540 		/*
6541 		 * The only current users of this function are ipsq_try_enter
6542 		 * and ipsq_enter which have made sure that ipsq_writer is
6543 		 * NULL before we reach here. ill_dlpi_pending is modified
6544 		 * only by an ipsq writer
6545 		 */
6546 		ASSERT(ipsq->ipsq_xop->ipx_writer == NULL);
6547 		phyi = ipsq->ipsq_phyint;
6548 		/*
6549 		 * phyi could be NULL if a phyint that is part of an
6550 		 * IPMP group is being unplumbed. A more detailed
6551 		 * comment is in ipmp_grp_update_kstats()
6552 		 */
6553 		if (phyi != NULL) {
6554 			ill = phyi->phyint_illv4;
6555 			if (ill != NULL &&
6556 			    (ill->ill_dlpi_pending != DL_PRIM_INVAL ||
6557 			    ill->ill_arl_dlpi_pending))
6558 				return (B_FALSE);
6559 
6560 			ill = phyi->phyint_illv6;
6561 			if (ill != NULL &&
6562 			    ill->ill_dlpi_pending != DL_PRIM_INVAL)
6563 				return (B_FALSE);
6564 		}
6565 
6566 	} while ((ipsq = ipsq->ipsq_next) != ipsq_start);
6567 
6568 	return (B_TRUE);
6569 }
6570 
6571 /*
6572  * Enter the ipsq corresponding to ill, by waiting synchronously till
6573  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
6574  * will have to drain completely before ipsq_enter returns success.
6575  * ipx_current_ipif will be set if some exclusive op is in progress,
6576  * and the ipsq_exit logic will start the next enqueued op after
6577  * completion of the current op. If 'force' is used, we don't wait
6578  * for the enqueued ops. This is needed when a conn_close wants to
6579  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
6580  * of an ill can also use this option. But we dont' use it currently.
6581  */
6582 #define	ENTER_SQ_WAIT_TICKS 100
6583 boolean_t
ipsq_enter(ill_t * ill,boolean_t force,int type)6584 ipsq_enter(ill_t *ill, boolean_t force, int type)
6585 {
6586 	ipsq_t	*ipsq;
6587 	ipxop_t *ipx;
6588 	boolean_t waited_enough = B_FALSE;
6589 	ip_stack_t *ipst = ill->ill_ipst;
6590 
6591 	/*
6592 	 * Note that the relationship between ill and ipsq is fixed as long as
6593 	 * the ill is not ILL_CONDEMNED.  Holding ipsq_lock ensures the
6594 	 * relationship between the IPSQ and xop cannot change.  However,
6595 	 * since we cannot hold ipsq_lock across the cv_wait(), it may change
6596 	 * while we're waiting.  We wait on ill_cv and rely on ipsq_exit()
6597 	 * waking up all ills in the xop when it becomes available.
6598 	 */
6599 	for (;;) {
6600 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6601 		mutex_enter(&ill->ill_lock);
6602 		if (ill->ill_state_flags & ILL_CONDEMNED) {
6603 			mutex_exit(&ill->ill_lock);
6604 			rw_exit(&ipst->ips_ill_g_lock);
6605 			return (B_FALSE);
6606 		}
6607 
6608 		ipsq = ill->ill_phyint->phyint_ipsq;
6609 		mutex_enter(&ipsq->ipsq_lock);
6610 		ipx = ipsq->ipsq_xop;
6611 		mutex_enter(&ipx->ipx_lock);
6612 
6613 		if (ipx->ipx_writer == NULL && (type == CUR_OP ||
6614 		    (ipx->ipx_current_ipif == NULL && ipsq_dlpi_done(ipsq)) ||
6615 		    waited_enough))
6616 			break;
6617 
6618 		rw_exit(&ipst->ips_ill_g_lock);
6619 
6620 		if (!force || ipx->ipx_writer != NULL) {
6621 			mutex_exit(&ipx->ipx_lock);
6622 			mutex_exit(&ipsq->ipsq_lock);
6623 			cv_wait(&ill->ill_cv, &ill->ill_lock);
6624 		} else {
6625 			mutex_exit(&ipx->ipx_lock);
6626 			mutex_exit(&ipsq->ipsq_lock);
6627 			(void) cv_reltimedwait(&ill->ill_cv,
6628 			    &ill->ill_lock, ENTER_SQ_WAIT_TICKS, TR_CLOCK_TICK);
6629 			waited_enough = B_TRUE;
6630 		}
6631 		mutex_exit(&ill->ill_lock);
6632 	}
6633 
6634 	ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6635 	ASSERT(ipx->ipx_reentry_cnt == 0);
6636 	ipx->ipx_writer = curthread;
6637 	ipx->ipx_forced = (ipx->ipx_current_ipif != NULL);
6638 	ipx->ipx_reentry_cnt++;
6639 #ifdef DEBUG
6640 	ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6641 #endif
6642 	mutex_exit(&ipx->ipx_lock);
6643 	mutex_exit(&ipsq->ipsq_lock);
6644 	mutex_exit(&ill->ill_lock);
6645 	rw_exit(&ipst->ips_ill_g_lock);
6646 
6647 	return (B_TRUE);
6648 }
6649 
6650 /*
6651  * ipif_set_values() has a constraint that it cannot drop the ips_ill_g_lock
6652  * across the call to the core interface ipsq_try_enter() and hence calls this
6653  * function directly. This is explained more fully in ipif_set_values().
6654  * In order to support the above constraint, ipsq_try_enter is implemented as
6655  * a wrapper that grabs the ips_ill_g_lock and calls this function subsequently
6656  */
6657 static ipsq_t *
ipsq_try_enter_internal(ill_t * ill,queue_t * q,mblk_t * mp,ipsq_func_t func,int type,boolean_t reentry_ok)6658 ipsq_try_enter_internal(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func,
6659     int type, boolean_t reentry_ok)
6660 {
6661 	ipsq_t	*ipsq;
6662 	ipxop_t	*ipx;
6663 	ip_stack_t *ipst = ill->ill_ipst;
6664 
6665 	/*
6666 	 * lock ordering:
6667 	 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock -> ipx_lock.
6668 	 *
6669 	 * ipx of an ipsq can't change when ipsq_lock is held.
6670 	 */
6671 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
6672 	GRAB_CONN_LOCK(q);
6673 	mutex_enter(&ill->ill_lock);
6674 	ipsq = ill->ill_phyint->phyint_ipsq;
6675 	mutex_enter(&ipsq->ipsq_lock);
6676 	ipx = ipsq->ipsq_xop;
6677 	mutex_enter(&ipx->ipx_lock);
6678 
6679 	/*
6680 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
6681 	 *    (Note: If the caller does not specify reentry_ok then neither
6682 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
6683 	 *    again. Otherwise it can lead to an infinite loop
6684 	 * 2. Enter the ipsq if there is no current writer and this attempted
6685 	 *    entry is part of the current operation
6686 	 * 3. Enter the ipsq if there is no current writer and this is a new
6687 	 *    operation and the operation queue is empty and there is no
6688 	 *    operation currently in progress and if all previously initiated
6689 	 *    DLPI operations have completed.
6690 	 */
6691 	if ((ipx->ipx_writer == curthread && reentry_ok) ||
6692 	    (ipx->ipx_writer == NULL && (type == CUR_OP || (type == NEW_OP &&
6693 	    !ipx->ipx_ipsq_queued && ipx->ipx_current_ipif == NULL &&
6694 	    ipsq_dlpi_done(ipsq))))) {
6695 		/* Success. */
6696 		ipx->ipx_reentry_cnt++;
6697 		ipx->ipx_writer = curthread;
6698 		ipx->ipx_forced = B_FALSE;
6699 		mutex_exit(&ipx->ipx_lock);
6700 		mutex_exit(&ipsq->ipsq_lock);
6701 		mutex_exit(&ill->ill_lock);
6702 		RELEASE_CONN_LOCK(q);
6703 #ifdef DEBUG
6704 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6705 #endif
6706 		return (ipsq);
6707 	}
6708 
6709 	if (func != NULL)
6710 		ipsq_enq(ipsq, q, mp, func, type, ill);
6711 
6712 	mutex_exit(&ipx->ipx_lock);
6713 	mutex_exit(&ipsq->ipsq_lock);
6714 	mutex_exit(&ill->ill_lock);
6715 	RELEASE_CONN_LOCK(q);
6716 	return (NULL);
6717 }
6718 
6719 /*
6720  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
6721  * certain critical operations like plumbing (i.e. most set ioctls), etc.
6722  * There is one ipsq per phyint. The ipsq
6723  * serializes exclusive ioctls issued by applications on a per ipsq basis in
6724  * ipsq_xopq_mphead. It also protects against multiple threads executing in
6725  * the ipsq. Responses from the driver pertain to the current ioctl (say a
6726  * DL_BIND_ACK in response to a DL_BIND_REQ initiated as part of bringing
6727  * up the interface) and are enqueued in ipx_mphead.
6728  *
6729  * If a thread does not want to reenter the ipsq when it is already writer,
6730  * it must make sure that the specified reentry point to be called later
6731  * when the ipsq is empty, nor any code path starting from the specified reentry
6732  * point must never ever try to enter the ipsq again. Otherwise it can lead
6733  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
6734  * When the thread that is currently exclusive finishes, it (ipsq_exit)
6735  * dequeues the requests waiting to become exclusive in ipx_mphead and calls
6736  * the reentry point. When the list at ipx_mphead becomes empty ipsq_exit
6737  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
6738  * ioctl if the current ioctl has completed. If the current ioctl is still
6739  * in progress it simply returns. The current ioctl could be waiting for
6740  * a response from another module (the driver or could be waiting for
6741  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipx_pending_mp
6742  * and ipx_pending_ipif are set. ipx_current_ipif is set throughout the
6743  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
6744  * ipx_current_ipif is NULL which happens only once the ioctl is complete and
6745  * all associated DLPI operations have completed.
6746  */
6747 
6748 /*
6749  * Try to enter the IPSQ corresponding to `ipif' or `ill' exclusively (`ipif'
6750  * and `ill' cannot both be specified).  Returns a pointer to the entered IPSQ
6751  * on success, or NULL on failure.  The caller ensures ipif/ill is valid by
6752  * refholding it as necessary.  If the IPSQ cannot be entered and `func' is
6753  * non-NULL, then `func' will be called back with `q' and `mp' once the IPSQ
6754  * can be entered.  If `func' is NULL, then `q' and `mp' are ignored.
6755  */
6756 ipsq_t *
ipsq_try_enter(ipif_t * ipif,ill_t * ill,queue_t * q,mblk_t * mp,ipsq_func_t func,int type,boolean_t reentry_ok)6757 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
6758     ipsq_func_t func, int type, boolean_t reentry_ok)
6759 {
6760 	ip_stack_t	*ipst;
6761 	ipsq_t		*ipsq;
6762 
6763 	/* Only 1 of ipif or ill can be specified */
6764 	ASSERT((ipif != NULL) ^ (ill != NULL));
6765 
6766 	if (ipif != NULL)
6767 		ill = ipif->ipif_ill;
6768 	ipst = ill->ill_ipst;
6769 
6770 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6771 	ipsq = ipsq_try_enter_internal(ill, q, mp, func, type, reentry_ok);
6772 	rw_exit(&ipst->ips_ill_g_lock);
6773 
6774 	return (ipsq);
6775 }
6776 
6777 /*
6778  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
6779  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
6780  * cannot be entered, the mp is queued for completion.
6781  */
6782 void
qwriter_ip(ill_t * ill,queue_t * q,mblk_t * mp,ipsq_func_t func,int type,boolean_t reentry_ok)6783 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6784     boolean_t reentry_ok)
6785 {
6786 	ipsq_t	*ipsq;
6787 
6788 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
6789 
6790 	/*
6791 	 * Drop the caller's refhold on the ill.  This is safe since we either
6792 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
6793 	 * IPSQ, in which case we return without accessing ill anymore.  This
6794 	 * is needed because func needs to see the correct refcount.
6795 	 * e.g. removeif can work only then.
6796 	 */
6797 	ill_refrele(ill);
6798 	if (ipsq != NULL) {
6799 		(*func)(ipsq, q, mp, NULL);
6800 		ipsq_exit(ipsq);
6801 	}
6802 }
6803 
6804 /*
6805  * Exit the specified IPSQ.  If this is the final exit on it then drain it
6806  * prior to exiting.  Caller must be writer on the specified IPSQ.
6807  */
6808 void
ipsq_exit(ipsq_t * ipsq)6809 ipsq_exit(ipsq_t *ipsq)
6810 {
6811 	mblk_t *mp;
6812 	ipsq_t *mp_ipsq;
6813 	queue_t	*q;
6814 	phyint_t *phyi;
6815 	ipsq_func_t func;
6816 
6817 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6818 
6819 	ASSERT(ipsq->ipsq_xop->ipx_reentry_cnt >= 1);
6820 	if (ipsq->ipsq_xop->ipx_reentry_cnt != 1) {
6821 		ipsq->ipsq_xop->ipx_reentry_cnt--;
6822 		return;
6823 	}
6824 
6825 	for (;;) {
6826 		phyi = ipsq->ipsq_phyint;
6827 		mp = ipsq_dq(ipsq);
6828 		mp_ipsq = (mp == NULL) ? NULL : (ipsq_t *)mp->b_next;
6829 
6830 		/*
6831 		 * If we've changed to a new IPSQ, and the phyint associated
6832 		 * with the old one has gone away, free the old IPSQ.  Note
6833 		 * that this cannot happen while the IPSQ is in a group.
6834 		 */
6835 		if (mp_ipsq != ipsq && phyi == NULL) {
6836 			ASSERT(ipsq->ipsq_next == ipsq);
6837 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6838 			ipsq_delete(ipsq);
6839 		}
6840 
6841 		if (mp == NULL)
6842 			break;
6843 
6844 		q = mp->b_queue;
6845 		func = (ipsq_func_t)mp->b_prev;
6846 		ipsq = mp_ipsq;
6847 		mp->b_next = mp->b_prev = NULL;
6848 		mp->b_queue = NULL;
6849 
6850 		/*
6851 		 * If 'q' is an conn queue, it is valid, since we did a
6852 		 * a refhold on the conn at the start of the ioctl.
6853 		 * If 'q' is an ill queue, it is valid, since close of an
6854 		 * ill will clean up its IPSQ.
6855 		 */
6856 		(*func)(ipsq, q, mp, NULL);
6857 	}
6858 }
6859 
6860 /*
6861  * Used to start any igmp or mld timers that could not be started
6862  * while holding ill_mcast_lock. The timers can't be started while holding
6863  * the lock, since mld/igmp_start_timers may need to call untimeout()
6864  * which can't be done while holding the lock which the timeout handler
6865  * acquires. Otherwise
6866  * there could be a deadlock since the timeout handlers
6867  * mld_timeout_handler_per_ill/igmp_timeout_handler_per_ill also acquire
6868  * ill_mcast_lock.
6869  */
6870 void
ill_mcast_timer_start(ip_stack_t * ipst)6871 ill_mcast_timer_start(ip_stack_t *ipst)
6872 {
6873 	int		next;
6874 
6875 	mutex_enter(&ipst->ips_igmp_timer_lock);
6876 	next = ipst->ips_igmp_deferred_next;
6877 	ipst->ips_igmp_deferred_next = INFINITY;
6878 	mutex_exit(&ipst->ips_igmp_timer_lock);
6879 
6880 	if (next != INFINITY)
6881 		igmp_start_timers(next, ipst);
6882 
6883 	mutex_enter(&ipst->ips_mld_timer_lock);
6884 	next = ipst->ips_mld_deferred_next;
6885 	ipst->ips_mld_deferred_next = INFINITY;
6886 	mutex_exit(&ipst->ips_mld_timer_lock);
6887 
6888 	if (next != INFINITY)
6889 		mld_start_timers(next, ipst);
6890 }
6891 
6892 /*
6893  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
6894  * and `ioccmd'.
6895  */
6896 void
ipsq_current_start(ipsq_t * ipsq,ipif_t * ipif,int ioccmd)6897 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
6898 {
6899 	ill_t *ill = ipif->ipif_ill;
6900 	ipxop_t *ipx = ipsq->ipsq_xop;
6901 
6902 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6903 	ASSERT(ipx->ipx_current_ipif == NULL);
6904 	ASSERT(ipx->ipx_current_ioctl == 0);
6905 
6906 	ipx->ipx_current_done = B_FALSE;
6907 	ipx->ipx_current_ioctl = ioccmd;
6908 	mutex_enter(&ipx->ipx_lock);
6909 	ipx->ipx_current_ipif = ipif;
6910 	mutex_exit(&ipx->ipx_lock);
6911 
6912 	/*
6913 	 * Set IPIF_CHANGING on one or more ipifs associated with the
6914 	 * current exclusive operation.  IPIF_CHANGING prevents any new
6915 	 * references to the ipif (so that the references will eventually
6916 	 * drop to zero) and also prevents any "get" operations (e.g.,
6917 	 * SIOCGLIFFLAGS) from being able to access the ipif until the
6918 	 * operation has completed and the ipif is again in a stable state.
6919 	 *
6920 	 * For ioctls, IPIF_CHANGING is set on the ipif associated with the
6921 	 * ioctl.  For internal operations (where ioccmd is zero), all ipifs
6922 	 * on the ill are marked with IPIF_CHANGING since it's unclear which
6923 	 * ipifs will be affected.
6924 	 *
6925 	 * Note that SIOCLIFREMOVEIF is a special case as it sets
6926 	 * IPIF_CONDEMNED internally after identifying the right ipif to
6927 	 * operate on.
6928 	 */
6929 	switch (ioccmd) {
6930 	case SIOCLIFREMOVEIF:
6931 		break;
6932 	case 0:
6933 		mutex_enter(&ill->ill_lock);
6934 		ipif = ipif->ipif_ill->ill_ipif;
6935 		for (; ipif != NULL; ipif = ipif->ipif_next)
6936 			ipif->ipif_state_flags |= IPIF_CHANGING;
6937 		mutex_exit(&ill->ill_lock);
6938 		break;
6939 	default:
6940 		mutex_enter(&ill->ill_lock);
6941 		ipif->ipif_state_flags |= IPIF_CHANGING;
6942 		mutex_exit(&ill->ill_lock);
6943 	}
6944 }
6945 
6946 /*
6947  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
6948  * the next exclusive operation to begin once we ipsq_exit().  However, if
6949  * pending DLPI operations remain, then we will wait for the queue to drain
6950  * before allowing the next exclusive operation to begin.  This ensures that
6951  * DLPI operations from one exclusive operation are never improperly processed
6952  * as part of a subsequent exclusive operation.
6953  */
6954 void
ipsq_current_finish(ipsq_t * ipsq)6955 ipsq_current_finish(ipsq_t *ipsq)
6956 {
6957 	ipxop_t	*ipx = ipsq->ipsq_xop;
6958 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
6959 	ipif_t	*ipif = ipx->ipx_current_ipif;
6960 
6961 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6962 
6963 	/*
6964 	 * For SIOCLIFREMOVEIF, the ipif has been already been blown away
6965 	 * (but in that case, IPIF_CHANGING will already be clear and no
6966 	 * pending DLPI messages can remain).
6967 	 */
6968 	if (ipx->ipx_current_ioctl != SIOCLIFREMOVEIF) {
6969 		ill_t *ill = ipif->ipif_ill;
6970 
6971 		mutex_enter(&ill->ill_lock);
6972 		dlpi_pending = ill->ill_dlpi_pending;
6973 		if (ipx->ipx_current_ioctl == 0) {
6974 			ipif = ill->ill_ipif;
6975 			for (; ipif != NULL; ipif = ipif->ipif_next)
6976 				ipif->ipif_state_flags &= ~IPIF_CHANGING;
6977 		} else {
6978 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
6979 		}
6980 		mutex_exit(&ill->ill_lock);
6981 	}
6982 
6983 	ASSERT(!ipx->ipx_current_done);
6984 	ipx->ipx_current_done = B_TRUE;
6985 	ipx->ipx_current_ioctl = 0;
6986 	if (dlpi_pending == DL_PRIM_INVAL) {
6987 		mutex_enter(&ipx->ipx_lock);
6988 		ipx->ipx_current_ipif = NULL;
6989 		mutex_exit(&ipx->ipx_lock);
6990 	}
6991 }
6992 
6993 /*
6994  * The ill is closing. Flush all messages on the ipsq that originated
6995  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
6996  * for this ill since ipsq_enter could not have entered until then.
6997  * New messages can't be queued since the CONDEMNED flag is set.
6998  */
6999 static void
ipsq_flush(ill_t * ill)7000 ipsq_flush(ill_t *ill)
7001 {
7002 	queue_t	*q;
7003 	mblk_t	*prev;
7004 	mblk_t	*mp;
7005 	mblk_t	*mp_next;
7006 	ipxop_t	*ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
7007 
7008 	ASSERT(IAM_WRITER_ILL(ill));
7009 
7010 	/*
7011 	 * Flush any messages sent up by the driver.
7012 	 */
7013 	mutex_enter(&ipx->ipx_lock);
7014 	for (prev = NULL, mp = ipx->ipx_mphead; mp != NULL; mp = mp_next) {
7015 		mp_next = mp->b_next;
7016 		q = mp->b_queue;
7017 		if (q == ill->ill_rq || q == ill->ill_wq) {
7018 			/* dequeue mp */
7019 			if (prev == NULL)
7020 				ipx->ipx_mphead = mp->b_next;
7021 			else
7022 				prev->b_next = mp->b_next;
7023 			if (ipx->ipx_mptail == mp) {
7024 				ASSERT(mp_next == NULL);
7025 				ipx->ipx_mptail = prev;
7026 			}
7027 			inet_freemsg(mp);
7028 		} else {
7029 			prev = mp;
7030 		}
7031 	}
7032 	mutex_exit(&ipx->ipx_lock);
7033 	(void) ipsq_pending_mp_cleanup(ill, NULL);
7034 	ipsq_xopq_mp_cleanup(ill, NULL);
7035 }
7036 
7037 /*
7038  * Parse an ifreq or lifreq struct coming down ioctls and refhold
7039  * and return the associated ipif.
7040  * Return value:
7041  *	Non zero: An error has occurred. ci may not be filled out.
7042  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
7043  *	a held ipif in ci.ci_ipif.
7044  */
7045 int
ip_extract_lifreq(queue_t * q,mblk_t * mp,const ip_ioctl_cmd_t * ipip,cmd_info_t * ci)7046 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
7047     cmd_info_t *ci)
7048 {
7049 	char		*name;
7050 	struct ifreq    *ifr;
7051 	struct lifreq    *lifr;
7052 	ipif_t		*ipif = NULL;
7053 	ill_t		*ill;
7054 	conn_t		*connp;
7055 	boolean_t	isv6;
7056 	int		err;
7057 	mblk_t		*mp1;
7058 	zoneid_t	zoneid;
7059 	ip_stack_t	*ipst;
7060 
7061 	if (q->q_next != NULL) {
7062 		ill = (ill_t *)q->q_ptr;
7063 		isv6 = ill->ill_isv6;
7064 		connp = NULL;
7065 		zoneid = ALL_ZONES;
7066 		ipst = ill->ill_ipst;
7067 	} else {
7068 		ill = NULL;
7069 		connp = Q_TO_CONN(q);
7070 		isv6 = (connp->conn_family == AF_INET6);
7071 		zoneid = connp->conn_zoneid;
7072 		if (zoneid == GLOBAL_ZONEID) {
7073 			/* global zone can access ipifs in all zones */
7074 			zoneid = ALL_ZONES;
7075 		}
7076 		ipst = connp->conn_netstack->netstack_ip;
7077 	}
7078 
7079 	/* Has been checked in ip_wput_nondata */
7080 	mp1 = mp->b_cont->b_cont;
7081 
7082 	if (ipip->ipi_cmd_type == IF_CMD) {
7083 		/* This a old style SIOC[GS]IF* command */
7084 		ifr = (struct ifreq *)mp1->b_rptr;
7085 		/*
7086 		 * Null terminate the string to protect against buffer
7087 		 * overrun. String was generated by user code and may not
7088 		 * be trusted.
7089 		 */
7090 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
7091 		name = ifr->ifr_name;
7092 		ci->ci_sin = (sin_t *)&ifr->ifr_addr;
7093 		ci->ci_sin6 = NULL;
7094 		ci->ci_lifr = (struct lifreq *)ifr;
7095 	} else {
7096 		/* This a new style SIOC[GS]LIF* command */
7097 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
7098 		lifr = (struct lifreq *)mp1->b_rptr;
7099 		/*
7100 		 * Null terminate the string to protect against buffer
7101 		 * overrun. String was generated by user code and may not
7102 		 * be trusted.
7103 		 */
7104 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
7105 		name = lifr->lifr_name;
7106 		ci->ci_sin = (sin_t *)&lifr->lifr_addr;
7107 		ci->ci_sin6 = (sin6_t *)&lifr->lifr_addr;
7108 		ci->ci_lifr = lifr;
7109 	}
7110 
7111 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
7112 		/*
7113 		 * The ioctl will be failed if the ioctl comes down
7114 		 * an conn stream
7115 		 */
7116 		if (ill == NULL) {
7117 			/*
7118 			 * Not an ill queue, return EINVAL same as the
7119 			 * old error code.
7120 			 */
7121 			return (ENXIO);
7122 		}
7123 		ipif = ill->ill_ipif;
7124 		ipif_refhold(ipif);
7125 	} else {
7126 		/*
7127 		 * Ensure that ioctls don't see any internal state changes
7128 		 * caused by set ioctls by deferring them if IPIF_CHANGING is
7129 		 * set.
7130 		 */
7131 		ipif = ipif_lookup_on_name_async(name, mi_strlen(name),
7132 		    isv6, zoneid, q, mp, ip_process_ioctl, &err, ipst);
7133 		if (ipif == NULL) {
7134 			if (err == EINPROGRESS)
7135 				return (err);
7136 			err = 0;	/* Ensure we don't use it below */
7137 		}
7138 	}
7139 
7140 	/*
7141 	 * Old style [GS]IFCMD does not admit IPv6 ipif
7142 	 */
7143 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
7144 		ipif_refrele(ipif);
7145 		return (ENXIO);
7146 	}
7147 
7148 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
7149 	    name[0] == '\0') {
7150 		/*
7151 		 * Handle a or a SIOC?IF* with a null name
7152 		 * during plumb (on the ill queue before the I_PLINK).
7153 		 */
7154 		ipif = ill->ill_ipif;
7155 		ipif_refhold(ipif);
7156 	}
7157 
7158 	if (ipif == NULL)
7159 		return (ENXIO);
7160 
7161 	DTRACE_PROBE4(ipif__ioctl, char *, "ip_extract_lifreq",
7162 	    int, ipip->ipi_cmd, ill_t *, ipif->ipif_ill, ipif_t *, ipif);
7163 
7164 	ci->ci_ipif = ipif;
7165 	return (0);
7166 }
7167 
7168 /*
7169  * Return the total number of ipifs.
7170  */
7171 static uint_t
ip_get_numifs(zoneid_t zoneid,ip_stack_t * ipst)7172 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
7173 {
7174 	uint_t numifs = 0;
7175 	ill_t	*ill;
7176 	ill_walk_context_t	ctx;
7177 	ipif_t	*ipif;
7178 
7179 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7180 	ill = ILL_START_WALK_V4(&ctx, ipst);
7181 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7182 		if (IS_UNDER_IPMP(ill))
7183 			continue;
7184 		for (ipif = ill->ill_ipif; ipif != NULL;
7185 		    ipif = ipif->ipif_next) {
7186 			if (ipif->ipif_zoneid == zoneid ||
7187 			    ipif->ipif_zoneid == ALL_ZONES)
7188 				numifs++;
7189 		}
7190 	}
7191 	rw_exit(&ipst->ips_ill_g_lock);
7192 	return (numifs);
7193 }
7194 
7195 /*
7196  * Return the total number of ipifs.
7197  */
7198 static uint_t
ip_get_numlifs(int family,int lifn_flags,zoneid_t zoneid,ip_stack_t * ipst)7199 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
7200 {
7201 	uint_t numifs = 0;
7202 	ill_t	*ill;
7203 	ipif_t	*ipif;
7204 	ill_walk_context_t	ctx;
7205 
7206 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
7207 
7208 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7209 	if (family == AF_INET)
7210 		ill = ILL_START_WALK_V4(&ctx, ipst);
7211 	else if (family == AF_INET6)
7212 		ill = ILL_START_WALK_V6(&ctx, ipst);
7213 	else
7214 		ill = ILL_START_WALK_ALL(&ctx, ipst);
7215 
7216 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7217 		if (IS_UNDER_IPMP(ill) && !(lifn_flags & LIFC_UNDER_IPMP))
7218 			continue;
7219 
7220 		for (ipif = ill->ill_ipif; ipif != NULL;
7221 		    ipif = ipif->ipif_next) {
7222 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7223 			    !(lifn_flags & LIFC_NOXMIT))
7224 				continue;
7225 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7226 			    !(lifn_flags & LIFC_TEMPORARY))
7227 				continue;
7228 			if (((ipif->ipif_flags &
7229 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7230 			    IPIF_DEPRECATED)) ||
7231 			    IS_LOOPBACK(ill) ||
7232 			    !(ipif->ipif_flags & IPIF_UP)) &&
7233 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
7234 				continue;
7235 
7236 			if (zoneid != ipif->ipif_zoneid &&
7237 			    ipif->ipif_zoneid != ALL_ZONES &&
7238 			    (zoneid != GLOBAL_ZONEID ||
7239 			    !(lifn_flags & LIFC_ALLZONES)))
7240 				continue;
7241 
7242 			numifs++;
7243 		}
7244 	}
7245 	rw_exit(&ipst->ips_ill_g_lock);
7246 	return (numifs);
7247 }
7248 
7249 uint_t
ip_get_lifsrcofnum(ill_t * ill)7250 ip_get_lifsrcofnum(ill_t *ill)
7251 {
7252 	uint_t numifs = 0;
7253 	ill_t	*ill_head = ill;
7254 	ip_stack_t	*ipst = ill->ill_ipst;
7255 
7256 	/*
7257 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
7258 	 * other thread may be trying to relink the ILLs in this usesrc group
7259 	 * and adjusting the ill_usesrc_grp_next pointers
7260 	 */
7261 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7262 	if ((ill->ill_usesrc_ifindex == 0) &&
7263 	    (ill->ill_usesrc_grp_next != NULL)) {
7264 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
7265 		    ill = ill->ill_usesrc_grp_next)
7266 			numifs++;
7267 	}
7268 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7269 
7270 	return (numifs);
7271 }
7272 
7273 /* Null values are passed in for ipif, sin, and ifreq */
7274 /* ARGSUSED */
7275 int
ip_sioctl_get_ifnum(ipif_t * dummy_ipif,sin_t * dummy_sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * ifreq)7276 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7277     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7278 {
7279 	int *nump;
7280 	conn_t *connp = Q_TO_CONN(q);
7281 
7282 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7283 
7284 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
7285 	nump = (int *)mp->b_cont->b_cont->b_rptr;
7286 
7287 	*nump = ip_get_numifs(connp->conn_zoneid,
7288 	    connp->conn_netstack->netstack_ip);
7289 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
7290 	return (0);
7291 }
7292 
7293 /* Null values are passed in for ipif, sin, and ifreq */
7294 /* ARGSUSED */
7295 int
ip_sioctl_get_lifnum(ipif_t * dummy_ipif,sin_t * dummy_sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * ifreq)7296 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
7297     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7298 {
7299 	struct lifnum *lifn;
7300 	mblk_t	*mp1;
7301 	conn_t *connp = Q_TO_CONN(q);
7302 
7303 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7304 
7305 	/* Existence checked in ip_wput_nondata */
7306 	mp1 = mp->b_cont->b_cont;
7307 
7308 	lifn = (struct lifnum *)mp1->b_rptr;
7309 	switch (lifn->lifn_family) {
7310 	case AF_UNSPEC:
7311 	case AF_INET:
7312 	case AF_INET6:
7313 		break;
7314 	default:
7315 		return (EAFNOSUPPORT);
7316 	}
7317 
7318 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
7319 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
7320 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
7321 	return (0);
7322 }
7323 
7324 /* ARGSUSED */
7325 int
ip_sioctl_get_ifconf(ipif_t * dummy_ipif,sin_t * dummy_sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * ifreq)7326 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7327     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7328 {
7329 	STRUCT_HANDLE(ifconf, ifc);
7330 	mblk_t *mp1;
7331 	struct iocblk *iocp;
7332 	struct ifreq *ifr;
7333 	ill_walk_context_t	ctx;
7334 	ill_t	*ill;
7335 	ipif_t	*ipif;
7336 	struct sockaddr_in *sin;
7337 	int32_t	ifclen;
7338 	zoneid_t zoneid;
7339 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7340 
7341 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
7342 
7343 	ip1dbg(("ip_sioctl_get_ifconf"));
7344 	/* Existence verified in ip_wput_nondata */
7345 	mp1 = mp->b_cont->b_cont;
7346 	iocp = (struct iocblk *)mp->b_rptr;
7347 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7348 
7349 	/*
7350 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
7351 	 * the user buffer address and length into which the list of struct
7352 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
7353 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
7354 	 * the SIOCGIFCONF operation was redefined to simply provide
7355 	 * a large output buffer into which we are supposed to jam the ifreq
7356 	 * array.  The same ioctl command code was used, despite the fact that
7357 	 * both the applications and the kernel code had to change, thus making
7358 	 * it impossible to support both interfaces.
7359 	 *
7360 	 * For reasons not good enough to try to explain, the following
7361 	 * algorithm is used for deciding what to do with one of these:
7362 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
7363 	 * form with the output buffer coming down as the continuation message.
7364 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
7365 	 * and we have to copy in the ifconf structure to find out how big the
7366 	 * output buffer is and where to copy out to.  Sure no problem...
7367 	 *
7368 	 */
7369 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
7370 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
7371 		int numifs = 0;
7372 		size_t ifc_bufsize;
7373 
7374 		/*
7375 		 * Must be (better be!) continuation of a TRANSPARENT
7376 		 * IOCTL.  We just copied in the ifconf structure.
7377 		 */
7378 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
7379 		    (struct ifconf *)mp1->b_rptr);
7380 
7381 		/*
7382 		 * Allocate a buffer to hold requested information.
7383 		 *
7384 		 * If ifc_len is larger than what is needed, we only
7385 		 * allocate what we will use.
7386 		 *
7387 		 * If ifc_len is smaller than what is needed, return
7388 		 * EINVAL.
7389 		 *
7390 		 * XXX: the ill_t structure can hava 2 counters, for
7391 		 * v4 and v6 (not just ill_ipif_up_count) to store the
7392 		 * number of interfaces for a device, so we don't need
7393 		 * to count them here...
7394 		 */
7395 		numifs = ip_get_numifs(zoneid, ipst);
7396 
7397 		ifclen = STRUCT_FGET(ifc, ifc_len);
7398 		ifc_bufsize = numifs * sizeof (struct ifreq);
7399 		if (ifc_bufsize > ifclen) {
7400 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7401 				/* old behaviour */
7402 				return (EINVAL);
7403 			} else {
7404 				ifc_bufsize = ifclen;
7405 			}
7406 		}
7407 
7408 		mp1 = mi_copyout_alloc(q, mp,
7409 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
7410 		if (mp1 == NULL)
7411 			return (ENOMEM);
7412 
7413 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
7414 	}
7415 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7416 	/*
7417 	 * the SIOCGIFCONF ioctl only knows about
7418 	 * IPv4 addresses, so don't try to tell
7419 	 * it about interfaces with IPv6-only
7420 	 * addresses. (Last parm 'isv6' is B_FALSE)
7421 	 */
7422 
7423 	ifr = (struct ifreq *)mp1->b_rptr;
7424 
7425 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7426 	ill = ILL_START_WALK_V4(&ctx, ipst);
7427 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7428 		if (IS_UNDER_IPMP(ill))
7429 			continue;
7430 		for (ipif = ill->ill_ipif; ipif != NULL;
7431 		    ipif = ipif->ipif_next) {
7432 			if (zoneid != ipif->ipif_zoneid &&
7433 			    ipif->ipif_zoneid != ALL_ZONES)
7434 				continue;
7435 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
7436 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7437 					/* old behaviour */
7438 					rw_exit(&ipst->ips_ill_g_lock);
7439 					return (EINVAL);
7440 				} else {
7441 					goto if_copydone;
7442 				}
7443 			}
7444 			ipif_get_name(ipif, ifr->ifr_name,
7445 			    sizeof (ifr->ifr_name));
7446 			sin = (sin_t *)&ifr->ifr_addr;
7447 			*sin = sin_null;
7448 			sin->sin_family = AF_INET;
7449 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7450 			ifr++;
7451 		}
7452 	}
7453 if_copydone:
7454 	rw_exit(&ipst->ips_ill_g_lock);
7455 	mp1->b_wptr = (uchar_t *)ifr;
7456 
7457 	if (STRUCT_BUF(ifc) != NULL) {
7458 		STRUCT_FSET(ifc, ifc_len,
7459 		    (int)((uchar_t *)ifr - mp1->b_rptr));
7460 	}
7461 	return (0);
7462 }
7463 
7464 /*
7465  * Get the interfaces using the address hosted on the interface passed in,
7466  * as a source adddress
7467  */
7468 /* ARGSUSED */
7469 int
ip_sioctl_get_lifsrcof(ipif_t * dummy_ipif,sin_t * dummy_sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * ifreq)7470 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7471     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7472 {
7473 	mblk_t *mp1;
7474 	ill_t	*ill, *ill_head;
7475 	ipif_t	*ipif, *orig_ipif;
7476 	int	numlifs = 0;
7477 	size_t	lifs_bufsize, lifsmaxlen;
7478 	struct	lifreq *lifr;
7479 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7480 	uint_t	ifindex;
7481 	zoneid_t zoneid;
7482 	boolean_t isv6 = B_FALSE;
7483 	struct	sockaddr_in	*sin;
7484 	struct	sockaddr_in6	*sin6;
7485 	STRUCT_HANDLE(lifsrcof, lifs);
7486 	ip_stack_t		*ipst;
7487 
7488 	ipst = CONNQ_TO_IPST(q);
7489 
7490 	ASSERT(q->q_next == NULL);
7491 
7492 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7493 
7494 	/* Existence verified in ip_wput_nondata */
7495 	mp1 = mp->b_cont->b_cont;
7496 
7497 	/*
7498 	 * Must be (better be!) continuation of a TRANSPARENT
7499 	 * IOCTL.  We just copied in the lifsrcof structure.
7500 	 */
7501 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
7502 	    (struct lifsrcof *)mp1->b_rptr);
7503 
7504 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
7505 		return (EINVAL);
7506 
7507 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
7508 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
7509 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, ipst);
7510 	if (ipif == NULL) {
7511 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
7512 		    ifindex));
7513 		return (ENXIO);
7514 	}
7515 
7516 	/* Allocate a buffer to hold requested information */
7517 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
7518 	lifs_bufsize = numlifs * sizeof (struct lifreq);
7519 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
7520 	/* The actual size needed is always returned in lifs_len */
7521 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
7522 
7523 	/* If the amount we need is more than what is passed in, abort */
7524 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
7525 		ipif_refrele(ipif);
7526 		return (0);
7527 	}
7528 
7529 	mp1 = mi_copyout_alloc(q, mp,
7530 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
7531 	if (mp1 == NULL) {
7532 		ipif_refrele(ipif);
7533 		return (ENOMEM);
7534 	}
7535 
7536 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
7537 	bzero(mp1->b_rptr, lifs_bufsize);
7538 
7539 	lifr = (struct lifreq *)mp1->b_rptr;
7540 
7541 	ill = ill_head = ipif->ipif_ill;
7542 	orig_ipif = ipif;
7543 
7544 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
7545 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7546 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7547 
7548 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
7549 	for (; (ill != NULL) && (ill != ill_head);
7550 	    ill = ill->ill_usesrc_grp_next) {
7551 
7552 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
7553 			break;
7554 
7555 		ipif = ill->ill_ipif;
7556 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
7557 		if (ipif->ipif_isv6) {
7558 			sin6 = (sin6_t *)&lifr->lifr_addr;
7559 			*sin6 = sin6_null;
7560 			sin6->sin6_family = AF_INET6;
7561 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
7562 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
7563 			    &ipif->ipif_v6net_mask);
7564 		} else {
7565 			sin = (sin_t *)&lifr->lifr_addr;
7566 			*sin = sin_null;
7567 			sin->sin_family = AF_INET;
7568 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7569 			lifr->lifr_addrlen = ip_mask_to_plen(
7570 			    ipif->ipif_net_mask);
7571 		}
7572 		lifr++;
7573 	}
7574 	rw_exit(&ipst->ips_ill_g_lock);
7575 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7576 	ipif_refrele(orig_ipif);
7577 	mp1->b_wptr = (uchar_t *)lifr;
7578 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
7579 
7580 	return (0);
7581 }
7582 
7583 /* ARGSUSED */
7584 int
ip_sioctl_get_lifconf(ipif_t * dummy_ipif,sin_t * dummy_sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * ifreq)7585 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7586     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7587 {
7588 	mblk_t *mp1;
7589 	int	list;
7590 	ill_t	*ill;
7591 	ipif_t	*ipif;
7592 	int	flags;
7593 	int	numlifs = 0;
7594 	size_t	lifc_bufsize;
7595 	struct	lifreq *lifr;
7596 	sa_family_t	family;
7597 	struct	sockaddr_in	*sin;
7598 	struct	sockaddr_in6	*sin6;
7599 	ill_walk_context_t	ctx;
7600 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7601 	int32_t	lifclen;
7602 	zoneid_t zoneid;
7603 	STRUCT_HANDLE(lifconf, lifc);
7604 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7605 
7606 	ip1dbg(("ip_sioctl_get_lifconf"));
7607 
7608 	ASSERT(q->q_next == NULL);
7609 
7610 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7611 
7612 	/* Existence verified in ip_wput_nondata */
7613 	mp1 = mp->b_cont->b_cont;
7614 
7615 	/*
7616 	 * An extended version of SIOCGIFCONF that takes an
7617 	 * additional address family and flags field.
7618 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
7619 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
7620 	 * interfaces are omitted.
7621 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
7622 	 * unless LIFC_TEMPORARY is specified.
7623 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
7624 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
7625 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
7626 	 * has priority over LIFC_NOXMIT.
7627 	 */
7628 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
7629 
7630 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
7631 		return (EINVAL);
7632 
7633 	/*
7634 	 * Must be (better be!) continuation of a TRANSPARENT
7635 	 * IOCTL.  We just copied in the lifconf structure.
7636 	 */
7637 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
7638 
7639 	family = STRUCT_FGET(lifc, lifc_family);
7640 	flags = STRUCT_FGET(lifc, lifc_flags);
7641 
7642 	switch (family) {
7643 	case AF_UNSPEC:
7644 		/*
7645 		 * walk all ILL's.
7646 		 */
7647 		list = MAX_G_HEADS;
7648 		break;
7649 	case AF_INET:
7650 		/*
7651 		 * walk only IPV4 ILL's.
7652 		 */
7653 		list = IP_V4_G_HEAD;
7654 		break;
7655 	case AF_INET6:
7656 		/*
7657 		 * walk only IPV6 ILL's.
7658 		 */
7659 		list = IP_V6_G_HEAD;
7660 		break;
7661 	default:
7662 		return (EAFNOSUPPORT);
7663 	}
7664 
7665 	/*
7666 	 * Allocate a buffer to hold requested information.
7667 	 *
7668 	 * If lifc_len is larger than what is needed, we only
7669 	 * allocate what we will use.
7670 	 *
7671 	 * If lifc_len is smaller than what is needed, return
7672 	 * EINVAL.
7673 	 */
7674 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
7675 	lifc_bufsize = numlifs * sizeof (struct lifreq);
7676 	lifclen = STRUCT_FGET(lifc, lifc_len);
7677 	if (lifc_bufsize > lifclen) {
7678 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
7679 			return (EINVAL);
7680 		else
7681 			lifc_bufsize = lifclen;
7682 	}
7683 
7684 	mp1 = mi_copyout_alloc(q, mp,
7685 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
7686 	if (mp1 == NULL)
7687 		return (ENOMEM);
7688 
7689 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
7690 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7691 
7692 	lifr = (struct lifreq *)mp1->b_rptr;
7693 
7694 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7695 	ill = ill_first(list, list, &ctx, ipst);
7696 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7697 		if (IS_UNDER_IPMP(ill) && !(flags & LIFC_UNDER_IPMP))
7698 			continue;
7699 
7700 		for (ipif = ill->ill_ipif; ipif != NULL;
7701 		    ipif = ipif->ipif_next) {
7702 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7703 			    !(flags & LIFC_NOXMIT))
7704 				continue;
7705 
7706 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7707 			    !(flags & LIFC_TEMPORARY))
7708 				continue;
7709 
7710 			if (((ipif->ipif_flags &
7711 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7712 			    IPIF_DEPRECATED)) ||
7713 			    IS_LOOPBACK(ill) ||
7714 			    !(ipif->ipif_flags & IPIF_UP)) &&
7715 			    (flags & LIFC_EXTERNAL_SOURCE))
7716 				continue;
7717 
7718 			if (zoneid != ipif->ipif_zoneid &&
7719 			    ipif->ipif_zoneid != ALL_ZONES &&
7720 			    (zoneid != GLOBAL_ZONEID ||
7721 			    !(flags & LIFC_ALLZONES)))
7722 				continue;
7723 
7724 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
7725 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
7726 					rw_exit(&ipst->ips_ill_g_lock);
7727 					return (EINVAL);
7728 				} else {
7729 					goto lif_copydone;
7730 				}
7731 			}
7732 
7733 			ipif_get_name(ipif, lifr->lifr_name,
7734 			    sizeof (lifr->lifr_name));
7735 			lifr->lifr_type = ill->ill_type;
7736 			if (ipif->ipif_isv6) {
7737 				sin6 = (sin6_t *)&lifr->lifr_addr;
7738 				*sin6 = sin6_null;
7739 				sin6->sin6_family = AF_INET6;
7740 				sin6->sin6_addr =
7741 				    ipif->ipif_v6lcl_addr;
7742 				lifr->lifr_addrlen =
7743 				    ip_mask_to_plen_v6(
7744 				    &ipif->ipif_v6net_mask);
7745 				if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
7746 					sin6->sin6_scope_id =
7747 					    ill->ill_phyint->phyint_ifindex;
7748 				}
7749 			} else {
7750 				sin = (sin_t *)&lifr->lifr_addr;
7751 				*sin = sin_null;
7752 				sin->sin_family = AF_INET;
7753 				sin->sin_addr.s_addr =
7754 				    ipif->ipif_lcl_addr;
7755 				lifr->lifr_addrlen =
7756 				    ip_mask_to_plen(
7757 				    ipif->ipif_net_mask);
7758 			}
7759 			lifr++;
7760 		}
7761 	}
7762 lif_copydone:
7763 	rw_exit(&ipst->ips_ill_g_lock);
7764 
7765 	mp1->b_wptr = (uchar_t *)lifr;
7766 	if (STRUCT_BUF(lifc) != NULL) {
7767 		STRUCT_FSET(lifc, lifc_len,
7768 		    (int)((uchar_t *)lifr - mp1->b_rptr));
7769 	}
7770 	return (0);
7771 }
7772 
7773 static void
ip_sioctl_ip6addrpolicy(queue_t * q,mblk_t * mp)7774 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
7775 {
7776 	ip6_asp_t *table;
7777 	size_t table_size;
7778 	mblk_t *data_mp;
7779 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7780 	ip_stack_t	*ipst;
7781 
7782 	if (q->q_next == NULL)
7783 		ipst = CONNQ_TO_IPST(q);
7784 	else
7785 		ipst = ILLQ_TO_IPST(q);
7786 
7787 	/* These two ioctls are I_STR only */
7788 	if (iocp->ioc_count == TRANSPARENT) {
7789 		miocnak(q, mp, 0, EINVAL);
7790 		return;
7791 	}
7792 
7793 	data_mp = mp->b_cont;
7794 	if (data_mp == NULL) {
7795 		/* The user passed us a NULL argument */
7796 		table = NULL;
7797 		table_size = iocp->ioc_count;
7798 	} else {
7799 		/*
7800 		 * The user provided a table.  The stream head
7801 		 * may have copied in the user data in chunks,
7802 		 * so make sure everything is pulled up
7803 		 * properly.
7804 		 */
7805 		if (MBLKL(data_mp) < iocp->ioc_count) {
7806 			mblk_t *new_data_mp;
7807 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
7808 			    NULL) {
7809 				miocnak(q, mp, 0, ENOMEM);
7810 				return;
7811 			}
7812 			freemsg(data_mp);
7813 			data_mp = new_data_mp;
7814 			mp->b_cont = data_mp;
7815 		}
7816 		table = (ip6_asp_t *)data_mp->b_rptr;
7817 		table_size = iocp->ioc_count;
7818 	}
7819 
7820 	switch (iocp->ioc_cmd) {
7821 	case SIOCGIP6ADDRPOLICY:
7822 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
7823 		if (iocp->ioc_rval == -1)
7824 			iocp->ioc_error = EINVAL;
7825 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7826 		else if (table != NULL &&
7827 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
7828 			ip6_asp_t *src = table;
7829 			ip6_asp32_t *dst = (void *)table;
7830 			int count = table_size / sizeof (ip6_asp_t);
7831 			int i;
7832 
7833 			/*
7834 			 * We need to do an in-place shrink of the array
7835 			 * to match the alignment attributes of the
7836 			 * 32-bit ABI looking at it.
7837 			 */
7838 			/* LINTED: logical expression always true: op "||" */
7839 			ASSERT(sizeof (*src) > sizeof (*dst));
7840 			for (i = 1; i < count; i++)
7841 				bcopy(src + i, dst + i, sizeof (*dst));
7842 		}
7843 #endif
7844 		break;
7845 
7846 	case SIOCSIP6ADDRPOLICY:
7847 		ASSERT(mp->b_prev == NULL);
7848 		mp->b_prev = (void *)q;
7849 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7850 		/*
7851 		 * We pass in the datamodel here so that the ip6_asp_replace()
7852 		 * routine can handle converting from 32-bit to native formats
7853 		 * where necessary.
7854 		 *
7855 		 * A better way to handle this might be to convert the inbound
7856 		 * data structure here, and hang it off a new 'mp'; thus the
7857 		 * ip6_asp_replace() logic would always be dealing with native
7858 		 * format data structures..
7859 		 *
7860 		 * (An even simpler way to handle these ioctls is to just
7861 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
7862 		 * and just recompile everything that depends on it.)
7863 		 */
7864 #endif
7865 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
7866 		    iocp->ioc_flag & IOC_MODELS);
7867 		return;
7868 	}
7869 
7870 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
7871 	qreply(q, mp);
7872 }
7873 
7874 static void
ip_sioctl_dstinfo(queue_t * q,mblk_t * mp)7875 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
7876 {
7877 	mblk_t		*data_mp;
7878 	struct dstinforeq	*dir;
7879 	uint8_t		*end, *cur;
7880 	in6_addr_t	*daddr, *saddr;
7881 	ipaddr_t	v4daddr;
7882 	ire_t		*ire;
7883 	ipaddr_t	v4setsrc;
7884 	in6_addr_t	v6setsrc;
7885 	char		*slabel, *dlabel;
7886 	boolean_t	isipv4;
7887 	int		match_ire;
7888 	ill_t		*dst_ill;
7889 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7890 	conn_t		*connp = Q_TO_CONN(q);
7891 	zoneid_t	zoneid = IPCL_ZONEID(connp);
7892 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
7893 	uint64_t	ipif_flags;
7894 
7895 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
7896 
7897 	/*
7898 	 * This ioctl is I_STR only, and must have a
7899 	 * data mblk following the M_IOCTL mblk.
7900 	 */
7901 	data_mp = mp->b_cont;
7902 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
7903 		miocnak(q, mp, 0, EINVAL);
7904 		return;
7905 	}
7906 
7907 	if (MBLKL(data_mp) < iocp->ioc_count) {
7908 		mblk_t *new_data_mp;
7909 
7910 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
7911 			miocnak(q, mp, 0, ENOMEM);
7912 			return;
7913 		}
7914 		freemsg(data_mp);
7915 		data_mp = new_data_mp;
7916 		mp->b_cont = data_mp;
7917 	}
7918 	match_ire = MATCH_IRE_DSTONLY;
7919 
7920 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
7921 	    end - cur >= sizeof (struct dstinforeq);
7922 	    cur += sizeof (struct dstinforeq)) {
7923 		dir = (struct dstinforeq *)cur;
7924 		daddr = &dir->dir_daddr;
7925 		saddr = &dir->dir_saddr;
7926 
7927 		/*
7928 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
7929 		 * v4 mapped addresses; ire_ftable_lookup_v6()
7930 		 * and ip_select_source_v6() do not.
7931 		 */
7932 		dir->dir_dscope = ip_addr_scope_v6(daddr);
7933 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
7934 
7935 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
7936 		if (isipv4) {
7937 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
7938 			v4setsrc = INADDR_ANY;
7939 			ire = ire_route_recursive_v4(v4daddr, 0, NULL, zoneid,
7940 			    NULL, match_ire, IRR_ALLOCATE, 0, ipst, &v4setsrc,
7941 			    NULL, NULL);
7942 		} else {
7943 			v6setsrc = ipv6_all_zeros;
7944 			ire = ire_route_recursive_v6(daddr, 0, NULL, zoneid,
7945 			    NULL, match_ire, IRR_ALLOCATE, 0, ipst, &v6setsrc,
7946 			    NULL, NULL);
7947 		}
7948 		ASSERT(ire != NULL);
7949 		if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
7950 			ire_refrele(ire);
7951 			dir->dir_dreachable = 0;
7952 
7953 			/* move on to next dst addr */
7954 			continue;
7955 		}
7956 		dir->dir_dreachable = 1;
7957 
7958 		dst_ill = ire_nexthop_ill(ire);
7959 		if (dst_ill == NULL) {
7960 			ire_refrele(ire);
7961 			continue;
7962 		}
7963 
7964 		/* With ipmp we most likely look at the ipmp ill here */
7965 		dir->dir_dmactype = dst_ill->ill_mactype;
7966 
7967 		if (isipv4) {
7968 			ipaddr_t v4saddr;
7969 
7970 			if (ip_select_source_v4(dst_ill, v4setsrc, v4daddr,
7971 			    connp->conn_ixa->ixa_multicast_ifaddr, zoneid, ipst,
7972 			    &v4saddr, NULL, &ipif_flags) != 0) {
7973 				v4saddr = INADDR_ANY;
7974 				ipif_flags = 0;
7975 			}
7976 			IN6_IPADDR_TO_V4MAPPED(v4saddr, saddr);
7977 		} else {
7978 			if (ip_select_source_v6(dst_ill, &v6setsrc, daddr,
7979 			    zoneid, ipst, B_FALSE, IPV6_PREFER_SRC_DEFAULT,
7980 			    saddr, NULL, &ipif_flags) != 0) {
7981 				*saddr = ipv6_all_zeros;
7982 				ipif_flags = 0;
7983 			}
7984 		}
7985 
7986 		dir->dir_sscope = ip_addr_scope_v6(saddr);
7987 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
7988 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
7989 		dir->dir_sdeprecated = (ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
7990 		ire_refrele(ire);
7991 		ill_refrele(dst_ill);
7992 	}
7993 	miocack(q, mp, iocp->ioc_count, 0);
7994 }
7995 
7996 /*
7997  * Check if this is an address assigned to this machine.
7998  * Skips interfaces that are down by using ire checks.
7999  * Translates mapped addresses to v4 addresses and then
8000  * treats them as such, returning true if the v4 address
8001  * associated with this mapped address is configured.
8002  * Note: Applications will have to be careful what they do
8003  * with the response; use of mapped addresses limits
8004  * what can be done with the socket, especially with
8005  * respect to socket options and ioctls - neither IPv4
8006  * options nor IPv6 sticky options/ancillary data options
8007  * may be used.
8008  */
8009 /* ARGSUSED */
8010 int
ip_sioctl_tmyaddr(ipif_t * dummy_ipif,sin_t * dummy_sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * dummy_ifreq)8011 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8012     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8013 {
8014 	struct sioc_addrreq *sia;
8015 	sin_t *sin;
8016 	ire_t *ire;
8017 	mblk_t *mp1;
8018 	zoneid_t zoneid;
8019 	ip_stack_t	*ipst;
8020 
8021 	ip1dbg(("ip_sioctl_tmyaddr"));
8022 
8023 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8024 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8025 	ipst = CONNQ_TO_IPST(q);
8026 
8027 	/* Existence verified in ip_wput_nondata */
8028 	mp1 = mp->b_cont->b_cont;
8029 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8030 	sin = (sin_t *)&sia->sa_addr;
8031 	switch (sin->sin_family) {
8032 	case AF_INET6: {
8033 		sin6_t *sin6 = (sin6_t *)sin;
8034 
8035 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8036 			ipaddr_t v4_addr;
8037 
8038 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8039 			    v4_addr);
8040 			ire = ire_ftable_lookup_v4(v4_addr, 0, 0,
8041 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
8042 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8043 		} else {
8044 			in6_addr_t v6addr;
8045 
8046 			v6addr = sin6->sin6_addr;
8047 			ire = ire_ftable_lookup_v6(&v6addr, 0, 0,
8048 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
8049 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8050 		}
8051 		break;
8052 	}
8053 	case AF_INET: {
8054 		ipaddr_t v4addr;
8055 
8056 		v4addr = sin->sin_addr.s_addr;
8057 		ire = ire_ftable_lookup_v4(v4addr, 0, 0,
8058 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8059 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8060 		break;
8061 	}
8062 	default:
8063 		return (EAFNOSUPPORT);
8064 	}
8065 	if (ire != NULL) {
8066 		sia->sa_res = 1;
8067 		ire_refrele(ire);
8068 	} else {
8069 		sia->sa_res = 0;
8070 	}
8071 	return (0);
8072 }
8073 
8074 /*
8075  * Check if this is an address assigned on-link i.e. neighbor,
8076  * and makes sure it's reachable from the current zone.
8077  * Returns true for my addresses as well.
8078  * Translates mapped addresses to v4 addresses and then
8079  * treats them as such, returning true if the v4 address
8080  * associated with this mapped address is configured.
8081  * Note: Applications will have to be careful what they do
8082  * with the response; use of mapped addresses limits
8083  * what can be done with the socket, especially with
8084  * respect to socket options and ioctls - neither IPv4
8085  * options nor IPv6 sticky options/ancillary data options
8086  * may be used.
8087  */
8088 /* ARGSUSED */
8089 int
ip_sioctl_tonlink(ipif_t * dummy_ipif,sin_t * dummy_sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * duymmy_ifreq)8090 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8091     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
8092 {
8093 	struct sioc_addrreq *sia;
8094 	sin_t *sin;
8095 	mblk_t	*mp1;
8096 	ire_t *ire = NULL;
8097 	zoneid_t zoneid;
8098 	ip_stack_t	*ipst;
8099 
8100 	ip1dbg(("ip_sioctl_tonlink"));
8101 
8102 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8103 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8104 	ipst = CONNQ_TO_IPST(q);
8105 
8106 	/* Existence verified in ip_wput_nondata */
8107 	mp1 = mp->b_cont->b_cont;
8108 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8109 	sin = (sin_t *)&sia->sa_addr;
8110 
8111 	/*
8112 	 * We check for IRE_ONLINK and exclude IRE_BROADCAST|IRE_MULTICAST
8113 	 * to make sure we only look at on-link unicast address.
8114 	 */
8115 	switch (sin->sin_family) {
8116 	case AF_INET6: {
8117 		sin6_t *sin6 = (sin6_t *)sin;
8118 
8119 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8120 			ipaddr_t v4_addr;
8121 
8122 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8123 			    v4_addr);
8124 			if (!CLASSD(v4_addr)) {
8125 				ire = ire_ftable_lookup_v4(v4_addr, 0, 0, 0,
8126 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY,
8127 				    0, ipst, NULL);
8128 			}
8129 		} else {
8130 			in6_addr_t v6addr;
8131 
8132 			v6addr = sin6->sin6_addr;
8133 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
8134 				ire = ire_ftable_lookup_v6(&v6addr, 0, 0, 0,
8135 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY, 0,
8136 				    ipst, NULL);
8137 			}
8138 		}
8139 		break;
8140 	}
8141 	case AF_INET: {
8142 		ipaddr_t v4addr;
8143 
8144 		v4addr = sin->sin_addr.s_addr;
8145 		if (!CLASSD(v4addr)) {
8146 			ire = ire_ftable_lookup_v4(v4addr, 0, 0, 0, NULL,
8147 			    zoneid, NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL);
8148 		}
8149 		break;
8150 	}
8151 	default:
8152 		return (EAFNOSUPPORT);
8153 	}
8154 	sia->sa_res = 0;
8155 	if (ire != NULL) {
8156 		ASSERT(!(ire->ire_type & IRE_MULTICAST));
8157 
8158 		if ((ire->ire_type & IRE_ONLINK) &&
8159 		    !(ire->ire_type & IRE_BROADCAST))
8160 			sia->sa_res = 1;
8161 		ire_refrele(ire);
8162 	}
8163 	return (0);
8164 }
8165 
8166 /*
8167  * TBD: implement when kernel maintaines a list of site prefixes.
8168  */
8169 /* ARGSUSED */
8170 int
ip_sioctl_tmysite(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * ifreq)8171 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8172     ip_ioctl_cmd_t *ipip, void *ifreq)
8173 {
8174 	return (ENXIO);
8175 }
8176 
8177 /* ARP IOCTLs. */
8178 /* ARGSUSED */
8179 int
ip_sioctl_arp(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * dummy_ifreq)8180 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8181     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8182 {
8183 	int		err;
8184 	ipaddr_t	ipaddr;
8185 	struct iocblk	*iocp;
8186 	conn_t		*connp;
8187 	struct arpreq	*ar;
8188 	struct xarpreq	*xar;
8189 	int		arp_flags, flags, alength;
8190 	uchar_t		*lladdr;
8191 	ip_stack_t	*ipst;
8192 	ill_t		*ill = ipif->ipif_ill;
8193 	ill_t		*proxy_ill = NULL;
8194 	ipmp_arpent_t	*entp = NULL;
8195 	boolean_t	proxyarp = B_FALSE;
8196 	boolean_t	if_arp_ioctl = B_FALSE;
8197 	ncec_t		*ncec = NULL;
8198 	nce_t		*nce;
8199 
8200 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8201 	connp = Q_TO_CONN(q);
8202 	ipst = connp->conn_netstack->netstack_ip;
8203 	iocp = (struct iocblk *)mp->b_rptr;
8204 
8205 	if (ipip->ipi_cmd_type == XARP_CMD) {
8206 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
8207 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
8208 		ar = NULL;
8209 
8210 		arp_flags = xar->xarp_flags;
8211 		lladdr = (uchar_t *)LLADDR(&xar->xarp_ha);
8212 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
8213 		/*
8214 		 * Validate against user's link layer address length
8215 		 * input and name and addr length limits.
8216 		 */
8217 		alength = ill->ill_phys_addr_length;
8218 		if (ipip->ipi_cmd == SIOCSXARP) {
8219 			if (alength != xar->xarp_ha.sdl_alen ||
8220 			    (alength + xar->xarp_ha.sdl_nlen >
8221 			    sizeof (xar->xarp_ha.sdl_data)))
8222 				return (EINVAL);
8223 		}
8224 	} else {
8225 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
8226 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
8227 		xar = NULL;
8228 
8229 		arp_flags = ar->arp_flags;
8230 		lladdr = (uchar_t *)ar->arp_ha.sa_data;
8231 		/*
8232 		 * Theoretically, the sa_family could tell us what link
8233 		 * layer type this operation is trying to deal with. By
8234 		 * common usage AF_UNSPEC means ethernet. We'll assume
8235 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
8236 		 * for now. Our new SIOC*XARP ioctls can be used more
8237 		 * generally.
8238 		 *
8239 		 * If the underlying media happens to have a non 6 byte
8240 		 * address, arp module will fail set/get, but the del
8241 		 * operation will succeed.
8242 		 */
8243 		alength = 6;
8244 		if ((ipip->ipi_cmd != SIOCDARP) &&
8245 		    (alength != ill->ill_phys_addr_length)) {
8246 			return (EINVAL);
8247 		}
8248 	}
8249 
8250 	/* Translate ATF* flags to NCE* flags */
8251 	flags = 0;
8252 	if (arp_flags & ATF_AUTHORITY)
8253 		flags |= NCE_F_AUTHORITY;
8254 	if (arp_flags & ATF_PERM)
8255 		flags |= NCE_F_NONUD; /* not subject to aging */
8256 	if (arp_flags & ATF_PUBL)
8257 		flags |= NCE_F_PUBLISH;
8258 
8259 	/*
8260 	 * IPMP ARP special handling:
8261 	 *
8262 	 * 1. Since ARP mappings must appear consistent across the group,
8263 	 *    prohibit changing ARP mappings on the underlying interfaces.
8264 	 *
8265 	 * 2. Since ARP mappings for IPMP data addresses are maintained by
8266 	 *    IP itself, prohibit changing them.
8267 	 *
8268 	 * 3. For proxy ARP, use a functioning hardware address in the group,
8269 	 *    provided one exists.  If one doesn't, just add the entry as-is;
8270 	 *    ipmp_illgrp_refresh_arpent() will refresh it if things change.
8271 	 */
8272 	if (IS_UNDER_IPMP(ill)) {
8273 		if (ipip->ipi_cmd != SIOCGARP && ipip->ipi_cmd != SIOCGXARP)
8274 			return (EPERM);
8275 	}
8276 	if (IS_IPMP(ill)) {
8277 		ipmp_illgrp_t *illg = ill->ill_grp;
8278 
8279 		switch (ipip->ipi_cmd) {
8280 		case SIOCSARP:
8281 		case SIOCSXARP:
8282 			proxy_ill = ipmp_illgrp_find_ill(illg, lladdr, alength);
8283 			if (proxy_ill != NULL) {
8284 				proxyarp = B_TRUE;
8285 				if (!ipmp_ill_is_active(proxy_ill))
8286 					proxy_ill = ipmp_illgrp_next_ill(illg);
8287 				if (proxy_ill != NULL)
8288 					lladdr = proxy_ill->ill_phys_addr;
8289 			}
8290 			/* FALLTHRU */
8291 		}
8292 	}
8293 
8294 	ipaddr = sin->sin_addr.s_addr;
8295 	/*
8296 	 * don't match across illgrp per case (1) and (2).
8297 	 * XXX use IS_IPMP(ill) like ndp_sioc_update?
8298 	 */
8299 	nce = nce_lookup_v4(ill, &ipaddr);
8300 	if (nce != NULL)
8301 		ncec = nce->nce_common;
8302 
8303 	switch (iocp->ioc_cmd) {
8304 	case SIOCDARP:
8305 	case SIOCDXARP: {
8306 		/*
8307 		 * Delete the NCE if any.
8308 		 */
8309 		if (ncec == NULL) {
8310 			iocp->ioc_error = ENXIO;
8311 			break;
8312 		}
8313 		/* Don't allow changes to arp mappings of local addresses. */
8314 		if (NCE_MYADDR(ncec)) {
8315 			nce_refrele(nce);
8316 			return (ENOTSUP);
8317 		}
8318 		iocp->ioc_error = 0;
8319 
8320 		/*
8321 		 * Delete the nce_common which has ncec_ill set to ipmp_ill.
8322 		 * This will delete all the nce entries on the under_ills.
8323 		 */
8324 		ncec_delete(ncec);
8325 		/*
8326 		 * Once the NCE has been deleted, then the ire_dep* consistency
8327 		 * mechanism will find any IRE which depended on the now
8328 		 * condemned NCE (as part of sending packets).
8329 		 * That mechanism handles redirects by deleting redirects
8330 		 * that refer to UNREACHABLE nces.
8331 		 */
8332 		break;
8333 	}
8334 	case SIOCGARP:
8335 	case SIOCGXARP:
8336 		if (ncec != NULL) {
8337 			lladdr = ncec->ncec_lladdr;
8338 			flags = ncec->ncec_flags;
8339 			iocp->ioc_error = 0;
8340 			ip_sioctl_garp_reply(mp, ncec->ncec_ill, lladdr, flags);
8341 		} else {
8342 			iocp->ioc_error = ENXIO;
8343 		}
8344 		break;
8345 	case SIOCSARP:
8346 	case SIOCSXARP:
8347 		/* Don't allow changes to arp mappings of local addresses. */
8348 		if (ncec != NULL && NCE_MYADDR(ncec)) {
8349 			nce_refrele(nce);
8350 			return (ENOTSUP);
8351 		}
8352 
8353 		/* static arp entries will undergo NUD if ATF_PERM is not set */
8354 		flags |= NCE_F_STATIC;
8355 		if (!if_arp_ioctl) {
8356 			ip_nce_lookup_and_update(&ipaddr, NULL, ipst,
8357 			    lladdr, alength, flags);
8358 		} else {
8359 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
8360 			if (ipif != NULL) {
8361 				ip_nce_lookup_and_update(&ipaddr, ipif, ipst,
8362 				    lladdr, alength, flags);
8363 				ipif_refrele(ipif);
8364 			}
8365 		}
8366 		if (nce != NULL) {
8367 			nce_refrele(nce);
8368 			nce = NULL;
8369 		}
8370 		/*
8371 		 * NCE_F_STATIC entries will be added in state ND_REACHABLE
8372 		 * by nce_add_common()
8373 		 */
8374 		err = nce_lookup_then_add_v4(ill, lladdr,
8375 		    ill->ill_phys_addr_length, &ipaddr, flags, ND_UNCHANGED,
8376 		    &nce);
8377 		if (err == EEXIST) {
8378 			ncec = nce->nce_common;
8379 			mutex_enter(&ncec->ncec_lock);
8380 			ncec->ncec_state = ND_REACHABLE;
8381 			ncec->ncec_flags = flags;
8382 			nce_update(ncec, ND_UNCHANGED, lladdr);
8383 			mutex_exit(&ncec->ncec_lock);
8384 			err = 0;
8385 		}
8386 		if (nce != NULL) {
8387 			nce_refrele(nce);
8388 			nce = NULL;
8389 		}
8390 		if (IS_IPMP(ill) && err == 0) {
8391 			entp = ipmp_illgrp_create_arpent(ill->ill_grp,
8392 			    proxyarp, ipaddr, lladdr, ill->ill_phys_addr_length,
8393 			    flags);
8394 			if (entp == NULL || (proxyarp && proxy_ill == NULL)) {
8395 				iocp->ioc_error = (entp == NULL ? ENOMEM : 0);
8396 				break;
8397 			}
8398 		}
8399 		iocp->ioc_error = err;
8400 	}
8401 
8402 	if (nce != NULL) {
8403 		nce_refrele(nce);
8404 	}
8405 
8406 	/*
8407 	 * If we created an IPMP ARP entry, mark that we've notified ARP.
8408 	 */
8409 	if (entp != NULL)
8410 		ipmp_illgrp_mark_arpent(ill->ill_grp, entp);
8411 
8412 	return (iocp->ioc_error);
8413 }
8414 
8415 /*
8416  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
8417  * the associated sin and refhold and return the associated ipif via `ci'.
8418  */
8419 int
ip_extract_arpreq(queue_t * q,mblk_t * mp,const ip_ioctl_cmd_t * ipip,cmd_info_t * ci)8420 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8421     cmd_info_t *ci)
8422 {
8423 	mblk_t	*mp1;
8424 	sin_t	*sin;
8425 	conn_t	*connp;
8426 	ipif_t	*ipif;
8427 	ire_t	*ire = NULL;
8428 	ill_t	*ill = NULL;
8429 	boolean_t exists;
8430 	ip_stack_t *ipst;
8431 	struct arpreq *ar;
8432 	struct xarpreq *xar;
8433 	struct sockaddr_dl *sdl;
8434 
8435 	/* ioctl comes down on a conn */
8436 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8437 	connp = Q_TO_CONN(q);
8438 	if (connp->conn_family == AF_INET6)
8439 		return (ENXIO);
8440 
8441 	ipst = connp->conn_netstack->netstack_ip;
8442 
8443 	/* Verified in ip_wput_nondata */
8444 	mp1 = mp->b_cont->b_cont;
8445 
8446 	if (ipip->ipi_cmd_type == XARP_CMD) {
8447 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
8448 		xar = (struct xarpreq *)mp1->b_rptr;
8449 		sin = (sin_t *)&xar->xarp_pa;
8450 		sdl = &xar->xarp_ha;
8451 
8452 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
8453 			return (ENXIO);
8454 		if (sdl->sdl_nlen >= LIFNAMSIZ)
8455 			return (EINVAL);
8456 	} else {
8457 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
8458 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
8459 		ar = (struct arpreq *)mp1->b_rptr;
8460 		sin = (sin_t *)&ar->arp_pa;
8461 	}
8462 
8463 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
8464 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
8465 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, ipst);
8466 		if (ipif == NULL)
8467 			return (ENXIO);
8468 		if (ipif->ipif_id != 0) {
8469 			ipif_refrele(ipif);
8470 			return (ENXIO);
8471 		}
8472 	} else {
8473 		/*
8474 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with an sdl_nlen
8475 		 * of 0: use the IP address to find the ipif.  If the IP
8476 		 * address is an IPMP test address, ire_ftable_lookup() will
8477 		 * find the wrong ill, so we first do an ipif_lookup_addr().
8478 		 */
8479 		ipif = ipif_lookup_addr(sin->sin_addr.s_addr, NULL, ALL_ZONES,
8480 		    ipst);
8481 		if (ipif == NULL) {
8482 			ire = ire_ftable_lookup_v4(sin->sin_addr.s_addr,
8483 			    0, 0, IRE_IF_RESOLVER, NULL, ALL_ZONES,
8484 			    NULL, MATCH_IRE_TYPE, 0, ipst, NULL);
8485 			if (ire == NULL || ((ill = ire->ire_ill) == NULL)) {
8486 				if (ire != NULL)
8487 					ire_refrele(ire);
8488 				return (ENXIO);
8489 			}
8490 			ASSERT(ire != NULL && ill != NULL);
8491 			ipif = ill->ill_ipif;
8492 			ipif_refhold(ipif);
8493 			ire_refrele(ire);
8494 		}
8495 	}
8496 
8497 	if (ipif->ipif_ill->ill_net_type != IRE_IF_RESOLVER) {
8498 		ipif_refrele(ipif);
8499 		return (ENXIO);
8500 	}
8501 
8502 	ci->ci_sin = sin;
8503 	ci->ci_ipif = ipif;
8504 	return (0);
8505 }
8506 
8507 /*
8508  * Link or unlink the illgrp on IPMP meta-interface `ill' depending on the
8509  * value of `ioccmd'.  While an illgrp is linked to an ipmp_grp_t, it is
8510  * accessible from that ipmp_grp_t, which means SIOCSLIFGROUPNAME can look it
8511  * up and thus an ill can join that illgrp.
8512  *
8513  * We use I_PLINK/I_PUNLINK to do the link/unlink operations rather than
8514  * open()/close() primarily because close() is not allowed to fail or block
8515  * forever.  On the other hand, I_PUNLINK *can* fail, and there's no reason
8516  * why anyone should ever need to I_PUNLINK an in-use IPMP stream.  To ensure
8517  * symmetric behavior (e.g., doing an I_PLINK after and I_PUNLINK undoes the
8518  * I_PUNLINK) we defer linking to I_PLINK.  Separately, we also fail attempts
8519  * to I_LINK since I_UNLINK is optional and we'd end up in an inconsistent
8520  * state if I_UNLINK didn't occur.
8521  *
8522  * Note that for each plumb/unplumb operation, we may end up here more than
8523  * once because of the way ifconfig works.  However, it's OK to link the same
8524  * illgrp more than once, or unlink an illgrp that's already unlinked.
8525  */
8526 static int
ip_sioctl_plink_ipmp(ill_t * ill,int ioccmd)8527 ip_sioctl_plink_ipmp(ill_t *ill, int ioccmd)
8528 {
8529 	int err;
8530 	ip_stack_t *ipst = ill->ill_ipst;
8531 
8532 	ASSERT(IS_IPMP(ill));
8533 	ASSERT(IAM_WRITER_ILL(ill));
8534 
8535 	switch (ioccmd) {
8536 	case I_LINK:
8537 		return (ENOTSUP);
8538 
8539 	case I_PLINK:
8540 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8541 		ipmp_illgrp_link_grp(ill->ill_grp, ill->ill_phyint->phyint_grp);
8542 		rw_exit(&ipst->ips_ipmp_lock);
8543 		break;
8544 
8545 	case I_PUNLINK:
8546 		/*
8547 		 * Require all UP ipifs be brought down prior to unlinking the
8548 		 * illgrp so any associated IREs (and other state) is torched.
8549 		 */
8550 		if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0)
8551 			return (EBUSY);
8552 
8553 		/*
8554 		 * NOTE: We hold ipmp_lock across the unlink to prevent a race
8555 		 * with an SIOCSLIFGROUPNAME request from an ill trying to
8556 		 * join this group.  Specifically: ills trying to join grab
8557 		 * ipmp_lock and bump a "pending join" counter checked by
8558 		 * ipmp_illgrp_unlink_grp().  During the unlink no new pending
8559 		 * joins can occur (since we have ipmp_lock).  Once we drop
8560 		 * ipmp_lock, subsequent SIOCSLIFGROUPNAME requests will not
8561 		 * find the illgrp (since we unlinked it) and will return
8562 		 * EAFNOSUPPORT.  This will then take them back through the
8563 		 * IPMP meta-interface plumbing logic in ifconfig, and thus
8564 		 * back through I_PLINK above.
8565 		 */
8566 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8567 		err = ipmp_illgrp_unlink_grp(ill->ill_grp);
8568 		rw_exit(&ipst->ips_ipmp_lock);
8569 		return (err);
8570 	default:
8571 		break;
8572 	}
8573 	return (0);
8574 }
8575 
8576 /*
8577  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
8578  * atomically set/clear the muxids. Also complete the ioctl by acking or
8579  * naking it.  Note that the code is structured such that the link type,
8580  * whether it's persistent or not, is treated equally.  ifconfig(8) and
8581  * its clones use the persistent link, while pppd(8) and perhaps many
8582  * other daemons may use non-persistent link.  When combined with some
8583  * ill_t states, linking and unlinking lower streams may be used as
8584  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
8585  */
8586 /* ARGSUSED */
8587 void
ip_sioctl_plink(ipsq_t * ipsq,queue_t * q,mblk_t * mp,void * dummy_arg)8588 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8589 {
8590 	mblk_t		*mp1;
8591 	struct linkblk	*li;
8592 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
8593 	int		err = 0;
8594 
8595 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
8596 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
8597 
8598 	mp1 = mp->b_cont;	/* This is the linkblk info */
8599 	li = (struct linkblk *)mp1->b_rptr;
8600 
8601 	err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li);
8602 	if (err == EINPROGRESS)
8603 		return;
8604 	if (err == 0)
8605 		miocack(q, mp, 0, 0);
8606 	else
8607 		miocnak(q, mp, 0, err);
8608 
8609 	/* Conn was refheld in ip_sioctl_copyin_setup */
8610 	if (CONN_Q(q)) {
8611 		CONN_DEC_IOCTLREF(Q_TO_CONN(q));
8612 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
8613 	}
8614 }
8615 
8616 /*
8617  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
8618  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
8619  * module stream).
8620  * Returns zero on success, EINPROGRESS if the operation is still pending, or
8621  * an error code on failure.
8622  */
8623 static int
ip_sioctl_plink_ipmod(ipsq_t * ipsq,queue_t * q,mblk_t * mp,int ioccmd,struct linkblk * li)8624 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
8625     struct linkblk *li)
8626 {
8627 	int		err = 0;
8628 	ill_t		*ill;
8629 	queue_t		*ipwq, *dwq;
8630 	const char	*name;
8631 	struct qinit	*qinfo;
8632 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
8633 	boolean_t	entered_ipsq = B_FALSE;
8634 	boolean_t	is_ip = B_FALSE;
8635 	arl_t		*arl;
8636 
8637 	/*
8638 	 * Walk the lower stream to verify it's the IP module stream.
8639 	 * The IP module is identified by its name, wput function,
8640 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
8641 	 * (li->l_qbot) will not vanish until this ioctl completes.
8642 	 */
8643 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
8644 		qinfo = ipwq->q_qinfo;
8645 		name = qinfo->qi_minfo->mi_idname;
8646 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
8647 		    qinfo->qi_putp != ip_lwput && ipwq->q_next != NULL) {
8648 			is_ip = B_TRUE;
8649 			break;
8650 		}
8651 		if (name != NULL && strcmp(name, arp_mod_info.mi_idname) == 0 &&
8652 		    qinfo->qi_putp != ip_lwput && ipwq->q_next != NULL) {
8653 			break;
8654 		}
8655 	}
8656 
8657 	/*
8658 	 * If this isn't an IP module stream, bail.
8659 	 */
8660 	if (ipwq == NULL)
8661 		return (0);
8662 
8663 	if (!is_ip) {
8664 		arl = (arl_t *)ipwq->q_ptr;
8665 		ill = arl_to_ill(arl);
8666 		if (ill == NULL)
8667 			return (0);
8668 	} else {
8669 		ill = ipwq->q_ptr;
8670 	}
8671 	ASSERT(ill != NULL);
8672 
8673 	if (ipsq == NULL) {
8674 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
8675 		    NEW_OP, B_FALSE);
8676 		if (ipsq == NULL) {
8677 			if (!is_ip)
8678 				ill_refrele(ill);
8679 			return (EINPROGRESS);
8680 		}
8681 		entered_ipsq = B_TRUE;
8682 	}
8683 	ASSERT(IAM_WRITER_ILL(ill));
8684 	mutex_enter(&ill->ill_lock);
8685 	if (!is_ip) {
8686 		if (islink && ill->ill_muxid == 0) {
8687 			/*
8688 			 * Plumbing has to be done with IP plumbed first, arp
8689 			 * second, but here we have arp being plumbed first.
8690 			 */
8691 			mutex_exit(&ill->ill_lock);
8692 			if (entered_ipsq)
8693 				ipsq_exit(ipsq);
8694 			ill_refrele(ill);
8695 			return (EINVAL);
8696 		}
8697 	}
8698 	mutex_exit(&ill->ill_lock);
8699 	if (!is_ip) {
8700 		arl->arl_muxid = islink ? li->l_index : 0;
8701 		ill_refrele(ill);
8702 		goto done;
8703 	}
8704 
8705 	if (IS_IPMP(ill) && (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
8706 		goto done;
8707 
8708 	/*
8709 	 * As part of I_{P}LINKing, stash the number of downstream modules and
8710 	 * the read queue of the module immediately below IP in the ill.
8711 	 * These are used during the capability negotiation below.
8712 	 */
8713 	ill->ill_lmod_rq = NULL;
8714 	ill->ill_lmod_cnt = 0;
8715 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
8716 		ill->ill_lmod_rq = RD(dwq);
8717 		for (; dwq != NULL; dwq = dwq->q_next)
8718 			ill->ill_lmod_cnt++;
8719 	}
8720 
8721 	ill->ill_muxid = islink ? li->l_index : 0;
8722 
8723 	/*
8724 	 * Mark the ipsq busy until the capability operations initiated below
8725 	 * complete. The PLINK/UNLINK ioctl itself completes when our caller
8726 	 * returns, but the capability operation may complete asynchronously
8727 	 * much later.
8728 	 */
8729 	ipsq_current_start(ipsq, ill->ill_ipif, ioccmd);
8730 	/*
8731 	 * If there's at least one up ipif on this ill, then we're bound to
8732 	 * the underlying driver via DLPI.  In that case, renegotiate
8733 	 * capabilities to account for any possible change in modules
8734 	 * interposed between IP and the driver.
8735 	 */
8736 	if (ill->ill_ipif_up_count > 0) {
8737 		if (islink)
8738 			ill_capability_probe(ill);
8739 		else
8740 			ill_capability_reset(ill, B_FALSE);
8741 	}
8742 	ipsq_current_finish(ipsq);
8743 done:
8744 	if (entered_ipsq)
8745 		ipsq_exit(ipsq);
8746 
8747 	return (err);
8748 }
8749 
8750 /*
8751  * Search the ioctl command in the ioctl tables and return a pointer
8752  * to the ioctl command information. The ioctl command tables are
8753  * static and fully populated at compile time.
8754  */
8755 ip_ioctl_cmd_t *
ip_sioctl_lookup(int ioc_cmd)8756 ip_sioctl_lookup(int ioc_cmd)
8757 {
8758 	int index;
8759 	ip_ioctl_cmd_t *ipip;
8760 	ip_ioctl_cmd_t *ipip_end;
8761 
8762 	if (ioc_cmd == IPI_DONTCARE)
8763 		return (NULL);
8764 
8765 	/*
8766 	 * Do a 2 step search. First search the indexed table
8767 	 * based on the least significant byte of the ioctl cmd.
8768 	 * If we don't find a match, then search the misc table
8769 	 * serially.
8770 	 */
8771 	index = ioc_cmd & 0xFF;
8772 	if (index < ip_ndx_ioctl_count) {
8773 		ipip = &ip_ndx_ioctl_table[index];
8774 		if (ipip->ipi_cmd == ioc_cmd) {
8775 			/* Found a match in the ndx table */
8776 			return (ipip);
8777 		}
8778 	}
8779 
8780 	/* Search the misc table */
8781 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
8782 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
8783 		if (ipip->ipi_cmd == ioc_cmd)
8784 			/* Found a match in the misc table */
8785 			return (ipip);
8786 	}
8787 
8788 	return (NULL);
8789 }
8790 
8791 /*
8792  * helper function for ip_sioctl_getsetprop(), which does some sanity checks
8793  */
8794 static boolean_t
getset_ioctl_checks(mblk_t * mp)8795 getset_ioctl_checks(mblk_t *mp)
8796 {
8797 	struct iocblk	*iocp = (struct iocblk *)mp->b_rptr;
8798 	mblk_t		*mp1 = mp->b_cont;
8799 	mod_ioc_prop_t	*pioc;
8800 	uint_t		flags;
8801 	uint_t		pioc_size;
8802 
8803 	/* do sanity checks on various arguments */
8804 	if (mp1 == NULL || iocp->ioc_count == 0 ||
8805 	    iocp->ioc_count == TRANSPARENT) {
8806 		return (B_FALSE);
8807 	}
8808 	if (msgdsize(mp1) < iocp->ioc_count) {
8809 		if (!pullupmsg(mp1, iocp->ioc_count))
8810 			return (B_FALSE);
8811 	}
8812 
8813 	pioc = (mod_ioc_prop_t *)mp1->b_rptr;
8814 
8815 	/* sanity checks on mpr_valsize */
8816 	pioc_size = sizeof (mod_ioc_prop_t);
8817 	if (pioc->mpr_valsize != 0)
8818 		pioc_size += pioc->mpr_valsize - 1;
8819 
8820 	if (iocp->ioc_count != pioc_size)
8821 		return (B_FALSE);
8822 
8823 	flags = pioc->mpr_flags;
8824 	if (iocp->ioc_cmd == SIOCSETPROP) {
8825 		/*
8826 		 * One can either reset the value to it's default value or
8827 		 * change the current value or append/remove the value from
8828 		 * a multi-valued properties.
8829 		 */
8830 		if ((flags & MOD_PROP_DEFAULT) != MOD_PROP_DEFAULT &&
8831 		    flags != MOD_PROP_ACTIVE &&
8832 		    flags != (MOD_PROP_ACTIVE|MOD_PROP_APPEND) &&
8833 		    flags != (MOD_PROP_ACTIVE|MOD_PROP_REMOVE))
8834 			return (B_FALSE);
8835 	} else {
8836 		ASSERT(iocp->ioc_cmd == SIOCGETPROP);
8837 
8838 		/*
8839 		 * One can retrieve only one kind of property information
8840 		 * at a time.
8841 		 */
8842 		if ((flags & MOD_PROP_ACTIVE) != MOD_PROP_ACTIVE &&
8843 		    (flags & MOD_PROP_DEFAULT) != MOD_PROP_DEFAULT &&
8844 		    (flags & MOD_PROP_POSSIBLE) != MOD_PROP_POSSIBLE &&
8845 		    (flags & MOD_PROP_PERM) != MOD_PROP_PERM)
8846 			return (B_FALSE);
8847 	}
8848 
8849 	return (B_TRUE);
8850 }
8851 
8852 /*
8853  * process the SIOC{SET|GET}PROP ioctl's
8854  */
8855 /* ARGSUSED */
8856 static void
ip_sioctl_getsetprop(queue_t * q,mblk_t * mp)8857 ip_sioctl_getsetprop(queue_t *q, mblk_t *mp)
8858 {
8859 	struct iocblk	*iocp = (struct iocblk *)mp->b_rptr;
8860 	mblk_t		*mp1 = mp->b_cont;
8861 	mod_ioc_prop_t	*pioc;
8862 	mod_prop_info_t *ptbl = NULL, *pinfo = NULL;
8863 	ip_stack_t	*ipst;
8864 	netstack_t	*stack;
8865 	cred_t		*cr;
8866 	boolean_t	set;
8867 	int		err;
8868 
8869 	ASSERT(q->q_next == NULL);
8870 	ASSERT(CONN_Q(q));
8871 
8872 	if (!getset_ioctl_checks(mp)) {
8873 		miocnak(q, mp, 0, EINVAL);
8874 		return;
8875 	}
8876 	ipst = CONNQ_TO_IPST(q);
8877 	stack = ipst->ips_netstack;
8878 	pioc = (mod_ioc_prop_t *)mp1->b_rptr;
8879 
8880 	switch (pioc->mpr_proto) {
8881 	case MOD_PROTO_IP:
8882 	case MOD_PROTO_IPV4:
8883 	case MOD_PROTO_IPV6:
8884 		ptbl = ipst->ips_propinfo_tbl;
8885 		break;
8886 	case MOD_PROTO_RAWIP:
8887 		ptbl = stack->netstack_icmp->is_propinfo_tbl;
8888 		break;
8889 	case MOD_PROTO_TCP:
8890 		ptbl = stack->netstack_tcp->tcps_propinfo_tbl;
8891 		break;
8892 	case MOD_PROTO_UDP:
8893 		ptbl = stack->netstack_udp->us_propinfo_tbl;
8894 		break;
8895 	case MOD_PROTO_SCTP:
8896 		ptbl = stack->netstack_sctp->sctps_propinfo_tbl;
8897 		break;
8898 	default:
8899 		miocnak(q, mp, 0, EINVAL);
8900 		return;
8901 	}
8902 
8903 	pinfo = mod_prop_lookup(ptbl, pioc->mpr_name, pioc->mpr_proto);
8904 	if (pinfo == NULL) {
8905 		miocnak(q, mp, 0, ENOENT);
8906 		return;
8907 	}
8908 
8909 	set = (iocp->ioc_cmd == SIOCSETPROP) ? B_TRUE : B_FALSE;
8910 	if (set && pinfo->mpi_setf != NULL) {
8911 		cr = msg_getcred(mp, NULL);
8912 		if (cr == NULL)
8913 			cr = iocp->ioc_cr;
8914 		err = pinfo->mpi_setf(stack, cr, pinfo, pioc->mpr_ifname,
8915 		    pioc->mpr_val, pioc->mpr_flags);
8916 	} else if (!set && pinfo->mpi_getf != NULL) {
8917 		err = pinfo->mpi_getf(stack, pinfo, pioc->mpr_ifname,
8918 		    pioc->mpr_val, pioc->mpr_valsize, pioc->mpr_flags);
8919 	} else {
8920 		err = EPERM;
8921 	}
8922 
8923 	if (err != 0) {
8924 		miocnak(q, mp, 0, err);
8925 	} else {
8926 		if (set)
8927 			miocack(q, mp, 0, 0);
8928 		else    /* For get, we need to return back the data */
8929 			miocack(q, mp, iocp->ioc_count, 0);
8930 	}
8931 }
8932 
8933 /*
8934  * process the legacy ND_GET, ND_SET ioctl just for {ip|ip6}_forwarding
8935  * as several routing daemons have unfortunately used this 'unpublished'
8936  * but well-known ioctls.
8937  */
8938 /* ARGSUSED */
8939 static void
ip_process_legacy_nddprop(queue_t * q,mblk_t * mp)8940 ip_process_legacy_nddprop(queue_t *q, mblk_t *mp)
8941 {
8942 	struct iocblk	*iocp = (struct iocblk *)mp->b_rptr;
8943 	mblk_t		*mp1 = mp->b_cont;
8944 	char		*pname, *pval, *buf;
8945 	uint_t		bufsize, proto;
8946 	mod_prop_info_t *pinfo = NULL;
8947 	ip_stack_t	*ipst;
8948 	int		err = 0;
8949 
8950 	ASSERT(CONN_Q(q));
8951 	ipst = CONNQ_TO_IPST(q);
8952 
8953 	if (iocp->ioc_count == 0 || mp1 == NULL) {
8954 		miocnak(q, mp, 0, EINVAL);
8955 		return;
8956 	}
8957 
8958 	mp1->b_datap->db_lim[-1] = '\0';	/* Force null termination */
8959 	pval = buf = pname = (char *)mp1->b_rptr;
8960 	bufsize = MBLKL(mp1);
8961 
8962 	if (strcmp(pname, "ip_forwarding") == 0) {
8963 		pname = "forwarding";
8964 		proto = MOD_PROTO_IPV4;
8965 	} else if (strcmp(pname, "ip6_forwarding") == 0) {
8966 		pname = "forwarding";
8967 		proto = MOD_PROTO_IPV6;
8968 	} else {
8969 		miocnak(q, mp, 0, EINVAL);
8970 		return;
8971 	}
8972 
8973 	pinfo = mod_prop_lookup(ipst->ips_propinfo_tbl, pname, proto);
8974 
8975 	switch (iocp->ioc_cmd) {
8976 	case ND_GET:
8977 		if ((err = pinfo->mpi_getf(ipst->ips_netstack, pinfo, NULL, buf,
8978 		    bufsize, 0)) == 0) {
8979 			miocack(q, mp, iocp->ioc_count, 0);
8980 			return;
8981 		}
8982 		break;
8983 	case ND_SET:
8984 		/*
8985 		 * buffer will have property name and value in the following
8986 		 * format,
8987 		 * <property name>'\0'<property value>'\0', extract them;
8988 		 */
8989 		while (*pval++)
8990 			noop;
8991 
8992 		if (!*pval || pval >= (char *)mp1->b_wptr) {
8993 			err = EINVAL;
8994 		} else if ((err = pinfo->mpi_setf(ipst->ips_netstack, NULL,
8995 		    pinfo, NULL, pval, 0)) == 0) {
8996 			miocack(q, mp, 0, 0);
8997 			return;
8998 		}
8999 		break;
9000 	default:
9001 		err = EINVAL;
9002 		break;
9003 	}
9004 	miocnak(q, mp, 0, err);
9005 }
9006 
9007 /*
9008  * Wrapper function for resuming deferred ioctl processing
9009  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
9010  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
9011  */
9012 /* ARGSUSED */
9013 void
ip_sioctl_copyin_resume(ipsq_t * dummy_ipsq,queue_t * q,mblk_t * mp,void * dummy_arg)9014 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
9015     void *dummy_arg)
9016 {
9017 	ip_sioctl_copyin_setup(q, mp);
9018 }
9019 
9020 /*
9021  * ip_sioctl_copyin_setup is called by ip_wput_nondata with any M_IOCTL message
9022  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
9023  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
9024  * We establish here the size of the block to be copied in.  mi_copyin
9025  * arranges for this to happen, an processing continues in ip_wput_nondata with
9026  * an M_IOCDATA message.
9027  */
9028 void
ip_sioctl_copyin_setup(queue_t * q,mblk_t * mp)9029 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
9030 {
9031 	int	copyin_size;
9032 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9033 	ip_ioctl_cmd_t *ipip;
9034 	cred_t *cr;
9035 	ip_stack_t	*ipst;
9036 
9037 	if (CONN_Q(q))
9038 		ipst = CONNQ_TO_IPST(q);
9039 	else
9040 		ipst = ILLQ_TO_IPST(q);
9041 
9042 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
9043 	if (ipip == NULL) {
9044 		/*
9045 		 * The ioctl is not one we understand or own.
9046 		 * Pass it along to be processed down stream,
9047 		 * if this is a module instance of IP, else nak
9048 		 * the ioctl.
9049 		 */
9050 		if (q->q_next == NULL) {
9051 			goto nak;
9052 		} else {
9053 			putnext(q, mp);
9054 			return;
9055 		}
9056 	}
9057 
9058 	/*
9059 	 * If this is deferred, then we will do all the checks when we
9060 	 * come back.
9061 	 */
9062 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
9063 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
9064 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
9065 		return;
9066 	}
9067 
9068 	/*
9069 	 * Only allow a very small subset of IP ioctls on this stream if
9070 	 * IP is a module and not a driver. Allowing ioctls to be processed
9071 	 * in this case may cause assert failures or data corruption.
9072 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
9073 	 * ioctls allowed on an IP module stream, after which this stream
9074 	 * normally becomes a multiplexor (at which time the stream head
9075 	 * will fail all ioctls).
9076 	 */
9077 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
9078 		goto nak;
9079 	}
9080 
9081 	/* Make sure we have ioctl data to process. */
9082 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
9083 		goto nak;
9084 
9085 	/*
9086 	 * Prefer dblk credential over ioctl credential; some synthesized
9087 	 * ioctls have kcred set because there's no way to crhold()
9088 	 * a credential in some contexts.  (ioc_cr is not crfree() by
9089 	 * the framework; the caller of ioctl needs to hold the reference
9090 	 * for the duration of the call).
9091 	 */
9092 	cr = msg_getcred(mp, NULL);
9093 	if (cr == NULL)
9094 		cr = iocp->ioc_cr;
9095 
9096 	/* Make sure normal users don't send down privileged ioctls */
9097 	if ((ipip->ipi_flags & IPI_PRIV) &&
9098 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
9099 		/* We checked the privilege earlier but log it here */
9100 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
9101 		return;
9102 	}
9103 
9104 	/*
9105 	 * The ioctl command tables can only encode fixed length
9106 	 * ioctl data. If the length is variable, the table will
9107 	 * encode the length as zero. Such special cases are handled
9108 	 * below in the switch.
9109 	 */
9110 	if (ipip->ipi_copyin_size != 0) {
9111 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
9112 		return;
9113 	}
9114 
9115 	switch (iocp->ioc_cmd) {
9116 	case O_SIOCGIFCONF:
9117 	case SIOCGIFCONF:
9118 		/*
9119 		 * This IOCTL is hilarious.  See comments in
9120 		 * ip_sioctl_get_ifconf for the story.
9121 		 */
9122 		if (iocp->ioc_count == TRANSPARENT)
9123 			copyin_size = SIZEOF_STRUCT(ifconf,
9124 			    iocp->ioc_flag);
9125 		else
9126 			copyin_size = iocp->ioc_count;
9127 		mi_copyin(q, mp, NULL, copyin_size);
9128 		return;
9129 
9130 	case O_SIOCGLIFCONF:
9131 	case SIOCGLIFCONF:
9132 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
9133 		mi_copyin(q, mp, NULL, copyin_size);
9134 		return;
9135 
9136 	case SIOCGLIFSRCOF:
9137 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
9138 		mi_copyin(q, mp, NULL, copyin_size);
9139 		return;
9140 
9141 	case SIOCGIP6ADDRPOLICY:
9142 		ip_sioctl_ip6addrpolicy(q, mp);
9143 		ip6_asp_table_refrele(ipst);
9144 		return;
9145 
9146 	case SIOCSIP6ADDRPOLICY:
9147 		ip_sioctl_ip6addrpolicy(q, mp);
9148 		return;
9149 
9150 	case SIOCGDSTINFO:
9151 		ip_sioctl_dstinfo(q, mp);
9152 		ip6_asp_table_refrele(ipst);
9153 		return;
9154 
9155 	case ND_SET:
9156 	case ND_GET:
9157 		ip_process_legacy_nddprop(q, mp);
9158 		return;
9159 
9160 	case SIOCSETPROP:
9161 	case SIOCGETPROP:
9162 		ip_sioctl_getsetprop(q, mp);
9163 		return;
9164 
9165 	case I_PLINK:
9166 	case I_PUNLINK:
9167 	case I_LINK:
9168 	case I_UNLINK:
9169 		/*
9170 		 * We treat non-persistent link similarly as the persistent
9171 		 * link case, in terms of plumbing/unplumbing, as well as
9172 		 * dynamic re-plumbing events indicator.  See comments
9173 		 * in ip_sioctl_plink() for more.
9174 		 *
9175 		 * Request can be enqueued in the 'ipsq' while waiting
9176 		 * to become exclusive. So bump up the conn ref.
9177 		 */
9178 		if (CONN_Q(q)) {
9179 			CONN_INC_REF(Q_TO_CONN(q));
9180 			CONN_INC_IOCTLREF(Q_TO_CONN(q))
9181 		}
9182 		ip_sioctl_plink(NULL, q, mp, NULL);
9183 		return;
9184 
9185 	case IP_IOCTL:
9186 		ip_wput_ioctl(q, mp);
9187 		return;
9188 
9189 	case SIOCILB:
9190 		/* The ioctl length varies depending on the ILB command. */
9191 		copyin_size = iocp->ioc_count;
9192 		if (copyin_size < sizeof (ilb_cmd_t))
9193 			goto nak;
9194 		mi_copyin(q, mp, NULL, copyin_size);
9195 		return;
9196 
9197 	default:
9198 		cmn_err(CE_WARN, "Unknown ioctl %d/0x%x slipped through.",
9199 		    iocp->ioc_cmd, iocp->ioc_cmd);
9200 		/* FALLTHRU */
9201 	}
9202 nak:
9203 	if (mp->b_cont != NULL) {
9204 		freemsg(mp->b_cont);
9205 		mp->b_cont = NULL;
9206 	}
9207 	iocp->ioc_error = EINVAL;
9208 	mp->b_datap->db_type = M_IOCNAK;
9209 	iocp->ioc_count = 0;
9210 	qreply(q, mp);
9211 }
9212 
9213 static void
ip_sioctl_garp_reply(mblk_t * mp,ill_t * ill,void * hwaddr,int flags)9214 ip_sioctl_garp_reply(mblk_t *mp, ill_t *ill, void *hwaddr, int flags)
9215 {
9216 	struct arpreq *ar;
9217 	struct xarpreq *xar;
9218 	mblk_t	*tmp;
9219 	struct iocblk *iocp;
9220 	int x_arp_ioctl = B_FALSE;
9221 	int *flagsp;
9222 	char *storage = NULL;
9223 
9224 	ASSERT(ill != NULL);
9225 
9226 	iocp = (struct iocblk *)mp->b_rptr;
9227 	ASSERT(iocp->ioc_cmd == SIOCGXARP || iocp->ioc_cmd == SIOCGARP);
9228 
9229 	tmp = (mp->b_cont)->b_cont; /* xarpreq/arpreq */
9230 	if ((iocp->ioc_cmd == SIOCGXARP) ||
9231 	    (iocp->ioc_cmd == SIOCSXARP)) {
9232 		x_arp_ioctl = B_TRUE;
9233 		xar = (struct xarpreq *)tmp->b_rptr;
9234 		flagsp = &xar->xarp_flags;
9235 		storage = xar->xarp_ha.sdl_data;
9236 	} else {
9237 		ar = (struct arpreq *)tmp->b_rptr;
9238 		flagsp = &ar->arp_flags;
9239 		storage = ar->arp_ha.sa_data;
9240 	}
9241 
9242 	/*
9243 	 * We're done if this is not an SIOCG{X}ARP
9244 	 */
9245 	if (x_arp_ioctl) {
9246 		storage += ill_xarp_info(&xar->xarp_ha, ill);
9247 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
9248 		    sizeof (xar->xarp_ha.sdl_data)) {
9249 			iocp->ioc_error = EINVAL;
9250 			return;
9251 		}
9252 	}
9253 	*flagsp = ATF_INUSE;
9254 	/*
9255 	 * If /sbin/arp told us we are the authority using the "permanent"
9256 	 * flag, or if this is one of my addresses print "permanent"
9257 	 * in the /sbin/arp output.
9258 	 */
9259 	if ((flags & NCE_F_MYADDR) || (flags & NCE_F_AUTHORITY))
9260 		*flagsp |= ATF_AUTHORITY;
9261 	if (flags & NCE_F_NONUD)
9262 		*flagsp |= ATF_PERM; /* not subject to aging */
9263 	if (flags & NCE_F_PUBLISH)
9264 		*flagsp |= ATF_PUBL;
9265 	if (hwaddr != NULL) {
9266 		*flagsp |= ATF_COM;
9267 		bcopy((char *)hwaddr, storage, ill->ill_phys_addr_length);
9268 	}
9269 }
9270 
9271 /*
9272  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
9273  * interface) create the next available logical interface for this
9274  * physical interface.
9275  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
9276  * ipif with the specified name.
9277  *
9278  * If the address family is not AF_UNSPEC then set the address as well.
9279  *
9280  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
9281  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
9282  *
9283  * Executed as a writer on the ill.
9284  * So no lock is needed to traverse the ipif chain, or examine the
9285  * phyint flags.
9286  */
9287 /* ARGSUSED */
9288 int
ip_sioctl_addif(ipif_t * dummy_ipif,sin_t * dummy_sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * dummy_ipip,void * dummy_ifreq)9289 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9290     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9291 {
9292 	mblk_t	*mp1;
9293 	struct lifreq *lifr;
9294 	boolean_t	isv6;
9295 	boolean_t	exists;
9296 	char	*name;
9297 	char	*endp;
9298 	char	*cp;
9299 	int	namelen;
9300 	ipif_t	*ipif;
9301 	long	id;
9302 	ipsq_t	*ipsq;
9303 	ill_t	*ill;
9304 	sin_t	*sin;
9305 	int	err = 0;
9306 	boolean_t found_sep = B_FALSE;
9307 	conn_t	*connp;
9308 	zoneid_t zoneid;
9309 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
9310 
9311 	ASSERT(q->q_next == NULL);
9312 	ip1dbg(("ip_sioctl_addif\n"));
9313 	/* Existence of mp1 has been checked in ip_wput_nondata */
9314 	mp1 = mp->b_cont->b_cont;
9315 	/*
9316 	 * Null terminate the string to protect against buffer
9317 	 * overrun. String was generated by user code and may not
9318 	 * be trusted.
9319 	 */
9320 	lifr = (struct lifreq *)mp1->b_rptr;
9321 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
9322 	name = lifr->lifr_name;
9323 	ASSERT(CONN_Q(q));
9324 	connp = Q_TO_CONN(q);
9325 	isv6 = (connp->conn_family == AF_INET6);
9326 	zoneid = connp->conn_zoneid;
9327 	namelen = mi_strlen(name);
9328 	if (namelen == 0)
9329 		return (EINVAL);
9330 
9331 	exists = B_FALSE;
9332 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
9333 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
9334 		/*
9335 		 * Allow creating lo0 using SIOCLIFADDIF.
9336 		 * can't be any other writer thread. So can pass null below
9337 		 * for the last 4 args to ipif_lookup_name.
9338 		 */
9339 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
9340 		    &exists, isv6, zoneid, ipst);
9341 		/* Prevent any further action */
9342 		if (ipif == NULL) {
9343 			return (ENOBUFS);
9344 		} else if (!exists) {
9345 			/* We created the ipif now and as writer */
9346 			ipif_refrele(ipif);
9347 			return (0);
9348 		} else {
9349 			ill = ipif->ipif_ill;
9350 			ill_refhold(ill);
9351 			ipif_refrele(ipif);
9352 		}
9353 	} else {
9354 		/* Look for a colon in the name. */
9355 		endp = &name[namelen];
9356 		for (cp = endp; --cp > name; ) {
9357 			if (*cp == IPIF_SEPARATOR_CHAR) {
9358 				found_sep = B_TRUE;
9359 				/*
9360 				 * Reject any non-decimal aliases for plumbing
9361 				 * of logical interfaces. Aliases with leading
9362 				 * zeroes are also rejected as they introduce
9363 				 * ambiguity in the naming of the interfaces.
9364 				 * Comparing with "0" takes care of all such
9365 				 * cases.
9366 				 */
9367 				if ((strncmp("0", cp+1, 1)) == 0)
9368 					return (EINVAL);
9369 
9370 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
9371 				    id <= 0 || *endp != '\0') {
9372 					return (EINVAL);
9373 				}
9374 				*cp = '\0';
9375 				break;
9376 			}
9377 		}
9378 		ill = ill_lookup_on_name(name, B_FALSE, isv6, NULL, ipst);
9379 		if (found_sep)
9380 			*cp = IPIF_SEPARATOR_CHAR;
9381 		if (ill == NULL)
9382 			return (ENXIO);
9383 	}
9384 
9385 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
9386 	    B_TRUE);
9387 
9388 	/*
9389 	 * Release the refhold due to the lookup, now that we are excl
9390 	 * or we are just returning
9391 	 */
9392 	ill_refrele(ill);
9393 
9394 	if (ipsq == NULL)
9395 		return (EINPROGRESS);
9396 
9397 	/* We are now exclusive on the IPSQ */
9398 	ASSERT(IAM_WRITER_ILL(ill));
9399 
9400 	if (found_sep) {
9401 		/* Now see if there is an IPIF with this unit number. */
9402 		for (ipif = ill->ill_ipif; ipif != NULL;
9403 		    ipif = ipif->ipif_next) {
9404 			if (ipif->ipif_id == id) {
9405 				err = EEXIST;
9406 				goto done;
9407 			}
9408 		}
9409 	}
9410 
9411 	/*
9412 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
9413 	 * of lo0.  Plumbing for lo0:0 happens in ipif_lookup_on_name()
9414 	 * instead.
9415 	 */
9416 	if ((ipif = ipif_allocate(ill, found_sep ? id : -1, IRE_LOCAL,
9417 	    B_TRUE, B_TRUE, &err)) == NULL) {
9418 		goto done;
9419 	}
9420 
9421 	/* Return created name with ioctl */
9422 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
9423 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
9424 	ip1dbg(("created %s\n", lifr->lifr_name));
9425 
9426 	/* Set address */
9427 	sin = (sin_t *)&lifr->lifr_addr;
9428 	if (sin->sin_family != AF_UNSPEC) {
9429 		err = ip_sioctl_addr(ipif, sin, q, mp,
9430 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
9431 	}
9432 
9433 done:
9434 	ipsq_exit(ipsq);
9435 	return (err);
9436 }
9437 
9438 /*
9439  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
9440  * interface) delete it based on the IP address (on this physical interface).
9441  * Otherwise delete it based on the ipif_id.
9442  * Also, special handling to allow a removeif of lo0.
9443  */
9444 /* ARGSUSED */
9445 int
ip_sioctl_removeif(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * dummy_if_req)9446 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9447     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9448 {
9449 	conn_t		*connp;
9450 	ill_t		*ill = ipif->ipif_ill;
9451 	boolean_t	 success;
9452 	ip_stack_t	*ipst;
9453 
9454 	ipst = CONNQ_TO_IPST(q);
9455 
9456 	ASSERT(q->q_next == NULL);
9457 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
9458 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9459 	ASSERT(IAM_WRITER_IPIF(ipif));
9460 
9461 	connp = Q_TO_CONN(q);
9462 	/*
9463 	 * Special case for unplumbing lo0 (the loopback physical interface).
9464 	 * If unplumbing lo0, the incoming address structure has been
9465 	 * initialized to all zeros. When unplumbing lo0, all its logical
9466 	 * interfaces must be removed too.
9467 	 *
9468 	 * Note that this interface may be called to remove a specific
9469 	 * loopback logical interface (eg, lo0:1). But in that case
9470 	 * ipif->ipif_id != 0 so that the code path for that case is the
9471 	 * same as any other interface (meaning it skips the code directly
9472 	 * below).
9473 	 */
9474 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9475 		if (sin->sin_family == AF_UNSPEC &&
9476 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
9477 			/*
9478 			 * Mark it condemned. No new ref. will be made to ill.
9479 			 */
9480 			mutex_enter(&ill->ill_lock);
9481 			ill->ill_state_flags |= ILL_CONDEMNED;
9482 			for (ipif = ill->ill_ipif; ipif != NULL;
9483 			    ipif = ipif->ipif_next) {
9484 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
9485 			}
9486 			mutex_exit(&ill->ill_lock);
9487 
9488 			ipif = ill->ill_ipif;
9489 			/* unplumb the loopback interface */
9490 			ill_delete(ill);
9491 			mutex_enter(&connp->conn_lock);
9492 			mutex_enter(&ill->ill_lock);
9493 
9494 			/* Are any references to this ill active */
9495 			if (ill_is_freeable(ill)) {
9496 				mutex_exit(&ill->ill_lock);
9497 				mutex_exit(&connp->conn_lock);
9498 				ill_delete_tail(ill);
9499 				mi_free(ill);
9500 				return (0);
9501 			}
9502 			success = ipsq_pending_mp_add(connp, ipif,
9503 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
9504 			mutex_exit(&connp->conn_lock);
9505 			mutex_exit(&ill->ill_lock);
9506 			if (success)
9507 				return (EINPROGRESS);
9508 			else
9509 				return (EINTR);
9510 		}
9511 	}
9512 
9513 	if (ipif->ipif_id == 0) {
9514 		ipsq_t *ipsq;
9515 
9516 		/* Find based on address */
9517 		if (ipif->ipif_isv6) {
9518 			sin6_t *sin6;
9519 
9520 			if (sin->sin_family != AF_INET6)
9521 				return (EAFNOSUPPORT);
9522 
9523 			sin6 = (sin6_t *)sin;
9524 			/* We are a writer, so we should be able to lookup */
9525 			ipif = ipif_lookup_addr_exact_v6(&sin6->sin6_addr, ill,
9526 			    ipst);
9527 		} else {
9528 			if (sin->sin_family != AF_INET)
9529 				return (EAFNOSUPPORT);
9530 
9531 			/* We are a writer, so we should be able to lookup */
9532 			ipif = ipif_lookup_addr_exact(sin->sin_addr.s_addr, ill,
9533 			    ipst);
9534 		}
9535 		if (ipif == NULL) {
9536 			return (EADDRNOTAVAIL);
9537 		}
9538 
9539 		/*
9540 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
9541 		 * lifr_name of the physical interface but with an ip address
9542 		 * lifr_addr of a logical interface plumbed over it.
9543 		 * So update ipx_current_ipif now that ipif points to the
9544 		 * correct one.
9545 		 */
9546 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
9547 		ipsq->ipsq_xop->ipx_current_ipif = ipif;
9548 
9549 		/* This is a writer */
9550 		ipif_refrele(ipif);
9551 	}
9552 
9553 	/*
9554 	 * Can not delete instance zero since it is tied to the ill.
9555 	 */
9556 	if (ipif->ipif_id == 0)
9557 		return (EBUSY);
9558 
9559 	mutex_enter(&ill->ill_lock);
9560 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
9561 	mutex_exit(&ill->ill_lock);
9562 
9563 	ipif_free(ipif);
9564 
9565 	mutex_enter(&connp->conn_lock);
9566 	mutex_enter(&ill->ill_lock);
9567 
9568 	/* Are any references to this ipif active */
9569 	if (ipif_is_freeable(ipif)) {
9570 		mutex_exit(&ill->ill_lock);
9571 		mutex_exit(&connp->conn_lock);
9572 		ipif_non_duplicate(ipif);
9573 		(void) ipif_down_tail(ipif);
9574 		ipif_free_tail(ipif); /* frees ipif */
9575 		return (0);
9576 	}
9577 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
9578 	    IPIF_FREE);
9579 	mutex_exit(&ill->ill_lock);
9580 	mutex_exit(&connp->conn_lock);
9581 	if (success)
9582 		return (EINPROGRESS);
9583 	else
9584 		return (EINTR);
9585 }
9586 
9587 /*
9588  * Restart the removeif ioctl. The refcnt has gone down to 0.
9589  * The ipif is already condemned. So can't find it thru lookups.
9590  */
9591 /* ARGSUSED */
9592 int
ip_sioctl_removeif_restart(ipif_t * ipif,sin_t * dummy_sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * dummy_if_req)9593 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
9594     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9595 {
9596 	ill_t *ill = ipif->ipif_ill;
9597 
9598 	ASSERT(IAM_WRITER_IPIF(ipif));
9599 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
9600 
9601 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
9602 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9603 
9604 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9605 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
9606 		ill_delete_tail(ill);
9607 		mi_free(ill);
9608 		return (0);
9609 	}
9610 
9611 	ipif_non_duplicate(ipif);
9612 	(void) ipif_down_tail(ipif);
9613 	ipif_free_tail(ipif);
9614 
9615 	return (0);
9616 }
9617 
9618 /*
9619  * Set the local interface address using the given prefix and ill_token.
9620  */
9621 /* ARGSUSED */
9622 int
ip_sioctl_prefix(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * dummy_ipip,void * dummy_ifreq)9623 ip_sioctl_prefix(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9624     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9625 {
9626 	int err;
9627 	in6_addr_t v6addr;
9628 	sin6_t *sin6;
9629 	ill_t *ill;
9630 	int i;
9631 
9632 	ip1dbg(("ip_sioctl_prefix(%s:%u %p)\n",
9633 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9634 
9635 	ASSERT(IAM_WRITER_IPIF(ipif));
9636 
9637 	if (!ipif->ipif_isv6)
9638 		return (EINVAL);
9639 
9640 	if (sin->sin_family != AF_INET6)
9641 		return (EAFNOSUPPORT);
9642 
9643 	sin6 = (sin6_t *)sin;
9644 	v6addr = sin6->sin6_addr;
9645 	ill = ipif->ipif_ill;
9646 
9647 	if (IN6_IS_ADDR_UNSPECIFIED(&v6addr) ||
9648 	    IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token))
9649 		return (EADDRNOTAVAIL);
9650 
9651 	for (i = 0; i < 4; i++)
9652 		sin6->sin6_addr.s6_addr32[i] |= ill->ill_token.s6_addr32[i];
9653 
9654 	err = ip_sioctl_addr(ipif, sin, q, mp,
9655 	    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], dummy_ifreq);
9656 	return (err);
9657 }
9658 
9659 /*
9660  * Restart entry point to restart the address set operation after the
9661  * refcounts have dropped to zero.
9662  */
9663 /* ARGSUSED */
9664 int
ip_sioctl_prefix_restart(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * ifreq)9665 ip_sioctl_prefix_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9666     ip_ioctl_cmd_t *ipip, void *ifreq)
9667 {
9668 	ip1dbg(("ip_sioctl_prefix_restart(%s:%u %p)\n",
9669 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9670 	return (ip_sioctl_addr_restart(ipif, sin, q, mp, ipip, ifreq));
9671 }
9672 
9673 /*
9674  * Set the local interface address.
9675  * Allow an address of all zero when the interface is down.
9676  */
9677 /* ARGSUSED */
9678 int
ip_sioctl_addr(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * dummy_ipip,void * dummy_ifreq)9679 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9680     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9681 {
9682 	int err = 0;
9683 	in6_addr_t v6addr;
9684 	boolean_t need_up = B_FALSE;
9685 	ill_t *ill;
9686 	int i;
9687 
9688 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
9689 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9690 
9691 	ASSERT(IAM_WRITER_IPIF(ipif));
9692 
9693 	ill = ipif->ipif_ill;
9694 	if (ipif->ipif_isv6) {
9695 		sin6_t *sin6;
9696 		phyint_t *phyi;
9697 
9698 		if (sin->sin_family != AF_INET6)
9699 			return (EAFNOSUPPORT);
9700 
9701 		sin6 = (sin6_t *)sin;
9702 		v6addr = sin6->sin6_addr;
9703 		phyi = ill->ill_phyint;
9704 
9705 		/*
9706 		 * Enforce that true multicast interfaces have a link-local
9707 		 * address for logical unit 0.
9708 		 *
9709 		 * However for those ipif's for which link-local address was
9710 		 * not created by default, also allow setting :: as the address.
9711 		 * This scenario would arise, when we delete an address on ipif
9712 		 * with logical unit 0, we would want to set :: as the address.
9713 		 */
9714 		if (ipif->ipif_id == 0 &&
9715 		    (ill->ill_flags & ILLF_MULTICAST) &&
9716 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
9717 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
9718 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
9719 
9720 			/*
9721 			 * if default link-local was not created by kernel for
9722 			 * this ill, allow setting :: as the address on ipif:0.
9723 			 */
9724 			if (ill->ill_flags & ILLF_NOLINKLOCAL) {
9725 				if (!IN6_IS_ADDR_UNSPECIFIED(&v6addr))
9726 					return (EADDRNOTAVAIL);
9727 			} else {
9728 				return (EADDRNOTAVAIL);
9729 			}
9730 		}
9731 
9732 		/*
9733 		 * up interfaces shouldn't have the unspecified address
9734 		 * unless they also have the IPIF_NOLOCAL flags set and
9735 		 * have a subnet assigned.
9736 		 */
9737 		if ((ipif->ipif_flags & IPIF_UP) &&
9738 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
9739 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
9740 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
9741 			return (EADDRNOTAVAIL);
9742 		}
9743 
9744 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9745 			return (EADDRNOTAVAIL);
9746 	} else {
9747 		ipaddr_t addr;
9748 
9749 		if (sin->sin_family != AF_INET)
9750 			return (EAFNOSUPPORT);
9751 
9752 		addr = sin->sin_addr.s_addr;
9753 
9754 		/* Allow INADDR_ANY as the local address. */
9755 		if (addr != INADDR_ANY &&
9756 		    !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
9757 			return (EADDRNOTAVAIL);
9758 
9759 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9760 	}
9761 	/*
9762 	 * verify that the address being configured is permitted by the
9763 	 * ill_allowed_ips[] for the interface.
9764 	 */
9765 	if (ill->ill_allowed_ips_cnt > 0) {
9766 		for (i = 0; i < ill->ill_allowed_ips_cnt; i++) {
9767 			if (IN6_ARE_ADDR_EQUAL(&ill->ill_allowed_ips[i],
9768 			    &v6addr))
9769 				break;
9770 		}
9771 		if (i == ill->ill_allowed_ips_cnt) {
9772 			pr_addr_dbg("!allowed addr %s\n", AF_INET6, &v6addr);
9773 			return (EPERM);
9774 		}
9775 	}
9776 	/*
9777 	 * Even if there is no change we redo things just to rerun
9778 	 * ipif_set_default.
9779 	 */
9780 	if (ipif->ipif_flags & IPIF_UP) {
9781 		/*
9782 		 * Setting a new local address, make sure
9783 		 * we have net and subnet bcast ire's for
9784 		 * the old address if we need them.
9785 		 */
9786 		/*
9787 		 * If the interface is already marked up,
9788 		 * we call ipif_down which will take care
9789 		 * of ditching any IREs that have been set
9790 		 * up based on the old interface address.
9791 		 */
9792 		err = ipif_logical_down(ipif, q, mp);
9793 		if (err == EINPROGRESS)
9794 			return (err);
9795 		(void) ipif_down_tail(ipif);
9796 		need_up = 1;
9797 	}
9798 
9799 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
9800 	return (err);
9801 }
9802 
9803 int
ip_sioctl_addr_tail(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,boolean_t need_up)9804 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9805     boolean_t need_up)
9806 {
9807 	in6_addr_t v6addr;
9808 	in6_addr_t ov6addr;
9809 	ipaddr_t addr;
9810 	sin6_t	*sin6;
9811 	int	sinlen;
9812 	int	err = 0;
9813 	ill_t	*ill = ipif->ipif_ill;
9814 	boolean_t need_dl_down;
9815 	boolean_t need_arp_down;
9816 	struct iocblk *iocp;
9817 
9818 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
9819 
9820 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
9821 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9822 	ASSERT(IAM_WRITER_IPIF(ipif));
9823 
9824 	/* Must cancel any pending timer before taking the ill_lock */
9825 	if (ipif->ipif_recovery_id != 0)
9826 		(void) untimeout(ipif->ipif_recovery_id);
9827 	ipif->ipif_recovery_id = 0;
9828 
9829 	if (ipif->ipif_isv6) {
9830 		sin6 = (sin6_t *)sin;
9831 		v6addr = sin6->sin6_addr;
9832 		sinlen = sizeof (struct sockaddr_in6);
9833 	} else {
9834 		addr = sin->sin_addr.s_addr;
9835 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9836 		sinlen = sizeof (struct sockaddr_in);
9837 	}
9838 	mutex_enter(&ill->ill_lock);
9839 	ov6addr = ipif->ipif_v6lcl_addr;
9840 	ipif->ipif_v6lcl_addr = v6addr;
9841 	sctp_update_ipif_addr(ipif, ov6addr);
9842 	ipif->ipif_addr_ready = 0;
9843 
9844 	ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
9845 
9846 	/*
9847 	 * If the interface was previously marked as a duplicate, then since
9848 	 * we've now got a "new" address, it should no longer be considered a
9849 	 * duplicate -- even if the "new" address is the same as the old one.
9850 	 * Note that if all ipifs are down, we may have a pending ARP down
9851 	 * event to handle.  This is because we want to recover from duplicates
9852 	 * and thus delay tearing down ARP until the duplicates have been
9853 	 * removed or disabled.
9854 	 */
9855 	need_dl_down = need_arp_down = B_FALSE;
9856 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
9857 		need_arp_down = !need_up;
9858 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
9859 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
9860 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
9861 			need_dl_down = B_TRUE;
9862 		}
9863 	}
9864 
9865 	ipif_set_default(ipif);
9866 
9867 	/*
9868 	 * If we've just manually set the IPv6 link-local address (0th ipif),
9869 	 * tag the ill so that future updates to the interface ID don't result
9870 	 * in this address getting automatically reconfigured from under the
9871 	 * administrator.
9872 	 */
9873 	if (ipif->ipif_isv6 && ipif->ipif_id == 0) {
9874 		if (iocp == NULL || (iocp->ioc_cmd == SIOCSLIFADDR &&
9875 		    !IN6_IS_ADDR_UNSPECIFIED(&v6addr)))
9876 			ill->ill_manual_linklocal = 1;
9877 	}
9878 
9879 	/*
9880 	 * When publishing an interface address change event, we only notify
9881 	 * the event listeners of the new address.  It is assumed that if they
9882 	 * actively care about the addresses assigned that they will have
9883 	 * already discovered the previous address assigned (if there was one.)
9884 	 *
9885 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
9886 	 */
9887 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
9888 		ill_nic_event_dispatch(ill, MAP_IPIF_ID(ipif->ipif_id),
9889 		    NE_ADDRESS_CHANGE, sin, sinlen);
9890 	}
9891 
9892 	mutex_exit(&ill->ill_lock);
9893 
9894 	if (need_up) {
9895 		/*
9896 		 * Now bring the interface back up.  If this
9897 		 * is the only IPIF for the ILL, ipif_up
9898 		 * will have to re-bind to the device, so
9899 		 * we may get back EINPROGRESS, in which
9900 		 * case, this IOCTL will get completed in
9901 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
9902 		 */
9903 		err = ipif_up(ipif, q, mp);
9904 	} else {
9905 		/* Perhaps ilgs should use this ill */
9906 		update_conn_ill(NULL, ill->ill_ipst);
9907 	}
9908 
9909 	if (need_dl_down)
9910 		ill_dl_down(ill);
9911 
9912 	if (need_arp_down && !ill->ill_isv6)
9913 		(void) ipif_arp_down(ipif);
9914 
9915 	/*
9916 	 * The default multicast interface might have changed (for
9917 	 * instance if the IPv6 scope of the address changed)
9918 	 */
9919 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
9920 
9921 	return (err);
9922 }
9923 
9924 /*
9925  * Restart entry point to restart the address set operation after the
9926  * refcounts have dropped to zero.
9927  */
9928 /* ARGSUSED */
9929 int
ip_sioctl_addr_restart(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * ifreq)9930 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9931     ip_ioctl_cmd_t *ipip, void *ifreq)
9932 {
9933 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
9934 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9935 	ASSERT(IAM_WRITER_IPIF(ipif));
9936 	(void) ipif_down_tail(ipif);
9937 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
9938 }
9939 
9940 /* ARGSUSED */
9941 int
ip_sioctl_get_addr(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * if_req)9942 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9943     ip_ioctl_cmd_t *ipip, void *if_req)
9944 {
9945 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
9946 	struct lifreq *lifr = (struct lifreq *)if_req;
9947 
9948 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
9949 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9950 	/*
9951 	 * The net mask and address can't change since we have a
9952 	 * reference to the ipif. So no lock is necessary.
9953 	 */
9954 	if (ipif->ipif_isv6) {
9955 		*sin6 = sin6_null;
9956 		sin6->sin6_family = AF_INET6;
9957 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
9958 		if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
9959 			sin6->sin6_scope_id =
9960 			    ipif->ipif_ill->ill_phyint->phyint_ifindex;
9961 		}
9962 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
9963 		lifr->lifr_addrlen =
9964 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
9965 	} else {
9966 		*sin = sin_null;
9967 		sin->sin_family = AF_INET;
9968 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
9969 		if (ipip->ipi_cmd_type == LIF_CMD) {
9970 			lifr->lifr_addrlen =
9971 			    ip_mask_to_plen(ipif->ipif_net_mask);
9972 		}
9973 	}
9974 	return (0);
9975 }
9976 
9977 /*
9978  * Set the destination address for a pt-pt interface.
9979  */
9980 /* ARGSUSED */
9981 int
ip_sioctl_dstaddr(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * if_req)9982 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9983     ip_ioctl_cmd_t *ipip, void *if_req)
9984 {
9985 	int err = 0;
9986 	in6_addr_t v6addr;
9987 	boolean_t need_up = B_FALSE;
9988 
9989 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
9990 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9991 	ASSERT(IAM_WRITER_IPIF(ipif));
9992 
9993 	if (ipif->ipif_isv6) {
9994 		sin6_t *sin6;
9995 
9996 		if (sin->sin_family != AF_INET6)
9997 			return (EAFNOSUPPORT);
9998 
9999 		sin6 = (sin6_t *)sin;
10000 		v6addr = sin6->sin6_addr;
10001 
10002 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
10003 			return (EADDRNOTAVAIL);
10004 	} else {
10005 		ipaddr_t addr;
10006 
10007 		if (sin->sin_family != AF_INET)
10008 			return (EAFNOSUPPORT);
10009 
10010 		addr = sin->sin_addr.s_addr;
10011 		if (addr != INADDR_ANY &&
10012 		    !ip_addr_ok_v4(addr, ipif->ipif_net_mask)) {
10013 			return (EADDRNOTAVAIL);
10014 		}
10015 
10016 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10017 	}
10018 
10019 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
10020 		return (0);	/* No change */
10021 
10022 	if (ipif->ipif_flags & IPIF_UP) {
10023 		/*
10024 		 * If the interface is already marked up,
10025 		 * we call ipif_down which will take care
10026 		 * of ditching any IREs that have been set
10027 		 * up based on the old pp dst address.
10028 		 */
10029 		err = ipif_logical_down(ipif, q, mp);
10030 		if (err == EINPROGRESS)
10031 			return (err);
10032 		(void) ipif_down_tail(ipif);
10033 		need_up = B_TRUE;
10034 	}
10035 	/*
10036 	 * could return EINPROGRESS. If so ioctl will complete in
10037 	 * ip_rput_dlpi_writer
10038 	 */
10039 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
10040 	return (err);
10041 }
10042 
10043 static int
ip_sioctl_dstaddr_tail(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,boolean_t need_up)10044 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10045     boolean_t need_up)
10046 {
10047 	in6_addr_t v6addr;
10048 	ill_t	*ill = ipif->ipif_ill;
10049 	int	err = 0;
10050 	boolean_t need_dl_down;
10051 	boolean_t need_arp_down;
10052 
10053 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
10054 	    ipif->ipif_id, (void *)ipif));
10055 
10056 	/* Must cancel any pending timer before taking the ill_lock */
10057 	if (ipif->ipif_recovery_id != 0)
10058 		(void) untimeout(ipif->ipif_recovery_id);
10059 	ipif->ipif_recovery_id = 0;
10060 
10061 	if (ipif->ipif_isv6) {
10062 		sin6_t *sin6;
10063 
10064 		sin6 = (sin6_t *)sin;
10065 		v6addr = sin6->sin6_addr;
10066 	} else {
10067 		ipaddr_t addr;
10068 
10069 		addr = sin->sin_addr.s_addr;
10070 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10071 	}
10072 	mutex_enter(&ill->ill_lock);
10073 	/* Set point to point destination address. */
10074 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10075 		/*
10076 		 * Allow this as a means of creating logical
10077 		 * pt-pt interfaces on top of e.g. an Ethernet.
10078 		 * XXX Undocumented HACK for testing.
10079 		 * pt-pt interfaces are created with NUD disabled.
10080 		 */
10081 		ipif->ipif_flags |= IPIF_POINTOPOINT;
10082 		ipif->ipif_flags &= ~IPIF_BROADCAST;
10083 		if (ipif->ipif_isv6)
10084 			ill->ill_flags |= ILLF_NONUD;
10085 	}
10086 
10087 	/*
10088 	 * If the interface was previously marked as a duplicate, then since
10089 	 * we've now got a "new" address, it should no longer be considered a
10090 	 * duplicate -- even if the "new" address is the same as the old one.
10091 	 * Note that if all ipifs are down, we may have a pending ARP down
10092 	 * event to handle.
10093 	 */
10094 	need_dl_down = need_arp_down = B_FALSE;
10095 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
10096 		need_arp_down = !need_up;
10097 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
10098 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
10099 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
10100 			need_dl_down = B_TRUE;
10101 		}
10102 	}
10103 
10104 	/*
10105 	 * If we've just manually set the IPv6 destination link-local address
10106 	 * (0th ipif), tag the ill so that future updates to the destination
10107 	 * interface ID (as can happen with interfaces over IP tunnels) don't
10108 	 * result in this address getting automatically reconfigured from
10109 	 * under the administrator.
10110 	 */
10111 	if (ipif->ipif_isv6 && ipif->ipif_id == 0)
10112 		ill->ill_manual_dst_linklocal = 1;
10113 
10114 	/* Set the new address. */
10115 	ipif->ipif_v6pp_dst_addr = v6addr;
10116 	/* Make sure subnet tracks pp_dst */
10117 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
10118 	mutex_exit(&ill->ill_lock);
10119 
10120 	if (need_up) {
10121 		/*
10122 		 * Now bring the interface back up.  If this
10123 		 * is the only IPIF for the ILL, ipif_up
10124 		 * will have to re-bind to the device, so
10125 		 * we may get back EINPROGRESS, in which
10126 		 * case, this IOCTL will get completed in
10127 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
10128 		 */
10129 		err = ipif_up(ipif, q, mp);
10130 	}
10131 
10132 	if (need_dl_down)
10133 		ill_dl_down(ill);
10134 	if (need_arp_down && !ipif->ipif_isv6)
10135 		(void) ipif_arp_down(ipif);
10136 
10137 	return (err);
10138 }
10139 
10140 /*
10141  * Restart entry point to restart the dstaddress set operation after the
10142  * refcounts have dropped to zero.
10143  */
10144 /* ARGSUSED */
10145 int
ip_sioctl_dstaddr_restart(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * ifreq)10146 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10147     ip_ioctl_cmd_t *ipip, void *ifreq)
10148 {
10149 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
10150 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10151 	(void) ipif_down_tail(ipif);
10152 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
10153 }
10154 
10155 /* ARGSUSED */
10156 int
ip_sioctl_get_dstaddr(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * if_req)10157 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10158     ip_ioctl_cmd_t *ipip, void *if_req)
10159 {
10160 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
10161 
10162 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
10163 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10164 	/*
10165 	 * Get point to point destination address. The addresses can't
10166 	 * change since we hold a reference to the ipif.
10167 	 */
10168 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
10169 		return (EADDRNOTAVAIL);
10170 
10171 	if (ipif->ipif_isv6) {
10172 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10173 		*sin6 = sin6_null;
10174 		sin6->sin6_family = AF_INET6;
10175 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
10176 	} else {
10177 		*sin = sin_null;
10178 		sin->sin_family = AF_INET;
10179 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
10180 	}
10181 	return (0);
10182 }
10183 
10184 /*
10185  * Check which flags will change by the given flags being set
10186  * silently ignore flags which userland is not allowed to control.
10187  * (Because these flags may change between SIOCGLIFFLAGS and
10188  * SIOCSLIFFLAGS, and that's outside of userland's control,
10189  * we need to silently ignore them rather than fail.)
10190  */
10191 static void
ip_sioctl_flags_onoff(ipif_t * ipif,uint64_t flags,uint64_t * onp,uint64_t * offp)10192 ip_sioctl_flags_onoff(ipif_t *ipif, uint64_t flags, uint64_t *onp,
10193     uint64_t *offp)
10194 {
10195 	ill_t		*ill = ipif->ipif_ill;
10196 	phyint_t	*phyi = ill->ill_phyint;
10197 	uint64_t	cantchange_flags, intf_flags;
10198 	uint64_t	turn_on, turn_off;
10199 
10200 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
10201 	cantchange_flags = IFF_CANTCHANGE;
10202 	if (IS_IPMP(ill))
10203 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
10204 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
10205 	turn_off = intf_flags & turn_on;
10206 	turn_on ^= turn_off;
10207 	*onp = turn_on;
10208 	*offp = turn_off;
10209 }
10210 
10211 /*
10212  * Set interface flags.  Many flags require special handling (e.g.,
10213  * bringing the interface down); see below for details.
10214  *
10215  * NOTE : We really don't enforce that ipif_id zero should be used
10216  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
10217  *	  is because applications generally does SICGLIFFLAGS and
10218  *	  ORs in the new flags (that affects the logical) and does a
10219  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
10220  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
10221  *	  flags that will be turned on is correct with respect to
10222  *	  ipif_id 0. For backward compatibility reasons, it is not done.
10223  */
10224 /* ARGSUSED */
10225 int
ip_sioctl_flags(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * if_req)10226 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10227     ip_ioctl_cmd_t *ipip, void *if_req)
10228 {
10229 	uint64_t turn_on;
10230 	uint64_t turn_off;
10231 	int	err = 0;
10232 	phyint_t *phyi;
10233 	ill_t *ill;
10234 	conn_t *connp;
10235 	uint64_t intf_flags;
10236 	boolean_t phyint_flags_modified = B_FALSE;
10237 	uint64_t flags;
10238 	struct ifreq *ifr;
10239 	struct lifreq *lifr;
10240 	boolean_t set_linklocal = B_FALSE;
10241 
10242 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
10243 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10244 
10245 	ASSERT(IAM_WRITER_IPIF(ipif));
10246 
10247 	ill = ipif->ipif_ill;
10248 	phyi = ill->ill_phyint;
10249 
10250 	if (ipip->ipi_cmd_type == IF_CMD) {
10251 		ifr = (struct ifreq *)if_req;
10252 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
10253 	} else {
10254 		lifr = (struct lifreq *)if_req;
10255 		flags = lifr->lifr_flags;
10256 	}
10257 
10258 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
10259 
10260 	/*
10261 	 * Have the flags been set correctly until now?
10262 	 */
10263 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10264 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10265 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10266 	/*
10267 	 * Compare the new flags to the old, and partition
10268 	 * into those coming on and those going off.
10269 	 * For the 16 bit command keep the bits above bit 16 unchanged.
10270 	 */
10271 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
10272 		flags |= intf_flags & ~0xFFFF;
10273 
10274 	/*
10275 	 * Explicitly fail attempts to change flags that are always invalid on
10276 	 * an IPMP meta-interface.
10277 	 */
10278 	if (IS_IPMP(ill) && ((flags ^ intf_flags) & IFF_IPMP_INVALID))
10279 		return (EINVAL);
10280 
10281 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10282 	if ((turn_on|turn_off) == 0)
10283 		return (0);	/* No change */
10284 
10285 	/*
10286 	 * All test addresses must be IFF_DEPRECATED (to ensure source address
10287 	 * selection avoids them) -- so force IFF_DEPRECATED on, and do not
10288 	 * allow it to be turned off.
10289 	 */
10290 	if ((turn_off & (IFF_DEPRECATED|IFF_NOFAILOVER)) == IFF_DEPRECATED &&
10291 	    (turn_on|intf_flags) & IFF_NOFAILOVER)
10292 		return (EINVAL);
10293 
10294 	if ((connp = Q_TO_CONN(q)) == NULL)
10295 		return (EINVAL);
10296 
10297 	/*
10298 	 * Only vrrp control socket is allowed to change IFF_UP and
10299 	 * IFF_NOACCEPT flags when IFF_VRRP is set.
10300 	 */
10301 	if ((intf_flags & IFF_VRRP) && ((turn_off | turn_on) & IFF_UP)) {
10302 		if (!connp->conn_isvrrp)
10303 			return (EINVAL);
10304 	}
10305 
10306 	/*
10307 	 * The IFF_NOACCEPT flag can only be set on an IFF_VRRP IP address by
10308 	 * VRRP control socket.
10309 	 */
10310 	if ((turn_off | turn_on) & IFF_NOACCEPT) {
10311 		if (!connp->conn_isvrrp || !(intf_flags & IFF_VRRP))
10312 			return (EINVAL);
10313 	}
10314 
10315 	if (turn_on & IFF_NOFAILOVER) {
10316 		turn_on |= IFF_DEPRECATED;
10317 		flags |= IFF_DEPRECATED;
10318 	}
10319 
10320 	/*
10321 	 * On underlying interfaces, only allow applications to manage test
10322 	 * addresses -- otherwise, they may get confused when the address
10323 	 * moves as part of being brought up.  Likewise, prevent an
10324 	 * application-managed test address from being converted to a data
10325 	 * address.  To prevent migration of administratively up addresses in
10326 	 * the kernel, we don't allow them to be converted either.
10327 	 */
10328 	if (IS_UNDER_IPMP(ill)) {
10329 		const uint64_t appflags = IFF_DHCPRUNNING | IFF_ADDRCONF;
10330 
10331 		if ((turn_on & appflags) && !(flags & IFF_NOFAILOVER))
10332 			return (EINVAL);
10333 
10334 		if ((turn_off & IFF_NOFAILOVER) &&
10335 		    (flags & (appflags | IFF_UP | IFF_DUPLICATE)))
10336 			return (EINVAL);
10337 	}
10338 
10339 	/*
10340 	 * Only allow IFF_TEMPORARY flag to be set on
10341 	 * IPv6 interfaces.
10342 	 */
10343 	if ((turn_on & IFF_TEMPORARY) && !(ipif->ipif_isv6))
10344 		return (EINVAL);
10345 
10346 	/*
10347 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
10348 	 */
10349 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
10350 		return (EINVAL);
10351 
10352 	/*
10353 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
10354 	 * interfaces.  It makes no sense in that context.
10355 	 */
10356 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
10357 		return (EINVAL);
10358 
10359 	/*
10360 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
10361 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
10362 	 * If the link local address isn't set, and can be set, it will get
10363 	 * set later on in this function.
10364 	 */
10365 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
10366 	    (flags & IFF_UP) && !(flags & (IFF_NOLOCAL|IFF_ANYCAST)) &&
10367 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
10368 		if (ipif_cant_setlinklocal(ipif))
10369 			return (EINVAL);
10370 		set_linklocal = B_TRUE;
10371 	}
10372 
10373 	/*
10374 	 * If we modify physical interface flags, we'll potentially need to
10375 	 * send up two routing socket messages for the changes (one for the
10376 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
10377 	 */
10378 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10379 		phyint_flags_modified = B_TRUE;
10380 
10381 	/*
10382 	 * All functioning PHYI_STANDBY interfaces start life PHYI_INACTIVE
10383 	 * (otherwise, we'd immediately use them, defeating standby).  Also,
10384 	 * since PHYI_INACTIVE has a separate meaning when PHYI_STANDBY is not
10385 	 * set, don't allow PHYI_STANDBY to be set if PHYI_INACTIVE is already
10386 	 * set, and clear PHYI_INACTIVE if PHYI_STANDBY is being cleared.  We
10387 	 * also don't allow PHYI_STANDBY if VNI is enabled since its semantics
10388 	 * will not be honored.
10389 	 */
10390 	if (turn_on & PHYI_STANDBY) {
10391 		/*
10392 		 * No need to grab ill_g_usesrc_lock here; see the
10393 		 * synchronization notes in ip.c.
10394 		 */
10395 		if (ill->ill_usesrc_grp_next != NULL ||
10396 		    intf_flags & PHYI_INACTIVE)
10397 			return (EINVAL);
10398 		if (!(flags & PHYI_FAILED)) {
10399 			flags |= PHYI_INACTIVE;
10400 			turn_on |= PHYI_INACTIVE;
10401 		}
10402 	}
10403 
10404 	if (turn_off & PHYI_STANDBY) {
10405 		flags &= ~PHYI_INACTIVE;
10406 		turn_off |= PHYI_INACTIVE;
10407 	}
10408 
10409 	/*
10410 	 * PHYI_FAILED and PHYI_INACTIVE are mutually exclusive; fail if both
10411 	 * would end up on.
10412 	 */
10413 	if ((flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
10414 	    (PHYI_FAILED | PHYI_INACTIVE))
10415 		return (EINVAL);
10416 
10417 	/*
10418 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
10419 	 * status of the interface.
10420 	 */
10421 	if ((turn_on | turn_off) & ILLF_ROUTER) {
10422 		err = ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
10423 		if (err != 0)
10424 			return (err);
10425 	}
10426 
10427 	/*
10428 	 * If the interface is not UP and we are not going to
10429 	 * bring it UP, record the flags and return. When the
10430 	 * interface comes UP later, the right actions will be
10431 	 * taken.
10432 	 */
10433 	if (!(ipif->ipif_flags & IPIF_UP) &&
10434 	    !(turn_on & IPIF_UP)) {
10435 		/* Record new flags in their respective places. */
10436 		mutex_enter(&ill->ill_lock);
10437 		mutex_enter(&ill->ill_phyint->phyint_lock);
10438 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10439 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10440 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10441 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10442 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10443 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10444 		mutex_exit(&ill->ill_lock);
10445 		mutex_exit(&ill->ill_phyint->phyint_lock);
10446 
10447 		/*
10448 		 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the
10449 		 * same to the kernel: if any of them has been set by
10450 		 * userland, the interface cannot be used for data traffic.
10451 		 */
10452 		if ((turn_on|turn_off) &
10453 		    (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10454 			ASSERT(!IS_IPMP(ill));
10455 			/*
10456 			 * It's possible the ill is part of an "anonymous"
10457 			 * IPMP group rather than a real group.  In that case,
10458 			 * there are no other interfaces in the group and thus
10459 			 * no need to call ipmp_phyint_refresh_active().
10460 			 */
10461 			if (IS_UNDER_IPMP(ill))
10462 				ipmp_phyint_refresh_active(phyi);
10463 		}
10464 
10465 		if (phyint_flags_modified) {
10466 			if (phyi->phyint_illv4 != NULL) {
10467 				ip_rts_ifmsg(phyi->phyint_illv4->
10468 				    ill_ipif, RTSQ_DEFAULT);
10469 			}
10470 			if (phyi->phyint_illv6 != NULL) {
10471 				ip_rts_ifmsg(phyi->phyint_illv6->
10472 				    ill_ipif, RTSQ_DEFAULT);
10473 			}
10474 		}
10475 		/* The default multicast interface might have changed */
10476 		ire_increment_multicast_generation(ill->ill_ipst,
10477 		    ill->ill_isv6);
10478 
10479 		return (0);
10480 	} else if (set_linklocal) {
10481 		mutex_enter(&ill->ill_lock);
10482 		if (set_linklocal)
10483 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
10484 		mutex_exit(&ill->ill_lock);
10485 	}
10486 
10487 	/*
10488 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
10489 	 * or point-to-point interfaces with an unspecified destination. We do
10490 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
10491 	 * have a subnet assigned, which is how in.ndpd currently manages its
10492 	 * onlink prefix list when no addresses are configured with those
10493 	 * prefixes.
10494 	 */
10495 	if (ipif->ipif_isv6 &&
10496 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
10497 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
10498 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
10499 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10500 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
10501 		return (EINVAL);
10502 	}
10503 
10504 	/*
10505 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
10506 	 * from being brought up.
10507 	 */
10508 	if (!ipif->ipif_isv6 &&
10509 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10510 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
10511 		return (EINVAL);
10512 	}
10513 
10514 	/*
10515 	 * If we are going to change one or more of the flags that are
10516 	 * IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL, ILLF_NOARP,
10517 	 * ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, IPIF_PREFERRED, and
10518 	 * IPIF_NOFAILOVER, we will take special action.  This is
10519 	 * done by bring the ipif down, changing the flags and bringing
10520 	 * it back up again.  For IPIF_NOFAILOVER, the act of bringing it
10521 	 * back up will trigger the address to be moved.
10522 	 *
10523 	 * If we are going to change IFF_NOACCEPT, we need to bring
10524 	 * all the ipifs down then bring them up again.	 The act of
10525 	 * bringing all the ipifs back up will trigger the local
10526 	 * ires being recreated with "no_accept" set/cleared.
10527 	 *
10528 	 * Note that ILLF_NOACCEPT is always set separately from the
10529 	 * other flags.
10530 	 */
10531 	if ((turn_on|turn_off) &
10532 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
10533 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED|
10534 	    IPIF_NOFAILOVER)) {
10535 		/*
10536 		 * ipif_down() will ire_delete bcast ire's for the subnet,
10537 		 * while the ire_identical_ref tracks the case of IRE_BROADCAST
10538 		 * entries shared between multiple ipifs on the same subnet.
10539 		 */
10540 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
10541 		    !(turn_off & IPIF_UP)) {
10542 			if (ipif->ipif_flags & IPIF_UP)
10543 				ill->ill_logical_down = 1;
10544 			turn_on &= ~IPIF_UP;
10545 		}
10546 		err = ipif_down(ipif, q, mp);
10547 		ip1dbg(("ipif_down returns %d err ", err));
10548 		if (err == EINPROGRESS)
10549 			return (err);
10550 		(void) ipif_down_tail(ipif);
10551 	} else if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10552 		/*
10553 		 * If we can quiesce the ill, then continue.  If not, then
10554 		 * ip_sioctl_flags_tail() will be called from
10555 		 * ipif_ill_refrele_tail().
10556 		 */
10557 		ill_down_ipifs(ill, B_TRUE);
10558 
10559 		mutex_enter(&connp->conn_lock);
10560 		mutex_enter(&ill->ill_lock);
10561 		if (!ill_is_quiescent(ill)) {
10562 			boolean_t success;
10563 
10564 			success = ipsq_pending_mp_add(connp, ill->ill_ipif,
10565 			    q, mp, ILL_DOWN);
10566 			mutex_exit(&ill->ill_lock);
10567 			mutex_exit(&connp->conn_lock);
10568 			return (success ? EINPROGRESS : EINTR);
10569 		}
10570 		mutex_exit(&ill->ill_lock);
10571 		mutex_exit(&connp->conn_lock);
10572 	}
10573 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10574 }
10575 
10576 static int
ip_sioctl_flags_tail(ipif_t * ipif,uint64_t flags,queue_t * q,mblk_t * mp)10577 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
10578 {
10579 	ill_t	*ill;
10580 	phyint_t *phyi;
10581 	uint64_t turn_on, turn_off;
10582 	boolean_t phyint_flags_modified = B_FALSE;
10583 	int	err = 0;
10584 	boolean_t set_linklocal = B_FALSE;
10585 
10586 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
10587 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
10588 
10589 	ASSERT(IAM_WRITER_IPIF(ipif));
10590 
10591 	ill = ipif->ipif_ill;
10592 	phyi = ill->ill_phyint;
10593 
10594 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10595 
10596 	/*
10597 	 * IFF_UP is handled separately.
10598 	 */
10599 	turn_on &= ~IFF_UP;
10600 	turn_off &= ~IFF_UP;
10601 
10602 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10603 		phyint_flags_modified = B_TRUE;
10604 
10605 	/*
10606 	 * Now we change the flags. Track current value of
10607 	 * other flags in their respective places.
10608 	 */
10609 	mutex_enter(&ill->ill_lock);
10610 	mutex_enter(&phyi->phyint_lock);
10611 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10612 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10613 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10614 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10615 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10616 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10617 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
10618 		set_linklocal = B_TRUE;
10619 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
10620 	}
10621 
10622 	mutex_exit(&ill->ill_lock);
10623 	mutex_exit(&phyi->phyint_lock);
10624 
10625 	if (set_linklocal)
10626 		(void) ipif_setlinklocal(ipif);
10627 
10628 	/*
10629 	 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the same to
10630 	 * the kernel: if any of them has been set by userland, the interface
10631 	 * cannot be used for data traffic.
10632 	 */
10633 	if ((turn_on|turn_off) & (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10634 		ASSERT(!IS_IPMP(ill));
10635 		/*
10636 		 * It's possible the ill is part of an "anonymous" IPMP group
10637 		 * rather than a real group.  In that case, there are no other
10638 		 * interfaces in the group and thus no need for us to call
10639 		 * ipmp_phyint_refresh_active().
10640 		 */
10641 		if (IS_UNDER_IPMP(ill))
10642 			ipmp_phyint_refresh_active(phyi);
10643 	}
10644 
10645 	if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10646 		/*
10647 		 * If the ILLF_NOACCEPT flag is changed, bring up all the
10648 		 * ipifs that were brought down.
10649 		 *
10650 		 * The routing sockets messages are sent as the result
10651 		 * of ill_up_ipifs(), further, SCTP's IPIF list was updated
10652 		 * as well.
10653 		 */
10654 		err = ill_up_ipifs(ill, q, mp);
10655 	} else if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
10656 		/*
10657 		 * XXX ipif_up really does not know whether a phyint flags
10658 		 * was modified or not. So, it sends up information on
10659 		 * only one routing sockets message. As we don't bring up
10660 		 * the interface and also set PHYI_ flags simultaneously
10661 		 * it should be okay.
10662 		 */
10663 		err = ipif_up(ipif, q, mp);
10664 	} else {
10665 		/*
10666 		 * Make sure routing socket sees all changes to the flags.
10667 		 * ipif_up_done* handles this when we use ipif_up.
10668 		 */
10669 		if (phyint_flags_modified) {
10670 			if (phyi->phyint_illv4 != NULL) {
10671 				ip_rts_ifmsg(phyi->phyint_illv4->
10672 				    ill_ipif, RTSQ_DEFAULT);
10673 			}
10674 			if (phyi->phyint_illv6 != NULL) {
10675 				ip_rts_ifmsg(phyi->phyint_illv6->
10676 				    ill_ipif, RTSQ_DEFAULT);
10677 			}
10678 		} else {
10679 			ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
10680 		}
10681 		/*
10682 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
10683 		 * this in need_up case.
10684 		 */
10685 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10686 	}
10687 
10688 	/* The default multicast interface might have changed */
10689 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
10690 	return (err);
10691 }
10692 
10693 /*
10694  * Restart the flags operation now that the refcounts have dropped to zero.
10695  */
10696 /* ARGSUSED */
10697 int
ip_sioctl_flags_restart(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * if_req)10698 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10699     ip_ioctl_cmd_t *ipip, void *if_req)
10700 {
10701 	uint64_t flags;
10702 	struct ifreq *ifr = if_req;
10703 	struct lifreq *lifr = if_req;
10704 	uint64_t turn_on, turn_off;
10705 
10706 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
10707 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10708 
10709 	if (ipip->ipi_cmd_type == IF_CMD) {
10710 		/* cast to uint16_t prevents unwanted sign extension */
10711 		flags = (uint16_t)ifr->ifr_flags;
10712 	} else {
10713 		flags = lifr->lifr_flags;
10714 	}
10715 
10716 	/*
10717 	 * If this function call is a result of the ILLF_NOACCEPT flag
10718 	 * change, do not call ipif_down_tail(). See ip_sioctl_flags().
10719 	 */
10720 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10721 	if (!((turn_on|turn_off) & ILLF_NOACCEPT))
10722 		(void) ipif_down_tail(ipif);
10723 
10724 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10725 }
10726 
10727 /*
10728  * Can operate on either a module or a driver queue.
10729  */
10730 /* ARGSUSED */
10731 int
ip_sioctl_get_flags(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * if_req)10732 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10733     ip_ioctl_cmd_t *ipip, void *if_req)
10734 {
10735 	/*
10736 	 * Has the flags been set correctly till now ?
10737 	 */
10738 	ill_t *ill = ipif->ipif_ill;
10739 	phyint_t *phyi = ill->ill_phyint;
10740 
10741 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
10742 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10743 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10744 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10745 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10746 
10747 	/*
10748 	 * Need a lock since some flags can be set even when there are
10749 	 * references to the ipif.
10750 	 */
10751 	mutex_enter(&ill->ill_lock);
10752 	if (ipip->ipi_cmd_type == IF_CMD) {
10753 		struct ifreq *ifr = (struct ifreq *)if_req;
10754 
10755 		/* Get interface flags (low 16 only). */
10756 		ifr->ifr_flags = ((ipif->ipif_flags |
10757 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
10758 	} else {
10759 		struct lifreq *lifr = (struct lifreq *)if_req;
10760 
10761 		/* Get interface flags. */
10762 		lifr->lifr_flags = ipif->ipif_flags |
10763 		    ill->ill_flags | phyi->phyint_flags;
10764 	}
10765 	mutex_exit(&ill->ill_lock);
10766 	return (0);
10767 }
10768 
10769 /*
10770  * We allow the MTU to be set on an ILL, but not have it be different
10771  * for different IPIFs since we don't actually send packets on IPIFs.
10772  */
10773 /* ARGSUSED */
10774 int
ip_sioctl_mtu(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * if_req)10775 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10776     ip_ioctl_cmd_t *ipip, void *if_req)
10777 {
10778 	int mtu;
10779 	int ip_min_mtu;
10780 	struct ifreq	*ifr;
10781 	struct lifreq *lifr;
10782 	ill_t	*ill;
10783 
10784 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
10785 	    ipif->ipif_id, (void *)ipif));
10786 	if (ipip->ipi_cmd_type == IF_CMD) {
10787 		ifr = (struct ifreq *)if_req;
10788 		mtu = ifr->ifr_metric;
10789 	} else {
10790 		lifr = (struct lifreq *)if_req;
10791 		mtu = lifr->lifr_mtu;
10792 	}
10793 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
10794 	if (ipif->ipif_id != 0)
10795 		return (EINVAL);
10796 
10797 	ill = ipif->ipif_ill;
10798 	if (ipif->ipif_isv6)
10799 		ip_min_mtu = IPV6_MIN_MTU;
10800 	else
10801 		ip_min_mtu = IP_MIN_MTU;
10802 
10803 	mutex_enter(&ill->ill_lock);
10804 	if (mtu > ill->ill_max_frag || mtu < ip_min_mtu) {
10805 		mutex_exit(&ill->ill_lock);
10806 		return (EINVAL);
10807 	}
10808 	/* Avoid increasing ill_mc_mtu */
10809 	if (ill->ill_mc_mtu > mtu)
10810 		ill->ill_mc_mtu = mtu;
10811 
10812 	/*
10813 	 * The dce and fragmentation code can handle changes to ill_mtu
10814 	 * concurrent with sending/fragmenting packets.
10815 	 */
10816 	ill->ill_mtu = mtu;
10817 	ill->ill_flags |= ILLF_FIXEDMTU;
10818 	mutex_exit(&ill->ill_lock);
10819 
10820 	/*
10821 	 * Make sure all dce_generation checks find out
10822 	 * that ill_mtu/ill_mc_mtu has changed.
10823 	 */
10824 	dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
10825 
10826 	/*
10827 	 * Refresh IPMP meta-interface MTU if necessary.
10828 	 */
10829 	if (IS_UNDER_IPMP(ill))
10830 		ipmp_illgrp_refresh_mtu(ill->ill_grp);
10831 
10832 	/* Update the MTU in SCTP's list */
10833 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10834 	return (0);
10835 }
10836 
10837 /* Get interface MTU. */
10838 /* ARGSUSED */
10839 int
ip_sioctl_get_mtu(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * if_req)10840 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10841     ip_ioctl_cmd_t *ipip, void *if_req)
10842 {
10843 	struct ifreq	*ifr;
10844 	struct lifreq	*lifr;
10845 
10846 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
10847 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10848 
10849 	/*
10850 	 * We allow a get on any logical interface even though the set
10851 	 * can only be done on logical unit 0.
10852 	 */
10853 	if (ipip->ipi_cmd_type == IF_CMD) {
10854 		ifr = (struct ifreq *)if_req;
10855 		ifr->ifr_metric = ipif->ipif_ill->ill_mtu;
10856 	} else {
10857 		lifr = (struct lifreq *)if_req;
10858 		lifr->lifr_mtu = ipif->ipif_ill->ill_mtu;
10859 	}
10860 	return (0);
10861 }
10862 
10863 /* Set interface broadcast address. */
10864 /* ARGSUSED2 */
10865 int
ip_sioctl_brdaddr(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * if_req)10866 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10867     ip_ioctl_cmd_t *ipip, void *if_req)
10868 {
10869 	ipaddr_t addr;
10870 	ire_t	*ire;
10871 	ill_t		*ill = ipif->ipif_ill;
10872 	ip_stack_t	*ipst = ill->ill_ipst;
10873 
10874 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ill->ill_name,
10875 	    ipif->ipif_id));
10876 
10877 	ASSERT(IAM_WRITER_IPIF(ipif));
10878 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10879 		return (EADDRNOTAVAIL);
10880 
10881 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
10882 
10883 	if (sin->sin_family != AF_INET)
10884 		return (EAFNOSUPPORT);
10885 
10886 	addr = sin->sin_addr.s_addr;
10887 
10888 	if (ipif->ipif_flags & IPIF_UP) {
10889 		/*
10890 		 * If we are already up, make sure the new
10891 		 * broadcast address makes sense.  If it does,
10892 		 * there should be an IRE for it already.
10893 		 */
10894 		ire = ire_ftable_lookup_v4(addr, 0, 0, IRE_BROADCAST,
10895 		    ill, ipif->ipif_zoneid, NULL,
10896 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), 0, ipst, NULL);
10897 		if (ire == NULL) {
10898 			return (EINVAL);
10899 		} else {
10900 			ire_refrele(ire);
10901 		}
10902 	}
10903 	/*
10904 	 * Changing the broadcast addr for this ipif. Since the IRE_BROADCAST
10905 	 * needs to already exist we never need to change the set of
10906 	 * IRE_BROADCASTs when we are UP.
10907 	 */
10908 	if (addr != ipif->ipif_brd_addr)
10909 		IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
10910 
10911 	return (0);
10912 }
10913 
10914 /* Get interface broadcast address. */
10915 /* ARGSUSED */
10916 int
ip_sioctl_get_brdaddr(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * if_req)10917 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10918     ip_ioctl_cmd_t *ipip, void *if_req)
10919 {
10920 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
10921 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10922 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10923 		return (EADDRNOTAVAIL);
10924 
10925 	/* IPIF_BROADCAST not possible with IPv6 */
10926 	ASSERT(!ipif->ipif_isv6);
10927 	*sin = sin_null;
10928 	sin->sin_family = AF_INET;
10929 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
10930 	return (0);
10931 }
10932 
10933 /*
10934  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
10935  */
10936 /* ARGSUSED */
10937 int
ip_sioctl_netmask(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * if_req)10938 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10939     ip_ioctl_cmd_t *ipip, void *if_req)
10940 {
10941 	int err = 0;
10942 	in6_addr_t v6mask;
10943 
10944 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
10945 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10946 
10947 	ASSERT(IAM_WRITER_IPIF(ipif));
10948 
10949 	if (ipif->ipif_isv6) {
10950 		sin6_t *sin6;
10951 
10952 		if (sin->sin_family != AF_INET6)
10953 			return (EAFNOSUPPORT);
10954 
10955 		sin6 = (sin6_t *)sin;
10956 		v6mask = sin6->sin6_addr;
10957 	} else {
10958 		ipaddr_t mask;
10959 
10960 		if (sin->sin_family != AF_INET)
10961 			return (EAFNOSUPPORT);
10962 
10963 		mask = sin->sin_addr.s_addr;
10964 		if (!ip_contiguous_mask(ntohl(mask)))
10965 			return (ENOTSUP);
10966 		V4MASK_TO_V6(mask, v6mask);
10967 	}
10968 
10969 	/*
10970 	 * No big deal if the interface isn't already up, or the mask
10971 	 * isn't really changing, or this is pt-pt.
10972 	 */
10973 	if (!(ipif->ipif_flags & IPIF_UP) ||
10974 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
10975 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
10976 		ipif->ipif_v6net_mask = v6mask;
10977 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10978 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
10979 			    ipif->ipif_v6net_mask,
10980 			    ipif->ipif_v6subnet);
10981 		}
10982 		return (0);
10983 	}
10984 	/*
10985 	 * Make sure we have valid net and subnet broadcast ire's
10986 	 * for the old netmask, if needed by other logical interfaces.
10987 	 */
10988 	err = ipif_logical_down(ipif, q, mp);
10989 	if (err == EINPROGRESS)
10990 		return (err);
10991 	(void) ipif_down_tail(ipif);
10992 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
10993 	return (err);
10994 }
10995 
10996 static int
ip_sioctl_netmask_tail(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp)10997 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
10998 {
10999 	in6_addr_t v6mask;
11000 	int err = 0;
11001 
11002 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
11003 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11004 
11005 	if (ipif->ipif_isv6) {
11006 		sin6_t *sin6;
11007 
11008 		sin6 = (sin6_t *)sin;
11009 		v6mask = sin6->sin6_addr;
11010 	} else {
11011 		ipaddr_t mask;
11012 
11013 		mask = sin->sin_addr.s_addr;
11014 		V4MASK_TO_V6(mask, v6mask);
11015 	}
11016 
11017 	ipif->ipif_v6net_mask = v6mask;
11018 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11019 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
11020 		    ipif->ipif_v6subnet);
11021 	}
11022 	err = ipif_up(ipif, q, mp);
11023 
11024 	if (err == 0 || err == EINPROGRESS) {
11025 		/*
11026 		 * The interface must be DL_BOUND if this packet has to
11027 		 * go out on the wire. Since we only go through a logical
11028 		 * down and are bound with the driver during an internal
11029 		 * down/up that is satisfied.
11030 		 */
11031 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
11032 			/* Potentially broadcast an address mask reply. */
11033 			ipif_mask_reply(ipif);
11034 		}
11035 	}
11036 	return (err);
11037 }
11038 
11039 /* ARGSUSED */
11040 int
ip_sioctl_netmask_restart(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * if_req)11041 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11042     ip_ioctl_cmd_t *ipip, void *if_req)
11043 {
11044 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
11045 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11046 	(void) ipif_down_tail(ipif);
11047 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
11048 }
11049 
11050 /* Get interface net mask. */
11051 /* ARGSUSED */
11052 int
ip_sioctl_get_netmask(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * if_req)11053 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11054     ip_ioctl_cmd_t *ipip, void *if_req)
11055 {
11056 	struct lifreq *lifr = (struct lifreq *)if_req;
11057 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
11058 
11059 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
11060 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11061 
11062 	/*
11063 	 * net mask can't change since we have a reference to the ipif.
11064 	 */
11065 	if (ipif->ipif_isv6) {
11066 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11067 		*sin6 = sin6_null;
11068 		sin6->sin6_family = AF_INET6;
11069 		sin6->sin6_addr = ipif->ipif_v6net_mask;
11070 		lifr->lifr_addrlen =
11071 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11072 	} else {
11073 		*sin = sin_null;
11074 		sin->sin_family = AF_INET;
11075 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
11076 		if (ipip->ipi_cmd_type == LIF_CMD) {
11077 			lifr->lifr_addrlen =
11078 			    ip_mask_to_plen(ipif->ipif_net_mask);
11079 		}
11080 	}
11081 	return (0);
11082 }
11083 
11084 /* ARGSUSED */
11085 int
ip_sioctl_metric(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * if_req)11086 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11087     ip_ioctl_cmd_t *ipip, void *if_req)
11088 {
11089 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
11090 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11091 
11092 	/*
11093 	 * Since no applications should ever be setting metrics on underlying
11094 	 * interfaces, we explicitly fail to smoke 'em out.
11095 	 */
11096 	if (IS_UNDER_IPMP(ipif->ipif_ill))
11097 		return (EINVAL);
11098 
11099 	/*
11100 	 * Set interface metric.  We don't use this for
11101 	 * anything but we keep track of it in case it is
11102 	 * important to routing applications or such.
11103 	 */
11104 	if (ipip->ipi_cmd_type == IF_CMD) {
11105 		struct ifreq    *ifr;
11106 
11107 		ifr = (struct ifreq *)if_req;
11108 		ipif->ipif_ill->ill_metric = ifr->ifr_metric;
11109 	} else {
11110 		struct lifreq   *lifr;
11111 
11112 		lifr = (struct lifreq *)if_req;
11113 		ipif->ipif_ill->ill_metric = lifr->lifr_metric;
11114 	}
11115 	return (0);
11116 }
11117 
11118 /* ARGSUSED */
11119 int
ip_sioctl_get_metric(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * if_req)11120 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11121     ip_ioctl_cmd_t *ipip, void *if_req)
11122 {
11123 	/* Get interface metric. */
11124 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
11125 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11126 
11127 	if (ipip->ipi_cmd_type == IF_CMD) {
11128 		struct ifreq    *ifr;
11129 
11130 		ifr = (struct ifreq *)if_req;
11131 		ifr->ifr_metric = ipif->ipif_ill->ill_metric;
11132 	} else {
11133 		struct lifreq   *lifr;
11134 
11135 		lifr = (struct lifreq *)if_req;
11136 		lifr->lifr_metric = ipif->ipif_ill->ill_metric;
11137 	}
11138 
11139 	return (0);
11140 }
11141 
11142 /* ARGSUSED */
11143 int
ip_sioctl_muxid(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * if_req)11144 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11145     ip_ioctl_cmd_t *ipip, void *if_req)
11146 {
11147 	int	arp_muxid;
11148 
11149 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
11150 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11151 	/*
11152 	 * Set the muxid returned from I_PLINK.
11153 	 */
11154 	if (ipip->ipi_cmd_type == IF_CMD) {
11155 		struct ifreq *ifr = (struct ifreq *)if_req;
11156 
11157 		ipif->ipif_ill->ill_muxid = ifr->ifr_ip_muxid;
11158 		arp_muxid = ifr->ifr_arp_muxid;
11159 	} else {
11160 		struct lifreq *lifr = (struct lifreq *)if_req;
11161 
11162 		ipif->ipif_ill->ill_muxid = lifr->lifr_ip_muxid;
11163 		arp_muxid = lifr->lifr_arp_muxid;
11164 	}
11165 	arl_set_muxid(ipif->ipif_ill, arp_muxid);
11166 	return (0);
11167 }
11168 
11169 /* ARGSUSED */
11170 int
ip_sioctl_get_muxid(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * if_req)11171 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11172     ip_ioctl_cmd_t *ipip, void *if_req)
11173 {
11174 	int	arp_muxid = 0;
11175 
11176 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
11177 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11178 	/*
11179 	 * Get the muxid saved in ill for I_PUNLINK.
11180 	 */
11181 	arp_muxid = arl_get_muxid(ipif->ipif_ill);
11182 	if (ipip->ipi_cmd_type == IF_CMD) {
11183 		struct ifreq *ifr = (struct ifreq *)if_req;
11184 
11185 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_muxid;
11186 		ifr->ifr_arp_muxid = arp_muxid;
11187 	} else {
11188 		struct lifreq *lifr = (struct lifreq *)if_req;
11189 
11190 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_muxid;
11191 		lifr->lifr_arp_muxid = arp_muxid;
11192 	}
11193 	return (0);
11194 }
11195 
11196 /*
11197  * Set the subnet prefix. Does not modify the broadcast address.
11198  */
11199 /* ARGSUSED */
11200 int
ip_sioctl_subnet(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * if_req)11201 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11202     ip_ioctl_cmd_t *ipip, void *if_req)
11203 {
11204 	int err = 0;
11205 	in6_addr_t v6addr;
11206 	in6_addr_t v6mask;
11207 	boolean_t need_up = B_FALSE;
11208 	int addrlen;
11209 
11210 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
11211 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11212 
11213 	ASSERT(IAM_WRITER_IPIF(ipif));
11214 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
11215 
11216 	if (ipif->ipif_isv6) {
11217 		sin6_t *sin6;
11218 
11219 		if (sin->sin_family != AF_INET6)
11220 			return (EAFNOSUPPORT);
11221 
11222 		sin6 = (sin6_t *)sin;
11223 		v6addr = sin6->sin6_addr;
11224 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
11225 			return (EADDRNOTAVAIL);
11226 	} else {
11227 		ipaddr_t addr;
11228 
11229 		if (sin->sin_family != AF_INET)
11230 			return (EAFNOSUPPORT);
11231 
11232 		addr = sin->sin_addr.s_addr;
11233 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
11234 			return (EADDRNOTAVAIL);
11235 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11236 		/* Add 96 bits */
11237 		addrlen += IPV6_ABITS - IP_ABITS;
11238 	}
11239 
11240 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
11241 		return (EINVAL);
11242 
11243 	/* Check if bits in the address is set past the mask */
11244 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
11245 		return (EINVAL);
11246 
11247 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
11248 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
11249 		return (0);	/* No change */
11250 
11251 	if (ipif->ipif_flags & IPIF_UP) {
11252 		/*
11253 		 * If the interface is already marked up,
11254 		 * we call ipif_down which will take care
11255 		 * of ditching any IREs that have been set
11256 		 * up based on the old interface address.
11257 		 */
11258 		err = ipif_logical_down(ipif, q, mp);
11259 		if (err == EINPROGRESS)
11260 			return (err);
11261 		(void) ipif_down_tail(ipif);
11262 		need_up = B_TRUE;
11263 	}
11264 
11265 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
11266 	return (err);
11267 }
11268 
11269 static int
ip_sioctl_subnet_tail(ipif_t * ipif,in6_addr_t v6addr,in6_addr_t v6mask,queue_t * q,mblk_t * mp,boolean_t need_up)11270 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
11271     queue_t *q, mblk_t *mp, boolean_t need_up)
11272 {
11273 	ill_t	*ill = ipif->ipif_ill;
11274 	int	err = 0;
11275 
11276 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
11277 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11278 
11279 	/* Set the new address. */
11280 	mutex_enter(&ill->ill_lock);
11281 	ipif->ipif_v6net_mask = v6mask;
11282 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11283 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
11284 		    ipif->ipif_v6subnet);
11285 	}
11286 	mutex_exit(&ill->ill_lock);
11287 
11288 	if (need_up) {
11289 		/*
11290 		 * Now bring the interface back up.  If this
11291 		 * is the only IPIF for the ILL, ipif_up
11292 		 * will have to re-bind to the device, so
11293 		 * we may get back EINPROGRESS, in which
11294 		 * case, this IOCTL will get completed in
11295 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11296 		 */
11297 		err = ipif_up(ipif, q, mp);
11298 		if (err == EINPROGRESS)
11299 			return (err);
11300 	}
11301 	return (err);
11302 }
11303 
11304 /* ARGSUSED */
11305 int
ip_sioctl_subnet_restart(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * if_req)11306 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11307     ip_ioctl_cmd_t *ipip, void *if_req)
11308 {
11309 	int	addrlen;
11310 	in6_addr_t v6addr;
11311 	in6_addr_t v6mask;
11312 	struct lifreq *lifr = (struct lifreq *)if_req;
11313 
11314 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
11315 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11316 	(void) ipif_down_tail(ipif);
11317 
11318 	addrlen = lifr->lifr_addrlen;
11319 	if (ipif->ipif_isv6) {
11320 		sin6_t *sin6;
11321 
11322 		sin6 = (sin6_t *)sin;
11323 		v6addr = sin6->sin6_addr;
11324 	} else {
11325 		ipaddr_t addr;
11326 
11327 		addr = sin->sin_addr.s_addr;
11328 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11329 		addrlen += IPV6_ABITS - IP_ABITS;
11330 	}
11331 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
11332 
11333 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
11334 }
11335 
11336 /* ARGSUSED */
11337 int
ip_sioctl_get_subnet(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * if_req)11338 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11339     ip_ioctl_cmd_t *ipip, void *if_req)
11340 {
11341 	struct lifreq *lifr = (struct lifreq *)if_req;
11342 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
11343 
11344 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
11345 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11346 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11347 
11348 	if (ipif->ipif_isv6) {
11349 		*sin6 = sin6_null;
11350 		sin6->sin6_family = AF_INET6;
11351 		sin6->sin6_addr = ipif->ipif_v6subnet;
11352 		lifr->lifr_addrlen =
11353 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11354 	} else {
11355 		*sin = sin_null;
11356 		sin->sin_family = AF_INET;
11357 		sin->sin_addr.s_addr = ipif->ipif_subnet;
11358 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
11359 	}
11360 	return (0);
11361 }
11362 
11363 /*
11364  * Set the IPv6 address token.
11365  */
11366 /* ARGSUSED */
11367 int
ip_sioctl_token(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipi,void * if_req)11368 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11369     ip_ioctl_cmd_t *ipi, void *if_req)
11370 {
11371 	ill_t *ill = ipif->ipif_ill;
11372 	int err;
11373 	in6_addr_t v6addr;
11374 	in6_addr_t v6mask;
11375 	boolean_t need_up = B_FALSE;
11376 	int i;
11377 	sin6_t *sin6 = (sin6_t *)sin;
11378 	struct lifreq *lifr = (struct lifreq *)if_req;
11379 	int addrlen;
11380 
11381 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
11382 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11383 	ASSERT(IAM_WRITER_IPIF(ipif));
11384 
11385 	addrlen = lifr->lifr_addrlen;
11386 	/* Only allow for logical unit zero i.e. not on "le0:17" */
11387 	if (ipif->ipif_id != 0)
11388 		return (EINVAL);
11389 
11390 	if (!ipif->ipif_isv6)
11391 		return (EINVAL);
11392 
11393 	if (addrlen > IPV6_ABITS)
11394 		return (EINVAL);
11395 
11396 	v6addr = sin6->sin6_addr;
11397 
11398 	/*
11399 	 * The length of the token is the length from the end.  To get
11400 	 * the proper mask for this, compute the mask of the bits not
11401 	 * in the token; ie. the prefix, and then xor to get the mask.
11402 	 */
11403 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
11404 		return (EINVAL);
11405 	for (i = 0; i < 4; i++) {
11406 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11407 	}
11408 
11409 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
11410 	    ill->ill_token_length == addrlen)
11411 		return (0);	/* No change */
11412 
11413 	if (ipif->ipif_flags & IPIF_UP) {
11414 		err = ipif_logical_down(ipif, q, mp);
11415 		if (err == EINPROGRESS)
11416 			return (err);
11417 		(void) ipif_down_tail(ipif);
11418 		need_up = B_TRUE;
11419 	}
11420 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
11421 	return (err);
11422 }
11423 
11424 static int
ip_sioctl_token_tail(ipif_t * ipif,sin6_t * sin6,int addrlen,queue_t * q,mblk_t * mp,boolean_t need_up)11425 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
11426     mblk_t *mp, boolean_t need_up)
11427 {
11428 	in6_addr_t v6addr;
11429 	in6_addr_t v6mask;
11430 	ill_t	*ill = ipif->ipif_ill;
11431 	int	i;
11432 	int	err = 0;
11433 
11434 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
11435 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11436 	v6addr = sin6->sin6_addr;
11437 	/*
11438 	 * The length of the token is the length from the end.  To get
11439 	 * the proper mask for this, compute the mask of the bits not
11440 	 * in the token; ie. the prefix, and then xor to get the mask.
11441 	 */
11442 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
11443 	for (i = 0; i < 4; i++)
11444 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11445 
11446 	mutex_enter(&ill->ill_lock);
11447 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
11448 	ill->ill_token_length = addrlen;
11449 	ill->ill_manual_token = 1;
11450 
11451 	/* Reconfigure the link-local address based on this new token */
11452 	ipif_setlinklocal(ill->ill_ipif);
11453 
11454 	mutex_exit(&ill->ill_lock);
11455 
11456 	if (need_up) {
11457 		/*
11458 		 * Now bring the interface back up.  If this
11459 		 * is the only IPIF for the ILL, ipif_up
11460 		 * will have to re-bind to the device, so
11461 		 * we may get back EINPROGRESS, in which
11462 		 * case, this IOCTL will get completed in
11463 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11464 		 */
11465 		err = ipif_up(ipif, q, mp);
11466 		if (err == EINPROGRESS)
11467 			return (err);
11468 	}
11469 	return (err);
11470 }
11471 
11472 /* ARGSUSED */
11473 int
ip_sioctl_get_token(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipi,void * if_req)11474 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11475     ip_ioctl_cmd_t *ipi, void *if_req)
11476 {
11477 	ill_t *ill;
11478 	sin6_t *sin6 = (sin6_t *)sin;
11479 	struct lifreq *lifr = (struct lifreq *)if_req;
11480 
11481 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
11482 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11483 	if (ipif->ipif_id != 0)
11484 		return (EINVAL);
11485 
11486 	ill = ipif->ipif_ill;
11487 	if (!ill->ill_isv6)
11488 		return (ENXIO);
11489 
11490 	*sin6 = sin6_null;
11491 	sin6->sin6_family = AF_INET6;
11492 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
11493 	sin6->sin6_addr = ill->ill_token;
11494 	lifr->lifr_addrlen = ill->ill_token_length;
11495 	return (0);
11496 }
11497 
11498 /*
11499  * Set (hardware) link specific information that might override
11500  * what was acquired through the DL_INFO_ACK.
11501  */
11502 /* ARGSUSED */
11503 int
ip_sioctl_lnkinfo(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipi,void * if_req)11504 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11505     ip_ioctl_cmd_t *ipi, void *if_req)
11506 {
11507 	ill_t		*ill = ipif->ipif_ill;
11508 	int		ip_min_mtu;
11509 	struct lifreq	*lifr = (struct lifreq *)if_req;
11510 	lif_ifinfo_req_t *lir;
11511 
11512 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
11513 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11514 	lir = &lifr->lifr_ifinfo;
11515 	ASSERT(IAM_WRITER_IPIF(ipif));
11516 
11517 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
11518 	if (ipif->ipif_id != 0)
11519 		return (EINVAL);
11520 
11521 	/* Set interface MTU. */
11522 	if (ipif->ipif_isv6)
11523 		ip_min_mtu = IPV6_MIN_MTU;
11524 	else
11525 		ip_min_mtu = IP_MIN_MTU;
11526 
11527 	/*
11528 	 * Verify values before we set anything. Allow zero to
11529 	 * mean unspecified.
11530 	 *
11531 	 * XXX We should be able to set the user-defined lir_mtu to some value
11532 	 * that is greater than ill_current_frag but less than ill_max_frag- the
11533 	 * ill_max_frag value tells us the max MTU that can be handled by the
11534 	 * datalink, whereas the ill_current_frag is dynamically computed for
11535 	 * some link-types like tunnels, based on the tunnel PMTU. However,
11536 	 * since there is currently no way of distinguishing between
11537 	 * administratively fixed link mtu values (e.g., those set via
11538 	 * /sbin/dladm) and dynamically discovered MTUs (e.g., those discovered
11539 	 * for tunnels) we conservatively choose the  ill_current_frag as the
11540 	 * upper-bound.
11541 	 */
11542 	if (lir->lir_maxmtu != 0 &&
11543 	    (lir->lir_maxmtu > ill->ill_current_frag ||
11544 	    lir->lir_maxmtu < ip_min_mtu))
11545 		return (EINVAL);
11546 	if (lir->lir_reachtime != 0 &&
11547 	    lir->lir_reachtime > ND_MAX_REACHTIME)
11548 		return (EINVAL);
11549 	if (lir->lir_reachretrans != 0 &&
11550 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
11551 		return (EINVAL);
11552 
11553 	mutex_enter(&ill->ill_lock);
11554 	/*
11555 	 * The dce and fragmentation code can handle changes to ill_mtu
11556 	 * concurrent with sending/fragmenting packets.
11557 	 */
11558 	if (lir->lir_maxmtu != 0)
11559 		ill->ill_user_mtu = lir->lir_maxmtu;
11560 
11561 	if (lir->lir_reachtime != 0)
11562 		ill->ill_reachable_time = lir->lir_reachtime;
11563 
11564 	if (lir->lir_reachretrans != 0)
11565 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
11566 
11567 	ill->ill_max_hops = lir->lir_maxhops;
11568 	ill->ill_max_buf = ND_MAX_Q;
11569 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && ill->ill_user_mtu != 0) {
11570 		/*
11571 		 * ill_mtu is the actual interface MTU, obtained as the min
11572 		 * of user-configured mtu and the value announced by the
11573 		 * driver (via DL_NOTE_SDU_SIZE/DL_INFO_ACK). Note that since
11574 		 * we have already made the choice of requiring
11575 		 * ill_user_mtu < ill_current_frag by the time we get here,
11576 		 * the ill_mtu effectively gets assigned to the ill_user_mtu
11577 		 * here.
11578 		 */
11579 		ill->ill_mtu = MIN(ill->ill_current_frag, ill->ill_user_mtu);
11580 		ill->ill_mc_mtu = MIN(ill->ill_mc_mtu, ill->ill_user_mtu);
11581 	}
11582 	mutex_exit(&ill->ill_lock);
11583 
11584 	/*
11585 	 * Make sure all dce_generation checks find out
11586 	 * that ill_mtu/ill_mc_mtu has changed.
11587 	 */
11588 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && (lir->lir_maxmtu != 0))
11589 		dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
11590 
11591 	/*
11592 	 * Refresh IPMP meta-interface MTU if necessary.
11593 	 */
11594 	if (IS_UNDER_IPMP(ill))
11595 		ipmp_illgrp_refresh_mtu(ill->ill_grp);
11596 
11597 	return (0);
11598 }
11599 
11600 /* ARGSUSED */
11601 int
ip_sioctl_get_lnkinfo(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipi,void * if_req)11602 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11603     ip_ioctl_cmd_t *ipi, void *if_req)
11604 {
11605 	struct lif_ifinfo_req *lir;
11606 	ill_t *ill = ipif->ipif_ill;
11607 
11608 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
11609 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11610 	if (ipif->ipif_id != 0)
11611 		return (EINVAL);
11612 
11613 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
11614 	lir->lir_maxhops = ill->ill_max_hops;
11615 	lir->lir_reachtime = ill->ill_reachable_time;
11616 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
11617 	lir->lir_maxmtu = ill->ill_mtu;
11618 
11619 	return (0);
11620 }
11621 
11622 /*
11623  * Return best guess as to the subnet mask for the specified address.
11624  * Based on the subnet masks for all the configured interfaces.
11625  *
11626  * We end up returning a zero mask in the case of default, multicast or
11627  * experimental.
11628  */
11629 static ipaddr_t
ip_subnet_mask(ipaddr_t addr,ipif_t ** ipifp,ip_stack_t * ipst)11630 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
11631 {
11632 	ipaddr_t net_mask;
11633 	ill_t	*ill;
11634 	ipif_t	*ipif;
11635 	ill_walk_context_t ctx;
11636 	ipif_t	*fallback_ipif = NULL;
11637 
11638 	net_mask = ip_net_mask(addr);
11639 	if (net_mask == 0) {
11640 		*ipifp = NULL;
11641 		return (0);
11642 	}
11643 
11644 	/* Let's check to see if this is maybe a local subnet route. */
11645 	/* this function only applies to IPv4 interfaces */
11646 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
11647 	ill = ILL_START_WALK_V4(&ctx, ipst);
11648 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
11649 		mutex_enter(&ill->ill_lock);
11650 		for (ipif = ill->ill_ipif; ipif != NULL;
11651 		    ipif = ipif->ipif_next) {
11652 			if (IPIF_IS_CONDEMNED(ipif))
11653 				continue;
11654 			if (!(ipif->ipif_flags & IPIF_UP))
11655 				continue;
11656 			if ((ipif->ipif_subnet & net_mask) ==
11657 			    (addr & net_mask)) {
11658 				/*
11659 				 * Don't trust pt-pt interfaces if there are
11660 				 * other interfaces.
11661 				 */
11662 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
11663 					if (fallback_ipif == NULL) {
11664 						ipif_refhold_locked(ipif);
11665 						fallback_ipif = ipif;
11666 					}
11667 					continue;
11668 				}
11669 
11670 				/*
11671 				 * Fine. Just assume the same net mask as the
11672 				 * directly attached subnet interface is using.
11673 				 */
11674 				ipif_refhold_locked(ipif);
11675 				mutex_exit(&ill->ill_lock);
11676 				rw_exit(&ipst->ips_ill_g_lock);
11677 				if (fallback_ipif != NULL)
11678 					ipif_refrele(fallback_ipif);
11679 				*ipifp = ipif;
11680 				return (ipif->ipif_net_mask);
11681 			}
11682 		}
11683 		mutex_exit(&ill->ill_lock);
11684 	}
11685 	rw_exit(&ipst->ips_ill_g_lock);
11686 
11687 	*ipifp = fallback_ipif;
11688 	return ((fallback_ipif != NULL) ?
11689 	    fallback_ipif->ipif_net_mask : net_mask);
11690 }
11691 
11692 /*
11693  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
11694  */
11695 static void
ip_wput_ioctl(queue_t * q,mblk_t * mp)11696 ip_wput_ioctl(queue_t *q, mblk_t *mp)
11697 {
11698 	IOCP	iocp;
11699 	ipft_t	*ipft;
11700 	ipllc_t	*ipllc;
11701 	mblk_t	*mp1;
11702 	cred_t	*cr;
11703 	int	error = 0;
11704 	conn_t	*connp;
11705 
11706 	ip1dbg(("ip_wput_ioctl"));
11707 	iocp = (IOCP)mp->b_rptr;
11708 	mp1 = mp->b_cont;
11709 	if (mp1 == NULL) {
11710 		iocp->ioc_error = EINVAL;
11711 		mp->b_datap->db_type = M_IOCNAK;
11712 		iocp->ioc_count = 0;
11713 		qreply(q, mp);
11714 		return;
11715 	}
11716 
11717 	/*
11718 	 * These IOCTLs provide various control capabilities to
11719 	 * upstream agents such as ULPs and processes.	There
11720 	 * are currently two such IOCTLs implemented.  They
11721 	 * are used by TCP to provide update information for
11722 	 * existing IREs and to forcibly delete an IRE for a
11723 	 * host that is not responding, thereby forcing an
11724 	 * attempt at a new route.
11725 	 */
11726 	iocp->ioc_error = EINVAL;
11727 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
11728 		goto done;
11729 
11730 	ipllc = (ipllc_t *)mp1->b_rptr;
11731 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
11732 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
11733 			break;
11734 	}
11735 	/*
11736 	 * prefer credential from mblk over ioctl;
11737 	 * see ip_sioctl_copyin_setup
11738 	 */
11739 	cr = msg_getcred(mp, NULL);
11740 	if (cr == NULL)
11741 		cr = iocp->ioc_cr;
11742 
11743 	/*
11744 	 * Refhold the conn in case the request gets queued up in some lookup
11745 	 */
11746 	ASSERT(CONN_Q(q));
11747 	connp = Q_TO_CONN(q);
11748 	CONN_INC_REF(connp);
11749 	CONN_INC_IOCTLREF(connp);
11750 	if (ipft->ipft_pfi &&
11751 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
11752 	    pullupmsg(mp1, ipft->ipft_min_size))) {
11753 		error = (*ipft->ipft_pfi)(q,
11754 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
11755 	}
11756 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
11757 		/*
11758 		 * CONN_OPER_PENDING_DONE happens in the function called
11759 		 * through ipft_pfi above.
11760 		 */
11761 		return;
11762 	}
11763 
11764 	CONN_DEC_IOCTLREF(connp);
11765 	CONN_OPER_PENDING_DONE(connp);
11766 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
11767 		freemsg(mp);
11768 		return;
11769 	}
11770 	iocp->ioc_error = error;
11771 
11772 done:
11773 	mp->b_datap->db_type = M_IOCACK;
11774 	if (iocp->ioc_error)
11775 		iocp->ioc_count = 0;
11776 	qreply(q, mp);
11777 }
11778 
11779 /*
11780  * Assign a unique id for the ipif. This is used by sctp_addr.c
11781  * Note: remove if sctp_addr.c is redone to not shadow ill/ipif data structures.
11782  */
11783 static void
ipif_assign_seqid(ipif_t * ipif)11784 ipif_assign_seqid(ipif_t *ipif)
11785 {
11786 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
11787 
11788 	ipif->ipif_seqid = atomic_inc_64_nv(&ipst->ips_ipif_g_seqid);
11789 }
11790 
11791 /*
11792  * Clone the contents of `sipif' to `dipif'.  Requires that both ipifs are
11793  * administratively down (i.e., no DAD), of the same type, and locked.  Note
11794  * that the clone is complete -- including the seqid -- and the expectation is
11795  * that the caller will either free or overwrite `sipif' before it's unlocked.
11796  */
11797 static void
ipif_clone(const ipif_t * sipif,ipif_t * dipif)11798 ipif_clone(const ipif_t *sipif, ipif_t *dipif)
11799 {
11800 	ASSERT(MUTEX_HELD(&sipif->ipif_ill->ill_lock));
11801 	ASSERT(MUTEX_HELD(&dipif->ipif_ill->ill_lock));
11802 	ASSERT(!(sipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11803 	ASSERT(!(dipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11804 	ASSERT(sipif->ipif_ire_type == dipif->ipif_ire_type);
11805 
11806 	dipif->ipif_flags = sipif->ipif_flags;
11807 	dipif->ipif_zoneid = sipif->ipif_zoneid;
11808 	dipif->ipif_v6subnet = sipif->ipif_v6subnet;
11809 	dipif->ipif_v6lcl_addr = sipif->ipif_v6lcl_addr;
11810 	dipif->ipif_v6net_mask = sipif->ipif_v6net_mask;
11811 	dipif->ipif_v6brd_addr = sipif->ipif_v6brd_addr;
11812 	dipif->ipif_v6pp_dst_addr = sipif->ipif_v6pp_dst_addr;
11813 
11814 	/*
11815 	 * As per the comment atop the function, we assume that these sipif
11816 	 * fields will be changed before sipif is unlocked.
11817 	 */
11818 	dipif->ipif_seqid = sipif->ipif_seqid;
11819 	dipif->ipif_state_flags = sipif->ipif_state_flags;
11820 }
11821 
11822 /*
11823  * Transfer the contents of `sipif' to `dipif', and then free (if `virgipif'
11824  * is NULL) or overwrite `sipif' with `virgipif', which must be a virgin
11825  * (unreferenced) ipif.  Also, if `sipif' is used by the current xop, then
11826  * transfer the xop to `dipif'.  Requires that all ipifs are administratively
11827  * down (i.e., no DAD), of the same type, and unlocked.
11828  */
11829 static void
ipif_transfer(ipif_t * sipif,ipif_t * dipif,ipif_t * virgipif)11830 ipif_transfer(ipif_t *sipif, ipif_t *dipif, ipif_t *virgipif)
11831 {
11832 	ipsq_t *ipsq = sipif->ipif_ill->ill_phyint->phyint_ipsq;
11833 	ipxop_t *ipx = ipsq->ipsq_xop;
11834 
11835 	ASSERT(sipif != dipif);
11836 	ASSERT(sipif != virgipif);
11837 
11838 	/*
11839 	 * Grab all of the locks that protect the ipif in a defined order.
11840 	 */
11841 	GRAB_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11842 
11843 	ipif_clone(sipif, dipif);
11844 	if (virgipif != NULL) {
11845 		ipif_clone(virgipif, sipif);
11846 		mi_free(virgipif);
11847 	}
11848 
11849 	RELEASE_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11850 
11851 	/*
11852 	 * Transfer ownership of the current xop, if necessary.
11853 	 */
11854 	if (ipx->ipx_current_ipif == sipif) {
11855 		ASSERT(ipx->ipx_pending_ipif == NULL);
11856 		mutex_enter(&ipx->ipx_lock);
11857 		ipx->ipx_current_ipif = dipif;
11858 		mutex_exit(&ipx->ipx_lock);
11859 	}
11860 
11861 	if (virgipif == NULL)
11862 		mi_free(sipif);
11863 }
11864 
11865 /*
11866  * checks if:
11867  *	- <ill_name>:<ipif_id> is at most LIFNAMSIZ - 1 and
11868  *	- logical interface is within the allowed range
11869  */
11870 static int
is_lifname_valid(ill_t * ill,unsigned int ipif_id)11871 is_lifname_valid(ill_t *ill, unsigned int ipif_id)
11872 {
11873 	if (snprintf(NULL, 0, "%s:%d", ill->ill_name, ipif_id) >= LIFNAMSIZ)
11874 		return (ENAMETOOLONG);
11875 
11876 	if (ipif_id >= ill->ill_ipst->ips_ip_addrs_per_if)
11877 		return (ERANGE);
11878 	return (0);
11879 }
11880 
11881 /*
11882  * Insert the ipif, so that the list of ipifs on the ill will be sorted
11883  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
11884  * be inserted into the first space available in the list. The value of
11885  * ipif_id will then be set to the appropriate value for its position.
11886  */
11887 static int
ipif_insert(ipif_t * ipif,boolean_t acquire_g_lock)11888 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock)
11889 {
11890 	ill_t *ill;
11891 	ipif_t *tipif;
11892 	ipif_t **tipifp;
11893 	int id, err;
11894 	ip_stack_t	*ipst;
11895 
11896 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
11897 	    IAM_WRITER_IPIF(ipif));
11898 
11899 	ill = ipif->ipif_ill;
11900 	ASSERT(ill != NULL);
11901 	ipst = ill->ill_ipst;
11902 
11903 	/*
11904 	 * In the case of lo0:0 we already hold the ill_g_lock.
11905 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
11906 	 * ipif_insert.
11907 	 */
11908 	if (acquire_g_lock)
11909 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
11910 	mutex_enter(&ill->ill_lock);
11911 	id = ipif->ipif_id;
11912 	tipifp = &(ill->ill_ipif);
11913 	if (id == -1) {	/* need to find a real id */
11914 		id = 0;
11915 		while ((tipif = *tipifp) != NULL) {
11916 			ASSERT(tipif->ipif_id >= id);
11917 			if (tipif->ipif_id != id)
11918 				break; /* non-consecutive id */
11919 			id++;
11920 			tipifp = &(tipif->ipif_next);
11921 		}
11922 		if ((err = is_lifname_valid(ill, id)) != 0) {
11923 			mutex_exit(&ill->ill_lock);
11924 			if (acquire_g_lock)
11925 				rw_exit(&ipst->ips_ill_g_lock);
11926 			return (err);
11927 		}
11928 		ipif->ipif_id = id; /* assign new id */
11929 	} else if ((err = is_lifname_valid(ill, id)) == 0) {
11930 		/* we have a real id; insert ipif in the right place */
11931 		while ((tipif = *tipifp) != NULL) {
11932 			ASSERT(tipif->ipif_id != id);
11933 			if (tipif->ipif_id > id)
11934 				break; /* found correct location */
11935 			tipifp = &(tipif->ipif_next);
11936 		}
11937 	} else {
11938 		mutex_exit(&ill->ill_lock);
11939 		if (acquire_g_lock)
11940 			rw_exit(&ipst->ips_ill_g_lock);
11941 		return (err);
11942 	}
11943 
11944 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
11945 
11946 	ipif->ipif_next = tipif;
11947 	*tipifp = ipif;
11948 	mutex_exit(&ill->ill_lock);
11949 	if (acquire_g_lock)
11950 		rw_exit(&ipst->ips_ill_g_lock);
11951 
11952 	return (0);
11953 }
11954 
11955 static void
ipif_remove(ipif_t * ipif)11956 ipif_remove(ipif_t *ipif)
11957 {
11958 	ipif_t	**ipifp;
11959 	ill_t	*ill = ipif->ipif_ill;
11960 
11961 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
11962 
11963 	mutex_enter(&ill->ill_lock);
11964 	ipifp = &ill->ill_ipif;
11965 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
11966 		if (*ipifp == ipif) {
11967 			*ipifp = ipif->ipif_next;
11968 			break;
11969 		}
11970 	}
11971 	mutex_exit(&ill->ill_lock);
11972 }
11973 
11974 /*
11975  * Allocate and initialize a new interface control structure.  (Always
11976  * called as writer.)
11977  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
11978  * is not part of the global linked list of ills. ipif_seqid is unique
11979  * in the system and to preserve the uniqueness, it is assigned only
11980  * when ill becomes part of the global list. At that point ill will
11981  * have a name. If it doesn't get assigned here, it will get assigned
11982  * in ipif_set_values() as part of SIOCSLIFNAME processing.
11983  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
11984  * the interface flags or any other information from the DL_INFO_ACK for
11985  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
11986  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
11987  * second DL_INFO_ACK comes in from the driver.
11988  */
11989 static ipif_t *
ipif_allocate(ill_t * ill,int id,uint_t ire_type,boolean_t initialize,boolean_t insert,int * errorp)11990 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize,
11991     boolean_t insert, int *errorp)
11992 {
11993 	int err;
11994 	ipif_t	*ipif;
11995 	ip_stack_t *ipst = ill->ill_ipst;
11996 
11997 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
11998 	    ill->ill_name, id, (void *)ill));
11999 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
12000 
12001 	if (errorp != NULL)
12002 		*errorp = 0;
12003 
12004 	if ((ipif = mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL) {
12005 		if (errorp != NULL)
12006 			*errorp = ENOMEM;
12007 		return (NULL);
12008 	}
12009 	*ipif = ipif_zero;	/* start clean */
12010 
12011 	ipif->ipif_ill = ill;
12012 	ipif->ipif_id = id;	/* could be -1 */
12013 	/*
12014 	 * Inherit the zoneid from the ill; for the shared stack instance
12015 	 * this is always the global zone
12016 	 */
12017 	ipif->ipif_zoneid = ill->ill_zoneid;
12018 
12019 	ipif->ipif_refcnt = 0;
12020 
12021 	if (insert) {
12022 		if ((err = ipif_insert(ipif, ire_type != IRE_LOOPBACK)) != 0) {
12023 			mi_free(ipif);
12024 			if (errorp != NULL)
12025 				*errorp = err;
12026 			return (NULL);
12027 		}
12028 		/* -1 id should have been replaced by real id */
12029 		id = ipif->ipif_id;
12030 		ASSERT(id >= 0);
12031 	}
12032 
12033 	if (ill->ill_name[0] != '\0')
12034 		ipif_assign_seqid(ipif);
12035 
12036 	/*
12037 	 * If this is the zeroth ipif on the IPMP ill, create the illgrp
12038 	 * (which must not exist yet because the zeroth ipif is created once
12039 	 * per ill).  However, do not not link it to the ipmp_grp_t until
12040 	 * I_PLINK is called; see ip_sioctl_plink_ipmp() for details.
12041 	 */
12042 	if (id == 0 && IS_IPMP(ill)) {
12043 		if (ipmp_illgrp_create(ill) == NULL) {
12044 			if (insert) {
12045 				rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
12046 				ipif_remove(ipif);
12047 				rw_exit(&ipst->ips_ill_g_lock);
12048 			}
12049 			mi_free(ipif);
12050 			if (errorp != NULL)
12051 				*errorp = ENOMEM;
12052 			return (NULL);
12053 		}
12054 	}
12055 
12056 	/*
12057 	 * We grab ill_lock to protect the flag changes.  The ipif is still
12058 	 * not up and can't be looked up until the ioctl completes and the
12059 	 * IPIF_CHANGING flag is cleared.
12060 	 */
12061 	mutex_enter(&ill->ill_lock);
12062 
12063 	ipif->ipif_ire_type = ire_type;
12064 
12065 	if (ipif->ipif_isv6) {
12066 		ill->ill_flags |= ILLF_IPV6;
12067 	} else {
12068 		ipaddr_t inaddr_any = INADDR_ANY;
12069 
12070 		ill->ill_flags |= ILLF_IPV4;
12071 
12072 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
12073 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12074 		    &ipif->ipif_v6lcl_addr);
12075 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12076 		    &ipif->ipif_v6subnet);
12077 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12078 		    &ipif->ipif_v6net_mask);
12079 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12080 		    &ipif->ipif_v6brd_addr);
12081 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12082 		    &ipif->ipif_v6pp_dst_addr);
12083 	}
12084 
12085 	/*
12086 	 * Don't set the interface flags etc. now, will do it in
12087 	 * ip_ll_subnet_defaults.
12088 	 */
12089 	if (!initialize)
12090 		goto out;
12091 
12092 	/*
12093 	 * NOTE: The IPMP meta-interface is special-cased because it starts
12094 	 * with no underlying interfaces (and thus an unknown broadcast
12095 	 * address length), but all interfaces that can be placed into an IPMP
12096 	 * group are required to be broadcast-capable.
12097 	 */
12098 	if (ill->ill_bcast_addr_length != 0 || IS_IPMP(ill)) {
12099 		/*
12100 		 * Later detect lack of DLPI driver multicast capability by
12101 		 * catching DL_ENABMULTI_REQ errors in ip_rput_dlpi().
12102 		 */
12103 		ill->ill_flags |= ILLF_MULTICAST;
12104 		if (!ipif->ipif_isv6)
12105 			ipif->ipif_flags |= IPIF_BROADCAST;
12106 	} else {
12107 		if (ill->ill_net_type != IRE_LOOPBACK) {
12108 			if (ipif->ipif_isv6)
12109 				/*
12110 				 * Note: xresolv interfaces will eventually need
12111 				 * NOARP set here as well, but that will require
12112 				 * those external resolvers to have some
12113 				 * knowledge of that flag and act appropriately.
12114 				 * Not to be changed at present.
12115 				 */
12116 				ill->ill_flags |= ILLF_NONUD;
12117 			else
12118 				ill->ill_flags |= ILLF_NOARP;
12119 		}
12120 		if (ill->ill_phys_addr_length == 0) {
12121 			if (IS_VNI(ill)) {
12122 				ipif->ipif_flags |= IPIF_NOXMIT;
12123 			} else {
12124 				/* pt-pt supports multicast. */
12125 				ill->ill_flags |= ILLF_MULTICAST;
12126 				if (ill->ill_net_type != IRE_LOOPBACK)
12127 					ipif->ipif_flags |= IPIF_POINTOPOINT;
12128 			}
12129 		}
12130 	}
12131 out:
12132 	mutex_exit(&ill->ill_lock);
12133 	return (ipif);
12134 }
12135 
12136 /*
12137  * Remove the neighbor cache entries associated with this logical
12138  * interface.
12139  */
12140 int
ipif_arp_down(ipif_t * ipif)12141 ipif_arp_down(ipif_t *ipif)
12142 {
12143 	ill_t	*ill = ipif->ipif_ill;
12144 	int	err = 0;
12145 
12146 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
12147 	ASSERT(IAM_WRITER_IPIF(ipif));
12148 
12149 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_down",
12150 	    ill_t *, ill, ipif_t *, ipif);
12151 	ipif_nce_down(ipif);
12152 
12153 	/*
12154 	 * If this is the last ipif that is going down and there are no
12155 	 * duplicate addresses we may yet attempt to re-probe, then we need to
12156 	 * clean up ARP completely.
12157 	 */
12158 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
12159 	    !ill->ill_logical_down && ill->ill_net_type == IRE_IF_RESOLVER) {
12160 		/*
12161 		 * If this was the last ipif on an IPMP interface, purge any
12162 		 * static ARP entries associated with it.
12163 		 */
12164 		if (IS_IPMP(ill))
12165 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
12166 
12167 		/* UNBIND, DETACH */
12168 		err = arp_ll_down(ill);
12169 	}
12170 
12171 	return (err);
12172 }
12173 
12174 /*
12175  * Get the resolver set up for a new IP address.  (Always called as writer.)
12176  * Called both for IPv4 and IPv6 interfaces, though it only does some
12177  * basic DAD related initialization for IPv6. Honors ILLF_NOARP.
12178  *
12179  * The enumerated value res_act tunes the behavior:
12180  *	* Res_act_initial: set up all the resolver structures for a new
12181  *	  IP address.
12182  *	* Res_act_defend: tell ARP that it needs to send a single gratuitous
12183  *	  ARP message in defense of the address.
12184  *	* Res_act_rebind: tell ARP to change the hardware address for an IP
12185  *	  address (and issue gratuitous ARPs).  Used by ipmp_ill_bind_ipif().
12186  *
12187  * Returns zero on success, or an errno upon failure.
12188  */
12189 int
ipif_resolver_up(ipif_t * ipif,enum ip_resolver_action res_act)12190 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
12191 {
12192 	ill_t		*ill = ipif->ipif_ill;
12193 	int		err;
12194 	boolean_t	was_dup;
12195 
12196 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
12197 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
12198 	ASSERT(IAM_WRITER_IPIF(ipif));
12199 
12200 	was_dup = B_FALSE;
12201 	if (res_act == Res_act_initial) {
12202 		ipif->ipif_addr_ready = 0;
12203 		/*
12204 		 * We're bringing an interface up here.  There's no way that we
12205 		 * should need to shut down ARP now.
12206 		 */
12207 		mutex_enter(&ill->ill_lock);
12208 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
12209 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
12210 			ill->ill_ipif_dup_count--;
12211 			was_dup = B_TRUE;
12212 		}
12213 		mutex_exit(&ill->ill_lock);
12214 	}
12215 	if (ipif->ipif_recovery_id != 0)
12216 		(void) untimeout(ipif->ipif_recovery_id);
12217 	ipif->ipif_recovery_id = 0;
12218 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
12219 		ipif->ipif_addr_ready = 1;
12220 		return (0);
12221 	}
12222 	/* NDP will set the ipif_addr_ready flag when it's ready */
12223 	if (ill->ill_isv6)
12224 		return (0);
12225 
12226 	err = ipif_arp_up(ipif, res_act, was_dup);
12227 	return (err);
12228 }
12229 
12230 /*
12231  * This routine restarts IPv4/IPv6 duplicate address detection (DAD)
12232  * when a link has just gone back up.
12233  */
12234 static void
ipif_nce_start_dad(ipif_t * ipif)12235 ipif_nce_start_dad(ipif_t *ipif)
12236 {
12237 	ncec_t *ncec;
12238 	ill_t *ill = ipif->ipif_ill;
12239 	boolean_t isv6 = ill->ill_isv6;
12240 
12241 	if (isv6) {
12242 		ncec = ncec_lookup_illgrp_v6(ipif->ipif_ill,
12243 		    &ipif->ipif_v6lcl_addr);
12244 	} else {
12245 		ipaddr_t v4addr;
12246 
12247 		if (ill->ill_net_type != IRE_IF_RESOLVER ||
12248 		    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
12249 		    ipif->ipif_lcl_addr == INADDR_ANY) {
12250 			/*
12251 			 * If we can't contact ARP for some reason,
12252 			 * that's not really a problem.  Just send
12253 			 * out the routing socket notification that
12254 			 * DAD completion would have done, and continue.
12255 			 */
12256 			ipif_mask_reply(ipif);
12257 			ipif_up_notify(ipif);
12258 			ipif->ipif_addr_ready = 1;
12259 			return;
12260 		}
12261 
12262 		IN6_V4MAPPED_TO_IPADDR(&ipif->ipif_v6lcl_addr, v4addr);
12263 		ncec = ncec_lookup_illgrp_v4(ipif->ipif_ill, &v4addr);
12264 	}
12265 
12266 	if (ncec == NULL) {
12267 		ip1dbg(("couldn't find ncec for ipif %p leaving !ready\n",
12268 		    (void *)ipif));
12269 		return;
12270 	}
12271 	if (!nce_restart_dad(ncec)) {
12272 		/*
12273 		 * If we can't restart DAD for some reason, that's not really a
12274 		 * problem.  Just send out the routing socket notification that
12275 		 * DAD completion would have done, and continue.
12276 		 */
12277 		ipif_up_notify(ipif);
12278 		ipif->ipif_addr_ready = 1;
12279 	}
12280 	ncec_refrele(ncec);
12281 }
12282 
12283 /*
12284  * Restart duplicate address detection on all interfaces on the given ill.
12285  *
12286  * This is called when an interface transitions from down to up
12287  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
12288  *
12289  * Note that since the underlying physical link has transitioned, we must cause
12290  * at least one routing socket message to be sent here, either via DAD
12291  * completion or just by default on the first ipif.  (If we don't do this, then
12292  * in.mpathd will see long delays when doing link-based failure recovery.)
12293  */
12294 void
ill_restart_dad(ill_t * ill,boolean_t went_up)12295 ill_restart_dad(ill_t *ill, boolean_t went_up)
12296 {
12297 	ipif_t *ipif;
12298 
12299 	if (ill == NULL)
12300 		return;
12301 
12302 	/*
12303 	 * If layer two doesn't support duplicate address detection, then just
12304 	 * send the routing socket message now and be done with it.
12305 	 */
12306 	if (!ill->ill_isv6 && arp_no_defense) {
12307 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
12308 		return;
12309 	}
12310 
12311 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12312 		if (went_up) {
12313 
12314 			if (ipif->ipif_flags & IPIF_UP) {
12315 				ipif_nce_start_dad(ipif);
12316 			} else if (ipif->ipif_flags & IPIF_DUPLICATE) {
12317 				/*
12318 				 * kick off the bring-up process now.
12319 				 */
12320 				ipif_do_recovery(ipif);
12321 			} else {
12322 				/*
12323 				 * Unfortunately, the first ipif is "special"
12324 				 * and represents the underlying ill in the
12325 				 * routing socket messages.  Thus, when this
12326 				 * one ipif is down, we must still notify so
12327 				 * that the user knows the IFF_RUNNING status
12328 				 * change.  (If the first ipif is up, then
12329 				 * we'll handle eventual routing socket
12330 				 * notification via DAD completion.)
12331 				 */
12332 				if (ipif == ill->ill_ipif) {
12333 					ip_rts_ifmsg(ill->ill_ipif,
12334 					    RTSQ_DEFAULT);
12335 				}
12336 			}
12337 		} else {
12338 			/*
12339 			 * After link down, we'll need to send a new routing
12340 			 * message when the link comes back, so clear
12341 			 * ipif_addr_ready.
12342 			 */
12343 			ipif->ipif_addr_ready = 0;
12344 		}
12345 	}
12346 
12347 	/*
12348 	 * If we've torn down links, then notify the user right away.
12349 	 */
12350 	if (!went_up)
12351 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
12352 }
12353 
12354 static void
ipsq_delete(ipsq_t * ipsq)12355 ipsq_delete(ipsq_t *ipsq)
12356 {
12357 	ipxop_t *ipx = ipsq->ipsq_xop;
12358 
12359 	ipsq->ipsq_ipst = NULL;
12360 	ASSERT(ipsq->ipsq_phyint == NULL);
12361 	ASSERT(ipsq->ipsq_xop != NULL);
12362 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipx->ipx_mphead == NULL);
12363 	ASSERT(ipx->ipx_pending_mp == NULL);
12364 	kmem_free(ipsq, sizeof (ipsq_t));
12365 }
12366 
12367 static int
ill_up_ipifs_on_ill(ill_t * ill,queue_t * q,mblk_t * mp)12368 ill_up_ipifs_on_ill(ill_t *ill, queue_t *q, mblk_t *mp)
12369 {
12370 	int err = 0;
12371 	ipif_t *ipif;
12372 
12373 	if (ill == NULL)
12374 		return (0);
12375 
12376 	ASSERT(IAM_WRITER_ILL(ill));
12377 	ill->ill_up_ipifs = B_TRUE;
12378 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12379 		if (ipif->ipif_was_up) {
12380 			if (!(ipif->ipif_flags & IPIF_UP))
12381 				err = ipif_up(ipif, q, mp);
12382 			ipif->ipif_was_up = B_FALSE;
12383 			if (err != 0) {
12384 				ASSERT(err == EINPROGRESS);
12385 				return (err);
12386 			}
12387 		}
12388 	}
12389 	ill->ill_up_ipifs = B_FALSE;
12390 	return (0);
12391 }
12392 
12393 /*
12394  * This function is called to bring up all the ipifs that were up before
12395  * bringing the ill down via ill_down_ipifs().
12396  */
12397 int
ill_up_ipifs(ill_t * ill,queue_t * q,mblk_t * mp)12398 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
12399 {
12400 	int err;
12401 
12402 	ASSERT(IAM_WRITER_ILL(ill));
12403 
12404 	if (ill->ill_replumbing) {
12405 		ill->ill_replumbing = 0;
12406 		/*
12407 		 * Send down REPLUMB_DONE notification followed by the
12408 		 * BIND_REQ on the arp stream.
12409 		 */
12410 		if (!ill->ill_isv6)
12411 			arp_send_replumb_conf(ill);
12412 	}
12413 	err = ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv4, q, mp);
12414 	if (err != 0)
12415 		return (err);
12416 
12417 	return (ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv6, q, mp));
12418 }
12419 
12420 /*
12421  * Bring down any IPIF_UP ipifs on ill. If "logical" is B_TRUE, we bring
12422  * down the ipifs without sending DL_UNBIND_REQ to the driver.
12423  */
12424 static void
ill_down_ipifs(ill_t * ill,boolean_t logical)12425 ill_down_ipifs(ill_t *ill, boolean_t logical)
12426 {
12427 	ipif_t *ipif;
12428 
12429 	ASSERT(IAM_WRITER_ILL(ill));
12430 
12431 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12432 		/*
12433 		 * We go through the ipif_down logic even if the ipif
12434 		 * is already down, since routes can be added based
12435 		 * on down ipifs. Going through ipif_down once again
12436 		 * will delete any IREs created based on these routes.
12437 		 */
12438 		if (ipif->ipif_flags & IPIF_UP)
12439 			ipif->ipif_was_up = B_TRUE;
12440 
12441 		if (logical) {
12442 			(void) ipif_logical_down(ipif, NULL, NULL);
12443 			ipif_non_duplicate(ipif);
12444 			(void) ipif_down_tail(ipif);
12445 		} else {
12446 			(void) ipif_down(ipif, NULL, NULL);
12447 		}
12448 	}
12449 }
12450 
12451 /*
12452  * Redo source address selection.  This makes IXAF_VERIFY_SOURCE take
12453  * a look again at valid source addresses.
12454  * This should be called each time after the set of source addresses has been
12455  * changed.
12456  */
12457 void
ip_update_source_selection(ip_stack_t * ipst)12458 ip_update_source_selection(ip_stack_t *ipst)
12459 {
12460 	/* We skip past SRC_GENERATION_VERIFY */
12461 	if (atomic_inc_32_nv(&ipst->ips_src_generation) ==
12462 	    SRC_GENERATION_VERIFY)
12463 		atomic_inc_32(&ipst->ips_src_generation);
12464 }
12465 
12466 /*
12467  * Finish the group join started in ip_sioctl_groupname().
12468  */
12469 /* ARGSUSED */
12470 static void
ip_join_illgrps(ipsq_t * ipsq,queue_t * q,mblk_t * mp,void * dummy)12471 ip_join_illgrps(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
12472 {
12473 	ill_t		*ill = q->q_ptr;
12474 	phyint_t	*phyi = ill->ill_phyint;
12475 	ipmp_grp_t	*grp = phyi->phyint_grp;
12476 	ip_stack_t	*ipst = ill->ill_ipst;
12477 
12478 	/* IS_UNDER_IPMP() won't work until ipmp_ill_join_illgrp() is called */
12479 	ASSERT(!IS_IPMP(ill) && grp != NULL);
12480 	ASSERT(IAM_WRITER_IPSQ(ipsq));
12481 
12482 	if (phyi->phyint_illv4 != NULL) {
12483 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12484 		VERIFY(grp->gr_pendv4-- > 0);
12485 		rw_exit(&ipst->ips_ipmp_lock);
12486 		ipmp_ill_join_illgrp(phyi->phyint_illv4, grp->gr_v4);
12487 	}
12488 	if (phyi->phyint_illv6 != NULL) {
12489 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12490 		VERIFY(grp->gr_pendv6-- > 0);
12491 		rw_exit(&ipst->ips_ipmp_lock);
12492 		ipmp_ill_join_illgrp(phyi->phyint_illv6, grp->gr_v6);
12493 	}
12494 	freemsg(mp);
12495 }
12496 
12497 /*
12498  * Process an SIOCSLIFGROUPNAME request.
12499  */
12500 /* ARGSUSED */
12501 int
ip_sioctl_groupname(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * ifreq)12502 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12503     ip_ioctl_cmd_t *ipip, void *ifreq)
12504 {
12505 	struct lifreq	*lifr = ifreq;
12506 	ill_t		*ill = ipif->ipif_ill;
12507 	ip_stack_t	*ipst = ill->ill_ipst;
12508 	phyint_t	*phyi = ill->ill_phyint;
12509 	ipmp_grp_t	*grp = phyi->phyint_grp;
12510 	mblk_t		*ipsq_mp;
12511 	int		err = 0;
12512 
12513 	/*
12514 	 * Note that phyint_grp can only change here, where we're exclusive.
12515 	 */
12516 	ASSERT(IAM_WRITER_ILL(ill));
12517 
12518 	if (ipif->ipif_id != 0 || ill->ill_usesrc_grp_next != NULL ||
12519 	    (phyi->phyint_flags & PHYI_VIRTUAL))
12520 		return (EINVAL);
12521 
12522 	lifr->lifr_groupname[LIFGRNAMSIZ - 1] = '\0';
12523 
12524 	rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12525 
12526 	/*
12527 	 * If the name hasn't changed, there's nothing to do.
12528 	 */
12529 	if (grp != NULL && strcmp(grp->gr_name, lifr->lifr_groupname) == 0)
12530 		goto unlock;
12531 
12532 	/*
12533 	 * Handle requests to rename an IPMP meta-interface.
12534 	 *
12535 	 * Note that creation of the IPMP meta-interface is handled in
12536 	 * userland through the standard plumbing sequence.  As part of the
12537 	 * plumbing the IPMP meta-interface, its initial groupname is set to
12538 	 * the name of the interface (see ipif_set_values_tail()).
12539 	 */
12540 	if (IS_IPMP(ill)) {
12541 		err = ipmp_grp_rename(grp, lifr->lifr_groupname);
12542 		goto unlock;
12543 	}
12544 
12545 	/*
12546 	 * Handle requests to add or remove an IP interface from a group.
12547 	 */
12548 	if (lifr->lifr_groupname[0] != '\0') {			/* add */
12549 		/*
12550 		 * Moves are handled by first removing the interface from
12551 		 * its existing group, and then adding it to another group.
12552 		 * So, fail if it's already in a group.
12553 		 */
12554 		if (IS_UNDER_IPMP(ill)) {
12555 			err = EALREADY;
12556 			goto unlock;
12557 		}
12558 
12559 		grp = ipmp_grp_lookup(lifr->lifr_groupname, ipst);
12560 		if (grp == NULL) {
12561 			err = ENOENT;
12562 			goto unlock;
12563 		}
12564 
12565 		/*
12566 		 * Check if the phyint and its ills are suitable for
12567 		 * inclusion into the group.
12568 		 */
12569 		if ((err = ipmp_grp_vet_phyint(grp, phyi)) != 0)
12570 			goto unlock;
12571 
12572 		/*
12573 		 * Checks pass; join the group, and enqueue the remaining
12574 		 * illgrp joins for when we've become part of the group xop
12575 		 * and are exclusive across its IPSQs.  Since qwriter_ip()
12576 		 * requires an mblk_t to scribble on, and since `mp' will be
12577 		 * freed as part of completing the ioctl, allocate another.
12578 		 */
12579 		if ((ipsq_mp = allocb(0, BPRI_MED)) == NULL) {
12580 			err = ENOMEM;
12581 			goto unlock;
12582 		}
12583 
12584 		/*
12585 		 * Before we drop ipmp_lock, bump gr_pend* to ensure that the
12586 		 * IPMP meta-interface ills needed by `phyi' cannot go away
12587 		 * before ip_join_illgrps() is called back.  See the comments
12588 		 * in ip_sioctl_plink_ipmp() for more.
12589 		 */
12590 		if (phyi->phyint_illv4 != NULL)
12591 			grp->gr_pendv4++;
12592 		if (phyi->phyint_illv6 != NULL)
12593 			grp->gr_pendv6++;
12594 
12595 		rw_exit(&ipst->ips_ipmp_lock);
12596 
12597 		ipmp_phyint_join_grp(phyi, grp);
12598 		ill_refhold(ill);
12599 		qwriter_ip(ill, ill->ill_rq, ipsq_mp, ip_join_illgrps,
12600 		    SWITCH_OP, B_FALSE);
12601 		return (0);
12602 	} else {
12603 		/*
12604 		 * Request to remove the interface from a group.  If the
12605 		 * interface is not in a group, this trivially succeeds.
12606 		 */
12607 		rw_exit(&ipst->ips_ipmp_lock);
12608 		if (IS_UNDER_IPMP(ill))
12609 			ipmp_phyint_leave_grp(phyi);
12610 		return (0);
12611 	}
12612 unlock:
12613 	rw_exit(&ipst->ips_ipmp_lock);
12614 	return (err);
12615 }
12616 
12617 /*
12618  * Process an SIOCGLIFBINDING request.
12619  */
12620 /* ARGSUSED */
12621 int
ip_sioctl_get_binding(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * ifreq)12622 ip_sioctl_get_binding(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12623     ip_ioctl_cmd_t *ipip, void *ifreq)
12624 {
12625 	ill_t		*ill;
12626 	struct lifreq	*lifr = ifreq;
12627 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12628 
12629 	if (!IS_IPMP(ipif->ipif_ill))
12630 		return (EINVAL);
12631 
12632 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12633 	if ((ill = ipif->ipif_bound_ill) == NULL)
12634 		lifr->lifr_binding[0] = '\0';
12635 	else
12636 		(void) strlcpy(lifr->lifr_binding, ill->ill_name, LIFNAMSIZ);
12637 	rw_exit(&ipst->ips_ipmp_lock);
12638 	return (0);
12639 }
12640 
12641 /*
12642  * Process an SIOCGLIFGROUPNAME request.
12643  */
12644 /* ARGSUSED */
12645 int
ip_sioctl_get_groupname(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * ifreq)12646 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12647     ip_ioctl_cmd_t *ipip, void *ifreq)
12648 {
12649 	ipmp_grp_t	*grp;
12650 	struct lifreq	*lifr = ifreq;
12651 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12652 
12653 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12654 	if ((grp = ipif->ipif_ill->ill_phyint->phyint_grp) == NULL)
12655 		lifr->lifr_groupname[0] = '\0';
12656 	else
12657 		(void) strlcpy(lifr->lifr_groupname, grp->gr_name, LIFGRNAMSIZ);
12658 	rw_exit(&ipst->ips_ipmp_lock);
12659 	return (0);
12660 }
12661 
12662 /*
12663  * Process an SIOCGLIFGROUPINFO request.
12664  */
12665 /* ARGSUSED */
12666 int
ip_sioctl_groupinfo(ipif_t * dummy_ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * dummy)12667 ip_sioctl_groupinfo(ipif_t *dummy_ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12668     ip_ioctl_cmd_t *ipip, void *dummy)
12669 {
12670 	ipmp_grp_t	*grp;
12671 	lifgroupinfo_t	*lifgr;
12672 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
12673 
12674 	/* ip_wput_nondata() verified mp->b_cont->b_cont */
12675 	lifgr = (lifgroupinfo_t *)mp->b_cont->b_cont->b_rptr;
12676 	lifgr->gi_grname[LIFGRNAMSIZ - 1] = '\0';
12677 
12678 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12679 	if ((grp = ipmp_grp_lookup(lifgr->gi_grname, ipst)) == NULL) {
12680 		rw_exit(&ipst->ips_ipmp_lock);
12681 		return (ENOENT);
12682 	}
12683 	ipmp_grp_info(grp, lifgr);
12684 	rw_exit(&ipst->ips_ipmp_lock);
12685 	return (0);
12686 }
12687 
12688 static void
ill_dl_down(ill_t * ill)12689 ill_dl_down(ill_t *ill)
12690 {
12691 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_down", ill_t *, ill);
12692 
12693 	/*
12694 	 * The ill is down; unbind but stay attached since we're still
12695 	 * associated with a PPA. If we have negotiated DLPI capabilites
12696 	 * with the data link service provider (IDS_OK) then reset them.
12697 	 * The interval between unbinding and rebinding is potentially
12698 	 * unbounded hence we cannot assume things will be the same.
12699 	 * The DLPI capabilities will be probed again when the data link
12700 	 * is brought up.
12701 	 */
12702 	mblk_t	*mp = ill->ill_unbind_mp;
12703 
12704 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
12705 
12706 	if (!ill->ill_replumbing) {
12707 		/* Free all ilms for this ill */
12708 		update_conn_ill(ill, ill->ill_ipst);
12709 	} else {
12710 		ill_leave_multicast(ill);
12711 	}
12712 
12713 	ill->ill_unbind_mp = NULL;
12714 	if (mp != NULL) {
12715 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
12716 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
12717 		    ill->ill_name));
12718 		mutex_enter(&ill->ill_lock);
12719 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
12720 		mutex_exit(&ill->ill_lock);
12721 		/*
12722 		 * ip_rput does not pass up normal (M_PROTO) DLPI messages
12723 		 * after ILL_CONDEMNED is set. So in the unplumb case, we call
12724 		 * ill_capability_dld_disable disable rightaway. If this is not
12725 		 * an unplumb operation then the disable happens on receipt of
12726 		 * the capab ack via ip_rput_dlpi_writer ->
12727 		 * ill_capability_ack_thr. In both cases the order of
12728 		 * the operations seen by DLD is capability disable followed
12729 		 * by DL_UNBIND. Also the DLD capability disable needs a
12730 		 * cv_wait'able context.
12731 		 */
12732 		if (ill->ill_state_flags & ILL_CONDEMNED)
12733 			ill_capability_dld_disable(ill);
12734 		ill_capability_reset(ill, B_FALSE);
12735 		ill_dlpi_send(ill, mp);
12736 	}
12737 	mutex_enter(&ill->ill_lock);
12738 	ill->ill_dl_up = 0;
12739 	ill_nic_event_dispatch(ill, 0, NE_DOWN, NULL, 0);
12740 	mutex_exit(&ill->ill_lock);
12741 }
12742 
12743 void
ill_dlpi_dispatch(ill_t * ill,mblk_t * mp)12744 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
12745 {
12746 	union DL_primitives *dlp;
12747 	t_uscalar_t prim;
12748 	boolean_t waitack = B_FALSE;
12749 
12750 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12751 
12752 	dlp = (union DL_primitives *)mp->b_rptr;
12753 	prim = dlp->dl_primitive;
12754 
12755 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
12756 	    dl_primstr(prim), prim, ill->ill_name));
12757 
12758 	switch (prim) {
12759 	case DL_PHYS_ADDR_REQ:
12760 	{
12761 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
12762 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
12763 		break;
12764 	}
12765 	case DL_BIND_REQ:
12766 		mutex_enter(&ill->ill_lock);
12767 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
12768 		mutex_exit(&ill->ill_lock);
12769 		break;
12770 	}
12771 
12772 	/*
12773 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
12774 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
12775 	 * we only wait for the ACK of the DL_UNBIND_REQ.
12776 	 */
12777 	mutex_enter(&ill->ill_lock);
12778 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
12779 	    (prim == DL_UNBIND_REQ)) {
12780 		ill->ill_dlpi_pending = prim;
12781 		waitack = B_TRUE;
12782 	}
12783 
12784 	mutex_exit(&ill->ill_lock);
12785 	DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_dispatch",
12786 	    char *, dl_primstr(prim), ill_t *, ill);
12787 	putnext(ill->ill_wq, mp);
12788 
12789 	/*
12790 	 * There is no ack for DL_NOTIFY_CONF messages
12791 	 */
12792 	if (waitack && prim == DL_NOTIFY_CONF)
12793 		ill_dlpi_done(ill, prim);
12794 }
12795 
12796 /*
12797  * Helper function for ill_dlpi_send().
12798  */
12799 /* ARGSUSED */
12800 static void
ill_dlpi_send_writer(ipsq_t * ipsq,queue_t * q,mblk_t * mp,void * arg)12801 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
12802 {
12803 	ill_dlpi_send(q->q_ptr, mp);
12804 }
12805 
12806 /*
12807  * Send a DLPI control message to the driver but make sure there
12808  * is only one outstanding message. Uses ill_dlpi_pending to tell
12809  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
12810  * when an ACK or a NAK is received to process the next queued message.
12811  */
12812 void
ill_dlpi_send(ill_t * ill,mblk_t * mp)12813 ill_dlpi_send(ill_t *ill, mblk_t *mp)
12814 {
12815 	mblk_t **mpp;
12816 
12817 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12818 
12819 	/*
12820 	 * To ensure that any DLPI requests for current exclusive operation
12821 	 * are always completely sent before any DLPI messages for other
12822 	 * operations, require writer access before enqueuing.
12823 	 */
12824 	if (!IAM_WRITER_ILL(ill)) {
12825 		ill_refhold(ill);
12826 		/* qwriter_ip() does the ill_refrele() */
12827 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
12828 		    NEW_OP, B_TRUE);
12829 		return;
12830 	}
12831 
12832 	mutex_enter(&ill->ill_lock);
12833 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
12834 		/* Must queue message. Tail insertion */
12835 		mpp = &ill->ill_dlpi_deferred;
12836 		while (*mpp != NULL)
12837 			mpp = &((*mpp)->b_next);
12838 
12839 		ip1dbg(("ill_dlpi_send: deferring request for %s "
12840 		    "while %s pending\n", ill->ill_name,
12841 		    dl_primstr(ill->ill_dlpi_pending)));
12842 
12843 		*mpp = mp;
12844 		mutex_exit(&ill->ill_lock);
12845 		return;
12846 	}
12847 	mutex_exit(&ill->ill_lock);
12848 	ill_dlpi_dispatch(ill, mp);
12849 }
12850 
12851 void
ill_capability_send(ill_t * ill,mblk_t * mp)12852 ill_capability_send(ill_t *ill, mblk_t *mp)
12853 {
12854 	ill->ill_capab_pending_cnt++;
12855 	ill_dlpi_send(ill, mp);
12856 }
12857 
12858 void
ill_capability_done(ill_t * ill)12859 ill_capability_done(ill_t *ill)
12860 {
12861 	ASSERT(ill->ill_capab_pending_cnt != 0);
12862 
12863 	ill_dlpi_done(ill, DL_CAPABILITY_REQ);
12864 
12865 	ill->ill_capab_pending_cnt--;
12866 	if (ill->ill_capab_pending_cnt == 0 &&
12867 	    ill->ill_dlpi_capab_state == IDCS_OK)
12868 		ill_capability_reset_alloc(ill);
12869 }
12870 
12871 /*
12872  * Send all deferred DLPI messages without waiting for their ACKs.
12873  */
12874 void
ill_dlpi_send_deferred(ill_t * ill)12875 ill_dlpi_send_deferred(ill_t *ill)
12876 {
12877 	mblk_t *mp, *nextmp;
12878 
12879 	/*
12880 	 * Clear ill_dlpi_pending so that the message is not queued in
12881 	 * ill_dlpi_send().
12882 	 */
12883 	mutex_enter(&ill->ill_lock);
12884 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
12885 	mp = ill->ill_dlpi_deferred;
12886 	ill->ill_dlpi_deferred = NULL;
12887 	mutex_exit(&ill->ill_lock);
12888 
12889 	for (; mp != NULL; mp = nextmp) {
12890 		nextmp = mp->b_next;
12891 		mp->b_next = NULL;
12892 		ill_dlpi_send(ill, mp);
12893 	}
12894 }
12895 
12896 /*
12897  * Clear all the deferred DLPI messages. Called on receiving an M_ERROR
12898  * or M_HANGUP
12899  */
12900 static void
ill_dlpi_clear_deferred(ill_t * ill)12901 ill_dlpi_clear_deferred(ill_t *ill)
12902 {
12903 	mblk_t	*mp, *nextmp;
12904 
12905 	mutex_enter(&ill->ill_lock);
12906 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
12907 	mp = ill->ill_dlpi_deferred;
12908 	ill->ill_dlpi_deferred = NULL;
12909 	mutex_exit(&ill->ill_lock);
12910 
12911 	for (; mp != NULL; mp = nextmp) {
12912 		nextmp = mp->b_next;
12913 		inet_freemsg(mp);
12914 	}
12915 }
12916 
12917 /*
12918  * Check if the DLPI primitive `prim' is pending; print a warning if not.
12919  */
12920 boolean_t
ill_dlpi_pending(ill_t * ill,t_uscalar_t prim)12921 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
12922 {
12923 	t_uscalar_t pending;
12924 
12925 	mutex_enter(&ill->ill_lock);
12926 	if (ill->ill_dlpi_pending == prim) {
12927 		mutex_exit(&ill->ill_lock);
12928 		return (B_TRUE);
12929 	}
12930 
12931 	/*
12932 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
12933 	 * without waiting, so don't print any warnings in that case.
12934 	 */
12935 	if (ill->ill_state_flags & ILL_CONDEMNED) {
12936 		mutex_exit(&ill->ill_lock);
12937 		return (B_FALSE);
12938 	}
12939 	pending = ill->ill_dlpi_pending;
12940 	mutex_exit(&ill->ill_lock);
12941 
12942 	if (pending == DL_PRIM_INVAL) {
12943 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12944 		    "received unsolicited ack for %s on %s\n",
12945 		    dl_primstr(prim), ill->ill_name);
12946 	} else {
12947 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12948 		    "received unexpected ack for %s on %s (expecting %s)\n",
12949 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
12950 	}
12951 	return (B_FALSE);
12952 }
12953 
12954 /*
12955  * Complete the current DLPI operation associated with `prim' on `ill' and
12956  * start the next queued DLPI operation (if any).  If there are no queued DLPI
12957  * operations and the ill's current exclusive IPSQ operation has finished
12958  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
12959  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
12960  * the comments above ipsq_current_finish() for details.
12961  */
12962 void
ill_dlpi_done(ill_t * ill,t_uscalar_t prim)12963 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
12964 {
12965 	mblk_t *mp;
12966 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
12967 	ipxop_t *ipx = ipsq->ipsq_xop;
12968 
12969 	ASSERT(IAM_WRITER_IPSQ(ipsq));
12970 	mutex_enter(&ill->ill_lock);
12971 
12972 	ASSERT(prim != DL_PRIM_INVAL);
12973 	ASSERT(ill->ill_dlpi_pending == prim);
12974 
12975 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
12976 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
12977 
12978 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
12979 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
12980 		if (ipx->ipx_current_done) {
12981 			mutex_enter(&ipx->ipx_lock);
12982 			ipx->ipx_current_ipif = NULL;
12983 			mutex_exit(&ipx->ipx_lock);
12984 		}
12985 		cv_signal(&ill->ill_cv);
12986 		mutex_exit(&ill->ill_lock);
12987 		return;
12988 	}
12989 
12990 	ill->ill_dlpi_deferred = mp->b_next;
12991 	mp->b_next = NULL;
12992 	mutex_exit(&ill->ill_lock);
12993 
12994 	ill_dlpi_dispatch(ill, mp);
12995 }
12996 
12997 /*
12998  * Queue a (multicast) DLPI control message to be sent to the driver by
12999  * later calling ill_dlpi_send_queued.
13000  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
13001  * are sent in order i.e., prevent a DL_DISABMULTI_REQ and DL_ENABMULTI_REQ
13002  * for the same group to race.
13003  * We send DLPI control messages in order using ill_lock.
13004  * For IPMP we should be called on the cast_ill.
13005  */
13006 void
ill_dlpi_queue(ill_t * ill,mblk_t * mp)13007 ill_dlpi_queue(ill_t *ill, mblk_t *mp)
13008 {
13009 	mblk_t **mpp;
13010 
13011 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
13012 
13013 	mutex_enter(&ill->ill_lock);
13014 	/* Must queue message. Tail insertion */
13015 	mpp = &ill->ill_dlpi_deferred;
13016 	while (*mpp != NULL)
13017 		mpp = &((*mpp)->b_next);
13018 
13019 	*mpp = mp;
13020 	mutex_exit(&ill->ill_lock);
13021 }
13022 
13023 /*
13024  * Send the messages that were queued. Make sure there is only
13025  * one outstanding message. ip_rput_dlpi_writer calls ill_dlpi_done()
13026  * when an ACK or a NAK is received to process the next queued message.
13027  * For IPMP we are called on the upper ill, but when send what is queued
13028  * on the cast_ill.
13029  */
13030 void
ill_dlpi_send_queued(ill_t * ill)13031 ill_dlpi_send_queued(ill_t *ill)
13032 {
13033 	mblk_t	*mp;
13034 	union DL_primitives *dlp;
13035 	t_uscalar_t prim;
13036 	ill_t *release_ill = NULL;
13037 
13038 	if (IS_IPMP(ill)) {
13039 		/* On the upper IPMP ill. */
13040 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
13041 		if (release_ill == NULL) {
13042 			/* Avoid ever sending anything down to the ipmpstub */
13043 			return;
13044 		}
13045 		ill = release_ill;
13046 	}
13047 	mutex_enter(&ill->ill_lock);
13048 	while ((mp = ill->ill_dlpi_deferred) != NULL) {
13049 		if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
13050 			/* Can't send. Somebody else will send it */
13051 			mutex_exit(&ill->ill_lock);
13052 			goto done;
13053 		}
13054 		ill->ill_dlpi_deferred = mp->b_next;
13055 		mp->b_next = NULL;
13056 		if (!ill->ill_dl_up) {
13057 			/*
13058 			 * Nobody there. All multicast addresses will be
13059 			 * re-joined when we get the DL_BIND_ACK bringing the
13060 			 * interface up.
13061 			 */
13062 			freemsg(mp);
13063 			continue;
13064 		}
13065 		dlp = (union DL_primitives *)mp->b_rptr;
13066 		prim = dlp->dl_primitive;
13067 
13068 		if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
13069 		    (prim == DL_UNBIND_REQ)) {
13070 			ill->ill_dlpi_pending = prim;
13071 		}
13072 		mutex_exit(&ill->ill_lock);
13073 
13074 		DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_send_queued",
13075 		    char *, dl_primstr(prim), ill_t *, ill);
13076 		putnext(ill->ill_wq, mp);
13077 		mutex_enter(&ill->ill_lock);
13078 	}
13079 	mutex_exit(&ill->ill_lock);
13080 done:
13081 	if (release_ill != NULL)
13082 		ill_refrele(release_ill);
13083 }
13084 
13085 /*
13086  * Queue an IP (IGMP/MLD) message to be sent by IP from
13087  * ill_mcast_send_queued
13088  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
13089  * are sent in order i.e., prevent a IGMP leave and IGMP join for the same
13090  * group to race.
13091  * We send them in order using ill_lock.
13092  * For IPMP we are called on the upper ill, but we queue on the cast_ill.
13093  */
13094 void
ill_mcast_queue(ill_t * ill,mblk_t * mp)13095 ill_mcast_queue(ill_t *ill, mblk_t *mp)
13096 {
13097 	mblk_t **mpp;
13098 	ill_t *release_ill = NULL;
13099 
13100 	ASSERT(RW_LOCK_HELD(&ill->ill_mcast_lock));
13101 
13102 	if (IS_IPMP(ill)) {
13103 		/* On the upper IPMP ill. */
13104 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
13105 		if (release_ill == NULL) {
13106 			/* Discard instead of queuing for the ipmp interface */
13107 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
13108 			ip_drop_output("ipIfStatsOutDiscards - no cast_ill",
13109 			    mp, ill);
13110 			freemsg(mp);
13111 			return;
13112 		}
13113 		ill = release_ill;
13114 	}
13115 
13116 	mutex_enter(&ill->ill_lock);
13117 	/* Must queue message. Tail insertion */
13118 	mpp = &ill->ill_mcast_deferred;
13119 	while (*mpp != NULL)
13120 		mpp = &((*mpp)->b_next);
13121 
13122 	*mpp = mp;
13123 	mutex_exit(&ill->ill_lock);
13124 	if (release_ill != NULL)
13125 		ill_refrele(release_ill);
13126 }
13127 
13128 /*
13129  * Send the IP packets that were queued by ill_mcast_queue.
13130  * These are IGMP/MLD packets.
13131  *
13132  * For IPMP we are called on the upper ill, but when send what is queued
13133  * on the cast_ill.
13134  *
13135  * Request loopback of the report if we are acting as a multicast
13136  * router, so that the process-level routing demon can hear it.
13137  * This will run multiple times for the same group if there are members
13138  * on the same group for multiple ipif's on the same ill. The
13139  * igmp_input/mld_input code will suppress this due to the loopback thus we
13140  * always loopback membership report.
13141  *
13142  * We also need to make sure that this does not get load balanced
13143  * by IPMP. We do this by passing an ill to ip_output_simple.
13144  */
13145 void
ill_mcast_send_queued(ill_t * ill)13146 ill_mcast_send_queued(ill_t *ill)
13147 {
13148 	mblk_t	*mp;
13149 	ip_xmit_attr_t ixas;
13150 	ill_t *release_ill = NULL;
13151 
13152 	if (IS_IPMP(ill)) {
13153 		/* On the upper IPMP ill. */
13154 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
13155 		if (release_ill == NULL) {
13156 			/*
13157 			 * We should have no messages on the ipmp interface
13158 			 * but no point in trying to send them.
13159 			 */
13160 			return;
13161 		}
13162 		ill = release_ill;
13163 	}
13164 	bzero(&ixas, sizeof (ixas));
13165 	ixas.ixa_zoneid = ALL_ZONES;
13166 	ixas.ixa_cred = kcred;
13167 	ixas.ixa_cpid = NOPID;
13168 	ixas.ixa_tsl = NULL;
13169 	/*
13170 	 * Here we set ixa_ifindex. If IPMP it will be the lower ill which
13171 	 * makes ip_select_route pick the IRE_MULTICAST for the cast_ill.
13172 	 * That is necessary to handle IGMP/MLD snooping switches.
13173 	 */
13174 	ixas.ixa_ifindex = ill->ill_phyint->phyint_ifindex;
13175 	ixas.ixa_ipst = ill->ill_ipst;
13176 
13177 	mutex_enter(&ill->ill_lock);
13178 	while ((mp = ill->ill_mcast_deferred) != NULL) {
13179 		ill->ill_mcast_deferred = mp->b_next;
13180 		mp->b_next = NULL;
13181 		if (!ill->ill_dl_up) {
13182 			/*
13183 			 * Nobody there. Just drop the ip packets.
13184 			 * IGMP/MLD will resend later, if this is a replumb.
13185 			 */
13186 			freemsg(mp);
13187 			continue;
13188 		}
13189 		mutex_enter(&ill->ill_phyint->phyint_lock);
13190 		if (IS_UNDER_IPMP(ill) && !ipmp_ill_is_active(ill)) {
13191 			/*
13192 			 * When the ill is getting deactivated, we only want to
13193 			 * send the DLPI messages, so drop IGMP/MLD packets.
13194 			 * DLPI messages are handled by ill_dlpi_send_queued()
13195 			 */
13196 			mutex_exit(&ill->ill_phyint->phyint_lock);
13197 			freemsg(mp);
13198 			continue;
13199 		}
13200 		mutex_exit(&ill->ill_phyint->phyint_lock);
13201 		mutex_exit(&ill->ill_lock);
13202 
13203 		/* Check whether we are sending IPv4 or IPv6. */
13204 		if (ill->ill_isv6) {
13205 			ip6_t  *ip6h = (ip6_t *)mp->b_rptr;
13206 
13207 			ixas.ixa_multicast_ttl = ip6h->ip6_hops;
13208 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V6;
13209 		} else {
13210 			ipha_t *ipha = (ipha_t *)mp->b_rptr;
13211 
13212 			ixas.ixa_multicast_ttl = ipha->ipha_ttl;
13213 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
13214 			ixas.ixa_flags &= ~IXAF_SET_ULP_CKSUM;
13215 		}
13216 		ixas.ixa_flags &= ~IXAF_VERIFY_SOURCE;
13217 		ixas.ixa_flags |= IXAF_MULTICAST_LOOP | IXAF_SET_SOURCE;
13218 		(void) ip_output_simple(mp, &ixas);
13219 		ixa_cleanup(&ixas);
13220 
13221 		mutex_enter(&ill->ill_lock);
13222 	}
13223 	mutex_exit(&ill->ill_lock);
13224 
13225 done:
13226 	if (release_ill != NULL)
13227 		ill_refrele(release_ill);
13228 }
13229 
13230 /*
13231  * Take down a specific interface, but don't lose any information about it.
13232  * (Always called as writer.)
13233  * This function goes through the down sequence even if the interface is
13234  * already down. There are 2 reasons.
13235  * a. Currently we permit interface routes that depend on down interfaces
13236  *    to be added. This behaviour itself is questionable. However it appears
13237  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
13238  *    time. We go thru the cleanup in order to remove these routes.
13239  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
13240  *    DL_ERROR_ACK in response to the DL_BIND request. The interface is
13241  *    down, but we need to cleanup i.e. do ill_dl_down and
13242  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
13243  *
13244  * IP-MT notes:
13245  *
13246  * Model of reference to interfaces.
13247  *
13248  * The following members in ipif_t track references to the ipif.
13249  *	int     ipif_refcnt;    Active reference count
13250  *
13251  * The following members in ill_t track references to the ill.
13252  *	int             ill_refcnt;     active refcnt
13253  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
13254  *	uint_t          ill_ncec_cnt;	Number of ncecs referencing ill
13255  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
13256  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
13257  *
13258  * Reference to an ipif or ill can be obtained in any of the following ways.
13259  *
13260  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
13261  * Pointers to ipif / ill from other data structures viz ire and conn.
13262  * Implicit reference to the ipif / ill by holding a reference to the ire.
13263  *
13264  * The ipif/ill lookup functions return a reference held ipif / ill.
13265  * ipif_refcnt and ill_refcnt track the reference counts respectively.
13266  * This is a purely dynamic reference count associated with threads holding
13267  * references to the ipif / ill. Pointers from other structures do not
13268  * count towards this reference count.
13269  *
13270  * ill_ire_cnt is the number of ire's associated with the
13271  * ill. This is incremented whenever a new ire is created referencing the
13272  * ill. This is done atomically inside ire_add_v[46] where the ire is
13273  * actually added to the ire hash table. The count is decremented in
13274  * ire_inactive where the ire is destroyed.
13275  *
13276  * ill_ncec_cnt is the number of ncec's referencing the ill thru ncec_ill.
13277  * This is incremented atomically in
13278  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
13279  * table. Similarly it is decremented in ncec_inactive() where the ncec
13280  * is destroyed.
13281  *
13282  * ill_nce_cnt is the number of nce's referencing the ill thru nce_ill. This is
13283  * incremented atomically in nce_add() where the nce is actually added to the
13284  * ill_nce. Similarly it is decremented in nce_inactive() where the nce
13285  * is destroyed.
13286  *
13287  * ill_ilm_cnt is the ilm's reference to the ill. It is incremented in
13288  * ilm_add() and decremented before the ilm is freed in ilm_delete().
13289  *
13290  * Flow of ioctls involving interface down/up
13291  *
13292  * The following is the sequence of an attempt to set some critical flags on an
13293  * up interface.
13294  * ip_sioctl_flags
13295  * ipif_down
13296  * wait for ipif to be quiescent
13297  * ipif_down_tail
13298  * ip_sioctl_flags_tail
13299  *
13300  * All set ioctls that involve down/up sequence would have a skeleton similar
13301  * to the above. All the *tail functions are called after the refcounts have
13302  * dropped to the appropriate values.
13303  *
13304  * SIOC ioctls during the IPIF_CHANGING interval.
13305  *
13306  * Threads handling SIOC set ioctls serialize on the squeue, but this
13307  * is not done for SIOC get ioctls. Since a set ioctl can cause several
13308  * steps of internal changes to the state, some of which are visible in
13309  * ipif_flags (such as IFF_UP being cleared and later set), and we want
13310  * the set ioctl to be atomic related to the get ioctls, the SIOC get code
13311  * will wait and restart ioctls if IPIF_CHANGING is set. The mblk is then
13312  * enqueued in the ipsq and the operation is restarted by ipsq_exit() when
13313  * the current exclusive operation completes. The IPIF_CHANGING check
13314  * and enqueue is atomic using the ill_lock and ipsq_lock. The
13315  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
13316  * change while the ill_lock is held. Before dropping the ill_lock we acquire
13317  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
13318  * until we release the ipsq_lock, even though the ill/ipif state flags
13319  * can change after we drop the ill_lock.
13320  */
13321 int
ipif_down(ipif_t * ipif,queue_t * q,mblk_t * mp)13322 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
13323 {
13324 	ill_t		*ill = ipif->ipif_ill;
13325 	conn_t		*connp;
13326 	boolean_t	success;
13327 	boolean_t	ipif_was_up = B_FALSE;
13328 	ip_stack_t	*ipst = ill->ill_ipst;
13329 
13330 	ASSERT(IAM_WRITER_IPIF(ipif));
13331 
13332 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13333 
13334 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down",
13335 	    ill_t *, ill, ipif_t *, ipif);
13336 
13337 	if (ipif->ipif_flags & IPIF_UP) {
13338 		mutex_enter(&ill->ill_lock);
13339 		ipif->ipif_flags &= ~IPIF_UP;
13340 		ASSERT(ill->ill_ipif_up_count > 0);
13341 		--ill->ill_ipif_up_count;
13342 		mutex_exit(&ill->ill_lock);
13343 		ipif_was_up = B_TRUE;
13344 		/* Update status in SCTP's list */
13345 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
13346 		ill_nic_event_dispatch(ipif->ipif_ill,
13347 		    MAP_IPIF_ID(ipif->ipif_id), NE_LIF_DOWN, NULL, 0);
13348 	}
13349 
13350 	/*
13351 	 * Removal of the last ipif from an ill may result in a DL_UNBIND
13352 	 * being sent to the driver, and we must not send any data packets to
13353 	 * the driver after the DL_UNBIND_REQ. To ensure this, all the
13354 	 * ire and nce entries used in the data path will be cleaned
13355 	 * up, and we also set  the ILL_DOWN_IN_PROGRESS bit to make
13356 	 * sure on new entries will be added until the ill is bound
13357 	 * again. The ILL_DOWN_IN_PROGRESS bit is turned off upon
13358 	 * receipt of a DL_BIND_ACK.
13359 	 */
13360 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
13361 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
13362 	    ill->ill_dl_up) {
13363 		ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
13364 	}
13365 
13366 	/*
13367 	 * Blow away memberships we established in ipif_multicast_up().
13368 	 */
13369 	ipif_multicast_down(ipif);
13370 
13371 	/*
13372 	 * Remove from the mapping for __sin6_src_id. We insert only
13373 	 * when the address is not INADDR_ANY. As IPv4 addresses are
13374 	 * stored as mapped addresses, we need to check for mapped
13375 	 * INADDR_ANY also.
13376 	 */
13377 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
13378 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
13379 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
13380 		int err;
13381 
13382 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
13383 		    ipif->ipif_zoneid, ipst);
13384 		if (err != 0) {
13385 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
13386 		}
13387 	}
13388 
13389 	if (ipif_was_up) {
13390 		/* only delete if we'd added ire's before */
13391 		if (ipif->ipif_isv6)
13392 			ipif_delete_ires_v6(ipif);
13393 		else
13394 			ipif_delete_ires_v4(ipif);
13395 	}
13396 
13397 	if (ipif_was_up && ill->ill_ipif_up_count == 0) {
13398 		/*
13399 		 * Since the interface is now down, it may have just become
13400 		 * inactive.  Note that this needs to be done even for a
13401 		 * lll_logical_down(), or ARP entries will not get correctly
13402 		 * restored when the interface comes back up.
13403 		 */
13404 		if (IS_UNDER_IPMP(ill))
13405 			ipmp_ill_refresh_active(ill);
13406 	}
13407 
13408 	/*
13409 	 * neighbor-discovery or arp entries for this interface. The ipif
13410 	 * has to be quiesced, so we walk all the nce's and delete those
13411 	 * that point at the ipif->ipif_ill. At the same time, we also
13412 	 * update IPMP so that ipifs for data addresses are unbound. We dont
13413 	 * call ipif_arp_down to DL_UNBIND the arp stream itself here, but defer
13414 	 * that for ipif_down_tail()
13415 	 */
13416 	ipif_nce_down(ipif);
13417 
13418 	/*
13419 	 * If this is the last ipif on the ill, we also need to remove
13420 	 * any IREs with ire_ill set. Otherwise ipif_is_quiescent() will
13421 	 * never succeed.
13422 	 */
13423 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0)
13424 		ire_walk_ill(0, 0, ill_downi, ill, ill);
13425 
13426 	/*
13427 	 * Walk all CONNs that can have a reference on an ire for this
13428 	 * ipif (we actually walk all that now have stale references).
13429 	 */
13430 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
13431 
13432 	/*
13433 	 * If mp is NULL the caller will wait for the appropriate refcnt.
13434 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
13435 	 * and ill_delete -> ipif_free -> ipif_down
13436 	 */
13437 	if (mp == NULL) {
13438 		ASSERT(q == NULL);
13439 		return (0);
13440 	}
13441 
13442 	if (CONN_Q(q)) {
13443 		connp = Q_TO_CONN(q);
13444 		mutex_enter(&connp->conn_lock);
13445 	} else {
13446 		connp = NULL;
13447 	}
13448 	mutex_enter(&ill->ill_lock);
13449 	/*
13450 	 * Are there any ire's pointing to this ipif that are still active ?
13451 	 * If this is the last ipif going down, are there any ire's pointing
13452 	 * to this ill that are still active ?
13453 	 */
13454 	if (ipif_is_quiescent(ipif)) {
13455 		mutex_exit(&ill->ill_lock);
13456 		if (connp != NULL)
13457 			mutex_exit(&connp->conn_lock);
13458 		return (0);
13459 	}
13460 
13461 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
13462 	    ill->ill_name, (void *)ill));
13463 	/*
13464 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
13465 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
13466 	 * which in turn is called by the last refrele on the ipif/ill/ire.
13467 	 */
13468 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
13469 	if (!success) {
13470 		/* The conn is closing. So just return */
13471 		ASSERT(connp != NULL);
13472 		mutex_exit(&ill->ill_lock);
13473 		mutex_exit(&connp->conn_lock);
13474 		return (EINTR);
13475 	}
13476 
13477 	mutex_exit(&ill->ill_lock);
13478 	if (connp != NULL)
13479 		mutex_exit(&connp->conn_lock);
13480 	return (EINPROGRESS);
13481 }
13482 
13483 int
ipif_down_tail(ipif_t * ipif)13484 ipif_down_tail(ipif_t *ipif)
13485 {
13486 	ill_t	*ill = ipif->ipif_ill;
13487 	int	err = 0;
13488 
13489 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down_tail",
13490 	    ill_t *, ill, ipif_t *, ipif);
13491 
13492 	/*
13493 	 * Skip any loopback interface (null wq).
13494 	 * If this is the last logical interface on the ill
13495 	 * have ill_dl_down tell the driver we are gone (unbind)
13496 	 * Note that lun 0 can ipif_down even though
13497 	 * there are other logical units that are up.
13498 	 * This occurs e.g. when we change a "significant" IFF_ flag.
13499 	 */
13500 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
13501 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
13502 	    ill->ill_dl_up) {
13503 		ill_dl_down(ill);
13504 	}
13505 	if (!ipif->ipif_isv6)
13506 		err = ipif_arp_down(ipif);
13507 
13508 	ill->ill_logical_down = 0;
13509 
13510 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
13511 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif, RTSQ_DEFAULT);
13512 	return (err);
13513 }
13514 
13515 /*
13516  * Bring interface logically down without bringing the physical interface
13517  * down e.g. when the netmask is changed. This avoids long lasting link
13518  * negotiations between an ethernet interface and a certain switches.
13519  */
13520 static int
ipif_logical_down(ipif_t * ipif,queue_t * q,mblk_t * mp)13521 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
13522 {
13523 	DTRACE_PROBE3(ipif__downup, char *, "ipif_logical_down",
13524 	    ill_t *, ipif->ipif_ill, ipif_t *, ipif);
13525 
13526 	/*
13527 	 * The ill_logical_down flag is a transient flag. It is set here
13528 	 * and is cleared once the down has completed in ipif_down_tail.
13529 	 * This flag does not indicate whether the ill stream is in the
13530 	 * DL_BOUND state with the driver. Instead this flag is used by
13531 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
13532 	 * the driver. The state of the ill stream i.e. whether it is
13533 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
13534 	 */
13535 	ipif->ipif_ill->ill_logical_down = 1;
13536 	return (ipif_down(ipif, q, mp));
13537 }
13538 
13539 /*
13540  * Initiate deallocate of an IPIF. Always called as writer. Called by
13541  * ill_delete or ip_sioctl_removeif.
13542  */
13543 static void
ipif_free(ipif_t * ipif)13544 ipif_free(ipif_t *ipif)
13545 {
13546 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13547 
13548 	ASSERT(IAM_WRITER_IPIF(ipif));
13549 
13550 	if (ipif->ipif_recovery_id != 0)
13551 		(void) untimeout(ipif->ipif_recovery_id);
13552 	ipif->ipif_recovery_id = 0;
13553 
13554 	/*
13555 	 * Take down the interface. We can be called either from ill_delete
13556 	 * or from ip_sioctl_removeif.
13557 	 */
13558 	(void) ipif_down(ipif, NULL, NULL);
13559 
13560 	/*
13561 	 * Now that the interface is down, there's no chance it can still
13562 	 * become a duplicate.  Cancel any timer that may have been set while
13563 	 * tearing down.
13564 	 */
13565 	if (ipif->ipif_recovery_id != 0)
13566 		(void) untimeout(ipif->ipif_recovery_id);
13567 	ipif->ipif_recovery_id = 0;
13568 
13569 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13570 	/* Remove pointers to this ill in the multicast routing tables */
13571 	reset_mrt_vif_ipif(ipif);
13572 	/* If necessary, clear the cached source ipif rotor. */
13573 	if (ipif->ipif_ill->ill_src_ipif == ipif)
13574 		ipif->ipif_ill->ill_src_ipif = NULL;
13575 	rw_exit(&ipst->ips_ill_g_lock);
13576 }
13577 
13578 static void
ipif_free_tail(ipif_t * ipif)13579 ipif_free_tail(ipif_t *ipif)
13580 {
13581 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
13582 
13583 	/*
13584 	 * Need to hold both ill_g_lock and ill_lock while
13585 	 * inserting or removing an ipif from the linked list
13586 	 * of ipifs hanging off the ill.
13587 	 */
13588 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13589 
13590 #ifdef DEBUG
13591 	ipif_trace_cleanup(ipif);
13592 #endif
13593 
13594 	/* Ask SCTP to take it out of it list */
13595 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
13596 	ip_rts_newaddrmsg(RTM_FREEADDR, 0, ipif, RTSQ_DEFAULT);
13597 
13598 	/* Get it out of the ILL interface list. */
13599 	ipif_remove(ipif);
13600 	rw_exit(&ipst->ips_ill_g_lock);
13601 
13602 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
13603 	ASSERT(ipif->ipif_recovery_id == 0);
13604 	ASSERT(ipif->ipif_ire_local == NULL);
13605 	ASSERT(ipif->ipif_ire_if == NULL);
13606 
13607 	/* Free the memory. */
13608 	mi_free(ipif);
13609 }
13610 
13611 /*
13612  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
13613  * is zero.
13614  */
13615 void
ipif_get_name(const ipif_t * ipif,char * buf,int len)13616 ipif_get_name(const ipif_t *ipif, char *buf, int len)
13617 {
13618 	char	lbuf[LIFNAMSIZ];
13619 	char	*name;
13620 	size_t	name_len;
13621 
13622 	buf[0] = '\0';
13623 	name = ipif->ipif_ill->ill_name;
13624 	name_len = ipif->ipif_ill->ill_name_length;
13625 	if (ipif->ipif_id != 0) {
13626 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
13627 		    ipif->ipif_id);
13628 		name = lbuf;
13629 		name_len = mi_strlen(name) + 1;
13630 	}
13631 	len -= 1;
13632 	buf[len] = '\0';
13633 	len = MIN(len, name_len);
13634 	bcopy(name, buf, len);
13635 }
13636 
13637 /*
13638  * Sets `buf' to an ill name.
13639  */
13640 void
ill_get_name(const ill_t * ill,char * buf,int len)13641 ill_get_name(const ill_t *ill, char *buf, int len)
13642 {
13643 	char	*name;
13644 	size_t	name_len;
13645 
13646 	name = ill->ill_name;
13647 	name_len = ill->ill_name_length;
13648 	len -= 1;
13649 	buf[len] = '\0';
13650 	len = MIN(len, name_len);
13651 	bcopy(name, buf, len);
13652 }
13653 
13654 /*
13655  * Find an IPIF based on the name passed in.  Names can be of the form <phys>
13656  * (e.g., le0) or <phys>:<#> (e.g., le0:1).  When there is no colon, the
13657  * implied unit id is zero. <phys> must correspond to the name of an ILL.
13658  * (May be called as writer.)
13659  */
13660 static ipif_t *
ipif_lookup_on_name(char * name,size_t namelen,boolean_t do_alloc,boolean_t * exists,boolean_t isv6,zoneid_t zoneid,ip_stack_t * ipst)13661 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
13662     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, ip_stack_t *ipst)
13663 {
13664 	char	*cp;
13665 	char	*endp;
13666 	long	id;
13667 	ill_t	*ill;
13668 	ipif_t	*ipif;
13669 	uint_t	ire_type;
13670 	boolean_t did_alloc = B_FALSE;
13671 	char	last;
13672 
13673 	/*
13674 	 * If the caller wants to us to create the ipif, make sure we have a
13675 	 * valid zoneid
13676 	 */
13677 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
13678 
13679 	if (namelen == 0) {
13680 		return (NULL);
13681 	}
13682 
13683 	*exists = B_FALSE;
13684 	/* Look for a colon in the name. */
13685 	endp = &name[namelen];
13686 	for (cp = endp; --cp > name; ) {
13687 		if (*cp == IPIF_SEPARATOR_CHAR)
13688 			break;
13689 	}
13690 
13691 	if (*cp == IPIF_SEPARATOR_CHAR) {
13692 		/*
13693 		 * Reject any non-decimal aliases for logical
13694 		 * interfaces. Aliases with leading zeroes
13695 		 * are also rejected as they introduce ambiguity
13696 		 * in the naming of the interfaces.
13697 		 * In order to confirm with existing semantics,
13698 		 * and to not break any programs/script relying
13699 		 * on that behaviour, if<0>:0 is considered to be
13700 		 * a valid interface.
13701 		 *
13702 		 * If alias has two or more digits and the first
13703 		 * is zero, fail.
13704 		 */
13705 		if (&cp[2] < endp && cp[1] == '0') {
13706 			return (NULL);
13707 		}
13708 	}
13709 
13710 	if (cp <= name) {
13711 		cp = endp;
13712 	}
13713 	last = *cp;
13714 	*cp = '\0';
13715 
13716 	/*
13717 	 * Look up the ILL, based on the portion of the name
13718 	 * before the slash. ill_lookup_on_name returns a held ill.
13719 	 * Temporary to check whether ill exists already. If so
13720 	 * ill_lookup_on_name will clear it.
13721 	 */
13722 	ill = ill_lookup_on_name(name, do_alloc, isv6,
13723 	    &did_alloc, ipst);
13724 	*cp = last;
13725 	if (ill == NULL)
13726 		return (NULL);
13727 
13728 	/* Establish the unit number in the name. */
13729 	id = 0;
13730 	if (cp < endp && *endp == '\0') {
13731 		/* If there was a colon, the unit number follows. */
13732 		cp++;
13733 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
13734 			ill_refrele(ill);
13735 			return (NULL);
13736 		}
13737 	}
13738 
13739 	mutex_enter(&ill->ill_lock);
13740 	/* Now see if there is an IPIF with this unit number. */
13741 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13742 		if (ipif->ipif_id == id) {
13743 			if (zoneid != ALL_ZONES &&
13744 			    zoneid != ipif->ipif_zoneid &&
13745 			    ipif->ipif_zoneid != ALL_ZONES) {
13746 				mutex_exit(&ill->ill_lock);
13747 				ill_refrele(ill);
13748 				return (NULL);
13749 			}
13750 			if (IPIF_CAN_LOOKUP(ipif)) {
13751 				ipif_refhold_locked(ipif);
13752 				mutex_exit(&ill->ill_lock);
13753 				if (!did_alloc)
13754 					*exists = B_TRUE;
13755 				/*
13756 				 * Drop locks before calling ill_refrele
13757 				 * since it can potentially call into
13758 				 * ipif_ill_refrele_tail which can end up
13759 				 * in trying to acquire any lock.
13760 				 */
13761 				ill_refrele(ill);
13762 				return (ipif);
13763 			}
13764 		}
13765 	}
13766 
13767 	if (!do_alloc) {
13768 		mutex_exit(&ill->ill_lock);
13769 		ill_refrele(ill);
13770 		return (NULL);
13771 	}
13772 
13773 	/*
13774 	 * If none found, atomically allocate and return a new one.
13775 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
13776 	 * to support "receive only" use of lo0:1 etc. as is still done
13777 	 * below as an initial guess.
13778 	 * However, this is now likely to be overriden later in ipif_up_done()
13779 	 * when we know for sure what address has been configured on the
13780 	 * interface, since we might have more than one loopback interface
13781 	 * with a loopback address, e.g. in the case of zones, and all the
13782 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
13783 	 */
13784 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
13785 		ire_type = IRE_LOOPBACK;
13786 	else
13787 		ire_type = IRE_LOCAL;
13788 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE, B_TRUE, NULL);
13789 	if (ipif != NULL)
13790 		ipif_refhold_locked(ipif);
13791 	mutex_exit(&ill->ill_lock);
13792 	ill_refrele(ill);
13793 	return (ipif);
13794 }
13795 
13796 /*
13797  * Variant of the above that queues the request on the ipsq when
13798  * IPIF_CHANGING is set.
13799  */
13800 static ipif_t *
ipif_lookup_on_name_async(char * name,size_t namelen,boolean_t isv6,zoneid_t zoneid,queue_t * q,mblk_t * mp,ipsq_func_t func,int * error,ip_stack_t * ipst)13801 ipif_lookup_on_name_async(char *name, size_t namelen, boolean_t isv6,
13802     zoneid_t zoneid, queue_t *q, mblk_t *mp, ipsq_func_t func, int *error,
13803     ip_stack_t *ipst)
13804 {
13805 	char	*cp;
13806 	char	*endp;
13807 	long	id;
13808 	ill_t	*ill;
13809 	ipif_t	*ipif;
13810 	boolean_t did_alloc = B_FALSE;
13811 	ipsq_t	*ipsq;
13812 
13813 	if (error != NULL)
13814 		*error = 0;
13815 
13816 	if (namelen == 0) {
13817 		if (error != NULL)
13818 			*error = ENXIO;
13819 		return (NULL);
13820 	}
13821 
13822 	/* Look for a colon in the name. */
13823 	endp = &name[namelen];
13824 	for (cp = endp; --cp > name; ) {
13825 		if (*cp == IPIF_SEPARATOR_CHAR)
13826 			break;
13827 	}
13828 
13829 	if (*cp == IPIF_SEPARATOR_CHAR) {
13830 		/*
13831 		 * Reject any non-decimal aliases for logical
13832 		 * interfaces. Aliases with leading zeroes
13833 		 * are also rejected as they introduce ambiguity
13834 		 * in the naming of the interfaces.
13835 		 * In order to confirm with existing semantics,
13836 		 * and to not break any programs/script relying
13837 		 * on that behaviour, if<0>:0 is considered to be
13838 		 * a valid interface.
13839 		 *
13840 		 * If alias has two or more digits and the first
13841 		 * is zero, fail.
13842 		 */
13843 		if (&cp[2] < endp && cp[1] == '0') {
13844 			if (error != NULL)
13845 				*error = EINVAL;
13846 			return (NULL);
13847 		}
13848 	}
13849 
13850 	if (cp <= name) {
13851 		cp = endp;
13852 	} else {
13853 		*cp = '\0';
13854 	}
13855 
13856 	/*
13857 	 * Look up the ILL, based on the portion of the name
13858 	 * before the slash. ill_lookup_on_name returns a held ill.
13859 	 * Temporary to check whether ill exists already. If so
13860 	 * ill_lookup_on_name will clear it.
13861 	 */
13862 	ill = ill_lookup_on_name(name, B_FALSE, isv6, &did_alloc, ipst);
13863 	if (cp != endp)
13864 		*cp = IPIF_SEPARATOR_CHAR;
13865 	if (ill == NULL)
13866 		return (NULL);
13867 
13868 	/* Establish the unit number in the name. */
13869 	id = 0;
13870 	if (cp < endp && *endp == '\0') {
13871 		/* If there was a colon, the unit number follows. */
13872 		cp++;
13873 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
13874 			ill_refrele(ill);
13875 			if (error != NULL)
13876 				*error = ENXIO;
13877 			return (NULL);
13878 		}
13879 	}
13880 
13881 	GRAB_CONN_LOCK(q);
13882 	mutex_enter(&ill->ill_lock);
13883 	/* Now see if there is an IPIF with this unit number. */
13884 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13885 		if (ipif->ipif_id == id) {
13886 			if (zoneid != ALL_ZONES &&
13887 			    zoneid != ipif->ipif_zoneid &&
13888 			    ipif->ipif_zoneid != ALL_ZONES) {
13889 				mutex_exit(&ill->ill_lock);
13890 				RELEASE_CONN_LOCK(q);
13891 				ill_refrele(ill);
13892 				if (error != NULL)
13893 					*error = ENXIO;
13894 				return (NULL);
13895 			}
13896 
13897 			if (!(IPIF_IS_CHANGING(ipif) ||
13898 			    IPIF_IS_CONDEMNED(ipif)) ||
13899 			    IAM_WRITER_IPIF(ipif)) {
13900 				ipif_refhold_locked(ipif);
13901 				mutex_exit(&ill->ill_lock);
13902 				/*
13903 				 * Drop locks before calling ill_refrele
13904 				 * since it can potentially call into
13905 				 * ipif_ill_refrele_tail which can end up
13906 				 * in trying to acquire any lock.
13907 				 */
13908 				RELEASE_CONN_LOCK(q);
13909 				ill_refrele(ill);
13910 				return (ipif);
13911 			} else if (q != NULL && !IPIF_IS_CONDEMNED(ipif)) {
13912 				ipsq = ill->ill_phyint->phyint_ipsq;
13913 				mutex_enter(&ipsq->ipsq_lock);
13914 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
13915 				mutex_exit(&ill->ill_lock);
13916 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
13917 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
13918 				mutex_exit(&ipsq->ipsq_lock);
13919 				RELEASE_CONN_LOCK(q);
13920 				ill_refrele(ill);
13921 				if (error != NULL)
13922 					*error = EINPROGRESS;
13923 				return (NULL);
13924 			}
13925 		}
13926 	}
13927 	RELEASE_CONN_LOCK(q);
13928 	mutex_exit(&ill->ill_lock);
13929 	ill_refrele(ill);
13930 	if (error != NULL)
13931 		*error = ENXIO;
13932 	return (NULL);
13933 }
13934 
13935 /*
13936  * This routine is called whenever a new address comes up on an ipif.  If
13937  * we are configured to respond to address mask requests, then we are supposed
13938  * to broadcast an address mask reply at this time.  This routine is also
13939  * called if we are already up, but a netmask change is made.  This is legal
13940  * but might not make the system manager very popular.	(May be called
13941  * as writer.)
13942  */
13943 void
ipif_mask_reply(ipif_t * ipif)13944 ipif_mask_reply(ipif_t *ipif)
13945 {
13946 	icmph_t	*icmph;
13947 	ipha_t	*ipha;
13948 	mblk_t	*mp;
13949 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13950 	ip_xmit_attr_t ixas;
13951 
13952 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
13953 
13954 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
13955 		return;
13956 
13957 	/* ICMP mask reply is IPv4 only */
13958 	ASSERT(!ipif->ipif_isv6);
13959 	/* ICMP mask reply is not for a loopback interface */
13960 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
13961 
13962 	if (ipif->ipif_lcl_addr == INADDR_ANY)
13963 		return;
13964 
13965 	mp = allocb(REPLY_LEN, BPRI_HI);
13966 	if (mp == NULL)
13967 		return;
13968 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
13969 
13970 	ipha = (ipha_t *)mp->b_rptr;
13971 	bzero(ipha, REPLY_LEN);
13972 	*ipha = icmp_ipha;
13973 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
13974 	ipha->ipha_src = ipif->ipif_lcl_addr;
13975 	ipha->ipha_dst = ipif->ipif_brd_addr;
13976 	ipha->ipha_length = htons(REPLY_LEN);
13977 	ipha->ipha_ident = 0;
13978 
13979 	icmph = (icmph_t *)&ipha[1];
13980 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
13981 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
13982 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
13983 
13984 	bzero(&ixas, sizeof (ixas));
13985 	ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
13986 	ixas.ixa_zoneid = ALL_ZONES;
13987 	ixas.ixa_ifindex = 0;
13988 	ixas.ixa_ipst = ipst;
13989 	ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
13990 	(void) ip_output_simple(mp, &ixas);
13991 	ixa_cleanup(&ixas);
13992 #undef	REPLY_LEN
13993 }
13994 
13995 /*
13996  * Join the ipif specific multicast groups.
13997  * Must be called after a mapping has been set up in the resolver.  (Always
13998  * called as writer.)
13999  */
14000 void
ipif_multicast_up(ipif_t * ipif)14001 ipif_multicast_up(ipif_t *ipif)
14002 {
14003 	int err;
14004 	ill_t *ill;
14005 	ilm_t *ilm;
14006 
14007 	ASSERT(IAM_WRITER_IPIF(ipif));
14008 
14009 	ill = ipif->ipif_ill;
14010 
14011 	ip1dbg(("ipif_multicast_up\n"));
14012 	if (!(ill->ill_flags & ILLF_MULTICAST) ||
14013 	    ipif->ipif_allhosts_ilm != NULL)
14014 		return;
14015 
14016 	if (ipif->ipif_isv6) {
14017 		in6_addr_t v6allmc = ipv6_all_hosts_mcast;
14018 		in6_addr_t v6solmc = ipv6_solicited_node_mcast;
14019 
14020 		v6solmc.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
14021 
14022 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
14023 			return;
14024 
14025 		ip1dbg(("ipif_multicast_up - addmulti\n"));
14026 
14027 		/*
14028 		 * Join the all hosts multicast address.  We skip this for
14029 		 * underlying IPMP interfaces since they should be invisible.
14030 		 */
14031 		if (!IS_UNDER_IPMP(ill)) {
14032 			ilm = ip_addmulti(&v6allmc, ill, ipif->ipif_zoneid,
14033 			    &err);
14034 			if (ilm == NULL) {
14035 				ASSERT(err != 0);
14036 				ip0dbg(("ipif_multicast_up: "
14037 				    "all_hosts_mcast failed %d\n", err));
14038 				return;
14039 			}
14040 			ipif->ipif_allhosts_ilm = ilm;
14041 		}
14042 
14043 		/*
14044 		 * Enable multicast for the solicited node multicast address.
14045 		 * If IPMP we need to put the membership on the upper ill.
14046 		 */
14047 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
14048 			ill_t *mcast_ill = NULL;
14049 			boolean_t need_refrele;
14050 
14051 			if (IS_UNDER_IPMP(ill) &&
14052 			    (mcast_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL) {
14053 				need_refrele = B_TRUE;
14054 			} else {
14055 				mcast_ill = ill;
14056 				need_refrele = B_FALSE;
14057 			}
14058 
14059 			ilm = ip_addmulti(&v6solmc, mcast_ill,
14060 			    ipif->ipif_zoneid, &err);
14061 			if (need_refrele)
14062 				ill_refrele(mcast_ill);
14063 
14064 			if (ilm == NULL) {
14065 				ASSERT(err != 0);
14066 				ip0dbg(("ipif_multicast_up: solicited MC"
14067 				    " failed %d\n", err));
14068 				if ((ilm = ipif->ipif_allhosts_ilm) != NULL) {
14069 					ipif->ipif_allhosts_ilm = NULL;
14070 					(void) ip_delmulti(ilm);
14071 				}
14072 				return;
14073 			}
14074 			ipif->ipif_solmulti_ilm = ilm;
14075 		}
14076 	} else {
14077 		in6_addr_t v6group;
14078 
14079 		if (ipif->ipif_lcl_addr == INADDR_ANY || IS_UNDER_IPMP(ill))
14080 			return;
14081 
14082 		/* Join the all hosts multicast address */
14083 		ip1dbg(("ipif_multicast_up - addmulti\n"));
14084 		IN6_IPADDR_TO_V4MAPPED(htonl(INADDR_ALLHOSTS_GROUP), &v6group);
14085 
14086 		ilm = ip_addmulti(&v6group, ill, ipif->ipif_zoneid, &err);
14087 		if (ilm == NULL) {
14088 			ASSERT(err != 0);
14089 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
14090 			return;
14091 		}
14092 		ipif->ipif_allhosts_ilm = ilm;
14093 	}
14094 }
14095 
14096 /*
14097  * Blow away any multicast groups that we joined in ipif_multicast_up().
14098  * (ilms from explicit memberships are handled in conn_update_ill.)
14099  */
14100 void
ipif_multicast_down(ipif_t * ipif)14101 ipif_multicast_down(ipif_t *ipif)
14102 {
14103 	ASSERT(IAM_WRITER_IPIF(ipif));
14104 
14105 	ip1dbg(("ipif_multicast_down\n"));
14106 
14107 	if (ipif->ipif_allhosts_ilm != NULL) {
14108 		(void) ip_delmulti(ipif->ipif_allhosts_ilm);
14109 		ipif->ipif_allhosts_ilm = NULL;
14110 	}
14111 	if (ipif->ipif_solmulti_ilm != NULL) {
14112 		(void) ip_delmulti(ipif->ipif_solmulti_ilm);
14113 		ipif->ipif_solmulti_ilm = NULL;
14114 	}
14115 }
14116 
14117 /*
14118  * Used when an interface comes up to recreate any extra routes on this
14119  * interface.
14120  */
14121 int
ill_recover_saved_ire(ill_t * ill)14122 ill_recover_saved_ire(ill_t *ill)
14123 {
14124 	mblk_t		*mp;
14125 	ip_stack_t	*ipst = ill->ill_ipst;
14126 
14127 	ip1dbg(("ill_recover_saved_ire(%s)", ill->ill_name));
14128 
14129 	mutex_enter(&ill->ill_saved_ire_lock);
14130 	for (mp = ill->ill_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
14131 		ire_t		*ire, *nire;
14132 		ifrt_t		*ifrt;
14133 
14134 		ifrt = (ifrt_t *)mp->b_rptr;
14135 		/*
14136 		 * Create a copy of the IRE with the saved address and netmask.
14137 		 */
14138 		if (ill->ill_isv6) {
14139 			ire = ire_create_v6(
14140 			    &ifrt->ifrt_v6addr,
14141 			    &ifrt->ifrt_v6mask,
14142 			    &ifrt->ifrt_v6gateway_addr,
14143 			    ifrt->ifrt_type,
14144 			    ill,
14145 			    ifrt->ifrt_zoneid,
14146 			    ifrt->ifrt_flags,
14147 			    NULL,
14148 			    ipst);
14149 		} else {
14150 			ire = ire_create(
14151 			    (uint8_t *)&ifrt->ifrt_addr,
14152 			    (uint8_t *)&ifrt->ifrt_mask,
14153 			    (uint8_t *)&ifrt->ifrt_gateway_addr,
14154 			    ifrt->ifrt_type,
14155 			    ill,
14156 			    ifrt->ifrt_zoneid,
14157 			    ifrt->ifrt_flags,
14158 			    NULL,
14159 			    ipst);
14160 		}
14161 		if (ire == NULL) {
14162 			mutex_exit(&ill->ill_saved_ire_lock);
14163 			return (ENOMEM);
14164 		}
14165 
14166 		if (ifrt->ifrt_flags & RTF_SETSRC) {
14167 			if (ill->ill_isv6) {
14168 				ire->ire_setsrc_addr_v6 =
14169 				    ifrt->ifrt_v6setsrc_addr;
14170 			} else {
14171 				ire->ire_setsrc_addr = ifrt->ifrt_setsrc_addr;
14172 			}
14173 		}
14174 
14175 		/*
14176 		 * Some software (for example, GateD and Sun Cluster) attempts
14177 		 * to create (what amount to) IRE_PREFIX routes with the
14178 		 * loopback address as the gateway.  This is primarily done to
14179 		 * set up prefixes with the RTF_REJECT flag set (for example,
14180 		 * when generating aggregate routes.)
14181 		 *
14182 		 * If the IRE type (as defined by ill->ill_net_type) is
14183 		 * IRE_LOOPBACK, then we map the request into a
14184 		 * IRE_IF_NORESOLVER.
14185 		 */
14186 		if (ill->ill_net_type == IRE_LOOPBACK)
14187 			ire->ire_type = IRE_IF_NORESOLVER;
14188 
14189 		/*
14190 		 * ire held by ire_add, will be refreled' towards the
14191 		 * the end of ipif_up_done
14192 		 */
14193 		nire = ire_add(ire);
14194 		/*
14195 		 * Check if it was a duplicate entry. This handles
14196 		 * the case of two racing route adds for the same route
14197 		 */
14198 		if (nire == NULL) {
14199 			ip1dbg(("ill_recover_saved_ire: FAILED\n"));
14200 		} else if (nire != ire) {
14201 			ip1dbg(("ill_recover_saved_ire: duplicate ire %p\n",
14202 			    (void *)nire));
14203 			ire_delete(nire);
14204 		} else {
14205 			ip1dbg(("ill_recover_saved_ire: added ire %p\n",
14206 			    (void *)nire));
14207 		}
14208 		if (nire != NULL)
14209 			ire_refrele(nire);
14210 	}
14211 	mutex_exit(&ill->ill_saved_ire_lock);
14212 	return (0);
14213 }
14214 
14215 /*
14216  * Used to set the netmask and broadcast address to default values when the
14217  * interface is brought up.  (Always called as writer.)
14218  */
14219 static void
ipif_set_default(ipif_t * ipif)14220 ipif_set_default(ipif_t *ipif)
14221 {
14222 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
14223 
14224 	if (!ipif->ipif_isv6) {
14225 		/*
14226 		 * Interface holds an IPv4 address. Default
14227 		 * mask is the natural netmask.
14228 		 */
14229 		if (!ipif->ipif_net_mask) {
14230 			ipaddr_t	v4mask;
14231 
14232 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
14233 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
14234 		}
14235 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14236 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14237 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
14238 		} else {
14239 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
14240 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
14241 		}
14242 		/*
14243 		 * NOTE: SunOS 4.X does this even if the broadcast address
14244 		 * has been already set thus we do the same here.
14245 		 */
14246 		if (ipif->ipif_flags & IPIF_BROADCAST) {
14247 			ipaddr_t	v4addr;
14248 
14249 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
14250 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
14251 		}
14252 	} else {
14253 		/*
14254 		 * Interface holds an IPv6-only address.  Default
14255 		 * mask is all-ones.
14256 		 */
14257 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
14258 			ipif->ipif_v6net_mask = ipv6_all_ones;
14259 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14260 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14261 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
14262 		} else {
14263 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
14264 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
14265 		}
14266 	}
14267 }
14268 
14269 /*
14270  * Return 0 if this address can be used as local address without causing
14271  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
14272  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
14273  * Note that the same IPv6 link-local address is allowed as long as the ills
14274  * are not on the same link.
14275  */
14276 int
ip_addr_availability_check(ipif_t * new_ipif)14277 ip_addr_availability_check(ipif_t *new_ipif)
14278 {
14279 	in6_addr_t our_v6addr;
14280 	ill_t *ill;
14281 	ipif_t *ipif;
14282 	ill_walk_context_t ctx;
14283 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
14284 
14285 	ASSERT(IAM_WRITER_IPIF(new_ipif));
14286 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
14287 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
14288 
14289 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
14290 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
14291 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
14292 		return (0);
14293 
14294 	our_v6addr = new_ipif->ipif_v6lcl_addr;
14295 
14296 	if (new_ipif->ipif_isv6)
14297 		ill = ILL_START_WALK_V6(&ctx, ipst);
14298 	else
14299 		ill = ILL_START_WALK_V4(&ctx, ipst);
14300 
14301 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
14302 		for (ipif = ill->ill_ipif; ipif != NULL;
14303 		    ipif = ipif->ipif_next) {
14304 			if ((ipif == new_ipif) ||
14305 			    !(ipif->ipif_flags & IPIF_UP) ||
14306 			    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
14307 			    !IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
14308 			    &our_v6addr))
14309 				continue;
14310 
14311 			if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
14312 				new_ipif->ipif_flags |= IPIF_UNNUMBERED;
14313 			else if (ipif->ipif_flags & IPIF_POINTOPOINT)
14314 				ipif->ipif_flags |= IPIF_UNNUMBERED;
14315 			else if ((IN6_IS_ADDR_LINKLOCAL(&our_v6addr) ||
14316 			    IN6_IS_ADDR_SITELOCAL(&our_v6addr)) &&
14317 			    !IS_ON_SAME_LAN(ill, new_ipif->ipif_ill))
14318 				continue;
14319 			else if (new_ipif->ipif_zoneid != ipif->ipif_zoneid &&
14320 			    ipif->ipif_zoneid != ALL_ZONES && IS_LOOPBACK(ill))
14321 				continue;
14322 			else if (new_ipif->ipif_ill == ill)
14323 				return (EADDRINUSE);
14324 			else
14325 				return (EADDRNOTAVAIL);
14326 		}
14327 	}
14328 
14329 	return (0);
14330 }
14331 
14332 /*
14333  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
14334  * IREs for the ipif.
14335  * When the routine returns EINPROGRESS then mp has been consumed and
14336  * the ioctl will be acked from ip_rput_dlpi.
14337  */
14338 int
ipif_up(ipif_t * ipif,queue_t * q,mblk_t * mp)14339 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
14340 {
14341 	ill_t		*ill = ipif->ipif_ill;
14342 	boolean_t	isv6 = ipif->ipif_isv6;
14343 	int		err = 0;
14344 	boolean_t	success;
14345 	uint_t		ipif_orig_id;
14346 	ip_stack_t	*ipst = ill->ill_ipst;
14347 
14348 	ASSERT(IAM_WRITER_IPIF(ipif));
14349 
14350 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
14351 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up",
14352 	    ill_t *, ill, ipif_t *, ipif);
14353 
14354 	/* Shouldn't get here if it is already up. */
14355 	if (ipif->ipif_flags & IPIF_UP)
14356 		return (EALREADY);
14357 
14358 	/*
14359 	 * If this is a request to bring up a data address on an interface
14360 	 * under IPMP, then move the address to its IPMP meta-interface and
14361 	 * try to bring it up.  One complication is that the zeroth ipif for
14362 	 * an ill is special, in that every ill always has one, and that code
14363 	 * throughout IP deferences ill->ill_ipif without holding any locks.
14364 	 */
14365 	if (IS_UNDER_IPMP(ill) && ipmp_ipif_is_dataaddr(ipif) &&
14366 	    (!ipif->ipif_isv6 || !V6_IPIF_LINKLOCAL(ipif))) {
14367 		ipif_t	*stubipif = NULL, *moveipif = NULL;
14368 		ill_t	*ipmp_ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
14369 
14370 		/*
14371 		 * The ipif being brought up should be quiesced.  If it's not,
14372 		 * something has gone amiss and we need to bail out.  (If it's
14373 		 * quiesced, we know it will remain so via IPIF_CONDEMNED.)
14374 		 */
14375 		mutex_enter(&ill->ill_lock);
14376 		if (!ipif_is_quiescent(ipif)) {
14377 			mutex_exit(&ill->ill_lock);
14378 			return (EINVAL);
14379 		}
14380 		mutex_exit(&ill->ill_lock);
14381 
14382 		/*
14383 		 * If we're going to need to allocate ipifs, do it prior
14384 		 * to starting the move (and grabbing locks).
14385 		 */
14386 		if (ipif->ipif_id == 0) {
14387 			if ((moveipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
14388 			    B_FALSE, &err)) == NULL) {
14389 				return (err);
14390 			}
14391 			if ((stubipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
14392 			    B_FALSE, &err)) == NULL) {
14393 				mi_free(moveipif);
14394 				return (err);
14395 			}
14396 		}
14397 
14398 		/*
14399 		 * Grab or transfer the ipif to move.  During the move, keep
14400 		 * ill_g_lock held to prevent any ill walker threads from
14401 		 * seeing things in an inconsistent state.
14402 		 */
14403 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14404 		if (ipif->ipif_id != 0) {
14405 			ipif_remove(ipif);
14406 		} else {
14407 			ipif_transfer(ipif, moveipif, stubipif);
14408 			ipif = moveipif;
14409 		}
14410 
14411 		/*
14412 		 * Place the ipif on the IPMP ill.  If the zeroth ipif on
14413 		 * the IPMP ill is a stub (0.0.0.0 down address) then we
14414 		 * replace that one.  Otherwise, pick the next available slot.
14415 		 */
14416 		ipif->ipif_ill = ipmp_ill;
14417 		ipif_orig_id = ipif->ipif_id;
14418 
14419 		if (ipmp_ipif_is_stubaddr(ipmp_ill->ill_ipif)) {
14420 			ipif_transfer(ipif, ipmp_ill->ill_ipif, NULL);
14421 			ipif = ipmp_ill->ill_ipif;
14422 		} else {
14423 			ipif->ipif_id = -1;
14424 			if ((err = ipif_insert(ipif, B_FALSE)) != 0) {
14425 				/*
14426 				 * No more available ipif_id's -- put it back
14427 				 * on the original ill and fail the operation.
14428 				 * Since we're writer on the ill, we can be
14429 				 * sure our old slot is still available.
14430 				 */
14431 				ipif->ipif_id = ipif_orig_id;
14432 				ipif->ipif_ill = ill;
14433 				if (ipif_orig_id == 0) {
14434 					ipif_transfer(ipif, ill->ill_ipif,
14435 					    NULL);
14436 				} else {
14437 					VERIFY(ipif_insert(ipif, B_FALSE) == 0);
14438 				}
14439 				rw_exit(&ipst->ips_ill_g_lock);
14440 				return (err);
14441 			}
14442 		}
14443 		rw_exit(&ipst->ips_ill_g_lock);
14444 
14445 		/*
14446 		 * Tell SCTP that the ipif has moved.  Note that even if we
14447 		 * had to allocate a new ipif, the original sequence id was
14448 		 * preserved and therefore SCTP won't know.
14449 		 */
14450 		sctp_move_ipif(ipif, ill, ipmp_ill);
14451 
14452 		/*
14453 		 * If the ipif being brought up was on slot zero, then we
14454 		 * first need to bring up the placeholder we stuck there.  In
14455 		 * ip_rput_dlpi_writer(), arp_bringup_done(), or the recursive
14456 		 * call to ipif_up() itself, if we successfully bring up the
14457 		 * placeholder, we'll check ill_move_ipif and bring it up too.
14458 		 */
14459 		if (ipif_orig_id == 0) {
14460 			ASSERT(ill->ill_move_ipif == NULL);
14461 			ill->ill_move_ipif = ipif;
14462 			if ((err = ipif_up(ill->ill_ipif, q, mp)) == 0)
14463 				ASSERT(ill->ill_move_ipif == NULL);
14464 			if (err != EINPROGRESS)
14465 				ill->ill_move_ipif = NULL;
14466 			return (err);
14467 		}
14468 
14469 		/*
14470 		 * Bring it up on the IPMP ill.
14471 		 */
14472 		return (ipif_up(ipif, q, mp));
14473 	}
14474 
14475 	/* Skip arp/ndp for any loopback interface. */
14476 	if (ill->ill_wq != NULL) {
14477 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
14478 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
14479 
14480 		if (!ill->ill_dl_up) {
14481 			/*
14482 			 * ill_dl_up is not yet set. i.e. we are yet to
14483 			 * DL_BIND with the driver and this is the first
14484 			 * logical interface on the ill to become "up".
14485 			 * Tell the driver to get going (via DL_BIND_REQ).
14486 			 * Note that changing "significant" IFF_ flags
14487 			 * address/netmask etc cause a down/up dance, but
14488 			 * does not cause an unbind (DL_UNBIND) with the driver
14489 			 */
14490 			return (ill_dl_up(ill, ipif, mp, q));
14491 		}
14492 
14493 		/*
14494 		 * ipif_resolver_up may end up needeing to bind/attach
14495 		 * the ARP stream, which in turn necessitates a
14496 		 * DLPI message exchange with the driver. ioctls are
14497 		 * serialized and so we cannot send more than one
14498 		 * interface up message at a time. If ipif_resolver_up
14499 		 * does need to wait for the DLPI handshake for the ARP stream,
14500 		 * we get EINPROGRESS and we will complete in arp_bringup_done.
14501 		 */
14502 
14503 		ASSERT(connp != NULL || !CONN_Q(q));
14504 		if (connp != NULL)
14505 			mutex_enter(&connp->conn_lock);
14506 		mutex_enter(&ill->ill_lock);
14507 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
14508 		mutex_exit(&ill->ill_lock);
14509 		if (connp != NULL)
14510 			mutex_exit(&connp->conn_lock);
14511 		if (!success)
14512 			return (EINTR);
14513 
14514 		/*
14515 		 * Crank up IPv6 neighbor discovery. Unlike ARP, this should
14516 		 * complete when ipif_ndp_up returns.
14517 		 */
14518 		err = ipif_resolver_up(ipif, Res_act_initial);
14519 		if (err == EINPROGRESS) {
14520 			/* We will complete it in arp_bringup_done() */
14521 			return (err);
14522 		}
14523 
14524 		if (isv6 && err == 0)
14525 			err = ipif_ndp_up(ipif, B_TRUE);
14526 
14527 		ASSERT(err != EINPROGRESS);
14528 		mp = ipsq_pending_mp_get(ipsq, &connp);
14529 		ASSERT(mp != NULL);
14530 		if (err != 0)
14531 			return (err);
14532 	} else {
14533 		/*
14534 		 * Interfaces without underlying hardware don't do duplicate
14535 		 * address detection.
14536 		 */
14537 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
14538 		ipif->ipif_addr_ready = 1;
14539 		err = ill_add_ires(ill);
14540 		/* allocation failure? */
14541 		if (err != 0)
14542 			return (err);
14543 	}
14544 
14545 	err = (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
14546 	if (err == 0 && ill->ill_move_ipif != NULL) {
14547 		ipif = ill->ill_move_ipif;
14548 		ill->ill_move_ipif = NULL;
14549 		return (ipif_up(ipif, q, mp));
14550 	}
14551 	return (err);
14552 }
14553 
14554 /*
14555  * Add any IREs tied to the ill. For now this is just an IRE_MULTICAST.
14556  * The identical set of IREs need to be removed in ill_delete_ires().
14557  */
14558 int
ill_add_ires(ill_t * ill)14559 ill_add_ires(ill_t *ill)
14560 {
14561 	ire_t	*ire;
14562 	in6_addr_t dummy6 = {(uint32_t)V6_MCAST, 0, 0, 1};
14563 	in_addr_t dummy4 = htonl(INADDR_ALLHOSTS_GROUP);
14564 
14565 	if (ill->ill_ire_multicast != NULL)
14566 		return (0);
14567 
14568 	/*
14569 	 * provide some dummy ire_addr for creating the ire.
14570 	 */
14571 	if (ill->ill_isv6) {
14572 		ire = ire_create_v6(&dummy6, 0, 0, IRE_MULTICAST, ill,
14573 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
14574 	} else {
14575 		ire = ire_create((uchar_t *)&dummy4, 0, 0, IRE_MULTICAST, ill,
14576 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
14577 	}
14578 	if (ire == NULL)
14579 		return (ENOMEM);
14580 
14581 	ill->ill_ire_multicast = ire;
14582 	return (0);
14583 }
14584 
14585 void
ill_delete_ires(ill_t * ill)14586 ill_delete_ires(ill_t *ill)
14587 {
14588 	if (ill->ill_ire_multicast != NULL) {
14589 		/*
14590 		 * BIND/ATTACH completed; Release the ref for ill_ire_multicast
14591 		 * which was taken without any th_tracing enabled.
14592 		 * We also mark it as condemned (note that it was never added)
14593 		 * so that caching conn's can move off of it.
14594 		 */
14595 		ire_make_condemned(ill->ill_ire_multicast);
14596 		ire_refrele_notr(ill->ill_ire_multicast);
14597 		ill->ill_ire_multicast = NULL;
14598 	}
14599 }
14600 
14601 /*
14602  * Perform a bind for the physical device.
14603  * When the routine returns EINPROGRESS then mp has been consumed and
14604  * the ioctl will be acked from ip_rput_dlpi.
14605  * Allocate an unbind message and save it until ipif_down.
14606  */
14607 static int
ill_dl_up(ill_t * ill,ipif_t * ipif,mblk_t * mp,queue_t * q)14608 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
14609 {
14610 	mblk_t	*bind_mp = NULL;
14611 	mblk_t	*unbind_mp = NULL;
14612 	conn_t	*connp;
14613 	boolean_t success;
14614 	int	err;
14615 
14616 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_up", ill_t *, ill);
14617 
14618 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
14619 	ASSERT(IAM_WRITER_ILL(ill));
14620 	ASSERT(mp != NULL);
14621 
14622 	/*
14623 	 * Make sure we have an IRE_MULTICAST in case we immediately
14624 	 * start receiving packets.
14625 	 */
14626 	err = ill_add_ires(ill);
14627 	if (err != 0)
14628 		goto bad;
14629 
14630 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
14631 	    DL_BIND_REQ);
14632 	if (bind_mp == NULL)
14633 		goto bad;
14634 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
14635 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
14636 
14637 	/*
14638 	 * ill_unbind_mp would be non-null if the following sequence had
14639 	 * happened:
14640 	 * - send DL_BIND_REQ to driver, wait for response
14641 	 * - multiple ioctls that need to bring the ipif up are encountered,
14642 	 *   but they cannot enter the ipsq due to the outstanding DL_BIND_REQ.
14643 	 *   These ioctls will then be enqueued on the ipsq
14644 	 * - a DL_ERROR_ACK is returned for the DL_BIND_REQ
14645 	 * At this point, the pending ioctls in the ipsq will be drained, and
14646 	 * since ill->ill_dl_up was not set, ill_dl_up would be invoked with
14647 	 * a non-null ill->ill_unbind_mp
14648 	 */
14649 	if (ill->ill_unbind_mp == NULL) {
14650 		unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t),
14651 		    DL_UNBIND_REQ);
14652 		if (unbind_mp == NULL)
14653 			goto bad;
14654 	}
14655 	/*
14656 	 * Record state needed to complete this operation when the
14657 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
14658 	 */
14659 	connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
14660 	ASSERT(connp != NULL || !CONN_Q(q));
14661 	GRAB_CONN_LOCK(q);
14662 	mutex_enter(&ipif->ipif_ill->ill_lock);
14663 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
14664 	mutex_exit(&ipif->ipif_ill->ill_lock);
14665 	RELEASE_CONN_LOCK(q);
14666 	if (!success)
14667 		goto bad;
14668 
14669 	/*
14670 	 * Save the unbind message for ill_dl_down(); it will be consumed when
14671 	 * the interface goes down.
14672 	 */
14673 	if (ill->ill_unbind_mp == NULL)
14674 		ill->ill_unbind_mp = unbind_mp;
14675 
14676 	ill_dlpi_send(ill, bind_mp);
14677 	/* Send down link-layer capabilities probe if not already done. */
14678 	ill_capability_probe(ill);
14679 
14680 	/*
14681 	 * Sysid used to rely on the fact that netboots set domainname
14682 	 * and the like. Now that miniroot boots aren't strictly netboots
14683 	 * and miniroot network configuration is driven from userland
14684 	 * these things still need to be set. This situation can be detected
14685 	 * by comparing the interface being configured here to the one
14686 	 * dhcifname was set to reference by the boot loader. Once sysid is
14687 	 * converted to use dhcp_ipc_getinfo() this call can go away.
14688 	 */
14689 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
14690 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
14691 	    (strlen(srpc_domain) == 0)) {
14692 		if (dhcpinit() != 0)
14693 			cmn_err(CE_WARN, "no cached dhcp response");
14694 	}
14695 
14696 	/*
14697 	 * This operation will complete in ip_rput_dlpi with either
14698 	 * a DL_BIND_ACK or DL_ERROR_ACK.
14699 	 */
14700 	return (EINPROGRESS);
14701 bad:
14702 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
14703 
14704 	freemsg(bind_mp);
14705 	freemsg(unbind_mp);
14706 	return (ENOMEM);
14707 }
14708 
14709 /* Add room for tcp+ip headers */
14710 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
14711 
14712 /*
14713  * DLPI and ARP is up.
14714  * Create all the IREs associated with an interface. Bring up multicast.
14715  * Set the interface flag and finish other initialization
14716  * that potentially had to be deferred to after DL_BIND_ACK.
14717  */
14718 int
ipif_up_done(ipif_t * ipif)14719 ipif_up_done(ipif_t *ipif)
14720 {
14721 	ill_t		*ill = ipif->ipif_ill;
14722 	int		err = 0;
14723 	boolean_t	loopback = B_FALSE;
14724 	boolean_t	update_src_selection = B_TRUE;
14725 	ipif_t		*tmp_ipif;
14726 
14727 	ip1dbg(("ipif_up_done(%s:%u)\n",
14728 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
14729 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up_done",
14730 	    ill_t *, ill, ipif_t *, ipif);
14731 
14732 	/* Check if this is a loopback interface */
14733 	if (ipif->ipif_ill->ill_wq == NULL)
14734 		loopback = B_TRUE;
14735 
14736 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
14737 
14738 	/*
14739 	 * If all other interfaces for this ill are down or DEPRECATED,
14740 	 * or otherwise unsuitable for source address selection,
14741 	 * reset the src generation numbers to make sure source
14742 	 * address selection gets to take this new ipif into account.
14743 	 * No need to hold ill_lock while traversing the ipif list since
14744 	 * we are writer
14745 	 */
14746 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
14747 	    tmp_ipif = tmp_ipif->ipif_next) {
14748 		if (((tmp_ipif->ipif_flags &
14749 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
14750 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
14751 		    (tmp_ipif == ipif))
14752 			continue;
14753 		/* first useable pre-existing interface */
14754 		update_src_selection = B_FALSE;
14755 		break;
14756 	}
14757 	if (update_src_selection)
14758 		ip_update_source_selection(ill->ill_ipst);
14759 
14760 	if (IS_LOOPBACK(ill) || ill->ill_net_type == IRE_IF_NORESOLVER) {
14761 		nce_t *loop_nce = NULL;
14762 		uint16_t flags = (NCE_F_MYADDR | NCE_F_AUTHORITY | NCE_F_NONUD);
14763 
14764 		/*
14765 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
14766 		 * ipif_lookup_on_name(), but in the case of zones we can have
14767 		 * several loopback addresses on lo0. So all the interfaces with
14768 		 * loopback addresses need to be marked IRE_LOOPBACK.
14769 		 */
14770 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
14771 		    htonl(INADDR_LOOPBACK))
14772 			ipif->ipif_ire_type = IRE_LOOPBACK;
14773 		else
14774 			ipif->ipif_ire_type = IRE_LOCAL;
14775 		if (ill->ill_net_type != IRE_LOOPBACK)
14776 			flags |= NCE_F_PUBLISH;
14777 
14778 		/* add unicast nce for the local addr */
14779 		err = nce_lookup_then_add_v4(ill, NULL,
14780 		    ill->ill_phys_addr_length, &ipif->ipif_lcl_addr, flags,
14781 		    ND_REACHABLE, &loop_nce);
14782 		/* A shared-IP zone sees EEXIST for lo0:N */
14783 		if (err == 0 || err == EEXIST) {
14784 			ipif->ipif_added_nce = 1;
14785 			loop_nce->nce_ipif_cnt++;
14786 			nce_refrele(loop_nce);
14787 			err = 0;
14788 		} else {
14789 			ASSERT(loop_nce == NULL);
14790 			return (err);
14791 		}
14792 	}
14793 
14794 	/* Create all the IREs associated with this interface */
14795 	err = ipif_add_ires_v4(ipif, loopback);
14796 	if (err != 0) {
14797 		/*
14798 		 * see comments about return value from
14799 		 * ip_addr_availability_check() in ipif_add_ires_v4().
14800 		 */
14801 		if (err != EADDRINUSE) {
14802 			(void) ipif_arp_down(ipif);
14803 		} else {
14804 			/*
14805 			 * Make IPMP aware of the deleted ipif so that
14806 			 * the needed ipmp cleanup (e.g., of ipif_bound_ill)
14807 			 * can be completed. Note that we do not want to
14808 			 * destroy the nce that was created on the ipmp_ill
14809 			 * for the active copy of the duplicate address in
14810 			 * use.
14811 			 */
14812 			if (IS_IPMP(ill))
14813 				ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
14814 			err = EADDRNOTAVAIL;
14815 		}
14816 		return (err);
14817 	}
14818 
14819 	if (ill->ill_ipif_up_count == 1 && !loopback) {
14820 		/* Recover any additional IREs entries for this ill */
14821 		(void) ill_recover_saved_ire(ill);
14822 	}
14823 
14824 	if (ill->ill_need_recover_multicast) {
14825 		/*
14826 		 * Need to recover all multicast memberships in the driver.
14827 		 * This had to be deferred until we had attached.  The same
14828 		 * code exists in ipif_up_done_v6() to recover IPv6
14829 		 * memberships.
14830 		 *
14831 		 * Note that it would be preferable to unconditionally do the
14832 		 * ill_recover_multicast() in ill_dl_up(), but we cannot do
14833 		 * that since ill_join_allmulti() depends on ill_dl_up being
14834 		 * set, and it is not set until we receive a DL_BIND_ACK after
14835 		 * having called ill_dl_up().
14836 		 */
14837 		ill_recover_multicast(ill);
14838 	}
14839 
14840 	if (ill->ill_ipif_up_count == 1) {
14841 		/*
14842 		 * Since the interface is now up, it may now be active.
14843 		 */
14844 		if (IS_UNDER_IPMP(ill))
14845 			ipmp_ill_refresh_active(ill);
14846 
14847 		/*
14848 		 * If this is an IPMP interface, we may now be able to
14849 		 * establish ARP entries.
14850 		 */
14851 		if (IS_IPMP(ill))
14852 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
14853 	}
14854 
14855 	/* Join the allhosts multicast address */
14856 	ipif_multicast_up(ipif);
14857 
14858 	if (!loopback && !update_src_selection &&
14859 	    !(ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)))
14860 		ip_update_source_selection(ill->ill_ipst);
14861 
14862 	if (!loopback && ipif->ipif_addr_ready) {
14863 		/* Broadcast an address mask reply. */
14864 		ipif_mask_reply(ipif);
14865 	}
14866 	/* Perhaps ilgs should use this ill */
14867 	update_conn_ill(NULL, ill->ill_ipst);
14868 
14869 	/*
14870 	 * This had to be deferred until we had bound.  Tell routing sockets and
14871 	 * others that this interface is up if it looks like the address has
14872 	 * been validated.  Otherwise, if it isn't ready yet, wait for
14873 	 * duplicate address detection to do its thing.
14874 	 */
14875 	if (ipif->ipif_addr_ready)
14876 		ipif_up_notify(ipif);
14877 	return (0);
14878 }
14879 
14880 /*
14881  * Add the IREs associated with the ipif.
14882  * Those MUST be explicitly removed in ipif_delete_ires_v4.
14883  */
14884 static int
ipif_add_ires_v4(ipif_t * ipif,boolean_t loopback)14885 ipif_add_ires_v4(ipif_t *ipif, boolean_t loopback)
14886 {
14887 	ill_t		*ill = ipif->ipif_ill;
14888 	ip_stack_t	*ipst = ill->ill_ipst;
14889 	ire_t		*ire_array[20];
14890 	ire_t		**irep = ire_array;
14891 	ire_t		**irep1;
14892 	ipaddr_t	net_mask = 0;
14893 	ipaddr_t	subnet_mask, route_mask;
14894 	int		err;
14895 	ire_t		*ire_local = NULL;	/* LOCAL or LOOPBACK */
14896 	ire_t		*ire_if = NULL;
14897 	uchar_t		*gw;
14898 
14899 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14900 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14901 		/*
14902 		 * If we're on a labeled system then make sure that zone-
14903 		 * private addresses have proper remote host database entries.
14904 		 */
14905 		if (is_system_labeled() &&
14906 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
14907 		    !tsol_check_interface_address(ipif))
14908 			return (EINVAL);
14909 
14910 		/* Register the source address for __sin6_src_id */
14911 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
14912 		    ipif->ipif_zoneid, ipst);
14913 		if (err != 0) {
14914 			ip0dbg(("ipif_add_ires: srcid_insert %d\n", err));
14915 			return (err);
14916 		}
14917 
14918 		if (loopback)
14919 			gw = (uchar_t *)&ipif->ipif_lcl_addr;
14920 		else
14921 			gw = NULL;
14922 
14923 		/* If the interface address is set, create the local IRE. */
14924 		ire_local = ire_create(
14925 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
14926 		    (uchar_t *)&ip_g_all_ones,		/* mask */
14927 		    gw,					/* gateway */
14928 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
14929 		    ipif->ipif_ill,
14930 		    ipif->ipif_zoneid,
14931 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14932 		    RTF_PRIVATE : 0) | RTF_KERNEL,
14933 		    NULL,
14934 		    ipst);
14935 		ip1dbg(("ipif_add_ires: 0x%p creating IRE %p type 0x%x"
14936 		    " for 0x%x\n", (void *)ipif, (void *)ire_local,
14937 		    ipif->ipif_ire_type,
14938 		    ntohl(ipif->ipif_lcl_addr)));
14939 		if (ire_local == NULL) {
14940 			ip1dbg(("ipif_up_done: NULL ire_local\n"));
14941 			err = ENOMEM;
14942 			goto bad;
14943 		}
14944 	} else {
14945 		ip1dbg((
14946 		    "ipif_add_ires: not creating IRE %d for 0x%x: flags 0x%x\n",
14947 		    ipif->ipif_ire_type,
14948 		    ntohl(ipif->ipif_lcl_addr),
14949 		    (uint_t)ipif->ipif_flags));
14950 	}
14951 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14952 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14953 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14954 	} else {
14955 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
14956 	}
14957 
14958 	subnet_mask = ipif->ipif_net_mask;
14959 
14960 	/*
14961 	 * If mask was not specified, use natural netmask of
14962 	 * interface address. Also, store this mask back into the
14963 	 * ipif struct.
14964 	 */
14965 	if (subnet_mask == 0) {
14966 		subnet_mask = net_mask;
14967 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
14968 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
14969 		    ipif->ipif_v6subnet);
14970 	}
14971 
14972 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
14973 	if (!loopback && !(ipif->ipif_flags & IPIF_NOXMIT) &&
14974 	    ipif->ipif_subnet != INADDR_ANY) {
14975 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14976 
14977 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14978 			route_mask = IP_HOST_MASK;
14979 		} else {
14980 			route_mask = subnet_mask;
14981 		}
14982 
14983 		ip1dbg(("ipif_add_ires: ipif 0x%p ill 0x%p "
14984 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
14985 		    (void *)ipif, (void *)ill, ill->ill_net_type,
14986 		    ntohl(ipif->ipif_subnet)));
14987 		ire_if = ire_create(
14988 		    (uchar_t *)&ipif->ipif_subnet,
14989 		    (uchar_t *)&route_mask,
14990 		    (uchar_t *)&ipif->ipif_lcl_addr,
14991 		    ill->ill_net_type,
14992 		    ill,
14993 		    ipif->ipif_zoneid,
14994 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14995 		    RTF_PRIVATE: 0) | RTF_KERNEL,
14996 		    NULL,
14997 		    ipst);
14998 		if (ire_if == NULL) {
14999 			ip1dbg(("ipif_up_done: NULL ire_if\n"));
15000 			err = ENOMEM;
15001 			goto bad;
15002 		}
15003 	}
15004 
15005 	/*
15006 	 * Create any necessary broadcast IREs.
15007 	 */
15008 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
15009 	    !(ipif->ipif_flags & IPIF_NOXMIT))
15010 		irep = ipif_create_bcast_ires(ipif, irep);
15011 
15012 	/* If an earlier ire_create failed, get out now */
15013 	for (irep1 = irep; irep1 > ire_array; ) {
15014 		irep1--;
15015 		if (*irep1 == NULL) {
15016 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
15017 			err = ENOMEM;
15018 			goto bad;
15019 		}
15020 	}
15021 
15022 	/*
15023 	 * Need to atomically check for IP address availability under
15024 	 * ip_addr_avail_lock.  ill_g_lock is held as reader to ensure no new
15025 	 * ills or new ipifs can be added while we are checking availability.
15026 	 */
15027 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
15028 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
15029 	/* Mark it up, and increment counters. */
15030 	ipif->ipif_flags |= IPIF_UP;
15031 	ill->ill_ipif_up_count++;
15032 	err = ip_addr_availability_check(ipif);
15033 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
15034 	rw_exit(&ipst->ips_ill_g_lock);
15035 
15036 	if (err != 0) {
15037 		/*
15038 		 * Our address may already be up on the same ill. In this case,
15039 		 * the ARP entry for our ipif replaced the one for the other
15040 		 * ipif. So we don't want to delete it (otherwise the other ipif
15041 		 * would be unable to send packets).
15042 		 * ip_addr_availability_check() identifies this case for us and
15043 		 * returns EADDRINUSE; Caller should turn it into EADDRNOTAVAIL
15044 		 * which is the expected error code.
15045 		 */
15046 		ill->ill_ipif_up_count--;
15047 		ipif->ipif_flags &= ~IPIF_UP;
15048 		goto bad;
15049 	}
15050 
15051 	/*
15052 	 * Add in all newly created IREs.  ire_create_bcast() has
15053 	 * already checked for duplicates of the IRE_BROADCAST type.
15054 	 * We add the IRE_INTERFACE before the IRE_LOCAL to ensure
15055 	 * that lookups find the IRE_LOCAL even if the IRE_INTERFACE is
15056 	 * a /32 route.
15057 	 */
15058 	if (ire_if != NULL) {
15059 		ire_if = ire_add(ire_if);
15060 		if (ire_if == NULL) {
15061 			err = ENOMEM;
15062 			goto bad2;
15063 		}
15064 #ifdef DEBUG
15065 		ire_refhold_notr(ire_if);
15066 		ire_refrele(ire_if);
15067 #endif
15068 	}
15069 	if (ire_local != NULL) {
15070 		ire_local = ire_add(ire_local);
15071 		if (ire_local == NULL) {
15072 			err = ENOMEM;
15073 			goto bad2;
15074 		}
15075 #ifdef DEBUG
15076 		ire_refhold_notr(ire_local);
15077 		ire_refrele(ire_local);
15078 #endif
15079 	}
15080 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15081 	if (ire_local != NULL)
15082 		ipif->ipif_ire_local = ire_local;
15083 	if (ire_if != NULL)
15084 		ipif->ipif_ire_if = ire_if;
15085 	rw_exit(&ipst->ips_ill_g_lock);
15086 	ire_local = NULL;
15087 	ire_if = NULL;
15088 
15089 	/*
15090 	 * We first add all of them, and if that succeeds we refrele the
15091 	 * bunch. That enables us to delete all of them should any of the
15092 	 * ire_adds fail.
15093 	 */
15094 	for (irep1 = irep; irep1 > ire_array; ) {
15095 		irep1--;
15096 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ill->ill_lock)));
15097 		*irep1 = ire_add(*irep1);
15098 		if (*irep1 == NULL) {
15099 			err = ENOMEM;
15100 			goto bad2;
15101 		}
15102 	}
15103 
15104 	for (irep1 = irep; irep1 > ire_array; ) {
15105 		irep1--;
15106 		/* refheld by ire_add. */
15107 		if (*irep1 != NULL) {
15108 			ire_refrele(*irep1);
15109 			*irep1 = NULL;
15110 		}
15111 	}
15112 
15113 	if (!loopback) {
15114 		/*
15115 		 * If the broadcast address has been set, make sure it makes
15116 		 * sense based on the interface address.
15117 		 * Only match on ill since we are sharing broadcast addresses.
15118 		 */
15119 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
15120 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
15121 			ire_t	*ire;
15122 
15123 			ire = ire_ftable_lookup_v4(ipif->ipif_brd_addr, 0, 0,
15124 			    IRE_BROADCAST, ipif->ipif_ill, ALL_ZONES, NULL,
15125 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL), 0, ipst, NULL);
15126 
15127 			if (ire == NULL) {
15128 				/*
15129 				 * If there isn't a matching broadcast IRE,
15130 				 * revert to the default for this netmask.
15131 				 */
15132 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
15133 				mutex_enter(&ipif->ipif_ill->ill_lock);
15134 				ipif_set_default(ipif);
15135 				mutex_exit(&ipif->ipif_ill->ill_lock);
15136 			} else {
15137 				ire_refrele(ire);
15138 			}
15139 		}
15140 
15141 	}
15142 	return (0);
15143 
15144 bad2:
15145 	ill->ill_ipif_up_count--;
15146 	ipif->ipif_flags &= ~IPIF_UP;
15147 
15148 bad:
15149 	ip1dbg(("ipif_add_ires: FAILED \n"));
15150 	if (ire_local != NULL)
15151 		ire_delete(ire_local);
15152 	if (ire_if != NULL)
15153 		ire_delete(ire_if);
15154 
15155 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15156 	ire_local = ipif->ipif_ire_local;
15157 	ipif->ipif_ire_local = NULL;
15158 	ire_if = ipif->ipif_ire_if;
15159 	ipif->ipif_ire_if = NULL;
15160 	rw_exit(&ipst->ips_ill_g_lock);
15161 	if (ire_local != NULL) {
15162 		ire_delete(ire_local);
15163 		ire_refrele_notr(ire_local);
15164 	}
15165 	if (ire_if != NULL) {
15166 		ire_delete(ire_if);
15167 		ire_refrele_notr(ire_if);
15168 	}
15169 
15170 	while (irep > ire_array) {
15171 		irep--;
15172 		if (*irep != NULL) {
15173 			ire_delete(*irep);
15174 		}
15175 	}
15176 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
15177 
15178 	return (err);
15179 }
15180 
15181 /* Remove all the IREs created by ipif_add_ires_v4 */
15182 void
ipif_delete_ires_v4(ipif_t * ipif)15183 ipif_delete_ires_v4(ipif_t *ipif)
15184 {
15185 	ill_t		*ill = ipif->ipif_ill;
15186 	ip_stack_t	*ipst = ill->ill_ipst;
15187 	ire_t		*ire;
15188 
15189 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15190 	ire = ipif->ipif_ire_local;
15191 	ipif->ipif_ire_local = NULL;
15192 	rw_exit(&ipst->ips_ill_g_lock);
15193 	if (ire != NULL) {
15194 		/*
15195 		 * Move count to ipif so we don't loose the count due to
15196 		 * a down/up dance.
15197 		 */
15198 		atomic_add_32(&ipif->ipif_ib_pkt_count, ire->ire_ib_pkt_count);
15199 
15200 		ire_delete(ire);
15201 		ire_refrele_notr(ire);
15202 	}
15203 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15204 	ire = ipif->ipif_ire_if;
15205 	ipif->ipif_ire_if = NULL;
15206 	rw_exit(&ipst->ips_ill_g_lock);
15207 	if (ire != NULL) {
15208 		ire_delete(ire);
15209 		ire_refrele_notr(ire);
15210 	}
15211 
15212 	/*
15213 	 * Delete the broadcast IREs.
15214 	 */
15215 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
15216 	    !(ipif->ipif_flags & IPIF_NOXMIT))
15217 		ipif_delete_bcast_ires(ipif);
15218 }
15219 
15220 /*
15221  * Checks for availbility of a usable source address (if there is one) when the
15222  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
15223  * this selection is done regardless of the destination.
15224  */
15225 boolean_t
ipif_zone_avail(uint_t ifindex,boolean_t isv6,zoneid_t zoneid,ip_stack_t * ipst)15226 ipif_zone_avail(uint_t ifindex, boolean_t isv6, zoneid_t zoneid,
15227     ip_stack_t *ipst)
15228 {
15229 	ipif_t		*ipif = NULL;
15230 	ill_t		*uill;
15231 
15232 	ASSERT(ifindex != 0);
15233 
15234 	uill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
15235 	if (uill == NULL)
15236 		return (B_FALSE);
15237 
15238 	mutex_enter(&uill->ill_lock);
15239 	for (ipif = uill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15240 		if (IPIF_IS_CONDEMNED(ipif))
15241 			continue;
15242 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
15243 			continue;
15244 		if (!(ipif->ipif_flags & IPIF_UP))
15245 			continue;
15246 		if (ipif->ipif_zoneid != zoneid)
15247 			continue;
15248 		if (isv6 ? IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
15249 		    ipif->ipif_lcl_addr == INADDR_ANY)
15250 			continue;
15251 		mutex_exit(&uill->ill_lock);
15252 		ill_refrele(uill);
15253 		return (B_TRUE);
15254 	}
15255 	mutex_exit(&uill->ill_lock);
15256 	ill_refrele(uill);
15257 	return (B_FALSE);
15258 }
15259 
15260 /*
15261  * Find an ipif with a good local address on the ill+zoneid.
15262  */
15263 ipif_t *
ipif_good_addr(ill_t * ill,zoneid_t zoneid)15264 ipif_good_addr(ill_t *ill, zoneid_t zoneid)
15265 {
15266 	ipif_t		*ipif;
15267 
15268 	mutex_enter(&ill->ill_lock);
15269 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15270 		if (IPIF_IS_CONDEMNED(ipif))
15271 			continue;
15272 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
15273 			continue;
15274 		if (!(ipif->ipif_flags & IPIF_UP))
15275 			continue;
15276 		if (ipif->ipif_zoneid != zoneid &&
15277 		    ipif->ipif_zoneid != ALL_ZONES && zoneid != ALL_ZONES)
15278 			continue;
15279 		if (ill->ill_isv6 ?
15280 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
15281 		    ipif->ipif_lcl_addr == INADDR_ANY)
15282 			continue;
15283 		ipif_refhold_locked(ipif);
15284 		mutex_exit(&ill->ill_lock);
15285 		return (ipif);
15286 	}
15287 	mutex_exit(&ill->ill_lock);
15288 	return (NULL);
15289 }
15290 
15291 /*
15292  * IP source address type, sorted from worst to best.  For a given type,
15293  * always prefer IP addresses on the same subnet.  All-zones addresses are
15294  * suboptimal because they pose problems with unlabeled destinations.
15295  */
15296 typedef enum {
15297 	IPIF_NONE,
15298 	IPIF_DIFFNET_DEPRECATED,	/* deprecated and different subnet */
15299 	IPIF_SAMENET_DEPRECATED,	/* deprecated and same subnet */
15300 	IPIF_DIFFNET_ALLZONES,		/* allzones and different subnet */
15301 	IPIF_SAMENET_ALLZONES,		/* allzones and same subnet */
15302 	IPIF_DIFFNET,			/* normal and different subnet */
15303 	IPIF_SAMENET,			/* normal and same subnet */
15304 	IPIF_LOCALADDR			/* local loopback */
15305 } ipif_type_t;
15306 
15307 /*
15308  * Pick the optimal ipif on `ill' for sending to destination `dst' from zone
15309  * `zoneid'.  We rate usable ipifs from low -> high as per the ipif_type_t
15310  * enumeration, and return the highest-rated ipif.  If there's a tie, we pick
15311  * the first one, unless IPMP is used in which case we round-robin among them;
15312  * see below for more.
15313  *
15314  * Returns NULL if there is no suitable source address for the ill.
15315  * This only occurs when there is no valid source address for the ill.
15316  */
15317 ipif_t *
ipif_select_source_v4(ill_t * ill,ipaddr_t dst,zoneid_t zoneid,boolean_t allow_usesrc,boolean_t * notreadyp)15318 ipif_select_source_v4(ill_t *ill, ipaddr_t dst, zoneid_t zoneid,
15319     boolean_t allow_usesrc, boolean_t *notreadyp)
15320 {
15321 	ill_t	*usill = NULL;
15322 	ill_t	*ipmp_ill = NULL;
15323 	ipif_t	*start_ipif, *next_ipif, *ipif, *best_ipif;
15324 	ipif_type_t type, best_type;
15325 	tsol_tpc_t *src_rhtp, *dst_rhtp;
15326 	ip_stack_t *ipst = ill->ill_ipst;
15327 	boolean_t samenet;
15328 
15329 	if (ill->ill_usesrc_ifindex != 0 && allow_usesrc) {
15330 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
15331 		    B_FALSE, ipst);
15332 		if (usill != NULL)
15333 			ill = usill;	/* Select source from usesrc ILL */
15334 		else
15335 			return (NULL);
15336 	}
15337 
15338 	/*
15339 	 * Test addresses should never be used for source address selection,
15340 	 * so if we were passed one, switch to the IPMP meta-interface.
15341 	 */
15342 	if (IS_UNDER_IPMP(ill)) {
15343 		if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL)
15344 			ill = ipmp_ill;	/* Select source from IPMP ill */
15345 		else
15346 			return (NULL);
15347 	}
15348 
15349 	/*
15350 	 * If we're dealing with an unlabeled destination on a labeled system,
15351 	 * make sure that we ignore source addresses that are incompatible with
15352 	 * the destination's default label.  That destination's default label
15353 	 * must dominate the minimum label on the source address.
15354 	 */
15355 	dst_rhtp = NULL;
15356 	if (is_system_labeled()) {
15357 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
15358 		if (dst_rhtp == NULL)
15359 			return (NULL);
15360 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
15361 			TPC_RELE(dst_rhtp);
15362 			dst_rhtp = NULL;
15363 		}
15364 	}
15365 
15366 	/*
15367 	 * Hold the ill_g_lock as reader. This makes sure that no ipif/ill
15368 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
15369 	 * After selecting the right ipif, under ill_lock make sure ipif is
15370 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
15371 	 * we retry. Inside the loop we still need to check for CONDEMNED,
15372 	 * but not under a lock.
15373 	 */
15374 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
15375 retry:
15376 	/*
15377 	 * For source address selection, we treat the ipif list as circular
15378 	 * and continue until we get back to where we started.  This allows
15379 	 * IPMP to vary source address selection (which improves inbound load
15380 	 * spreading) by caching its last ending point and starting from
15381 	 * there.  NOTE: we don't have to worry about ill_src_ipif changing
15382 	 * ills since that can't happen on the IPMP ill.
15383 	 */
15384 	start_ipif = ill->ill_ipif;
15385 	if (IS_IPMP(ill) && ill->ill_src_ipif != NULL)
15386 		start_ipif = ill->ill_src_ipif;
15387 
15388 	ipif = start_ipif;
15389 	best_ipif = NULL;
15390 	best_type = IPIF_NONE;
15391 	do {
15392 		if ((next_ipif = ipif->ipif_next) == NULL)
15393 			next_ipif = ill->ill_ipif;
15394 
15395 		if (IPIF_IS_CONDEMNED(ipif))
15396 			continue;
15397 		/* Always skip NOLOCAL and ANYCAST interfaces */
15398 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
15399 			continue;
15400 		/* Always skip NOACCEPT interfaces */
15401 		if (ipif->ipif_ill->ill_flags & ILLF_NOACCEPT)
15402 			continue;
15403 		if (!(ipif->ipif_flags & IPIF_UP))
15404 			continue;
15405 
15406 		if (!ipif->ipif_addr_ready) {
15407 			if (notreadyp != NULL)
15408 				*notreadyp = B_TRUE;
15409 			continue;
15410 		}
15411 
15412 		if (zoneid != ALL_ZONES &&
15413 		    ipif->ipif_zoneid != zoneid &&
15414 		    ipif->ipif_zoneid != ALL_ZONES)
15415 			continue;
15416 
15417 		/*
15418 		 * Interfaces with 0.0.0.0 address are allowed to be UP, but
15419 		 * are not valid as source addresses.
15420 		 */
15421 		if (ipif->ipif_lcl_addr == INADDR_ANY)
15422 			continue;
15423 
15424 		/*
15425 		 * Check compatibility of local address for destination's
15426 		 * default label if we're on a labeled system.	Incompatible
15427 		 * addresses can't be used at all.
15428 		 */
15429 		if (dst_rhtp != NULL) {
15430 			boolean_t incompat;
15431 
15432 			src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
15433 			    IPV4_VERSION, B_FALSE);
15434 			if (src_rhtp == NULL)
15435 				continue;
15436 			incompat = src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
15437 			    src_rhtp->tpc_tp.tp_doi !=
15438 			    dst_rhtp->tpc_tp.tp_doi ||
15439 			    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
15440 			    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
15441 			    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
15442 			    src_rhtp->tpc_tp.tp_sl_set_cipso));
15443 			TPC_RELE(src_rhtp);
15444 			if (incompat)
15445 				continue;
15446 		}
15447 
15448 		samenet = ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet);
15449 
15450 		if (ipif->ipif_lcl_addr == dst) {
15451 			type = IPIF_LOCALADDR;
15452 		} else if (ipif->ipif_flags & IPIF_DEPRECATED) {
15453 			type = samenet ? IPIF_SAMENET_DEPRECATED :
15454 			    IPIF_DIFFNET_DEPRECATED;
15455 		} else if (ipif->ipif_zoneid == ALL_ZONES) {
15456 			type = samenet ? IPIF_SAMENET_ALLZONES :
15457 			    IPIF_DIFFNET_ALLZONES;
15458 		} else {
15459 			type = samenet ? IPIF_SAMENET : IPIF_DIFFNET;
15460 		}
15461 
15462 		if (type > best_type) {
15463 			best_type = type;
15464 			best_ipif = ipif;
15465 			if (best_type == IPIF_LOCALADDR)
15466 				break; /* can't get better */
15467 		}
15468 	} while ((ipif = next_ipif) != start_ipif);
15469 
15470 	if ((ipif = best_ipif) != NULL) {
15471 		mutex_enter(&ipif->ipif_ill->ill_lock);
15472 		if (IPIF_IS_CONDEMNED(ipif)) {
15473 			mutex_exit(&ipif->ipif_ill->ill_lock);
15474 			goto retry;
15475 		}
15476 		ipif_refhold_locked(ipif);
15477 
15478 		/*
15479 		 * For IPMP, update the source ipif rotor to the next ipif,
15480 		 * provided we can look it up.  (We must not use it if it's
15481 		 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after
15482 		 * ipif_free() checked ill_src_ipif.)
15483 		 */
15484 		if (IS_IPMP(ill) && ipif != NULL) {
15485 			next_ipif = ipif->ipif_next;
15486 			if (next_ipif != NULL && !IPIF_IS_CONDEMNED(next_ipif))
15487 				ill->ill_src_ipif = next_ipif;
15488 			else
15489 				ill->ill_src_ipif = NULL;
15490 		}
15491 		mutex_exit(&ipif->ipif_ill->ill_lock);
15492 	}
15493 
15494 	rw_exit(&ipst->ips_ill_g_lock);
15495 	if (usill != NULL)
15496 		ill_refrele(usill);
15497 	if (ipmp_ill != NULL)
15498 		ill_refrele(ipmp_ill);
15499 	if (dst_rhtp != NULL)
15500 		TPC_RELE(dst_rhtp);
15501 
15502 #ifdef DEBUG
15503 	if (ipif == NULL) {
15504 		char buf1[INET6_ADDRSTRLEN];
15505 
15506 		ip1dbg(("ipif_select_source_v4(%s, %s) -> NULL\n",
15507 		    ill->ill_name,
15508 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
15509 	} else {
15510 		char buf1[INET6_ADDRSTRLEN];
15511 		char buf2[INET6_ADDRSTRLEN];
15512 
15513 		ip1dbg(("ipif_select_source_v4(%s, %s) -> %s\n",
15514 		    ipif->ipif_ill->ill_name,
15515 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
15516 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
15517 		    buf2, sizeof (buf2))));
15518 	}
15519 #endif /* DEBUG */
15520 	return (ipif);
15521 }
15522 
15523 /*
15524  * Pick a source address based on the destination ill and an optional setsrc
15525  * address.
15526  * The result is stored in srcp. If generation is set, then put the source
15527  * generation number there before we look for the source address (to avoid
15528  * missing changes in the set of source addresses.
15529  * If flagsp is set, then us it to pass back ipif_flags.
15530  *
15531  * If the caller wants to cache the returned source address and detect when
15532  * that might be stale, the caller should pass in a generation argument,
15533  * which the caller can later compare against ips_src_generation
15534  *
15535  * The precedence order for selecting an IPv4 source address is:
15536  *  - RTF_SETSRC on the offlink ire always wins.
15537  *  - If usrsrc is set, swap the ill to be the usesrc one.
15538  *  - If IPMP is used on the ill, select a random address from the most
15539  *    preferred ones below:
15540  * 1. If onlink destination, same subnet and not deprecated, not ALL_ZONES
15541  * 2. Not deprecated, not ALL_ZONES
15542  * 3. If onlink destination, same subnet and not deprecated, ALL_ZONES
15543  * 4. Not deprecated, ALL_ZONES
15544  * 5. If onlink destination, same subnet and deprecated
15545  * 6. Deprecated.
15546  *
15547  * We have lower preference for ALL_ZONES IP addresses,
15548  * as they pose problems with unlabeled destinations.
15549  *
15550  * Note that when multiple IP addresses match e.g., #1 we pick
15551  * the first one if IPMP is not in use. With IPMP we randomize.
15552  */
15553 int
ip_select_source_v4(ill_t * ill,ipaddr_t setsrc,ipaddr_t dst,ipaddr_t multicast_ifaddr,zoneid_t zoneid,ip_stack_t * ipst,ipaddr_t * srcp,uint32_t * generation,uint64_t * flagsp)15554 ip_select_source_v4(ill_t *ill, ipaddr_t setsrc, ipaddr_t dst,
15555     ipaddr_t multicast_ifaddr,
15556     zoneid_t zoneid, ip_stack_t *ipst, ipaddr_t *srcp,
15557     uint32_t *generation, uint64_t *flagsp)
15558 {
15559 	ipif_t *ipif;
15560 	boolean_t notready = B_FALSE;	/* Set if !ipif_addr_ready found */
15561 
15562 	if (flagsp != NULL)
15563 		*flagsp = 0;
15564 
15565 	/*
15566 	 * Need to grab the generation number before we check to
15567 	 * avoid a race with a change to the set of local addresses.
15568 	 * No lock needed since the thread which updates the set of local
15569 	 * addresses use ipif/ill locks and exit those (hence a store memory
15570 	 * barrier) before doing the atomic increase of ips_src_generation.
15571 	 */
15572 	if (generation != NULL) {
15573 		*generation = ipst->ips_src_generation;
15574 	}
15575 
15576 	if (CLASSD(dst) && multicast_ifaddr != INADDR_ANY) {
15577 		*srcp = multicast_ifaddr;
15578 		return (0);
15579 	}
15580 
15581 	/* Was RTF_SETSRC set on the first IRE in the recursive lookup? */
15582 	if (setsrc != INADDR_ANY) {
15583 		*srcp = setsrc;
15584 		return (0);
15585 	}
15586 	ipif = ipif_select_source_v4(ill, dst, zoneid, B_TRUE, &notready);
15587 	if (ipif == NULL) {
15588 		if (notready)
15589 			return (ENETDOWN);
15590 		else
15591 			return (EADDRNOTAVAIL);
15592 	}
15593 	*srcp = ipif->ipif_lcl_addr;
15594 	if (flagsp != NULL)
15595 		*flagsp = ipif->ipif_flags;
15596 	ipif_refrele(ipif);
15597 	return (0);
15598 }
15599 
15600 /* ARGSUSED */
15601 int
if_unitsel_restart(ipif_t * ipif,sin_t * dummy_sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * dummy_ifreq)15602 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15603     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15604 {
15605 	/*
15606 	 * ill_phyint_reinit merged the v4 and v6 into a single
15607 	 * ipsq.  We might not have been able to complete the
15608 	 * operation in ipif_set_values, if we could not become
15609 	 * exclusive.  If so restart it here.
15610 	 */
15611 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15612 }
15613 
15614 /*
15615  * Can operate on either a module or a driver queue.
15616  * Returns an error if not a module queue.
15617  */
15618 /* ARGSUSED */
15619 int
if_unitsel(ipif_t * dummy_ipif,sin_t * dummy_sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * dummy_ifreq)15620 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15621     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15622 {
15623 	queue_t		*q1 = q;
15624 	char		*cp;
15625 	char		interf_name[LIFNAMSIZ];
15626 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
15627 
15628 	if (q->q_next == NULL) {
15629 		ip1dbg((
15630 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
15631 		return (EINVAL);
15632 	}
15633 
15634 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
15635 		return (EALREADY);
15636 
15637 	do {
15638 		q1 = q1->q_next;
15639 	} while (q1->q_next);
15640 	cp = q1->q_qinfo->qi_minfo->mi_idname;
15641 	(void) sprintf(interf_name, "%s%d", cp, ppa);
15642 
15643 	/*
15644 	 * Here we are not going to delay the ioack until after
15645 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
15646 	 * original ioctl message before sending the requests.
15647 	 */
15648 	return (ipif_set_values(q, mp, interf_name, &ppa));
15649 }
15650 
15651 /* ARGSUSED */
15652 int
ip_sioctl_sifname(ipif_t * dummy_ipif,sin_t * dummy_sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * dummy_ifreq)15653 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15654     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15655 {
15656 	return (ENXIO);
15657 }
15658 
15659 /*
15660  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
15661  * `irep'.  Returns a pointer to the next free `irep' entry
15662  * A mirror exists in ipif_delete_bcast_ires().
15663  *
15664  * The management of any "extra" or seemingly duplicate IRE_BROADCASTs is
15665  * done in ire_add.
15666  */
15667 static ire_t **
ipif_create_bcast_ires(ipif_t * ipif,ire_t ** irep)15668 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
15669 {
15670 	ipaddr_t addr;
15671 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
15672 	ipaddr_t subnetmask = ipif->ipif_net_mask;
15673 	ill_t *ill = ipif->ipif_ill;
15674 	zoneid_t zoneid = ipif->ipif_zoneid;
15675 
15676 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
15677 
15678 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15679 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15680 
15681 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
15682 	    (ipif->ipif_flags & IPIF_NOLOCAL))
15683 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
15684 
15685 	irep = ire_create_bcast(ill, 0, zoneid, irep);
15686 	irep = ire_create_bcast(ill, INADDR_BROADCAST, zoneid, irep);
15687 
15688 	/*
15689 	 * For backward compatibility, we create net broadcast IREs based on
15690 	 * the old "IP address class system", since some old machines only
15691 	 * respond to these class derived net broadcast.  However, we must not
15692 	 * create these net broadcast IREs if the subnetmask is shorter than
15693 	 * the IP address class based derived netmask.  Otherwise, we may
15694 	 * create a net broadcast address which is the same as an IP address
15695 	 * on the subnet -- and then TCP will refuse to talk to that address.
15696 	 */
15697 	if (netmask < subnetmask) {
15698 		addr = netmask & ipif->ipif_subnet;
15699 		irep = ire_create_bcast(ill, addr, zoneid, irep);
15700 		irep = ire_create_bcast(ill, ~netmask | addr, zoneid, irep);
15701 	}
15702 
15703 	/*
15704 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15705 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15706 	 * created.  Creating these broadcast IREs will only create confusion
15707 	 * as `addr' will be the same as the IP address.
15708 	 */
15709 	if (subnetmask != 0xFFFFFFFF) {
15710 		addr = ipif->ipif_subnet;
15711 		irep = ire_create_bcast(ill, addr, zoneid, irep);
15712 		irep = ire_create_bcast(ill, ~subnetmask | addr, zoneid, irep);
15713 	}
15714 
15715 	return (irep);
15716 }
15717 
15718 /*
15719  * Mirror of ipif_create_bcast_ires()
15720  */
15721 static void
ipif_delete_bcast_ires(ipif_t * ipif)15722 ipif_delete_bcast_ires(ipif_t *ipif)
15723 {
15724 	ipaddr_t	addr;
15725 	ipaddr_t	netmask = ip_net_mask(ipif->ipif_lcl_addr);
15726 	ipaddr_t	subnetmask = ipif->ipif_net_mask;
15727 	ill_t		*ill = ipif->ipif_ill;
15728 	zoneid_t	zoneid = ipif->ipif_zoneid;
15729 	ire_t		*ire;
15730 
15731 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15732 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15733 
15734 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
15735 	    (ipif->ipif_flags & IPIF_NOLOCAL))
15736 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
15737 
15738 	ire = ire_lookup_bcast(ill, 0, zoneid);
15739 	ASSERT(ire != NULL);
15740 	ire_delete(ire); ire_refrele(ire);
15741 	ire = ire_lookup_bcast(ill, INADDR_BROADCAST, zoneid);
15742 	ASSERT(ire != NULL);
15743 	ire_delete(ire); ire_refrele(ire);
15744 
15745 	/*
15746 	 * For backward compatibility, we create net broadcast IREs based on
15747 	 * the old "IP address class system", since some old machines only
15748 	 * respond to these class derived net broadcast.  However, we must not
15749 	 * create these net broadcast IREs if the subnetmask is shorter than
15750 	 * the IP address class based derived netmask.  Otherwise, we may
15751 	 * create a net broadcast address which is the same as an IP address
15752 	 * on the subnet -- and then TCP will refuse to talk to that address.
15753 	 */
15754 	if (netmask < subnetmask) {
15755 		addr = netmask & ipif->ipif_subnet;
15756 		ire = ire_lookup_bcast(ill, addr, zoneid);
15757 		ASSERT(ire != NULL);
15758 		ire_delete(ire); ire_refrele(ire);
15759 		ire = ire_lookup_bcast(ill, ~netmask | addr, zoneid);
15760 		ASSERT(ire != NULL);
15761 		ire_delete(ire); ire_refrele(ire);
15762 	}
15763 
15764 	/*
15765 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15766 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15767 	 * created.  Creating these broadcast IREs will only create confusion
15768 	 * as `addr' will be the same as the IP address.
15769 	 */
15770 	if (subnetmask != 0xFFFFFFFF) {
15771 		addr = ipif->ipif_subnet;
15772 		ire = ire_lookup_bcast(ill, addr, zoneid);
15773 		ASSERT(ire != NULL);
15774 		ire_delete(ire); ire_refrele(ire);
15775 		ire = ire_lookup_bcast(ill, ~subnetmask | addr, zoneid);
15776 		ASSERT(ire != NULL);
15777 		ire_delete(ire); ire_refrele(ire);
15778 	}
15779 }
15780 
15781 /*
15782  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
15783  * from lifr_flags and the name from lifr_name.
15784  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
15785  * since ipif_lookup_on_name uses the _isv6 flags when matching.
15786  * Returns EINPROGRESS when mp has been consumed by queueing it on
15787  * ipx_pending_mp and the ioctl will complete in ip_rput.
15788  *
15789  * Can operate on either a module or a driver queue.
15790  * Returns an error if not a module queue.
15791  */
15792 /* ARGSUSED */
15793 int
ip_sioctl_slifname(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * if_req)15794 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15795     ip_ioctl_cmd_t *ipip, void *if_req)
15796 {
15797 	ill_t	*ill = q->q_ptr;
15798 	phyint_t *phyi;
15799 	ip_stack_t *ipst;
15800 	struct lifreq *lifr = if_req;
15801 	uint64_t new_flags;
15802 
15803 	ASSERT(ipif != NULL);
15804 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
15805 
15806 	if (q->q_next == NULL) {
15807 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
15808 		return (EINVAL);
15809 	}
15810 
15811 	/*
15812 	 * If we are not writer on 'q' then this interface exists already
15813 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
15814 	 * so return EALREADY.
15815 	 */
15816 	if (ill != ipif->ipif_ill)
15817 		return (EALREADY);
15818 
15819 	if (ill->ill_name[0] != '\0')
15820 		return (EALREADY);
15821 
15822 	/*
15823 	 * If there's another ill already with the requested name, ensure
15824 	 * that it's of the same type.  Otherwise, ill_phyint_reinit() will
15825 	 * fuse together two unrelated ills, which will cause chaos.
15826 	 */
15827 	ipst = ill->ill_ipst;
15828 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
15829 	    lifr->lifr_name, NULL);
15830 	if (phyi != NULL) {
15831 		ill_t *ill_mate = phyi->phyint_illv4;
15832 
15833 		if (ill_mate == NULL)
15834 			ill_mate = phyi->phyint_illv6;
15835 		ASSERT(ill_mate != NULL);
15836 
15837 		if (ill_mate->ill_media->ip_m_mac_type !=
15838 		    ill->ill_media->ip_m_mac_type) {
15839 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
15840 			    "use the same ill name on differing media\n"));
15841 			return (EINVAL);
15842 		}
15843 	}
15844 
15845 	/*
15846 	 * We start off as IFF_IPV4 in ipif_allocate and become
15847 	 * IFF_IPV4 or IFF_IPV6 here depending  on lifr_flags value.
15848 	 * The only flags that we read from user space are IFF_IPV4,
15849 	 * IFF_IPV6, and IFF_BROADCAST.
15850 	 *
15851 	 * This ill has not been inserted into the global list.
15852 	 * So we are still single threaded and don't need any lock
15853 	 *
15854 	 * Saniy check the flags.
15855 	 */
15856 
15857 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
15858 	    ((lifr->lifr_flags & IFF_IPV6) ||
15859 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
15860 		ip1dbg(("ip_sioctl_slifname: link not broadcast capable "
15861 		    "or IPv6 i.e., no broadcast \n"));
15862 		return (EINVAL);
15863 	}
15864 
15865 	new_flags =
15866 	    lifr->lifr_flags & (IFF_IPV6|IFF_IPV4|IFF_BROADCAST);
15867 
15868 	if ((new_flags ^ (IFF_IPV6|IFF_IPV4)) == 0) {
15869 		ip1dbg(("ip_sioctl_slifname: flags must be exactly one of "
15870 		    "IFF_IPV4 or IFF_IPV6\n"));
15871 		return (EINVAL);
15872 	}
15873 
15874 	/*
15875 	 * We always start off as IPv4, so only need to check for IPv6.
15876 	 */
15877 	if ((new_flags & IFF_IPV6) != 0) {
15878 		ill->ill_flags |= ILLF_IPV6;
15879 		ill->ill_flags &= ~ILLF_IPV4;
15880 
15881 		if (lifr->lifr_flags & IFF_NOLINKLOCAL)
15882 			ill->ill_flags |= ILLF_NOLINKLOCAL;
15883 	}
15884 
15885 	if ((new_flags & IFF_BROADCAST) != 0)
15886 		ipif->ipif_flags |= IPIF_BROADCAST;
15887 	else
15888 		ipif->ipif_flags &= ~IPIF_BROADCAST;
15889 
15890 	/* We started off as V4. */
15891 	if (ill->ill_flags & ILLF_IPV6) {
15892 		ill->ill_phyint->phyint_illv6 = ill;
15893 		ill->ill_phyint->phyint_illv4 = NULL;
15894 	}
15895 
15896 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
15897 }
15898 
15899 /* ARGSUSED */
15900 int
ip_sioctl_slifname_restart(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * if_req)15901 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15902     ip_ioctl_cmd_t *ipip, void *if_req)
15903 {
15904 	/*
15905 	 * ill_phyint_reinit merged the v4 and v6 into a single
15906 	 * ipsq.  We might not have been able to complete the
15907 	 * slifname in ipif_set_values, if we could not become
15908 	 * exclusive.  If so restart it here
15909 	 */
15910 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15911 }
15912 
15913 /*
15914  * Return a pointer to the ipif which matches the index, IP version type and
15915  * zoneid.
15916  */
15917 ipif_t *
ipif_lookup_on_ifindex(uint_t index,boolean_t isv6,zoneid_t zoneid,ip_stack_t * ipst)15918 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
15919     ip_stack_t *ipst)
15920 {
15921 	ill_t	*ill;
15922 	ipif_t	*ipif = NULL;
15923 
15924 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
15925 	if (ill != NULL) {
15926 		mutex_enter(&ill->ill_lock);
15927 		for (ipif = ill->ill_ipif; ipif != NULL;
15928 		    ipif = ipif->ipif_next) {
15929 			if (!IPIF_IS_CONDEMNED(ipif) && (zoneid == ALL_ZONES ||
15930 			    zoneid == ipif->ipif_zoneid ||
15931 			    ipif->ipif_zoneid == ALL_ZONES)) {
15932 				ipif_refhold_locked(ipif);
15933 				break;
15934 			}
15935 		}
15936 		mutex_exit(&ill->ill_lock);
15937 		ill_refrele(ill);
15938 	}
15939 	return (ipif);
15940 }
15941 
15942 /*
15943  * Change an existing physical interface's index. If the new index
15944  * is acceptable we update the index and the phyint_list_avl_by_index tree.
15945  * Finally, we update other systems which may have a dependence on the
15946  * index value.
15947  */
15948 /* ARGSUSED */
15949 int
ip_sioctl_slifindex(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * ifreq)15950 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15951     ip_ioctl_cmd_t *ipip, void *ifreq)
15952 {
15953 	ill_t		*ill;
15954 	phyint_t	*phyi;
15955 	struct ifreq	*ifr = (struct ifreq *)ifreq;
15956 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15957 	uint_t	old_index, index;
15958 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15959 	avl_index_t	where;
15960 
15961 	if (ipip->ipi_cmd_type == IF_CMD)
15962 		index = ifr->ifr_index;
15963 	else
15964 		index = lifr->lifr_index;
15965 
15966 	/*
15967 	 * Only allow on physical interface. Also, index zero is illegal.
15968 	 */
15969 	ill = ipif->ipif_ill;
15970 	phyi = ill->ill_phyint;
15971 	if (ipif->ipif_id != 0 || index == 0 || index > IF_INDEX_MAX) {
15972 		return (EINVAL);
15973 	}
15974 
15975 	/* If the index is not changing, no work to do */
15976 	if (phyi->phyint_ifindex == index)
15977 		return (0);
15978 
15979 	/*
15980 	 * Use phyint_exists() to determine if the new interface index
15981 	 * is already in use. If the index is unused then we need to
15982 	 * change the phyint's position in the phyint_list_avl_by_index
15983 	 * tree. If we do not do this, subsequent lookups (using the new
15984 	 * index value) will not find the phyint.
15985 	 */
15986 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15987 	if (phyint_exists(index, ipst)) {
15988 		rw_exit(&ipst->ips_ill_g_lock);
15989 		return (EEXIST);
15990 	}
15991 
15992 	/*
15993 	 * The new index is unused. Set it in the phyint. However we must not
15994 	 * forget to trigger NE_IFINDEX_CHANGE event before the ifindex
15995 	 * changes. The event must be bound to old ifindex value.
15996 	 */
15997 	ill_nic_event_dispatch(ill, 0, NE_IFINDEX_CHANGE,
15998 	    &index, sizeof (index));
15999 
16000 	old_index = phyi->phyint_ifindex;
16001 	phyi->phyint_ifindex = index;
16002 
16003 	avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, phyi);
16004 	(void) avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16005 	    &index, &where);
16006 	avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16007 	    phyi, where);
16008 	rw_exit(&ipst->ips_ill_g_lock);
16009 
16010 	/* Update SCTP's ILL list */
16011 	sctp_ill_reindex(ill, old_index);
16012 
16013 	/* Send the routing sockets message */
16014 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
16015 	if (ILL_OTHER(ill))
16016 		ip_rts_ifmsg(ILL_OTHER(ill)->ill_ipif, RTSQ_DEFAULT);
16017 
16018 	/* Perhaps ilgs should use this ill */
16019 	update_conn_ill(NULL, ill->ill_ipst);
16020 	return (0);
16021 }
16022 
16023 /* ARGSUSED */
16024 int
ip_sioctl_get_lifindex(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * ifreq)16025 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16026     ip_ioctl_cmd_t *ipip, void *ifreq)
16027 {
16028 	struct ifreq	*ifr = (struct ifreq *)ifreq;
16029 	struct lifreq	*lifr = (struct lifreq *)ifreq;
16030 
16031 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
16032 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
16033 	/* Get the interface index */
16034 	if (ipip->ipi_cmd_type == IF_CMD) {
16035 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
16036 	} else {
16037 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
16038 	}
16039 	return (0);
16040 }
16041 
16042 /* ARGSUSED */
16043 int
ip_sioctl_get_lifzone(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * ifreq)16044 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16045     ip_ioctl_cmd_t *ipip, void *ifreq)
16046 {
16047 	struct lifreq	*lifr = (struct lifreq *)ifreq;
16048 
16049 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
16050 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
16051 	/* Get the interface zone */
16052 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
16053 	lifr->lifr_zoneid = ipif->ipif_zoneid;
16054 	return (0);
16055 }
16056 
16057 /*
16058  * Set the zoneid of an interface.
16059  */
16060 /* ARGSUSED */
16061 int
ip_sioctl_slifzone(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * ifreq)16062 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16063     ip_ioctl_cmd_t *ipip, void *ifreq)
16064 {
16065 	struct lifreq	*lifr = (struct lifreq *)ifreq;
16066 	int err = 0;
16067 	boolean_t need_up = B_FALSE;
16068 	zone_t *zptr;
16069 	zone_status_t status;
16070 	zoneid_t zoneid;
16071 
16072 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
16073 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
16074 		if (!is_system_labeled())
16075 			return (ENOTSUP);
16076 		zoneid = GLOBAL_ZONEID;
16077 	}
16078 
16079 	/* cannot assign instance zero to a non-global zone */
16080 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
16081 		return (ENOTSUP);
16082 
16083 	/*
16084 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
16085 	 * the event of a race with the zone shutdown processing, since IP
16086 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
16087 	 * interface will be cleaned up even if the zone is shut down
16088 	 * immediately after the status check. If the interface can't be brought
16089 	 * down right away, and the zone is shut down before the restart
16090 	 * function is called, we resolve the possible races by rechecking the
16091 	 * zone status in the restart function.
16092 	 */
16093 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
16094 		return (EINVAL);
16095 	status = zone_status_get(zptr);
16096 	zone_rele(zptr);
16097 
16098 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
16099 		return (EINVAL);
16100 
16101 	if (ipif->ipif_flags & IPIF_UP) {
16102 		/*
16103 		 * If the interface is already marked up,
16104 		 * we call ipif_down which will take care
16105 		 * of ditching any IREs that have been set
16106 		 * up based on the old interface address.
16107 		 */
16108 		err = ipif_logical_down(ipif, q, mp);
16109 		if (err == EINPROGRESS)
16110 			return (err);
16111 		(void) ipif_down_tail(ipif);
16112 		need_up = B_TRUE;
16113 	}
16114 
16115 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
16116 	return (err);
16117 }
16118 
16119 static int
ip_sioctl_slifzone_tail(ipif_t * ipif,zoneid_t zoneid,queue_t * q,mblk_t * mp,boolean_t need_up)16120 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
16121     queue_t *q, mblk_t *mp, boolean_t need_up)
16122 {
16123 	int	err = 0;
16124 	ip_stack_t	*ipst;
16125 
16126 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
16127 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
16128 
16129 	if (CONN_Q(q))
16130 		ipst = CONNQ_TO_IPST(q);
16131 	else
16132 		ipst = ILLQ_TO_IPST(q);
16133 
16134 	/*
16135 	 * For exclusive stacks we don't allow a different zoneid than
16136 	 * global.
16137 	 */
16138 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
16139 	    zoneid != GLOBAL_ZONEID)
16140 		return (EINVAL);
16141 
16142 	/* Set the new zone id. */
16143 	ipif->ipif_zoneid = zoneid;
16144 
16145 	/* Update sctp list */
16146 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
16147 
16148 	/* The default multicast interface might have changed */
16149 	ire_increment_multicast_generation(ipst, ipif->ipif_ill->ill_isv6);
16150 
16151 	if (need_up) {
16152 		/*
16153 		 * Now bring the interface back up.  If this
16154 		 * is the only IPIF for the ILL, ipif_up
16155 		 * will have to re-bind to the device, so
16156 		 * we may get back EINPROGRESS, in which
16157 		 * case, this IOCTL will get completed in
16158 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
16159 		 */
16160 		err = ipif_up(ipif, q, mp);
16161 	}
16162 	return (err);
16163 }
16164 
16165 /* ARGSUSED */
16166 int
ip_sioctl_slifzone_restart(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * if_req)16167 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16168     ip_ioctl_cmd_t *ipip, void *if_req)
16169 {
16170 	struct lifreq *lifr = (struct lifreq *)if_req;
16171 	zoneid_t zoneid;
16172 	zone_t *zptr;
16173 	zone_status_t status;
16174 
16175 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
16176 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
16177 		zoneid = GLOBAL_ZONEID;
16178 
16179 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
16180 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
16181 
16182 	/*
16183 	 * We recheck the zone status to resolve the following race condition:
16184 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
16185 	 * 2) hme0:1 is up and can't be brought down right away;
16186 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
16187 	 * 3) zone "myzone" is halted; the zone status switches to
16188 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
16189 	 * the interfaces to remove - hme0:1 is not returned because it's not
16190 	 * yet in "myzone", so it won't be removed;
16191 	 * 4) the restart function for SIOCSLIFZONE is called; without the
16192 	 * status check here, we would have hme0:1 in "myzone" after it's been
16193 	 * destroyed.
16194 	 * Note that if the status check fails, we need to bring the interface
16195 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
16196 	 * ipif_up_done[_v6]().
16197 	 */
16198 	status = ZONE_IS_UNINITIALIZED;
16199 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
16200 		status = zone_status_get(zptr);
16201 		zone_rele(zptr);
16202 	}
16203 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
16204 		if (ipif->ipif_isv6) {
16205 			(void) ipif_up_done_v6(ipif);
16206 		} else {
16207 			(void) ipif_up_done(ipif);
16208 		}
16209 		return (EINVAL);
16210 	}
16211 
16212 	(void) ipif_down_tail(ipif);
16213 
16214 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
16215 	    B_TRUE));
16216 }
16217 
16218 /*
16219  * Return the number of addresses on `ill' with one or more of the values
16220  * in `set' set and all of the values in `clear' clear.
16221  */
16222 static uint_t
ill_flagaddr_cnt(const ill_t * ill,uint64_t set,uint64_t clear)16223 ill_flagaddr_cnt(const ill_t *ill, uint64_t set, uint64_t clear)
16224 {
16225 	ipif_t	*ipif;
16226 	uint_t	cnt = 0;
16227 
16228 	ASSERT(IAM_WRITER_ILL(ill));
16229 
16230 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
16231 		if ((ipif->ipif_flags & set) && !(ipif->ipif_flags & clear))
16232 			cnt++;
16233 
16234 	return (cnt);
16235 }
16236 
16237 /*
16238  * Return the number of migratable addresses on `ill' that are under
16239  * application control.
16240  */
16241 uint_t
ill_appaddr_cnt(const ill_t * ill)16242 ill_appaddr_cnt(const ill_t *ill)
16243 {
16244 	return (ill_flagaddr_cnt(ill, IPIF_DHCPRUNNING | IPIF_ADDRCONF,
16245 	    IPIF_NOFAILOVER));
16246 }
16247 
16248 /*
16249  * Return the number of point-to-point addresses on `ill'.
16250  */
16251 uint_t
ill_ptpaddr_cnt(const ill_t * ill)16252 ill_ptpaddr_cnt(const ill_t *ill)
16253 {
16254 	return (ill_flagaddr_cnt(ill, IPIF_POINTOPOINT, 0));
16255 }
16256 
16257 /* ARGSUSED */
16258 int
ip_sioctl_get_lifusesrc(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * ifreq)16259 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16260     ip_ioctl_cmd_t *ipip, void *ifreq)
16261 {
16262 	struct lifreq	*lifr = ifreq;
16263 
16264 	ASSERT(q->q_next == NULL);
16265 	ASSERT(CONN_Q(q));
16266 
16267 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
16268 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
16269 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
16270 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
16271 
16272 	return (0);
16273 }
16274 
16275 /* Find the previous ILL in this usesrc group */
16276 static ill_t *
ill_prev_usesrc(ill_t * uill)16277 ill_prev_usesrc(ill_t *uill)
16278 {
16279 	ill_t *ill;
16280 
16281 	for (ill = uill->ill_usesrc_grp_next;
16282 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
16283 	    ill = ill->ill_usesrc_grp_next)
16284 		/* do nothing */;
16285 	return (ill);
16286 }
16287 
16288 /*
16289  * Release all members of the usesrc group. This routine is called
16290  * from ill_delete when the interface being unplumbed is the
16291  * group head.
16292  *
16293  * This silently clears the usesrc that ifconfig setup.
16294  * An alternative would be to keep that ifindex, and drop packets on the floor
16295  * since no source address can be selected.
16296  * Even if we keep the current semantics, don't need a lock and a linked list.
16297  * Can walk all the ills checking if they have a ill_usesrc_ifindex matching
16298  * the one that is being removed. Issue is how we return the usesrc users
16299  * (SIOCGLIFSRCOF). We want to be able to find the ills which have an
16300  * ill_usesrc_ifindex matching a target ill. We could also do that with an
16301  * ill walk, but the walker would need to insert in the ioctl response.
16302  */
16303 static void
ill_disband_usesrc_group(ill_t * uill)16304 ill_disband_usesrc_group(ill_t *uill)
16305 {
16306 	ill_t *next_ill, *tmp_ill;
16307 	ip_stack_t	*ipst = uill->ill_ipst;
16308 
16309 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
16310 	next_ill = uill->ill_usesrc_grp_next;
16311 
16312 	do {
16313 		ASSERT(next_ill != NULL);
16314 		tmp_ill = next_ill->ill_usesrc_grp_next;
16315 		ASSERT(tmp_ill != NULL);
16316 		next_ill->ill_usesrc_grp_next = NULL;
16317 		next_ill->ill_usesrc_ifindex = 0;
16318 		next_ill = tmp_ill;
16319 	} while (next_ill->ill_usesrc_ifindex != 0);
16320 	uill->ill_usesrc_grp_next = NULL;
16321 }
16322 
16323 /*
16324  * Remove the client usesrc ILL from the list and relink to a new list
16325  */
16326 int
ill_relink_usesrc_ills(ill_t * ucill,ill_t * uill,uint_t ifindex)16327 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
16328 {
16329 	ill_t *ill, *tmp_ill;
16330 	ip_stack_t	*ipst = ucill->ill_ipst;
16331 
16332 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
16333 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
16334 
16335 	/*
16336 	 * Check if the usesrc client ILL passed in is not already
16337 	 * in use as a usesrc ILL i.e one whose source address is
16338 	 * in use OR a usesrc ILL is not already in use as a usesrc
16339 	 * client ILL
16340 	 */
16341 	if ((ucill->ill_usesrc_ifindex == 0) ||
16342 	    (uill->ill_usesrc_ifindex != 0)) {
16343 		return (-1);
16344 	}
16345 
16346 	ill = ill_prev_usesrc(ucill);
16347 	ASSERT(ill->ill_usesrc_grp_next != NULL);
16348 
16349 	/* Remove from the current list */
16350 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
16351 		/* Only two elements in the list */
16352 		ASSERT(ill->ill_usesrc_ifindex == 0);
16353 		ill->ill_usesrc_grp_next = NULL;
16354 	} else {
16355 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
16356 	}
16357 
16358 	if (ifindex == 0) {
16359 		ucill->ill_usesrc_ifindex = 0;
16360 		ucill->ill_usesrc_grp_next = NULL;
16361 		return (0);
16362 	}
16363 
16364 	ucill->ill_usesrc_ifindex = ifindex;
16365 	tmp_ill = uill->ill_usesrc_grp_next;
16366 	uill->ill_usesrc_grp_next = ucill;
16367 	ucill->ill_usesrc_grp_next =
16368 	    (tmp_ill != NULL) ? tmp_ill : uill;
16369 	return (0);
16370 }
16371 
16372 /*
16373  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
16374  * ip.c for locking details.
16375  */
16376 /* ARGSUSED */
16377 int
ip_sioctl_slifusesrc(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * ifreq)16378 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16379     ip_ioctl_cmd_t *ipip, void *ifreq)
16380 {
16381 	struct lifreq *lifr = (struct lifreq *)ifreq;
16382 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE;
16383 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
16384 	int err = 0, ret;
16385 	uint_t ifindex;
16386 	ipsq_t *ipsq = NULL;
16387 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
16388 
16389 	ASSERT(IAM_WRITER_IPIF(ipif));
16390 	ASSERT(q->q_next == NULL);
16391 	ASSERT(CONN_Q(q));
16392 
16393 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
16394 
16395 	ifindex = lifr->lifr_index;
16396 	if (ifindex == 0) {
16397 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
16398 			/* non usesrc group interface, nothing to reset */
16399 			return (0);
16400 		}
16401 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
16402 		/* valid reset request */
16403 		reset_flg = B_TRUE;
16404 	}
16405 
16406 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
16407 	if (usesrc_ill == NULL)
16408 		return (ENXIO);
16409 	if (usesrc_ill == ipif->ipif_ill) {
16410 		ill_refrele(usesrc_ill);
16411 		return (EINVAL);
16412 	}
16413 
16414 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
16415 	    NEW_OP, B_TRUE);
16416 	if (ipsq == NULL) {
16417 		err = EINPROGRESS;
16418 		/* Operation enqueued on the ipsq of the usesrc ILL */
16419 		goto done;
16420 	}
16421 
16422 	/* USESRC isn't currently supported with IPMP */
16423 	if (IS_IPMP(usesrc_ill) || IS_UNDER_IPMP(usesrc_ill)) {
16424 		err = ENOTSUP;
16425 		goto done;
16426 	}
16427 
16428 	/*
16429 	 * USESRC isn't compatible with the STANDBY flag.  (STANDBY is only
16430 	 * used by IPMP underlying interfaces, but someone might think it's
16431 	 * more general and try to use it independently with VNI.)
16432 	 */
16433 	if (usesrc_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
16434 		err = ENOTSUP;
16435 		goto done;
16436 	}
16437 
16438 	/*
16439 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
16440 	 * already a client then return EINVAL
16441 	 */
16442 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
16443 		err = EINVAL;
16444 		goto done;
16445 	}
16446 
16447 	/*
16448 	 * If the ill_usesrc_ifindex field is already set to what it needs to
16449 	 * be then this is a duplicate operation.
16450 	 */
16451 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
16452 		err = 0;
16453 		goto done;
16454 	}
16455 
16456 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
16457 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
16458 	    usesrc_ill->ill_isv6));
16459 
16460 	/*
16461 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
16462 	 * and the ill_usesrc_ifindex fields
16463 	 */
16464 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
16465 
16466 	if (reset_flg) {
16467 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
16468 		if (ret != 0) {
16469 			err = EINVAL;
16470 		}
16471 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
16472 		goto done;
16473 	}
16474 
16475 	/*
16476 	 * Four possibilities to consider:
16477 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
16478 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
16479 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
16480 	 * 4. Both are part of their respective usesrc groups
16481 	 */
16482 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
16483 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
16484 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
16485 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
16486 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
16487 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
16488 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
16489 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
16490 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
16491 		/* Insert at head of list */
16492 		usesrc_cli_ill->ill_usesrc_grp_next =
16493 		    usesrc_ill->ill_usesrc_grp_next;
16494 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
16495 	} else {
16496 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
16497 		    ifindex);
16498 		if (ret != 0)
16499 			err = EINVAL;
16500 	}
16501 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
16502 
16503 done:
16504 	if (ipsq != NULL)
16505 		ipsq_exit(ipsq);
16506 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
16507 	ill_refrele(usesrc_ill);
16508 
16509 	/* Let conn_ixa caching know that source address selection changed */
16510 	ip_update_source_selection(ipst);
16511 
16512 	return (err);
16513 }
16514 
16515 /* ARGSUSED */
16516 int
ip_sioctl_get_dadstate(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * if_req)16517 ip_sioctl_get_dadstate(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16518     ip_ioctl_cmd_t *ipip, void *if_req)
16519 {
16520 	struct lifreq	*lifr = (struct lifreq *)if_req;
16521 	ill_t		*ill = ipif->ipif_ill;
16522 
16523 	/*
16524 	 * Need a lock since IFF_UP can be set even when there are
16525 	 * references to the ipif.
16526 	 */
16527 	mutex_enter(&ill->ill_lock);
16528 	if ((ipif->ipif_flags & IPIF_UP) && ipif->ipif_addr_ready == 0)
16529 		lifr->lifr_dadstate = DAD_IN_PROGRESS;
16530 	else
16531 		lifr->lifr_dadstate = DAD_DONE;
16532 	mutex_exit(&ill->ill_lock);
16533 	return (0);
16534 }
16535 
16536 /*
16537  * comparison function used by avl.
16538  */
16539 static int
ill_phyint_compare_index(const void * index_ptr,const void * phyip)16540 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
16541 {
16542 
16543 	uint_t index;
16544 
16545 	ASSERT(phyip != NULL && index_ptr != NULL);
16546 
16547 	index = *((uint_t *)index_ptr);
16548 	/*
16549 	 * let the phyint with the lowest index be on top.
16550 	 */
16551 	if (((phyint_t *)phyip)->phyint_ifindex < index)
16552 		return (1);
16553 	if (((phyint_t *)phyip)->phyint_ifindex > index)
16554 		return (-1);
16555 	return (0);
16556 }
16557 
16558 /*
16559  * comparison function used by avl.
16560  */
16561 static int
ill_phyint_compare_name(const void * name_ptr,const void * phyip)16562 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
16563 {
16564 	ill_t *ill;
16565 	int res = 0;
16566 
16567 	ASSERT(phyip != NULL && name_ptr != NULL);
16568 
16569 	if (((phyint_t *)phyip)->phyint_illv4)
16570 		ill = ((phyint_t *)phyip)->phyint_illv4;
16571 	else
16572 		ill = ((phyint_t *)phyip)->phyint_illv6;
16573 	ASSERT(ill != NULL);
16574 
16575 	res = strcmp(ill->ill_name, (char *)name_ptr);
16576 	if (res > 0)
16577 		return (1);
16578 	else if (res < 0)
16579 		return (-1);
16580 	return (0);
16581 }
16582 
16583 /*
16584  * This function is called on the unplumb path via ill_glist_delete() when
16585  * there are no ills left on the phyint and thus the phyint can be freed.
16586  */
16587 static void
phyint_free(phyint_t * phyi)16588 phyint_free(phyint_t *phyi)
16589 {
16590 	ip_stack_t *ipst = PHYINT_TO_IPST(phyi);
16591 
16592 	ASSERT(phyi->phyint_illv4 == NULL && phyi->phyint_illv6 == NULL);
16593 
16594 	/*
16595 	 * If this phyint was an IPMP meta-interface, blow away the group.
16596 	 * This is safe to do because all of the illgrps have already been
16597 	 * removed by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find us.
16598 	 * If we're cleaning up as a result of failed initialization,
16599 	 * phyint_grp may be NULL.
16600 	 */
16601 	if ((phyi->phyint_flags & PHYI_IPMP) && (phyi->phyint_grp != NULL)) {
16602 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16603 		ipmp_grp_destroy(phyi->phyint_grp);
16604 		phyi->phyint_grp = NULL;
16605 		rw_exit(&ipst->ips_ipmp_lock);
16606 	}
16607 
16608 	/*
16609 	 * If this interface was under IPMP, take it out of the group.
16610 	 */
16611 	if (phyi->phyint_grp != NULL)
16612 		ipmp_phyint_leave_grp(phyi);
16613 
16614 	/*
16615 	 * Delete the phyint and disassociate its ipsq.  The ipsq itself
16616 	 * will be freed in ipsq_exit().
16617 	 */
16618 	phyi->phyint_ipsq->ipsq_phyint = NULL;
16619 	phyi->phyint_name[0] = '\0';
16620 
16621 	mi_free(phyi);
16622 }
16623 
16624 /*
16625  * Attach the ill to the phyint structure which can be shared by both
16626  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
16627  * function is called from ipif_set_values and ill_lookup_on_name (for
16628  * loopback) where we know the name of the ill. We lookup the ill and if
16629  * there is one present already with the name use that phyint. Otherwise
16630  * reuse the one allocated by ill_init.
16631  */
16632 static void
ill_phyint_reinit(ill_t * ill)16633 ill_phyint_reinit(ill_t *ill)
16634 {
16635 	boolean_t isv6 = ill->ill_isv6;
16636 	phyint_t *phyi_old;
16637 	phyint_t *phyi;
16638 	avl_index_t where = 0;
16639 	ill_t	*ill_other = NULL;
16640 	ip_stack_t	*ipst = ill->ill_ipst;
16641 
16642 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16643 
16644 	phyi_old = ill->ill_phyint;
16645 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
16646 	    phyi_old->phyint_illv6 == NULL));
16647 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
16648 	    phyi_old->phyint_illv4 == NULL));
16649 	ASSERT(phyi_old->phyint_ifindex == 0);
16650 
16651 	/*
16652 	 * Now that our ill has a name, set it in the phyint.
16653 	 */
16654 	(void) strlcpy(ill->ill_phyint->phyint_name, ill->ill_name, LIFNAMSIZ);
16655 
16656 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16657 	    ill->ill_name, &where);
16658 
16659 	/*
16660 	 * 1. We grabbed the ill_g_lock before inserting this ill into
16661 	 *    the global list of ills. So no other thread could have located
16662 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
16663 	 * 2. Now locate the other protocol instance of this ill.
16664 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
16665 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
16666 	 *    of neither ill can change.
16667 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
16668 	 *    other ill.
16669 	 * 5. Release all locks.
16670 	 */
16671 
16672 	/*
16673 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
16674 	 * we are initializing IPv4.
16675 	 */
16676 	if (phyi != NULL) {
16677 		ill_other = (isv6) ? phyi->phyint_illv4 : phyi->phyint_illv6;
16678 		ASSERT(ill_other->ill_phyint != NULL);
16679 		ASSERT((isv6 && !ill_other->ill_isv6) ||
16680 		    (!isv6 && ill_other->ill_isv6));
16681 		GRAB_ILL_LOCKS(ill, ill_other);
16682 		/*
16683 		 * We are potentially throwing away phyint_flags which
16684 		 * could be different from the one that we obtain from
16685 		 * ill_other->ill_phyint. But it is okay as we are assuming
16686 		 * that the state maintained within IP is correct.
16687 		 */
16688 		mutex_enter(&phyi->phyint_lock);
16689 		if (isv6) {
16690 			ASSERT(phyi->phyint_illv6 == NULL);
16691 			phyi->phyint_illv6 = ill;
16692 		} else {
16693 			ASSERT(phyi->phyint_illv4 == NULL);
16694 			phyi->phyint_illv4 = ill;
16695 		}
16696 
16697 		/*
16698 		 * Delete the old phyint and make its ipsq eligible
16699 		 * to be freed in ipsq_exit().
16700 		 */
16701 		phyi_old->phyint_illv4 = NULL;
16702 		phyi_old->phyint_illv6 = NULL;
16703 		phyi_old->phyint_ipsq->ipsq_phyint = NULL;
16704 		phyi_old->phyint_name[0] = '\0';
16705 		mi_free(phyi_old);
16706 	} else {
16707 		mutex_enter(&ill->ill_lock);
16708 		/*
16709 		 * We don't need to acquire any lock, since
16710 		 * the ill is not yet visible globally  and we
16711 		 * have not yet released the ill_g_lock.
16712 		 */
16713 		phyi = phyi_old;
16714 		mutex_enter(&phyi->phyint_lock);
16715 		/* XXX We need a recovery strategy here. */
16716 		if (!phyint_assign_ifindex(phyi, ipst))
16717 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
16718 
16719 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16720 		    (void *)phyi, where);
16721 
16722 		(void) avl_find(&ipst->ips_phyint_g_list->
16723 		    phyint_list_avl_by_index,
16724 		    &phyi->phyint_ifindex, &where);
16725 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16726 		    (void *)phyi, where);
16727 	}
16728 
16729 	/*
16730 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
16731 	 * pending mp is not affected because that is per ill basis.
16732 	 */
16733 	ill->ill_phyint = phyi;
16734 
16735 	/*
16736 	 * Now that the phyint's ifindex has been assigned, complete the
16737 	 * remaining
16738 	 */
16739 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
16740 	if (ill->ill_isv6) {
16741 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
16742 		    ill->ill_phyint->phyint_ifindex;
16743 		ill->ill_mcast_type = ipst->ips_mld_max_version;
16744 	} else {
16745 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
16746 	}
16747 
16748 	/*
16749 	 * Generate an event within the hooks framework to indicate that
16750 	 * a new interface has just been added to IP.  For this event to
16751 	 * be generated, the network interface must, at least, have an
16752 	 * ifindex assigned to it.  (We don't generate the event for
16753 	 * loopback since ill_lookup_on_name() has its own NE_PLUMB event.)
16754 	 *
16755 	 * This needs to be run inside the ill_g_lock perimeter to ensure
16756 	 * that the ordering of delivered events to listeners matches the
16757 	 * order of them in the kernel.
16758 	 */
16759 	if (!IS_LOOPBACK(ill)) {
16760 		ill_nic_event_dispatch(ill, 0, NE_PLUMB, ill->ill_name,
16761 		    ill->ill_name_length);
16762 	}
16763 	RELEASE_ILL_LOCKS(ill, ill_other);
16764 	mutex_exit(&phyi->phyint_lock);
16765 }
16766 
16767 /*
16768  * Notify any downstream modules of the name of this interface.
16769  * An M_IOCTL is used even though we don't expect a successful reply.
16770  * Any reply message from the driver (presumably an M_IOCNAK) will
16771  * eventually get discarded somewhere upstream.  The message format is
16772  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
16773  * to IP.
16774  */
16775 static void
ip_ifname_notify(ill_t * ill,queue_t * q)16776 ip_ifname_notify(ill_t *ill, queue_t *q)
16777 {
16778 	mblk_t *mp1, *mp2;
16779 	struct iocblk *iocp;
16780 	struct lifreq *lifr;
16781 
16782 	mp1 = mkiocb(SIOCSLIFNAME);
16783 	if (mp1 == NULL)
16784 		return;
16785 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
16786 	if (mp2 == NULL) {
16787 		freeb(mp1);
16788 		return;
16789 	}
16790 
16791 	mp1->b_cont = mp2;
16792 	iocp = (struct iocblk *)mp1->b_rptr;
16793 	iocp->ioc_count = sizeof (struct lifreq);
16794 
16795 	lifr = (struct lifreq *)mp2->b_rptr;
16796 	mp2->b_wptr += sizeof (struct lifreq);
16797 	bzero(lifr, sizeof (struct lifreq));
16798 
16799 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
16800 	lifr->lifr_ppa = ill->ill_ppa;
16801 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
16802 
16803 	DTRACE_PROBE3(ill__dlpi, char *, "ip_ifname_notify",
16804 	    char *, "SIOCSLIFNAME", ill_t *, ill);
16805 	putnext(q, mp1);
16806 }
16807 
16808 static int
ipif_set_values_tail(ill_t * ill,ipif_t * ipif,mblk_t * mp,queue_t * q)16809 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
16810 {
16811 	int		err;
16812 	ip_stack_t	*ipst = ill->ill_ipst;
16813 	phyint_t	*phyi = ill->ill_phyint;
16814 
16815 	/*
16816 	 * Now that ill_name is set, the configuration for the IPMP
16817 	 * meta-interface can be performed.
16818 	 */
16819 	if (IS_IPMP(ill)) {
16820 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16821 		/*
16822 		 * If phyi->phyint_grp is NULL, then this is the first IPMP
16823 		 * meta-interface and we need to create the IPMP group.
16824 		 */
16825 		if (phyi->phyint_grp == NULL) {
16826 			/*
16827 			 * If someone has renamed another IPMP group to have
16828 			 * the same name as our interface, bail.
16829 			 */
16830 			if (ipmp_grp_lookup(ill->ill_name, ipst) != NULL) {
16831 				rw_exit(&ipst->ips_ipmp_lock);
16832 				return (EEXIST);
16833 			}
16834 			phyi->phyint_grp = ipmp_grp_create(ill->ill_name, phyi);
16835 			if (phyi->phyint_grp == NULL) {
16836 				rw_exit(&ipst->ips_ipmp_lock);
16837 				return (ENOMEM);
16838 			}
16839 		}
16840 		rw_exit(&ipst->ips_ipmp_lock);
16841 	}
16842 
16843 	/* Tell downstream modules where they are. */
16844 	ip_ifname_notify(ill, q);
16845 
16846 	/*
16847 	 * ill_dl_phys returns EINPROGRESS in the usual case.
16848 	 * Error cases are ENOMEM ...
16849 	 */
16850 	err = ill_dl_phys(ill, ipif, mp, q);
16851 
16852 	if (ill->ill_isv6) {
16853 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
16854 		if (ipst->ips_mld_slowtimeout_id == 0) {
16855 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
16856 			    (void *)ipst,
16857 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16858 		}
16859 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
16860 	} else {
16861 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
16862 		if (ipst->ips_igmp_slowtimeout_id == 0) {
16863 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
16864 			    (void *)ipst,
16865 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16866 		}
16867 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
16868 	}
16869 
16870 	return (err);
16871 }
16872 
16873 /*
16874  * Common routine for ppa and ifname setting. Should be called exclusive.
16875  *
16876  * Returns EINPROGRESS when mp has been consumed by queueing it on
16877  * ipx_pending_mp and the ioctl will complete in ip_rput.
16878  *
16879  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
16880  * the new name and new ppa in lifr_name and lifr_ppa respectively.
16881  * For SLIFNAME, we pass these values back to the userland.
16882  */
16883 static int
ipif_set_values(queue_t * q,mblk_t * mp,char * interf_name,uint_t * new_ppa_ptr)16884 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
16885 {
16886 	ill_t	*ill;
16887 	ipif_t	*ipif;
16888 	ipsq_t	*ipsq;
16889 	char	*ppa_ptr;
16890 	char	*old_ptr;
16891 	char	old_char;
16892 	int	error;
16893 	ip_stack_t	*ipst;
16894 
16895 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
16896 	ASSERT(q->q_next != NULL);
16897 	ASSERT(interf_name != NULL);
16898 
16899 	ill = (ill_t *)q->q_ptr;
16900 	ipst = ill->ill_ipst;
16901 
16902 	ASSERT(ill->ill_ipst != NULL);
16903 	ASSERT(ill->ill_name[0] == '\0');
16904 	ASSERT(IAM_WRITER_ILL(ill));
16905 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
16906 	ASSERT(ill->ill_ppa == UINT_MAX);
16907 
16908 	ill->ill_defend_start = ill->ill_defend_count = 0;
16909 	/* The ppa is sent down by ifconfig or is chosen */
16910 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
16911 		return (EINVAL);
16912 	}
16913 
16914 	/*
16915 	 * make sure ppa passed in is same as ppa in the name.
16916 	 * This check is not made when ppa == UINT_MAX in that case ppa
16917 	 * in the name could be anything. System will choose a ppa and
16918 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
16919 	 */
16920 	if (*new_ppa_ptr != UINT_MAX) {
16921 		/* stoi changes the pointer */
16922 		old_ptr = ppa_ptr;
16923 		/*
16924 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
16925 		 * (they don't have an externally visible ppa).  We assign one
16926 		 * here so that we can manage the interface.  Note that in
16927 		 * the past this value was always 0 for DLPI 1 drivers.
16928 		 */
16929 		if (*new_ppa_ptr == 0)
16930 			*new_ppa_ptr = stoi(&old_ptr);
16931 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
16932 			return (EINVAL);
16933 	}
16934 	/*
16935 	 * terminate string before ppa
16936 	 * save char at that location.
16937 	 */
16938 	old_char = ppa_ptr[0];
16939 	ppa_ptr[0] = '\0';
16940 
16941 	ill->ill_ppa = *new_ppa_ptr;
16942 	/*
16943 	 * Finish as much work now as possible before calling ill_glist_insert
16944 	 * which makes the ill globally visible and also merges it with the
16945 	 * other protocol instance of this phyint. The remaining work is
16946 	 * done after entering the ipsq which may happen sometime later.
16947 	 */
16948 	ipif = ill->ill_ipif;
16949 
16950 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
16951 	ipif_assign_seqid(ipif);
16952 
16953 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
16954 		ill->ill_flags |= ILLF_IPV4;
16955 
16956 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
16957 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
16958 
16959 	if (ill->ill_flags & ILLF_IPV6) {
16960 
16961 		ill->ill_isv6 = B_TRUE;
16962 		ill_set_inputfn(ill);
16963 		if (ill->ill_rq != NULL) {
16964 			ill->ill_rq->q_qinfo = &iprinitv6;
16965 		}
16966 
16967 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
16968 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
16969 		ipif->ipif_v6subnet = ipv6_all_zeros;
16970 		ipif->ipif_v6net_mask = ipv6_all_zeros;
16971 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
16972 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
16973 		ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
16974 		/*
16975 		 * point-to-point or Non-mulicast capable
16976 		 * interfaces won't do NUD unless explicitly
16977 		 * configured to do so.
16978 		 */
16979 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
16980 		    !(ill->ill_flags & ILLF_MULTICAST)) {
16981 			ill->ill_flags |= ILLF_NONUD;
16982 		}
16983 		/* Make sure IPv4 specific flag is not set on IPv6 if */
16984 		if (ill->ill_flags & ILLF_NOARP) {
16985 			/*
16986 			 * Note: xresolv interfaces will eventually need
16987 			 * NOARP set here as well, but that will require
16988 			 * those external resolvers to have some
16989 			 * knowledge of that flag and act appropriately.
16990 			 * Not to be changed at present.
16991 			 */
16992 			ill->ill_flags &= ~ILLF_NOARP;
16993 		}
16994 		/*
16995 		 * Set the ILLF_ROUTER flag according to the global
16996 		 * IPv6 forwarding policy.
16997 		 */
16998 		if (ipst->ips_ipv6_forwarding != 0)
16999 			ill->ill_flags |= ILLF_ROUTER;
17000 	} else if (ill->ill_flags & ILLF_IPV4) {
17001 		ill->ill_isv6 = B_FALSE;
17002 		ill_set_inputfn(ill);
17003 		ill->ill_reachable_retrans_time = ARP_RETRANS_TIMER;
17004 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
17005 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
17006 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
17007 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
17008 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
17009 		/*
17010 		 * Set the ILLF_ROUTER flag according to the global
17011 		 * IPv4 forwarding policy.
17012 		 */
17013 		if (ipst->ips_ip_forwarding != 0)
17014 			ill->ill_flags |= ILLF_ROUTER;
17015 	}
17016 
17017 	ASSERT(ill->ill_phyint != NULL);
17018 
17019 	/*
17020 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
17021 	 * be completed in ill_glist_insert -> ill_phyint_reinit
17022 	 */
17023 	if (!ill_allocate_mibs(ill))
17024 		return (ENOMEM);
17025 
17026 	/*
17027 	 * Pick a default sap until we get the DL_INFO_ACK back from
17028 	 * the driver.
17029 	 */
17030 	ill->ill_sap = (ill->ill_isv6) ? ill->ill_media->ip_m_ipv6sap :
17031 	    ill->ill_media->ip_m_ipv4sap;
17032 
17033 	ill->ill_ifname_pending = 1;
17034 	ill->ill_ifname_pending_err = 0;
17035 
17036 	/*
17037 	 * When the first ipif comes up in ipif_up_done(), multicast groups
17038 	 * that were joined while this ill was not bound to the DLPI link need
17039 	 * to be recovered by ill_recover_multicast().
17040 	 */
17041 	ill->ill_need_recover_multicast = 1;
17042 
17043 	ill_refhold(ill);
17044 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17045 	if ((error = ill_glist_insert(ill, interf_name,
17046 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
17047 		ill->ill_ppa = UINT_MAX;
17048 		ill->ill_name[0] = '\0';
17049 		/*
17050 		 * undo null termination done above.
17051 		 */
17052 		ppa_ptr[0] = old_char;
17053 		rw_exit(&ipst->ips_ill_g_lock);
17054 		ill_refrele(ill);
17055 		return (error);
17056 	}
17057 
17058 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
17059 
17060 	/*
17061 	 * When we return the buffer pointed to by interf_name should contain
17062 	 * the same name as in ill_name.
17063 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
17064 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
17065 	 * so copy full name and update the ppa ptr.
17066 	 * When ppa passed in != UINT_MAX all values are correct just undo
17067 	 * null termination, this saves a bcopy.
17068 	 */
17069 	if (*new_ppa_ptr == UINT_MAX) {
17070 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
17071 		*new_ppa_ptr = ill->ill_ppa;
17072 	} else {
17073 		/*
17074 		 * undo null termination done above.
17075 		 */
17076 		ppa_ptr[0] = old_char;
17077 	}
17078 
17079 	/* Let SCTP know about this ILL */
17080 	sctp_update_ill(ill, SCTP_ILL_INSERT);
17081 
17082 	/*
17083 	 * ill_glist_insert has made the ill visible globally, and
17084 	 * ill_phyint_reinit could have changed the ipsq. At this point,
17085 	 * we need to hold the ips_ill_g_lock across the call to enter the
17086 	 * ipsq to enforce atomicity and prevent reordering. In the event
17087 	 * the ipsq has changed, and if the new ipsq is currently busy,
17088 	 * we need to make sure that this half-completed ioctl is ahead of
17089 	 * any subsequent ioctl. We achieve this by not dropping the
17090 	 * ips_ill_g_lock which prevents any ill lookup itself thereby
17091 	 * ensuring that new ioctls can't start.
17092 	 */
17093 	ipsq = ipsq_try_enter_internal(ill, q, mp, ip_reprocess_ioctl, NEW_OP,
17094 	    B_TRUE);
17095 
17096 	rw_exit(&ipst->ips_ill_g_lock);
17097 	ill_refrele(ill);
17098 	if (ipsq == NULL)
17099 		return (EINPROGRESS);
17100 
17101 	/*
17102 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
17103 	 */
17104 	if (ipsq->ipsq_xop->ipx_current_ipif == NULL)
17105 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
17106 	else
17107 		ASSERT(ipsq->ipsq_xop->ipx_current_ipif == ipif);
17108 
17109 	error = ipif_set_values_tail(ill, ipif, mp, q);
17110 	ipsq_exit(ipsq);
17111 	if (error != 0 && error != EINPROGRESS) {
17112 		/*
17113 		 * restore previous values
17114 		 */
17115 		ill->ill_isv6 = B_FALSE;
17116 		ill_set_inputfn(ill);
17117 	}
17118 	return (error);
17119 }
17120 
17121 void
ipif_init(ip_stack_t * ipst)17122 ipif_init(ip_stack_t *ipst)
17123 {
17124 	int i;
17125 
17126 	for (i = 0; i < MAX_G_HEADS; i++) {
17127 		ipst->ips_ill_g_heads[i].ill_g_list_head =
17128 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
17129 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
17130 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
17131 	}
17132 
17133 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
17134 	    ill_phyint_compare_index,
17135 	    sizeof (phyint_t),
17136 	    offsetof(struct phyint, phyint_avl_by_index));
17137 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
17138 	    ill_phyint_compare_name,
17139 	    sizeof (phyint_t),
17140 	    offsetof(struct phyint, phyint_avl_by_name));
17141 }
17142 
17143 /*
17144  * Save enough information so that we can recreate the IRE if
17145  * the interface goes down and then up.
17146  */
17147 void
ill_save_ire(ill_t * ill,ire_t * ire)17148 ill_save_ire(ill_t *ill, ire_t *ire)
17149 {
17150 	mblk_t	*save_mp;
17151 
17152 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
17153 	if (save_mp != NULL) {
17154 		ifrt_t	*ifrt;
17155 
17156 		save_mp->b_wptr += sizeof (ifrt_t);
17157 		ifrt = (ifrt_t *)save_mp->b_rptr;
17158 		bzero(ifrt, sizeof (ifrt_t));
17159 		ifrt->ifrt_type = ire->ire_type;
17160 		if (ire->ire_ipversion == IPV4_VERSION) {
17161 			ASSERT(!ill->ill_isv6);
17162 			ifrt->ifrt_addr = ire->ire_addr;
17163 			ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
17164 			ifrt->ifrt_setsrc_addr = ire->ire_setsrc_addr;
17165 			ifrt->ifrt_mask = ire->ire_mask;
17166 		} else {
17167 			ASSERT(ill->ill_isv6);
17168 			ifrt->ifrt_v6addr = ire->ire_addr_v6;
17169 			/* ire_gateway_addr_v6 can change due to RTM_CHANGE */
17170 			mutex_enter(&ire->ire_lock);
17171 			ifrt->ifrt_v6gateway_addr = ire->ire_gateway_addr_v6;
17172 			mutex_exit(&ire->ire_lock);
17173 			ifrt->ifrt_v6setsrc_addr = ire->ire_setsrc_addr_v6;
17174 			ifrt->ifrt_v6mask = ire->ire_mask_v6;
17175 		}
17176 		ifrt->ifrt_flags = ire->ire_flags;
17177 		ifrt->ifrt_zoneid = ire->ire_zoneid;
17178 		mutex_enter(&ill->ill_saved_ire_lock);
17179 		save_mp->b_cont = ill->ill_saved_ire_mp;
17180 		ill->ill_saved_ire_mp = save_mp;
17181 		ill->ill_saved_ire_cnt++;
17182 		mutex_exit(&ill->ill_saved_ire_lock);
17183 	}
17184 }
17185 
17186 /*
17187  * Remove one entry from ill_saved_ire_mp.
17188  */
17189 void
ill_remove_saved_ire(ill_t * ill,ire_t * ire)17190 ill_remove_saved_ire(ill_t *ill, ire_t *ire)
17191 {
17192 	mblk_t	**mpp;
17193 	mblk_t	*mp;
17194 	ifrt_t	*ifrt;
17195 
17196 	/* Remove from ill_saved_ire_mp list if it is there */
17197 	mutex_enter(&ill->ill_saved_ire_lock);
17198 	for (mpp = &ill->ill_saved_ire_mp; *mpp != NULL;
17199 	    mpp = &(*mpp)->b_cont) {
17200 		in6_addr_t	gw_addr_v6;
17201 
17202 		/*
17203 		 * On a given ill, the tuple of address, gateway, mask,
17204 		 * ire_type, and zoneid is unique for each saved IRE.
17205 		 */
17206 		mp = *mpp;
17207 		ifrt = (ifrt_t *)mp->b_rptr;
17208 		/* ire_gateway_addr_v6 can change - need lock */
17209 		mutex_enter(&ire->ire_lock);
17210 		gw_addr_v6 = ire->ire_gateway_addr_v6;
17211 		mutex_exit(&ire->ire_lock);
17212 
17213 		if (ifrt->ifrt_zoneid != ire->ire_zoneid ||
17214 		    ifrt->ifrt_type != ire->ire_type)
17215 			continue;
17216 
17217 		if (ill->ill_isv6 ?
17218 		    (IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6addr,
17219 		    &ire->ire_addr_v6) &&
17220 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6gateway_addr,
17221 		    &gw_addr_v6) &&
17222 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6mask,
17223 		    &ire->ire_mask_v6)) :
17224 		    (ifrt->ifrt_addr == ire->ire_addr &&
17225 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
17226 		    ifrt->ifrt_mask == ire->ire_mask)) {
17227 			*mpp = mp->b_cont;
17228 			ill->ill_saved_ire_cnt--;
17229 			freeb(mp);
17230 			break;
17231 		}
17232 	}
17233 	mutex_exit(&ill->ill_saved_ire_lock);
17234 }
17235 
17236 /*
17237  * IP multirouting broadcast routes handling
17238  * Append CGTP broadcast IREs to regular ones created
17239  * at ifconfig time.
17240  * The usage is a route add <cgtp_bc> <nic_bc> -multirt i.e., both
17241  * the destination and the gateway are broadcast addresses.
17242  * The caller has verified that the destination is an IRE_BROADCAST and that
17243  * RTF_MULTIRT was set. Here if the gateway is a broadcast address, then
17244  * we create a MULTIRT IRE_BROADCAST.
17245  * Note that the IRE_HOST created by ire_rt_add doesn't get found by anything
17246  * since the IRE_BROADCAST takes precedence; ire_add_v4 does head insertion.
17247  */
17248 static void
ip_cgtp_bcast_add(ire_t * ire,ip_stack_t * ipst)17249 ip_cgtp_bcast_add(ire_t *ire, ip_stack_t *ipst)
17250 {
17251 	ire_t *ire_prim;
17252 
17253 	ASSERT(ire != NULL);
17254 
17255 	ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
17256 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
17257 	    NULL);
17258 	if (ire_prim != NULL) {
17259 		/*
17260 		 * We are in the special case of broadcasts for
17261 		 * CGTP. We add an IRE_BROADCAST that holds
17262 		 * the RTF_MULTIRT flag, the destination
17263 		 * address and the low level
17264 		 * info of ire_prim. In other words, CGTP
17265 		 * broadcast is added to the redundant ipif.
17266 		 */
17267 		ill_t *ill_prim;
17268 		ire_t  *bcast_ire;
17269 
17270 		ill_prim = ire_prim->ire_ill;
17271 
17272 		ip2dbg(("ip_cgtp_filter_bcast_add: ire_prim %p, ill_prim %p\n",
17273 		    (void *)ire_prim, (void *)ill_prim));
17274 
17275 		bcast_ire = ire_create(
17276 		    (uchar_t *)&ire->ire_addr,
17277 		    (uchar_t *)&ip_g_all_ones,
17278 		    (uchar_t *)&ire->ire_gateway_addr,
17279 		    IRE_BROADCAST,
17280 		    ill_prim,
17281 		    GLOBAL_ZONEID,	/* CGTP is only for the global zone */
17282 		    ire->ire_flags | RTF_KERNEL,
17283 		    NULL,
17284 		    ipst);
17285 
17286 		/*
17287 		 * Here we assume that ire_add does head insertion so that
17288 		 * the added IRE_BROADCAST comes before the existing IRE_HOST.
17289 		 */
17290 		if (bcast_ire != NULL) {
17291 			if (ire->ire_flags & RTF_SETSRC) {
17292 				bcast_ire->ire_setsrc_addr =
17293 				    ire->ire_setsrc_addr;
17294 			}
17295 			bcast_ire = ire_add(bcast_ire);
17296 			if (bcast_ire != NULL) {
17297 				ip2dbg(("ip_cgtp_filter_bcast_add: "
17298 				    "added bcast_ire %p\n",
17299 				    (void *)bcast_ire));
17300 
17301 				ill_save_ire(ill_prim, bcast_ire);
17302 				ire_refrele(bcast_ire);
17303 			}
17304 		}
17305 		ire_refrele(ire_prim);
17306 	}
17307 }
17308 
17309 /*
17310  * IP multirouting broadcast routes handling
17311  * Remove the broadcast ire.
17312  * The usage is a route delete <cgtp_bc> <nic_bc> -multirt i.e., both
17313  * the destination and the gateway are broadcast addresses.
17314  * The caller has only verified that RTF_MULTIRT was set. We check
17315  * that the destination is broadcast and that the gateway is a broadcast
17316  * address, and if so delete the IRE added by ip_cgtp_bcast_add().
17317  */
17318 static void
ip_cgtp_bcast_delete(ire_t * ire,ip_stack_t * ipst)17319 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
17320 {
17321 	ASSERT(ire != NULL);
17322 
17323 	if (ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST) {
17324 		ire_t *ire_prim;
17325 
17326 		ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
17327 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0,
17328 		    ipst, NULL);
17329 		if (ire_prim != NULL) {
17330 			ill_t *ill_prim;
17331 			ire_t  *bcast_ire;
17332 
17333 			ill_prim = ire_prim->ire_ill;
17334 
17335 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
17336 			    "ire_prim %p, ill_prim %p\n",
17337 			    (void *)ire_prim, (void *)ill_prim));
17338 
17339 			bcast_ire = ire_ftable_lookup_v4(ire->ire_addr, 0,
17340 			    ire->ire_gateway_addr, IRE_BROADCAST,
17341 			    ill_prim, ALL_ZONES, NULL,
17342 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_ILL |
17343 			    MATCH_IRE_MASK, 0, ipst, NULL);
17344 
17345 			if (bcast_ire != NULL) {
17346 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
17347 				    "looked up bcast_ire %p\n",
17348 				    (void *)bcast_ire));
17349 				ill_remove_saved_ire(bcast_ire->ire_ill,
17350 				    bcast_ire);
17351 				ire_delete(bcast_ire);
17352 				ire_refrele(bcast_ire);
17353 			}
17354 			ire_refrele(ire_prim);
17355 		}
17356 	}
17357 }
17358 
17359 /*
17360  * Derive an interface id from the link layer address.
17361  * Knows about IEEE 802 and IEEE EUI-64 mappings.
17362  */
17363 static void
ip_ether_v6intfid(ill_t * ill,in6_addr_t * v6addr)17364 ip_ether_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17365 {
17366 	char		*addr;
17367 
17368 	/*
17369 	 * Note that some IPv6 interfaces get plumbed over links that claim to
17370 	 * be DL_ETHER, but don't actually have Ethernet MAC addresses (e.g.
17371 	 * PPP links).  The ETHERADDRL check here ensures that we only set the
17372 	 * interface ID on IPv6 interfaces above links that actually have real
17373 	 * Ethernet addresses.
17374 	 */
17375 	if (ill->ill_phys_addr_length == ETHERADDRL) {
17376 		/* Form EUI-64 like address */
17377 		addr = (char *)&v6addr->s6_addr32[2];
17378 		bcopy(ill->ill_phys_addr, addr, 3);
17379 		addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
17380 		addr[3] = (char)0xff;
17381 		addr[4] = (char)0xfe;
17382 		bcopy(ill->ill_phys_addr + 3, addr + 5, 3);
17383 	}
17384 }
17385 
17386 /* ARGSUSED */
17387 static void
ip_nodef_v6intfid(ill_t * ill,in6_addr_t * v6addr)17388 ip_nodef_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17389 {
17390 }
17391 
17392 typedef struct ipmp_ifcookie {
17393 	uint32_t	ic_hostid;
17394 	char		ic_ifname[LIFNAMSIZ];
17395 	char		ic_zonename[ZONENAME_MAX];
17396 } ipmp_ifcookie_t;
17397 
17398 /*
17399  * Construct a pseudo-random interface ID for the IPMP interface that's both
17400  * predictable and (almost) guaranteed to be unique.
17401  */
17402 static void
ip_ipmp_v6intfid(ill_t * ill,in6_addr_t * v6addr)17403 ip_ipmp_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17404 {
17405 	zone_t		*zp;
17406 	uint8_t		*addr;
17407 	uchar_t		hash[16];
17408 	ulong_t		hostid;
17409 	MD5_CTX		ctx;
17410 	ipmp_ifcookie_t	ic = { 0 };
17411 
17412 	ASSERT(IS_IPMP(ill));
17413 
17414 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
17415 	ic.ic_hostid = htonl((uint32_t)hostid);
17416 
17417 	(void) strlcpy(ic.ic_ifname, ill->ill_name, LIFNAMSIZ);
17418 
17419 	if ((zp = zone_find_by_id(ill->ill_zoneid)) != NULL) {
17420 		(void) strlcpy(ic.ic_zonename, zp->zone_name, ZONENAME_MAX);
17421 		zone_rele(zp);
17422 	}
17423 
17424 	MD5Init(&ctx);
17425 	MD5Update(&ctx, &ic, sizeof (ic));
17426 	MD5Final(hash, &ctx);
17427 
17428 	/*
17429 	 * Map the hash to an interface ID per the basic approach in RFC3041.
17430 	 */
17431 	addr = &v6addr->s6_addr8[8];
17432 	bcopy(hash + 8, addr, sizeof (uint64_t));
17433 	addr[0] &= ~0x2;				/* set local bit */
17434 }
17435 
17436 /*
17437  * Map the multicast in6_addr_t in m_ip6addr to the physaddr for ethernet.
17438  */
17439 static void
ip_ether_v6_mapping(ill_t * ill,uchar_t * m_ip6addr,uchar_t * m_physaddr)17440 ip_ether_v6_mapping(ill_t *ill, uchar_t *m_ip6addr, uchar_t *m_physaddr)
17441 {
17442 	phyint_t *phyi = ill->ill_phyint;
17443 
17444 	/*
17445 	 * Check PHYI_MULTI_BCAST and length of physical
17446 	 * address to determine if we use the mapping or the
17447 	 * broadcast address.
17448 	 */
17449 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
17450 	    ill->ill_phys_addr_length != ETHERADDRL) {
17451 		ip_mbcast_mapping(ill, m_ip6addr, m_physaddr);
17452 		return;
17453 	}
17454 	m_physaddr[0] = 0x33;
17455 	m_physaddr[1] = 0x33;
17456 	m_physaddr[2] = m_ip6addr[12];
17457 	m_physaddr[3] = m_ip6addr[13];
17458 	m_physaddr[4] = m_ip6addr[14];
17459 	m_physaddr[5] = m_ip6addr[15];
17460 }
17461 
17462 /*
17463  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for ethernet.
17464  */
17465 static void
ip_ether_v4_mapping(ill_t * ill,uchar_t * m_ipaddr,uchar_t * m_physaddr)17466 ip_ether_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17467 {
17468 	phyint_t *phyi = ill->ill_phyint;
17469 
17470 	/*
17471 	 * Check PHYI_MULTI_BCAST and length of physical
17472 	 * address to determine if we use the mapping or the
17473 	 * broadcast address.
17474 	 */
17475 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
17476 	    ill->ill_phys_addr_length != ETHERADDRL) {
17477 		ip_mbcast_mapping(ill, m_ipaddr, m_physaddr);
17478 		return;
17479 	}
17480 	m_physaddr[0] = 0x01;
17481 	m_physaddr[1] = 0x00;
17482 	m_physaddr[2] = 0x5e;
17483 	m_physaddr[3] = m_ipaddr[1] & 0x7f;
17484 	m_physaddr[4] = m_ipaddr[2];
17485 	m_physaddr[5] = m_ipaddr[3];
17486 }
17487 
17488 /* ARGSUSED */
17489 static void
ip_mbcast_mapping(ill_t * ill,uchar_t * m_ipaddr,uchar_t * m_physaddr)17490 ip_mbcast_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17491 {
17492 	/*
17493 	 * for the MULTI_BCAST case and other cases when we want to
17494 	 * use the link-layer broadcast address for multicast.
17495 	 */
17496 	uint8_t	*bphys_addr;
17497 	dl_unitdata_req_t *dlur;
17498 
17499 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17500 	if (ill->ill_sap_length < 0) {
17501 		bphys_addr = (uchar_t *)dlur +
17502 		    dlur->dl_dest_addr_offset;
17503 	} else  {
17504 		bphys_addr = (uchar_t *)dlur +
17505 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
17506 	}
17507 
17508 	bcopy(bphys_addr, m_physaddr, ill->ill_phys_addr_length);
17509 }
17510 
17511 /*
17512  * Derive IPoIB interface id from the link layer address.
17513  */
17514 static void
ip_ib_v6intfid(ill_t * ill,in6_addr_t * v6addr)17515 ip_ib_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17516 {
17517 	char		*addr;
17518 
17519 	ASSERT(ill->ill_phys_addr_length == 20);
17520 	addr = (char *)&v6addr->s6_addr32[2];
17521 	bcopy(ill->ill_phys_addr + 12, addr, 8);
17522 	/*
17523 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
17524 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
17525 	 * rules. In these cases, the IBA considers these GUIDs to be in
17526 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
17527 	 * required; vendors are required not to assign global EUI-64's
17528 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
17529 	 * of the interface identifier. Whether the GUID is in modified
17530 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
17531 	 * bit set to 1.
17532 	 */
17533 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
17534 }
17535 
17536 /*
17537  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for InfiniBand.
17538  * Note on mapping from multicast IP addresses to IPoIB multicast link
17539  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
17540  * The format of an IPoIB multicast address is:
17541  *
17542  *  4 byte QPN      Scope Sign.  Pkey
17543  * +--------------------------------------------+
17544  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
17545  * +--------------------------------------------+
17546  *
17547  * The Scope and Pkey components are properties of the IBA port and
17548  * network interface. They can be ascertained from the broadcast address.
17549  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
17550  */
17551 static void
ip_ib_v4_mapping(ill_t * ill,uchar_t * m_ipaddr,uchar_t * m_physaddr)17552 ip_ib_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17553 {
17554 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
17555 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
17556 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17557 	uint8_t	*bphys_addr;
17558 	dl_unitdata_req_t *dlur;
17559 
17560 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
17561 
17562 	/*
17563 	 * RFC 4391: IPv4 MGID is 28-bit long.
17564 	 */
17565 	m_physaddr[16] = m_ipaddr[0] & 0x0f;
17566 	m_physaddr[17] = m_ipaddr[1];
17567 	m_physaddr[18] = m_ipaddr[2];
17568 	m_physaddr[19] = m_ipaddr[3];
17569 
17570 
17571 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17572 	if (ill->ill_sap_length < 0) {
17573 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
17574 	} else  {
17575 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
17576 		    ill->ill_sap_length;
17577 	}
17578 	/*
17579 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
17580 	 */
17581 	m_physaddr[5] = bphys_addr[5];
17582 	m_physaddr[8] = bphys_addr[8];
17583 	m_physaddr[9] = bphys_addr[9];
17584 }
17585 
17586 static void
ip_ib_v6_mapping(ill_t * ill,uchar_t * m_ipaddr,uchar_t * m_physaddr)17587 ip_ib_v6_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17588 {
17589 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
17590 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
17591 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17592 	uint8_t	*bphys_addr;
17593 	dl_unitdata_req_t *dlur;
17594 
17595 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
17596 
17597 	/*
17598 	 * RFC 4391: IPv4 MGID is 80-bit long.
17599 	 */
17600 	bcopy(&m_ipaddr[6], &m_physaddr[10], 10);
17601 
17602 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17603 	if (ill->ill_sap_length < 0) {
17604 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
17605 	} else  {
17606 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
17607 		    ill->ill_sap_length;
17608 	}
17609 	/*
17610 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
17611 	 */
17612 	m_physaddr[5] = bphys_addr[5];
17613 	m_physaddr[8] = bphys_addr[8];
17614 	m_physaddr[9] = bphys_addr[9];
17615 }
17616 
17617 /*
17618  * Derive IPv6 interface id from an IPv4 link-layer address (e.g. from an IPv4
17619  * tunnel).  The IPv4 address simply get placed in the lower 4 bytes of the
17620  * IPv6 interface id.  This is a suggested mechanism described in section 3.7
17621  * of RFC4213.
17622  */
17623 static void
ip_ipv4_genv6intfid(ill_t * ill,uint8_t * physaddr,in6_addr_t * v6addr)17624 ip_ipv4_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17625 {
17626 	ASSERT(ill->ill_phys_addr_length == sizeof (ipaddr_t));
17627 	v6addr->s6_addr32[2] = 0;
17628 	bcopy(physaddr, &v6addr->s6_addr32[3], sizeof (ipaddr_t));
17629 }
17630 
17631 /*
17632  * Derive IPv6 interface id from an IPv6 link-layer address (e.g. from an IPv6
17633  * tunnel).  The lower 8 bytes of the IPv6 address simply become the interface
17634  * id.
17635  */
17636 static void
ip_ipv6_genv6intfid(ill_t * ill,uint8_t * physaddr,in6_addr_t * v6addr)17637 ip_ipv6_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17638 {
17639 	in6_addr_t *v6lladdr = (in6_addr_t *)physaddr;
17640 
17641 	ASSERT(ill->ill_phys_addr_length == sizeof (in6_addr_t));
17642 	bcopy(&v6lladdr->s6_addr32[2], &v6addr->s6_addr32[2], 8);
17643 }
17644 
17645 static void
ip_ipv6_v6intfid(ill_t * ill,in6_addr_t * v6addr)17646 ip_ipv6_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17647 {
17648 	ip_ipv6_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17649 }
17650 
17651 static void
ip_ipv6_v6destintfid(ill_t * ill,in6_addr_t * v6addr)17652 ip_ipv6_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17653 {
17654 	ip_ipv6_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17655 }
17656 
17657 static void
ip_ipv4_v6intfid(ill_t * ill,in6_addr_t * v6addr)17658 ip_ipv4_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17659 {
17660 	ip_ipv4_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17661 }
17662 
17663 static void
ip_ipv4_v6destintfid(ill_t * ill,in6_addr_t * v6addr)17664 ip_ipv4_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17665 {
17666 	ip_ipv4_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17667 }
17668 
17669 /*
17670  * Lookup an ill and verify that the zoneid has an ipif on that ill.
17671  * Returns an held ill, or NULL.
17672  */
17673 ill_t *
ill_lookup_on_ifindex_zoneid(uint_t index,zoneid_t zoneid,boolean_t isv6,ip_stack_t * ipst)17674 ill_lookup_on_ifindex_zoneid(uint_t index, zoneid_t zoneid, boolean_t isv6,
17675     ip_stack_t *ipst)
17676 {
17677 	ill_t	*ill;
17678 	ipif_t	*ipif;
17679 
17680 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
17681 	if (ill == NULL)
17682 		return (NULL);
17683 
17684 	mutex_enter(&ill->ill_lock);
17685 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17686 		if (IPIF_IS_CONDEMNED(ipif))
17687 			continue;
17688 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
17689 		    ipif->ipif_zoneid != ALL_ZONES)
17690 			continue;
17691 
17692 		mutex_exit(&ill->ill_lock);
17693 		return (ill);
17694 	}
17695 	mutex_exit(&ill->ill_lock);
17696 	ill_refrele(ill);
17697 	return (NULL);
17698 }
17699 
17700 /*
17701  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
17702  * If a pointer to an ipif_t is returned then the caller will need to do
17703  * an ill_refrele().
17704  */
17705 ipif_t *
ipif_getby_indexes(uint_t ifindex,uint_t lifidx,boolean_t isv6,ip_stack_t * ipst)17706 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
17707     ip_stack_t *ipst)
17708 {
17709 	ipif_t *ipif;
17710 	ill_t *ill;
17711 
17712 	ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
17713 	if (ill == NULL)
17714 		return (NULL);
17715 
17716 	mutex_enter(&ill->ill_lock);
17717 	if (ill->ill_state_flags & ILL_CONDEMNED) {
17718 		mutex_exit(&ill->ill_lock);
17719 		ill_refrele(ill);
17720 		return (NULL);
17721 	}
17722 
17723 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17724 		if (!IPIF_CAN_LOOKUP(ipif))
17725 			continue;
17726 		if (lifidx == ipif->ipif_id) {
17727 			ipif_refhold_locked(ipif);
17728 			break;
17729 		}
17730 	}
17731 
17732 	mutex_exit(&ill->ill_lock);
17733 	ill_refrele(ill);
17734 	return (ipif);
17735 }
17736 
17737 /*
17738  * Set ill_inputfn based on the current know state.
17739  * This needs to be called when any of the factors taken into
17740  * account changes.
17741  */
17742 void
ill_set_inputfn(ill_t * ill)17743 ill_set_inputfn(ill_t *ill)
17744 {
17745 	ip_stack_t	*ipst = ill->ill_ipst;
17746 
17747 	if (ill->ill_isv6) {
17748 		if (is_system_labeled())
17749 			ill->ill_inputfn = ill_input_full_v6;
17750 		else
17751 			ill->ill_inputfn = ill_input_short_v6;
17752 	} else {
17753 		if (is_system_labeled())
17754 			ill->ill_inputfn = ill_input_full_v4;
17755 		else if (ill->ill_dhcpinit != 0)
17756 			ill->ill_inputfn = ill_input_full_v4;
17757 		else if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_RSVP].connf_head
17758 		    != NULL)
17759 			ill->ill_inputfn = ill_input_full_v4;
17760 		else if (ipst->ips_ip_cgtp_filter &&
17761 		    ipst->ips_ip_cgtp_filter_ops != NULL)
17762 			ill->ill_inputfn = ill_input_full_v4;
17763 		else
17764 			ill->ill_inputfn = ill_input_short_v4;
17765 	}
17766 }
17767 
17768 /*
17769  * Re-evaluate ill_inputfn for all the IPv4 ills.
17770  * Used when RSVP and CGTP comes and goes.
17771  */
17772 void
ill_set_inputfn_all(ip_stack_t * ipst)17773 ill_set_inputfn_all(ip_stack_t *ipst)
17774 {
17775 	ill_walk_context_t	ctx;
17776 	ill_t			*ill;
17777 
17778 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
17779 	ill = ILL_START_WALK_V4(&ctx, ipst);
17780 	for (; ill != NULL; ill = ill_next(&ctx, ill))
17781 		ill_set_inputfn(ill);
17782 
17783 	rw_exit(&ipst->ips_ill_g_lock);
17784 }
17785 
17786 /*
17787  * Set the physical address information for `ill' to the contents of the
17788  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
17789  * asynchronous if `ill' cannot immediately be quiesced -- in which case
17790  * EINPROGRESS will be returned.
17791  */
17792 int
ill_set_phys_addr(ill_t * ill,mblk_t * mp)17793 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
17794 {
17795 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17796 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
17797 
17798 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17799 
17800 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
17801 	    dlindp->dl_data != DL_CURR_DEST_ADDR &&
17802 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
17803 		/* Changing DL_IPV6_TOKEN is not yet supported */
17804 		return (0);
17805 	}
17806 
17807 	/*
17808 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
17809 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
17810 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
17811 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
17812 	 */
17813 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
17814 		freemsg(mp);
17815 		return (ENOMEM);
17816 	}
17817 
17818 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17819 
17820 	/*
17821 	 * Since we'll only do a logical down, we can't rely on ipif_down
17822 	 * to turn on ILL_DOWN_IN_PROGRESS, or for the DL_BIND_ACK to reset
17823 	 * ILL_DOWN_IN_PROGRESS. We instead manage this separately for this
17824 	 * case, to quiesce ire's and nce's for ill_is_quiescent.
17825 	 */
17826 	mutex_enter(&ill->ill_lock);
17827 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
17828 	/* no more ire/nce addition allowed */
17829 	mutex_exit(&ill->ill_lock);
17830 
17831 	/*
17832 	 * If we can quiesce the ill, then set the address.  If not, then
17833 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
17834 	 */
17835 	ill_down_ipifs(ill, B_TRUE);
17836 	mutex_enter(&ill->ill_lock);
17837 	if (!ill_is_quiescent(ill)) {
17838 		/* call cannot fail since `conn_t *' argument is NULL */
17839 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17840 		    mp, ILL_DOWN);
17841 		mutex_exit(&ill->ill_lock);
17842 		return (EINPROGRESS);
17843 	}
17844 	mutex_exit(&ill->ill_lock);
17845 
17846 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
17847 	return (0);
17848 }
17849 
17850 /*
17851  * When the allowed-ips link property is set on the datalink, IP receives a
17852  * DL_NOTE_ALLOWED_IPS notification that is processed in ill_set_allowed_ips()
17853  * to initialize the ill_allowed_ips[] array in the ill_t. This array is then
17854  * used to vet addresses passed to ip_sioctl_addr() and to ensure that the
17855  * only IP addresses configured on the ill_t are those in the ill_allowed_ips[]
17856  * array.
17857  */
17858 void
ill_set_allowed_ips(ill_t * ill,mblk_t * mp)17859 ill_set_allowed_ips(ill_t *ill, mblk_t *mp)
17860 {
17861 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17862 	dl_notify_ind_t	*dlip = (dl_notify_ind_t *)mp->b_rptr;
17863 	mac_protect_t *mrp;
17864 	int i;
17865 
17866 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17867 	mrp = (mac_protect_t *)&dlip[1];
17868 
17869 	if (mrp->mp_ipaddrcnt == 0) { /* reset allowed-ips */
17870 		kmem_free(ill->ill_allowed_ips,
17871 		    ill->ill_allowed_ips_cnt * sizeof (in6_addr_t));
17872 		ill->ill_allowed_ips_cnt = 0;
17873 		ill->ill_allowed_ips = NULL;
17874 		mutex_enter(&ill->ill_phyint->phyint_lock);
17875 		ill->ill_phyint->phyint_flags &= ~PHYI_L3PROTECT;
17876 		mutex_exit(&ill->ill_phyint->phyint_lock);
17877 		return;
17878 	}
17879 
17880 	if (ill->ill_allowed_ips != NULL) {
17881 		kmem_free(ill->ill_allowed_ips,
17882 		    ill->ill_allowed_ips_cnt * sizeof (in6_addr_t));
17883 	}
17884 	ill->ill_allowed_ips_cnt = mrp->mp_ipaddrcnt;
17885 	ill->ill_allowed_ips = kmem_alloc(
17886 	    ill->ill_allowed_ips_cnt * sizeof (in6_addr_t), KM_SLEEP);
17887 	for (i = 0; i < mrp->mp_ipaddrcnt;  i++)
17888 		ill->ill_allowed_ips[i] = mrp->mp_ipaddrs[i].ip_addr;
17889 
17890 	mutex_enter(&ill->ill_phyint->phyint_lock);
17891 	ill->ill_phyint->phyint_flags |= PHYI_L3PROTECT;
17892 	mutex_exit(&ill->ill_phyint->phyint_lock);
17893 }
17894 
17895 /*
17896  * Once the ill associated with `q' has quiesced, set its physical address
17897  * information to the values in `addrmp'.  Note that two copies of `addrmp'
17898  * are passed (linked by b_cont), since we sometimes need to save two distinct
17899  * copies in the ill_t, and our context doesn't permit sleeping or allocation
17900  * failure (we'll free the other copy if it's not needed).  Since the ill_t
17901  * is quiesced, we know any stale nce's with the old address information have
17902  * already been removed, so we don't need to call nce_flush().
17903  */
17904 /* ARGSUSED */
17905 static void
ill_set_phys_addr_tail(ipsq_t * ipsq,queue_t * q,mblk_t * addrmp,void * dummy)17906 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
17907 {
17908 	ill_t		*ill = q->q_ptr;
17909 	mblk_t		*addrmp2 = unlinkb(addrmp);
17910 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
17911 	uint_t		addrlen, addroff;
17912 	int		status;
17913 
17914 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17915 
17916 	addroff	= dlindp->dl_addr_offset;
17917 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
17918 
17919 	switch (dlindp->dl_data) {
17920 	case DL_IPV6_LINK_LAYER_ADDR:
17921 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
17922 		freemsg(addrmp2);
17923 		break;
17924 
17925 	case DL_CURR_DEST_ADDR:
17926 		freemsg(ill->ill_dest_addr_mp);
17927 		ill->ill_dest_addr = addrmp->b_rptr + addroff;
17928 		ill->ill_dest_addr_mp = addrmp;
17929 		if (ill->ill_isv6) {
17930 			ill_setdesttoken(ill);
17931 			ipif_setdestlinklocal(ill->ill_ipif);
17932 		}
17933 		freemsg(addrmp2);
17934 		break;
17935 
17936 	case DL_CURR_PHYS_ADDR:
17937 		freemsg(ill->ill_phys_addr_mp);
17938 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
17939 		ill->ill_phys_addr_mp = addrmp;
17940 		ill->ill_phys_addr_length = addrlen;
17941 		if (ill->ill_isv6)
17942 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
17943 		else
17944 			freemsg(addrmp2);
17945 		if (ill->ill_isv6) {
17946 			ill_setdefaulttoken(ill);
17947 			ipif_setlinklocal(ill->ill_ipif);
17948 		}
17949 		break;
17950 	default:
17951 		ASSERT(0);
17952 	}
17953 
17954 	/*
17955 	 * reset ILL_DOWN_IN_PROGRESS so that we can successfully add ires
17956 	 * as we bring the ipifs up again.
17957 	 */
17958 	mutex_enter(&ill->ill_lock);
17959 	ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
17960 	mutex_exit(&ill->ill_lock);
17961 	/*
17962 	 * If there are ipifs to bring up, ill_up_ipifs() will return
17963 	 * EINPROGRESS, and ipsq_current_finish() will be called by
17964 	 * ip_rput_dlpi_writer() or arp_bringup_done() when the last ipif is
17965 	 * brought up.
17966 	 */
17967 	status = ill_up_ipifs(ill, q, addrmp);
17968 	if (status != EINPROGRESS)
17969 		ipsq_current_finish(ipsq);
17970 }
17971 
17972 /*
17973  * Helper routine for setting the ill_nd_lla fields.
17974  */
17975 void
ill_set_ndmp(ill_t * ill,mblk_t * ndmp,uint_t addroff,uint_t addrlen)17976 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
17977 {
17978 	freemsg(ill->ill_nd_lla_mp);
17979 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
17980 	ill->ill_nd_lla_mp = ndmp;
17981 	ill->ill_nd_lla_len = addrlen;
17982 }
17983 
17984 /*
17985  * Replumb the ill.
17986  */
17987 int
ill_replumb(ill_t * ill,mblk_t * mp)17988 ill_replumb(ill_t *ill, mblk_t *mp)
17989 {
17990 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17991 
17992 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17993 
17994 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17995 
17996 	/*
17997 	 * If we can quiesce the ill, then continue.  If not, then
17998 	 * ill_replumb_tail() will be called from ipif_ill_refrele_tail().
17999 	 */
18000 	ill_down_ipifs(ill, B_FALSE);
18001 
18002 	mutex_enter(&ill->ill_lock);
18003 	if (!ill_is_quiescent(ill)) {
18004 		/* call cannot fail since `conn_t *' argument is NULL */
18005 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
18006 		    mp, ILL_DOWN);
18007 		mutex_exit(&ill->ill_lock);
18008 		return (EINPROGRESS);
18009 	}
18010 	mutex_exit(&ill->ill_lock);
18011 
18012 	ill_replumb_tail(ipsq, ill->ill_rq, mp, NULL);
18013 	return (0);
18014 }
18015 
18016 /* ARGSUSED */
18017 static void
ill_replumb_tail(ipsq_t * ipsq,queue_t * q,mblk_t * mp,void * dummy)18018 ill_replumb_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
18019 {
18020 	ill_t *ill = q->q_ptr;
18021 	int err;
18022 	conn_t *connp = NULL;
18023 
18024 	ASSERT(IAM_WRITER_IPSQ(ipsq));
18025 	freemsg(ill->ill_replumb_mp);
18026 	ill->ill_replumb_mp = copyb(mp);
18027 
18028 	if (ill->ill_replumb_mp == NULL) {
18029 		/* out of memory */
18030 		ipsq_current_finish(ipsq);
18031 		return;
18032 	}
18033 
18034 	mutex_enter(&ill->ill_lock);
18035 	ill->ill_up_ipifs = ipsq_pending_mp_add(NULL, ill->ill_ipif,
18036 	    ill->ill_rq, ill->ill_replumb_mp, 0);
18037 	mutex_exit(&ill->ill_lock);
18038 
18039 	if (!ill->ill_up_ipifs) {
18040 		/* already closing */
18041 		ipsq_current_finish(ipsq);
18042 		return;
18043 	}
18044 	ill->ill_replumbing = 1;
18045 	err = ill_down_ipifs_tail(ill);
18046 
18047 	/*
18048 	 * Successfully quiesced and brought down the interface, now we send
18049 	 * the DL_NOTE_REPLUMB_DONE message down to the driver. Reuse the
18050 	 * DL_NOTE_REPLUMB message.
18051 	 */
18052 	mp = mexchange(NULL, mp, sizeof (dl_notify_conf_t), M_PROTO,
18053 	    DL_NOTIFY_CONF);
18054 	ASSERT(mp != NULL);
18055 	((dl_notify_conf_t *)mp->b_rptr)->dl_notification =
18056 	    DL_NOTE_REPLUMB_DONE;
18057 	ill_dlpi_send(ill, mp);
18058 
18059 	/*
18060 	 * For IPv4, we would usually get EINPROGRESS because the ETHERTYPE_ARP
18061 	 * streams have to be unbound. When all the DLPI exchanges are done,
18062 	 * ipsq_current_finish() will be called by arp_bringup_done(). The
18063 	 * remainder of ipif bringup via ill_up_ipifs() will also be done in
18064 	 * arp_bringup_done().
18065 	 */
18066 	ASSERT(ill->ill_replumb_mp != NULL);
18067 	if (err == EINPROGRESS)
18068 		return;
18069 	else
18070 		ill->ill_replumb_mp = ipsq_pending_mp_get(ipsq, &connp);
18071 	ASSERT(connp == NULL);
18072 	if (err == 0 && ill->ill_replumb_mp != NULL &&
18073 	    ill_up_ipifs(ill, q, ill->ill_replumb_mp) == EINPROGRESS) {
18074 		return;
18075 	}
18076 	ipsq_current_finish(ipsq);
18077 }
18078 
18079 /*
18080  * Issue ioctl `cmd' on `lh'; caller provides the initial payload in `buf'
18081  * which is `bufsize' bytes.  On success, zero is returned and `buf' updated
18082  * as per the ioctl.  On failure, an errno is returned.
18083  */
18084 static int
ip_ioctl(ldi_handle_t lh,int cmd,void * buf,uint_t bufsize,cred_t * cr)18085 ip_ioctl(ldi_handle_t lh, int cmd, void *buf, uint_t bufsize, cred_t *cr)
18086 {
18087 	int rval;
18088 	struct strioctl iocb;
18089 
18090 	iocb.ic_cmd = cmd;
18091 	iocb.ic_timout = 15;
18092 	iocb.ic_len = bufsize;
18093 	iocb.ic_dp = buf;
18094 
18095 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval));
18096 }
18097 
18098 /*
18099  * Issue an SIOCGLIFCONF for address family `af' and store the result into a
18100  * dynamically-allocated `lifcp' that will be `bufsizep' bytes on success.
18101  */
18102 static int
ip_lifconf_ioctl(ldi_handle_t lh,int af,struct lifconf * lifcp,uint_t * bufsizep,cred_t * cr)18103 ip_lifconf_ioctl(ldi_handle_t lh, int af, struct lifconf *lifcp,
18104     uint_t *bufsizep, cred_t *cr)
18105 {
18106 	int err;
18107 	struct lifnum lifn;
18108 
18109 	bzero(&lifn, sizeof (lifn));
18110 	lifn.lifn_family = af;
18111 	lifn.lifn_flags = LIFC_UNDER_IPMP;
18112 
18113 	if ((err = ip_ioctl(lh, SIOCGLIFNUM, &lifn, sizeof (lifn), cr)) != 0)
18114 		return (err);
18115 
18116 	/*
18117 	 * Pad the interface count to account for additional interfaces that
18118 	 * may have been configured between the SIOCGLIFNUM and SIOCGLIFCONF.
18119 	 */
18120 	lifn.lifn_count += 4;
18121 	bzero(lifcp, sizeof (*lifcp));
18122 	lifcp->lifc_flags = LIFC_UNDER_IPMP;
18123 	lifcp->lifc_family = af;
18124 	lifcp->lifc_len = *bufsizep = lifn.lifn_count * sizeof (struct lifreq);
18125 	lifcp->lifc_buf = kmem_zalloc(*bufsizep, KM_SLEEP);
18126 
18127 	err = ip_ioctl(lh, SIOCGLIFCONF, lifcp, sizeof (*lifcp), cr);
18128 	if (err != 0) {
18129 		kmem_free(lifcp->lifc_buf, *bufsizep);
18130 		return (err);
18131 	}
18132 
18133 	return (0);
18134 }
18135 
18136 /*
18137  * Helper for ip_interface_cleanup() that removes the loopback interface.
18138  */
18139 static void
ip_loopback_removeif(ldi_handle_t lh,boolean_t isv6,cred_t * cr)18140 ip_loopback_removeif(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
18141 {
18142 	int err;
18143 	struct lifreq lifr;
18144 
18145 	bzero(&lifr, sizeof (lifr));
18146 	(void) strcpy(lifr.lifr_name, ipif_loopback_name);
18147 
18148 	/*
18149 	 * Attempt to remove the interface.  It may legitimately not exist
18150 	 * (e.g. the zone administrator unplumbed it), so ignore ENXIO.
18151 	 */
18152 	err = ip_ioctl(lh, SIOCLIFREMOVEIF, &lifr, sizeof (lifr), cr);
18153 	if (err != 0 && err != ENXIO) {
18154 		ip0dbg(("ip_loopback_removeif: IP%s SIOCLIFREMOVEIF failed: "
18155 		    "error %d\n", isv6 ? "v6" : "v4", err));
18156 	}
18157 }
18158 
18159 /*
18160  * Helper for ip_interface_cleanup() that ensures no IP interfaces are in IPMP
18161  * groups and that IPMP data addresses are down.  These conditions must be met
18162  * so that IPMP interfaces can be I_PUNLINK'd, as per ip_sioctl_plink_ipmp().
18163  */
18164 static void
ip_ipmp_cleanup(ldi_handle_t lh,boolean_t isv6,cred_t * cr)18165 ip_ipmp_cleanup(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
18166 {
18167 	int af = isv6 ? AF_INET6 : AF_INET;
18168 	int i, nifs;
18169 	int err;
18170 	uint_t bufsize;
18171 	uint_t lifrsize = sizeof (struct lifreq);
18172 	struct lifconf lifc;
18173 	struct lifreq *lifrp;
18174 
18175 	if ((err = ip_lifconf_ioctl(lh, af, &lifc, &bufsize, cr)) != 0) {
18176 		cmn_err(CE_WARN, "ip_ipmp_cleanup: cannot get interface list "
18177 		    "(error %d); any IPMP interfaces cannot be shutdown", err);
18178 		return;
18179 	}
18180 
18181 	nifs = lifc.lifc_len / lifrsize;
18182 	for (lifrp = lifc.lifc_req, i = 0; i < nifs; i++, lifrp++) {
18183 		err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
18184 		if (err != 0) {
18185 			cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot get "
18186 			    "flags: error %d", lifrp->lifr_name, err);
18187 			continue;
18188 		}
18189 
18190 		if (lifrp->lifr_flags & IFF_IPMP) {
18191 			if ((lifrp->lifr_flags & (IFF_UP|IFF_DUPLICATE)) == 0)
18192 				continue;
18193 
18194 			lifrp->lifr_flags &= ~IFF_UP;
18195 			err = ip_ioctl(lh, SIOCSLIFFLAGS, lifrp, lifrsize, cr);
18196 			if (err != 0) {
18197 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
18198 				    "bring down (error %d); IPMP interface may "
18199 				    "not be shutdown", lifrp->lifr_name, err);
18200 			}
18201 
18202 			/*
18203 			 * Check if IFF_DUPLICATE is still set -- and if so,
18204 			 * reset the address to clear it.
18205 			 */
18206 			err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
18207 			if (err != 0 || !(lifrp->lifr_flags & IFF_DUPLICATE))
18208 				continue;
18209 
18210 			err = ip_ioctl(lh, SIOCGLIFADDR, lifrp, lifrsize, cr);
18211 			if (err != 0 || (err = ip_ioctl(lh, SIOCGLIFADDR,
18212 			    lifrp, lifrsize, cr)) != 0) {
18213 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
18214 				    "reset DAD (error %d); IPMP interface may "
18215 				    "not be shutdown", lifrp->lifr_name, err);
18216 			}
18217 			continue;
18218 		}
18219 
18220 		if (strchr(lifrp->lifr_name, IPIF_SEPARATOR_CHAR) == 0) {
18221 			lifrp->lifr_groupname[0] = '\0';
18222 			if ((err = ip_ioctl(lh, SIOCSLIFGROUPNAME, lifrp,
18223 			    lifrsize, cr)) != 0) {
18224 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
18225 				    "leave IPMP group (error %d); associated "
18226 				    "IPMP interface may not be shutdown",
18227 				    lifrp->lifr_name, err);
18228 				continue;
18229 			}
18230 		}
18231 	}
18232 
18233 	kmem_free(lifc.lifc_buf, bufsize);
18234 }
18235 
18236 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
18237 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
18238 
18239 /*
18240  * Remove the loopback interfaces and prep the IPMP interfaces to be torn down.
18241  * Non-loopback interfaces are either I_LINK'd or I_PLINK'd; the former go away
18242  * when the user-level processes in the zone are killed and the latter are
18243  * cleaned up by str_stack_shutdown().
18244  */
18245 void
ip_interface_cleanup(ip_stack_t * ipst)18246 ip_interface_cleanup(ip_stack_t *ipst)
18247 {
18248 	ldi_handle_t	lh;
18249 	ldi_ident_t	li;
18250 	cred_t		*cr;
18251 	int		err;
18252 	int		i;
18253 	char		*devs[] = { UDP6DEV, UDPDEV };
18254 	netstackid_t	stackid = ipst->ips_netstack->netstack_stackid;
18255 
18256 	if ((err = ldi_ident_from_major(ddi_name_to_major("ip"), &li)) != 0) {
18257 		cmn_err(CE_WARN, "ip_interface_cleanup: cannot get ldi ident:"
18258 		    " error %d", err);
18259 		return;
18260 	}
18261 
18262 	cr = zone_get_kcred(netstackid_to_zoneid(stackid));
18263 	ASSERT(cr != NULL);
18264 
18265 	/*
18266 	 * NOTE: loop executes exactly twice and is hardcoded to know that the
18267 	 * first iteration is IPv6.  (Unrolling yields repetitious code, hence
18268 	 * the loop.)
18269 	 */
18270 	for (i = 0; i < 2; i++) {
18271 		err = ldi_open_by_name(devs[i], FREAD|FWRITE, cr, &lh, li);
18272 		if (err != 0) {
18273 			cmn_err(CE_WARN, "ip_interface_cleanup: cannot open %s:"
18274 			    " error %d", devs[i], err);
18275 			continue;
18276 		}
18277 
18278 		ip_loopback_removeif(lh, i == 0, cr);
18279 		ip_ipmp_cleanup(lh, i == 0, cr);
18280 
18281 		(void) ldi_close(lh, FREAD|FWRITE, cr);
18282 	}
18283 
18284 	ldi_ident_release(li);
18285 	crfree(cr);
18286 }
18287 
18288 /*
18289  * This needs to be in-sync with nic_event_t definition
18290  */
18291 static const char *
ill_hook_event2str(nic_event_t event)18292 ill_hook_event2str(nic_event_t event)
18293 {
18294 	switch (event) {
18295 	case NE_PLUMB:
18296 		return ("PLUMB");
18297 	case NE_UNPLUMB:
18298 		return ("UNPLUMB");
18299 	case NE_UP:
18300 		return ("UP");
18301 	case NE_DOWN:
18302 		return ("DOWN");
18303 	case NE_ADDRESS_CHANGE:
18304 		return ("ADDRESS_CHANGE");
18305 	case NE_LIF_UP:
18306 		return ("LIF_UP");
18307 	case NE_LIF_DOWN:
18308 		return ("LIF_DOWN");
18309 	case NE_IFINDEX_CHANGE:
18310 		return ("IFINDEX_CHANGE");
18311 	default:
18312 		return ("UNKNOWN");
18313 	}
18314 }
18315 
18316 void
ill_nic_event_dispatch(ill_t * ill,lif_if_t lif,nic_event_t event,nic_event_data_t data,size_t datalen)18317 ill_nic_event_dispatch(ill_t *ill, lif_if_t lif, nic_event_t event,
18318     nic_event_data_t data, size_t datalen)
18319 {
18320 	ip_stack_t		*ipst = ill->ill_ipst;
18321 	hook_nic_event_int_t	*info;
18322 	const char		*str = NULL;
18323 
18324 	/* create a new nic event info */
18325 	if ((info = kmem_alloc(sizeof (*info), KM_NOSLEEP)) == NULL)
18326 		goto fail;
18327 
18328 	info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
18329 	info->hnei_event.hne_lif = lif;
18330 	info->hnei_event.hne_event = event;
18331 	info->hnei_event.hne_protocol = ill->ill_isv6 ?
18332 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
18333 	info->hnei_event.hne_data = NULL;
18334 	info->hnei_event.hne_datalen = 0;
18335 	info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
18336 
18337 	if (data != NULL && datalen != 0) {
18338 		info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
18339 		if (info->hnei_event.hne_data == NULL)
18340 			goto fail;
18341 		bcopy(data, info->hnei_event.hne_data, datalen);
18342 		info->hnei_event.hne_datalen = datalen;
18343 	}
18344 
18345 	if (ddi_taskq_dispatch(eventq_queue_nic, ip_ne_queue_func, info,
18346 	    DDI_NOSLEEP) == DDI_SUCCESS)
18347 		return;
18348 
18349 fail:
18350 	if (info != NULL) {
18351 		if (info->hnei_event.hne_data != NULL) {
18352 			kmem_free(info->hnei_event.hne_data,
18353 			    info->hnei_event.hne_datalen);
18354 		}
18355 		kmem_free(info, sizeof (hook_nic_event_t));
18356 	}
18357 	str = ill_hook_event2str(event);
18358 	ip2dbg(("ill_nic_event_dispatch: could not dispatch %s nic event "
18359 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
18360 }
18361 
18362 static int
ipif_arp_up_done_tail(ipif_t * ipif,enum ip_resolver_action res_act)18363 ipif_arp_up_done_tail(ipif_t *ipif, enum ip_resolver_action res_act)
18364 {
18365 	int		err = 0;
18366 	const in_addr_t	*addr = NULL;
18367 	nce_t		*nce = NULL;
18368 	ill_t		*ill = ipif->ipif_ill;
18369 	ill_t		*bound_ill;
18370 	boolean_t	added_ipif = B_FALSE;
18371 	uint16_t	state;
18372 	uint16_t	flags;
18373 
18374 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up_done_tail",
18375 	    ill_t *, ill, ipif_t *, ipif);
18376 	if (ipif->ipif_lcl_addr != INADDR_ANY) {
18377 		addr = &ipif->ipif_lcl_addr;
18378 	}
18379 
18380 	if ((ipif->ipif_flags & IPIF_UNNUMBERED) || addr == NULL) {
18381 		if (res_act != Res_act_initial)
18382 			return (EINVAL);
18383 	}
18384 
18385 	if (addr != NULL) {
18386 		ipmp_illgrp_t	*illg = ill->ill_grp;
18387 
18388 		/* add unicast nce for the local addr */
18389 
18390 		if (IS_IPMP(ill)) {
18391 			/*
18392 			 * If we're here via ipif_up(), then the ipif
18393 			 * won't be bound yet -- add it to the group,
18394 			 * which will bind it if possible. (We would
18395 			 * add it in ipif_up(), but deleting on failure
18396 			 * there is gruesome.)  If we're here via
18397 			 * ipmp_ill_bind_ipif(), then the ipif has
18398 			 * already been added to the group and we
18399 			 * just need to use the binding.
18400 			 */
18401 			if ((bound_ill = ipmp_ipif_bound_ill(ipif)) == NULL) {
18402 				bound_ill  = ipmp_illgrp_add_ipif(illg, ipif);
18403 				if (bound_ill == NULL) {
18404 					/*
18405 					 * We couldn't bind the ipif to an ill
18406 					 * yet, so we have nothing to publish.
18407 					 * Mark the address as ready and return.
18408 					 */
18409 					ipif->ipif_addr_ready = 1;
18410 					return (0);
18411 				}
18412 				added_ipif = B_TRUE;
18413 			}
18414 		} else {
18415 			bound_ill = ill;
18416 		}
18417 
18418 		flags = (NCE_F_MYADDR | NCE_F_PUBLISH | NCE_F_AUTHORITY |
18419 		    NCE_F_NONUD);
18420 		/*
18421 		 * If this is an initial bring-up (or the ipif was never
18422 		 * completely brought up), do DAD.  Otherwise, we're here
18423 		 * because IPMP has rebound an address to this ill: send
18424 		 * unsolicited advertisements (ARP announcements) to
18425 		 * inform others.
18426 		 */
18427 		if (res_act == Res_act_initial || !ipif->ipif_addr_ready) {
18428 			state = ND_UNCHANGED; /* compute in nce_add_common() */
18429 		} else {
18430 			state = ND_REACHABLE;
18431 			flags |= NCE_F_UNSOL_ADV;
18432 		}
18433 
18434 retry:
18435 		err = nce_lookup_then_add_v4(ill,
18436 		    bound_ill->ill_phys_addr, bound_ill->ill_phys_addr_length,
18437 		    addr, flags, state, &nce);
18438 
18439 		/*
18440 		 * note that we may encounter EEXIST if we are moving
18441 		 * the nce as a result of a rebind operation.
18442 		 */
18443 		switch (err) {
18444 		case 0:
18445 			ipif->ipif_added_nce = 1;
18446 			nce->nce_ipif_cnt++;
18447 			break;
18448 		case EEXIST:
18449 			ip1dbg(("ipif_arp_up: NCE already exists for %s\n",
18450 			    ill->ill_name));
18451 			if (!NCE_MYADDR(nce->nce_common)) {
18452 				/*
18453 				 * A leftover nce from before this address
18454 				 * existed
18455 				 */
18456 				ncec_delete(nce->nce_common);
18457 				nce_refrele(nce);
18458 				nce = NULL;
18459 				goto retry;
18460 			}
18461 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
18462 				nce_refrele(nce);
18463 				nce = NULL;
18464 				ip1dbg(("ipif_arp_up: NCE already exists "
18465 				    "for %s:%u\n", ill->ill_name,
18466 				    ipif->ipif_id));
18467 				goto arp_up_done;
18468 			}
18469 			/*
18470 			 * Duplicate local addresses are permissible for
18471 			 * IPIF_POINTOPOINT interfaces which will get marked
18472 			 * IPIF_UNNUMBERED later in
18473 			 * ip_addr_availability_check().
18474 			 *
18475 			 * The nce_ipif_cnt field tracks the number of
18476 			 * ipifs that have nce_addr as their local address.
18477 			 */
18478 			ipif->ipif_addr_ready = 1;
18479 			ipif->ipif_added_nce = 1;
18480 			nce->nce_ipif_cnt++;
18481 			err = 0;
18482 			break;
18483 		default:
18484 			ASSERT(nce == NULL);
18485 			goto arp_up_done;
18486 		}
18487 		if (arp_no_defense) {
18488 			if ((ipif->ipif_flags & IPIF_UP) &&
18489 			    !ipif->ipif_addr_ready)
18490 				ipif_up_notify(ipif);
18491 			ipif->ipif_addr_ready = 1;
18492 		}
18493 	} else {
18494 		/* zero address. nothing to publish */
18495 		ipif->ipif_addr_ready = 1;
18496 	}
18497 	if (nce != NULL)
18498 		nce_refrele(nce);
18499 arp_up_done:
18500 	if (added_ipif && err != 0)
18501 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
18502 	return (err);
18503 }
18504 
18505 int
ipif_arp_up(ipif_t * ipif,enum ip_resolver_action res_act,boolean_t was_dup)18506 ipif_arp_up(ipif_t *ipif, enum ip_resolver_action res_act, boolean_t was_dup)
18507 {
18508 	int		err = 0;
18509 	ill_t		*ill = ipif->ipif_ill;
18510 	boolean_t	first_interface, wait_for_dlpi = B_FALSE;
18511 
18512 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up",
18513 	    ill_t *, ill, ipif_t *, ipif);
18514 
18515 	/*
18516 	 * need to bring up ARP or setup mcast mapping only
18517 	 * when the first interface is coming UP.
18518 	 */
18519 	first_interface = (ill->ill_ipif_up_count == 0 &&
18520 	    ill->ill_ipif_dup_count == 0 && !was_dup);
18521 
18522 	if (res_act == Res_act_initial && first_interface) {
18523 		/*
18524 		 * Send ATTACH + BIND
18525 		 */
18526 		err = arp_ll_up(ill);
18527 		if (err != EINPROGRESS && err != 0)
18528 			return (err);
18529 
18530 		/*
18531 		 * Add NCE for local address. Start DAD.
18532 		 * we'll wait to hear that DAD has finished
18533 		 * before using the interface.
18534 		 */
18535 		if (err == EINPROGRESS)
18536 			wait_for_dlpi = B_TRUE;
18537 	}
18538 
18539 	if (!wait_for_dlpi)
18540 		(void) ipif_arp_up_done_tail(ipif, res_act);
18541 
18542 	return (!wait_for_dlpi ? 0 : EINPROGRESS);
18543 }
18544 
18545 /*
18546  * Finish processing of "arp_up" after all the DLPI message
18547  * exchanges have completed between arp and the driver.
18548  */
18549 void
arp_bringup_done(ill_t * ill,int err)18550 arp_bringup_done(ill_t *ill, int err)
18551 {
18552 	mblk_t	*mp1;
18553 	ipif_t  *ipif;
18554 	conn_t *connp = NULL;
18555 	ipsq_t	*ipsq;
18556 	queue_t *q;
18557 
18558 	ip1dbg(("arp_bringup_done(%s)\n", ill->ill_name));
18559 
18560 	ASSERT(IAM_WRITER_ILL(ill));
18561 
18562 	ipsq = ill->ill_phyint->phyint_ipsq;
18563 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
18564 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
18565 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
18566 	if (mp1 == NULL) /* bringup was aborted by the user */
18567 		return;
18568 
18569 	/*
18570 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
18571 	 * must have an associated conn_t.  Otherwise, we're bringing this
18572 	 * interface back up as part of handling an asynchronous event (e.g.,
18573 	 * physical address change).
18574 	 */
18575 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18576 		ASSERT(connp != NULL);
18577 		q = CONNP_TO_WQ(connp);
18578 	} else {
18579 		ASSERT(connp == NULL);
18580 		q = ill->ill_rq;
18581 	}
18582 	if (err == 0) {
18583 		if (ipif->ipif_isv6) {
18584 			if ((err = ipif_up_done_v6(ipif)) != 0)
18585 				ip0dbg(("arp_bringup_done: init failed\n"));
18586 		} else {
18587 			err = ipif_arp_up_done_tail(ipif, Res_act_initial);
18588 			if (err != 0 ||
18589 			    (err = ipif_up_done(ipif)) != 0) {
18590 				ip0dbg(("arp_bringup_done: "
18591 				    "init failed err %x\n", err));
18592 				(void) ipif_arp_down(ipif);
18593 			}
18594 
18595 		}
18596 	} else {
18597 		ip0dbg(("arp_bringup_done: DL_BIND_REQ failed\n"));
18598 	}
18599 
18600 	if ((err == 0) && (ill->ill_up_ipifs)) {
18601 		err = ill_up_ipifs(ill, q, mp1);
18602 		if (err == EINPROGRESS)
18603 			return;
18604 	}
18605 
18606 	/*
18607 	 * If we have a moved ipif to bring up, and everything has succeeded
18608 	 * to this point, bring it up on the IPMP ill.  Otherwise, leave it
18609 	 * down -- the admin can try to bring it up by hand if need be.
18610 	 */
18611 	if (ill->ill_move_ipif != NULL) {
18612 		ipif = ill->ill_move_ipif;
18613 		ip1dbg(("bringing up ipif %p on ill %s\n", (void *)ipif,
18614 		    ipif->ipif_ill->ill_name));
18615 		ill->ill_move_ipif = NULL;
18616 		if (err == 0) {
18617 			err = ipif_up(ipif, q, mp1);
18618 			if (err == EINPROGRESS)
18619 				return;
18620 		}
18621 	}
18622 
18623 	/*
18624 	 * The operation must complete without EINPROGRESS since
18625 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
18626 	 * Otherwise, the operation will be stuck forever in the ipsq.
18627 	 */
18628 	ASSERT(err != EINPROGRESS);
18629 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18630 		DTRACE_PROBE4(ipif__ioctl, char *, "arp_bringup_done finish",
18631 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
18632 		    ill_t *, ill, ipif_t *, ipif);
18633 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
18634 	} else {
18635 		ipsq_current_finish(ipsq);
18636 	}
18637 }
18638 
18639 /*
18640  * Finish processing of arp replumb after all the DLPI message
18641  * exchanges have completed between arp and the driver.
18642  */
18643 void
arp_replumb_done(ill_t * ill,int err)18644 arp_replumb_done(ill_t *ill, int err)
18645 {
18646 	mblk_t	*mp1;
18647 	ipif_t  *ipif;
18648 	conn_t *connp = NULL;
18649 	ipsq_t	*ipsq;
18650 	queue_t *q;
18651 
18652 	ASSERT(IAM_WRITER_ILL(ill));
18653 
18654 	ipsq = ill->ill_phyint->phyint_ipsq;
18655 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
18656 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
18657 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
18658 	if (mp1 == NULL) {
18659 		ip0dbg(("arp_replumb_done: bringup aborted ioctl %x\n",
18660 		    ipsq->ipsq_xop->ipx_current_ioctl));
18661 		/* bringup was aborted by the user */
18662 		return;
18663 	}
18664 	/*
18665 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
18666 	 * must have an associated conn_t.  Otherwise, we're bringing this
18667 	 * interface back up as part of handling an asynchronous event (e.g.,
18668 	 * physical address change).
18669 	 */
18670 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18671 		ASSERT(connp != NULL);
18672 		q = CONNP_TO_WQ(connp);
18673 	} else {
18674 		ASSERT(connp == NULL);
18675 		q = ill->ill_rq;
18676 	}
18677 	if ((err == 0) && (ill->ill_up_ipifs)) {
18678 		err = ill_up_ipifs(ill, q, mp1);
18679 		if (err == EINPROGRESS)
18680 			return;
18681 	}
18682 	/*
18683 	 * The operation must complete without EINPROGRESS since
18684 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
18685 	 * Otherwise, the operation will be stuck forever in the ipsq.
18686 	 */
18687 	ASSERT(err != EINPROGRESS);
18688 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18689 		DTRACE_PROBE4(ipif__ioctl, char *,
18690 		    "arp_replumb_done finish",
18691 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
18692 		    ill_t *, ill, ipif_t *, ipif);
18693 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
18694 	} else {
18695 		ipsq_current_finish(ipsq);
18696 	}
18697 }
18698 
18699 void
ipif_up_notify(ipif_t * ipif)18700 ipif_up_notify(ipif_t *ipif)
18701 {
18702 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
18703 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif, RTSQ_DEFAULT);
18704 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
18705 	ill_nic_event_dispatch(ipif->ipif_ill, MAP_IPIF_ID(ipif->ipif_id),
18706 	    NE_LIF_UP, NULL, 0);
18707 }
18708 
18709 /*
18710  * ILB ioctl uses cv_wait (such as deleting a rule or adding a server) and
18711  * this assumes the context is cv_wait'able.  Hence it shouldnt' be used on
18712  * TPI end points with STREAMS modules pushed above.  This is assured by not
18713  * having the IPI_MODOK flag for the ioctl.  And IP ensures the ILB ioctl
18714  * never ends up on an ipsq, otherwise we may end up processing the ioctl
18715  * while unwinding from the ispq and that could be a thread from the bottom.
18716  */
18717 /* ARGSUSED */
18718 int
ip_sioctl_ilb_cmd(ipif_t * ipif,sin_t * sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * arg)18719 ip_sioctl_ilb_cmd(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18720     ip_ioctl_cmd_t *ipip, void *arg)
18721 {
18722 	mblk_t *cmd_mp = mp->b_cont->b_cont;
18723 	ilb_cmd_t command = *((ilb_cmd_t *)cmd_mp->b_rptr);
18724 	int ret = 0;
18725 	int i;
18726 	size_t size;
18727 	ip_stack_t *ipst;
18728 	zoneid_t zoneid;
18729 	ilb_stack_t *ilbs;
18730 
18731 	ipst = CONNQ_TO_IPST(q);
18732 	ilbs = ipst->ips_netstack->netstack_ilb;
18733 	zoneid = Q_TO_CONN(q)->conn_zoneid;
18734 
18735 	switch (command) {
18736 	case ILB_CREATE_RULE: {
18737 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18738 
18739 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18740 			ret = EINVAL;
18741 			break;
18742 		}
18743 
18744 		ret = ilb_rule_add(ilbs, zoneid, cmd);
18745 		break;
18746 	}
18747 	case ILB_DESTROY_RULE:
18748 	case ILB_ENABLE_RULE:
18749 	case ILB_DISABLE_RULE: {
18750 		ilb_name_cmd_t *cmd = (ilb_name_cmd_t *)cmd_mp->b_rptr;
18751 
18752 		if (MBLKL(cmd_mp) != sizeof (ilb_name_cmd_t)) {
18753 			ret = EINVAL;
18754 			break;
18755 		}
18756 
18757 		if (cmd->flags & ILB_RULE_ALLRULES) {
18758 			if (command == ILB_DESTROY_RULE) {
18759 				ilb_rule_del_all(ilbs, zoneid);
18760 				break;
18761 			} else if (command == ILB_ENABLE_RULE) {
18762 				ilb_rule_enable_all(ilbs, zoneid);
18763 				break;
18764 			} else if (command == ILB_DISABLE_RULE) {
18765 				ilb_rule_disable_all(ilbs, zoneid);
18766 				break;
18767 			}
18768 		} else {
18769 			if (command == ILB_DESTROY_RULE) {
18770 				ret = ilb_rule_del(ilbs, zoneid, cmd->name);
18771 			} else if (command == ILB_ENABLE_RULE) {
18772 				ret = ilb_rule_enable(ilbs, zoneid, cmd->name,
18773 				    NULL);
18774 			} else if (command == ILB_DISABLE_RULE) {
18775 				ret = ilb_rule_disable(ilbs, zoneid, cmd->name,
18776 				    NULL);
18777 			}
18778 		}
18779 		break;
18780 	}
18781 	case ILB_NUM_RULES: {
18782 		ilb_num_rules_cmd_t *cmd;
18783 
18784 		if (MBLKL(cmd_mp) != sizeof (ilb_num_rules_cmd_t)) {
18785 			ret = EINVAL;
18786 			break;
18787 		}
18788 		cmd = (ilb_num_rules_cmd_t *)cmd_mp->b_rptr;
18789 		ilb_get_num_rules(ilbs, zoneid, &(cmd->num));
18790 		break;
18791 	}
18792 	case ILB_RULE_NAMES: {
18793 		ilb_rule_names_cmd_t *cmd;
18794 
18795 		cmd = (ilb_rule_names_cmd_t *)cmd_mp->b_rptr;
18796 		if (MBLKL(cmd_mp) < sizeof (ilb_rule_names_cmd_t) ||
18797 		    cmd->num_names == 0) {
18798 			ret = EINVAL;
18799 			break;
18800 		}
18801 		size = cmd->num_names * ILB_RULE_NAMESZ;
18802 		if (cmd_mp->b_rptr + offsetof(ilb_rule_names_cmd_t, buf) +
18803 		    size != cmd_mp->b_wptr) {
18804 			ret = EINVAL;
18805 			break;
18806 		}
18807 		ilb_get_rulenames(ilbs, zoneid, &cmd->num_names, cmd->buf);
18808 		break;
18809 	}
18810 	case ILB_NUM_SERVERS: {
18811 		ilb_num_servers_cmd_t *cmd;
18812 
18813 		if (MBLKL(cmd_mp) != sizeof (ilb_num_servers_cmd_t)) {
18814 			ret = EINVAL;
18815 			break;
18816 		}
18817 		cmd = (ilb_num_servers_cmd_t *)cmd_mp->b_rptr;
18818 		ret = ilb_get_num_servers(ilbs, zoneid, cmd->name,
18819 		    &(cmd->num));
18820 		break;
18821 	}
18822 	case ILB_LIST_RULE: {
18823 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18824 
18825 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18826 			ret = EINVAL;
18827 			break;
18828 		}
18829 		ret = ilb_rule_list(ilbs, zoneid, cmd);
18830 		break;
18831 	}
18832 	case ILB_LIST_SERVERS: {
18833 		ilb_servers_info_cmd_t *cmd;
18834 
18835 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18836 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t) ||
18837 		    cmd->num_servers == 0) {
18838 			ret = EINVAL;
18839 			break;
18840 		}
18841 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18842 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18843 		    size != cmd_mp->b_wptr) {
18844 			ret = EINVAL;
18845 			break;
18846 		}
18847 
18848 		ret = ilb_get_servers(ilbs, zoneid, cmd->name, cmd->servers,
18849 		    &cmd->num_servers);
18850 		break;
18851 	}
18852 	case ILB_ADD_SERVERS: {
18853 		ilb_servers_info_cmd_t *cmd;
18854 		ilb_rule_t *rule;
18855 
18856 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18857 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t)) {
18858 			ret = EINVAL;
18859 			break;
18860 		}
18861 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18862 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18863 		    size != cmd_mp->b_wptr) {
18864 			ret = EINVAL;
18865 			break;
18866 		}
18867 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18868 		if (rule == NULL) {
18869 			ASSERT(ret != 0);
18870 			break;
18871 		}
18872 		for (i = 0; i < cmd->num_servers; i++) {
18873 			ilb_server_info_t *s;
18874 
18875 			s = &cmd->servers[i];
18876 			s->err = ilb_server_add(ilbs, rule, s);
18877 		}
18878 		ILB_RULE_REFRELE(rule);
18879 		break;
18880 	}
18881 	case ILB_DEL_SERVERS:
18882 	case ILB_ENABLE_SERVERS:
18883 	case ILB_DISABLE_SERVERS: {
18884 		ilb_servers_cmd_t *cmd;
18885 		ilb_rule_t *rule;
18886 		int (*f)();
18887 
18888 		cmd = (ilb_servers_cmd_t *)cmd_mp->b_rptr;
18889 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_cmd_t)) {
18890 			ret = EINVAL;
18891 			break;
18892 		}
18893 		size = cmd->num_servers * sizeof (ilb_server_arg_t);
18894 		if (cmd_mp->b_rptr + offsetof(ilb_servers_cmd_t, servers) +
18895 		    size != cmd_mp->b_wptr) {
18896 			ret = EINVAL;
18897 			break;
18898 		}
18899 
18900 		if (command == ILB_DEL_SERVERS)
18901 			f = ilb_server_del;
18902 		else if (command == ILB_ENABLE_SERVERS)
18903 			f = ilb_server_enable;
18904 		else if (command == ILB_DISABLE_SERVERS)
18905 			f = ilb_server_disable;
18906 
18907 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18908 		if (rule == NULL) {
18909 			ASSERT(ret != 0);
18910 			break;
18911 		}
18912 
18913 		for (i = 0; i < cmd->num_servers; i++) {
18914 			ilb_server_arg_t *s;
18915 
18916 			s = &cmd->servers[i];
18917 			s->err = f(ilbs, zoneid, NULL, rule, &s->addr);
18918 		}
18919 		ILB_RULE_REFRELE(rule);
18920 		break;
18921 	}
18922 	case ILB_LIST_NAT_TABLE: {
18923 		ilb_list_nat_cmd_t *cmd;
18924 
18925 		cmd = (ilb_list_nat_cmd_t *)cmd_mp->b_rptr;
18926 		if (MBLKL(cmd_mp) < sizeof (ilb_list_nat_cmd_t)) {
18927 			ret = EINVAL;
18928 			break;
18929 		}
18930 		size = cmd->num_nat * sizeof (ilb_nat_entry_t);
18931 		if (cmd_mp->b_rptr + offsetof(ilb_list_nat_cmd_t, entries) +
18932 		    size != cmd_mp->b_wptr) {
18933 			ret = EINVAL;
18934 			break;
18935 		}
18936 
18937 		ret = ilb_list_nat(ilbs, zoneid, cmd->entries, &cmd->num_nat,
18938 		    &cmd->flags);
18939 		break;
18940 	}
18941 	case ILB_LIST_STICKY_TABLE: {
18942 		ilb_list_sticky_cmd_t *cmd;
18943 
18944 		cmd = (ilb_list_sticky_cmd_t *)cmd_mp->b_rptr;
18945 		if (MBLKL(cmd_mp) < sizeof (ilb_list_sticky_cmd_t)) {
18946 			ret = EINVAL;
18947 			break;
18948 		}
18949 		size = cmd->num_sticky * sizeof (ilb_sticky_entry_t);
18950 		if (cmd_mp->b_rptr + offsetof(ilb_list_sticky_cmd_t, entries) +
18951 		    size != cmd_mp->b_wptr) {
18952 			ret = EINVAL;
18953 			break;
18954 		}
18955 
18956 		ret = ilb_list_sticky(ilbs, zoneid, cmd->entries,
18957 		    &cmd->num_sticky, &cmd->flags);
18958 		break;
18959 	}
18960 	default:
18961 		ret = EINVAL;
18962 		break;
18963 	}
18964 done:
18965 	return (ret);
18966 }
18967 
18968 /* Remove all cache entries for this logical interface */
18969 void
ipif_nce_down(ipif_t * ipif)18970 ipif_nce_down(ipif_t *ipif)
18971 {
18972 	ill_t *ill = ipif->ipif_ill;
18973 	nce_t *nce;
18974 
18975 	DTRACE_PROBE3(ipif__downup, char *, "ipif_nce_down",
18976 	    ill_t *, ill, ipif_t *, ipif);
18977 	if (ipif->ipif_added_nce) {
18978 		if (ipif->ipif_isv6)
18979 			nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
18980 		else
18981 			nce = nce_lookup_v4(ill, &ipif->ipif_lcl_addr);
18982 		if (nce != NULL) {
18983 			if (--nce->nce_ipif_cnt == 0)
18984 				ncec_delete(nce->nce_common);
18985 			ipif->ipif_added_nce = 0;
18986 			nce_refrele(nce);
18987 		} else {
18988 			/*
18989 			 * nce may already be NULL because it was already
18990 			 * flushed, e.g., due to a call to nce_flush
18991 			 */
18992 			ipif->ipif_added_nce = 0;
18993 		}
18994 	}
18995 	/*
18996 	 * Make IPMP aware of the deleted data address.
18997 	 */
18998 	if (IS_IPMP(ill))
18999 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
19000 
19001 	/*
19002 	 * Remove all other nces dependent on this ill when the last ipif
19003 	 * is going away.
19004 	 */
19005 	if (ill->ill_ipif_up_count == 0) {
19006 		ncec_walk(ill, ncec_delete_per_ill, ill, ill->ill_ipst);
19007 		if (IS_UNDER_IPMP(ill))
19008 			nce_flush(ill, B_TRUE);
19009 	}
19010 }
19011 
19012 /*
19013  * find the first interface that uses usill for its source address.
19014  */
19015 ill_t *
ill_lookup_usesrc(ill_t * usill)19016 ill_lookup_usesrc(ill_t *usill)
19017 {
19018 	ip_stack_t *ipst = usill->ill_ipst;
19019 	ill_t *ill;
19020 
19021 	ASSERT(usill != NULL);
19022 
19023 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
19024 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
19025 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
19026 	for (ill = usill->ill_usesrc_grp_next; ill != NULL && ill != usill;
19027 	    ill = ill->ill_usesrc_grp_next) {
19028 		if (!IS_UNDER_IPMP(ill) && (ill->ill_flags & ILLF_MULTICAST) &&
19029 		    !ILL_IS_CONDEMNED(ill)) {
19030 			ill_refhold(ill);
19031 			break;
19032 		}
19033 	}
19034 	rw_exit(&ipst->ips_ill_g_lock);
19035 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
19036 	return (ill);
19037 }
19038 
19039 /*
19040  * This comment applies to both ip_sioctl_get_ifhwaddr and
19041  * ip_sioctl_get_lifhwaddr as the basic function of these two functions
19042  * is the same.
19043  *
19044  * The goal here is to find an IP interface that corresponds to the name
19045  * provided by the caller in the ifreq/lifreq structure held in the mblk_t
19046  * chain and to fill out a sockaddr/sockaddr_storage structure with the
19047  * mac address.
19048  *
19049  * The SIOCGIFHWADDR/SIOCGLIFHWADDR ioctl may return an error for a number
19050  * of different reasons:
19051  * ENXIO - the device name is not known to IP.
19052  * EADDRNOTAVAIL - the device has no hardware address. This is indicated
19053  * by ill_phys_addr not pointing to an actual address.
19054  * EPFNOSUPPORT - this will indicate that a request is being made for a
19055  * mac address that will not fit in the data structure supplier (struct
19056  * sockaddr).
19057  *
19058  */
19059 /* ARGSUSED */
19060 int
ip_sioctl_get_ifhwaddr(ipif_t * ipif,sin_t * dummy_sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * if_req)19061 ip_sioctl_get_ifhwaddr(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
19062     ip_ioctl_cmd_t *ipip, void *if_req)
19063 {
19064 	struct sockaddr *sock;
19065 	struct ifreq *ifr;
19066 	mblk_t *mp1;
19067 	ill_t *ill;
19068 
19069 	ASSERT(ipif != NULL);
19070 	ill = ipif->ipif_ill;
19071 
19072 	if (ill->ill_phys_addr == NULL) {
19073 		return (EADDRNOTAVAIL);
19074 	}
19075 	if (ill->ill_phys_addr_length > sizeof (sock->sa_data)) {
19076 		return (EPFNOSUPPORT);
19077 	}
19078 
19079 	ip1dbg(("ip_sioctl_get_hwaddr(%s)\n", ill->ill_name));
19080 
19081 	/* Existence of mp1 has been checked in ip_wput_nondata */
19082 	mp1 = mp->b_cont->b_cont;
19083 	ifr = (struct ifreq *)mp1->b_rptr;
19084 
19085 	sock = &ifr->ifr_addr;
19086 	/*
19087 	 * The "family" field in the returned structure is set to a value
19088 	 * that represents the type of device to which the address belongs.
19089 	 * The value returned may differ to that on Linux but it will still
19090 	 * represent the correct symbol on Solaris.
19091 	 */
19092 	sock->sa_family = arp_hw_type(ill->ill_mactype);
19093 	bcopy(ill->ill_phys_addr, &sock->sa_data, ill->ill_phys_addr_length);
19094 
19095 	return (0);
19096 }
19097 
19098 /*
19099  * The expection of applications using SIOCGIFHWADDR is that data will
19100  * be returned in the sa_data field of the sockaddr structure. With
19101  * SIOCGLIFHWADDR, we're breaking new ground as there is no Linux
19102  * equivalent. In light of this, struct sockaddr_dl is used as it
19103  * offers more space for address storage in sll_data.
19104  */
19105 /* ARGSUSED */
19106 int
ip_sioctl_get_lifhwaddr(ipif_t * ipif,sin_t * dummy_sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * if_req)19107 ip_sioctl_get_lifhwaddr(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
19108     ip_ioctl_cmd_t *ipip, void *if_req)
19109 {
19110 	struct sockaddr_dl *sock;
19111 	struct lifreq *lifr;
19112 	mblk_t *mp1;
19113 	ill_t *ill;
19114 
19115 	ASSERT(ipif != NULL);
19116 	ill = ipif->ipif_ill;
19117 
19118 	if (ill->ill_phys_addr == NULL) {
19119 		return (EADDRNOTAVAIL);
19120 	}
19121 	if (ill->ill_phys_addr_length > sizeof (sock->sdl_data)) {
19122 		return (EPFNOSUPPORT);
19123 	}
19124 
19125 	ip1dbg(("ip_sioctl_get_lifhwaddr(%s)\n", ill->ill_name));
19126 
19127 	/* Existence of mp1 has been checked in ip_wput_nondata */
19128 	mp1 = mp->b_cont->b_cont;
19129 	lifr = (struct lifreq *)mp1->b_rptr;
19130 
19131 	/*
19132 	 * sockaddr_ll is used here because it is also the structure used in
19133 	 * responding to the same ioctl in sockpfp. The only other choice is
19134 	 * sockaddr_dl which contains fields that are not required here
19135 	 * because its purpose is different.
19136 	 */
19137 	lifr->lifr_type = ill->ill_type;
19138 	sock = (struct sockaddr_dl *)&lifr->lifr_addr;
19139 	sock->sdl_family = AF_LINK;
19140 	sock->sdl_index = ill->ill_phyint->phyint_ifindex;
19141 	sock->sdl_type = ill->ill_mactype;
19142 	sock->sdl_nlen = 0;
19143 	sock->sdl_slen = 0;
19144 	sock->sdl_alen = ill->ill_phys_addr_length;
19145 	bcopy(ill->ill_phys_addr, sock->sdl_data, ill->ill_phys_addr_length);
19146 
19147 	return (0);
19148 }
19149