kill tsol ("Trusted Solaris") aka TX ("Trusted Extensions")
[unleashed.git] / kernel / net / ip / ip_ire.c
blob992b4e1c0b8ff0fa2b10d325cbfa4fd61f36b8d8
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 != NULL)
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;
576 } else if (ire->ire_flags & RTF_MULTIRT) {
577 ire->ire_postfragfn = ip_postfrag_multirt_v4;
578 ire->ire_sendfn = ire_send_multirt_v4;
579 /* Multirt receive of broadcast uses ire_recv_broadcast_v4 */
580 if (ire->ire_type != IRE_BROADCAST)
581 ire->ire_recvfn = ire_recv_multirt_v4;
583 ire->ire_nce_capable = ire_determine_nce_capable(ire);
584 return (0);
588 * Determine ire_nce_capable
590 boolean_t
591 ire_determine_nce_capable(ire_t *ire)
593 int max_masklen;
595 if ((ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) ||
596 (ire->ire_type & IRE_MULTICAST))
597 return (B_TRUE);
599 if (ire->ire_ipversion == IPV4_VERSION)
600 max_masklen = IPV4_ABITS;
601 else
602 max_masklen = IPV6_ABITS;
604 if ((ire->ire_type & IRE_ONLINK) && ire->ire_masklen == max_masklen)
605 return (B_TRUE);
606 return (B_FALSE);
610 * ire_create is called to allocate and initialize a new IRE.
612 * NOTE : This is called as writer sometimes though not required
613 * by this function.
615 ire_t *
616 ire_create(uchar_t *addr, uchar_t *mask, uchar_t *gateway,
617 ushort_t type, ill_t *ill, zoneid_t zoneid, uint_t flags,
618 ip_stack_t *ipst)
620 ire_t *ire;
621 int error;
623 ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
624 if (ire == NULL) {
625 DTRACE_PROBE(kmem__cache__alloc);
626 return (NULL);
628 *ire = ire_null;
630 error = ire_init_v4(ire, addr, mask, gateway, type, ill, zoneid, flags,
631 ipst);
632 if (error != 0) {
633 DTRACE_PROBE2(ire__init, ire_t *, ire, int, error);
634 kmem_cache_free(ire_cache, ire);
635 return (NULL);
637 return (ire);
641 * Common to IPv4 and IPv6
642 * Returns zero or errno.
645 ire_init_common(ire_t *ire, ushort_t type, ill_t *ill, zoneid_t zoneid,
646 uint_t flags, uchar_t ipversion, ip_stack_t *ipst)
648 int error;
650 #ifdef DEBUG
651 if (ill != NULL) {
652 if (ill->ill_isv6)
653 ASSERT(ipversion == IPV6_VERSION);
654 else
655 ASSERT(ipversion == IPV4_VERSION);
657 #endif /* DEBUG */
659 ire->ire_type = type;
660 ire->ire_flags = RTF_UP | flags;
661 ire->ire_create_time = (uint32_t)gethrestime_sec();
662 ire->ire_generation = IRE_GENERATION_INITIAL;
665 * The ill_ire_cnt isn't increased until
666 * the IRE is added to ensure that a walker will find
667 * all IREs that hold a reference on an ill.
669 * Note that ill_ire_multicast doesn't hold a ref on the ill since
670 * ire_add() is not called for the IRE_MULTICAST.
672 ire->ire_ill = ill;
673 ire->ire_zoneid = zoneid;
674 ire->ire_ipversion = ipversion;
676 mutex_init(&ire->ire_lock, NULL, MUTEX_DEFAULT, NULL);
677 ire->ire_refcnt = 1;
678 ire->ire_identical_ref = 1; /* Number of ire_delete's needed */
679 ire->ire_ipst = ipst; /* No netstack_hold */
680 ire->ire_trace_disable = B_FALSE;
682 return (0);
686 * This creates an IRE_BROADCAST based on the arguments.
687 * A mirror is ire_lookup_bcast().
689 * Any supression of unneeded ones is done in ire_add_v4.
690 * We add one IRE_BROADCAST per address. ire_send_broadcast_v4()
691 * takes care of generating a loopback copy of the packet.
693 ire_t **
694 ire_create_bcast(ill_t *ill, ipaddr_t addr, zoneid_t zoneid, ire_t **irep)
696 ip_stack_t *ipst = ill->ill_ipst;
698 ASSERT(IAM_WRITER_ILL(ill));
700 *irep++ = ire_create(
701 (uchar_t *)&addr, /* dest addr */
702 (uchar_t *)&ip_g_all_ones, /* mask */
703 NULL, /* no gateway */
704 IRE_BROADCAST,
705 ill,
706 zoneid,
707 RTF_KERNEL,
708 ipst);
710 return (irep);
714 * This looks up an IRE_BROADCAST based on the arguments.
715 * Mirrors ire_create_bcast().
717 ire_t *
718 ire_lookup_bcast(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
720 ire_t *ire;
721 int match_args;
723 match_args = MATCH_IRE_TYPE | MATCH_IRE_ILL | MATCH_IRE_GW |
724 MATCH_IRE_MASK | MATCH_IRE_ZONEONLY;
726 if (IS_UNDER_IPMP(ill))
727 match_args |= MATCH_IRE_TESTHIDDEN;
729 ire = ire_ftable_lookup_v4(
730 addr, /* dest addr */
731 ip_g_all_ones, /* mask */
732 0, /* no gateway */
733 IRE_BROADCAST,
734 ill,
735 zoneid,
736 match_args,
738 ill->ill_ipst,
739 NULL);
740 return (ire);
743 /* Arrange to call the specified function for every IRE in the world. */
744 void
745 ire_walk(pfv_t func, void *arg, ip_stack_t *ipst)
747 ire_walk_ipvers(func, arg, 0, ALL_ZONES, ipst);
750 void
751 ire_walk_v4(pfv_t func, void *arg, zoneid_t zoneid, ip_stack_t *ipst)
753 ire_walk_ipvers(func, arg, IPV4_VERSION, zoneid, ipst);
756 void
757 ire_walk_v6(pfv_t func, void *arg, zoneid_t zoneid, ip_stack_t *ipst)
759 ire_walk_ipvers(func, arg, IPV6_VERSION, zoneid, ipst);
763 * Walk a particular version. version == 0 means both v4 and v6.
765 static void
766 ire_walk_ipvers(pfv_t func, void *arg, uchar_t vers, zoneid_t zoneid,
767 ip_stack_t *ipst)
769 if (vers != IPV6_VERSION) {
771 * ip_forwarding_table variable doesn't matter for IPv4 since
772 * ire_walk_ill_tables uses ips_ip_ftable for IPv4.
774 ire_walk_ill_tables(0, 0, func, arg, IP_MASK_TABLE_SIZE,
775 0, NULL,
776 NULL, zoneid, ipst);
778 if (vers != IPV4_VERSION) {
779 ire_walk_ill_tables(0, 0, func, arg, IP6_MASK_TABLE_SIZE,
780 ipst->ips_ip6_ftable_hash_size,
781 ipst->ips_ip_forwarding_table_v6,
782 NULL, zoneid, ipst);
787 * Arrange to call the specified function for every IRE that matches the ill.
789 void
790 ire_walk_ill(uint_t match_flags, uint_t ire_type, pfv_t func, void *arg,
791 ill_t *ill)
793 uchar_t vers = (ill->ill_isv6 ? IPV6_VERSION : IPV4_VERSION);
795 ire_walk_ill_ipvers(match_flags, ire_type, func, arg, vers, ill);
799 * Walk a particular ill and version.
801 static void
802 ire_walk_ill_ipvers(uint_t match_flags, uint_t ire_type, pfv_t func,
803 void *arg, uchar_t vers, ill_t *ill)
805 ip_stack_t *ipst = ill->ill_ipst;
807 if (vers == IPV4_VERSION) {
808 ire_walk_ill_tables(match_flags, ire_type, func, arg,
809 IP_MASK_TABLE_SIZE,
810 0, NULL,
811 ill, ALL_ZONES, ipst);
813 if (vers != IPV4_VERSION) {
814 ire_walk_ill_tables(match_flags, ire_type, func, arg,
815 IP6_MASK_TABLE_SIZE, ipst->ips_ip6_ftable_hash_size,
816 ipst->ips_ip_forwarding_table_v6,
817 ill, ALL_ZONES, ipst);
822 * Do the specific matching of IREs to shared-IP zones.
824 * We have the same logic as in ire_match_args but implemented slightly
825 * differently.
827 boolean_t
828 ire_walk_ill_match(uint_t match_flags, uint_t ire_type, ire_t *ire,
829 ill_t *ill, zoneid_t zoneid, ip_stack_t *ipst)
831 ill_t *dst_ill = ire->ire_ill;
833 ASSERT(match_flags != 0 || zoneid != ALL_ZONES);
835 if (zoneid != ALL_ZONES && zoneid != ire->ire_zoneid &&
836 ire->ire_zoneid != ALL_ZONES) {
838 * We're walking the IREs for a specific zone. The only relevant
839 * IREs are:
840 * - all IREs with a matching ire_zoneid
841 * - IRE_IF_ALL IREs for interfaces with a usable source addr
842 * with a matching zone
843 * - IRE_OFFLINK with a gateway reachable from the zone
844 * Note that ealier we only did the IRE_OFFLINK check for
845 * IRE_DEFAULT (and only when we had multiple IRE_DEFAULTs).
847 if (ire->ire_type & IRE_ONLINK) {
848 uint_t ifindex;
851 * Note there is no IRE_INTERFACE on vniN thus
852 * can't do an IRE lookup for a matching route.
854 ifindex = dst_ill->ill_usesrc_ifindex;
855 if (ifindex == 0)
856 return (B_FALSE);
859 * If there is a usable source address in the
860 * zone, then it's ok to return an
861 * IRE_INTERFACE
863 if (!ipif_zone_avail(ifindex, dst_ill->ill_isv6,
864 zoneid, ipst)) {
865 return (B_FALSE);
868 if (dst_ill != NULL && (ire->ire_type & IRE_OFFLINK)) {
869 ipif_t *tipif;
871 mutex_enter(&dst_ill->ill_lock);
872 for (tipif = dst_ill->ill_ipif;
873 tipif != NULL; tipif = tipif->ipif_next) {
874 if (!IPIF_IS_CONDEMNED(tipif) &&
875 (tipif->ipif_flags & IPIF_UP) &&
876 (tipif->ipif_zoneid == zoneid ||
877 tipif->ipif_zoneid == ALL_ZONES))
878 break;
880 mutex_exit(&dst_ill->ill_lock);
881 if (tipif == NULL) {
882 return (B_FALSE);
887 * Except for ALL_ZONES, we only match the offlink routes
888 * where ire_gateway_addr has an IRE_INTERFACE for the zoneid.
889 * Since we can have leftover routes after the IP addresses have
890 * changed, the global zone will also match offlink routes where the
891 * gateway is unreachable from any zone.
893 if ((ire->ire_type & IRE_OFFLINK) && zoneid != ALL_ZONES) {
894 in6_addr_t gw_addr_v6;
895 boolean_t reach;
897 if (ire->ire_ipversion == IPV4_VERSION) {
898 reach = ire_gateway_ok_zone_v4(ire->ire_gateway_addr,
899 zoneid, dst_ill, ipst, B_FALSE);
900 } else {
901 ASSERT(ire->ire_ipversion == IPV6_VERSION);
902 mutex_enter(&ire->ire_lock);
903 gw_addr_v6 = ire->ire_gateway_addr_v6;
904 mutex_exit(&ire->ire_lock);
906 reach = ire_gateway_ok_zone_v6(&gw_addr_v6, zoneid,
907 dst_ill, ipst, B_FALSE);
909 if (!reach) {
910 if (zoneid != GLOBAL_ZONEID)
911 return (B_FALSE);
914 * Check if ALL_ZONES reachable - if not then let the
915 * global zone see it.
917 if (ire->ire_ipversion == IPV4_VERSION) {
918 reach = ire_gateway_ok_zone_v4(
919 ire->ire_gateway_addr, ALL_ZONES,
920 dst_ill, ipst, B_FALSE);
921 } else {
922 reach = ire_gateway_ok_zone_v6(&gw_addr_v6,
923 ALL_ZONES, dst_ill, ipst, B_FALSE);
925 if (reach) {
927 * Some other zone could see it, hence hide it
928 * in the global zone.
930 return (B_FALSE);
935 if (((!(match_flags & MATCH_IRE_TYPE)) ||
936 (ire->ire_type & ire_type)) &&
937 ((!(match_flags & MATCH_IRE_ILL)) ||
938 (dst_ill == ill ||
939 dst_ill != NULL && IS_IN_SAME_ILLGRP(dst_ill, ill)))) {
940 return (B_TRUE);
942 return (B_FALSE);
946 rtfunc(struct radix_node *rn, void *arg)
948 struct rtfuncarg *rtf = arg;
949 struct rt_entry *rt;
950 irb_t *irb;
951 ire_t *ire;
952 boolean_t ret;
954 rt = (struct rt_entry *)rn;
955 ASSERT(rt != NULL);
956 irb = &rt->rt_irb;
957 for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
958 if ((rtf->rt_match_flags != 0) ||
959 (rtf->rt_zoneid != ALL_ZONES)) {
960 ret = ire_walk_ill_match(rtf->rt_match_flags,
961 rtf->rt_ire_type, ire,
962 rtf->rt_ill, rtf->rt_zoneid, rtf->rt_ipst);
963 } else {
964 ret = B_TRUE;
966 if (ret)
967 (*rtf->rt_func)(ire, rtf->rt_arg);
969 return (0);
973 * Walk the ftable entries that match the ill.
975 void
976 ire_walk_ill_tables(uint_t match_flags, uint_t ire_type, pfv_t func,
977 void *arg, size_t ftbl_sz, size_t htbl_sz, irb_t **ipftbl,
978 ill_t *ill, zoneid_t zoneid,
979 ip_stack_t *ipst)
981 irb_t *irb_ptr;
982 irb_t *irb;
983 ire_t *ire;
984 int i, j;
985 boolean_t ret;
986 struct rtfuncarg rtfarg;
988 ASSERT((!(match_flags & MATCH_IRE_ILL)) || (ill != NULL));
989 ASSERT(!(match_flags & MATCH_IRE_TYPE) || (ire_type != 0));
991 /* knobs such that routine is called only for v6 case */
992 if (ipftbl == ipst->ips_ip_forwarding_table_v6) {
993 for (i = (ftbl_sz - 1); i >= 0; i--) {
994 if ((irb_ptr = ipftbl[i]) == NULL)
995 continue;
996 for (j = 0; j < htbl_sz; j++) {
997 irb = &irb_ptr[j];
998 if (irb->irb_ire == NULL)
999 continue;
1001 irb_refhold(irb);
1002 for (ire = irb->irb_ire; ire != NULL;
1003 ire = ire->ire_next) {
1004 if (match_flags == 0 &&
1005 zoneid == ALL_ZONES) {
1006 ret = B_TRUE;
1007 } else {
1008 ret =
1009 ire_walk_ill_match(
1010 match_flags,
1011 ire_type, ire, ill,
1012 zoneid, ipst);
1014 if (ret)
1015 (*func)(ire, arg);
1017 irb_refrele(irb);
1020 } else {
1021 bzero(&rtfarg, sizeof (rtfarg));
1022 rtfarg.rt_func = func;
1023 rtfarg.rt_arg = arg;
1024 if (match_flags != 0) {
1025 rtfarg.rt_match_flags = match_flags;
1027 rtfarg.rt_ire_type = ire_type;
1028 rtfarg.rt_ill = ill;
1029 rtfarg.rt_zoneid = zoneid;
1030 rtfarg.rt_ipst = ipst; /* No netstack_hold */
1031 (void) ipst->ips_ip_ftable->rnh_walktree_mt(
1032 ipst->ips_ip_ftable,
1033 rtfunc, &rtfarg, irb_refhold_rn, irb_refrele_rn);
1038 * This function takes a mask and returns
1039 * number of bits set in the mask. If no
1040 * bit is set it returns 0.
1041 * Assumes a contiguous mask.
1044 ip_mask_to_plen(ipaddr_t mask)
1046 return (mask == 0 ? 0 : IP_ABITS - (ffs(ntohl(mask)) -1));
1050 * Convert length for a mask to the mask.
1052 ipaddr_t
1053 ip_plen_to_mask(uint_t masklen)
1055 if (masklen == 0)
1056 return (0);
1058 return (htonl(IP_HOST_MASK << (IP_ABITS - masklen)));
1061 void
1062 ire_atomic_end(irb_t *irb_ptr, ire_t *ire)
1064 ill_t *ill;
1066 ill = ire->ire_ill;
1067 if (ill != NULL)
1068 mutex_exit(&ill->ill_lock);
1069 rw_exit(&irb_ptr->irb_lock);
1073 * ire_add_v[46] atomically make sure that the ill associated
1074 * with the new ire is not going away i.e., we check ILL_CONDEMNED.
1077 ire_atomic_start(irb_t *irb_ptr, ire_t *ire)
1079 ill_t *ill;
1081 ill = ire->ire_ill;
1083 rw_enter(&irb_ptr->irb_lock, RW_WRITER);
1084 if (ill != NULL) {
1085 mutex_enter(&ill->ill_lock);
1088 * Don't allow IRE's to be created on dying ills, or on
1089 * ill's for which the last ipif is going down, or ones which
1090 * don't have even a single UP interface
1092 if ((ill->ill_state_flags &
1093 (ILL_CONDEMNED|ILL_DOWN_IN_PROGRESS)) != 0) {
1094 ire_atomic_end(irb_ptr, ire);
1095 DTRACE_PROBE1(ire__add__on__dying__ill, ire_t *, ire);
1096 return (ENXIO);
1099 if (IS_UNDER_IPMP(ill)) {
1100 int error = 0;
1101 mutex_enter(&ill->ill_phyint->phyint_lock);
1102 if (!ipmp_ill_is_active(ill) &&
1103 IRE_HIDDEN_TYPE(ire->ire_type) &&
1104 !ire->ire_testhidden) {
1105 error = EINVAL;
1107 mutex_exit(&ill->ill_phyint->phyint_lock);
1108 if (error != 0) {
1109 ire_atomic_end(irb_ptr, ire);
1110 return (error);
1115 return (0);
1119 * Add a fully initialized IRE to the forwarding table.
1120 * This returns NULL on failure, or a held IRE on success.
1121 * Normally the returned IRE is the same as the argument. But a different
1122 * IRE will be returned if the added IRE is deemed identical to an existing
1123 * one. In that case ire_identical_ref will be increased.
1124 * The caller always needs to do an ire_refrele() on the returned IRE.
1126 ire_t *
1127 ire_add(ire_t *ire)
1129 if (IRE_HIDDEN_TYPE(ire->ire_type) &&
1130 ire->ire_ill != NULL && IS_UNDER_IPMP(ire->ire_ill)) {
1132 * IREs hosted on interfaces that are under IPMP
1133 * should be hidden so that applications don't
1134 * accidentally end up sending packets with test
1135 * addresses as their source addresses, or
1136 * sending out interfaces that are e.g. IFF_INACTIVE.
1137 * Hide them here.
1139 ire->ire_testhidden = B_TRUE;
1142 if (ire->ire_ipversion == IPV6_VERSION)
1143 return (ire_add_v6(ire));
1144 else
1145 return (ire_add_v4(ire));
1149 * Add a fully initialized IPv4 IRE to the forwarding table.
1150 * This returns NULL on failure, or a held IRE on success.
1151 * Normally the returned IRE is the same as the argument. But a different
1152 * IRE will be returned if the added IRE is deemed identical to an existing
1153 * one. In that case ire_identical_ref will be increased.
1154 * The caller always needs to do an ire_refrele() on the returned IRE.
1156 static ire_t *
1157 ire_add_v4(ire_t *ire)
1159 ire_t *ire1;
1160 irb_t *irb_ptr;
1161 ire_t **irep;
1162 int match_flags;
1163 int error;
1164 ip_stack_t *ipst = ire->ire_ipst;
1166 if (ire->ire_ill != NULL)
1167 ASSERT(!MUTEX_HELD(&ire->ire_ill->ill_lock));
1168 ASSERT(ire->ire_ipversion == IPV4_VERSION);
1170 /* Make sure the address is properly masked. */
1171 ire->ire_addr &= ire->ire_mask;
1173 match_flags = (MATCH_IRE_MASK | MATCH_IRE_TYPE | MATCH_IRE_GW);
1175 if (ire->ire_ill != NULL) {
1176 match_flags |= MATCH_IRE_ILL;
1178 irb_ptr = ire_get_bucket(ire);
1179 if (irb_ptr == NULL) {
1180 printf("no bucket for %p\n", (void *)ire);
1181 ire_delete(ire);
1182 return (NULL);
1186 * Start the atomic add of the ire. Grab the ill lock,
1187 * the bucket lock. Check for condemned.
1189 error = ire_atomic_start(irb_ptr, ire);
1190 if (error != 0) {
1191 printf("no ire_atomic_start for %p\n", (void *)ire);
1192 ire_delete(ire);
1193 irb_refrele(irb_ptr);
1194 return (NULL);
1197 * If we are creating a hidden IRE, make sure we search for
1198 * hidden IREs when searching for duplicates below.
1199 * Otherwise, we might find an IRE on some other interface
1200 * that's not marked hidden.
1202 if (ire->ire_testhidden)
1203 match_flags |= MATCH_IRE_TESTHIDDEN;
1206 * Atomically check for duplicate and insert in the table.
1208 for (ire1 = irb_ptr->irb_ire; ire1 != NULL; ire1 = ire1->ire_next) {
1209 if (IRE_IS_CONDEMNED(ire1))
1210 continue;
1212 * Here we need an exact match on zoneid, i.e.,
1213 * ire_match_args doesn't fit.
1215 if (ire1->ire_zoneid != ire->ire_zoneid)
1216 continue;
1218 if (ire1->ire_type != ire->ire_type)
1219 continue;
1222 * Note: We do not allow multiple routes that differ only
1223 * in the gateway security attributes; such routes are
1224 * considered duplicates.
1225 * To change that we explicitly have to treat them as
1226 * different here.
1228 if (ire_match_args(ire1, ire->ire_addr, ire->ire_mask,
1229 ire->ire_gateway_addr, ire->ire_type, ire->ire_ill,
1230 ire->ire_zoneid, match_flags)) {
1232 * Return the old ire after doing a REFHOLD.
1233 * As most of the callers continue to use the IRE
1234 * after adding, we return a held ire. This will
1235 * avoid a lookup in the caller again. If the callers
1236 * don't want to use it, they need to do a REFRELE.
1238 * We only allow exactly one IRE_IF_CLONE for any dst,
1239 * so, if the is an IF_CLONE, return the ire without
1240 * an identical_ref, but with an ire_ref held.
1242 if (ire->ire_type != IRE_IF_CLONE) {
1243 atomic_inc_32(&ire1->ire_identical_ref);
1244 DTRACE_PROBE2(ire__add__exist, ire_t *, ire1,
1245 ire_t *, ire);
1247 ire_refhold(ire1);
1248 ire_atomic_end(irb_ptr, ire);
1249 ire_delete(ire);
1250 irb_refrele(irb_ptr);
1251 return (ire1);
1256 * Normally we do head insertion since most things do not care about
1257 * the order of the IREs in the bucket. Note that ip_cgtp_bcast_add
1258 * assumes we at least do head insertion so that its IRE_BROADCAST
1259 * arrive ahead of existing IRE_HOST for the same address.
1260 * However, due to shared-IP zones (and restrict_interzone_loopback)
1261 * we can have an IRE_LOCAL as well as IRE_IF_CLONE for the same
1262 * address. For that reason we do tail insertion for IRE_IF_CLONE.
1263 * Due to the IRE_BROADCAST on cgtp0, which must be last in the bucket,
1264 * we do tail insertion of IRE_BROADCASTs that do not have RTF_MULTIRT
1265 * set.
1267 irep = (ire_t **)irb_ptr;
1268 if ((ire->ire_type & IRE_IF_CLONE) ||
1269 ((ire->ire_type & IRE_BROADCAST) &&
1270 !(ire->ire_flags & RTF_MULTIRT))) {
1271 while ((ire1 = *irep) != NULL)
1272 irep = &ire1->ire_next;
1274 /* Insert at *irep */
1275 ire1 = *irep;
1276 if (ire1 != NULL)
1277 ire1->ire_ptpn = &ire->ire_next;
1278 ire->ire_next = ire1;
1279 /* Link the new one in. */
1280 ire->ire_ptpn = irep;
1283 * ire_walk routines de-reference ire_next without holding
1284 * a lock. Before we point to the new ire, we want to make
1285 * sure the store that sets the ire_next of the new ire
1286 * reaches global visibility, so that ire_walk routines
1287 * don't see a truncated list of ires i.e if the ire_next
1288 * of the new ire gets set after we do "*irep = ire" due
1289 * to re-ordering, the ire_walk thread will see a NULL
1290 * once it accesses the ire_next of the new ire.
1291 * membar_producer() makes sure that the following store
1292 * happens *after* all of the above stores.
1294 membar_producer();
1295 *irep = ire;
1296 ire->ire_bucket = irb_ptr;
1298 * We return a bumped up IRE above. Keep it symmetrical
1299 * so that the callers will always have to release. This
1300 * helps the callers of this function because they continue
1301 * to use the IRE after adding and hence they don't have to
1302 * lookup again after we return the IRE.
1304 * NOTE : We don't have to use atomics as this is appearing
1305 * in the list for the first time and no one else can bump
1306 * up the reference count on this yet.
1308 ire_refhold_locked(ire);
1309 BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted);
1311 irb_ptr->irb_ire_cnt++;
1312 if (irb_ptr->irb_marks & IRB_MARK_DYNAMIC)
1313 irb_ptr->irb_nire++;
1315 if (ire->ire_ill != NULL) {
1316 ire->ire_ill->ill_ire_cnt++;
1317 ASSERT(ire->ire_ill->ill_ire_cnt != 0); /* Wraparound */
1320 ire_atomic_end(irb_ptr, ire);
1322 /* Make any caching of the IREs be notified or updated */
1323 ire_flush_cache_v4(ire, IRE_FLUSH_ADD);
1325 if (ire->ire_ill != NULL)
1326 ASSERT(!MUTEX_HELD(&ire->ire_ill->ill_lock));
1327 irb_refrele(irb_ptr);
1328 return (ire);
1332 * irb_refrele is the only caller of the function. ire_unlink calls to
1333 * do the final cleanup for this ire.
1335 void
1336 ire_cleanup(ire_t *ire)
1338 ire_t *ire_next;
1339 ip_stack_t *ipst = ire->ire_ipst;
1341 ASSERT(ire != NULL);
1343 while (ire != NULL) {
1344 ire_next = ire->ire_next;
1345 if (ire->ire_ipversion == IPV4_VERSION) {
1346 ire_delete_v4(ire);
1347 BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
1348 ire_stats_deleted);
1349 } else {
1350 ASSERT(ire->ire_ipversion == IPV6_VERSION);
1351 ire_delete_v6(ire);
1352 BUMP_IRE_STATS(ipst->ips_ire_stats_v6,
1353 ire_stats_deleted);
1356 * Now it's really out of the list. Before doing the
1357 * REFRELE, set ire_next to NULL as ire_inactive asserts
1358 * so.
1360 ire->ire_next = NULL;
1361 ire_refrele_notr(ire);
1362 ire = ire_next;
1367 * irb_refrele is the only caller of the function. It calls to unlink
1368 * all the CONDEMNED ires from this bucket.
1370 ire_t *
1371 ire_unlink(irb_t *irb)
1373 ire_t *ire;
1374 ire_t *ire1;
1375 ire_t **ptpn;
1376 ire_t *ire_list = NULL;
1378 ASSERT(RW_WRITE_HELD(&irb->irb_lock));
1379 ASSERT(((irb->irb_marks & IRB_MARK_DYNAMIC) && irb->irb_refcnt == 1) ||
1380 (irb->irb_refcnt == 0));
1381 ASSERT(irb->irb_marks & IRB_MARK_CONDEMNED);
1382 ASSERT(irb->irb_ire != NULL);
1384 for (ire = irb->irb_ire; ire != NULL; ire = ire1) {
1385 ire1 = ire->ire_next;
1386 if (IRE_IS_CONDEMNED(ire)) {
1387 ptpn = ire->ire_ptpn;
1388 ire1 = ire->ire_next;
1389 if (ire1)
1390 ire1->ire_ptpn = ptpn;
1391 *ptpn = ire1;
1392 ire->ire_ptpn = NULL;
1393 ire->ire_next = NULL;
1396 * We need to call ire_delete_v4 or ire_delete_v6 to
1397 * clean up dependents and the redirects pointing at
1398 * the default gateway. We need to drop the lock
1399 * as ire_flush_cache/ire_delete_host_redircts require
1400 * so. But we can't drop the lock, as ire_unlink needs
1401 * to atomically remove the ires from the list.
1402 * So, create a temporary list of CONDEMNED ires
1403 * for doing ire_delete_v4/ire_delete_v6 operations
1404 * later on.
1406 ire->ire_next = ire_list;
1407 ire_list = ire;
1410 irb->irb_marks &= ~IRB_MARK_CONDEMNED;
1411 return (ire_list);
1415 * Clean up the radix node for this ire. Must be called by irb_refrele
1416 * when there are no ire's left in the bucket. Returns TRUE if the bucket
1417 * is deleted and freed.
1419 boolean_t
1420 irb_inactive(irb_t *irb)
1422 struct rt_entry *rt;
1423 struct radix_node *rn;
1424 ip_stack_t *ipst = irb->irb_ipst;
1426 ASSERT(irb->irb_ipst != NULL);
1428 rt = IRB2RT(irb);
1429 rn = (struct radix_node *)rt;
1431 /* first remove it from the radix tree. */
1432 RADIX_NODE_HEAD_WLOCK(ipst->ips_ip_ftable);
1433 rw_enter(&irb->irb_lock, RW_WRITER);
1434 if (irb->irb_refcnt == 1 && irb->irb_nire == 0) {
1435 rn = ipst->ips_ip_ftable->rnh_deladdr(rn->rn_key, rn->rn_mask,
1436 ipst->ips_ip_ftable);
1437 DTRACE_PROBE1(irb__free, rt_t *, rt);
1438 ASSERT((void *)rn == (void *)rt);
1439 Free(rt, rt_entry_cache);
1440 /* irb_lock is freed */
1441 RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
1442 return (B_TRUE);
1444 rw_exit(&irb->irb_lock);
1445 RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
1446 return (B_FALSE);
1450 * Delete the specified IRE.
1451 * We assume that if ire_bucket is not set then ire_ill->ill_ire_cnt was
1452 * not incremented i.e., that the insertion in the bucket and the increment
1453 * of that counter is done atomically.
1455 void
1456 ire_delete(ire_t *ire)
1458 ire_t *ire1;
1459 ire_t **ptpn;
1460 irb_t *irb;
1461 ip_stack_t *ipst = ire->ire_ipst;
1463 if ((irb = ire->ire_bucket) == NULL) {
1465 * It was never inserted in the list. Should call REFRELE
1466 * to free this IRE.
1468 ire_make_condemned(ire);
1469 ire_refrele_notr(ire);
1470 return;
1474 * Move the use counts from an IRE_IF_CLONE to its parent
1475 * IRE_INTERFACE.
1476 * We need to do this before acquiring irb_lock.
1478 if (ire->ire_type & IRE_IF_CLONE) {
1479 ire_t *parent;
1481 rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
1482 if ((parent = ire->ire_dep_parent) != NULL) {
1483 parent->ire_ob_pkt_count += ire->ire_ob_pkt_count;
1484 parent->ire_ib_pkt_count += ire->ire_ib_pkt_count;
1485 ire->ire_ob_pkt_count = 0;
1486 ire->ire_ib_pkt_count = 0;
1488 rw_exit(&ipst->ips_ire_dep_lock);
1491 rw_enter(&irb->irb_lock, RW_WRITER);
1492 if (ire->ire_ptpn == NULL) {
1494 * Some other thread has removed us from the list.
1495 * It should have done the REFRELE for us.
1497 rw_exit(&irb->irb_lock);
1498 return;
1501 if (!IRE_IS_CONDEMNED(ire)) {
1502 /* Is this an IRE representing multiple duplicate entries? */
1503 ASSERT(ire->ire_identical_ref >= 1);
1504 if (atomic_dec_32_nv(&ire->ire_identical_ref) != 0) {
1505 /* Removed one of the identical parties */
1506 rw_exit(&irb->irb_lock);
1507 return;
1510 irb->irb_ire_cnt--;
1511 ire_make_condemned(ire);
1514 if (irb->irb_refcnt != 0) {
1516 * The last thread to leave this bucket will
1517 * delete this ire.
1519 irb->irb_marks |= IRB_MARK_CONDEMNED;
1520 rw_exit(&irb->irb_lock);
1521 return;
1525 * Normally to delete an ire, we walk the bucket. While we
1526 * walk the bucket, we normally bump up irb_refcnt and hence
1527 * we return from above where we mark CONDEMNED and the ire
1528 * gets deleted from ire_unlink. This case is where somebody
1529 * knows the ire e.g by doing a lookup, and wants to delete the
1530 * IRE. irb_refcnt would be 0 in this case if nobody is walking
1531 * the bucket.
1533 ptpn = ire->ire_ptpn;
1534 ire1 = ire->ire_next;
1535 if (ire1 != NULL)
1536 ire1->ire_ptpn = ptpn;
1537 ASSERT(ptpn != NULL);
1538 *ptpn = ire1;
1539 ire->ire_ptpn = NULL;
1540 ire->ire_next = NULL;
1541 if (ire->ire_ipversion == IPV6_VERSION) {
1542 BUMP_IRE_STATS(ipst->ips_ire_stats_v6, ire_stats_deleted);
1543 } else {
1544 BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_deleted);
1546 rw_exit(&irb->irb_lock);
1548 /* Cleanup dependents and related stuff */
1549 if (ire->ire_ipversion == IPV6_VERSION) {
1550 ire_delete_v6(ire);
1551 } else {
1552 ire_delete_v4(ire);
1555 * We removed it from the list. Decrement the
1556 * reference count.
1558 ire_refrele_notr(ire);
1562 * Delete the specified IRE.
1563 * All calls should use ire_delete().
1564 * Sometimes called as writer though not required by this function.
1566 * NOTE : This function is called only if the ire was added
1567 * in the list.
1569 static void
1570 ire_delete_v4(ire_t *ire)
1572 ip_stack_t *ipst = ire->ire_ipst;
1574 ASSERT(ire->ire_refcnt >= 1);
1575 ASSERT(ire->ire_ipversion == IPV4_VERSION);
1577 ire_flush_cache_v4(ire, IRE_FLUSH_DELETE);
1578 if (ire->ire_type == IRE_DEFAULT) {
1580 * when a default gateway is going away
1581 * delete all the host redirects pointing at that
1582 * gateway.
1584 ire_delete_host_redirects(ire->ire_gateway_addr, ipst);
1588 * If we are deleting an IRE_INTERFACE then we make sure we also
1589 * delete any IRE_IF_CLONE that has been created from it.
1590 * Those are always in ire_dep_children.
1592 if ((ire->ire_type & IRE_INTERFACE) && ire->ire_dep_children != NULL)
1593 ire_dep_delete_if_clone(ire);
1595 /* Remove from parent dependencies and child */
1596 rw_enter(&ipst->ips_ire_dep_lock, RW_WRITER);
1597 if (ire->ire_dep_parent != NULL)
1598 ire_dep_remove(ire);
1600 while (ire->ire_dep_children != NULL)
1601 ire_dep_remove(ire->ire_dep_children);
1602 rw_exit(&ipst->ips_ire_dep_lock);
1606 * ire_refrele is the only caller of the function. It calls
1607 * to free the ire when the reference count goes to zero.
1609 void
1610 ire_inactive(ire_t *ire)
1612 ill_t *ill;
1613 irb_t *irb;
1614 ip_stack_t *ipst = ire->ire_ipst;
1616 ASSERT(ire->ire_refcnt == 0);
1617 ASSERT(ire->ire_ptpn == NULL);
1618 ASSERT(ire->ire_next == NULL);
1620 /* Count how many condemned ires for kmem_cache callback */
1621 ASSERT(IRE_IS_CONDEMNED(ire));
1622 atomic_add_32(&ipst->ips_num_ire_condemned, -1);
1625 * ire_nce_cache is cleared in ire_delete, and we make sure we don't
1626 * set it once the ire is marked condemned.
1628 ASSERT(ire->ire_nce_cache == NULL);
1631 * Since any parent would have a refhold on us they would already
1632 * have been removed.
1634 ASSERT(ire->ire_dep_parent == NULL);
1635 ASSERT(ire->ire_dep_sib_next == NULL);
1636 ASSERT(ire->ire_dep_sib_ptpn == NULL);
1639 * Since any children would have a refhold on us they should have
1640 * already been removed.
1642 ASSERT(ire->ire_dep_children == NULL);
1645 * ill_ire_ref is increased when the IRE is inserted in the
1646 * bucket - not when the IRE is created.
1648 irb = ire->ire_bucket;
1649 ill = ire->ire_ill;
1650 if (irb != NULL && ill != NULL) {
1651 mutex_enter(&ill->ill_lock);
1652 ASSERT(ill->ill_ire_cnt != 0);
1653 DTRACE_PROBE3(ill__decr__cnt, (ill_t *), ill,
1654 (char *), "ire", (void *), ire);
1655 ill->ill_ire_cnt--;
1656 if (ILL_DOWN_OK(ill)) {
1657 /* Drops the ill lock */
1658 ipif_ill_refrele_tail(ill);
1659 } else {
1660 mutex_exit(&ill->ill_lock);
1663 ire->ire_ill = NULL;
1665 /* This should be true for both V4 and V6 */
1666 if (irb != NULL && (irb->irb_marks & IRB_MARK_DYNAMIC)) {
1667 rw_enter(&irb->irb_lock, RW_WRITER);
1668 irb->irb_nire--;
1670 * Instead of examining the conditions for freeing
1671 * the radix node here, we do it by calling
1672 * irb_refrele which is a single point in the code
1673 * that embeds that logic. Bump up the refcnt to
1674 * be able to call irb_refrele
1676 irb_refhold_locked(irb);
1677 rw_exit(&irb->irb_lock);
1678 irb_refrele(irb);
1681 #ifdef DEBUG
1682 ire_trace_cleanup(ire);
1683 #endif
1684 mutex_destroy(&ire->ire_lock);
1685 if (ire->ire_ipversion == IPV6_VERSION) {
1686 BUMP_IRE_STATS(ipst->ips_ire_stats_v6, ire_stats_freed);
1687 } else {
1688 BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_freed);
1690 kmem_cache_free(ire_cache, ire);
1694 * ire_update_generation is the callback function provided by
1695 * ire_get_bucket() to update the generation number of any
1696 * matching shorter route when a new route is added.
1698 * This fucntion always returns a failure return (B_FALSE)
1699 * to force the caller (rn_matchaddr_args)
1700 * to back-track up the tree looking for shorter matches.
1702 /* ARGSUSED */
1703 static boolean_t
1704 ire_update_generation(struct radix_node *rn, void *arg)
1706 struct rt_entry *rt = (struct rt_entry *)rn;
1708 /* We need to handle all in the same bucket */
1709 irb_increment_generation(&rt->rt_irb);
1710 return (B_FALSE);
1714 * Take care of all the generation numbers in the bucket.
1716 void
1717 irb_increment_generation(irb_t *irb)
1719 ire_t *ire;
1720 ip_stack_t *ipst;
1722 if (irb == NULL || irb->irb_ire_cnt == 0)
1723 return;
1725 ipst = irb->irb_ipst;
1727 * we cannot do an irb_refhold/irb_refrele here as the caller
1728 * already has the global RADIX_NODE_HEAD_WLOCK, and the irb_refrele
1729 * may result in an attempt to free the irb_t, which also needs
1730 * the RADIX_NODE_HEAD lock. However, since we want to traverse the
1731 * irb_ire list without fear of having a condemned ire removed from
1732 * the list, we acquire the irb_lock as WRITER. Moreover, since
1733 * the ire_generation increments are done under the ire_dep_lock,
1734 * acquire the locks in the prescribed lock order first.
1736 rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
1737 rw_enter(&irb->irb_lock, RW_WRITER);
1738 for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
1739 if (!IRE_IS_CONDEMNED(ire))
1740 ire_increment_generation(ire); /* Ourselves */
1741 ire_dep_incr_generation_locked(ire); /* Dependants */
1743 rw_exit(&irb->irb_lock);
1744 rw_exit(&ipst->ips_ire_dep_lock);
1748 * When an IRE is added or deleted this routine is called to make sure
1749 * any caching of IRE information is notified or updated.
1751 * The flag argument indicates if the flush request is due to addition
1752 * of new route (IRE_FLUSH_ADD), deletion of old route (IRE_FLUSH_DELETE),
1753 * or a change to ire_gateway_addr (IRE_FLUSH_GWCHANGE).
1755 void
1756 ire_flush_cache_v4(ire_t *ire, int flag)
1758 irb_t *irb = ire->ire_bucket;
1759 struct rt_entry *rt = IRB2RT(irb);
1760 ip_stack_t *ipst = ire->ire_ipst;
1763 * IRE_IF_CLONE ire's don't provide any new information
1764 * than the parent from which they are cloned, so don't
1765 * perturb the generation numbers.
1767 if (ire->ire_type & IRE_IF_CLONE)
1768 return;
1771 * Ensure that an ire_add during a lookup serializes the updates of the
1772 * generation numbers under the radix head lock so that the lookup gets
1773 * either the old ire and old generation number, or a new ire and new
1774 * generation number.
1776 RADIX_NODE_HEAD_WLOCK(ipst->ips_ip_ftable);
1779 * If a route was just added, we need to notify everybody that
1780 * has cached an IRE_NOROUTE since there might now be a better
1781 * route for them.
1783 if (flag == IRE_FLUSH_ADD) {
1784 ire_increment_generation(ipst->ips_ire_reject_v4);
1785 ire_increment_generation(ipst->ips_ire_blackhole_v4);
1788 /* Adding a default can't otherwise provide a better route */
1789 if (ire->ire_type == IRE_DEFAULT && flag == IRE_FLUSH_ADD) {
1790 RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
1791 return;
1794 switch (flag) {
1795 case IRE_FLUSH_DELETE:
1796 case IRE_FLUSH_GWCHANGE:
1798 * Update ire_generation for all ire_dep_children chains
1799 * starting with this IRE
1801 ire_dep_incr_generation(ire);
1802 break;
1803 case IRE_FLUSH_ADD:
1805 * Update the generation numbers of all shorter matching routes.
1806 * ire_update_generation takes care of the dependants by
1807 * using ire_dep_incr_generation.
1809 (void) ipst->ips_ip_ftable->rnh_matchaddr_args(&rt->rt_dst,
1810 ipst->ips_ip_ftable, ire_update_generation, NULL);
1811 break;
1813 RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
1817 * Matches the arguments passed with the values in the ire.
1819 * Note: for match types that match using "ill" passed in, ill
1820 * must be checked for non-NULL before calling this routine.
1822 boolean_t
1823 ire_match_args(ire_t *ire, ipaddr_t addr, ipaddr_t mask, ipaddr_t gateway,
1824 int type, const ill_t *ill, zoneid_t zoneid, int match_flags)
1826 ill_t *ire_ill = NULL, *dst_ill;
1827 ip_stack_t *ipst = ire->ire_ipst;
1829 ASSERT(ire->ire_ipversion == IPV4_VERSION);
1830 ASSERT((ire->ire_addr & ~ire->ire_mask) == 0);
1831 ASSERT((!(match_flags & (MATCH_IRE_ILL|MATCH_IRE_SRC_ILL))) ||
1832 (ill != NULL && !ill->ill_isv6));
1835 * If MATCH_IRE_TESTHIDDEN is set, then only return the IRE if it is
1836 * in fact hidden, to ensure the caller gets the right one.
1838 if (ire->ire_testhidden) {
1839 if (!(match_flags & MATCH_IRE_TESTHIDDEN))
1840 return (B_FALSE);
1843 if (zoneid != ALL_ZONES && zoneid != ire->ire_zoneid &&
1844 ire->ire_zoneid != ALL_ZONES) {
1846 * If MATCH_IRE_ZONEONLY has been set and the supplied zoneid
1847 * does not match that of ire_zoneid, a failure to
1848 * match is reported at this point. Otherwise, since some IREs
1849 * that are available in the global zone can be used in local
1850 * zones, additional checks need to be performed:
1852 * IRE_LOOPBACK
1853 * entries should never be matched in this situation.
1854 * Each zone has its own IRE_LOOPBACK.
1856 * IRE_LOCAL
1857 * We allow them for any zoneid. ire_route_recursive
1858 * does additional checks when
1859 * ip_restrict_interzone_loopback is set.
1861 * If ill_usesrc_ifindex is set
1862 * Then we check if the zone has a valid source address
1863 * on the usesrc ill.
1865 * If ire_ill is set, then check that the zone has an ipif
1866 * on that ill.
1868 * Outside of this function (in ire_round_robin) we check
1869 * that any IRE_OFFLINK has a gateway that reachable from the
1870 * zone when we have multiple choices (ECMP).
1872 if (match_flags & MATCH_IRE_ZONEONLY)
1873 return (B_FALSE);
1874 if (ire->ire_type & IRE_LOOPBACK)
1875 return (B_FALSE);
1877 if (ire->ire_type & IRE_LOCAL)
1878 goto matchit;
1881 * The normal case of IRE_ONLINK has a matching zoneid.
1882 * Here we handle the case when shared-IP zones have been
1883 * configured with IP addresses on vniN. In that case it
1884 * is ok for traffic from a zone to use IRE_ONLINK routes
1885 * if the ill has a usesrc pointing at vniN
1887 dst_ill = ire->ire_ill;
1888 if (ire->ire_type & IRE_ONLINK) {
1889 uint_t ifindex;
1892 * Note there is no IRE_INTERFACE on vniN thus
1893 * can't do an IRE lookup for a matching route.
1895 ifindex = dst_ill->ill_usesrc_ifindex;
1896 if (ifindex == 0)
1897 return (B_FALSE);
1900 * If there is a usable source address in the
1901 * zone, then it's ok to return this IRE_INTERFACE
1903 if (!ipif_zone_avail(ifindex, dst_ill->ill_isv6,
1904 zoneid, ipst)) {
1905 ip3dbg(("ire_match_args: no usrsrc for zone"
1906 " dst_ill %p\n", (void *)dst_ill));
1907 return (B_FALSE);
1911 * For example, with
1912 * route add 11.0.0.0 gw1 -ifp bge0
1913 * route add 11.0.0.0 gw2 -ifp bge1
1914 * this code would differentiate based on
1915 * where the sending zone has addresses.
1916 * Only if the zone has an address on bge0 can it use the first
1917 * route. It isn't clear if this behavior is documented
1918 * anywhere.
1920 if (dst_ill != NULL && (ire->ire_type & IRE_OFFLINK)) {
1921 ipif_t *tipif;
1923 mutex_enter(&dst_ill->ill_lock);
1924 for (tipif = dst_ill->ill_ipif;
1925 tipif != NULL; tipif = tipif->ipif_next) {
1926 if (!IPIF_IS_CONDEMNED(tipif) &&
1927 (tipif->ipif_flags & IPIF_UP) &&
1928 (tipif->ipif_zoneid == zoneid ||
1929 tipif->ipif_zoneid == ALL_ZONES))
1930 break;
1932 mutex_exit(&dst_ill->ill_lock);
1933 if (tipif == NULL) {
1934 return (B_FALSE);
1939 matchit:
1940 ire_ill = ire->ire_ill;
1941 if (match_flags & MATCH_IRE_ILL) {
1944 * If asked to match an ill, we *must* match
1945 * on the ire_ill for ipmp test addresses, or
1946 * any of the ill in the group for data addresses.
1947 * If we don't, we may as well fail.
1948 * However, we need an exception for IRE_LOCALs to ensure
1949 * we loopback packets even sent to test addresses on different
1950 * interfaces in the group.
1952 if ((match_flags & MATCH_IRE_TESTHIDDEN) &&
1953 !(ire->ire_type & IRE_LOCAL)) {
1954 if (ire->ire_ill != ill)
1955 return (B_FALSE);
1956 } else {
1957 match_flags &= ~MATCH_IRE_TESTHIDDEN;
1959 * We know that ill is not NULL, but ire_ill could be
1960 * NULL
1962 if (ire_ill == NULL || !IS_ON_SAME_LAN(ill, ire_ill))
1963 return (B_FALSE);
1966 if (match_flags & MATCH_IRE_SRC_ILL) {
1967 if (ire_ill == NULL)
1968 return (B_FALSE);
1969 if (!IS_ON_SAME_LAN(ill, ire_ill)) {
1970 if (ire_ill->ill_usesrc_ifindex == 0 ||
1971 (ire_ill->ill_usesrc_ifindex !=
1972 ill->ill_phyint->phyint_ifindex))
1973 return (B_FALSE);
1977 if ((ire->ire_addr == (addr & mask)) &&
1978 ((!(match_flags & MATCH_IRE_GW)) ||
1979 (ire->ire_gateway_addr == gateway)) &&
1980 ((!(match_flags & MATCH_IRE_DIRECT)) ||
1981 !(ire->ire_flags & RTF_INDIRECT)) &&
1982 ((!(match_flags & MATCH_IRE_TYPE)) || (ire->ire_type & type)) &&
1983 ((!(match_flags & MATCH_IRE_TESTHIDDEN)) || ire->ire_testhidden) &&
1984 ((!(match_flags & MATCH_IRE_MASK)) || (ire->ire_mask == mask))) {
1985 /* We found the matched IRE */
1986 return (B_TRUE);
1988 return (B_FALSE);
1992 * Check if the IRE_LOCAL uses the same ill as another route would use.
1993 * If there is no alternate route, or the alternate is a REJECT or BLACKHOLE,
1994 * then we don't allow this IRE_LOCAL to be used.
1995 * We always return an IRE; will be RTF_REJECT if no route available.
1997 ire_t *
1998 ire_alt_local(ire_t *ire, zoneid_t zoneid, const ill_t *ill,
1999 uint_t *generationp)
2001 ip_stack_t *ipst = ire->ire_ipst;
2002 ire_t *alt_ire;
2003 uint_t ire_type;
2004 uint_t generation;
2005 uint_t match_flags;
2007 ASSERT(ire->ire_type & IRE_LOCAL);
2008 ASSERT(ire->ire_ill != NULL);
2011 * Need to match on everything but local.
2012 * This might result in the creation of a IRE_IF_CLONE for the
2013 * same address as the IRE_LOCAL when restrict_interzone_loopback is
2014 * set. ire_add_*() ensures that the IRE_IF_CLONE are tail inserted
2015 * to make sure the IRE_LOCAL is always found first.
2017 ire_type = (IRE_ONLINK | IRE_OFFLINK) & ~(IRE_LOCAL|IRE_LOOPBACK);
2018 match_flags = MATCH_IRE_TYPE;
2019 if (ill != NULL)
2020 match_flags |= MATCH_IRE_ILL;
2022 if (ire->ire_ipversion == IPV4_VERSION) {
2023 alt_ire = ire_route_recursive_v4(ire->ire_addr, ire_type,
2024 ill, zoneid, match_flags, IRR_ALLOCATE, 0, ipst, NULL,
2025 &generation);
2026 } else {
2027 alt_ire = ire_route_recursive_v6(&ire->ire_addr_v6, ire_type,
2028 ill, zoneid, match_flags, IRR_ALLOCATE, 0, ipst, NULL,
2029 &generation);
2031 ASSERT(alt_ire != NULL);
2033 if (alt_ire->ire_ill == ire->ire_ill) {
2034 /* Going out the same ILL - ok to send to IRE_LOCAL */
2035 ire_refrele(alt_ire);
2036 } else {
2037 /* Different ill - ignore IRE_LOCAL */
2038 ire_refrele(ire);
2039 ire = alt_ire;
2040 if (generationp != NULL)
2041 *generationp = generation;
2043 return (ire);
2046 boolean_t
2047 ire_find_zoneid(struct radix_node *rn, void *arg)
2049 struct rt_entry *rt = (struct rt_entry *)rn;
2050 irb_t *irb;
2051 ire_t *ire;
2052 ire_ftable_args_t *margs = arg;
2054 ASSERT(rt != NULL);
2056 irb = &rt->rt_irb;
2058 if (irb->irb_ire_cnt == 0)
2059 return (B_FALSE);
2061 rw_enter(&irb->irb_lock, RW_READER);
2062 for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
2063 if (IRE_IS_CONDEMNED(ire))
2064 continue;
2066 if (!(ire->ire_type & IRE_INTERFACE))
2067 continue;
2069 if (ire->ire_zoneid != ALL_ZONES &&
2070 ire->ire_zoneid != margs->ift_zoneid)
2071 continue;
2073 if (margs->ift_ill != NULL && margs->ift_ill != ire->ire_ill)
2074 continue;
2076 rw_exit(&irb->irb_lock);
2077 return (B_TRUE);
2079 rw_exit(&irb->irb_lock);
2080 return (B_FALSE);
2084 * Check if the zoneid (not ALL_ZONES) has an IRE_INTERFACE for the specified
2085 * gateway address. If ill is non-NULL we also match on it.
2086 * The caller must hold a read lock on RADIX_NODE_HEAD if lock_held is set.
2088 boolean_t
2089 ire_gateway_ok_zone_v4(ipaddr_t gateway, zoneid_t zoneid, ill_t *ill,
2090 ip_stack_t *ipst, boolean_t lock_held)
2092 struct rt_sockaddr rdst;
2093 struct rt_entry *rt;
2094 ire_ftable_args_t margs;
2096 ASSERT(ill == NULL || !ill->ill_isv6);
2097 if (lock_held)
2098 ASSERT(RW_READ_HELD(&ipst->ips_ip_ftable->rnh_lock));
2099 else
2100 RADIX_NODE_HEAD_RLOCK(ipst->ips_ip_ftable);
2102 bzero(&rdst, sizeof (rdst));
2103 rdst.rt_sin_len = sizeof (rdst);
2104 rdst.rt_sin_family = AF_INET;
2105 rdst.rt_sin_addr.s_addr = gateway;
2107 /* We only use margs for ill and zoneidmatching in ire_find_zoneid */
2108 bzero(&margs, sizeof (margs));
2109 margs.ift_ill = ill;
2110 margs.ift_zoneid = zoneid;
2111 rt = (struct rt_entry *)ipst->ips_ip_ftable->rnh_matchaddr_args(&rdst,
2112 ipst->ips_ip_ftable, ire_find_zoneid, (void *)&margs);
2114 if (!lock_held)
2115 RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
2117 return (rt != NULL);
2121 * ire_walk routine to delete a fraction of redirect IREs and IRE_CLONE_IF IREs.
2122 * The fraction argument tells us what fraction of the IREs to delete.
2123 * Common for IPv4 and IPv6.
2124 * Used when memory backpressure.
2126 static void
2127 ire_delete_reclaim(ire_t *ire, char *arg)
2129 ip_stack_t *ipst = ire->ire_ipst;
2130 uint_t fraction = *(uint_t *)arg;
2131 uint_t rand;
2133 if ((ire->ire_flags & RTF_DYNAMIC) ||
2134 (ire->ire_type & IRE_IF_CLONE)) {
2136 /* Pick a random number */
2137 rand = (uint_t)ddi_get_lbolt() +
2138 IRE_ADDR_HASH_V6(ire->ire_addr_v6, 256);
2140 /* Use truncation */
2141 if ((rand/fraction)*fraction == rand) {
2142 IP_STAT(ipst, ip_ire_reclaim_deleted);
2143 ire_delete(ire);
2150 * kmem_cache callback to free up memory.
2152 * Free a fraction (ips_ip_ire_reclaim_fraction) of things IP added dynamically
2153 * (RTF_DYNAMIC and IRE_IF_CLONE).
2155 static void
2156 ip_ire_reclaim_stack(ip_stack_t *ipst)
2158 uint_t fraction = ipst->ips_ip_ire_reclaim_fraction;
2160 IP_STAT(ipst, ip_ire_reclaim_calls);
2162 ire_walk(ire_delete_reclaim, &fraction, ipst);
2165 * Walk all CONNs that can have a reference on an ire, nce or dce.
2166 * Get them to update any stale references to drop any refholds they
2167 * have.
2169 ipcl_walk(conn_ixa_cleanup, (void *)B_FALSE, ipst);
2173 * Called by the memory allocator subsystem directly, when the system
2174 * is running low on memory.
2176 /* ARGSUSED */
2177 void
2178 ip_ire_reclaim(void *args)
2180 netstack_handle_t nh;
2181 netstack_t *ns;
2182 ip_stack_t *ipst;
2184 netstack_next_init(&nh);
2185 while ((ns = netstack_next(&nh)) != NULL) {
2187 * netstack_next() can return a netstack_t with a NULL
2188 * netstack_ip at boot time.
2190 if ((ipst = ns->netstack_ip) == NULL) {
2191 netstack_rele(ns);
2192 continue;
2194 ip_ire_reclaim_stack(ipst);
2195 netstack_rele(ns);
2197 netstack_next_fini(&nh);
2200 static void
2201 power2_roundup(uint32_t *value)
2203 int i;
2205 for (i = 1; i < 31; i++) {
2206 if (*value <= (1 << i))
2207 break;
2209 *value = (1 << i);
2212 /* Global init for all zones */
2213 void
2214 ip_ire_g_init()
2217 * Create kmem_caches. ip_ire_reclaim() and ip_nce_reclaim()
2218 * will give disposable IREs back to system when needed.
2219 * This needs to be done here before anything else, since
2220 * ire_add() expects the cache to be created.
2222 ire_cache = kmem_cache_create("ire_cache",
2223 sizeof (ire_t), 0, NULL, NULL,
2224 ip_ire_reclaim, NULL, NULL, 0);
2226 ncec_cache = kmem_cache_create("ncec_cache",
2227 sizeof (ncec_t), 0, NULL, NULL,
2228 ip_nce_reclaim, NULL, NULL, 0);
2229 nce_cache = kmem_cache_create("nce_cache",
2230 sizeof (nce_t), 0, NULL, NULL,
2231 NULL, NULL, NULL, 0);
2233 rt_entry_cache = kmem_cache_create("rt_entry",
2234 sizeof (struct rt_entry), 0, NULL, NULL, NULL, NULL, NULL, 0);
2237 * Have radix code setup kmem caches etc.
2239 rn_init();
2242 void
2243 ip_ire_init(ip_stack_t *ipst)
2245 ire_t *ire;
2246 int error;
2248 mutex_init(&ipst->ips_ire_ft_init_lock, NULL, MUTEX_DEFAULT, 0);
2250 (void) rn_inithead((void **)&ipst->ips_ip_ftable, 32);
2253 * Make sure that the forwarding table size is a power of 2.
2254 * The IRE*_ADDR_HASH() macroes depend on that.
2256 ipst->ips_ip6_ftable_hash_size = ip6_ftable_hash_size;
2257 power2_roundup(&ipst->ips_ip6_ftable_hash_size);
2260 * Allocate/initialize a pair of IRE_NOROUTEs for each of IPv4 and IPv6.
2261 * The ire_reject_v* has RTF_REJECT set, and the ire_blackhole_v* has
2262 * RTF_BLACKHOLE set. We use the latter for transient errors such
2263 * as memory allocation failures and tripping on IRE_IS_CONDEMNED
2264 * entries.
2266 ire = kmem_cache_alloc(ire_cache, KM_SLEEP);
2267 *ire = ire_null;
2268 error = ire_init_v4(ire, 0, 0, 0, IRE_NOROUTE, NULL, ALL_ZONES,
2269 RTF_REJECT|RTF_UP, ipst);
2270 ASSERT(error == 0);
2271 ipst->ips_ire_reject_v4 = ire;
2273 ire = kmem_cache_alloc(ire_cache, KM_SLEEP);
2274 *ire = ire_null;
2275 error = ire_init_v6(ire, 0, 0, 0, IRE_NOROUTE, NULL, ALL_ZONES,
2276 RTF_REJECT|RTF_UP, ipst);
2277 ASSERT(error == 0);
2278 ipst->ips_ire_reject_v6 = ire;
2280 ire = kmem_cache_alloc(ire_cache, KM_SLEEP);
2281 *ire = ire_null;
2282 error = ire_init_v4(ire, 0, 0, 0, IRE_NOROUTE, NULL, ALL_ZONES,
2283 RTF_BLACKHOLE|RTF_UP, ipst);
2284 ASSERT(error == 0);
2285 ipst->ips_ire_blackhole_v4 = ire;
2287 ire = kmem_cache_alloc(ire_cache, KM_SLEEP);
2288 *ire = ire_null;
2289 error = ire_init_v6(ire, 0, 0, 0, IRE_NOROUTE, NULL, ALL_ZONES,
2290 RTF_BLACKHOLE|RTF_UP, ipst);
2291 ASSERT(error == 0);
2292 ipst->ips_ire_blackhole_v6 = ire;
2294 rw_init(&ipst->ips_ip6_ire_head_lock, NULL, RW_DEFAULT, NULL);
2295 rw_init(&ipst->ips_ire_dep_lock, NULL, RW_DEFAULT, NULL);
2298 void
2299 ip_ire_g_fini(void)
2301 kmem_cache_destroy(ire_cache);
2302 kmem_cache_destroy(ncec_cache);
2303 kmem_cache_destroy(nce_cache);
2304 kmem_cache_destroy(rt_entry_cache);
2306 rn_fini();
2309 void
2310 ip_ire_fini(ip_stack_t *ipst)
2312 int i;
2314 ire_make_condemned(ipst->ips_ire_reject_v6);
2315 ire_refrele_notr(ipst->ips_ire_reject_v6);
2316 ipst->ips_ire_reject_v6 = NULL;
2318 ire_make_condemned(ipst->ips_ire_reject_v4);
2319 ire_refrele_notr(ipst->ips_ire_reject_v4);
2320 ipst->ips_ire_reject_v4 = NULL;
2322 ire_make_condemned(ipst->ips_ire_blackhole_v6);
2323 ire_refrele_notr(ipst->ips_ire_blackhole_v6);
2324 ipst->ips_ire_blackhole_v6 = NULL;
2326 ire_make_condemned(ipst->ips_ire_blackhole_v4);
2327 ire_refrele_notr(ipst->ips_ire_blackhole_v4);
2328 ipst->ips_ire_blackhole_v4 = NULL;
2331 * Delete all IREs - assumes that the ill/ipifs have
2332 * been removed so what remains are just the ftable to handle.
2334 ire_walk(ire_delete, NULL, ipst);
2336 rn_freehead(ipst->ips_ip_ftable);
2337 ipst->ips_ip_ftable = NULL;
2339 rw_destroy(&ipst->ips_ire_dep_lock);
2340 rw_destroy(&ipst->ips_ip6_ire_head_lock);
2342 mutex_destroy(&ipst->ips_ire_ft_init_lock);
2344 for (i = 0; i < IP6_MASK_TABLE_SIZE; i++) {
2345 irb_t *ptr;
2346 int j;
2348 if ((ptr = ipst->ips_ip_forwarding_table_v6[i]) == NULL)
2349 continue;
2351 for (j = 0; j < ipst->ips_ip6_ftable_hash_size; j++) {
2352 ASSERT(ptr[j].irb_ire == NULL);
2353 rw_destroy(&ptr[j].irb_lock);
2355 mi_free(ptr);
2356 ipst->ips_ip_forwarding_table_v6[i] = NULL;
2360 #ifdef DEBUG
2361 void
2362 ire_trace_ref(ire_t *ire)
2364 mutex_enter(&ire->ire_lock);
2365 if (ire->ire_trace_disable) {
2366 mutex_exit(&ire->ire_lock);
2367 return;
2370 if (th_trace_ref(ire, ire->ire_ipst)) {
2371 mutex_exit(&ire->ire_lock);
2372 } else {
2373 ire->ire_trace_disable = B_TRUE;
2374 mutex_exit(&ire->ire_lock);
2375 ire_trace_cleanup(ire);
2379 void
2380 ire_untrace_ref(ire_t *ire)
2382 mutex_enter(&ire->ire_lock);
2383 if (!ire->ire_trace_disable)
2384 th_trace_unref(ire);
2385 mutex_exit(&ire->ire_lock);
2388 static void
2389 ire_trace_cleanup(const ire_t *ire)
2391 th_trace_cleanup(ire, ire->ire_trace_disable);
2393 #endif /* DEBUG */
2396 * Find, or create if needed, the nce_t pointer to the neighbor cache
2397 * entry ncec_t for an IPv4 address. The nce_t will be created on the ill_t
2398 * in the non-IPMP case, or on the cast-ill in the IPMP bcast/mcast case, or
2399 * on the next available under-ill (selected by the IPMP rotor) in the
2400 * unicast IPMP case.
2402 * If a neighbor-cache entry has to be created (i.e., one does not already
2403 * exist in the nce list) the ncec_lladdr and ncec_state of the neighbor cache
2404 * entry are initialized in nce_add_v4(). The broadcast, multicast, and
2405 * link-layer type determine the contents of {ncec_state, ncec_lladdr} of
2406 * the ncec_t created. The ncec_lladdr is non-null for all link types with
2407 * non-zero ill_phys_addr_length, though the contents may be zero in cases
2408 * where the link-layer type is not known at the time of creation
2409 * (e.g., IRE_IFRESOLVER links)
2411 * All IRE_BROADCAST entries have ncec_state = ND_REACHABLE, and the nce_lladr
2412 * has the physical broadcast address of the outgoing interface.
2413 * For unicast ire entries,
2414 * - if the outgoing interface is of type IRE_IF_RESOLVER, a newly created
2415 * ncec_t with 0 nce_lladr contents, and will be in the ND_INITIAL state.
2416 * - if the outgoing interface is a IRE_IF_NORESOLVER interface, no link
2417 * layer resolution is necessary, so that the ncec_t will be in the
2418 * ND_REACHABLE state
2420 * The link layer information needed for broadcast addresses, and for
2421 * packets sent on IRE_IF_NORESOLVER interfaces is a constant mapping that
2422 * never needs re-verification for the lifetime of the ncec_t. These are
2423 * therefore marked NCE_F_NONUD.
2425 * The nce returned will be created such that the nce_ill == ill that
2426 * is passed in. Note that the nce itself may not have ncec_ill == ill
2427 * where IPMP links are involved.
2429 static nce_t *
2430 ire_nce_init(ill_t *ill, const void *addr, int ire_type)
2432 int err;
2433 nce_t *nce = NULL;
2434 uint16_t ncec_flags;
2435 uchar_t *hwaddr;
2436 boolean_t need_refrele = B_FALSE;
2437 ill_t *in_ill = ill;
2438 boolean_t is_unicast;
2439 uint_t hwaddr_len;
2441 is_unicast = ((ire_type & (IRE_MULTICAST|IRE_BROADCAST)) == 0);
2442 if (IS_IPMP(ill) ||
2443 ((ire_type & IRE_BROADCAST) && IS_UNDER_IPMP(ill))) {
2444 if ((ill = ipmp_ill_hold_xmit_ill(ill, is_unicast)) == NULL)
2445 return (NULL);
2446 need_refrele = B_TRUE;
2448 ncec_flags = (ill->ill_flags & ILLF_NONUD) ? NCE_F_NONUD : 0;
2450 switch (ire_type) {
2451 case IRE_BROADCAST:
2452 ASSERT(!ill->ill_isv6);
2453 ncec_flags |= (NCE_F_BCAST|NCE_F_NONUD);
2454 break;
2455 case IRE_MULTICAST:
2456 ncec_flags |= (NCE_F_MCAST|NCE_F_NONUD);
2457 break;
2460 if (ill->ill_net_type == IRE_IF_NORESOLVER && is_unicast) {
2461 hwaddr = ill->ill_dest_addr;
2462 } else {
2463 hwaddr = NULL;
2465 hwaddr_len = ill->ill_phys_addr_length;
2467 retry:
2468 /* nce_state will be computed by nce_add_common() */
2469 if (!ill->ill_isv6) {
2470 err = nce_lookup_then_add_v4(ill, hwaddr, hwaddr_len, addr,
2471 ncec_flags, ND_UNCHANGED, &nce);
2472 } else {
2473 err = nce_lookup_then_add_v6(ill, hwaddr, hwaddr_len, addr,
2474 ncec_flags, ND_UNCHANGED, &nce);
2477 switch (err) {
2478 case 0:
2479 break;
2480 case EEXIST:
2482 * When subnets change or partially overlap what was once
2483 * a broadcast address could now be a unicast, or vice versa.
2485 if (((ncec_flags ^ nce->nce_common->ncec_flags) &
2486 NCE_F_BCAST) != 0) {
2487 ASSERT(!ill->ill_isv6);
2488 ncec_delete(nce->nce_common);
2489 nce_refrele(nce);
2490 goto retry;
2492 break;
2493 default:
2494 DTRACE_PROBE2(nce__init__fail, ill_t *, ill, int, err);
2495 if (need_refrele)
2496 ill_refrele(ill);
2497 return (NULL);
2500 * If the ill was an under-ill of an IPMP group, we need to verify
2501 * that it is still active so that we select an active interface in
2502 * the group. However, since ipmp_ill_is_active ASSERTs for
2503 * IS_UNDER_IPMP(), we first need to verify that the ill is an
2504 * under-ill, and since this is being done in the data path, the
2505 * only way to ascertain this is by holding the ill_g_lock.
2507 rw_enter(&ill->ill_ipst->ips_ill_g_lock, RW_READER);
2508 mutex_enter(&ill->ill_lock);
2509 mutex_enter(&ill->ill_phyint->phyint_lock);
2510 if (need_refrele && IS_UNDER_IPMP(ill) && !ipmp_ill_is_active(ill)) {
2512 * need_refrele implies that the under ill was selected by
2513 * ipmp_ill_hold_xmit_ill() because either the in_ill was an
2514 * ipmp_ill, or we are sending a non-unicast packet on an
2515 * under_ill. However, when we get here, the ill selected by
2516 * ipmp_ill_hold_xmit_ill was pulled out of the active set
2517 * (for unicast) or cast_ill nomination (for !unicast) after
2518 * it was picked as the outgoing ill. We have to pick an
2519 * active interface and/or cast_ill in the group.
2521 mutex_exit(&ill->ill_phyint->phyint_lock);
2522 nce_delete(nce);
2523 mutex_exit(&ill->ill_lock);
2524 rw_exit(&ill->ill_ipst->ips_ill_g_lock);
2525 nce_refrele(nce);
2526 ill_refrele(ill);
2527 if ((ill = ipmp_ill_hold_xmit_ill(in_ill, is_unicast)) == NULL)
2528 return (NULL);
2529 goto retry;
2530 } else {
2531 mutex_exit(&ill->ill_phyint->phyint_lock);
2532 mutex_exit(&ill->ill_lock);
2533 rw_exit(&ill->ill_ipst->ips_ill_g_lock);
2535 done:
2536 ASSERT(nce->nce_ill == ill);
2537 if (need_refrele)
2538 ill_refrele(ill);
2539 return (nce);
2542 nce_t *
2543 arp_nce_init(ill_t *ill, in_addr_t addr4, int ire_type)
2545 return (ire_nce_init(ill, &addr4, ire_type));
2548 nce_t *
2549 ndp_nce_init(ill_t *ill, const in6_addr_t *addr6, int ire_type)
2551 ASSERT((ire_type & IRE_BROADCAST) == 0);
2552 return (ire_nce_init(ill, addr6, ire_type));
2556 * The caller should hold irb_lock as a writer if the ire is in a bucket.
2557 * This routine will clear ire_nce_cache, and we make sure that we can never
2558 * set ire_nce_cache after the ire is marked condemned.
2560 void
2561 ire_make_condemned(ire_t *ire)
2563 ip_stack_t *ipst = ire->ire_ipst;
2564 nce_t *nce;
2566 mutex_enter(&ire->ire_lock);
2567 ASSERT(ire->ire_bucket == NULL ||
2568 RW_WRITE_HELD(&ire->ire_bucket->irb_lock));
2569 ASSERT(!IRE_IS_CONDEMNED(ire));
2570 ire->ire_generation = IRE_GENERATION_CONDEMNED;
2571 /* Count how many condemned ires for kmem_cache callback */
2572 atomic_inc_32(&ipst->ips_num_ire_condemned);
2573 nce = ire->ire_nce_cache;
2574 ire->ire_nce_cache = NULL;
2575 mutex_exit(&ire->ire_lock);
2576 if (nce != NULL)
2577 nce_refrele(nce);
2581 * Increment the generation avoiding the special condemned value
2583 void
2584 ire_increment_generation(ire_t *ire)
2586 uint_t generation;
2588 mutex_enter(&ire->ire_lock);
2590 * Even though the caller has a hold it can't prevent a concurrent
2591 * ire_delete marking the IRE condemned
2593 if (!IRE_IS_CONDEMNED(ire)) {
2594 generation = ire->ire_generation + 1;
2595 if (generation == IRE_GENERATION_CONDEMNED)
2596 generation = IRE_GENERATION_INITIAL;
2597 ASSERT(generation != IRE_GENERATION_VERIFY);
2598 ire->ire_generation = generation;
2600 mutex_exit(&ire->ire_lock);
2604 * Increment ire_generation on all the IRE_MULTICASTs
2605 * Used when the default multicast interface (as determined by
2606 * ill_lookup_multicast) might have changed.
2608 * That includes the zoneid, IFF_ flags, the IPv6 scope of the address, and
2609 * ill unplumb.
2611 void
2612 ire_increment_multicast_generation(ip_stack_t *ipst, boolean_t isv6)
2614 ill_t *ill;
2615 ill_walk_context_t ctx;
2617 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
2618 if (isv6)
2619 ill = ILL_START_WALK_V6(&ctx, ipst);
2620 else
2621 ill = ILL_START_WALK_V4(&ctx, ipst);
2622 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
2623 if (ILL_IS_CONDEMNED(ill))
2624 continue;
2625 if (ill->ill_ire_multicast != NULL)
2626 ire_increment_generation(ill->ill_ire_multicast);
2628 rw_exit(&ipst->ips_ill_g_lock);
2632 * Return a held IRE_NOROUTE with RTF_REJECT set
2634 ire_t *
2635 ire_reject(ip_stack_t *ipst, boolean_t isv6)
2637 ire_t *ire;
2639 if (isv6)
2640 ire = ipst->ips_ire_reject_v6;
2641 else
2642 ire = ipst->ips_ire_reject_v4;
2644 ASSERT(ire->ire_generation != IRE_GENERATION_CONDEMNED);
2645 ire_refhold(ire);
2646 return (ire);
2650 * Return a held IRE_NOROUTE with RTF_BLACKHOLE set
2652 ire_t *
2653 ire_blackhole(ip_stack_t *ipst, boolean_t isv6)
2655 ire_t *ire;
2657 if (isv6)
2658 ire = ipst->ips_ire_blackhole_v6;
2659 else
2660 ire = ipst->ips_ire_blackhole_v4;
2662 ASSERT(ire->ire_generation != IRE_GENERATION_CONDEMNED);
2663 ire_refhold(ire);
2664 return (ire);
2668 * Return a held IRE_MULTICAST.
2670 ire_t *
2671 ire_multicast(ill_t *ill)
2673 ire_t *ire = ill->ill_ire_multicast;
2675 ASSERT(ire == NULL || ire->ire_generation != IRE_GENERATION_CONDEMNED);
2676 if (ire == NULL)
2677 ire = ire_blackhole(ill->ill_ipst, ill->ill_isv6);
2678 else
2679 ire_refhold(ire);
2680 return (ire);
2684 * Given an IRE return its nexthop IRE. The nexthop IRE is an IRE_ONLINK
2685 * that is an exact match (i.e., a /32 for IPv4 and /128 for IPv6).
2686 * This can return an RTF_REJECT|RTF_BLACKHOLE.
2687 * The returned IRE is held.
2688 * The assumption is that ip_select_route() has been called and returned the
2689 * IRE (thus ip_select_route would have set up the ire_dep* information.)
2690 * If some IRE is deleteted then ire_dep_remove() will have been called and
2691 * we might not find a nexthop IRE, in which case we return NULL.
2693 ire_t *
2694 ire_nexthop(ire_t *ire)
2696 ip_stack_t *ipst = ire->ire_ipst;
2698 /* Acquire lock to walk ire_dep_parent */
2699 rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
2700 while (ire != NULL) {
2701 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
2702 goto done;
2705 * If we find an IRE_ONLINK we are done. This includes
2706 * the case of IRE_MULTICAST.
2707 * Note that in order to send packets we need a host-specific
2708 * IRE_IF_ALL first in the ire_dep_parent chain. Normally this
2709 * is done by inserting an IRE_IF_CLONE if the IRE_INTERFACE
2710 * was not host specific.
2711 * However, ip_rts_request doesn't want to send packets
2712 * hence doesn't want to allocate an IRE_IF_CLONE. Yet
2713 * it needs an IRE_IF_ALL to get to the ill. Thus
2714 * we return IRE_IF_ALL that are not host specific here.
2716 if (ire->ire_type & IRE_ONLINK)
2717 goto done;
2718 ire = ire->ire_dep_parent;
2720 rw_exit(&ipst->ips_ire_dep_lock);
2721 return (NULL);
2723 done:
2724 ire_refhold(ire);
2725 rw_exit(&ipst->ips_ire_dep_lock);
2726 return (ire);
2730 * Find the ill used to send packets. This will be NULL in case
2731 * of a reject or blackhole.
2732 * The returned ill is held; caller needs to do ill_refrele when done.
2734 ill_t *
2735 ire_nexthop_ill(ire_t *ire)
2737 ill_t *ill;
2739 ire = ire_nexthop(ire);
2740 if (ire == NULL)
2741 return (NULL);
2743 /* ire_ill can not change for an existing ire */
2744 ill = ire->ire_ill;
2745 if (ill != NULL)
2746 ill_refhold(ill);
2747 ire_refrele(ire);
2748 return (ill);
2751 #ifdef DEBUG
2752 static boolean_t
2753 parent_has_child(ire_t *parent, ire_t *child)
2755 ire_t *ire;
2756 ire_t *prev;
2758 ire = parent->ire_dep_children;
2759 prev = NULL;
2760 while (ire != NULL) {
2761 if (prev == NULL) {
2762 ASSERT(ire->ire_dep_sib_ptpn ==
2763 &(parent->ire_dep_children));
2764 } else {
2765 ASSERT(ire->ire_dep_sib_ptpn ==
2766 &(prev->ire_dep_sib_next));
2768 if (ire == child)
2769 return (B_TRUE);
2770 prev = ire;
2771 ire = ire->ire_dep_sib_next;
2773 return (B_FALSE);
2776 static void
2777 ire_dep_verify(ire_t *ire)
2779 ire_t *parent = ire->ire_dep_parent;
2780 ire_t *child = ire->ire_dep_children;
2782 ASSERT(ire->ire_ipversion == IPV4_VERSION ||
2783 ire->ire_ipversion == IPV6_VERSION);
2784 if (parent != NULL) {
2785 ASSERT(parent->ire_ipversion == IPV4_VERSION ||
2786 parent->ire_ipversion == IPV6_VERSION);
2787 ASSERT(parent->ire_refcnt >= 1);
2788 ASSERT(parent_has_child(parent, ire));
2790 if (child != NULL) {
2791 ASSERT(child->ire_ipversion == IPV4_VERSION ||
2792 child->ire_ipversion == IPV6_VERSION);
2793 ASSERT(child->ire_dep_parent == ire);
2794 ASSERT(child->ire_dep_sib_ptpn != NULL);
2795 ASSERT(parent_has_child(ire, child));
2798 #endif /* DEBUG */
2801 * Assumes ire_dep_parent is set. Remove this child from its parent's linkage.
2803 void
2804 ire_dep_remove(ire_t *ire)
2806 ip_stack_t *ipst = ire->ire_ipst;
2807 ire_t *parent = ire->ire_dep_parent;
2808 ire_t *next;
2809 nce_t *nce;
2811 ASSERT(RW_WRITE_HELD(&ipst->ips_ire_dep_lock));
2812 ASSERT(ire->ire_dep_parent != NULL);
2813 ASSERT(ire->ire_dep_sib_ptpn != NULL);
2815 #ifdef DEBUG
2816 ire_dep_verify(ire);
2817 ire_dep_verify(parent);
2818 #endif
2820 next = ire->ire_dep_sib_next;
2821 if (next != NULL)
2822 next->ire_dep_sib_ptpn = ire->ire_dep_sib_ptpn;
2824 ASSERT(*(ire->ire_dep_sib_ptpn) == ire);
2825 *(ire->ire_dep_sib_ptpn) = ire->ire_dep_sib_next;
2827 ire->ire_dep_sib_ptpn = NULL;
2828 ire->ire_dep_sib_next = NULL;
2830 mutex_enter(&ire->ire_lock);
2831 parent = ire->ire_dep_parent;
2832 ire->ire_dep_parent = NULL;
2833 mutex_exit(&ire->ire_lock);
2836 * Make sure all our children, grandchildren, etc set
2837 * ire_dep_parent_generation to IRE_GENERATION_VERIFY since
2838 * we can no longer guarantee than the children have a current
2839 * ire_nce_cache and ire_nexthop_ill().
2841 if (ire->ire_dep_children != NULL)
2842 ire_dep_invalidate_children(ire->ire_dep_children);
2845 * Since the parent is gone we make sure we clear ire_nce_cache.
2846 * We can clear it under ire_lock even if the IRE is used
2848 mutex_enter(&ire->ire_lock);
2849 nce = ire->ire_nce_cache;
2850 ire->ire_nce_cache = NULL;
2851 mutex_exit(&ire->ire_lock);
2852 if (nce != NULL)
2853 nce_refrele(nce);
2855 #ifdef DEBUG
2856 ire_dep_verify(ire);
2857 ire_dep_verify(parent);
2858 #endif
2860 ire_refrele_notr(parent);
2861 ire_refrele_notr(ire);
2865 * Insert the child in the linkage of the parent
2867 static void
2868 ire_dep_parent_insert(ire_t *child, ire_t *parent)
2870 ip_stack_t *ipst = child->ire_ipst;
2871 ire_t *next;
2873 ASSERT(RW_WRITE_HELD(&ipst->ips_ire_dep_lock));
2874 ASSERT(child->ire_dep_parent == NULL);
2876 #ifdef DEBUG
2877 ire_dep_verify(child);
2878 ire_dep_verify(parent);
2879 #endif
2880 /* No parents => no siblings */
2881 ASSERT(child->ire_dep_sib_ptpn == NULL);
2882 ASSERT(child->ire_dep_sib_next == NULL);
2884 ire_refhold_notr(parent);
2885 ire_refhold_notr(child);
2887 /* Head insertion */
2888 next = parent->ire_dep_children;
2889 if (next != NULL) {
2890 ASSERT(next->ire_dep_sib_ptpn == &(parent->ire_dep_children));
2891 child->ire_dep_sib_next = next;
2892 next->ire_dep_sib_ptpn = &(child->ire_dep_sib_next);
2894 parent->ire_dep_children = child;
2895 child->ire_dep_sib_ptpn = &(parent->ire_dep_children);
2897 mutex_enter(&child->ire_lock);
2898 child->ire_dep_parent = parent;
2899 mutex_exit(&child->ire_lock);
2901 #ifdef DEBUG
2902 ire_dep_verify(child);
2903 ire_dep_verify(parent);
2904 #endif
2909 * Given count worth of ires and generations, build ire_dep_* relationships
2910 * from ires[0] to ires[count-1]. Record generations[i+1] in
2911 * ire_dep_parent_generation for ires[i].
2912 * We graft onto an existing parent chain by making sure that we don't
2913 * touch ire_dep_parent for ires[count-1].
2915 * We check for any condemned ire_generation count and return B_FALSE in
2916 * that case so that the caller can tear it apart.
2918 * Note that generations[0] is not used. Caller handles that.
2920 boolean_t
2921 ire_dep_build(ire_t *ires[], uint_t generations[], uint_t count)
2923 ire_t *ire = ires[0];
2924 ip_stack_t *ipst;
2925 uint_t i;
2927 ASSERT(count > 0);
2928 if (count == 1) {
2929 /* No work to do */
2930 return (B_TRUE);
2932 ipst = ire->ire_ipst;
2933 rw_enter(&ipst->ips_ire_dep_lock, RW_WRITER);
2935 * Do not remove the linkage for any existing parent chain i.e.,
2936 * ires[count-1] is left alone.
2938 for (i = 0; i < count-1; i++) {
2939 /* Remove existing parent if we need to change it */
2940 if (ires[i]->ire_dep_parent != NULL &&
2941 ires[i]->ire_dep_parent != ires[i+1])
2942 ire_dep_remove(ires[i]);
2945 for (i = 0; i < count - 1; i++) {
2946 ASSERT(ires[i]->ire_ipversion == IPV4_VERSION ||
2947 ires[i]->ire_ipversion == IPV6_VERSION);
2948 /* Does it need to change? */
2949 if (ires[i]->ire_dep_parent != ires[i+1])
2950 ire_dep_parent_insert(ires[i], ires[i+1]);
2952 mutex_enter(&ires[i+1]->ire_lock);
2953 if (IRE_IS_CONDEMNED(ires[i+1])) {
2954 mutex_exit(&ires[i+1]->ire_lock);
2955 rw_exit(&ipst->ips_ire_dep_lock);
2956 return (B_FALSE);
2958 mutex_exit(&ires[i+1]->ire_lock);
2960 mutex_enter(&ires[i]->ire_lock);
2961 ires[i]->ire_dep_parent_generation = generations[i+1];
2962 mutex_exit(&ires[i]->ire_lock);
2964 rw_exit(&ipst->ips_ire_dep_lock);
2965 return (B_TRUE);
2969 * Given count worth of ires, unbuild ire_dep_* relationships
2970 * from ires[0] to ires[count-1].
2972 void
2973 ire_dep_unbuild(ire_t *ires[], uint_t count)
2975 ip_stack_t *ipst;
2976 uint_t i;
2978 if (count == 0) {
2979 /* No work to do */
2980 return;
2982 ipst = ires[0]->ire_ipst;
2983 rw_enter(&ipst->ips_ire_dep_lock, RW_WRITER);
2984 for (i = 0; i < count; i++) {
2985 ASSERT(ires[i]->ire_ipversion == IPV4_VERSION ||
2986 ires[i]->ire_ipversion == IPV6_VERSION);
2987 if (ires[i]->ire_dep_parent != NULL)
2988 ire_dep_remove(ires[i]);
2989 mutex_enter(&ires[i]->ire_lock);
2990 ires[i]->ire_dep_parent_generation = IRE_GENERATION_VERIFY;
2991 mutex_exit(&ires[i]->ire_lock);
2993 rw_exit(&ipst->ips_ire_dep_lock);
2997 * Both the forwarding and the outbound code paths can trip on
2998 * a condemned NCE, in which case we call this function.
2999 * We have two different behaviors: if the NCE was UNREACHABLE
3000 * it is an indication that something failed. In that case
3001 * we see if we should look for a different IRE (for example,
3002 * delete any matching redirect IRE, or try a different
3003 * IRE_DEFAULT (ECMP)). We mark the ire as bad so a hopefully
3004 * different IRE will be picked next time we send/forward.
3006 * If we are called by the output path then fail_if_better is set
3007 * and we return NULL if there could be a better IRE. This is because the
3008 * output path retries the IRE lookup. (The input/forward path can not retry.)
3010 * If the NCE was not unreachable then we pick/allocate a
3011 * new (most likely ND_INITIAL) NCE and proceed with it.
3013 * ipha/ip6h are needed for multicast packets; ipha needs to be
3014 * set for IPv4 and ip6h needs to be set for IPv6 packets.
3016 nce_t *
3017 ire_handle_condemned_nce(nce_t *nce, ire_t *ire, ipha_t *ipha, ip6_t *ip6h,
3018 boolean_t fail_if_better)
3020 if (nce->nce_common->ncec_state == ND_UNREACHABLE) {
3021 if (ire_no_good(ire) && fail_if_better) {
3023 * Did some changes, or ECMP likely to exist.
3024 * Make ip_output look for a different IRE
3026 return (NULL);
3029 if (ire_revalidate_nce(ire) == ENETUNREACH) {
3030 /* The ire_dep_parent chain went bad, or no memory? */
3031 (void) ire_no_good(ire);
3032 return (NULL);
3034 if (ire->ire_ipversion == IPV4_VERSION) {
3035 ASSERT(ipha != NULL);
3036 nce = ire_to_nce(ire, ipha->ipha_dst, NULL);
3037 } else {
3038 ASSERT(ip6h != NULL);
3039 nce = ire_to_nce(ire, INADDR_ANY, &ip6h->ip6_dst);
3042 if (nce == NULL)
3043 return (NULL);
3044 if (nce->nce_is_condemned) {
3045 nce_refrele(nce);
3046 return (NULL);
3048 return (nce);
3052 * The caller has found that the ire is bad, either due to a reference to an NCE
3053 * in ND_UNREACHABLE state, or a MULTIRT route whose gateway can't be resolved.
3054 * We update things so a subsequent attempt to send to the destination
3055 * is likely to find different IRE, or that a new NCE would be created.
3057 * Returns B_TRUE if it is likely that a subsequent ire_ftable_lookup would
3058 * find a different route (either due to having deleted a redirect, or there
3059 * being ECMP routes.)
3061 * If we have a redirect (RTF_DYNAMIC) we delete it.
3062 * Otherwise we increment ire_badcnt and increment the generation number so
3063 * that a cached ixa_ire will redo the route selection. ire_badcnt is taken
3064 * into account in the route selection when we have multiple choices (multiple
3065 * default routes or ECMP in general).
3066 * Any time ip_select_route find an ire with a condemned ire_nce_cache
3067 * (e.g., if no equal cost route to the bad one) ip_select_route will make
3068 * sure the NCE is revalidated to avoid getting stuck on a
3069 * NCE_F_CONDMNED ncec that caused ire_no_good to be called.
3071 boolean_t
3072 ire_no_good(ire_t *ire)
3074 ip_stack_t *ipst = ire->ire_ipst;
3075 ire_t *ire2;
3076 nce_t *nce;
3078 if (ire->ire_flags & RTF_DYNAMIC) {
3079 ire_delete(ire);
3080 return (B_TRUE);
3082 if (ire->ire_flags & RTF_INDIRECT) {
3083 /* Check if next IRE is a redirect */
3084 rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
3085 if (ire->ire_dep_parent != NULL &&
3086 (ire->ire_dep_parent->ire_flags & RTF_DYNAMIC)) {
3087 ire2 = ire->ire_dep_parent;
3088 ire_refhold(ire2);
3089 } else {
3090 ire2 = NULL;
3092 rw_exit(&ipst->ips_ire_dep_lock);
3093 if (ire2 != NULL) {
3094 ire_delete(ire2);
3095 ire_refrele(ire2);
3096 return (B_TRUE);
3100 * No redirect involved. Increment badcnt so that if we have ECMP
3101 * routes we are likely to pick a different one for the next packet.
3103 * If the NCE is unreachable and condemned we should drop the reference
3104 * to it so that a new NCE can be created.
3106 * Finally we increment the generation number so that any ixa_ire
3107 * cache will be revalidated.
3109 mutex_enter(&ire->ire_lock);
3110 ire->ire_badcnt++;
3111 ire->ire_last_badcnt = TICK_TO_SEC(ddi_get_lbolt64());
3112 nce = ire->ire_nce_cache;
3113 if (nce != NULL && nce->nce_is_condemned &&
3114 nce->nce_common->ncec_state == ND_UNREACHABLE)
3115 ire->ire_nce_cache = NULL;
3116 else
3117 nce = NULL;
3118 mutex_exit(&ire->ire_lock);
3119 if (nce != NULL)
3120 nce_refrele(nce);
3122 ire_increment_generation(ire);
3123 ire_dep_incr_generation(ire);
3125 return (ire->ire_bucket->irb_ire_cnt > 1);
3129 * Walk ire_dep_parent chain and validate that ire_dep_parent->ire_generation ==
3130 * ire_dep_parent_generation.
3131 * If they all match we just return ire_generation from the topmost IRE.
3132 * Otherwise we propagate the mismatch by setting all ire_dep_parent_generation
3133 * above the mismatch to IRE_GENERATION_VERIFY and also returning
3134 * IRE_GENERATION_VERIFY.
3136 uint_t
3137 ire_dep_validate_generations(ire_t *ire)
3139 ip_stack_t *ipst = ire->ire_ipst;
3140 uint_t generation;
3141 ire_t *ire1;
3143 rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
3144 generation = ire->ire_generation; /* Assuming things match */
3145 for (ire1 = ire; ire1 != NULL; ire1 = ire1->ire_dep_parent) {
3146 ASSERT(ire1->ire_ipversion == IPV4_VERSION ||
3147 ire1->ire_ipversion == IPV6_VERSION);
3148 if (ire1->ire_dep_parent == NULL)
3149 break;
3150 if (ire1->ire_dep_parent_generation !=
3151 ire1->ire_dep_parent->ire_generation)
3152 goto mismatch;
3154 rw_exit(&ipst->ips_ire_dep_lock);
3155 return (generation);
3157 mismatch:
3158 generation = IRE_GENERATION_VERIFY;
3159 /* Fill from top down to the mismatch with _VERIFY */
3160 while (ire != ire1) {
3161 ASSERT(ire->ire_ipversion == IPV4_VERSION ||
3162 ire->ire_ipversion == IPV6_VERSION);
3163 mutex_enter(&ire->ire_lock);
3164 ire->ire_dep_parent_generation = IRE_GENERATION_VERIFY;
3165 mutex_exit(&ire->ire_lock);
3166 ire = ire->ire_dep_parent;
3168 rw_exit(&ipst->ips_ire_dep_lock);
3169 return (generation);
3173 * Used when we need to return an ire with ire_dep_parent, but we
3174 * know the chain is invalid for instance we didn't create an IRE_IF_CLONE
3175 * Using IRE_GENERATION_VERIFY means that next time we'll redo the
3176 * recursive lookup.
3178 void
3179 ire_dep_invalidate_generations(ire_t *ire)
3181 ip_stack_t *ipst = ire->ire_ipst;
3183 rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
3184 while (ire != NULL) {
3185 ASSERT(ire->ire_ipversion == IPV4_VERSION ||
3186 ire->ire_ipversion == IPV6_VERSION);
3187 mutex_enter(&ire->ire_lock);
3188 ire->ire_dep_parent_generation = IRE_GENERATION_VERIFY;
3189 mutex_exit(&ire->ire_lock);
3190 ire = ire->ire_dep_parent;
3192 rw_exit(&ipst->ips_ire_dep_lock);
3195 /* Set _VERIFY ire_dep_parent_generation for all children recursively */
3196 static void
3197 ire_dep_invalidate_children(ire_t *child)
3199 ip_stack_t *ipst = child->ire_ipst;
3201 ASSERT(RW_WRITE_HELD(&ipst->ips_ire_dep_lock));
3202 /* Depth first */
3203 if (child->ire_dep_children != NULL)
3204 ire_dep_invalidate_children(child->ire_dep_children);
3206 while (child != NULL) {
3207 mutex_enter(&child->ire_lock);
3208 child->ire_dep_parent_generation = IRE_GENERATION_VERIFY;
3209 mutex_exit(&child->ire_lock);
3210 child = child->ire_dep_sib_next;
3214 static void
3215 ire_dep_increment_children(ire_t *child)
3217 ip_stack_t *ipst = child->ire_ipst;
3219 ASSERT(RW_READ_HELD(&ipst->ips_ire_dep_lock));
3220 /* Depth first */
3221 if (child->ire_dep_children != NULL)
3222 ire_dep_increment_children(child->ire_dep_children);
3224 while (child != NULL) {
3225 if (!IRE_IS_CONDEMNED(child))
3226 ire_increment_generation(child);
3227 child = child->ire_dep_sib_next;
3232 * Walk all the children of this ire recursively and increment their
3233 * generation number.
3235 static void
3236 ire_dep_incr_generation_locked(ire_t *parent)
3238 ASSERT(RW_READ_HELD(&parent->ire_ipst->ips_ire_dep_lock));
3239 if (parent->ire_dep_children != NULL)
3240 ire_dep_increment_children(parent->ire_dep_children);
3243 void
3244 ire_dep_incr_generation(ire_t *parent)
3246 ip_stack_t *ipst = parent->ire_ipst;
3248 rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
3249 ire_dep_incr_generation_locked(parent);
3250 rw_exit(&ipst->ips_ire_dep_lock);
3254 * Get a new ire_nce_cache for this IRE as well as its nexthop.
3255 * Returns zero if it succeeds. Can fail due to lack of memory or when
3256 * the route has become unreachable. Returns ENOMEM and ENETUNREACH in those
3257 * cases.
3259 * In the in.mpathd case, the ire will have ire_testhidden
3260 * set; so we should create the ncec for the underlying ill.
3262 * Note that the error returned by ire_revalidate_nce() is ignored by most
3263 * callers except ire_handle_condemned_nce(), which handles the ENETUNREACH
3264 * error to mark potentially bad ire's. For all the other callers, an
3265 * error return could indicate a transient condition like ENOMEM, or could
3266 * be the result of an interface that is going down/unplumbing. In the former
3267 * case (transient error), we would leave the old stale ire/ire_nce_cache
3268 * in place, and possibly use incorrect link-layer information to send packets
3269 * but would eventually recover. In the latter case (ill down/replumb),
3270 * ire_revalidate_nce() might return a condemned nce back, but we would then
3271 * recover in the packet output path.
3274 ire_revalidate_nce(ire_t *ire)
3276 nce_t *nce, *old_nce;
3277 ire_t *nexthop;
3280 * For multicast we conceptually have an NCE but we don't store it
3281 * in ire_nce_cache; when ire_to_nce is called we allocate the nce.
3283 if (ire->ire_type & IRE_MULTICAST)
3284 return (0);
3286 /* ire_testhidden should only be set on under-interfaces */
3287 ASSERT(!ire->ire_testhidden || !IS_IPMP(ire->ire_ill));
3289 nexthop = ire_nexthop(ire);
3290 if (nexthop == NULL) {
3291 /* The route is potentially bad */
3292 (void) ire_no_good(ire);
3293 return (ENETUNREACH);
3295 if (ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) {
3296 ASSERT(ire->ire_ill != NULL);
3298 if (ire->ire_ipversion == IPV4_VERSION)
3299 nce = nce_lookup_v4(ire->ire_ill, &ire->ire_addr);
3300 else
3301 nce = nce_lookup_v6(ire->ire_ill, &ire->ire_addr_v6);
3302 } else {
3303 ASSERT(nexthop->ire_type & IRE_ONLINK);
3304 if (ire->ire_ipversion == IPV4_VERSION) {
3305 nce = arp_nce_init(nexthop->ire_ill, nexthop->ire_addr,
3306 nexthop->ire_type);
3307 } else {
3308 nce = ndp_nce_init(nexthop->ire_ill,
3309 &nexthop->ire_addr_v6, nexthop->ire_type);
3312 if (nce == NULL) {
3314 * Leave the old stale one in place to avoid a NULL
3315 * ire_nce_cache.
3317 ire_refrele(nexthop);
3318 return (ENOMEM);
3321 if (nexthop != ire) {
3322 /* Update the nexthop ire */
3323 mutex_enter(&nexthop->ire_lock);
3324 old_nce = nexthop->ire_nce_cache;
3325 if (!IRE_IS_CONDEMNED(nexthop)) {
3326 nce_refhold(nce);
3327 nexthop->ire_nce_cache = nce;
3328 } else {
3329 nexthop->ire_nce_cache = NULL;
3331 mutex_exit(&nexthop->ire_lock);
3332 if (old_nce != NULL)
3333 nce_refrele(old_nce);
3335 ire_refrele(nexthop);
3337 mutex_enter(&ire->ire_lock);
3338 old_nce = ire->ire_nce_cache;
3339 if (!IRE_IS_CONDEMNED(ire)) {
3340 nce_refhold(nce);
3341 ire->ire_nce_cache = nce;
3342 } else {
3343 ire->ire_nce_cache = NULL;
3345 mutex_exit(&ire->ire_lock);
3346 if (old_nce != NULL)
3347 nce_refrele(old_nce);
3349 nce_refrele(nce);
3350 return (0);
3354 * Get a held nce for a given ire.
3355 * In the common case this is just from ire_nce_cache.
3356 * For IRE_MULTICAST this needs to do an explicit lookup since we do not
3357 * have an IRE_MULTICAST per address.
3358 * Note that this explicitly returns CONDEMNED NCEs. The caller needs those
3359 * so they can check whether the NCE went unreachable (as opposed to was
3360 * condemned for some other reason).
3362 nce_t *
3363 ire_to_nce(ire_t *ire, ipaddr_t v4nexthop, const in6_addr_t *v6nexthop)
3365 nce_t *nce;
3367 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))
3368 return (NULL);
3370 /* ire_testhidden should only be set on under-interfaces */
3371 ASSERT(!ire->ire_testhidden || !IS_IPMP(ire->ire_ill));
3373 mutex_enter(&ire->ire_lock);
3374 nce = ire->ire_nce_cache;
3375 if (nce != NULL) {
3376 nce_refhold(nce);
3377 mutex_exit(&ire->ire_lock);
3378 return (nce);
3380 mutex_exit(&ire->ire_lock);
3382 if (ire->ire_type & IRE_MULTICAST) {
3383 ASSERT(ire->ire_ill != NULL);
3385 if (ire->ire_ipversion == IPV4_VERSION) {
3386 ASSERT(v6nexthop == NULL);
3388 nce = arp_nce_init(ire->ire_ill, v4nexthop,
3389 ire->ire_type);
3390 } else {
3391 ASSERT(v6nexthop != NULL);
3392 ASSERT(v4nexthop == 0);
3393 nce = ndp_nce_init(ire->ire_ill, v6nexthop,
3394 ire->ire_type);
3396 return (nce);
3398 return (NULL);
3401 nce_t *
3402 ire_to_nce_pkt(ire_t *ire, mblk_t *mp)
3404 ipha_t *ipha;
3405 ip6_t *ip6h;
3407 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) {
3408 ipha = (ipha_t *)mp->b_rptr;
3409 return (ire_to_nce(ire, ipha->ipha_dst, NULL));
3410 } else {
3411 ip6h = (ip6_t *)mp->b_rptr;
3412 return (ire_to_nce(ire, INADDR_ANY, &ip6h->ip6_dst));
3417 * Given an IRE_INTERFACE (that matches more than one address) create
3418 * and return an IRE_IF_CLONE for the specific address.
3419 * Return the generation number.
3420 * Returns NULL is no memory for the IRE.
3421 * Handles both IPv4 and IPv6.
3423 * IRE_IF_CLONE entries may only be created adn added by calling
3424 * ire_create_if_clone(), and we depend on the fact that ire_add will
3425 * atomically ensure that attempts to add multiple identical IRE_IF_CLONE
3426 * entries will not result in duplicate (i.e., ire_identical_ref > 1)
3427 * CLONE entries, so that a single ire_delete is sufficient to remove the
3428 * CLONE.
3430 ire_t *
3431 ire_create_if_clone(ire_t *ire_if, const in6_addr_t *addr, uint_t *generationp)
3433 ire_t *ire;
3434 ire_t *nire;
3436 if (ire_if->ire_ipversion == IPV4_VERSION) {
3437 ipaddr_t v4addr;
3438 ipaddr_t mask = IP_HOST_MASK;
3440 ASSERT(IN6_IS_ADDR_V4MAPPED(addr));
3441 IN6_V4MAPPED_TO_IPADDR(addr, v4addr);
3443 ire = ire_create(
3444 (uchar_t *)&v4addr, /* dest address */
3445 (uchar_t *)&mask, /* mask */
3446 (uchar_t *)&ire_if->ire_gateway_addr,
3447 IRE_IF_CLONE, /* IRE type */
3448 ire_if->ire_ill,
3449 ire_if->ire_zoneid,
3450 ire_if->ire_flags | RTF_HOST,
3451 ire_if->ire_ipst);
3452 } else {
3453 ASSERT(!IN6_IS_ADDR_V4MAPPED(addr));
3454 ire = ire_create_v6(
3455 addr, /* dest address */
3456 &ipv6_all_ones, /* mask */
3457 &ire_if->ire_gateway_addr_v6, /* gateway addr */
3458 IRE_IF_CLONE, /* IRE type */
3459 ire_if->ire_ill,
3460 ire_if->ire_zoneid,
3461 ire_if->ire_flags | RTF_HOST,
3462 ire_if->ire_ipst);
3464 if (ire == NULL)
3465 return (NULL);
3467 /* Take the metrics, in particular the mtu, from the IRE_IF */
3468 ire->ire_metrics = ire_if->ire_metrics;
3470 nire = ire_add(ire);
3471 if (nire == NULL) /* Some failure */
3472 return (NULL);
3474 if (generationp != NULL)
3475 *generationp = nire->ire_generation;
3477 return (nire);
3481 * The argument is an IRE_INTERFACE. Delete all of IRE_IF_CLONE in the
3482 * ire_dep_children (just walk the ire_dep_sib_next since they are all
3483 * immediate children.)
3484 * Since we hold a lock while we remove them we need to defer the actual
3485 * calls to ire_delete() until we have dropped the lock. This makes things
3486 * less efficient since we restart at the top after dropping the lock. But
3487 * we only run when an IRE_INTERFACE is deleted which is infrquent.
3489 * Note that ire_dep_children can be any mixture of offlink routes and
3490 * IRE_IF_CLONE entries.
3492 void
3493 ire_dep_delete_if_clone(ire_t *parent)
3495 ip_stack_t *ipst = parent->ire_ipst;
3496 ire_t *child, *next;
3498 restart:
3499 rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
3500 if (parent->ire_dep_children == NULL) {
3501 rw_exit(&ipst->ips_ire_dep_lock);
3502 return;
3504 child = parent->ire_dep_children;
3505 while (child != NULL) {
3506 next = child->ire_dep_sib_next;
3507 if ((child->ire_type & IRE_IF_CLONE) &&
3508 !IRE_IS_CONDEMNED(child)) {
3509 ire_refhold(child);
3510 rw_exit(&ipst->ips_ire_dep_lock);
3511 ire_delete(child);
3512 ASSERT(IRE_IS_CONDEMNED(child));
3513 ire_refrele(child);
3514 goto restart;
3516 child = next;
3518 rw_exit(&ipst->ips_ire_dep_lock);
3522 * In the preferred/strict src multihoming modes, unbound routes (i.e.,
3523 * ire_t entries with ire_unbound set to B_TRUE) are bound to an interface
3524 * by selecting the first available interface that has an interface route for
3525 * the ire_gateway. If that interface is subsequently brought down, ill_downi()
3526 * will call ire_rebind() so that the unbound route can be bound to some other
3527 * matching interface thereby preserving the intended reachability information
3528 * from the original unbound route.
3530 void
3531 ire_rebind(ire_t *ire)
3533 ire_t *gw_ire, *new_ire;
3534 int match_flags = MATCH_IRE_TYPE;
3535 ill_t *gw_ill;
3536 boolean_t isv6 = (ire->ire_ipversion == IPV6_VERSION);
3537 ip_stack_t *ipst = ire->ire_ipst;
3539 ASSERT(ire->ire_unbound);
3540 again:
3541 if (isv6) {
3542 gw_ire = ire_ftable_lookup_v6(&ire->ire_gateway_addr_v6, 0, 0,
3543 IRE_INTERFACE, NULL, ALL_ZONES, match_flags, 0, ipst,
3544 NULL);
3545 } else {
3546 gw_ire = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
3547 IRE_INTERFACE, NULL, ALL_ZONES, match_flags, 0, ipst,
3548 NULL);
3550 if (gw_ire == NULL) {
3551 /* see comments in ip_rt_add[_v6]() for IPMP */
3552 if (match_flags & MATCH_IRE_TESTHIDDEN)
3553 return;
3555 match_flags |= MATCH_IRE_TESTHIDDEN;
3556 goto again;
3558 gw_ill = gw_ire->ire_ill;
3559 if (isv6) {
3560 new_ire = ire_create_v6(&ire->ire_addr_v6, &ire->ire_mask_v6,
3561 &ire->ire_gateway_addr_v6, ire->ire_type, gw_ill,
3562 ire->ire_zoneid, ire->ire_flags, ipst);
3563 } else {
3564 new_ire = ire_create((uchar_t *)&ire->ire_addr,
3565 (uchar_t *)&ire->ire_mask,
3566 (uchar_t *)&ire->ire_gateway_addr, ire->ire_type, gw_ill,
3567 ire->ire_zoneid, ire->ire_flags, ipst);
3569 ire_refrele(gw_ire);
3570 if (new_ire == NULL)
3571 return;
3572 new_ire->ire_unbound = B_TRUE;
3573 new_ire = ire_add(new_ire);
3574 if (new_ire != NULL)
3575 ire_refrele(new_ire);