Merge commit 'b1e7e97d3b60469b243b3b2e22c7d8cbd11c7c90'
[unleashed.git] / kernel / net / ip / ip_ire.c
blobbc62025398acdfb71adf1b58c3e99582c9837451
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.
27 * This file contains routines that manipulate Internet Routing Entries (IREs).
30 #include <sys/types.h>
31 #include <sys/stream.h>
32 #include <sys/stropts.h>
33 #include <sys/strsun.h>
34 #include <sys/strsubr.h>
35 #include <sys/ddi.h>
36 #include <sys/cmn_err.h>
37 #include <sys/policy.h>
39 #include <sys/systm.h>
40 #include <sys/kmem.h>
41 #include <sys/param.h>
42 #include <sys/socket.h>
43 #include <net/if.h>
44 #include <net/route.h>
45 #include <netinet/in.h>
46 #include <net/if_dl.h>
47 #include <netinet/ip6.h>
48 #include <netinet/icmp6.h>
50 #include <inet/common.h>
51 #include <inet/mi.h>
52 #include <inet/ip.h>
53 #include <inet/ip6.h>
54 #include <inet/ip_ndp.h>
55 #include <inet/arp.h>
56 #include <inet/ip_if.h>
57 #include <inet/ip_ire.h>
58 #include <inet/ip_ftable.h>
59 #include <inet/ip_rts.h>
60 #include <inet/nd.h>
61 #include <inet/tunables.h>
63 #include <inet/tcp.h>
64 #include <inet/ipclassifier.h>
65 #include <sys/zone.h>
66 #include <sys/cpuvar.h>
68 struct kmem_cache *rt_entry_cache;
70 typedef struct nce_clookup_s {
71 ipaddr_t ncecl_addr;
72 boolean_t ncecl_found;
73 } nce_clookup_t;
76 * Synchronization notes:
78 * The fields of the ire_t struct are protected in the following way :
80 * ire_next/ire_ptpn
82 * - bucket lock of the forwarding table in which is ire stored.
84 * ire_ill, ire_u *except* ire_gateway_addr[v6], ire_mask,
85 * ire_type, ire_create_time, ire_masklen, ire_ipversion, ire_flags,
86 * ire_bucket
88 * - Set in ire_create_v4/v6 and never changes after that. Thus,
89 * we don't need a lock whenever these fields are accessed.
91 * - ire_bucket and ire_masklen (also set in ire_create) is set in
92 * ire_add before inserting in the bucket and never
93 * changes after that. Thus we don't need a lock whenever these
94 * fields are accessed.
96 * ire_gateway_addr_v4[v6]
98 * - ire_gateway_addr_v4[v6] is set during ire_create and later modified
99 * by rts_setgwr[v6]. As ire_gateway_addr is a uint32_t, updates to
100 * it assumed to be atomic and hence the other parts of the code
101 * does not use any locks. ire_gateway_addr_v6 updates are not atomic
102 * and hence any access to it uses ire_lock to get/set the right value.
104 * ire_refcnt, ire_identical_ref
106 * - Updated atomically using atomic_add_32
108 * ire_ssthresh, ire_rtt_sd, ire_rtt, ire_ib_pkt_count, ire_ob_pkt_count
110 * - Assumes that 32 bit writes are atomic. No locks. ire_lock is
111 * used to serialize updates to ire_ssthresh, ire_rtt_sd, ire_rtt.
113 * ire_generation
114 * - Under ire_lock
116 * ire_nce_cache
117 * - Under ire_lock
119 * ire_dep_parent (To next IRE in recursive lookup chain)
120 * - Under ips_ire_dep_lock. Write held when modifying. Read held when
121 * walking. We also hold ire_lock when modifying to allow the data path
122 * to only acquire ire_lock.
124 * ire_dep_parent_generation (Generation number from ire_dep_parent)
125 * - Under ips_ire_dep_lock and/or ire_lock. (A read claim on the dep_lock
126 * and ire_lock held when modifying)
128 * ire_dep_children (From parent to first child)
129 * ire_dep_sib_next (linked list of siblings)
130 * ire_dep_sib_ptpn (linked list of siblings)
131 * - Under ips_ire_dep_lock. Write held when modifying. Read held when
132 * walking.
134 * As we always hold the bucket locks in all the places while accessing
135 * the above values, it is natural to use them for protecting them.
137 * We have a forwarding table for IPv4 and IPv6. The IPv6 forwarding table
138 * (ip_forwarding_table_v6) is an array of pointers to arrays of irb_t
139 * structures. ip_forwarding_table_v6 is allocated dynamically in
140 * ire_add_v6. ire_ft_init_lock is used to serialize multiple threads
141 * initializing the same bucket. Once a bucket is initialized, it is never
142 * de-alloacted. This assumption enables us to access
143 * ip_forwarding_table_v6[i] without any locks.
145 * The forwarding table for IPv4 is a radix tree whose leaves
146 * are rt_entry structures containing the irb_t for the rt_dst. The irb_t
147 * for IPv4 is dynamically allocated and freed.
149 * Each irb_t - ire bucket structure has a lock to protect
150 * a bucket and the ires residing in the bucket have a back pointer to
151 * the bucket structure. It also has a reference count for the number
152 * of threads walking the bucket - irb_refcnt which is bumped up
153 * using the irb_refhold function. The flags irb_marks can be
154 * set to IRB_MARK_CONDEMNED indicating that there are some ires
155 * in this bucket that are IRE_IS_CONDEMNED and the
156 * last thread to leave the bucket should delete the ires. Usually
157 * this is done by the irb_refrele function which is used to decrement
158 * the reference count on a bucket. See comments above irb_t structure
159 * definition in ip.h for further details.
161 * The ire_refhold/ire_refrele functions operate on the ire which increments/
162 * decrements the reference count, ire_refcnt, atomically on the ire.
163 * ire_refcnt is modified only using those functions. Operations on the IRE
164 * could be described as follows :
166 * CREATE an ire with reference count initialized to 1.
168 * ADDITION of an ire holds the bucket lock, checks for duplicates
169 * and then adds the ire. ire_add returns the ire after
170 * bumping up once more i.e the reference count is 2. This is to avoid
171 * an extra lookup in the functions calling ire_add which wants to
172 * work with the ire after adding.
174 * LOOKUP of an ire bumps up the reference count using ire_refhold
175 * function. It is valid to bump up the referece count of the IRE,
176 * after the lookup has returned an ire. Following are the lookup
177 * functions that return an HELD ire :
179 * ire_ftable_lookup[_v6], ire_lookup_multi_ill[_v6]
181 * DELETION of an ire holds the bucket lock, removes it from the list
182 * and then decrements the reference count for having removed from the list
183 * by using the ire_refrele function. If some other thread has looked up
184 * the ire, the reference count would have been bumped up and hence
185 * this ire will not be freed once deleted. It will be freed once the
186 * reference count drops to zero.
188 * Add and Delete acquires the bucket lock as RW_WRITER, while all the
189 * lookups acquire the bucket lock as RW_READER.
191 * The general rule is to do the ire_refrele in the function
192 * that is passing the ire as an argument.
194 * In trying to locate ires the following points are to be noted.
196 * IRE_IS_CONDEMNED signifies that the ire has been logically deleted and is
197 * to be ignored when walking the ires using ire_next.
199 * Zones note:
200 * Walking IREs within a given zone also walks certain ires in other
201 * zones. This is done intentionally. IRE walks with a specified
202 * zoneid are used only when doing informational reports, and
203 * zone users want to see things that they can access. See block
204 * comment in ire_walk_ill_match().
208 * The size of the forwarding table. We will make sure that it is a
209 * power of 2 in ip_ire_init().
210 * Setable in /etc/system
212 uint32_t ip6_ftable_hash_size = IP6_FTABLE_HASH_SIZE;
214 struct kmem_cache *ire_cache;
215 struct kmem_cache *ncec_cache;
216 struct kmem_cache *nce_cache;
218 static ire_t ire_null;
220 static ire_t *ire_add_v4(ire_t *ire);
221 static void ire_delete_v4(ire_t *ire);
222 static void ire_dep_invalidate_children(ire_t *child);
223 static void ire_walk_ipvers(pfv_t func, void *arg, uchar_t vers,
224 zoneid_t zoneid, ip_stack_t *);
225 static void ire_walk_ill_ipvers(uint_t match_flags, uint_t ire_type,
226 pfv_t func, void *arg, uchar_t vers, ill_t *ill);
227 #ifdef DEBUG
228 static void ire_trace_cleanup(const ire_t *);
229 #endif
230 static void ire_dep_incr_generation_locked(ire_t *);
233 * Following are the functions to increment/decrement the reference
234 * count of the IREs and IRBs (ire bucket).
236 * 1) We bump up the reference count of an IRE to make sure that
237 * it does not get deleted and freed while we are using it.
238 * Typically all the lookup functions hold the bucket lock,
239 * and look for the IRE. If it finds an IRE, it bumps up the
240 * reference count before dropping the lock. Sometimes we *may* want
241 * to bump up the reference count after we *looked* up i.e without
242 * holding the bucket lock. So, the ire_refhold function does not assert
243 * on the bucket lock being held. Any thread trying to delete from
244 * the hash bucket can still do so but cannot free the IRE if
245 * ire_refcnt is not 0.
247 * 2) We bump up the reference count on the bucket where the IRE resides
248 * (IRB), when we want to prevent the IREs getting deleted from a given
249 * hash bucket. This makes life easier for ire_walk type functions which
250 * wants to walk the IRE list, call a function, but needs to drop
251 * the bucket lock to prevent recursive rw_enters. While the
252 * lock is dropped, the list could be changed by other threads or
253 * the same thread could end up deleting the ire or the ire pointed by
254 * ire_next. ire_refholding the ire or ire_next is not sufficient as
255 * a delete will still remove the ire from the bucket while we have
256 * dropped the lock and hence the ire_next would be NULL. Thus, we
257 * need a mechanism to prevent deletions from a given bucket.
259 * To prevent deletions, we bump up the reference count on the
260 * bucket. If the bucket is held, ire_delete just marks both
261 * the ire and irb as CONDEMNED. When the
262 * reference count on the bucket drops to zero, all the CONDEMNED ires
263 * are deleted. We don't have to bump up the reference count on the
264 * bucket if we are walking the bucket and never have to drop the bucket
265 * lock. Note that irb_refhold does not prevent addition of new ires
266 * in the list. It is okay because addition of new ires will not cause
267 * ire_next to point to freed memory. We do irb_refhold only when
268 * all of the 3 conditions are true :
270 * 1) The code needs to walk the IRE bucket from start to end.
271 * 2) It may have to drop the bucket lock sometimes while doing (1)
272 * 3) It does not want any ires to be deleted meanwhile.
276 * Bump up the reference count on the hash bucket - IRB to
277 * prevent ires from being deleted in this bucket.
279 void
280 irb_refhold(irb_t *irb)
282 rw_enter(&irb->irb_lock, RW_WRITER);
283 irb->irb_refcnt++;
284 ASSERT(irb->irb_refcnt != 0);
285 rw_exit(&irb->irb_lock);
288 void
289 irb_refhold_locked(irb_t *irb)
291 ASSERT(RW_WRITE_HELD(&irb->irb_lock));
292 irb->irb_refcnt++;
293 ASSERT(irb->irb_refcnt != 0);
297 * Note: when IRB_MARK_DYNAMIC is not set the irb_t
298 * is statically allocated, so that when the irb_refcnt goes to 0,
299 * we simply clean up the ire list and continue.
301 void
302 irb_refrele(irb_t *irb)
304 if (irb->irb_marks & IRB_MARK_DYNAMIC) {
305 irb_refrele_ftable(irb);
306 } else {
307 rw_enter(&irb->irb_lock, RW_WRITER);
308 ASSERT(irb->irb_refcnt != 0);
309 if (--irb->irb_refcnt == 0 &&
310 (irb->irb_marks & IRB_MARK_CONDEMNED)) {
311 ire_t *ire_list;
313 ire_list = ire_unlink(irb);
314 rw_exit(&irb->irb_lock);
315 ASSERT(ire_list != NULL);
316 ire_cleanup(ire_list);
317 } else {
318 rw_exit(&irb->irb_lock);
325 * Bump up the reference count on the IRE. We cannot assert that the
326 * bucket lock is being held as it is legal to bump up the reference
327 * count after the first lookup has returned the IRE without
328 * holding the lock.
330 void
331 ire_refhold(ire_t *ire)
333 atomic_inc_32(&(ire)->ire_refcnt);
334 ASSERT((ire)->ire_refcnt != 0);
335 #ifdef DEBUG
336 ire_trace_ref(ire);
337 #endif
340 void
341 ire_refhold_notr(ire_t *ire)
343 atomic_inc_32(&(ire)->ire_refcnt);
344 ASSERT((ire)->ire_refcnt != 0);
347 void
348 ire_refhold_locked(ire_t *ire)
350 #ifdef DEBUG
351 ire_trace_ref(ire);
352 #endif
353 ire->ire_refcnt++;
357 * Release a ref on an IRE.
359 * Must not be called while holding any locks. Otherwise if this is
360 * the last reference to be released there is a chance of recursive mutex
361 * panic due to ire_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
362 * to restart an ioctl. The one exception is when the caller is sure that
363 * this is not the last reference to be released. Eg. if the caller is
364 * sure that the ire has not been deleted and won't be deleted.
366 * In architectures e.g sun4u, where atomic_add_32_nv is just
367 * a cas, we need to maintain the right memory barrier semantics
368 * as that of mutex_exit i.e all the loads and stores should complete
369 * before the cas is executed. membar_exit() does that here.
371 void
372 ire_refrele(ire_t *ire)
374 #ifdef DEBUG
375 ire_untrace_ref(ire);
376 #endif
377 ASSERT((ire)->ire_refcnt != 0);
378 membar_exit();
379 if (atomic_dec_32_nv(&(ire)->ire_refcnt) == 0)
380 ire_inactive(ire);
383 void
384 ire_refrele_notr(ire_t *ire)
386 ASSERT((ire)->ire_refcnt != 0);
387 membar_exit();
388 if (atomic_dec_32_nv(&(ire)->ire_refcnt) == 0)
389 ire_inactive(ire);
393 * This function is associated with the IP_IOC_IRE_DELETE[_NO_REPLY]
394 * IOCTL[s]. The NO_REPLY form is used by TCP to tell IP that it is
395 * having problems reaching a particular destination.
396 * This will make IP consider alternate routes (e.g., when there are
397 * muliple default routes), and it will also make IP discard any (potentially)
398 * stale redirect.
399 * Management processes may want to use the version that generates a reply.
401 * With the use of NUD like behavior for IPv4/ARP in addition to IPv6
402 * this function shouldn't be necessary for IP to recover from a bad redirect,
403 * a bad default router (when there are multiple default routers), or
404 * a stale ND/ARP entry. But we retain it in any case.
405 * For instance, this is helpful when TCP suspects a failure before NUD does.
408 ip_ire_delete(queue_t *q, mblk_t *mp, cred_t *ioc_cr)
410 uchar_t *addr_ucp;
411 uint_t ipversion;
412 sin_t *sin;
413 sin6_t *sin6;
414 ipaddr_t v4addr;
415 in6_addr_t v6addr;
416 ire_t *ire;
417 ipid_t *ipid;
418 zoneid_t zoneid;
419 ip_stack_t *ipst;
421 ASSERT(q->q_next == NULL);
422 zoneid = IPCL_ZONEID(Q_TO_CONN(q));
423 ipst = CONNQ_TO_IPST(q);
426 * Check privilege using the ioctl credential; if it is NULL
427 * then this is a kernel message and therefor privileged.
429 if (ioc_cr != NULL && secpolicy_ip_config(ioc_cr, B_FALSE) != 0)
430 return (EPERM);
432 ipid = (ipid_t *)mp->b_rptr;
434 addr_ucp = mi_offset_param(mp, ipid->ipid_addr_offset,
435 ipid->ipid_addr_length);
436 if (addr_ucp == NULL || !OK_32PTR(addr_ucp))
437 return (EINVAL);
438 switch (ipid->ipid_addr_length) {
439 case sizeof (sin_t):
441 * got complete (sockaddr) address - increment addr_ucp to point
442 * at the ip_addr field.
444 sin = (sin_t *)addr_ucp;
445 addr_ucp = (uchar_t *)&sin->sin_addr.s_addr;
446 ipversion = IPV4_VERSION;
447 break;
448 case sizeof (sin6_t):
450 * got complete (sockaddr) address - increment addr_ucp to point
451 * at the ip_addr field.
453 sin6 = (sin6_t *)addr_ucp;
454 addr_ucp = (uchar_t *)&sin6->sin6_addr;
455 ipversion = IPV6_VERSION;
456 break;
457 default:
458 return (EINVAL);
460 if (ipversion == IPV4_VERSION) {
461 /* Extract the destination address. */
462 bcopy(addr_ucp, &v4addr, IP_ADDR_LEN);
464 ire = ire_ftable_lookup_v4(v4addr, 0, 0, 0, NULL,
465 zoneid, MATCH_IRE_DSTONLY, 0, ipst, NULL);
466 } else {
467 /* Extract the destination address. */
468 bcopy(addr_ucp, &v6addr, IPV6_ADDR_LEN);
470 ire = ire_ftable_lookup_v6(&v6addr, NULL, NULL, 0, NULL,
471 zoneid, MATCH_IRE_DSTONLY, 0, ipst, NULL);
473 if (ire != NULL) {
474 if (ipversion == IPV4_VERSION) {
475 ip_rts_change(RTM_LOSING, ire->ire_addr,
476 ire->ire_gateway_addr, ire->ire_mask,
477 (Q_TO_CONN(q))->conn_laddr_v4, 0, 0, 0,
478 (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_IFA),
479 ire->ire_ipst);
481 (void) ire_no_good(ire);
482 ire_refrele(ire);
484 return (0);
488 * Initialize the ire that is specific to IPv4 part and call
489 * ire_init_common to finish it.
490 * Returns zero or errno.
493 ire_init_v4(ire_t *ire, uchar_t *addr, uchar_t *mask, uchar_t *gateway,
494 ushort_t type, ill_t *ill, zoneid_t zoneid, uint_t flags,
495 ip_stack_t *ipst)
497 int error;
499 BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_alloced);
501 if (addr != NULL)
502 bcopy(addr, &ire->ire_addr, IP_ADDR_LEN);
503 if (gateway != NULL)
504 bcopy(gateway, &ire->ire_gateway_addr, IP_ADDR_LEN);
506 /* Make sure we don't have stray values in some fields */
507 switch (type) {
508 case IRE_LOOPBACK:
509 case IRE_HOST:
510 case IRE_BROADCAST:
511 case IRE_LOCAL:
512 case IRE_IF_CLONE:
513 ire->ire_mask = IP_HOST_MASK;
514 ire->ire_masklen = IPV4_ABITS;
515 break;
516 case IRE_PREFIX:
517 case IRE_DEFAULT:
518 case IRE_IF_RESOLVER:
519 case IRE_IF_NORESOLVER:
520 if (mask != NULL) {
521 bcopy(mask, &ire->ire_mask, IP_ADDR_LEN);
522 ire->ire_masklen = ip_mask_to_plen(ire->ire_mask);
524 break;
525 case IRE_MULTICAST:
526 case IRE_NOROUTE:
527 ASSERT(mask == NULL);
528 break;
529 default:
530 ASSERT(0);
531 return (EINVAL);
534 error = ire_init_common(ire, type, ill, zoneid, flags, IPV4_VERSION,
535 ipst);
536 if (error != 0)
537 return (error);
539 /* Determine which function pointers to use */
540 ire->ire_postfragfn = ip_xmit; /* Common case */
542 switch (ire->ire_type) {
543 case IRE_LOCAL:
544 ire->ire_sendfn = ire_send_local_v4;
545 ire->ire_recvfn = ire_recv_local_v4;
546 ASSERT(ire->ire_ill != NULL);
547 if (ire->ire_ill->ill_flags & ILLF_NOACCEPT)
548 ire->ire_recvfn = ire_recv_noaccept_v6;
549 break;
550 case IRE_LOOPBACK:
551 ire->ire_sendfn = ire_send_local_v4;
552 ire->ire_recvfn = ire_recv_loopback_v4;
553 break;
554 case IRE_BROADCAST:
555 ire->ire_postfragfn = ip_postfrag_loopcheck;
556 ire->ire_sendfn = ire_send_broadcast_v4;
557 ire->ire_recvfn = ire_recv_broadcast_v4;
558 break;
559 case IRE_MULTICAST:
560 ire->ire_postfragfn = ip_postfrag_loopcheck;
561 ire->ire_sendfn = ire_send_multicast_v4;
562 ire->ire_recvfn = ire_recv_multicast_v4;
563 break;
564 default:
566 * For IRE_IF_ALL and IRE_OFFLINK we forward received
567 * packets by default.
569 ire->ire_sendfn = ire_send_wire_v4;
570 ire->ire_recvfn = ire_recv_forward_v4;
571 break;
573 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
574 ire->ire_sendfn = ire_send_noroute_v4;
575 ire->ire_recvfn = ire_recv_noroute_v4;
577 ire->ire_nce_capable = ire_determine_nce_capable(ire);
578 return (0);
582 * Determine ire_nce_capable
584 boolean_t
585 ire_determine_nce_capable(ire_t *ire)
587 int max_masklen;
589 if ((ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) ||
590 (ire->ire_type & IRE_MULTICAST))
591 return (B_TRUE);
593 if (ire->ire_ipversion == IPV4_VERSION)
594 max_masklen = IPV4_ABITS;
595 else
596 max_masklen = IPV6_ABITS;
598 if ((ire->ire_type & IRE_ONLINK) && ire->ire_masklen == max_masklen)
599 return (B_TRUE);
600 return (B_FALSE);
604 * ire_create is called to allocate and initialize a new IRE.
606 * NOTE : This is called as writer sometimes though not required
607 * by this function.
609 ire_t *
610 ire_create(uchar_t *addr, uchar_t *mask, uchar_t *gateway,
611 ushort_t type, ill_t *ill, zoneid_t zoneid, uint_t flags,
612 ip_stack_t *ipst)
614 ire_t *ire;
615 int error;
617 ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
618 if (ire == NULL) {
619 DTRACE_PROBE(kmem__cache__alloc);
620 return (NULL);
622 *ire = ire_null;
624 error = ire_init_v4(ire, addr, mask, gateway, type, ill, zoneid, flags,
625 ipst);
626 if (error != 0) {
627 DTRACE_PROBE2(ire__init, ire_t *, ire, int, error);
628 kmem_cache_free(ire_cache, ire);
629 return (NULL);
631 return (ire);
635 * Common to IPv4 and IPv6
636 * Returns zero or errno.
639 ire_init_common(ire_t *ire, ushort_t type, ill_t *ill, zoneid_t zoneid,
640 uint_t flags, uchar_t ipversion, ip_stack_t *ipst)
642 int error;
644 #ifdef DEBUG
645 if (ill != NULL) {
646 if (ill->ill_isv6)
647 ASSERT(ipversion == IPV6_VERSION);
648 else
649 ASSERT(ipversion == IPV4_VERSION);
651 #endif /* DEBUG */
653 ire->ire_type = type;
654 ire->ire_flags = RTF_UP | flags;
655 ire->ire_create_time = (uint32_t)gethrestime_sec();
656 ire->ire_generation = IRE_GENERATION_INITIAL;
659 * The ill_ire_cnt isn't increased until
660 * the IRE is added to ensure that a walker will find
661 * all IREs that hold a reference on an ill.
663 * Note that ill_ire_multicast doesn't hold a ref on the ill since
664 * ire_add() is not called for the IRE_MULTICAST.
666 ire->ire_ill = ill;
667 ire->ire_zoneid = zoneid;
668 ire->ire_ipversion = ipversion;
670 mutex_init(&ire->ire_lock, NULL, MUTEX_DEFAULT, NULL);
671 ire->ire_refcnt = 1;
672 ire->ire_identical_ref = 1; /* Number of ire_delete's needed */
673 ire->ire_ipst = ipst; /* No netstack_hold */
674 ire->ire_trace_disable = B_FALSE;
676 return (0);
680 * This creates an IRE_BROADCAST based on the arguments.
681 * A mirror is ire_lookup_bcast().
683 * Any supression of unneeded ones is done in ire_add_v4.
684 * We add one IRE_BROADCAST per address. ire_send_broadcast_v4()
685 * takes care of generating a loopback copy of the packet.
687 ire_t **
688 ire_create_bcast(ill_t *ill, ipaddr_t addr, zoneid_t zoneid, ire_t **irep)
690 ip_stack_t *ipst = ill->ill_ipst;
692 ASSERT(IAM_WRITER_ILL(ill));
694 *irep++ = ire_create(
695 (uchar_t *)&addr, /* dest addr */
696 (uchar_t *)&ip_g_all_ones, /* mask */
697 NULL, /* no gateway */
698 IRE_BROADCAST,
699 ill,
700 zoneid,
701 RTF_KERNEL,
702 ipst);
704 return (irep);
708 * This looks up an IRE_BROADCAST based on the arguments.
709 * Mirrors ire_create_bcast().
711 ire_t *
712 ire_lookup_bcast(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
714 ire_t *ire;
715 int match_args;
717 match_args = MATCH_IRE_TYPE | MATCH_IRE_ILL | MATCH_IRE_GW |
718 MATCH_IRE_MASK | MATCH_IRE_ZONEONLY;
720 if (IS_UNDER_IPMP(ill))
721 match_args |= MATCH_IRE_TESTHIDDEN;
723 ire = ire_ftable_lookup_v4(
724 addr, /* dest addr */
725 ip_g_all_ones, /* mask */
726 0, /* no gateway */
727 IRE_BROADCAST,
728 ill,
729 zoneid,
730 match_args,
732 ill->ill_ipst,
733 NULL);
734 return (ire);
737 /* Arrange to call the specified function for every IRE in the world. */
738 void
739 ire_walk(pfv_t func, void *arg, ip_stack_t *ipst)
741 ire_walk_ipvers(func, arg, 0, ALL_ZONES, ipst);
744 void
745 ire_walk_v4(pfv_t func, void *arg, zoneid_t zoneid, ip_stack_t *ipst)
747 ire_walk_ipvers(func, arg, IPV4_VERSION, zoneid, ipst);
750 void
751 ire_walk_v6(pfv_t func, void *arg, zoneid_t zoneid, ip_stack_t *ipst)
753 ire_walk_ipvers(func, arg, IPV6_VERSION, zoneid, ipst);
757 * Walk a particular version. version == 0 means both v4 and v6.
759 static void
760 ire_walk_ipvers(pfv_t func, void *arg, uchar_t vers, zoneid_t zoneid,
761 ip_stack_t *ipst)
763 if (vers != IPV6_VERSION) {
765 * ip_forwarding_table variable doesn't matter for IPv4 since
766 * ire_walk_ill_tables uses ips_ip_ftable for IPv4.
768 ire_walk_ill_tables(0, 0, func, arg, IP_MASK_TABLE_SIZE,
769 0, NULL,
770 NULL, zoneid, ipst);
772 if (vers != IPV4_VERSION) {
773 ire_walk_ill_tables(0, 0, func, arg, IP6_MASK_TABLE_SIZE,
774 ipst->ips_ip6_ftable_hash_size,
775 ipst->ips_ip_forwarding_table_v6,
776 NULL, zoneid, ipst);
781 * Arrange to call the specified function for every IRE that matches the ill.
783 void
784 ire_walk_ill(uint_t match_flags, uint_t ire_type, pfv_t func, void *arg,
785 ill_t *ill)
787 uchar_t vers = (ill->ill_isv6 ? IPV6_VERSION : IPV4_VERSION);
789 ire_walk_ill_ipvers(match_flags, ire_type, func, arg, vers, ill);
793 * Walk a particular ill and version.
795 static void
796 ire_walk_ill_ipvers(uint_t match_flags, uint_t ire_type, pfv_t func,
797 void *arg, uchar_t vers, ill_t *ill)
799 ip_stack_t *ipst = ill->ill_ipst;
801 if (vers == IPV4_VERSION) {
802 ire_walk_ill_tables(match_flags, ire_type, func, arg,
803 IP_MASK_TABLE_SIZE,
804 0, NULL,
805 ill, ALL_ZONES, ipst);
807 if (vers != IPV4_VERSION) {
808 ire_walk_ill_tables(match_flags, ire_type, func, arg,
809 IP6_MASK_TABLE_SIZE, ipst->ips_ip6_ftable_hash_size,
810 ipst->ips_ip_forwarding_table_v6,
811 ill, ALL_ZONES, ipst);
816 * Do the specific matching of IREs to shared-IP zones.
818 * We have the same logic as in ire_match_args but implemented slightly
819 * differently.
821 boolean_t
822 ire_walk_ill_match(uint_t match_flags, uint_t ire_type, ire_t *ire,
823 ill_t *ill, zoneid_t zoneid, ip_stack_t *ipst)
825 ill_t *dst_ill = ire->ire_ill;
827 ASSERT(match_flags != 0 || zoneid != ALL_ZONES);
829 if (zoneid != ALL_ZONES && zoneid != ire->ire_zoneid &&
830 ire->ire_zoneid != ALL_ZONES) {
832 * We're walking the IREs for a specific zone. The only relevant
833 * IREs are:
834 * - all IREs with a matching ire_zoneid
835 * - IRE_IF_ALL IREs for interfaces with a usable source addr
836 * with a matching zone
837 * - IRE_OFFLINK with a gateway reachable from the zone
838 * Note that ealier we only did the IRE_OFFLINK check for
839 * IRE_DEFAULT (and only when we had multiple IRE_DEFAULTs).
841 if (ire->ire_type & IRE_ONLINK) {
842 uint_t ifindex;
845 * Note there is no IRE_INTERFACE on vniN thus
846 * can't do an IRE lookup for a matching route.
848 ifindex = dst_ill->ill_usesrc_ifindex;
849 if (ifindex == 0)
850 return (B_FALSE);
853 * If there is a usable source address in the
854 * zone, then it's ok to return an
855 * IRE_INTERFACE
857 if (!ipif_zone_avail(ifindex, dst_ill->ill_isv6,
858 zoneid, ipst)) {
859 return (B_FALSE);
862 if (dst_ill != NULL && (ire->ire_type & IRE_OFFLINK)) {
863 ipif_t *tipif;
865 mutex_enter(&dst_ill->ill_lock);
866 for (tipif = dst_ill->ill_ipif;
867 tipif != NULL; tipif = tipif->ipif_next) {
868 if (!IPIF_IS_CONDEMNED(tipif) &&
869 (tipif->ipif_flags & IPIF_UP) &&
870 (tipif->ipif_zoneid == zoneid ||
871 tipif->ipif_zoneid == ALL_ZONES))
872 break;
874 mutex_exit(&dst_ill->ill_lock);
875 if (tipif == NULL) {
876 return (B_FALSE);
881 * Except for ALL_ZONES, we only match the offlink routes
882 * where ire_gateway_addr has an IRE_INTERFACE for the zoneid.
883 * Since we can have leftover routes after the IP addresses have
884 * changed, the global zone will also match offlink routes where the
885 * gateway is unreachable from any zone.
887 if ((ire->ire_type & IRE_OFFLINK) && zoneid != ALL_ZONES) {
888 in6_addr_t gw_addr_v6;
889 boolean_t reach;
891 if (ire->ire_ipversion == IPV4_VERSION) {
892 reach = ire_gateway_ok_zone_v4(ire->ire_gateway_addr,
893 zoneid, dst_ill, ipst, B_FALSE);
894 } else {
895 ASSERT(ire->ire_ipversion == IPV6_VERSION);
896 mutex_enter(&ire->ire_lock);
897 gw_addr_v6 = ire->ire_gateway_addr_v6;
898 mutex_exit(&ire->ire_lock);
900 reach = ire_gateway_ok_zone_v6(&gw_addr_v6, zoneid,
901 dst_ill, ipst, B_FALSE);
903 if (!reach) {
904 if (zoneid != GLOBAL_ZONEID)
905 return (B_FALSE);
908 * Check if ALL_ZONES reachable - if not then let the
909 * global zone see it.
911 if (ire->ire_ipversion == IPV4_VERSION) {
912 reach = ire_gateway_ok_zone_v4(
913 ire->ire_gateway_addr, ALL_ZONES,
914 dst_ill, ipst, B_FALSE);
915 } else {
916 reach = ire_gateway_ok_zone_v6(&gw_addr_v6,
917 ALL_ZONES, dst_ill, ipst, B_FALSE);
919 if (reach) {
921 * Some other zone could see it, hence hide it
922 * in the global zone.
924 return (B_FALSE);
929 if (((!(match_flags & MATCH_IRE_TYPE)) ||
930 (ire->ire_type & ire_type)) &&
931 ((!(match_flags & MATCH_IRE_ILL)) ||
932 (dst_ill == ill ||
933 dst_ill != NULL && IS_IN_SAME_ILLGRP(dst_ill, ill)))) {
934 return (B_TRUE);
936 return (B_FALSE);
940 rtfunc(struct radix_node *rn, void *arg)
942 struct rtfuncarg *rtf = arg;
943 struct rt_entry *rt;
944 irb_t *irb;
945 ire_t *ire;
946 boolean_t ret;
948 rt = (struct rt_entry *)rn;
949 ASSERT(rt != NULL);
950 irb = &rt->rt_irb;
951 for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
952 if ((rtf->rt_match_flags != 0) ||
953 (rtf->rt_zoneid != ALL_ZONES)) {
954 ret = ire_walk_ill_match(rtf->rt_match_flags,
955 rtf->rt_ire_type, ire,
956 rtf->rt_ill, rtf->rt_zoneid, rtf->rt_ipst);
957 } else {
958 ret = B_TRUE;
960 if (ret)
961 (*rtf->rt_func)(ire, rtf->rt_arg);
963 return (0);
967 * Walk the ftable entries that match the ill.
969 void
970 ire_walk_ill_tables(uint_t match_flags, uint_t ire_type, pfv_t func,
971 void *arg, size_t ftbl_sz, size_t htbl_sz, irb_t **ipftbl,
972 ill_t *ill, zoneid_t zoneid,
973 ip_stack_t *ipst)
975 irb_t *irb_ptr;
976 irb_t *irb;
977 ire_t *ire;
978 int i, j;
979 boolean_t ret;
980 struct rtfuncarg rtfarg;
982 ASSERT((!(match_flags & MATCH_IRE_ILL)) || (ill != NULL));
983 ASSERT(!(match_flags & MATCH_IRE_TYPE) || (ire_type != 0));
985 /* knobs such that routine is called only for v6 case */
986 if (ipftbl == ipst->ips_ip_forwarding_table_v6) {
987 for (i = (ftbl_sz - 1); i >= 0; i--) {
988 if ((irb_ptr = ipftbl[i]) == NULL)
989 continue;
990 for (j = 0; j < htbl_sz; j++) {
991 irb = &irb_ptr[j];
992 if (irb->irb_ire == NULL)
993 continue;
995 irb_refhold(irb);
996 for (ire = irb->irb_ire; ire != NULL;
997 ire = ire->ire_next) {
998 if (match_flags == 0 &&
999 zoneid == ALL_ZONES) {
1000 ret = B_TRUE;
1001 } else {
1002 ret =
1003 ire_walk_ill_match(
1004 match_flags,
1005 ire_type, ire, ill,
1006 zoneid, ipst);
1008 if (ret)
1009 (*func)(ire, arg);
1011 irb_refrele(irb);
1014 } else {
1015 bzero(&rtfarg, sizeof (rtfarg));
1016 rtfarg.rt_func = func;
1017 rtfarg.rt_arg = arg;
1018 if (match_flags != 0) {
1019 rtfarg.rt_match_flags = match_flags;
1021 rtfarg.rt_ire_type = ire_type;
1022 rtfarg.rt_ill = ill;
1023 rtfarg.rt_zoneid = zoneid;
1024 rtfarg.rt_ipst = ipst; /* No netstack_hold */
1025 (void) ipst->ips_ip_ftable->rnh_walktree_mt(
1026 ipst->ips_ip_ftable,
1027 rtfunc, &rtfarg, irb_refhold_rn, irb_refrele_rn);
1032 * This function takes a mask and returns
1033 * number of bits set in the mask. If no
1034 * bit is set it returns 0.
1035 * Assumes a contiguous mask.
1038 ip_mask_to_plen(ipaddr_t mask)
1040 return (mask == 0 ? 0 : IP_ABITS - (ffs(ntohl(mask)) -1));
1044 * Convert length for a mask to the mask.
1046 ipaddr_t
1047 ip_plen_to_mask(uint_t masklen)
1049 if (masklen == 0)
1050 return (0);
1052 return (htonl(IP_HOST_MASK << (IP_ABITS - masklen)));
1055 void
1056 ire_atomic_end(irb_t *irb_ptr, ire_t *ire)
1058 ill_t *ill;
1060 ill = ire->ire_ill;
1061 if (ill != NULL)
1062 mutex_exit(&ill->ill_lock);
1063 rw_exit(&irb_ptr->irb_lock);
1067 * ire_add_v[46] atomically make sure that the ill associated
1068 * with the new ire is not going away i.e., we check ILL_CONDEMNED.
1071 ire_atomic_start(irb_t *irb_ptr, ire_t *ire)
1073 ill_t *ill;
1075 ill = ire->ire_ill;
1077 rw_enter(&irb_ptr->irb_lock, RW_WRITER);
1078 if (ill != NULL) {
1079 mutex_enter(&ill->ill_lock);
1082 * Don't allow IRE's to be created on dying ills, or on
1083 * ill's for which the last ipif is going down, or ones which
1084 * don't have even a single UP interface
1086 if ((ill->ill_state_flags &
1087 (ILL_CONDEMNED|ILL_DOWN_IN_PROGRESS)) != 0) {
1088 ire_atomic_end(irb_ptr, ire);
1089 DTRACE_PROBE1(ire__add__on__dying__ill, ire_t *, ire);
1090 return (ENXIO);
1093 if (IS_UNDER_IPMP(ill)) {
1094 int error = 0;
1095 mutex_enter(&ill->ill_phyint->phyint_lock);
1096 if (!ipmp_ill_is_active(ill) &&
1097 IRE_HIDDEN_TYPE(ire->ire_type) &&
1098 !ire->ire_testhidden) {
1099 error = EINVAL;
1101 mutex_exit(&ill->ill_phyint->phyint_lock);
1102 if (error != 0) {
1103 ire_atomic_end(irb_ptr, ire);
1104 return (error);
1109 return (0);
1113 * Add a fully initialized IRE to the forwarding table.
1114 * This returns NULL on failure, or a held IRE on success.
1115 * Normally the returned IRE is the same as the argument. But a different
1116 * IRE will be returned if the added IRE is deemed identical to an existing
1117 * one. In that case ire_identical_ref will be increased.
1118 * The caller always needs to do an ire_refrele() on the returned IRE.
1120 ire_t *
1121 ire_add(ire_t *ire)
1123 if (IRE_HIDDEN_TYPE(ire->ire_type) &&
1124 ire->ire_ill != NULL && IS_UNDER_IPMP(ire->ire_ill)) {
1126 * IREs hosted on interfaces that are under IPMP
1127 * should be hidden so that applications don't
1128 * accidentally end up sending packets with test
1129 * addresses as their source addresses, or
1130 * sending out interfaces that are e.g. IFF_INACTIVE.
1131 * Hide them here.
1133 ire->ire_testhidden = B_TRUE;
1136 if (ire->ire_ipversion == IPV6_VERSION)
1137 return (ire_add_v6(ire));
1138 else
1139 return (ire_add_v4(ire));
1143 * Add a fully initialized IPv4 IRE to the forwarding table.
1144 * This returns NULL on failure, or a held IRE on success.
1145 * Normally the returned IRE is the same as the argument. But a different
1146 * IRE will be returned if the added IRE is deemed identical to an existing
1147 * one. In that case ire_identical_ref will be increased.
1148 * The caller always needs to do an ire_refrele() on the returned IRE.
1150 static ire_t *
1151 ire_add_v4(ire_t *ire)
1153 ire_t *ire1;
1154 irb_t *irb_ptr;
1155 ire_t **irep;
1156 int match_flags;
1157 int error;
1158 ip_stack_t *ipst = ire->ire_ipst;
1160 if (ire->ire_ill != NULL)
1161 ASSERT(!MUTEX_HELD(&ire->ire_ill->ill_lock));
1162 ASSERT(ire->ire_ipversion == IPV4_VERSION);
1164 /* Make sure the address is properly masked. */
1165 ire->ire_addr &= ire->ire_mask;
1167 match_flags = (MATCH_IRE_MASK | MATCH_IRE_TYPE | MATCH_IRE_GW);
1169 if (ire->ire_ill != NULL) {
1170 match_flags |= MATCH_IRE_ILL;
1172 irb_ptr = ire_get_bucket(ire);
1173 if (irb_ptr == NULL) {
1174 printf("no bucket for %p\n", (void *)ire);
1175 ire_delete(ire);
1176 return (NULL);
1180 * Start the atomic add of the ire. Grab the ill lock,
1181 * the bucket lock. Check for condemned.
1183 error = ire_atomic_start(irb_ptr, ire);
1184 if (error != 0) {
1185 printf("no ire_atomic_start for %p\n", (void *)ire);
1186 ire_delete(ire);
1187 irb_refrele(irb_ptr);
1188 return (NULL);
1191 * If we are creating a hidden IRE, make sure we search for
1192 * hidden IREs when searching for duplicates below.
1193 * Otherwise, we might find an IRE on some other interface
1194 * that's not marked hidden.
1196 if (ire->ire_testhidden)
1197 match_flags |= MATCH_IRE_TESTHIDDEN;
1200 * Atomically check for duplicate and insert in the table.
1202 for (ire1 = irb_ptr->irb_ire; ire1 != NULL; ire1 = ire1->ire_next) {
1203 if (IRE_IS_CONDEMNED(ire1))
1204 continue;
1206 * Here we need an exact match on zoneid, i.e.,
1207 * ire_match_args doesn't fit.
1209 if (ire1->ire_zoneid != ire->ire_zoneid)
1210 continue;
1212 if (ire1->ire_type != ire->ire_type)
1213 continue;
1216 * Note: We do not allow multiple routes that differ only
1217 * in the gateway security attributes; such routes are
1218 * considered duplicates.
1219 * To change that we explicitly have to treat them as
1220 * different here.
1222 if (ire_match_args(ire1, ire->ire_addr, ire->ire_mask,
1223 ire->ire_gateway_addr, ire->ire_type, ire->ire_ill,
1224 ire->ire_zoneid, match_flags)) {
1226 * Return the old ire after doing a REFHOLD.
1227 * As most of the callers continue to use the IRE
1228 * after adding, we return a held ire. This will
1229 * avoid a lookup in the caller again. If the callers
1230 * don't want to use it, they need to do a REFRELE.
1232 * We only allow exactly one IRE_IF_CLONE for any dst,
1233 * so, if the is an IF_CLONE, return the ire without
1234 * an identical_ref, but with an ire_ref held.
1236 if (ire->ire_type != IRE_IF_CLONE) {
1237 atomic_inc_32(&ire1->ire_identical_ref);
1238 DTRACE_PROBE2(ire__add__exist, ire_t *, ire1,
1239 ire_t *, ire);
1241 ire_refhold(ire1);
1242 ire_atomic_end(irb_ptr, ire);
1243 ire_delete(ire);
1244 irb_refrele(irb_ptr);
1245 return (ire1);
1250 * Normally we do head insertion since most things do not care about
1251 * the order of the IREs in the bucket. However, due to shared-IP
1252 * zones (and restrict_interzone_loopback) we can have an IRE_LOCAL
1253 * as well as IRE_IF_CLONE for the same address. For that reason we
1254 * do tail insertion for IRE_IF_CLONE. The IRE_BROADCAST case is a
1255 * bit of a mystery.
1257 irep = (ire_t **)irb_ptr;
1258 if ((ire->ire_type & IRE_IF_CLONE) || (ire->ire_type & IRE_BROADCAST)) {
1259 while ((ire1 = *irep) != NULL)
1260 irep = &ire1->ire_next;
1262 /* Insert at *irep */
1263 ire1 = *irep;
1264 if (ire1 != NULL)
1265 ire1->ire_ptpn = &ire->ire_next;
1266 ire->ire_next = ire1;
1267 /* Link the new one in. */
1268 ire->ire_ptpn = irep;
1271 * ire_walk routines de-reference ire_next without holding
1272 * a lock. Before we point to the new ire, we want to make
1273 * sure the store that sets the ire_next of the new ire
1274 * reaches global visibility, so that ire_walk routines
1275 * don't see a truncated list of ires i.e if the ire_next
1276 * of the new ire gets set after we do "*irep = ire" due
1277 * to re-ordering, the ire_walk thread will see a NULL
1278 * once it accesses the ire_next of the new ire.
1279 * membar_producer() makes sure that the following store
1280 * happens *after* all of the above stores.
1282 membar_producer();
1283 *irep = ire;
1284 ire->ire_bucket = irb_ptr;
1286 * We return a bumped up IRE above. Keep it symmetrical
1287 * so that the callers will always have to release. This
1288 * helps the callers of this function because they continue
1289 * to use the IRE after adding and hence they don't have to
1290 * lookup again after we return the IRE.
1292 * NOTE : We don't have to use atomics as this is appearing
1293 * in the list for the first time and no one else can bump
1294 * up the reference count on this yet.
1296 ire_refhold_locked(ire);
1297 BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted);
1299 irb_ptr->irb_ire_cnt++;
1300 if (irb_ptr->irb_marks & IRB_MARK_DYNAMIC)
1301 irb_ptr->irb_nire++;
1303 if (ire->ire_ill != NULL) {
1304 ire->ire_ill->ill_ire_cnt++;
1305 ASSERT(ire->ire_ill->ill_ire_cnt != 0); /* Wraparound */
1308 ire_atomic_end(irb_ptr, ire);
1310 /* Make any caching of the IREs be notified or updated */
1311 ire_flush_cache_v4(ire, IRE_FLUSH_ADD);
1313 if (ire->ire_ill != NULL)
1314 ASSERT(!MUTEX_HELD(&ire->ire_ill->ill_lock));
1315 irb_refrele(irb_ptr);
1316 return (ire);
1320 * irb_refrele is the only caller of the function. ire_unlink calls to
1321 * do the final cleanup for this ire.
1323 void
1324 ire_cleanup(ire_t *ire)
1326 ire_t *ire_next;
1327 ip_stack_t *ipst = ire->ire_ipst;
1329 ASSERT(ire != NULL);
1331 while (ire != NULL) {
1332 ire_next = ire->ire_next;
1333 if (ire->ire_ipversion == IPV4_VERSION) {
1334 ire_delete_v4(ire);
1335 BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
1336 ire_stats_deleted);
1337 } else {
1338 ASSERT(ire->ire_ipversion == IPV6_VERSION);
1339 ire_delete_v6(ire);
1340 BUMP_IRE_STATS(ipst->ips_ire_stats_v6,
1341 ire_stats_deleted);
1344 * Now it's really out of the list. Before doing the
1345 * REFRELE, set ire_next to NULL as ire_inactive asserts
1346 * so.
1348 ire->ire_next = NULL;
1349 ire_refrele_notr(ire);
1350 ire = ire_next;
1355 * irb_refrele is the only caller of the function. It calls to unlink
1356 * all the CONDEMNED ires from this bucket.
1358 ire_t *
1359 ire_unlink(irb_t *irb)
1361 ire_t *ire;
1362 ire_t *ire1;
1363 ire_t **ptpn;
1364 ire_t *ire_list = NULL;
1366 ASSERT(RW_WRITE_HELD(&irb->irb_lock));
1367 ASSERT(((irb->irb_marks & IRB_MARK_DYNAMIC) && irb->irb_refcnt == 1) ||
1368 (irb->irb_refcnt == 0));
1369 ASSERT(irb->irb_marks & IRB_MARK_CONDEMNED);
1370 ASSERT(irb->irb_ire != NULL);
1372 for (ire = irb->irb_ire; ire != NULL; ire = ire1) {
1373 ire1 = ire->ire_next;
1374 if (IRE_IS_CONDEMNED(ire)) {
1375 ptpn = ire->ire_ptpn;
1376 ire1 = ire->ire_next;
1377 if (ire1)
1378 ire1->ire_ptpn = ptpn;
1379 *ptpn = ire1;
1380 ire->ire_ptpn = NULL;
1381 ire->ire_next = NULL;
1384 * We need to call ire_delete_v4 or ire_delete_v6 to
1385 * clean up dependents and the redirects pointing at
1386 * the default gateway. We need to drop the lock
1387 * as ire_flush_cache/ire_delete_host_redircts require
1388 * so. But we can't drop the lock, as ire_unlink needs
1389 * to atomically remove the ires from the list.
1390 * So, create a temporary list of CONDEMNED ires
1391 * for doing ire_delete_v4/ire_delete_v6 operations
1392 * later on.
1394 ire->ire_next = ire_list;
1395 ire_list = ire;
1398 irb->irb_marks &= ~IRB_MARK_CONDEMNED;
1399 return (ire_list);
1403 * Clean up the radix node for this ire. Must be called by irb_refrele
1404 * when there are no ire's left in the bucket. Returns TRUE if the bucket
1405 * is deleted and freed.
1407 boolean_t
1408 irb_inactive(irb_t *irb)
1410 struct rt_entry *rt;
1411 struct radix_node *rn;
1412 ip_stack_t *ipst = irb->irb_ipst;
1414 ASSERT(irb->irb_ipst != NULL);
1416 rt = IRB2RT(irb);
1417 rn = (struct radix_node *)rt;
1419 /* first remove it from the radix tree. */
1420 RADIX_NODE_HEAD_WLOCK(ipst->ips_ip_ftable);
1421 rw_enter(&irb->irb_lock, RW_WRITER);
1422 if (irb->irb_refcnt == 1 && irb->irb_nire == 0) {
1423 rn = ipst->ips_ip_ftable->rnh_deladdr(rn->rn_key, rn->rn_mask,
1424 ipst->ips_ip_ftable);
1425 DTRACE_PROBE1(irb__free, rt_t *, rt);
1426 ASSERT((void *)rn == (void *)rt);
1427 Free(rt, rt_entry_cache);
1428 /* irb_lock is freed */
1429 RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
1430 return (B_TRUE);
1432 rw_exit(&irb->irb_lock);
1433 RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
1434 return (B_FALSE);
1438 * Delete the specified IRE.
1439 * We assume that if ire_bucket is not set then ire_ill->ill_ire_cnt was
1440 * not incremented i.e., that the insertion in the bucket and the increment
1441 * of that counter is done atomically.
1443 void
1444 ire_delete(ire_t *ire)
1446 ire_t *ire1;
1447 ire_t **ptpn;
1448 irb_t *irb;
1449 ip_stack_t *ipst = ire->ire_ipst;
1451 if ((irb = ire->ire_bucket) == NULL) {
1453 * It was never inserted in the list. Should call REFRELE
1454 * to free this IRE.
1456 ire_make_condemned(ire);
1457 ire_refrele_notr(ire);
1458 return;
1462 * Move the use counts from an IRE_IF_CLONE to its parent
1463 * IRE_INTERFACE.
1464 * We need to do this before acquiring irb_lock.
1466 if (ire->ire_type & IRE_IF_CLONE) {
1467 ire_t *parent;
1469 rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
1470 if ((parent = ire->ire_dep_parent) != NULL) {
1471 parent->ire_ob_pkt_count += ire->ire_ob_pkt_count;
1472 parent->ire_ib_pkt_count += ire->ire_ib_pkt_count;
1473 ire->ire_ob_pkt_count = 0;
1474 ire->ire_ib_pkt_count = 0;
1476 rw_exit(&ipst->ips_ire_dep_lock);
1479 rw_enter(&irb->irb_lock, RW_WRITER);
1480 if (ire->ire_ptpn == NULL) {
1482 * Some other thread has removed us from the list.
1483 * It should have done the REFRELE for us.
1485 rw_exit(&irb->irb_lock);
1486 return;
1489 if (!IRE_IS_CONDEMNED(ire)) {
1490 /* Is this an IRE representing multiple duplicate entries? */
1491 ASSERT(ire->ire_identical_ref >= 1);
1492 if (atomic_dec_32_nv(&ire->ire_identical_ref) != 0) {
1493 /* Removed one of the identical parties */
1494 rw_exit(&irb->irb_lock);
1495 return;
1498 irb->irb_ire_cnt--;
1499 ire_make_condemned(ire);
1502 if (irb->irb_refcnt != 0) {
1504 * The last thread to leave this bucket will
1505 * delete this ire.
1507 irb->irb_marks |= IRB_MARK_CONDEMNED;
1508 rw_exit(&irb->irb_lock);
1509 return;
1513 * Normally to delete an ire, we walk the bucket. While we
1514 * walk the bucket, we normally bump up irb_refcnt and hence
1515 * we return from above where we mark CONDEMNED and the ire
1516 * gets deleted from ire_unlink. This case is where somebody
1517 * knows the ire e.g by doing a lookup, and wants to delete the
1518 * IRE. irb_refcnt would be 0 in this case if nobody is walking
1519 * the bucket.
1521 ptpn = ire->ire_ptpn;
1522 ire1 = ire->ire_next;
1523 if (ire1 != NULL)
1524 ire1->ire_ptpn = ptpn;
1525 ASSERT(ptpn != NULL);
1526 *ptpn = ire1;
1527 ire->ire_ptpn = NULL;
1528 ire->ire_next = NULL;
1529 if (ire->ire_ipversion == IPV6_VERSION) {
1530 BUMP_IRE_STATS(ipst->ips_ire_stats_v6, ire_stats_deleted);
1531 } else {
1532 BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_deleted);
1534 rw_exit(&irb->irb_lock);
1536 /* Cleanup dependents and related stuff */
1537 if (ire->ire_ipversion == IPV6_VERSION) {
1538 ire_delete_v6(ire);
1539 } else {
1540 ire_delete_v4(ire);
1543 * We removed it from the list. Decrement the
1544 * reference count.
1546 ire_refrele_notr(ire);
1550 * Delete the specified IRE.
1551 * All calls should use ire_delete().
1552 * Sometimes called as writer though not required by this function.
1554 * NOTE : This function is called only if the ire was added
1555 * in the list.
1557 static void
1558 ire_delete_v4(ire_t *ire)
1560 ip_stack_t *ipst = ire->ire_ipst;
1562 ASSERT(ire->ire_refcnt >= 1);
1563 ASSERT(ire->ire_ipversion == IPV4_VERSION);
1565 ire_flush_cache_v4(ire, IRE_FLUSH_DELETE);
1566 if (ire->ire_type == IRE_DEFAULT) {
1568 * when a default gateway is going away
1569 * delete all the host redirects pointing at that
1570 * gateway.
1572 ire_delete_host_redirects(ire->ire_gateway_addr, ipst);
1576 * If we are deleting an IRE_INTERFACE then we make sure we also
1577 * delete any IRE_IF_CLONE that has been created from it.
1578 * Those are always in ire_dep_children.
1580 if ((ire->ire_type & IRE_INTERFACE) && ire->ire_dep_children != NULL)
1581 ire_dep_delete_if_clone(ire);
1583 /* Remove from parent dependencies and child */
1584 rw_enter(&ipst->ips_ire_dep_lock, RW_WRITER);
1585 if (ire->ire_dep_parent != NULL)
1586 ire_dep_remove(ire);
1588 while (ire->ire_dep_children != NULL)
1589 ire_dep_remove(ire->ire_dep_children);
1590 rw_exit(&ipst->ips_ire_dep_lock);
1594 * ire_refrele is the only caller of the function. It calls
1595 * to free the ire when the reference count goes to zero.
1597 void
1598 ire_inactive(ire_t *ire)
1600 ill_t *ill;
1601 irb_t *irb;
1602 ip_stack_t *ipst = ire->ire_ipst;
1604 ASSERT(ire->ire_refcnt == 0);
1605 ASSERT(ire->ire_ptpn == NULL);
1606 ASSERT(ire->ire_next == NULL);
1608 /* Count how many condemned ires for kmem_cache callback */
1609 ASSERT(IRE_IS_CONDEMNED(ire));
1610 atomic_add_32(&ipst->ips_num_ire_condemned, -1);
1613 * ire_nce_cache is cleared in ire_delete, and we make sure we don't
1614 * set it once the ire is marked condemned.
1616 ASSERT(ire->ire_nce_cache == NULL);
1619 * Since any parent would have a refhold on us they would already
1620 * have been removed.
1622 ASSERT(ire->ire_dep_parent == NULL);
1623 ASSERT(ire->ire_dep_sib_next == NULL);
1624 ASSERT(ire->ire_dep_sib_ptpn == NULL);
1627 * Since any children would have a refhold on us they should have
1628 * already been removed.
1630 ASSERT(ire->ire_dep_children == NULL);
1633 * ill_ire_ref is increased when the IRE is inserted in the
1634 * bucket - not when the IRE is created.
1636 irb = ire->ire_bucket;
1637 ill = ire->ire_ill;
1638 if (irb != NULL && ill != NULL) {
1639 mutex_enter(&ill->ill_lock);
1640 ASSERT(ill->ill_ire_cnt != 0);
1641 DTRACE_PROBE3(ill__decr__cnt, (ill_t *), ill,
1642 (char *), "ire", (void *), ire);
1643 ill->ill_ire_cnt--;
1644 if (ILL_DOWN_OK(ill)) {
1645 /* Drops the ill lock */
1646 ipif_ill_refrele_tail(ill);
1647 } else {
1648 mutex_exit(&ill->ill_lock);
1651 ire->ire_ill = NULL;
1653 /* This should be true for both V4 and V6 */
1654 if (irb != NULL && (irb->irb_marks & IRB_MARK_DYNAMIC)) {
1655 rw_enter(&irb->irb_lock, RW_WRITER);
1656 irb->irb_nire--;
1658 * Instead of examining the conditions for freeing
1659 * the radix node here, we do it by calling
1660 * irb_refrele which is a single point in the code
1661 * that embeds that logic. Bump up the refcnt to
1662 * be able to call irb_refrele
1664 irb_refhold_locked(irb);
1665 rw_exit(&irb->irb_lock);
1666 irb_refrele(irb);
1669 #ifdef DEBUG
1670 ire_trace_cleanup(ire);
1671 #endif
1672 mutex_destroy(&ire->ire_lock);
1673 if (ire->ire_ipversion == IPV6_VERSION) {
1674 BUMP_IRE_STATS(ipst->ips_ire_stats_v6, ire_stats_freed);
1675 } else {
1676 BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_freed);
1678 kmem_cache_free(ire_cache, ire);
1682 * ire_update_generation is the callback function provided by
1683 * ire_get_bucket() to update the generation number of any
1684 * matching shorter route when a new route is added.
1686 * This fucntion always returns a failure return (B_FALSE)
1687 * to force the caller (rn_matchaddr_args)
1688 * to back-track up the tree looking for shorter matches.
1690 /* ARGSUSED */
1691 static boolean_t
1692 ire_update_generation(struct radix_node *rn, void *arg)
1694 struct rt_entry *rt = (struct rt_entry *)rn;
1696 /* We need to handle all in the same bucket */
1697 irb_increment_generation(&rt->rt_irb);
1698 return (B_FALSE);
1702 * Take care of all the generation numbers in the bucket.
1704 void
1705 irb_increment_generation(irb_t *irb)
1707 ire_t *ire;
1708 ip_stack_t *ipst;
1710 if (irb == NULL || irb->irb_ire_cnt == 0)
1711 return;
1713 ipst = irb->irb_ipst;
1715 * we cannot do an irb_refhold/irb_refrele here as the caller
1716 * already has the global RADIX_NODE_HEAD_WLOCK, and the irb_refrele
1717 * may result in an attempt to free the irb_t, which also needs
1718 * the RADIX_NODE_HEAD lock. However, since we want to traverse the
1719 * irb_ire list without fear of having a condemned ire removed from
1720 * the list, we acquire the irb_lock as WRITER. Moreover, since
1721 * the ire_generation increments are done under the ire_dep_lock,
1722 * acquire the locks in the prescribed lock order first.
1724 rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
1725 rw_enter(&irb->irb_lock, RW_WRITER);
1726 for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
1727 if (!IRE_IS_CONDEMNED(ire))
1728 ire_increment_generation(ire); /* Ourselves */
1729 ire_dep_incr_generation_locked(ire); /* Dependants */
1731 rw_exit(&irb->irb_lock);
1732 rw_exit(&ipst->ips_ire_dep_lock);
1736 * When an IRE is added or deleted this routine is called to make sure
1737 * any caching of IRE information is notified or updated.
1739 * The flag argument indicates if the flush request is due to addition
1740 * of new route (IRE_FLUSH_ADD), deletion of old route (IRE_FLUSH_DELETE),
1741 * or a change to ire_gateway_addr (IRE_FLUSH_GWCHANGE).
1743 void
1744 ire_flush_cache_v4(ire_t *ire, int flag)
1746 irb_t *irb = ire->ire_bucket;
1747 struct rt_entry *rt = IRB2RT(irb);
1748 ip_stack_t *ipst = ire->ire_ipst;
1751 * IRE_IF_CLONE ire's don't provide any new information
1752 * than the parent from which they are cloned, so don't
1753 * perturb the generation numbers.
1755 if (ire->ire_type & IRE_IF_CLONE)
1756 return;
1759 * Ensure that an ire_add during a lookup serializes the updates of the
1760 * generation numbers under the radix head lock so that the lookup gets
1761 * either the old ire and old generation number, or a new ire and new
1762 * generation number.
1764 RADIX_NODE_HEAD_WLOCK(ipst->ips_ip_ftable);
1767 * If a route was just added, we need to notify everybody that
1768 * has cached an IRE_NOROUTE since there might now be a better
1769 * route for them.
1771 if (flag == IRE_FLUSH_ADD) {
1772 ire_increment_generation(ipst->ips_ire_reject_v4);
1773 ire_increment_generation(ipst->ips_ire_blackhole_v4);
1776 /* Adding a default can't otherwise provide a better route */
1777 if (ire->ire_type == IRE_DEFAULT && flag == IRE_FLUSH_ADD) {
1778 RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
1779 return;
1782 switch (flag) {
1783 case IRE_FLUSH_DELETE:
1784 case IRE_FLUSH_GWCHANGE:
1786 * Update ire_generation for all ire_dep_children chains
1787 * starting with this IRE
1789 ire_dep_incr_generation(ire);
1790 break;
1791 case IRE_FLUSH_ADD:
1793 * Update the generation numbers of all shorter matching routes.
1794 * ire_update_generation takes care of the dependants by
1795 * using ire_dep_incr_generation.
1797 (void) ipst->ips_ip_ftable->rnh_matchaddr_args(&rt->rt_dst,
1798 ipst->ips_ip_ftable, ire_update_generation, NULL);
1799 break;
1801 RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
1805 * Matches the arguments passed with the values in the ire.
1807 * Note: for match types that match using "ill" passed in, ill
1808 * must be checked for non-NULL before calling this routine.
1810 boolean_t
1811 ire_match_args(ire_t *ire, ipaddr_t addr, ipaddr_t mask, ipaddr_t gateway,
1812 int type, const ill_t *ill, zoneid_t zoneid, int match_flags)
1814 ill_t *ire_ill = NULL, *dst_ill;
1815 ip_stack_t *ipst = ire->ire_ipst;
1817 ASSERT(ire->ire_ipversion == IPV4_VERSION);
1818 ASSERT((ire->ire_addr & ~ire->ire_mask) == 0);
1819 ASSERT((!(match_flags & (MATCH_IRE_ILL|MATCH_IRE_SRC_ILL))) ||
1820 (ill != NULL && !ill->ill_isv6));
1823 * If MATCH_IRE_TESTHIDDEN is set, then only return the IRE if it is
1824 * in fact hidden, to ensure the caller gets the right one.
1826 if (ire->ire_testhidden) {
1827 if (!(match_flags & MATCH_IRE_TESTHIDDEN))
1828 return (B_FALSE);
1831 if (zoneid != ALL_ZONES && zoneid != ire->ire_zoneid &&
1832 ire->ire_zoneid != ALL_ZONES) {
1834 * If MATCH_IRE_ZONEONLY has been set and the supplied zoneid
1835 * does not match that of ire_zoneid, a failure to
1836 * match is reported at this point. Otherwise, since some IREs
1837 * that are available in the global zone can be used in local
1838 * zones, additional checks need to be performed:
1840 * IRE_LOOPBACK
1841 * entries should never be matched in this situation.
1842 * Each zone has its own IRE_LOOPBACK.
1844 * IRE_LOCAL
1845 * We allow them for any zoneid. ire_route_recursive
1846 * does additional checks when
1847 * ip_restrict_interzone_loopback is set.
1849 * If ill_usesrc_ifindex is set
1850 * Then we check if the zone has a valid source address
1851 * on the usesrc ill.
1853 * If ire_ill is set, then check that the zone has an ipif
1854 * on that ill.
1856 * Outside of this function (in ire_round_robin) we check
1857 * that any IRE_OFFLINK has a gateway that reachable from the
1858 * zone when we have multiple choices (ECMP).
1860 if (match_flags & MATCH_IRE_ZONEONLY)
1861 return (B_FALSE);
1862 if (ire->ire_type & IRE_LOOPBACK)
1863 return (B_FALSE);
1865 if (ire->ire_type & IRE_LOCAL)
1866 goto matchit;
1869 * The normal case of IRE_ONLINK has a matching zoneid.
1870 * Here we handle the case when shared-IP zones have been
1871 * configured with IP addresses on vniN. In that case it
1872 * is ok for traffic from a zone to use IRE_ONLINK routes
1873 * if the ill has a usesrc pointing at vniN
1875 dst_ill = ire->ire_ill;
1876 if (ire->ire_type & IRE_ONLINK) {
1877 uint_t ifindex;
1880 * Note there is no IRE_INTERFACE on vniN thus
1881 * can't do an IRE lookup for a matching route.
1883 ifindex = dst_ill->ill_usesrc_ifindex;
1884 if (ifindex == 0)
1885 return (B_FALSE);
1888 * If there is a usable source address in the
1889 * zone, then it's ok to return this IRE_INTERFACE
1891 if (!ipif_zone_avail(ifindex, dst_ill->ill_isv6,
1892 zoneid, ipst)) {
1893 ip3dbg(("ire_match_args: no usrsrc for zone"
1894 " dst_ill %p\n", (void *)dst_ill));
1895 return (B_FALSE);
1899 * For example, with
1900 * route add 11.0.0.0 gw1 -ifp bge0
1901 * route add 11.0.0.0 gw2 -ifp bge1
1902 * this code would differentiate based on
1903 * where the sending zone has addresses.
1904 * Only if the zone has an address on bge0 can it use the first
1905 * route. It isn't clear if this behavior is documented
1906 * anywhere.
1908 if (dst_ill != NULL && (ire->ire_type & IRE_OFFLINK)) {
1909 ipif_t *tipif;
1911 mutex_enter(&dst_ill->ill_lock);
1912 for (tipif = dst_ill->ill_ipif;
1913 tipif != NULL; tipif = tipif->ipif_next) {
1914 if (!IPIF_IS_CONDEMNED(tipif) &&
1915 (tipif->ipif_flags & IPIF_UP) &&
1916 (tipif->ipif_zoneid == zoneid ||
1917 tipif->ipif_zoneid == ALL_ZONES))
1918 break;
1920 mutex_exit(&dst_ill->ill_lock);
1921 if (tipif == NULL) {
1922 return (B_FALSE);
1927 matchit:
1928 ire_ill = ire->ire_ill;
1929 if (match_flags & MATCH_IRE_ILL) {
1932 * If asked to match an ill, we *must* match
1933 * on the ire_ill for ipmp test addresses, or
1934 * any of the ill in the group for data addresses.
1935 * If we don't, we may as well fail.
1936 * However, we need an exception for IRE_LOCALs to ensure
1937 * we loopback packets even sent to test addresses on different
1938 * interfaces in the group.
1940 if ((match_flags & MATCH_IRE_TESTHIDDEN) &&
1941 !(ire->ire_type & IRE_LOCAL)) {
1942 if (ire->ire_ill != ill)
1943 return (B_FALSE);
1944 } else {
1945 match_flags &= ~MATCH_IRE_TESTHIDDEN;
1947 * We know that ill is not NULL, but ire_ill could be
1948 * NULL
1950 if (ire_ill == NULL || !IS_ON_SAME_LAN(ill, ire_ill))
1951 return (B_FALSE);
1954 if (match_flags & MATCH_IRE_SRC_ILL) {
1955 if (ire_ill == NULL)
1956 return (B_FALSE);
1957 if (!IS_ON_SAME_LAN(ill, ire_ill)) {
1958 if (ire_ill->ill_usesrc_ifindex == 0 ||
1959 (ire_ill->ill_usesrc_ifindex !=
1960 ill->ill_phyint->phyint_ifindex))
1961 return (B_FALSE);
1965 if ((ire->ire_addr == (addr & mask)) &&
1966 ((!(match_flags & MATCH_IRE_GW)) ||
1967 (ire->ire_gateway_addr == gateway)) &&
1968 ((!(match_flags & MATCH_IRE_DIRECT)) ||
1969 !(ire->ire_flags & RTF_INDIRECT)) &&
1970 ((!(match_flags & MATCH_IRE_TYPE)) || (ire->ire_type & type)) &&
1971 ((!(match_flags & MATCH_IRE_TESTHIDDEN)) || ire->ire_testhidden) &&
1972 ((!(match_flags & MATCH_IRE_MASK)) || (ire->ire_mask == mask))) {
1973 /* We found the matched IRE */
1974 return (B_TRUE);
1976 return (B_FALSE);
1980 * Check if the IRE_LOCAL uses the same ill as another route would use.
1981 * If there is no alternate route, or the alternate is a REJECT or BLACKHOLE,
1982 * then we don't allow this IRE_LOCAL to be used.
1983 * We always return an IRE; will be RTF_REJECT if no route available.
1985 ire_t *
1986 ire_alt_local(ire_t *ire, zoneid_t zoneid, const ill_t *ill,
1987 uint_t *generationp)
1989 ip_stack_t *ipst = ire->ire_ipst;
1990 ire_t *alt_ire;
1991 uint_t ire_type;
1992 uint_t generation;
1993 uint_t match_flags;
1995 ASSERT(ire->ire_type & IRE_LOCAL);
1996 ASSERT(ire->ire_ill != NULL);
1999 * Need to match on everything but local.
2000 * This might result in the creation of a IRE_IF_CLONE for the
2001 * same address as the IRE_LOCAL when restrict_interzone_loopback is
2002 * set. ire_add_*() ensures that the IRE_IF_CLONE are tail inserted
2003 * to make sure the IRE_LOCAL is always found first.
2005 ire_type = (IRE_ONLINK | IRE_OFFLINK) & ~(IRE_LOCAL|IRE_LOOPBACK);
2006 match_flags = MATCH_IRE_TYPE;
2007 if (ill != NULL)
2008 match_flags |= MATCH_IRE_ILL;
2010 if (ire->ire_ipversion == IPV4_VERSION) {
2011 alt_ire = ire_route_recursive_v4(ire->ire_addr, ire_type,
2012 ill, zoneid, match_flags, IRR_ALLOCATE, 0, ipst, NULL,
2013 &generation);
2014 } else {
2015 alt_ire = ire_route_recursive_v6(&ire->ire_addr_v6, ire_type,
2016 ill, zoneid, match_flags, IRR_ALLOCATE, 0, ipst, NULL,
2017 &generation);
2019 ASSERT(alt_ire != NULL);
2021 if (alt_ire->ire_ill == ire->ire_ill) {
2022 /* Going out the same ILL - ok to send to IRE_LOCAL */
2023 ire_refrele(alt_ire);
2024 } else {
2025 /* Different ill - ignore IRE_LOCAL */
2026 ire_refrele(ire);
2027 ire = alt_ire;
2028 if (generationp != NULL)
2029 *generationp = generation;
2031 return (ire);
2034 boolean_t
2035 ire_find_zoneid(struct radix_node *rn, void *arg)
2037 struct rt_entry *rt = (struct rt_entry *)rn;
2038 irb_t *irb;
2039 ire_t *ire;
2040 ire_ftable_args_t *margs = arg;
2042 ASSERT(rt != NULL);
2044 irb = &rt->rt_irb;
2046 if (irb->irb_ire_cnt == 0)
2047 return (B_FALSE);
2049 rw_enter(&irb->irb_lock, RW_READER);
2050 for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
2051 if (IRE_IS_CONDEMNED(ire))
2052 continue;
2054 if (!(ire->ire_type & IRE_INTERFACE))
2055 continue;
2057 if (ire->ire_zoneid != ALL_ZONES &&
2058 ire->ire_zoneid != margs->ift_zoneid)
2059 continue;
2061 if (margs->ift_ill != NULL && margs->ift_ill != ire->ire_ill)
2062 continue;
2064 rw_exit(&irb->irb_lock);
2065 return (B_TRUE);
2067 rw_exit(&irb->irb_lock);
2068 return (B_FALSE);
2072 * Check if the zoneid (not ALL_ZONES) has an IRE_INTERFACE for the specified
2073 * gateway address. If ill is non-NULL we also match on it.
2074 * The caller must hold a read lock on RADIX_NODE_HEAD if lock_held is set.
2076 boolean_t
2077 ire_gateway_ok_zone_v4(ipaddr_t gateway, zoneid_t zoneid, ill_t *ill,
2078 ip_stack_t *ipst, boolean_t lock_held)
2080 struct rt_sockaddr rdst;
2081 struct rt_entry *rt;
2082 ire_ftable_args_t margs;
2084 ASSERT(ill == NULL || !ill->ill_isv6);
2085 if (lock_held)
2086 ASSERT(RW_READ_HELD(&ipst->ips_ip_ftable->rnh_lock));
2087 else
2088 RADIX_NODE_HEAD_RLOCK(ipst->ips_ip_ftable);
2090 bzero(&rdst, sizeof (rdst));
2091 rdst.rt_sin_len = sizeof (rdst);
2092 rdst.rt_sin_family = AF_INET;
2093 rdst.rt_sin_addr.s_addr = gateway;
2095 /* We only use margs for ill and zoneidmatching in ire_find_zoneid */
2096 bzero(&margs, sizeof (margs));
2097 margs.ift_ill = ill;
2098 margs.ift_zoneid = zoneid;
2099 rt = (struct rt_entry *)ipst->ips_ip_ftable->rnh_matchaddr_args(&rdst,
2100 ipst->ips_ip_ftable, ire_find_zoneid, (void *)&margs);
2102 if (!lock_held)
2103 RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
2105 return (rt != NULL);
2109 * ire_walk routine to delete a fraction of redirect IREs and IRE_CLONE_IF IREs.
2110 * The fraction argument tells us what fraction of the IREs to delete.
2111 * Common for IPv4 and IPv6.
2112 * Used when memory backpressure.
2114 static void
2115 ire_delete_reclaim(ire_t *ire, char *arg)
2117 ip_stack_t *ipst = ire->ire_ipst;
2118 uint_t fraction = *(uint_t *)arg;
2119 uint_t rand;
2121 if ((ire->ire_flags & RTF_DYNAMIC) ||
2122 (ire->ire_type & IRE_IF_CLONE)) {
2124 /* Pick a random number */
2125 rand = (uint_t)ddi_get_lbolt() +
2126 IRE_ADDR_HASH_V6(ire->ire_addr_v6, 256);
2128 /* Use truncation */
2129 if ((rand/fraction)*fraction == rand) {
2130 IP_STAT(ipst, ip_ire_reclaim_deleted);
2131 ire_delete(ire);
2138 * kmem_cache callback to free up memory.
2140 * Free a fraction (ips_ip_ire_reclaim_fraction) of things IP added dynamically
2141 * (RTF_DYNAMIC and IRE_IF_CLONE).
2143 static void
2144 ip_ire_reclaim_stack(ip_stack_t *ipst)
2146 uint_t fraction = ipst->ips_ip_ire_reclaim_fraction;
2148 IP_STAT(ipst, ip_ire_reclaim_calls);
2150 ire_walk(ire_delete_reclaim, &fraction, ipst);
2153 * Walk all CONNs that can have a reference on an ire, nce or dce.
2154 * Get them to update any stale references to drop any refholds they
2155 * have.
2157 ipcl_walk(conn_ixa_cleanup, (void *)B_FALSE, ipst);
2161 * Called by the memory allocator subsystem directly, when the system
2162 * is running low on memory.
2164 /* ARGSUSED */
2165 void
2166 ip_ire_reclaim(void *args)
2168 netstack_handle_t nh;
2169 netstack_t *ns;
2170 ip_stack_t *ipst;
2172 netstack_next_init(&nh);
2173 while ((ns = netstack_next(&nh)) != NULL) {
2175 * netstack_next() can return a netstack_t with a NULL
2176 * netstack_ip at boot time.
2178 if ((ipst = ns->netstack_ip) == NULL) {
2179 netstack_rele(ns);
2180 continue;
2182 ip_ire_reclaim_stack(ipst);
2183 netstack_rele(ns);
2185 netstack_next_fini(&nh);
2188 static void
2189 power2_roundup(uint32_t *value)
2191 int i;
2193 for (i = 1; i < 31; i++) {
2194 if (*value <= (1 << i))
2195 break;
2197 *value = (1 << i);
2200 /* Global init for all zones */
2201 void
2202 ip_ire_g_init()
2205 * Create kmem_caches. ip_ire_reclaim() and ip_nce_reclaim()
2206 * will give disposable IREs back to system when needed.
2207 * This needs to be done here before anything else, since
2208 * ire_add() expects the cache to be created.
2210 ire_cache = kmem_cache_create("ire_cache",
2211 sizeof (ire_t), 0, NULL, NULL,
2212 ip_ire_reclaim, NULL, NULL, 0);
2214 ncec_cache = kmem_cache_create("ncec_cache",
2215 sizeof (ncec_t), 0, NULL, NULL,
2216 ip_nce_reclaim, NULL, NULL, 0);
2217 nce_cache = kmem_cache_create("nce_cache",
2218 sizeof (nce_t), 0, NULL, NULL,
2219 NULL, NULL, NULL, 0);
2221 rt_entry_cache = kmem_cache_create("rt_entry",
2222 sizeof (struct rt_entry), 0, NULL, NULL, NULL, NULL, NULL, 0);
2225 * Have radix code setup kmem caches etc.
2227 rn_init();
2230 void
2231 ip_ire_init(ip_stack_t *ipst)
2233 ire_t *ire;
2234 int error;
2236 mutex_init(&ipst->ips_ire_ft_init_lock, NULL, MUTEX_DEFAULT, 0);
2238 (void) rn_inithead((void **)&ipst->ips_ip_ftable, 32);
2241 * Make sure that the forwarding table size is a power of 2.
2242 * The IRE*_ADDR_HASH() macroes depend on that.
2244 ipst->ips_ip6_ftable_hash_size = ip6_ftable_hash_size;
2245 power2_roundup(&ipst->ips_ip6_ftable_hash_size);
2248 * Allocate/initialize a pair of IRE_NOROUTEs for each of IPv4 and IPv6.
2249 * The ire_reject_v* has RTF_REJECT set, and the ire_blackhole_v* has
2250 * RTF_BLACKHOLE set. We use the latter for transient errors such
2251 * as memory allocation failures and tripping on IRE_IS_CONDEMNED
2252 * entries.
2254 ire = kmem_cache_alloc(ire_cache, KM_SLEEP);
2255 *ire = ire_null;
2256 error = ire_init_v4(ire, 0, 0, 0, IRE_NOROUTE, NULL, ALL_ZONES,
2257 RTF_REJECT|RTF_UP, ipst);
2258 ASSERT(error == 0);
2259 ipst->ips_ire_reject_v4 = ire;
2261 ire = kmem_cache_alloc(ire_cache, KM_SLEEP);
2262 *ire = ire_null;
2263 error = ire_init_v6(ire, 0, 0, 0, IRE_NOROUTE, NULL, ALL_ZONES,
2264 RTF_REJECT|RTF_UP, ipst);
2265 ASSERT(error == 0);
2266 ipst->ips_ire_reject_v6 = ire;
2268 ire = kmem_cache_alloc(ire_cache, KM_SLEEP);
2269 *ire = ire_null;
2270 error = ire_init_v4(ire, 0, 0, 0, IRE_NOROUTE, NULL, ALL_ZONES,
2271 RTF_BLACKHOLE|RTF_UP, ipst);
2272 ASSERT(error == 0);
2273 ipst->ips_ire_blackhole_v4 = ire;
2275 ire = kmem_cache_alloc(ire_cache, KM_SLEEP);
2276 *ire = ire_null;
2277 error = ire_init_v6(ire, 0, 0, 0, IRE_NOROUTE, NULL, ALL_ZONES,
2278 RTF_BLACKHOLE|RTF_UP, ipst);
2279 ASSERT(error == 0);
2280 ipst->ips_ire_blackhole_v6 = ire;
2282 rw_init(&ipst->ips_ip6_ire_head_lock, NULL, RW_DEFAULT, NULL);
2283 rw_init(&ipst->ips_ire_dep_lock, NULL, RW_DEFAULT, NULL);
2286 void
2287 ip_ire_g_fini(void)
2289 kmem_cache_destroy(ire_cache);
2290 kmem_cache_destroy(ncec_cache);
2291 kmem_cache_destroy(nce_cache);
2292 kmem_cache_destroy(rt_entry_cache);
2294 rn_fini();
2297 void
2298 ip_ire_fini(ip_stack_t *ipst)
2300 int i;
2302 ire_make_condemned(ipst->ips_ire_reject_v6);
2303 ire_refrele_notr(ipst->ips_ire_reject_v6);
2304 ipst->ips_ire_reject_v6 = NULL;
2306 ire_make_condemned(ipst->ips_ire_reject_v4);
2307 ire_refrele_notr(ipst->ips_ire_reject_v4);
2308 ipst->ips_ire_reject_v4 = NULL;
2310 ire_make_condemned(ipst->ips_ire_blackhole_v6);
2311 ire_refrele_notr(ipst->ips_ire_blackhole_v6);
2312 ipst->ips_ire_blackhole_v6 = NULL;
2314 ire_make_condemned(ipst->ips_ire_blackhole_v4);
2315 ire_refrele_notr(ipst->ips_ire_blackhole_v4);
2316 ipst->ips_ire_blackhole_v4 = NULL;
2319 * Delete all IREs - assumes that the ill/ipifs have
2320 * been removed so what remains are just the ftable to handle.
2322 ire_walk(ire_delete, NULL, ipst);
2324 rn_freehead(ipst->ips_ip_ftable);
2325 ipst->ips_ip_ftable = NULL;
2327 rw_destroy(&ipst->ips_ire_dep_lock);
2328 rw_destroy(&ipst->ips_ip6_ire_head_lock);
2330 mutex_destroy(&ipst->ips_ire_ft_init_lock);
2332 for (i = 0; i < IP6_MASK_TABLE_SIZE; i++) {
2333 irb_t *ptr;
2334 int j;
2336 if ((ptr = ipst->ips_ip_forwarding_table_v6[i]) == NULL)
2337 continue;
2339 for (j = 0; j < ipst->ips_ip6_ftable_hash_size; j++) {
2340 ASSERT(ptr[j].irb_ire == NULL);
2341 rw_destroy(&ptr[j].irb_lock);
2343 mi_free(ptr);
2344 ipst->ips_ip_forwarding_table_v6[i] = NULL;
2348 #ifdef DEBUG
2349 void
2350 ire_trace_ref(ire_t *ire)
2352 mutex_enter(&ire->ire_lock);
2353 if (ire->ire_trace_disable) {
2354 mutex_exit(&ire->ire_lock);
2355 return;
2358 if (th_trace_ref(ire, ire->ire_ipst)) {
2359 mutex_exit(&ire->ire_lock);
2360 } else {
2361 ire->ire_trace_disable = B_TRUE;
2362 mutex_exit(&ire->ire_lock);
2363 ire_trace_cleanup(ire);
2367 void
2368 ire_untrace_ref(ire_t *ire)
2370 mutex_enter(&ire->ire_lock);
2371 if (!ire->ire_trace_disable)
2372 th_trace_unref(ire);
2373 mutex_exit(&ire->ire_lock);
2376 static void
2377 ire_trace_cleanup(const ire_t *ire)
2379 th_trace_cleanup(ire, ire->ire_trace_disable);
2381 #endif /* DEBUG */
2384 * Find, or create if needed, the nce_t pointer to the neighbor cache
2385 * entry ncec_t for an IPv4 address. The nce_t will be created on the ill_t
2386 * in the non-IPMP case, or on the cast-ill in the IPMP bcast/mcast case, or
2387 * on the next available under-ill (selected by the IPMP rotor) in the
2388 * unicast IPMP case.
2390 * If a neighbor-cache entry has to be created (i.e., one does not already
2391 * exist in the nce list) the ncec_lladdr and ncec_state of the neighbor cache
2392 * entry are initialized in nce_add_v4(). The broadcast, multicast, and
2393 * link-layer type determine the contents of {ncec_state, ncec_lladdr} of
2394 * the ncec_t created. The ncec_lladdr is non-null for all link types with
2395 * non-zero ill_phys_addr_length, though the contents may be zero in cases
2396 * where the link-layer type is not known at the time of creation
2397 * (e.g., IRE_IFRESOLVER links)
2399 * All IRE_BROADCAST entries have ncec_state = ND_REACHABLE, and the nce_lladr
2400 * has the physical broadcast address of the outgoing interface.
2401 * For unicast ire entries,
2402 * - if the outgoing interface is of type IRE_IF_RESOLVER, a newly created
2403 * ncec_t with 0 nce_lladr contents, and will be in the ND_INITIAL state.
2404 * - if the outgoing interface is a IRE_IF_NORESOLVER interface, no link
2405 * layer resolution is necessary, so that the ncec_t will be in the
2406 * ND_REACHABLE state
2408 * The link layer information needed for broadcast addresses, and for
2409 * packets sent on IRE_IF_NORESOLVER interfaces is a constant mapping that
2410 * never needs re-verification for the lifetime of the ncec_t. These are
2411 * therefore marked NCE_F_NONUD.
2413 * The nce returned will be created such that the nce_ill == ill that
2414 * is passed in. Note that the nce itself may not have ncec_ill == ill
2415 * where IPMP links are involved.
2417 static nce_t *
2418 ire_nce_init(ill_t *ill, const void *addr, int ire_type)
2420 int err;
2421 nce_t *nce = NULL;
2422 uint16_t ncec_flags;
2423 uchar_t *hwaddr;
2424 boolean_t need_refrele = B_FALSE;
2425 ill_t *in_ill = ill;
2426 boolean_t is_unicast;
2427 uint_t hwaddr_len;
2429 is_unicast = ((ire_type & (IRE_MULTICAST|IRE_BROADCAST)) == 0);
2430 if (IS_IPMP(ill) ||
2431 ((ire_type & IRE_BROADCAST) && IS_UNDER_IPMP(ill))) {
2432 if ((ill = ipmp_ill_hold_xmit_ill(ill, is_unicast)) == NULL)
2433 return (NULL);
2434 need_refrele = B_TRUE;
2436 ncec_flags = (ill->ill_flags & ILLF_NONUD) ? NCE_F_NONUD : 0;
2438 switch (ire_type) {
2439 case IRE_BROADCAST:
2440 ASSERT(!ill->ill_isv6);
2441 ncec_flags |= (NCE_F_BCAST|NCE_F_NONUD);
2442 break;
2443 case IRE_MULTICAST:
2444 ncec_flags |= (NCE_F_MCAST|NCE_F_NONUD);
2445 break;
2448 if (ill->ill_net_type == IRE_IF_NORESOLVER && is_unicast) {
2449 hwaddr = ill->ill_dest_addr;
2450 } else {
2451 hwaddr = NULL;
2453 hwaddr_len = ill->ill_phys_addr_length;
2455 retry:
2456 /* nce_state will be computed by nce_add_common() */
2457 if (!ill->ill_isv6) {
2458 err = nce_lookup_then_add_v4(ill, hwaddr, hwaddr_len, addr,
2459 ncec_flags, ND_UNCHANGED, &nce);
2460 } else {
2461 err = nce_lookup_then_add_v6(ill, hwaddr, hwaddr_len, addr,
2462 ncec_flags, ND_UNCHANGED, &nce);
2465 switch (err) {
2466 case 0:
2467 break;
2468 case EEXIST:
2470 * When subnets change or partially overlap what was once
2471 * a broadcast address could now be a unicast, or vice versa.
2473 if (((ncec_flags ^ nce->nce_common->ncec_flags) &
2474 NCE_F_BCAST) != 0) {
2475 ASSERT(!ill->ill_isv6);
2476 ncec_delete(nce->nce_common);
2477 nce_refrele(nce);
2478 goto retry;
2480 break;
2481 default:
2482 DTRACE_PROBE2(nce__init__fail, ill_t *, ill, int, err);
2483 if (need_refrele)
2484 ill_refrele(ill);
2485 return (NULL);
2488 * If the ill was an under-ill of an IPMP group, we need to verify
2489 * that it is still active so that we select an active interface in
2490 * the group. However, since ipmp_ill_is_active ASSERTs for
2491 * IS_UNDER_IPMP(), we first need to verify that the ill is an
2492 * under-ill, and since this is being done in the data path, the
2493 * only way to ascertain this is by holding the ill_g_lock.
2495 rw_enter(&ill->ill_ipst->ips_ill_g_lock, RW_READER);
2496 mutex_enter(&ill->ill_lock);
2497 mutex_enter(&ill->ill_phyint->phyint_lock);
2498 if (need_refrele && IS_UNDER_IPMP(ill) && !ipmp_ill_is_active(ill)) {
2500 * need_refrele implies that the under ill was selected by
2501 * ipmp_ill_hold_xmit_ill() because either the in_ill was an
2502 * ipmp_ill, or we are sending a non-unicast packet on an
2503 * under_ill. However, when we get here, the ill selected by
2504 * ipmp_ill_hold_xmit_ill was pulled out of the active set
2505 * (for unicast) or cast_ill nomination (for !unicast) after
2506 * it was picked as the outgoing ill. We have to pick an
2507 * active interface and/or cast_ill in the group.
2509 mutex_exit(&ill->ill_phyint->phyint_lock);
2510 nce_delete(nce);
2511 mutex_exit(&ill->ill_lock);
2512 rw_exit(&ill->ill_ipst->ips_ill_g_lock);
2513 nce_refrele(nce);
2514 ill_refrele(ill);
2515 if ((ill = ipmp_ill_hold_xmit_ill(in_ill, is_unicast)) == NULL)
2516 return (NULL);
2517 goto retry;
2518 } else {
2519 mutex_exit(&ill->ill_phyint->phyint_lock);
2520 mutex_exit(&ill->ill_lock);
2521 rw_exit(&ill->ill_ipst->ips_ill_g_lock);
2523 done:
2524 ASSERT(nce->nce_ill == ill);
2525 if (need_refrele)
2526 ill_refrele(ill);
2527 return (nce);
2530 nce_t *
2531 arp_nce_init(ill_t *ill, in_addr_t addr4, int ire_type)
2533 return (ire_nce_init(ill, &addr4, ire_type));
2536 nce_t *
2537 ndp_nce_init(ill_t *ill, const in6_addr_t *addr6, int ire_type)
2539 ASSERT((ire_type & IRE_BROADCAST) == 0);
2540 return (ire_nce_init(ill, addr6, ire_type));
2544 * The caller should hold irb_lock as a writer if the ire is in a bucket.
2545 * This routine will clear ire_nce_cache, and we make sure that we can never
2546 * set ire_nce_cache after the ire is marked condemned.
2548 void
2549 ire_make_condemned(ire_t *ire)
2551 ip_stack_t *ipst = ire->ire_ipst;
2552 nce_t *nce;
2554 mutex_enter(&ire->ire_lock);
2555 ASSERT(ire->ire_bucket == NULL ||
2556 RW_WRITE_HELD(&ire->ire_bucket->irb_lock));
2557 ASSERT(!IRE_IS_CONDEMNED(ire));
2558 ire->ire_generation = IRE_GENERATION_CONDEMNED;
2559 /* Count how many condemned ires for kmem_cache callback */
2560 atomic_inc_32(&ipst->ips_num_ire_condemned);
2561 nce = ire->ire_nce_cache;
2562 ire->ire_nce_cache = NULL;
2563 mutex_exit(&ire->ire_lock);
2564 if (nce != NULL)
2565 nce_refrele(nce);
2569 * Increment the generation avoiding the special condemned value
2571 void
2572 ire_increment_generation(ire_t *ire)
2574 uint_t generation;
2576 mutex_enter(&ire->ire_lock);
2578 * Even though the caller has a hold it can't prevent a concurrent
2579 * ire_delete marking the IRE condemned
2581 if (!IRE_IS_CONDEMNED(ire)) {
2582 generation = ire->ire_generation + 1;
2583 if (generation == IRE_GENERATION_CONDEMNED)
2584 generation = IRE_GENERATION_INITIAL;
2585 ASSERT(generation != IRE_GENERATION_VERIFY);
2586 ire->ire_generation = generation;
2588 mutex_exit(&ire->ire_lock);
2592 * Increment ire_generation on all the IRE_MULTICASTs
2593 * Used when the default multicast interface (as determined by
2594 * ill_lookup_multicast) might have changed.
2596 * That includes the zoneid, IFF_ flags, the IPv6 scope of the address, and
2597 * ill unplumb.
2599 void
2600 ire_increment_multicast_generation(ip_stack_t *ipst, boolean_t isv6)
2602 ill_t *ill;
2603 ill_walk_context_t ctx;
2605 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
2606 if (isv6)
2607 ill = ILL_START_WALK_V6(&ctx, ipst);
2608 else
2609 ill = ILL_START_WALK_V4(&ctx, ipst);
2610 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
2611 if (ILL_IS_CONDEMNED(ill))
2612 continue;
2613 if (ill->ill_ire_multicast != NULL)
2614 ire_increment_generation(ill->ill_ire_multicast);
2616 rw_exit(&ipst->ips_ill_g_lock);
2620 * Return a held IRE_NOROUTE with RTF_REJECT set
2622 ire_t *
2623 ire_reject(ip_stack_t *ipst, boolean_t isv6)
2625 ire_t *ire;
2627 if (isv6)
2628 ire = ipst->ips_ire_reject_v6;
2629 else
2630 ire = ipst->ips_ire_reject_v4;
2632 ASSERT(ire->ire_generation != IRE_GENERATION_CONDEMNED);
2633 ire_refhold(ire);
2634 return (ire);
2638 * Return a held IRE_NOROUTE with RTF_BLACKHOLE set
2640 ire_t *
2641 ire_blackhole(ip_stack_t *ipst, boolean_t isv6)
2643 ire_t *ire;
2645 if (isv6)
2646 ire = ipst->ips_ire_blackhole_v6;
2647 else
2648 ire = ipst->ips_ire_blackhole_v4;
2650 ASSERT(ire->ire_generation != IRE_GENERATION_CONDEMNED);
2651 ire_refhold(ire);
2652 return (ire);
2656 * Return a held IRE_MULTICAST.
2658 ire_t *
2659 ire_multicast(ill_t *ill)
2661 ire_t *ire = ill->ill_ire_multicast;
2663 ASSERT(ire == NULL || ire->ire_generation != IRE_GENERATION_CONDEMNED);
2664 if (ire == NULL)
2665 ire = ire_blackhole(ill->ill_ipst, ill->ill_isv6);
2666 else
2667 ire_refhold(ire);
2668 return (ire);
2672 * Given an IRE return its nexthop IRE. The nexthop IRE is an IRE_ONLINK
2673 * that is an exact match (i.e., a /32 for IPv4 and /128 for IPv6).
2674 * This can return an RTF_REJECT|RTF_BLACKHOLE.
2675 * The returned IRE is held.
2676 * The assumption is that ip_select_route() has been called and returned the
2677 * IRE (thus ip_select_route would have set up the ire_dep* information.)
2678 * If some IRE is deleteted then ire_dep_remove() will have been called and
2679 * we might not find a nexthop IRE, in which case we return NULL.
2681 ire_t *
2682 ire_nexthop(ire_t *ire)
2684 ip_stack_t *ipst = ire->ire_ipst;
2686 /* Acquire lock to walk ire_dep_parent */
2687 rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
2688 while (ire != NULL) {
2689 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
2690 goto done;
2693 * If we find an IRE_ONLINK we are done. This includes
2694 * the case of IRE_MULTICAST.
2695 * Note that in order to send packets we need a host-specific
2696 * IRE_IF_ALL first in the ire_dep_parent chain. Normally this
2697 * is done by inserting an IRE_IF_CLONE if the IRE_INTERFACE
2698 * was not host specific.
2699 * However, ip_rts_request doesn't want to send packets
2700 * hence doesn't want to allocate an IRE_IF_CLONE. Yet
2701 * it needs an IRE_IF_ALL to get to the ill. Thus
2702 * we return IRE_IF_ALL that are not host specific here.
2704 if (ire->ire_type & IRE_ONLINK)
2705 goto done;
2706 ire = ire->ire_dep_parent;
2708 rw_exit(&ipst->ips_ire_dep_lock);
2709 return (NULL);
2711 done:
2712 ire_refhold(ire);
2713 rw_exit(&ipst->ips_ire_dep_lock);
2714 return (ire);
2718 * Find the ill used to send packets. This will be NULL in case
2719 * of a reject or blackhole.
2720 * The returned ill is held; caller needs to do ill_refrele when done.
2722 ill_t *
2723 ire_nexthop_ill(ire_t *ire)
2725 ill_t *ill;
2727 ire = ire_nexthop(ire);
2728 if (ire == NULL)
2729 return (NULL);
2731 /* ire_ill can not change for an existing ire */
2732 ill = ire->ire_ill;
2733 if (ill != NULL)
2734 ill_refhold(ill);
2735 ire_refrele(ire);
2736 return (ill);
2739 #ifdef DEBUG
2740 static boolean_t
2741 parent_has_child(ire_t *parent, ire_t *child)
2743 ire_t *ire;
2744 ire_t *prev;
2746 ire = parent->ire_dep_children;
2747 prev = NULL;
2748 while (ire != NULL) {
2749 if (prev == NULL) {
2750 ASSERT(ire->ire_dep_sib_ptpn ==
2751 &(parent->ire_dep_children));
2752 } else {
2753 ASSERT(ire->ire_dep_sib_ptpn ==
2754 &(prev->ire_dep_sib_next));
2756 if (ire == child)
2757 return (B_TRUE);
2758 prev = ire;
2759 ire = ire->ire_dep_sib_next;
2761 return (B_FALSE);
2764 static void
2765 ire_dep_verify(ire_t *ire)
2767 ire_t *parent = ire->ire_dep_parent;
2768 ire_t *child = ire->ire_dep_children;
2770 ASSERT(ire->ire_ipversion == IPV4_VERSION ||
2771 ire->ire_ipversion == IPV6_VERSION);
2772 if (parent != NULL) {
2773 ASSERT(parent->ire_ipversion == IPV4_VERSION ||
2774 parent->ire_ipversion == IPV6_VERSION);
2775 ASSERT(parent->ire_refcnt >= 1);
2776 ASSERT(parent_has_child(parent, ire));
2778 if (child != NULL) {
2779 ASSERT(child->ire_ipversion == IPV4_VERSION ||
2780 child->ire_ipversion == IPV6_VERSION);
2781 ASSERT(child->ire_dep_parent == ire);
2782 ASSERT(child->ire_dep_sib_ptpn != NULL);
2783 ASSERT(parent_has_child(ire, child));
2786 #endif /* DEBUG */
2789 * Assumes ire_dep_parent is set. Remove this child from its parent's linkage.
2791 void
2792 ire_dep_remove(ire_t *ire)
2794 ip_stack_t *ipst = ire->ire_ipst;
2795 ire_t *parent = ire->ire_dep_parent;
2796 ire_t *next;
2797 nce_t *nce;
2799 ASSERT(RW_WRITE_HELD(&ipst->ips_ire_dep_lock));
2800 ASSERT(ire->ire_dep_parent != NULL);
2801 ASSERT(ire->ire_dep_sib_ptpn != NULL);
2803 #ifdef DEBUG
2804 ire_dep_verify(ire);
2805 ire_dep_verify(parent);
2806 #endif
2808 next = ire->ire_dep_sib_next;
2809 if (next != NULL)
2810 next->ire_dep_sib_ptpn = ire->ire_dep_sib_ptpn;
2812 ASSERT(*(ire->ire_dep_sib_ptpn) == ire);
2813 *(ire->ire_dep_sib_ptpn) = ire->ire_dep_sib_next;
2815 ire->ire_dep_sib_ptpn = NULL;
2816 ire->ire_dep_sib_next = NULL;
2818 mutex_enter(&ire->ire_lock);
2819 parent = ire->ire_dep_parent;
2820 ire->ire_dep_parent = NULL;
2821 mutex_exit(&ire->ire_lock);
2824 * Make sure all our children, grandchildren, etc set
2825 * ire_dep_parent_generation to IRE_GENERATION_VERIFY since
2826 * we can no longer guarantee than the children have a current
2827 * ire_nce_cache and ire_nexthop_ill().
2829 if (ire->ire_dep_children != NULL)
2830 ire_dep_invalidate_children(ire->ire_dep_children);
2833 * Since the parent is gone we make sure we clear ire_nce_cache.
2834 * We can clear it under ire_lock even if the IRE is used
2836 mutex_enter(&ire->ire_lock);
2837 nce = ire->ire_nce_cache;
2838 ire->ire_nce_cache = NULL;
2839 mutex_exit(&ire->ire_lock);
2840 if (nce != NULL)
2841 nce_refrele(nce);
2843 #ifdef DEBUG
2844 ire_dep_verify(ire);
2845 ire_dep_verify(parent);
2846 #endif
2848 ire_refrele_notr(parent);
2849 ire_refrele_notr(ire);
2853 * Insert the child in the linkage of the parent
2855 static void
2856 ire_dep_parent_insert(ire_t *child, ire_t *parent)
2858 ip_stack_t *ipst = child->ire_ipst;
2859 ire_t *next;
2861 ASSERT(RW_WRITE_HELD(&ipst->ips_ire_dep_lock));
2862 ASSERT(child->ire_dep_parent == NULL);
2864 #ifdef DEBUG
2865 ire_dep_verify(child);
2866 ire_dep_verify(parent);
2867 #endif
2868 /* No parents => no siblings */
2869 ASSERT(child->ire_dep_sib_ptpn == NULL);
2870 ASSERT(child->ire_dep_sib_next == NULL);
2872 ire_refhold_notr(parent);
2873 ire_refhold_notr(child);
2875 /* Head insertion */
2876 next = parent->ire_dep_children;
2877 if (next != NULL) {
2878 ASSERT(next->ire_dep_sib_ptpn == &(parent->ire_dep_children));
2879 child->ire_dep_sib_next = next;
2880 next->ire_dep_sib_ptpn = &(child->ire_dep_sib_next);
2882 parent->ire_dep_children = child;
2883 child->ire_dep_sib_ptpn = &(parent->ire_dep_children);
2885 mutex_enter(&child->ire_lock);
2886 child->ire_dep_parent = parent;
2887 mutex_exit(&child->ire_lock);
2889 #ifdef DEBUG
2890 ire_dep_verify(child);
2891 ire_dep_verify(parent);
2892 #endif
2897 * Given count worth of ires and generations, build ire_dep_* relationships
2898 * from ires[0] to ires[count-1]. Record generations[i+1] in
2899 * ire_dep_parent_generation for ires[i].
2900 * We graft onto an existing parent chain by making sure that we don't
2901 * touch ire_dep_parent for ires[count-1].
2903 * We check for any condemned ire_generation count and return B_FALSE in
2904 * that case so that the caller can tear it apart.
2906 * Note that generations[0] is not used. Caller handles that.
2908 boolean_t
2909 ire_dep_build(ire_t *ires[], uint_t generations[], uint_t count)
2911 ire_t *ire = ires[0];
2912 ip_stack_t *ipst;
2913 uint_t i;
2915 ASSERT(count > 0);
2916 if (count == 1) {
2917 /* No work to do */
2918 return (B_TRUE);
2920 ipst = ire->ire_ipst;
2921 rw_enter(&ipst->ips_ire_dep_lock, RW_WRITER);
2923 * Do not remove the linkage for any existing parent chain i.e.,
2924 * ires[count-1] is left alone.
2926 for (i = 0; i < count-1; i++) {
2927 /* Remove existing parent if we need to change it */
2928 if (ires[i]->ire_dep_parent != NULL &&
2929 ires[i]->ire_dep_parent != ires[i+1])
2930 ire_dep_remove(ires[i]);
2933 for (i = 0; i < count - 1; i++) {
2934 ASSERT(ires[i]->ire_ipversion == IPV4_VERSION ||
2935 ires[i]->ire_ipversion == IPV6_VERSION);
2936 /* Does it need to change? */
2937 if (ires[i]->ire_dep_parent != ires[i+1])
2938 ire_dep_parent_insert(ires[i], ires[i+1]);
2940 mutex_enter(&ires[i+1]->ire_lock);
2941 if (IRE_IS_CONDEMNED(ires[i+1])) {
2942 mutex_exit(&ires[i+1]->ire_lock);
2943 rw_exit(&ipst->ips_ire_dep_lock);
2944 return (B_FALSE);
2946 mutex_exit(&ires[i+1]->ire_lock);
2948 mutex_enter(&ires[i]->ire_lock);
2949 ires[i]->ire_dep_parent_generation = generations[i+1];
2950 mutex_exit(&ires[i]->ire_lock);
2952 rw_exit(&ipst->ips_ire_dep_lock);
2953 return (B_TRUE);
2957 * Given count worth of ires, unbuild ire_dep_* relationships
2958 * from ires[0] to ires[count-1].
2960 void
2961 ire_dep_unbuild(ire_t *ires[], uint_t count)
2963 ip_stack_t *ipst;
2964 uint_t i;
2966 if (count == 0) {
2967 /* No work to do */
2968 return;
2970 ipst = ires[0]->ire_ipst;
2971 rw_enter(&ipst->ips_ire_dep_lock, RW_WRITER);
2972 for (i = 0; i < count; i++) {
2973 ASSERT(ires[i]->ire_ipversion == IPV4_VERSION ||
2974 ires[i]->ire_ipversion == IPV6_VERSION);
2975 if (ires[i]->ire_dep_parent != NULL)
2976 ire_dep_remove(ires[i]);
2977 mutex_enter(&ires[i]->ire_lock);
2978 ires[i]->ire_dep_parent_generation = IRE_GENERATION_VERIFY;
2979 mutex_exit(&ires[i]->ire_lock);
2981 rw_exit(&ipst->ips_ire_dep_lock);
2985 * Both the forwarding and the outbound code paths can trip on
2986 * a condemned NCE, in which case we call this function.
2987 * We have two different behaviors: if the NCE was UNREACHABLE
2988 * it is an indication that something failed. In that case
2989 * we see if we should look for a different IRE (for example,
2990 * delete any matching redirect IRE, or try a different
2991 * IRE_DEFAULT (ECMP)). We mark the ire as bad so a hopefully
2992 * different IRE will be picked next time we send/forward.
2994 * If we are called by the output path then fail_if_better is set
2995 * and we return NULL if there could be a better IRE. This is because the
2996 * output path retries the IRE lookup. (The input/forward path can not retry.)
2998 * If the NCE was not unreachable then we pick/allocate a
2999 * new (most likely ND_INITIAL) NCE and proceed with it.
3001 * ipha/ip6h are needed for multicast packets; ipha needs to be
3002 * set for IPv4 and ip6h needs to be set for IPv6 packets.
3004 nce_t *
3005 ire_handle_condemned_nce(nce_t *nce, ire_t *ire, ipha_t *ipha, ip6_t *ip6h,
3006 boolean_t fail_if_better)
3008 if (nce->nce_common->ncec_state == ND_UNREACHABLE) {
3009 if (ire_no_good(ire) && fail_if_better) {
3011 * Did some changes, or ECMP likely to exist.
3012 * Make ip_output look for a different IRE
3014 return (NULL);
3017 if (ire_revalidate_nce(ire) == ENETUNREACH) {
3018 /* The ire_dep_parent chain went bad, or no memory? */
3019 (void) ire_no_good(ire);
3020 return (NULL);
3022 if (ire->ire_ipversion == IPV4_VERSION) {
3023 ASSERT(ipha != NULL);
3024 nce = ire_to_nce(ire, ipha->ipha_dst, NULL);
3025 } else {
3026 ASSERT(ip6h != NULL);
3027 nce = ire_to_nce(ire, INADDR_ANY, &ip6h->ip6_dst);
3030 if (nce == NULL)
3031 return (NULL);
3032 if (nce->nce_is_condemned) {
3033 nce_refrele(nce);
3034 return (NULL);
3036 return (nce);
3040 * The caller has found that the ire is bad due to a reference to an NCE in
3041 * ND_UNREACHABLE state. We update things so a subsequent attempt to send
3042 * to the destination is likely to find different IRE, or that a new NCE
3043 * would be created.
3045 * Returns B_TRUE if it is likely that a subsequent ire_ftable_lookup would
3046 * find a different route (either due to having deleted a redirect, or there
3047 * being ECMP routes.)
3049 * If we have a redirect (RTF_DYNAMIC) we delete it.
3050 * Otherwise we increment ire_badcnt and increment the generation number so
3051 * that a cached ixa_ire will redo the route selection. ire_badcnt is taken
3052 * into account in the route selection when we have multiple choices (multiple
3053 * default routes or ECMP in general).
3054 * Any time ip_select_route find an ire with a condemned ire_nce_cache
3055 * (e.g., if no equal cost route to the bad one) ip_select_route will make
3056 * sure the NCE is revalidated to avoid getting stuck on a
3057 * NCE_F_CONDMNED ncec that caused ire_no_good to be called.
3059 boolean_t
3060 ire_no_good(ire_t *ire)
3062 ip_stack_t *ipst = ire->ire_ipst;
3063 ire_t *ire2;
3064 nce_t *nce;
3066 if (ire->ire_flags & RTF_DYNAMIC) {
3067 ire_delete(ire);
3068 return (B_TRUE);
3070 if (ire->ire_flags & RTF_INDIRECT) {
3071 /* Check if next IRE is a redirect */
3072 rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
3073 if (ire->ire_dep_parent != NULL &&
3074 (ire->ire_dep_parent->ire_flags & RTF_DYNAMIC)) {
3075 ire2 = ire->ire_dep_parent;
3076 ire_refhold(ire2);
3077 } else {
3078 ire2 = NULL;
3080 rw_exit(&ipst->ips_ire_dep_lock);
3081 if (ire2 != NULL) {
3082 ire_delete(ire2);
3083 ire_refrele(ire2);
3084 return (B_TRUE);
3088 * No redirect involved. Increment badcnt so that if we have ECMP
3089 * routes we are likely to pick a different one for the next packet.
3091 * If the NCE is unreachable and condemned we should drop the reference
3092 * to it so that a new NCE can be created.
3094 * Finally we increment the generation number so that any ixa_ire
3095 * cache will be revalidated.
3097 mutex_enter(&ire->ire_lock);
3098 ire->ire_badcnt++;
3099 ire->ire_last_badcnt = TICK_TO_SEC(ddi_get_lbolt64());
3100 nce = ire->ire_nce_cache;
3101 if (nce != NULL && nce->nce_is_condemned &&
3102 nce->nce_common->ncec_state == ND_UNREACHABLE)
3103 ire->ire_nce_cache = NULL;
3104 else
3105 nce = NULL;
3106 mutex_exit(&ire->ire_lock);
3107 if (nce != NULL)
3108 nce_refrele(nce);
3110 ire_increment_generation(ire);
3111 ire_dep_incr_generation(ire);
3113 return (ire->ire_bucket->irb_ire_cnt > 1);
3117 * Walk ire_dep_parent chain and validate that ire_dep_parent->ire_generation ==
3118 * ire_dep_parent_generation.
3119 * If they all match we just return ire_generation from the topmost IRE.
3120 * Otherwise we propagate the mismatch by setting all ire_dep_parent_generation
3121 * above the mismatch to IRE_GENERATION_VERIFY and also returning
3122 * IRE_GENERATION_VERIFY.
3124 uint_t
3125 ire_dep_validate_generations(ire_t *ire)
3127 ip_stack_t *ipst = ire->ire_ipst;
3128 uint_t generation;
3129 ire_t *ire1;
3131 rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
3132 generation = ire->ire_generation; /* Assuming things match */
3133 for (ire1 = ire; ire1 != NULL; ire1 = ire1->ire_dep_parent) {
3134 ASSERT(ire1->ire_ipversion == IPV4_VERSION ||
3135 ire1->ire_ipversion == IPV6_VERSION);
3136 if (ire1->ire_dep_parent == NULL)
3137 break;
3138 if (ire1->ire_dep_parent_generation !=
3139 ire1->ire_dep_parent->ire_generation)
3140 goto mismatch;
3142 rw_exit(&ipst->ips_ire_dep_lock);
3143 return (generation);
3145 mismatch:
3146 generation = IRE_GENERATION_VERIFY;
3147 /* Fill from top down to the mismatch with _VERIFY */
3148 while (ire != ire1) {
3149 ASSERT(ire->ire_ipversion == IPV4_VERSION ||
3150 ire->ire_ipversion == IPV6_VERSION);
3151 mutex_enter(&ire->ire_lock);
3152 ire->ire_dep_parent_generation = IRE_GENERATION_VERIFY;
3153 mutex_exit(&ire->ire_lock);
3154 ire = ire->ire_dep_parent;
3156 rw_exit(&ipst->ips_ire_dep_lock);
3157 return (generation);
3161 * Used when we need to return an ire with ire_dep_parent, but we
3162 * know the chain is invalid for instance we didn't create an IRE_IF_CLONE
3163 * Using IRE_GENERATION_VERIFY means that next time we'll redo the
3164 * recursive lookup.
3166 void
3167 ire_dep_invalidate_generations(ire_t *ire)
3169 ip_stack_t *ipst = ire->ire_ipst;
3171 rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
3172 while (ire != NULL) {
3173 ASSERT(ire->ire_ipversion == IPV4_VERSION ||
3174 ire->ire_ipversion == IPV6_VERSION);
3175 mutex_enter(&ire->ire_lock);
3176 ire->ire_dep_parent_generation = IRE_GENERATION_VERIFY;
3177 mutex_exit(&ire->ire_lock);
3178 ire = ire->ire_dep_parent;
3180 rw_exit(&ipst->ips_ire_dep_lock);
3183 /* Set _VERIFY ire_dep_parent_generation for all children recursively */
3184 static void
3185 ire_dep_invalidate_children(ire_t *child)
3187 ip_stack_t *ipst = child->ire_ipst;
3189 ASSERT(RW_WRITE_HELD(&ipst->ips_ire_dep_lock));
3190 /* Depth first */
3191 if (child->ire_dep_children != NULL)
3192 ire_dep_invalidate_children(child->ire_dep_children);
3194 while (child != NULL) {
3195 mutex_enter(&child->ire_lock);
3196 child->ire_dep_parent_generation = IRE_GENERATION_VERIFY;
3197 mutex_exit(&child->ire_lock);
3198 child = child->ire_dep_sib_next;
3202 static void
3203 ire_dep_increment_children(ire_t *child)
3205 ip_stack_t *ipst = child->ire_ipst;
3207 ASSERT(RW_READ_HELD(&ipst->ips_ire_dep_lock));
3208 /* Depth first */
3209 if (child->ire_dep_children != NULL)
3210 ire_dep_increment_children(child->ire_dep_children);
3212 while (child != NULL) {
3213 if (!IRE_IS_CONDEMNED(child))
3214 ire_increment_generation(child);
3215 child = child->ire_dep_sib_next;
3220 * Walk all the children of this ire recursively and increment their
3221 * generation number.
3223 static void
3224 ire_dep_incr_generation_locked(ire_t *parent)
3226 ASSERT(RW_READ_HELD(&parent->ire_ipst->ips_ire_dep_lock));
3227 if (parent->ire_dep_children != NULL)
3228 ire_dep_increment_children(parent->ire_dep_children);
3231 void
3232 ire_dep_incr_generation(ire_t *parent)
3234 ip_stack_t *ipst = parent->ire_ipst;
3236 rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
3237 ire_dep_incr_generation_locked(parent);
3238 rw_exit(&ipst->ips_ire_dep_lock);
3242 * Get a new ire_nce_cache for this IRE as well as its nexthop.
3243 * Returns zero if it succeeds. Can fail due to lack of memory or when
3244 * the route has become unreachable. Returns ENOMEM and ENETUNREACH in those
3245 * cases.
3247 * In the in.mpathd case, the ire will have ire_testhidden
3248 * set; so we should create the ncec for the underlying ill.
3250 * Note that the error returned by ire_revalidate_nce() is ignored by most
3251 * callers except ire_handle_condemned_nce(), which handles the ENETUNREACH
3252 * error to mark potentially bad ire's. For all the other callers, an
3253 * error return could indicate a transient condition like ENOMEM, or could
3254 * be the result of an interface that is going down/unplumbing. In the former
3255 * case (transient error), we would leave the old stale ire/ire_nce_cache
3256 * in place, and possibly use incorrect link-layer information to send packets
3257 * but would eventually recover. In the latter case (ill down/replumb),
3258 * ire_revalidate_nce() might return a condemned nce back, but we would then
3259 * recover in the packet output path.
3262 ire_revalidate_nce(ire_t *ire)
3264 nce_t *nce, *old_nce;
3265 ire_t *nexthop;
3268 * For multicast we conceptually have an NCE but we don't store it
3269 * in ire_nce_cache; when ire_to_nce is called we allocate the nce.
3271 if (ire->ire_type & IRE_MULTICAST)
3272 return (0);
3274 /* ire_testhidden should only be set on under-interfaces */
3275 ASSERT(!ire->ire_testhidden || !IS_IPMP(ire->ire_ill));
3277 nexthop = ire_nexthop(ire);
3278 if (nexthop == NULL) {
3279 /* The route is potentially bad */
3280 (void) ire_no_good(ire);
3281 return (ENETUNREACH);
3283 if (ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) {
3284 ASSERT(ire->ire_ill != NULL);
3286 if (ire->ire_ipversion == IPV4_VERSION)
3287 nce = nce_lookup_v4(ire->ire_ill, &ire->ire_addr);
3288 else
3289 nce = nce_lookup_v6(ire->ire_ill, &ire->ire_addr_v6);
3290 } else {
3291 ASSERT(nexthop->ire_type & IRE_ONLINK);
3292 if (ire->ire_ipversion == IPV4_VERSION) {
3293 nce = arp_nce_init(nexthop->ire_ill, nexthop->ire_addr,
3294 nexthop->ire_type);
3295 } else {
3296 nce = ndp_nce_init(nexthop->ire_ill,
3297 &nexthop->ire_addr_v6, nexthop->ire_type);
3300 if (nce == NULL) {
3302 * Leave the old stale one in place to avoid a NULL
3303 * ire_nce_cache.
3305 ire_refrele(nexthop);
3306 return (ENOMEM);
3309 if (nexthop != ire) {
3310 /* Update the nexthop ire */
3311 mutex_enter(&nexthop->ire_lock);
3312 old_nce = nexthop->ire_nce_cache;
3313 if (!IRE_IS_CONDEMNED(nexthop)) {
3314 nce_refhold(nce);
3315 nexthop->ire_nce_cache = nce;
3316 } else {
3317 nexthop->ire_nce_cache = NULL;
3319 mutex_exit(&nexthop->ire_lock);
3320 if (old_nce != NULL)
3321 nce_refrele(old_nce);
3323 ire_refrele(nexthop);
3325 mutex_enter(&ire->ire_lock);
3326 old_nce = ire->ire_nce_cache;
3327 if (!IRE_IS_CONDEMNED(ire)) {
3328 nce_refhold(nce);
3329 ire->ire_nce_cache = nce;
3330 } else {
3331 ire->ire_nce_cache = NULL;
3333 mutex_exit(&ire->ire_lock);
3334 if (old_nce != NULL)
3335 nce_refrele(old_nce);
3337 nce_refrele(nce);
3338 return (0);
3342 * Get a held nce for a given ire.
3343 * In the common case this is just from ire_nce_cache.
3344 * For IRE_MULTICAST this needs to do an explicit lookup since we do not
3345 * have an IRE_MULTICAST per address.
3346 * Note that this explicitly returns CONDEMNED NCEs. The caller needs those
3347 * so they can check whether the NCE went unreachable (as opposed to was
3348 * condemned for some other reason).
3350 nce_t *
3351 ire_to_nce(ire_t *ire, ipaddr_t v4nexthop, const in6_addr_t *v6nexthop)
3353 nce_t *nce;
3355 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))
3356 return (NULL);
3358 /* ire_testhidden should only be set on under-interfaces */
3359 ASSERT(!ire->ire_testhidden || !IS_IPMP(ire->ire_ill));
3361 mutex_enter(&ire->ire_lock);
3362 nce = ire->ire_nce_cache;
3363 if (nce != NULL) {
3364 nce_refhold(nce);
3365 mutex_exit(&ire->ire_lock);
3366 return (nce);
3368 mutex_exit(&ire->ire_lock);
3370 if (ire->ire_type & IRE_MULTICAST) {
3371 ASSERT(ire->ire_ill != NULL);
3373 if (ire->ire_ipversion == IPV4_VERSION) {
3374 ASSERT(v6nexthop == NULL);
3376 nce = arp_nce_init(ire->ire_ill, v4nexthop,
3377 ire->ire_type);
3378 } else {
3379 ASSERT(v6nexthop != NULL);
3380 ASSERT(v4nexthop == 0);
3381 nce = ndp_nce_init(ire->ire_ill, v6nexthop,
3382 ire->ire_type);
3384 return (nce);
3386 return (NULL);
3389 nce_t *
3390 ire_to_nce_pkt(ire_t *ire, mblk_t *mp)
3392 ipha_t *ipha;
3393 ip6_t *ip6h;
3395 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) {
3396 ipha = (ipha_t *)mp->b_rptr;
3397 return (ire_to_nce(ire, ipha->ipha_dst, NULL));
3398 } else {
3399 ip6h = (ip6_t *)mp->b_rptr;
3400 return (ire_to_nce(ire, INADDR_ANY, &ip6h->ip6_dst));
3405 * Given an IRE_INTERFACE (that matches more than one address) create
3406 * and return an IRE_IF_CLONE for the specific address.
3407 * Return the generation number.
3408 * Returns NULL is no memory for the IRE.
3409 * Handles both IPv4 and IPv6.
3411 * IRE_IF_CLONE entries may only be created adn added by calling
3412 * ire_create_if_clone(), and we depend on the fact that ire_add will
3413 * atomically ensure that attempts to add multiple identical IRE_IF_CLONE
3414 * entries will not result in duplicate (i.e., ire_identical_ref > 1)
3415 * CLONE entries, so that a single ire_delete is sufficient to remove the
3416 * CLONE.
3418 ire_t *
3419 ire_create_if_clone(ire_t *ire_if, const in6_addr_t *addr, uint_t *generationp)
3421 ire_t *ire;
3422 ire_t *nire;
3424 if (ire_if->ire_ipversion == IPV4_VERSION) {
3425 ipaddr_t v4addr;
3426 ipaddr_t mask = IP_HOST_MASK;
3428 ASSERT(IN6_IS_ADDR_V4MAPPED(addr));
3429 IN6_V4MAPPED_TO_IPADDR(addr, v4addr);
3431 ire = ire_create(
3432 (uchar_t *)&v4addr, /* dest address */
3433 (uchar_t *)&mask, /* mask */
3434 (uchar_t *)&ire_if->ire_gateway_addr,
3435 IRE_IF_CLONE, /* IRE type */
3436 ire_if->ire_ill,
3437 ire_if->ire_zoneid,
3438 ire_if->ire_flags | RTF_HOST,
3439 ire_if->ire_ipst);
3440 } else {
3441 ASSERT(!IN6_IS_ADDR_V4MAPPED(addr));
3442 ire = ire_create_v6(
3443 addr, /* dest address */
3444 &ipv6_all_ones, /* mask */
3445 &ire_if->ire_gateway_addr_v6, /* gateway addr */
3446 IRE_IF_CLONE, /* IRE type */
3447 ire_if->ire_ill,
3448 ire_if->ire_zoneid,
3449 ire_if->ire_flags | RTF_HOST,
3450 ire_if->ire_ipst);
3452 if (ire == NULL)
3453 return (NULL);
3455 /* Take the metrics, in particular the mtu, from the IRE_IF */
3456 ire->ire_metrics = ire_if->ire_metrics;
3458 nire = ire_add(ire);
3459 if (nire == NULL) /* Some failure */
3460 return (NULL);
3462 if (generationp != NULL)
3463 *generationp = nire->ire_generation;
3465 return (nire);
3469 * The argument is an IRE_INTERFACE. Delete all of IRE_IF_CLONE in the
3470 * ire_dep_children (just walk the ire_dep_sib_next since they are all
3471 * immediate children.)
3472 * Since we hold a lock while we remove them we need to defer the actual
3473 * calls to ire_delete() until we have dropped the lock. This makes things
3474 * less efficient since we restart at the top after dropping the lock. But
3475 * we only run when an IRE_INTERFACE is deleted which is infrquent.
3477 * Note that ire_dep_children can be any mixture of offlink routes and
3478 * IRE_IF_CLONE entries.
3480 void
3481 ire_dep_delete_if_clone(ire_t *parent)
3483 ip_stack_t *ipst = parent->ire_ipst;
3484 ire_t *child, *next;
3486 restart:
3487 rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
3488 if (parent->ire_dep_children == NULL) {
3489 rw_exit(&ipst->ips_ire_dep_lock);
3490 return;
3492 child = parent->ire_dep_children;
3493 while (child != NULL) {
3494 next = child->ire_dep_sib_next;
3495 if ((child->ire_type & IRE_IF_CLONE) &&
3496 !IRE_IS_CONDEMNED(child)) {
3497 ire_refhold(child);
3498 rw_exit(&ipst->ips_ire_dep_lock);
3499 ire_delete(child);
3500 ASSERT(IRE_IS_CONDEMNED(child));
3501 ire_refrele(child);
3502 goto restart;
3504 child = next;
3506 rw_exit(&ipst->ips_ire_dep_lock);
3510 * In the preferred/strict src multihoming modes, unbound routes (i.e.,
3511 * ire_t entries with ire_unbound set to B_TRUE) are bound to an interface
3512 * by selecting the first available interface that has an interface route for
3513 * the ire_gateway. If that interface is subsequently brought down, ill_downi()
3514 * will call ire_rebind() so that the unbound route can be bound to some other
3515 * matching interface thereby preserving the intended reachability information
3516 * from the original unbound route.
3518 void
3519 ire_rebind(ire_t *ire)
3521 ire_t *gw_ire, *new_ire;
3522 int match_flags = MATCH_IRE_TYPE;
3523 ill_t *gw_ill;
3524 boolean_t isv6 = (ire->ire_ipversion == IPV6_VERSION);
3525 ip_stack_t *ipst = ire->ire_ipst;
3527 ASSERT(ire->ire_unbound);
3528 again:
3529 if (isv6) {
3530 gw_ire = ire_ftable_lookup_v6(&ire->ire_gateway_addr_v6, 0, 0,
3531 IRE_INTERFACE, NULL, ALL_ZONES, match_flags, 0, ipst,
3532 NULL);
3533 } else {
3534 gw_ire = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
3535 IRE_INTERFACE, NULL, ALL_ZONES, match_flags, 0, ipst,
3536 NULL);
3538 if (gw_ire == NULL) {
3539 /* see comments in ip_rt_add[_v6]() for IPMP */
3540 if (match_flags & MATCH_IRE_TESTHIDDEN)
3541 return;
3543 match_flags |= MATCH_IRE_TESTHIDDEN;
3544 goto again;
3546 gw_ill = gw_ire->ire_ill;
3547 if (isv6) {
3548 new_ire = ire_create_v6(&ire->ire_addr_v6, &ire->ire_mask_v6,
3549 &ire->ire_gateway_addr_v6, ire->ire_type, gw_ill,
3550 ire->ire_zoneid, ire->ire_flags, ipst);
3551 } else {
3552 new_ire = ire_create((uchar_t *)&ire->ire_addr,
3553 (uchar_t *)&ire->ire_mask,
3554 (uchar_t *)&ire->ire_gateway_addr, ire->ire_type, gw_ill,
3555 ire->ire_zoneid, ire->ire_flags, ipst);
3557 ire_refrele(gw_ire);
3558 if (new_ire == NULL)
3559 return;
3560 new_ire->ire_unbound = B_TRUE;
3561 new_ire = ire_add(new_ire);
3562 if (new_ire != NULL)
3563 ire_refrele(new_ire);