kill tsol ("Trusted Solaris") aka TX ("Trusted Extensions")
[unleashed.git] / kernel / net / ip / ip_if.c
blobbb66975483fccf4032fd8e872ed517811ce09a1e
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
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.
30 * This file contains the interface control functions for IP.
33 #include <sys/types.h>
34 #include <sys/stream.h>
35 #include <sys/dlpi.h>
36 #include <sys/stropts.h>
37 #include <sys/strsun.h>
38 #include <sys/sysmacros.h>
39 #include <sys/strsubr.h>
40 #include <sys/strlog.h>
41 #include <sys/ddi.h>
42 #include <sys/sunddi.h>
43 #include <sys/cmn_err.h>
44 #include <sys/kstat.h>
45 #include <sys/debug.h>
46 #include <sys/zone.h>
47 #include <sys/sunldi.h>
48 #include <sys/file.h>
49 #include <sys/bitmap.h>
50 #include <sys/cpuvar.h>
51 #include <sys/time.h>
52 #include <sys/ctype.h>
53 #include <sys/kmem.h>
54 #include <sys/systm.h>
55 #include <sys/param.h>
56 #include <sys/socket.h>
57 #include <sys/isa_defs.h>
58 #include <net/if.h>
59 #include <net/if_arp.h>
60 #include <net/if_types.h>
61 #include <net/if_dl.h>
62 #include <net/route.h>
63 #include <sys/sockio.h>
64 #include <netinet/in.h>
65 #include <netinet/ip6.h>
66 #include <netinet/icmp6.h>
67 #include <netinet/igmp_var.h>
68 #include <sys/policy.h>
69 #include <sys/ethernet.h>
70 #include <sys/callb.h>
71 #include <sys/md5.h>
73 #include <inet/common.h> /* for various inet/mi.h and inet/nd.h needs */
74 #include <inet/mi.h>
75 #include <inet/nd.h>
76 #include <inet/tunables.h>
77 #include <inet/arp.h>
78 #include <inet/ip_arp.h>
79 #include <inet/mib2.h>
80 #include <inet/ip.h>
81 #include <inet/ip6.h>
82 #include <inet/ip6_asp.h>
83 #include <inet/tcp.h>
84 #include <inet/ip_multi.h>
85 #include <inet/ip_ire.h>
86 #include <inet/ip_ftable.h>
87 #include <inet/ip_rts.h>
88 #include <inet/ip_ndp.h>
89 #include <inet/ip_if.h>
90 #include <inet/ip_impl.h>
91 #include <inet/sctp_ip.h>
92 #include <inet/ip_netinfo.h>
93 #include <inet/ilb_ip.h>
95 #include <netinet/igmp.h>
96 #include <inet/ip_listutils.h>
97 #include <inet/ipclassifier.h>
98 #include <sys/mac_client.h>
99 #include <sys/dld.h>
100 #include <sys/mac_flow.h>
102 #include <sys/systeminfo.h>
103 #include <sys/bootconf.h>
105 #include <inet/rawip_impl.h> /* needed for icmp_stack_t */
106 #include <inet/udp_impl.h> /* needed for udp_stack_t */
108 /* The character which tells where the ill_name ends */
109 #define IPIF_SEPARATOR_CHAR ':'
111 /* IP ioctl function table entry */
112 typedef struct ipft_s {
113 int ipft_cmd;
114 pfi_t ipft_pfi;
115 int ipft_min_size;
116 int ipft_flags;
117 } ipft_t;
118 #define IPFT_F_NO_REPLY 0x1 /* IP ioctl does not expect any reply */
119 #define IPFT_F_SELF_REPLY 0x2 /* ioctl callee does the ioctl reply */
121 static int nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
122 static int nd_ill_forward_set(queue_t *q, mblk_t *mp,
123 char *value, caddr_t cp, cred_t *ioc_cr);
125 static boolean_t ill_is_quiescent(ill_t *);
126 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
127 static ip_m_t *ip_m_lookup(t_uscalar_t mac_type);
128 static int ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
129 mblk_t *mp, boolean_t need_up);
130 static int ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
131 mblk_t *mp, boolean_t need_up);
132 static int ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
133 queue_t *q, mblk_t *mp, boolean_t need_up);
134 static int ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
135 mblk_t *mp);
136 static int ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
137 mblk_t *mp);
138 static int ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
139 queue_t *q, mblk_t *mp, boolean_t need_up);
140 static int ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
141 int ioccmd, struct linkblk *li);
142 static ipaddr_t ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
143 static void ip_wput_ioctl(queue_t *q, mblk_t *mp);
144 static void ipsq_flush(ill_t *ill);
146 static int ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
147 queue_t *q, mblk_t *mp, boolean_t need_up);
148 static void ipsq_delete(ipsq_t *);
150 static ipif_t *ipif_allocate(ill_t *ill, int id, uint_t ire_type,
151 boolean_t initialize, boolean_t insert, int *errorp);
152 static ire_t **ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
153 static void ipif_delete_bcast_ires(ipif_t *ipif);
154 static int ipif_add_ires_v4(ipif_t *, boolean_t);
155 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
156 boolean_t isv6);
157 static int ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
158 static void ipif_free(ipif_t *ipif);
159 static void ipif_free_tail(ipif_t *ipif);
160 static void ipif_set_default(ipif_t *ipif);
161 static int ipif_set_values(queue_t *q, mblk_t *mp,
162 char *interf_name, uint_t *ppa);
163 static int ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
164 queue_t *q);
165 static ipif_t *ipif_lookup_on_name(char *name, size_t namelen,
166 boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
167 ip_stack_t *);
168 static ipif_t *ipif_lookup_on_name_async(char *name, size_t namelen,
169 boolean_t isv6, zoneid_t zoneid, queue_t *q, mblk_t *mp, ipsq_func_t func,
170 int *error, ip_stack_t *);
172 static int ill_alloc_ppa(ill_if_t *, ill_t *);
173 static void ill_delete_interface_type(ill_if_t *);
174 static int ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
175 static void ill_dl_down(ill_t *ill);
176 static void ill_down(ill_t *ill);
177 static void ill_down_ipifs(ill_t *, boolean_t);
178 static void ill_free_mib(ill_t *ill);
179 static void ill_glist_delete(ill_t *);
180 static void ill_phyint_reinit(ill_t *ill);
181 static void ill_set_nce_router_flags(ill_t *, boolean_t);
182 static void ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
183 static void ill_replumb_tail(ipsq_t *, queue_t *, mblk_t *, void *);
185 static ip_v6intfid_func_t ip_ether_v6intfid, ip_ib_v6intfid;
186 static ip_v6intfid_func_t ip_ipv4_v6intfid, ip_ipv6_v6intfid;
187 static ip_v6intfid_func_t ip_ipmp_v6intfid, ip_nodef_v6intfid;
188 static ip_v6intfid_func_t ip_ipv4_v6destintfid, ip_ipv6_v6destintfid;
189 static ip_v4mapinfo_func_t ip_ether_v4_mapping;
190 static ip_v6mapinfo_func_t ip_ether_v6_mapping;
191 static ip_v4mapinfo_func_t ip_ib_v4_mapping;
192 static ip_v6mapinfo_func_t ip_ib_v6_mapping;
193 static ip_v4mapinfo_func_t ip_mbcast_mapping;
194 static void ip_cgtp_bcast_add(ire_t *, ip_stack_t *);
195 static void ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
196 static void phyint_free(phyint_t *);
198 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *);
199 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
200 static void ill_capability_vrrp_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
201 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
202 static void ill_capability_hcksum_reset_fill(ill_t *, mblk_t *);
203 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
204 dl_capability_sub_t *);
205 static void ill_capability_zerocopy_reset_fill(ill_t *, mblk_t *);
206 static void ill_capability_dld_reset_fill(ill_t *, mblk_t *);
207 static void ill_capability_dld_ack(ill_t *, mblk_t *,
208 dl_capability_sub_t *);
209 static void ill_capability_dld_enable(ill_t *);
210 static void ill_capability_ack_thr(void *);
211 static void ill_capability_lso_enable(ill_t *);
213 static ill_t *ill_prev_usesrc(ill_t *);
214 static int ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
215 static void ill_disband_usesrc_group(ill_t *);
216 static void ip_sioctl_garp_reply(mblk_t *, ill_t *, void *, int);
218 #ifdef DEBUG
219 static void ill_trace_cleanup(const ill_t *);
220 static void ipif_trace_cleanup(const ipif_t *);
221 #endif
223 static void ill_dlpi_clear_deferred(ill_t *ill);
225 static void phyint_flags_init(phyint_t *, t_uscalar_t);
228 * if we go over the memory footprint limit more than once in this msec
229 * interval, we'll start pruning aggressively.
231 int ip_min_frag_prune_time = 0;
233 static ipft_t ip_ioctl_ftbl[] = {
234 { IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
235 { IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
236 IPFT_F_NO_REPLY },
237 { IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
238 { 0 }
241 /* Simple ICMP IP Header Template */
242 static ipha_t icmp_ipha = {
243 IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
246 static uchar_t ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
248 static ip_m_t ip_m_tbl[] = {
249 { DL_ETHER, IFT_ETHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
250 ip_ether_v4_mapping, ip_ether_v6_mapping, ip_ether_v6intfid,
251 ip_nodef_v6intfid },
252 { DL_CSMACD, IFT_ISO88023, ETHERTYPE_IP, ETHERTYPE_IPV6,
253 ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
254 ip_nodef_v6intfid },
255 { DL_TPB, IFT_ISO88024, ETHERTYPE_IP, ETHERTYPE_IPV6,
256 ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
257 ip_nodef_v6intfid },
258 { DL_TPR, IFT_ISO88025, ETHERTYPE_IP, ETHERTYPE_IPV6,
259 ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
260 ip_nodef_v6intfid },
261 { DL_FDDI, IFT_FDDI, ETHERTYPE_IP, ETHERTYPE_IPV6,
262 ip_ether_v4_mapping, ip_ether_v6_mapping, ip_ether_v6intfid,
263 ip_nodef_v6intfid },
264 { DL_IB, IFT_IB, ETHERTYPE_IP, ETHERTYPE_IPV6,
265 ip_ib_v4_mapping, ip_ib_v6_mapping, ip_ib_v6intfid,
266 ip_nodef_v6intfid },
267 { DL_IPV4, IFT_IPV4, IPPROTO_ENCAP, IPPROTO_IPV6,
268 ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv4_v6intfid,
269 ip_ipv4_v6destintfid },
270 { DL_IPV6, IFT_IPV6, IPPROTO_ENCAP, IPPROTO_IPV6,
271 ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv6_v6intfid,
272 ip_ipv6_v6destintfid },
273 { DL_6TO4, IFT_6TO4, IPPROTO_ENCAP, IPPROTO_IPV6,
274 ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv4_v6intfid,
275 ip_nodef_v6intfid },
276 { SUNW_DL_VNI, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
277 NULL, NULL, ip_nodef_v6intfid, ip_nodef_v6intfid },
278 { SUNW_DL_IPMP, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
279 NULL, NULL, ip_ipmp_v6intfid, ip_nodef_v6intfid },
280 { DL_OTHER, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
281 ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
282 ip_nodef_v6intfid }
285 char ipif_loopback_name[] = "lo0";
287 /* These are used by all IP network modules. */
288 sin6_t sin6_null; /* Zero address for quick clears */
289 sin_t sin_null; /* Zero address for quick clears */
291 /* When set search for unused ipif_seqid */
292 static ipif_t ipif_zero;
295 * ppa arena is created after these many
296 * interfaces have been plumbed.
298 uint_t ill_no_arena = 12; /* Setable in /etc/system */
301 * Allocate per-interface mibs.
302 * Returns true if ok. False otherwise.
303 * ipsq may not yet be allocated (loopback case ).
305 static boolean_t
306 ill_allocate_mibs(ill_t *ill)
308 /* Already allocated? */
309 if (ill->ill_ip_mib != NULL) {
310 if (ill->ill_isv6)
311 ASSERT(ill->ill_icmp6_mib != NULL);
312 return (B_TRUE);
315 ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
316 KM_NOSLEEP);
317 if (ill->ill_ip_mib == NULL) {
318 return (B_FALSE);
321 /* Setup static information */
322 SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
323 sizeof (mib2_ipIfStatsEntry_t));
324 if (ill->ill_isv6) {
325 ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
326 SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
327 sizeof (mib2_ipv6AddrEntry_t));
328 SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
329 sizeof (mib2_ipv6RouteEntry_t));
330 SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
331 sizeof (mib2_ipv6NetToMediaEntry_t));
332 SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
333 sizeof (ipv6_member_t));
334 SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
335 sizeof (ipv6_grpsrc_t));
336 } else {
337 ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
338 SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
339 sizeof (mib2_ipAddrEntry_t));
340 SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
341 sizeof (mib2_ipRouteEntry_t));
342 SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
343 sizeof (mib2_ipNetToMediaEntry_t));
344 SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
345 sizeof (ip_member_t));
346 SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
347 sizeof (ip_grpsrc_t));
350 * For a v4 ill, we are done at this point, because per ill
351 * icmp mibs are only used for v6.
353 return (B_TRUE);
356 ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
357 KM_NOSLEEP);
358 if (ill->ill_icmp6_mib == NULL) {
359 kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
360 ill->ill_ip_mib = NULL;
361 return (B_FALSE);
363 /* static icmp info */
364 ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
365 sizeof (mib2_ipv6IfIcmpEntry_t);
367 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
368 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
369 * -> ill_phyint_reinit
371 return (B_TRUE);
375 * Completely vaporize a lower level tap and all associated interfaces.
376 * ill_delete is called only out of ip_close when the device control
377 * stream is being closed.
379 void
380 ill_delete(ill_t *ill)
382 ipif_t *ipif;
383 ill_t *prev_ill;
384 ip_stack_t *ipst = ill->ill_ipst;
387 * ill_delete may be forcibly entering the ipsq. The previous
388 * ioctl may not have completed and may need to be aborted.
389 * ipsq_flush takes care of it. If we don't need to enter the
390 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
391 * ill_delete_tail is sufficient.
393 ipsq_flush(ill);
396 * Nuke all interfaces. ipif_free will take down the interface,
397 * remove it from the list, and free the data structure.
398 * Walk down the ipif list and remove the logical interfaces
399 * first before removing the main ipif. We can't unplumb
400 * zeroth interface first in the case of IPv6 as update_conn_ill
401 * -> ip_ll_multireq de-references ill_ipif for checking
402 * POINTOPOINT.
404 * If ill_ipif was not properly initialized (i.e low on memory),
405 * then no interfaces to clean up. In this case just clean up the
406 * ill.
408 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
409 ipif_free(ipif);
412 * clean out all the nce_t entries that depend on this
413 * ill for the ill_phys_addr.
415 nce_flush(ill, B_TRUE);
417 /* Clean up msgs on pending upcalls for mrouted */
418 reset_mrt_ill(ill);
420 update_conn_ill(ill, ipst);
423 * Remove multicast references added as a result of calls to
424 * ip_join_allmulti().
426 ip_purge_allmulti(ill);
429 * If the ill being deleted is under IPMP, boot it out of the illgrp.
431 if (IS_UNDER_IPMP(ill))
432 ipmp_ill_leave_illgrp(ill);
435 * ill_down will arrange to blow off any IRE's dependent on this
436 * ILL, and shut down fragmentation reassembly.
438 ill_down(ill);
440 /* Let SCTP know, so that it can remove this from its list. */
441 sctp_update_ill(ill, SCTP_ILL_REMOVE);
444 * Walk all CONNs that can have a reference on an ire or nce for this
445 * ill (we actually walk all that now have stale references).
447 ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
449 /* With IPv6 we have dce_ifindex. Cleanup for neatness */
450 if (ill->ill_isv6)
451 dce_cleanup(ill->ill_phyint->phyint_ifindex, ipst);
454 * If an address on this ILL is being used as a source address then
455 * clear out the pointers in other ILLs that point to this ILL.
457 rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
458 if (ill->ill_usesrc_grp_next != NULL) {
459 if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
460 ill_disband_usesrc_group(ill);
461 } else { /* consumer of the usesrc ILL */
462 prev_ill = ill_prev_usesrc(ill);
463 prev_ill->ill_usesrc_grp_next =
464 ill->ill_usesrc_grp_next;
467 rw_exit(&ipst->ips_ill_g_usesrc_lock);
470 static void
471 ipif_non_duplicate(ipif_t *ipif)
473 ill_t *ill = ipif->ipif_ill;
474 mutex_enter(&ill->ill_lock);
475 if (ipif->ipif_flags & IPIF_DUPLICATE) {
476 ipif->ipif_flags &= ~IPIF_DUPLICATE;
477 ASSERT(ill->ill_ipif_dup_count > 0);
478 ill->ill_ipif_dup_count--;
480 mutex_exit(&ill->ill_lock);
484 * ill_delete_tail is called from ip_modclose after all references
485 * to the closing ill are gone. The wait is done in ip_modclose
487 void
488 ill_delete_tail(ill_t *ill)
490 mblk_t **mpp;
491 ipif_t *ipif;
492 ip_stack_t *ipst = ill->ill_ipst;
494 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
495 ipif_non_duplicate(ipif);
496 (void) ipif_down_tail(ipif);
499 ASSERT(ill->ill_ipif_dup_count == 0);
502 * If polling capability is enabled (which signifies direct
503 * upcall into IP and driver has ill saved as a handle),
504 * we need to make sure that unbind has completed before we
505 * let the ill disappear and driver no longer has any reference
506 * to this ill.
508 mutex_enter(&ill->ill_lock);
509 while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
510 cv_wait(&ill->ill_cv, &ill->ill_lock);
511 mutex_exit(&ill->ill_lock);
512 ASSERT(!(ill->ill_capabilities &
513 (ILL_CAPAB_DLD | ILL_CAPAB_DLD_POLL | ILL_CAPAB_DLD_DIRECT)));
515 if (ill->ill_net_type != IRE_LOOPBACK)
516 qprocsoff(ill->ill_rq);
519 * We do an ipsq_flush once again now. New messages could have
520 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
521 * could also have landed up if an ioctl thread had looked up
522 * the ill before we set the ILL_CONDEMNED flag, but not yet
523 * enqueued the ioctl when we did the ipsq_flush last time.
525 ipsq_flush(ill);
528 * Free capabilities.
530 if (ill->ill_hcksum_capab != NULL) {
531 kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
532 ill->ill_hcksum_capab = NULL;
535 if (ill->ill_zerocopy_capab != NULL) {
536 kmem_free(ill->ill_zerocopy_capab,
537 sizeof (ill_zerocopy_capab_t));
538 ill->ill_zerocopy_capab = NULL;
541 if (ill->ill_lso_capab != NULL) {
542 kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
543 ill->ill_lso_capab = NULL;
546 if (ill->ill_dld_capab != NULL) {
547 kmem_free(ill->ill_dld_capab, sizeof (ill_dld_capab_t));
548 ill->ill_dld_capab = NULL;
551 /* Clean up ill_allowed_ips* related state */
552 if (ill->ill_allowed_ips != NULL) {
553 ASSERT(ill->ill_allowed_ips_cnt > 0);
554 kmem_free(ill->ill_allowed_ips,
555 ill->ill_allowed_ips_cnt * sizeof (in6_addr_t));
556 ill->ill_allowed_ips = NULL;
557 ill->ill_allowed_ips_cnt = 0;
560 while (ill->ill_ipif != NULL)
561 ipif_free_tail(ill->ill_ipif);
564 * We have removed all references to ilm from conn and the ones joined
565 * within the kernel.
567 * We don't walk conns, mrts and ires because
569 * 1) update_conn_ill and reset_mrt_ill cleans up conns and mrts.
570 * 2) ill_down ->ill_downi walks all the ires and cleans up
571 * ill references.
575 * If this ill is an IPMP meta-interface, blow away the illgrp. This
576 * is safe to do because the illgrp has already been unlinked from the
577 * group by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find it.
579 if (IS_IPMP(ill)) {
580 ipmp_illgrp_destroy(ill->ill_grp);
581 ill->ill_grp = NULL;
584 if (ill->ill_mphysaddr_list != NULL) {
585 multiphysaddr_t *mpa, *tmpa;
587 mpa = ill->ill_mphysaddr_list;
588 ill->ill_mphysaddr_list = NULL;
589 while (mpa) {
590 tmpa = mpa->mpa_next;
591 kmem_free(mpa, sizeof (*mpa));
592 mpa = tmpa;
596 * Take us out of the list of ILLs. ill_glist_delete -> phyint_free
597 * could free the phyint. No more reference to the phyint after this
598 * point.
600 (void) ill_glist_delete(ill);
602 if (ill->ill_frag_ptr != NULL) {
603 uint_t count;
605 for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
606 mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
608 mi_free(ill->ill_frag_ptr);
609 ill->ill_frag_ptr = NULL;
610 ill->ill_frag_hash_tbl = NULL;
613 freemsg(ill->ill_nd_lla_mp);
614 /* Free all retained control messages. */
615 mpp = &ill->ill_first_mp_to_free;
616 do {
617 while (mpp[0]) {
618 mblk_t *mp;
619 mblk_t *mp1;
621 mp = mpp[0];
622 mpp[0] = mp->b_next;
623 for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
624 mp1->b_next = NULL;
625 mp1->b_prev = NULL;
627 freemsg(mp);
629 } while (mpp++ != &ill->ill_last_mp_to_free);
631 ill_free_mib(ill);
633 #ifdef DEBUG
634 ill_trace_cleanup(ill);
635 #endif
637 /* The default multicast interface might have changed */
638 ire_increment_multicast_generation(ipst, ill->ill_isv6);
640 /* Drop refcnt here */
641 netstack_rele(ill->ill_ipst->ips_netstack);
642 ill->ill_ipst = NULL;
645 static void
646 ill_free_mib(ill_t *ill)
648 ip_stack_t *ipst = ill->ill_ipst;
651 * MIB statistics must not be lost, so when an interface
652 * goes away the counter values will be added to the global
653 * MIBs.
655 if (ill->ill_ip_mib != NULL) {
656 if (ill->ill_isv6) {
657 ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
658 ill->ill_ip_mib);
659 } else {
660 ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
661 ill->ill_ip_mib);
664 kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
665 ill->ill_ip_mib = NULL;
667 if (ill->ill_icmp6_mib != NULL) {
668 ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
669 ill->ill_icmp6_mib);
670 kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
671 ill->ill_icmp6_mib = NULL;
676 * Concatenate together a physical address and a sap.
678 * Sap_lengths are interpreted as follows:
679 * sap_length == 0 ==> no sap
680 * sap_length > 0 ==> sap is at the head of the dlpi address
681 * sap_length < 0 ==> sap is at the tail of the dlpi address
683 static void
684 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
685 t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
687 uint16_t sap_addr = (uint16_t)sap_src;
689 if (sap_length == 0) {
690 if (phys_src == NULL)
691 bzero(dst, phys_length);
692 else
693 bcopy(phys_src, dst, phys_length);
694 } else if (sap_length < 0) {
695 if (phys_src == NULL)
696 bzero(dst, phys_length);
697 else
698 bcopy(phys_src, dst, phys_length);
699 bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
700 } else {
701 bcopy(&sap_addr, dst, sizeof (sap_addr));
702 if (phys_src == NULL)
703 bzero((char *)dst + sap_length, phys_length);
704 else
705 bcopy(phys_src, (char *)dst + sap_length, phys_length);
710 * Generate a dl_unitdata_req mblk for the device and address given.
711 * addr_length is the length of the physical portion of the address.
712 * If addr is NULL include an all zero address of the specified length.
713 * TRUE? In any case, addr_length is taken to be the entire length of the
714 * dlpi address, including the absolute value of sap_length.
716 mblk_t *
717 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
718 t_scalar_t sap_length)
720 dl_unitdata_req_t *dlur;
721 mblk_t *mp;
722 t_scalar_t abs_sap_length; /* absolute value */
724 abs_sap_length = ABS(sap_length);
725 mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
726 DL_UNITDATA_REQ);
727 if (mp == NULL)
728 return (NULL);
729 dlur = (dl_unitdata_req_t *)mp->b_rptr;
730 /* HACK: accomodate incompatible DLPI drivers */
731 if (addr_length == 8)
732 addr_length = 6;
733 dlur->dl_dest_addr_length = addr_length + abs_sap_length;
734 dlur->dl_dest_addr_offset = sizeof (*dlur);
735 dlur->dl_priority.dl_min = 0;
736 dlur->dl_priority.dl_max = 0;
737 ill_dlur_copy_address(addr, addr_length, sap, sap_length,
738 (uchar_t *)&dlur[1]);
739 return (mp);
743 * Add the pending mp to the list. There can be only 1 pending mp
744 * in the list. Any exclusive ioctl that needs to wait for a response
745 * from another module or driver needs to use this function to set
746 * the ipx_pending_mp to the ioctl mblk and wait for the response from
747 * the other module/driver. This is also used while waiting for the
748 * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
750 boolean_t
751 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
752 int waitfor)
754 ipxop_t *ipx = ipif->ipif_ill->ill_phyint->phyint_ipsq->ipsq_xop;
756 ASSERT(IAM_WRITER_IPIF(ipif));
757 ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
758 ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
759 ASSERT(ipx->ipx_pending_mp == NULL);
761 * The caller may be using a different ipif than the one passed into
762 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
763 * ill needs to wait for the V6 ill to quiesce). So we can't ASSERT
764 * that `ipx_current_ipif == ipif'.
766 ASSERT(ipx->ipx_current_ipif != NULL);
769 * M_IOCDATA from ioctls, M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the
770 * driver.
772 ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_ERROR) ||
773 (DB_TYPE(add_mp) == M_HANGUP) || (DB_TYPE(add_mp) == M_PROTO) ||
774 (DB_TYPE(add_mp) == M_PCPROTO));
776 if (connp != NULL) {
777 ASSERT(MUTEX_HELD(&connp->conn_lock));
779 * Return error if the conn has started closing. The conn
780 * could have finished cleaning up the pending mp list,
781 * If so we should not add another mp to the list negating
782 * the cleanup.
784 if (connp->conn_state_flags & CONN_CLOSING)
785 return (B_FALSE);
787 mutex_enter(&ipx->ipx_lock);
788 ipx->ipx_pending_ipif = ipif;
790 * Note down the queue in b_queue. This will be returned by
791 * ipsq_pending_mp_get. Caller will then use these values to restart
792 * the processing
794 add_mp->b_next = NULL;
795 add_mp->b_queue = q;
796 ipx->ipx_pending_mp = add_mp;
797 ipx->ipx_waitfor = waitfor;
798 mutex_exit(&ipx->ipx_lock);
800 if (connp != NULL)
801 connp->conn_oper_pending_ill = ipif->ipif_ill;
803 return (B_TRUE);
807 * Retrieve the ipx_pending_mp and return it. There can be only 1 mp
808 * queued in the list.
810 mblk_t *
811 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
813 mblk_t *curr = NULL;
814 ipxop_t *ipx = ipsq->ipsq_xop;
816 *connpp = NULL;
817 mutex_enter(&ipx->ipx_lock);
818 if (ipx->ipx_pending_mp == NULL) {
819 mutex_exit(&ipx->ipx_lock);
820 return (NULL);
823 /* There can be only 1 such excl message */
824 curr = ipx->ipx_pending_mp;
825 ASSERT(curr->b_next == NULL);
826 ipx->ipx_pending_ipif = NULL;
827 ipx->ipx_pending_mp = NULL;
828 ipx->ipx_waitfor = 0;
829 mutex_exit(&ipx->ipx_lock);
831 if (CONN_Q(curr->b_queue)) {
833 * This mp did a refhold on the conn, at the start of the ioctl.
834 * So we can safely return a pointer to the conn to the caller.
836 *connpp = Q_TO_CONN(curr->b_queue);
837 } else {
838 *connpp = NULL;
840 curr->b_next = NULL;
841 curr->b_prev = NULL;
842 return (curr);
846 * Cleanup the ioctl mp queued in ipx_pending_mp
847 * - Called in the ill_delete path
848 * - Called in the M_ERROR or M_HANGUP path on the ill.
849 * - Called in the conn close path.
851 * Returns success on finding the pending mblk associated with the ioctl or
852 * exclusive operation in progress, failure otherwise.
854 boolean_t
855 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
857 mblk_t *mp;
858 ipxop_t *ipx;
859 queue_t *q;
860 ipif_t *ipif;
861 int cmd;
863 ASSERT(IAM_WRITER_ILL(ill));
864 ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
866 mutex_enter(&ipx->ipx_lock);
867 mp = ipx->ipx_pending_mp;
868 if (connp != NULL) {
869 if (mp == NULL || mp->b_queue != CONNP_TO_WQ(connp)) {
871 * Nothing to clean since the conn that is closing
872 * does not have a matching pending mblk in
873 * ipx_pending_mp.
875 mutex_exit(&ipx->ipx_lock);
876 return (B_FALSE);
878 } else {
880 * A non-zero ill_error signifies we are called in the
881 * M_ERROR or M_HANGUP path and we need to unconditionally
882 * abort any current ioctl and do the corresponding cleanup.
883 * A zero ill_error means we are in the ill_delete path and
884 * we do the cleanup only if there is a pending mp.
886 if (mp == NULL && ill->ill_error == 0) {
887 mutex_exit(&ipx->ipx_lock);
888 return (B_FALSE);
892 /* Now remove from the ipx_pending_mp */
893 ipx->ipx_pending_mp = NULL;
894 ipif = ipx->ipx_pending_ipif;
895 ipx->ipx_pending_ipif = NULL;
896 ipx->ipx_waitfor = 0;
897 ipx->ipx_current_ipif = NULL;
898 cmd = ipx->ipx_current_ioctl;
899 ipx->ipx_current_ioctl = 0;
900 ipx->ipx_current_done = B_TRUE;
901 mutex_exit(&ipx->ipx_lock);
903 if (mp == NULL)
904 return (B_FALSE);
906 q = mp->b_queue;
907 mp->b_next = NULL;
908 mp->b_prev = NULL;
909 mp->b_queue = NULL;
911 if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
912 DTRACE_PROBE4(ipif__ioctl,
913 char *, "ipsq_pending_mp_cleanup",
914 int, cmd, ill_t *, ipif == NULL ? NULL : ipif->ipif_ill,
915 ipif_t *, ipif);
916 if (connp == NULL) {
917 ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
918 } else {
919 ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
920 mutex_enter(&ipif->ipif_ill->ill_lock);
921 ipif->ipif_state_flags &= ~IPIF_CHANGING;
922 mutex_exit(&ipif->ipif_ill->ill_lock);
924 } else {
925 inet_freemsg(mp);
927 return (B_TRUE);
931 * Called in the conn close path and ill delete path
933 static void
934 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
936 ipsq_t *ipsq;
937 mblk_t *prev;
938 mblk_t *curr;
939 mblk_t *next;
940 queue_t *wq, *rq = NULL;
941 mblk_t *tmp_list = NULL;
943 ASSERT(IAM_WRITER_ILL(ill));
944 if (connp != NULL)
945 wq = CONNP_TO_WQ(connp);
946 else
947 wq = ill->ill_wq;
950 * In the case of lo0 being unplumbed, ill_wq will be NULL. Guard
951 * against this here.
953 if (wq != NULL)
954 rq = RD(wq);
956 ipsq = ill->ill_phyint->phyint_ipsq;
958 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
959 * In the case of ioctl from a conn, there can be only 1 mp
960 * queued on the ipsq. If an ill is being unplumbed flush all
961 * the messages.
963 mutex_enter(&ipsq->ipsq_lock);
964 for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
965 curr = next) {
966 next = curr->b_next;
967 if (connp == NULL ||
968 (curr->b_queue == wq || curr->b_queue == rq)) {
969 /* Unlink the mblk from the pending mp list */
970 if (prev != NULL) {
971 prev->b_next = curr->b_next;
972 } else {
973 ASSERT(ipsq->ipsq_xopq_mphead == curr);
974 ipsq->ipsq_xopq_mphead = curr->b_next;
976 if (ipsq->ipsq_xopq_mptail == curr)
977 ipsq->ipsq_xopq_mptail = prev;
979 * Create a temporary list and release the ipsq lock
980 * New elements are added to the head of the tmp_list
982 curr->b_next = tmp_list;
983 tmp_list = curr;
984 } else {
985 prev = curr;
988 mutex_exit(&ipsq->ipsq_lock);
990 while (tmp_list != NULL) {
991 curr = tmp_list;
992 tmp_list = curr->b_next;
993 curr->b_next = NULL;
994 curr->b_prev = NULL;
995 wq = curr->b_queue;
996 curr->b_queue = NULL;
997 if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
998 DTRACE_PROBE4(ipif__ioctl,
999 char *, "ipsq_xopq_mp_cleanup",
1000 int, 0, ill_t *, NULL, ipif_t *, NULL);
1001 ip_ioctl_finish(wq, curr, ENXIO, connp != NULL ?
1002 CONN_CLOSE : NO_COPYOUT, NULL);
1003 } else {
1005 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1006 * this can't be just inet_freemsg. we have to
1007 * restart it otherwise the thread will be stuck.
1009 inet_freemsg(curr);
1015 * This conn has started closing. Cleanup any pending ioctl from this conn.
1016 * STREAMS ensures that there can be at most 1 active ioctl on a stream.
1018 void
1019 conn_ioctl_cleanup(conn_t *connp)
1021 ipsq_t *ipsq;
1022 ill_t *ill;
1023 boolean_t refheld;
1026 * Check for a queued ioctl. If the ioctl has not yet started, the mp
1027 * is pending in the list headed by ipsq_xopq_head. If the ioctl has
1028 * started the mp could be present in ipx_pending_mp. Note that if
1029 * conn_oper_pending_ill is NULL, the ioctl may still be in flight and
1030 * not yet queued anywhere. In this case, the conn close code will wait
1031 * until the conn_ref is dropped. If the stream was a tcp stream, then
1032 * tcp_close will wait first until all ioctls have completed for this
1033 * conn.
1035 mutex_enter(&connp->conn_lock);
1036 ill = connp->conn_oper_pending_ill;
1037 if (ill == NULL) {
1038 mutex_exit(&connp->conn_lock);
1039 return;
1043 * We may not be able to refhold the ill if the ill/ipif
1044 * is changing. But we need to make sure that the ill will
1045 * not vanish. So we just bump up the ill_waiter count.
1047 refheld = ill_waiter_inc(ill);
1048 mutex_exit(&connp->conn_lock);
1049 if (refheld) {
1050 if (ipsq_enter(ill, B_TRUE, NEW_OP)) {
1051 ill_waiter_dcr(ill);
1053 * Check whether this ioctl has started and is
1054 * pending. If it is not found there then check
1055 * whether this ioctl has not even started and is in
1056 * the ipsq_xopq list.
1058 if (!ipsq_pending_mp_cleanup(ill, connp))
1059 ipsq_xopq_mp_cleanup(ill, connp);
1060 ipsq = ill->ill_phyint->phyint_ipsq;
1061 ipsq_exit(ipsq);
1062 return;
1067 * The ill is also closing and we could not bump up the
1068 * ill_waiter_count or we could not enter the ipsq. Leave
1069 * the cleanup to ill_delete
1071 mutex_enter(&connp->conn_lock);
1072 while (connp->conn_oper_pending_ill != NULL)
1073 cv_wait(&connp->conn_refcv, &connp->conn_lock);
1074 mutex_exit(&connp->conn_lock);
1075 if (refheld)
1076 ill_waiter_dcr(ill);
1080 * ipcl_walk function for cleaning up conn_*_ill fields.
1081 * Note that we leave ixa_multicast_ifindex, conn_incoming_ifindex, and
1082 * conn_bound_if in place. We prefer dropping
1083 * packets instead of sending them out the wrong interface, or accepting
1084 * packets from the wrong ifindex.
1086 static void
1087 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1089 ill_t *ill = (ill_t *)arg;
1091 mutex_enter(&connp->conn_lock);
1092 if (connp->conn_dhcpinit_ill == ill) {
1093 connp->conn_dhcpinit_ill = NULL;
1094 ASSERT(ill->ill_dhcpinit != 0);
1095 atomic_dec_32(&ill->ill_dhcpinit);
1096 ill_set_inputfn(ill);
1098 mutex_exit(&connp->conn_lock);
1101 static int
1102 ill_down_ipifs_tail(ill_t *ill)
1104 ipif_t *ipif;
1105 int err;
1107 ASSERT(IAM_WRITER_ILL(ill));
1108 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1109 ipif_non_duplicate(ipif);
1111 * ipif_down_tail will call arp_ll_down on the last ipif
1112 * and typically return EINPROGRESS when the DL_UNBIND is sent.
1114 if ((err = ipif_down_tail(ipif)) != 0)
1115 return (err);
1117 return (0);
1120 /* ARGSUSED */
1121 void
1122 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1124 ASSERT(IAM_WRITER_IPSQ(ipsq));
1125 (void) ill_down_ipifs_tail(q->q_ptr);
1126 freemsg(mp);
1127 ipsq_current_finish(ipsq);
1131 * ill_down_start is called when we want to down this ill and bring it up again
1132 * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1133 * all interfaces, but don't tear down any plumbing.
1135 boolean_t
1136 ill_down_start(queue_t *q, mblk_t *mp)
1138 ill_t *ill = q->q_ptr;
1139 ipif_t *ipif;
1141 ASSERT(IAM_WRITER_ILL(ill));
1143 * It is possible that some ioctl is already in progress while we
1144 * received the M_ERROR / M_HANGUP in which case, we need to abort
1145 * the ioctl. ill_down_start() is being processed as CUR_OP rather
1146 * than as NEW_OP since the cause of the M_ERROR / M_HANGUP may prevent
1147 * the in progress ioctl from ever completing.
1149 * The thread that started the ioctl (if any) must have returned,
1150 * since we are now executing as writer. After the 2 calls below,
1151 * the state of the ipsq and the ill would reflect no trace of any
1152 * pending operation. Subsequently if there is any response to the
1153 * original ioctl from the driver, it would be discarded as an
1154 * unsolicited message from the driver.
1156 (void) ipsq_pending_mp_cleanup(ill, NULL);
1157 ill_dlpi_clear_deferred(ill);
1159 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1160 (void) ipif_down(ipif, NULL, NULL);
1162 ill_down(ill);
1165 * Walk all CONNs that can have a reference on an ire or nce for this
1166 * ill (we actually walk all that now have stale references).
1168 ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ill->ill_ipst);
1170 /* With IPv6 we have dce_ifindex. Cleanup for neatness */
1171 if (ill->ill_isv6)
1172 dce_cleanup(ill->ill_phyint->phyint_ifindex, ill->ill_ipst);
1174 ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1177 * Atomically test and add the pending mp if references are active.
1179 mutex_enter(&ill->ill_lock);
1180 if (!ill_is_quiescent(ill)) {
1181 /* call cannot fail since `conn_t *' argument is NULL */
1182 (void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1183 mp, ILL_DOWN);
1184 mutex_exit(&ill->ill_lock);
1185 return (B_FALSE);
1187 mutex_exit(&ill->ill_lock);
1188 return (B_TRUE);
1191 static void
1192 ill_down(ill_t *ill)
1194 mblk_t *mp;
1195 ip_stack_t *ipst = ill->ill_ipst;
1198 * Blow off any IREs dependent on this ILL.
1199 * The caller needs to handle conn_ixa_cleanup
1201 ill_delete_ires(ill);
1203 ire_walk_ill(0, 0, ill_downi, ill, ill);
1205 /* Remove any conn_*_ill depending on this ill */
1206 ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1209 * Free state for additional IREs.
1211 mutex_enter(&ill->ill_saved_ire_lock);
1212 mp = ill->ill_saved_ire_mp;
1213 ill->ill_saved_ire_mp = NULL;
1214 ill->ill_saved_ire_cnt = 0;
1215 mutex_exit(&ill->ill_saved_ire_lock);
1216 freemsg(mp);
1220 * ire_walk routine used to delete every IRE that depends on
1221 * 'ill'. (Always called as writer, and may only be called from ire_walk.)
1223 * Note: since the routes added by the kernel are deleted separately,
1224 * this will only be 1) IRE_IF_CLONE and 2) manually added IRE_INTERFACE.
1226 * We also remove references on ire_nce_cache entries that refer to the ill.
1228 void
1229 ill_downi(ire_t *ire, char *ill_arg)
1231 ill_t *ill = (ill_t *)ill_arg;
1232 nce_t *nce;
1234 mutex_enter(&ire->ire_lock);
1235 nce = ire->ire_nce_cache;
1236 if (nce != NULL && nce->nce_ill == ill)
1237 ire->ire_nce_cache = NULL;
1238 else
1239 nce = NULL;
1240 mutex_exit(&ire->ire_lock);
1241 if (nce != NULL)
1242 nce_refrele(nce);
1243 if (ire->ire_ill == ill) {
1245 * The existing interface binding for ire must be
1246 * deleted before trying to bind the route to another
1247 * interface. However, since we are using the contents of the
1248 * ire after ire_delete, the caller has to ensure that
1249 * CONDEMNED (deleted) ire's are not removed from the list
1250 * when ire_delete() returns. Currently ill_downi() is
1251 * only called as part of ire_walk*() routines, so that
1252 * the irb_refhold() done by ire_walk*() will ensure that
1253 * ire_delete() does not lead to ire_inactive().
1255 ASSERT(ire->ire_bucket->irb_refcnt > 0);
1256 ire_delete(ire);
1257 if (ire->ire_unbound)
1258 ire_rebind(ire);
1262 /* Remove IRE_IF_CLONE on this ill */
1263 void
1264 ill_downi_if_clone(ire_t *ire, char *ill_arg)
1266 ill_t *ill = (ill_t *)ill_arg;
1268 ASSERT(ire->ire_type & IRE_IF_CLONE);
1269 if (ire->ire_ill == ill)
1270 ire_delete(ire);
1273 /* Consume an M_IOCACK of the fastpath probe. */
1274 void
1275 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1277 mblk_t *mp1 = mp;
1280 * If this was the first attempt turn on the fastpath probing.
1282 mutex_enter(&ill->ill_lock);
1283 if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1284 ill->ill_dlpi_fastpath_state = IDS_OK;
1285 mutex_exit(&ill->ill_lock);
1287 /* Free the M_IOCACK mblk, hold on to the data */
1288 mp = mp->b_cont;
1289 freeb(mp1);
1290 if (mp == NULL)
1291 return;
1292 if (mp->b_cont != NULL)
1293 nce_fastpath_update(ill, mp);
1294 else
1295 ip0dbg(("ill_fastpath_ack: no b_cont\n"));
1296 freemsg(mp);
1300 * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1301 * The data portion of the request is a dl_unitdata_req_t template for
1302 * what we would send downstream in the absence of a fastpath confirmation.
1305 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1307 struct iocblk *ioc;
1308 mblk_t *mp;
1310 if (dlur_mp == NULL)
1311 return (EINVAL);
1313 mutex_enter(&ill->ill_lock);
1314 switch (ill->ill_dlpi_fastpath_state) {
1315 case IDS_FAILED:
1317 * Driver NAKed the first fastpath ioctl - assume it doesn't
1318 * support it.
1320 mutex_exit(&ill->ill_lock);
1321 return (ENOTSUP);
1322 case IDS_UNKNOWN:
1323 /* This is the first probe */
1324 ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1325 break;
1326 default:
1327 break;
1329 mutex_exit(&ill->ill_lock);
1331 if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1332 return (EAGAIN);
1334 mp->b_cont = copyb(dlur_mp);
1335 if (mp->b_cont == NULL) {
1336 freeb(mp);
1337 return (EAGAIN);
1340 ioc = (struct iocblk *)mp->b_rptr;
1341 ioc->ioc_count = msgdsize(mp->b_cont);
1343 DTRACE_PROBE3(ill__dlpi, char *, "ill_fastpath_probe",
1344 char *, "DL_IOC_HDR_INFO", ill_t *, ill);
1345 putnext(ill->ill_wq, mp);
1346 return (0);
1349 void
1350 ill_capability_probe(ill_t *ill)
1352 mblk_t *mp;
1354 ASSERT(IAM_WRITER_ILL(ill));
1356 if (ill->ill_dlpi_capab_state != IDCS_UNKNOWN &&
1357 ill->ill_dlpi_capab_state != IDCS_FAILED)
1358 return;
1361 * We are starting a new cycle of capability negotiation.
1362 * Free up the capab reset messages of any previous incarnation.
1363 * We will do a fresh allocation when we get the response to our probe
1365 if (ill->ill_capab_reset_mp != NULL) {
1366 freemsg(ill->ill_capab_reset_mp);
1367 ill->ill_capab_reset_mp = NULL;
1370 ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1372 mp = ip_dlpi_alloc(sizeof (dl_capability_req_t), DL_CAPABILITY_REQ);
1373 if (mp == NULL)
1374 return;
1376 ill_capability_send(ill, mp);
1377 ill->ill_dlpi_capab_state = IDCS_PROBE_SENT;
1380 void
1381 ill_capability_reset(ill_t *ill, boolean_t reneg)
1383 ASSERT(IAM_WRITER_ILL(ill));
1385 if (ill->ill_dlpi_capab_state != IDCS_OK)
1386 return;
1388 ill->ill_dlpi_capab_state = reneg ? IDCS_RENEG : IDCS_RESET_SENT;
1390 ill_capability_send(ill, ill->ill_capab_reset_mp);
1391 ill->ill_capab_reset_mp = NULL;
1393 * We turn off all capabilities except those pertaining to
1394 * direct function call capabilities viz. ILL_CAPAB_DLD*
1395 * which will be turned off by the corresponding reset functions.
1397 ill->ill_capabilities &= ~(ILL_CAPAB_HCKSUM | ILL_CAPAB_ZEROCOPY);
1400 static void
1401 ill_capability_reset_alloc(ill_t *ill)
1403 mblk_t *mp;
1404 size_t size = 0;
1405 int err;
1406 dl_capability_req_t *capb;
1408 ASSERT(IAM_WRITER_ILL(ill));
1409 ASSERT(ill->ill_capab_reset_mp == NULL);
1411 if (ILL_HCKSUM_CAPABLE(ill)) {
1412 size += sizeof (dl_capability_sub_t) +
1413 sizeof (dl_capab_hcksum_t);
1416 if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) {
1417 size += sizeof (dl_capability_sub_t) +
1418 sizeof (dl_capab_zerocopy_t);
1421 if (ill->ill_capabilities & ILL_CAPAB_DLD) {
1422 size += sizeof (dl_capability_sub_t) +
1423 sizeof (dl_capab_dld_t);
1426 mp = allocb_wait(size + sizeof (dl_capability_req_t), BPRI_MED,
1427 STR_NOSIG, &err);
1429 mp->b_datap->db_type = M_PROTO;
1430 bzero(mp->b_rptr, size + sizeof (dl_capability_req_t));
1432 capb = (dl_capability_req_t *)mp->b_rptr;
1433 capb->dl_primitive = DL_CAPABILITY_REQ;
1434 capb->dl_sub_offset = sizeof (dl_capability_req_t);
1435 capb->dl_sub_length = size;
1437 mp->b_wptr += sizeof (dl_capability_req_t);
1440 * Each handler fills in the corresponding dl_capability_sub_t
1441 * inside the mblk,
1443 ill_capability_hcksum_reset_fill(ill, mp);
1444 ill_capability_zerocopy_reset_fill(ill, mp);
1445 ill_capability_dld_reset_fill(ill, mp);
1447 ill->ill_capab_reset_mp = mp;
1450 static void
1451 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1453 dl_capab_id_t *id_ic;
1454 uint_t sub_dl_cap = outers->dl_cap;
1455 dl_capability_sub_t *inners;
1456 uint8_t *capend;
1458 ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1461 * Note: range checks here are not absolutely sufficient to
1462 * make us robust against malformed messages sent by drivers;
1463 * this is in keeping with the rest of IP's dlpi handling.
1464 * (Remember, it's coming from something else in the kernel
1465 * address space)
1468 capend = (uint8_t *)(outers + 1) + outers->dl_length;
1469 if (capend > mp->b_wptr) {
1470 cmn_err(CE_WARN, "ill_capability_id_ack: "
1471 "malformed sub-capability too long for mblk");
1472 return;
1475 id_ic = (dl_capab_id_t *)(outers + 1);
1477 if (outers->dl_length < sizeof (*id_ic) ||
1478 (inners = &id_ic->id_subcap,
1479 inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1480 cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1481 "encapsulated capab type %d too long for mblk",
1482 inners->dl_cap);
1483 return;
1486 if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1487 ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1488 "isn't as expected; pass-thru module(s) detected, "
1489 "discarding capability\n", inners->dl_cap));
1490 return;
1493 /* Process the encapsulated sub-capability */
1494 ill_capability_dispatch(ill, mp, inners);
1497 static void
1498 ill_capability_dld_reset_fill(ill_t *ill, mblk_t *mp)
1500 dl_capability_sub_t *dl_subcap;
1502 if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
1503 return;
1506 * The dl_capab_dld_t that follows the dl_capability_sub_t is not
1507 * initialized below since it is not used by DLD.
1509 dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1510 dl_subcap->dl_cap = DL_CAPAB_DLD;
1511 dl_subcap->dl_length = sizeof (dl_capab_dld_t);
1513 mp->b_wptr += sizeof (dl_capability_sub_t) + sizeof (dl_capab_dld_t);
1516 static void
1517 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp)
1520 * If no ipif was brought up over this ill, this DL_CAPABILITY_REQ/ACK
1521 * is only to get the VRRP capability.
1523 * Note that we cannot check ill_ipif_up_count here since
1524 * ill_ipif_up_count is only incremented when the resolver is setup.
1525 * That is done asynchronously, and can race with this function.
1527 if (!ill->ill_dl_up) {
1528 if (subp->dl_cap == DL_CAPAB_VRRP)
1529 ill_capability_vrrp_ack(ill, mp, subp);
1530 return;
1533 switch (subp->dl_cap) {
1534 case DL_CAPAB_HCKSUM:
1535 ill_capability_hcksum_ack(ill, mp, subp);
1536 break;
1537 case DL_CAPAB_ZEROCOPY:
1538 ill_capability_zerocopy_ack(ill, mp, subp);
1539 break;
1540 case DL_CAPAB_DLD:
1541 ill_capability_dld_ack(ill, mp, subp);
1542 break;
1543 case DL_CAPAB_VRRP:
1544 break;
1545 default:
1546 ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
1547 subp->dl_cap));
1552 * Process the vrrp capability received from a DLS Provider. isub must point
1553 * to the sub-capability (DL_CAPAB_VRRP) of a DL_CAPABILITY_ACK message.
1555 static void
1556 ill_capability_vrrp_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1558 dl_capab_vrrp_t *vrrp;
1559 uint_t sub_dl_cap = isub->dl_cap;
1560 uint8_t *capend;
1562 ASSERT(IAM_WRITER_ILL(ill));
1563 ASSERT(sub_dl_cap == DL_CAPAB_VRRP);
1566 * Note: range checks here are not absolutely sufficient to
1567 * make us robust against malformed messages sent by drivers;
1568 * this is in keeping with the rest of IP's dlpi handling.
1569 * (Remember, it's coming from something else in the kernel
1570 * address space)
1572 capend = (uint8_t *)(isub + 1) + isub->dl_length;
1573 if (capend > mp->b_wptr) {
1574 cmn_err(CE_WARN, "ill_capability_vrrp_ack: "
1575 "malformed sub-capability too long for mblk");
1576 return;
1578 vrrp = (dl_capab_vrrp_t *)(isub + 1);
1581 * Compare the IP address family and set ILLF_VRRP for the right ill.
1583 if ((vrrp->vrrp_af == AF_INET6 && ill->ill_isv6) ||
1584 (vrrp->vrrp_af == AF_INET && !ill->ill_isv6)) {
1585 ill->ill_flags |= ILLF_VRRP;
1590 * Process a hardware checksum offload capability negotiation ack received
1591 * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
1592 * of a DL_CAPABILITY_ACK message.
1594 static void
1595 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1597 dl_capability_req_t *ocap;
1598 dl_capab_hcksum_t *ihck, *ohck;
1599 ill_hcksum_capab_t **ill_hcksum;
1600 mblk_t *nmp = NULL;
1601 uint_t sub_dl_cap = isub->dl_cap;
1602 uint8_t *capend;
1604 ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
1606 ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
1609 * Note: range checks here are not absolutely sufficient to
1610 * make us robust against malformed messages sent by drivers;
1611 * this is in keeping with the rest of IP's dlpi handling.
1612 * (Remember, it's coming from something else in the kernel
1613 * address space)
1615 capend = (uint8_t *)(isub + 1) + isub->dl_length;
1616 if (capend > mp->b_wptr) {
1617 cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1618 "malformed sub-capability too long for mblk");
1619 return;
1623 * There are two types of acks we process here:
1624 * 1. acks in reply to a (first form) generic capability req
1625 * (no ENABLE flag set)
1626 * 2. acks in reply to a ENABLE capability req.
1627 * (ENABLE flag set)
1629 ihck = (dl_capab_hcksum_t *)(isub + 1);
1631 if (ihck->hcksum_version != HCKSUM_VERSION_1) {
1632 cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
1633 "unsupported hardware checksum "
1634 "sub-capability (version %d, expected %d)",
1635 ihck->hcksum_version, HCKSUM_VERSION_1);
1636 return;
1639 if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
1640 ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
1641 "checksum capability isn't as expected; pass-thru "
1642 "module(s) detected, discarding capability\n"));
1643 return;
1646 #define CURR_HCKSUM_CAPAB \
1647 (HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 | \
1648 HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
1650 if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
1651 (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
1652 /* do ENABLE processing */
1653 if (*ill_hcksum == NULL) {
1654 *ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
1655 KM_NOSLEEP);
1657 if (*ill_hcksum == NULL) {
1658 cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1659 "could not enable hcksum version %d "
1660 "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
1661 ill->ill_name);
1662 return;
1666 (*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
1667 (*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
1668 ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
1669 ip1dbg(("ill_capability_hcksum_ack: interface %s "
1670 "has enabled hardware checksumming\n ",
1671 ill->ill_name));
1672 } else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
1674 * Enabling hardware checksum offload
1675 * Currently IP supports {TCP,UDP}/IPv4
1676 * partial and full cksum offload and
1677 * IPv4 header checksum offload.
1678 * Allocate new mblk which will
1679 * contain a new capability request
1680 * to enable hardware checksum offload.
1682 uint_t size;
1683 uchar_t *rptr;
1685 size = sizeof (dl_capability_req_t) +
1686 sizeof (dl_capability_sub_t) + isub->dl_length;
1688 if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1689 cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1690 "could not enable hardware cksum for %s (ENOMEM)\n",
1691 ill->ill_name);
1692 return;
1695 rptr = nmp->b_rptr;
1696 /* initialize dl_capability_req_t */
1697 ocap = (dl_capability_req_t *)nmp->b_rptr;
1698 ocap->dl_sub_offset =
1699 sizeof (dl_capability_req_t);
1700 ocap->dl_sub_length =
1701 sizeof (dl_capability_sub_t) +
1702 isub->dl_length;
1703 nmp->b_rptr += sizeof (dl_capability_req_t);
1705 /* initialize dl_capability_sub_t */
1706 bcopy(isub, nmp->b_rptr, sizeof (*isub));
1707 nmp->b_rptr += sizeof (*isub);
1709 /* initialize dl_capab_hcksum_t */
1710 ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
1711 bcopy(ihck, ohck, sizeof (*ihck));
1713 nmp->b_rptr = rptr;
1714 ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
1716 /* Set ENABLE flag */
1717 ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
1718 ohck->hcksum_txflags |= HCKSUM_ENABLE;
1721 * nmp points to a DL_CAPABILITY_REQ message to enable
1722 * hardware checksum acceleration.
1724 ill_capability_send(ill, nmp);
1725 } else {
1726 ip1dbg(("ill_capability_hcksum_ack: interface %s has "
1727 "advertised %x hardware checksum capability flags\n",
1728 ill->ill_name, ihck->hcksum_txflags));
1732 static void
1733 ill_capability_hcksum_reset_fill(ill_t *ill, mblk_t *mp)
1735 dl_capab_hcksum_t *hck_subcap;
1736 dl_capability_sub_t *dl_subcap;
1738 if (!ILL_HCKSUM_CAPABLE(ill))
1739 return;
1741 ASSERT(ill->ill_hcksum_capab != NULL);
1743 dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1744 dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
1745 dl_subcap->dl_length = sizeof (*hck_subcap);
1747 hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
1748 hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
1749 hck_subcap->hcksum_txflags = 0;
1751 mp->b_wptr += sizeof (*dl_subcap) + sizeof (*hck_subcap);
1754 static void
1755 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1757 mblk_t *nmp = NULL;
1758 dl_capability_req_t *oc;
1759 dl_capab_zerocopy_t *zc_ic, *zc_oc;
1760 ill_zerocopy_capab_t **ill_zerocopy_capab;
1761 uint_t sub_dl_cap = isub->dl_cap;
1762 uint8_t *capend;
1764 ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
1766 ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
1769 * Note: range checks here are not absolutely sufficient to
1770 * make us robust against malformed messages sent by drivers;
1771 * this is in keeping with the rest of IP's dlpi handling.
1772 * (Remember, it's coming from something else in the kernel
1773 * address space)
1775 capend = (uint8_t *)(isub + 1) + isub->dl_length;
1776 if (capend > mp->b_wptr) {
1777 cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1778 "malformed sub-capability too long for mblk");
1779 return;
1782 zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
1783 if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
1784 cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
1785 "unsupported ZEROCOPY sub-capability (version %d, "
1786 "expected %d)", zc_ic->zerocopy_version,
1787 ZEROCOPY_VERSION_1);
1788 return;
1791 if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
1792 ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
1793 "capability isn't as expected; pass-thru module(s) "
1794 "detected, discarding capability\n"));
1795 return;
1798 if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
1799 if (*ill_zerocopy_capab == NULL) {
1800 *ill_zerocopy_capab =
1801 kmem_zalloc(sizeof (ill_zerocopy_capab_t),
1802 KM_NOSLEEP);
1804 if (*ill_zerocopy_capab == NULL) {
1805 cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1806 "could not enable Zero-copy version %d "
1807 "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
1808 ill->ill_name);
1809 return;
1813 ip1dbg(("ill_capability_zerocopy_ack: interface %s "
1814 "supports Zero-copy version %d\n", ill->ill_name,
1815 ZEROCOPY_VERSION_1));
1817 (*ill_zerocopy_capab)->ill_zerocopy_version =
1818 zc_ic->zerocopy_version;
1819 (*ill_zerocopy_capab)->ill_zerocopy_flags =
1820 zc_ic->zerocopy_flags;
1822 ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
1823 } else {
1824 uint_t size;
1825 uchar_t *rptr;
1827 size = sizeof (dl_capability_req_t) +
1828 sizeof (dl_capability_sub_t) +
1829 sizeof (dl_capab_zerocopy_t);
1831 if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1832 cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1833 "could not enable zerocopy for %s (ENOMEM)\n",
1834 ill->ill_name);
1835 return;
1838 rptr = nmp->b_rptr;
1839 /* initialize dl_capability_req_t */
1840 oc = (dl_capability_req_t *)rptr;
1841 oc->dl_sub_offset = sizeof (dl_capability_req_t);
1842 oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1843 sizeof (dl_capab_zerocopy_t);
1844 rptr += sizeof (dl_capability_req_t);
1846 /* initialize dl_capability_sub_t */
1847 bcopy(isub, rptr, sizeof (*isub));
1848 rptr += sizeof (*isub);
1850 /* initialize dl_capab_zerocopy_t */
1851 zc_oc = (dl_capab_zerocopy_t *)rptr;
1852 *zc_oc = *zc_ic;
1854 ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
1855 "to enable zero-copy version %d\n", ill->ill_name,
1856 ZEROCOPY_VERSION_1));
1858 /* set VMSAFE_MEM flag */
1859 zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
1861 /* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
1862 ill_capability_send(ill, nmp);
1866 static void
1867 ill_capability_zerocopy_reset_fill(ill_t *ill, mblk_t *mp)
1869 dl_capab_zerocopy_t *zerocopy_subcap;
1870 dl_capability_sub_t *dl_subcap;
1872 if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
1873 return;
1875 ASSERT(ill->ill_zerocopy_capab != NULL);
1877 dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1878 dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
1879 dl_subcap->dl_length = sizeof (*zerocopy_subcap);
1881 zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
1882 zerocopy_subcap->zerocopy_version =
1883 ill->ill_zerocopy_capab->ill_zerocopy_version;
1884 zerocopy_subcap->zerocopy_flags = 0;
1886 mp->b_wptr += sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
1890 * DLD capability
1891 * Refer to dld.h for more information regarding the purpose and usage
1892 * of this capability.
1894 static void
1895 ill_capability_dld_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1897 dl_capab_dld_t *dld_ic, dld;
1898 uint_t sub_dl_cap = isub->dl_cap;
1899 uint8_t *capend;
1900 ill_dld_capab_t *idc;
1902 ASSERT(IAM_WRITER_ILL(ill));
1903 ASSERT(sub_dl_cap == DL_CAPAB_DLD);
1906 * Note: range checks here are not absolutely sufficient to
1907 * make us robust against malformed messages sent by drivers;
1908 * this is in keeping with the rest of IP's dlpi handling.
1909 * (Remember, it's coming from something else in the kernel
1910 * address space)
1912 capend = (uint8_t *)(isub + 1) + isub->dl_length;
1913 if (capend > mp->b_wptr) {
1914 cmn_err(CE_WARN, "ill_capability_dld_ack: "
1915 "malformed sub-capability too long for mblk");
1916 return;
1918 dld_ic = (dl_capab_dld_t *)(isub + 1);
1919 if (dld_ic->dld_version != DLD_CURRENT_VERSION) {
1920 cmn_err(CE_CONT, "ill_capability_dld_ack: "
1921 "unsupported DLD sub-capability (version %d, "
1922 "expected %d)", dld_ic->dld_version,
1923 DLD_CURRENT_VERSION);
1924 return;
1926 if (!dlcapabcheckqid(&dld_ic->dld_mid, ill->ill_lmod_rq)) {
1927 ip1dbg(("ill_capability_dld_ack: mid token for dld "
1928 "capability isn't as expected; pass-thru module(s) "
1929 "detected, discarding capability\n"));
1930 return;
1934 * Copy locally to ensure alignment.
1936 bcopy(dld_ic, &dld, sizeof (dl_capab_dld_t));
1938 if ((idc = ill->ill_dld_capab) == NULL) {
1939 idc = kmem_zalloc(sizeof (ill_dld_capab_t), KM_NOSLEEP);
1940 if (idc == NULL) {
1941 cmn_err(CE_WARN, "ill_capability_dld_ack: "
1942 "could not enable DLD version %d "
1943 "for %s (ENOMEM)\n", DLD_CURRENT_VERSION,
1944 ill->ill_name);
1945 return;
1947 ill->ill_dld_capab = idc;
1949 idc->idc_capab_df = (ip_capab_func_t)dld.dld_capab;
1950 idc->idc_capab_dh = (void *)dld.dld_capab_handle;
1951 ip1dbg(("ill_capability_dld_ack: interface %s "
1952 "supports DLD version %d\n", ill->ill_name, DLD_CURRENT_VERSION));
1954 ill_capability_dld_enable(ill);
1958 * Typically capability negotiation between IP and the driver happens via
1959 * DLPI message exchange. However GLD also offers a direct function call
1960 * mechanism to exchange the DLD_DIRECT_CAPAB and DLD_POLL_CAPAB capabilities,
1961 * But arbitrary function calls into IP or GLD are not permitted, since both
1962 * of them are protected by their own perimeter mechanism. The perimeter can
1963 * be viewed as a coarse lock or serialization mechanism. The hierarchy of
1964 * these perimeters is IP -> MAC. Thus for example to enable the squeue
1965 * polling, IP needs to enter its perimeter, then call ill_mac_perim_enter
1966 * to enter the mac perimeter and then do the direct function calls into
1967 * GLD to enable squeue polling. The ring related callbacks from the mac into
1968 * the stack to add, bind, quiesce, restart or cleanup a ring are all
1969 * protected by the mac perimeter.
1971 static void
1972 ill_mac_perim_enter(ill_t *ill, mac_perim_handle_t *mphp)
1974 ill_dld_capab_t *idc = ill->ill_dld_capab;
1975 int err;
1977 err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mphp,
1978 DLD_ENABLE);
1979 ASSERT(err == 0);
1982 static void
1983 ill_mac_perim_exit(ill_t *ill, mac_perim_handle_t mph)
1985 ill_dld_capab_t *idc = ill->ill_dld_capab;
1986 int err;
1988 err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mph,
1989 DLD_DISABLE);
1990 ASSERT(err == 0);
1993 boolean_t
1994 ill_mac_perim_held(ill_t *ill)
1996 ill_dld_capab_t *idc = ill->ill_dld_capab;
1998 return (idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, NULL,
1999 DLD_QUERY));
2002 static void
2003 ill_capability_direct_enable(ill_t *ill)
2005 ill_dld_capab_t *idc = ill->ill_dld_capab;
2006 ill_dld_direct_t *idd = &idc->idc_direct;
2007 dld_capab_direct_t direct;
2008 int rc;
2010 ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
2012 bzero(&direct, sizeof (direct));
2013 direct.di_rx_cf = (uintptr_t)ip_input;
2014 direct.di_rx_ch = ill;
2016 rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT, &direct,
2017 DLD_ENABLE);
2018 if (rc == 0) {
2019 idd->idd_tx_df = (ip_dld_tx_t)direct.di_tx_df;
2020 idd->idd_tx_dh = direct.di_tx_dh;
2021 idd->idd_tx_cb_df = (ip_dld_callb_t)direct.di_tx_cb_df;
2022 idd->idd_tx_cb_dh = direct.di_tx_cb_dh;
2023 idd->idd_tx_fctl_df = (ip_dld_fctl_t)direct.di_tx_fctl_df;
2024 idd->idd_tx_fctl_dh = direct.di_tx_fctl_dh;
2025 ASSERT(idd->idd_tx_cb_df != NULL);
2026 ASSERT(idd->idd_tx_fctl_df != NULL);
2027 ASSERT(idd->idd_tx_df != NULL);
2029 * One time registration of flow enable callback function
2031 ill->ill_flownotify_mh = idd->idd_tx_cb_df(idd->idd_tx_cb_dh,
2032 ill_flow_enable, ill);
2033 ill->ill_capabilities |= ILL_CAPAB_DLD_DIRECT;
2034 DTRACE_PROBE1(direct_on, (ill_t *), ill);
2035 } else {
2036 cmn_err(CE_WARN, "warning: could not enable DIRECT "
2037 "capability, rc = %d\n", rc);
2038 DTRACE_PROBE2(direct_off, (ill_t *), ill, (int), rc);
2042 static void
2043 ill_capability_poll_enable(ill_t *ill)
2045 ill_dld_capab_t *idc = ill->ill_dld_capab;
2046 dld_capab_poll_t poll;
2047 int rc;
2049 ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
2051 bzero(&poll, sizeof (poll));
2052 poll.poll_ring_add_cf = (uintptr_t)ip_squeue_add_ring;
2053 poll.poll_ring_remove_cf = (uintptr_t)ip_squeue_clean_ring;
2054 poll.poll_ring_quiesce_cf = (uintptr_t)ip_squeue_quiesce_ring;
2055 poll.poll_ring_restart_cf = (uintptr_t)ip_squeue_restart_ring;
2056 poll.poll_ring_bind_cf = (uintptr_t)ip_squeue_bind_ring;
2057 poll.poll_ring_ch = ill;
2058 rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL, &poll,
2059 DLD_ENABLE);
2060 if (rc == 0) {
2061 ill->ill_capabilities |= ILL_CAPAB_DLD_POLL;
2062 DTRACE_PROBE1(poll_on, (ill_t *), ill);
2063 } else {
2064 ip1dbg(("warning: could not enable POLL "
2065 "capability, rc = %d\n", rc));
2066 DTRACE_PROBE2(poll_off, (ill_t *), ill, (int), rc);
2071 * Enable the LSO capability.
2073 static void
2074 ill_capability_lso_enable(ill_t *ill)
2076 ill_dld_capab_t *idc = ill->ill_dld_capab;
2077 dld_capab_lso_t lso;
2078 int rc;
2080 ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
2082 if (ill->ill_lso_capab == NULL) {
2083 ill->ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
2084 KM_NOSLEEP);
2085 if (ill->ill_lso_capab == NULL) {
2086 cmn_err(CE_WARN, "ill_capability_lso_enable: "
2087 "could not enable LSO for %s (ENOMEM)\n",
2088 ill->ill_name);
2089 return;
2093 bzero(&lso, sizeof (lso));
2094 if ((rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO, &lso,
2095 DLD_ENABLE)) == 0) {
2096 ill->ill_lso_capab->ill_lso_flags = lso.lso_flags;
2097 ill->ill_lso_capab->ill_lso_max = lso.lso_max;
2098 ill->ill_capabilities |= ILL_CAPAB_LSO;
2099 ip1dbg(("ill_capability_lso_enable: interface %s "
2100 "has enabled LSO\n ", ill->ill_name));
2101 } else {
2102 kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
2103 ill->ill_lso_capab = NULL;
2104 DTRACE_PROBE2(lso_off, (ill_t *), ill, (int), rc);
2108 static void
2109 ill_capability_dld_enable(ill_t *ill)
2111 mac_perim_handle_t mph;
2113 ASSERT(IAM_WRITER_ILL(ill));
2115 if (ill->ill_isv6)
2116 return;
2118 ill_mac_perim_enter(ill, &mph);
2119 if (!ill->ill_isv6) {
2120 ill_capability_direct_enable(ill);
2121 ill_capability_poll_enable(ill);
2122 ill_capability_lso_enable(ill);
2124 ill->ill_capabilities |= ILL_CAPAB_DLD;
2125 ill_mac_perim_exit(ill, mph);
2128 static void
2129 ill_capability_dld_disable(ill_t *ill)
2131 ill_dld_capab_t *idc;
2132 ill_dld_direct_t *idd;
2133 mac_perim_handle_t mph;
2135 ASSERT(IAM_WRITER_ILL(ill));
2137 if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
2138 return;
2140 ill_mac_perim_enter(ill, &mph);
2142 idc = ill->ill_dld_capab;
2143 if ((ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT) != 0) {
2145 * For performance we avoid locks in the transmit data path
2146 * and don't maintain a count of the number of threads using
2147 * direct calls. Thus some threads could be using direct
2148 * transmit calls to GLD, even after the capability mechanism
2149 * turns it off. This is still safe since the handles used in
2150 * the direct calls continue to be valid until the unplumb is
2151 * completed. Remove the callback that was added (1-time) at
2152 * capab enable time.
2154 mutex_enter(&ill->ill_lock);
2155 ill->ill_capabilities &= ~ILL_CAPAB_DLD_DIRECT;
2156 mutex_exit(&ill->ill_lock);
2157 if (ill->ill_flownotify_mh != NULL) {
2158 idd = &idc->idc_direct;
2159 idd->idd_tx_cb_df(idd->idd_tx_cb_dh, NULL,
2160 ill->ill_flownotify_mh);
2161 ill->ill_flownotify_mh = NULL;
2163 (void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT,
2164 NULL, DLD_DISABLE);
2167 if ((ill->ill_capabilities & ILL_CAPAB_DLD_POLL) != 0) {
2168 ill->ill_capabilities &= ~ILL_CAPAB_DLD_POLL;
2169 ip_squeue_clean_all(ill);
2170 (void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL,
2171 NULL, DLD_DISABLE);
2174 if ((ill->ill_capabilities & ILL_CAPAB_LSO) != 0) {
2175 ASSERT(ill->ill_lso_capab != NULL);
2177 * Clear the capability flag for LSO but retain the
2178 * ill_lso_capab structure since it's possible that another
2179 * thread is still referring to it. The structure only gets
2180 * deallocated when we destroy the ill.
2183 ill->ill_capabilities &= ~ILL_CAPAB_LSO;
2184 (void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO,
2185 NULL, DLD_DISABLE);
2188 ill->ill_capabilities &= ~ILL_CAPAB_DLD;
2189 ill_mac_perim_exit(ill, mph);
2193 * Capability Negotiation protocol
2195 * We don't wait for DLPI capability operations to finish during interface
2196 * bringup or teardown. Doing so would introduce more asynchrony and the
2197 * interface up/down operations will need multiple return and restarts.
2198 * Instead the 'ipsq_current_ipif' of the ipsq is not cleared as long as
2199 * the 'ill_dlpi_deferred' chain is non-empty. This ensures that the next
2200 * exclusive operation won't start until the DLPI operations of the previous
2201 * exclusive operation complete.
2203 * The capability state machine is shown below.
2205 * state next state event, action
2207 * IDCS_UNKNOWN IDCS_PROBE_SENT ill_capability_probe
2208 * IDCS_PROBE_SENT IDCS_OK ill_capability_ack
2209 * IDCS_PROBE_SENT IDCS_FAILED ip_rput_dlpi_writer (nack)
2210 * IDCS_OK IDCS_RENEG Receipt of DL_NOTE_CAPAB_RENEG
2211 * IDCS_OK IDCS_RESET_SENT ill_capability_reset
2212 * IDCS_RESET_SENT IDCS_UNKNOWN ill_capability_ack_thr
2213 * IDCS_RENEG IDCS_PROBE_SENT ill_capability_ack_thr ->
2214 * ill_capability_probe.
2218 * Dedicated thread started from ip_stack_init that handles capability
2219 * disable. This thread ensures the taskq dispatch does not fail by waiting
2220 * for resources using TQ_SLEEP. The taskq mechanism is used to ensure
2221 * that direct calls to DLD are done in a cv_waitable context.
2223 void
2224 ill_taskq_dispatch(ip_stack_t *ipst)
2226 callb_cpr_t cprinfo;
2227 char name[64];
2228 mblk_t *mp;
2230 (void) snprintf(name, sizeof (name), "ill_taskq_dispatch_%d",
2231 ipst->ips_netstack->netstack_stackid);
2232 CALLB_CPR_INIT(&cprinfo, &ipst->ips_capab_taskq_lock, callb_generic_cpr,
2233 name);
2234 mutex_enter(&ipst->ips_capab_taskq_lock);
2236 for (;;) {
2237 mp = ipst->ips_capab_taskq_head;
2238 while (mp != NULL) {
2239 ipst->ips_capab_taskq_head = mp->b_next;
2240 if (ipst->ips_capab_taskq_head == NULL)
2241 ipst->ips_capab_taskq_tail = NULL;
2242 mutex_exit(&ipst->ips_capab_taskq_lock);
2243 mp->b_next = NULL;
2245 VERIFY(taskq_dispatch(system_taskq,
2246 ill_capability_ack_thr, mp, TQ_SLEEP) != 0);
2247 mutex_enter(&ipst->ips_capab_taskq_lock);
2248 mp = ipst->ips_capab_taskq_head;
2251 if (ipst->ips_capab_taskq_quit)
2252 break;
2253 CALLB_CPR_SAFE_BEGIN(&cprinfo);
2254 cv_wait(&ipst->ips_capab_taskq_cv, &ipst->ips_capab_taskq_lock);
2255 CALLB_CPR_SAFE_END(&cprinfo, &ipst->ips_capab_taskq_lock);
2257 VERIFY(ipst->ips_capab_taskq_head == NULL);
2258 VERIFY(ipst->ips_capab_taskq_tail == NULL);
2259 CALLB_CPR_EXIT(&cprinfo);
2260 thread_exit();
2264 * Consume a new-style hardware capabilities negotiation ack.
2265 * Called via taskq on receipt of DL_CAPABILITY_ACK.
2267 static void
2268 ill_capability_ack_thr(void *arg)
2270 mblk_t *mp = arg;
2271 dl_capability_ack_t *capp;
2272 dl_capability_sub_t *subp, *endp;
2273 ill_t *ill;
2274 boolean_t reneg;
2276 ill = (ill_t *)mp->b_prev;
2277 mp->b_prev = NULL;
2279 VERIFY(ipsq_enter(ill, B_FALSE, CUR_OP) == B_TRUE);
2281 if (ill->ill_dlpi_capab_state == IDCS_RESET_SENT ||
2282 ill->ill_dlpi_capab_state == IDCS_RENEG) {
2284 * We have received the ack for our DL_CAPAB reset request.
2285 * There isnt' anything in the message that needs processing.
2286 * All message based capabilities have been disabled, now
2287 * do the function call based capability disable.
2289 reneg = ill->ill_dlpi_capab_state == IDCS_RENEG;
2290 ill_capability_dld_disable(ill);
2291 ill->ill_dlpi_capab_state = IDCS_UNKNOWN;
2292 if (reneg)
2293 ill_capability_probe(ill);
2294 goto done;
2297 if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
2298 ill->ill_dlpi_capab_state = IDCS_OK;
2300 capp = (dl_capability_ack_t *)mp->b_rptr;
2302 if (capp->dl_sub_length == 0) {
2303 /* no new-style capabilities */
2304 goto done;
2307 /* make sure the driver supplied correct dl_sub_length */
2308 if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
2309 ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
2310 "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
2311 goto done;
2314 #define SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
2316 * There are sub-capabilities. Process the ones we know about.
2317 * Loop until we don't have room for another sub-cap header..
2319 for (subp = SC(capp, capp->dl_sub_offset),
2320 endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
2321 subp <= endp;
2322 subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
2324 switch (subp->dl_cap) {
2325 case DL_CAPAB_ID_WRAPPER:
2326 ill_capability_id_ack(ill, mp, subp);
2327 break;
2328 default:
2329 ill_capability_dispatch(ill, mp, subp);
2330 break;
2333 #undef SC
2334 done:
2335 inet_freemsg(mp);
2336 ill_capability_done(ill);
2337 ipsq_exit(ill->ill_phyint->phyint_ipsq);
2341 * This needs to be started in a taskq thread to provide a cv_waitable
2342 * context.
2344 void
2345 ill_capability_ack(ill_t *ill, mblk_t *mp)
2347 ip_stack_t *ipst = ill->ill_ipst;
2349 mp->b_prev = (mblk_t *)ill;
2350 ASSERT(mp->b_next == NULL);
2352 if (taskq_dispatch(system_taskq, ill_capability_ack_thr, mp,
2353 TQ_NOSLEEP) != 0)
2354 return;
2357 * The taskq dispatch failed. Signal the ill_taskq_dispatch thread
2358 * which will do the dispatch using TQ_SLEEP to guarantee success.
2360 mutex_enter(&ipst->ips_capab_taskq_lock);
2361 if (ipst->ips_capab_taskq_head == NULL) {
2362 ASSERT(ipst->ips_capab_taskq_tail == NULL);
2363 ipst->ips_capab_taskq_head = mp;
2364 } else {
2365 ipst->ips_capab_taskq_tail->b_next = mp;
2367 ipst->ips_capab_taskq_tail = mp;
2369 cv_signal(&ipst->ips_capab_taskq_cv);
2370 mutex_exit(&ipst->ips_capab_taskq_lock);
2374 * This routine is called to scan the fragmentation reassembly table for
2375 * the specified ILL for any packets that are starting to smell.
2376 * dead_interval is the maximum time in seconds that will be tolerated. It
2377 * will either be the value specified in ip_g_frag_timeout, or zero if the
2378 * ILL is shutting down and it is time to blow everything off.
2380 * It returns the number of seconds (as a time_t) that the next frag timer
2381 * should be scheduled for, 0 meaning that the timer doesn't need to be
2382 * re-started. Note that the method of calculating next_timeout isn't
2383 * entirely accurate since time will flow between the time we grab
2384 * current_time and the time we schedule the next timeout. This isn't a
2385 * big problem since this is the timer for sending an ICMP reassembly time
2386 * exceeded messages, and it doesn't have to be exactly accurate.
2388 * This function is
2389 * sometimes called as writer, although this is not required.
2391 time_t
2392 ill_frag_timeout(ill_t *ill, time_t dead_interval)
2394 ipfb_t *ipfb;
2395 ipfb_t *endp;
2396 ipf_t *ipf;
2397 ipf_t *ipfnext;
2398 mblk_t *mp;
2399 time_t current_time = gethrestime_sec();
2400 time_t next_timeout = 0;
2401 uint32_t hdr_length;
2402 mblk_t *send_icmp_head;
2403 mblk_t *send_icmp_head_v6;
2404 ip_stack_t *ipst = ill->ill_ipst;
2405 ip_recv_attr_t iras;
2407 bzero(&iras, sizeof (iras));
2408 iras.ira_flags = 0;
2409 iras.ira_ill = iras.ira_rill = ill;
2410 iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
2411 iras.ira_rifindex = iras.ira_ruifindex;
2413 ipfb = ill->ill_frag_hash_tbl;
2414 if (ipfb == NULL)
2415 return (B_FALSE);
2416 endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
2417 /* Walk the frag hash table. */
2418 for (; ipfb < endp; ipfb++) {
2419 send_icmp_head = NULL;
2420 send_icmp_head_v6 = NULL;
2421 mutex_enter(&ipfb->ipfb_lock);
2422 while ((ipf = ipfb->ipfb_ipf) != 0) {
2423 time_t frag_time = current_time - ipf->ipf_timestamp;
2424 time_t frag_timeout;
2426 if (frag_time < dead_interval) {
2428 * There are some outstanding fragments
2429 * that will timeout later. Make note of
2430 * the time so that we can reschedule the
2431 * next timeout appropriately.
2433 frag_timeout = dead_interval - frag_time;
2434 if (next_timeout == 0 ||
2435 frag_timeout < next_timeout) {
2436 next_timeout = frag_timeout;
2438 break;
2440 /* Time's up. Get it out of here. */
2441 hdr_length = ipf->ipf_nf_hdr_len;
2442 ipfnext = ipf->ipf_hash_next;
2443 if (ipfnext)
2444 ipfnext->ipf_ptphn = ipf->ipf_ptphn;
2445 *ipf->ipf_ptphn = ipfnext;
2446 mp = ipf->ipf_mp->b_cont;
2447 for (; mp; mp = mp->b_cont) {
2448 /* Extra points for neatness. */
2449 IP_REASS_SET_START(mp, 0);
2450 IP_REASS_SET_END(mp, 0);
2452 mp = ipf->ipf_mp->b_cont;
2453 atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
2454 ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
2455 ipfb->ipfb_count -= ipf->ipf_count;
2456 ASSERT(ipfb->ipfb_frag_pkts > 0);
2457 ipfb->ipfb_frag_pkts--;
2459 * We do not send any icmp message from here because
2460 * we currently are holding the ipfb_lock for this
2461 * hash chain. If we try and send any icmp messages
2462 * from here we may end up via a put back into ip
2463 * trying to get the same lock, causing a recursive
2464 * mutex panic. Instead we build a list and send all
2465 * the icmp messages after we have dropped the lock.
2467 if (ill->ill_isv6) {
2468 if (hdr_length != 0) {
2469 mp->b_next = send_icmp_head_v6;
2470 send_icmp_head_v6 = mp;
2471 } else {
2472 freemsg(mp);
2474 } else {
2475 if (hdr_length != 0) {
2476 mp->b_next = send_icmp_head;
2477 send_icmp_head = mp;
2478 } else {
2479 freemsg(mp);
2482 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2483 ip_drop_input("ipIfStatsReasmFails", ipf->ipf_mp, ill);
2484 freeb(ipf->ipf_mp);
2486 mutex_exit(&ipfb->ipfb_lock);
2488 * Now need to send any icmp messages that we delayed from
2489 * above.
2491 while (send_icmp_head_v6 != NULL) {
2492 ip6_t *ip6h;
2494 mp = send_icmp_head_v6;
2495 send_icmp_head_v6 = send_icmp_head_v6->b_next;
2496 mp->b_next = NULL;
2497 ip6h = (ip6_t *)mp->b_rptr;
2498 iras.ira_flags = 0;
2500 * This will result in an incorrect ALL_ZONES zoneid
2501 * for multicast packets, but we
2502 * don't send ICMP errors for those in any case.
2504 iras.ira_zoneid =
2505 ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
2506 ill, ipst);
2507 ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2508 icmp_time_exceeded_v6(mp,
2509 ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
2510 &iras);
2511 ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2513 while (send_icmp_head != NULL) {
2514 ipaddr_t dst;
2516 mp = send_icmp_head;
2517 send_icmp_head = send_icmp_head->b_next;
2518 mp->b_next = NULL;
2520 dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
2522 iras.ira_flags = IRAF_IS_IPV4;
2524 * This will result in an incorrect ALL_ZONES zoneid
2525 * for broadcast and multicast packets, but we
2526 * don't send ICMP errors for those in any case.
2528 iras.ira_zoneid = ipif_lookup_addr_zoneid(dst,
2529 ill, ipst);
2530 ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2531 icmp_time_exceeded(mp,
2532 ICMP_REASSEMBLY_TIME_EXCEEDED, &iras);
2533 ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2537 * A non-dying ILL will use the return value to decide whether to
2538 * restart the frag timer, and for how long.
2540 return (next_timeout);
2544 * This routine is called when the approximate count of mblk memory used
2545 * for the specified ILL has exceeded max_count.
2547 void
2548 ill_frag_prune(ill_t *ill, uint_t max_count)
2550 ipfb_t *ipfb;
2551 ipf_t *ipf;
2552 size_t count;
2553 clock_t now;
2556 * If we are here within ip_min_frag_prune_time msecs remove
2557 * ill_frag_free_num_pkts oldest packets from each bucket and increment
2558 * ill_frag_free_num_pkts.
2560 mutex_enter(&ill->ill_lock);
2561 now = ddi_get_lbolt();
2562 if (TICK_TO_MSEC(now - ill->ill_last_frag_clean_time) <=
2563 (ip_min_frag_prune_time != 0 ?
2564 ip_min_frag_prune_time : msec_per_tick)) {
2566 ill->ill_frag_free_num_pkts++;
2568 } else {
2569 ill->ill_frag_free_num_pkts = 0;
2571 ill->ill_last_frag_clean_time = now;
2572 mutex_exit(&ill->ill_lock);
2575 * free ill_frag_free_num_pkts oldest packets from each bucket.
2577 if (ill->ill_frag_free_num_pkts != 0) {
2578 int ix;
2580 for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2581 ipfb = &ill->ill_frag_hash_tbl[ix];
2582 mutex_enter(&ipfb->ipfb_lock);
2583 if (ipfb->ipfb_ipf != NULL) {
2584 ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
2585 ill->ill_frag_free_num_pkts);
2587 mutex_exit(&ipfb->ipfb_lock);
2591 * While the reassembly list for this ILL is too big, prune a fragment
2592 * queue by age, oldest first.
2594 while (ill->ill_frag_count > max_count) {
2595 int ix;
2596 ipfb_t *oipfb = NULL;
2597 uint_t oldest = UINT_MAX;
2599 count = 0;
2600 for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2601 ipfb = &ill->ill_frag_hash_tbl[ix];
2602 mutex_enter(&ipfb->ipfb_lock);
2603 ipf = ipfb->ipfb_ipf;
2604 if (ipf != NULL && ipf->ipf_gen < oldest) {
2605 oldest = ipf->ipf_gen;
2606 oipfb = ipfb;
2608 count += ipfb->ipfb_count;
2609 mutex_exit(&ipfb->ipfb_lock);
2611 if (oipfb == NULL)
2612 break;
2614 if (count <= max_count)
2615 return; /* Somebody beat us to it, nothing to do */
2616 mutex_enter(&oipfb->ipfb_lock);
2617 ipf = oipfb->ipfb_ipf;
2618 if (ipf != NULL) {
2619 ill_frag_free_pkts(ill, oipfb, ipf, 1);
2621 mutex_exit(&oipfb->ipfb_lock);
2626 * free 'free_cnt' fragmented packets starting at ipf.
2628 void
2629 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
2631 size_t count;
2632 mblk_t *mp;
2633 mblk_t *tmp;
2634 ipf_t **ipfp = ipf->ipf_ptphn;
2636 ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
2637 ASSERT(ipfp != NULL);
2638 ASSERT(ipf != NULL);
2640 while (ipf != NULL && free_cnt-- > 0) {
2641 count = ipf->ipf_count;
2642 mp = ipf->ipf_mp;
2643 ipf = ipf->ipf_hash_next;
2644 for (tmp = mp; tmp; tmp = tmp->b_cont) {
2645 IP_REASS_SET_START(tmp, 0);
2646 IP_REASS_SET_END(tmp, 0);
2648 atomic_add_32(&ill->ill_frag_count, -count);
2649 ASSERT(ipfb->ipfb_count >= count);
2650 ipfb->ipfb_count -= count;
2651 ASSERT(ipfb->ipfb_frag_pkts > 0);
2652 ipfb->ipfb_frag_pkts--;
2653 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2654 ip_drop_input("ipIfStatsReasmFails", mp, ill);
2655 freemsg(mp);
2658 if (ipf)
2659 ipf->ipf_ptphn = ipfp;
2660 ipfp[0] = ipf;
2664 * Helper function for ill_forward_set().
2666 static void
2667 ill_forward_set_on_ill(ill_t *ill, boolean_t enable)
2669 ip_stack_t *ipst = ill->ill_ipst;
2671 ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2673 ip1dbg(("ill_forward_set: %s %s forwarding on %s",
2674 (enable ? "Enabling" : "Disabling"),
2675 (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
2676 mutex_enter(&ill->ill_lock);
2677 if (enable)
2678 ill->ill_flags |= ILLF_ROUTER;
2679 else
2680 ill->ill_flags &= ~ILLF_ROUTER;
2681 mutex_exit(&ill->ill_lock);
2682 if (ill->ill_isv6)
2683 ill_set_nce_router_flags(ill, enable);
2684 /* Notify routing socket listeners of this change. */
2685 if (ill->ill_ipif != NULL)
2686 ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
2690 * Set an ill's ILLF_ROUTER flag appropriately. Send up RTS_IFINFO routing
2691 * socket messages for each interface whose flags we change.
2694 ill_forward_set(ill_t *ill, boolean_t enable)
2696 ipmp_illgrp_t *illg;
2697 ip_stack_t *ipst = ill->ill_ipst;
2699 ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2701 if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
2702 (!enable && !(ill->ill_flags & ILLF_ROUTER)))
2703 return (0);
2705 if (IS_LOOPBACK(ill))
2706 return (EINVAL);
2708 if (enable && ill->ill_allowed_ips_cnt > 0)
2709 return (EPERM);
2711 if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
2713 * Update all of the interfaces in the group.
2715 illg = ill->ill_grp;
2716 ill = list_head(&illg->ig_if);
2717 for (; ill != NULL; ill = list_next(&illg->ig_if, ill))
2718 ill_forward_set_on_ill(ill, enable);
2721 * Update the IPMP meta-interface.
2723 ill_forward_set_on_ill(ipmp_illgrp_ipmp_ill(illg), enable);
2724 return (0);
2727 ill_forward_set_on_ill(ill, enable);
2728 return (0);
2732 * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
2733 * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
2734 * set or clear.
2736 static void
2737 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
2739 ipif_t *ipif;
2740 ncec_t *ncec;
2741 nce_t *nce;
2743 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
2745 * NOTE: we match across the illgrp because nce's for
2746 * addresses on IPMP interfaces have an nce_ill that points to
2747 * the bound underlying ill.
2749 nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
2750 if (nce != NULL) {
2751 ncec = nce->nce_common;
2752 mutex_enter(&ncec->ncec_lock);
2753 if (enable)
2754 ncec->ncec_flags |= NCE_F_ISROUTER;
2755 else
2756 ncec->ncec_flags &= ~NCE_F_ISROUTER;
2757 mutex_exit(&ncec->ncec_lock);
2758 nce_refrele(nce);
2764 * Intializes the context structure and returns the first ill in the list
2765 * cuurently start_list and end_list can have values:
2766 * MAX_G_HEADS Traverse both IPV4 and IPV6 lists.
2767 * IP_V4_G_HEAD Traverse IPV4 list only.
2768 * IP_V6_G_HEAD Traverse IPV6 list only.
2772 * We don't check for CONDEMNED ills here. Caller must do that if
2773 * necessary under the ill lock.
2775 ill_t *
2776 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
2777 ip_stack_t *ipst)
2779 ill_if_t *ifp;
2780 ill_t *ill;
2781 avl_tree_t *avl_tree;
2783 ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
2784 ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
2787 * setup the lists to search
2789 if (end_list != MAX_G_HEADS) {
2790 ctx->ctx_current_list = start_list;
2791 ctx->ctx_last_list = end_list;
2792 } else {
2793 ctx->ctx_last_list = MAX_G_HEADS - 1;
2794 ctx->ctx_current_list = 0;
2797 while (ctx->ctx_current_list <= ctx->ctx_last_list) {
2798 ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2799 if (ifp != (ill_if_t *)
2800 &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2801 avl_tree = &ifp->illif_avl_by_ppa;
2802 ill = avl_first(avl_tree);
2804 * ill is guaranteed to be non NULL or ifp should have
2805 * not existed.
2807 ASSERT(ill != NULL);
2808 return (ill);
2810 ctx->ctx_current_list++;
2813 return (NULL);
2817 * returns the next ill in the list. ill_first() must have been called
2818 * before calling ill_next() or bad things will happen.
2822 * We don't check for CONDEMNED ills here. Caller must do that if
2823 * necessary under the ill lock.
2825 ill_t *
2826 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
2828 ill_if_t *ifp;
2829 ill_t *ill;
2830 ip_stack_t *ipst = lastill->ill_ipst;
2832 ASSERT(lastill->ill_ifptr != (ill_if_t *)
2833 &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
2834 if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
2835 AVL_AFTER)) != NULL) {
2836 return (ill);
2839 /* goto next ill_ifp in the list. */
2840 ifp = lastill->ill_ifptr->illif_next;
2842 /* make sure not at end of circular list */
2843 while (ifp ==
2844 (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2845 if (++ctx->ctx_current_list > ctx->ctx_last_list)
2846 return (NULL);
2847 ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2850 return (avl_first(&ifp->illif_avl_by_ppa));
2854 * Check interface name for correct format: [a-zA-Z]+[a-zA-Z0-9._]*[0-9]+
2855 * The final number (PPA) must not have any leading zeros. Upon success, a
2856 * pointer to the start of the PPA is returned; otherwise NULL is returned.
2858 static char *
2859 ill_get_ppa_ptr(char *name)
2861 int namelen = strlen(name);
2862 int end_ndx = namelen - 1;
2863 int ppa_ndx, i;
2866 * Check that the first character is [a-zA-Z], and that the last
2867 * character is [0-9].
2869 if (namelen == 0 || !isalpha(name[0]) || !isdigit(name[end_ndx]))
2870 return (NULL);
2873 * Set `ppa_ndx' to the PPA start, and check for leading zeroes.
2875 for (ppa_ndx = end_ndx; ppa_ndx > 0; ppa_ndx--)
2876 if (!isdigit(name[ppa_ndx - 1]))
2877 break;
2879 if (name[ppa_ndx] == '0' && ppa_ndx < end_ndx)
2880 return (NULL);
2883 * Check that the intermediate characters are [a-z0-9.]
2885 for (i = 1; i < ppa_ndx; i++) {
2886 if (!isalpha(name[i]) && !isdigit(name[i]) &&
2887 name[i] != '.' && name[i] != '_') {
2888 return (NULL);
2892 return (name + ppa_ndx);
2896 * use avl tree to locate the ill.
2898 static ill_t *
2899 ill_find_by_name(char *name, boolean_t isv6, ip_stack_t *ipst)
2901 char *ppa_ptr = NULL;
2902 int len;
2903 uint_t ppa;
2904 ill_t *ill = NULL;
2905 ill_if_t *ifp;
2906 int list;
2909 * get ppa ptr
2911 if (isv6)
2912 list = IP_V6_G_HEAD;
2913 else
2914 list = IP_V4_G_HEAD;
2916 if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
2917 return (NULL);
2920 len = ppa_ptr - name + 1;
2922 ppa = stoi(&ppa_ptr);
2924 ifp = IP_VX_ILL_G_LIST(list, ipst);
2926 while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
2928 * match is done on len - 1 as the name is not null
2929 * terminated it contains ppa in addition to the interface
2930 * name.
2932 if ((ifp->illif_name_len == len) &&
2933 bcmp(ifp->illif_name, name, len - 1) == 0) {
2934 break;
2935 } else {
2936 ifp = ifp->illif_next;
2940 if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
2942 * Even the interface type does not exist.
2944 return (NULL);
2947 ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
2948 if (ill != NULL) {
2949 mutex_enter(&ill->ill_lock);
2950 if (ILL_CAN_LOOKUP(ill)) {
2951 ill_refhold_locked(ill);
2952 mutex_exit(&ill->ill_lock);
2953 return (ill);
2955 mutex_exit(&ill->ill_lock);
2957 return (NULL);
2961 * comparison function for use with avl.
2963 static int
2964 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
2966 uint_t ppa;
2967 uint_t ill_ppa;
2969 ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
2971 ppa = *((uint_t *)ppa_ptr);
2972 ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
2974 * We want the ill with the lowest ppa to be on the
2975 * top.
2977 if (ill_ppa < ppa)
2978 return (1);
2979 if (ill_ppa > ppa)
2980 return (-1);
2981 return (0);
2985 * remove an interface type from the global list.
2987 static void
2988 ill_delete_interface_type(ill_if_t *interface)
2990 ASSERT(interface != NULL);
2991 ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
2993 avl_destroy(&interface->illif_avl_by_ppa);
2994 if (interface->illif_ppa_arena != NULL)
2995 vmem_destroy(interface->illif_ppa_arena);
2997 remque(interface);
2999 mi_free(interface);
3003 * remove ill from the global list.
3005 static void
3006 ill_glist_delete(ill_t *ill)
3008 ip_stack_t *ipst;
3009 phyint_t *phyi;
3011 if (ill == NULL)
3012 return;
3013 ipst = ill->ill_ipst;
3014 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
3017 * If the ill was never inserted into the AVL tree
3018 * we skip the if branch.
3020 if (ill->ill_ifptr != NULL) {
3022 * remove from AVL tree and free ppa number
3024 avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
3026 if (ill->ill_ifptr->illif_ppa_arena != NULL) {
3027 vmem_free(ill->ill_ifptr->illif_ppa_arena,
3028 (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3030 if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
3031 ill_delete_interface_type(ill->ill_ifptr);
3035 * Indicate ill is no longer in the list.
3037 ill->ill_ifptr = NULL;
3038 ill->ill_name_length = 0;
3039 ill->ill_name[0] = '\0';
3040 ill->ill_ppa = UINT_MAX;
3043 /* Generate one last event for this ill. */
3044 ill_nic_event_dispatch(ill, 0, NE_UNPLUMB, ill->ill_name,
3045 ill->ill_name_length);
3047 ASSERT(ill->ill_phyint != NULL);
3048 phyi = ill->ill_phyint;
3049 ill->ill_phyint = NULL;
3052 * ill_init allocates a phyint always to store the copy
3053 * of flags relevant to phyint. At that point in time, we could
3054 * not assign the name and hence phyint_illv4/v6 could not be
3055 * initialized. Later in ipif_set_values, we assign the name to
3056 * the ill, at which point in time we assign phyint_illv4/v6.
3057 * Thus we don't rely on phyint_illv6 to be initialized always.
3059 if (ill->ill_flags & ILLF_IPV6)
3060 phyi->phyint_illv6 = NULL;
3061 else
3062 phyi->phyint_illv4 = NULL;
3064 if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL) {
3065 rw_exit(&ipst->ips_ill_g_lock);
3066 return;
3070 * There are no ills left on this phyint; pull it out of the phyint
3071 * avl trees, and free it.
3073 if (phyi->phyint_ifindex > 0) {
3074 avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3075 phyi);
3076 avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
3077 phyi);
3079 rw_exit(&ipst->ips_ill_g_lock);
3081 phyint_free(phyi);
3085 * allocate a ppa, if the number of plumbed interfaces of this type are
3086 * less than ill_no_arena do a linear search to find a unused ppa.
3087 * When the number goes beyond ill_no_arena switch to using an arena.
3088 * Note: ppa value of zero cannot be allocated from vmem_arena as it
3089 * is the return value for an error condition, so allocation starts at one
3090 * and is decremented by one.
3092 static int
3093 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
3095 ill_t *tmp_ill;
3096 uint_t start, end;
3097 int ppa;
3099 if (ifp->illif_ppa_arena == NULL &&
3100 (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
3102 * Create an arena.
3104 ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
3105 (void *)1, UINT_MAX - 1, 1, NULL, NULL,
3106 NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
3107 /* allocate what has already been assigned */
3108 for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
3109 tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
3110 tmp_ill, AVL_AFTER)) {
3111 ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3112 1, /* size */
3113 1, /* align/quantum */
3114 0, /* phase */
3115 0, /* nocross */
3116 /* minaddr */
3117 (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
3118 /* maxaddr */
3119 (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
3120 VM_NOSLEEP|VM_FIRSTFIT);
3121 if (ppa == 0) {
3122 ip1dbg(("ill_alloc_ppa: ppa allocation"
3123 " failed while switching"));
3124 vmem_destroy(ifp->illif_ppa_arena);
3125 ifp->illif_ppa_arena = NULL;
3126 break;
3131 if (ifp->illif_ppa_arena != NULL) {
3132 if (ill->ill_ppa == UINT_MAX) {
3133 ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
3134 1, VM_NOSLEEP|VM_FIRSTFIT);
3135 if (ppa == 0)
3136 return (EAGAIN);
3137 ill->ill_ppa = --ppa;
3138 } else {
3139 ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3140 1, /* size */
3141 1, /* align/quantum */
3142 0, /* phase */
3143 0, /* nocross */
3144 (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
3145 (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
3146 VM_NOSLEEP|VM_FIRSTFIT);
3148 * Most likely the allocation failed because
3149 * the requested ppa was in use.
3151 if (ppa == 0)
3152 return (EEXIST);
3154 return (0);
3158 * No arena is in use and not enough (>ill_no_arena) interfaces have
3159 * been plumbed to create one. Do a linear search to get a unused ppa.
3161 if (ill->ill_ppa == UINT_MAX) {
3162 end = UINT_MAX - 1;
3163 start = 0;
3164 } else {
3165 end = start = ill->ill_ppa;
3168 tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
3169 while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
3170 if (start++ >= end) {
3171 if (ill->ill_ppa == UINT_MAX)
3172 return (EAGAIN);
3173 else
3174 return (EEXIST);
3176 tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
3178 ill->ill_ppa = start;
3179 return (0);
3183 * Insert ill into the list of configured ill's. Once this function completes,
3184 * the ill is globally visible and is available through lookups. More precisely
3185 * this happens after the caller drops the ill_g_lock.
3187 static int
3188 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
3190 ill_if_t *ill_interface;
3191 avl_index_t where = 0;
3192 int error;
3193 int name_length;
3194 int index;
3195 boolean_t check_length = B_FALSE;
3196 ip_stack_t *ipst = ill->ill_ipst;
3198 ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
3200 name_length = mi_strlen(name) + 1;
3202 if (isv6)
3203 index = IP_V6_G_HEAD;
3204 else
3205 index = IP_V4_G_HEAD;
3207 ill_interface = IP_VX_ILL_G_LIST(index, ipst);
3209 * Search for interface type based on name
3211 while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3212 if ((ill_interface->illif_name_len == name_length) &&
3213 (strcmp(ill_interface->illif_name, name) == 0)) {
3214 break;
3216 ill_interface = ill_interface->illif_next;
3220 * Interface type not found, create one.
3222 if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3223 ill_g_head_t ghead;
3226 * allocate ill_if_t structure
3228 ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
3229 if (ill_interface == NULL) {
3230 return (ENOMEM);
3233 (void) strcpy(ill_interface->illif_name, name);
3234 ill_interface->illif_name_len = name_length;
3236 avl_create(&ill_interface->illif_avl_by_ppa,
3237 ill_compare_ppa, sizeof (ill_t),
3238 offsetof(struct ill_s, ill_avl_byppa));
3241 * link the structure in the back to maintain order
3242 * of configuration for ifconfig output.
3244 ghead = ipst->ips_ill_g_heads[index];
3245 insque(ill_interface, ghead.ill_g_list_tail);
3248 if (ill->ill_ppa == UINT_MAX)
3249 check_length = B_TRUE;
3251 error = ill_alloc_ppa(ill_interface, ill);
3252 if (error != 0) {
3253 if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3254 ill_delete_interface_type(ill->ill_ifptr);
3255 return (error);
3259 * When the ppa is choosen by the system, check that there is
3260 * enough space to insert ppa. if a specific ppa was passed in this
3261 * check is not required as the interface name passed in will have
3262 * the right ppa in it.
3264 if (check_length) {
3266 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
3268 char buf[sizeof (uint_t) * 3];
3271 * convert ppa to string to calculate the amount of space
3272 * required for it in the name.
3274 numtos(ill->ill_ppa, buf);
3276 /* Do we have enough space to insert ppa ? */
3278 if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
3279 /* Free ppa and interface type struct */
3280 if (ill_interface->illif_ppa_arena != NULL) {
3281 vmem_free(ill_interface->illif_ppa_arena,
3282 (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3284 if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3285 ill_delete_interface_type(ill->ill_ifptr);
3287 return (EINVAL);
3291 (void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
3292 ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
3294 (void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
3295 &where);
3296 ill->ill_ifptr = ill_interface;
3297 avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
3299 ill_phyint_reinit(ill);
3300 return (0);
3303 /* Initialize the per phyint ipsq used for serialization */
3304 static boolean_t
3305 ipsq_init(ill_t *ill, boolean_t enter)
3307 ipsq_t *ipsq;
3308 ipxop_t *ipx;
3310 if ((ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP)) == NULL)
3311 return (B_FALSE);
3313 ill->ill_phyint->phyint_ipsq = ipsq;
3314 ipx = ipsq->ipsq_xop = &ipsq->ipsq_ownxop;
3315 ipx->ipx_ipsq = ipsq;
3316 ipsq->ipsq_next = ipsq;
3317 ipsq->ipsq_phyint = ill->ill_phyint;
3318 mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
3319 mutex_init(&ipx->ipx_lock, NULL, MUTEX_DEFAULT, 0);
3320 ipsq->ipsq_ipst = ill->ill_ipst; /* No netstack_hold */
3321 if (enter) {
3322 ipx->ipx_writer = curthread;
3323 ipx->ipx_forced = B_FALSE;
3324 ipx->ipx_reentry_cnt = 1;
3325 #ifdef DEBUG
3326 ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
3327 #endif
3329 return (B_TRUE);
3333 * Here we perform initialisation of the ill_t common to both regular
3334 * interface ILLs and the special loopback ILL created by ill_lookup_on_name.
3336 static int
3337 ill_init_common(ill_t *ill, queue_t *q, boolean_t isv6, boolean_t is_loopback,
3338 boolean_t ipsq_enter)
3340 int count;
3341 uchar_t *frag_ptr;
3343 mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
3344 mutex_init(&ill->ill_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
3345 ill->ill_saved_ire_cnt = 0;
3347 if (is_loopback) {
3348 ill->ill_max_frag = isv6 ? ip_loopback_mtu_v6plus :
3349 ip_loopback_mtuplus;
3351 * No resolver here.
3353 ill->ill_net_type = IRE_LOOPBACK;
3354 } else {
3355 ill->ill_rq = q;
3356 ill->ill_wq = WR(q);
3357 ill->ill_ppa = UINT_MAX;
3360 ill->ill_isv6 = isv6;
3363 * Allocate sufficient space to contain our fragment hash table and
3364 * the device name.
3366 frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE + 2 * LIFNAMSIZ);
3367 if (frag_ptr == NULL)
3368 return (ENOMEM);
3369 ill->ill_frag_ptr = frag_ptr;
3370 ill->ill_frag_free_num_pkts = 0;
3371 ill->ill_last_frag_clean_time = 0;
3372 ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
3373 ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
3374 for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
3375 mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
3376 NULL, MUTEX_DEFAULT, NULL);
3379 ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3380 if (ill->ill_phyint == NULL) {
3381 mi_free(frag_ptr);
3382 return (ENOMEM);
3385 mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3386 if (isv6) {
3387 ill->ill_phyint->phyint_illv6 = ill;
3388 } else {
3389 ill->ill_phyint->phyint_illv4 = ill;
3391 if (is_loopback) {
3392 phyint_flags_init(ill->ill_phyint, DL_LOOP);
3395 list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3397 ill_set_inputfn(ill);
3399 if (!ipsq_init(ill, ipsq_enter)) {
3400 mi_free(frag_ptr);
3401 mi_free(ill->ill_phyint);
3402 return (ENOMEM);
3405 /* Frag queue limit stuff */
3406 ill->ill_frag_count = 0;
3407 ill->ill_ipf_gen = 0;
3409 rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3410 mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3411 ill->ill_global_timer = INFINITY;
3412 ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3413 ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3414 ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3415 ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3418 * Initialize IPv6 configuration variables. The IP module is always
3419 * opened as an IPv4 module. Instead tracking down the cases where
3420 * it switches to do ipv6, we'll just initialize the IPv6 configuration
3421 * here for convenience, this has no effect until the ill is set to do
3422 * IPv6.
3424 ill->ill_reachable_time = ND_REACHABLE_TIME;
3425 ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
3426 ill->ill_max_buf = ND_MAX_Q;
3427 ill->ill_refcnt = 0;
3429 return (0);
3433 * ill_init is called by ip_open when a device control stream is opened.
3434 * It does a few initializations, and shoots a DL_INFO_REQ message down
3435 * to the driver. The response is later picked up in ip_rput_dlpi and
3436 * used to set up default mechanisms for talking to the driver. (Always
3437 * called as writer.)
3439 * If this function returns error, ip_open will call ip_close which in
3440 * turn will call ill_delete to clean up any memory allocated here that
3441 * is not yet freed.
3443 * Note: ill_ipst and ill_zoneid must be set before calling ill_init.
3446 ill_init(queue_t *q, ill_t *ill)
3448 int ret;
3449 dl_info_req_t *dlir;
3450 mblk_t *info_mp;
3452 info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
3453 BPRI_HI);
3454 if (info_mp == NULL)
3455 return (ENOMEM);
3458 * For now pretend this is a v4 ill. We need to set phyint_ill*
3459 * at this point because of the following reason. If we can't
3460 * enter the ipsq at some point and cv_wait, the writer that
3461 * wakes us up tries to locate us using the list of all phyints
3462 * in an ipsq and the ills from the phyint thru the phyint_ill*.
3463 * If we don't set it now, we risk a missed wakeup.
3465 if ((ret = ill_init_common(ill, q, B_FALSE, B_FALSE, B_TRUE)) != 0) {
3466 freemsg(info_mp);
3467 return (ret);
3470 ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
3472 /* Send down the Info Request to the driver. */
3473 info_mp->b_datap->db_type = M_PCPROTO;
3474 dlir = (dl_info_req_t *)info_mp->b_rptr;
3475 info_mp->b_wptr = (uchar_t *)&dlir[1];
3476 dlir->dl_primitive = DL_INFO_REQ;
3478 ill->ill_dlpi_pending = DL_PRIM_INVAL;
3480 qprocson(q);
3481 ill_dlpi_send(ill, info_mp);
3483 return (0);
3487 * ill_dls_info
3488 * creates datalink socket info from the device.
3491 ill_dls_info(struct sockaddr_dl *sdl, const ill_t *ill)
3493 size_t len;
3495 sdl->sdl_family = AF_LINK;
3496 sdl->sdl_index = ill_get_upper_ifindex(ill);
3497 sdl->sdl_type = ill->ill_type;
3498 ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3499 len = strlen(sdl->sdl_data);
3500 ASSERT(len < 256);
3501 sdl->sdl_nlen = (uchar_t)len;
3502 sdl->sdl_alen = ill->ill_phys_addr_length;
3503 sdl->sdl_slen = 0;
3504 if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
3505 bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
3507 return (sizeof (struct sockaddr_dl));
3511 * ill_xarp_info
3512 * creates xarp info from the device.
3514 static int
3515 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
3517 sdl->sdl_family = AF_LINK;
3518 sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
3519 sdl->sdl_type = ill->ill_type;
3520 ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3521 sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
3522 sdl->sdl_alen = ill->ill_phys_addr_length;
3523 sdl->sdl_slen = 0;
3524 return (sdl->sdl_nlen);
3527 static int
3528 loopback_kstat_update(kstat_t *ksp, int rw)
3530 kstat_named_t *kn;
3531 netstackid_t stackid;
3532 netstack_t *ns;
3533 ip_stack_t *ipst;
3535 if (ksp == NULL || ksp->ks_data == NULL)
3536 return (EIO);
3538 if (rw == KSTAT_WRITE)
3539 return (EACCES);
3541 kn = KSTAT_NAMED_PTR(ksp);
3542 stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
3544 ns = netstack_find_by_stackid(stackid);
3545 if (ns == NULL)
3546 return (-1);
3548 ipst = ns->netstack_ip;
3549 if (ipst == NULL) {
3550 netstack_rele(ns);
3551 return (-1);
3553 kn[0].value.ui32 = ipst->ips_loopback_packets;
3554 kn[1].value.ui32 = ipst->ips_loopback_packets;
3555 netstack_rele(ns);
3556 return (0);
3560 * Has ifindex been plumbed already?
3562 static boolean_t
3563 phyint_exists(uint_t index, ip_stack_t *ipst)
3565 ASSERT(index != 0);
3566 ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
3568 return (avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3569 &index, NULL) != NULL);
3573 * Pick a unique ifindex.
3574 * When the index counter passes IF_INDEX_MAX for the first time, the wrap
3575 * flag is set so that next time time ip_assign_ifindex() is called, it
3576 * falls through and resets the index counter back to 1, the minimum value
3577 * for the interface index. The logic below assumes that ips_ill_index
3578 * can hold a value of IF_INDEX_MAX+1 without there being any loss
3579 * (i.e. reset back to 0.)
3581 boolean_t
3582 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
3584 uint_t loops;
3586 if (!ipst->ips_ill_index_wrap) {
3587 *indexp = ipst->ips_ill_index++;
3588 if (ipst->ips_ill_index > IF_INDEX_MAX) {
3590 * Reached the maximum ifindex value, set the wrap
3591 * flag to indicate that it is no longer possible
3592 * to assume that a given index is unallocated.
3594 ipst->ips_ill_index_wrap = B_TRUE;
3596 return (B_TRUE);
3599 if (ipst->ips_ill_index > IF_INDEX_MAX)
3600 ipst->ips_ill_index = 1;
3603 * Start reusing unused indexes. Note that we hold the ill_g_lock
3604 * at this point and don't want to call any function that attempts
3605 * to get the lock again.
3607 for (loops = IF_INDEX_MAX; loops > 0; loops--) {
3608 if (!phyint_exists(ipst->ips_ill_index, ipst)) {
3609 /* found unused index - use it */
3610 *indexp = ipst->ips_ill_index;
3611 return (B_TRUE);
3614 ipst->ips_ill_index++;
3615 if (ipst->ips_ill_index > IF_INDEX_MAX)
3616 ipst->ips_ill_index = 1;
3620 * all interface indicies are inuse.
3622 return (B_FALSE);
3626 * Assign a unique interface index for the phyint.
3628 static boolean_t
3629 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
3631 ASSERT(phyi->phyint_ifindex == 0);
3632 return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
3636 * Initialize the flags on `phyi' as per the provided mactype.
3638 static void
3639 phyint_flags_init(phyint_t *phyi, t_uscalar_t mactype)
3641 uint64_t flags = 0;
3644 * Initialize PHYI_RUNNING and PHYI_FAILED. For non-IPMP interfaces,
3645 * we always presume the underlying hardware is working and set
3646 * PHYI_RUNNING (if it's not, the driver will subsequently send a
3647 * DL_NOTE_LINK_DOWN message). For IPMP interfaces, at initialization
3648 * there are no active interfaces in the group so we set PHYI_FAILED.
3650 if (mactype == SUNW_DL_IPMP)
3651 flags |= PHYI_FAILED;
3652 else
3653 flags |= PHYI_RUNNING;
3655 switch (mactype) {
3656 case SUNW_DL_VNI:
3657 flags |= PHYI_VIRTUAL;
3658 break;
3659 case SUNW_DL_IPMP:
3660 flags |= PHYI_IPMP;
3661 break;
3662 case DL_LOOP:
3663 flags |= (PHYI_LOOPBACK | PHYI_VIRTUAL);
3664 break;
3667 mutex_enter(&phyi->phyint_lock);
3668 phyi->phyint_flags |= flags;
3669 mutex_exit(&phyi->phyint_lock);
3673 * Return a pointer to the ill which matches the supplied name. Note that
3674 * the ill name length includes the null termination character. (May be
3675 * called as writer.)
3676 * If do_alloc and the interface is "lo0" it will be automatically created.
3677 * Cannot bump up reference on condemned ills. So dup detect can't be done
3678 * using this func.
3680 ill_t *
3681 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
3682 boolean_t *did_alloc, ip_stack_t *ipst)
3684 ill_t *ill;
3685 ipif_t *ipif;
3686 ipsq_t *ipsq;
3687 kstat_named_t *kn;
3688 boolean_t isloopback;
3689 in6_addr_t ov6addr;
3691 isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
3693 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3694 ill = ill_find_by_name(name, isv6, ipst);
3695 rw_exit(&ipst->ips_ill_g_lock);
3696 if (ill != NULL)
3697 return (ill);
3700 * Couldn't find it. Does this happen to be a lookup for the
3701 * loopback device and are we allowed to allocate it?
3703 if (!isloopback || !do_alloc)
3704 return (NULL);
3706 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
3707 ill = ill_find_by_name(name, isv6, ipst);
3708 if (ill != NULL) {
3709 rw_exit(&ipst->ips_ill_g_lock);
3710 return (ill);
3713 /* Create the loopback device on demand */
3714 ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
3715 sizeof (ipif_loopback_name), BPRI_MED));
3716 if (ill == NULL)
3717 goto done;
3719 bzero(ill, sizeof (*ill));
3720 ill->ill_ipst = ipst;
3721 netstack_hold(ipst->ips_netstack);
3723 * For exclusive stacks we set the zoneid to zero
3724 * to make IP operate as if in the global zone.
3726 ill->ill_zoneid = GLOBAL_ZONEID;
3728 if (ill_init_common(ill, NULL, isv6, B_TRUE, B_FALSE) != 0)
3729 goto done;
3731 if (!ill_allocate_mibs(ill))
3732 goto done;
3734 ill->ill_current_frag = ill->ill_max_frag;
3735 ill->ill_mtu = ill->ill_max_frag; /* Initial value */
3736 ill->ill_mc_mtu = ill->ill_mtu;
3738 * ipif_loopback_name can't be pointed at directly because its used
3739 * by both the ipv4 and ipv6 interfaces. When the ill is removed
3740 * from the glist, ill_glist_delete() sets the first character of
3741 * ill_name to '\0'.
3743 ill->ill_name = (char *)ill + sizeof (*ill);
3744 (void) strcpy(ill->ill_name, ipif_loopback_name);
3745 ill->ill_name_length = sizeof (ipif_loopback_name);
3746 /* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
3747 ill->ill_dlpi_pending = DL_PRIM_INVAL;
3749 ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE, B_TRUE, NULL);
3750 if (ipif == NULL)
3751 goto done;
3753 ill->ill_flags = ILLF_MULTICAST;
3755 ov6addr = ipif->ipif_v6lcl_addr;
3756 /* Set up default loopback address and mask. */
3757 if (!isv6) {
3758 ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
3760 IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
3761 V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
3762 V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3763 ipif->ipif_v6subnet);
3764 ill->ill_flags |= ILLF_IPV4;
3765 } else {
3766 ipif->ipif_v6lcl_addr = ipv6_loopback;
3767 ipif->ipif_v6net_mask = ipv6_all_ones;
3768 V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3769 ipif->ipif_v6subnet);
3770 ill->ill_flags |= ILLF_IPV6;
3774 * Chain us in at the end of the ill list. hold the ill
3775 * before we make it globally visible. 1 for the lookup.
3777 ill_refhold(ill);
3779 ipsq = ill->ill_phyint->phyint_ipsq;
3781 if (ill_glist_insert(ill, "lo", isv6) != 0)
3782 cmn_err(CE_PANIC, "cannot insert loopback interface");
3784 /* Let SCTP know so that it can add this to its list */
3785 sctp_update_ill(ill, SCTP_ILL_INSERT);
3788 * We have already assigned ipif_v6lcl_addr above, but we need to
3789 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
3790 * requires to be after ill_glist_insert() since we need the
3791 * ill_index set. Pass on ipv6_loopback as the old address.
3793 sctp_update_ipif_addr(ipif, ov6addr);
3795 ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
3798 * ill_glist_insert() -> ill_phyint_reinit() may have merged IPSQs.
3799 * If so, free our original one.
3801 if (ipsq != ill->ill_phyint->phyint_ipsq)
3802 ipsq_delete(ipsq);
3804 if (ipst->ips_loopback_ksp == NULL) {
3805 /* Export loopback interface statistics */
3806 ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
3807 ipif_loopback_name, "net",
3808 KSTAT_TYPE_NAMED, 2, 0,
3809 ipst->ips_netstack->netstack_stackid);
3810 if (ipst->ips_loopback_ksp != NULL) {
3811 ipst->ips_loopback_ksp->ks_update =
3812 loopback_kstat_update;
3813 kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
3814 kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
3815 kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
3816 ipst->ips_loopback_ksp->ks_private =
3817 (void *)(uintptr_t)ipst->ips_netstack->
3818 netstack_stackid;
3819 kstat_install(ipst->ips_loopback_ksp);
3823 *did_alloc = B_TRUE;
3824 rw_exit(&ipst->ips_ill_g_lock);
3825 ill_nic_event_dispatch(ill, MAP_IPIF_ID(ill->ill_ipif->ipif_id),
3826 NE_PLUMB, ill->ill_name, ill->ill_name_length);
3827 return (ill);
3828 done:
3829 if (ill != NULL) {
3830 if (ill->ill_phyint != NULL) {
3831 ipsq = ill->ill_phyint->phyint_ipsq;
3832 if (ipsq != NULL) {
3833 ipsq->ipsq_phyint = NULL;
3834 ipsq_delete(ipsq);
3836 mi_free(ill->ill_phyint);
3838 ill_free_mib(ill);
3839 if (ill->ill_ipst != NULL)
3840 netstack_rele(ill->ill_ipst->ips_netstack);
3841 mi_free(ill);
3843 rw_exit(&ipst->ips_ill_g_lock);
3844 return (NULL);
3848 * For IPP calls - use the ip_stack_t for global stack.
3850 ill_t *
3851 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6)
3853 ip_stack_t *ipst;
3854 ill_t *ill;
3855 netstack_t *ns;
3857 ns = netstack_find_by_stackid(GLOBAL_NETSTACKID);
3859 if ((ipst = ns->netstack_ip) == NULL) {
3860 cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
3861 netstack_rele(ns);
3862 return (NULL);
3865 ill = ill_lookup_on_ifindex(index, isv6, ipst);
3866 netstack_rele(ns);
3867 return (ill);
3871 * Return a pointer to the ill which matches the index and IP version type.
3873 ill_t *
3874 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3876 ill_t *ill;
3877 phyint_t *phyi;
3880 * Indexes are stored in the phyint - a common structure
3881 * to both IPv4 and IPv6.
3883 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3884 phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3885 (void *) &index, NULL);
3886 if (phyi != NULL) {
3887 ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
3888 if (ill != NULL) {
3889 mutex_enter(&ill->ill_lock);
3890 if (!ILL_IS_CONDEMNED(ill)) {
3891 ill_refhold_locked(ill);
3892 mutex_exit(&ill->ill_lock);
3893 rw_exit(&ipst->ips_ill_g_lock);
3894 return (ill);
3896 mutex_exit(&ill->ill_lock);
3899 rw_exit(&ipst->ips_ill_g_lock);
3900 return (NULL);
3904 * Verify whether or not an interface index is valid for the specified zoneid
3905 * to transmit packets.
3906 * It can be zero (meaning "reset") or an interface index assigned
3907 * to a non-VNI interface. (We don't use VNI interface to send packets.)
3909 boolean_t
3910 ip_xmit_ifindex_valid(uint_t ifindex, zoneid_t zoneid, boolean_t isv6,
3911 ip_stack_t *ipst)
3913 ill_t *ill;
3915 if (ifindex == 0)
3916 return (B_TRUE);
3918 ill = ill_lookup_on_ifindex_zoneid(ifindex, zoneid, isv6, ipst);
3919 if (ill == NULL)
3920 return (B_FALSE);
3921 if (IS_VNI(ill)) {
3922 ill_refrele(ill);
3923 return (B_FALSE);
3925 ill_refrele(ill);
3926 return (B_TRUE);
3930 * Return the ifindex next in sequence after the passed in ifindex.
3931 * If there is no next ifindex for the given protocol, return 0.
3933 uint_t
3934 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3936 phyint_t *phyi;
3937 phyint_t *phyi_initial;
3938 uint_t ifindex;
3940 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3942 if (index == 0) {
3943 phyi = avl_first(
3944 &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
3945 } else {
3946 phyi = phyi_initial = avl_find(
3947 &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3948 (void *) &index, NULL);
3951 for (; phyi != NULL;
3952 phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3953 phyi, AVL_AFTER)) {
3955 * If we're not returning the first interface in the tree
3956 * and we still haven't moved past the phyint_t that
3957 * corresponds to index, avl_walk needs to be called again
3959 if (!((index != 0) && (phyi == phyi_initial))) {
3960 if (isv6) {
3961 if ((phyi->phyint_illv6) &&
3962 ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
3963 (phyi->phyint_illv6->ill_isv6 == 1))
3964 break;
3965 } else {
3966 if ((phyi->phyint_illv4) &&
3967 ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
3968 (phyi->phyint_illv4->ill_isv6 == 0))
3969 break;
3974 rw_exit(&ipst->ips_ill_g_lock);
3976 if (phyi != NULL)
3977 ifindex = phyi->phyint_ifindex;
3978 else
3979 ifindex = 0;
3981 return (ifindex);
3985 * Return the ifindex for the named interface.
3986 * If there is no next ifindex for the interface, return 0.
3988 uint_t
3989 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
3991 phyint_t *phyi;
3992 avl_index_t where = 0;
3993 uint_t ifindex;
3995 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3997 if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
3998 name, &where)) == NULL) {
3999 rw_exit(&ipst->ips_ill_g_lock);
4000 return (0);
4003 ifindex = phyi->phyint_ifindex;
4005 rw_exit(&ipst->ips_ill_g_lock);
4007 return (ifindex);
4011 * Return the ifindex to be used by upper layer protocols for instance
4012 * for IPV6_RECVPKTINFO. If IPMP this is the one for the upper ill.
4014 uint_t
4015 ill_get_upper_ifindex(const ill_t *ill)
4017 if (IS_UNDER_IPMP(ill))
4018 return (ipmp_ill_get_ipmp_ifindex(ill));
4019 else
4020 return (ill->ill_phyint->phyint_ifindex);
4025 * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
4026 * that gives a running thread a reference to the ill. This reference must be
4027 * released by the thread when it is done accessing the ill and related
4028 * objects. ill_refcnt can not be used to account for static references
4029 * such as other structures pointing to an ill. Callers must generally
4030 * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
4031 * or be sure that the ill is not being deleted or changing state before
4032 * calling the refhold functions. A non-zero ill_refcnt ensures that the
4033 * ill won't change any of its critical state such as address, netmask etc.
4035 void
4036 ill_refhold(ill_t *ill)
4038 mutex_enter(&ill->ill_lock);
4039 ill->ill_refcnt++;
4040 ILL_TRACE_REF(ill);
4041 mutex_exit(&ill->ill_lock);
4044 void
4045 ill_refhold_locked(ill_t *ill)
4047 ASSERT(MUTEX_HELD(&ill->ill_lock));
4048 ill->ill_refcnt++;
4049 ILL_TRACE_REF(ill);
4052 /* Returns true if we managed to get a refhold */
4053 boolean_t
4054 ill_check_and_refhold(ill_t *ill)
4056 mutex_enter(&ill->ill_lock);
4057 if (!ILL_IS_CONDEMNED(ill)) {
4058 ill_refhold_locked(ill);
4059 mutex_exit(&ill->ill_lock);
4060 return (B_TRUE);
4062 mutex_exit(&ill->ill_lock);
4063 return (B_FALSE);
4067 * Must not be called while holding any locks. Otherwise if this is
4068 * the last reference to be released, there is a chance of recursive mutex
4069 * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
4070 * to restart an ioctl.
4072 void
4073 ill_refrele(ill_t *ill)
4075 mutex_enter(&ill->ill_lock);
4076 ASSERT(ill->ill_refcnt != 0);
4077 ill->ill_refcnt--;
4078 ILL_UNTRACE_REF(ill);
4079 if (ill->ill_refcnt != 0) {
4080 /* Every ire pointing to the ill adds 1 to ill_refcnt */
4081 mutex_exit(&ill->ill_lock);
4082 return;
4085 /* Drops the ill_lock */
4086 ipif_ill_refrele_tail(ill);
4090 * Obtain a weak reference count on the ill. This reference ensures the
4091 * ill won't be freed, but the ill may change any of its critical state
4092 * such as netmask, address etc. Returns an error if the ill has started
4093 * closing.
4095 boolean_t
4096 ill_waiter_inc(ill_t *ill)
4098 mutex_enter(&ill->ill_lock);
4099 if (ill->ill_state_flags & ILL_CONDEMNED) {
4100 mutex_exit(&ill->ill_lock);
4101 return (B_FALSE);
4103 ill->ill_waiters++;
4104 mutex_exit(&ill->ill_lock);
4105 return (B_TRUE);
4108 void
4109 ill_waiter_dcr(ill_t *ill)
4111 mutex_enter(&ill->ill_lock);
4112 ill->ill_waiters--;
4113 if (ill->ill_waiters == 0)
4114 cv_broadcast(&ill->ill_cv);
4115 mutex_exit(&ill->ill_lock);
4119 * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
4120 * driver. We construct best guess defaults for lower level information that
4121 * we need. If an interface is brought up without injection of any overriding
4122 * information from outside, we have to be ready to go with these defaults.
4123 * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
4124 * we primarely want the dl_provider_style.
4125 * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
4126 * at which point we assume the other part of the information is valid.
4128 void
4129 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
4131 uchar_t *brdcst_addr;
4132 uint_t brdcst_addr_length, phys_addr_length;
4133 t_scalar_t sap_length;
4134 dl_info_ack_t *dlia;
4135 ip_m_t *ipm;
4136 dl_qos_cl_sel1_t *sel1;
4137 int min_mtu;
4139 ASSERT(IAM_WRITER_ILL(ill));
4142 * Till the ill is fully up the ill is not globally visible.
4143 * So no need for a lock.
4145 dlia = (dl_info_ack_t *)mp->b_rptr;
4146 ill->ill_mactype = dlia->dl_mac_type;
4148 ipm = ip_m_lookup(dlia->dl_mac_type);
4149 if (ipm == NULL) {
4150 ipm = ip_m_lookup(DL_OTHER);
4151 ASSERT(ipm != NULL);
4153 ill->ill_media = ipm;
4156 * When the new DLPI stuff is ready we'll pull lengths
4157 * from dlia.
4159 if (dlia->dl_version == DL_VERSION_2) {
4160 brdcst_addr_length = dlia->dl_brdcst_addr_length;
4161 brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
4162 brdcst_addr_length);
4163 if (brdcst_addr == NULL) {
4164 brdcst_addr_length = 0;
4166 sap_length = dlia->dl_sap_length;
4167 phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
4168 ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
4169 brdcst_addr_length, sap_length, phys_addr_length));
4170 } else {
4171 brdcst_addr_length = 6;
4172 brdcst_addr = ip_six_byte_all_ones;
4173 sap_length = -2;
4174 phys_addr_length = brdcst_addr_length;
4177 ill->ill_bcast_addr_length = brdcst_addr_length;
4178 ill->ill_phys_addr_length = phys_addr_length;
4179 ill->ill_sap_length = sap_length;
4182 * Synthetic DLPI types such as SUNW_DL_IPMP specify a zero SDU,
4183 * but we must ensure a minimum IP MTU is used since other bits of
4184 * IP will fly apart otherwise.
4186 min_mtu = ill->ill_isv6 ? IPV6_MIN_MTU : IP_MIN_MTU;
4187 ill->ill_max_frag = MAX(min_mtu, dlia->dl_max_sdu);
4188 ill->ill_current_frag = ill->ill_max_frag;
4189 ill->ill_mtu = ill->ill_max_frag;
4190 ill->ill_mc_mtu = ill->ill_mtu; /* Overridden by DL_NOTE_SDU_SIZE2 */
4192 ill->ill_type = ipm->ip_m_type;
4194 if (!ill->ill_dlpi_style_set) {
4195 if (dlia->dl_provider_style == DL_STYLE2)
4196 ill->ill_needs_attach = 1;
4198 phyint_flags_init(ill->ill_phyint, ill->ill_mactype);
4201 * Allocate the first ipif on this ill. We don't delay it
4202 * further as ioctl handling assumes at least one ipif exists.
4204 * At this point we don't know whether the ill is v4 or v6.
4205 * We will know this whan the SIOCSLIFNAME happens and
4206 * the correct value for ill_isv6 will be assigned in
4207 * ipif_set_values(). We need to hold the ill lock and
4208 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
4209 * the wakeup.
4211 (void) ipif_allocate(ill, 0, IRE_LOCAL,
4212 dlia->dl_provider_style != DL_STYLE2, B_TRUE, NULL);
4213 mutex_enter(&ill->ill_lock);
4214 ASSERT(ill->ill_dlpi_style_set == 0);
4215 ill->ill_dlpi_style_set = 1;
4216 ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
4217 cv_broadcast(&ill->ill_cv);
4218 mutex_exit(&ill->ill_lock);
4219 freemsg(mp);
4220 return;
4222 ASSERT(ill->ill_ipif != NULL);
4224 * We know whether it is IPv4 or IPv6 now, as this is the
4225 * second DL_INFO_ACK we are recieving in response to the
4226 * DL_INFO_REQ sent in ipif_set_values.
4228 ill->ill_sap = (ill->ill_isv6) ? ipm->ip_m_ipv6sap : ipm->ip_m_ipv4sap;
4230 * Clear all the flags that were set based on ill_bcast_addr_length
4231 * and ill_phys_addr_length (in ipif_set_values) as these could have
4232 * changed now and we need to re-evaluate.
4234 ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
4235 ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
4238 * Free ill_bcast_mp as things could have changed now.
4240 * NOTE: The IPMP meta-interface is special-cased because it starts
4241 * with no underlying interfaces (and thus an unknown broadcast
4242 * address length), but we enforce that an interface is broadcast-
4243 * capable as part of allowing it to join a group.
4245 if (ill->ill_bcast_addr_length == 0 && !IS_IPMP(ill)) {
4246 if (ill->ill_bcast_mp != NULL)
4247 freemsg(ill->ill_bcast_mp);
4248 ill->ill_net_type = IRE_IF_NORESOLVER;
4250 ill->ill_bcast_mp = ill_dlur_gen(NULL,
4251 ill->ill_phys_addr_length,
4252 ill->ill_sap,
4253 ill->ill_sap_length);
4255 if (ill->ill_isv6)
4257 * Note: xresolv interfaces will eventually need NOARP
4258 * set here as well, but that will require those
4259 * external resolvers to have some knowledge of
4260 * that flag and act appropriately. Not to be changed
4261 * at present.
4263 ill->ill_flags |= ILLF_NONUD;
4264 else
4265 ill->ill_flags |= ILLF_NOARP;
4267 if (ill->ill_mactype == SUNW_DL_VNI) {
4268 ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
4269 } else if (ill->ill_phys_addr_length == 0 ||
4270 ill->ill_mactype == DL_IPV4 ||
4271 ill->ill_mactype == DL_IPV6) {
4273 * The underying link is point-to-point, so mark the
4274 * interface as such. We can do IP multicast over
4275 * such a link since it transmits all network-layer
4276 * packets to the remote side the same way.
4278 ill->ill_flags |= ILLF_MULTICAST;
4279 ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
4281 } else {
4282 ill->ill_net_type = IRE_IF_RESOLVER;
4283 if (ill->ill_bcast_mp != NULL)
4284 freemsg(ill->ill_bcast_mp);
4285 ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
4286 ill->ill_bcast_addr_length, ill->ill_sap,
4287 ill->ill_sap_length);
4289 * Later detect lack of DLPI driver multicast
4290 * capability by catching DL_ENABMULTI errors in
4291 * ip_rput_dlpi.
4293 ill->ill_flags |= ILLF_MULTICAST;
4294 if (!ill->ill_isv6)
4295 ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
4298 /* For IPMP, PHYI_IPMP should already be set by phyint_flags_init() */
4299 if (ill->ill_mactype == SUNW_DL_IPMP)
4300 ASSERT(ill->ill_phyint->phyint_flags & PHYI_IPMP);
4302 /* By default an interface does not support any CoS marking */
4303 ill->ill_flags &= ~ILLF_COS_ENABLED;
4306 * If we get QoS information in DL_INFO_ACK, the device supports
4307 * some form of CoS marking, set ILLF_COS_ENABLED.
4309 sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
4310 dlia->dl_qos_length);
4311 if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
4312 ill->ill_flags |= ILLF_COS_ENABLED;
4315 /* Clear any previous error indication. */
4316 ill->ill_error = 0;
4317 freemsg(mp);
4321 * Perform various checks to verify that an address would make sense as a
4322 * local, remote, or subnet interface address.
4324 static boolean_t
4325 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
4327 ipaddr_t net_mask;
4330 * Don't allow all zeroes, or all ones, but allow
4331 * all ones netmask.
4333 if ((net_mask = ip_net_mask(addr)) == 0)
4334 return (B_FALSE);
4335 /* A given netmask overrides the "guess" netmask */
4336 if (subnet_mask != 0)
4337 net_mask = subnet_mask;
4338 if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
4339 (addr == (addr | ~net_mask)))) {
4340 return (B_FALSE);
4344 * Even if the netmask is all ones, we do not allow address to be
4345 * 255.255.255.255
4347 if (addr == INADDR_BROADCAST)
4348 return (B_FALSE);
4350 if (CLASSD(addr))
4351 return (B_FALSE);
4353 return (B_TRUE);
4356 #define V6_IPIF_LINKLOCAL(p) \
4357 IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
4360 * Compare two given ipifs and check if the second one is better than
4361 * the first one using the order of preference (not taking deprecated
4362 * into acount) specified in ipif_lookup_multicast().
4364 static boolean_t
4365 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
4367 /* Check the least preferred first. */
4368 if (IS_LOOPBACK(old_ipif->ipif_ill)) {
4369 /* If both ipifs are the same, use the first one. */
4370 if (IS_LOOPBACK(new_ipif->ipif_ill))
4371 return (B_FALSE);
4372 else
4373 return (B_TRUE);
4376 /* For IPv6, check for link local address. */
4377 if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
4378 if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4379 V6_IPIF_LINKLOCAL(new_ipif)) {
4380 /* The second one is equal or less preferred. */
4381 return (B_FALSE);
4382 } else {
4383 return (B_TRUE);
4387 /* Then check for point to point interface. */
4388 if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
4389 if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4390 (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
4391 (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
4392 return (B_FALSE);
4393 } else {
4394 return (B_TRUE);
4398 /* old_ipif is a normal interface, so no need to use the new one. */
4399 return (B_FALSE);
4403 * Find a mulitcast-capable ipif given an IP instance and zoneid.
4404 * The ipif must be up, and its ill must multicast-capable, not
4405 * condemned, not an underlying interface in an IPMP group, and
4406 * not a VNI interface. Order of preference:
4408 * 1a. normal
4409 * 1b. normal, but deprecated
4410 * 2a. point to point
4411 * 2b. point to point, but deprecated
4412 * 3a. link local
4413 * 3b. link local, but deprecated
4414 * 4. loopback.
4416 static ipif_t *
4417 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4419 ill_t *ill;
4420 ill_walk_context_t ctx;
4421 ipif_t *ipif;
4422 ipif_t *saved_ipif = NULL;
4423 ipif_t *dep_ipif = NULL;
4425 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4426 if (isv6)
4427 ill = ILL_START_WALK_V6(&ctx, ipst);
4428 else
4429 ill = ILL_START_WALK_V4(&ctx, ipst);
4431 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4432 mutex_enter(&ill->ill_lock);
4433 if (IS_VNI(ill) || IS_UNDER_IPMP(ill) ||
4434 ILL_IS_CONDEMNED(ill) ||
4435 !(ill->ill_flags & ILLF_MULTICAST)) {
4436 mutex_exit(&ill->ill_lock);
4437 continue;
4439 for (ipif = ill->ill_ipif; ipif != NULL;
4440 ipif = ipif->ipif_next) {
4441 if (zoneid != ipif->ipif_zoneid &&
4442 zoneid != ALL_ZONES &&
4443 ipif->ipif_zoneid != ALL_ZONES) {
4444 continue;
4446 if (!(ipif->ipif_flags & IPIF_UP) ||
4447 IPIF_IS_CONDEMNED(ipif)) {
4448 continue;
4452 * Found one candidate. If it is deprecated,
4453 * remember it in dep_ipif. If it is not deprecated,
4454 * remember it in saved_ipif.
4456 if (ipif->ipif_flags & IPIF_DEPRECATED) {
4457 if (dep_ipif == NULL) {
4458 dep_ipif = ipif;
4459 } else if (ipif_comp_multi(dep_ipif, ipif,
4460 isv6)) {
4462 * If the previous dep_ipif does not
4463 * belong to the same ill, we've done
4464 * a ipif_refhold() on it. So we need
4465 * to release it.
4467 if (dep_ipif->ipif_ill != ill)
4468 ipif_refrele(dep_ipif);
4469 dep_ipif = ipif;
4471 continue;
4473 if (saved_ipif == NULL) {
4474 saved_ipif = ipif;
4475 } else {
4476 if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
4477 if (saved_ipif->ipif_ill != ill)
4478 ipif_refrele(saved_ipif);
4479 saved_ipif = ipif;
4484 * Before going to the next ill, do a ipif_refhold() on the
4485 * saved ones.
4487 if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
4488 ipif_refhold_locked(saved_ipif);
4489 if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
4490 ipif_refhold_locked(dep_ipif);
4491 mutex_exit(&ill->ill_lock);
4493 rw_exit(&ipst->ips_ill_g_lock);
4496 * If we have only the saved_ipif, return it. But if we have both
4497 * saved_ipif and dep_ipif, check to see which one is better.
4499 if (saved_ipif != NULL) {
4500 if (dep_ipif != NULL) {
4501 if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
4502 ipif_refrele(saved_ipif);
4503 return (dep_ipif);
4504 } else {
4505 ipif_refrele(dep_ipif);
4506 return (saved_ipif);
4509 return (saved_ipif);
4510 } else {
4511 return (dep_ipif);
4515 ill_t *
4516 ill_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4518 ipif_t *ipif;
4519 ill_t *ill;
4521 ipif = ipif_lookup_multicast(ipst, zoneid, isv6);
4522 if (ipif == NULL)
4523 return (NULL);
4525 ill = ipif->ipif_ill;
4526 ill_refhold(ill);
4527 ipif_refrele(ipif);
4528 return (ill);
4532 * This function is called when an application does not specify an interface
4533 * to be used for multicast traffic (joining a group/sending data). It
4534 * calls ire_lookup_multi() to look for an interface route for the
4535 * specified multicast group. Doing this allows the administrator to add
4536 * prefix routes for multicast to indicate which interface to be used for
4537 * multicast traffic in the above scenario. The route could be for all
4538 * multicast (224.0/4), for a single multicast group (a /32 route) or
4539 * anything in between. If there is no such multicast route, we just find
4540 * any multicast capable interface and return it. The returned ipif
4541 * is refhold'ed.
4543 * We support MULTIRT and RTF_SETSRC on the multicast routes added to the
4544 * unicast table. This is used by CGTP.
4546 ill_t *
4547 ill_lookup_group_v4(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst,
4548 boolean_t *multirtp, ipaddr_t *setsrcp)
4550 ill_t *ill;
4552 ill = ire_lookup_multi_ill_v4(group, zoneid, ipst, multirtp, setsrcp);
4553 if (ill != NULL)
4554 return (ill);
4556 return (ill_lookup_multicast(ipst, zoneid, B_FALSE));
4560 * Look for an ipif with the specified interface address and destination.
4561 * The destination address is used only for matching point-to-point interfaces.
4563 ipif_t *
4564 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, ip_stack_t *ipst)
4566 ipif_t *ipif;
4567 ill_t *ill;
4568 ill_walk_context_t ctx;
4571 * First match all the point-to-point interfaces
4572 * before looking at non-point-to-point interfaces.
4573 * This is done to avoid returning non-point-to-point
4574 * ipif instead of unnumbered point-to-point ipif.
4576 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4577 ill = ILL_START_WALK_V4(&ctx, ipst);
4578 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4579 mutex_enter(&ill->ill_lock);
4580 for (ipif = ill->ill_ipif; ipif != NULL;
4581 ipif = ipif->ipif_next) {
4582 /* Allow the ipif to be down */
4583 if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
4584 (ipif->ipif_lcl_addr == if_addr) &&
4585 (ipif->ipif_pp_dst_addr == dst)) {
4586 if (!IPIF_IS_CONDEMNED(ipif)) {
4587 ipif_refhold_locked(ipif);
4588 mutex_exit(&ill->ill_lock);
4589 rw_exit(&ipst->ips_ill_g_lock);
4590 return (ipif);
4594 mutex_exit(&ill->ill_lock);
4596 rw_exit(&ipst->ips_ill_g_lock);
4598 /* lookup the ipif based on interface address */
4599 ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, ipst);
4600 ASSERT(ipif == NULL || !ipif->ipif_isv6);
4601 return (ipif);
4605 * Common function for ipif_lookup_addr() and ipif_lookup_addr_exact().
4607 static ipif_t *
4608 ipif_lookup_addr_common(ipaddr_t addr, ill_t *match_ill, uint32_t match_flags,
4609 zoneid_t zoneid, ip_stack_t *ipst)
4611 ipif_t *ipif;
4612 ill_t *ill;
4613 boolean_t ptp = B_FALSE;
4614 ill_walk_context_t ctx;
4615 boolean_t match_illgrp = (match_flags & IPIF_MATCH_ILLGRP);
4616 boolean_t no_duplicate = (match_flags & IPIF_MATCH_NONDUP);
4618 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4620 * Repeat twice, first based on local addresses and
4621 * next time for pointopoint.
4623 repeat:
4624 ill = ILL_START_WALK_V4(&ctx, ipst);
4625 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4626 if (match_ill != NULL && ill != match_ill &&
4627 (!match_illgrp || !IS_IN_SAME_ILLGRP(ill, match_ill))) {
4628 continue;
4630 mutex_enter(&ill->ill_lock);
4631 for (ipif = ill->ill_ipif; ipif != NULL;
4632 ipif = ipif->ipif_next) {
4633 if (zoneid != ALL_ZONES &&
4634 zoneid != ipif->ipif_zoneid &&
4635 ipif->ipif_zoneid != ALL_ZONES)
4636 continue;
4638 if (no_duplicate && !(ipif->ipif_flags & IPIF_UP))
4639 continue;
4641 /* Allow the ipif to be down */
4642 if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4643 ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4644 (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4645 (ipif->ipif_pp_dst_addr == addr))) {
4646 if (!IPIF_IS_CONDEMNED(ipif)) {
4647 ipif_refhold_locked(ipif);
4648 mutex_exit(&ill->ill_lock);
4649 rw_exit(&ipst->ips_ill_g_lock);
4650 return (ipif);
4654 mutex_exit(&ill->ill_lock);
4657 /* If we already did the ptp case, then we are done */
4658 if (ptp) {
4659 rw_exit(&ipst->ips_ill_g_lock);
4660 return (NULL);
4662 ptp = B_TRUE;
4663 goto repeat;
4667 * Lookup an ipif with the specified address. For point-to-point links we
4668 * look for matches on either the destination address or the local address,
4669 * but we skip the local address check if IPIF_UNNUMBERED is set. If the
4670 * `match_ill' argument is non-NULL, the lookup is restricted to that ill
4671 * (or illgrp if `match_ill' is in an IPMP group).
4673 ipif_t *
4674 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4675 ip_stack_t *ipst)
4677 return (ipif_lookup_addr_common(addr, match_ill, IPIF_MATCH_ILLGRP,
4678 zoneid, ipst));
4682 * Lookup an ipif with the specified address. Similar to ipif_lookup_addr,
4683 * except that we will only return an address if it is not marked as
4684 * IPIF_DUPLICATE
4686 ipif_t *
4687 ipif_lookup_addr_nondup(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4688 ip_stack_t *ipst)
4690 return (ipif_lookup_addr_common(addr, match_ill,
4691 (IPIF_MATCH_ILLGRP | IPIF_MATCH_NONDUP),
4692 zoneid, ipst));
4696 * Special abbreviated version of ipif_lookup_addr() that doesn't match
4697 * `match_ill' across the IPMP group. This function is only needed in some
4698 * corner-cases; almost everything should use ipif_lookup_addr().
4700 ipif_t *
4701 ipif_lookup_addr_exact(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4703 ASSERT(match_ill != NULL);
4704 return (ipif_lookup_addr_common(addr, match_ill, 0, ALL_ZONES,
4705 ipst));
4709 * Look for an ipif with the specified address. For point-point links
4710 * we look for matches on either the destination address and the local
4711 * address, but we ignore the check on the local address if IPIF_UNNUMBERED
4712 * is set.
4713 * If the `match_ill' argument is non-NULL, the lookup is restricted to that
4714 * ill (or illgrp if `match_ill' is in an IPMP group).
4715 * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
4717 zoneid_t
4718 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4720 zoneid_t zoneid;
4721 ipif_t *ipif;
4722 ill_t *ill;
4723 boolean_t ptp = B_FALSE;
4724 ill_walk_context_t ctx;
4726 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4728 * Repeat twice, first based on local addresses and
4729 * next time for pointopoint.
4731 repeat:
4732 ill = ILL_START_WALK_V4(&ctx, ipst);
4733 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4734 if (match_ill != NULL && ill != match_ill &&
4735 !IS_IN_SAME_ILLGRP(ill, match_ill)) {
4736 continue;
4738 mutex_enter(&ill->ill_lock);
4739 for (ipif = ill->ill_ipif; ipif != NULL;
4740 ipif = ipif->ipif_next) {
4741 /* Allow the ipif to be down */
4742 if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4743 ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4744 (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4745 (ipif->ipif_pp_dst_addr == addr)) &&
4746 !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
4747 zoneid = ipif->ipif_zoneid;
4748 mutex_exit(&ill->ill_lock);
4749 rw_exit(&ipst->ips_ill_g_lock);
4750 return (zoneid);
4753 mutex_exit(&ill->ill_lock);
4756 /* If we already did the ptp case, then we are done */
4757 if (ptp) {
4758 rw_exit(&ipst->ips_ill_g_lock);
4759 return (ALL_ZONES);
4761 ptp = B_TRUE;
4762 goto repeat;
4766 * Look for an ipif that matches the specified remote address i.e. the
4767 * ipif that would receive the specified packet.
4768 * First look for directly connected interfaces and then do a recursive
4769 * IRE lookup and pick the first ipif corresponding to the source address in the
4770 * ire.
4771 * Returns: held ipif
4773 * This is only used for ICMP_ADDRESS_MASK_REQUESTs
4775 ipif_t *
4776 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
4778 ipif_t *ipif;
4780 ASSERT(!ill->ill_isv6);
4783 * Someone could be changing this ipif currently or change it
4784 * after we return this. Thus a few packets could use the old
4785 * old values. However structure updates/creates (ire, ilg, ilm etc)
4786 * will atomically be updated or cleaned up with the new value
4787 * Thus we don't need a lock to check the flags or other attrs below.
4789 mutex_enter(&ill->ill_lock);
4790 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4791 if (IPIF_IS_CONDEMNED(ipif))
4792 continue;
4793 if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
4794 ipif->ipif_zoneid != ALL_ZONES)
4795 continue;
4796 /* Allow the ipif to be down */
4797 if (ipif->ipif_flags & IPIF_POINTOPOINT) {
4798 if ((ipif->ipif_pp_dst_addr == addr) ||
4799 (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
4800 ipif->ipif_lcl_addr == addr)) {
4801 ipif_refhold_locked(ipif);
4802 mutex_exit(&ill->ill_lock);
4803 return (ipif);
4805 } else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
4806 ipif_refhold_locked(ipif);
4807 mutex_exit(&ill->ill_lock);
4808 return (ipif);
4811 mutex_exit(&ill->ill_lock);
4813 * For a remote destination it isn't possible to nail down a particular
4814 * ipif.
4817 /* Pick the first interface */
4818 ipif = ipif_get_next_ipif(NULL, ill);
4819 return (ipif);
4823 * This func does not prevent refcnt from increasing. But if
4824 * the caller has taken steps to that effect, then this func
4825 * can be used to determine whether the ill has become quiescent
4827 static boolean_t
4828 ill_is_quiescent(ill_t *ill)
4830 ipif_t *ipif;
4832 ASSERT(MUTEX_HELD(&ill->ill_lock));
4834 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4835 if (ipif->ipif_refcnt != 0)
4836 return (B_FALSE);
4838 if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
4839 return (B_FALSE);
4841 return (B_TRUE);
4844 boolean_t
4845 ill_is_freeable(ill_t *ill)
4847 ipif_t *ipif;
4849 ASSERT(MUTEX_HELD(&ill->ill_lock));
4851 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4852 if (ipif->ipif_refcnt != 0) {
4853 return (B_FALSE);
4856 if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
4857 return (B_FALSE);
4859 return (B_TRUE);
4863 * This func does not prevent refcnt from increasing. But if
4864 * the caller has taken steps to that effect, then this func
4865 * can be used to determine whether the ipif has become quiescent
4867 static boolean_t
4868 ipif_is_quiescent(ipif_t *ipif)
4870 ill_t *ill;
4872 ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4874 if (ipif->ipif_refcnt != 0)
4875 return (B_FALSE);
4877 ill = ipif->ipif_ill;
4878 if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
4879 ill->ill_logical_down) {
4880 return (B_TRUE);
4883 /* This is the last ipif going down or being deleted on this ill */
4884 if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
4885 return (B_FALSE);
4888 return (B_TRUE);
4892 * return true if the ipif can be destroyed: the ipif has to be quiescent
4893 * with zero references from ire/ilm to it.
4895 static boolean_t
4896 ipif_is_freeable(ipif_t *ipif)
4898 ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4899 ASSERT(ipif->ipif_id != 0);
4900 return (ipif->ipif_refcnt == 0);
4904 * The ipif/ill/ire has been refreled. Do the tail processing.
4905 * Determine if the ipif or ill in question has become quiescent and if so
4906 * wakeup close and/or restart any queued pending ioctl that is waiting
4907 * for the ipif_down (or ill_down)
4909 void
4910 ipif_ill_refrele_tail(ill_t *ill)
4912 mblk_t *mp;
4913 conn_t *connp;
4914 ipsq_t *ipsq;
4915 ipxop_t *ipx;
4916 ipif_t *ipif;
4917 dl_notify_ind_t *dlindp;
4919 ASSERT(MUTEX_HELD(&ill->ill_lock));
4921 if ((ill->ill_state_flags & ILL_CONDEMNED) && ill_is_freeable(ill)) {
4922 /* ip_modclose() may be waiting */
4923 cv_broadcast(&ill->ill_cv);
4926 ipsq = ill->ill_phyint->phyint_ipsq;
4927 mutex_enter(&ipsq->ipsq_lock);
4928 ipx = ipsq->ipsq_xop;
4929 mutex_enter(&ipx->ipx_lock);
4930 if (ipx->ipx_waitfor == 0) /* no one's waiting; bail */
4931 goto unlock;
4933 ASSERT(ipx->ipx_pending_mp != NULL && ipx->ipx_pending_ipif != NULL);
4935 ipif = ipx->ipx_pending_ipif;
4936 if (ipif->ipif_ill != ill) /* wait is for another ill; bail */
4937 goto unlock;
4939 switch (ipx->ipx_waitfor) {
4940 case IPIF_DOWN:
4941 if (!ipif_is_quiescent(ipif))
4942 goto unlock;
4943 break;
4944 case IPIF_FREE:
4945 if (!ipif_is_freeable(ipif))
4946 goto unlock;
4947 break;
4948 case ILL_DOWN:
4949 if (!ill_is_quiescent(ill))
4950 goto unlock;
4951 break;
4952 case ILL_FREE:
4954 * ILL_FREE is only for loopback; normal ill teardown waits
4955 * synchronously in ip_modclose() without using ipx_waitfor,
4956 * handled by the cv_broadcast() at the top of this function.
4958 if (!ill_is_freeable(ill))
4959 goto unlock;
4960 break;
4961 default:
4962 cmn_err(CE_PANIC, "ipsq: %p unknown ipx_waitfor %d\n",
4963 (void *)ipsq, ipx->ipx_waitfor);
4966 ill_refhold_locked(ill); /* for qwriter_ip() call below */
4967 mutex_exit(&ipx->ipx_lock);
4968 mp = ipsq_pending_mp_get(ipsq, &connp);
4969 mutex_exit(&ipsq->ipsq_lock);
4970 mutex_exit(&ill->ill_lock);
4972 ASSERT(mp != NULL);
4974 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
4975 * we can only get here when the current operation decides it
4976 * it needs to quiesce via ipsq_pending_mp_add().
4978 switch (mp->b_datap->db_type) {
4979 case M_PCPROTO:
4980 case M_PROTO:
4982 * For now, only DL_NOTIFY_IND messages can use this facility.
4984 dlindp = (dl_notify_ind_t *)mp->b_rptr;
4985 ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
4987 switch (dlindp->dl_notification) {
4988 case DL_NOTE_PHYS_ADDR:
4989 qwriter_ip(ill, ill->ill_rq, mp,
4990 ill_set_phys_addr_tail, CUR_OP, B_TRUE);
4991 return;
4992 case DL_NOTE_REPLUMB:
4993 qwriter_ip(ill, ill->ill_rq, mp,
4994 ill_replumb_tail, CUR_OP, B_TRUE);
4995 return;
4996 default:
4997 ASSERT(0);
4998 ill_refrele(ill);
5000 break;
5002 case M_ERROR:
5003 case M_HANGUP:
5004 qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
5005 B_TRUE);
5006 return;
5008 case M_IOCTL:
5009 case M_IOCDATA:
5010 qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
5011 ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
5012 return;
5014 default:
5015 cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
5016 "db_type %d\n", (void *)mp, mp->b_datap->db_type);
5018 return;
5019 unlock:
5020 mutex_exit(&ipsq->ipsq_lock);
5021 mutex_exit(&ipx->ipx_lock);
5022 mutex_exit(&ill->ill_lock);
5025 #ifdef DEBUG
5026 /* Reuse trace buffer from beginning (if reached the end) and record trace */
5027 static void
5028 th_trace_rrecord(th_trace_t *th_trace)
5030 tr_buf_t *tr_buf;
5031 uint_t lastref;
5033 lastref = th_trace->th_trace_lastref;
5034 lastref++;
5035 if (lastref == TR_BUF_MAX)
5036 lastref = 0;
5037 th_trace->th_trace_lastref = lastref;
5038 tr_buf = &th_trace->th_trbuf[lastref];
5039 tr_buf->tr_time = ddi_get_lbolt();
5040 tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
5043 static void
5044 th_trace_free(void *value)
5046 th_trace_t *th_trace = value;
5048 ASSERT(th_trace->th_refcnt == 0);
5049 kmem_free(th_trace, sizeof (*th_trace));
5053 * Find or create the per-thread hash table used to track object references.
5054 * The ipst argument is NULL if we shouldn't allocate.
5056 * Accesses per-thread data, so there's no need to lock here.
5058 static mod_hash_t *
5059 th_trace_gethash(ip_stack_t *ipst)
5061 th_hash_t *thh;
5063 if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
5064 mod_hash_t *mh;
5065 char name[256];
5066 size_t objsize, rshift;
5067 int retv;
5069 if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
5070 return (NULL);
5071 (void) snprintf(name, sizeof (name), "th_trace_%p",
5072 (void *)curthread);
5075 * We use mod_hash_create_extended here rather than the more
5076 * obvious mod_hash_create_ptrhash because the latter has a
5077 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
5078 * block.
5080 objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
5081 MAX(sizeof (ire_t), sizeof (ncec_t)));
5082 rshift = highbit(objsize);
5083 mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
5084 th_trace_free, mod_hash_byptr, (void *)rshift,
5085 mod_hash_ptrkey_cmp, KM_NOSLEEP);
5086 if (mh == NULL) {
5087 kmem_free(thh, sizeof (*thh));
5088 return (NULL);
5090 thh->thh_hash = mh;
5091 thh->thh_ipst = ipst;
5093 * We trace ills, ipifs, ires, and nces. All of these are
5094 * per-IP-stack, so the lock on the thread list is as well.
5096 rw_enter(&ip_thread_rwlock, RW_WRITER);
5097 list_insert_tail(&ip_thread_list, thh);
5098 rw_exit(&ip_thread_rwlock);
5099 retv = tsd_set(ip_thread_data, thh);
5100 ASSERT(retv == 0);
5102 return (thh != NULL ? thh->thh_hash : NULL);
5105 boolean_t
5106 th_trace_ref(const void *obj, ip_stack_t *ipst)
5108 th_trace_t *th_trace;
5109 mod_hash_t *mh;
5110 mod_hash_val_t val;
5112 if ((mh = th_trace_gethash(ipst)) == NULL)
5113 return (B_FALSE);
5116 * Attempt to locate the trace buffer for this obj and thread.
5117 * If it does not exist, then allocate a new trace buffer and
5118 * insert into the hash.
5120 if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
5121 th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
5122 if (th_trace == NULL)
5123 return (B_FALSE);
5125 th_trace->th_id = curthread;
5126 if (mod_hash_insert(mh, (mod_hash_key_t)obj,
5127 (mod_hash_val_t)th_trace) != 0) {
5128 kmem_free(th_trace, sizeof (th_trace_t));
5129 return (B_FALSE);
5131 } else {
5132 th_trace = (th_trace_t *)val;
5135 ASSERT(th_trace->th_refcnt >= 0 &&
5136 th_trace->th_refcnt < TR_BUF_MAX - 1);
5138 th_trace->th_refcnt++;
5139 th_trace_rrecord(th_trace);
5140 return (B_TRUE);
5144 * For the purpose of tracing a reference release, we assume that global
5145 * tracing is always on and that the same thread initiated the reference hold
5146 * is releasing.
5148 void
5149 th_trace_unref(const void *obj)
5151 int retv;
5152 mod_hash_t *mh;
5153 th_trace_t *th_trace;
5154 mod_hash_val_t val;
5156 mh = th_trace_gethash(NULL);
5157 retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
5158 ASSERT(retv == 0);
5159 th_trace = (th_trace_t *)val;
5161 ASSERT(th_trace->th_refcnt > 0);
5162 th_trace->th_refcnt--;
5163 th_trace_rrecord(th_trace);
5167 * If tracing has been disabled, then we assume that the reference counts are
5168 * now useless, and we clear them out before destroying the entries.
5170 void
5171 th_trace_cleanup(const void *obj, boolean_t trace_disable)
5173 th_hash_t *thh;
5174 mod_hash_t *mh;
5175 mod_hash_val_t val;
5176 th_trace_t *th_trace;
5177 int retv;
5179 rw_enter(&ip_thread_rwlock, RW_READER);
5180 for (thh = list_head(&ip_thread_list); thh != NULL;
5181 thh = list_next(&ip_thread_list, thh)) {
5182 if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
5183 &val) == 0) {
5184 th_trace = (th_trace_t *)val;
5185 if (trace_disable)
5186 th_trace->th_refcnt = 0;
5187 retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
5188 ASSERT(retv == 0);
5191 rw_exit(&ip_thread_rwlock);
5194 void
5195 ipif_trace_ref(ipif_t *ipif)
5197 ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5199 if (ipif->ipif_trace_disable)
5200 return;
5202 if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
5203 ipif->ipif_trace_disable = B_TRUE;
5204 ipif_trace_cleanup(ipif);
5208 void
5209 ipif_untrace_ref(ipif_t *ipif)
5211 ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5213 if (!ipif->ipif_trace_disable)
5214 th_trace_unref(ipif);
5217 void
5218 ill_trace_ref(ill_t *ill)
5220 ASSERT(MUTEX_HELD(&ill->ill_lock));
5222 if (ill->ill_trace_disable)
5223 return;
5225 if (!th_trace_ref(ill, ill->ill_ipst)) {
5226 ill->ill_trace_disable = B_TRUE;
5227 ill_trace_cleanup(ill);
5231 void
5232 ill_untrace_ref(ill_t *ill)
5234 ASSERT(MUTEX_HELD(&ill->ill_lock));
5236 if (!ill->ill_trace_disable)
5237 th_trace_unref(ill);
5241 * Called when ipif is unplumbed or when memory alloc fails. Note that on
5242 * failure, ipif_trace_disable is set.
5244 static void
5245 ipif_trace_cleanup(const ipif_t *ipif)
5247 th_trace_cleanup(ipif, ipif->ipif_trace_disable);
5251 * Called when ill is unplumbed or when memory alloc fails. Note that on
5252 * failure, ill_trace_disable is set.
5254 static void
5255 ill_trace_cleanup(const ill_t *ill)
5257 th_trace_cleanup(ill, ill->ill_trace_disable);
5259 #endif /* DEBUG */
5261 void
5262 ipif_refhold_locked(ipif_t *ipif)
5264 ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5265 ipif->ipif_refcnt++;
5266 IPIF_TRACE_REF(ipif);
5269 void
5270 ipif_refhold(ipif_t *ipif)
5272 ill_t *ill;
5274 ill = ipif->ipif_ill;
5275 mutex_enter(&ill->ill_lock);
5276 ipif->ipif_refcnt++;
5277 IPIF_TRACE_REF(ipif);
5278 mutex_exit(&ill->ill_lock);
5282 * Must not be called while holding any locks. Otherwise if this is
5283 * the last reference to be released there is a chance of recursive mutex
5284 * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5285 * to restart an ioctl.
5287 void
5288 ipif_refrele(ipif_t *ipif)
5290 ill_t *ill;
5292 ill = ipif->ipif_ill;
5294 mutex_enter(&ill->ill_lock);
5295 ASSERT(ipif->ipif_refcnt != 0);
5296 ipif->ipif_refcnt--;
5297 IPIF_UNTRACE_REF(ipif);
5298 if (ipif->ipif_refcnt != 0) {
5299 mutex_exit(&ill->ill_lock);
5300 return;
5303 /* Drops the ill_lock */
5304 ipif_ill_refrele_tail(ill);
5307 ipif_t *
5308 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
5310 ipif_t *ipif;
5312 mutex_enter(&ill->ill_lock);
5313 for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
5314 ipif != NULL; ipif = ipif->ipif_next) {
5315 if (IPIF_IS_CONDEMNED(ipif))
5316 continue;
5317 ipif_refhold_locked(ipif);
5318 mutex_exit(&ill->ill_lock);
5319 return (ipif);
5321 mutex_exit(&ill->ill_lock);
5322 return (NULL);
5326 * TODO: make this table extendible at run time
5327 * Return a pointer to the mac type info for 'mac_type'
5329 static ip_m_t *
5330 ip_m_lookup(t_uscalar_t mac_type)
5332 ip_m_t *ipm;
5334 for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
5335 if (ipm->ip_m_mac_type == mac_type)
5336 return (ipm);
5337 return (NULL);
5341 * Make a link layer address from the multicast IP address *addr.
5342 * To form the link layer address, invoke the ip_m_v*mapping function
5343 * associated with the link-layer type.
5345 void
5346 ip_mcast_mapping(ill_t *ill, uchar_t *addr, uchar_t *hwaddr)
5348 ip_m_t *ipm;
5350 if (ill->ill_net_type == IRE_IF_NORESOLVER)
5351 return;
5353 ASSERT(addr != NULL);
5355 ipm = ip_m_lookup(ill->ill_mactype);
5356 if (ipm == NULL ||
5357 (ill->ill_isv6 && ipm->ip_m_v6mapping == NULL) ||
5358 (!ill->ill_isv6 && ipm->ip_m_v4mapping == NULL)) {
5359 ip0dbg(("no mapping for ill %s mactype 0x%x\n",
5360 ill->ill_name, ill->ill_mactype));
5361 return;
5363 if (ill->ill_isv6)
5364 (*ipm->ip_m_v6mapping)(ill, addr, hwaddr);
5365 else
5366 (*ipm->ip_m_v4mapping)(ill, addr, hwaddr);
5370 * Returns B_FALSE if the IPv4 netmask pointed by `mask' is non-contiguous.
5371 * Otherwise returns B_TRUE.
5373 * The netmask can be verified to be contiguous with 32 shifts and or
5374 * operations. Take the contiguous mask (in host byte order) and compute
5375 * mask | mask << 1 | mask << 2 | ... | mask << 31
5376 * the result will be the same as the 'mask' for contiguous mask.
5378 static boolean_t
5379 ip_contiguous_mask(uint32_t mask)
5381 uint32_t m = mask;
5382 int i;
5384 for (i = 1; i < 32; i++)
5385 m |= (mask << i);
5387 return (m == mask);
5391 * ip_rt_add is called to add an IPv4 route to the forwarding table.
5392 * ill is passed in to associate it with the correct interface.
5393 * If ire_arg is set, then we return the held IRE in that location.
5396 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5397 ipaddr_t src_addr, int flags, ill_t *ill, ire_t **ire_arg,
5398 boolean_t ioctl_msg, ip_stack_t *ipst, zoneid_t zoneid)
5400 ire_t *ire, *nire;
5401 ire_t *gw_ire = NULL;
5402 ipif_t *ipif = NULL;
5403 uint_t type;
5404 int match_flags = MATCH_IRE_TYPE;
5405 boolean_t cgtp_broadcast;
5406 boolean_t unbound = B_FALSE;
5408 ip1dbg(("ip_rt_add:"));
5410 if (ire_arg != NULL)
5411 *ire_arg = NULL;
5413 /* disallow non-contiguous netmasks */
5414 if (!ip_contiguous_mask(ntohl(mask)))
5415 return (ENOTSUP);
5418 * If this is the case of RTF_HOST being set, then we set the netmask
5419 * to all ones (regardless if one was supplied).
5421 if (flags & RTF_HOST)
5422 mask = IP_HOST_MASK;
5425 * Prevent routes with a zero gateway from being created (since
5426 * interfaces can currently be plumbed and brought up no assigned
5427 * address).
5429 if (gw_addr == 0)
5430 return (ENETUNREACH);
5432 * Get the ipif, if any, corresponding to the gw_addr
5433 * If -ifp was specified we restrict ourselves to the ill, otherwise
5434 * we match on the gatway and destination to handle unnumbered pt-pt
5435 * interfaces.
5437 if (ill != NULL)
5438 ipif = ipif_lookup_addr(gw_addr, ill, ALL_ZONES, ipst);
5439 else
5440 ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
5441 if (ipif != NULL) {
5442 if (IS_VNI(ipif->ipif_ill)) {
5443 ipif_refrele(ipif);
5444 return (EINVAL);
5449 * GateD will attempt to create routes with a loopback interface
5450 * address as the gateway and with RTF_GATEWAY set. We allow
5451 * these routes to be added, but create them as interface routes
5452 * since the gateway is an interface address.
5454 if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
5455 flags &= ~RTF_GATEWAY;
5456 if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
5457 mask == IP_HOST_MASK) {
5458 ire = ire_ftable_lookup_v4(dst_addr, 0, 0, IRE_LOOPBACK,
5459 NULL, ALL_ZONES, MATCH_IRE_TYPE, 0, ipst, NULL);
5460 if (ire != NULL) {
5461 ire_refrele(ire);
5462 ipif_refrele(ipif);
5463 return (EEXIST);
5465 ip1dbg(("ip_rt_add: 0x%p creating IRE 0x%x"
5466 "for 0x%x\n", (void *)ipif,
5467 ipif->ipif_ire_type,
5468 ntohl(ipif->ipif_lcl_addr)));
5469 ire = ire_create(
5470 (uchar_t *)&dst_addr, /* dest address */
5471 (uchar_t *)&mask, /* mask */
5472 NULL, /* no gateway */
5473 ipif->ipif_ire_type, /* LOOPBACK */
5474 ipif->ipif_ill,
5475 zoneid,
5476 (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0,
5477 ipst);
5479 if (ire == NULL) {
5480 ipif_refrele(ipif);
5481 return (ENOMEM);
5483 /* src address assigned by the caller? */
5484 if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5485 ire->ire_setsrc_addr = src_addr;
5487 nire = ire_add(ire);
5488 if (nire == NULL) {
5490 * In the result of failure, ire_add() will have
5491 * already deleted the ire in question, so there
5492 * is no need to do that here.
5494 ipif_refrele(ipif);
5495 return (ENOMEM);
5498 * Check if it was a duplicate entry. This handles
5499 * the case of two racing route adds for the same route
5501 if (nire != ire) {
5502 ASSERT(nire->ire_identical_ref > 1);
5503 ire_delete(nire);
5504 ire_refrele(nire);
5505 ipif_refrele(ipif);
5506 return (EEXIST);
5508 ire = nire;
5509 goto save_ire;
5514 * The routes for multicast with CGTP are quite special in that
5515 * the gateway is the local interface address, yet RTF_GATEWAY
5516 * is set. We turn off RTF_GATEWAY to provide compatibility with
5517 * this undocumented and unusual use of multicast routes.
5519 if ((flags & RTF_MULTIRT) && ipif != NULL)
5520 flags &= ~RTF_GATEWAY;
5523 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
5524 * and the gateway address provided is one of the system's interface
5525 * addresses. By using the routing socket interface and supplying an
5526 * RTA_IFP sockaddr with an interface index, an alternate method of
5527 * specifying an interface route to be created is available which uses
5528 * the interface index that specifies the outgoing interface rather than
5529 * the address of an outgoing interface (which may not be able to
5530 * uniquely identify an interface). When coupled with the RTF_GATEWAY
5531 * flag, routes can be specified which not only specify the next-hop to
5532 * be used when routing to a certain prefix, but also which outgoing
5533 * interface should be used.
5535 * Previously, interfaces would have unique addresses assigned to them
5536 * and so the address assigned to a particular interface could be used
5537 * to identify a particular interface. One exception to this was the
5538 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
5540 * With the advent of IPv6 and its link-local addresses, this
5541 * restriction was relaxed and interfaces could share addresses between
5542 * themselves. In fact, typically all of the link-local interfaces on
5543 * an IPv6 node or router will have the same link-local address. In
5544 * order to differentiate between these interfaces, the use of an
5545 * interface index is necessary and this index can be carried inside a
5546 * RTA_IFP sockaddr (which is actually a sockaddr_dl). One restriction
5547 * of using the interface index, however, is that all of the ipif's that
5548 * are part of an ill have the same index and so the RTA_IFP sockaddr
5549 * cannot be used to differentiate between ipif's (or logical
5550 * interfaces) that belong to the same ill (physical interface).
5552 * For example, in the following case involving IPv4 interfaces and
5553 * logical interfaces
5555 * 192.0.2.32 255.255.255.224 192.0.2.33 U if0
5556 * 192.0.2.32 255.255.255.224 192.0.2.34 U if0
5557 * 192.0.2.32 255.255.255.224 192.0.2.35 U if0
5559 * the ipif's corresponding to each of these interface routes can be
5560 * uniquely identified by the "gateway" (actually interface address).
5562 * In this case involving multiple IPv6 default routes to a particular
5563 * link-local gateway, the use of RTA_IFP is necessary to specify which
5564 * default route is of interest:
5566 * default fe80::123:4567:89ab:cdef U if0
5567 * default fe80::123:4567:89ab:cdef U if1
5570 /* RTF_GATEWAY not set */
5571 if (!(flags & RTF_GATEWAY)) {
5573 * Whether or not ill (RTA_IFP) is set, we require that
5574 * the gateway is one of our local addresses.
5576 if (ipif == NULL)
5577 return (ENETUNREACH);
5580 * We use MATCH_IRE_ILL here. If the caller specified an
5581 * interface (from the RTA_IFP sockaddr) we use it, otherwise
5582 * we use the ill derived from the gateway address.
5583 * We can always match the gateway address since we record it
5584 * in ire_gateway_addr.
5585 * We don't allow RTA_IFP to specify a different ill than the
5586 * one matching the ipif to make sure we can delete the route.
5588 match_flags |= MATCH_IRE_GW | MATCH_IRE_ILL;
5589 if (ill == NULL) {
5590 ill = ipif->ipif_ill;
5591 } else if (ill != ipif->ipif_ill) {
5592 ipif_refrele(ipif);
5593 return (EINVAL);
5597 * We check for an existing entry at this point.
5599 * Since a netmask isn't passed in via the ioctl interface
5600 * (SIOCADDRT), we don't check for a matching netmask in that
5601 * case.
5603 if (!ioctl_msg)
5604 match_flags |= MATCH_IRE_MASK;
5605 ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
5606 IRE_INTERFACE, ill, ALL_ZONES, match_flags, 0, ipst, NULL);
5607 if (ire != NULL) {
5608 ire_refrele(ire);
5609 ipif_refrele(ipif);
5610 return (EEXIST);
5614 * Some software (for example, GateD and Sun Cluster) attempts
5615 * to create (what amount to) IRE_PREFIX routes with the
5616 * loopback address as the gateway. This is primarily done to
5617 * set up prefixes with the RTF_REJECT flag set (for example,
5618 * when generating aggregate routes.)
5620 * If the IRE type (as defined by ill->ill_net_type) would be
5621 * IRE_LOOPBACK, then we map the request into a
5622 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
5623 * these interface routes, by definition, can only be that.
5625 * Needless to say, the real IRE_LOOPBACK is NOT created by this
5626 * routine, but rather using ire_create() directly.
5629 type = ill->ill_net_type;
5630 if (type == IRE_LOOPBACK) {
5631 type = IRE_IF_NORESOLVER;
5632 flags |= RTF_BLACKHOLE;
5636 * Create a copy of the IRE_IF_NORESOLVER or
5637 * IRE_IF_RESOLVER with the modified address, netmask, and
5638 * gateway.
5640 ire = ire_create(
5641 (uchar_t *)&dst_addr,
5642 (uint8_t *)&mask,
5643 (uint8_t *)&gw_addr,
5644 type,
5645 ill,
5646 zoneid,
5647 flags,
5648 ipst);
5649 if (ire == NULL) {
5650 ipif_refrele(ipif);
5651 return (ENOMEM);
5654 /* src address assigned by the caller? */
5655 if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5656 ire->ire_setsrc_addr = src_addr;
5658 nire = ire_add(ire);
5659 if (nire == NULL) {
5661 * In the result of failure, ire_add() will have
5662 * already deleted the ire in question, so there
5663 * is no need to do that here.
5665 ipif_refrele(ipif);
5666 return (ENOMEM);
5669 * Check if it was a duplicate entry. This handles
5670 * the case of two racing route adds for the same route
5672 if (nire != ire) {
5673 ire_delete(nire);
5674 ire_refrele(nire);
5675 ipif_refrele(ipif);
5676 return (EEXIST);
5678 ire = nire;
5679 goto save_ire;
5683 * Get an interface IRE for the specified gateway.
5684 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
5685 * gateway, it is currently unreachable and we fail the request
5686 * accordingly. We reject any RTF_GATEWAY routes where the gateway
5687 * is an IRE_LOCAL or IRE_LOOPBACK.
5688 * If RTA_IFP was specified we look on that particular ill.
5690 if (ill != NULL)
5691 match_flags |= MATCH_IRE_ILL;
5693 /* Check whether the gateway is reachable. */
5694 again:
5695 type = IRE_INTERFACE | IRE_LOCAL | IRE_LOOPBACK;
5696 if (flags & RTF_INDIRECT)
5697 type |= IRE_OFFLINK;
5699 gw_ire = ire_ftable_lookup_v4(gw_addr, 0, 0, type, ill,
5700 ALL_ZONES, match_flags, 0, ipst, NULL);
5701 if (gw_ire == NULL) {
5703 * With IPMP, we allow host routes to influence in.mpathd's
5704 * target selection. However, if the test addresses are on
5705 * their own network, the above lookup will fail since the
5706 * underlying IRE_INTERFACEs are marked hidden. So allow
5707 * hidden test IREs to be found and try again.
5709 if (!(match_flags & MATCH_IRE_TESTHIDDEN)) {
5710 match_flags |= MATCH_IRE_TESTHIDDEN;
5711 goto again;
5713 if (ipif != NULL)
5714 ipif_refrele(ipif);
5715 return (ENETUNREACH);
5717 if (gw_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) {
5718 ire_refrele(gw_ire);
5719 if (ipif != NULL)
5720 ipif_refrele(ipif);
5721 return (ENETUNREACH);
5724 if (ill == NULL && !(flags & RTF_INDIRECT)) {
5725 unbound = B_TRUE;
5726 if (ipst->ips_ip_strict_src_multihoming > 0)
5727 ill = gw_ire->ire_ill;
5731 * We create one of three types of IREs as a result of this request
5732 * based on the netmask. A netmask of all ones (which is automatically
5733 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
5734 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
5735 * created. Otherwise, an IRE_PREFIX route is created for the
5736 * destination prefix.
5738 if (mask == IP_HOST_MASK)
5739 type = IRE_HOST;
5740 else if (mask == 0)
5741 type = IRE_DEFAULT;
5742 else
5743 type = IRE_PREFIX;
5745 /* check for a duplicate entry */
5746 ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
5747 ALL_ZONES, match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, 0, ipst,
5748 NULL);
5749 if (ire != NULL) {
5750 if (ipif != NULL)
5751 ipif_refrele(ipif);
5752 ire_refrele(gw_ire);
5753 ire_refrele(ire);
5754 return (EEXIST);
5757 /* Create the IRE. */
5758 ire = ire_create(
5759 (uchar_t *)&dst_addr, /* dest address */
5760 (uchar_t *)&mask, /* mask */
5761 (uchar_t *)&gw_addr, /* gateway address */
5762 (ushort_t)type, /* IRE type */
5763 ill,
5764 zoneid,
5765 flags,
5766 ipst);
5768 if (ire == NULL) {
5769 if (ipif != NULL)
5770 ipif_refrele(ipif);
5771 ire_refrele(gw_ire);
5772 return (ENOMEM);
5775 /* Before we add, check if an extra CGTP broadcast is needed */
5776 cgtp_broadcast = ((flags & RTF_MULTIRT) &&
5777 ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST);
5779 /* src address assigned by the caller? */
5780 if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5781 ire->ire_setsrc_addr = src_addr;
5783 ire->ire_unbound = unbound;
5786 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
5787 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
5790 /* Add the new IRE. */
5791 nire = ire_add(ire);
5792 if (nire == NULL) {
5794 * In the result of failure, ire_add() will have
5795 * already deleted the ire in question, so there
5796 * is no need to do that here.
5798 if (ipif != NULL)
5799 ipif_refrele(ipif);
5800 ire_refrele(gw_ire);
5801 return (ENOMEM);
5804 * Check if it was a duplicate entry. This handles
5805 * the case of two racing route adds for the same route
5807 if (nire != ire) {
5808 ire_delete(nire);
5809 ire_refrele(nire);
5810 if (ipif != NULL)
5811 ipif_refrele(ipif);
5812 ire_refrele(gw_ire);
5813 return (EEXIST);
5815 ire = nire;
5817 if (flags & RTF_MULTIRT) {
5819 * Invoke the CGTP (multirouting) filtering module
5820 * to add the dst address in the filtering database.
5821 * Replicated inbound packets coming from that address
5822 * will be filtered to discard the duplicates.
5823 * It is not necessary to call the CGTP filter hook
5824 * when the dst address is a broadcast or multicast,
5825 * because an IP source address cannot be a broadcast
5826 * or a multicast.
5828 if (cgtp_broadcast) {
5829 ip_cgtp_bcast_add(ire, ipst);
5830 goto save_ire;
5832 if (ipst->ips_ip_cgtp_filter_ops != NULL &&
5833 !CLASSD(ire->ire_addr)) {
5834 int res;
5835 ipif_t *src_ipif;
5837 /* Find the source address corresponding to gw_ire */
5838 src_ipif = ipif_lookup_addr(gw_ire->ire_gateway_addr,
5839 NULL, zoneid, ipst);
5840 if (src_ipif != NULL) {
5841 res = ipst->ips_ip_cgtp_filter_ops->
5842 cfo_add_dest_v4(
5843 ipst->ips_netstack->netstack_stackid,
5844 ire->ire_addr,
5845 ire->ire_gateway_addr,
5846 ire->ire_setsrc_addr,
5847 src_ipif->ipif_lcl_addr);
5848 ipif_refrele(src_ipif);
5849 } else {
5850 res = EADDRNOTAVAIL;
5852 if (res != 0) {
5853 if (ipif != NULL)
5854 ipif_refrele(ipif);
5855 ire_refrele(gw_ire);
5856 ire_delete(ire);
5857 ire_refrele(ire); /* Held in ire_add */
5858 return (res);
5863 save_ire:
5864 if (gw_ire != NULL) {
5865 ire_refrele(gw_ire);
5866 gw_ire = NULL;
5868 if (ill != NULL) {
5870 * Save enough information so that we can recreate the IRE if
5871 * the interface goes down and then up. The metrics associated
5872 * with the route will be saved as well when rts_setmetrics() is
5873 * called after the IRE has been created. In the case where
5874 * memory cannot be allocated, none of this information will be
5875 * saved.
5877 ill_save_ire(ill, ire);
5879 if (ioctl_msg)
5880 ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
5881 if (ire_arg != NULL) {
5883 * Store the ire that was successfully added into where ire_arg
5884 * points to so that callers don't have to look it up
5885 * themselves (but they are responsible for ire_refrele()ing
5886 * the ire when they are finished with it).
5888 *ire_arg = ire;
5889 } else {
5890 ire_refrele(ire); /* Held in ire_add */
5892 if (ipif != NULL)
5893 ipif_refrele(ipif);
5894 return (0);
5898 * ip_rt_delete is called to delete an IPv4 route.
5899 * ill is passed in to associate it with the correct interface.
5901 /* ARGSUSED4 */
5903 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5904 uint_t rtm_addrs, int flags, ill_t *ill, boolean_t ioctl_msg,
5905 ip_stack_t *ipst, zoneid_t zoneid)
5907 ire_t *ire = NULL;
5908 ipif_t *ipif;
5909 uint_t type;
5910 uint_t match_flags = MATCH_IRE_TYPE;
5911 int err = 0;
5913 ip1dbg(("ip_rt_delete:"));
5915 * If this is the case of RTF_HOST being set, then we set the netmask
5916 * to all ones. Otherwise, we use the netmask if one was supplied.
5918 if (flags & RTF_HOST) {
5919 mask = IP_HOST_MASK;
5920 match_flags |= MATCH_IRE_MASK;
5921 } else if (rtm_addrs & RTA_NETMASK) {
5922 match_flags |= MATCH_IRE_MASK;
5926 * Note that RTF_GATEWAY is never set on a delete, therefore
5927 * we check if the gateway address is one of our interfaces first,
5928 * and fall back on RTF_GATEWAY routes.
5930 * This makes it possible to delete an original
5931 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
5932 * However, we have RTF_KERNEL set on the ones created by ipif_up
5933 * and those can not be deleted here.
5935 * We use MATCH_IRE_ILL if we know the interface. If the caller
5936 * specified an interface (from the RTA_IFP sockaddr) we use it,
5937 * otherwise we use the ill derived from the gateway address.
5938 * We can always match the gateway address since we record it
5939 * in ire_gateway_addr.
5941 * For more detail on specifying routes by gateway address and by
5942 * interface index, see the comments in ip_rt_add().
5944 ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
5945 if (ipif != NULL) {
5946 ill_t *ill_match;
5948 if (ill != NULL)
5949 ill_match = ill;
5950 else
5951 ill_match = ipif->ipif_ill;
5953 match_flags |= MATCH_IRE_ILL;
5954 if (ipif->ipif_ire_type == IRE_LOOPBACK) {
5955 ire = ire_ftable_lookup_v4(dst_addr, mask, 0,
5956 IRE_LOOPBACK, ill_match, ALL_ZONES, match_flags, 0,
5957 ipst, NULL);
5959 if (ire == NULL) {
5960 match_flags |= MATCH_IRE_GW;
5961 ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
5962 IRE_INTERFACE, ill_match, ALL_ZONES, match_flags,
5963 0, ipst, NULL);
5965 /* Avoid deleting routes created by kernel from an ipif */
5966 if (ire != NULL && (ire->ire_flags & RTF_KERNEL)) {
5967 ire_refrele(ire);
5968 ire = NULL;
5971 /* Restore in case we didn't find a match */
5972 match_flags &= ~(MATCH_IRE_GW|MATCH_IRE_ILL);
5975 if (ire == NULL) {
5977 * At this point, the gateway address is not one of our own
5978 * addresses or a matching interface route was not found. We
5979 * set the IRE type to lookup based on whether
5980 * this is a host route, a default route or just a prefix.
5982 * If an ill was passed in, then the lookup is based on an
5983 * interface index so MATCH_IRE_ILL is added to match_flags.
5985 match_flags |= MATCH_IRE_GW;
5986 if (ill != NULL)
5987 match_flags |= MATCH_IRE_ILL;
5988 if (mask == IP_HOST_MASK)
5989 type = IRE_HOST;
5990 else if (mask == 0)
5991 type = IRE_DEFAULT;
5992 else
5993 type = IRE_PREFIX;
5994 ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
5995 ALL_ZONES, match_flags, 0, ipst, NULL);
5998 if (ipif != NULL) {
5999 ipif_refrele(ipif);
6000 ipif = NULL;
6003 if (ire == NULL)
6004 return (ESRCH);
6006 if (ire->ire_flags & RTF_MULTIRT) {
6008 * Invoke the CGTP (multirouting) filtering module
6009 * to remove the dst address from the filtering database.
6010 * Packets coming from that address will no longer be
6011 * filtered to remove duplicates.
6013 if (ipst->ips_ip_cgtp_filter_ops != NULL) {
6014 err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
6015 ipst->ips_netstack->netstack_stackid,
6016 ire->ire_addr, ire->ire_gateway_addr);
6018 ip_cgtp_bcast_delete(ire, ipst);
6021 ill = ire->ire_ill;
6022 if (ill != NULL)
6023 ill_remove_saved_ire(ill, ire);
6024 if (ioctl_msg)
6025 ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
6026 ire_delete(ire);
6027 ire_refrele(ire);
6028 return (err);
6032 * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
6034 /* ARGSUSED */
6036 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6037 ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6039 ipaddr_t dst_addr;
6040 ipaddr_t gw_addr;
6041 ipaddr_t mask;
6042 int error = 0;
6043 mblk_t *mp1;
6044 struct rtentry *rt;
6045 ipif_t *ipif = NULL;
6046 ip_stack_t *ipst;
6048 ASSERT(q->q_next == NULL);
6049 ipst = CONNQ_TO_IPST(q);
6051 ip1dbg(("ip_siocaddrt:"));
6052 /* Existence of mp1 verified in ip_wput_nondata */
6053 mp1 = mp->b_cont->b_cont;
6054 rt = (struct rtentry *)mp1->b_rptr;
6056 dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6057 gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6060 * If the RTF_HOST flag is on, this is a request to assign a gateway
6061 * to a particular host address. In this case, we set the netmask to
6062 * all ones for the particular destination address. Otherwise,
6063 * determine the netmask to be used based on dst_addr and the interfaces
6064 * in use.
6066 if (rt->rt_flags & RTF_HOST) {
6067 mask = IP_HOST_MASK;
6068 } else {
6070 * Note that ip_subnet_mask returns a zero mask in the case of
6071 * default (an all-zeroes address).
6073 mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6076 error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
6077 B_TRUE, ipst, ALL_ZONES);
6078 if (ipif != NULL)
6079 ipif_refrele(ipif);
6080 return (error);
6084 * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
6086 /* ARGSUSED */
6088 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6089 ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6091 ipaddr_t dst_addr;
6092 ipaddr_t gw_addr;
6093 ipaddr_t mask;
6094 int error;
6095 mblk_t *mp1;
6096 struct rtentry *rt;
6097 ipif_t *ipif = NULL;
6098 ip_stack_t *ipst;
6100 ASSERT(q->q_next == NULL);
6101 ipst = CONNQ_TO_IPST(q);
6103 ip1dbg(("ip_siocdelrt:"));
6104 /* Existence of mp1 verified in ip_wput_nondata */
6105 mp1 = mp->b_cont->b_cont;
6106 rt = (struct rtentry *)mp1->b_rptr;
6108 dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6109 gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6112 * If the RTF_HOST flag is on, this is a request to delete a gateway
6113 * to a particular host address. In this case, we set the netmask to
6114 * all ones for the particular destination address. Otherwise,
6115 * determine the netmask to be used based on dst_addr and the interfaces
6116 * in use.
6118 if (rt->rt_flags & RTF_HOST) {
6119 mask = IP_HOST_MASK;
6120 } else {
6122 * Note that ip_subnet_mask returns a zero mask in the case of
6123 * default (an all-zeroes address).
6125 mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6128 error = ip_rt_delete(dst_addr, mask, gw_addr,
6129 RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE,
6130 ipst, ALL_ZONES);
6131 if (ipif != NULL)
6132 ipif_refrele(ipif);
6133 return (error);
6137 * Enqueue the mp onto the ipsq, chained by b_next.
6138 * b_prev stores the function to be executed later, and b_queue the queue
6139 * where this mp originated.
6141 void
6142 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6143 ill_t *pending_ill)
6145 conn_t *connp;
6146 ipxop_t *ipx = ipsq->ipsq_xop;
6148 ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
6149 ASSERT(MUTEX_HELD(&ipx->ipx_lock));
6150 ASSERT(func != NULL);
6152 mp->b_queue = q;
6153 mp->b_prev = (void *)func;
6154 mp->b_next = NULL;
6156 switch (type) {
6157 case CUR_OP:
6158 if (ipx->ipx_mptail != NULL) {
6159 ASSERT(ipx->ipx_mphead != NULL);
6160 ipx->ipx_mptail->b_next = mp;
6161 } else {
6162 ASSERT(ipx->ipx_mphead == NULL);
6163 ipx->ipx_mphead = mp;
6165 ipx->ipx_mptail = mp;
6166 break;
6168 case NEW_OP:
6169 if (ipsq->ipsq_xopq_mptail != NULL) {
6170 ASSERT(ipsq->ipsq_xopq_mphead != NULL);
6171 ipsq->ipsq_xopq_mptail->b_next = mp;
6172 } else {
6173 ASSERT(ipsq->ipsq_xopq_mphead == NULL);
6174 ipsq->ipsq_xopq_mphead = mp;
6176 ipsq->ipsq_xopq_mptail = mp;
6177 ipx->ipx_ipsq_queued = B_TRUE;
6178 break;
6180 case SWITCH_OP:
6181 ASSERT(ipsq->ipsq_swxop != NULL);
6182 /* only one switch operation is currently allowed */
6183 ASSERT(ipsq->ipsq_switch_mp == NULL);
6184 ipsq->ipsq_switch_mp = mp;
6185 ipx->ipx_ipsq_queued = B_TRUE;
6186 break;
6187 default:
6188 cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
6191 if (CONN_Q(q) && pending_ill != NULL) {
6192 connp = Q_TO_CONN(q);
6193 ASSERT(MUTEX_HELD(&connp->conn_lock));
6194 connp->conn_oper_pending_ill = pending_ill;
6199 * Dequeue the next message that requested exclusive access to this IPSQ's
6200 * xop. Specifically:
6202 * 1. If we're still processing the current operation on `ipsq', then
6203 * dequeue the next message for the operation (from ipx_mphead), or
6204 * return NULL if there are no queued messages for the operation.
6205 * These messages are queued via CUR_OP to qwriter_ip() and friends.
6207 * 2. If the current operation on `ipsq' has completed (ipx_current_ipif is
6208 * not set) see if the ipsq has requested an xop switch. If so, switch
6209 * `ipsq' to a different xop. Xop switches only happen when joining or
6210 * leaving IPMP groups and require a careful dance -- see the comments
6211 * in-line below for details. If we're leaving a group xop or if we're
6212 * joining a group xop and become writer on it, then we proceed to (3).
6213 * Otherwise, we return NULL and exit the xop.
6215 * 3. For each IPSQ in the xop, return any switch operation stored on
6216 * ipsq_switch_mp (set via SWITCH_OP); these must be processed before
6217 * any other messages queued on the IPSQ. Otherwise, dequeue the next
6218 * exclusive operation (queued via NEW_OP) stored on ipsq_xopq_mphead.
6219 * Note that if the phyint tied to `ipsq' is not using IPMP there will
6220 * only be one IPSQ in the xop. Otherwise, there will be one IPSQ for
6221 * each phyint in the group, including the IPMP meta-interface phyint.
6223 static mblk_t *
6224 ipsq_dq(ipsq_t *ipsq)
6226 ill_t *illv4, *illv6;
6227 mblk_t *mp;
6228 ipsq_t *xopipsq;
6229 ipsq_t *leftipsq = NULL;
6230 ipxop_t *ipx;
6231 phyint_t *phyi = ipsq->ipsq_phyint;
6232 ip_stack_t *ipst = ipsq->ipsq_ipst;
6233 boolean_t emptied = B_FALSE;
6236 * Grab all the locks we need in the defined order (ill_g_lock ->
6237 * ipsq_lock -> ipx_lock); ill_g_lock is needed to use ipsq_next.
6239 rw_enter(&ipst->ips_ill_g_lock,
6240 ipsq->ipsq_swxop != NULL ? RW_WRITER : RW_READER);
6241 mutex_enter(&ipsq->ipsq_lock);
6242 ipx = ipsq->ipsq_xop;
6243 mutex_enter(&ipx->ipx_lock);
6246 * Dequeue the next message associated with the current exclusive
6247 * operation, if any.
6249 if ((mp = ipx->ipx_mphead) != NULL) {
6250 ipx->ipx_mphead = mp->b_next;
6251 if (ipx->ipx_mphead == NULL)
6252 ipx->ipx_mptail = NULL;
6253 mp->b_next = (void *)ipsq;
6254 goto out;
6257 if (ipx->ipx_current_ipif != NULL)
6258 goto empty;
6260 if (ipsq->ipsq_swxop != NULL) {
6262 * The exclusive operation that is now being completed has
6263 * requested a switch to a different xop. This happens
6264 * when an interface joins or leaves an IPMP group. Joins
6265 * happen through SIOCSLIFGROUPNAME (ip_sioctl_groupname()).
6266 * Leaves happen via SIOCSLIFGROUPNAME, interface unplumb
6267 * (phyint_free()), or interface plumb for an ill type
6268 * not in the IPMP group (ip_rput_dlpi_writer()).
6270 * Xop switches are not allowed on the IPMP meta-interface.
6272 ASSERT(phyi == NULL || !(phyi->phyint_flags & PHYI_IPMP));
6273 ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
6274 DTRACE_PROBE1(ipsq__switch, (ipsq_t *), ipsq);
6276 if (ipsq->ipsq_swxop == &ipsq->ipsq_ownxop) {
6278 * We're switching back to our own xop, so we have two
6279 * xop's to drain/exit: our own, and the group xop
6280 * that we are leaving.
6282 * First, pull ourselves out of the group ipsq list.
6283 * This is safe since we're writer on ill_g_lock.
6285 ASSERT(ipsq->ipsq_xop != &ipsq->ipsq_ownxop);
6287 xopipsq = ipx->ipx_ipsq;
6288 while (xopipsq->ipsq_next != ipsq)
6289 xopipsq = xopipsq->ipsq_next;
6291 xopipsq->ipsq_next = ipsq->ipsq_next;
6292 ipsq->ipsq_next = ipsq;
6293 ipsq->ipsq_xop = ipsq->ipsq_swxop;
6294 ipsq->ipsq_swxop = NULL;
6297 * Second, prepare to exit the group xop. The actual
6298 * ipsq_exit() is done at the end of this function
6299 * since we cannot hold any locks across ipsq_exit().
6300 * Note that although we drop the group's ipx_lock, no
6301 * threads can proceed since we're still ipx_writer.
6303 leftipsq = xopipsq;
6304 mutex_exit(&ipx->ipx_lock);
6307 * Third, set ipx to point to our own xop (which was
6308 * inactive and therefore can be entered).
6310 ipx = ipsq->ipsq_xop;
6311 mutex_enter(&ipx->ipx_lock);
6312 ASSERT(ipx->ipx_writer == NULL);
6313 ASSERT(ipx->ipx_current_ipif == NULL);
6314 } else {
6316 * We're switching from our own xop to a group xop.
6317 * The requestor of the switch must ensure that the
6318 * group xop cannot go away (e.g. by ensuring the
6319 * phyint associated with the xop cannot go away).
6321 * If we can become writer on our new xop, then we'll
6322 * do the drain. Otherwise, the current writer of our
6323 * new xop will do the drain when it exits.
6325 * First, splice ourselves into the group IPSQ list.
6326 * This is safe since we're writer on ill_g_lock.
6328 ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6330 xopipsq = ipsq->ipsq_swxop->ipx_ipsq;
6331 while (xopipsq->ipsq_next != ipsq->ipsq_swxop->ipx_ipsq)
6332 xopipsq = xopipsq->ipsq_next;
6334 xopipsq->ipsq_next = ipsq;
6335 ipsq->ipsq_next = ipsq->ipsq_swxop->ipx_ipsq;
6336 ipsq->ipsq_xop = ipsq->ipsq_swxop;
6337 ipsq->ipsq_swxop = NULL;
6340 * Second, exit our own xop, since it's now unused.
6341 * This is safe since we've got the only reference.
6343 ASSERT(ipx->ipx_writer == curthread);
6344 ipx->ipx_writer = NULL;
6345 VERIFY(--ipx->ipx_reentry_cnt == 0);
6346 ipx->ipx_ipsq_queued = B_FALSE;
6347 mutex_exit(&ipx->ipx_lock);
6350 * Third, set ipx to point to our new xop, and check
6351 * if we can become writer on it. If we cannot, then
6352 * the current writer will drain the IPSQ group when
6353 * it exits. Our ipsq_xop is guaranteed to be stable
6354 * because we're still holding ipsq_lock.
6356 ipx = ipsq->ipsq_xop;
6357 mutex_enter(&ipx->ipx_lock);
6358 if (ipx->ipx_writer != NULL ||
6359 ipx->ipx_current_ipif != NULL) {
6360 goto out;
6365 * Fourth, become writer on our new ipx before we continue
6366 * with the drain. Note that we never dropped ipsq_lock
6367 * above, so no other thread could've raced with us to
6368 * become writer first. Also, we're holding ipx_lock, so
6369 * no other thread can examine the ipx right now.
6371 ASSERT(ipx->ipx_current_ipif == NULL);
6372 ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6373 VERIFY(ipx->ipx_reentry_cnt++ == 0);
6374 ipx->ipx_writer = curthread;
6375 ipx->ipx_forced = B_FALSE;
6376 #ifdef DEBUG
6377 ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6378 #endif
6381 xopipsq = ipsq;
6382 do {
6384 * So that other operations operate on a consistent and
6385 * complete phyint, a switch message on an IPSQ must be
6386 * handled prior to any other operations on that IPSQ.
6388 if ((mp = xopipsq->ipsq_switch_mp) != NULL) {
6389 xopipsq->ipsq_switch_mp = NULL;
6390 ASSERT(mp->b_next == NULL);
6391 mp->b_next = (void *)xopipsq;
6392 goto out;
6395 if ((mp = xopipsq->ipsq_xopq_mphead) != NULL) {
6396 xopipsq->ipsq_xopq_mphead = mp->b_next;
6397 if (xopipsq->ipsq_xopq_mphead == NULL)
6398 xopipsq->ipsq_xopq_mptail = NULL;
6399 mp->b_next = (void *)xopipsq;
6400 goto out;
6402 } while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6403 empty:
6405 * There are no messages. Further, we are holding ipx_lock, hence no
6406 * new messages can end up on any IPSQ in the xop.
6408 ipx->ipx_writer = NULL;
6409 ipx->ipx_forced = B_FALSE;
6410 VERIFY(--ipx->ipx_reentry_cnt == 0);
6411 ipx->ipx_ipsq_queued = B_FALSE;
6412 emptied = B_TRUE;
6413 #ifdef DEBUG
6414 ipx->ipx_depth = 0;
6415 #endif
6416 out:
6417 mutex_exit(&ipx->ipx_lock);
6418 mutex_exit(&ipsq->ipsq_lock);
6421 * If we completely emptied the xop, then wake up any threads waiting
6422 * to enter any of the IPSQ's associated with it.
6424 if (emptied) {
6425 xopipsq = ipsq;
6426 do {
6427 if ((phyi = xopipsq->ipsq_phyint) == NULL)
6428 continue;
6430 illv4 = phyi->phyint_illv4;
6431 illv6 = phyi->phyint_illv6;
6433 GRAB_ILL_LOCKS(illv4, illv6);
6434 if (illv4 != NULL)
6435 cv_broadcast(&illv4->ill_cv);
6436 if (illv6 != NULL)
6437 cv_broadcast(&illv6->ill_cv);
6438 RELEASE_ILL_LOCKS(illv4, illv6);
6439 } while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6441 rw_exit(&ipst->ips_ill_g_lock);
6444 * Now that all locks are dropped, exit the IPSQ we left.
6446 if (leftipsq != NULL)
6447 ipsq_exit(leftipsq);
6449 return (mp);
6453 * Return completion status of previously initiated DLPI operations on
6454 * ills in the purview of an ipsq.
6456 static boolean_t
6457 ipsq_dlpi_done(ipsq_t *ipsq)
6459 ipsq_t *ipsq_start;
6460 phyint_t *phyi;
6461 ill_t *ill;
6463 ASSERT(RW_LOCK_HELD(&ipsq->ipsq_ipst->ips_ill_g_lock));
6464 ipsq_start = ipsq;
6466 do {
6468 * The only current users of this function are ipsq_try_enter
6469 * and ipsq_enter which have made sure that ipsq_writer is
6470 * NULL before we reach here. ill_dlpi_pending is modified
6471 * only by an ipsq writer
6473 ASSERT(ipsq->ipsq_xop->ipx_writer == NULL);
6474 phyi = ipsq->ipsq_phyint;
6476 * phyi could be NULL if a phyint that is part of an
6477 * IPMP group is being unplumbed. A more detailed
6478 * comment is in ipmp_grp_update_kstats()
6480 if (phyi != NULL) {
6481 ill = phyi->phyint_illv4;
6482 if (ill != NULL &&
6483 (ill->ill_dlpi_pending != DL_PRIM_INVAL ||
6484 ill->ill_arl_dlpi_pending))
6485 return (B_FALSE);
6487 ill = phyi->phyint_illv6;
6488 if (ill != NULL &&
6489 ill->ill_dlpi_pending != DL_PRIM_INVAL)
6490 return (B_FALSE);
6493 } while ((ipsq = ipsq->ipsq_next) != ipsq_start);
6495 return (B_TRUE);
6499 * Enter the ipsq corresponding to ill, by waiting synchronously till
6500 * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
6501 * will have to drain completely before ipsq_enter returns success.
6502 * ipx_current_ipif will be set if some exclusive op is in progress,
6503 * and the ipsq_exit logic will start the next enqueued op after
6504 * completion of the current op. If 'force' is used, we don't wait
6505 * for the enqueued ops. This is needed when a conn_close wants to
6506 * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
6507 * of an ill can also use this option. But we dont' use it currently.
6509 #define ENTER_SQ_WAIT_TICKS 100
6510 boolean_t
6511 ipsq_enter(ill_t *ill, boolean_t force, int type)
6513 ipsq_t *ipsq;
6514 ipxop_t *ipx;
6515 boolean_t waited_enough = B_FALSE;
6516 ip_stack_t *ipst = ill->ill_ipst;
6519 * Note that the relationship between ill and ipsq is fixed as long as
6520 * the ill is not ILL_CONDEMNED. Holding ipsq_lock ensures the
6521 * relationship between the IPSQ and xop cannot change. However,
6522 * since we cannot hold ipsq_lock across the cv_wait(), it may change
6523 * while we're waiting. We wait on ill_cv and rely on ipsq_exit()
6524 * waking up all ills in the xop when it becomes available.
6526 for (;;) {
6527 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6528 mutex_enter(&ill->ill_lock);
6529 if (ill->ill_state_flags & ILL_CONDEMNED) {
6530 mutex_exit(&ill->ill_lock);
6531 rw_exit(&ipst->ips_ill_g_lock);
6532 return (B_FALSE);
6535 ipsq = ill->ill_phyint->phyint_ipsq;
6536 mutex_enter(&ipsq->ipsq_lock);
6537 ipx = ipsq->ipsq_xop;
6538 mutex_enter(&ipx->ipx_lock);
6540 if (ipx->ipx_writer == NULL && (type == CUR_OP ||
6541 (ipx->ipx_current_ipif == NULL && ipsq_dlpi_done(ipsq)) ||
6542 waited_enough))
6543 break;
6545 rw_exit(&ipst->ips_ill_g_lock);
6547 if (!force || ipx->ipx_writer != NULL) {
6548 mutex_exit(&ipx->ipx_lock);
6549 mutex_exit(&ipsq->ipsq_lock);
6550 cv_wait(&ill->ill_cv, &ill->ill_lock);
6551 } else {
6552 mutex_exit(&ipx->ipx_lock);
6553 mutex_exit(&ipsq->ipsq_lock);
6554 (void) cv_reltimedwait(&ill->ill_cv,
6555 &ill->ill_lock, ENTER_SQ_WAIT_TICKS, TR_CLOCK_TICK);
6556 waited_enough = B_TRUE;
6558 mutex_exit(&ill->ill_lock);
6561 ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6562 ASSERT(ipx->ipx_reentry_cnt == 0);
6563 ipx->ipx_writer = curthread;
6564 ipx->ipx_forced = (ipx->ipx_current_ipif != NULL);
6565 ipx->ipx_reentry_cnt++;
6566 #ifdef DEBUG
6567 ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6568 #endif
6569 mutex_exit(&ipx->ipx_lock);
6570 mutex_exit(&ipsq->ipsq_lock);
6571 mutex_exit(&ill->ill_lock);
6572 rw_exit(&ipst->ips_ill_g_lock);
6574 return (B_TRUE);
6578 * ipif_set_values() has a constraint that it cannot drop the ips_ill_g_lock
6579 * across the call to the core interface ipsq_try_enter() and hence calls this
6580 * function directly. This is explained more fully in ipif_set_values().
6581 * In order to support the above constraint, ipsq_try_enter is implemented as
6582 * a wrapper that grabs the ips_ill_g_lock and calls this function subsequently
6584 static ipsq_t *
6585 ipsq_try_enter_internal(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func,
6586 int type, boolean_t reentry_ok)
6588 ipsq_t *ipsq;
6589 ipxop_t *ipx;
6590 ip_stack_t *ipst = ill->ill_ipst;
6593 * lock ordering:
6594 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock -> ipx_lock.
6596 * ipx of an ipsq can't change when ipsq_lock is held.
6598 ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
6599 GRAB_CONN_LOCK(q);
6600 mutex_enter(&ill->ill_lock);
6601 ipsq = ill->ill_phyint->phyint_ipsq;
6602 mutex_enter(&ipsq->ipsq_lock);
6603 ipx = ipsq->ipsq_xop;
6604 mutex_enter(&ipx->ipx_lock);
6607 * 1. Enter the ipsq if we are already writer and reentry is ok.
6608 * (Note: If the caller does not specify reentry_ok then neither
6609 * 'func' nor any of its callees must ever attempt to enter the ipsq
6610 * again. Otherwise it can lead to an infinite loop
6611 * 2. Enter the ipsq if there is no current writer and this attempted
6612 * entry is part of the current operation
6613 * 3. Enter the ipsq if there is no current writer and this is a new
6614 * operation and the operation queue is empty and there is no
6615 * operation currently in progress and if all previously initiated
6616 * DLPI operations have completed.
6618 if ((ipx->ipx_writer == curthread && reentry_ok) ||
6619 (ipx->ipx_writer == NULL && (type == CUR_OP || (type == NEW_OP &&
6620 !ipx->ipx_ipsq_queued && ipx->ipx_current_ipif == NULL &&
6621 ipsq_dlpi_done(ipsq))))) {
6622 /* Success. */
6623 ipx->ipx_reentry_cnt++;
6624 ipx->ipx_writer = curthread;
6625 ipx->ipx_forced = B_FALSE;
6626 mutex_exit(&ipx->ipx_lock);
6627 mutex_exit(&ipsq->ipsq_lock);
6628 mutex_exit(&ill->ill_lock);
6629 RELEASE_CONN_LOCK(q);
6630 #ifdef DEBUG
6631 ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6632 #endif
6633 return (ipsq);
6636 if (func != NULL)
6637 ipsq_enq(ipsq, q, mp, func, type, ill);
6639 mutex_exit(&ipx->ipx_lock);
6640 mutex_exit(&ipsq->ipsq_lock);
6641 mutex_exit(&ill->ill_lock);
6642 RELEASE_CONN_LOCK(q);
6643 return (NULL);
6647 * The ipsq_t (ipsq) is the synchronization data structure used to serialize
6648 * certain critical operations like plumbing (i.e. most set ioctls), etc.
6649 * There is one ipsq per phyint. The ipsq
6650 * serializes exclusive ioctls issued by applications on a per ipsq basis in
6651 * ipsq_xopq_mphead. It also protects against multiple threads executing in
6652 * the ipsq. Responses from the driver pertain to the current ioctl (say a
6653 * DL_BIND_ACK in response to a DL_BIND_REQ initiated as part of bringing
6654 * up the interface) and are enqueued in ipx_mphead.
6656 * If a thread does not want to reenter the ipsq when it is already writer,
6657 * it must make sure that the specified reentry point to be called later
6658 * when the ipsq is empty, nor any code path starting from the specified reentry
6659 * point must never ever try to enter the ipsq again. Otherwise it can lead
6660 * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
6661 * When the thread that is currently exclusive finishes, it (ipsq_exit)
6662 * dequeues the requests waiting to become exclusive in ipx_mphead and calls
6663 * the reentry point. When the list at ipx_mphead becomes empty ipsq_exit
6664 * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
6665 * ioctl if the current ioctl has completed. If the current ioctl is still
6666 * in progress it simply returns. The current ioctl could be waiting for
6667 * a response from another module (the driver or could be waiting for
6668 * the ipif/ill/ire refcnts to drop to zero. In such a case the ipx_pending_mp
6669 * and ipx_pending_ipif are set. ipx_current_ipif is set throughout the
6670 * execution of the ioctl and ipsq_exit does not start the next ioctl unless
6671 * ipx_current_ipif is NULL which happens only once the ioctl is complete and
6672 * all associated DLPI operations have completed.
6676 * Try to enter the IPSQ corresponding to `ipif' or `ill' exclusively (`ipif'
6677 * and `ill' cannot both be specified). Returns a pointer to the entered IPSQ
6678 * on success, or NULL on failure. The caller ensures ipif/ill is valid by
6679 * refholding it as necessary. If the IPSQ cannot be entered and `func' is
6680 * non-NULL, then `func' will be called back with `q' and `mp' once the IPSQ
6681 * can be entered. If `func' is NULL, then `q' and `mp' are ignored.
6683 ipsq_t *
6684 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
6685 ipsq_func_t func, int type, boolean_t reentry_ok)
6687 ip_stack_t *ipst;
6688 ipsq_t *ipsq;
6690 /* Only 1 of ipif or ill can be specified */
6691 ASSERT((ipif != NULL) ^ (ill != NULL));
6693 if (ipif != NULL)
6694 ill = ipif->ipif_ill;
6695 ipst = ill->ill_ipst;
6697 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6698 ipsq = ipsq_try_enter_internal(ill, q, mp, func, type, reentry_ok);
6699 rw_exit(&ipst->ips_ill_g_lock);
6701 return (ipsq);
6705 * Try to enter the IPSQ corresponding to `ill' as writer. The caller ensures
6706 * ill is valid by refholding it if necessary; we will refrele. If the IPSQ
6707 * cannot be entered, the mp is queued for completion.
6709 void
6710 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6711 boolean_t reentry_ok)
6713 ipsq_t *ipsq;
6715 ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
6718 * Drop the caller's refhold on the ill. This is safe since we either
6719 * entered the IPSQ (and thus are exclusive), or failed to enter the
6720 * IPSQ, in which case we return without accessing ill anymore. This
6721 * is needed because func needs to see the correct refcount.
6722 * e.g. removeif can work only then.
6724 ill_refrele(ill);
6725 if (ipsq != NULL) {
6726 (*func)(ipsq, q, mp, NULL);
6727 ipsq_exit(ipsq);
6732 * Exit the specified IPSQ. If this is the final exit on it then drain it
6733 * prior to exiting. Caller must be writer on the specified IPSQ.
6735 void
6736 ipsq_exit(ipsq_t *ipsq)
6738 mblk_t *mp;
6739 ipsq_t *mp_ipsq;
6740 queue_t *q;
6741 phyint_t *phyi;
6742 ipsq_func_t func;
6744 ASSERT(IAM_WRITER_IPSQ(ipsq));
6746 ASSERT(ipsq->ipsq_xop->ipx_reentry_cnt >= 1);
6747 if (ipsq->ipsq_xop->ipx_reentry_cnt != 1) {
6748 ipsq->ipsq_xop->ipx_reentry_cnt--;
6749 return;
6752 for (;;) {
6753 phyi = ipsq->ipsq_phyint;
6754 mp = ipsq_dq(ipsq);
6755 mp_ipsq = (mp == NULL) ? NULL : (ipsq_t *)mp->b_next;
6758 * If we've changed to a new IPSQ, and the phyint associated
6759 * with the old one has gone away, free the old IPSQ. Note
6760 * that this cannot happen while the IPSQ is in a group.
6762 if (mp_ipsq != ipsq && phyi == NULL) {
6763 ASSERT(ipsq->ipsq_next == ipsq);
6764 ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6765 ipsq_delete(ipsq);
6768 if (mp == NULL)
6769 break;
6771 q = mp->b_queue;
6772 func = (ipsq_func_t)mp->b_prev;
6773 ipsq = mp_ipsq;
6774 mp->b_next = mp->b_prev = NULL;
6775 mp->b_queue = NULL;
6778 * If 'q' is an conn queue, it is valid, since we did a
6779 * a refhold on the conn at the start of the ioctl.
6780 * If 'q' is an ill queue, it is valid, since close of an
6781 * ill will clean up its IPSQ.
6783 (*func)(ipsq, q, mp, NULL);
6788 * Used to start any igmp or mld timers that could not be started
6789 * while holding ill_mcast_lock. The timers can't be started while holding
6790 * the lock, since mld/igmp_start_timers may need to call untimeout()
6791 * which can't be done while holding the lock which the timeout handler
6792 * acquires. Otherwise
6793 * there could be a deadlock since the timeout handlers
6794 * mld_timeout_handler_per_ill/igmp_timeout_handler_per_ill also acquire
6795 * ill_mcast_lock.
6797 void
6798 ill_mcast_timer_start(ip_stack_t *ipst)
6800 int next;
6802 mutex_enter(&ipst->ips_igmp_timer_lock);
6803 next = ipst->ips_igmp_deferred_next;
6804 ipst->ips_igmp_deferred_next = INFINITY;
6805 mutex_exit(&ipst->ips_igmp_timer_lock);
6807 if (next != INFINITY)
6808 igmp_start_timers(next, ipst);
6810 mutex_enter(&ipst->ips_mld_timer_lock);
6811 next = ipst->ips_mld_deferred_next;
6812 ipst->ips_mld_deferred_next = INFINITY;
6813 mutex_exit(&ipst->ips_mld_timer_lock);
6815 if (next != INFINITY)
6816 mld_start_timers(next, ipst);
6820 * Start the current exclusive operation on `ipsq'; associate it with `ipif'
6821 * and `ioccmd'.
6823 void
6824 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
6826 ill_t *ill = ipif->ipif_ill;
6827 ipxop_t *ipx = ipsq->ipsq_xop;
6829 ASSERT(IAM_WRITER_IPSQ(ipsq));
6830 ASSERT(ipx->ipx_current_ipif == NULL);
6831 ASSERT(ipx->ipx_current_ioctl == 0);
6833 ipx->ipx_current_done = B_FALSE;
6834 ipx->ipx_current_ioctl = ioccmd;
6835 mutex_enter(&ipx->ipx_lock);
6836 ipx->ipx_current_ipif = ipif;
6837 mutex_exit(&ipx->ipx_lock);
6840 * Set IPIF_CHANGING on one or more ipifs associated with the
6841 * current exclusive operation. IPIF_CHANGING prevents any new
6842 * references to the ipif (so that the references will eventually
6843 * drop to zero) and also prevents any "get" operations (e.g.,
6844 * SIOCGLIFFLAGS) from being able to access the ipif until the
6845 * operation has completed and the ipif is again in a stable state.
6847 * For ioctls, IPIF_CHANGING is set on the ipif associated with the
6848 * ioctl. For internal operations (where ioccmd is zero), all ipifs
6849 * on the ill are marked with IPIF_CHANGING since it's unclear which
6850 * ipifs will be affected.
6852 * Note that SIOCLIFREMOVEIF is a special case as it sets
6853 * IPIF_CONDEMNED internally after identifying the right ipif to
6854 * operate on.
6856 switch (ioccmd) {
6857 case SIOCLIFREMOVEIF:
6858 break;
6859 case 0:
6860 mutex_enter(&ill->ill_lock);
6861 ipif = ipif->ipif_ill->ill_ipif;
6862 for (; ipif != NULL; ipif = ipif->ipif_next)
6863 ipif->ipif_state_flags |= IPIF_CHANGING;
6864 mutex_exit(&ill->ill_lock);
6865 break;
6866 default:
6867 mutex_enter(&ill->ill_lock);
6868 ipif->ipif_state_flags |= IPIF_CHANGING;
6869 mutex_exit(&ill->ill_lock);
6874 * Finish the current exclusive operation on `ipsq'. Usually, this will allow
6875 * the next exclusive operation to begin once we ipsq_exit(). However, if
6876 * pending DLPI operations remain, then we will wait for the queue to drain
6877 * before allowing the next exclusive operation to begin. This ensures that
6878 * DLPI operations from one exclusive operation are never improperly processed
6879 * as part of a subsequent exclusive operation.
6881 void
6882 ipsq_current_finish(ipsq_t *ipsq)
6884 ipxop_t *ipx = ipsq->ipsq_xop;
6885 t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
6886 ipif_t *ipif = ipx->ipx_current_ipif;
6888 ASSERT(IAM_WRITER_IPSQ(ipsq));
6891 * For SIOCLIFREMOVEIF, the ipif has been already been blown away
6892 * (but in that case, IPIF_CHANGING will already be clear and no
6893 * pending DLPI messages can remain).
6895 if (ipx->ipx_current_ioctl != SIOCLIFREMOVEIF) {
6896 ill_t *ill = ipif->ipif_ill;
6898 mutex_enter(&ill->ill_lock);
6899 dlpi_pending = ill->ill_dlpi_pending;
6900 if (ipx->ipx_current_ioctl == 0) {
6901 ipif = ill->ill_ipif;
6902 for (; ipif != NULL; ipif = ipif->ipif_next)
6903 ipif->ipif_state_flags &= ~IPIF_CHANGING;
6904 } else {
6905 ipif->ipif_state_flags &= ~IPIF_CHANGING;
6907 mutex_exit(&ill->ill_lock);
6910 ASSERT(!ipx->ipx_current_done);
6911 ipx->ipx_current_done = B_TRUE;
6912 ipx->ipx_current_ioctl = 0;
6913 if (dlpi_pending == DL_PRIM_INVAL) {
6914 mutex_enter(&ipx->ipx_lock);
6915 ipx->ipx_current_ipif = NULL;
6916 mutex_exit(&ipx->ipx_lock);
6921 * The ill is closing. Flush all messages on the ipsq that originated
6922 * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
6923 * for this ill since ipsq_enter could not have entered until then.
6924 * New messages can't be queued since the CONDEMNED flag is set.
6926 static void
6927 ipsq_flush(ill_t *ill)
6929 queue_t *q;
6930 mblk_t *prev;
6931 mblk_t *mp;
6932 mblk_t *mp_next;
6933 ipxop_t *ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
6935 ASSERT(IAM_WRITER_ILL(ill));
6938 * Flush any messages sent up by the driver.
6940 mutex_enter(&ipx->ipx_lock);
6941 for (prev = NULL, mp = ipx->ipx_mphead; mp != NULL; mp = mp_next) {
6942 mp_next = mp->b_next;
6943 q = mp->b_queue;
6944 if (q == ill->ill_rq || q == ill->ill_wq) {
6945 /* dequeue mp */
6946 if (prev == NULL)
6947 ipx->ipx_mphead = mp->b_next;
6948 else
6949 prev->b_next = mp->b_next;
6950 if (ipx->ipx_mptail == mp) {
6951 ASSERT(mp_next == NULL);
6952 ipx->ipx_mptail = prev;
6954 inet_freemsg(mp);
6955 } else {
6956 prev = mp;
6959 mutex_exit(&ipx->ipx_lock);
6960 (void) ipsq_pending_mp_cleanup(ill, NULL);
6961 ipsq_xopq_mp_cleanup(ill, NULL);
6965 * Parse an ifreq or lifreq struct coming down ioctls and refhold
6966 * and return the associated ipif.
6967 * Return value:
6968 * Non zero: An error has occurred. ci may not be filled out.
6969 * zero : ci is filled out with the ioctl cmd in ci.ci_name, and
6970 * a held ipif in ci.ci_ipif.
6973 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
6974 cmd_info_t *ci)
6976 char *name;
6977 struct ifreq *ifr;
6978 struct lifreq *lifr;
6979 ipif_t *ipif = NULL;
6980 ill_t *ill;
6981 conn_t *connp;
6982 boolean_t isv6;
6983 int err;
6984 mblk_t *mp1;
6985 zoneid_t zoneid;
6986 ip_stack_t *ipst;
6988 if (q->q_next != NULL) {
6989 ill = (ill_t *)q->q_ptr;
6990 isv6 = ill->ill_isv6;
6991 connp = NULL;
6992 zoneid = ALL_ZONES;
6993 ipst = ill->ill_ipst;
6994 } else {
6995 ill = NULL;
6996 connp = Q_TO_CONN(q);
6997 isv6 = (connp->conn_family == AF_INET6);
6998 zoneid = connp->conn_zoneid;
6999 if (zoneid == GLOBAL_ZONEID) {
7000 /* global zone can access ipifs in all zones */
7001 zoneid = ALL_ZONES;
7003 ipst = connp->conn_netstack->netstack_ip;
7006 /* Has been checked in ip_wput_nondata */
7007 mp1 = mp->b_cont->b_cont;
7009 if (ipip->ipi_cmd_type == IF_CMD) {
7010 /* This a old style SIOC[GS]IF* command */
7011 ifr = (struct ifreq *)mp1->b_rptr;
7013 * Null terminate the string to protect against buffer
7014 * overrun. String was generated by user code and may not
7015 * be trusted.
7017 ifr->ifr_name[IFNAMSIZ - 1] = '\0';
7018 name = ifr->ifr_name;
7019 ci->ci_sin = (sin_t *)&ifr->ifr_addr;
7020 ci->ci_sin6 = NULL;
7021 ci->ci_lifr = (struct lifreq *)ifr;
7022 } else {
7023 /* This a new style SIOC[GS]LIF* command */
7024 ASSERT(ipip->ipi_cmd_type == LIF_CMD);
7025 lifr = (struct lifreq *)mp1->b_rptr;
7027 * Null terminate the string to protect against buffer
7028 * overrun. String was generated by user code and may not
7029 * be trusted.
7031 lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
7032 name = lifr->lifr_name;
7033 ci->ci_sin = (sin_t *)&lifr->lifr_addr;
7034 ci->ci_sin6 = (sin6_t *)&lifr->lifr_addr;
7035 ci->ci_lifr = lifr;
7038 if (ipip->ipi_cmd == SIOCSLIFNAME) {
7040 * The ioctl will be failed if the ioctl comes down
7041 * an conn stream
7043 if (ill == NULL) {
7045 * Not an ill queue, return EINVAL same as the
7046 * old error code.
7048 return (ENXIO);
7050 ipif = ill->ill_ipif;
7051 ipif_refhold(ipif);
7052 } else {
7054 * Ensure that ioctls don't see any internal state changes
7055 * caused by set ioctls by deferring them if IPIF_CHANGING is
7056 * set.
7058 ipif = ipif_lookup_on_name_async(name, mi_strlen(name),
7059 isv6, zoneid, q, mp, ip_process_ioctl, &err, ipst);
7060 if (ipif == NULL) {
7061 if (err == EINPROGRESS)
7062 return (err);
7063 err = 0; /* Ensure we don't use it below */
7068 * Old style [GS]IFCMD does not admit IPv6 ipif
7070 if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
7071 ipif_refrele(ipif);
7072 return (ENXIO);
7075 if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
7076 name[0] == '\0') {
7078 * Handle a or a SIOC?IF* with a null name
7079 * during plumb (on the ill queue before the I_PLINK).
7081 ipif = ill->ill_ipif;
7082 ipif_refhold(ipif);
7085 if (ipif == NULL)
7086 return (ENXIO);
7088 DTRACE_PROBE4(ipif__ioctl, char *, "ip_extract_lifreq",
7089 int, ipip->ipi_cmd, ill_t *, ipif->ipif_ill, ipif_t *, ipif);
7091 ci->ci_ipif = ipif;
7092 return (0);
7096 * Return the total number of ipifs.
7098 static uint_t
7099 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
7101 uint_t numifs = 0;
7102 ill_t *ill;
7103 ill_walk_context_t ctx;
7104 ipif_t *ipif;
7106 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7107 ill = ILL_START_WALK_V4(&ctx, ipst);
7108 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7109 if (IS_UNDER_IPMP(ill))
7110 continue;
7111 for (ipif = ill->ill_ipif; ipif != NULL;
7112 ipif = ipif->ipif_next) {
7113 if (ipif->ipif_zoneid == zoneid ||
7114 ipif->ipif_zoneid == ALL_ZONES)
7115 numifs++;
7118 rw_exit(&ipst->ips_ill_g_lock);
7119 return (numifs);
7123 * Return the total number of ipifs.
7125 static uint_t
7126 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
7128 uint_t numifs = 0;
7129 ill_t *ill;
7130 ipif_t *ipif;
7131 ill_walk_context_t ctx;
7133 ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
7135 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7136 if (family == AF_INET)
7137 ill = ILL_START_WALK_V4(&ctx, ipst);
7138 else if (family == AF_INET6)
7139 ill = ILL_START_WALK_V6(&ctx, ipst);
7140 else
7141 ill = ILL_START_WALK_ALL(&ctx, ipst);
7143 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7144 if (IS_UNDER_IPMP(ill) && !(lifn_flags & LIFC_UNDER_IPMP))
7145 continue;
7147 for (ipif = ill->ill_ipif; ipif != NULL;
7148 ipif = ipif->ipif_next) {
7149 if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7150 !(lifn_flags & LIFC_NOXMIT))
7151 continue;
7152 if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7153 !(lifn_flags & LIFC_TEMPORARY))
7154 continue;
7155 if (((ipif->ipif_flags &
7156 (IPIF_NOXMIT|IPIF_NOLOCAL|
7157 IPIF_DEPRECATED)) ||
7158 IS_LOOPBACK(ill) ||
7159 !(ipif->ipif_flags & IPIF_UP)) &&
7160 (lifn_flags & LIFC_EXTERNAL_SOURCE))
7161 continue;
7163 if (zoneid != ipif->ipif_zoneid &&
7164 ipif->ipif_zoneid != ALL_ZONES &&
7165 (zoneid != GLOBAL_ZONEID ||
7166 !(lifn_flags & LIFC_ALLZONES)))
7167 continue;
7169 numifs++;
7172 rw_exit(&ipst->ips_ill_g_lock);
7173 return (numifs);
7176 uint_t
7177 ip_get_lifsrcofnum(ill_t *ill)
7179 uint_t numifs = 0;
7180 ill_t *ill_head = ill;
7181 ip_stack_t *ipst = ill->ill_ipst;
7184 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
7185 * other thread may be trying to relink the ILLs in this usesrc group
7186 * and adjusting the ill_usesrc_grp_next pointers
7188 rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7189 if ((ill->ill_usesrc_ifindex == 0) &&
7190 (ill->ill_usesrc_grp_next != NULL)) {
7191 for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
7192 ill = ill->ill_usesrc_grp_next)
7193 numifs++;
7195 rw_exit(&ipst->ips_ill_g_usesrc_lock);
7197 return (numifs);
7200 /* Null values are passed in for ipif, sin, and ifreq */
7201 /* ARGSUSED */
7203 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7204 mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7206 int *nump;
7207 conn_t *connp = Q_TO_CONN(q);
7209 ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7211 /* Existence of b_cont->b_cont checked in ip_wput_nondata */
7212 nump = (int *)mp->b_cont->b_cont->b_rptr;
7214 *nump = ip_get_numifs(connp->conn_zoneid,
7215 connp->conn_netstack->netstack_ip);
7216 ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
7217 return (0);
7220 /* Null values are passed in for ipif, sin, and ifreq */
7221 /* ARGSUSED */
7223 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
7224 queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7226 struct lifnum *lifn;
7227 mblk_t *mp1;
7228 conn_t *connp = Q_TO_CONN(q);
7230 ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7232 /* Existence checked in ip_wput_nondata */
7233 mp1 = mp->b_cont->b_cont;
7235 lifn = (struct lifnum *)mp1->b_rptr;
7236 switch (lifn->lifn_family) {
7237 case AF_UNSPEC:
7238 case AF_INET:
7239 case AF_INET6:
7240 break;
7241 default:
7242 return (EAFNOSUPPORT);
7245 lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
7246 connp->conn_zoneid, connp->conn_netstack->netstack_ip);
7247 ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
7248 return (0);
7251 /* ARGSUSED */
7253 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7254 mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7256 STRUCT_HANDLE(ifconf, ifc);
7257 mblk_t *mp1;
7258 struct iocblk *iocp;
7259 struct ifreq *ifr;
7260 ill_walk_context_t ctx;
7261 ill_t *ill;
7262 ipif_t *ipif;
7263 struct sockaddr_in *sin;
7264 int32_t ifclen;
7265 zoneid_t zoneid;
7266 ip_stack_t *ipst = CONNQ_TO_IPST(q);
7268 ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
7270 ip1dbg(("ip_sioctl_get_ifconf"));
7271 /* Existence verified in ip_wput_nondata */
7272 mp1 = mp->b_cont->b_cont;
7273 iocp = (struct iocblk *)mp->b_rptr;
7274 zoneid = Q_TO_CONN(q)->conn_zoneid;
7277 * The original SIOCGIFCONF passed in a struct ifconf which specified
7278 * the user buffer address and length into which the list of struct
7279 * ifreqs was to be copied. Since AT&T Streams does not seem to
7280 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
7281 * the SIOCGIFCONF operation was redefined to simply provide
7282 * a large output buffer into which we are supposed to jam the ifreq
7283 * array. The same ioctl command code was used, despite the fact that
7284 * both the applications and the kernel code had to change, thus making
7285 * it impossible to support both interfaces.
7287 * For reasons not good enough to try to explain, the following
7288 * algorithm is used for deciding what to do with one of these:
7289 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
7290 * form with the output buffer coming down as the continuation message.
7291 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
7292 * and we have to copy in the ifconf structure to find out how big the
7293 * output buffer is and where to copy out to. Sure no problem...
7296 STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
7297 if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
7298 int numifs = 0;
7299 size_t ifc_bufsize;
7302 * Must be (better be!) continuation of a TRANSPARENT
7303 * IOCTL. We just copied in the ifconf structure.
7305 STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
7306 (struct ifconf *)mp1->b_rptr);
7309 * Allocate a buffer to hold requested information.
7311 * If ifc_len is larger than what is needed, we only
7312 * allocate what we will use.
7314 * If ifc_len is smaller than what is needed, return
7315 * EINVAL.
7317 * XXX: the ill_t structure can hava 2 counters, for
7318 * v4 and v6 (not just ill_ipif_up_count) to store the
7319 * number of interfaces for a device, so we don't need
7320 * to count them here...
7322 numifs = ip_get_numifs(zoneid, ipst);
7324 ifclen = STRUCT_FGET(ifc, ifc_len);
7325 ifc_bufsize = numifs * sizeof (struct ifreq);
7326 if (ifc_bufsize > ifclen) {
7327 if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7328 /* old behaviour */
7329 return (EINVAL);
7330 } else {
7331 ifc_bufsize = ifclen;
7335 mp1 = mi_copyout_alloc(q, mp,
7336 STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
7337 if (mp1 == NULL)
7338 return (ENOMEM);
7340 mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
7342 bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7344 * the SIOCGIFCONF ioctl only knows about
7345 * IPv4 addresses, so don't try to tell
7346 * it about interfaces with IPv6-only
7347 * addresses. (Last parm 'isv6' is B_FALSE)
7350 ifr = (struct ifreq *)mp1->b_rptr;
7352 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7353 ill = ILL_START_WALK_V4(&ctx, ipst);
7354 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7355 if (IS_UNDER_IPMP(ill))
7356 continue;
7357 for (ipif = ill->ill_ipif; ipif != NULL;
7358 ipif = ipif->ipif_next) {
7359 if (zoneid != ipif->ipif_zoneid &&
7360 ipif->ipif_zoneid != ALL_ZONES)
7361 continue;
7362 if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
7363 if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7364 /* old behaviour */
7365 rw_exit(&ipst->ips_ill_g_lock);
7366 return (EINVAL);
7367 } else {
7368 goto if_copydone;
7371 ipif_get_name(ipif, ifr->ifr_name,
7372 sizeof (ifr->ifr_name));
7373 sin = (sin_t *)&ifr->ifr_addr;
7374 *sin = sin_null;
7375 sin->sin_family = AF_INET;
7376 sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7377 ifr++;
7380 if_copydone:
7381 rw_exit(&ipst->ips_ill_g_lock);
7382 mp1->b_wptr = (uchar_t *)ifr;
7384 if (STRUCT_BUF(ifc) != NULL) {
7385 STRUCT_FSET(ifc, ifc_len,
7386 (int)((uchar_t *)ifr - mp1->b_rptr));
7388 return (0);
7392 * Get the interfaces using the address hosted on the interface passed in,
7393 * as a source adddress
7395 /* ARGSUSED */
7397 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7398 mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7400 mblk_t *mp1;
7401 ill_t *ill, *ill_head;
7402 ipif_t *ipif, *orig_ipif;
7403 int numlifs = 0;
7404 size_t lifs_bufsize, lifsmaxlen;
7405 struct lifreq *lifr;
7406 struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7407 uint_t ifindex;
7408 zoneid_t zoneid;
7409 boolean_t isv6 = B_FALSE;
7410 struct sockaddr_in *sin;
7411 struct sockaddr_in6 *sin6;
7412 STRUCT_HANDLE(lifsrcof, lifs);
7413 ip_stack_t *ipst;
7415 ipst = CONNQ_TO_IPST(q);
7417 ASSERT(q->q_next == NULL);
7419 zoneid = Q_TO_CONN(q)->conn_zoneid;
7421 /* Existence verified in ip_wput_nondata */
7422 mp1 = mp->b_cont->b_cont;
7425 * Must be (better be!) continuation of a TRANSPARENT
7426 * IOCTL. We just copied in the lifsrcof structure.
7428 STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
7429 (struct lifsrcof *)mp1->b_rptr);
7431 if (MBLKL(mp1) != STRUCT_SIZE(lifs))
7432 return (EINVAL);
7434 ifindex = STRUCT_FGET(lifs, lifs_ifindex);
7435 isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
7436 ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, ipst);
7437 if (ipif == NULL) {
7438 ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
7439 ifindex));
7440 return (ENXIO);
7443 /* Allocate a buffer to hold requested information */
7444 numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
7445 lifs_bufsize = numlifs * sizeof (struct lifreq);
7446 lifsmaxlen = STRUCT_FGET(lifs, lifs_maxlen);
7447 /* The actual size needed is always returned in lifs_len */
7448 STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
7450 /* If the amount we need is more than what is passed in, abort */
7451 if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
7452 ipif_refrele(ipif);
7453 return (0);
7456 mp1 = mi_copyout_alloc(q, mp,
7457 STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
7458 if (mp1 == NULL) {
7459 ipif_refrele(ipif);
7460 return (ENOMEM);
7463 mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
7464 bzero(mp1->b_rptr, lifs_bufsize);
7466 lifr = (struct lifreq *)mp1->b_rptr;
7468 ill = ill_head = ipif->ipif_ill;
7469 orig_ipif = ipif;
7471 /* ill_g_usesrc_lock protects ill_usesrc_grp_next */
7472 rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7473 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7475 ill = ill->ill_usesrc_grp_next; /* start from next ill */
7476 for (; (ill != NULL) && (ill != ill_head);
7477 ill = ill->ill_usesrc_grp_next) {
7479 if ((uchar_t *)&lifr[1] > mp1->b_wptr)
7480 break;
7482 ipif = ill->ill_ipif;
7483 ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
7484 if (ipif->ipif_isv6) {
7485 sin6 = (sin6_t *)&lifr->lifr_addr;
7486 *sin6 = sin6_null;
7487 sin6->sin6_family = AF_INET6;
7488 sin6->sin6_addr = ipif->ipif_v6lcl_addr;
7489 lifr->lifr_addrlen = ip_mask_to_plen_v6(
7490 &ipif->ipif_v6net_mask);
7491 } else {
7492 sin = (sin_t *)&lifr->lifr_addr;
7493 *sin = sin_null;
7494 sin->sin_family = AF_INET;
7495 sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7496 lifr->lifr_addrlen = ip_mask_to_plen(
7497 ipif->ipif_net_mask);
7499 lifr++;
7501 rw_exit(&ipst->ips_ill_g_lock);
7502 rw_exit(&ipst->ips_ill_g_usesrc_lock);
7503 ipif_refrele(orig_ipif);
7504 mp1->b_wptr = (uchar_t *)lifr;
7505 STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
7507 return (0);
7510 /* ARGSUSED */
7512 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7513 mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7515 mblk_t *mp1;
7516 int list;
7517 ill_t *ill;
7518 ipif_t *ipif;
7519 int flags;
7520 int numlifs = 0;
7521 size_t lifc_bufsize;
7522 struct lifreq *lifr;
7523 sa_family_t family;
7524 struct sockaddr_in *sin;
7525 struct sockaddr_in6 *sin6;
7526 ill_walk_context_t ctx;
7527 struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7528 int32_t lifclen;
7529 zoneid_t zoneid;
7530 STRUCT_HANDLE(lifconf, lifc);
7531 ip_stack_t *ipst = CONNQ_TO_IPST(q);
7533 ip1dbg(("ip_sioctl_get_lifconf"));
7535 ASSERT(q->q_next == NULL);
7537 zoneid = Q_TO_CONN(q)->conn_zoneid;
7539 /* Existence verified in ip_wput_nondata */
7540 mp1 = mp->b_cont->b_cont;
7543 * An extended version of SIOCGIFCONF that takes an
7544 * additional address family and flags field.
7545 * AF_UNSPEC retrieve both IPv4 and IPv6.
7546 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
7547 * interfaces are omitted.
7548 * Similarly, IPIF_TEMPORARY interfaces are omitted
7549 * unless LIFC_TEMPORARY is specified.
7550 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
7551 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
7552 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
7553 * has priority over LIFC_NOXMIT.
7555 STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
7557 if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
7558 return (EINVAL);
7561 * Must be (better be!) continuation of a TRANSPARENT
7562 * IOCTL. We just copied in the lifconf structure.
7564 STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
7566 family = STRUCT_FGET(lifc, lifc_family);
7567 flags = STRUCT_FGET(lifc, lifc_flags);
7569 switch (family) {
7570 case AF_UNSPEC:
7572 * walk all ILL's.
7574 list = MAX_G_HEADS;
7575 break;
7576 case AF_INET:
7578 * walk only IPV4 ILL's.
7580 list = IP_V4_G_HEAD;
7581 break;
7582 case AF_INET6:
7584 * walk only IPV6 ILL's.
7586 list = IP_V6_G_HEAD;
7587 break;
7588 default:
7589 return (EAFNOSUPPORT);
7593 * Allocate a buffer to hold requested information.
7595 * If lifc_len is larger than what is needed, we only
7596 * allocate what we will use.
7598 * If lifc_len is smaller than what is needed, return
7599 * EINVAL.
7601 numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
7602 lifc_bufsize = numlifs * sizeof (struct lifreq);
7603 lifclen = STRUCT_FGET(lifc, lifc_len);
7604 if (lifc_bufsize > lifclen) {
7605 if (iocp->ioc_cmd == O_SIOCGLIFCONF)
7606 return (EINVAL);
7607 else
7608 lifc_bufsize = lifclen;
7611 mp1 = mi_copyout_alloc(q, mp,
7612 STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
7613 if (mp1 == NULL)
7614 return (ENOMEM);
7616 mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
7617 bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7619 lifr = (struct lifreq *)mp1->b_rptr;
7621 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7622 ill = ill_first(list, list, &ctx, ipst);
7623 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7624 if (IS_UNDER_IPMP(ill) && !(flags & LIFC_UNDER_IPMP))
7625 continue;
7627 for (ipif = ill->ill_ipif; ipif != NULL;
7628 ipif = ipif->ipif_next) {
7629 if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7630 !(flags & LIFC_NOXMIT))
7631 continue;
7633 if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7634 !(flags & LIFC_TEMPORARY))
7635 continue;
7637 if (((ipif->ipif_flags &
7638 (IPIF_NOXMIT|IPIF_NOLOCAL|
7639 IPIF_DEPRECATED)) ||
7640 IS_LOOPBACK(ill) ||
7641 !(ipif->ipif_flags & IPIF_UP)) &&
7642 (flags & LIFC_EXTERNAL_SOURCE))
7643 continue;
7645 if (zoneid != ipif->ipif_zoneid &&
7646 ipif->ipif_zoneid != ALL_ZONES &&
7647 (zoneid != GLOBAL_ZONEID ||
7648 !(flags & LIFC_ALLZONES)))
7649 continue;
7651 if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
7652 if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
7653 rw_exit(&ipst->ips_ill_g_lock);
7654 return (EINVAL);
7655 } else {
7656 goto lif_copydone;
7660 ipif_get_name(ipif, lifr->lifr_name,
7661 sizeof (lifr->lifr_name));
7662 lifr->lifr_type = ill->ill_type;
7663 if (ipif->ipif_isv6) {
7664 sin6 = (sin6_t *)&lifr->lifr_addr;
7665 *sin6 = sin6_null;
7666 sin6->sin6_family = AF_INET6;
7667 sin6->sin6_addr =
7668 ipif->ipif_v6lcl_addr;
7669 lifr->lifr_addrlen =
7670 ip_mask_to_plen_v6(
7671 &ipif->ipif_v6net_mask);
7672 } else {
7673 sin = (sin_t *)&lifr->lifr_addr;
7674 *sin = sin_null;
7675 sin->sin_family = AF_INET;
7676 sin->sin_addr.s_addr =
7677 ipif->ipif_lcl_addr;
7678 lifr->lifr_addrlen =
7679 ip_mask_to_plen(
7680 ipif->ipif_net_mask);
7682 lifr++;
7685 lif_copydone:
7686 rw_exit(&ipst->ips_ill_g_lock);
7688 mp1->b_wptr = (uchar_t *)lifr;
7689 if (STRUCT_BUF(lifc) != NULL) {
7690 STRUCT_FSET(lifc, lifc_len,
7691 (int)((uchar_t *)lifr - mp1->b_rptr));
7693 return (0);
7696 static void
7697 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
7699 ip6_asp_t *table;
7700 size_t table_size;
7701 mblk_t *data_mp;
7702 struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7703 ip_stack_t *ipst;
7705 if (q->q_next == NULL)
7706 ipst = CONNQ_TO_IPST(q);
7707 else
7708 ipst = ILLQ_TO_IPST(q);
7710 /* These two ioctls are I_STR only */
7711 if (iocp->ioc_count == TRANSPARENT) {
7712 miocnak(q, mp, 0, EINVAL);
7713 return;
7716 data_mp = mp->b_cont;
7717 if (data_mp == NULL) {
7718 /* The user passed us a NULL argument */
7719 table = NULL;
7720 table_size = iocp->ioc_count;
7721 } else {
7723 * The user provided a table. The stream head
7724 * may have copied in the user data in chunks,
7725 * so make sure everything is pulled up
7726 * properly.
7728 if (MBLKL(data_mp) < iocp->ioc_count) {
7729 mblk_t *new_data_mp;
7730 if ((new_data_mp = msgpullup(data_mp, -1)) ==
7731 NULL) {
7732 miocnak(q, mp, 0, ENOMEM);
7733 return;
7735 freemsg(data_mp);
7736 data_mp = new_data_mp;
7737 mp->b_cont = data_mp;
7739 table = (ip6_asp_t *)data_mp->b_rptr;
7740 table_size = iocp->ioc_count;
7743 switch (iocp->ioc_cmd) {
7744 case SIOCGIP6ADDRPOLICY:
7745 iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
7746 if (iocp->ioc_rval == -1)
7747 iocp->ioc_error = EINVAL;
7748 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7749 else if (table != NULL &&
7750 (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
7751 ip6_asp_t *src = table;
7752 ip6_asp32_t *dst = (void *)table;
7753 int count = table_size / sizeof (ip6_asp_t);
7754 int i;
7757 * We need to do an in-place shrink of the array
7758 * to match the alignment attributes of the
7759 * 32-bit ABI looking at it.
7761 /* LINTED: logical expression always true: op "||" */
7762 ASSERT(sizeof (*src) > sizeof (*dst));
7763 for (i = 1; i < count; i++)
7764 bcopy(src + i, dst + i, sizeof (*dst));
7766 #endif
7767 break;
7769 case SIOCSIP6ADDRPOLICY:
7770 ASSERT(mp->b_prev == NULL);
7771 mp->b_prev = (void *)q;
7772 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7774 * We pass in the datamodel here so that the ip6_asp_replace()
7775 * routine can handle converting from 32-bit to native formats
7776 * where necessary.
7778 * A better way to handle this might be to convert the inbound
7779 * data structure here, and hang it off a new 'mp'; thus the
7780 * ip6_asp_replace() logic would always be dealing with native
7781 * format data structures..
7783 * (An even simpler way to handle these ioctls is to just
7784 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
7785 * and just recompile everything that depends on it.)
7787 #endif
7788 ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
7789 iocp->ioc_flag & IOC_MODELS);
7790 return;
7793 DB_TYPE(mp) = (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
7794 qreply(q, mp);
7797 static void
7798 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
7800 mblk_t *data_mp;
7801 struct dstinforeq *dir;
7802 uint8_t *end, *cur;
7803 in6_addr_t *daddr, *saddr;
7804 ipaddr_t v4daddr;
7805 ire_t *ire;
7806 ipaddr_t v4setsrc;
7807 in6_addr_t v6setsrc;
7808 char *slabel, *dlabel;
7809 boolean_t isipv4;
7810 int match_ire;
7811 ill_t *dst_ill;
7812 struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7813 conn_t *connp = Q_TO_CONN(q);
7814 zoneid_t zoneid = IPCL_ZONEID(connp);
7815 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
7816 uint64_t ipif_flags;
7818 ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
7821 * This ioctl is I_STR only, and must have a
7822 * data mblk following the M_IOCTL mblk.
7824 data_mp = mp->b_cont;
7825 if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
7826 miocnak(q, mp, 0, EINVAL);
7827 return;
7830 if (MBLKL(data_mp) < iocp->ioc_count) {
7831 mblk_t *new_data_mp;
7833 if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
7834 miocnak(q, mp, 0, ENOMEM);
7835 return;
7837 freemsg(data_mp);
7838 data_mp = new_data_mp;
7839 mp->b_cont = data_mp;
7841 match_ire = MATCH_IRE_DSTONLY;
7843 for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
7844 end - cur >= sizeof (struct dstinforeq);
7845 cur += sizeof (struct dstinforeq)) {
7846 dir = (struct dstinforeq *)cur;
7847 daddr = &dir->dir_daddr;
7848 saddr = &dir->dir_saddr;
7851 * ip_addr_scope_v6() and ip6_asp_lookup() handle
7852 * v4 mapped addresses; ire_ftable_lookup_v6()
7853 * and ip_select_source_v6() do not.
7855 dir->dir_dscope = ip_addr_scope_v6(daddr);
7856 dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
7858 isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
7859 if (isipv4) {
7860 IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
7861 v4setsrc = INADDR_ANY;
7862 ire = ire_route_recursive_v4(v4daddr, 0, NULL, zoneid,
7863 match_ire, IRR_ALLOCATE, 0, ipst, &v4setsrc, NULL);
7864 } else {
7865 v6setsrc = ipv6_all_zeros;
7866 ire = ire_route_recursive_v6(daddr, 0, NULL, zoneid,
7867 match_ire, IRR_ALLOCATE, 0, ipst, &v6setsrc, NULL);
7869 ASSERT(ire != NULL);
7870 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
7871 ire_refrele(ire);
7872 dir->dir_dreachable = 0;
7874 /* move on to next dst addr */
7875 continue;
7877 dir->dir_dreachable = 1;
7879 dst_ill = ire_nexthop_ill(ire);
7880 if (dst_ill == NULL) {
7881 ire_refrele(ire);
7882 continue;
7885 /* With ipmp we most likely look at the ipmp ill here */
7886 dir->dir_dmactype = dst_ill->ill_mactype;
7888 if (isipv4) {
7889 ipaddr_t v4saddr;
7891 if (ip_select_source_v4(dst_ill, v4setsrc, v4daddr,
7892 connp->conn_ixa->ixa_multicast_ifaddr, zoneid, ipst,
7893 &v4saddr, NULL, &ipif_flags) != 0) {
7894 v4saddr = INADDR_ANY;
7895 ipif_flags = 0;
7897 IN6_IPADDR_TO_V4MAPPED(v4saddr, saddr);
7898 } else {
7899 if (ip_select_source_v6(dst_ill, &v6setsrc, daddr,
7900 zoneid, ipst, B_FALSE, IPV6_PREFER_SRC_DEFAULT,
7901 saddr, NULL, &ipif_flags) != 0) {
7902 *saddr = ipv6_all_zeros;
7903 ipif_flags = 0;
7907 dir->dir_sscope = ip_addr_scope_v6(saddr);
7908 slabel = ip6_asp_lookup(saddr, NULL, ipst);
7909 dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
7910 dir->dir_sdeprecated = (ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
7911 ire_refrele(ire);
7912 ill_refrele(dst_ill);
7914 miocack(q, mp, iocp->ioc_count, 0);
7918 * Check if this is an address assigned to this machine.
7919 * Skips interfaces that are down by using ire checks.
7920 * Translates mapped addresses to v4 addresses and then
7921 * treats them as such, returning true if the v4 address
7922 * associated with this mapped address is configured.
7923 * Note: Applications will have to be careful what they do
7924 * with the response; use of mapped addresses limits
7925 * what can be done with the socket, especially with
7926 * respect to socket options and ioctls - neither IPv4
7927 * options nor IPv6 sticky options/ancillary data options
7928 * may be used.
7930 /* ARGSUSED */
7932 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7933 ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
7935 struct sioc_addrreq *sia;
7936 sin_t *sin;
7937 ire_t *ire;
7938 mblk_t *mp1;
7939 zoneid_t zoneid;
7940 ip_stack_t *ipst;
7942 ip1dbg(("ip_sioctl_tmyaddr"));
7944 ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
7945 zoneid = Q_TO_CONN(q)->conn_zoneid;
7946 ipst = CONNQ_TO_IPST(q);
7948 /* Existence verified in ip_wput_nondata */
7949 mp1 = mp->b_cont->b_cont;
7950 sia = (struct sioc_addrreq *)mp1->b_rptr;
7951 sin = (sin_t *)&sia->sa_addr;
7952 switch (sin->sin_family) {
7953 case AF_INET6: {
7954 sin6_t *sin6 = (sin6_t *)sin;
7956 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
7957 ipaddr_t v4_addr;
7959 IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
7960 v4_addr);
7961 ire = ire_ftable_lookup_v4(v4_addr, 0, 0,
7962 IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
7963 MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
7964 } else {
7965 in6_addr_t v6addr;
7967 v6addr = sin6->sin6_addr;
7968 ire = ire_ftable_lookup_v6(&v6addr, 0, 0,
7969 IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
7970 MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
7972 break;
7974 case AF_INET: {
7975 ipaddr_t v4addr;
7977 v4addr = sin->sin_addr.s_addr;
7978 ire = ire_ftable_lookup_v4(v4addr, 0, 0,
7979 IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
7980 MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
7981 break;
7983 default:
7984 return (EAFNOSUPPORT);
7986 if (ire != NULL) {
7987 sia->sa_res = 1;
7988 ire_refrele(ire);
7989 } else {
7990 sia->sa_res = 0;
7992 return (0);
7996 * Check if this is an address assigned on-link i.e. neighbor,
7997 * and makes sure it's reachable from the current zone.
7998 * Returns true for my addresses as well.
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.
8009 /* ARGSUSED */
8011 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8012 ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
8014 struct sioc_addrreq *sia;
8015 sin_t *sin;
8016 mblk_t *mp1;
8017 ire_t *ire = NULL;
8018 zoneid_t zoneid;
8019 ip_stack_t *ipst;
8021 ip1dbg(("ip_sioctl_tonlink"));
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);
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;
8033 * We check for IRE_ONLINK and exclude IRE_BROADCAST|IRE_MULTICAST
8034 * to make sure we only look at on-link unicast address.
8036 switch (sin->sin_family) {
8037 case AF_INET6: {
8038 sin6_t *sin6 = (sin6_t *)sin;
8040 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8041 ipaddr_t v4_addr;
8043 IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8044 v4_addr);
8045 if (!CLASSD(v4_addr)) {
8046 ire = ire_ftable_lookup_v4(v4_addr, 0, 0, 0,
8047 NULL, zoneid, MATCH_IRE_DSTONLY, 0, ipst,
8048 NULL);
8050 } else {
8051 in6_addr_t v6addr;
8053 v6addr = sin6->sin6_addr;
8054 if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
8055 ire = ire_ftable_lookup_v6(&v6addr, 0, 0, 0,
8056 NULL, zoneid, MATCH_IRE_DSTONLY, 0, ipst,
8057 NULL);
8060 break;
8062 case AF_INET: {
8063 ipaddr_t v4addr;
8065 v4addr = sin->sin_addr.s_addr;
8066 if (!CLASSD(v4addr)) {
8067 ire = ire_ftable_lookup_v4(v4addr, 0, 0, 0, NULL,
8068 zoneid, MATCH_IRE_DSTONLY, 0, ipst, NULL);
8070 break;
8072 default:
8073 return (EAFNOSUPPORT);
8075 sia->sa_res = 0;
8076 if (ire != NULL) {
8077 ASSERT(!(ire->ire_type & IRE_MULTICAST));
8079 if ((ire->ire_type & IRE_ONLINK) &&
8080 !(ire->ire_type & IRE_BROADCAST))
8081 sia->sa_res = 1;
8082 ire_refrele(ire);
8084 return (0);
8088 * TBD: implement when kernel maintaines a list of site prefixes.
8090 /* ARGSUSED */
8092 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8093 ip_ioctl_cmd_t *ipip, void *ifreq)
8095 return (ENXIO);
8098 /* ARP IOCTLs. */
8099 /* ARGSUSED */
8101 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8102 ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8104 int err;
8105 ipaddr_t ipaddr;
8106 struct iocblk *iocp;
8107 conn_t *connp;
8108 struct arpreq *ar;
8109 struct xarpreq *xar;
8110 int arp_flags, flags, alength;
8111 uchar_t *lladdr;
8112 ip_stack_t *ipst;
8113 ill_t *ill = ipif->ipif_ill;
8114 ill_t *proxy_ill = NULL;
8115 ipmp_arpent_t *entp = NULL;
8116 boolean_t proxyarp = B_FALSE;
8117 boolean_t if_arp_ioctl = B_FALSE;
8118 ncec_t *ncec = NULL;
8119 nce_t *nce;
8121 ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8122 connp = Q_TO_CONN(q);
8123 ipst = connp->conn_netstack->netstack_ip;
8124 iocp = (struct iocblk *)mp->b_rptr;
8126 if (ipip->ipi_cmd_type == XARP_CMD) {
8127 /* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
8128 xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
8129 ar = NULL;
8131 arp_flags = xar->xarp_flags;
8132 lladdr = (uchar_t *)LLADDR(&xar->xarp_ha);
8133 if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
8135 * Validate against user's link layer address length
8136 * input and name and addr length limits.
8138 alength = ill->ill_phys_addr_length;
8139 if (ipip->ipi_cmd == SIOCSXARP) {
8140 if (alength != xar->xarp_ha.sdl_alen ||
8141 (alength + xar->xarp_ha.sdl_nlen >
8142 sizeof (xar->xarp_ha.sdl_data)))
8143 return (EINVAL);
8145 } else {
8146 /* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
8147 ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
8148 xar = NULL;
8150 arp_flags = ar->arp_flags;
8151 lladdr = (uchar_t *)ar->arp_ha.sa_data;
8153 * Theoretically, the sa_family could tell us what link
8154 * layer type this operation is trying to deal with. By
8155 * common usage AF_UNSPEC means ethernet. We'll assume
8156 * any attempt to use the SIOC?ARP ioctls is for ethernet,
8157 * for now. Our new SIOC*XARP ioctls can be used more
8158 * generally.
8160 * If the underlying media happens to have a non 6 byte
8161 * address, arp module will fail set/get, but the del
8162 * operation will succeed.
8164 alength = 6;
8165 if ((ipip->ipi_cmd != SIOCDARP) &&
8166 (alength != ill->ill_phys_addr_length)) {
8167 return (EINVAL);
8171 /* Translate ATF* flags to NCE* flags */
8172 flags = 0;
8173 if (arp_flags & ATF_AUTHORITY)
8174 flags |= NCE_F_AUTHORITY;
8175 if (arp_flags & ATF_PERM)
8176 flags |= NCE_F_NONUD; /* not subject to aging */
8177 if (arp_flags & ATF_PUBL)
8178 flags |= NCE_F_PUBLISH;
8181 * IPMP ARP special handling:
8183 * 1. Since ARP mappings must appear consistent across the group,
8184 * prohibit changing ARP mappings on the underlying interfaces.
8186 * 2. Since ARP mappings for IPMP data addresses are maintained by
8187 * IP itself, prohibit changing them.
8189 * 3. For proxy ARP, use a functioning hardware address in the group,
8190 * provided one exists. If one doesn't, just add the entry as-is;
8191 * ipmp_illgrp_refresh_arpent() will refresh it if things change.
8193 if (IS_UNDER_IPMP(ill)) {
8194 if (ipip->ipi_cmd != SIOCGARP && ipip->ipi_cmd != SIOCGXARP)
8195 return (EPERM);
8197 if (IS_IPMP(ill)) {
8198 ipmp_illgrp_t *illg = ill->ill_grp;
8200 switch (ipip->ipi_cmd) {
8201 case SIOCSARP:
8202 case SIOCSXARP:
8203 proxy_ill = ipmp_illgrp_find_ill(illg, lladdr, alength);
8204 if (proxy_ill != NULL) {
8205 proxyarp = B_TRUE;
8206 if (!ipmp_ill_is_active(proxy_ill))
8207 proxy_ill = ipmp_illgrp_next_ill(illg);
8208 if (proxy_ill != NULL)
8209 lladdr = proxy_ill->ill_phys_addr;
8211 /* FALLTHRU */
8215 ipaddr = sin->sin_addr.s_addr;
8217 * don't match across illgrp per case (1) and (2).
8218 * XXX use IS_IPMP(ill) like ndp_sioc_update?
8220 nce = nce_lookup_v4(ill, &ipaddr);
8221 if (nce != NULL)
8222 ncec = nce->nce_common;
8224 switch (iocp->ioc_cmd) {
8225 case SIOCDARP:
8226 case SIOCDXARP: {
8228 * Delete the NCE if any.
8230 if (ncec == NULL) {
8231 iocp->ioc_error = ENXIO;
8232 break;
8234 /* Don't allow changes to arp mappings of local addresses. */
8235 if (NCE_MYADDR(ncec)) {
8236 nce_refrele(nce);
8237 return (ENOTSUP);
8239 iocp->ioc_error = 0;
8242 * Delete the nce_common which has ncec_ill set to ipmp_ill.
8243 * This will delete all the nce entries on the under_ills.
8245 ncec_delete(ncec);
8247 * Once the NCE has been deleted, then the ire_dep* consistency
8248 * mechanism will find any IRE which depended on the now
8249 * condemned NCE (as part of sending packets).
8250 * That mechanism handles redirects by deleting redirects
8251 * that refer to UNREACHABLE nces.
8253 break;
8255 case SIOCGARP:
8256 case SIOCGXARP:
8257 if (ncec != NULL) {
8258 lladdr = ncec->ncec_lladdr;
8259 flags = ncec->ncec_flags;
8260 iocp->ioc_error = 0;
8261 ip_sioctl_garp_reply(mp, ncec->ncec_ill, lladdr, flags);
8262 } else {
8263 iocp->ioc_error = ENXIO;
8265 break;
8266 case SIOCSARP:
8267 case SIOCSXARP:
8268 /* Don't allow changes to arp mappings of local addresses. */
8269 if (ncec != NULL && NCE_MYADDR(ncec)) {
8270 nce_refrele(nce);
8271 return (ENOTSUP);
8274 /* static arp entries will undergo NUD if ATF_PERM is not set */
8275 flags |= NCE_F_STATIC;
8276 if (!if_arp_ioctl) {
8277 ip_nce_lookup_and_update(&ipaddr, NULL, ipst,
8278 lladdr, alength, flags);
8279 } else {
8280 ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
8281 if (ipif != NULL) {
8282 ip_nce_lookup_and_update(&ipaddr, ipif, ipst,
8283 lladdr, alength, flags);
8284 ipif_refrele(ipif);
8287 if (nce != NULL) {
8288 nce_refrele(nce);
8289 nce = NULL;
8292 * NCE_F_STATIC entries will be added in state ND_REACHABLE
8293 * by nce_add_common()
8295 err = nce_lookup_then_add_v4(ill, lladdr,
8296 ill->ill_phys_addr_length, &ipaddr, flags, ND_UNCHANGED,
8297 &nce);
8298 if (err == EEXIST) {
8299 ncec = nce->nce_common;
8300 mutex_enter(&ncec->ncec_lock);
8301 ncec->ncec_state = ND_REACHABLE;
8302 ncec->ncec_flags = flags;
8303 nce_update(ncec, ND_UNCHANGED, lladdr);
8304 mutex_exit(&ncec->ncec_lock);
8305 err = 0;
8307 if (nce != NULL) {
8308 nce_refrele(nce);
8309 nce = NULL;
8311 if (IS_IPMP(ill) && err == 0) {
8312 entp = ipmp_illgrp_create_arpent(ill->ill_grp,
8313 proxyarp, ipaddr, lladdr, ill->ill_phys_addr_length,
8314 flags);
8315 if (entp == NULL || (proxyarp && proxy_ill == NULL)) {
8316 iocp->ioc_error = (entp == NULL ? ENOMEM : 0);
8317 break;
8320 iocp->ioc_error = err;
8323 if (nce != NULL) {
8324 nce_refrele(nce);
8328 * If we created an IPMP ARP entry, mark that we've notified ARP.
8330 if (entp != NULL)
8331 ipmp_illgrp_mark_arpent(ill->ill_grp, entp);
8333 return (iocp->ioc_error);
8337 * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
8338 * the associated sin and refhold and return the associated ipif via `ci'.
8341 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8342 cmd_info_t *ci)
8344 mblk_t *mp1;
8345 sin_t *sin;
8346 conn_t *connp;
8347 ipif_t *ipif;
8348 ire_t *ire = NULL;
8349 ill_t *ill = NULL;
8350 boolean_t exists;
8351 ip_stack_t *ipst;
8352 struct arpreq *ar;
8353 struct xarpreq *xar;
8354 struct sockaddr_dl *sdl;
8356 /* ioctl comes down on a conn */
8357 ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8358 connp = Q_TO_CONN(q);
8359 if (connp->conn_family == AF_INET6)
8360 return (ENXIO);
8362 ipst = connp->conn_netstack->netstack_ip;
8364 /* Verified in ip_wput_nondata */
8365 mp1 = mp->b_cont->b_cont;
8367 if (ipip->ipi_cmd_type == XARP_CMD) {
8368 ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
8369 xar = (struct xarpreq *)mp1->b_rptr;
8370 sin = (sin_t *)&xar->xarp_pa;
8371 sdl = &xar->xarp_ha;
8373 if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
8374 return (ENXIO);
8375 if (sdl->sdl_nlen >= LIFNAMSIZ)
8376 return (EINVAL);
8377 } else {
8378 ASSERT(ipip->ipi_cmd_type == ARP_CMD);
8379 ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
8380 ar = (struct arpreq *)mp1->b_rptr;
8381 sin = (sin_t *)&ar->arp_pa;
8384 if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
8385 ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
8386 B_FALSE, &exists, B_FALSE, ALL_ZONES, ipst);
8387 if (ipif == NULL)
8388 return (ENXIO);
8389 if (ipif->ipif_id != 0) {
8390 ipif_refrele(ipif);
8391 return (ENXIO);
8393 } else {
8395 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with an sdl_nlen
8396 * of 0: use the IP address to find the ipif. If the IP
8397 * address is an IPMP test address, ire_ftable_lookup() will
8398 * find the wrong ill, so we first do an ipif_lookup_addr().
8400 ipif = ipif_lookup_addr(sin->sin_addr.s_addr, NULL, ALL_ZONES,
8401 ipst);
8402 if (ipif == NULL) {
8403 ire = ire_ftable_lookup_v4(sin->sin_addr.s_addr,
8404 0, 0, IRE_IF_RESOLVER, NULL, ALL_ZONES,
8405 MATCH_IRE_TYPE, 0, ipst, NULL);
8406 if (ire == NULL || ((ill = ire->ire_ill) == NULL)) {
8407 if (ire != NULL)
8408 ire_refrele(ire);
8409 return (ENXIO);
8411 ASSERT(ire != NULL && ill != NULL);
8412 ipif = ill->ill_ipif;
8413 ipif_refhold(ipif);
8414 ire_refrele(ire);
8418 if (ipif->ipif_ill->ill_net_type != IRE_IF_RESOLVER) {
8419 ipif_refrele(ipif);
8420 return (ENXIO);
8423 ci->ci_sin = sin;
8424 ci->ci_ipif = ipif;
8425 return (0);
8429 * Link or unlink the illgrp on IPMP meta-interface `ill' depending on the
8430 * value of `ioccmd'. While an illgrp is linked to an ipmp_grp_t, it is
8431 * accessible from that ipmp_grp_t, which means SIOCSLIFGROUPNAME can look it
8432 * up and thus an ill can join that illgrp.
8434 * We use I_PLINK/I_PUNLINK to do the link/unlink operations rather than
8435 * open()/close() primarily because close() is not allowed to fail or block
8436 * forever. On the other hand, I_PUNLINK *can* fail, and there's no reason
8437 * why anyone should ever need to I_PUNLINK an in-use IPMP stream. To ensure
8438 * symmetric behavior (e.g., doing an I_PLINK after and I_PUNLINK undoes the
8439 * I_PUNLINK) we defer linking to I_PLINK. Separately, we also fail attempts
8440 * to I_LINK since I_UNLINK is optional and we'd end up in an inconsistent
8441 * state if I_UNLINK didn't occur.
8443 * Note that for each plumb/unplumb operation, we may end up here more than
8444 * once because of the way ifconfig works. However, it's OK to link the same
8445 * illgrp more than once, or unlink an illgrp that's already unlinked.
8447 static int
8448 ip_sioctl_plink_ipmp(ill_t *ill, int ioccmd)
8450 int err;
8451 ip_stack_t *ipst = ill->ill_ipst;
8453 ASSERT(IS_IPMP(ill));
8454 ASSERT(IAM_WRITER_ILL(ill));
8456 switch (ioccmd) {
8457 case I_LINK:
8458 return (ENOTSUP);
8460 case I_PLINK:
8461 rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8462 ipmp_illgrp_link_grp(ill->ill_grp, ill->ill_phyint->phyint_grp);
8463 rw_exit(&ipst->ips_ipmp_lock);
8464 break;
8466 case I_PUNLINK:
8468 * Require all UP ipifs be brought down prior to unlinking the
8469 * illgrp so any associated IREs (and other state) is torched.
8471 if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0)
8472 return (EBUSY);
8475 * NOTE: We hold ipmp_lock across the unlink to prevent a race
8476 * with an SIOCSLIFGROUPNAME request from an ill trying to
8477 * join this group. Specifically: ills trying to join grab
8478 * ipmp_lock and bump a "pending join" counter checked by
8479 * ipmp_illgrp_unlink_grp(). During the unlink no new pending
8480 * joins can occur (since we have ipmp_lock). Once we drop
8481 * ipmp_lock, subsequent SIOCSLIFGROUPNAME requests will not
8482 * find the illgrp (since we unlinked it) and will return
8483 * EAFNOSUPPORT. This will then take them back through the
8484 * IPMP meta-interface plumbing logic in ifconfig, and thus
8485 * back through I_PLINK above.
8487 rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8488 err = ipmp_illgrp_unlink_grp(ill->ill_grp);
8489 rw_exit(&ipst->ips_ipmp_lock);
8490 return (err);
8491 default:
8492 break;
8494 return (0);
8498 * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
8499 * atomically set/clear the muxids. Also complete the ioctl by acking or
8500 * naking it. Note that the code is structured such that the link type,
8501 * whether it's persistent or not, is treated equally. ifconfig(1M) and
8502 * its clones use the persistent link, while pppd(1M) and perhaps many
8503 * other daemons may use non-persistent link. When combined with some
8504 * ill_t states, linking and unlinking lower streams may be used as
8505 * indicators of dynamic re-plumbing events [see PSARC/1999/348].
8507 /* ARGSUSED */
8508 void
8509 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8511 mblk_t *mp1;
8512 struct linkblk *li;
8513 int ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
8514 int err = 0;
8516 ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
8517 ioccmd == I_LINK || ioccmd == I_UNLINK);
8519 mp1 = mp->b_cont; /* This is the linkblk info */
8520 li = (struct linkblk *)mp1->b_rptr;
8522 err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li);
8523 if (err == EINPROGRESS)
8524 return;
8525 if (err == 0)
8526 miocack(q, mp, 0, 0);
8527 else
8528 miocnak(q, mp, 0, err);
8530 /* Conn was refheld in ip_sioctl_copyin_setup */
8531 if (CONN_Q(q)) {
8532 CONN_DEC_IOCTLREF(Q_TO_CONN(q));
8533 CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
8538 * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
8539 * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
8540 * module stream).
8541 * Returns zero on success, EINPROGRESS if the operation is still pending, or
8542 * an error code on failure.
8544 static int
8545 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
8546 struct linkblk *li)
8548 int err = 0;
8549 ill_t *ill;
8550 queue_t *ipwq, *dwq;
8551 const char *name;
8552 struct qinit *qinfo;
8553 boolean_t islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
8554 boolean_t entered_ipsq = B_FALSE;
8555 boolean_t is_ip = B_FALSE;
8556 arl_t *arl;
8559 * Walk the lower stream to verify it's the IP module stream.
8560 * The IP module is identified by its name, wput function,
8561 * and non-NULL q_next. STREAMS ensures that the lower stream
8562 * (li->l_qbot) will not vanish until this ioctl completes.
8564 for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
8565 qinfo = ipwq->q_qinfo;
8566 name = qinfo->qi_minfo->mi_idname;
8567 if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
8568 qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8569 is_ip = B_TRUE;
8570 break;
8572 if (name != NULL && strcmp(name, arp_mod_info.mi_idname) == 0 &&
8573 qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8574 break;
8579 * If this isn't an IP module stream, bail.
8581 if (ipwq == NULL)
8582 return (0);
8584 if (!is_ip) {
8585 arl = (arl_t *)ipwq->q_ptr;
8586 ill = arl_to_ill(arl);
8587 if (ill == NULL)
8588 return (0);
8589 } else {
8590 ill = ipwq->q_ptr;
8592 ASSERT(ill != NULL);
8594 if (ipsq == NULL) {
8595 ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
8596 NEW_OP, B_FALSE);
8597 if (ipsq == NULL) {
8598 if (!is_ip)
8599 ill_refrele(ill);
8600 return (EINPROGRESS);
8602 entered_ipsq = B_TRUE;
8604 ASSERT(IAM_WRITER_ILL(ill));
8605 mutex_enter(&ill->ill_lock);
8606 if (!is_ip) {
8607 if (islink && ill->ill_muxid == 0) {
8609 * Plumbing has to be done with IP plumbed first, arp
8610 * second, but here we have arp being plumbed first.
8612 mutex_exit(&ill->ill_lock);
8613 if (entered_ipsq)
8614 ipsq_exit(ipsq);
8615 ill_refrele(ill);
8616 return (EINVAL);
8619 mutex_exit(&ill->ill_lock);
8620 if (!is_ip) {
8621 arl->arl_muxid = islink ? li->l_index : 0;
8622 ill_refrele(ill);
8623 goto done;
8626 if (IS_IPMP(ill) && (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
8627 goto done;
8630 * As part of I_{P}LINKing, stash the number of downstream modules and
8631 * the read queue of the module immediately below IP in the ill.
8632 * These are used during the capability negotiation below.
8634 ill->ill_lmod_rq = NULL;
8635 ill->ill_lmod_cnt = 0;
8636 if (islink && ((dwq = ipwq->q_next) != NULL)) {
8637 ill->ill_lmod_rq = RD(dwq);
8638 for (; dwq != NULL; dwq = dwq->q_next)
8639 ill->ill_lmod_cnt++;
8642 ill->ill_muxid = islink ? li->l_index : 0;
8645 * Mark the ipsq busy until the capability operations initiated below
8646 * complete. The PLINK/UNLINK ioctl itself completes when our caller
8647 * returns, but the capability operation may complete asynchronously
8648 * much later.
8650 ipsq_current_start(ipsq, ill->ill_ipif, ioccmd);
8652 * If there's at least one up ipif on this ill, then we're bound to
8653 * the underlying driver via DLPI. In that case, renegotiate
8654 * capabilities to account for any possible change in modules
8655 * interposed between IP and the driver.
8657 if (ill->ill_ipif_up_count > 0) {
8658 if (islink)
8659 ill_capability_probe(ill);
8660 else
8661 ill_capability_reset(ill, B_FALSE);
8663 ipsq_current_finish(ipsq);
8664 done:
8665 if (entered_ipsq)
8666 ipsq_exit(ipsq);
8668 return (err);
8672 * Search the ioctl command in the ioctl tables and return a pointer
8673 * to the ioctl command information. The ioctl command tables are
8674 * static and fully populated at compile time.
8676 ip_ioctl_cmd_t *
8677 ip_sioctl_lookup(int ioc_cmd)
8679 int index;
8680 ip_ioctl_cmd_t *ipip;
8681 ip_ioctl_cmd_t *ipip_end;
8683 if (ioc_cmd == IPI_DONTCARE)
8684 return (NULL);
8687 * Do a 2 step search. First search the indexed table
8688 * based on the least significant byte of the ioctl cmd.
8689 * If we don't find a match, then search the misc table
8690 * serially.
8692 index = ioc_cmd & 0xFF;
8693 if (index < ip_ndx_ioctl_count) {
8694 ipip = &ip_ndx_ioctl_table[index];
8695 if (ipip->ipi_cmd == ioc_cmd) {
8696 /* Found a match in the ndx table */
8697 return (ipip);
8701 /* Search the misc table */
8702 ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
8703 for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
8704 if (ipip->ipi_cmd == ioc_cmd)
8705 /* Found a match in the misc table */
8706 return (ipip);
8709 return (NULL);
8713 * helper function for ip_sioctl_getsetprop(), which does some sanity checks
8715 static boolean_t
8716 getset_ioctl_checks(mblk_t *mp)
8718 struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8719 mblk_t *mp1 = mp->b_cont;
8720 mod_ioc_prop_t *pioc;
8721 uint_t flags;
8722 uint_t pioc_size;
8724 /* do sanity checks on various arguments */
8725 if (mp1 == NULL || iocp->ioc_count == 0 ||
8726 iocp->ioc_count == TRANSPARENT) {
8727 return (B_FALSE);
8729 if (msgdsize(mp1) < iocp->ioc_count) {
8730 if (!pullupmsg(mp1, iocp->ioc_count))
8731 return (B_FALSE);
8734 pioc = (mod_ioc_prop_t *)mp1->b_rptr;
8736 /* sanity checks on mpr_valsize */
8737 pioc_size = sizeof (mod_ioc_prop_t);
8738 if (pioc->mpr_valsize != 0)
8739 pioc_size += pioc->mpr_valsize - 1;
8741 if (iocp->ioc_count != pioc_size)
8742 return (B_FALSE);
8744 flags = pioc->mpr_flags;
8745 if (iocp->ioc_cmd == SIOCSETPROP) {
8747 * One can either reset the value to it's default value or
8748 * change the current value or append/remove the value from
8749 * a multi-valued properties.
8751 if ((flags & MOD_PROP_DEFAULT) != MOD_PROP_DEFAULT &&
8752 flags != MOD_PROP_ACTIVE &&
8753 flags != (MOD_PROP_ACTIVE|MOD_PROP_APPEND) &&
8754 flags != (MOD_PROP_ACTIVE|MOD_PROP_REMOVE))
8755 return (B_FALSE);
8756 } else {
8757 ASSERT(iocp->ioc_cmd == SIOCGETPROP);
8760 * One can retrieve only one kind of property information
8761 * at a time.
8763 if ((flags & MOD_PROP_ACTIVE) != MOD_PROP_ACTIVE &&
8764 (flags & MOD_PROP_DEFAULT) != MOD_PROP_DEFAULT &&
8765 (flags & MOD_PROP_POSSIBLE) != MOD_PROP_POSSIBLE &&
8766 (flags & MOD_PROP_PERM) != MOD_PROP_PERM)
8767 return (B_FALSE);
8770 return (B_TRUE);
8774 * process the SIOC{SET|GET}PROP ioctl's
8776 /* ARGSUSED */
8777 static void
8778 ip_sioctl_getsetprop(queue_t *q, mblk_t *mp)
8780 struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8781 mblk_t *mp1 = mp->b_cont;
8782 mod_ioc_prop_t *pioc;
8783 mod_prop_info_t *ptbl = NULL, *pinfo = NULL;
8784 ip_stack_t *ipst;
8785 netstack_t *stack;
8786 cred_t *cr;
8787 boolean_t set;
8788 int err;
8790 ASSERT(q->q_next == NULL);
8791 ASSERT(CONN_Q(q));
8793 if (!getset_ioctl_checks(mp)) {
8794 miocnak(q, mp, 0, EINVAL);
8795 return;
8797 ipst = CONNQ_TO_IPST(q);
8798 stack = ipst->ips_netstack;
8799 pioc = (mod_ioc_prop_t *)mp1->b_rptr;
8801 switch (pioc->mpr_proto) {
8802 case MOD_PROTO_IP:
8803 case MOD_PROTO_IPV4:
8804 case MOD_PROTO_IPV6:
8805 ptbl = ipst->ips_propinfo_tbl;
8806 break;
8807 case MOD_PROTO_RAWIP:
8808 ptbl = stack->netstack_icmp->is_propinfo_tbl;
8809 break;
8810 case MOD_PROTO_TCP:
8811 ptbl = stack->netstack_tcp->tcps_propinfo_tbl;
8812 break;
8813 case MOD_PROTO_UDP:
8814 ptbl = stack->netstack_udp->us_propinfo_tbl;
8815 break;
8816 case MOD_PROTO_SCTP:
8817 ptbl = stack->netstack_sctp->sctps_propinfo_tbl;
8818 break;
8819 default:
8820 miocnak(q, mp, 0, EINVAL);
8821 return;
8824 pinfo = mod_prop_lookup(ptbl, pioc->mpr_name, pioc->mpr_proto);
8825 if (pinfo == NULL) {
8826 miocnak(q, mp, 0, ENOENT);
8827 return;
8830 set = (iocp->ioc_cmd == SIOCSETPROP) ? B_TRUE : B_FALSE;
8831 if (set && pinfo->mpi_setf != NULL) {
8832 cr = msg_getcred(mp, NULL);
8833 if (cr == NULL)
8834 cr = iocp->ioc_cr;
8835 err = pinfo->mpi_setf(stack, cr, pinfo, pioc->mpr_ifname,
8836 pioc->mpr_val, pioc->mpr_flags);
8837 } else if (!set && pinfo->mpi_getf != NULL) {
8838 err = pinfo->mpi_getf(stack, pinfo, pioc->mpr_ifname,
8839 pioc->mpr_val, pioc->mpr_valsize, pioc->mpr_flags);
8840 } else {
8841 err = EPERM;
8844 if (err != 0) {
8845 miocnak(q, mp, 0, err);
8846 } else {
8847 if (set)
8848 miocack(q, mp, 0, 0);
8849 else /* For get, we need to return back the data */
8850 miocack(q, mp, iocp->ioc_count, 0);
8855 * process the legacy ND_GET, ND_SET ioctl just for {ip|ip6}_forwarding
8856 * as several routing daemons have unfortunately used this 'unpublished'
8857 * but well-known ioctls.
8859 /* ARGSUSED */
8860 static void
8861 ip_process_legacy_nddprop(queue_t *q, mblk_t *mp)
8863 struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8864 mblk_t *mp1 = mp->b_cont;
8865 char *pname, *pval, *buf;
8866 uint_t bufsize, proto;
8867 mod_prop_info_t *pinfo = NULL;
8868 ip_stack_t *ipst;
8869 int err = 0;
8871 ASSERT(CONN_Q(q));
8872 ipst = CONNQ_TO_IPST(q);
8874 if (iocp->ioc_count == 0 || mp1 == NULL) {
8875 miocnak(q, mp, 0, EINVAL);
8876 return;
8879 mp1->b_datap->db_lim[-1] = '\0'; /* Force null termination */
8880 pval = buf = pname = (char *)mp1->b_rptr;
8881 bufsize = MBLKL(mp1);
8883 if (strcmp(pname, "ip_forwarding") == 0) {
8884 pname = "forwarding";
8885 proto = MOD_PROTO_IPV4;
8886 } else if (strcmp(pname, "ip6_forwarding") == 0) {
8887 pname = "forwarding";
8888 proto = MOD_PROTO_IPV6;
8889 } else {
8890 miocnak(q, mp, 0, EINVAL);
8891 return;
8894 pinfo = mod_prop_lookup(ipst->ips_propinfo_tbl, pname, proto);
8896 switch (iocp->ioc_cmd) {
8897 case ND_GET:
8898 if ((err = pinfo->mpi_getf(ipst->ips_netstack, pinfo, NULL, buf,
8899 bufsize, 0)) == 0) {
8900 miocack(q, mp, iocp->ioc_count, 0);
8901 return;
8903 break;
8904 case ND_SET:
8906 * buffer will have property name and value in the following
8907 * format,
8908 * <property name>'\0'<property value>'\0', extract them;
8910 while (*pval++)
8911 noop;
8913 if (!*pval || pval >= (char *)mp1->b_wptr) {
8914 err = EINVAL;
8915 } else if ((err = pinfo->mpi_setf(ipst->ips_netstack, NULL,
8916 pinfo, NULL, pval, 0)) == 0) {
8917 miocack(q, mp, 0, 0);
8918 return;
8920 break;
8921 default:
8922 err = EINVAL;
8923 break;
8925 miocnak(q, mp, 0, err);
8929 * Wrapper function for resuming deferred ioctl processing
8930 * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
8931 * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
8933 /* ARGSUSED */
8934 void
8935 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
8936 void *dummy_arg)
8938 ip_sioctl_copyin_setup(q, mp);
8942 * ip_sioctl_copyin_setup is called by ip_wput_nondata with any M_IOCTL message
8943 * that arrives. Most of the IOCTLs are "socket" IOCTLs which we handle
8944 * in either I_STR or TRANSPARENT form, using the mi_copy facility.
8945 * We establish here the size of the block to be copied in. mi_copyin
8946 * arranges for this to happen, an processing continues in ip_wput_nondata with
8947 * an M_IOCDATA message.
8949 void
8950 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
8952 int copyin_size;
8953 struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8954 ip_ioctl_cmd_t *ipip;
8955 cred_t *cr;
8956 ip_stack_t *ipst;
8958 if (CONN_Q(q))
8959 ipst = CONNQ_TO_IPST(q);
8960 else
8961 ipst = ILLQ_TO_IPST(q);
8963 ipip = ip_sioctl_lookup(iocp->ioc_cmd);
8964 if (ipip == NULL) {
8966 * The ioctl is not one we understand or own.
8967 * Pass it along to be processed down stream,
8968 * if this is a module instance of IP, else nak
8969 * the ioctl.
8971 if (q->q_next == NULL) {
8972 goto nak;
8973 } else {
8974 putnext(q, mp);
8975 return;
8980 * If this is deferred, then we will do all the checks when we
8981 * come back.
8983 if ((iocp->ioc_cmd == SIOCGDSTINFO ||
8984 iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
8985 ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
8986 return;
8990 * Only allow a very small subset of IP ioctls on this stream if
8991 * IP is a module and not a driver. Allowing ioctls to be processed
8992 * in this case may cause assert failures or data corruption.
8993 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
8994 * ioctls allowed on an IP module stream, after which this stream
8995 * normally becomes a multiplexor (at which time the stream head
8996 * will fail all ioctls).
8998 if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
8999 goto nak;
9002 /* Make sure we have ioctl data to process. */
9003 if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
9004 goto nak;
9007 * Prefer dblk credential over ioctl credential; some synthesized
9008 * ioctls have kcred set because there's no way to crhold()
9009 * a credential in some contexts. (ioc_cr is not crfree() by
9010 * the framework; the caller of ioctl needs to hold the reference
9011 * for the duration of the call).
9013 cr = msg_getcred(mp, NULL);
9014 if (cr == NULL)
9015 cr = iocp->ioc_cr;
9017 /* Make sure normal users don't send down privileged ioctls */
9018 if ((ipip->ipi_flags & IPI_PRIV) &&
9019 (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
9020 /* We checked the privilege earlier but log it here */
9021 miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
9022 return;
9026 * The ioctl command tables can only encode fixed length
9027 * ioctl data. If the length is variable, the table will
9028 * encode the length as zero. Such special cases are handled
9029 * below in the switch.
9031 if (ipip->ipi_copyin_size != 0) {
9032 mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
9033 return;
9036 switch (iocp->ioc_cmd) {
9037 case O_SIOCGIFCONF:
9038 case SIOCGIFCONF:
9040 * This IOCTL is hilarious. See comments in
9041 * ip_sioctl_get_ifconf for the story.
9043 if (iocp->ioc_count == TRANSPARENT)
9044 copyin_size = SIZEOF_STRUCT(ifconf,
9045 iocp->ioc_flag);
9046 else
9047 copyin_size = iocp->ioc_count;
9048 mi_copyin(q, mp, NULL, copyin_size);
9049 return;
9051 case O_SIOCGLIFCONF:
9052 case SIOCGLIFCONF:
9053 copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
9054 mi_copyin(q, mp, NULL, copyin_size);
9055 return;
9057 case SIOCGLIFSRCOF:
9058 copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
9059 mi_copyin(q, mp, NULL, copyin_size);
9060 return;
9062 case SIOCGIP6ADDRPOLICY:
9063 ip_sioctl_ip6addrpolicy(q, mp);
9064 ip6_asp_table_refrele(ipst);
9065 return;
9067 case SIOCSIP6ADDRPOLICY:
9068 ip_sioctl_ip6addrpolicy(q, mp);
9069 return;
9071 case SIOCGDSTINFO:
9072 ip_sioctl_dstinfo(q, mp);
9073 ip6_asp_table_refrele(ipst);
9074 return;
9076 case ND_SET:
9077 case ND_GET:
9078 ip_process_legacy_nddprop(q, mp);
9079 return;
9081 case SIOCSETPROP:
9082 case SIOCGETPROP:
9083 ip_sioctl_getsetprop(q, mp);
9084 return;
9086 case I_PLINK:
9087 case I_PUNLINK:
9088 case I_LINK:
9089 case I_UNLINK:
9091 * We treat non-persistent link similarly as the persistent
9092 * link case, in terms of plumbing/unplumbing, as well as
9093 * dynamic re-plumbing events indicator. See comments
9094 * in ip_sioctl_plink() for more.
9096 * Request can be enqueued in the 'ipsq' while waiting
9097 * to become exclusive. So bump up the conn ref.
9099 if (CONN_Q(q)) {
9100 CONN_INC_REF(Q_TO_CONN(q));
9101 CONN_INC_IOCTLREF(Q_TO_CONN(q))
9103 ip_sioctl_plink(NULL, q, mp, NULL);
9104 return;
9106 case IP_IOCTL:
9107 ip_wput_ioctl(q, mp);
9108 return;
9110 case SIOCILB:
9111 /* The ioctl length varies depending on the ILB command. */
9112 copyin_size = iocp->ioc_count;
9113 if (copyin_size < sizeof (ilb_cmd_t))
9114 goto nak;
9115 mi_copyin(q, mp, NULL, copyin_size);
9116 return;
9118 default:
9119 cmn_err(CE_WARN, "Unknown ioctl %d/0x%x slipped through.",
9120 iocp->ioc_cmd, iocp->ioc_cmd);
9121 /* FALLTHRU */
9123 nak:
9124 if (mp->b_cont != NULL) {
9125 freemsg(mp->b_cont);
9126 mp->b_cont = NULL;
9128 iocp->ioc_error = EINVAL;
9129 mp->b_datap->db_type = M_IOCNAK;
9130 iocp->ioc_count = 0;
9131 qreply(q, mp);
9134 static void
9135 ip_sioctl_garp_reply(mblk_t *mp, ill_t *ill, void *hwaddr, int flags)
9137 struct arpreq *ar;
9138 struct xarpreq *xar;
9139 mblk_t *tmp;
9140 struct iocblk *iocp;
9141 int x_arp_ioctl = B_FALSE;
9142 int *flagsp;
9143 char *storage = NULL;
9145 ASSERT(ill != NULL);
9147 iocp = (struct iocblk *)mp->b_rptr;
9148 ASSERT(iocp->ioc_cmd == SIOCGXARP || iocp->ioc_cmd == SIOCGARP);
9150 tmp = (mp->b_cont)->b_cont; /* xarpreq/arpreq */
9151 if ((iocp->ioc_cmd == SIOCGXARP) ||
9152 (iocp->ioc_cmd == SIOCSXARP)) {
9153 x_arp_ioctl = B_TRUE;
9154 xar = (struct xarpreq *)tmp->b_rptr;
9155 flagsp = &xar->xarp_flags;
9156 storage = xar->xarp_ha.sdl_data;
9157 } else {
9158 ar = (struct arpreq *)tmp->b_rptr;
9159 flagsp = &ar->arp_flags;
9160 storage = ar->arp_ha.sa_data;
9164 * We're done if this is not an SIOCG{X}ARP
9166 if (x_arp_ioctl) {
9167 storage += ill_xarp_info(&xar->xarp_ha, ill);
9168 if ((ill->ill_phys_addr_length + ill->ill_name_length) >
9169 sizeof (xar->xarp_ha.sdl_data)) {
9170 iocp->ioc_error = EINVAL;
9171 return;
9174 *flagsp = ATF_INUSE;
9176 * If /sbin/arp told us we are the authority using the "permanent"
9177 * flag, or if this is one of my addresses print "permanent"
9178 * in the /sbin/arp output.
9180 if ((flags & NCE_F_MYADDR) || (flags & NCE_F_AUTHORITY))
9181 *flagsp |= ATF_AUTHORITY;
9182 if (flags & NCE_F_NONUD)
9183 *flagsp |= ATF_PERM; /* not subject to aging */
9184 if (flags & NCE_F_PUBLISH)
9185 *flagsp |= ATF_PUBL;
9186 if (hwaddr != NULL) {
9187 *flagsp |= ATF_COM;
9188 bcopy((char *)hwaddr, storage, ill->ill_phys_addr_length);
9193 * Create a new logical interface. If ipif_id is zero (i.e. not a logical
9194 * interface) create the next available logical interface for this
9195 * physical interface.
9196 * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
9197 * ipif with the specified name.
9199 * If the address family is not AF_UNSPEC then set the address as well.
9201 * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
9202 * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
9204 * Executed as a writer on the ill.
9205 * So no lock is needed to traverse the ipif chain, or examine the
9206 * phyint flags.
9208 /* ARGSUSED */
9210 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9211 ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9213 mblk_t *mp1;
9214 struct lifreq *lifr;
9215 boolean_t isv6;
9216 boolean_t exists;
9217 char *name;
9218 char *endp;
9219 char *cp;
9220 int namelen;
9221 ipif_t *ipif;
9222 long id;
9223 ipsq_t *ipsq;
9224 ill_t *ill;
9225 sin_t *sin;
9226 int err = 0;
9227 boolean_t found_sep = B_FALSE;
9228 conn_t *connp;
9229 zoneid_t zoneid;
9230 ip_stack_t *ipst = CONNQ_TO_IPST(q);
9232 ASSERT(q->q_next == NULL);
9233 ip1dbg(("ip_sioctl_addif\n"));
9234 /* Existence of mp1 has been checked in ip_wput_nondata */
9235 mp1 = mp->b_cont->b_cont;
9237 * Null terminate the string to protect against buffer
9238 * overrun. String was generated by user code and may not
9239 * be trusted.
9241 lifr = (struct lifreq *)mp1->b_rptr;
9242 lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
9243 name = lifr->lifr_name;
9244 ASSERT(CONN_Q(q));
9245 connp = Q_TO_CONN(q);
9246 isv6 = (connp->conn_family == AF_INET6);
9247 zoneid = connp->conn_zoneid;
9248 namelen = mi_strlen(name);
9249 if (namelen == 0)
9250 return (EINVAL);
9252 exists = B_FALSE;
9253 if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
9254 (mi_strcmp(name, ipif_loopback_name) == 0)) {
9256 * Allow creating lo0 using SIOCLIFADDIF.
9257 * can't be any other writer thread. So can pass null below
9258 * for the last 4 args to ipif_lookup_name.
9260 ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
9261 &exists, isv6, zoneid, ipst);
9262 /* Prevent any further action */
9263 if (ipif == NULL) {
9264 return (ENOBUFS);
9265 } else if (!exists) {
9266 /* We created the ipif now and as writer */
9267 ipif_refrele(ipif);
9268 return (0);
9269 } else {
9270 ill = ipif->ipif_ill;
9271 ill_refhold(ill);
9272 ipif_refrele(ipif);
9274 } else {
9275 /* Look for a colon in the name. */
9276 endp = &name[namelen];
9277 for (cp = endp; --cp > name; ) {
9278 if (*cp == IPIF_SEPARATOR_CHAR) {
9279 found_sep = B_TRUE;
9281 * Reject any non-decimal aliases for plumbing
9282 * of logical interfaces. Aliases with leading
9283 * zeroes are also rejected as they introduce
9284 * ambiguity in the naming of the interfaces.
9285 * Comparing with "0" takes care of all such
9286 * cases.
9288 if ((strncmp("0", cp+1, 1)) == 0)
9289 return (EINVAL);
9291 if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
9292 id <= 0 || *endp != '\0') {
9293 return (EINVAL);
9295 *cp = '\0';
9296 break;
9299 ill = ill_lookup_on_name(name, B_FALSE, isv6, NULL, ipst);
9300 if (found_sep)
9301 *cp = IPIF_SEPARATOR_CHAR;
9302 if (ill == NULL)
9303 return (ENXIO);
9306 ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
9307 B_TRUE);
9310 * Release the refhold due to the lookup, now that we are excl
9311 * or we are just returning
9313 ill_refrele(ill);
9315 if (ipsq == NULL)
9316 return (EINPROGRESS);
9318 /* We are now exclusive on the IPSQ */
9319 ASSERT(IAM_WRITER_ILL(ill));
9321 if (found_sep) {
9322 /* Now see if there is an IPIF with this unit number. */
9323 for (ipif = ill->ill_ipif; ipif != NULL;
9324 ipif = ipif->ipif_next) {
9325 if (ipif->ipif_id == id) {
9326 err = EEXIST;
9327 goto done;
9333 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
9334 * of lo0. Plumbing for lo0:0 happens in ipif_lookup_on_name()
9335 * instead.
9337 if ((ipif = ipif_allocate(ill, found_sep ? id : -1, IRE_LOCAL,
9338 B_TRUE, B_TRUE, &err)) == NULL) {
9339 goto done;
9342 /* Return created name with ioctl */
9343 (void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
9344 IPIF_SEPARATOR_CHAR, ipif->ipif_id);
9345 ip1dbg(("created %s\n", lifr->lifr_name));
9347 /* Set address */
9348 sin = (sin_t *)&lifr->lifr_addr;
9349 if (sin->sin_family != AF_UNSPEC) {
9350 err = ip_sioctl_addr(ipif, sin, q, mp,
9351 &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
9354 done:
9355 ipsq_exit(ipsq);
9356 return (err);
9360 * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
9361 * interface) delete it based on the IP address (on this physical interface).
9362 * Otherwise delete it based on the ipif_id.
9363 * Also, special handling to allow a removeif of lo0.
9365 /* ARGSUSED */
9367 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9368 ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9370 conn_t *connp;
9371 ill_t *ill = ipif->ipif_ill;
9372 boolean_t success;
9373 ip_stack_t *ipst;
9375 ipst = CONNQ_TO_IPST(q);
9377 ASSERT(q->q_next == NULL);
9378 ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
9379 ill->ill_name, ipif->ipif_id, (void *)ipif));
9380 ASSERT(IAM_WRITER_IPIF(ipif));
9382 connp = Q_TO_CONN(q);
9384 * Special case for unplumbing lo0 (the loopback physical interface).
9385 * If unplumbing lo0, the incoming address structure has been
9386 * initialized to all zeros. When unplumbing lo0, all its logical
9387 * interfaces must be removed too.
9389 * Note that this interface may be called to remove a specific
9390 * loopback logical interface (eg, lo0:1). But in that case
9391 * ipif->ipif_id != 0 so that the code path for that case is the
9392 * same as any other interface (meaning it skips the code directly
9393 * below).
9395 if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9396 if (sin->sin_family == AF_UNSPEC &&
9397 (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
9399 * Mark it condemned. No new ref. will be made to ill.
9401 mutex_enter(&ill->ill_lock);
9402 ill->ill_state_flags |= ILL_CONDEMNED;
9403 for (ipif = ill->ill_ipif; ipif != NULL;
9404 ipif = ipif->ipif_next) {
9405 ipif->ipif_state_flags |= IPIF_CONDEMNED;
9407 mutex_exit(&ill->ill_lock);
9409 ipif = ill->ill_ipif;
9410 /* unplumb the loopback interface */
9411 ill_delete(ill);
9412 mutex_enter(&connp->conn_lock);
9413 mutex_enter(&ill->ill_lock);
9415 /* Are any references to this ill active */
9416 if (ill_is_freeable(ill)) {
9417 mutex_exit(&ill->ill_lock);
9418 mutex_exit(&connp->conn_lock);
9419 ill_delete_tail(ill);
9420 mi_free(ill);
9421 return (0);
9423 success = ipsq_pending_mp_add(connp, ipif,
9424 CONNP_TO_WQ(connp), mp, ILL_FREE);
9425 mutex_exit(&connp->conn_lock);
9426 mutex_exit(&ill->ill_lock);
9427 if (success)
9428 return (EINPROGRESS);
9429 else
9430 return (EINTR);
9434 if (ipif->ipif_id == 0) {
9435 ipsq_t *ipsq;
9437 /* Find based on address */
9438 if (ipif->ipif_isv6) {
9439 sin6_t *sin6;
9441 if (sin->sin_family != AF_INET6)
9442 return (EAFNOSUPPORT);
9444 sin6 = (sin6_t *)sin;
9445 /* We are a writer, so we should be able to lookup */
9446 ipif = ipif_lookup_addr_exact_v6(&sin6->sin6_addr, ill,
9447 ipst);
9448 } else {
9449 if (sin->sin_family != AF_INET)
9450 return (EAFNOSUPPORT);
9452 /* We are a writer, so we should be able to lookup */
9453 ipif = ipif_lookup_addr_exact(sin->sin_addr.s_addr, ill,
9454 ipst);
9456 if (ipif == NULL) {
9457 return (EADDRNOTAVAIL);
9461 * It is possible for a user to send an SIOCLIFREMOVEIF with
9462 * lifr_name of the physical interface but with an ip address
9463 * lifr_addr of a logical interface plumbed over it.
9464 * So update ipx_current_ipif now that ipif points to the
9465 * correct one.
9467 ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
9468 ipsq->ipsq_xop->ipx_current_ipif = ipif;
9470 /* This is a writer */
9471 ipif_refrele(ipif);
9475 * Can not delete instance zero since it is tied to the ill.
9477 if (ipif->ipif_id == 0)
9478 return (EBUSY);
9480 mutex_enter(&ill->ill_lock);
9481 ipif->ipif_state_flags |= IPIF_CONDEMNED;
9482 mutex_exit(&ill->ill_lock);
9484 ipif_free(ipif);
9486 mutex_enter(&connp->conn_lock);
9487 mutex_enter(&ill->ill_lock);
9489 /* Are any references to this ipif active */
9490 if (ipif_is_freeable(ipif)) {
9491 mutex_exit(&ill->ill_lock);
9492 mutex_exit(&connp->conn_lock);
9493 ipif_non_duplicate(ipif);
9494 (void) ipif_down_tail(ipif);
9495 ipif_free_tail(ipif); /* frees ipif */
9496 return (0);
9498 success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
9499 IPIF_FREE);
9500 mutex_exit(&ill->ill_lock);
9501 mutex_exit(&connp->conn_lock);
9502 if (success)
9503 return (EINPROGRESS);
9504 else
9505 return (EINTR);
9509 * Restart the removeif ioctl. The refcnt has gone down to 0.
9510 * The ipif is already condemned. So can't find it thru lookups.
9512 /* ARGSUSED */
9514 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
9515 mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9517 ill_t *ill = ipif->ipif_ill;
9519 ASSERT(IAM_WRITER_IPIF(ipif));
9520 ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
9522 ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
9523 ill->ill_name, ipif->ipif_id, (void *)ipif));
9525 if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9526 ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
9527 ill_delete_tail(ill);
9528 mi_free(ill);
9529 return (0);
9532 ipif_non_duplicate(ipif);
9533 (void) ipif_down_tail(ipif);
9534 ipif_free_tail(ipif);
9536 return (0);
9540 * Set the local interface address using the given prefix and ill_token.
9542 /* ARGSUSED */
9544 ip_sioctl_prefix(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9545 ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9547 int err;
9548 in6_addr_t v6addr;
9549 sin6_t *sin6;
9550 ill_t *ill;
9551 int i;
9553 ip1dbg(("ip_sioctl_prefix(%s:%u %p)\n",
9554 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9556 ASSERT(IAM_WRITER_IPIF(ipif));
9558 if (!ipif->ipif_isv6)
9559 return (EINVAL);
9561 if (sin->sin_family != AF_INET6)
9562 return (EAFNOSUPPORT);
9564 sin6 = (sin6_t *)sin;
9565 v6addr = sin6->sin6_addr;
9566 ill = ipif->ipif_ill;
9568 if (IN6_IS_ADDR_UNSPECIFIED(&v6addr) ||
9569 IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token))
9570 return (EADDRNOTAVAIL);
9572 for (i = 0; i < 4; i++)
9573 sin6->sin6_addr.s6_addr32[i] |= ill->ill_token.s6_addr32[i];
9575 err = ip_sioctl_addr(ipif, sin, q, mp,
9576 &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], dummy_ifreq);
9577 return (err);
9581 * Restart entry point to restart the address set operation after the
9582 * refcounts have dropped to zero.
9584 /* ARGSUSED */
9586 ip_sioctl_prefix_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9587 ip_ioctl_cmd_t *ipip, void *ifreq)
9589 ip1dbg(("ip_sioctl_prefix_restart(%s:%u %p)\n",
9590 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9591 return (ip_sioctl_addr_restart(ipif, sin, q, mp, ipip, ifreq));
9595 * Set the local interface address.
9596 * Allow an address of all zero when the interface is down.
9598 /* ARGSUSED */
9600 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9601 ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9603 int err = 0;
9604 in6_addr_t v6addr;
9605 boolean_t need_up = B_FALSE;
9606 ill_t *ill;
9607 int i;
9609 ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
9610 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9612 ASSERT(IAM_WRITER_IPIF(ipif));
9614 ill = ipif->ipif_ill;
9615 if (ipif->ipif_isv6) {
9616 sin6_t *sin6;
9617 phyint_t *phyi;
9619 if (sin->sin_family != AF_INET6)
9620 return (EAFNOSUPPORT);
9622 sin6 = (sin6_t *)sin;
9623 v6addr = sin6->sin6_addr;
9624 phyi = ill->ill_phyint;
9627 * Enforce that true multicast interfaces have a link-local
9628 * address for logical unit 0.
9630 * However for those ipif's for which link-local address was
9631 * not created by default, also allow setting :: as the address.
9632 * This scenario would arise, when we delete an address on ipif
9633 * with logical unit 0, we would want to set :: as the address.
9635 if (ipif->ipif_id == 0 &&
9636 (ill->ill_flags & ILLF_MULTICAST) &&
9637 !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
9638 !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
9639 !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
9642 * if default link-local was not created by kernel for
9643 * this ill, allow setting :: as the address on ipif:0.
9645 if (ill->ill_flags & ILLF_NOLINKLOCAL) {
9646 if (!IN6_IS_ADDR_UNSPECIFIED(&v6addr))
9647 return (EADDRNOTAVAIL);
9648 } else {
9649 return (EADDRNOTAVAIL);
9654 * up interfaces shouldn't have the unspecified address
9655 * unless they also have the IPIF_NOLOCAL flags set and
9656 * have a subnet assigned.
9658 if ((ipif->ipif_flags & IPIF_UP) &&
9659 IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
9660 (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
9661 IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
9662 return (EADDRNOTAVAIL);
9665 if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9666 return (EADDRNOTAVAIL);
9667 } else {
9668 ipaddr_t addr;
9670 if (sin->sin_family != AF_INET)
9671 return (EAFNOSUPPORT);
9673 addr = sin->sin_addr.s_addr;
9675 /* Allow INADDR_ANY as the local address. */
9676 if (addr != INADDR_ANY &&
9677 !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
9678 return (EADDRNOTAVAIL);
9680 IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9683 * verify that the address being configured is permitted by the
9684 * ill_allowed_ips[] for the interface.
9686 if (ill->ill_allowed_ips_cnt > 0) {
9687 for (i = 0; i < ill->ill_allowed_ips_cnt; i++) {
9688 if (IN6_ARE_ADDR_EQUAL(&ill->ill_allowed_ips[i],
9689 &v6addr))
9690 break;
9692 if (i == ill->ill_allowed_ips_cnt) {
9693 pr_addr_dbg("!allowed addr %s\n", AF_INET6, &v6addr);
9694 return (EPERM);
9698 * Even if there is no change we redo things just to rerun
9699 * ipif_set_default.
9701 if (ipif->ipif_flags & IPIF_UP) {
9703 * Setting a new local address, make sure
9704 * we have net and subnet bcast ire's for
9705 * the old address if we need them.
9708 * If the interface is already marked up,
9709 * we call ipif_down which will take care
9710 * of ditching any IREs that have been set
9711 * up based on the old interface address.
9713 err = ipif_logical_down(ipif, q, mp);
9714 if (err == EINPROGRESS)
9715 return (err);
9716 (void) ipif_down_tail(ipif);
9717 need_up = 1;
9720 err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
9721 return (err);
9725 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9726 boolean_t need_up)
9728 in6_addr_t v6addr;
9729 in6_addr_t ov6addr;
9730 ipaddr_t addr;
9731 sin6_t *sin6;
9732 int sinlen;
9733 int err = 0;
9734 ill_t *ill = ipif->ipif_ill;
9735 boolean_t need_dl_down;
9736 boolean_t need_arp_down;
9737 struct iocblk *iocp;
9739 iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
9741 ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
9742 ill->ill_name, ipif->ipif_id, (void *)ipif));
9743 ASSERT(IAM_WRITER_IPIF(ipif));
9745 /* Must cancel any pending timer before taking the ill_lock */
9746 if (ipif->ipif_recovery_id != 0)
9747 (void) untimeout(ipif->ipif_recovery_id);
9748 ipif->ipif_recovery_id = 0;
9750 if (ipif->ipif_isv6) {
9751 sin6 = (sin6_t *)sin;
9752 v6addr = sin6->sin6_addr;
9753 sinlen = sizeof (struct sockaddr_in6);
9754 } else {
9755 addr = sin->sin_addr.s_addr;
9756 IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9757 sinlen = sizeof (struct sockaddr_in);
9759 mutex_enter(&ill->ill_lock);
9760 ov6addr = ipif->ipif_v6lcl_addr;
9761 ipif->ipif_v6lcl_addr = v6addr;
9762 sctp_update_ipif_addr(ipif, ov6addr);
9763 ipif->ipif_addr_ready = 0;
9765 ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
9768 * If the interface was previously marked as a duplicate, then since
9769 * we've now got a "new" address, it should no longer be considered a
9770 * duplicate -- even if the "new" address is the same as the old one.
9771 * Note that if all ipifs are down, we may have a pending ARP down
9772 * event to handle. This is because we want to recover from duplicates
9773 * and thus delay tearing down ARP until the duplicates have been
9774 * removed or disabled.
9776 need_dl_down = need_arp_down = B_FALSE;
9777 if (ipif->ipif_flags & IPIF_DUPLICATE) {
9778 need_arp_down = !need_up;
9779 ipif->ipif_flags &= ~IPIF_DUPLICATE;
9780 if (--ill->ill_ipif_dup_count == 0 && !need_up &&
9781 ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
9782 need_dl_down = B_TRUE;
9786 ipif_set_default(ipif);
9789 * If we've just manually set the IPv6 link-local address (0th ipif),
9790 * tag the ill so that future updates to the interface ID don't result
9791 * in this address getting automatically reconfigured from under the
9792 * administrator.
9794 if (ipif->ipif_isv6 && ipif->ipif_id == 0) {
9795 if (iocp == NULL || (iocp->ioc_cmd == SIOCSLIFADDR &&
9796 !IN6_IS_ADDR_UNSPECIFIED(&v6addr)))
9797 ill->ill_manual_linklocal = 1;
9801 * When publishing an interface address change event, we only notify
9802 * the event listeners of the new address. It is assumed that if they
9803 * actively care about the addresses assigned that they will have
9804 * already discovered the previous address assigned (if there was one.)
9806 * Don't attach nic event message for SIOCLIFADDIF ioctl.
9808 if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
9809 ill_nic_event_dispatch(ill, MAP_IPIF_ID(ipif->ipif_id),
9810 NE_ADDRESS_CHANGE, sin, sinlen);
9813 mutex_exit(&ill->ill_lock);
9815 if (need_up) {
9817 * Now bring the interface back up. If this
9818 * is the only IPIF for the ILL, ipif_up
9819 * will have to re-bind to the device, so
9820 * we may get back EINPROGRESS, in which
9821 * case, this IOCTL will get completed in
9822 * ip_rput_dlpi when we see the DL_BIND_ACK.
9824 err = ipif_up(ipif, q, mp);
9825 } else {
9826 /* Perhaps ilgs should use this ill */
9827 update_conn_ill(NULL, ill->ill_ipst);
9830 if (need_dl_down)
9831 ill_dl_down(ill);
9833 if (need_arp_down && !ill->ill_isv6)
9834 (void) ipif_arp_down(ipif);
9837 * The default multicast interface might have changed (for
9838 * instance if the IPv6 scope of the address changed)
9840 ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
9842 return (err);
9846 * Restart entry point to restart the address set operation after the
9847 * refcounts have dropped to zero.
9849 /* ARGSUSED */
9851 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9852 ip_ioctl_cmd_t *ipip, void *ifreq)
9854 ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
9855 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9856 ASSERT(IAM_WRITER_IPIF(ipif));
9857 (void) ipif_down_tail(ipif);
9858 return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
9861 /* ARGSUSED */
9863 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9864 ip_ioctl_cmd_t *ipip, void *if_req)
9866 sin6_t *sin6 = (struct sockaddr_in6 *)sin;
9867 struct lifreq *lifr = (struct lifreq *)if_req;
9869 ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
9870 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9872 * The net mask and address can't change since we have a
9873 * reference to the ipif. So no lock is necessary.
9875 if (ipif->ipif_isv6) {
9876 *sin6 = sin6_null;
9877 sin6->sin6_family = AF_INET6;
9878 sin6->sin6_addr = ipif->ipif_v6lcl_addr;
9879 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
9880 sin6->sin6_scope_id =
9881 ipif->ipif_ill->ill_phyint->phyint_ifindex;
9883 ASSERT(ipip->ipi_cmd_type == LIF_CMD);
9884 lifr->lifr_addrlen =
9885 ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
9886 } else {
9887 *sin = sin_null;
9888 sin->sin_family = AF_INET;
9889 sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
9890 if (ipip->ipi_cmd_type == LIF_CMD) {
9891 lifr->lifr_addrlen =
9892 ip_mask_to_plen(ipif->ipif_net_mask);
9895 return (0);
9899 * Set the destination address for a pt-pt interface.
9901 /* ARGSUSED */
9903 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9904 ip_ioctl_cmd_t *ipip, void *if_req)
9906 int err = 0;
9907 in6_addr_t v6addr;
9908 boolean_t need_up = B_FALSE;
9910 ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
9911 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9912 ASSERT(IAM_WRITER_IPIF(ipif));
9914 if (ipif->ipif_isv6) {
9915 sin6_t *sin6;
9917 if (sin->sin_family != AF_INET6)
9918 return (EAFNOSUPPORT);
9920 sin6 = (sin6_t *)sin;
9921 v6addr = sin6->sin6_addr;
9923 if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9924 return (EADDRNOTAVAIL);
9925 } else {
9926 ipaddr_t addr;
9928 if (sin->sin_family != AF_INET)
9929 return (EAFNOSUPPORT);
9931 addr = sin->sin_addr.s_addr;
9932 if (addr != INADDR_ANY &&
9933 !ip_addr_ok_v4(addr, ipif->ipif_net_mask)) {
9934 return (EADDRNOTAVAIL);
9937 IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9940 if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
9941 return (0); /* No change */
9943 if (ipif->ipif_flags & IPIF_UP) {
9945 * If the interface is already marked up,
9946 * we call ipif_down which will take care
9947 * of ditching any IREs that have been set
9948 * up based on the old pp dst address.
9950 err = ipif_logical_down(ipif, q, mp);
9951 if (err == EINPROGRESS)
9952 return (err);
9953 (void) ipif_down_tail(ipif);
9954 need_up = B_TRUE;
9957 * could return EINPROGRESS. If so ioctl will complete in
9958 * ip_rput_dlpi_writer
9960 err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
9961 return (err);
9964 static int
9965 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9966 boolean_t need_up)
9968 in6_addr_t v6addr;
9969 ill_t *ill = ipif->ipif_ill;
9970 int err = 0;
9971 boolean_t need_dl_down;
9972 boolean_t need_arp_down;
9974 ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
9975 ipif->ipif_id, (void *)ipif));
9977 /* Must cancel any pending timer before taking the ill_lock */
9978 if (ipif->ipif_recovery_id != 0)
9979 (void) untimeout(ipif->ipif_recovery_id);
9980 ipif->ipif_recovery_id = 0;
9982 if (ipif->ipif_isv6) {
9983 sin6_t *sin6;
9985 sin6 = (sin6_t *)sin;
9986 v6addr = sin6->sin6_addr;
9987 } else {
9988 ipaddr_t addr;
9990 addr = sin->sin_addr.s_addr;
9991 IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9993 mutex_enter(&ill->ill_lock);
9994 /* Set point to point destination address. */
9995 if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
9997 * Allow this as a means of creating logical
9998 * pt-pt interfaces on top of e.g. an Ethernet.
9999 * XXX Undocumented HACK for testing.
10000 * pt-pt interfaces are created with NUD disabled.
10002 ipif->ipif_flags |= IPIF_POINTOPOINT;
10003 ipif->ipif_flags &= ~IPIF_BROADCAST;
10004 if (ipif->ipif_isv6)
10005 ill->ill_flags |= ILLF_NONUD;
10009 * If the interface was previously marked as a duplicate, then since
10010 * we've now got a "new" address, it should no longer be considered a
10011 * duplicate -- even if the "new" address is the same as the old one.
10012 * Note that if all ipifs are down, we may have a pending ARP down
10013 * event to handle.
10015 need_dl_down = need_arp_down = B_FALSE;
10016 if (ipif->ipif_flags & IPIF_DUPLICATE) {
10017 need_arp_down = !need_up;
10018 ipif->ipif_flags &= ~IPIF_DUPLICATE;
10019 if (--ill->ill_ipif_dup_count == 0 && !need_up &&
10020 ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
10021 need_dl_down = B_TRUE;
10026 * If we've just manually set the IPv6 destination link-local address
10027 * (0th ipif), tag the ill so that future updates to the destination
10028 * interface ID (as can happen with interfaces over IP tunnels) don't
10029 * result in this address getting automatically reconfigured from
10030 * under the administrator.
10032 if (ipif->ipif_isv6 && ipif->ipif_id == 0)
10033 ill->ill_manual_dst_linklocal = 1;
10035 /* Set the new address. */
10036 ipif->ipif_v6pp_dst_addr = v6addr;
10037 /* Make sure subnet tracks pp_dst */
10038 ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
10039 mutex_exit(&ill->ill_lock);
10041 if (need_up) {
10043 * Now bring the interface back up. If this
10044 * is the only IPIF for the ILL, ipif_up
10045 * will have to re-bind to the device, so
10046 * we may get back EINPROGRESS, in which
10047 * case, this IOCTL will get completed in
10048 * ip_rput_dlpi when we see the DL_BIND_ACK.
10050 err = ipif_up(ipif, q, mp);
10053 if (need_dl_down)
10054 ill_dl_down(ill);
10055 if (need_arp_down && !ipif->ipif_isv6)
10056 (void) ipif_arp_down(ipif);
10058 return (err);
10062 * Restart entry point to restart the dstaddress set operation after the
10063 * refcounts have dropped to zero.
10065 /* ARGSUSED */
10067 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10068 ip_ioctl_cmd_t *ipip, void *ifreq)
10070 ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
10071 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10072 (void) ipif_down_tail(ipif);
10073 return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
10076 /* ARGSUSED */
10078 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10079 ip_ioctl_cmd_t *ipip, void *if_req)
10081 sin6_t *sin6 = (struct sockaddr_in6 *)sin;
10083 ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
10084 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10086 * Get point to point destination address. The addresses can't
10087 * change since we hold a reference to the ipif.
10089 if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
10090 return (EADDRNOTAVAIL);
10092 if (ipif->ipif_isv6) {
10093 ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10094 *sin6 = sin6_null;
10095 sin6->sin6_family = AF_INET6;
10096 sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
10097 } else {
10098 *sin = sin_null;
10099 sin->sin_family = AF_INET;
10100 sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
10102 return (0);
10106 * Check which flags will change by the given flags being set
10107 * silently ignore flags which userland is not allowed to control.
10108 * (Because these flags may change between SIOCGLIFFLAGS and
10109 * SIOCSLIFFLAGS, and that's outside of userland's control,
10110 * we need to silently ignore them rather than fail.)
10112 static void
10113 ip_sioctl_flags_onoff(ipif_t *ipif, uint64_t flags, uint64_t *onp,
10114 uint64_t *offp)
10116 ill_t *ill = ipif->ipif_ill;
10117 phyint_t *phyi = ill->ill_phyint;
10118 uint64_t cantchange_flags, intf_flags;
10119 uint64_t turn_on, turn_off;
10121 intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
10122 cantchange_flags = IFF_CANTCHANGE;
10123 if (IS_IPMP(ill))
10124 cantchange_flags |= IFF_IPMP_CANTCHANGE;
10125 turn_on = (flags ^ intf_flags) & ~cantchange_flags;
10126 turn_off = intf_flags & turn_on;
10127 turn_on ^= turn_off;
10128 *onp = turn_on;
10129 *offp = turn_off;
10133 * Set interface flags. Many flags require special handling (e.g.,
10134 * bringing the interface down); see below for details.
10136 * NOTE : We really don't enforce that ipif_id zero should be used
10137 * for setting any flags other than IFF_LOGINT_FLAGS. This
10138 * is because applications generally does SICGLIFFLAGS and
10139 * ORs in the new flags (that affects the logical) and does a
10140 * SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
10141 * than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
10142 * flags that will be turned on is correct with respect to
10143 * ipif_id 0. For backward compatibility reasons, it is not done.
10145 /* ARGSUSED */
10147 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10148 ip_ioctl_cmd_t *ipip, void *if_req)
10150 uint64_t turn_on;
10151 uint64_t turn_off;
10152 int err = 0;
10153 phyint_t *phyi;
10154 ill_t *ill;
10155 conn_t *connp;
10156 uint64_t intf_flags;
10157 boolean_t phyint_flags_modified = B_FALSE;
10158 uint64_t flags;
10159 struct ifreq *ifr;
10160 struct lifreq *lifr;
10161 boolean_t set_linklocal = B_FALSE;
10163 ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
10164 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10166 ASSERT(IAM_WRITER_IPIF(ipif));
10168 ill = ipif->ipif_ill;
10169 phyi = ill->ill_phyint;
10171 if (ipip->ipi_cmd_type == IF_CMD) {
10172 ifr = (struct ifreq *)if_req;
10173 flags = (uint64_t)(ifr->ifr_flags & 0x0000ffff);
10174 } else {
10175 lifr = (struct lifreq *)if_req;
10176 flags = lifr->lifr_flags;
10179 intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
10182 * Have the flags been set correctly until now?
10184 ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10185 ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10186 ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10188 * Compare the new flags to the old, and partition
10189 * into those coming on and those going off.
10190 * For the 16 bit command keep the bits above bit 16 unchanged.
10192 if (ipip->ipi_cmd == SIOCSIFFLAGS)
10193 flags |= intf_flags & ~0xFFFF;
10196 * Explicitly fail attempts to change flags that are always invalid on
10197 * an IPMP meta-interface.
10199 if (IS_IPMP(ill) && ((flags ^ intf_flags) & IFF_IPMP_INVALID))
10200 return (EINVAL);
10202 ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10203 if ((turn_on|turn_off) == 0)
10204 return (0); /* No change */
10207 * All test addresses must be IFF_DEPRECATED (to ensure source address
10208 * selection avoids them) -- so force IFF_DEPRECATED on, and do not
10209 * allow it to be turned off.
10211 if ((turn_off & (IFF_DEPRECATED|IFF_NOFAILOVER)) == IFF_DEPRECATED &&
10212 (turn_on|intf_flags) & IFF_NOFAILOVER)
10213 return (EINVAL);
10215 if ((connp = Q_TO_CONN(q)) == NULL)
10216 return (EINVAL);
10219 * Only vrrp control socket is allowed to change IFF_UP and
10220 * IFF_NOACCEPT flags when IFF_VRRP is set.
10222 if ((intf_flags & IFF_VRRP) && ((turn_off | turn_on) & IFF_UP)) {
10223 if (!connp->conn_isvrrp)
10224 return (EINVAL);
10228 * The IFF_NOACCEPT flag can only be set on an IFF_VRRP IP address by
10229 * VRRP control socket.
10231 if ((turn_off | turn_on) & IFF_NOACCEPT) {
10232 if (!connp->conn_isvrrp || !(intf_flags & IFF_VRRP))
10233 return (EINVAL);
10236 if (turn_on & IFF_NOFAILOVER) {
10237 turn_on |= IFF_DEPRECATED;
10238 flags |= IFF_DEPRECATED;
10242 * On underlying interfaces, only allow applications to manage test
10243 * addresses -- otherwise, they may get confused when the address
10244 * moves as part of being brought up. Likewise, prevent an
10245 * application-managed test address from being converted to a data
10246 * address. To prevent migration of administratively up addresses in
10247 * the kernel, we don't allow them to be converted either.
10249 if (IS_UNDER_IPMP(ill)) {
10250 const uint64_t appflags = IFF_DHCPRUNNING | IFF_ADDRCONF;
10252 if ((turn_on & appflags) && !(flags & IFF_NOFAILOVER))
10253 return (EINVAL);
10255 if ((turn_off & IFF_NOFAILOVER) &&
10256 (flags & (appflags | IFF_UP | IFF_DUPLICATE)))
10257 return (EINVAL);
10261 * Only allow IFF_TEMPORARY flag to be set on
10262 * IPv6 interfaces.
10264 if ((turn_on & IFF_TEMPORARY) && !(ipif->ipif_isv6))
10265 return (EINVAL);
10268 * cannot turn off IFF_NOXMIT on VNI interfaces.
10270 if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
10271 return (EINVAL);
10274 * Don't allow the IFF_ROUTER flag to be turned on on loopback
10275 * interfaces. It makes no sense in that context.
10277 if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
10278 return (EINVAL);
10281 * For IPv6 ipif_id 0, don't allow the interface to be up without
10282 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
10283 * If the link local address isn't set, and can be set, it will get
10284 * set later on in this function.
10286 if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
10287 (flags & IFF_UP) && !(flags & (IFF_NOLOCAL|IFF_ANYCAST)) &&
10288 IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
10289 if (ipif_cant_setlinklocal(ipif))
10290 return (EINVAL);
10291 set_linklocal = B_TRUE;
10295 * If we modify physical interface flags, we'll potentially need to
10296 * send up two routing socket messages for the changes (one for the
10297 * IPv4 ill, and another for the IPv6 ill). Note that here.
10299 if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10300 phyint_flags_modified = B_TRUE;
10303 * All functioning PHYI_STANDBY interfaces start life PHYI_INACTIVE
10304 * (otherwise, we'd immediately use them, defeating standby). Also,
10305 * since PHYI_INACTIVE has a separate meaning when PHYI_STANDBY is not
10306 * set, don't allow PHYI_STANDBY to be set if PHYI_INACTIVE is already
10307 * set, and clear PHYI_INACTIVE if PHYI_STANDBY is being cleared. We
10308 * also don't allow PHYI_STANDBY if VNI is enabled since its semantics
10309 * will not be honored.
10311 if (turn_on & PHYI_STANDBY) {
10313 * No need to grab ill_g_usesrc_lock here; see the
10314 * synchronization notes in ip.c.
10316 if (ill->ill_usesrc_grp_next != NULL ||
10317 intf_flags & PHYI_INACTIVE)
10318 return (EINVAL);
10319 if (!(flags & PHYI_FAILED)) {
10320 flags |= PHYI_INACTIVE;
10321 turn_on |= PHYI_INACTIVE;
10325 if (turn_off & PHYI_STANDBY) {
10326 flags &= ~PHYI_INACTIVE;
10327 turn_off |= PHYI_INACTIVE;
10331 * PHYI_FAILED and PHYI_INACTIVE are mutually exclusive; fail if both
10332 * would end up on.
10334 if ((flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
10335 (PHYI_FAILED | PHYI_INACTIVE))
10336 return (EINVAL);
10339 * If ILLF_ROUTER changes, we need to change the ip forwarding
10340 * status of the interface.
10342 if ((turn_on | turn_off) & ILLF_ROUTER) {
10343 err = ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
10344 if (err != 0)
10345 return (err);
10349 * If the interface is not UP and we are not going to
10350 * bring it UP, record the flags and return. When the
10351 * interface comes UP later, the right actions will be
10352 * taken.
10354 if (!(ipif->ipif_flags & IPIF_UP) &&
10355 !(turn_on & IPIF_UP)) {
10356 /* Record new flags in their respective places. */
10357 mutex_enter(&ill->ill_lock);
10358 mutex_enter(&ill->ill_phyint->phyint_lock);
10359 ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10360 ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10361 ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10362 ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10363 phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10364 phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10365 mutex_exit(&ill->ill_lock);
10366 mutex_exit(&ill->ill_phyint->phyint_lock);
10369 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the
10370 * same to the kernel: if any of them has been set by
10371 * userland, the interface cannot be used for data traffic.
10373 if ((turn_on|turn_off) &
10374 (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10375 ASSERT(!IS_IPMP(ill));
10377 * It's possible the ill is part of an "anonymous"
10378 * IPMP group rather than a real group. In that case,
10379 * there are no other interfaces in the group and thus
10380 * no need to call ipmp_phyint_refresh_active().
10382 if (IS_UNDER_IPMP(ill))
10383 ipmp_phyint_refresh_active(phyi);
10386 if (phyint_flags_modified) {
10387 if (phyi->phyint_illv4 != NULL) {
10388 ip_rts_ifmsg(phyi->phyint_illv4->
10389 ill_ipif, RTSQ_DEFAULT);
10391 if (phyi->phyint_illv6 != NULL) {
10392 ip_rts_ifmsg(phyi->phyint_illv6->
10393 ill_ipif, RTSQ_DEFAULT);
10396 /* The default multicast interface might have changed */
10397 ire_increment_multicast_generation(ill->ill_ipst,
10398 ill->ill_isv6);
10400 return (0);
10401 } else if (set_linklocal) {
10402 mutex_enter(&ill->ill_lock);
10403 if (set_linklocal)
10404 ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
10405 mutex_exit(&ill->ill_lock);
10409 * Disallow IPv6 interfaces coming up that have the unspecified address,
10410 * or point-to-point interfaces with an unspecified destination. We do
10411 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
10412 * have a subnet assigned, which is how in.ndpd currently manages its
10413 * onlink prefix list when no addresses are configured with those
10414 * prefixes.
10416 if (ipif->ipif_isv6 &&
10417 ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
10418 (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
10419 IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
10420 ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10421 IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
10422 return (EINVAL);
10426 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
10427 * from being brought up.
10429 if (!ipif->ipif_isv6 &&
10430 ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10431 ipif->ipif_pp_dst_addr == INADDR_ANY)) {
10432 return (EINVAL);
10436 * If we are going to change one or more of the flags that are
10437 * IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL, ILLF_NOARP,
10438 * ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, IPIF_PREFERRED, and
10439 * IPIF_NOFAILOVER, we will take special action. This is
10440 * done by bring the ipif down, changing the flags and bringing
10441 * it back up again. For IPIF_NOFAILOVER, the act of bringing it
10442 * back up will trigger the address to be moved.
10444 * If we are going to change IFF_NOACCEPT, we need to bring
10445 * all the ipifs down then bring them up again. The act of
10446 * bringing all the ipifs back up will trigger the local
10447 * ires being recreated with "no_accept" set/cleared.
10449 * Note that ILLF_NOACCEPT is always set separately from the
10450 * other flags.
10452 if ((turn_on|turn_off) &
10453 (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
10454 ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED|
10455 IPIF_NOFAILOVER)) {
10457 * ipif_down() will ire_delete bcast ire's for the subnet,
10458 * while the ire_identical_ref tracks the case of IRE_BROADCAST
10459 * entries shared between multiple ipifs on the same subnet.
10461 if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
10462 !(turn_off & IPIF_UP)) {
10463 if (ipif->ipif_flags & IPIF_UP)
10464 ill->ill_logical_down = 1;
10465 turn_on &= ~IPIF_UP;
10467 err = ipif_down(ipif, q, mp);
10468 ip1dbg(("ipif_down returns %d err ", err));
10469 if (err == EINPROGRESS)
10470 return (err);
10471 (void) ipif_down_tail(ipif);
10472 } else if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10474 * If we can quiesce the ill, then continue. If not, then
10475 * ip_sioctl_flags_tail() will be called from
10476 * ipif_ill_refrele_tail().
10478 ill_down_ipifs(ill, B_TRUE);
10480 mutex_enter(&connp->conn_lock);
10481 mutex_enter(&ill->ill_lock);
10482 if (!ill_is_quiescent(ill)) {
10483 boolean_t success;
10485 success = ipsq_pending_mp_add(connp, ill->ill_ipif,
10486 q, mp, ILL_DOWN);
10487 mutex_exit(&ill->ill_lock);
10488 mutex_exit(&connp->conn_lock);
10489 return (success ? EINPROGRESS : EINTR);
10491 mutex_exit(&ill->ill_lock);
10492 mutex_exit(&connp->conn_lock);
10494 return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10497 static int
10498 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
10500 ill_t *ill;
10501 phyint_t *phyi;
10502 uint64_t turn_on, turn_off;
10503 boolean_t phyint_flags_modified = B_FALSE;
10504 int err = 0;
10505 boolean_t set_linklocal = B_FALSE;
10507 ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
10508 ipif->ipif_ill->ill_name, ipif->ipif_id));
10510 ASSERT(IAM_WRITER_IPIF(ipif));
10512 ill = ipif->ipif_ill;
10513 phyi = ill->ill_phyint;
10515 ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10518 * IFF_UP is handled separately.
10520 turn_on &= ~IFF_UP;
10521 turn_off &= ~IFF_UP;
10523 if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10524 phyint_flags_modified = B_TRUE;
10527 * Now we change the flags. Track current value of
10528 * other flags in their respective places.
10530 mutex_enter(&ill->ill_lock);
10531 mutex_enter(&phyi->phyint_lock);
10532 ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10533 ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10534 ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10535 ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10536 phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10537 phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10538 if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
10539 set_linklocal = B_TRUE;
10540 ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
10543 mutex_exit(&ill->ill_lock);
10544 mutex_exit(&phyi->phyint_lock);
10546 if (set_linklocal)
10547 (void) ipif_setlinklocal(ipif);
10550 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the same to
10551 * the kernel: if any of them has been set by userland, the interface
10552 * cannot be used for data traffic.
10554 if ((turn_on|turn_off) & (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10555 ASSERT(!IS_IPMP(ill));
10557 * It's possible the ill is part of an "anonymous" IPMP group
10558 * rather than a real group. In that case, there are no other
10559 * interfaces in the group and thus no need for us to call
10560 * ipmp_phyint_refresh_active().
10562 if (IS_UNDER_IPMP(ill))
10563 ipmp_phyint_refresh_active(phyi);
10566 if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10568 * If the ILLF_NOACCEPT flag is changed, bring up all the
10569 * ipifs that were brought down.
10571 * The routing sockets messages are sent as the result
10572 * of ill_up_ipifs(), further, SCTP's IPIF list was updated
10573 * as well.
10575 err = ill_up_ipifs(ill, q, mp);
10576 } else if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
10578 * XXX ipif_up really does not know whether a phyint flags
10579 * was modified or not. So, it sends up information on
10580 * only one routing sockets message. As we don't bring up
10581 * the interface and also set PHYI_ flags simultaneously
10582 * it should be okay.
10584 err = ipif_up(ipif, q, mp);
10585 } else {
10587 * Make sure routing socket sees all changes to the flags.
10588 * ipif_up_done* handles this when we use ipif_up.
10590 if (phyint_flags_modified) {
10591 if (phyi->phyint_illv4 != NULL) {
10592 ip_rts_ifmsg(phyi->phyint_illv4->
10593 ill_ipif, RTSQ_DEFAULT);
10595 if (phyi->phyint_illv6 != NULL) {
10596 ip_rts_ifmsg(phyi->phyint_illv6->
10597 ill_ipif, RTSQ_DEFAULT);
10599 } else {
10600 ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
10603 * Update the flags in SCTP's IPIF list, ipif_up() will do
10604 * this in need_up case.
10606 sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10609 /* The default multicast interface might have changed */
10610 ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
10611 return (err);
10615 * Restart the flags operation now that the refcounts have dropped to zero.
10617 /* ARGSUSED */
10619 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10620 ip_ioctl_cmd_t *ipip, void *if_req)
10622 uint64_t flags;
10623 struct ifreq *ifr = if_req;
10624 struct lifreq *lifr = if_req;
10625 uint64_t turn_on, turn_off;
10627 ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
10628 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10630 if (ipip->ipi_cmd_type == IF_CMD) {
10631 /* cast to uint16_t prevents unwanted sign extension */
10632 flags = (uint16_t)ifr->ifr_flags;
10633 } else {
10634 flags = lifr->lifr_flags;
10638 * If this function call is a result of the ILLF_NOACCEPT flag
10639 * change, do not call ipif_down_tail(). See ip_sioctl_flags().
10641 ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10642 if (!((turn_on|turn_off) & ILLF_NOACCEPT))
10643 (void) ipif_down_tail(ipif);
10645 return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10649 * Can operate on either a module or a driver queue.
10651 /* ARGSUSED */
10653 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10654 ip_ioctl_cmd_t *ipip, void *if_req)
10657 * Has the flags been set correctly till now ?
10659 ill_t *ill = ipif->ipif_ill;
10660 phyint_t *phyi = ill->ill_phyint;
10662 ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
10663 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10664 ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10665 ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10666 ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10669 * Need a lock since some flags can be set even when there are
10670 * references to the ipif.
10672 mutex_enter(&ill->ill_lock);
10673 if (ipip->ipi_cmd_type == IF_CMD) {
10674 struct ifreq *ifr = (struct ifreq *)if_req;
10676 /* Get interface flags (low 16 only). */
10677 ifr->ifr_flags = ((ipif->ipif_flags |
10678 ill->ill_flags | phyi->phyint_flags) & 0xffff);
10679 } else {
10680 struct lifreq *lifr = (struct lifreq *)if_req;
10682 /* Get interface flags. */
10683 lifr->lifr_flags = ipif->ipif_flags |
10684 ill->ill_flags | phyi->phyint_flags;
10686 mutex_exit(&ill->ill_lock);
10687 return (0);
10691 * We allow the MTU to be set on an ILL, but not have it be different
10692 * for different IPIFs since we don't actually send packets on IPIFs.
10694 /* ARGSUSED */
10696 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10697 ip_ioctl_cmd_t *ipip, void *if_req)
10699 int mtu;
10700 int ip_min_mtu;
10701 struct ifreq *ifr;
10702 struct lifreq *lifr;
10703 ill_t *ill;
10705 ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
10706 ipif->ipif_id, (void *)ipif));
10707 if (ipip->ipi_cmd_type == IF_CMD) {
10708 ifr = (struct ifreq *)if_req;
10709 mtu = ifr->ifr_metric;
10710 } else {
10711 lifr = (struct lifreq *)if_req;
10712 mtu = lifr->lifr_mtu;
10714 /* Only allow for logical unit zero i.e. not on "bge0:17" */
10715 if (ipif->ipif_id != 0)
10716 return (EINVAL);
10718 ill = ipif->ipif_ill;
10719 if (ipif->ipif_isv6)
10720 ip_min_mtu = IPV6_MIN_MTU;
10721 else
10722 ip_min_mtu = IP_MIN_MTU;
10724 mutex_enter(&ill->ill_lock);
10725 if (mtu > ill->ill_max_frag || mtu < ip_min_mtu) {
10726 mutex_exit(&ill->ill_lock);
10727 return (EINVAL);
10729 /* Avoid increasing ill_mc_mtu */
10730 if (ill->ill_mc_mtu > mtu)
10731 ill->ill_mc_mtu = mtu;
10734 * The dce and fragmentation code can handle changes to ill_mtu
10735 * concurrent with sending/fragmenting packets.
10737 ill->ill_mtu = mtu;
10738 ill->ill_flags |= ILLF_FIXEDMTU;
10739 mutex_exit(&ill->ill_lock);
10742 * Make sure all dce_generation checks find out
10743 * that ill_mtu/ill_mc_mtu has changed.
10745 dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
10748 * Refresh IPMP meta-interface MTU if necessary.
10750 if (IS_UNDER_IPMP(ill))
10751 ipmp_illgrp_refresh_mtu(ill->ill_grp);
10753 /* Update the MTU in SCTP's list */
10754 sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10755 return (0);
10758 /* Get interface MTU. */
10759 /* ARGSUSED */
10761 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10762 ip_ioctl_cmd_t *ipip, void *if_req)
10764 struct ifreq *ifr;
10765 struct lifreq *lifr;
10767 ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
10768 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10771 * We allow a get on any logical interface even though the set
10772 * can only be done on logical unit 0.
10774 if (ipip->ipi_cmd_type == IF_CMD) {
10775 ifr = (struct ifreq *)if_req;
10776 ifr->ifr_metric = ipif->ipif_ill->ill_mtu;
10777 } else {
10778 lifr = (struct lifreq *)if_req;
10779 lifr->lifr_mtu = ipif->ipif_ill->ill_mtu;
10781 return (0);
10784 /* Set interface broadcast address. */
10785 /* ARGSUSED2 */
10787 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10788 ip_ioctl_cmd_t *ipip, void *if_req)
10790 ipaddr_t addr;
10791 ire_t *ire;
10792 ill_t *ill = ipif->ipif_ill;
10793 ip_stack_t *ipst = ill->ill_ipst;
10795 ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ill->ill_name,
10796 ipif->ipif_id));
10798 ASSERT(IAM_WRITER_IPIF(ipif));
10799 if (!(ipif->ipif_flags & IPIF_BROADCAST))
10800 return (EADDRNOTAVAIL);
10802 ASSERT(!(ipif->ipif_isv6)); /* No IPv6 broadcast */
10804 if (sin->sin_family != AF_INET)
10805 return (EAFNOSUPPORT);
10807 addr = sin->sin_addr.s_addr;
10809 if (ipif->ipif_flags & IPIF_UP) {
10811 * If we are already up, make sure the new
10812 * broadcast address makes sense. If it does,
10813 * there should be an IRE for it already.
10815 ire = ire_ftable_lookup_v4(addr, 0, 0, IRE_BROADCAST,
10816 ill, ipif->ipif_zoneid,
10817 (MATCH_IRE_ILL | MATCH_IRE_TYPE), 0, ipst, NULL);
10818 if (ire == NULL) {
10819 return (EINVAL);
10820 } else {
10821 ire_refrele(ire);
10825 * Changing the broadcast addr for this ipif. Since the IRE_BROADCAST
10826 * needs to already exist we never need to change the set of
10827 * IRE_BROADCASTs when we are UP.
10829 if (addr != ipif->ipif_brd_addr)
10830 IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
10832 return (0);
10835 /* Get interface broadcast address. */
10836 /* ARGSUSED */
10838 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10839 ip_ioctl_cmd_t *ipip, void *if_req)
10841 ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
10842 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10843 if (!(ipif->ipif_flags & IPIF_BROADCAST))
10844 return (EADDRNOTAVAIL);
10846 /* IPIF_BROADCAST not possible with IPv6 */
10847 ASSERT(!ipif->ipif_isv6);
10848 *sin = sin_null;
10849 sin->sin_family = AF_INET;
10850 sin->sin_addr.s_addr = ipif->ipif_brd_addr;
10851 return (0);
10855 * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
10857 /* ARGSUSED */
10859 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10860 ip_ioctl_cmd_t *ipip, void *if_req)
10862 int err = 0;
10863 in6_addr_t v6mask;
10865 ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
10866 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10868 ASSERT(IAM_WRITER_IPIF(ipif));
10870 if (ipif->ipif_isv6) {
10871 sin6_t *sin6;
10873 if (sin->sin_family != AF_INET6)
10874 return (EAFNOSUPPORT);
10876 sin6 = (sin6_t *)sin;
10877 v6mask = sin6->sin6_addr;
10878 } else {
10879 ipaddr_t mask;
10881 if (sin->sin_family != AF_INET)
10882 return (EAFNOSUPPORT);
10884 mask = sin->sin_addr.s_addr;
10885 if (!ip_contiguous_mask(ntohl(mask)))
10886 return (ENOTSUP);
10887 V4MASK_TO_V6(mask, v6mask);
10891 * No big deal if the interface isn't already up, or the mask
10892 * isn't really changing, or this is pt-pt.
10894 if (!(ipif->ipif_flags & IPIF_UP) ||
10895 IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
10896 (ipif->ipif_flags & IPIF_POINTOPOINT)) {
10897 ipif->ipif_v6net_mask = v6mask;
10898 if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10899 V6_MASK_COPY(ipif->ipif_v6lcl_addr,
10900 ipif->ipif_v6net_mask,
10901 ipif->ipif_v6subnet);
10903 return (0);
10906 * Make sure we have valid net and subnet broadcast ire's
10907 * for the old netmask, if needed by other logical interfaces.
10909 err = ipif_logical_down(ipif, q, mp);
10910 if (err == EINPROGRESS)
10911 return (err);
10912 (void) ipif_down_tail(ipif);
10913 err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
10914 return (err);
10917 static int
10918 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
10920 in6_addr_t v6mask;
10921 int err = 0;
10923 ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
10924 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10926 if (ipif->ipif_isv6) {
10927 sin6_t *sin6;
10929 sin6 = (sin6_t *)sin;
10930 v6mask = sin6->sin6_addr;
10931 } else {
10932 ipaddr_t mask;
10934 mask = sin->sin_addr.s_addr;
10935 V4MASK_TO_V6(mask, v6mask);
10938 ipif->ipif_v6net_mask = v6mask;
10939 if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10940 V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
10941 ipif->ipif_v6subnet);
10943 err = ipif_up(ipif, q, mp);
10945 if (err == 0 || err == EINPROGRESS) {
10947 * The interface must be DL_BOUND if this packet has to
10948 * go out on the wire. Since we only go through a logical
10949 * down and are bound with the driver during an internal
10950 * down/up that is satisfied.
10952 if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
10953 /* Potentially broadcast an address mask reply. */
10954 ipif_mask_reply(ipif);
10957 return (err);
10960 /* ARGSUSED */
10962 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10963 ip_ioctl_cmd_t *ipip, void *if_req)
10965 ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
10966 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10967 (void) ipif_down_tail(ipif);
10968 return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
10971 /* Get interface net mask. */
10972 /* ARGSUSED */
10974 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10975 ip_ioctl_cmd_t *ipip, void *if_req)
10977 struct lifreq *lifr = (struct lifreq *)if_req;
10978 struct sockaddr_in6 *sin6 = (sin6_t *)sin;
10980 ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
10981 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10984 * net mask can't change since we have a reference to the ipif.
10986 if (ipif->ipif_isv6) {
10987 ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10988 *sin6 = sin6_null;
10989 sin6->sin6_family = AF_INET6;
10990 sin6->sin6_addr = ipif->ipif_v6net_mask;
10991 lifr->lifr_addrlen =
10992 ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
10993 } else {
10994 *sin = sin_null;
10995 sin->sin_family = AF_INET;
10996 sin->sin_addr.s_addr = ipif->ipif_net_mask;
10997 if (ipip->ipi_cmd_type == LIF_CMD) {
10998 lifr->lifr_addrlen =
10999 ip_mask_to_plen(ipif->ipif_net_mask);
11002 return (0);
11005 /* ARGSUSED */
11007 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11008 ip_ioctl_cmd_t *ipip, void *if_req)
11010 ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
11011 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11014 * Since no applications should ever be setting metrics on underlying
11015 * interfaces, we explicitly fail to smoke 'em out.
11017 if (IS_UNDER_IPMP(ipif->ipif_ill))
11018 return (EINVAL);
11021 * Set interface metric. We don't use this for
11022 * anything but we keep track of it in case it is
11023 * important to routing applications or such.
11025 if (ipip->ipi_cmd_type == IF_CMD) {
11026 struct ifreq *ifr;
11028 ifr = (struct ifreq *)if_req;
11029 ipif->ipif_ill->ill_metric = ifr->ifr_metric;
11030 } else {
11031 struct lifreq *lifr;
11033 lifr = (struct lifreq *)if_req;
11034 ipif->ipif_ill->ill_metric = lifr->lifr_metric;
11036 return (0);
11039 /* ARGSUSED */
11041 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11042 ip_ioctl_cmd_t *ipip, void *if_req)
11044 /* Get interface metric. */
11045 ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
11046 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11048 if (ipip->ipi_cmd_type == IF_CMD) {
11049 struct ifreq *ifr;
11051 ifr = (struct ifreq *)if_req;
11052 ifr->ifr_metric = ipif->ipif_ill->ill_metric;
11053 } else {
11054 struct lifreq *lifr;
11056 lifr = (struct lifreq *)if_req;
11057 lifr->lifr_metric = ipif->ipif_ill->ill_metric;
11060 return (0);
11063 /* ARGSUSED */
11065 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11066 ip_ioctl_cmd_t *ipip, void *if_req)
11068 int arp_muxid;
11070 ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
11071 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11073 * Set the muxid returned from I_PLINK.
11075 if (ipip->ipi_cmd_type == IF_CMD) {
11076 struct ifreq *ifr = (struct ifreq *)if_req;
11078 ipif->ipif_ill->ill_muxid = ifr->ifr_ip_muxid;
11079 arp_muxid = ifr->ifr_arp_muxid;
11080 } else {
11081 struct lifreq *lifr = (struct lifreq *)if_req;
11083 ipif->ipif_ill->ill_muxid = lifr->lifr_ip_muxid;
11084 arp_muxid = lifr->lifr_arp_muxid;
11086 arl_set_muxid(ipif->ipif_ill, arp_muxid);
11087 return (0);
11090 /* ARGSUSED */
11092 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11093 ip_ioctl_cmd_t *ipip, void *if_req)
11095 int arp_muxid = 0;
11097 ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
11098 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11100 * Get the muxid saved in ill for I_PUNLINK.
11102 arp_muxid = arl_get_muxid(ipif->ipif_ill);
11103 if (ipip->ipi_cmd_type == IF_CMD) {
11104 struct ifreq *ifr = (struct ifreq *)if_req;
11106 ifr->ifr_ip_muxid = ipif->ipif_ill->ill_muxid;
11107 ifr->ifr_arp_muxid = arp_muxid;
11108 } else {
11109 struct lifreq *lifr = (struct lifreq *)if_req;
11111 lifr->lifr_ip_muxid = ipif->ipif_ill->ill_muxid;
11112 lifr->lifr_arp_muxid = arp_muxid;
11114 return (0);
11118 * Set the subnet prefix. Does not modify the broadcast address.
11120 /* ARGSUSED */
11122 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11123 ip_ioctl_cmd_t *ipip, void *if_req)
11125 int err = 0;
11126 in6_addr_t v6addr;
11127 in6_addr_t v6mask;
11128 boolean_t need_up = B_FALSE;
11129 int addrlen;
11131 ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
11132 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11134 ASSERT(IAM_WRITER_IPIF(ipif));
11135 addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
11137 if (ipif->ipif_isv6) {
11138 sin6_t *sin6;
11140 if (sin->sin_family != AF_INET6)
11141 return (EAFNOSUPPORT);
11143 sin6 = (sin6_t *)sin;
11144 v6addr = sin6->sin6_addr;
11145 if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
11146 return (EADDRNOTAVAIL);
11147 } else {
11148 ipaddr_t addr;
11150 if (sin->sin_family != AF_INET)
11151 return (EAFNOSUPPORT);
11153 addr = sin->sin_addr.s_addr;
11154 if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
11155 return (EADDRNOTAVAIL);
11156 IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11157 /* Add 96 bits */
11158 addrlen += IPV6_ABITS - IP_ABITS;
11161 if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
11162 return (EINVAL);
11164 /* Check if bits in the address is set past the mask */
11165 if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
11166 return (EINVAL);
11168 if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
11169 IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
11170 return (0); /* No change */
11172 if (ipif->ipif_flags & IPIF_UP) {
11174 * If the interface is already marked up,
11175 * we call ipif_down which will take care
11176 * of ditching any IREs that have been set
11177 * up based on the old interface address.
11179 err = ipif_logical_down(ipif, q, mp);
11180 if (err == EINPROGRESS)
11181 return (err);
11182 (void) ipif_down_tail(ipif);
11183 need_up = B_TRUE;
11186 err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
11187 return (err);
11190 static int
11191 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
11192 queue_t *q, mblk_t *mp, boolean_t need_up)
11194 ill_t *ill = ipif->ipif_ill;
11195 int err = 0;
11197 ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
11198 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11200 /* Set the new address. */
11201 mutex_enter(&ill->ill_lock);
11202 ipif->ipif_v6net_mask = v6mask;
11203 if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11204 V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
11205 ipif->ipif_v6subnet);
11207 mutex_exit(&ill->ill_lock);
11209 if (need_up) {
11211 * Now bring the interface back up. If this
11212 * is the only IPIF for the ILL, ipif_up
11213 * will have to re-bind to the device, so
11214 * we may get back EINPROGRESS, in which
11215 * case, this IOCTL will get completed in
11216 * ip_rput_dlpi when we see the DL_BIND_ACK.
11218 err = ipif_up(ipif, q, mp);
11219 if (err == EINPROGRESS)
11220 return (err);
11222 return (err);
11225 /* ARGSUSED */
11227 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11228 ip_ioctl_cmd_t *ipip, void *if_req)
11230 int addrlen;
11231 in6_addr_t v6addr;
11232 in6_addr_t v6mask;
11233 struct lifreq *lifr = (struct lifreq *)if_req;
11235 ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
11236 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11237 (void) ipif_down_tail(ipif);
11239 addrlen = lifr->lifr_addrlen;
11240 if (ipif->ipif_isv6) {
11241 sin6_t *sin6;
11243 sin6 = (sin6_t *)sin;
11244 v6addr = sin6->sin6_addr;
11245 } else {
11246 ipaddr_t addr;
11248 addr = sin->sin_addr.s_addr;
11249 IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11250 addrlen += IPV6_ABITS - IP_ABITS;
11252 (void) ip_plen_to_mask_v6(addrlen, &v6mask);
11254 return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
11257 /* ARGSUSED */
11259 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11260 ip_ioctl_cmd_t *ipip, void *if_req)
11262 struct lifreq *lifr = (struct lifreq *)if_req;
11263 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
11265 ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
11266 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11267 ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11269 if (ipif->ipif_isv6) {
11270 *sin6 = sin6_null;
11271 sin6->sin6_family = AF_INET6;
11272 sin6->sin6_addr = ipif->ipif_v6subnet;
11273 lifr->lifr_addrlen =
11274 ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11275 } else {
11276 *sin = sin_null;
11277 sin->sin_family = AF_INET;
11278 sin->sin_addr.s_addr = ipif->ipif_subnet;
11279 lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
11281 return (0);
11285 * Set the IPv6 address token.
11287 /* ARGSUSED */
11289 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11290 ip_ioctl_cmd_t *ipi, void *if_req)
11292 ill_t *ill = ipif->ipif_ill;
11293 int err;
11294 in6_addr_t v6addr;
11295 in6_addr_t v6mask;
11296 boolean_t need_up = B_FALSE;
11297 int i;
11298 sin6_t *sin6 = (sin6_t *)sin;
11299 struct lifreq *lifr = (struct lifreq *)if_req;
11300 int addrlen;
11302 ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
11303 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11304 ASSERT(IAM_WRITER_IPIF(ipif));
11306 addrlen = lifr->lifr_addrlen;
11307 /* Only allow for logical unit zero i.e. not on "le0:17" */
11308 if (ipif->ipif_id != 0)
11309 return (EINVAL);
11311 if (!ipif->ipif_isv6)
11312 return (EINVAL);
11314 if (addrlen > IPV6_ABITS)
11315 return (EINVAL);
11317 v6addr = sin6->sin6_addr;
11320 * The length of the token is the length from the end. To get
11321 * the proper mask for this, compute the mask of the bits not
11322 * in the token; ie. the prefix, and then xor to get the mask.
11324 if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
11325 return (EINVAL);
11326 for (i = 0; i < 4; i++) {
11327 v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11330 if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
11331 ill->ill_token_length == addrlen)
11332 return (0); /* No change */
11334 if (ipif->ipif_flags & IPIF_UP) {
11335 err = ipif_logical_down(ipif, q, mp);
11336 if (err == EINPROGRESS)
11337 return (err);
11338 (void) ipif_down_tail(ipif);
11339 need_up = B_TRUE;
11341 err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
11342 return (err);
11345 static int
11346 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
11347 mblk_t *mp, boolean_t need_up)
11349 in6_addr_t v6addr;
11350 in6_addr_t v6mask;
11351 ill_t *ill = ipif->ipif_ill;
11352 int i;
11353 int err = 0;
11355 ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
11356 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11357 v6addr = sin6->sin6_addr;
11359 * The length of the token is the length from the end. To get
11360 * the proper mask for this, compute the mask of the bits not
11361 * in the token; ie. the prefix, and then xor to get the mask.
11363 (void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
11364 for (i = 0; i < 4; i++)
11365 v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11367 mutex_enter(&ill->ill_lock);
11368 V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
11369 ill->ill_token_length = addrlen;
11370 ill->ill_manual_token = 1;
11372 /* Reconfigure the link-local address based on this new token */
11373 ipif_setlinklocal(ill->ill_ipif);
11375 mutex_exit(&ill->ill_lock);
11377 if (need_up) {
11379 * Now bring the interface back up. If this
11380 * is the only IPIF for the ILL, ipif_up
11381 * will have to re-bind to the device, so
11382 * we may get back EINPROGRESS, in which
11383 * case, this IOCTL will get completed in
11384 * ip_rput_dlpi when we see the DL_BIND_ACK.
11386 err = ipif_up(ipif, q, mp);
11387 if (err == EINPROGRESS)
11388 return (err);
11390 return (err);
11393 /* ARGSUSED */
11395 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11396 ip_ioctl_cmd_t *ipi, void *if_req)
11398 ill_t *ill;
11399 sin6_t *sin6 = (sin6_t *)sin;
11400 struct lifreq *lifr = (struct lifreq *)if_req;
11402 ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
11403 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11404 if (ipif->ipif_id != 0)
11405 return (EINVAL);
11407 ill = ipif->ipif_ill;
11408 if (!ill->ill_isv6)
11409 return (ENXIO);
11411 *sin6 = sin6_null;
11412 sin6->sin6_family = AF_INET6;
11413 ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
11414 sin6->sin6_addr = ill->ill_token;
11415 lifr->lifr_addrlen = ill->ill_token_length;
11416 return (0);
11420 * Set (hardware) link specific information that might override
11421 * what was acquired through the DL_INFO_ACK.
11423 /* ARGSUSED */
11425 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11426 ip_ioctl_cmd_t *ipi, void *if_req)
11428 ill_t *ill = ipif->ipif_ill;
11429 int ip_min_mtu;
11430 struct lifreq *lifr = (struct lifreq *)if_req;
11431 lif_ifinfo_req_t *lir;
11433 ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
11434 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11435 lir = &lifr->lifr_ifinfo;
11436 ASSERT(IAM_WRITER_IPIF(ipif));
11438 /* Only allow for logical unit zero i.e. not on "bge0:17" */
11439 if (ipif->ipif_id != 0)
11440 return (EINVAL);
11442 /* Set interface MTU. */
11443 if (ipif->ipif_isv6)
11444 ip_min_mtu = IPV6_MIN_MTU;
11445 else
11446 ip_min_mtu = IP_MIN_MTU;
11449 * Verify values before we set anything. Allow zero to
11450 * mean unspecified.
11452 * XXX We should be able to set the user-defined lir_mtu to some value
11453 * that is greater than ill_current_frag but less than ill_max_frag- the
11454 * ill_max_frag value tells us the max MTU that can be handled by the
11455 * datalink, whereas the ill_current_frag is dynamically computed for
11456 * some link-types like tunnels, based on the tunnel PMTU. However,
11457 * since there is currently no way of distinguishing between
11458 * administratively fixed link mtu values (e.g., those set via
11459 * /sbin/dladm) and dynamically discovered MTUs (e.g., those discovered
11460 * for tunnels) we conservatively choose the ill_current_frag as the
11461 * upper-bound.
11463 if (lir->lir_maxmtu != 0 &&
11464 (lir->lir_maxmtu > ill->ill_current_frag ||
11465 lir->lir_maxmtu < ip_min_mtu))
11466 return (EINVAL);
11467 if (lir->lir_reachtime != 0 &&
11468 lir->lir_reachtime > ND_MAX_REACHTIME)
11469 return (EINVAL);
11470 if (lir->lir_reachretrans != 0 &&
11471 lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
11472 return (EINVAL);
11474 mutex_enter(&ill->ill_lock);
11476 * The dce and fragmentation code can handle changes to ill_mtu
11477 * concurrent with sending/fragmenting packets.
11479 if (lir->lir_maxmtu != 0)
11480 ill->ill_user_mtu = lir->lir_maxmtu;
11482 if (lir->lir_reachtime != 0)
11483 ill->ill_reachable_time = lir->lir_reachtime;
11485 if (lir->lir_reachretrans != 0)
11486 ill->ill_reachable_retrans_time = lir->lir_reachretrans;
11488 ill->ill_max_hops = lir->lir_maxhops;
11489 ill->ill_max_buf = ND_MAX_Q;
11490 if (!(ill->ill_flags & ILLF_FIXEDMTU) && ill->ill_user_mtu != 0) {
11492 * ill_mtu is the actual interface MTU, obtained as the min
11493 * of user-configured mtu and the value announced by the
11494 * driver (via DL_NOTE_SDU_SIZE/DL_INFO_ACK). Note that since
11495 * we have already made the choice of requiring
11496 * ill_user_mtu < ill_current_frag by the time we get here,
11497 * the ill_mtu effectively gets assigned to the ill_user_mtu
11498 * here.
11500 ill->ill_mtu = MIN(ill->ill_current_frag, ill->ill_user_mtu);
11501 ill->ill_mc_mtu = MIN(ill->ill_mc_mtu, ill->ill_user_mtu);
11503 mutex_exit(&ill->ill_lock);
11506 * Make sure all dce_generation checks find out
11507 * that ill_mtu/ill_mc_mtu has changed.
11509 if (!(ill->ill_flags & ILLF_FIXEDMTU) && (lir->lir_maxmtu != 0))
11510 dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
11513 * Refresh IPMP meta-interface MTU if necessary.
11515 if (IS_UNDER_IPMP(ill))
11516 ipmp_illgrp_refresh_mtu(ill->ill_grp);
11518 return (0);
11521 /* ARGSUSED */
11523 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11524 ip_ioctl_cmd_t *ipi, void *if_req)
11526 struct lif_ifinfo_req *lir;
11527 ill_t *ill = ipif->ipif_ill;
11529 ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
11530 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11531 if (ipif->ipif_id != 0)
11532 return (EINVAL);
11534 lir = &((struct lifreq *)if_req)->lifr_ifinfo;
11535 lir->lir_maxhops = ill->ill_max_hops;
11536 lir->lir_reachtime = ill->ill_reachable_time;
11537 lir->lir_reachretrans = ill->ill_reachable_retrans_time;
11538 lir->lir_maxmtu = ill->ill_mtu;
11540 return (0);
11544 * Return best guess as to the subnet mask for the specified address.
11545 * Based on the subnet masks for all the configured interfaces.
11547 * We end up returning a zero mask in the case of default, multicast or
11548 * experimental.
11550 static ipaddr_t
11551 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
11553 ipaddr_t net_mask;
11554 ill_t *ill;
11555 ipif_t *ipif;
11556 ill_walk_context_t ctx;
11557 ipif_t *fallback_ipif = NULL;
11559 net_mask = ip_net_mask(addr);
11560 if (net_mask == 0) {
11561 *ipifp = NULL;
11562 return (0);
11565 /* Let's check to see if this is maybe a local subnet route. */
11566 /* this function only applies to IPv4 interfaces */
11567 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
11568 ill = ILL_START_WALK_V4(&ctx, ipst);
11569 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
11570 mutex_enter(&ill->ill_lock);
11571 for (ipif = ill->ill_ipif; ipif != NULL;
11572 ipif = ipif->ipif_next) {
11573 if (IPIF_IS_CONDEMNED(ipif))
11574 continue;
11575 if (!(ipif->ipif_flags & IPIF_UP))
11576 continue;
11577 if ((ipif->ipif_subnet & net_mask) ==
11578 (addr & net_mask)) {
11580 * Don't trust pt-pt interfaces if there are
11581 * other interfaces.
11583 if (ipif->ipif_flags & IPIF_POINTOPOINT) {
11584 if (fallback_ipif == NULL) {
11585 ipif_refhold_locked(ipif);
11586 fallback_ipif = ipif;
11588 continue;
11592 * Fine. Just assume the same net mask as the
11593 * directly attached subnet interface is using.
11595 ipif_refhold_locked(ipif);
11596 mutex_exit(&ill->ill_lock);
11597 rw_exit(&ipst->ips_ill_g_lock);
11598 if (fallback_ipif != NULL)
11599 ipif_refrele(fallback_ipif);
11600 *ipifp = ipif;
11601 return (ipif->ipif_net_mask);
11604 mutex_exit(&ill->ill_lock);
11606 rw_exit(&ipst->ips_ill_g_lock);
11608 *ipifp = fallback_ipif;
11609 return ((fallback_ipif != NULL) ?
11610 fallback_ipif->ipif_net_mask : net_mask);
11614 * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
11616 static void
11617 ip_wput_ioctl(queue_t *q, mblk_t *mp)
11619 IOCP iocp;
11620 ipft_t *ipft;
11621 ipllc_t *ipllc;
11622 mblk_t *mp1;
11623 cred_t *cr;
11624 int error = 0;
11625 conn_t *connp;
11627 ip1dbg(("ip_wput_ioctl"));
11628 iocp = (IOCP)mp->b_rptr;
11629 mp1 = mp->b_cont;
11630 if (mp1 == NULL) {
11631 iocp->ioc_error = EINVAL;
11632 mp->b_datap->db_type = M_IOCNAK;
11633 iocp->ioc_count = 0;
11634 qreply(q, mp);
11635 return;
11639 * These IOCTLs provide various control capabilities to
11640 * upstream agents such as ULPs and processes. There
11641 * are currently two such IOCTLs implemented. They
11642 * are used by TCP to provide update information for
11643 * existing IREs and to forcibly delete an IRE for a
11644 * host that is not responding, thereby forcing an
11645 * attempt at a new route.
11647 iocp->ioc_error = EINVAL;
11648 if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
11649 goto done;
11651 ipllc = (ipllc_t *)mp1->b_rptr;
11652 for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
11653 if (ipllc->ipllc_cmd == ipft->ipft_cmd)
11654 break;
11657 * prefer credential from mblk over ioctl;
11658 * see ip_sioctl_copyin_setup
11660 cr = msg_getcred(mp, NULL);
11661 if (cr == NULL)
11662 cr = iocp->ioc_cr;
11665 * Refhold the conn in case the request gets queued up in some lookup
11667 ASSERT(CONN_Q(q));
11668 connp = Q_TO_CONN(q);
11669 CONN_INC_REF(connp);
11670 CONN_INC_IOCTLREF(connp);
11671 if (ipft->ipft_pfi &&
11672 ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
11673 pullupmsg(mp1, ipft->ipft_min_size))) {
11674 error = (*ipft->ipft_pfi)(q,
11675 (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
11677 if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
11679 * CONN_OPER_PENDING_DONE happens in the function called
11680 * through ipft_pfi above.
11682 return;
11685 CONN_DEC_IOCTLREF(connp);
11686 CONN_OPER_PENDING_DONE(connp);
11687 if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
11688 freemsg(mp);
11689 return;
11691 iocp->ioc_error = error;
11693 done:
11694 mp->b_datap->db_type = M_IOCACK;
11695 if (iocp->ioc_error)
11696 iocp->ioc_count = 0;
11697 qreply(q, mp);
11701 * Assign a unique id for the ipif. This is used by sctp_addr.c
11702 * Note: remove if sctp_addr.c is redone to not shadow ill/ipif data structures.
11704 static void
11705 ipif_assign_seqid(ipif_t *ipif)
11707 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
11709 ipif->ipif_seqid = atomic_inc_64_nv(&ipst->ips_ipif_g_seqid);
11713 * Clone the contents of `sipif' to `dipif'. Requires that both ipifs are
11714 * administratively down (i.e., no DAD), of the same type, and locked. Note
11715 * that the clone is complete -- including the seqid -- and the expectation is
11716 * that the caller will either free or overwrite `sipif' before it's unlocked.
11718 static void
11719 ipif_clone(const ipif_t *sipif, ipif_t *dipif)
11721 ASSERT(MUTEX_HELD(&sipif->ipif_ill->ill_lock));
11722 ASSERT(MUTEX_HELD(&dipif->ipif_ill->ill_lock));
11723 ASSERT(!(sipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11724 ASSERT(!(dipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11725 ASSERT(sipif->ipif_ire_type == dipif->ipif_ire_type);
11727 dipif->ipif_flags = sipif->ipif_flags;
11728 dipif->ipif_zoneid = sipif->ipif_zoneid;
11729 dipif->ipif_v6subnet = sipif->ipif_v6subnet;
11730 dipif->ipif_v6lcl_addr = sipif->ipif_v6lcl_addr;
11731 dipif->ipif_v6net_mask = sipif->ipif_v6net_mask;
11732 dipif->ipif_v6brd_addr = sipif->ipif_v6brd_addr;
11733 dipif->ipif_v6pp_dst_addr = sipif->ipif_v6pp_dst_addr;
11736 * As per the comment atop the function, we assume that these sipif
11737 * fields will be changed before sipif is unlocked.
11739 dipif->ipif_seqid = sipif->ipif_seqid;
11740 dipif->ipif_state_flags = sipif->ipif_state_flags;
11744 * Transfer the contents of `sipif' to `dipif', and then free (if `virgipif'
11745 * is NULL) or overwrite `sipif' with `virgipif', which must be a virgin
11746 * (unreferenced) ipif. Also, if `sipif' is used by the current xop, then
11747 * transfer the xop to `dipif'. Requires that all ipifs are administratively
11748 * down (i.e., no DAD), of the same type, and unlocked.
11750 static void
11751 ipif_transfer(ipif_t *sipif, ipif_t *dipif, ipif_t *virgipif)
11753 ipsq_t *ipsq = sipif->ipif_ill->ill_phyint->phyint_ipsq;
11754 ipxop_t *ipx = ipsq->ipsq_xop;
11756 ASSERT(sipif != dipif);
11757 ASSERT(sipif != virgipif);
11760 * Grab all of the locks that protect the ipif in a defined order.
11762 GRAB_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11764 ipif_clone(sipif, dipif);
11765 if (virgipif != NULL) {
11766 ipif_clone(virgipif, sipif);
11767 mi_free(virgipif);
11770 RELEASE_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11773 * Transfer ownership of the current xop, if necessary.
11775 if (ipx->ipx_current_ipif == sipif) {
11776 ASSERT(ipx->ipx_pending_ipif == NULL);
11777 mutex_enter(&ipx->ipx_lock);
11778 ipx->ipx_current_ipif = dipif;
11779 mutex_exit(&ipx->ipx_lock);
11782 if (virgipif == NULL)
11783 mi_free(sipif);
11787 * checks if:
11788 * - <ill_name>:<ipif_id> is at most LIFNAMSIZ - 1 and
11789 * - logical interface is within the allowed range
11791 static int
11792 is_lifname_valid(ill_t *ill, unsigned int ipif_id)
11794 if (snprintf(NULL, 0, "%s:%d", ill->ill_name, ipif_id) >= LIFNAMSIZ)
11795 return (ENAMETOOLONG);
11797 if (ipif_id >= ill->ill_ipst->ips_ip_addrs_per_if)
11798 return (ERANGE);
11799 return (0);
11803 * Insert the ipif, so that the list of ipifs on the ill will be sorted
11804 * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
11805 * be inserted into the first space available in the list. The value of
11806 * ipif_id will then be set to the appropriate value for its position.
11808 static int
11809 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock)
11811 ill_t *ill;
11812 ipif_t *tipif;
11813 ipif_t **tipifp;
11814 int id, err;
11815 ip_stack_t *ipst;
11817 ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
11818 IAM_WRITER_IPIF(ipif));
11820 ill = ipif->ipif_ill;
11821 ASSERT(ill != NULL);
11822 ipst = ill->ill_ipst;
11825 * In the case of lo0:0 we already hold the ill_g_lock.
11826 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
11827 * ipif_insert.
11829 if (acquire_g_lock)
11830 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
11831 mutex_enter(&ill->ill_lock);
11832 id = ipif->ipif_id;
11833 tipifp = &(ill->ill_ipif);
11834 if (id == -1) { /* need to find a real id */
11835 id = 0;
11836 while ((tipif = *tipifp) != NULL) {
11837 ASSERT(tipif->ipif_id >= id);
11838 if (tipif->ipif_id != id)
11839 break; /* non-consecutive id */
11840 id++;
11841 tipifp = &(tipif->ipif_next);
11843 if ((err = is_lifname_valid(ill, id)) != 0) {
11844 mutex_exit(&ill->ill_lock);
11845 if (acquire_g_lock)
11846 rw_exit(&ipst->ips_ill_g_lock);
11847 return (err);
11849 ipif->ipif_id = id; /* assign new id */
11850 } else if ((err = is_lifname_valid(ill, id)) == 0) {
11851 /* we have a real id; insert ipif in the right place */
11852 while ((tipif = *tipifp) != NULL) {
11853 ASSERT(tipif->ipif_id != id);
11854 if (tipif->ipif_id > id)
11855 break; /* found correct location */
11856 tipifp = &(tipif->ipif_next);
11858 } else {
11859 mutex_exit(&ill->ill_lock);
11860 if (acquire_g_lock)
11861 rw_exit(&ipst->ips_ill_g_lock);
11862 return (err);
11865 ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
11867 ipif->ipif_next = tipif;
11868 *tipifp = ipif;
11869 mutex_exit(&ill->ill_lock);
11870 if (acquire_g_lock)
11871 rw_exit(&ipst->ips_ill_g_lock);
11873 return (0);
11876 static void
11877 ipif_remove(ipif_t *ipif)
11879 ipif_t **ipifp;
11880 ill_t *ill = ipif->ipif_ill;
11882 ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
11884 mutex_enter(&ill->ill_lock);
11885 ipifp = &ill->ill_ipif;
11886 for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
11887 if (*ipifp == ipif) {
11888 *ipifp = ipif->ipif_next;
11889 break;
11892 mutex_exit(&ill->ill_lock);
11896 * Allocate and initialize a new interface control structure. (Always
11897 * called as writer.)
11898 * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
11899 * is not part of the global linked list of ills. ipif_seqid is unique
11900 * in the system and to preserve the uniqueness, it is assigned only
11901 * when ill becomes part of the global list. At that point ill will
11902 * have a name. If it doesn't get assigned here, it will get assigned
11903 * in ipif_set_values() as part of SIOCSLIFNAME processing.
11904 * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
11905 * the interface flags or any other information from the DL_INFO_ACK for
11906 * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
11907 * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
11908 * second DL_INFO_ACK comes in from the driver.
11910 static ipif_t *
11911 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize,
11912 boolean_t insert, int *errorp)
11914 int err;
11915 ipif_t *ipif;
11916 ip_stack_t *ipst = ill->ill_ipst;
11918 ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
11919 ill->ill_name, id, (void *)ill));
11920 ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
11922 if (errorp != NULL)
11923 *errorp = 0;
11925 if ((ipif = mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL) {
11926 if (errorp != NULL)
11927 *errorp = ENOMEM;
11928 return (NULL);
11930 *ipif = ipif_zero; /* start clean */
11932 ipif->ipif_ill = ill;
11933 ipif->ipif_id = id; /* could be -1 */
11935 * Inherit the zoneid from the ill; for the shared stack instance
11936 * this is always the global zone
11938 ipif->ipif_zoneid = ill->ill_zoneid;
11940 ipif->ipif_refcnt = 0;
11942 if (insert) {
11943 if ((err = ipif_insert(ipif, ire_type != IRE_LOOPBACK)) != 0) {
11944 mi_free(ipif);
11945 if (errorp != NULL)
11946 *errorp = err;
11947 return (NULL);
11949 /* -1 id should have been replaced by real id */
11950 id = ipif->ipif_id;
11951 ASSERT(id >= 0);
11954 if (ill->ill_name[0] != '\0')
11955 ipif_assign_seqid(ipif);
11958 * If this is the zeroth ipif on the IPMP ill, create the illgrp
11959 * (which must not exist yet because the zeroth ipif is created once
11960 * per ill). However, do not not link it to the ipmp_grp_t until
11961 * I_PLINK is called; see ip_sioctl_plink_ipmp() for details.
11963 if (id == 0 && IS_IPMP(ill)) {
11964 if (ipmp_illgrp_create(ill) == NULL) {
11965 if (insert) {
11966 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
11967 ipif_remove(ipif);
11968 rw_exit(&ipst->ips_ill_g_lock);
11970 mi_free(ipif);
11971 if (errorp != NULL)
11972 *errorp = ENOMEM;
11973 return (NULL);
11978 * We grab ill_lock to protect the flag changes. The ipif is still
11979 * not up and can't be looked up until the ioctl completes and the
11980 * IPIF_CHANGING flag is cleared.
11982 mutex_enter(&ill->ill_lock);
11984 ipif->ipif_ire_type = ire_type;
11986 if (ipif->ipif_isv6) {
11987 ill->ill_flags |= ILLF_IPV6;
11988 } else {
11989 ipaddr_t inaddr_any = INADDR_ANY;
11991 ill->ill_flags |= ILLF_IPV4;
11993 /* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
11994 IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11995 &ipif->ipif_v6lcl_addr);
11996 IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11997 &ipif->ipif_v6subnet);
11998 IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11999 &ipif->ipif_v6net_mask);
12000 IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12001 &ipif->ipif_v6brd_addr);
12002 IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12003 &ipif->ipif_v6pp_dst_addr);
12007 * Don't set the interface flags etc. now, will do it in
12008 * ip_ll_subnet_defaults.
12010 if (!initialize)
12011 goto out;
12014 * NOTE: The IPMP meta-interface is special-cased because it starts
12015 * with no underlying interfaces (and thus an unknown broadcast
12016 * address length), but all interfaces that can be placed into an IPMP
12017 * group are required to be broadcast-capable.
12019 if (ill->ill_bcast_addr_length != 0 || IS_IPMP(ill)) {
12021 * Later detect lack of DLPI driver multicast capability by
12022 * catching DL_ENABMULTI_REQ errors in ip_rput_dlpi().
12024 ill->ill_flags |= ILLF_MULTICAST;
12025 if (!ipif->ipif_isv6)
12026 ipif->ipif_flags |= IPIF_BROADCAST;
12027 } else {
12028 if (ill->ill_net_type != IRE_LOOPBACK) {
12029 if (ipif->ipif_isv6)
12031 * Note: xresolv interfaces will eventually need
12032 * NOARP set here as well, but that will require
12033 * those external resolvers to have some
12034 * knowledge of that flag and act appropriately.
12035 * Not to be changed at present.
12037 ill->ill_flags |= ILLF_NONUD;
12038 else
12039 ill->ill_flags |= ILLF_NOARP;
12041 if (ill->ill_phys_addr_length == 0) {
12042 if (IS_VNI(ill)) {
12043 ipif->ipif_flags |= IPIF_NOXMIT;
12044 } else {
12045 /* pt-pt supports multicast. */
12046 ill->ill_flags |= ILLF_MULTICAST;
12047 if (ill->ill_net_type != IRE_LOOPBACK)
12048 ipif->ipif_flags |= IPIF_POINTOPOINT;
12052 out:
12053 mutex_exit(&ill->ill_lock);
12054 return (ipif);
12058 * Remove the neighbor cache entries associated with this logical
12059 * interface.
12062 ipif_arp_down(ipif_t *ipif)
12064 ill_t *ill = ipif->ipif_ill;
12065 int err = 0;
12067 ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
12068 ASSERT(IAM_WRITER_IPIF(ipif));
12070 DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_down",
12071 ill_t *, ill, ipif_t *, ipif);
12072 ipif_nce_down(ipif);
12075 * If this is the last ipif that is going down and there are no
12076 * duplicate addresses we may yet attempt to re-probe, then we need to
12077 * clean up ARP completely.
12079 if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
12080 !ill->ill_logical_down && ill->ill_net_type == IRE_IF_RESOLVER) {
12082 * If this was the last ipif on an IPMP interface, purge any
12083 * static ARP entries associated with it.
12085 if (IS_IPMP(ill))
12086 ipmp_illgrp_refresh_arpent(ill->ill_grp);
12088 /* UNBIND, DETACH */
12089 err = arp_ll_down(ill);
12092 return (err);
12096 * Get the resolver set up for a new IP address. (Always called as writer.)
12097 * Called both for IPv4 and IPv6 interfaces, though it only does some
12098 * basic DAD related initialization for IPv6. Honors ILLF_NOARP.
12100 * The enumerated value res_act tunes the behavior:
12101 * * Res_act_initial: set up all the resolver structures for a new
12102 * IP address.
12103 * * Res_act_defend: tell ARP that it needs to send a single gratuitous
12104 * ARP message in defense of the address.
12105 * * Res_act_rebind: tell ARP to change the hardware address for an IP
12106 * address (and issue gratuitous ARPs). Used by ipmp_ill_bind_ipif().
12108 * Returns zero on success, or an errno upon failure.
12111 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
12113 ill_t *ill = ipif->ipif_ill;
12114 int err;
12115 boolean_t was_dup;
12117 ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
12118 ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
12119 ASSERT(IAM_WRITER_IPIF(ipif));
12121 was_dup = B_FALSE;
12122 if (res_act == Res_act_initial) {
12123 ipif->ipif_addr_ready = 0;
12125 * We're bringing an interface up here. There's no way that we
12126 * should need to shut down ARP now.
12128 mutex_enter(&ill->ill_lock);
12129 if (ipif->ipif_flags & IPIF_DUPLICATE) {
12130 ipif->ipif_flags &= ~IPIF_DUPLICATE;
12131 ill->ill_ipif_dup_count--;
12132 was_dup = B_TRUE;
12134 mutex_exit(&ill->ill_lock);
12136 if (ipif->ipif_recovery_id != 0)
12137 (void) untimeout(ipif->ipif_recovery_id);
12138 ipif->ipif_recovery_id = 0;
12139 if (ill->ill_net_type != IRE_IF_RESOLVER) {
12140 ipif->ipif_addr_ready = 1;
12141 return (0);
12143 /* NDP will set the ipif_addr_ready flag when it's ready */
12144 if (ill->ill_isv6)
12145 return (0);
12147 err = ipif_arp_up(ipif, res_act, was_dup);
12148 return (err);
12152 * This routine restarts IPv4/IPv6 duplicate address detection (DAD)
12153 * when a link has just gone back up.
12155 static void
12156 ipif_nce_start_dad(ipif_t *ipif)
12158 ncec_t *ncec;
12159 ill_t *ill = ipif->ipif_ill;
12160 boolean_t isv6 = ill->ill_isv6;
12162 if (isv6) {
12163 ncec = ncec_lookup_illgrp_v6(ipif->ipif_ill,
12164 &ipif->ipif_v6lcl_addr);
12165 } else {
12166 ipaddr_t v4addr;
12168 if (ill->ill_net_type != IRE_IF_RESOLVER ||
12169 (ipif->ipif_flags & IPIF_UNNUMBERED) ||
12170 ipif->ipif_lcl_addr == INADDR_ANY) {
12172 * If we can't contact ARP for some reason,
12173 * that's not really a problem. Just send
12174 * out the routing socket notification that
12175 * DAD completion would have done, and continue.
12177 ipif_mask_reply(ipif);
12178 ipif_up_notify(ipif);
12179 ipif->ipif_addr_ready = 1;
12180 return;
12183 IN6_V4MAPPED_TO_IPADDR(&ipif->ipif_v6lcl_addr, v4addr);
12184 ncec = ncec_lookup_illgrp_v4(ipif->ipif_ill, &v4addr);
12187 if (ncec == NULL) {
12188 ip1dbg(("couldn't find ncec for ipif %p leaving !ready\n",
12189 (void *)ipif));
12190 return;
12192 if (!nce_restart_dad(ncec)) {
12194 * If we can't restart DAD for some reason, that's not really a
12195 * problem. Just send out the routing socket notification that
12196 * DAD completion would have done, and continue.
12198 ipif_up_notify(ipif);
12199 ipif->ipif_addr_ready = 1;
12201 ncec_refrele(ncec);
12205 * Restart duplicate address detection on all interfaces on the given ill.
12207 * This is called when an interface transitions from down to up
12208 * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
12210 * Note that since the underlying physical link has transitioned, we must cause
12211 * at least one routing socket message to be sent here, either via DAD
12212 * completion or just by default on the first ipif. (If we don't do this, then
12213 * in.mpathd will see long delays when doing link-based failure recovery.)
12215 void
12216 ill_restart_dad(ill_t *ill, boolean_t went_up)
12218 ipif_t *ipif;
12220 if (ill == NULL)
12221 return;
12224 * If layer two doesn't support duplicate address detection, then just
12225 * send the routing socket message now and be done with it.
12227 if (!ill->ill_isv6 && arp_no_defense) {
12228 ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
12229 return;
12232 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12233 if (went_up) {
12235 if (ipif->ipif_flags & IPIF_UP) {
12236 ipif_nce_start_dad(ipif);
12237 } else if (ipif->ipif_flags & IPIF_DUPLICATE) {
12239 * kick off the bring-up process now.
12241 ipif_do_recovery(ipif);
12242 } else {
12244 * Unfortunately, the first ipif is "special"
12245 * and represents the underlying ill in the
12246 * routing socket messages. Thus, when this
12247 * one ipif is down, we must still notify so
12248 * that the user knows the IFF_RUNNING status
12249 * change. (If the first ipif is up, then
12250 * we'll handle eventual routing socket
12251 * notification via DAD completion.)
12253 if (ipif == ill->ill_ipif) {
12254 ip_rts_ifmsg(ill->ill_ipif,
12255 RTSQ_DEFAULT);
12258 } else {
12260 * After link down, we'll need to send a new routing
12261 * message when the link comes back, so clear
12262 * ipif_addr_ready.
12264 ipif->ipif_addr_ready = 0;
12269 * If we've torn down links, then notify the user right away.
12271 if (!went_up)
12272 ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
12275 static void
12276 ipsq_delete(ipsq_t *ipsq)
12278 ipxop_t *ipx = ipsq->ipsq_xop;
12280 ipsq->ipsq_ipst = NULL;
12281 ASSERT(ipsq->ipsq_phyint == NULL);
12282 ASSERT(ipsq->ipsq_xop != NULL);
12283 ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipx->ipx_mphead == NULL);
12284 ASSERT(ipx->ipx_pending_mp == NULL);
12285 kmem_free(ipsq, sizeof (ipsq_t));
12288 static int
12289 ill_up_ipifs_on_ill(ill_t *ill, queue_t *q, mblk_t *mp)
12291 int err = 0;
12292 ipif_t *ipif;
12294 if (ill == NULL)
12295 return (0);
12297 ASSERT(IAM_WRITER_ILL(ill));
12298 ill->ill_up_ipifs = B_TRUE;
12299 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12300 if (ipif->ipif_was_up) {
12301 if (!(ipif->ipif_flags & IPIF_UP))
12302 err = ipif_up(ipif, q, mp);
12303 ipif->ipif_was_up = B_FALSE;
12304 if (err != 0) {
12305 ASSERT(err == EINPROGRESS);
12306 return (err);
12310 ill->ill_up_ipifs = B_FALSE;
12311 return (0);
12315 * This function is called to bring up all the ipifs that were up before
12316 * bringing the ill down via ill_down_ipifs().
12319 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
12321 int err;
12323 ASSERT(IAM_WRITER_ILL(ill));
12325 if (ill->ill_replumbing) {
12326 ill->ill_replumbing = 0;
12328 * Send down REPLUMB_DONE notification followed by the
12329 * BIND_REQ on the arp stream.
12331 if (!ill->ill_isv6)
12332 arp_send_replumb_conf(ill);
12334 err = ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv4, q, mp);
12335 if (err != 0)
12336 return (err);
12338 return (ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv6, q, mp));
12342 * Bring down any IPIF_UP ipifs on ill. If "logical" is B_TRUE, we bring
12343 * down the ipifs without sending DL_UNBIND_REQ to the driver.
12345 static void
12346 ill_down_ipifs(ill_t *ill, boolean_t logical)
12348 ipif_t *ipif;
12350 ASSERT(IAM_WRITER_ILL(ill));
12352 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12354 * We go through the ipif_down logic even if the ipif
12355 * is already down, since routes can be added based
12356 * on down ipifs. Going through ipif_down once again
12357 * will delete any IREs created based on these routes.
12359 if (ipif->ipif_flags & IPIF_UP)
12360 ipif->ipif_was_up = B_TRUE;
12362 if (logical) {
12363 (void) ipif_logical_down(ipif, NULL, NULL);
12364 ipif_non_duplicate(ipif);
12365 (void) ipif_down_tail(ipif);
12366 } else {
12367 (void) ipif_down(ipif, NULL, NULL);
12373 * Redo source address selection. This makes IXAF_VERIFY_SOURCE take
12374 * a look again at valid source addresses.
12375 * This should be called each time after the set of source addresses has been
12376 * changed.
12378 void
12379 ip_update_source_selection(ip_stack_t *ipst)
12381 /* We skip past SRC_GENERATION_VERIFY */
12382 if (atomic_inc_32_nv(&ipst->ips_src_generation) ==
12383 SRC_GENERATION_VERIFY)
12384 atomic_inc_32(&ipst->ips_src_generation);
12388 * Finish the group join started in ip_sioctl_groupname().
12390 /* ARGSUSED */
12391 static void
12392 ip_join_illgrps(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
12394 ill_t *ill = q->q_ptr;
12395 phyint_t *phyi = ill->ill_phyint;
12396 ipmp_grp_t *grp = phyi->phyint_grp;
12397 ip_stack_t *ipst = ill->ill_ipst;
12399 /* IS_UNDER_IPMP() won't work until ipmp_ill_join_illgrp() is called */
12400 ASSERT(!IS_IPMP(ill) && grp != NULL);
12401 ASSERT(IAM_WRITER_IPSQ(ipsq));
12403 if (phyi->phyint_illv4 != NULL) {
12404 rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12405 VERIFY(grp->gr_pendv4-- > 0);
12406 rw_exit(&ipst->ips_ipmp_lock);
12407 ipmp_ill_join_illgrp(phyi->phyint_illv4, grp->gr_v4);
12409 if (phyi->phyint_illv6 != NULL) {
12410 rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12411 VERIFY(grp->gr_pendv6-- > 0);
12412 rw_exit(&ipst->ips_ipmp_lock);
12413 ipmp_ill_join_illgrp(phyi->phyint_illv6, grp->gr_v6);
12415 freemsg(mp);
12419 * Process an SIOCSLIFGROUPNAME request.
12421 /* ARGSUSED */
12423 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12424 ip_ioctl_cmd_t *ipip, void *ifreq)
12426 struct lifreq *lifr = ifreq;
12427 ill_t *ill = ipif->ipif_ill;
12428 ip_stack_t *ipst = ill->ill_ipst;
12429 phyint_t *phyi = ill->ill_phyint;
12430 ipmp_grp_t *grp = phyi->phyint_grp;
12431 mblk_t *ipsq_mp;
12432 int err = 0;
12435 * Note that phyint_grp can only change here, where we're exclusive.
12437 ASSERT(IAM_WRITER_ILL(ill));
12439 if (ipif->ipif_id != 0 || ill->ill_usesrc_grp_next != NULL ||
12440 (phyi->phyint_flags & PHYI_VIRTUAL))
12441 return (EINVAL);
12443 lifr->lifr_groupname[LIFGRNAMSIZ - 1] = '\0';
12445 rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12448 * If the name hasn't changed, there's nothing to do.
12450 if (grp != NULL && strcmp(grp->gr_name, lifr->lifr_groupname) == 0)
12451 goto unlock;
12454 * Handle requests to rename an IPMP meta-interface.
12456 * Note that creation of the IPMP meta-interface is handled in
12457 * userland through the standard plumbing sequence. As part of the
12458 * plumbing the IPMP meta-interface, its initial groupname is set to
12459 * the name of the interface (see ipif_set_values_tail()).
12461 if (IS_IPMP(ill)) {
12462 err = ipmp_grp_rename(grp, lifr->lifr_groupname);
12463 goto unlock;
12467 * Handle requests to add or remove an IP interface from a group.
12469 if (lifr->lifr_groupname[0] != '\0') { /* add */
12471 * Moves are handled by first removing the interface from
12472 * its existing group, and then adding it to another group.
12473 * So, fail if it's already in a group.
12475 if (IS_UNDER_IPMP(ill)) {
12476 err = EALREADY;
12477 goto unlock;
12480 grp = ipmp_grp_lookup(lifr->lifr_groupname, ipst);
12481 if (grp == NULL) {
12482 err = ENOENT;
12483 goto unlock;
12487 * Check if the phyint and its ills are suitable for
12488 * inclusion into the group.
12490 if ((err = ipmp_grp_vet_phyint(grp, phyi)) != 0)
12491 goto unlock;
12494 * Checks pass; join the group, and enqueue the remaining
12495 * illgrp joins for when we've become part of the group xop
12496 * and are exclusive across its IPSQs. Since qwriter_ip()
12497 * requires an mblk_t to scribble on, and since `mp' will be
12498 * freed as part of completing the ioctl, allocate another.
12500 if ((ipsq_mp = allocb(0, BPRI_MED)) == NULL) {
12501 err = ENOMEM;
12502 goto unlock;
12506 * Before we drop ipmp_lock, bump gr_pend* to ensure that the
12507 * IPMP meta-interface ills needed by `phyi' cannot go away
12508 * before ip_join_illgrps() is called back. See the comments
12509 * in ip_sioctl_plink_ipmp() for more.
12511 if (phyi->phyint_illv4 != NULL)
12512 grp->gr_pendv4++;
12513 if (phyi->phyint_illv6 != NULL)
12514 grp->gr_pendv6++;
12516 rw_exit(&ipst->ips_ipmp_lock);
12518 ipmp_phyint_join_grp(phyi, grp);
12519 ill_refhold(ill);
12520 qwriter_ip(ill, ill->ill_rq, ipsq_mp, ip_join_illgrps,
12521 SWITCH_OP, B_FALSE);
12522 return (0);
12523 } else {
12525 * Request to remove the interface from a group. If the
12526 * interface is not in a group, this trivially succeeds.
12528 rw_exit(&ipst->ips_ipmp_lock);
12529 if (IS_UNDER_IPMP(ill))
12530 ipmp_phyint_leave_grp(phyi);
12531 return (0);
12533 unlock:
12534 rw_exit(&ipst->ips_ipmp_lock);
12535 return (err);
12539 * Process an SIOCGLIFBINDING request.
12541 /* ARGSUSED */
12543 ip_sioctl_get_binding(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12544 ip_ioctl_cmd_t *ipip, void *ifreq)
12546 ill_t *ill;
12547 struct lifreq *lifr = ifreq;
12548 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
12550 if (!IS_IPMP(ipif->ipif_ill))
12551 return (EINVAL);
12553 rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12554 if ((ill = ipif->ipif_bound_ill) == NULL)
12555 lifr->lifr_binding[0] = '\0';
12556 else
12557 (void) strlcpy(lifr->lifr_binding, ill->ill_name, LIFNAMSIZ);
12558 rw_exit(&ipst->ips_ipmp_lock);
12559 return (0);
12563 * Process an SIOCGLIFGROUPNAME request.
12565 /* ARGSUSED */
12567 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12568 ip_ioctl_cmd_t *ipip, void *ifreq)
12570 ipmp_grp_t *grp;
12571 struct lifreq *lifr = ifreq;
12572 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
12574 rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12575 if ((grp = ipif->ipif_ill->ill_phyint->phyint_grp) == NULL)
12576 lifr->lifr_groupname[0] = '\0';
12577 else
12578 (void) strlcpy(lifr->lifr_groupname, grp->gr_name, LIFGRNAMSIZ);
12579 rw_exit(&ipst->ips_ipmp_lock);
12580 return (0);
12584 * Process an SIOCGLIFGROUPINFO request.
12586 /* ARGSUSED */
12588 ip_sioctl_groupinfo(ipif_t *dummy_ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12589 ip_ioctl_cmd_t *ipip, void *dummy)
12591 ipmp_grp_t *grp;
12592 lifgroupinfo_t *lifgr;
12593 ip_stack_t *ipst = CONNQ_TO_IPST(q);
12595 /* ip_wput_nondata() verified mp->b_cont->b_cont */
12596 lifgr = (lifgroupinfo_t *)mp->b_cont->b_cont->b_rptr;
12597 lifgr->gi_grname[LIFGRNAMSIZ - 1] = '\0';
12599 rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12600 if ((grp = ipmp_grp_lookup(lifgr->gi_grname, ipst)) == NULL) {
12601 rw_exit(&ipst->ips_ipmp_lock);
12602 return (ENOENT);
12604 ipmp_grp_info(grp, lifgr);
12605 rw_exit(&ipst->ips_ipmp_lock);
12606 return (0);
12609 static void
12610 ill_dl_down(ill_t *ill)
12612 DTRACE_PROBE2(ill__downup, char *, "ill_dl_down", ill_t *, ill);
12615 * The ill is down; unbind but stay attached since we're still
12616 * associated with a PPA. If we have negotiated DLPI capabilites
12617 * with the data link service provider (IDS_OK) then reset them.
12618 * The interval between unbinding and rebinding is potentially
12619 * unbounded hence we cannot assume things will be the same.
12620 * The DLPI capabilities will be probed again when the data link
12621 * is brought up.
12623 mblk_t *mp = ill->ill_unbind_mp;
12625 ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
12627 if (!ill->ill_replumbing) {
12628 /* Free all ilms for this ill */
12629 update_conn_ill(ill, ill->ill_ipst);
12630 } else {
12631 ill_leave_multicast(ill);
12634 ill->ill_unbind_mp = NULL;
12635 if (mp != NULL) {
12636 ip1dbg(("ill_dl_down: %s (%u) for %s\n",
12637 dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
12638 ill->ill_name));
12639 mutex_enter(&ill->ill_lock);
12640 ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
12641 mutex_exit(&ill->ill_lock);
12643 * ip_rput does not pass up normal (M_PROTO) DLPI messages
12644 * after ILL_CONDEMNED is set. So in the unplumb case, we call
12645 * ill_capability_dld_disable disable rightaway. If this is not
12646 * an unplumb operation then the disable happens on receipt of
12647 * the capab ack via ip_rput_dlpi_writer ->
12648 * ill_capability_ack_thr. In both cases the order of
12649 * the operations seen by DLD is capability disable followed
12650 * by DL_UNBIND. Also the DLD capability disable needs a
12651 * cv_wait'able context.
12653 if (ill->ill_state_flags & ILL_CONDEMNED)
12654 ill_capability_dld_disable(ill);
12655 ill_capability_reset(ill, B_FALSE);
12656 ill_dlpi_send(ill, mp);
12658 mutex_enter(&ill->ill_lock);
12659 ill->ill_dl_up = 0;
12660 ill_nic_event_dispatch(ill, 0, NE_DOWN, NULL, 0);
12661 mutex_exit(&ill->ill_lock);
12664 void
12665 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
12667 union DL_primitives *dlp;
12668 t_uscalar_t prim;
12669 boolean_t waitack = B_FALSE;
12671 ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12673 dlp = (union DL_primitives *)mp->b_rptr;
12674 prim = dlp->dl_primitive;
12676 ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
12677 dl_primstr(prim), prim, ill->ill_name));
12679 switch (prim) {
12680 case DL_PHYS_ADDR_REQ:
12682 dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
12683 ill->ill_phys_addr_pend = dlpap->dl_addr_type;
12684 break;
12686 case DL_BIND_REQ:
12687 mutex_enter(&ill->ill_lock);
12688 ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
12689 mutex_exit(&ill->ill_lock);
12690 break;
12694 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
12695 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
12696 * we only wait for the ACK of the DL_UNBIND_REQ.
12698 mutex_enter(&ill->ill_lock);
12699 if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
12700 (prim == DL_UNBIND_REQ)) {
12701 ill->ill_dlpi_pending = prim;
12702 waitack = B_TRUE;
12705 mutex_exit(&ill->ill_lock);
12706 DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_dispatch",
12707 char *, dl_primstr(prim), ill_t *, ill);
12708 putnext(ill->ill_wq, mp);
12711 * There is no ack for DL_NOTIFY_CONF messages
12713 if (waitack && prim == DL_NOTIFY_CONF)
12714 ill_dlpi_done(ill, prim);
12718 * Helper function for ill_dlpi_send().
12720 /* ARGSUSED */
12721 static void
12722 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
12724 ill_dlpi_send(q->q_ptr, mp);
12728 * Send a DLPI control message to the driver but make sure there
12729 * is only one outstanding message. Uses ill_dlpi_pending to tell
12730 * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
12731 * when an ACK or a NAK is received to process the next queued message.
12733 void
12734 ill_dlpi_send(ill_t *ill, mblk_t *mp)
12736 mblk_t **mpp;
12738 ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12741 * To ensure that any DLPI requests for current exclusive operation
12742 * are always completely sent before any DLPI messages for other
12743 * operations, require writer access before enqueuing.
12745 if (!IAM_WRITER_ILL(ill)) {
12746 ill_refhold(ill);
12747 /* qwriter_ip() does the ill_refrele() */
12748 qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
12749 NEW_OP, B_TRUE);
12750 return;
12753 mutex_enter(&ill->ill_lock);
12754 if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
12755 /* Must queue message. Tail insertion */
12756 mpp = &ill->ill_dlpi_deferred;
12757 while (*mpp != NULL)
12758 mpp = &((*mpp)->b_next);
12760 ip1dbg(("ill_dlpi_send: deferring request for %s "
12761 "while %s pending\n", ill->ill_name,
12762 dl_primstr(ill->ill_dlpi_pending)));
12764 *mpp = mp;
12765 mutex_exit(&ill->ill_lock);
12766 return;
12768 mutex_exit(&ill->ill_lock);
12769 ill_dlpi_dispatch(ill, mp);
12772 void
12773 ill_capability_send(ill_t *ill, mblk_t *mp)
12775 ill->ill_capab_pending_cnt++;
12776 ill_dlpi_send(ill, mp);
12779 void
12780 ill_capability_done(ill_t *ill)
12782 ASSERT(ill->ill_capab_pending_cnt != 0);
12784 ill_dlpi_done(ill, DL_CAPABILITY_REQ);
12786 ill->ill_capab_pending_cnt--;
12787 if (ill->ill_capab_pending_cnt == 0 &&
12788 ill->ill_dlpi_capab_state == IDCS_OK)
12789 ill_capability_reset_alloc(ill);
12793 * Send all deferred DLPI messages without waiting for their ACKs.
12795 void
12796 ill_dlpi_send_deferred(ill_t *ill)
12798 mblk_t *mp, *nextmp;
12801 * Clear ill_dlpi_pending so that the message is not queued in
12802 * ill_dlpi_send().
12804 mutex_enter(&ill->ill_lock);
12805 ill->ill_dlpi_pending = DL_PRIM_INVAL;
12806 mp = ill->ill_dlpi_deferred;
12807 ill->ill_dlpi_deferred = NULL;
12808 mutex_exit(&ill->ill_lock);
12810 for (; mp != NULL; mp = nextmp) {
12811 nextmp = mp->b_next;
12812 mp->b_next = NULL;
12813 ill_dlpi_send(ill, mp);
12818 * Clear all the deferred DLPI messages. Called on receiving an M_ERROR
12819 * or M_HANGUP
12821 static void
12822 ill_dlpi_clear_deferred(ill_t *ill)
12824 mblk_t *mp, *nextmp;
12826 mutex_enter(&ill->ill_lock);
12827 ill->ill_dlpi_pending = DL_PRIM_INVAL;
12828 mp = ill->ill_dlpi_deferred;
12829 ill->ill_dlpi_deferred = NULL;
12830 mutex_exit(&ill->ill_lock);
12832 for (; mp != NULL; mp = nextmp) {
12833 nextmp = mp->b_next;
12834 inet_freemsg(mp);
12839 * Check if the DLPI primitive `prim' is pending; print a warning if not.
12841 boolean_t
12842 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
12844 t_uscalar_t pending;
12846 mutex_enter(&ill->ill_lock);
12847 if (ill->ill_dlpi_pending == prim) {
12848 mutex_exit(&ill->ill_lock);
12849 return (B_TRUE);
12853 * During teardown, ill_dlpi_dispatch() will send DLPI requests
12854 * without waiting, so don't print any warnings in that case.
12856 if (ill->ill_state_flags & ILL_CONDEMNED) {
12857 mutex_exit(&ill->ill_lock);
12858 return (B_FALSE);
12860 pending = ill->ill_dlpi_pending;
12861 mutex_exit(&ill->ill_lock);
12863 if (pending == DL_PRIM_INVAL) {
12864 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12865 "received unsolicited ack for %s on %s\n",
12866 dl_primstr(prim), ill->ill_name);
12867 } else {
12868 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12869 "received unexpected ack for %s on %s (expecting %s)\n",
12870 dl_primstr(prim), ill->ill_name, dl_primstr(pending));
12872 return (B_FALSE);
12876 * Complete the current DLPI operation associated with `prim' on `ill' and
12877 * start the next queued DLPI operation (if any). If there are no queued DLPI
12878 * operations and the ill's current exclusive IPSQ operation has finished
12879 * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
12880 * allow the next exclusive IPSQ operation to begin upon ipsq_exit(). See
12881 * the comments above ipsq_current_finish() for details.
12883 void
12884 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
12886 mblk_t *mp;
12887 ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
12888 ipxop_t *ipx = ipsq->ipsq_xop;
12890 ASSERT(IAM_WRITER_IPSQ(ipsq));
12891 mutex_enter(&ill->ill_lock);
12893 ASSERT(prim != DL_PRIM_INVAL);
12894 ASSERT(ill->ill_dlpi_pending == prim);
12896 ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
12897 dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
12899 if ((mp = ill->ill_dlpi_deferred) == NULL) {
12900 ill->ill_dlpi_pending = DL_PRIM_INVAL;
12901 if (ipx->ipx_current_done) {
12902 mutex_enter(&ipx->ipx_lock);
12903 ipx->ipx_current_ipif = NULL;
12904 mutex_exit(&ipx->ipx_lock);
12906 cv_signal(&ill->ill_cv);
12907 mutex_exit(&ill->ill_lock);
12908 return;
12911 ill->ill_dlpi_deferred = mp->b_next;
12912 mp->b_next = NULL;
12913 mutex_exit(&ill->ill_lock);
12915 ill_dlpi_dispatch(ill, mp);
12919 * Queue a (multicast) DLPI control message to be sent to the driver by
12920 * later calling ill_dlpi_send_queued.
12921 * We queue them while holding a lock (ill_mcast_lock) to ensure that they
12922 * are sent in order i.e., prevent a DL_DISABMULTI_REQ and DL_ENABMULTI_REQ
12923 * for the same group to race.
12924 * We send DLPI control messages in order using ill_lock.
12925 * For IPMP we should be called on the cast_ill.
12927 void
12928 ill_dlpi_queue(ill_t *ill, mblk_t *mp)
12930 mblk_t **mpp;
12932 ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12934 mutex_enter(&ill->ill_lock);
12935 /* Must queue message. Tail insertion */
12936 mpp = &ill->ill_dlpi_deferred;
12937 while (*mpp != NULL)
12938 mpp = &((*mpp)->b_next);
12940 *mpp = mp;
12941 mutex_exit(&ill->ill_lock);
12945 * Send the messages that were queued. Make sure there is only
12946 * one outstanding message. ip_rput_dlpi_writer calls ill_dlpi_done()
12947 * when an ACK or a NAK is received to process the next queued message.
12948 * For IPMP we are called on the upper ill, but when send what is queued
12949 * on the cast_ill.
12951 void
12952 ill_dlpi_send_queued(ill_t *ill)
12954 mblk_t *mp;
12955 union DL_primitives *dlp;
12956 t_uscalar_t prim;
12957 ill_t *release_ill = NULL;
12959 if (IS_IPMP(ill)) {
12960 /* On the upper IPMP ill. */
12961 release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
12962 if (release_ill == NULL) {
12963 /* Avoid ever sending anything down to the ipmpstub */
12964 return;
12966 ill = release_ill;
12968 mutex_enter(&ill->ill_lock);
12969 while ((mp = ill->ill_dlpi_deferred) != NULL) {
12970 if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
12971 /* Can't send. Somebody else will send it */
12972 mutex_exit(&ill->ill_lock);
12973 goto done;
12975 ill->ill_dlpi_deferred = mp->b_next;
12976 mp->b_next = NULL;
12977 if (!ill->ill_dl_up) {
12979 * Nobody there. All multicast addresses will be
12980 * re-joined when we get the DL_BIND_ACK bringing the
12981 * interface up.
12983 freemsg(mp);
12984 continue;
12986 dlp = (union DL_primitives *)mp->b_rptr;
12987 prim = dlp->dl_primitive;
12989 if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
12990 (prim == DL_UNBIND_REQ)) {
12991 ill->ill_dlpi_pending = prim;
12993 mutex_exit(&ill->ill_lock);
12995 DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_send_queued",
12996 char *, dl_primstr(prim), ill_t *, ill);
12997 putnext(ill->ill_wq, mp);
12998 mutex_enter(&ill->ill_lock);
13000 mutex_exit(&ill->ill_lock);
13001 done:
13002 if (release_ill != NULL)
13003 ill_refrele(release_ill);
13007 * Queue an IP (IGMP/MLD) message to be sent by IP from
13008 * ill_mcast_send_queued
13009 * We queue them while holding a lock (ill_mcast_lock) to ensure that they
13010 * are sent in order i.e., prevent a IGMP leave and IGMP join for the same
13011 * group to race.
13012 * We send them in order using ill_lock.
13013 * For IPMP we are called on the upper ill, but we queue on the cast_ill.
13015 void
13016 ill_mcast_queue(ill_t *ill, mblk_t *mp)
13018 mblk_t **mpp;
13019 ill_t *release_ill = NULL;
13021 ASSERT(RW_LOCK_HELD(&ill->ill_mcast_lock));
13023 if (IS_IPMP(ill)) {
13024 /* On the upper IPMP ill. */
13025 release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
13026 if (release_ill == NULL) {
13027 /* Discard instead of queuing for the ipmp interface */
13028 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
13029 ip_drop_output("ipIfStatsOutDiscards - no cast_ill",
13030 mp, ill);
13031 freemsg(mp);
13032 return;
13034 ill = release_ill;
13037 mutex_enter(&ill->ill_lock);
13038 /* Must queue message. Tail insertion */
13039 mpp = &ill->ill_mcast_deferred;
13040 while (*mpp != NULL)
13041 mpp = &((*mpp)->b_next);
13043 *mpp = mp;
13044 mutex_exit(&ill->ill_lock);
13045 if (release_ill != NULL)
13046 ill_refrele(release_ill);
13050 * Send the IP packets that were queued by ill_mcast_queue.
13051 * These are IGMP/MLD packets.
13053 * For IPMP we are called on the upper ill, but when send what is queued
13054 * on the cast_ill.
13056 * Request loopback of the report if we are acting as a multicast
13057 * router, so that the process-level routing demon can hear it.
13058 * This will run multiple times for the same group if there are members
13059 * on the same group for multiple ipif's on the same ill. The
13060 * igmp_input/mld_input code will suppress this due to the loopback thus we
13061 * always loopback membership report.
13063 * We also need to make sure that this does not get load balanced
13064 * by IPMP. We do this by passing an ill to ip_output_simple.
13066 void
13067 ill_mcast_send_queued(ill_t *ill)
13069 mblk_t *mp;
13070 ip_xmit_attr_t ixas;
13071 ill_t *release_ill = NULL;
13073 if (IS_IPMP(ill)) {
13074 /* On the upper IPMP ill. */
13075 release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
13076 if (release_ill == NULL) {
13078 * We should have no messages on the ipmp interface
13079 * but no point in trying to send them.
13081 return;
13083 ill = release_ill;
13085 bzero(&ixas, sizeof (ixas));
13086 ixas.ixa_zoneid = ALL_ZONES;
13087 ixas.ixa_cred = kcred;
13088 ixas.ixa_cpid = NOPID;
13090 * Here we set ixa_ifindex. If IPMP it will be the lower ill which
13091 * makes ip_select_route pick the IRE_MULTICAST for the cast_ill.
13092 * That is necessary to handle IGMP/MLD snooping switches.
13094 ixas.ixa_ifindex = ill->ill_phyint->phyint_ifindex;
13095 ixas.ixa_ipst = ill->ill_ipst;
13097 mutex_enter(&ill->ill_lock);
13098 while ((mp = ill->ill_mcast_deferred) != NULL) {
13099 ill->ill_mcast_deferred = mp->b_next;
13100 mp->b_next = NULL;
13101 if (!ill->ill_dl_up) {
13103 * Nobody there. Just drop the ip packets.
13104 * IGMP/MLD will resend later, if this is a replumb.
13106 freemsg(mp);
13107 continue;
13109 mutex_enter(&ill->ill_phyint->phyint_lock);
13110 if (IS_UNDER_IPMP(ill) && !ipmp_ill_is_active(ill)) {
13112 * When the ill is getting deactivated, we only want to
13113 * send the DLPI messages, so drop IGMP/MLD packets.
13114 * DLPI messages are handled by ill_dlpi_send_queued()
13116 mutex_exit(&ill->ill_phyint->phyint_lock);
13117 freemsg(mp);
13118 continue;
13120 mutex_exit(&ill->ill_phyint->phyint_lock);
13121 mutex_exit(&ill->ill_lock);
13123 /* Check whether we are sending IPv4 or IPv6. */
13124 if (ill->ill_isv6) {
13125 ip6_t *ip6h = (ip6_t *)mp->b_rptr;
13127 ixas.ixa_multicast_ttl = ip6h->ip6_hops;
13128 ixas.ixa_flags = IXAF_BASIC_SIMPLE_V6;
13129 } else {
13130 ipha_t *ipha = (ipha_t *)mp->b_rptr;
13132 ixas.ixa_multicast_ttl = ipha->ipha_ttl;
13133 ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
13134 ixas.ixa_flags &= ~IXAF_SET_ULP_CKSUM;
13136 ixas.ixa_flags &= ~IXAF_VERIFY_SOURCE;
13137 ixas.ixa_flags |= IXAF_MULTICAST_LOOP | IXAF_SET_SOURCE;
13138 (void) ip_output_simple(mp, &ixas);
13139 ixa_cleanup(&ixas);
13141 mutex_enter(&ill->ill_lock);
13143 mutex_exit(&ill->ill_lock);
13145 done:
13146 if (release_ill != NULL)
13147 ill_refrele(release_ill);
13151 * Take down a specific interface, but don't lose any information about it.
13152 * (Always called as writer.)
13153 * This function goes through the down sequence even if the interface is
13154 * already down. There are 2 reasons.
13155 * a. Currently we permit interface routes that depend on down interfaces
13156 * to be added. This behaviour itself is questionable. However it appears
13157 * that both Solaris and 4.3 BSD have exhibited this behaviour for a long
13158 * time. We go thru the cleanup in order to remove these routes.
13159 * b. The bringup of the interface could fail in ill_dl_up i.e. we get
13160 * DL_ERROR_ACK in response to the DL_BIND request. The interface is
13161 * down, but we need to cleanup i.e. do ill_dl_down and
13162 * ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
13164 * IP-MT notes:
13166 * Model of reference to interfaces.
13168 * The following members in ipif_t track references to the ipif.
13169 * int ipif_refcnt; Active reference count
13171 * The following members in ill_t track references to the ill.
13172 * int ill_refcnt; active refcnt
13173 * uint_t ill_ire_cnt; Number of ires referencing ill
13174 * uint_t ill_ncec_cnt; Number of ncecs referencing ill
13175 * uint_t ill_nce_cnt; Number of nces referencing ill
13176 * uint_t ill_ilm_cnt; Number of ilms referencing ill
13178 * Reference to an ipif or ill can be obtained in any of the following ways.
13180 * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
13181 * Pointers to ipif / ill from other data structures viz ire and conn.
13182 * Implicit reference to the ipif / ill by holding a reference to the ire.
13184 * The ipif/ill lookup functions return a reference held ipif / ill.
13185 * ipif_refcnt and ill_refcnt track the reference counts respectively.
13186 * This is a purely dynamic reference count associated with threads holding
13187 * references to the ipif / ill. Pointers from other structures do not
13188 * count towards this reference count.
13190 * ill_ire_cnt is the number of ire's associated with the
13191 * ill. This is incremented whenever a new ire is created referencing the
13192 * ill. This is done atomically inside ire_add_v[46] where the ire is
13193 * actually added to the ire hash table. The count is decremented in
13194 * ire_inactive where the ire is destroyed.
13196 * ill_ncec_cnt is the number of ncec's referencing the ill thru ncec_ill.
13197 * This is incremented atomically in
13198 * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
13199 * table. Similarly it is decremented in ncec_inactive() where the ncec
13200 * is destroyed.
13202 * ill_nce_cnt is the number of nce's referencing the ill thru nce_ill. This is
13203 * incremented atomically in nce_add() where the nce is actually added to the
13204 * ill_nce. Similarly it is decremented in nce_inactive() where the nce
13205 * is destroyed.
13207 * ill_ilm_cnt is the ilm's reference to the ill. It is incremented in
13208 * ilm_add() and decremented before the ilm is freed in ilm_delete().
13210 * Flow of ioctls involving interface down/up
13212 * The following is the sequence of an attempt to set some critical flags on an
13213 * up interface.
13214 * ip_sioctl_flags
13215 * ipif_down
13216 * wait for ipif to be quiescent
13217 * ipif_down_tail
13218 * ip_sioctl_flags_tail
13220 * All set ioctls that involve down/up sequence would have a skeleton similar
13221 * to the above. All the *tail functions are called after the refcounts have
13222 * dropped to the appropriate values.
13224 * SIOC ioctls during the IPIF_CHANGING interval.
13226 * Threads handling SIOC set ioctls serialize on the squeue, but this
13227 * is not done for SIOC get ioctls. Since a set ioctl can cause several
13228 * steps of internal changes to the state, some of which are visible in
13229 * ipif_flags (such as IFF_UP being cleared and later set), and we want
13230 * the set ioctl to be atomic related to the get ioctls, the SIOC get code
13231 * will wait and restart ioctls if IPIF_CHANGING is set. The mblk is then
13232 * enqueued in the ipsq and the operation is restarted by ipsq_exit() when
13233 * the current exclusive operation completes. The IPIF_CHANGING check
13234 * and enqueue is atomic using the ill_lock and ipsq_lock. The
13235 * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
13236 * change while the ill_lock is held. Before dropping the ill_lock we acquire
13237 * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
13238 * until we release the ipsq_lock, even though the ill/ipif state flags
13239 * can change after we drop the ill_lock.
13242 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
13244 ill_t *ill = ipif->ipif_ill;
13245 conn_t *connp;
13246 boolean_t success;
13247 boolean_t ipif_was_up = B_FALSE;
13248 ip_stack_t *ipst = ill->ill_ipst;
13250 ASSERT(IAM_WRITER_IPIF(ipif));
13252 ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13254 DTRACE_PROBE3(ipif__downup, char *, "ipif_down",
13255 ill_t *, ill, ipif_t *, ipif);
13257 if (ipif->ipif_flags & IPIF_UP) {
13258 mutex_enter(&ill->ill_lock);
13259 ipif->ipif_flags &= ~IPIF_UP;
13260 ASSERT(ill->ill_ipif_up_count > 0);
13261 --ill->ill_ipif_up_count;
13262 mutex_exit(&ill->ill_lock);
13263 ipif_was_up = B_TRUE;
13264 /* Update status in SCTP's list */
13265 sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
13266 ill_nic_event_dispatch(ipif->ipif_ill,
13267 MAP_IPIF_ID(ipif->ipif_id), NE_LIF_DOWN, NULL, 0);
13271 * Removal of the last ipif from an ill may result in a DL_UNBIND
13272 * being sent to the driver, and we must not send any data packets to
13273 * the driver after the DL_UNBIND_REQ. To ensure this, all the
13274 * ire and nce entries used in the data path will be cleaned
13275 * up, and we also set the ILL_DOWN_IN_PROGRESS bit to make
13276 * sure on new entries will be added until the ill is bound
13277 * again. The ILL_DOWN_IN_PROGRESS bit is turned off upon
13278 * receipt of a DL_BIND_ACK.
13280 if (ill->ill_wq != NULL && !ill->ill_logical_down &&
13281 ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
13282 ill->ill_dl_up) {
13283 ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
13287 * Blow away memberships we established in ipif_multicast_up().
13289 ipif_multicast_down(ipif);
13292 * Remove from the mapping for __sin6_src_id. We insert only
13293 * when the address is not INADDR_ANY. As IPv4 addresses are
13294 * stored as mapped addresses, we need to check for mapped
13295 * INADDR_ANY also.
13297 if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
13298 !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
13299 !(ipif->ipif_flags & IPIF_NOLOCAL)) {
13300 int err;
13302 err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
13303 ipif->ipif_zoneid, ipst);
13304 if (err != 0) {
13305 ip0dbg(("ipif_down: srcid_remove %d\n", err));
13309 if (ipif_was_up) {
13310 /* only delete if we'd added ire's before */
13311 if (ipif->ipif_isv6)
13312 ipif_delete_ires_v6(ipif);
13313 else
13314 ipif_delete_ires_v4(ipif);
13317 if (ipif_was_up && ill->ill_ipif_up_count == 0) {
13319 * Since the interface is now down, it may have just become
13320 * inactive. Note that this needs to be done even for a
13321 * lll_logical_down(), or ARP entries will not get correctly
13322 * restored when the interface comes back up.
13324 if (IS_UNDER_IPMP(ill))
13325 ipmp_ill_refresh_active(ill);
13329 * neighbor-discovery or arp entries for this interface. The ipif
13330 * has to be quiesced, so we walk all the nce's and delete those
13331 * that point at the ipif->ipif_ill. At the same time, we also
13332 * update IPMP so that ipifs for data addresses are unbound. We dont
13333 * call ipif_arp_down to DL_UNBIND the arp stream itself here, but defer
13334 * that for ipif_down_tail()
13336 ipif_nce_down(ipif);
13339 * If this is the last ipif on the ill, we also need to remove
13340 * any IREs with ire_ill set. Otherwise ipif_is_quiescent() will
13341 * never succeed.
13343 if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0)
13344 ire_walk_ill(0, 0, ill_downi, ill, ill);
13347 * Walk all CONNs that can have a reference on an ire for this
13348 * ipif (we actually walk all that now have stale references).
13350 ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
13353 * If mp is NULL the caller will wait for the appropriate refcnt.
13354 * Eg. ip_sioctl_removeif -> ipif_free -> ipif_down
13355 * and ill_delete -> ipif_free -> ipif_down
13357 if (mp == NULL) {
13358 ASSERT(q == NULL);
13359 return (0);
13362 if (CONN_Q(q)) {
13363 connp = Q_TO_CONN(q);
13364 mutex_enter(&connp->conn_lock);
13365 } else {
13366 connp = NULL;
13368 mutex_enter(&ill->ill_lock);
13370 * Are there any ire's pointing to this ipif that are still active ?
13371 * If this is the last ipif going down, are there any ire's pointing
13372 * to this ill that are still active ?
13374 if (ipif_is_quiescent(ipif)) {
13375 mutex_exit(&ill->ill_lock);
13376 if (connp != NULL)
13377 mutex_exit(&connp->conn_lock);
13378 return (0);
13381 ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
13382 ill->ill_name, (void *)ill));
13384 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
13385 * drops down, the operation will be restarted by ipif_ill_refrele_tail
13386 * which in turn is called by the last refrele on the ipif/ill/ire.
13388 success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
13389 if (!success) {
13390 /* The conn is closing. So just return */
13391 ASSERT(connp != NULL);
13392 mutex_exit(&ill->ill_lock);
13393 mutex_exit(&connp->conn_lock);
13394 return (EINTR);
13397 mutex_exit(&ill->ill_lock);
13398 if (connp != NULL)
13399 mutex_exit(&connp->conn_lock);
13400 return (EINPROGRESS);
13404 ipif_down_tail(ipif_t *ipif)
13406 ill_t *ill = ipif->ipif_ill;
13407 int err = 0;
13409 DTRACE_PROBE3(ipif__downup, char *, "ipif_down_tail",
13410 ill_t *, ill, ipif_t *, ipif);
13413 * Skip any loopback interface (null wq).
13414 * If this is the last logical interface on the ill
13415 * have ill_dl_down tell the driver we are gone (unbind)
13416 * Note that lun 0 can ipif_down even though
13417 * there are other logical units that are up.
13418 * This occurs e.g. when we change a "significant" IFF_ flag.
13420 if (ill->ill_wq != NULL && !ill->ill_logical_down &&
13421 ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
13422 ill->ill_dl_up) {
13423 ill_dl_down(ill);
13425 if (!ipif->ipif_isv6)
13426 err = ipif_arp_down(ipif);
13428 ill->ill_logical_down = 0;
13430 ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
13431 ip_rts_newaddrmsg(RTM_DELETE, 0, ipif, RTSQ_DEFAULT);
13432 return (err);
13436 * Bring interface logically down without bringing the physical interface
13437 * down e.g. when the netmask is changed. This avoids long lasting link
13438 * negotiations between an ethernet interface and a certain switches.
13440 static int
13441 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
13443 DTRACE_PROBE3(ipif__downup, char *, "ipif_logical_down",
13444 ill_t *, ipif->ipif_ill, ipif_t *, ipif);
13447 * The ill_logical_down flag is a transient flag. It is set here
13448 * and is cleared once the down has completed in ipif_down_tail.
13449 * This flag does not indicate whether the ill stream is in the
13450 * DL_BOUND state with the driver. Instead this flag is used by
13451 * ipif_down_tail to determine whether to DL_UNBIND the stream with
13452 * the driver. The state of the ill stream i.e. whether it is
13453 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
13455 ipif->ipif_ill->ill_logical_down = 1;
13456 return (ipif_down(ipif, q, mp));
13460 * Initiate deallocate of an IPIF. Always called as writer. Called by
13461 * ill_delete or ip_sioctl_removeif.
13463 static void
13464 ipif_free(ipif_t *ipif)
13466 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
13468 ASSERT(IAM_WRITER_IPIF(ipif));
13470 if (ipif->ipif_recovery_id != 0)
13471 (void) untimeout(ipif->ipif_recovery_id);
13472 ipif->ipif_recovery_id = 0;
13475 * Take down the interface. We can be called either from ill_delete
13476 * or from ip_sioctl_removeif.
13478 (void) ipif_down(ipif, NULL, NULL);
13481 * Now that the interface is down, there's no chance it can still
13482 * become a duplicate. Cancel any timer that may have been set while
13483 * tearing down.
13485 if (ipif->ipif_recovery_id != 0)
13486 (void) untimeout(ipif->ipif_recovery_id);
13487 ipif->ipif_recovery_id = 0;
13489 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13490 /* Remove pointers to this ill in the multicast routing tables */
13491 reset_mrt_vif_ipif(ipif);
13492 /* If necessary, clear the cached source ipif rotor. */
13493 if (ipif->ipif_ill->ill_src_ipif == ipif)
13494 ipif->ipif_ill->ill_src_ipif = NULL;
13495 rw_exit(&ipst->ips_ill_g_lock);
13498 static void
13499 ipif_free_tail(ipif_t *ipif)
13501 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
13504 * Need to hold both ill_g_lock and ill_lock while
13505 * inserting or removing an ipif from the linked list
13506 * of ipifs hanging off the ill.
13508 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13510 #ifdef DEBUG
13511 ipif_trace_cleanup(ipif);
13512 #endif
13514 /* Ask SCTP to take it out of it list */
13515 sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
13516 ip_rts_newaddrmsg(RTM_FREEADDR, 0, ipif, RTSQ_DEFAULT);
13518 /* Get it out of the ILL interface list. */
13519 ipif_remove(ipif);
13520 rw_exit(&ipst->ips_ill_g_lock);
13522 ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
13523 ASSERT(ipif->ipif_recovery_id == 0);
13524 ASSERT(ipif->ipif_ire_local == NULL);
13525 ASSERT(ipif->ipif_ire_if == NULL);
13527 /* Free the memory. */
13528 mi_free(ipif);
13532 * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
13533 * is zero.
13535 void
13536 ipif_get_name(const ipif_t *ipif, char *buf, int len)
13538 char lbuf[LIFNAMSIZ];
13539 char *name;
13540 size_t name_len;
13542 buf[0] = '\0';
13543 name = ipif->ipif_ill->ill_name;
13544 name_len = ipif->ipif_ill->ill_name_length;
13545 if (ipif->ipif_id != 0) {
13546 (void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
13547 ipif->ipif_id);
13548 name = lbuf;
13549 name_len = mi_strlen(name) + 1;
13551 len -= 1;
13552 buf[len] = '\0';
13553 len = MIN(len, name_len);
13554 bcopy(name, buf, len);
13558 * Sets `buf' to an ill name.
13560 void
13561 ill_get_name(const ill_t *ill, char *buf, int len)
13563 char *name;
13564 size_t name_len;
13566 name = ill->ill_name;
13567 name_len = ill->ill_name_length;
13568 len -= 1;
13569 buf[len] = '\0';
13570 len = MIN(len, name_len);
13571 bcopy(name, buf, len);
13575 * Find an IPIF based on the name passed in. Names can be of the form <phys>
13576 * (e.g., le0) or <phys>:<#> (e.g., le0:1). When there is no colon, the
13577 * implied unit id is zero. <phys> must correspond to the name of an ILL.
13578 * (May be called as writer.)
13580 static ipif_t *
13581 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
13582 boolean_t *exists, boolean_t isv6, zoneid_t zoneid, ip_stack_t *ipst)
13584 char *cp;
13585 char *endp;
13586 long id;
13587 ill_t *ill;
13588 ipif_t *ipif;
13589 uint_t ire_type;
13590 boolean_t did_alloc = B_FALSE;
13591 char last;
13594 * If the caller wants to us to create the ipif, make sure we have a
13595 * valid zoneid
13597 ASSERT(!do_alloc || zoneid != ALL_ZONES);
13599 if (namelen == 0) {
13600 return (NULL);
13603 *exists = B_FALSE;
13604 /* Look for a colon in the name. */
13605 endp = &name[namelen];
13606 for (cp = endp; --cp > name; ) {
13607 if (*cp == IPIF_SEPARATOR_CHAR)
13608 break;
13611 if (*cp == IPIF_SEPARATOR_CHAR) {
13613 * Reject any non-decimal aliases for logical
13614 * interfaces. Aliases with leading zeroes
13615 * are also rejected as they introduce ambiguity
13616 * in the naming of the interfaces.
13617 * In order to confirm with existing semantics,
13618 * and to not break any programs/script relying
13619 * on that behaviour, if<0>:0 is considered to be
13620 * a valid interface.
13622 * If alias has two or more digits and the first
13623 * is zero, fail.
13625 if (&cp[2] < endp && cp[1] == '0') {
13626 return (NULL);
13630 if (cp <= name) {
13631 cp = endp;
13633 last = *cp;
13634 *cp = '\0';
13637 * Look up the ILL, based on the portion of the name
13638 * before the slash. ill_lookup_on_name returns a held ill.
13639 * Temporary to check whether ill exists already. If so
13640 * ill_lookup_on_name will clear it.
13642 ill = ill_lookup_on_name(name, do_alloc, isv6,
13643 &did_alloc, ipst);
13644 *cp = last;
13645 if (ill == NULL)
13646 return (NULL);
13648 /* Establish the unit number in the name. */
13649 id = 0;
13650 if (cp < endp && *endp == '\0') {
13651 /* If there was a colon, the unit number follows. */
13652 cp++;
13653 if (ddi_strtol(cp, NULL, 0, &id) != 0) {
13654 ill_refrele(ill);
13655 return (NULL);
13659 mutex_enter(&ill->ill_lock);
13660 /* Now see if there is an IPIF with this unit number. */
13661 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13662 if (ipif->ipif_id == id) {
13663 if (zoneid != ALL_ZONES &&
13664 zoneid != ipif->ipif_zoneid &&
13665 ipif->ipif_zoneid != ALL_ZONES) {
13666 mutex_exit(&ill->ill_lock);
13667 ill_refrele(ill);
13668 return (NULL);
13670 if (IPIF_CAN_LOOKUP(ipif)) {
13671 ipif_refhold_locked(ipif);
13672 mutex_exit(&ill->ill_lock);
13673 if (!did_alloc)
13674 *exists = B_TRUE;
13676 * Drop locks before calling ill_refrele
13677 * since it can potentially call into
13678 * ipif_ill_refrele_tail which can end up
13679 * in trying to acquire any lock.
13681 ill_refrele(ill);
13682 return (ipif);
13687 if (!do_alloc) {
13688 mutex_exit(&ill->ill_lock);
13689 ill_refrele(ill);
13690 return (NULL);
13694 * If none found, atomically allocate and return a new one.
13695 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
13696 * to support "receive only" use of lo0:1 etc. as is still done
13697 * below as an initial guess.
13698 * However, this is now likely to be overriden later in ipif_up_done()
13699 * when we know for sure what address has been configured on the
13700 * interface, since we might have more than one loopback interface
13701 * with a loopback address, e.g. in the case of zones, and all the
13702 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
13704 if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
13705 ire_type = IRE_LOOPBACK;
13706 else
13707 ire_type = IRE_LOCAL;
13708 ipif = ipif_allocate(ill, id, ire_type, B_TRUE, B_TRUE, NULL);
13709 if (ipif != NULL)
13710 ipif_refhold_locked(ipif);
13711 mutex_exit(&ill->ill_lock);
13712 ill_refrele(ill);
13713 return (ipif);
13717 * Variant of the above that queues the request on the ipsq when
13718 * IPIF_CHANGING is set.
13720 static ipif_t *
13721 ipif_lookup_on_name_async(char *name, size_t namelen, boolean_t isv6,
13722 zoneid_t zoneid, queue_t *q, mblk_t *mp, ipsq_func_t func, int *error,
13723 ip_stack_t *ipst)
13725 char *cp;
13726 char *endp;
13727 long id;
13728 ill_t *ill;
13729 ipif_t *ipif;
13730 boolean_t did_alloc = B_FALSE;
13731 ipsq_t *ipsq;
13733 if (error != NULL)
13734 *error = 0;
13736 if (namelen == 0) {
13737 if (error != NULL)
13738 *error = ENXIO;
13739 return (NULL);
13742 /* Look for a colon in the name. */
13743 endp = &name[namelen];
13744 for (cp = endp; --cp > name; ) {
13745 if (*cp == IPIF_SEPARATOR_CHAR)
13746 break;
13749 if (*cp == IPIF_SEPARATOR_CHAR) {
13751 * Reject any non-decimal aliases for logical
13752 * interfaces. Aliases with leading zeroes
13753 * are also rejected as they introduce ambiguity
13754 * in the naming of the interfaces.
13755 * In order to confirm with existing semantics,
13756 * and to not break any programs/script relying
13757 * on that behaviour, if<0>:0 is considered to be
13758 * a valid interface.
13760 * If alias has two or more digits and the first
13761 * is zero, fail.
13763 if (&cp[2] < endp && cp[1] == '0') {
13764 if (error != NULL)
13765 *error = EINVAL;
13766 return (NULL);
13770 if (cp <= name) {
13771 cp = endp;
13772 } else {
13773 *cp = '\0';
13777 * Look up the ILL, based on the portion of the name
13778 * before the slash. ill_lookup_on_name returns a held ill.
13779 * Temporary to check whether ill exists already. If so
13780 * ill_lookup_on_name will clear it.
13782 ill = ill_lookup_on_name(name, B_FALSE, isv6, &did_alloc, ipst);
13783 if (cp != endp)
13784 *cp = IPIF_SEPARATOR_CHAR;
13785 if (ill == NULL)
13786 return (NULL);
13788 /* Establish the unit number in the name. */
13789 id = 0;
13790 if (cp < endp && *endp == '\0') {
13791 /* If there was a colon, the unit number follows. */
13792 cp++;
13793 if (ddi_strtol(cp, NULL, 0, &id) != 0) {
13794 ill_refrele(ill);
13795 if (error != NULL)
13796 *error = ENXIO;
13797 return (NULL);
13801 GRAB_CONN_LOCK(q);
13802 mutex_enter(&ill->ill_lock);
13803 /* Now see if there is an IPIF with this unit number. */
13804 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13805 if (ipif->ipif_id == id) {
13806 if (zoneid != ALL_ZONES &&
13807 zoneid != ipif->ipif_zoneid &&
13808 ipif->ipif_zoneid != ALL_ZONES) {
13809 mutex_exit(&ill->ill_lock);
13810 RELEASE_CONN_LOCK(q);
13811 ill_refrele(ill);
13812 if (error != NULL)
13813 *error = ENXIO;
13814 return (NULL);
13817 if (!(IPIF_IS_CHANGING(ipif) ||
13818 IPIF_IS_CONDEMNED(ipif)) ||
13819 IAM_WRITER_IPIF(ipif)) {
13820 ipif_refhold_locked(ipif);
13821 mutex_exit(&ill->ill_lock);
13823 * Drop locks before calling ill_refrele
13824 * since it can potentially call into
13825 * ipif_ill_refrele_tail which can end up
13826 * in trying to acquire any lock.
13828 RELEASE_CONN_LOCK(q);
13829 ill_refrele(ill);
13830 return (ipif);
13831 } else if (q != NULL && !IPIF_IS_CONDEMNED(ipif)) {
13832 ipsq = ill->ill_phyint->phyint_ipsq;
13833 mutex_enter(&ipsq->ipsq_lock);
13834 mutex_enter(&ipsq->ipsq_xop->ipx_lock);
13835 mutex_exit(&ill->ill_lock);
13836 ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
13837 mutex_exit(&ipsq->ipsq_xop->ipx_lock);
13838 mutex_exit(&ipsq->ipsq_lock);
13839 RELEASE_CONN_LOCK(q);
13840 ill_refrele(ill);
13841 if (error != NULL)
13842 *error = EINPROGRESS;
13843 return (NULL);
13847 RELEASE_CONN_LOCK(q);
13848 mutex_exit(&ill->ill_lock);
13849 ill_refrele(ill);
13850 if (error != NULL)
13851 *error = ENXIO;
13852 return (NULL);
13856 * This routine is called whenever a new address comes up on an ipif. If
13857 * we are configured to respond to address mask requests, then we are supposed
13858 * to broadcast an address mask reply at this time. This routine is also
13859 * called if we are already up, but a netmask change is made. This is legal
13860 * but might not make the system manager very popular. (May be called
13861 * as writer.)
13863 void
13864 ipif_mask_reply(ipif_t *ipif)
13866 icmph_t *icmph;
13867 ipha_t *ipha;
13868 mblk_t *mp;
13869 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
13870 ip_xmit_attr_t ixas;
13872 #define REPLY_LEN (sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
13874 if (!ipst->ips_ip_respond_to_address_mask_broadcast)
13875 return;
13877 /* ICMP mask reply is IPv4 only */
13878 ASSERT(!ipif->ipif_isv6);
13879 /* ICMP mask reply is not for a loopback interface */
13880 ASSERT(ipif->ipif_ill->ill_wq != NULL);
13882 if (ipif->ipif_lcl_addr == INADDR_ANY)
13883 return;
13885 mp = allocb(REPLY_LEN, BPRI_HI);
13886 if (mp == NULL)
13887 return;
13888 mp->b_wptr = mp->b_rptr + REPLY_LEN;
13890 ipha = (ipha_t *)mp->b_rptr;
13891 bzero(ipha, REPLY_LEN);
13892 *ipha = icmp_ipha;
13893 ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
13894 ipha->ipha_src = ipif->ipif_lcl_addr;
13895 ipha->ipha_dst = ipif->ipif_brd_addr;
13896 ipha->ipha_length = htons(REPLY_LEN);
13897 ipha->ipha_ident = 0;
13899 icmph = (icmph_t *)&ipha[1];
13900 icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
13901 bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
13902 icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
13904 bzero(&ixas, sizeof (ixas));
13905 ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
13906 ixas.ixa_zoneid = ALL_ZONES;
13907 ixas.ixa_ifindex = 0;
13908 ixas.ixa_ipst = ipst;
13909 ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
13910 (void) ip_output_simple(mp, &ixas);
13911 ixa_cleanup(&ixas);
13912 #undef REPLY_LEN
13916 * Join the ipif specific multicast groups.
13917 * Must be called after a mapping has been set up in the resolver. (Always
13918 * called as writer.)
13920 void
13921 ipif_multicast_up(ipif_t *ipif)
13923 int err;
13924 ill_t *ill;
13925 ilm_t *ilm;
13927 ASSERT(IAM_WRITER_IPIF(ipif));
13929 ill = ipif->ipif_ill;
13931 ip1dbg(("ipif_multicast_up\n"));
13932 if (!(ill->ill_flags & ILLF_MULTICAST) ||
13933 ipif->ipif_allhosts_ilm != NULL)
13934 return;
13936 if (ipif->ipif_isv6) {
13937 in6_addr_t v6allmc = ipv6_all_hosts_mcast;
13938 in6_addr_t v6solmc = ipv6_solicited_node_mcast;
13940 v6solmc.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
13942 if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
13943 return;
13945 ip1dbg(("ipif_multicast_up - addmulti\n"));
13948 * Join the all hosts multicast address. We skip this for
13949 * underlying IPMP interfaces since they should be invisible.
13951 if (!IS_UNDER_IPMP(ill)) {
13952 ilm = ip_addmulti(&v6allmc, ill, ipif->ipif_zoneid,
13953 &err);
13954 if (ilm == NULL) {
13955 ASSERT(err != 0);
13956 ip0dbg(("ipif_multicast_up: "
13957 "all_hosts_mcast failed %d\n", err));
13958 return;
13960 ipif->ipif_allhosts_ilm = ilm;
13964 * Enable multicast for the solicited node multicast address.
13965 * If IPMP we need to put the membership on the upper ill.
13967 if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
13968 ill_t *mcast_ill = NULL;
13969 boolean_t need_refrele;
13971 if (IS_UNDER_IPMP(ill) &&
13972 (mcast_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL) {
13973 need_refrele = B_TRUE;
13974 } else {
13975 mcast_ill = ill;
13976 need_refrele = B_FALSE;
13979 ilm = ip_addmulti(&v6solmc, mcast_ill,
13980 ipif->ipif_zoneid, &err);
13981 if (need_refrele)
13982 ill_refrele(mcast_ill);
13984 if (ilm == NULL) {
13985 ASSERT(err != 0);
13986 ip0dbg(("ipif_multicast_up: solicited MC"
13987 " failed %d\n", err));
13988 if ((ilm = ipif->ipif_allhosts_ilm) != NULL) {
13989 ipif->ipif_allhosts_ilm = NULL;
13990 (void) ip_delmulti(ilm);
13992 return;
13994 ipif->ipif_solmulti_ilm = ilm;
13996 } else {
13997 in6_addr_t v6group;
13999 if (ipif->ipif_lcl_addr == INADDR_ANY || IS_UNDER_IPMP(ill))
14000 return;
14002 /* Join the all hosts multicast address */
14003 ip1dbg(("ipif_multicast_up - addmulti\n"));
14004 IN6_IPADDR_TO_V4MAPPED(htonl(INADDR_ALLHOSTS_GROUP), &v6group);
14006 ilm = ip_addmulti(&v6group, ill, ipif->ipif_zoneid, &err);
14007 if (ilm == NULL) {
14008 ASSERT(err != 0);
14009 ip0dbg(("ipif_multicast_up: failed %d\n", err));
14010 return;
14012 ipif->ipif_allhosts_ilm = ilm;
14017 * Blow away any multicast groups that we joined in ipif_multicast_up().
14018 * (ilms from explicit memberships are handled in conn_update_ill.)
14020 void
14021 ipif_multicast_down(ipif_t *ipif)
14023 ASSERT(IAM_WRITER_IPIF(ipif));
14025 ip1dbg(("ipif_multicast_down\n"));
14027 if (ipif->ipif_allhosts_ilm != NULL) {
14028 (void) ip_delmulti(ipif->ipif_allhosts_ilm);
14029 ipif->ipif_allhosts_ilm = NULL;
14031 if (ipif->ipif_solmulti_ilm != NULL) {
14032 (void) ip_delmulti(ipif->ipif_solmulti_ilm);
14033 ipif->ipif_solmulti_ilm = NULL;
14038 * Used when an interface comes up to recreate any extra routes on this
14039 * interface.
14042 ill_recover_saved_ire(ill_t *ill)
14044 mblk_t *mp;
14045 ip_stack_t *ipst = ill->ill_ipst;
14047 ip1dbg(("ill_recover_saved_ire(%s)", ill->ill_name));
14049 mutex_enter(&ill->ill_saved_ire_lock);
14050 for (mp = ill->ill_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
14051 ire_t *ire, *nire;
14052 ifrt_t *ifrt;
14054 ifrt = (ifrt_t *)mp->b_rptr;
14056 * Create a copy of the IRE with the saved address and netmask.
14058 if (ill->ill_isv6) {
14059 ire = ire_create_v6(
14060 &ifrt->ifrt_v6addr,
14061 &ifrt->ifrt_v6mask,
14062 &ifrt->ifrt_v6gateway_addr,
14063 ifrt->ifrt_type,
14064 ill,
14065 ifrt->ifrt_zoneid,
14066 ifrt->ifrt_flags,
14067 ipst);
14068 } else {
14069 ire = ire_create(
14070 (uint8_t *)&ifrt->ifrt_addr,
14071 (uint8_t *)&ifrt->ifrt_mask,
14072 (uint8_t *)&ifrt->ifrt_gateway_addr,
14073 ifrt->ifrt_type,
14074 ill,
14075 ifrt->ifrt_zoneid,
14076 ifrt->ifrt_flags,
14077 ipst);
14079 if (ire == NULL) {
14080 mutex_exit(&ill->ill_saved_ire_lock);
14081 return (ENOMEM);
14084 if (ifrt->ifrt_flags & RTF_SETSRC) {
14085 if (ill->ill_isv6) {
14086 ire->ire_setsrc_addr_v6 =
14087 ifrt->ifrt_v6setsrc_addr;
14088 } else {
14089 ire->ire_setsrc_addr = ifrt->ifrt_setsrc_addr;
14094 * Some software (for example, GateD and Sun Cluster) attempts
14095 * to create (what amount to) IRE_PREFIX routes with the
14096 * loopback address as the gateway. This is primarily done to
14097 * set up prefixes with the RTF_REJECT flag set (for example,
14098 * when generating aggregate routes.)
14100 * If the IRE type (as defined by ill->ill_net_type) is
14101 * IRE_LOOPBACK, then we map the request into a
14102 * IRE_IF_NORESOLVER.
14104 if (ill->ill_net_type == IRE_LOOPBACK)
14105 ire->ire_type = IRE_IF_NORESOLVER;
14108 * ire held by ire_add, will be refreled' towards the
14109 * the end of ipif_up_done
14111 nire = ire_add(ire);
14113 * Check if it was a duplicate entry. This handles
14114 * the case of two racing route adds for the same route
14116 if (nire == NULL) {
14117 ip1dbg(("ill_recover_saved_ire: FAILED\n"));
14118 } else if (nire != ire) {
14119 ip1dbg(("ill_recover_saved_ire: duplicate ire %p\n",
14120 (void *)nire));
14121 ire_delete(nire);
14122 } else {
14123 ip1dbg(("ill_recover_saved_ire: added ire %p\n",
14124 (void *)nire));
14126 if (nire != NULL)
14127 ire_refrele(nire);
14129 mutex_exit(&ill->ill_saved_ire_lock);
14130 return (0);
14134 * Used to set the netmask and broadcast address to default values when the
14135 * interface is brought up. (Always called as writer.)
14137 static void
14138 ipif_set_default(ipif_t *ipif)
14140 ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
14142 if (!ipif->ipif_isv6) {
14144 * Interface holds an IPv4 address. Default
14145 * mask is the natural netmask.
14147 if (!ipif->ipif_net_mask) {
14148 ipaddr_t v4mask;
14150 v4mask = ip_net_mask(ipif->ipif_lcl_addr);
14151 V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
14153 if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14154 /* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14155 ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
14156 } else {
14157 V6_MASK_COPY(ipif->ipif_v6lcl_addr,
14158 ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
14161 * NOTE: SunOS 4.X does this even if the broadcast address
14162 * has been already set thus we do the same here.
14164 if (ipif->ipif_flags & IPIF_BROADCAST) {
14165 ipaddr_t v4addr;
14167 v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
14168 IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
14170 } else {
14172 * Interface holds an IPv6-only address. Default
14173 * mask is all-ones.
14175 if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
14176 ipif->ipif_v6net_mask = ipv6_all_ones;
14177 if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14178 /* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14179 ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
14180 } else {
14181 V6_MASK_COPY(ipif->ipif_v6lcl_addr,
14182 ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
14188 * Return 0 if this address can be used as local address without causing
14189 * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
14190 * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
14191 * Note that the same IPv6 link-local address is allowed as long as the ills
14192 * are not on the same link.
14195 ip_addr_availability_check(ipif_t *new_ipif)
14197 in6_addr_t our_v6addr;
14198 ill_t *ill;
14199 ipif_t *ipif;
14200 ill_walk_context_t ctx;
14201 ip_stack_t *ipst = new_ipif->ipif_ill->ill_ipst;
14203 ASSERT(IAM_WRITER_IPIF(new_ipif));
14204 ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
14205 ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
14207 new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
14208 if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
14209 IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
14210 return (0);
14212 our_v6addr = new_ipif->ipif_v6lcl_addr;
14214 if (new_ipif->ipif_isv6)
14215 ill = ILL_START_WALK_V6(&ctx, ipst);
14216 else
14217 ill = ILL_START_WALK_V4(&ctx, ipst);
14219 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
14220 for (ipif = ill->ill_ipif; ipif != NULL;
14221 ipif = ipif->ipif_next) {
14222 if ((ipif == new_ipif) ||
14223 !(ipif->ipif_flags & IPIF_UP) ||
14224 (ipif->ipif_flags & IPIF_UNNUMBERED) ||
14225 !IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
14226 &our_v6addr))
14227 continue;
14229 if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
14230 new_ipif->ipif_flags |= IPIF_UNNUMBERED;
14231 else if (ipif->ipif_flags & IPIF_POINTOPOINT)
14232 ipif->ipif_flags |= IPIF_UNNUMBERED;
14233 else if ((IN6_IS_ADDR_LINKLOCAL(&our_v6addr) ||
14234 IN6_IS_ADDR_SITELOCAL(&our_v6addr)) &&
14235 !IS_ON_SAME_LAN(ill, new_ipif->ipif_ill))
14236 continue;
14237 else if (new_ipif->ipif_zoneid != ipif->ipif_zoneid &&
14238 ipif->ipif_zoneid != ALL_ZONES && IS_LOOPBACK(ill))
14239 continue;
14240 else if (new_ipif->ipif_ill == ill)
14241 return (EADDRINUSE);
14242 else
14243 return (EADDRNOTAVAIL);
14247 return (0);
14251 * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
14252 * IREs for the ipif.
14253 * When the routine returns EINPROGRESS then mp has been consumed and
14254 * the ioctl will be acked from ip_rput_dlpi.
14257 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
14259 ill_t *ill = ipif->ipif_ill;
14260 boolean_t isv6 = ipif->ipif_isv6;
14261 int err = 0;
14262 boolean_t success;
14263 uint_t ipif_orig_id;
14264 ip_stack_t *ipst = ill->ill_ipst;
14266 ASSERT(IAM_WRITER_IPIF(ipif));
14268 ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
14269 DTRACE_PROBE3(ipif__downup, char *, "ipif_up",
14270 ill_t *, ill, ipif_t *, ipif);
14272 /* Shouldn't get here if it is already up. */
14273 if (ipif->ipif_flags & IPIF_UP)
14274 return (EALREADY);
14277 * If this is a request to bring up a data address on an interface
14278 * under IPMP, then move the address to its IPMP meta-interface and
14279 * try to bring it up. One complication is that the zeroth ipif for
14280 * an ill is special, in that every ill always has one, and that code
14281 * throughout IP deferences ill->ill_ipif without holding any locks.
14283 if (IS_UNDER_IPMP(ill) && ipmp_ipif_is_dataaddr(ipif) &&
14284 (!ipif->ipif_isv6 || !V6_IPIF_LINKLOCAL(ipif))) {
14285 ipif_t *stubipif = NULL, *moveipif = NULL;
14286 ill_t *ipmp_ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
14289 * The ipif being brought up should be quiesced. If it's not,
14290 * something has gone amiss and we need to bail out. (If it's
14291 * quiesced, we know it will remain so via IPIF_CONDEMNED.)
14293 mutex_enter(&ill->ill_lock);
14294 if (!ipif_is_quiescent(ipif)) {
14295 mutex_exit(&ill->ill_lock);
14296 return (EINVAL);
14298 mutex_exit(&ill->ill_lock);
14301 * If we're going to need to allocate ipifs, do it prior
14302 * to starting the move (and grabbing locks).
14304 if (ipif->ipif_id == 0) {
14305 if ((moveipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
14306 B_FALSE, &err)) == NULL) {
14307 return (err);
14309 if ((stubipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
14310 B_FALSE, &err)) == NULL) {
14311 mi_free(moveipif);
14312 return (err);
14317 * Grab or transfer the ipif to move. During the move, keep
14318 * ill_g_lock held to prevent any ill walker threads from
14319 * seeing things in an inconsistent state.
14321 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14322 if (ipif->ipif_id != 0) {
14323 ipif_remove(ipif);
14324 } else {
14325 ipif_transfer(ipif, moveipif, stubipif);
14326 ipif = moveipif;
14330 * Place the ipif on the IPMP ill. If the zeroth ipif on
14331 * the IPMP ill is a stub (0.0.0.0 down address) then we
14332 * replace that one. Otherwise, pick the next available slot.
14334 ipif->ipif_ill = ipmp_ill;
14335 ipif_orig_id = ipif->ipif_id;
14337 if (ipmp_ipif_is_stubaddr(ipmp_ill->ill_ipif)) {
14338 ipif_transfer(ipif, ipmp_ill->ill_ipif, NULL);
14339 ipif = ipmp_ill->ill_ipif;
14340 } else {
14341 ipif->ipif_id = -1;
14342 if ((err = ipif_insert(ipif, B_FALSE)) != 0) {
14344 * No more available ipif_id's -- put it back
14345 * on the original ill and fail the operation.
14346 * Since we're writer on the ill, we can be
14347 * sure our old slot is still available.
14349 ipif->ipif_id = ipif_orig_id;
14350 ipif->ipif_ill = ill;
14351 if (ipif_orig_id == 0) {
14352 ipif_transfer(ipif, ill->ill_ipif,
14353 NULL);
14354 } else {
14355 VERIFY(ipif_insert(ipif, B_FALSE) == 0);
14357 rw_exit(&ipst->ips_ill_g_lock);
14358 return (err);
14361 rw_exit(&ipst->ips_ill_g_lock);
14364 * Tell SCTP that the ipif has moved. Note that even if we
14365 * had to allocate a new ipif, the original sequence id was
14366 * preserved and therefore SCTP won't know.
14368 sctp_move_ipif(ipif, ill, ipmp_ill);
14371 * If the ipif being brought up was on slot zero, then we
14372 * first need to bring up the placeholder we stuck there. In
14373 * ip_rput_dlpi_writer(), arp_bringup_done(), or the recursive
14374 * call to ipif_up() itself, if we successfully bring up the
14375 * placeholder, we'll check ill_move_ipif and bring it up too.
14377 if (ipif_orig_id == 0) {
14378 ASSERT(ill->ill_move_ipif == NULL);
14379 ill->ill_move_ipif = ipif;
14380 if ((err = ipif_up(ill->ill_ipif, q, mp)) == 0)
14381 ASSERT(ill->ill_move_ipif == NULL);
14382 if (err != EINPROGRESS)
14383 ill->ill_move_ipif = NULL;
14384 return (err);
14388 * Bring it up on the IPMP ill.
14390 return (ipif_up(ipif, q, mp));
14393 /* Skip arp/ndp for any loopback interface. */
14394 if (ill->ill_wq != NULL) {
14395 conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
14396 ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
14398 if (!ill->ill_dl_up) {
14400 * ill_dl_up is not yet set. i.e. we are yet to
14401 * DL_BIND with the driver and this is the first
14402 * logical interface on the ill to become "up".
14403 * Tell the driver to get going (via DL_BIND_REQ).
14404 * Note that changing "significant" IFF_ flags
14405 * address/netmask etc cause a down/up dance, but
14406 * does not cause an unbind (DL_UNBIND) with the driver
14408 return (ill_dl_up(ill, ipif, mp, q));
14412 * ipif_resolver_up may end up needeing to bind/attach
14413 * the ARP stream, which in turn necessitates a
14414 * DLPI message exchange with the driver. ioctls are
14415 * serialized and so we cannot send more than one
14416 * interface up message at a time. If ipif_resolver_up
14417 * does need to wait for the DLPI handshake for the ARP stream,
14418 * we get EINPROGRESS and we will complete in arp_bringup_done.
14421 ASSERT(connp != NULL || !CONN_Q(q));
14422 if (connp != NULL)
14423 mutex_enter(&connp->conn_lock);
14424 mutex_enter(&ill->ill_lock);
14425 success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
14426 mutex_exit(&ill->ill_lock);
14427 if (connp != NULL)
14428 mutex_exit(&connp->conn_lock);
14429 if (!success)
14430 return (EINTR);
14433 * Crank up IPv6 neighbor discovery. Unlike ARP, this should
14434 * complete when ipif_ndp_up returns.
14436 err = ipif_resolver_up(ipif, Res_act_initial);
14437 if (err == EINPROGRESS) {
14438 /* We will complete it in arp_bringup_done() */
14439 return (err);
14442 if (isv6 && err == 0)
14443 err = ipif_ndp_up(ipif, B_TRUE);
14445 ASSERT(err != EINPROGRESS);
14446 mp = ipsq_pending_mp_get(ipsq, &connp);
14447 ASSERT(mp != NULL);
14448 if (err != 0)
14449 return (err);
14450 } else {
14452 * Interfaces without underlying hardware don't do duplicate
14453 * address detection.
14455 ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
14456 ipif->ipif_addr_ready = 1;
14457 err = ill_add_ires(ill);
14458 /* allocation failure? */
14459 if (err != 0)
14460 return (err);
14463 err = (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
14464 if (err == 0 && ill->ill_move_ipif != NULL) {
14465 ipif = ill->ill_move_ipif;
14466 ill->ill_move_ipif = NULL;
14467 return (ipif_up(ipif, q, mp));
14469 return (err);
14473 * Add any IREs tied to the ill. For now this is just an IRE_MULTICAST.
14474 * The identical set of IREs need to be removed in ill_delete_ires().
14477 ill_add_ires(ill_t *ill)
14479 ire_t *ire;
14480 in6_addr_t dummy6 = {(uint32_t)V6_MCAST, 0, 0, 1};
14481 in_addr_t dummy4 = htonl(INADDR_ALLHOSTS_GROUP);
14483 if (ill->ill_ire_multicast != NULL)
14484 return (0);
14487 * provide some dummy ire_addr for creating the ire.
14489 if (ill->ill_isv6) {
14490 ire = ire_create_v6(&dummy6, 0, 0, IRE_MULTICAST, ill,
14491 ALL_ZONES, RTF_UP, ill->ill_ipst);
14492 } else {
14493 ire = ire_create((uchar_t *)&dummy4, 0, 0, IRE_MULTICAST, ill,
14494 ALL_ZONES, RTF_UP, ill->ill_ipst);
14496 if (ire == NULL)
14497 return (ENOMEM);
14499 ill->ill_ire_multicast = ire;
14500 return (0);
14503 void
14504 ill_delete_ires(ill_t *ill)
14506 if (ill->ill_ire_multicast != NULL) {
14508 * BIND/ATTACH completed; Release the ref for ill_ire_multicast
14509 * which was taken without any th_tracing enabled.
14510 * We also mark it as condemned (note that it was never added)
14511 * so that caching conn's can move off of it.
14513 ire_make_condemned(ill->ill_ire_multicast);
14514 ire_refrele_notr(ill->ill_ire_multicast);
14515 ill->ill_ire_multicast = NULL;
14520 * Perform a bind for the physical device.
14521 * When the routine returns EINPROGRESS then mp has been consumed and
14522 * the ioctl will be acked from ip_rput_dlpi.
14523 * Allocate an unbind message and save it until ipif_down.
14525 static int
14526 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
14528 mblk_t *bind_mp = NULL;
14529 mblk_t *unbind_mp = NULL;
14530 conn_t *connp;
14531 boolean_t success;
14532 int err;
14534 DTRACE_PROBE2(ill__downup, char *, "ill_dl_up", ill_t *, ill);
14536 ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
14537 ASSERT(IAM_WRITER_ILL(ill));
14538 ASSERT(mp != NULL);
14541 * Make sure we have an IRE_MULTICAST in case we immediately
14542 * start receiving packets.
14544 err = ill_add_ires(ill);
14545 if (err != 0)
14546 goto bad;
14548 bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
14549 DL_BIND_REQ);
14550 if (bind_mp == NULL)
14551 goto bad;
14552 ((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
14553 ((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
14556 * ill_unbind_mp would be non-null if the following sequence had
14557 * happened:
14558 * - send DL_BIND_REQ to driver, wait for response
14559 * - multiple ioctls that need to bring the ipif up are encountered,
14560 * but they cannot enter the ipsq due to the outstanding DL_BIND_REQ.
14561 * These ioctls will then be enqueued on the ipsq
14562 * - a DL_ERROR_ACK is returned for the DL_BIND_REQ
14563 * At this point, the pending ioctls in the ipsq will be drained, and
14564 * since ill->ill_dl_up was not set, ill_dl_up would be invoked with
14565 * a non-null ill->ill_unbind_mp
14567 if (ill->ill_unbind_mp == NULL) {
14568 unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t),
14569 DL_UNBIND_REQ);
14570 if (unbind_mp == NULL)
14571 goto bad;
14574 * Record state needed to complete this operation when the
14575 * DL_BIND_ACK shows up. Also remember the pre-allocated mblks.
14577 connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
14578 ASSERT(connp != NULL || !CONN_Q(q));
14579 GRAB_CONN_LOCK(q);
14580 mutex_enter(&ipif->ipif_ill->ill_lock);
14581 success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
14582 mutex_exit(&ipif->ipif_ill->ill_lock);
14583 RELEASE_CONN_LOCK(q);
14584 if (!success)
14585 goto bad;
14588 * Save the unbind message for ill_dl_down(); it will be consumed when
14589 * the interface goes down.
14591 if (ill->ill_unbind_mp == NULL)
14592 ill->ill_unbind_mp = unbind_mp;
14594 ill_dlpi_send(ill, bind_mp);
14595 /* Send down link-layer capabilities probe if not already done. */
14596 ill_capability_probe(ill);
14599 * Sysid used to rely on the fact that netboots set domainname
14600 * and the like. Now that miniroot boots aren't strictly netboots
14601 * and miniroot network configuration is driven from userland
14602 * these things still need to be set. This situation can be detected
14603 * by comparing the interface being configured here to the one
14604 * dhcifname was set to reference by the boot loader. Once sysid is
14605 * converted to use dhcp_ipc_getinfo() this call can go away.
14607 if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
14608 (strcmp(ill->ill_name, dhcifname) == 0) &&
14609 (strlen(srpc_domain) == 0)) {
14610 if (dhcpinit() != 0)
14611 cmn_err(CE_WARN, "no cached dhcp response");
14615 * This operation will complete in ip_rput_dlpi with either
14616 * a DL_BIND_ACK or DL_ERROR_ACK.
14618 return (EINPROGRESS);
14619 bad:
14620 ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
14622 freemsg(bind_mp);
14623 freemsg(unbind_mp);
14624 return (ENOMEM);
14627 /* Add room for tcp+ip headers */
14628 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
14631 * DLPI and ARP is up.
14632 * Create all the IREs associated with an interface. Bring up multicast.
14633 * Set the interface flag and finish other initialization
14634 * that potentially had to be deferred to after DL_BIND_ACK.
14637 ipif_up_done(ipif_t *ipif)
14639 ill_t *ill = ipif->ipif_ill;
14640 int err = 0;
14641 boolean_t loopback = B_FALSE;
14642 boolean_t update_src_selection = B_TRUE;
14643 ipif_t *tmp_ipif;
14645 ip1dbg(("ipif_up_done(%s:%u)\n",
14646 ipif->ipif_ill->ill_name, ipif->ipif_id));
14647 DTRACE_PROBE3(ipif__downup, char *, "ipif_up_done",
14648 ill_t *, ill, ipif_t *, ipif);
14650 /* Check if this is a loopback interface */
14651 if (ipif->ipif_ill->ill_wq == NULL)
14652 loopback = B_TRUE;
14654 ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
14657 * If all other interfaces for this ill are down or DEPRECATED,
14658 * or otherwise unsuitable for source address selection,
14659 * reset the src generation numbers to make sure source
14660 * address selection gets to take this new ipif into account.
14661 * No need to hold ill_lock while traversing the ipif list since
14662 * we are writer
14664 for (tmp_ipif = ill->ill_ipif; tmp_ipif;
14665 tmp_ipif = tmp_ipif->ipif_next) {
14666 if (((tmp_ipif->ipif_flags &
14667 (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
14668 !(tmp_ipif->ipif_flags & IPIF_UP)) ||
14669 (tmp_ipif == ipif))
14670 continue;
14671 /* first useable pre-existing interface */
14672 update_src_selection = B_FALSE;
14673 break;
14675 if (update_src_selection)
14676 ip_update_source_selection(ill->ill_ipst);
14678 if (IS_LOOPBACK(ill) || ill->ill_net_type == IRE_IF_NORESOLVER) {
14679 nce_t *loop_nce = NULL;
14680 uint16_t flags = (NCE_F_MYADDR | NCE_F_AUTHORITY | NCE_F_NONUD);
14683 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
14684 * ipif_lookup_on_name(), but in the case of zones we can have
14685 * several loopback addresses on lo0. So all the interfaces with
14686 * loopback addresses need to be marked IRE_LOOPBACK.
14688 if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
14689 htonl(INADDR_LOOPBACK))
14690 ipif->ipif_ire_type = IRE_LOOPBACK;
14691 else
14692 ipif->ipif_ire_type = IRE_LOCAL;
14693 if (ill->ill_net_type != IRE_LOOPBACK)
14694 flags |= NCE_F_PUBLISH;
14696 /* add unicast nce for the local addr */
14697 err = nce_lookup_then_add_v4(ill, NULL,
14698 ill->ill_phys_addr_length, &ipif->ipif_lcl_addr, flags,
14699 ND_REACHABLE, &loop_nce);
14700 /* A shared-IP zone sees EEXIST for lo0:N */
14701 if (err == 0 || err == EEXIST) {
14702 ipif->ipif_added_nce = 1;
14703 loop_nce->nce_ipif_cnt++;
14704 nce_refrele(loop_nce);
14705 err = 0;
14706 } else {
14707 ASSERT(loop_nce == NULL);
14708 return (err);
14712 /* Create all the IREs associated with this interface */
14713 err = ipif_add_ires_v4(ipif, loopback);
14714 if (err != 0) {
14716 * see comments about return value from
14717 * ip_addr_availability_check() in ipif_add_ires_v4().
14719 if (err != EADDRINUSE) {
14720 (void) ipif_arp_down(ipif);
14721 } else {
14723 * Make IPMP aware of the deleted ipif so that
14724 * the needed ipmp cleanup (e.g., of ipif_bound_ill)
14725 * can be completed. Note that we do not want to
14726 * destroy the nce that was created on the ipmp_ill
14727 * for the active copy of the duplicate address in
14728 * use.
14730 if (IS_IPMP(ill))
14731 ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
14732 err = EADDRNOTAVAIL;
14734 return (err);
14737 if (ill->ill_ipif_up_count == 1 && !loopback) {
14738 /* Recover any additional IREs entries for this ill */
14739 (void) ill_recover_saved_ire(ill);
14742 if (ill->ill_need_recover_multicast) {
14744 * Need to recover all multicast memberships in the driver.
14745 * This had to be deferred until we had attached. The same
14746 * code exists in ipif_up_done_v6() to recover IPv6
14747 * memberships.
14749 * Note that it would be preferable to unconditionally do the
14750 * ill_recover_multicast() in ill_dl_up(), but we cannot do
14751 * that since ill_join_allmulti() depends on ill_dl_up being
14752 * set, and it is not set until we receive a DL_BIND_ACK after
14753 * having called ill_dl_up().
14755 ill_recover_multicast(ill);
14758 if (ill->ill_ipif_up_count == 1) {
14760 * Since the interface is now up, it may now be active.
14762 if (IS_UNDER_IPMP(ill))
14763 ipmp_ill_refresh_active(ill);
14766 * If this is an IPMP interface, we may now be able to
14767 * establish ARP entries.
14769 if (IS_IPMP(ill))
14770 ipmp_illgrp_refresh_arpent(ill->ill_grp);
14773 /* Join the allhosts multicast address */
14774 ipif_multicast_up(ipif);
14776 if (!loopback && !update_src_selection &&
14777 !(ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)))
14778 ip_update_source_selection(ill->ill_ipst);
14780 if (!loopback && ipif->ipif_addr_ready) {
14781 /* Broadcast an address mask reply. */
14782 ipif_mask_reply(ipif);
14784 /* Perhaps ilgs should use this ill */
14785 update_conn_ill(NULL, ill->ill_ipst);
14788 * This had to be deferred until we had bound. Tell routing sockets and
14789 * others that this interface is up if it looks like the address has
14790 * been validated. Otherwise, if it isn't ready yet, wait for
14791 * duplicate address detection to do its thing.
14793 if (ipif->ipif_addr_ready)
14794 ipif_up_notify(ipif);
14795 return (0);
14799 * Add the IREs associated with the ipif.
14800 * Those MUST be explicitly removed in ipif_delete_ires_v4.
14802 static int
14803 ipif_add_ires_v4(ipif_t *ipif, boolean_t loopback)
14805 ill_t *ill = ipif->ipif_ill;
14806 ip_stack_t *ipst = ill->ill_ipst;
14807 ire_t *ire_array[20];
14808 ire_t **irep = ire_array;
14809 ire_t **irep1;
14810 ipaddr_t net_mask = 0;
14811 ipaddr_t subnet_mask, route_mask;
14812 int err;
14813 ire_t *ire_local = NULL; /* LOCAL or LOOPBACK */
14814 ire_t *ire_if = NULL;
14815 uchar_t *gw;
14817 if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14818 !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14819 /* Register the source address for __sin6_src_id */
14820 err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
14821 ipif->ipif_zoneid, ipst);
14822 if (err != 0) {
14823 ip0dbg(("ipif_add_ires: srcid_insert %d\n", err));
14824 return (err);
14827 if (loopback)
14828 gw = (uchar_t *)&ipif->ipif_lcl_addr;
14829 else
14830 gw = NULL;
14832 /* If the interface address is set, create the local IRE. */
14833 ire_local = ire_create(
14834 (uchar_t *)&ipif->ipif_lcl_addr, /* dest address */
14835 (uchar_t *)&ip_g_all_ones, /* mask */
14836 gw, /* gateway */
14837 ipif->ipif_ire_type, /* LOCAL or LOOPBACK */
14838 ipif->ipif_ill,
14839 ipif->ipif_zoneid,
14840 ((ipif->ipif_flags & IPIF_PRIVATE) ?
14841 RTF_PRIVATE : 0) | RTF_KERNEL,
14842 ipst);
14843 ip1dbg(("ipif_add_ires: 0x%p creating IRE %p type 0x%x"
14844 " for 0x%x\n", (void *)ipif, (void *)ire_local,
14845 ipif->ipif_ire_type,
14846 ntohl(ipif->ipif_lcl_addr)));
14847 if (ire_local == NULL) {
14848 ip1dbg(("ipif_up_done: NULL ire_local\n"));
14849 err = ENOMEM;
14850 goto bad;
14852 } else {
14853 ip1dbg((
14854 "ipif_add_ires: not creating IRE %d for 0x%x: flags 0x%x\n",
14855 ipif->ipif_ire_type,
14856 ntohl(ipif->ipif_lcl_addr),
14857 (uint_t)ipif->ipif_flags));
14859 if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14860 !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14861 net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14862 } else {
14863 net_mask = htonl(IN_CLASSA_NET); /* fallback */
14866 subnet_mask = ipif->ipif_net_mask;
14869 * If mask was not specified, use natural netmask of
14870 * interface address. Also, store this mask back into the
14871 * ipif struct.
14873 if (subnet_mask == 0) {
14874 subnet_mask = net_mask;
14875 V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
14876 V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
14877 ipif->ipif_v6subnet);
14880 /* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
14881 if (!loopback && !(ipif->ipif_flags & IPIF_NOXMIT) &&
14882 ipif->ipif_subnet != INADDR_ANY) {
14883 /* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14885 if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14886 route_mask = IP_HOST_MASK;
14887 } else {
14888 route_mask = subnet_mask;
14891 ip1dbg(("ipif_add_ires: ipif 0x%p ill 0x%p "
14892 "creating if IRE ill_net_type 0x%x for 0x%x\n",
14893 (void *)ipif, (void *)ill, ill->ill_net_type,
14894 ntohl(ipif->ipif_subnet)));
14895 ire_if = ire_create(
14896 (uchar_t *)&ipif->ipif_subnet,
14897 (uchar_t *)&route_mask,
14898 (uchar_t *)&ipif->ipif_lcl_addr,
14899 ill->ill_net_type,
14900 ill,
14901 ipif->ipif_zoneid,
14902 ((ipif->ipif_flags & IPIF_PRIVATE) ?
14903 RTF_PRIVATE: 0) | RTF_KERNEL,
14904 ipst);
14905 if (ire_if == NULL) {
14906 ip1dbg(("ipif_up_done: NULL ire_if\n"));
14907 err = ENOMEM;
14908 goto bad;
14913 * Create any necessary broadcast IREs.
14915 if ((ipif->ipif_flags & IPIF_BROADCAST) &&
14916 !(ipif->ipif_flags & IPIF_NOXMIT))
14917 irep = ipif_create_bcast_ires(ipif, irep);
14919 /* If an earlier ire_create failed, get out now */
14920 for (irep1 = irep; irep1 > ire_array; ) {
14921 irep1--;
14922 if (*irep1 == NULL) {
14923 ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
14924 err = ENOMEM;
14925 goto bad;
14930 * Need to atomically check for IP address availability under
14931 * ip_addr_avail_lock. ill_g_lock is held as reader to ensure no new
14932 * ills or new ipifs can be added while we are checking availability.
14934 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
14935 mutex_enter(&ipst->ips_ip_addr_avail_lock);
14936 /* Mark it up, and increment counters. */
14937 ipif->ipif_flags |= IPIF_UP;
14938 ill->ill_ipif_up_count++;
14939 err = ip_addr_availability_check(ipif);
14940 mutex_exit(&ipst->ips_ip_addr_avail_lock);
14941 rw_exit(&ipst->ips_ill_g_lock);
14943 if (err != 0) {
14945 * Our address may already be up on the same ill. In this case,
14946 * the ARP entry for our ipif replaced the one for the other
14947 * ipif. So we don't want to delete it (otherwise the other ipif
14948 * would be unable to send packets).
14949 * ip_addr_availability_check() identifies this case for us and
14950 * returns EADDRINUSE; Caller should turn it into EADDRNOTAVAIL
14951 * which is the expected error code.
14953 ill->ill_ipif_up_count--;
14954 ipif->ipif_flags &= ~IPIF_UP;
14955 goto bad;
14959 * Add in all newly created IREs. ire_create_bcast() has
14960 * already checked for duplicates of the IRE_BROADCAST type.
14961 * We add the IRE_INTERFACE before the IRE_LOCAL to ensure
14962 * that lookups find the IRE_LOCAL even if the IRE_INTERFACE is
14963 * a /32 route.
14965 if (ire_if != NULL) {
14966 ire_if = ire_add(ire_if);
14967 if (ire_if == NULL) {
14968 err = ENOMEM;
14969 goto bad2;
14971 #ifdef DEBUG
14972 ire_refhold_notr(ire_if);
14973 ire_refrele(ire_if);
14974 #endif
14976 if (ire_local != NULL) {
14977 ire_local = ire_add(ire_local);
14978 if (ire_local == NULL) {
14979 err = ENOMEM;
14980 goto bad2;
14982 #ifdef DEBUG
14983 ire_refhold_notr(ire_local);
14984 ire_refrele(ire_local);
14985 #endif
14987 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14988 if (ire_local != NULL)
14989 ipif->ipif_ire_local = ire_local;
14990 if (ire_if != NULL)
14991 ipif->ipif_ire_if = ire_if;
14992 rw_exit(&ipst->ips_ill_g_lock);
14993 ire_local = NULL;
14994 ire_if = NULL;
14997 * We first add all of them, and if that succeeds we refrele the
14998 * bunch. That enables us to delete all of them should any of the
14999 * ire_adds fail.
15001 for (irep1 = irep; irep1 > ire_array; ) {
15002 irep1--;
15003 ASSERT(!MUTEX_HELD(&((*irep1)->ire_ill->ill_lock)));
15004 *irep1 = ire_add(*irep1);
15005 if (*irep1 == NULL) {
15006 err = ENOMEM;
15007 goto bad2;
15011 for (irep1 = irep; irep1 > ire_array; ) {
15012 irep1--;
15013 /* refheld by ire_add. */
15014 if (*irep1 != NULL) {
15015 ire_refrele(*irep1);
15016 *irep1 = NULL;
15020 if (!loopback) {
15022 * If the broadcast address has been set, make sure it makes
15023 * sense based on the interface address.
15024 * Only match on ill since we are sharing broadcast addresses.
15026 if ((ipif->ipif_brd_addr != INADDR_ANY) &&
15027 (ipif->ipif_flags & IPIF_BROADCAST)) {
15028 ire_t *ire;
15030 ire = ire_ftable_lookup_v4(ipif->ipif_brd_addr, 0, 0,
15031 IRE_BROADCAST, ipif->ipif_ill, ALL_ZONES,
15032 (MATCH_IRE_TYPE | MATCH_IRE_ILL), 0, ipst, NULL);
15034 if (ire == NULL) {
15036 * If there isn't a matching broadcast IRE,
15037 * revert to the default for this netmask.
15039 ipif->ipif_v6brd_addr = ipv6_all_zeros;
15040 mutex_enter(&ipif->ipif_ill->ill_lock);
15041 ipif_set_default(ipif);
15042 mutex_exit(&ipif->ipif_ill->ill_lock);
15043 } else {
15044 ire_refrele(ire);
15049 return (0);
15051 bad2:
15052 ill->ill_ipif_up_count--;
15053 ipif->ipif_flags &= ~IPIF_UP;
15055 bad:
15056 ip1dbg(("ipif_add_ires: FAILED \n"));
15057 if (ire_local != NULL)
15058 ire_delete(ire_local);
15059 if (ire_if != NULL)
15060 ire_delete(ire_if);
15062 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15063 ire_local = ipif->ipif_ire_local;
15064 ipif->ipif_ire_local = NULL;
15065 ire_if = ipif->ipif_ire_if;
15066 ipif->ipif_ire_if = NULL;
15067 rw_exit(&ipst->ips_ill_g_lock);
15068 if (ire_local != NULL) {
15069 ire_delete(ire_local);
15070 ire_refrele_notr(ire_local);
15072 if (ire_if != NULL) {
15073 ire_delete(ire_if);
15074 ire_refrele_notr(ire_if);
15077 while (irep > ire_array) {
15078 irep--;
15079 if (*irep != NULL) {
15080 ire_delete(*irep);
15083 (void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
15085 return (err);
15088 /* Remove all the IREs created by ipif_add_ires_v4 */
15089 void
15090 ipif_delete_ires_v4(ipif_t *ipif)
15092 ill_t *ill = ipif->ipif_ill;
15093 ip_stack_t *ipst = ill->ill_ipst;
15094 ire_t *ire;
15096 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15097 ire = ipif->ipif_ire_local;
15098 ipif->ipif_ire_local = NULL;
15099 rw_exit(&ipst->ips_ill_g_lock);
15100 if (ire != NULL) {
15102 * Move count to ipif so we don't loose the count due to
15103 * a down/up dance.
15105 atomic_add_32(&ipif->ipif_ib_pkt_count, ire->ire_ib_pkt_count);
15107 ire_delete(ire);
15108 ire_refrele_notr(ire);
15110 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15111 ire = ipif->ipif_ire_if;
15112 ipif->ipif_ire_if = NULL;
15113 rw_exit(&ipst->ips_ill_g_lock);
15114 if (ire != NULL) {
15115 ire_delete(ire);
15116 ire_refrele_notr(ire);
15120 * Delete the broadcast IREs.
15122 if ((ipif->ipif_flags & IPIF_BROADCAST) &&
15123 !(ipif->ipif_flags & IPIF_NOXMIT))
15124 ipif_delete_bcast_ires(ipif);
15128 * Checks for availbility of a usable source address (if there is one) when the
15129 * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
15130 * this selection is done regardless of the destination.
15132 boolean_t
15133 ipif_zone_avail(uint_t ifindex, boolean_t isv6, zoneid_t zoneid,
15134 ip_stack_t *ipst)
15136 ipif_t *ipif = NULL;
15137 ill_t *uill;
15139 ASSERT(ifindex != 0);
15141 uill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
15142 if (uill == NULL)
15143 return (B_FALSE);
15145 mutex_enter(&uill->ill_lock);
15146 for (ipif = uill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15147 if (IPIF_IS_CONDEMNED(ipif))
15148 continue;
15149 if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
15150 continue;
15151 if (!(ipif->ipif_flags & IPIF_UP))
15152 continue;
15153 if (ipif->ipif_zoneid != zoneid)
15154 continue;
15155 if (isv6 ? IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
15156 ipif->ipif_lcl_addr == INADDR_ANY)
15157 continue;
15158 mutex_exit(&uill->ill_lock);
15159 ill_refrele(uill);
15160 return (B_TRUE);
15162 mutex_exit(&uill->ill_lock);
15163 ill_refrele(uill);
15164 return (B_FALSE);
15168 * Find an ipif with a good local address on the ill+zoneid.
15170 ipif_t *
15171 ipif_good_addr(ill_t *ill, zoneid_t zoneid)
15173 ipif_t *ipif;
15175 mutex_enter(&ill->ill_lock);
15176 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15177 if (IPIF_IS_CONDEMNED(ipif))
15178 continue;
15179 if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
15180 continue;
15181 if (!(ipif->ipif_flags & IPIF_UP))
15182 continue;
15183 if (ipif->ipif_zoneid != zoneid &&
15184 ipif->ipif_zoneid != ALL_ZONES && zoneid != ALL_ZONES)
15185 continue;
15186 if (ill->ill_isv6 ?
15187 IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
15188 ipif->ipif_lcl_addr == INADDR_ANY)
15189 continue;
15190 ipif_refhold_locked(ipif);
15191 mutex_exit(&ill->ill_lock);
15192 return (ipif);
15194 mutex_exit(&ill->ill_lock);
15195 return (NULL);
15199 * IP source address type, sorted from worst to best. For a given type,
15200 * always prefer IP addresses on the same subnet. All-zones addresses are
15201 * suboptimal because they pose problems with unlabeled destinations.
15203 typedef enum {
15204 IPIF_NONE,
15205 IPIF_DIFFNET_DEPRECATED, /* deprecated and different subnet */
15206 IPIF_SAMENET_DEPRECATED, /* deprecated and same subnet */
15207 IPIF_DIFFNET_ALLZONES, /* allzones and different subnet */
15208 IPIF_SAMENET_ALLZONES, /* allzones and same subnet */
15209 IPIF_DIFFNET, /* normal and different subnet */
15210 IPIF_SAMENET, /* normal and same subnet */
15211 IPIF_LOCALADDR /* local loopback */
15212 } ipif_type_t;
15215 * Pick the optimal ipif on `ill' for sending to destination `dst' from zone
15216 * `zoneid'. We rate usable ipifs from low -> high as per the ipif_type_t
15217 * enumeration, and return the highest-rated ipif. If there's a tie, we pick
15218 * the first one, unless IPMP is used in which case we round-robin among them;
15219 * see below for more.
15221 * Returns NULL if there is no suitable source address for the ill.
15222 * This only occurs when there is no valid source address for the ill.
15224 ipif_t *
15225 ipif_select_source_v4(ill_t *ill, ipaddr_t dst, zoneid_t zoneid,
15226 boolean_t allow_usesrc, boolean_t *notreadyp)
15228 ill_t *usill = NULL;
15229 ill_t *ipmp_ill = NULL;
15230 ipif_t *start_ipif, *next_ipif, *ipif, *best_ipif;
15231 ipif_type_t type, best_type;
15232 ip_stack_t *ipst = ill->ill_ipst;
15233 boolean_t samenet;
15235 if (ill->ill_usesrc_ifindex != 0 && allow_usesrc) {
15236 usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
15237 B_FALSE, ipst);
15238 if (usill != NULL)
15239 ill = usill; /* Select source from usesrc ILL */
15240 else
15241 return (NULL);
15245 * Test addresses should never be used for source address selection,
15246 * so if we were passed one, switch to the IPMP meta-interface.
15248 if (IS_UNDER_IPMP(ill)) {
15249 if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL)
15250 ill = ipmp_ill; /* Select source from IPMP ill */
15251 else
15252 return (NULL);
15256 * Hold the ill_g_lock as reader. This makes sure that no ipif/ill
15257 * can be deleted. But an ipif/ill can get CONDEMNED any time.
15258 * After selecting the right ipif, under ill_lock make sure ipif is
15259 * not condemned, and increment refcnt. If ipif is CONDEMNED,
15260 * we retry. Inside the loop we still need to check for CONDEMNED,
15261 * but not under a lock.
15263 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
15264 retry:
15266 * For source address selection, we treat the ipif list as circular
15267 * and continue until we get back to where we started. This allows
15268 * IPMP to vary source address selection (which improves inbound load
15269 * spreading) by caching its last ending point and starting from
15270 * there. NOTE: we don't have to worry about ill_src_ipif changing
15271 * ills since that can't happen on the IPMP ill.
15273 start_ipif = ill->ill_ipif;
15274 if (IS_IPMP(ill) && ill->ill_src_ipif != NULL)
15275 start_ipif = ill->ill_src_ipif;
15277 ipif = start_ipif;
15278 best_ipif = NULL;
15279 best_type = IPIF_NONE;
15280 do {
15281 if ((next_ipif = ipif->ipif_next) == NULL)
15282 next_ipif = ill->ill_ipif;
15284 if (IPIF_IS_CONDEMNED(ipif))
15285 continue;
15286 /* Always skip NOLOCAL and ANYCAST interfaces */
15287 if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
15288 continue;
15289 /* Always skip NOACCEPT interfaces */
15290 if (ipif->ipif_ill->ill_flags & ILLF_NOACCEPT)
15291 continue;
15292 if (!(ipif->ipif_flags & IPIF_UP))
15293 continue;
15295 if (!ipif->ipif_addr_ready) {
15296 if (notreadyp != NULL)
15297 *notreadyp = B_TRUE;
15298 continue;
15301 if (zoneid != ALL_ZONES &&
15302 ipif->ipif_zoneid != zoneid &&
15303 ipif->ipif_zoneid != ALL_ZONES)
15304 continue;
15307 * Interfaces with 0.0.0.0 address are allowed to be UP, but
15308 * are not valid as source addresses.
15310 if (ipif->ipif_lcl_addr == INADDR_ANY)
15311 continue;
15313 samenet = ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet);
15315 if (ipif->ipif_lcl_addr == dst) {
15316 type = IPIF_LOCALADDR;
15317 } else if (ipif->ipif_flags & IPIF_DEPRECATED) {
15318 type = samenet ? IPIF_SAMENET_DEPRECATED :
15319 IPIF_DIFFNET_DEPRECATED;
15320 } else if (ipif->ipif_zoneid == ALL_ZONES) {
15321 type = samenet ? IPIF_SAMENET_ALLZONES :
15322 IPIF_DIFFNET_ALLZONES;
15323 } else {
15324 type = samenet ? IPIF_SAMENET : IPIF_DIFFNET;
15327 if (type > best_type) {
15328 best_type = type;
15329 best_ipif = ipif;
15330 if (best_type == IPIF_LOCALADDR)
15331 break; /* can't get better */
15333 } while ((ipif = next_ipif) != start_ipif);
15335 if ((ipif = best_ipif) != NULL) {
15336 mutex_enter(&ipif->ipif_ill->ill_lock);
15337 if (IPIF_IS_CONDEMNED(ipif)) {
15338 mutex_exit(&ipif->ipif_ill->ill_lock);
15339 goto retry;
15341 ipif_refhold_locked(ipif);
15344 * For IPMP, update the source ipif rotor to the next ipif,
15345 * provided we can look it up. (We must not use it if it's
15346 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after
15347 * ipif_free() checked ill_src_ipif.)
15349 if (IS_IPMP(ill) && ipif != NULL) {
15350 next_ipif = ipif->ipif_next;
15351 if (next_ipif != NULL && !IPIF_IS_CONDEMNED(next_ipif))
15352 ill->ill_src_ipif = next_ipif;
15353 else
15354 ill->ill_src_ipif = NULL;
15356 mutex_exit(&ipif->ipif_ill->ill_lock);
15359 rw_exit(&ipst->ips_ill_g_lock);
15360 if (usill != NULL)
15361 ill_refrele(usill);
15362 if (ipmp_ill != NULL)
15363 ill_refrele(ipmp_ill);
15365 #ifdef DEBUG
15366 if (ipif == NULL) {
15367 char buf1[INET6_ADDRSTRLEN];
15369 ip1dbg(("ipif_select_source_v4(%s, %s) -> NULL\n",
15370 ill->ill_name,
15371 inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
15372 } else {
15373 char buf1[INET6_ADDRSTRLEN];
15374 char buf2[INET6_ADDRSTRLEN];
15376 ip1dbg(("ipif_select_source_v4(%s, %s) -> %s\n",
15377 ipif->ipif_ill->ill_name,
15378 inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
15379 inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
15380 buf2, sizeof (buf2))));
15382 #endif /* DEBUG */
15383 return (ipif);
15387 * Pick a source address based on the destination ill and an optional setsrc
15388 * address.
15389 * The result is stored in srcp. If generation is set, then put the source
15390 * generation number there before we look for the source address (to avoid
15391 * missing changes in the set of source addresses.
15392 * If flagsp is set, then us it to pass back ipif_flags.
15394 * If the caller wants to cache the returned source address and detect when
15395 * that might be stale, the caller should pass in a generation argument,
15396 * which the caller can later compare against ips_src_generation
15398 * The precedence order for selecting an IPv4 source address is:
15399 * - RTF_SETSRC on the offlink ire always wins.
15400 * - If usrsrc is set, swap the ill to be the usesrc one.
15401 * - If IPMP is used on the ill, select a random address from the most
15402 * preferred ones below:
15403 * 1. If onlink destination, same subnet and not deprecated, not ALL_ZONES
15404 * 2. Not deprecated, not ALL_ZONES
15405 * 3. If onlink destination, same subnet and not deprecated, ALL_ZONES
15406 * 4. Not deprecated, ALL_ZONES
15407 * 5. If onlink destination, same subnet and deprecated
15408 * 6. Deprecated.
15410 * We have lower preference for ALL_ZONES IP addresses,
15411 * as they pose problems with unlabeled destinations.
15413 * Note that when multiple IP addresses match e.g., #1 we pick
15414 * the first one if IPMP is not in use. With IPMP we randomize.
15417 ip_select_source_v4(ill_t *ill, ipaddr_t setsrc, ipaddr_t dst,
15418 ipaddr_t multicast_ifaddr,
15419 zoneid_t zoneid, ip_stack_t *ipst, ipaddr_t *srcp,
15420 uint32_t *generation, uint64_t *flagsp)
15422 ipif_t *ipif;
15423 boolean_t notready = B_FALSE; /* Set if !ipif_addr_ready found */
15425 if (flagsp != NULL)
15426 *flagsp = 0;
15429 * Need to grab the generation number before we check to
15430 * avoid a race with a change to the set of local addresses.
15431 * No lock needed since the thread which updates the set of local
15432 * addresses use ipif/ill locks and exit those (hence a store memory
15433 * barrier) before doing the atomic increase of ips_src_generation.
15435 if (generation != NULL) {
15436 *generation = ipst->ips_src_generation;
15439 if (CLASSD(dst) && multicast_ifaddr != INADDR_ANY) {
15440 *srcp = multicast_ifaddr;
15441 return (0);
15444 /* Was RTF_SETSRC set on the first IRE in the recursive lookup? */
15445 if (setsrc != INADDR_ANY) {
15446 *srcp = setsrc;
15447 return (0);
15449 ipif = ipif_select_source_v4(ill, dst, zoneid, B_TRUE, &notready);
15450 if (ipif == NULL) {
15451 if (notready)
15452 return (ENETDOWN);
15453 else
15454 return (EADDRNOTAVAIL);
15456 *srcp = ipif->ipif_lcl_addr;
15457 if (flagsp != NULL)
15458 *flagsp = ipif->ipif_flags;
15459 ipif_refrele(ipif);
15460 return (0);
15463 /* ARGSUSED */
15465 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15466 ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15469 * ill_phyint_reinit merged the v4 and v6 into a single
15470 * ipsq. We might not have been able to complete the
15471 * operation in ipif_set_values, if we could not become
15472 * exclusive. If so restart it here.
15474 return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15478 * Can operate on either a module or a driver queue.
15479 * Returns an error if not a module queue.
15481 /* ARGSUSED */
15483 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15484 ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15486 queue_t *q1 = q;
15487 char *cp;
15488 char interf_name[LIFNAMSIZ];
15489 uint_t ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
15491 if (q->q_next == NULL) {
15492 ip1dbg((
15493 "if_unitsel: IF_UNITSEL: no q_next\n"));
15494 return (EINVAL);
15497 if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
15498 return (EALREADY);
15500 do {
15501 q1 = q1->q_next;
15502 } while (q1->q_next);
15503 cp = q1->q_qinfo->qi_minfo->mi_idname;
15504 (void) sprintf(interf_name, "%s%d", cp, ppa);
15507 * Here we are not going to delay the ioack until after
15508 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
15509 * original ioctl message before sending the requests.
15511 return (ipif_set_values(q, mp, interf_name, &ppa));
15514 /* ARGSUSED */
15516 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15517 ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15519 return (ENXIO);
15523 * Create any IRE_BROADCAST entries for `ipif', and store those entries in
15524 * `irep'. Returns a pointer to the next free `irep' entry
15525 * A mirror exists in ipif_delete_bcast_ires().
15527 * The management of any "extra" or seemingly duplicate IRE_BROADCASTs is
15528 * done in ire_add.
15530 static ire_t **
15531 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
15533 ipaddr_t addr;
15534 ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
15535 ipaddr_t subnetmask = ipif->ipif_net_mask;
15536 ill_t *ill = ipif->ipif_ill;
15537 zoneid_t zoneid = ipif->ipif_zoneid;
15539 ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
15541 ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15542 ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15544 if (ipif->ipif_lcl_addr == INADDR_ANY ||
15545 (ipif->ipif_flags & IPIF_NOLOCAL))
15546 netmask = htonl(IN_CLASSA_NET); /* fallback */
15548 irep = ire_create_bcast(ill, 0, zoneid, irep);
15549 irep = ire_create_bcast(ill, INADDR_BROADCAST, zoneid, irep);
15552 * For backward compatibility, we create net broadcast IREs based on
15553 * the old "IP address class system", since some old machines only
15554 * respond to these class derived net broadcast. However, we must not
15555 * create these net broadcast IREs if the subnetmask is shorter than
15556 * the IP address class based derived netmask. Otherwise, we may
15557 * create a net broadcast address which is the same as an IP address
15558 * on the subnet -- and then TCP will refuse to talk to that address.
15560 if (netmask < subnetmask) {
15561 addr = netmask & ipif->ipif_subnet;
15562 irep = ire_create_bcast(ill, addr, zoneid, irep);
15563 irep = ire_create_bcast(ill, ~netmask | addr, zoneid, irep);
15567 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15568 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15569 * created. Creating these broadcast IREs will only create confusion
15570 * as `addr' will be the same as the IP address.
15572 if (subnetmask != 0xFFFFFFFF) {
15573 addr = ipif->ipif_subnet;
15574 irep = ire_create_bcast(ill, addr, zoneid, irep);
15575 irep = ire_create_bcast(ill, ~subnetmask | addr, zoneid, irep);
15578 return (irep);
15582 * Mirror of ipif_create_bcast_ires()
15584 static void
15585 ipif_delete_bcast_ires(ipif_t *ipif)
15587 ipaddr_t addr;
15588 ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
15589 ipaddr_t subnetmask = ipif->ipif_net_mask;
15590 ill_t *ill = ipif->ipif_ill;
15591 zoneid_t zoneid = ipif->ipif_zoneid;
15592 ire_t *ire;
15594 ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15595 ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15597 if (ipif->ipif_lcl_addr == INADDR_ANY ||
15598 (ipif->ipif_flags & IPIF_NOLOCAL))
15599 netmask = htonl(IN_CLASSA_NET); /* fallback */
15601 ire = ire_lookup_bcast(ill, 0, zoneid);
15602 ASSERT(ire != NULL);
15603 ire_delete(ire); ire_refrele(ire);
15604 ire = ire_lookup_bcast(ill, INADDR_BROADCAST, zoneid);
15605 ASSERT(ire != NULL);
15606 ire_delete(ire); ire_refrele(ire);
15609 * For backward compatibility, we create net broadcast IREs based on
15610 * the old "IP address class system", since some old machines only
15611 * respond to these class derived net broadcast. However, we must not
15612 * create these net broadcast IREs if the subnetmask is shorter than
15613 * the IP address class based derived netmask. Otherwise, we may
15614 * create a net broadcast address which is the same as an IP address
15615 * on the subnet -- and then TCP will refuse to talk to that address.
15617 if (netmask < subnetmask) {
15618 addr = netmask & ipif->ipif_subnet;
15619 ire = ire_lookup_bcast(ill, addr, zoneid);
15620 ASSERT(ire != NULL);
15621 ire_delete(ire); ire_refrele(ire);
15622 ire = ire_lookup_bcast(ill, ~netmask | addr, zoneid);
15623 ASSERT(ire != NULL);
15624 ire_delete(ire); ire_refrele(ire);
15628 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15629 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15630 * created. Creating these broadcast IREs will only create confusion
15631 * as `addr' will be the same as the IP address.
15633 if (subnetmask != 0xFFFFFFFF) {
15634 addr = ipif->ipif_subnet;
15635 ire = ire_lookup_bcast(ill, addr, zoneid);
15636 ASSERT(ire != NULL);
15637 ire_delete(ire); ire_refrele(ire);
15638 ire = ire_lookup_bcast(ill, ~subnetmask | addr, zoneid);
15639 ASSERT(ire != NULL);
15640 ire_delete(ire); ire_refrele(ire);
15645 * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
15646 * from lifr_flags and the name from lifr_name.
15647 * Set IFF_IPV* and ill_isv6 prior to doing the lookup
15648 * since ipif_lookup_on_name uses the _isv6 flags when matching.
15649 * Returns EINPROGRESS when mp has been consumed by queueing it on
15650 * ipx_pending_mp and the ioctl will complete in ip_rput.
15652 * Can operate on either a module or a driver queue.
15653 * Returns an error if not a module queue.
15655 /* ARGSUSED */
15657 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15658 ip_ioctl_cmd_t *ipip, void *if_req)
15660 ill_t *ill = q->q_ptr;
15661 phyint_t *phyi;
15662 ip_stack_t *ipst;
15663 struct lifreq *lifr = if_req;
15664 uint64_t new_flags;
15666 ASSERT(ipif != NULL);
15667 ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
15669 if (q->q_next == NULL) {
15670 ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
15671 return (EINVAL);
15675 * If we are not writer on 'q' then this interface exists already
15676 * and previous lookups (ip_extract_lifreq()) found this ipif --
15677 * so return EALREADY.
15679 if (ill != ipif->ipif_ill)
15680 return (EALREADY);
15682 if (ill->ill_name[0] != '\0')
15683 return (EALREADY);
15686 * If there's another ill already with the requested name, ensure
15687 * that it's of the same type. Otherwise, ill_phyint_reinit() will
15688 * fuse together two unrelated ills, which will cause chaos.
15690 ipst = ill->ill_ipst;
15691 phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
15692 lifr->lifr_name, NULL);
15693 if (phyi != NULL) {
15694 ill_t *ill_mate = phyi->phyint_illv4;
15696 if (ill_mate == NULL)
15697 ill_mate = phyi->phyint_illv6;
15698 ASSERT(ill_mate != NULL);
15700 if (ill_mate->ill_media->ip_m_mac_type !=
15701 ill->ill_media->ip_m_mac_type) {
15702 ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
15703 "use the same ill name on differing media\n"));
15704 return (EINVAL);
15709 * We start off as IFF_IPV4 in ipif_allocate and become
15710 * IFF_IPV4 or IFF_IPV6 here depending on lifr_flags value.
15711 * The only flags that we read from user space are IFF_IPV4,
15712 * IFF_IPV6, and IFF_BROADCAST.
15714 * This ill has not been inserted into the global list.
15715 * So we are still single threaded and don't need any lock
15717 * Saniy check the flags.
15720 if ((lifr->lifr_flags & IFF_BROADCAST) &&
15721 ((lifr->lifr_flags & IFF_IPV6) ||
15722 (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
15723 ip1dbg(("ip_sioctl_slifname: link not broadcast capable "
15724 "or IPv6 i.e., no broadcast \n"));
15725 return (EINVAL);
15728 new_flags =
15729 lifr->lifr_flags & (IFF_IPV6|IFF_IPV4|IFF_BROADCAST);
15731 if ((new_flags ^ (IFF_IPV6|IFF_IPV4)) == 0) {
15732 ip1dbg(("ip_sioctl_slifname: flags must be exactly one of "
15733 "IFF_IPV4 or IFF_IPV6\n"));
15734 return (EINVAL);
15738 * We always start off as IPv4, so only need to check for IPv6.
15740 if ((new_flags & IFF_IPV6) != 0) {
15741 ill->ill_flags |= ILLF_IPV6;
15742 ill->ill_flags &= ~ILLF_IPV4;
15744 if (lifr->lifr_flags & IFF_NOLINKLOCAL)
15745 ill->ill_flags |= ILLF_NOLINKLOCAL;
15748 if ((new_flags & IFF_BROADCAST) != 0)
15749 ipif->ipif_flags |= IPIF_BROADCAST;
15750 else
15751 ipif->ipif_flags &= ~IPIF_BROADCAST;
15753 /* We started off as V4. */
15754 if (ill->ill_flags & ILLF_IPV6) {
15755 ill->ill_phyint->phyint_illv6 = ill;
15756 ill->ill_phyint->phyint_illv4 = NULL;
15759 return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
15762 /* ARGSUSED */
15764 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15765 ip_ioctl_cmd_t *ipip, void *if_req)
15768 * ill_phyint_reinit merged the v4 and v6 into a single
15769 * ipsq. We might not have been able to complete the
15770 * slifname in ipif_set_values, if we could not become
15771 * exclusive. If so restart it here
15773 return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15777 * Return a pointer to the ipif which matches the index, IP version type and
15778 * zoneid.
15780 ipif_t *
15781 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
15782 ip_stack_t *ipst)
15784 ill_t *ill;
15785 ipif_t *ipif = NULL;
15787 ill = ill_lookup_on_ifindex(index, isv6, ipst);
15788 if (ill != NULL) {
15789 mutex_enter(&ill->ill_lock);
15790 for (ipif = ill->ill_ipif; ipif != NULL;
15791 ipif = ipif->ipif_next) {
15792 if (!IPIF_IS_CONDEMNED(ipif) && (zoneid == ALL_ZONES ||
15793 zoneid == ipif->ipif_zoneid ||
15794 ipif->ipif_zoneid == ALL_ZONES)) {
15795 ipif_refhold_locked(ipif);
15796 break;
15799 mutex_exit(&ill->ill_lock);
15800 ill_refrele(ill);
15802 return (ipif);
15806 * Change an existing physical interface's index. If the new index
15807 * is acceptable we update the index and the phyint_list_avl_by_index tree.
15808 * Finally, we update other systems which may have a dependence on the
15809 * index value.
15811 /* ARGSUSED */
15813 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15814 ip_ioctl_cmd_t *ipip, void *ifreq)
15816 ill_t *ill;
15817 phyint_t *phyi;
15818 struct ifreq *ifr = (struct ifreq *)ifreq;
15819 struct lifreq *lifr = (struct lifreq *)ifreq;
15820 uint_t old_index, index;
15821 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
15822 avl_index_t where;
15824 if (ipip->ipi_cmd_type == IF_CMD)
15825 index = ifr->ifr_index;
15826 else
15827 index = lifr->lifr_index;
15830 * Only allow on physical interface. Also, index zero is illegal.
15832 ill = ipif->ipif_ill;
15833 phyi = ill->ill_phyint;
15834 if (ipif->ipif_id != 0 || index == 0 || index > IF_INDEX_MAX) {
15835 return (EINVAL);
15838 /* If the index is not changing, no work to do */
15839 if (phyi->phyint_ifindex == index)
15840 return (0);
15843 * Use phyint_exists() to determine if the new interface index
15844 * is already in use. If the index is unused then we need to
15845 * change the phyint's position in the phyint_list_avl_by_index
15846 * tree. If we do not do this, subsequent lookups (using the new
15847 * index value) will not find the phyint.
15849 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15850 if (phyint_exists(index, ipst)) {
15851 rw_exit(&ipst->ips_ill_g_lock);
15852 return (EEXIST);
15856 * The new index is unused. Set it in the phyint. However we must not
15857 * forget to trigger NE_IFINDEX_CHANGE event before the ifindex
15858 * changes. The event must be bound to old ifindex value.
15860 ill_nic_event_dispatch(ill, 0, NE_IFINDEX_CHANGE,
15861 &index, sizeof (index));
15863 old_index = phyi->phyint_ifindex;
15864 phyi->phyint_ifindex = index;
15866 avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, phyi);
15867 (void) avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15868 &index, &where);
15869 avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15870 phyi, where);
15871 rw_exit(&ipst->ips_ill_g_lock);
15873 /* Update SCTP's ILL list */
15874 sctp_ill_reindex(ill, old_index);
15876 /* Send the routing sockets message */
15877 ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
15878 if (ILL_OTHER(ill))
15879 ip_rts_ifmsg(ILL_OTHER(ill)->ill_ipif, RTSQ_DEFAULT);
15881 /* Perhaps ilgs should use this ill */
15882 update_conn_ill(NULL, ill->ill_ipst);
15883 return (0);
15886 /* ARGSUSED */
15888 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15889 ip_ioctl_cmd_t *ipip, void *ifreq)
15891 struct ifreq *ifr = (struct ifreq *)ifreq;
15892 struct lifreq *lifr = (struct lifreq *)ifreq;
15894 ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
15895 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15896 /* Get the interface index */
15897 if (ipip->ipi_cmd_type == IF_CMD) {
15898 ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
15899 } else {
15900 lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
15902 return (0);
15905 /* ARGSUSED */
15907 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15908 ip_ioctl_cmd_t *ipip, void *ifreq)
15910 struct lifreq *lifr = (struct lifreq *)ifreq;
15912 ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
15913 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15914 /* Get the interface zone */
15915 ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15916 lifr->lifr_zoneid = ipif->ipif_zoneid;
15917 return (0);
15921 * Set the zoneid of an interface.
15923 /* ARGSUSED */
15925 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15926 ip_ioctl_cmd_t *ipip, void *ifreq)
15928 struct lifreq *lifr = (struct lifreq *)ifreq;
15929 int err = 0;
15930 boolean_t need_up = B_FALSE;
15931 zone_t *zptr;
15932 zone_status_t status;
15933 zoneid_t zoneid;
15935 ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15936 if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
15937 return (ENOTSUP);
15939 /* cannot assign instance zero to a non-global zone */
15940 if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
15941 return (ENOTSUP);
15944 * Cannot assign to a zone that doesn't exist or is shutting down. In
15945 * the event of a race with the zone shutdown processing, since IP
15946 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
15947 * interface will be cleaned up even if the zone is shut down
15948 * immediately after the status check. If the interface can't be brought
15949 * down right away, and the zone is shut down before the restart
15950 * function is called, we resolve the possible races by rechecking the
15951 * zone status in the restart function.
15953 if ((zptr = zone_find_by_id(zoneid)) == NULL)
15954 return (EINVAL);
15955 status = zone_status_get(zptr);
15956 zone_rele(zptr);
15958 if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
15959 return (EINVAL);
15961 if (ipif->ipif_flags & IPIF_UP) {
15963 * If the interface is already marked up,
15964 * we call ipif_down which will take care
15965 * of ditching any IREs that have been set
15966 * up based on the old interface address.
15968 err = ipif_logical_down(ipif, q, mp);
15969 if (err == EINPROGRESS)
15970 return (err);
15971 (void) ipif_down_tail(ipif);
15972 need_up = B_TRUE;
15975 err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
15976 return (err);
15979 static int
15980 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
15981 queue_t *q, mblk_t *mp, boolean_t need_up)
15983 int err = 0;
15984 ip_stack_t *ipst;
15986 ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
15987 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15989 if (CONN_Q(q))
15990 ipst = CONNQ_TO_IPST(q);
15991 else
15992 ipst = ILLQ_TO_IPST(q);
15995 * For exclusive stacks we don't allow a different zoneid than
15996 * global.
15998 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
15999 zoneid != GLOBAL_ZONEID)
16000 return (EINVAL);
16002 /* Set the new zone id. */
16003 ipif->ipif_zoneid = zoneid;
16005 /* Update sctp list */
16006 sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
16008 /* The default multicast interface might have changed */
16009 ire_increment_multicast_generation(ipst, ipif->ipif_ill->ill_isv6);
16011 if (need_up) {
16013 * Now bring the interface back up. If this
16014 * is the only IPIF for the ILL, ipif_up
16015 * will have to re-bind to the device, so
16016 * we may get back EINPROGRESS, in which
16017 * case, this IOCTL will get completed in
16018 * ip_rput_dlpi when we see the DL_BIND_ACK.
16020 err = ipif_up(ipif, q, mp);
16022 return (err);
16025 /* ARGSUSED */
16027 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16028 ip_ioctl_cmd_t *ipip, void *if_req)
16030 struct lifreq *lifr = (struct lifreq *)if_req;
16031 zoneid_t zoneid;
16032 zone_t *zptr;
16033 zone_status_t status;
16035 ASSERT(ipip->ipi_cmd_type == LIF_CMD);
16036 if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
16037 zoneid = GLOBAL_ZONEID;
16039 ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
16040 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
16043 * We recheck the zone status to resolve the following race condition:
16044 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
16045 * 2) hme0:1 is up and can't be brought down right away;
16046 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
16047 * 3) zone "myzone" is halted; the zone status switches to
16048 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
16049 * the interfaces to remove - hme0:1 is not returned because it's not
16050 * yet in "myzone", so it won't be removed;
16051 * 4) the restart function for SIOCSLIFZONE is called; without the
16052 * status check here, we would have hme0:1 in "myzone" after it's been
16053 * destroyed.
16054 * Note that if the status check fails, we need to bring the interface
16055 * back to its state prior to ip_sioctl_slifzone(), hence the call to
16056 * ipif_up_done[_v6]().
16058 status = ZONE_IS_UNINITIALIZED;
16059 if ((zptr = zone_find_by_id(zoneid)) != NULL) {
16060 status = zone_status_get(zptr);
16061 zone_rele(zptr);
16063 if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
16064 if (ipif->ipif_isv6) {
16065 (void) ipif_up_done_v6(ipif);
16066 } else {
16067 (void) ipif_up_done(ipif);
16069 return (EINVAL);
16072 (void) ipif_down_tail(ipif);
16074 return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
16075 B_TRUE));
16079 * Return the number of addresses on `ill' with one or more of the values
16080 * in `set' set and all of the values in `clear' clear.
16082 static uint_t
16083 ill_flagaddr_cnt(const ill_t *ill, uint64_t set, uint64_t clear)
16085 ipif_t *ipif;
16086 uint_t cnt = 0;
16088 ASSERT(IAM_WRITER_ILL(ill));
16090 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
16091 if ((ipif->ipif_flags & set) && !(ipif->ipif_flags & clear))
16092 cnt++;
16094 return (cnt);
16098 * Return the number of migratable addresses on `ill' that are under
16099 * application control.
16101 uint_t
16102 ill_appaddr_cnt(const ill_t *ill)
16104 return (ill_flagaddr_cnt(ill, IPIF_DHCPRUNNING | IPIF_ADDRCONF,
16105 IPIF_NOFAILOVER));
16109 * Return the number of point-to-point addresses on `ill'.
16111 uint_t
16112 ill_ptpaddr_cnt(const ill_t *ill)
16114 return (ill_flagaddr_cnt(ill, IPIF_POINTOPOINT, 0));
16117 /* ARGSUSED */
16119 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16120 ip_ioctl_cmd_t *ipip, void *ifreq)
16122 struct lifreq *lifr = ifreq;
16124 ASSERT(q->q_next == NULL);
16125 ASSERT(CONN_Q(q));
16127 ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
16128 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
16129 lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
16130 ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
16132 return (0);
16135 /* Find the previous ILL in this usesrc group */
16136 static ill_t *
16137 ill_prev_usesrc(ill_t *uill)
16139 ill_t *ill;
16141 for (ill = uill->ill_usesrc_grp_next;
16142 ASSERT(ill), ill->ill_usesrc_grp_next != uill;
16143 ill = ill->ill_usesrc_grp_next)
16144 /* do nothing */;
16145 return (ill);
16149 * Release all members of the usesrc group. This routine is called
16150 * from ill_delete when the interface being unplumbed is the
16151 * group head.
16153 * This silently clears the usesrc that ifconfig setup.
16154 * An alternative would be to keep that ifindex, and drop packets on the floor
16155 * since no source address can be selected.
16156 * Even if we keep the current semantics, don't need a lock and a linked list.
16157 * Can walk all the ills checking if they have a ill_usesrc_ifindex matching
16158 * the one that is being removed. Issue is how we return the usesrc users
16159 * (SIOCGLIFSRCOF). We want to be able to find the ills which have an
16160 * ill_usesrc_ifindex matching a target ill. We could also do that with an
16161 * ill walk, but the walker would need to insert in the ioctl response.
16163 static void
16164 ill_disband_usesrc_group(ill_t *uill)
16166 ill_t *next_ill, *tmp_ill;
16167 ip_stack_t *ipst = uill->ill_ipst;
16169 ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
16170 next_ill = uill->ill_usesrc_grp_next;
16172 do {
16173 ASSERT(next_ill != NULL);
16174 tmp_ill = next_ill->ill_usesrc_grp_next;
16175 ASSERT(tmp_ill != NULL);
16176 next_ill->ill_usesrc_grp_next = NULL;
16177 next_ill->ill_usesrc_ifindex = 0;
16178 next_ill = tmp_ill;
16179 } while (next_ill->ill_usesrc_ifindex != 0);
16180 uill->ill_usesrc_grp_next = NULL;
16184 * Remove the client usesrc ILL from the list and relink to a new list
16187 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
16189 ill_t *ill, *tmp_ill;
16190 ip_stack_t *ipst = ucill->ill_ipst;
16192 ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
16193 (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
16196 * Check if the usesrc client ILL passed in is not already
16197 * in use as a usesrc ILL i.e one whose source address is
16198 * in use OR a usesrc ILL is not already in use as a usesrc
16199 * client ILL
16201 if ((ucill->ill_usesrc_ifindex == 0) ||
16202 (uill->ill_usesrc_ifindex != 0)) {
16203 return (-1);
16206 ill = ill_prev_usesrc(ucill);
16207 ASSERT(ill->ill_usesrc_grp_next != NULL);
16209 /* Remove from the current list */
16210 if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
16211 /* Only two elements in the list */
16212 ASSERT(ill->ill_usesrc_ifindex == 0);
16213 ill->ill_usesrc_grp_next = NULL;
16214 } else {
16215 ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
16218 if (ifindex == 0) {
16219 ucill->ill_usesrc_ifindex = 0;
16220 ucill->ill_usesrc_grp_next = NULL;
16221 return (0);
16224 ucill->ill_usesrc_ifindex = ifindex;
16225 tmp_ill = uill->ill_usesrc_grp_next;
16226 uill->ill_usesrc_grp_next = ucill;
16227 ucill->ill_usesrc_grp_next =
16228 (tmp_ill != NULL) ? tmp_ill : uill;
16229 return (0);
16233 * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
16234 * ip.c for locking details.
16236 /* ARGSUSED */
16238 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16239 ip_ioctl_cmd_t *ipip, void *ifreq)
16241 struct lifreq *lifr = (struct lifreq *)ifreq;
16242 boolean_t isv6 = B_FALSE, reset_flg = B_FALSE;
16243 ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
16244 int err = 0, ret;
16245 uint_t ifindex;
16246 ipsq_t *ipsq = NULL;
16247 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
16249 ASSERT(IAM_WRITER_IPIF(ipif));
16250 ASSERT(q->q_next == NULL);
16251 ASSERT(CONN_Q(q));
16253 isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
16255 ifindex = lifr->lifr_index;
16256 if (ifindex == 0) {
16257 if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
16258 /* non usesrc group interface, nothing to reset */
16259 return (0);
16261 ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
16262 /* valid reset request */
16263 reset_flg = B_TRUE;
16266 usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
16267 if (usesrc_ill == NULL)
16268 return (ENXIO);
16269 if (usesrc_ill == ipif->ipif_ill) {
16270 ill_refrele(usesrc_ill);
16271 return (EINVAL);
16274 ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
16275 NEW_OP, B_TRUE);
16276 if (ipsq == NULL) {
16277 err = EINPROGRESS;
16278 /* Operation enqueued on the ipsq of the usesrc ILL */
16279 goto done;
16282 /* USESRC isn't currently supported with IPMP */
16283 if (IS_IPMP(usesrc_ill) || IS_UNDER_IPMP(usesrc_ill)) {
16284 err = ENOTSUP;
16285 goto done;
16289 * USESRC isn't compatible with the STANDBY flag. (STANDBY is only
16290 * used by IPMP underlying interfaces, but someone might think it's
16291 * more general and try to use it independently with VNI.)
16293 if (usesrc_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
16294 err = ENOTSUP;
16295 goto done;
16299 * If the client is already in use as a usesrc_ill or a usesrc_ill is
16300 * already a client then return EINVAL
16302 if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
16303 err = EINVAL;
16304 goto done;
16308 * If the ill_usesrc_ifindex field is already set to what it needs to
16309 * be then this is a duplicate operation.
16311 if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
16312 err = 0;
16313 goto done;
16316 ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
16317 " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
16318 usesrc_ill->ill_isv6));
16321 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
16322 * and the ill_usesrc_ifindex fields
16324 rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
16326 if (reset_flg) {
16327 ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
16328 if (ret != 0) {
16329 err = EINVAL;
16331 rw_exit(&ipst->ips_ill_g_usesrc_lock);
16332 goto done;
16336 * Four possibilities to consider:
16337 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
16338 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
16339 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
16340 * 4. Both are part of their respective usesrc groups
16342 if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
16343 (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
16344 ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
16345 usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
16346 usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
16347 usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
16348 } else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
16349 (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
16350 usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
16351 /* Insert at head of list */
16352 usesrc_cli_ill->ill_usesrc_grp_next =
16353 usesrc_ill->ill_usesrc_grp_next;
16354 usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
16355 } else {
16356 ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
16357 ifindex);
16358 if (ret != 0)
16359 err = EINVAL;
16361 rw_exit(&ipst->ips_ill_g_usesrc_lock);
16363 done:
16364 if (ipsq != NULL)
16365 ipsq_exit(ipsq);
16366 /* The refrele on the lifr_name ipif is done by ip_process_ioctl */
16367 ill_refrele(usesrc_ill);
16369 /* Let conn_ixa caching know that source address selection changed */
16370 ip_update_source_selection(ipst);
16372 return (err);
16375 /* ARGSUSED */
16377 ip_sioctl_get_dadstate(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16378 ip_ioctl_cmd_t *ipip, void *if_req)
16380 struct lifreq *lifr = (struct lifreq *)if_req;
16381 ill_t *ill = ipif->ipif_ill;
16384 * Need a lock since IFF_UP can be set even when there are
16385 * references to the ipif.
16387 mutex_enter(&ill->ill_lock);
16388 if ((ipif->ipif_flags & IPIF_UP) && ipif->ipif_addr_ready == 0)
16389 lifr->lifr_dadstate = DAD_IN_PROGRESS;
16390 else
16391 lifr->lifr_dadstate = DAD_DONE;
16392 mutex_exit(&ill->ill_lock);
16393 return (0);
16397 * comparison function used by avl.
16399 static int
16400 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
16403 uint_t index;
16405 ASSERT(phyip != NULL && index_ptr != NULL);
16407 index = *((uint_t *)index_ptr);
16409 * let the phyint with the lowest index be on top.
16411 if (((phyint_t *)phyip)->phyint_ifindex < index)
16412 return (1);
16413 if (((phyint_t *)phyip)->phyint_ifindex > index)
16414 return (-1);
16415 return (0);
16419 * comparison function used by avl.
16421 static int
16422 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
16424 ill_t *ill;
16425 int res = 0;
16427 ASSERT(phyip != NULL && name_ptr != NULL);
16429 if (((phyint_t *)phyip)->phyint_illv4)
16430 ill = ((phyint_t *)phyip)->phyint_illv4;
16431 else
16432 ill = ((phyint_t *)phyip)->phyint_illv6;
16433 ASSERT(ill != NULL);
16435 res = strcmp(ill->ill_name, (char *)name_ptr);
16436 if (res > 0)
16437 return (1);
16438 else if (res < 0)
16439 return (-1);
16440 return (0);
16444 * This function is called on the unplumb path via ill_glist_delete() when
16445 * there are no ills left on the phyint and thus the phyint can be freed.
16447 static void
16448 phyint_free(phyint_t *phyi)
16450 ip_stack_t *ipst = PHYINT_TO_IPST(phyi);
16452 ASSERT(phyi->phyint_illv4 == NULL && phyi->phyint_illv6 == NULL);
16455 * If this phyint was an IPMP meta-interface, blow away the group.
16456 * This is safe to do because all of the illgrps have already been
16457 * removed by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find us.
16458 * If we're cleaning up as a result of failed initialization,
16459 * phyint_grp may be NULL.
16461 if ((phyi->phyint_flags & PHYI_IPMP) && (phyi->phyint_grp != NULL)) {
16462 rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16463 ipmp_grp_destroy(phyi->phyint_grp);
16464 phyi->phyint_grp = NULL;
16465 rw_exit(&ipst->ips_ipmp_lock);
16469 * If this interface was under IPMP, take it out of the group.
16471 if (phyi->phyint_grp != NULL)
16472 ipmp_phyint_leave_grp(phyi);
16475 * Delete the phyint and disassociate its ipsq. The ipsq itself
16476 * will be freed in ipsq_exit().
16478 phyi->phyint_ipsq->ipsq_phyint = NULL;
16479 phyi->phyint_name[0] = '\0';
16481 mi_free(phyi);
16485 * Attach the ill to the phyint structure which can be shared by both
16486 * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
16487 * function is called from ipif_set_values and ill_lookup_on_name (for
16488 * loopback) where we know the name of the ill. We lookup the ill and if
16489 * there is one present already with the name use that phyint. Otherwise
16490 * reuse the one allocated by ill_init.
16492 static void
16493 ill_phyint_reinit(ill_t *ill)
16495 boolean_t isv6 = ill->ill_isv6;
16496 phyint_t *phyi_old;
16497 phyint_t *phyi;
16498 avl_index_t where = 0;
16499 ill_t *ill_other = NULL;
16500 ip_stack_t *ipst = ill->ill_ipst;
16502 ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16504 phyi_old = ill->ill_phyint;
16505 ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
16506 phyi_old->phyint_illv6 == NULL));
16507 ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
16508 phyi_old->phyint_illv4 == NULL));
16509 ASSERT(phyi_old->phyint_ifindex == 0);
16512 * Now that our ill has a name, set it in the phyint.
16514 (void) strlcpy(ill->ill_phyint->phyint_name, ill->ill_name, LIFNAMSIZ);
16516 phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16517 ill->ill_name, &where);
16520 * 1. We grabbed the ill_g_lock before inserting this ill into
16521 * the global list of ills. So no other thread could have located
16522 * this ill and hence the ipsq of this ill is guaranteed to be empty.
16523 * 2. Now locate the other protocol instance of this ill.
16524 * 3. Now grab both ill locks in the right order, and the phyint lock of
16525 * the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
16526 * of neither ill can change.
16527 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
16528 * other ill.
16529 * 5. Release all locks.
16533 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
16534 * we are initializing IPv4.
16536 if (phyi != NULL) {
16537 ill_other = (isv6) ? phyi->phyint_illv4 : phyi->phyint_illv6;
16538 ASSERT(ill_other->ill_phyint != NULL);
16539 ASSERT((isv6 && !ill_other->ill_isv6) ||
16540 (!isv6 && ill_other->ill_isv6));
16541 GRAB_ILL_LOCKS(ill, ill_other);
16543 * We are potentially throwing away phyint_flags which
16544 * could be different from the one that we obtain from
16545 * ill_other->ill_phyint. But it is okay as we are assuming
16546 * that the state maintained within IP is correct.
16548 mutex_enter(&phyi->phyint_lock);
16549 if (isv6) {
16550 ASSERT(phyi->phyint_illv6 == NULL);
16551 phyi->phyint_illv6 = ill;
16552 } else {
16553 ASSERT(phyi->phyint_illv4 == NULL);
16554 phyi->phyint_illv4 = ill;
16558 * Delete the old phyint and make its ipsq eligible
16559 * to be freed in ipsq_exit().
16561 phyi_old->phyint_illv4 = NULL;
16562 phyi_old->phyint_illv6 = NULL;
16563 phyi_old->phyint_ipsq->ipsq_phyint = NULL;
16564 phyi_old->phyint_name[0] = '\0';
16565 mi_free(phyi_old);
16566 } else {
16567 mutex_enter(&ill->ill_lock);
16569 * We don't need to acquire any lock, since
16570 * the ill is not yet visible globally and we
16571 * have not yet released the ill_g_lock.
16573 phyi = phyi_old;
16574 mutex_enter(&phyi->phyint_lock);
16575 /* XXX We need a recovery strategy here. */
16576 if (!phyint_assign_ifindex(phyi, ipst))
16577 cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
16579 avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16580 (void *)phyi, where);
16582 (void) avl_find(&ipst->ips_phyint_g_list->
16583 phyint_list_avl_by_index,
16584 &phyi->phyint_ifindex, &where);
16585 avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16586 (void *)phyi, where);
16590 * Reassigning ill_phyint automatically reassigns the ipsq also.
16591 * pending mp is not affected because that is per ill basis.
16593 ill->ill_phyint = phyi;
16596 * Now that the phyint's ifindex has been assigned, complete the
16597 * remaining
16599 ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
16600 if (ill->ill_isv6) {
16601 ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
16602 ill->ill_phyint->phyint_ifindex;
16603 ill->ill_mcast_type = ipst->ips_mld_max_version;
16604 } else {
16605 ill->ill_mcast_type = ipst->ips_igmp_max_version;
16609 * Generate an event within the hooks framework to indicate that
16610 * a new interface has just been added to IP. For this event to
16611 * be generated, the network interface must, at least, have an
16612 * ifindex assigned to it. (We don't generate the event for
16613 * loopback since ill_lookup_on_name() has its own NE_PLUMB event.)
16615 * This needs to be run inside the ill_g_lock perimeter to ensure
16616 * that the ordering of delivered events to listeners matches the
16617 * order of them in the kernel.
16619 if (!IS_LOOPBACK(ill)) {
16620 ill_nic_event_dispatch(ill, 0, NE_PLUMB, ill->ill_name,
16621 ill->ill_name_length);
16623 RELEASE_ILL_LOCKS(ill, ill_other);
16624 mutex_exit(&phyi->phyint_lock);
16628 * Notify any downstream modules of the name of this interface.
16629 * An M_IOCTL is used even though we don't expect a successful reply.
16630 * Any reply message from the driver (presumably an M_IOCNAK) will
16631 * eventually get discarded somewhere upstream. The message format is
16632 * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
16633 * to IP.
16635 static void
16636 ip_ifname_notify(ill_t *ill, queue_t *q)
16638 mblk_t *mp1, *mp2;
16639 struct iocblk *iocp;
16640 struct lifreq *lifr;
16642 mp1 = mkiocb(SIOCSLIFNAME);
16643 if (mp1 == NULL)
16644 return;
16645 mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
16646 if (mp2 == NULL) {
16647 freeb(mp1);
16648 return;
16651 mp1->b_cont = mp2;
16652 iocp = (struct iocblk *)mp1->b_rptr;
16653 iocp->ioc_count = sizeof (struct lifreq);
16655 lifr = (struct lifreq *)mp2->b_rptr;
16656 mp2->b_wptr += sizeof (struct lifreq);
16657 bzero(lifr, sizeof (struct lifreq));
16659 (void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
16660 lifr->lifr_ppa = ill->ill_ppa;
16661 lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
16663 DTRACE_PROBE3(ill__dlpi, char *, "ip_ifname_notify",
16664 char *, "SIOCSLIFNAME", ill_t *, ill);
16665 putnext(q, mp1);
16668 static int
16669 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
16671 int err;
16672 ip_stack_t *ipst = ill->ill_ipst;
16673 phyint_t *phyi = ill->ill_phyint;
16676 * Now that ill_name is set, the configuration for the IPMP
16677 * meta-interface can be performed.
16679 if (IS_IPMP(ill)) {
16680 rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16682 * If phyi->phyint_grp is NULL, then this is the first IPMP
16683 * meta-interface and we need to create the IPMP group.
16685 if (phyi->phyint_grp == NULL) {
16687 * If someone has renamed another IPMP group to have
16688 * the same name as our interface, bail.
16690 if (ipmp_grp_lookup(ill->ill_name, ipst) != NULL) {
16691 rw_exit(&ipst->ips_ipmp_lock);
16692 return (EEXIST);
16694 phyi->phyint_grp = ipmp_grp_create(ill->ill_name, phyi);
16695 if (phyi->phyint_grp == NULL) {
16696 rw_exit(&ipst->ips_ipmp_lock);
16697 return (ENOMEM);
16700 rw_exit(&ipst->ips_ipmp_lock);
16703 /* Tell downstream modules where they are. */
16704 ip_ifname_notify(ill, q);
16707 * ill_dl_phys returns EINPROGRESS in the usual case.
16708 * Error cases are ENOMEM ...
16710 err = ill_dl_phys(ill, ipif, mp, q);
16712 if (ill->ill_isv6) {
16713 mutex_enter(&ipst->ips_mld_slowtimeout_lock);
16714 if (ipst->ips_mld_slowtimeout_id == 0) {
16715 ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
16716 (void *)ipst,
16717 MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16719 mutex_exit(&ipst->ips_mld_slowtimeout_lock);
16720 } else {
16721 mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
16722 if (ipst->ips_igmp_slowtimeout_id == 0) {
16723 ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
16724 (void *)ipst,
16725 MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16727 mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
16730 return (err);
16734 * Common routine for ppa and ifname setting. Should be called exclusive.
16736 * Returns EINPROGRESS when mp has been consumed by queueing it on
16737 * ipx_pending_mp and the ioctl will complete in ip_rput.
16739 * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
16740 * the new name and new ppa in lifr_name and lifr_ppa respectively.
16741 * For SLIFNAME, we pass these values back to the userland.
16743 static int
16744 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
16746 ill_t *ill;
16747 ipif_t *ipif;
16748 ipsq_t *ipsq;
16749 char *ppa_ptr;
16750 char *old_ptr;
16751 char old_char;
16752 int error;
16753 ip_stack_t *ipst;
16755 ip1dbg(("ipif_set_values: interface %s\n", interf_name));
16756 ASSERT(q->q_next != NULL);
16757 ASSERT(interf_name != NULL);
16759 ill = (ill_t *)q->q_ptr;
16760 ipst = ill->ill_ipst;
16762 ASSERT(ill->ill_ipst != NULL);
16763 ASSERT(ill->ill_name[0] == '\0');
16764 ASSERT(IAM_WRITER_ILL(ill));
16765 ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
16766 ASSERT(ill->ill_ppa == UINT_MAX);
16768 ill->ill_defend_start = ill->ill_defend_count = 0;
16769 /* The ppa is sent down by ifconfig or is chosen */
16770 if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
16771 return (EINVAL);
16775 * make sure ppa passed in is same as ppa in the name.
16776 * This check is not made when ppa == UINT_MAX in that case ppa
16777 * in the name could be anything. System will choose a ppa and
16778 * update new_ppa_ptr and inter_name to contain the choosen ppa.
16780 if (*new_ppa_ptr != UINT_MAX) {
16781 /* stoi changes the pointer */
16782 old_ptr = ppa_ptr;
16784 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
16785 * (they don't have an externally visible ppa). We assign one
16786 * here so that we can manage the interface. Note that in
16787 * the past this value was always 0 for DLPI 1 drivers.
16789 if (*new_ppa_ptr == 0)
16790 *new_ppa_ptr = stoi(&old_ptr);
16791 else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
16792 return (EINVAL);
16795 * terminate string before ppa
16796 * save char at that location.
16798 old_char = ppa_ptr[0];
16799 ppa_ptr[0] = '\0';
16801 ill->ill_ppa = *new_ppa_ptr;
16803 * Finish as much work now as possible before calling ill_glist_insert
16804 * which makes the ill globally visible and also merges it with the
16805 * other protocol instance of this phyint. The remaining work is
16806 * done after entering the ipsq which may happen sometime later.
16808 ipif = ill->ill_ipif;
16810 /* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
16811 ipif_assign_seqid(ipif);
16813 if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
16814 ill->ill_flags |= ILLF_IPV4;
16816 ASSERT(ipif->ipif_next == NULL); /* Only one ipif on ill */
16817 ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
16819 if (ill->ill_flags & ILLF_IPV6) {
16821 ill->ill_isv6 = B_TRUE;
16822 ill_set_inputfn(ill);
16823 if (ill->ill_rq != NULL) {
16824 ill->ill_rq->q_qinfo = &iprinitv6;
16827 /* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
16828 ipif->ipif_v6lcl_addr = ipv6_all_zeros;
16829 ipif->ipif_v6subnet = ipv6_all_zeros;
16830 ipif->ipif_v6net_mask = ipv6_all_zeros;
16831 ipif->ipif_v6brd_addr = ipv6_all_zeros;
16832 ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
16833 ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
16835 * point-to-point or Non-mulicast capable
16836 * interfaces won't do NUD unless explicitly
16837 * configured to do so.
16839 if (ipif->ipif_flags & IPIF_POINTOPOINT ||
16840 !(ill->ill_flags & ILLF_MULTICAST)) {
16841 ill->ill_flags |= ILLF_NONUD;
16843 /* Make sure IPv4 specific flag is not set on IPv6 if */
16844 if (ill->ill_flags & ILLF_NOARP) {
16846 * Note: xresolv interfaces will eventually need
16847 * NOARP set here as well, but that will require
16848 * those external resolvers to have some
16849 * knowledge of that flag and act appropriately.
16850 * Not to be changed at present.
16852 ill->ill_flags &= ~ILLF_NOARP;
16855 * Set the ILLF_ROUTER flag according to the global
16856 * IPv6 forwarding policy.
16858 if (ipst->ips_ipv6_forwarding != 0)
16859 ill->ill_flags |= ILLF_ROUTER;
16860 } else if (ill->ill_flags & ILLF_IPV4) {
16861 ill->ill_isv6 = B_FALSE;
16862 ill_set_inputfn(ill);
16863 ill->ill_reachable_retrans_time = ARP_RETRANS_TIMER;
16864 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
16865 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
16866 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
16867 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
16868 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
16870 * Set the ILLF_ROUTER flag according to the global
16871 * IPv4 forwarding policy.
16873 if (ipst->ips_ip_forwarding != 0)
16874 ill->ill_flags |= ILLF_ROUTER;
16877 ASSERT(ill->ill_phyint != NULL);
16880 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
16881 * be completed in ill_glist_insert -> ill_phyint_reinit
16883 if (!ill_allocate_mibs(ill))
16884 return (ENOMEM);
16887 * Pick a default sap until we get the DL_INFO_ACK back from
16888 * the driver.
16890 ill->ill_sap = (ill->ill_isv6) ? ill->ill_media->ip_m_ipv6sap :
16891 ill->ill_media->ip_m_ipv4sap;
16893 ill->ill_ifname_pending = 1;
16894 ill->ill_ifname_pending_err = 0;
16897 * When the first ipif comes up in ipif_up_done(), multicast groups
16898 * that were joined while this ill was not bound to the DLPI link need
16899 * to be recovered by ill_recover_multicast().
16901 ill->ill_need_recover_multicast = 1;
16903 ill_refhold(ill);
16904 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16905 if ((error = ill_glist_insert(ill, interf_name,
16906 (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
16907 ill->ill_ppa = UINT_MAX;
16908 ill->ill_name[0] = '\0';
16910 * undo null termination done above.
16912 ppa_ptr[0] = old_char;
16913 rw_exit(&ipst->ips_ill_g_lock);
16914 ill_refrele(ill);
16915 return (error);
16918 ASSERT(ill->ill_name_length <= LIFNAMSIZ);
16921 * When we return the buffer pointed to by interf_name should contain
16922 * the same name as in ill_name.
16923 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
16924 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
16925 * so copy full name and update the ppa ptr.
16926 * When ppa passed in != UINT_MAX all values are correct just undo
16927 * null termination, this saves a bcopy.
16929 if (*new_ppa_ptr == UINT_MAX) {
16930 bcopy(ill->ill_name, interf_name, ill->ill_name_length);
16931 *new_ppa_ptr = ill->ill_ppa;
16932 } else {
16934 * undo null termination done above.
16936 ppa_ptr[0] = old_char;
16939 /* Let SCTP know about this ILL */
16940 sctp_update_ill(ill, SCTP_ILL_INSERT);
16943 * ill_glist_insert has made the ill visible globally, and
16944 * ill_phyint_reinit could have changed the ipsq. At this point,
16945 * we need to hold the ips_ill_g_lock across the call to enter the
16946 * ipsq to enforce atomicity and prevent reordering. In the event
16947 * the ipsq has changed, and if the new ipsq is currently busy,
16948 * we need to make sure that this half-completed ioctl is ahead of
16949 * any subsequent ioctl. We achieve this by not dropping the
16950 * ips_ill_g_lock which prevents any ill lookup itself thereby
16951 * ensuring that new ioctls can't start.
16953 ipsq = ipsq_try_enter_internal(ill, q, mp, ip_reprocess_ioctl, NEW_OP,
16954 B_TRUE);
16956 rw_exit(&ipst->ips_ill_g_lock);
16957 ill_refrele(ill);
16958 if (ipsq == NULL)
16959 return (EINPROGRESS);
16962 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
16964 if (ipsq->ipsq_xop->ipx_current_ipif == NULL)
16965 ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
16966 else
16967 ASSERT(ipsq->ipsq_xop->ipx_current_ipif == ipif);
16969 error = ipif_set_values_tail(ill, ipif, mp, q);
16970 ipsq_exit(ipsq);
16971 if (error != 0 && error != EINPROGRESS) {
16973 * restore previous values
16975 ill->ill_isv6 = B_FALSE;
16976 ill_set_inputfn(ill);
16978 return (error);
16981 void
16982 ipif_init(ip_stack_t *ipst)
16984 int i;
16986 for (i = 0; i < MAX_G_HEADS; i++) {
16987 ipst->ips_ill_g_heads[i].ill_g_list_head =
16988 (ill_if_t *)&ipst->ips_ill_g_heads[i];
16989 ipst->ips_ill_g_heads[i].ill_g_list_tail =
16990 (ill_if_t *)&ipst->ips_ill_g_heads[i];
16993 avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16994 ill_phyint_compare_index,
16995 sizeof (phyint_t),
16996 offsetof(struct phyint, phyint_avl_by_index));
16997 avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16998 ill_phyint_compare_name,
16999 sizeof (phyint_t),
17000 offsetof(struct phyint, phyint_avl_by_name));
17004 * Save enough information so that we can recreate the IRE if
17005 * the interface goes down and then up.
17007 void
17008 ill_save_ire(ill_t *ill, ire_t *ire)
17010 mblk_t *save_mp;
17012 save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
17013 if (save_mp != NULL) {
17014 ifrt_t *ifrt;
17016 save_mp->b_wptr += sizeof (ifrt_t);
17017 ifrt = (ifrt_t *)save_mp->b_rptr;
17018 bzero(ifrt, sizeof (ifrt_t));
17019 ifrt->ifrt_type = ire->ire_type;
17020 if (ire->ire_ipversion == IPV4_VERSION) {
17021 ASSERT(!ill->ill_isv6);
17022 ifrt->ifrt_addr = ire->ire_addr;
17023 ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
17024 ifrt->ifrt_setsrc_addr = ire->ire_setsrc_addr;
17025 ifrt->ifrt_mask = ire->ire_mask;
17026 } else {
17027 ASSERT(ill->ill_isv6);
17028 ifrt->ifrt_v6addr = ire->ire_addr_v6;
17029 /* ire_gateway_addr_v6 can change due to RTM_CHANGE */
17030 mutex_enter(&ire->ire_lock);
17031 ifrt->ifrt_v6gateway_addr = ire->ire_gateway_addr_v6;
17032 mutex_exit(&ire->ire_lock);
17033 ifrt->ifrt_v6setsrc_addr = ire->ire_setsrc_addr_v6;
17034 ifrt->ifrt_v6mask = ire->ire_mask_v6;
17036 ifrt->ifrt_flags = ire->ire_flags;
17037 ifrt->ifrt_zoneid = ire->ire_zoneid;
17038 mutex_enter(&ill->ill_saved_ire_lock);
17039 save_mp->b_cont = ill->ill_saved_ire_mp;
17040 ill->ill_saved_ire_mp = save_mp;
17041 ill->ill_saved_ire_cnt++;
17042 mutex_exit(&ill->ill_saved_ire_lock);
17047 * Remove one entry from ill_saved_ire_mp.
17049 void
17050 ill_remove_saved_ire(ill_t *ill, ire_t *ire)
17052 mblk_t **mpp;
17053 mblk_t *mp;
17054 ifrt_t *ifrt;
17056 /* Remove from ill_saved_ire_mp list if it is there */
17057 mutex_enter(&ill->ill_saved_ire_lock);
17058 for (mpp = &ill->ill_saved_ire_mp; *mpp != NULL;
17059 mpp = &(*mpp)->b_cont) {
17060 in6_addr_t gw_addr_v6;
17063 * On a given ill, the tuple of address, gateway, mask,
17064 * ire_type, and zoneid is unique for each saved IRE.
17066 mp = *mpp;
17067 ifrt = (ifrt_t *)mp->b_rptr;
17068 /* ire_gateway_addr_v6 can change - need lock */
17069 mutex_enter(&ire->ire_lock);
17070 gw_addr_v6 = ire->ire_gateway_addr_v6;
17071 mutex_exit(&ire->ire_lock);
17073 if (ifrt->ifrt_zoneid != ire->ire_zoneid ||
17074 ifrt->ifrt_type != ire->ire_type)
17075 continue;
17077 if (ill->ill_isv6 ?
17078 (IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6addr,
17079 &ire->ire_addr_v6) &&
17080 IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6gateway_addr,
17081 &gw_addr_v6) &&
17082 IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6mask,
17083 &ire->ire_mask_v6)) :
17084 (ifrt->ifrt_addr == ire->ire_addr &&
17085 ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
17086 ifrt->ifrt_mask == ire->ire_mask)) {
17087 *mpp = mp->b_cont;
17088 ill->ill_saved_ire_cnt--;
17089 freeb(mp);
17090 break;
17093 mutex_exit(&ill->ill_saved_ire_lock);
17097 * IP multirouting broadcast routes handling
17098 * Append CGTP broadcast IREs to regular ones created
17099 * at ifconfig time.
17100 * The usage is a route add <cgtp_bc> <nic_bc> -multirt i.e., both
17101 * the destination and the gateway are broadcast addresses.
17102 * The caller has verified that the destination is an IRE_BROADCAST and that
17103 * RTF_MULTIRT was set. Here if the gateway is a broadcast address, then
17104 * we create a MULTIRT IRE_BROADCAST.
17105 * Note that the IRE_HOST created by ire_rt_add doesn't get found by anything
17106 * since the IRE_BROADCAST takes precedence; ire_add_v4 does head insertion.
17108 static void
17109 ip_cgtp_bcast_add(ire_t *ire, ip_stack_t *ipst)
17111 ire_t *ire_prim;
17113 ASSERT(ire != NULL);
17115 ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
17116 IRE_BROADCAST, NULL, ALL_ZONES, MATCH_IRE_TYPE, 0, ipst, NULL);
17117 if (ire_prim != NULL) {
17119 * We are in the special case of broadcasts for
17120 * CGTP. We add an IRE_BROADCAST that holds
17121 * the RTF_MULTIRT flag, the destination
17122 * address and the low level
17123 * info of ire_prim. In other words, CGTP
17124 * broadcast is added to the redundant ipif.
17126 ill_t *ill_prim;
17127 ire_t *bcast_ire;
17129 ill_prim = ire_prim->ire_ill;
17131 ip2dbg(("ip_cgtp_filter_bcast_add: ire_prim %p, ill_prim %p\n",
17132 (void *)ire_prim, (void *)ill_prim));
17134 bcast_ire = ire_create(
17135 (uchar_t *)&ire->ire_addr,
17136 (uchar_t *)&ip_g_all_ones,
17137 (uchar_t *)&ire->ire_gateway_addr,
17138 IRE_BROADCAST,
17139 ill_prim,
17140 GLOBAL_ZONEID, /* CGTP is only for the global zone */
17141 ire->ire_flags | RTF_KERNEL,
17142 ipst);
17145 * Here we assume that ire_add does head insertion so that
17146 * the added IRE_BROADCAST comes before the existing IRE_HOST.
17148 if (bcast_ire != NULL) {
17149 if (ire->ire_flags & RTF_SETSRC) {
17150 bcast_ire->ire_setsrc_addr =
17151 ire->ire_setsrc_addr;
17153 bcast_ire = ire_add(bcast_ire);
17154 if (bcast_ire != NULL) {
17155 ip2dbg(("ip_cgtp_filter_bcast_add: "
17156 "added bcast_ire %p\n",
17157 (void *)bcast_ire));
17159 ill_save_ire(ill_prim, bcast_ire);
17160 ire_refrele(bcast_ire);
17163 ire_refrele(ire_prim);
17168 * IP multirouting broadcast routes handling
17169 * Remove the broadcast ire.
17170 * The usage is a route delete <cgtp_bc> <nic_bc> -multirt i.e., both
17171 * the destination and the gateway are broadcast addresses.
17172 * The caller has only verified that RTF_MULTIRT was set. We check
17173 * that the destination is broadcast and that the gateway is a broadcast
17174 * address, and if so delete the IRE added by ip_cgtp_bcast_add().
17176 static void
17177 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
17179 ASSERT(ire != NULL);
17181 if (ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST) {
17182 ire_t *ire_prim;
17184 ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
17185 IRE_BROADCAST, NULL, ALL_ZONES, MATCH_IRE_TYPE, 0, ipst,
17186 NULL);
17187 if (ire_prim != NULL) {
17188 ill_t *ill_prim;
17189 ire_t *bcast_ire;
17191 ill_prim = ire_prim->ire_ill;
17193 ip2dbg(("ip_cgtp_filter_bcast_delete: "
17194 "ire_prim %p, ill_prim %p\n",
17195 (void *)ire_prim, (void *)ill_prim));
17197 bcast_ire = ire_ftable_lookup_v4(ire->ire_addr, 0,
17198 ire->ire_gateway_addr, IRE_BROADCAST,
17199 ill_prim, ALL_ZONES,
17200 MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_ILL |
17201 MATCH_IRE_MASK, 0, ipst, NULL);
17203 if (bcast_ire != NULL) {
17204 ip2dbg(("ip_cgtp_filter_bcast_delete: "
17205 "looked up bcast_ire %p\n",
17206 (void *)bcast_ire));
17207 ill_remove_saved_ire(bcast_ire->ire_ill,
17208 bcast_ire);
17209 ire_delete(bcast_ire);
17210 ire_refrele(bcast_ire);
17212 ire_refrele(ire_prim);
17218 * Derive an interface id from the link layer address.
17219 * Knows about IEEE 802 and IEEE EUI-64 mappings.
17221 static void
17222 ip_ether_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17224 char *addr;
17227 * Note that some IPv6 interfaces get plumbed over links that claim to
17228 * be DL_ETHER, but don't actually have Ethernet MAC addresses (e.g.
17229 * PPP links). The ETHERADDRL check here ensures that we only set the
17230 * interface ID on IPv6 interfaces above links that actually have real
17231 * Ethernet addresses.
17233 if (ill->ill_phys_addr_length == ETHERADDRL) {
17234 /* Form EUI-64 like address */
17235 addr = (char *)&v6addr->s6_addr32[2];
17236 bcopy(ill->ill_phys_addr, addr, 3);
17237 addr[0] ^= 0x2; /* Toggle Universal/Local bit */
17238 addr[3] = (char)0xff;
17239 addr[4] = (char)0xfe;
17240 bcopy(ill->ill_phys_addr + 3, addr + 5, 3);
17244 /* ARGSUSED */
17245 static void
17246 ip_nodef_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17250 typedef struct ipmp_ifcookie {
17251 uint32_t ic_hostid;
17252 char ic_ifname[LIFNAMSIZ];
17253 char ic_zonename[ZONENAME_MAX];
17254 } ipmp_ifcookie_t;
17257 * Construct a pseudo-random interface ID for the IPMP interface that's both
17258 * predictable and (almost) guaranteed to be unique.
17260 static void
17261 ip_ipmp_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17263 zone_t *zp;
17264 uint8_t *addr;
17265 uchar_t hash[16];
17266 ulong_t hostid;
17267 MD5_CTX ctx;
17268 ipmp_ifcookie_t ic = { 0 };
17270 ASSERT(IS_IPMP(ill));
17272 (void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
17273 ic.ic_hostid = htonl((uint32_t)hostid);
17275 (void) strlcpy(ic.ic_ifname, ill->ill_name, LIFNAMSIZ);
17277 if ((zp = zone_find_by_id(ill->ill_zoneid)) != NULL) {
17278 (void) strlcpy(ic.ic_zonename, zp->zone_name, ZONENAME_MAX);
17279 zone_rele(zp);
17282 MD5Init(&ctx);
17283 MD5Update(&ctx, &ic, sizeof (ic));
17284 MD5Final(hash, &ctx);
17287 * Map the hash to an interface ID per the basic approach in RFC3041.
17289 addr = &v6addr->s6_addr8[8];
17290 bcopy(hash + 8, addr, sizeof (uint64_t));
17291 addr[0] &= ~0x2; /* set local bit */
17295 * Map the multicast in6_addr_t in m_ip6addr to the physaddr for ethernet.
17297 static void
17298 ip_ether_v6_mapping(ill_t *ill, uchar_t *m_ip6addr, uchar_t *m_physaddr)
17300 phyint_t *phyi = ill->ill_phyint;
17303 * Check PHYI_MULTI_BCAST and length of physical
17304 * address to determine if we use the mapping or the
17305 * broadcast address.
17307 if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
17308 ill->ill_phys_addr_length != ETHERADDRL) {
17309 ip_mbcast_mapping(ill, m_ip6addr, m_physaddr);
17310 return;
17312 m_physaddr[0] = 0x33;
17313 m_physaddr[1] = 0x33;
17314 m_physaddr[2] = m_ip6addr[12];
17315 m_physaddr[3] = m_ip6addr[13];
17316 m_physaddr[4] = m_ip6addr[14];
17317 m_physaddr[5] = m_ip6addr[15];
17321 * Map the multicast ipaddr_t in m_ipaddr to the physaddr for ethernet.
17323 static void
17324 ip_ether_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17326 phyint_t *phyi = ill->ill_phyint;
17329 * Check PHYI_MULTI_BCAST and length of physical
17330 * address to determine if we use the mapping or the
17331 * broadcast address.
17333 if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
17334 ill->ill_phys_addr_length != ETHERADDRL) {
17335 ip_mbcast_mapping(ill, m_ipaddr, m_physaddr);
17336 return;
17338 m_physaddr[0] = 0x01;
17339 m_physaddr[1] = 0x00;
17340 m_physaddr[2] = 0x5e;
17341 m_physaddr[3] = m_ipaddr[1] & 0x7f;
17342 m_physaddr[4] = m_ipaddr[2];
17343 m_physaddr[5] = m_ipaddr[3];
17346 /* ARGSUSED */
17347 static void
17348 ip_mbcast_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17351 * for the MULTI_BCAST case and other cases when we want to
17352 * use the link-layer broadcast address for multicast.
17354 uint8_t *bphys_addr;
17355 dl_unitdata_req_t *dlur;
17357 dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17358 if (ill->ill_sap_length < 0) {
17359 bphys_addr = (uchar_t *)dlur +
17360 dlur->dl_dest_addr_offset;
17361 } else {
17362 bphys_addr = (uchar_t *)dlur +
17363 dlur->dl_dest_addr_offset + ill->ill_sap_length;
17366 bcopy(bphys_addr, m_physaddr, ill->ill_phys_addr_length);
17370 * Derive IPoIB interface id from the link layer address.
17372 static void
17373 ip_ib_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17375 char *addr;
17377 ASSERT(ill->ill_phys_addr_length == 20);
17378 addr = (char *)&v6addr->s6_addr32[2];
17379 bcopy(ill->ill_phys_addr + 12, addr, 8);
17381 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
17382 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
17383 * rules. In these cases, the IBA considers these GUIDs to be in
17384 * "Modified EUI-64" format, and thus toggling the u/l bit is not
17385 * required; vendors are required not to assign global EUI-64's
17386 * that differ only in u/l bit values, thus guaranteeing uniqueness
17387 * of the interface identifier. Whether the GUID is in modified
17388 * or proper EUI-64 format, the ipv6 identifier must have the u/l
17389 * bit set to 1.
17391 addr[0] |= 2; /* Set Universal/Local bit to 1 */
17395 * Map the multicast ipaddr_t in m_ipaddr to the physaddr for InfiniBand.
17396 * Note on mapping from multicast IP addresses to IPoIB multicast link
17397 * addresses. IPoIB multicast link addresses are based on IBA link addresses.
17398 * The format of an IPoIB multicast address is:
17400 * 4 byte QPN Scope Sign. Pkey
17401 * +--------------------------------------------+
17402 * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
17403 * +--------------------------------------------+
17405 * The Scope and Pkey components are properties of the IBA port and
17406 * network interface. They can be ascertained from the broadcast address.
17407 * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
17409 static void
17410 ip_ib_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17412 static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
17413 0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
17414 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17415 uint8_t *bphys_addr;
17416 dl_unitdata_req_t *dlur;
17418 bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
17421 * RFC 4391: IPv4 MGID is 28-bit long.
17423 m_physaddr[16] = m_ipaddr[0] & 0x0f;
17424 m_physaddr[17] = m_ipaddr[1];
17425 m_physaddr[18] = m_ipaddr[2];
17426 m_physaddr[19] = m_ipaddr[3];
17429 dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17430 if (ill->ill_sap_length < 0) {
17431 bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
17432 } else {
17433 bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
17434 ill->ill_sap_length;
17437 * Now fill in the IBA scope/Pkey values from the broadcast address.
17439 m_physaddr[5] = bphys_addr[5];
17440 m_physaddr[8] = bphys_addr[8];
17441 m_physaddr[9] = bphys_addr[9];
17444 static void
17445 ip_ib_v6_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17447 static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
17448 0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
17449 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17450 uint8_t *bphys_addr;
17451 dl_unitdata_req_t *dlur;
17453 bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
17456 * RFC 4391: IPv4 MGID is 80-bit long.
17458 bcopy(&m_ipaddr[6], &m_physaddr[10], 10);
17460 dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17461 if (ill->ill_sap_length < 0) {
17462 bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
17463 } else {
17464 bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
17465 ill->ill_sap_length;
17468 * Now fill in the IBA scope/Pkey values from the broadcast address.
17470 m_physaddr[5] = bphys_addr[5];
17471 m_physaddr[8] = bphys_addr[8];
17472 m_physaddr[9] = bphys_addr[9];
17476 * Derive IPv6 interface id from an IPv4 link-layer address (e.g. from an IPv4
17477 * tunnel). The IPv4 address simply get placed in the lower 4 bytes of the
17478 * IPv6 interface id. This is a suggested mechanism described in section 3.7
17479 * of RFC4213.
17481 static void
17482 ip_ipv4_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17484 ASSERT(ill->ill_phys_addr_length == sizeof (ipaddr_t));
17485 v6addr->s6_addr32[2] = 0;
17486 bcopy(physaddr, &v6addr->s6_addr32[3], sizeof (ipaddr_t));
17490 * Derive IPv6 interface id from an IPv6 link-layer address (e.g. from an IPv6
17491 * tunnel). The lower 8 bytes of the IPv6 address simply become the interface
17492 * id.
17494 static void
17495 ip_ipv6_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17497 in6_addr_t *v6lladdr = (in6_addr_t *)physaddr;
17499 ASSERT(ill->ill_phys_addr_length == sizeof (in6_addr_t));
17500 bcopy(&v6lladdr->s6_addr32[2], &v6addr->s6_addr32[2], 8);
17503 static void
17504 ip_ipv6_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17506 ip_ipv6_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17509 static void
17510 ip_ipv6_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17512 ip_ipv6_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17515 static void
17516 ip_ipv4_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17518 ip_ipv4_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17521 static void
17522 ip_ipv4_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17524 ip_ipv4_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17528 * Lookup an ill and verify that the zoneid has an ipif on that ill.
17529 * Returns an held ill, or NULL.
17531 ill_t *
17532 ill_lookup_on_ifindex_zoneid(uint_t index, zoneid_t zoneid, boolean_t isv6,
17533 ip_stack_t *ipst)
17535 ill_t *ill;
17536 ipif_t *ipif;
17538 ill = ill_lookup_on_ifindex(index, isv6, ipst);
17539 if (ill == NULL)
17540 return (NULL);
17542 mutex_enter(&ill->ill_lock);
17543 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17544 if (IPIF_IS_CONDEMNED(ipif))
17545 continue;
17546 if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
17547 ipif->ipif_zoneid != ALL_ZONES)
17548 continue;
17550 mutex_exit(&ill->ill_lock);
17551 return (ill);
17553 mutex_exit(&ill->ill_lock);
17554 ill_refrele(ill);
17555 return (NULL);
17559 * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
17560 * If a pointer to an ipif_t is returned then the caller will need to do
17561 * an ill_refrele().
17563 ipif_t *
17564 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
17565 ip_stack_t *ipst)
17567 ipif_t *ipif;
17568 ill_t *ill;
17570 ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
17571 if (ill == NULL)
17572 return (NULL);
17574 mutex_enter(&ill->ill_lock);
17575 if (ill->ill_state_flags & ILL_CONDEMNED) {
17576 mutex_exit(&ill->ill_lock);
17577 ill_refrele(ill);
17578 return (NULL);
17581 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17582 if (!IPIF_CAN_LOOKUP(ipif))
17583 continue;
17584 if (lifidx == ipif->ipif_id) {
17585 ipif_refhold_locked(ipif);
17586 break;
17590 mutex_exit(&ill->ill_lock);
17591 ill_refrele(ill);
17592 return (ipif);
17596 * Set ill_inputfn based on the current know state.
17597 * This needs to be called when any of the factors taken into
17598 * account changes.
17600 void
17601 ill_set_inputfn(ill_t *ill)
17603 ip_stack_t *ipst = ill->ill_ipst;
17605 if (ill->ill_isv6) {
17606 ill->ill_inputfn = ill_input_short_v6;
17607 } else {
17608 if (ill->ill_dhcpinit != 0)
17609 ill->ill_inputfn = ill_input_full_v4;
17610 else if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_RSVP].connf_head
17611 != NULL)
17612 ill->ill_inputfn = ill_input_full_v4;
17613 else if (ipst->ips_ip_cgtp_filter &&
17614 ipst->ips_ip_cgtp_filter_ops != NULL)
17615 ill->ill_inputfn = ill_input_full_v4;
17616 else
17617 ill->ill_inputfn = ill_input_short_v4;
17622 * Re-evaluate ill_inputfn for all the IPv4 ills.
17623 * Used when RSVP and CGTP comes and goes.
17625 void
17626 ill_set_inputfn_all(ip_stack_t *ipst)
17628 ill_walk_context_t ctx;
17629 ill_t *ill;
17631 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
17632 ill = ILL_START_WALK_V4(&ctx, ipst);
17633 for (; ill != NULL; ill = ill_next(&ctx, ill))
17634 ill_set_inputfn(ill);
17636 rw_exit(&ipst->ips_ill_g_lock);
17640 * Set the physical address information for `ill' to the contents of the
17641 * dl_notify_ind_t pointed to by `mp'. Must be called as writer, and will be
17642 * asynchronous if `ill' cannot immediately be quiesced -- in which case
17643 * EINPROGRESS will be returned.
17646 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
17648 ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17649 dl_notify_ind_t *dlindp = (dl_notify_ind_t *)mp->b_rptr;
17651 ASSERT(IAM_WRITER_IPSQ(ipsq));
17653 if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
17654 dlindp->dl_data != DL_CURR_DEST_ADDR &&
17655 dlindp->dl_data != DL_CURR_PHYS_ADDR) {
17656 /* Changing DL_IPV6_TOKEN is not yet supported */
17657 return (0);
17661 * We need to store up to two copies of `mp' in `ill'. Due to the
17662 * design of ipsq_pending_mp_add(), we can't pass them as separate
17663 * arguments to ill_set_phys_addr_tail(). Instead, chain them
17664 * together here, then pull 'em apart in ill_set_phys_addr_tail().
17666 if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
17667 freemsg(mp);
17668 return (ENOMEM);
17671 ipsq_current_start(ipsq, ill->ill_ipif, 0);
17674 * Since we'll only do a logical down, we can't rely on ipif_down
17675 * to turn on ILL_DOWN_IN_PROGRESS, or for the DL_BIND_ACK to reset
17676 * ILL_DOWN_IN_PROGRESS. We instead manage this separately for this
17677 * case, to quiesce ire's and nce's for ill_is_quiescent.
17679 mutex_enter(&ill->ill_lock);
17680 ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
17681 /* no more ire/nce addition allowed */
17682 mutex_exit(&ill->ill_lock);
17685 * If we can quiesce the ill, then set the address. If not, then
17686 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
17688 ill_down_ipifs(ill, B_TRUE);
17689 mutex_enter(&ill->ill_lock);
17690 if (!ill_is_quiescent(ill)) {
17691 /* call cannot fail since `conn_t *' argument is NULL */
17692 (void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17693 mp, ILL_DOWN);
17694 mutex_exit(&ill->ill_lock);
17695 return (EINPROGRESS);
17697 mutex_exit(&ill->ill_lock);
17699 ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
17700 return (0);
17704 * When the allowed-ips link property is set on the datalink, IP receives a
17705 * DL_NOTE_ALLOWED_IPS notification that is processed in ill_set_allowed_ips()
17706 * to initialize the ill_allowed_ips[] array in the ill_t. This array is then
17707 * used to vet addresses passed to ip_sioctl_addr() and to ensure that the
17708 * only IP addresses configured on the ill_t are those in the ill_allowed_ips[]
17709 * array.
17711 void
17712 ill_set_allowed_ips(ill_t *ill, mblk_t *mp)
17714 ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17715 dl_notify_ind_t *dlip = (dl_notify_ind_t *)mp->b_rptr;
17716 mac_protect_t *mrp;
17717 int i;
17719 ASSERT(IAM_WRITER_IPSQ(ipsq));
17720 mrp = (mac_protect_t *)&dlip[1];
17722 if (mrp->mp_ipaddrcnt == 0) { /* reset allowed-ips */
17723 kmem_free(ill->ill_allowed_ips,
17724 ill->ill_allowed_ips_cnt * sizeof (in6_addr_t));
17725 ill->ill_allowed_ips_cnt = 0;
17726 ill->ill_allowed_ips = NULL;
17727 mutex_enter(&ill->ill_phyint->phyint_lock);
17728 ill->ill_phyint->phyint_flags &= ~PHYI_L3PROTECT;
17729 mutex_exit(&ill->ill_phyint->phyint_lock);
17730 return;
17733 if (ill->ill_allowed_ips != NULL) {
17734 kmem_free(ill->ill_allowed_ips,
17735 ill->ill_allowed_ips_cnt * sizeof (in6_addr_t));
17737 ill->ill_allowed_ips_cnt = mrp->mp_ipaddrcnt;
17738 ill->ill_allowed_ips = kmem_alloc(
17739 ill->ill_allowed_ips_cnt * sizeof (in6_addr_t), KM_SLEEP);
17740 for (i = 0; i < mrp->mp_ipaddrcnt; i++)
17741 ill->ill_allowed_ips[i] = mrp->mp_ipaddrs[i].ip_addr;
17743 mutex_enter(&ill->ill_phyint->phyint_lock);
17744 ill->ill_phyint->phyint_flags |= PHYI_L3PROTECT;
17745 mutex_exit(&ill->ill_phyint->phyint_lock);
17749 * Once the ill associated with `q' has quiesced, set its physical address
17750 * information to the values in `addrmp'. Note that two copies of `addrmp'
17751 * are passed (linked by b_cont), since we sometimes need to save two distinct
17752 * copies in the ill_t, and our context doesn't permit sleeping or allocation
17753 * failure (we'll free the other copy if it's not needed). Since the ill_t
17754 * is quiesced, we know any stale nce's with the old address information have
17755 * already been removed, so we don't need to call nce_flush().
17757 /* ARGSUSED */
17758 static void
17759 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
17761 ill_t *ill = q->q_ptr;
17762 mblk_t *addrmp2 = unlinkb(addrmp);
17763 dl_notify_ind_t *dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
17764 uint_t addrlen, addroff;
17765 int status;
17767 ASSERT(IAM_WRITER_IPSQ(ipsq));
17769 addroff = dlindp->dl_addr_offset;
17770 addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
17772 switch (dlindp->dl_data) {
17773 case DL_IPV6_LINK_LAYER_ADDR:
17774 ill_set_ndmp(ill, addrmp, addroff, addrlen);
17775 freemsg(addrmp2);
17776 break;
17778 case DL_CURR_DEST_ADDR:
17779 freemsg(ill->ill_dest_addr_mp);
17780 ill->ill_dest_addr = addrmp->b_rptr + addroff;
17781 ill->ill_dest_addr_mp = addrmp;
17782 if (ill->ill_isv6) {
17783 ill_setdesttoken(ill);
17784 ipif_setdestlinklocal(ill->ill_ipif);
17786 freemsg(addrmp2);
17787 break;
17789 case DL_CURR_PHYS_ADDR:
17790 freemsg(ill->ill_phys_addr_mp);
17791 ill->ill_phys_addr = addrmp->b_rptr + addroff;
17792 ill->ill_phys_addr_mp = addrmp;
17793 ill->ill_phys_addr_length = addrlen;
17794 if (ill->ill_isv6)
17795 ill_set_ndmp(ill, addrmp2, addroff, addrlen);
17796 else
17797 freemsg(addrmp2);
17798 if (ill->ill_isv6) {
17799 ill_setdefaulttoken(ill);
17800 ipif_setlinklocal(ill->ill_ipif);
17802 break;
17803 default:
17804 ASSERT(0);
17808 * reset ILL_DOWN_IN_PROGRESS so that we can successfully add ires
17809 * as we bring the ipifs up again.
17811 mutex_enter(&ill->ill_lock);
17812 ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
17813 mutex_exit(&ill->ill_lock);
17815 * If there are ipifs to bring up, ill_up_ipifs() will return
17816 * EINPROGRESS, and ipsq_current_finish() will be called by
17817 * ip_rput_dlpi_writer() or arp_bringup_done() when the last ipif is
17818 * brought up.
17820 status = ill_up_ipifs(ill, q, addrmp);
17821 if (status != EINPROGRESS)
17822 ipsq_current_finish(ipsq);
17826 * Helper routine for setting the ill_nd_lla fields.
17828 void
17829 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
17831 freemsg(ill->ill_nd_lla_mp);
17832 ill->ill_nd_lla = ndmp->b_rptr + addroff;
17833 ill->ill_nd_lla_mp = ndmp;
17834 ill->ill_nd_lla_len = addrlen;
17838 * Replumb the ill.
17841 ill_replumb(ill_t *ill, mblk_t *mp)
17843 ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17845 ASSERT(IAM_WRITER_IPSQ(ipsq));
17847 ipsq_current_start(ipsq, ill->ill_ipif, 0);
17850 * If we can quiesce the ill, then continue. If not, then
17851 * ill_replumb_tail() will be called from ipif_ill_refrele_tail().
17853 ill_down_ipifs(ill, B_FALSE);
17855 mutex_enter(&ill->ill_lock);
17856 if (!ill_is_quiescent(ill)) {
17857 /* call cannot fail since `conn_t *' argument is NULL */
17858 (void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17859 mp, ILL_DOWN);
17860 mutex_exit(&ill->ill_lock);
17861 return (EINPROGRESS);
17863 mutex_exit(&ill->ill_lock);
17865 ill_replumb_tail(ipsq, ill->ill_rq, mp, NULL);
17866 return (0);
17869 /* ARGSUSED */
17870 static void
17871 ill_replumb_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
17873 ill_t *ill = q->q_ptr;
17874 int err;
17875 conn_t *connp = NULL;
17877 ASSERT(IAM_WRITER_IPSQ(ipsq));
17878 freemsg(ill->ill_replumb_mp);
17879 ill->ill_replumb_mp = copyb(mp);
17881 if (ill->ill_replumb_mp == NULL) {
17882 /* out of memory */
17883 ipsq_current_finish(ipsq);
17884 return;
17887 mutex_enter(&ill->ill_lock);
17888 ill->ill_up_ipifs = ipsq_pending_mp_add(NULL, ill->ill_ipif,
17889 ill->ill_rq, ill->ill_replumb_mp, 0);
17890 mutex_exit(&ill->ill_lock);
17892 if (!ill->ill_up_ipifs) {
17893 /* already closing */
17894 ipsq_current_finish(ipsq);
17895 return;
17897 ill->ill_replumbing = 1;
17898 err = ill_down_ipifs_tail(ill);
17901 * Successfully quiesced and brought down the interface, now we send
17902 * the DL_NOTE_REPLUMB_DONE message down to the driver. Reuse the
17903 * DL_NOTE_REPLUMB message.
17905 mp = mexchange(NULL, mp, sizeof (dl_notify_conf_t), M_PROTO,
17906 DL_NOTIFY_CONF);
17907 ASSERT(mp != NULL);
17908 ((dl_notify_conf_t *)mp->b_rptr)->dl_notification =
17909 DL_NOTE_REPLUMB_DONE;
17910 ill_dlpi_send(ill, mp);
17913 * For IPv4, we would usually get EINPROGRESS because the ETHERTYPE_ARP
17914 * streams have to be unbound. When all the DLPI exchanges are done,
17915 * ipsq_current_finish() will be called by arp_bringup_done(). The
17916 * remainder of ipif bringup via ill_up_ipifs() will also be done in
17917 * arp_bringup_done().
17919 ASSERT(ill->ill_replumb_mp != NULL);
17920 if (err == EINPROGRESS)
17921 return;
17922 else
17923 ill->ill_replumb_mp = ipsq_pending_mp_get(ipsq, &connp);
17924 ASSERT(connp == NULL);
17925 if (err == 0 && ill->ill_replumb_mp != NULL &&
17926 ill_up_ipifs(ill, q, ill->ill_replumb_mp) == EINPROGRESS) {
17927 return;
17929 ipsq_current_finish(ipsq);
17933 * Issue ioctl `cmd' on `lh'; caller provides the initial payload in `buf'
17934 * which is `bufsize' bytes. On success, zero is returned and `buf' updated
17935 * as per the ioctl. On failure, an errno is returned.
17937 static int
17938 ip_ioctl(ldi_handle_t lh, int cmd, void *buf, uint_t bufsize, cred_t *cr)
17940 int rval;
17941 struct strioctl iocb;
17943 iocb.ic_cmd = cmd;
17944 iocb.ic_timout = 15;
17945 iocb.ic_len = bufsize;
17946 iocb.ic_dp = buf;
17948 return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval));
17952 * Issue an SIOCGLIFCONF for address family `af' and store the result into a
17953 * dynamically-allocated `lifcp' that will be `bufsizep' bytes on success.
17955 static int
17956 ip_lifconf_ioctl(ldi_handle_t lh, int af, struct lifconf *lifcp,
17957 uint_t *bufsizep, cred_t *cr)
17959 int err;
17960 struct lifnum lifn;
17962 bzero(&lifn, sizeof (lifn));
17963 lifn.lifn_family = af;
17964 lifn.lifn_flags = LIFC_UNDER_IPMP;
17966 if ((err = ip_ioctl(lh, SIOCGLIFNUM, &lifn, sizeof (lifn), cr)) != 0)
17967 return (err);
17970 * Pad the interface count to account for additional interfaces that
17971 * may have been configured between the SIOCGLIFNUM and SIOCGLIFCONF.
17973 lifn.lifn_count += 4;
17974 bzero(lifcp, sizeof (*lifcp));
17975 lifcp->lifc_flags = LIFC_UNDER_IPMP;
17976 lifcp->lifc_family = af;
17977 lifcp->lifc_len = *bufsizep = lifn.lifn_count * sizeof (struct lifreq);
17978 lifcp->lifc_buf = kmem_zalloc(*bufsizep, KM_SLEEP);
17980 err = ip_ioctl(lh, SIOCGLIFCONF, lifcp, sizeof (*lifcp), cr);
17981 if (err != 0) {
17982 kmem_free(lifcp->lifc_buf, *bufsizep);
17983 return (err);
17986 return (0);
17990 * Helper for ip_interface_cleanup() that removes the loopback interface.
17992 static void
17993 ip_loopback_removeif(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
17995 int err;
17996 struct lifreq lifr;
17998 bzero(&lifr, sizeof (lifr));
17999 (void) strcpy(lifr.lifr_name, ipif_loopback_name);
18002 * Attempt to remove the interface. It may legitimately not exist
18003 * (e.g. the zone administrator unplumbed it), so ignore ENXIO.
18005 err = ip_ioctl(lh, SIOCLIFREMOVEIF, &lifr, sizeof (lifr), cr);
18006 if (err != 0 && err != ENXIO) {
18007 ip0dbg(("ip_loopback_removeif: IP%s SIOCLIFREMOVEIF failed: "
18008 "error %d\n", isv6 ? "v6" : "v4", err));
18013 * Helper for ip_interface_cleanup() that ensures no IP interfaces are in IPMP
18014 * groups and that IPMP data addresses are down. These conditions must be met
18015 * so that IPMP interfaces can be I_PUNLINK'd, as per ip_sioctl_plink_ipmp().
18017 static void
18018 ip_ipmp_cleanup(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
18020 int af = isv6 ? AF_INET6 : AF_INET;
18021 int i, nifs;
18022 int err;
18023 uint_t bufsize;
18024 uint_t lifrsize = sizeof (struct lifreq);
18025 struct lifconf lifc;
18026 struct lifreq *lifrp;
18028 if ((err = ip_lifconf_ioctl(lh, af, &lifc, &bufsize, cr)) != 0) {
18029 cmn_err(CE_WARN, "ip_ipmp_cleanup: cannot get interface list "
18030 "(error %d); any IPMP interfaces cannot be shutdown", err);
18031 return;
18034 nifs = lifc.lifc_len / lifrsize;
18035 for (lifrp = lifc.lifc_req, i = 0; i < nifs; i++, lifrp++) {
18036 err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
18037 if (err != 0) {
18038 cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot get "
18039 "flags: error %d", lifrp->lifr_name, err);
18040 continue;
18043 if (lifrp->lifr_flags & IFF_IPMP) {
18044 if ((lifrp->lifr_flags & (IFF_UP|IFF_DUPLICATE)) == 0)
18045 continue;
18047 lifrp->lifr_flags &= ~IFF_UP;
18048 err = ip_ioctl(lh, SIOCSLIFFLAGS, lifrp, lifrsize, cr);
18049 if (err != 0) {
18050 cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
18051 "bring down (error %d); IPMP interface may "
18052 "not be shutdown", lifrp->lifr_name, err);
18056 * Check if IFF_DUPLICATE is still set -- and if so,
18057 * reset the address to clear it.
18059 err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
18060 if (err != 0 || !(lifrp->lifr_flags & IFF_DUPLICATE))
18061 continue;
18063 err = ip_ioctl(lh, SIOCGLIFADDR, lifrp, lifrsize, cr);
18064 if (err != 0 || (err = ip_ioctl(lh, SIOCGLIFADDR,
18065 lifrp, lifrsize, cr)) != 0) {
18066 cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
18067 "reset DAD (error %d); IPMP interface may "
18068 "not be shutdown", lifrp->lifr_name, err);
18070 continue;
18073 if (strchr(lifrp->lifr_name, IPIF_SEPARATOR_CHAR) == 0) {
18074 lifrp->lifr_groupname[0] = '\0';
18075 if ((err = ip_ioctl(lh, SIOCSLIFGROUPNAME, lifrp,
18076 lifrsize, cr)) != 0) {
18077 cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
18078 "leave IPMP group (error %d); associated "
18079 "IPMP interface may not be shutdown",
18080 lifrp->lifr_name, err);
18081 continue;
18086 kmem_free(lifc.lifc_buf, bufsize);
18089 #define UDPDEV "/devices/pseudo/udp@0:udp"
18090 #define UDP6DEV "/devices/pseudo/udp6@0:udp6"
18093 * Remove the loopback interfaces and prep the IPMP interfaces to be torn down.
18094 * Non-loopback interfaces are either I_LINK'd or I_PLINK'd; the former go away
18095 * when the user-level processes in the zone are killed and the latter are
18096 * cleaned up by str_stack_shutdown().
18098 void
18099 ip_interface_cleanup(ip_stack_t *ipst)
18101 ldi_handle_t lh;
18102 ldi_ident_t li;
18103 cred_t *cr;
18104 int err;
18105 int i;
18106 char *devs[] = { UDP6DEV, UDPDEV };
18107 netstackid_t stackid = ipst->ips_netstack->netstack_stackid;
18109 if ((err = ldi_ident_from_major(ddi_name_to_major("ip"), &li)) != 0) {
18110 cmn_err(CE_WARN, "ip_interface_cleanup: cannot get ldi ident:"
18111 " error %d", err);
18112 return;
18115 cr = zone_get_kcred(netstackid_to_zoneid(stackid));
18116 ASSERT(cr != NULL);
18119 * NOTE: loop executes exactly twice and is hardcoded to know that the
18120 * first iteration is IPv6. (Unrolling yields repetitious code, hence
18121 * the loop.)
18123 for (i = 0; i < 2; i++) {
18124 err = ldi_open_by_name(devs[i], FREAD|FWRITE, cr, &lh, li);
18125 if (err != 0) {
18126 cmn_err(CE_WARN, "ip_interface_cleanup: cannot open %s:"
18127 " error %d", devs[i], err);
18128 continue;
18131 ip_loopback_removeif(lh, i == 0, cr);
18132 ip_ipmp_cleanup(lh, i == 0, cr);
18134 (void) ldi_close(lh, FREAD|FWRITE, cr);
18137 ldi_ident_release(li);
18138 crfree(cr);
18142 * This needs to be in-sync with nic_event_t definition
18144 static const char *
18145 ill_hook_event2str(nic_event_t event)
18147 switch (event) {
18148 case NE_PLUMB:
18149 return ("PLUMB");
18150 case NE_UNPLUMB:
18151 return ("UNPLUMB");
18152 case NE_UP:
18153 return ("UP");
18154 case NE_DOWN:
18155 return ("DOWN");
18156 case NE_ADDRESS_CHANGE:
18157 return ("ADDRESS_CHANGE");
18158 case NE_LIF_UP:
18159 return ("LIF_UP");
18160 case NE_LIF_DOWN:
18161 return ("LIF_DOWN");
18162 case NE_IFINDEX_CHANGE:
18163 return ("IFINDEX_CHANGE");
18164 default:
18165 return ("UNKNOWN");
18169 void
18170 ill_nic_event_dispatch(ill_t *ill, lif_if_t lif, nic_event_t event,
18171 nic_event_data_t data, size_t datalen)
18173 ip_stack_t *ipst = ill->ill_ipst;
18174 hook_nic_event_int_t *info;
18175 const char *str = NULL;
18177 /* create a new nic event info */
18178 if ((info = kmem_alloc(sizeof (*info), KM_NOSLEEP)) == NULL)
18179 goto fail;
18181 info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
18182 info->hnei_event.hne_lif = lif;
18183 info->hnei_event.hne_event = event;
18184 info->hnei_event.hne_protocol = ill->ill_isv6 ?
18185 ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
18186 info->hnei_event.hne_data = NULL;
18187 info->hnei_event.hne_datalen = 0;
18188 info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
18190 if (data != NULL && datalen != 0) {
18191 info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
18192 if (info->hnei_event.hne_data == NULL)
18193 goto fail;
18194 bcopy(data, info->hnei_event.hne_data, datalen);
18195 info->hnei_event.hne_datalen = datalen;
18198 if (ddi_taskq_dispatch(eventq_queue_nic, ip_ne_queue_func, info,
18199 DDI_NOSLEEP) == DDI_SUCCESS)
18200 return;
18202 fail:
18203 if (info != NULL) {
18204 if (info->hnei_event.hne_data != NULL) {
18205 kmem_free(info->hnei_event.hne_data,
18206 info->hnei_event.hne_datalen);
18208 kmem_free(info, sizeof (hook_nic_event_t));
18210 str = ill_hook_event2str(event);
18211 ip2dbg(("ill_nic_event_dispatch: could not dispatch %s nic event "
18212 "information for %s (ENOMEM)\n", str, ill->ill_name));
18215 static int
18216 ipif_arp_up_done_tail(ipif_t *ipif, enum ip_resolver_action res_act)
18218 int err = 0;
18219 const in_addr_t *addr = NULL;
18220 nce_t *nce = NULL;
18221 ill_t *ill = ipif->ipif_ill;
18222 ill_t *bound_ill;
18223 boolean_t added_ipif = B_FALSE;
18224 uint16_t state;
18225 uint16_t flags;
18227 DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up_done_tail",
18228 ill_t *, ill, ipif_t *, ipif);
18229 if (ipif->ipif_lcl_addr != INADDR_ANY) {
18230 addr = &ipif->ipif_lcl_addr;
18233 if ((ipif->ipif_flags & IPIF_UNNUMBERED) || addr == NULL) {
18234 if (res_act != Res_act_initial)
18235 return (EINVAL);
18238 if (addr != NULL) {
18239 ipmp_illgrp_t *illg = ill->ill_grp;
18241 /* add unicast nce for the local addr */
18243 if (IS_IPMP(ill)) {
18245 * If we're here via ipif_up(), then the ipif
18246 * won't be bound yet -- add it to the group,
18247 * which will bind it if possible. (We would
18248 * add it in ipif_up(), but deleting on failure
18249 * there is gruesome.) If we're here via
18250 * ipmp_ill_bind_ipif(), then the ipif has
18251 * already been added to the group and we
18252 * just need to use the binding.
18254 if ((bound_ill = ipmp_ipif_bound_ill(ipif)) == NULL) {
18255 bound_ill = ipmp_illgrp_add_ipif(illg, ipif);
18256 if (bound_ill == NULL) {
18258 * We couldn't bind the ipif to an ill
18259 * yet, so we have nothing to publish.
18260 * Mark the address as ready and return.
18262 ipif->ipif_addr_ready = 1;
18263 return (0);
18265 added_ipif = B_TRUE;
18267 } else {
18268 bound_ill = ill;
18271 flags = (NCE_F_MYADDR | NCE_F_PUBLISH | NCE_F_AUTHORITY |
18272 NCE_F_NONUD);
18274 * If this is an initial bring-up (or the ipif was never
18275 * completely brought up), do DAD. Otherwise, we're here
18276 * because IPMP has rebound an address to this ill: send
18277 * unsolicited advertisements (ARP announcements) to
18278 * inform others.
18280 if (res_act == Res_act_initial || !ipif->ipif_addr_ready) {
18281 state = ND_UNCHANGED; /* compute in nce_add_common() */
18282 } else {
18283 state = ND_REACHABLE;
18284 flags |= NCE_F_UNSOL_ADV;
18287 retry:
18288 err = nce_lookup_then_add_v4(ill,
18289 bound_ill->ill_phys_addr, bound_ill->ill_phys_addr_length,
18290 addr, flags, state, &nce);
18293 * note that we may encounter EEXIST if we are moving
18294 * the nce as a result of a rebind operation.
18296 switch (err) {
18297 case 0:
18298 ipif->ipif_added_nce = 1;
18299 nce->nce_ipif_cnt++;
18300 break;
18301 case EEXIST:
18302 ip1dbg(("ipif_arp_up: NCE already exists for %s\n",
18303 ill->ill_name));
18304 if (!NCE_MYADDR(nce->nce_common)) {
18306 * A leftover nce from before this address
18307 * existed
18309 ncec_delete(nce->nce_common);
18310 nce_refrele(nce);
18311 nce = NULL;
18312 goto retry;
18314 if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
18315 nce_refrele(nce);
18316 nce = NULL;
18317 ip1dbg(("ipif_arp_up: NCE already exists "
18318 "for %s:%u\n", ill->ill_name,
18319 ipif->ipif_id));
18320 goto arp_up_done;
18323 * Duplicate local addresses are permissible for
18324 * IPIF_POINTOPOINT interfaces which will get marked
18325 * IPIF_UNNUMBERED later in
18326 * ip_addr_availability_check().
18328 * The nce_ipif_cnt field tracks the number of
18329 * ipifs that have nce_addr as their local address.
18331 ipif->ipif_addr_ready = 1;
18332 ipif->ipif_added_nce = 1;
18333 nce->nce_ipif_cnt++;
18334 err = 0;
18335 break;
18336 default:
18337 ASSERT(nce == NULL);
18338 goto arp_up_done;
18340 if (arp_no_defense) {
18341 if ((ipif->ipif_flags & IPIF_UP) &&
18342 !ipif->ipif_addr_ready)
18343 ipif_up_notify(ipif);
18344 ipif->ipif_addr_ready = 1;
18346 } else {
18347 /* zero address. nothing to publish */
18348 ipif->ipif_addr_ready = 1;
18350 if (nce != NULL)
18351 nce_refrele(nce);
18352 arp_up_done:
18353 if (added_ipif && err != 0)
18354 ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
18355 return (err);
18359 ipif_arp_up(ipif_t *ipif, enum ip_resolver_action res_act, boolean_t was_dup)
18361 int err = 0;
18362 ill_t *ill = ipif->ipif_ill;
18363 boolean_t first_interface, wait_for_dlpi = B_FALSE;
18365 DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up",
18366 ill_t *, ill, ipif_t *, ipif);
18369 * need to bring up ARP or setup mcast mapping only
18370 * when the first interface is coming UP.
18372 first_interface = (ill->ill_ipif_up_count == 0 &&
18373 ill->ill_ipif_dup_count == 0 && !was_dup);
18375 if (res_act == Res_act_initial && first_interface) {
18377 * Send ATTACH + BIND
18379 err = arp_ll_up(ill);
18380 if (err != EINPROGRESS && err != 0)
18381 return (err);
18384 * Add NCE for local address. Start DAD.
18385 * we'll wait to hear that DAD has finished
18386 * before using the interface.
18388 if (err == EINPROGRESS)
18389 wait_for_dlpi = B_TRUE;
18392 if (!wait_for_dlpi)
18393 (void) ipif_arp_up_done_tail(ipif, res_act);
18395 return (!wait_for_dlpi ? 0 : EINPROGRESS);
18399 * Finish processing of "arp_up" after all the DLPI message
18400 * exchanges have completed between arp and the driver.
18402 void
18403 arp_bringup_done(ill_t *ill, int err)
18405 mblk_t *mp1;
18406 ipif_t *ipif;
18407 conn_t *connp = NULL;
18408 ipsq_t *ipsq;
18409 queue_t *q;
18411 ip1dbg(("arp_bringup_done(%s)\n", ill->ill_name));
18413 ASSERT(IAM_WRITER_ILL(ill));
18415 ipsq = ill->ill_phyint->phyint_ipsq;
18416 ipif = ipsq->ipsq_xop->ipx_pending_ipif;
18417 mp1 = ipsq_pending_mp_get(ipsq, &connp);
18418 ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
18419 if (mp1 == NULL) /* bringup was aborted by the user */
18420 return;
18423 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
18424 * must have an associated conn_t. Otherwise, we're bringing this
18425 * interface back up as part of handling an asynchronous event (e.g.,
18426 * physical address change).
18428 if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18429 ASSERT(connp != NULL);
18430 q = CONNP_TO_WQ(connp);
18431 } else {
18432 ASSERT(connp == NULL);
18433 q = ill->ill_rq;
18435 if (err == 0) {
18436 if (ipif->ipif_isv6) {
18437 if ((err = ipif_up_done_v6(ipif)) != 0)
18438 ip0dbg(("arp_bringup_done: init failed\n"));
18439 } else {
18440 err = ipif_arp_up_done_tail(ipif, Res_act_initial);
18441 if (err != 0 ||
18442 (err = ipif_up_done(ipif)) != 0) {
18443 ip0dbg(("arp_bringup_done: "
18444 "init failed err %x\n", err));
18445 (void) ipif_arp_down(ipif);
18449 } else {
18450 ip0dbg(("arp_bringup_done: DL_BIND_REQ failed\n"));
18453 if ((err == 0) && (ill->ill_up_ipifs)) {
18454 err = ill_up_ipifs(ill, q, mp1);
18455 if (err == EINPROGRESS)
18456 return;
18460 * If we have a moved ipif to bring up, and everything has succeeded
18461 * to this point, bring it up on the IPMP ill. Otherwise, leave it
18462 * down -- the admin can try to bring it up by hand if need be.
18464 if (ill->ill_move_ipif != NULL) {
18465 ipif = ill->ill_move_ipif;
18466 ip1dbg(("bringing up ipif %p on ill %s\n", (void *)ipif,
18467 ipif->ipif_ill->ill_name));
18468 ill->ill_move_ipif = NULL;
18469 if (err == 0) {
18470 err = ipif_up(ipif, q, mp1);
18471 if (err == EINPROGRESS)
18472 return;
18477 * The operation must complete without EINPROGRESS since
18478 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
18479 * Otherwise, the operation will be stuck forever in the ipsq.
18481 ASSERT(err != EINPROGRESS);
18482 if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18483 DTRACE_PROBE4(ipif__ioctl, char *, "arp_bringup_done finish",
18484 int, ipsq->ipsq_xop->ipx_current_ioctl,
18485 ill_t *, ill, ipif_t *, ipif);
18486 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
18487 } else {
18488 ipsq_current_finish(ipsq);
18493 * Finish processing of arp replumb after all the DLPI message
18494 * exchanges have completed between arp and the driver.
18496 void
18497 arp_replumb_done(ill_t *ill, int err)
18499 mblk_t *mp1;
18500 ipif_t *ipif;
18501 conn_t *connp = NULL;
18502 ipsq_t *ipsq;
18503 queue_t *q;
18505 ASSERT(IAM_WRITER_ILL(ill));
18507 ipsq = ill->ill_phyint->phyint_ipsq;
18508 ipif = ipsq->ipsq_xop->ipx_pending_ipif;
18509 mp1 = ipsq_pending_mp_get(ipsq, &connp);
18510 ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
18511 if (mp1 == NULL) {
18512 ip0dbg(("arp_replumb_done: bringup aborted ioctl %x\n",
18513 ipsq->ipsq_xop->ipx_current_ioctl));
18514 /* bringup was aborted by the user */
18515 return;
18518 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
18519 * must have an associated conn_t. Otherwise, we're bringing this
18520 * interface back up as part of handling an asynchronous event (e.g.,
18521 * physical address change).
18523 if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18524 ASSERT(connp != NULL);
18525 q = CONNP_TO_WQ(connp);
18526 } else {
18527 ASSERT(connp == NULL);
18528 q = ill->ill_rq;
18530 if ((err == 0) && (ill->ill_up_ipifs)) {
18531 err = ill_up_ipifs(ill, q, mp1);
18532 if (err == EINPROGRESS)
18533 return;
18536 * The operation must complete without EINPROGRESS since
18537 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
18538 * Otherwise, the operation will be stuck forever in the ipsq.
18540 ASSERT(err != EINPROGRESS);
18541 if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18542 DTRACE_PROBE4(ipif__ioctl, char *,
18543 "arp_replumb_done finish",
18544 int, ipsq->ipsq_xop->ipx_current_ioctl,
18545 ill_t *, ill, ipif_t *, ipif);
18546 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
18547 } else {
18548 ipsq_current_finish(ipsq);
18552 void
18553 ipif_up_notify(ipif_t *ipif)
18555 ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
18556 ip_rts_newaddrmsg(RTM_ADD, 0, ipif, RTSQ_DEFAULT);
18557 sctp_update_ipif(ipif, SCTP_IPIF_UP);
18558 ill_nic_event_dispatch(ipif->ipif_ill, MAP_IPIF_ID(ipif->ipif_id),
18559 NE_LIF_UP, NULL, 0);
18563 * ILB ioctl uses cv_wait (such as deleting a rule or adding a server) and
18564 * this assumes the context is cv_wait'able. Hence it shouldnt' be used on
18565 * TPI end points with STREAMS modules pushed above. This is assured by not
18566 * having the IPI_MODOK flag for the ioctl. And IP ensures the ILB ioctl
18567 * never ends up on an ipsq, otherwise we may end up processing the ioctl
18568 * while unwinding from the ispq and that could be a thread from the bottom.
18570 /* ARGSUSED */
18572 ip_sioctl_ilb_cmd(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18573 ip_ioctl_cmd_t *ipip, void *arg)
18575 mblk_t *cmd_mp = mp->b_cont->b_cont;
18576 ilb_cmd_t command = *((ilb_cmd_t *)cmd_mp->b_rptr);
18577 int ret = 0;
18578 int i;
18579 size_t size;
18580 ip_stack_t *ipst;
18581 zoneid_t zoneid;
18582 ilb_stack_t *ilbs;
18584 ipst = CONNQ_TO_IPST(q);
18585 ilbs = ipst->ips_netstack->netstack_ilb;
18586 zoneid = Q_TO_CONN(q)->conn_zoneid;
18588 switch (command) {
18589 case ILB_CREATE_RULE: {
18590 ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18592 if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18593 ret = EINVAL;
18594 break;
18597 ret = ilb_rule_add(ilbs, zoneid, cmd);
18598 break;
18600 case ILB_DESTROY_RULE:
18601 case ILB_ENABLE_RULE:
18602 case ILB_DISABLE_RULE: {
18603 ilb_name_cmd_t *cmd = (ilb_name_cmd_t *)cmd_mp->b_rptr;
18605 if (MBLKL(cmd_mp) != sizeof (ilb_name_cmd_t)) {
18606 ret = EINVAL;
18607 break;
18610 if (cmd->flags & ILB_RULE_ALLRULES) {
18611 if (command == ILB_DESTROY_RULE) {
18612 ilb_rule_del_all(ilbs, zoneid);
18613 break;
18614 } else if (command == ILB_ENABLE_RULE) {
18615 ilb_rule_enable_all(ilbs, zoneid);
18616 break;
18617 } else if (command == ILB_DISABLE_RULE) {
18618 ilb_rule_disable_all(ilbs, zoneid);
18619 break;
18621 } else {
18622 if (command == ILB_DESTROY_RULE) {
18623 ret = ilb_rule_del(ilbs, zoneid, cmd->name);
18624 } else if (command == ILB_ENABLE_RULE) {
18625 ret = ilb_rule_enable(ilbs, zoneid, cmd->name,
18626 NULL);
18627 } else if (command == ILB_DISABLE_RULE) {
18628 ret = ilb_rule_disable(ilbs, zoneid, cmd->name,
18629 NULL);
18632 break;
18634 case ILB_NUM_RULES: {
18635 ilb_num_rules_cmd_t *cmd;
18637 if (MBLKL(cmd_mp) != sizeof (ilb_num_rules_cmd_t)) {
18638 ret = EINVAL;
18639 break;
18641 cmd = (ilb_num_rules_cmd_t *)cmd_mp->b_rptr;
18642 ilb_get_num_rules(ilbs, zoneid, &(cmd->num));
18643 break;
18645 case ILB_RULE_NAMES: {
18646 ilb_rule_names_cmd_t *cmd;
18648 cmd = (ilb_rule_names_cmd_t *)cmd_mp->b_rptr;
18649 if (MBLKL(cmd_mp) < sizeof (ilb_rule_names_cmd_t) ||
18650 cmd->num_names == 0) {
18651 ret = EINVAL;
18652 break;
18654 size = cmd->num_names * ILB_RULE_NAMESZ;
18655 if (cmd_mp->b_rptr + offsetof(ilb_rule_names_cmd_t, buf) +
18656 size != cmd_mp->b_wptr) {
18657 ret = EINVAL;
18658 break;
18660 ilb_get_rulenames(ilbs, zoneid, &cmd->num_names, cmd->buf);
18661 break;
18663 case ILB_NUM_SERVERS: {
18664 ilb_num_servers_cmd_t *cmd;
18666 if (MBLKL(cmd_mp) != sizeof (ilb_num_servers_cmd_t)) {
18667 ret = EINVAL;
18668 break;
18670 cmd = (ilb_num_servers_cmd_t *)cmd_mp->b_rptr;
18671 ret = ilb_get_num_servers(ilbs, zoneid, cmd->name,
18672 &(cmd->num));
18673 break;
18675 case ILB_LIST_RULE: {
18676 ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18678 if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18679 ret = EINVAL;
18680 break;
18682 ret = ilb_rule_list(ilbs, zoneid, cmd);
18683 break;
18685 case ILB_LIST_SERVERS: {
18686 ilb_servers_info_cmd_t *cmd;
18688 cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18689 if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t) ||
18690 cmd->num_servers == 0) {
18691 ret = EINVAL;
18692 break;
18694 size = cmd->num_servers * sizeof (ilb_server_info_t);
18695 if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18696 size != cmd_mp->b_wptr) {
18697 ret = EINVAL;
18698 break;
18701 ret = ilb_get_servers(ilbs, zoneid, cmd->name, cmd->servers,
18702 &cmd->num_servers);
18703 break;
18705 case ILB_ADD_SERVERS: {
18706 ilb_servers_info_cmd_t *cmd;
18707 ilb_rule_t *rule;
18709 cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18710 if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t)) {
18711 ret = EINVAL;
18712 break;
18714 size = cmd->num_servers * sizeof (ilb_server_info_t);
18715 if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18716 size != cmd_mp->b_wptr) {
18717 ret = EINVAL;
18718 break;
18720 rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18721 if (rule == NULL) {
18722 ASSERT(ret != 0);
18723 break;
18725 for (i = 0; i < cmd->num_servers; i++) {
18726 ilb_server_info_t *s;
18728 s = &cmd->servers[i];
18729 s->err = ilb_server_add(ilbs, rule, s);
18731 ILB_RULE_REFRELE(rule);
18732 break;
18734 case ILB_DEL_SERVERS:
18735 case ILB_ENABLE_SERVERS:
18736 case ILB_DISABLE_SERVERS: {
18737 ilb_servers_cmd_t *cmd;
18738 ilb_rule_t *rule;
18739 int (*f)();
18741 cmd = (ilb_servers_cmd_t *)cmd_mp->b_rptr;
18742 if (MBLKL(cmd_mp) < sizeof (ilb_servers_cmd_t)) {
18743 ret = EINVAL;
18744 break;
18746 size = cmd->num_servers * sizeof (ilb_server_arg_t);
18747 if (cmd_mp->b_rptr + offsetof(ilb_servers_cmd_t, servers) +
18748 size != cmd_mp->b_wptr) {
18749 ret = EINVAL;
18750 break;
18753 if (command == ILB_DEL_SERVERS)
18754 f = ilb_server_del;
18755 else if (command == ILB_ENABLE_SERVERS)
18756 f = ilb_server_enable;
18757 else if (command == ILB_DISABLE_SERVERS)
18758 f = ilb_server_disable;
18760 rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18761 if (rule == NULL) {
18762 ASSERT(ret != 0);
18763 break;
18766 for (i = 0; i < cmd->num_servers; i++) {
18767 ilb_server_arg_t *s;
18769 s = &cmd->servers[i];
18770 s->err = f(ilbs, zoneid, NULL, rule, &s->addr);
18772 ILB_RULE_REFRELE(rule);
18773 break;
18775 case ILB_LIST_NAT_TABLE: {
18776 ilb_list_nat_cmd_t *cmd;
18778 cmd = (ilb_list_nat_cmd_t *)cmd_mp->b_rptr;
18779 if (MBLKL(cmd_mp) < sizeof (ilb_list_nat_cmd_t)) {
18780 ret = EINVAL;
18781 break;
18783 size = cmd->num_nat * sizeof (ilb_nat_entry_t);
18784 if (cmd_mp->b_rptr + offsetof(ilb_list_nat_cmd_t, entries) +
18785 size != cmd_mp->b_wptr) {
18786 ret = EINVAL;
18787 break;
18790 ret = ilb_list_nat(ilbs, zoneid, cmd->entries, &cmd->num_nat,
18791 &cmd->flags);
18792 break;
18794 case ILB_LIST_STICKY_TABLE: {
18795 ilb_list_sticky_cmd_t *cmd;
18797 cmd = (ilb_list_sticky_cmd_t *)cmd_mp->b_rptr;
18798 if (MBLKL(cmd_mp) < sizeof (ilb_list_sticky_cmd_t)) {
18799 ret = EINVAL;
18800 break;
18802 size = cmd->num_sticky * sizeof (ilb_sticky_entry_t);
18803 if (cmd_mp->b_rptr + offsetof(ilb_list_sticky_cmd_t, entries) +
18804 size != cmd_mp->b_wptr) {
18805 ret = EINVAL;
18806 break;
18809 ret = ilb_list_sticky(ilbs, zoneid, cmd->entries,
18810 &cmd->num_sticky, &cmd->flags);
18811 break;
18813 default:
18814 ret = EINVAL;
18815 break;
18817 done:
18818 return (ret);
18821 /* Remove all cache entries for this logical interface */
18822 void
18823 ipif_nce_down(ipif_t *ipif)
18825 ill_t *ill = ipif->ipif_ill;
18826 nce_t *nce;
18828 DTRACE_PROBE3(ipif__downup, char *, "ipif_nce_down",
18829 ill_t *, ill, ipif_t *, ipif);
18830 if (ipif->ipif_added_nce) {
18831 if (ipif->ipif_isv6)
18832 nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
18833 else
18834 nce = nce_lookup_v4(ill, &ipif->ipif_lcl_addr);
18835 if (nce != NULL) {
18836 if (--nce->nce_ipif_cnt == 0)
18837 ncec_delete(nce->nce_common);
18838 ipif->ipif_added_nce = 0;
18839 nce_refrele(nce);
18840 } else {
18842 * nce may already be NULL because it was already
18843 * flushed, e.g., due to a call to nce_flush
18845 ipif->ipif_added_nce = 0;
18849 * Make IPMP aware of the deleted data address.
18851 if (IS_IPMP(ill))
18852 ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
18855 * Remove all other nces dependent on this ill when the last ipif
18856 * is going away.
18858 if (ill->ill_ipif_up_count == 0) {
18859 ncec_walk(ill, (pfi_t)ncec_delete_per_ill,
18860 (uchar_t *)ill, ill->ill_ipst);
18861 if (IS_UNDER_IPMP(ill))
18862 nce_flush(ill, B_TRUE);
18867 * find the first interface that uses usill for its source address.
18869 ill_t *
18870 ill_lookup_usesrc(ill_t *usill)
18872 ip_stack_t *ipst = usill->ill_ipst;
18873 ill_t *ill;
18875 ASSERT(usill != NULL);
18877 /* ill_g_usesrc_lock protects ill_usesrc_grp_next */
18878 rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
18879 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
18880 for (ill = usill->ill_usesrc_grp_next; ill != NULL && ill != usill;
18881 ill = ill->ill_usesrc_grp_next) {
18882 if (!IS_UNDER_IPMP(ill) && (ill->ill_flags & ILLF_MULTICAST) &&
18883 !ILL_IS_CONDEMNED(ill)) {
18884 ill_refhold(ill);
18885 break;
18888 rw_exit(&ipst->ips_ill_g_lock);
18889 rw_exit(&ipst->ips_ill_g_usesrc_lock);
18890 return (ill);
18894 * This comment applies to both ip_sioctl_get_ifhwaddr and
18895 * ip_sioctl_get_lifhwaddr as the basic function of these two functions
18896 * is the same.
18898 * The goal here is to find an IP interface that corresponds to the name
18899 * provided by the caller in the ifreq/lifreq structure held in the mblk_t
18900 * chain and to fill out a sockaddr/sockaddr_storage structure with the
18901 * mac address.
18903 * The SIOCGIFHWADDR/SIOCGLIFHWADDR ioctl may return an error for a number
18904 * of different reasons:
18905 * ENXIO - the device name is not known to IP.
18906 * EADDRNOTAVAIL - the device has no hardware address. This is indicated
18907 * by ill_phys_addr not pointing to an actual address.
18908 * EPFNOSUPPORT - this will indicate that a request is being made for a
18909 * mac address that will not fit in the data structure supplier (struct
18910 * sockaddr).
18913 /* ARGSUSED */
18915 ip_sioctl_get_ifhwaddr(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
18916 ip_ioctl_cmd_t *ipip, void *if_req)
18918 struct sockaddr *sock;
18919 struct ifreq *ifr;
18920 mblk_t *mp1;
18921 ill_t *ill;
18923 ASSERT(ipif != NULL);
18924 ill = ipif->ipif_ill;
18926 if (ill->ill_phys_addr == NULL) {
18927 return (EADDRNOTAVAIL);
18929 if (ill->ill_phys_addr_length > sizeof (sock->sa_data)) {
18930 return (EPFNOSUPPORT);
18933 ip1dbg(("ip_sioctl_get_hwaddr(%s)\n", ill->ill_name));
18935 /* Existence of mp1 has been checked in ip_wput_nondata */
18936 mp1 = mp->b_cont->b_cont;
18937 ifr = (struct ifreq *)mp1->b_rptr;
18939 sock = &ifr->ifr_addr;
18941 * The "family" field in the returned structure is set to a value
18942 * that represents the type of device to which the address belongs.
18943 * The value returned may differ to that on Linux but it will still
18944 * represent the correct symbol on Solaris.
18946 sock->sa_family = arp_hw_type(ill->ill_mactype);
18947 bcopy(ill->ill_phys_addr, &sock->sa_data, ill->ill_phys_addr_length);
18949 return (0);
18953 * The expection of applications using SIOCGIFHWADDR is that data will
18954 * be returned in the sa_data field of the sockaddr structure. With
18955 * SIOCGLIFHWADDR, we're breaking new ground as there is no Linux
18956 * equivalent. In light of this, struct sockaddr_dl is used as it
18957 * offers more space for address storage in sll_data.
18959 /* ARGSUSED */
18961 ip_sioctl_get_lifhwaddr(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
18962 ip_ioctl_cmd_t *ipip, void *if_req)
18964 struct sockaddr_dl *sock;
18965 struct lifreq *lifr;
18966 mblk_t *mp1;
18967 ill_t *ill;
18969 ASSERT(ipif != NULL);
18970 ill = ipif->ipif_ill;
18972 if (ill->ill_phys_addr == NULL) {
18973 return (EADDRNOTAVAIL);
18975 if (ill->ill_phys_addr_length > sizeof (sock->sdl_data)) {
18976 return (EPFNOSUPPORT);
18979 ip1dbg(("ip_sioctl_get_lifhwaddr(%s)\n", ill->ill_name));
18981 /* Existence of mp1 has been checked in ip_wput_nondata */
18982 mp1 = mp->b_cont->b_cont;
18983 lifr = (struct lifreq *)mp1->b_rptr;
18986 * sockaddr_ll is used here because it is also the structure used in
18987 * responding to the same ioctl in sockpfp. The only other choice is
18988 * sockaddr_dl which contains fields that are not required here
18989 * because its purpose is different.
18991 lifr->lifr_type = ill->ill_type;
18992 sock = (struct sockaddr_dl *)&lifr->lifr_addr;
18993 sock->sdl_family = AF_LINK;
18994 sock->sdl_index = ill->ill_phyint->phyint_ifindex;
18995 sock->sdl_type = ill->ill_mactype;
18996 sock->sdl_nlen = 0;
18997 sock->sdl_slen = 0;
18998 sock->sdl_alen = ill->ill_phys_addr_length;
18999 bcopy(ill->ill_phys_addr, sock->sdl_data, ill->ill_phys_addr_length);
19001 return (0);