1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 /* Copyright (c) 1990 Mentat Inc. */ 26 27 /* 28 * This file contains routines that manipulate Internet Routing Entries (IREs). 29 */ 30 31 #include <sys/types.h> 32 #include <sys/stream.h> 33 #include <sys/stropts.h> 34 #include <sys/strsun.h> 35 #include <sys/strsubr.h> 36 #include <sys/ddi.h> 37 #include <sys/cmn_err.h> 38 #include <sys/policy.h> 39 40 #include <sys/systm.h> 41 #include <sys/kmem.h> 42 #include <sys/param.h> 43 #include <sys/socket.h> 44 #include <net/if.h> 45 #include <net/route.h> 46 #include <netinet/in.h> 47 #include <net/if_dl.h> 48 #include <netinet/ip6.h> 49 #include <netinet/icmp6.h> 50 51 #include <inet/common.h> 52 #include <inet/mi.h> 53 #include <inet/ip.h> 54 #include <inet/ip6.h> 55 #include <inet/ip_ndp.h> 56 #include <inet/arp.h> 57 #include <inet/ip_if.h> 58 #include <inet/ip_ire.h> 59 #include <inet/ip_ftable.h> 60 #include <inet/ip_rts.h> 61 #include <inet/nd.h> 62 63 #include <inet/tcp.h> 64 #include <inet/ipclassifier.h> 65 #include <sys/zone.h> 66 #include <sys/cpuvar.h> 67 68 #include <sys/tsol/label.h> 69 #include <sys/tsol/tnet.h> 70 71 struct kmem_cache *rt_entry_cache; 72 73 typedef struct nce_clookup_s { 74 ipaddr_t ncecl_addr; 75 boolean_t ncecl_found; 76 } nce_clookup_t; 77 78 /* 79 * Synchronization notes: 80 * 81 * The fields of the ire_t struct are protected in the following way : 82 * 83 * ire_next/ire_ptpn 84 * 85 * - bucket lock of the forwarding table in which is ire stored. 86 * 87 * ire_ill, ire_u *except* ire_gateway_addr[v6], ire_mask, 88 * ire_type, ire_create_time, ire_masklen, ire_ipversion, ire_flags, 89 * ire_bucket 90 * 91 * - Set in ire_create_v4/v6 and never changes after that. Thus, 92 * we don't need a lock whenever these fields are accessed. 93 * 94 * - ire_bucket and ire_masklen (also set in ire_create) is set in 95 * ire_add before inserting in the bucket and never 96 * changes after that. Thus we don't need a lock whenever these 97 * fields are accessed. 98 * 99 * ire_gateway_addr_v4[v6] 100 * 101 * - ire_gateway_addr_v4[v6] is set during ire_create and later modified 102 * by rts_setgwr[v6]. As ire_gateway_addr is a uint32_t, updates to 103 * it assumed to be atomic and hence the other parts of the code 104 * does not use any locks. ire_gateway_addr_v6 updates are not atomic 105 * and hence any access to it uses ire_lock to get/set the right value. 106 * 107 * ire_refcnt, ire_identical_ref 108 * 109 * - Updated atomically using atomic_add_32 110 * 111 * ire_ssthresh, ire_rtt_sd, ire_rtt, ire_ib_pkt_count, ire_ob_pkt_count 112 * 113 * - Assumes that 32 bit writes are atomic. No locks. ire_lock is 114 * used to serialize updates to ire_ssthresh, ire_rtt_sd, ire_rtt. 115 * 116 * ire_generation 117 * - Under ire_lock 118 * 119 * ire_nce_cache 120 * - Under ire_lock 121 * 122 * ire_dep_parent (To next IRE in recursive lookup chain) 123 * - Under ips_ire_dep_lock. Write held when modifying. Read held when 124 * walking. We also hold ire_lock when modifying to allow the data path 125 * to only acquire ire_lock. 126 * 127 * ire_dep_parent_generation (Generation number from ire_dep_parent) 128 * - Under ips_ire_dep_lock and/or ire_lock. (A read claim on the dep_lock 129 * and ire_lock held when modifying) 130 * 131 * ire_dep_children (From parent to first child) 132 * ire_dep_sib_next (linked list of siblings) 133 * ire_dep_sib_ptpn (linked list of siblings) 134 * - Under ips_ire_dep_lock. Write held when modifying. Read held when 135 * walking. 136 * 137 * As we always hold the bucket locks in all the places while accessing 138 * the above values, it is natural to use them for protecting them. 139 * 140 * We have a forwarding table for IPv4 and IPv6. The IPv6 forwarding table 141 * (ip_forwarding_table_v6) is an array of pointers to arrays of irb_t 142 * structures. ip_forwarding_table_v6 is allocated dynamically in 143 * ire_add_v6. ire_ft_init_lock is used to serialize multiple threads 144 * initializing the same bucket. Once a bucket is initialized, it is never 145 * de-alloacted. This assumption enables us to access 146 * ip_forwarding_table_v6[i] without any locks. 147 * 148 * The forwarding table for IPv4 is a radix tree whose leaves 149 * are rt_entry structures containing the irb_t for the rt_dst. The irb_t 150 * for IPv4 is dynamically allocated and freed. 151 * 152 * Each irb_t - ire bucket structure has a lock to protect 153 * a bucket and the ires residing in the bucket have a back pointer to 154 * the bucket structure. It also has a reference count for the number 155 * of threads walking the bucket - irb_refcnt which is bumped up 156 * using the irb_refhold function. The flags irb_marks can be 157 * set to IRB_MARK_CONDEMNED indicating that there are some ires 158 * in this bucket that are IRE_IS_CONDEMNED and the 159 * last thread to leave the bucket should delete the ires. Usually 160 * this is done by the irb_refrele function which is used to decrement 161 * the reference count on a bucket. See comments above irb_t structure 162 * definition in ip.h for further details. 163 * 164 * The ire_refhold/ire_refrele functions operate on the ire which increments/ 165 * decrements the reference count, ire_refcnt, atomically on the ire. 166 * ire_refcnt is modified only using those functions. Operations on the IRE 167 * could be described as follows : 168 * 169 * CREATE an ire with reference count initialized to 1. 170 * 171 * ADDITION of an ire holds the bucket lock, checks for duplicates 172 * and then adds the ire. ire_add returns the ire after 173 * bumping up once more i.e the reference count is 2. This is to avoid 174 * an extra lookup in the functions calling ire_add which wants to 175 * work with the ire after adding. 176 * 177 * LOOKUP of an ire bumps up the reference count using ire_refhold 178 * function. It is valid to bump up the referece count of the IRE, 179 * after the lookup has returned an ire. Following are the lookup 180 * functions that return an HELD ire : 181 * 182 * ire_ftable_lookup[_v6], ire_lookup_multi_ill[_v6] 183 * 184 * DELETION of an ire holds the bucket lock, removes it from the list 185 * and then decrements the reference count for having removed from the list 186 * by using the ire_refrele function. If some other thread has looked up 187 * the ire, the reference count would have been bumped up and hence 188 * this ire will not be freed once deleted. It will be freed once the 189 * reference count drops to zero. 190 * 191 * Add and Delete acquires the bucket lock as RW_WRITER, while all the 192 * lookups acquire the bucket lock as RW_READER. 193 * 194 * The general rule is to do the ire_refrele in the function 195 * that is passing the ire as an argument. 196 * 197 * In trying to locate ires the following points are to be noted. 198 * 199 * IRE_IS_CONDEMNED signifies that the ire has been logically deleted and is 200 * to be ignored when walking the ires using ire_next. 201 * 202 * Zones note: 203 * Walking IREs within a given zone also walks certain ires in other 204 * zones. This is done intentionally. IRE walks with a specified 205 * zoneid are used only when doing informational reports, and 206 * zone users want to see things that they can access. See block 207 * comment in ire_walk_ill_match(). 208 */ 209 210 /* 211 * The size of the forwarding table. We will make sure that it is a 212 * power of 2 in ip_ire_init(). 213 * Setable in /etc/system 214 */ 215 uint32_t ip6_ftable_hash_size = IP6_FTABLE_HASH_SIZE; 216 217 struct kmem_cache *ire_cache; 218 struct kmem_cache *ncec_cache; 219 struct kmem_cache *nce_cache; 220 221 static ire_t ire_null; 222 223 static ire_t *ire_add_v4(ire_t *ire); 224 static void ire_delete_v4(ire_t *ire); 225 static void ire_dep_invalidate_children(ire_t *child); 226 static void ire_walk_ipvers(pfv_t func, void *arg, uchar_t vers, 227 zoneid_t zoneid, ip_stack_t *); 228 static void ire_walk_ill_ipvers(uint_t match_flags, uint_t ire_type, 229 pfv_t func, void *arg, uchar_t vers, ill_t *ill); 230 #ifdef DEBUG 231 static void ire_trace_cleanup(const ire_t *); 232 #endif 233 234 /* 235 * Following are the functions to increment/decrement the reference 236 * count of the IREs and IRBs (ire bucket). 237 * 238 * 1) We bump up the reference count of an IRE to make sure that 239 * it does not get deleted and freed while we are using it. 240 * Typically all the lookup functions hold the bucket lock, 241 * and look for the IRE. If it finds an IRE, it bumps up the 242 * reference count before dropping the lock. Sometimes we *may* want 243 * to bump up the reference count after we *looked* up i.e without 244 * holding the bucket lock. So, the ire_refhold function does not assert 245 * on the bucket lock being held. Any thread trying to delete from 246 * the hash bucket can still do so but cannot free the IRE if 247 * ire_refcnt is not 0. 248 * 249 * 2) We bump up the reference count on the bucket where the IRE resides 250 * (IRB), when we want to prevent the IREs getting deleted from a given 251 * hash bucket. This makes life easier for ire_walk type functions which 252 * wants to walk the IRE list, call a function, but needs to drop 253 * the bucket lock to prevent recursive rw_enters. While the 254 * lock is dropped, the list could be changed by other threads or 255 * the same thread could end up deleting the ire or the ire pointed by 256 * ire_next. ire_refholding the ire or ire_next is not sufficient as 257 * a delete will still remove the ire from the bucket while we have 258 * dropped the lock and hence the ire_next would be NULL. Thus, we 259 * need a mechanism to prevent deletions from a given bucket. 260 * 261 * To prevent deletions, we bump up the reference count on the 262 * bucket. If the bucket is held, ire_delete just marks both 263 * the ire and irb as CONDEMNED. When the 264 * reference count on the bucket drops to zero, all the CONDEMNED ires 265 * are deleted. We don't have to bump up the reference count on the 266 * bucket if we are walking the bucket and never have to drop the bucket 267 * lock. Note that irb_refhold does not prevent addition of new ires 268 * in the list. It is okay because addition of new ires will not cause 269 * ire_next to point to freed memory. We do irb_refhold only when 270 * all of the 3 conditions are true : 271 * 272 * 1) The code needs to walk the IRE bucket from start to end. 273 * 2) It may have to drop the bucket lock sometimes while doing (1) 274 * 3) It does not want any ires to be deleted meanwhile. 275 */ 276 277 /* 278 * Bump up the reference count on the hash bucket - IRB to 279 * prevent ires from being deleted in this bucket. 280 */ 281 void 282 irb_refhold(irb_t *irb) 283 { 284 rw_enter(&irb->irb_lock, RW_WRITER); 285 irb->irb_refcnt++; 286 ASSERT(irb->irb_refcnt != 0); 287 rw_exit(&irb->irb_lock); 288 } 289 290 void 291 irb_refhold_locked(irb_t *irb) 292 { 293 ASSERT(RW_WRITE_HELD(&irb->irb_lock)); 294 irb->irb_refcnt++; 295 ASSERT(irb->irb_refcnt != 0); 296 } 297 298 /* 299 * Note: when IRB_MARK_DYNAMIC is not set the irb_t 300 * is statically allocated, so that when the irb_refcnt goes to 0, 301 * we simply clean up the ire list and continue. 302 */ 303 void 304 irb_refrele(irb_t *irb) 305 { 306 if (irb->irb_marks & IRB_MARK_DYNAMIC) { 307 irb_refrele_ftable(irb); 308 } else { 309 rw_enter(&irb->irb_lock, RW_WRITER); 310 ASSERT(irb->irb_refcnt != 0); 311 if (--irb->irb_refcnt == 0 && 312 (irb->irb_marks & IRB_MARK_CONDEMNED)) { 313 ire_t *ire_list; 314 315 ire_list = ire_unlink(irb); 316 rw_exit(&irb->irb_lock); 317 ASSERT(ire_list != NULL); 318 ire_cleanup(ire_list); 319 } else { 320 rw_exit(&irb->irb_lock); 321 } 322 } 323 } 324 325 326 /* 327 * Bump up the reference count on the IRE. We cannot assert that the 328 * bucket lock is being held as it is legal to bump up the reference 329 * count after the first lookup has returned the IRE without 330 * holding the lock. 331 */ 332 void 333 ire_refhold(ire_t *ire) 334 { 335 atomic_add_32(&(ire)->ire_refcnt, 1); 336 ASSERT((ire)->ire_refcnt != 0); 337 #ifdef DEBUG 338 ire_trace_ref(ire); 339 #endif 340 } 341 342 void 343 ire_refhold_notr(ire_t *ire) 344 { 345 atomic_add_32(&(ire)->ire_refcnt, 1); 346 ASSERT((ire)->ire_refcnt != 0); 347 } 348 349 void 350 ire_refhold_locked(ire_t *ire) 351 { 352 #ifdef DEBUG 353 ire_trace_ref(ire); 354 #endif 355 ire->ire_refcnt++; 356 } 357 358 /* 359 * Release a ref on an IRE. 360 * 361 * Must not be called while holding any locks. Otherwise if this is 362 * the last reference to be released there is a chance of recursive mutex 363 * panic due to ire_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying 364 * to restart an ioctl. The one exception is when the caller is sure that 365 * this is not the last reference to be released. Eg. if the caller is 366 * sure that the ire has not been deleted and won't be deleted. 367 * 368 * In architectures e.g sun4u, where atomic_add_32_nv is just 369 * a cas, we need to maintain the right memory barrier semantics 370 * as that of mutex_exit i.e all the loads and stores should complete 371 * before the cas is executed. membar_exit() does that here. 372 */ 373 void 374 ire_refrele(ire_t *ire) 375 { 376 #ifdef DEBUG 377 ire_untrace_ref(ire); 378 #endif 379 ASSERT((ire)->ire_refcnt != 0); 380 membar_exit(); 381 if (atomic_add_32_nv(&(ire)->ire_refcnt, -1) == 0) 382 ire_inactive(ire); 383 } 384 385 void 386 ire_refrele_notr(ire_t *ire) 387 { 388 ASSERT((ire)->ire_refcnt != 0); 389 membar_exit(); 390 if (atomic_add_32_nv(&(ire)->ire_refcnt, -1) == 0) 391 ire_inactive(ire); 392 } 393 394 /* 395 * This function is associated with the IP_IOC_IRE_DELETE[_NO_REPLY] 396 * IOCTL[s]. The NO_REPLY form is used by TCP to tell IP that it is 397 * having problems reaching a particular destination. 398 * This will make IP consider alternate routes (e.g., when there are 399 * muliple default routes), and it will also make IP discard any (potentially) 400 * stale redirect. 401 * Management processes may want to use the version that generates a reply. 402 * 403 * With the use of NUD like behavior for IPv4/ARP in addition to IPv6 404 * this function shouldn't be necessary for IP to recover from a bad redirect, 405 * a bad default router (when there are multiple default routers), or 406 * a stale ND/ARP entry. But we retain it in any case. 407 * For instance, this is helpful when TCP suspects a failure before NUD does. 408 */ 409 int 410 ip_ire_delete(queue_t *q, mblk_t *mp, cred_t *ioc_cr) 411 { 412 uchar_t *addr_ucp; 413 uint_t ipversion; 414 sin_t *sin; 415 sin6_t *sin6; 416 ipaddr_t v4addr; 417 in6_addr_t v6addr; 418 ire_t *ire; 419 ipid_t *ipid; 420 zoneid_t zoneid; 421 ip_stack_t *ipst; 422 423 ASSERT(q->q_next == NULL); 424 zoneid = IPCL_ZONEID(Q_TO_CONN(q)); 425 ipst = CONNQ_TO_IPST(q); 426 427 /* 428 * Check privilege using the ioctl credential; if it is NULL 429 * then this is a kernel message and therefor privileged. 430 */ 431 if (ioc_cr != NULL && secpolicy_ip_config(ioc_cr, B_FALSE) != 0) 432 return (EPERM); 433 434 ipid = (ipid_t *)mp->b_rptr; 435 436 addr_ucp = mi_offset_param(mp, ipid->ipid_addr_offset, 437 ipid->ipid_addr_length); 438 if (addr_ucp == NULL || !OK_32PTR(addr_ucp)) 439 return (EINVAL); 440 switch (ipid->ipid_addr_length) { 441 case sizeof (sin_t): 442 /* 443 * got complete (sockaddr) address - increment addr_ucp to point 444 * at the ip_addr field. 445 */ 446 sin = (sin_t *)addr_ucp; 447 addr_ucp = (uchar_t *)&sin->sin_addr.s_addr; 448 ipversion = IPV4_VERSION; 449 break; 450 case sizeof (sin6_t): 451 /* 452 * got complete (sockaddr) address - increment addr_ucp to point 453 * at the ip_addr field. 454 */ 455 sin6 = (sin6_t *)addr_ucp; 456 addr_ucp = (uchar_t *)&sin6->sin6_addr; 457 ipversion = IPV6_VERSION; 458 break; 459 default: 460 return (EINVAL); 461 } 462 if (ipversion == IPV4_VERSION) { 463 /* Extract the destination address. */ 464 bcopy(addr_ucp, &v4addr, IP_ADDR_LEN); 465 466 ire = ire_ftable_lookup_v4(v4addr, 0, 0, 0, NULL, 467 zoneid, NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL); 468 } else { 469 /* Extract the destination address. */ 470 bcopy(addr_ucp, &v6addr, IPV6_ADDR_LEN); 471 472 ire = ire_ftable_lookup_v6(&v6addr, NULL, NULL, 0, NULL, 473 zoneid, NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL); 474 } 475 if (ire != NULL) { 476 if (ipversion == IPV4_VERSION) { 477 ip_rts_change(RTM_LOSING, ire->ire_addr, 478 ire->ire_gateway_addr, ire->ire_mask, 479 (Q_TO_CONN(q))->conn_laddr_v4, 0, 0, 0, 480 (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_IFA), 481 ire->ire_ipst); 482 } 483 (void) ire_no_good(ire); 484 ire_refrele(ire); 485 } 486 return (0); 487 } 488 489 /* 490 * Initialize the ire that is specific to IPv4 part and call 491 * ire_init_common to finish it. 492 * Returns zero or errno. 493 */ 494 int 495 ire_init_v4(ire_t *ire, uchar_t *addr, uchar_t *mask, uchar_t *gateway, 496 ushort_t type, ill_t *ill, zoneid_t zoneid, uint_t flags, 497 tsol_gc_t *gc, ip_stack_t *ipst) 498 { 499 int error; 500 501 /* 502 * Reject IRE security attribute creation/initialization 503 * if system is not running in Trusted mode. 504 */ 505 if (gc != NULL && !is_system_labeled()) 506 return (EINVAL); 507 508 BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_alloced); 509 510 if (addr != NULL) 511 bcopy(addr, &ire->ire_addr, IP_ADDR_LEN); 512 if (gateway != NULL) 513 bcopy(gateway, &ire->ire_gateway_addr, IP_ADDR_LEN); 514 515 /* Make sure we don't have stray values in some fields */ 516 switch (type) { 517 case IRE_LOOPBACK: 518 bcopy(&ire->ire_addr, &ire->ire_gateway_addr, IP_ADDR_LEN); 519 /* FALLTHRU */ 520 case IRE_HOST: 521 case IRE_BROADCAST: 522 case IRE_LOCAL: 523 case IRE_IF_CLONE: 524 ire->ire_mask = IP_HOST_MASK; 525 ire->ire_masklen = IPV4_ABITS; 526 break; 527 case IRE_PREFIX: 528 case IRE_DEFAULT: 529 case IRE_IF_RESOLVER: 530 case IRE_IF_NORESOLVER: 531 if (mask != NULL) { 532 bcopy(mask, &ire->ire_mask, IP_ADDR_LEN); 533 ire->ire_masklen = ip_mask_to_plen(ire->ire_mask); 534 } 535 break; 536 case IRE_MULTICAST: 537 case IRE_NOROUTE: 538 ASSERT(mask == NULL); 539 break; 540 default: 541 ASSERT(0); 542 return (EINVAL); 543 } 544 545 error = ire_init_common(ire, type, ill, zoneid, flags, IPV4_VERSION, 546 gc, ipst); 547 if (error != NULL) 548 return (error); 549 550 /* Determine which function pointers to use */ 551 ire->ire_postfragfn = ip_xmit; /* Common case */ 552 553 switch (ire->ire_type) { 554 case IRE_LOCAL: 555 ire->ire_sendfn = ire_send_local_v4; 556 ire->ire_recvfn = ire_recv_local_v4; 557 #ifdef SO_VRRP 558 ASSERT(ire->ire_ill != NULL); 559 if (ire->ire_ill->ill_flags & ILLF_NOACCEPT) { 560 ire->ire_noaccept = B_TRUE; 561 ire->ire_recvfn = ire_recv_noaccept_v6; 562 } 563 #endif 564 break; 565 case IRE_LOOPBACK: 566 ire->ire_sendfn = ire_send_local_v4; 567 ire->ire_recvfn = ire_recv_loopback_v4; 568 break; 569 case IRE_BROADCAST: 570 ire->ire_postfragfn = ip_postfrag_loopcheck; 571 ire->ire_sendfn = ire_send_broadcast_v4; 572 ire->ire_recvfn = ire_recv_broadcast_v4; 573 break; 574 case IRE_MULTICAST: 575 ire->ire_postfragfn = ip_postfrag_loopcheck; 576 ire->ire_sendfn = ire_send_multicast_v4; 577 ire->ire_recvfn = ire_recv_multicast_v4; 578 break; 579 default: 580 /* 581 * For IRE_IF_ALL and IRE_OFFLINK we forward received 582 * packets by default. 583 */ 584 ire->ire_sendfn = ire_send_wire_v4; 585 ire->ire_recvfn = ire_recv_forward_v4; 586 break; 587 } 588 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 589 ire->ire_sendfn = ire_send_noroute_v4; 590 ire->ire_recvfn = ire_recv_noroute_v4; 591 } else if (ire->ire_flags & RTF_MULTIRT) { 592 ire->ire_postfragfn = ip_postfrag_multirt_v4; 593 ire->ire_sendfn = ire_send_multirt_v4; 594 /* Multirt receive of broadcast uses ire_recv_broadcast_v4 */ 595 if (ire->ire_type != IRE_BROADCAST) 596 ire->ire_recvfn = ire_recv_multirt_v4; 597 } 598 ire->ire_nce_capable = ire_determine_nce_capable(ire); 599 return (0); 600 } 601 602 /* 603 * Determine ire_nce_capable 604 */ 605 boolean_t 606 ire_determine_nce_capable(ire_t *ire) 607 { 608 int max_masklen; 609 610 if ((ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) || 611 (ire->ire_type & IRE_MULTICAST)) 612 return (B_TRUE); 613 614 if (ire->ire_ipversion == IPV4_VERSION) 615 max_masklen = IPV4_ABITS; 616 else 617 max_masklen = IPV6_ABITS; 618 619 if ((ire->ire_type & IRE_ONLINK) && ire->ire_masklen == max_masklen) 620 return (B_TRUE); 621 return (B_FALSE); 622 } 623 624 /* 625 * ire_create is called to allocate and initialize a new IRE. 626 * 627 * NOTE : This is called as writer sometimes though not required 628 * by this function. 629 */ 630 ire_t * 631 ire_create(uchar_t *addr, uchar_t *mask, uchar_t *gateway, 632 ushort_t type, ill_t *ill, zoneid_t zoneid, uint_t flags, tsol_gc_t *gc, 633 ip_stack_t *ipst) 634 { 635 ire_t *ire; 636 int error; 637 638 ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP); 639 if (ire == NULL) { 640 DTRACE_PROBE(kmem__cache__alloc); 641 return (NULL); 642 } 643 *ire = ire_null; 644 645 error = ire_init_v4(ire, addr, mask, gateway, type, ill, zoneid, flags, 646 gc, ipst); 647 if (error != 0) { 648 DTRACE_PROBE2(ire__init, ire_t *, ire, int, error); 649 kmem_cache_free(ire_cache, ire); 650 return (NULL); 651 } 652 return (ire); 653 } 654 655 /* 656 * Common to IPv4 and IPv6 657 * Returns zero or errno. 658 */ 659 int 660 ire_init_common(ire_t *ire, ushort_t type, ill_t *ill, zoneid_t zoneid, 661 uint_t flags, uchar_t ipversion, tsol_gc_t *gc, ip_stack_t *ipst) 662 { 663 int error; 664 665 #ifdef DEBUG 666 if (ill != NULL) { 667 if (ill->ill_isv6) 668 ASSERT(ipversion == IPV6_VERSION); 669 else 670 ASSERT(ipversion == IPV4_VERSION); 671 } 672 #endif /* DEBUG */ 673 674 /* 675 * Create/initialize IRE security attribute only in Trusted mode; 676 * if the passed in gc is non-NULL, we expect that the caller 677 * has held a reference to it and will release it when this routine 678 * returns a failure, otherwise we own the reference. We do this 679 * prior to initializing the rest IRE fields. 680 */ 681 if (is_system_labeled()) { 682 if ((type & (IRE_LOCAL | IRE_LOOPBACK | IRE_BROADCAST | 683 IRE_IF_ALL | IRE_MULTICAST | IRE_NOROUTE)) != 0) { 684 /* release references on behalf of caller */ 685 if (gc != NULL) 686 GC_REFRELE(gc); 687 } else { 688 error = tsol_ire_init_gwattr(ire, ipversion, gc); 689 if (error != 0) 690 return (error); 691 } 692 } 693 694 ire->ire_type = type; 695 ire->ire_flags = RTF_UP | flags; 696 ire->ire_create_time = (uint32_t)gethrestime_sec(); 697 ire->ire_generation = IRE_GENERATION_INITIAL; 698 699 /* 700 * The ill_ire_cnt isn't increased until 701 * the IRE is added to ensure that a walker will find 702 * all IREs that hold a reference on an ill. 703 * 704 * Note that ill_ire_multicast doesn't hold a ref on the ill since 705 * ire_add() is not called for the IRE_MULTICAST. 706 */ 707 ire->ire_ill = ill; 708 ire->ire_zoneid = zoneid; 709 ire->ire_ipversion = ipversion; 710 711 mutex_init(&ire->ire_lock, NULL, MUTEX_DEFAULT, NULL); 712 ire->ire_refcnt = 1; 713 ire->ire_identical_ref = 1; /* Number of ire_delete's needed */ 714 ire->ire_ipst = ipst; /* No netstack_hold */ 715 ire->ire_trace_disable = B_FALSE; 716 717 return (0); 718 } 719 720 /* 721 * This creates an IRE_BROADCAST based on the arguments. 722 * A mirror is ire_lookup_bcast(). 723 * 724 * Any supression of unneeded ones is done in ire_add_v4. 725 * We add one IRE_BROADCAST per address. ire_send_broadcast_v4() 726 * takes care of generating a loopback copy of the packet. 727 */ 728 ire_t ** 729 ire_create_bcast(ill_t *ill, ipaddr_t addr, zoneid_t zoneid, ire_t **irep) 730 { 731 ip_stack_t *ipst = ill->ill_ipst; 732 733 ASSERT(IAM_WRITER_ILL(ill)); 734 735 *irep++ = ire_create( 736 (uchar_t *)&addr, /* dest addr */ 737 (uchar_t *)&ip_g_all_ones, /* mask */ 738 NULL, /* no gateway */ 739 IRE_BROADCAST, 740 ill, 741 zoneid, 742 RTF_KERNEL, 743 NULL, 744 ipst); 745 746 return (irep); 747 } 748 749 /* 750 * This looks up an IRE_BROADCAST based on the arguments. 751 * Mirrors ire_create_bcast(). 752 */ 753 ire_t * 754 ire_lookup_bcast(ill_t *ill, ipaddr_t addr, zoneid_t zoneid) 755 { 756 ire_t *ire; 757 int match_args; 758 759 match_args = MATCH_IRE_TYPE | MATCH_IRE_ILL | MATCH_IRE_GW | 760 MATCH_IRE_MASK | MATCH_IRE_ZONEONLY; 761 762 if (IS_UNDER_IPMP(ill)) 763 match_args |= MATCH_IRE_TESTHIDDEN; 764 765 ire = ire_ftable_lookup_v4( 766 addr, /* dest addr */ 767 ip_g_all_ones, /* mask */ 768 0, /* no gateway */ 769 IRE_BROADCAST, 770 ill, 771 zoneid, 772 NULL, 773 match_args, 774 0, 775 ill->ill_ipst, 776 NULL); 777 return (ire); 778 } 779 780 /* Arrange to call the specified function for every IRE in the world. */ 781 void 782 ire_walk(pfv_t func, void *arg, ip_stack_t *ipst) 783 { 784 ire_walk_ipvers(func, arg, 0, ALL_ZONES, ipst); 785 } 786 787 void 788 ire_walk_v4(pfv_t func, void *arg, zoneid_t zoneid, ip_stack_t *ipst) 789 { 790 ire_walk_ipvers(func, arg, IPV4_VERSION, zoneid, ipst); 791 } 792 793 void 794 ire_walk_v6(pfv_t func, void *arg, zoneid_t zoneid, ip_stack_t *ipst) 795 { 796 ire_walk_ipvers(func, arg, IPV6_VERSION, zoneid, ipst); 797 } 798 799 /* 800 * Walk a particular version. version == 0 means both v4 and v6. 801 */ 802 static void 803 ire_walk_ipvers(pfv_t func, void *arg, uchar_t vers, zoneid_t zoneid, 804 ip_stack_t *ipst) 805 { 806 if (vers != IPV6_VERSION) { 807 /* 808 * ip_forwarding_table variable doesn't matter for IPv4 since 809 * ire_walk_ill_tables uses ips_ip_ftable for IPv4. 810 */ 811 ire_walk_ill_tables(0, 0, func, arg, IP_MASK_TABLE_SIZE, 812 0, NULL, 813 NULL, zoneid, ipst); 814 } 815 if (vers != IPV4_VERSION) { 816 ire_walk_ill_tables(0, 0, func, arg, IP6_MASK_TABLE_SIZE, 817 ipst->ips_ip6_ftable_hash_size, 818 ipst->ips_ip_forwarding_table_v6, 819 NULL, zoneid, ipst); 820 } 821 } 822 823 /* 824 * Arrange to call the specified function for every IRE that matches the ill. 825 */ 826 void 827 ire_walk_ill(uint_t match_flags, uint_t ire_type, pfv_t func, void *arg, 828 ill_t *ill) 829 { 830 uchar_t vers = (ill->ill_isv6 ? IPV6_VERSION : IPV4_VERSION); 831 832 ire_walk_ill_ipvers(match_flags, ire_type, func, arg, vers, ill); 833 } 834 835 /* 836 * Walk a particular ill and version. 837 */ 838 static void 839 ire_walk_ill_ipvers(uint_t match_flags, uint_t ire_type, pfv_t func, 840 void *arg, uchar_t vers, ill_t *ill) 841 { 842 ip_stack_t *ipst = ill->ill_ipst; 843 844 if (vers == IPV4_VERSION) { 845 ire_walk_ill_tables(match_flags, ire_type, func, arg, 846 IP_MASK_TABLE_SIZE, 847 0, NULL, 848 ill, ALL_ZONES, ipst); 849 } 850 if (vers != IPV4_VERSION) { 851 ire_walk_ill_tables(match_flags, ire_type, func, arg, 852 IP6_MASK_TABLE_SIZE, ipst->ips_ip6_ftable_hash_size, 853 ipst->ips_ip_forwarding_table_v6, 854 ill, ALL_ZONES, ipst); 855 } 856 } 857 858 /* 859 * Do the specific matching of IREs to shared-IP zones. 860 * 861 * We have the same logic as in ire_match_args but implemented slightly 862 * differently. 863 */ 864 boolean_t 865 ire_walk_ill_match(uint_t match_flags, uint_t ire_type, ire_t *ire, 866 ill_t *ill, zoneid_t zoneid, ip_stack_t *ipst) 867 { 868 ill_t *dst_ill = NULL; 869 870 ASSERT(match_flags != 0 || zoneid != ALL_ZONES); 871 if (match_flags & MATCH_IRE_ILL) { 872 dst_ill = ire->ire_ill; 873 } 874 875 if (zoneid != ALL_ZONES && zoneid != ire->ire_zoneid && 876 ire->ire_zoneid != ALL_ZONES) { 877 /* 878 * We're walking the IREs for a specific zone. The only relevant 879 * IREs are: 880 * - all IREs with a matching ire_zoneid 881 * - IRE_IF_ALL IREs for interfaces with a usable source addr 882 * with a matching zone 883 * - IRE_OFFLINK with a gateway reachable from the zone 884 * Note that ealier we only did the IRE_OFFLINK check for 885 * IRE_DEFAULT (and only when we had multiple IRE_DEFAULTs). 886 */ 887 dst_ill = ire->ire_ill; 888 889 if (ire->ire_type & IRE_ONLINK) { 890 uint_t ifindex; 891 892 /* 893 * Note there is no IRE_INTERFACE on vniN thus 894 * can't do an IRE lookup for a matching route. 895 */ 896 ifindex = dst_ill->ill_usesrc_ifindex; 897 if (ifindex == 0) 898 return (B_FALSE); 899 900 /* 901 * If there is a usable source address in the 902 * zone, then it's ok to return an 903 * IRE_INTERFACE 904 */ 905 if (!ipif_zone_avail(ifindex, dst_ill->ill_isv6, 906 zoneid, ipst)) { 907 return (B_FALSE); 908 } 909 } 910 911 if (dst_ill != NULL && (ire->ire_type & IRE_OFFLINK)) { 912 ipif_t *tipif; 913 914 mutex_enter(&dst_ill->ill_lock); 915 for (tipif = dst_ill->ill_ipif; 916 tipif != NULL; tipif = tipif->ipif_next) { 917 if (!IPIF_IS_CONDEMNED(tipif) && 918 (tipif->ipif_flags & IPIF_UP) && 919 (tipif->ipif_zoneid == zoneid || 920 tipif->ipif_zoneid == ALL_ZONES)) 921 break; 922 } 923 mutex_exit(&dst_ill->ill_lock); 924 if (tipif == NULL) { 925 return (B_FALSE); 926 } 927 } 928 929 /* 930 * Match all offlink routes from the global zone, irrespective 931 * of reachability. For a non-global zone only match those 932 * where ire_gateway_addr has an IRE_INTERFACE for the zoneid. 933 */ 934 if ((ire->ire_type & IRE_OFFLINK) && zoneid != GLOBAL_ZONEID && 935 zoneid != ALL_ZONES) { 936 in6_addr_t gw_addr_v6; 937 938 if (ire->ire_ipversion == IPV4_VERSION) { 939 if (!ire_gateway_ok_zone_v4( 940 ire->ire_gateway_addr, zoneid, 941 dst_ill, NULL, ipst, B_FALSE)) 942 return (B_FALSE); 943 } else { 944 ASSERT(ire->ire_ipversion == IPV6_VERSION); 945 mutex_enter(&ire->ire_lock); 946 gw_addr_v6 = ire->ire_gateway_addr_v6; 947 mutex_exit(&ire->ire_lock); 948 949 if (!ire_gateway_ok_zone_v6(&gw_addr_v6, zoneid, 950 dst_ill, NULL, ipst, B_FALSE)) 951 return (B_FALSE); 952 } 953 } 954 } 955 956 if (((!(match_flags & MATCH_IRE_TYPE)) || 957 (ire->ire_type & ire_type)) && 958 ((!(match_flags & MATCH_IRE_ILL)) || 959 (dst_ill == ill || 960 dst_ill != NULL && IS_IN_SAME_ILLGRP(dst_ill, ill)))) { 961 return (B_TRUE); 962 } 963 return (B_FALSE); 964 } 965 966 int 967 rtfunc(struct radix_node *rn, void *arg) 968 { 969 struct rtfuncarg *rtf = arg; 970 struct rt_entry *rt; 971 irb_t *irb; 972 ire_t *ire; 973 boolean_t ret; 974 975 rt = (struct rt_entry *)rn; 976 ASSERT(rt != NULL); 977 irb = &rt->rt_irb; 978 for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) { 979 if ((rtf->rt_match_flags != 0) || 980 (rtf->rt_zoneid != ALL_ZONES)) { 981 ret = ire_walk_ill_match(rtf->rt_match_flags, 982 rtf->rt_ire_type, ire, 983 rtf->rt_ill, rtf->rt_zoneid, rtf->rt_ipst); 984 } else { 985 ret = B_TRUE; 986 } 987 if (ret) 988 (*rtf->rt_func)(ire, rtf->rt_arg); 989 } 990 return (0); 991 } 992 993 /* 994 * Walk the ftable entries that match the ill. 995 */ 996 void 997 ire_walk_ill_tables(uint_t match_flags, uint_t ire_type, pfv_t func, 998 void *arg, size_t ftbl_sz, size_t htbl_sz, irb_t **ipftbl, 999 ill_t *ill, zoneid_t zoneid, 1000 ip_stack_t *ipst) 1001 { 1002 irb_t *irb_ptr; 1003 irb_t *irb; 1004 ire_t *ire; 1005 int i, j; 1006 boolean_t ret; 1007 struct rtfuncarg rtfarg; 1008 1009 ASSERT((!(match_flags & MATCH_IRE_ILL)) || (ill != NULL)); 1010 ASSERT(!(match_flags & MATCH_IRE_TYPE) || (ire_type != 0)); 1011 1012 /* knobs such that routine is called only for v6 case */ 1013 if (ipftbl == ipst->ips_ip_forwarding_table_v6) { 1014 for (i = (ftbl_sz - 1); i >= 0; i--) { 1015 if ((irb_ptr = ipftbl[i]) == NULL) 1016 continue; 1017 for (j = 0; j < htbl_sz; j++) { 1018 irb = &irb_ptr[j]; 1019 if (irb->irb_ire == NULL) 1020 continue; 1021 1022 irb_refhold(irb); 1023 for (ire = irb->irb_ire; ire != NULL; 1024 ire = ire->ire_next) { 1025 if (match_flags == 0 && 1026 zoneid == ALL_ZONES) { 1027 ret = B_TRUE; 1028 } else { 1029 ret = 1030 ire_walk_ill_match( 1031 match_flags, 1032 ire_type, ire, ill, 1033 zoneid, ipst); 1034 } 1035 if (ret) 1036 (*func)(ire, arg); 1037 } 1038 irb_refrele(irb); 1039 } 1040 } 1041 } else { 1042 (void) memset(&rtfarg, 0, sizeof (rtfarg)); 1043 rtfarg.rt_func = func; 1044 rtfarg.rt_arg = arg; 1045 if (match_flags != 0) { 1046 rtfarg.rt_match_flags = match_flags; 1047 } 1048 rtfarg.rt_ire_type = ire_type; 1049 rtfarg.rt_ill = ill; 1050 rtfarg.rt_zoneid = zoneid; 1051 rtfarg.rt_ipst = ipst; /* No netstack_hold */ 1052 (void) ipst->ips_ip_ftable->rnh_walktree_mt( 1053 ipst->ips_ip_ftable, 1054 rtfunc, &rtfarg, irb_refhold_rn, irb_refrele_rn); 1055 } 1056 } 1057 1058 /* 1059 * This function takes a mask and returns 1060 * number of bits set in the mask. If no 1061 * bit is set it returns 0. 1062 * Assumes a contiguous mask. 1063 */ 1064 int 1065 ip_mask_to_plen(ipaddr_t mask) 1066 { 1067 return (mask == 0 ? 0 : IP_ABITS - (ffs(ntohl(mask)) -1)); 1068 } 1069 1070 /* 1071 * Convert length for a mask to the mask. 1072 */ 1073 ipaddr_t 1074 ip_plen_to_mask(uint_t masklen) 1075 { 1076 if (masklen == 0) 1077 return (0); 1078 1079 return (htonl(IP_HOST_MASK << (IP_ABITS - masklen))); 1080 } 1081 1082 void 1083 ire_atomic_end(irb_t *irb_ptr, ire_t *ire) 1084 { 1085 ill_t *ill; 1086 1087 ill = ire->ire_ill; 1088 if (ill != NULL) 1089 mutex_exit(&ill->ill_lock); 1090 rw_exit(&irb_ptr->irb_lock); 1091 } 1092 1093 /* 1094 * ire_add_v[46] atomically make sure that the ill associated 1095 * with the new ire is not going away i.e., we check ILL_CONDEMNED. 1096 */ 1097 int 1098 ire_atomic_start(irb_t *irb_ptr, ire_t *ire) 1099 { 1100 ill_t *ill; 1101 1102 ill = ire->ire_ill; 1103 1104 rw_enter(&irb_ptr->irb_lock, RW_WRITER); 1105 if (ill != NULL) { 1106 mutex_enter(&ill->ill_lock); 1107 1108 /* 1109 * Don't allow IRE's to be created on dying ills. 1110 */ 1111 if (ill->ill_state_flags & ILL_CONDEMNED) { 1112 ire_atomic_end(irb_ptr, ire); 1113 return (ENXIO); 1114 } 1115 1116 if (IS_UNDER_IPMP(ill)) { 1117 int error = 0; 1118 mutex_enter(&ill->ill_phyint->phyint_lock); 1119 if (!ipmp_ill_is_active(ill) && 1120 IRE_HIDDEN_TYPE(ire->ire_type) && 1121 !ire->ire_testhidden) { 1122 error = EINVAL; 1123 } 1124 mutex_exit(&ill->ill_phyint->phyint_lock); 1125 if (error != 0) { 1126 ire_atomic_end(irb_ptr, ire); 1127 return (error); 1128 } 1129 } 1130 1131 } 1132 return (0); 1133 } 1134 1135 /* 1136 * Add a fully initialized IRE to the forwarding table. 1137 * This returns NULL on failure, or a held IRE on success. 1138 * Normally the returned IRE is the same as the argument. But a different 1139 * IRE will be returned if the added IRE is deemed identical to an existing 1140 * one. In that case ire_identical_ref will be increased. 1141 * The caller always needs to do an ire_refrele() on the returned IRE. 1142 */ 1143 ire_t * 1144 ire_add(ire_t *ire) 1145 { 1146 if (IRE_HIDDEN_TYPE(ire->ire_type) && 1147 ire->ire_ill != NULL && IS_UNDER_IPMP(ire->ire_ill)) { 1148 /* 1149 * IREs hosted on interfaces that are under IPMP 1150 * should be hidden so that applications don't 1151 * accidentally end up sending packets with test 1152 * addresses as their source addresses, or 1153 * sending out interfaces that are e.g. IFF_INACTIVE. 1154 * Hide them here. 1155 */ 1156 ire->ire_testhidden = B_TRUE; 1157 } 1158 1159 if (ire->ire_ipversion == IPV6_VERSION) 1160 return (ire_add_v6(ire)); 1161 else 1162 return (ire_add_v4(ire)); 1163 } 1164 1165 /* 1166 * Add a fully initialized IPv4 IRE to the forwarding table. 1167 * This returns NULL on failure, or a held IRE on success. 1168 * Normally the returned IRE is the same as the argument. But a different 1169 * IRE will be returned if the added IRE is deemed identical to an existing 1170 * one. In that case ire_identical_ref will be increased. 1171 * The caller always needs to do an ire_refrele() on the returned IRE. 1172 */ 1173 static ire_t * 1174 ire_add_v4(ire_t *ire) 1175 { 1176 ire_t *ire1; 1177 irb_t *irb_ptr; 1178 ire_t **irep; 1179 int match_flags; 1180 int error; 1181 ip_stack_t *ipst = ire->ire_ipst; 1182 1183 if (ire->ire_ill != NULL) 1184 ASSERT(!MUTEX_HELD(&ire->ire_ill->ill_lock)); 1185 ASSERT(ire->ire_ipversion == IPV4_VERSION); 1186 1187 /* Make sure the address is properly masked. */ 1188 ire->ire_addr &= ire->ire_mask; 1189 1190 match_flags = (MATCH_IRE_MASK | MATCH_IRE_TYPE | MATCH_IRE_GW); 1191 1192 if (ire->ire_ill != NULL) { 1193 match_flags |= MATCH_IRE_ILL; 1194 } 1195 irb_ptr = ire_get_bucket(ire); 1196 if (irb_ptr == NULL) { 1197 printf("no bucket for %p\n", (void *)ire); 1198 ire_delete(ire); 1199 return (NULL); 1200 } 1201 1202 /* 1203 * Start the atomic add of the ire. Grab the ill lock, 1204 * the bucket lock. Check for condemned. 1205 */ 1206 error = ire_atomic_start(irb_ptr, ire); 1207 if (error != 0) { 1208 printf("no ire_atomic_start for %p\n", (void *)ire); 1209 ire_delete(ire); 1210 irb_refrele(irb_ptr); 1211 return (NULL); 1212 } 1213 /* 1214 * If we are creating a hidden IRE, make sure we search for 1215 * hidden IREs when searching for duplicates below. 1216 * Otherwise, we might find an IRE on some other interface 1217 * that's not marked hidden. 1218 */ 1219 if (ire->ire_testhidden) 1220 match_flags |= MATCH_IRE_TESTHIDDEN; 1221 1222 /* 1223 * Atomically check for duplicate and insert in the table. 1224 */ 1225 for (ire1 = irb_ptr->irb_ire; ire1 != NULL; ire1 = ire1->ire_next) { 1226 if (IRE_IS_CONDEMNED(ire1)) 1227 continue; 1228 /* 1229 * Here we need an exact match on zoneid, i.e., 1230 * ire_match_args doesn't fit. 1231 */ 1232 if (ire1->ire_zoneid != ire->ire_zoneid) 1233 continue; 1234 1235 if (ire1->ire_type != ire->ire_type) 1236 continue; 1237 1238 /* 1239 * Note: We do not allow multiple routes that differ only 1240 * in the gateway security attributes; such routes are 1241 * considered duplicates. 1242 * To change that we explicitly have to treat them as 1243 * different here. 1244 */ 1245 if (ire_match_args(ire1, ire->ire_addr, ire->ire_mask, 1246 ire->ire_gateway_addr, ire->ire_type, ire->ire_ill, 1247 ire->ire_zoneid, NULL, match_flags)) { 1248 /* 1249 * Return the old ire after doing a REFHOLD. 1250 * As most of the callers continue to use the IRE 1251 * after adding, we return a held ire. This will 1252 * avoid a lookup in the caller again. If the callers 1253 * don't want to use it, they need to do a REFRELE. 1254 */ 1255 atomic_add_32(&ire1->ire_identical_ref, 1); 1256 DTRACE_PROBE2(ire__add__exist, ire_t *, ire1, 1257 ire_t *, ire); 1258 ire_refhold(ire1); 1259 ire_atomic_end(irb_ptr, ire); 1260 ire_delete(ire); 1261 irb_refrele(irb_ptr); 1262 return (ire1); 1263 } 1264 } 1265 1266 /* 1267 * Normally we do head insertion since most things do not care about 1268 * the order of the IREs in the bucket. Note that ip_cgtp_bcast_add 1269 * assumes we at least do head insertion so that its IRE_BROADCAST 1270 * arrive ahead of existing IRE_HOST for the same address. 1271 * However, due to shared-IP zones (and restrict_interzone_loopback) 1272 * we can have an IRE_LOCAL as well as IRE_IF_CLONE for the same 1273 * address. For that reason we do tail insertion for IRE_IF_CLONE. 1274 * Due to the IRE_BROADCAST on cgtp0, which must be last in the bucket, 1275 * we do tail insertion of IRE_BROADCASTs that do not have RTF_MULTIRT 1276 * set. 1277 */ 1278 irep = (ire_t **)irb_ptr; 1279 if ((ire->ire_type & IRE_IF_CLONE) || 1280 ((ire->ire_type & IRE_BROADCAST) && 1281 !(ire->ire_flags & RTF_MULTIRT))) { 1282 while ((ire1 = *irep) != NULL) 1283 irep = &ire1->ire_next; 1284 } 1285 /* Insert at *irep */ 1286 ire1 = *irep; 1287 if (ire1 != NULL) 1288 ire1->ire_ptpn = &ire->ire_next; 1289 ire->ire_next = ire1; 1290 /* Link the new one in. */ 1291 ire->ire_ptpn = irep; 1292 1293 /* 1294 * ire_walk routines de-reference ire_next without holding 1295 * a lock. Before we point to the new ire, we want to make 1296 * sure the store that sets the ire_next of the new ire 1297 * reaches global visibility, so that ire_walk routines 1298 * don't see a truncated list of ires i.e if the ire_next 1299 * of the new ire gets set after we do "*irep = ire" due 1300 * to re-ordering, the ire_walk thread will see a NULL 1301 * once it accesses the ire_next of the new ire. 1302 * membar_producer() makes sure that the following store 1303 * happens *after* all of the above stores. 1304 */ 1305 membar_producer(); 1306 *irep = ire; 1307 ire->ire_bucket = irb_ptr; 1308 /* 1309 * We return a bumped up IRE above. Keep it symmetrical 1310 * so that the callers will always have to release. This 1311 * helps the callers of this function because they continue 1312 * to use the IRE after adding and hence they don't have to 1313 * lookup again after we return the IRE. 1314 * 1315 * NOTE : We don't have to use atomics as this is appearing 1316 * in the list for the first time and no one else can bump 1317 * up the reference count on this yet. 1318 */ 1319 ire_refhold_locked(ire); 1320 BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted); 1321 1322 irb_ptr->irb_ire_cnt++; 1323 if (irb_ptr->irb_marks & IRB_MARK_DYNAMIC) 1324 irb_ptr->irb_nire++; 1325 1326 if (ire->ire_ill != NULL) { 1327 ire->ire_ill->ill_ire_cnt++; 1328 ASSERT(ire->ire_ill->ill_ire_cnt != 0); /* Wraparound */ 1329 } 1330 1331 ire_atomic_end(irb_ptr, ire); 1332 1333 /* Make any caching of the IREs be notified or updated */ 1334 ire_flush_cache_v4(ire, IRE_FLUSH_ADD); 1335 1336 if (ire->ire_ill != NULL) 1337 ASSERT(!MUTEX_HELD(&ire->ire_ill->ill_lock)); 1338 irb_refrele(irb_ptr); 1339 return (ire); 1340 } 1341 1342 /* 1343 * irb_refrele is the only caller of the function. ire_unlink calls to 1344 * do the final cleanup for this ire. 1345 */ 1346 void 1347 ire_cleanup(ire_t *ire) 1348 { 1349 ire_t *ire_next; 1350 ip_stack_t *ipst = ire->ire_ipst; 1351 1352 ASSERT(ire != NULL); 1353 1354 while (ire != NULL) { 1355 ire_next = ire->ire_next; 1356 if (ire->ire_ipversion == IPV4_VERSION) { 1357 ire_delete_v4(ire); 1358 BUMP_IRE_STATS(ipst->ips_ire_stats_v4, 1359 ire_stats_deleted); 1360 } else { 1361 ASSERT(ire->ire_ipversion == IPV6_VERSION); 1362 ire_delete_v6(ire); 1363 BUMP_IRE_STATS(ipst->ips_ire_stats_v6, 1364 ire_stats_deleted); 1365 } 1366 /* 1367 * Now it's really out of the list. Before doing the 1368 * REFRELE, set ire_next to NULL as ire_inactive asserts 1369 * so. 1370 */ 1371 ire->ire_next = NULL; 1372 ire_refrele_notr(ire); 1373 ire = ire_next; 1374 } 1375 } 1376 1377 /* 1378 * irb_refrele is the only caller of the function. It calls to unlink 1379 * all the CONDEMNED ires from this bucket. 1380 */ 1381 ire_t * 1382 ire_unlink(irb_t *irb) 1383 { 1384 ire_t *ire; 1385 ire_t *ire1; 1386 ire_t **ptpn; 1387 ire_t *ire_list = NULL; 1388 1389 ASSERT(RW_WRITE_HELD(&irb->irb_lock)); 1390 ASSERT(((irb->irb_marks & IRB_MARK_DYNAMIC) && irb->irb_refcnt == 1) || 1391 (irb->irb_refcnt == 0)); 1392 ASSERT(irb->irb_marks & IRB_MARK_CONDEMNED); 1393 ASSERT(irb->irb_ire != NULL); 1394 1395 for (ire = irb->irb_ire; ire != NULL; ire = ire1) { 1396 ire1 = ire->ire_next; 1397 if (IRE_IS_CONDEMNED(ire)) { 1398 ptpn = ire->ire_ptpn; 1399 ire1 = ire->ire_next; 1400 if (ire1) 1401 ire1->ire_ptpn = ptpn; 1402 *ptpn = ire1; 1403 ire->ire_ptpn = NULL; 1404 ire->ire_next = NULL; 1405 1406 /* 1407 * We need to call ire_delete_v4 or ire_delete_v6 to 1408 * clean up dependents and the redirects pointing at 1409 * the default gateway. We need to drop the lock 1410 * as ire_flush_cache/ire_delete_host_redircts require 1411 * so. But we can't drop the lock, as ire_unlink needs 1412 * to atomically remove the ires from the list. 1413 * So, create a temporary list of CONDEMNED ires 1414 * for doing ire_delete_v4/ire_delete_v6 operations 1415 * later on. 1416 */ 1417 ire->ire_next = ire_list; 1418 ire_list = ire; 1419 } 1420 } 1421 irb->irb_marks &= ~IRB_MARK_CONDEMNED; 1422 return (ire_list); 1423 } 1424 1425 /* 1426 * Clean up the radix node for this ire. Must be called by irb_refrele 1427 * when there are no ire's left in the bucket. Returns TRUE if the bucket 1428 * is deleted and freed. 1429 */ 1430 boolean_t 1431 irb_inactive(irb_t *irb) 1432 { 1433 struct rt_entry *rt; 1434 struct radix_node *rn; 1435 ip_stack_t *ipst = irb->irb_ipst; 1436 1437 ASSERT(irb->irb_ipst != NULL); 1438 1439 rt = IRB2RT(irb); 1440 rn = (struct radix_node *)rt; 1441 1442 /* first remove it from the radix tree. */ 1443 RADIX_NODE_HEAD_WLOCK(ipst->ips_ip_ftable); 1444 rw_enter(&irb->irb_lock, RW_WRITER); 1445 if (irb->irb_refcnt == 1 && irb->irb_nire == 0) { 1446 rn = ipst->ips_ip_ftable->rnh_deladdr(rn->rn_key, rn->rn_mask, 1447 ipst->ips_ip_ftable); 1448 DTRACE_PROBE1(irb__free, rt_t *, rt); 1449 ASSERT((void *)rn == (void *)rt); 1450 Free(rt, rt_entry_cache); 1451 /* irb_lock is freed */ 1452 RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable); 1453 return (B_TRUE); 1454 } 1455 rw_exit(&irb->irb_lock); 1456 RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable); 1457 return (B_FALSE); 1458 } 1459 1460 /* 1461 * Delete the specified IRE. 1462 * We assume that if ire_bucket is not set then ire_ill->ill_ire_cnt was 1463 * not incremented i.e., that the insertion in the bucket and the increment 1464 * of that counter is done atomically. 1465 */ 1466 void 1467 ire_delete(ire_t *ire) 1468 { 1469 ire_t *ire1; 1470 ire_t **ptpn; 1471 irb_t *irb; 1472 nce_t *nce; 1473 ip_stack_t *ipst = ire->ire_ipst; 1474 1475 /* We can clear ire_nce_cache under ire_lock even if the IRE is used */ 1476 mutex_enter(&ire->ire_lock); 1477 nce = ire->ire_nce_cache; 1478 ire->ire_nce_cache = NULL; 1479 mutex_exit(&ire->ire_lock); 1480 if (nce != NULL) 1481 nce_refrele(nce); 1482 1483 if ((irb = ire->ire_bucket) == NULL) { 1484 /* 1485 * It was never inserted in the list. Should call REFRELE 1486 * to free this IRE. 1487 */ 1488 ire_refrele_notr(ire); 1489 return; 1490 } 1491 1492 /* 1493 * Move the use counts from an IRE_IF_CLONE to its parent 1494 * IRE_INTERFACE. 1495 * We need to do this before acquiring irb_lock. 1496 */ 1497 if (ire->ire_type & IRE_IF_CLONE) { 1498 ire_t *parent; 1499 1500 rw_enter(&ipst->ips_ire_dep_lock, RW_READER); 1501 if ((parent = ire->ire_dep_parent) != NULL) { 1502 parent->ire_ob_pkt_count += ire->ire_ob_pkt_count; 1503 parent->ire_ib_pkt_count += ire->ire_ib_pkt_count; 1504 ire->ire_ob_pkt_count = 0; 1505 ire->ire_ib_pkt_count = 0; 1506 } 1507 rw_exit(&ipst->ips_ire_dep_lock); 1508 } 1509 1510 rw_enter(&irb->irb_lock, RW_WRITER); 1511 if (ire->ire_ptpn == NULL) { 1512 /* 1513 * Some other thread has removed us from the list. 1514 * It should have done the REFRELE for us. 1515 */ 1516 rw_exit(&irb->irb_lock); 1517 return; 1518 } 1519 1520 if (!IRE_IS_CONDEMNED(ire)) { 1521 /* Is this an IRE representing multiple duplicate entries? */ 1522 ASSERT(ire->ire_identical_ref >= 1); 1523 if (atomic_add_32_nv(&ire->ire_identical_ref, -1) != 0) { 1524 /* Removed one of the identical parties */ 1525 rw_exit(&irb->irb_lock); 1526 return; 1527 } 1528 1529 irb->irb_ire_cnt--; 1530 ire_make_condemned(ire); 1531 } 1532 1533 if (irb->irb_refcnt != 0) { 1534 /* 1535 * The last thread to leave this bucket will 1536 * delete this ire. 1537 */ 1538 irb->irb_marks |= IRB_MARK_CONDEMNED; 1539 rw_exit(&irb->irb_lock); 1540 return; 1541 } 1542 1543 /* 1544 * Normally to delete an ire, we walk the bucket. While we 1545 * walk the bucket, we normally bump up irb_refcnt and hence 1546 * we return from above where we mark CONDEMNED and the ire 1547 * gets deleted from ire_unlink. This case is where somebody 1548 * knows the ire e.g by doing a lookup, and wants to delete the 1549 * IRE. irb_refcnt would be 0 in this case if nobody is walking 1550 * the bucket. 1551 */ 1552 ptpn = ire->ire_ptpn; 1553 ire1 = ire->ire_next; 1554 if (ire1 != NULL) 1555 ire1->ire_ptpn = ptpn; 1556 ASSERT(ptpn != NULL); 1557 *ptpn = ire1; 1558 ire->ire_ptpn = NULL; 1559 ire->ire_next = NULL; 1560 if (ire->ire_ipversion == IPV6_VERSION) { 1561 BUMP_IRE_STATS(ipst->ips_ire_stats_v6, ire_stats_deleted); 1562 } else { 1563 BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_deleted); 1564 } 1565 rw_exit(&irb->irb_lock); 1566 1567 /* Cleanup dependents and related stuff */ 1568 if (ire->ire_ipversion == IPV6_VERSION) { 1569 ire_delete_v6(ire); 1570 } else { 1571 ire_delete_v4(ire); 1572 } 1573 /* 1574 * We removed it from the list. Decrement the 1575 * reference count. 1576 */ 1577 ire_refrele_notr(ire); 1578 } 1579 1580 /* 1581 * Delete the specified IRE. 1582 * All calls should use ire_delete(). 1583 * Sometimes called as writer though not required by this function. 1584 * 1585 * NOTE : This function is called only if the ire was added 1586 * in the list. 1587 */ 1588 static void 1589 ire_delete_v4(ire_t *ire) 1590 { 1591 ip_stack_t *ipst = ire->ire_ipst; 1592 1593 ASSERT(ire->ire_refcnt >= 1); 1594 ASSERT(ire->ire_ipversion == IPV4_VERSION); 1595 1596 ire_flush_cache_v4(ire, IRE_FLUSH_DELETE); 1597 if (ire->ire_type == IRE_DEFAULT) { 1598 /* 1599 * when a default gateway is going away 1600 * delete all the host redirects pointing at that 1601 * gateway. 1602 */ 1603 ire_delete_host_redirects(ire->ire_gateway_addr, ipst); 1604 } 1605 1606 /* 1607 * If we are deleting an IRE_INTERFACE then we make sure we also 1608 * delete any IRE_IF_CLONE that has been created from it. 1609 * Those are always in ire_dep_children. 1610 */ 1611 if ((ire->ire_type & IRE_INTERFACE) && ire->ire_dep_children != NULL) 1612 ire_dep_delete_if_clone(ire); 1613 1614 /* Remove from parent dependencies and child */ 1615 rw_enter(&ipst->ips_ire_dep_lock, RW_WRITER); 1616 if (ire->ire_dep_parent != NULL) 1617 ire_dep_remove(ire); 1618 1619 while (ire->ire_dep_children != NULL) 1620 ire_dep_remove(ire->ire_dep_children); 1621 rw_exit(&ipst->ips_ire_dep_lock); 1622 } 1623 1624 /* 1625 * ire_refrele is the only caller of the function. It calls 1626 * to free the ire when the reference count goes to zero. 1627 */ 1628 void 1629 ire_inactive(ire_t *ire) 1630 { 1631 ill_t *ill; 1632 irb_t *irb; 1633 ip_stack_t *ipst = ire->ire_ipst; 1634 1635 ASSERT(ire->ire_refcnt == 0); 1636 ASSERT(ire->ire_ptpn == NULL); 1637 ASSERT(ire->ire_next == NULL); 1638 1639 /* Count how many condemned ires for kmem_cache callback */ 1640 if (IRE_IS_CONDEMNED(ire)) 1641 atomic_add_32(&ipst->ips_num_ire_condemned, -1); 1642 1643 if (ire->ire_gw_secattr != NULL) { 1644 ire_gw_secattr_free(ire->ire_gw_secattr); 1645 ire->ire_gw_secattr = NULL; 1646 } 1647 1648 /* 1649 * ire_nce_cache is cleared in ire_delete, and we make sure we don't 1650 * set it once the ire is marked condemned. 1651 */ 1652 ASSERT(ire->ire_nce_cache == NULL); 1653 1654 /* 1655 * Since any parent would have a refhold on us they would already 1656 * have been removed. 1657 */ 1658 ASSERT(ire->ire_dep_parent == NULL); 1659 ASSERT(ire->ire_dep_sib_next == NULL); 1660 ASSERT(ire->ire_dep_sib_ptpn == NULL); 1661 1662 /* 1663 * Since any children would have a refhold on us they should have 1664 * already been removed. 1665 */ 1666 ASSERT(ire->ire_dep_children == NULL); 1667 1668 /* 1669 * ill_ire_ref is increased when the IRE is inserted in the 1670 * bucket - not when the IRE is created. 1671 */ 1672 irb = ire->ire_bucket; 1673 ill = ire->ire_ill; 1674 if (irb != NULL && ill != NULL) { 1675 mutex_enter(&ill->ill_lock); 1676 ASSERT(ill->ill_ire_cnt != 0); 1677 DTRACE_PROBE3(ill__decr__cnt, (ill_t *), ill, 1678 (char *), "ire", (void *), ire); 1679 ill->ill_ire_cnt--; 1680 if (ILL_DOWN_OK(ill)) { 1681 /* Drops the ill lock */ 1682 ipif_ill_refrele_tail(ill); 1683 } else { 1684 mutex_exit(&ill->ill_lock); 1685 } 1686 } 1687 ire->ire_ill = NULL; 1688 1689 /* This should be true for both V4 and V6 */ 1690 if (irb != NULL && (irb->irb_marks & IRB_MARK_DYNAMIC)) { 1691 rw_enter(&irb->irb_lock, RW_WRITER); 1692 irb->irb_nire--; 1693 /* 1694 * Instead of examining the conditions for freeing 1695 * the radix node here, we do it by calling 1696 * irb_refrele which is a single point in the code 1697 * that embeds that logic. Bump up the refcnt to 1698 * be able to call irb_refrele 1699 */ 1700 irb_refhold_locked(irb); 1701 rw_exit(&irb->irb_lock); 1702 irb_refrele(irb); 1703 } 1704 1705 #ifdef DEBUG 1706 ire_trace_cleanup(ire); 1707 #endif 1708 mutex_destroy(&ire->ire_lock); 1709 if (ire->ire_ipversion == IPV6_VERSION) { 1710 BUMP_IRE_STATS(ipst->ips_ire_stats_v6, ire_stats_freed); 1711 } else { 1712 BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_freed); 1713 } 1714 kmem_cache_free(ire_cache, ire); 1715 } 1716 1717 /* 1718 * ire_update_generation is the callback function provided by 1719 * ire_get_bucket() to update the generation number of any 1720 * matching shorter route when a new route is added. 1721 * 1722 * This fucntion always returns a failure return (B_FALSE) 1723 * to force the caller (rn_matchaddr_args) 1724 * to back-track up the tree looking for shorter matches. 1725 */ 1726 /* ARGSUSED */ 1727 static boolean_t 1728 ire_update_generation(struct radix_node *rn, void *arg) 1729 { 1730 struct rt_entry *rt = (struct rt_entry *)rn; 1731 1732 /* We need to handle all in the same bucket */ 1733 irb_increment_generation(&rt->rt_irb); 1734 return (B_FALSE); 1735 } 1736 1737 /* 1738 * Take care of all the generation numbers in the bucket. 1739 */ 1740 void 1741 irb_increment_generation(irb_t *irb) 1742 { 1743 ire_t *ire; 1744 1745 if (irb == NULL || irb->irb_ire_cnt == 0) 1746 return; 1747 1748 irb_refhold(irb); 1749 for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) { 1750 if (!IRE_IS_CONDEMNED(ire)) 1751 ire_increment_generation(ire); /* Ourselves */ 1752 ire_dep_incr_generation(ire); /* Dependants */ 1753 } 1754 irb_refrele(irb); 1755 } 1756 1757 /* 1758 * When an IRE is added or deleted this routine is called to make sure 1759 * any caching of IRE information is notified or updated. 1760 * 1761 * The flag argument indicates if the flush request is due to addition 1762 * of new route (IRE_FLUSH_ADD), deletion of old route (IRE_FLUSH_DELETE), 1763 * or a change to ire_gateway_addr (IRE_FLUSH_GWCHANGE). 1764 */ 1765 void 1766 ire_flush_cache_v4(ire_t *ire, int flag) 1767 { 1768 irb_t *irb = ire->ire_bucket; 1769 struct rt_entry *rt = IRB2RT(irb); 1770 ip_stack_t *ipst = ire->ire_ipst; 1771 1772 /* 1773 * IRE_IF_CLONE ire's don't provide any new information 1774 * than the parent from which they are cloned, so don't 1775 * perturb the generation numbers. 1776 */ 1777 if (ire->ire_type & IRE_IF_CLONE) 1778 return; 1779 1780 /* 1781 * Ensure that an ire_add during a lookup serializes the updates of the 1782 * generation numbers under the radix head lock so that the lookup gets 1783 * either the old ire and old generation number, or a new ire and new 1784 * generation number. 1785 */ 1786 RADIX_NODE_HEAD_WLOCK(ipst->ips_ip_ftable); 1787 1788 /* 1789 * If a route was just added, we need to notify everybody that 1790 * has cached an IRE_NOROUTE since there might now be a better 1791 * route for them. 1792 */ 1793 if (flag == IRE_FLUSH_ADD) { 1794 ire_increment_generation(ipst->ips_ire_reject_v4); 1795 ire_increment_generation(ipst->ips_ire_blackhole_v4); 1796 } 1797 1798 /* Adding a default can't otherwise provide a better route */ 1799 if (ire->ire_type == IRE_DEFAULT && flag == IRE_FLUSH_ADD) { 1800 RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable); 1801 return; 1802 } 1803 1804 switch (flag) { 1805 case IRE_FLUSH_DELETE: 1806 case IRE_FLUSH_GWCHANGE: 1807 /* 1808 * Update ire_generation for all ire_dep_children chains 1809 * starting with this IRE 1810 */ 1811 ire_dep_incr_generation(ire); 1812 break; 1813 case IRE_FLUSH_ADD: 1814 /* 1815 * Update the generation numbers of all shorter matching routes. 1816 * ire_update_generation takes care of the dependants by 1817 * using ire_dep_incr_generation. 1818 */ 1819 (void) ipst->ips_ip_ftable->rnh_matchaddr_args(&rt->rt_dst, 1820 ipst->ips_ip_ftable, ire_update_generation, NULL); 1821 break; 1822 } 1823 RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable); 1824 } 1825 1826 /* 1827 * Matches the arguments passed with the values in the ire. 1828 * 1829 * Note: for match types that match using "ill" passed in, ill 1830 * must be checked for non-NULL before calling this routine. 1831 */ 1832 boolean_t 1833 ire_match_args(ire_t *ire, ipaddr_t addr, ipaddr_t mask, ipaddr_t gateway, 1834 int type, const ill_t *ill, zoneid_t zoneid, 1835 const ts_label_t *tsl, int match_flags) 1836 { 1837 ill_t *ire_ill = NULL, *dst_ill; 1838 ip_stack_t *ipst = ire->ire_ipst; 1839 1840 ASSERT(ire->ire_ipversion == IPV4_VERSION); 1841 ASSERT((ire->ire_addr & ~ire->ire_mask) == 0); 1842 ASSERT((!(match_flags & MATCH_IRE_ILL)) || 1843 (ill != NULL && !ill->ill_isv6)); 1844 1845 /* 1846 * If MATCH_IRE_TESTHIDDEN is set, then only return the IRE if it is 1847 * in fact hidden, to ensure the caller gets the right one. 1848 */ 1849 if (ire->ire_testhidden) { 1850 if (!(match_flags & MATCH_IRE_TESTHIDDEN)) 1851 return (B_FALSE); 1852 } 1853 1854 if (zoneid != ALL_ZONES && zoneid != ire->ire_zoneid && 1855 ire->ire_zoneid != ALL_ZONES) { 1856 /* 1857 * If MATCH_IRE_ZONEONLY has been set and the supplied zoneid 1858 * does not match that of ire_zoneid, a failure to 1859 * match is reported at this point. Otherwise, since some IREs 1860 * that are available in the global zone can be used in local 1861 * zones, additional checks need to be performed: 1862 * 1863 * IRE_LOOPBACK 1864 * entries should never be matched in this situation. 1865 * Each zone has its own IRE_LOOPBACK. 1866 * 1867 * IRE_LOCAL 1868 * We allow them for any zoneid. ire_route_recursive 1869 * does additional checks when 1870 * ip_restrict_interzone_loopback is set. 1871 * 1872 * If ill_usesrc_ifindex is set 1873 * Then we check if the zone has a valid source address 1874 * on the usesrc ill. 1875 * 1876 * If ire_ill is set, then check that the zone has an ipif 1877 * on that ill. 1878 * 1879 * Outside of this function (in ire_round_robin) we check 1880 * that any IRE_OFFLINK has a gateway that reachable from the 1881 * zone when we have multiple choices (ECMP). 1882 */ 1883 if (match_flags & MATCH_IRE_ZONEONLY) 1884 return (B_FALSE); 1885 if (ire->ire_type & IRE_LOOPBACK) 1886 return (B_FALSE); 1887 1888 if (ire->ire_type & IRE_LOCAL) 1889 goto matchit; 1890 1891 /* 1892 * The normal case of IRE_ONLINK has a matching zoneid. 1893 * Here we handle the case when shared-IP zones have been 1894 * configured with IP addresses on vniN. In that case it 1895 * is ok for traffic from a zone to use IRE_ONLINK routes 1896 * if the ill has a usesrc pointing at vniN 1897 */ 1898 dst_ill = ire->ire_ill; 1899 if (ire->ire_type & IRE_ONLINK) { 1900 uint_t ifindex; 1901 1902 /* 1903 * Note there is no IRE_INTERFACE on vniN thus 1904 * can't do an IRE lookup for a matching route. 1905 */ 1906 ifindex = dst_ill->ill_usesrc_ifindex; 1907 if (ifindex == 0) 1908 return (B_FALSE); 1909 1910 /* 1911 * If there is a usable source address in the 1912 * zone, then it's ok to return this IRE_INTERFACE 1913 */ 1914 if (!ipif_zone_avail(ifindex, dst_ill->ill_isv6, 1915 zoneid, ipst)) { 1916 ip3dbg(("ire_match_args: no usrsrc for zone" 1917 " dst_ill %p\n", (void *)dst_ill)); 1918 return (B_FALSE); 1919 } 1920 } 1921 /* 1922 * For exampe, with 1923 * route add 11.0.0.0 gw1 -ifp bge0 1924 * route add 11.0.0.0 gw2 -ifp bge1 1925 * this code would differentiate based on 1926 * where the sending zone has addresses. 1927 * Only if the zone has an address on bge0 can it use the first 1928 * route. It isn't clear if this behavior is documented 1929 * anywhere. 1930 */ 1931 if (dst_ill != NULL && (ire->ire_type & IRE_OFFLINK)) { 1932 ipif_t *tipif; 1933 1934 mutex_enter(&dst_ill->ill_lock); 1935 for (tipif = dst_ill->ill_ipif; 1936 tipif != NULL; tipif = tipif->ipif_next) { 1937 if (!IPIF_IS_CONDEMNED(tipif) && 1938 (tipif->ipif_flags & IPIF_UP) && 1939 (tipif->ipif_zoneid == zoneid || 1940 tipif->ipif_zoneid == ALL_ZONES)) 1941 break; 1942 } 1943 mutex_exit(&dst_ill->ill_lock); 1944 if (tipif == NULL) { 1945 return (B_FALSE); 1946 } 1947 } 1948 } 1949 1950 matchit: 1951 if (match_flags & MATCH_IRE_ILL) { 1952 ire_ill = ire->ire_ill; 1953 1954 /* 1955 * If asked to match an ill, we *must* match 1956 * on the ire_ill for ipmp test addresses, or 1957 * any of the ill in the group for data addresses. 1958 * If we don't, we may as well fail. 1959 * However, we need an exception for IRE_LOCALs to ensure 1960 * we loopback packets even sent to test addresses on different 1961 * interfaces in the group. 1962 */ 1963 if ((match_flags & MATCH_IRE_TESTHIDDEN) && 1964 !(ire->ire_type & IRE_LOCAL)) { 1965 if (ire->ire_ill != ill) 1966 return (B_FALSE); 1967 } else { 1968 match_flags &= ~MATCH_IRE_TESTHIDDEN; 1969 /* 1970 * We know that ill is not NULL, but ire_ill could be 1971 * NULL 1972 */ 1973 if (ire_ill == NULL || !IS_ON_SAME_LAN(ill, ire_ill)) 1974 return (B_FALSE); 1975 } 1976 } 1977 1978 if ((ire->ire_addr == (addr & mask)) && 1979 ((!(match_flags & MATCH_IRE_GW)) || 1980 (ire->ire_gateway_addr == gateway)) && 1981 ((!(match_flags & MATCH_IRE_TYPE)) || (ire->ire_type & type)) && 1982 ((!(match_flags & MATCH_IRE_TESTHIDDEN)) || ire->ire_testhidden) && 1983 ((!(match_flags & MATCH_IRE_MASK)) || (ire->ire_mask == mask)) && 1984 ((!(match_flags & MATCH_IRE_SECATTR)) || 1985 (!is_system_labeled()) || 1986 (tsol_ire_match_gwattr(ire, tsl) == 0))) { 1987 /* We found the matched IRE */ 1988 return (B_TRUE); 1989 } 1990 return (B_FALSE); 1991 } 1992 1993 /* 1994 * Check if the IRE_LOCAL uses the same ill as another route would use. 1995 * If there is no alternate route, or the alternate is a REJECT or BLACKHOLE, 1996 * then we don't allow this IRE_LOCAL to be used. 1997 * We always return an IRE; will be RTF_REJECT if no route available. 1998 */ 1999 ire_t * 2000 ire_alt_local(ire_t *ire, zoneid_t zoneid, const ts_label_t *tsl, 2001 const ill_t *ill, uint_t *generationp) 2002 { 2003 ip_stack_t *ipst = ire->ire_ipst; 2004 ire_t *alt_ire; 2005 uint_t ire_type; 2006 uint_t generation; 2007 uint_t match_flags; 2008 2009 ASSERT(ire->ire_type & IRE_LOCAL); 2010 ASSERT(ire->ire_ill != NULL); 2011 2012 /* 2013 * Need to match on everything but local. 2014 * This might result in the creation of a IRE_IF_CLONE for the 2015 * same address as the IRE_LOCAL when restrict_interzone_loopback is 2016 * set. ire_add_*() ensures that the IRE_IF_CLONE are tail inserted 2017 * to make sure the IRE_LOCAL is always found first. 2018 */ 2019 ire_type = (IRE_ONLINK | IRE_OFFLINK) & ~(IRE_LOCAL|IRE_LOOPBACK); 2020 match_flags = MATCH_IRE_TYPE | MATCH_IRE_SECATTR; 2021 if (ill != NULL) 2022 match_flags |= MATCH_IRE_ILL; 2023 2024 if (ire->ire_ipversion == IPV4_VERSION) { 2025 alt_ire = ire_route_recursive_v4(ire->ire_addr, ire_type, 2026 ill, zoneid, tsl, match_flags, B_TRUE, 0, ipst, NULL, NULL, 2027 &generation); 2028 } else { 2029 alt_ire = ire_route_recursive_v6(&ire->ire_addr_v6, ire_type, 2030 ill, zoneid, tsl, match_flags, B_TRUE, 0, ipst, NULL, NULL, 2031 &generation); 2032 } 2033 ASSERT(alt_ire != NULL); 2034 2035 if (alt_ire->ire_ill == ire->ire_ill) { 2036 /* Going out the same ILL - ok to send to IRE_LOCAL */ 2037 ire_refrele(alt_ire); 2038 } else { 2039 /* Different ill - ignore IRE_LOCAL */ 2040 ire_refrele(ire); 2041 ire = alt_ire; 2042 if (generationp != NULL) 2043 *generationp = generation; 2044 } 2045 return (ire); 2046 } 2047 2048 boolean_t 2049 ire_find_zoneid(struct radix_node *rn, void *arg) 2050 { 2051 struct rt_entry *rt = (struct rt_entry *)rn; 2052 irb_t *irb; 2053 ire_t *ire; 2054 ire_ftable_args_t *margs = arg; 2055 2056 ASSERT(rt != NULL); 2057 2058 irb = &rt->rt_irb; 2059 2060 if (irb->irb_ire_cnt == 0) 2061 return (B_FALSE); 2062 2063 rw_enter(&irb->irb_lock, RW_READER); 2064 for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) { 2065 if (IRE_IS_CONDEMNED(ire)) 2066 continue; 2067 2068 if (ire->ire_zoneid != ALL_ZONES && 2069 ire->ire_zoneid != margs->ift_zoneid) 2070 continue; 2071 2072 if (margs->ift_ill != NULL && margs->ift_ill != ire->ire_ill) 2073 continue; 2074 2075 if (is_system_labeled() && 2076 tsol_ire_match_gwattr(ire, margs->ift_tsl) != 0) 2077 continue; 2078 2079 rw_exit(&irb->irb_lock); 2080 return (B_TRUE); 2081 } 2082 rw_exit(&irb->irb_lock); 2083 return (B_FALSE); 2084 } 2085 2086 /* 2087 * Check if the zoneid (not ALL_ZONES) has an IRE_INTERFACE for the specified 2088 * gateway address. If ill is non-NULL we also match on it. 2089 * The caller must hold a read lock on RADIX_NODE_HEAD if lock_held is set. 2090 */ 2091 boolean_t 2092 ire_gateway_ok_zone_v4(ipaddr_t gateway, zoneid_t zoneid, ill_t *ill, 2093 const ts_label_t *tsl, ip_stack_t *ipst, boolean_t lock_held) 2094 { 2095 struct rt_sockaddr rdst; 2096 struct rt_entry *rt; 2097 ire_ftable_args_t margs; 2098 2099 ASSERT(ill == NULL || !ill->ill_isv6); 2100 if (lock_held) 2101 ASSERT(RW_READ_HELD(&ipst->ips_ip_ftable->rnh_lock)); 2102 else 2103 RADIX_NODE_HEAD_RLOCK(ipst->ips_ip_ftable); 2104 2105 rdst.rt_sin_len = sizeof (rdst); 2106 rdst.rt_sin_family = AF_INET; 2107 rdst.rt_sin_addr.s_addr = gateway; 2108 2109 /* 2110 * We only use margs for ill, zoneid, and tsl matching in 2111 * ire_find_zoneid 2112 */ 2113 (void) memset(&margs, 0, sizeof (margs)); 2114 margs.ift_ill = ill; 2115 margs.ift_zoneid = zoneid; 2116 margs.ift_tsl = tsl; 2117 rt = (struct rt_entry *)ipst->ips_ip_ftable->rnh_matchaddr_args(&rdst, 2118 ipst->ips_ip_ftable, ire_find_zoneid, (void *)&margs); 2119 2120 if (!lock_held) 2121 RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable); 2122 2123 return (rt != NULL); 2124 } 2125 2126 /* 2127 * ire_walk routine to delete a fraction of redirect IREs and IRE_CLONE_IF IREs. 2128 * The fraction argument tells us what fraction of the IREs to delete. 2129 * Common for IPv4 and IPv6. 2130 * Used when memory backpressure. 2131 */ 2132 static void 2133 ire_delete_reclaim(ire_t *ire, char *arg) 2134 { 2135 ip_stack_t *ipst = ire->ire_ipst; 2136 uint_t fraction = *(uint_t *)arg; 2137 uint_t rand; 2138 2139 if ((ire->ire_flags & RTF_DYNAMIC) || 2140 (ire->ire_type & IRE_IF_CLONE)) { 2141 2142 /* Pick a random number */ 2143 rand = (uint_t)lbolt + 2144 IRE_ADDR_HASH_V6(ire->ire_addr_v6, 256); 2145 2146 /* Use truncation */ 2147 if ((rand/fraction)*fraction == rand) { 2148 IP_STAT(ipst, ip_ire_reclaim_deleted); 2149 ire_delete(ire); 2150 } 2151 } 2152 2153 } 2154 2155 /* 2156 * kmem_cache callback to free up memory. 2157 * 2158 * Free a fraction (ips_ip_ire_reclaim_fraction) of things IP added dynamically 2159 * (RTF_DYNAMIC and IRE_IF_CLONE). 2160 */ 2161 static void 2162 ip_ire_reclaim_stack(ip_stack_t *ipst) 2163 { 2164 uint_t fraction = ipst->ips_ip_ire_reclaim_fraction; 2165 2166 IP_STAT(ipst, ip_ire_reclaim_calls); 2167 2168 ire_walk(ire_delete_reclaim, &fraction, ipst); 2169 2170 /* 2171 * Walk all CONNs that can have a reference on an ire, nce or dce. 2172 * Get them to update any stale references to drop any refholds they 2173 * have. 2174 */ 2175 ipcl_walk(conn_ixa_cleanup, (void *)B_FALSE, ipst); 2176 } 2177 2178 /* 2179 * Called by the memory allocator subsystem directly, when the system 2180 * is running low on memory. 2181 */ 2182 /* ARGSUSED */ 2183 void 2184 ip_ire_reclaim(void *args) 2185 { 2186 netstack_handle_t nh; 2187 netstack_t *ns; 2188 2189 netstack_next_init(&nh); 2190 while ((ns = netstack_next(&nh)) != NULL) { 2191 ip_ire_reclaim_stack(ns->netstack_ip); 2192 netstack_rele(ns); 2193 } 2194 netstack_next_fini(&nh); 2195 } 2196 2197 static void 2198 power2_roundup(uint32_t *value) 2199 { 2200 int i; 2201 2202 for (i = 1; i < 31; i++) { 2203 if (*value <= (1 << i)) 2204 break; 2205 } 2206 *value = (1 << i); 2207 } 2208 2209 /* Global init for all zones */ 2210 void 2211 ip_ire_g_init() 2212 { 2213 /* 2214 * Create kmem_caches. ip_ire_reclaim() and ip_nce_reclaim() 2215 * will give disposable IREs back to system when needed. 2216 * This needs to be done here before anything else, since 2217 * ire_add() expects the cache to be created. 2218 */ 2219 ire_cache = kmem_cache_create("ire_cache", 2220 sizeof (ire_t), 0, NULL, NULL, 2221 ip_ire_reclaim, NULL, NULL, 0); 2222 2223 ncec_cache = kmem_cache_create("ncec_cache", 2224 sizeof (ncec_t), 0, NULL, NULL, 2225 ip_nce_reclaim, NULL, NULL, 0); 2226 nce_cache = kmem_cache_create("nce_cache", 2227 sizeof (nce_t), 0, NULL, NULL, 2228 NULL, NULL, NULL, 0); 2229 2230 rt_entry_cache = kmem_cache_create("rt_entry", 2231 sizeof (struct rt_entry), 0, NULL, NULL, NULL, NULL, NULL, 0); 2232 2233 /* 2234 * Have radix code setup kmem caches etc. 2235 */ 2236 rn_init(); 2237 } 2238 2239 void 2240 ip_ire_init(ip_stack_t *ipst) 2241 { 2242 ire_t *ire; 2243 int error; 2244 2245 mutex_init(&ipst->ips_ire_ft_init_lock, NULL, MUTEX_DEFAULT, 0); 2246 2247 (void) rn_inithead((void **)&ipst->ips_ip_ftable, 32); 2248 2249 /* 2250 * Make sure that the forwarding table size is a power of 2. 2251 * The IRE*_ADDR_HASH() macroes depend on that. 2252 */ 2253 ipst->ips_ip6_ftable_hash_size = ip6_ftable_hash_size; 2254 power2_roundup(&ipst->ips_ip6_ftable_hash_size); 2255 2256 /* 2257 * Allocate/initialize a pair of IRE_NOROUTEs for each of IPv4 and IPv6. 2258 * The ire_reject_v* has RTF_REJECT set, and the ire_blackhole_v* has 2259 * RTF_BLACKHOLE set. We use the latter for transient errors such 2260 * as memory allocation failures and tripping on IRE_IS_CONDEMNED 2261 * entries. 2262 */ 2263 ire = kmem_cache_alloc(ire_cache, KM_SLEEP); 2264 *ire = ire_null; 2265 error = ire_init_v4(ire, 0, 0, 0, IRE_NOROUTE, NULL, ALL_ZONES, 2266 RTF_REJECT|RTF_UP, NULL, ipst); 2267 ASSERT(error == 0); 2268 ipst->ips_ire_reject_v4 = ire; 2269 2270 ire = kmem_cache_alloc(ire_cache, KM_SLEEP); 2271 *ire = ire_null; 2272 error = ire_init_v6(ire, 0, 0, 0, IRE_NOROUTE, NULL, ALL_ZONES, 2273 RTF_REJECT|RTF_UP, NULL, ipst); 2274 ASSERT(error == 0); 2275 ipst->ips_ire_reject_v6 = ire; 2276 2277 ire = kmem_cache_alloc(ire_cache, KM_SLEEP); 2278 *ire = ire_null; 2279 error = ire_init_v4(ire, 0, 0, 0, IRE_NOROUTE, NULL, ALL_ZONES, 2280 RTF_BLACKHOLE|RTF_UP, NULL, ipst); 2281 ASSERT(error == 0); 2282 ipst->ips_ire_blackhole_v4 = ire; 2283 2284 ire = kmem_cache_alloc(ire_cache, KM_SLEEP); 2285 *ire = ire_null; 2286 error = ire_init_v6(ire, 0, 0, 0, IRE_NOROUTE, NULL, ALL_ZONES, 2287 RTF_BLACKHOLE|RTF_UP, NULL, ipst); 2288 ASSERT(error == 0); 2289 ipst->ips_ire_blackhole_v6 = ire; 2290 2291 rw_init(&ipst->ips_ip6_ire_head_lock, NULL, RW_DEFAULT, NULL); 2292 rw_init(&ipst->ips_ire_dep_lock, NULL, RW_DEFAULT, NULL); 2293 } 2294 2295 void 2296 ip_ire_g_fini(void) 2297 { 2298 kmem_cache_destroy(ire_cache); 2299 kmem_cache_destroy(ncec_cache); 2300 kmem_cache_destroy(nce_cache); 2301 kmem_cache_destroy(rt_entry_cache); 2302 2303 rn_fini(); 2304 } 2305 2306 void 2307 ip_ire_fini(ip_stack_t *ipst) 2308 { 2309 int i; 2310 2311 rw_destroy(&ipst->ips_ire_dep_lock); 2312 rw_destroy(&ipst->ips_ip6_ire_head_lock); 2313 2314 ire_refrele_notr(ipst->ips_ire_reject_v6); 2315 ipst->ips_ire_reject_v6 = NULL; 2316 ire_refrele_notr(ipst->ips_ire_reject_v4); 2317 ipst->ips_ire_reject_v4 = NULL; 2318 ire_refrele_notr(ipst->ips_ire_blackhole_v6); 2319 ipst->ips_ire_blackhole_v6 = NULL; 2320 ire_refrele_notr(ipst->ips_ire_blackhole_v4); 2321 ipst->ips_ire_blackhole_v4 = NULL; 2322 2323 /* 2324 * Delete all IREs - assumes that the ill/ipifs have 2325 * been removed so what remains are just the ftable to handle. 2326 */ 2327 ire_walk(ire_delete, NULL, ipst); 2328 2329 rn_freehead(ipst->ips_ip_ftable); 2330 ipst->ips_ip_ftable = NULL; 2331 2332 mutex_destroy(&ipst->ips_ire_ft_init_lock); 2333 2334 for (i = 0; i < IP6_MASK_TABLE_SIZE; i++) { 2335 irb_t *ptr; 2336 int j; 2337 2338 if ((ptr = ipst->ips_ip_forwarding_table_v6[i]) == NULL) 2339 continue; 2340 2341 for (j = 0; j < ipst->ips_ip6_ftable_hash_size; j++) { 2342 ASSERT(ptr[j].irb_ire == NULL); 2343 rw_destroy(&ptr[j].irb_lock); 2344 } 2345 mi_free(ptr); 2346 ipst->ips_ip_forwarding_table_v6[i] = NULL; 2347 } 2348 } 2349 2350 #ifdef DEBUG 2351 void 2352 ire_trace_ref(ire_t *ire) 2353 { 2354 mutex_enter(&ire->ire_lock); 2355 if (ire->ire_trace_disable) { 2356 mutex_exit(&ire->ire_lock); 2357 return; 2358 } 2359 2360 if (th_trace_ref(ire, ire->ire_ipst)) { 2361 mutex_exit(&ire->ire_lock); 2362 } else { 2363 ire->ire_trace_disable = B_TRUE; 2364 mutex_exit(&ire->ire_lock); 2365 ire_trace_cleanup(ire); 2366 } 2367 } 2368 2369 void 2370 ire_untrace_ref(ire_t *ire) 2371 { 2372 mutex_enter(&ire->ire_lock); 2373 if (!ire->ire_trace_disable) 2374 th_trace_unref(ire); 2375 mutex_exit(&ire->ire_lock); 2376 } 2377 2378 static void 2379 ire_trace_cleanup(const ire_t *ire) 2380 { 2381 th_trace_cleanup(ire, ire->ire_trace_disable); 2382 } 2383 #endif /* DEBUG */ 2384 2385 /* 2386 * Find, or create if needed, the nce_t pointer to the neighbor cache 2387 * entry ncec_t for an IPv4 address. The nce_t will be created on the ill_t 2388 * in the non-IPMP case, or on the cast-ill in the IPMP bcast/mcast case, or 2389 * on the next available under-ill (selected by the IPMP rotor) in the 2390 * unicast IPMP case. 2391 * 2392 * If a neighbor-cache entry has to be created (i.e., one does not already 2393 * exist in the nce list) the ncec_lladdr and ncec_state of the neighbor cache 2394 * entry are initialized in nce_add_v4(). The broadcast, multicast, and 2395 * link-layer type determine the contents of {ncec_state, ncec_lladdr} of 2396 * the ncec_t created. The ncec_lladdr is non-null for all link types with 2397 * non-zero ill_phys_addr_length, though the contents may be zero in cases 2398 * where the link-layer type is not known at the time of creation 2399 * (e.g., IRE_IFRESOLVER links) 2400 * 2401 * All IRE_BROADCAST entries have ncec_state = ND_REACHABLE, and the nce_lladr 2402 * has the physical broadcast address of the outgoing interface. 2403 * For unicast ire entries, 2404 * - if the outgoing interface is of type IRE_IF_RESOLVER, a newly created 2405 * ncec_t with 0 nce_lladr contents, and will be in the ND_INITIAL state. 2406 * - if the outgoing interface is a IRE_IF_NORESOLVER interface, no link 2407 * layer resolution is necessary, so that the ncec_t will be in the 2408 * ND_REACHABLE state 2409 * 2410 * The link layer information needed for broadcast addresses, and for 2411 * packets sent on IRE_IF_NORESOLVER interfaces is a constant mapping that 2412 * never needs re-verification for the lifetime of the ncec_t. These are 2413 * therefore marked NCE_F_NONUD. 2414 * 2415 * The nce returned will be created such that the nce_ill == ill that 2416 * is passed in. Note that the nce itself may not have ncec_ill == ill 2417 * where IPMP links are involved. 2418 */ 2419 static nce_t * 2420 ire_nce_init(ill_t *ill, const void *addr, int ire_type) 2421 { 2422 int err; 2423 nce_t *nce = NULL; 2424 uint16_t ncec_flags; 2425 uchar_t *hwaddr; 2426 boolean_t need_refrele = B_FALSE; 2427 ill_t *in_ill = ill; 2428 boolean_t is_unicast; 2429 uint_t hwaddr_len; 2430 2431 is_unicast = ((ire_type & (IRE_MULTICAST|IRE_BROADCAST)) == 0); 2432 if (IS_IPMP(ill) || 2433 ((ire_type & IRE_BROADCAST) && IS_UNDER_IPMP(ill))) { 2434 if ((ill = ipmp_ill_get_xmit_ill(ill, is_unicast)) == NULL) 2435 return (NULL); 2436 need_refrele = B_TRUE; 2437 } 2438 ncec_flags = (ill->ill_flags & ILLF_NONUD) ? NCE_F_NONUD : 0; 2439 2440 switch (ire_type) { 2441 case IRE_BROADCAST: 2442 ASSERT(!ill->ill_isv6); 2443 ncec_flags |= (NCE_F_BCAST|NCE_F_NONUD); 2444 break; 2445 case IRE_MULTICAST: 2446 ncec_flags |= (NCE_F_MCAST|NCE_F_NONUD); 2447 break; 2448 } 2449 2450 if (ill->ill_net_type == IRE_IF_NORESOLVER && is_unicast) { 2451 hwaddr = ill->ill_dest_addr; 2452 } else { 2453 hwaddr = NULL; 2454 } 2455 hwaddr_len = ill->ill_phys_addr_length; 2456 2457 retry: 2458 /* nce_state will be computed by nce_add_common() */ 2459 if (!ill->ill_isv6) { 2460 err = nce_lookup_then_add_v4(ill, hwaddr, hwaddr_len, addr, 2461 ncec_flags, ND_UNCHANGED, &nce); 2462 } else { 2463 err = nce_lookup_then_add_v6(ill, hwaddr, hwaddr_len, addr, 2464 ncec_flags, ND_UNCHANGED, &nce); 2465 } 2466 2467 switch (err) { 2468 case 0: 2469 break; 2470 case EEXIST: 2471 /* 2472 * When subnets change or partially overlap what was once 2473 * a broadcast address could now be a unicast, or vice versa. 2474 */ 2475 if (((ncec_flags ^ nce->nce_common->ncec_flags) & 2476 NCE_F_BCAST) != 0) { 2477 ASSERT(!ill->ill_isv6); 2478 ncec_delete(nce->nce_common); 2479 nce_refrele(nce); 2480 goto retry; 2481 } 2482 break; 2483 default: 2484 DTRACE_PROBE2(nce__init__fail, ill_t *, ill, int, err); 2485 if (need_refrele) 2486 ill_refrele(ill); 2487 return (NULL); 2488 } 2489 /* 2490 * If the ill was an under-ill of an IPMP group, we need to verify 2491 * that it is still active so that we select an active interface in 2492 * the group. However, since ipmp_ill_is_active ASSERTs for 2493 * IS_UNDER_IPMP(), we first need to verify that the ill is an 2494 * under-ill, and since this is being done in the data path, the 2495 * only way to ascertain this is by holding the ill_g_lock. 2496 */ 2497 rw_enter(&ill->ill_ipst->ips_ill_g_lock, RW_READER); 2498 mutex_enter(&ill->ill_lock); 2499 mutex_enter(&ill->ill_phyint->phyint_lock); 2500 if (need_refrele && IS_UNDER_IPMP(ill) && !ipmp_ill_is_active(ill)) { 2501 /* 2502 * need_refrele implies that the under ill was selected by 2503 * ipmp_ill_get_xmit_ill() because either the in_ill was an 2504 * ipmp_ill, or we are sending a non-unicast packet on 2505 * an under_ill. However, when we get here, the ill selected by 2506 * ipmp_ill_get_xmit_ill was pulled out of the active set 2507 * (for unicast) or cast_ill nomination (for 2508 * !unicast) after it was picked as the outgoing ill. 2509 * We have to pick an active interface and/or cast_ill in the 2510 * group. 2511 */ 2512 mutex_exit(&ill->ill_phyint->phyint_lock); 2513 nce_delete(nce); 2514 mutex_exit(&ill->ill_lock); 2515 rw_exit(&ill->ill_ipst->ips_ill_g_lock); 2516 nce_refrele(nce); 2517 ill_refrele(ill); 2518 if ((ill = ipmp_ill_get_xmit_ill(in_ill, is_unicast)) == NULL) 2519 return (NULL); 2520 goto retry; 2521 } else { 2522 mutex_exit(&ill->ill_phyint->phyint_lock); 2523 mutex_exit(&ill->ill_lock); 2524 rw_exit(&ill->ill_ipst->ips_ill_g_lock); 2525 } 2526 done: 2527 ASSERT(nce->nce_ill == ill); 2528 if (need_refrele) 2529 ill_refrele(ill); 2530 return (nce); 2531 } 2532 2533 nce_t * 2534 arp_nce_init(ill_t *ill, in_addr_t addr4, int ire_type) 2535 { 2536 return (ire_nce_init(ill, &addr4, ire_type)); 2537 } 2538 2539 nce_t * 2540 ndp_nce_init(ill_t *ill, const in6_addr_t *addr6, int ire_type) 2541 { 2542 ASSERT((ire_type & IRE_BROADCAST) == 0); 2543 return (ire_nce_init(ill, addr6, ire_type)); 2544 } 2545 2546 /* 2547 * The caller should hold irb_lock as a writer if the ire is in a bucket. 2548 */ 2549 void 2550 ire_make_condemned(ire_t *ire) 2551 { 2552 ip_stack_t *ipst = ire->ire_ipst; 2553 2554 mutex_enter(&ire->ire_lock); 2555 ASSERT(ire->ire_bucket == NULL || 2556 RW_WRITE_HELD(&ire->ire_bucket->irb_lock)); 2557 ASSERT(!IRE_IS_CONDEMNED(ire)); 2558 ire->ire_generation = IRE_GENERATION_CONDEMNED; 2559 /* Count how many condemned ires for kmem_cache callback */ 2560 atomic_add_32(&ipst->ips_num_ire_condemned, 1); 2561 mutex_exit(&ire->ire_lock); 2562 } 2563 2564 /* 2565 * Increment the generation avoiding the special condemned value 2566 */ 2567 void 2568 ire_increment_generation(ire_t *ire) 2569 { 2570 uint_t generation; 2571 2572 mutex_enter(&ire->ire_lock); 2573 /* 2574 * Even though the caller has a hold it can't prevent a concurrent 2575 * ire_delete marking the IRE condemned 2576 */ 2577 if (!IRE_IS_CONDEMNED(ire)) { 2578 generation = ire->ire_generation + 1; 2579 if (generation == IRE_GENERATION_CONDEMNED) 2580 generation = IRE_GENERATION_INITIAL; 2581 ASSERT(generation != IRE_GENERATION_VERIFY); 2582 ire->ire_generation = generation; 2583 } 2584 mutex_exit(&ire->ire_lock); 2585 } 2586 2587 /* 2588 * Increment ire_generation on all the IRE_MULTICASTs 2589 * Used when the default multicast interface (as determined by 2590 * ill_lookup_multicast) might have changed. 2591 * 2592 * That includes the zoneid, IFF_ flags, the IPv6 scope of the address, and 2593 * ill unplumb. 2594 */ 2595 void 2596 ire_increment_multicast_generation(ip_stack_t *ipst, boolean_t isv6) 2597 { 2598 ill_t *ill; 2599 ill_walk_context_t ctx; 2600 2601 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 2602 if (isv6) 2603 ill = ILL_START_WALK_V6(&ctx, ipst); 2604 else 2605 ill = ILL_START_WALK_V4(&ctx, ipst); 2606 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 2607 if (ILL_IS_CONDEMNED(ill)) 2608 continue; 2609 if (ill->ill_ire_multicast != NULL) 2610 ire_increment_generation(ill->ill_ire_multicast); 2611 } 2612 rw_exit(&ipst->ips_ill_g_lock); 2613 } 2614 2615 /* 2616 * Return a held IRE_NOROUTE with RTF_REJECT set 2617 */ 2618 ire_t * 2619 ire_reject(ip_stack_t *ipst, boolean_t isv6) 2620 { 2621 ire_t *ire; 2622 2623 if (isv6) 2624 ire = ipst->ips_ire_reject_v6; 2625 else 2626 ire = ipst->ips_ire_reject_v4; 2627 2628 ASSERT(ire->ire_generation != IRE_GENERATION_CONDEMNED); 2629 ire_refhold(ire); 2630 return (ire); 2631 } 2632 2633 /* 2634 * Return a held IRE_NOROUTE with RTF_BLACKHOLE set 2635 */ 2636 ire_t * 2637 ire_blackhole(ip_stack_t *ipst, boolean_t isv6) 2638 { 2639 ire_t *ire; 2640 2641 if (isv6) 2642 ire = ipst->ips_ire_blackhole_v6; 2643 else 2644 ire = ipst->ips_ire_blackhole_v4; 2645 2646 ASSERT(ire->ire_generation != IRE_GENERATION_CONDEMNED); 2647 ire_refhold(ire); 2648 return (ire); 2649 } 2650 2651 /* 2652 * Return a held IRE_MULTICAST. 2653 */ 2654 ire_t * 2655 ire_multicast(ill_t *ill) 2656 { 2657 ire_t *ire = ill->ill_ire_multicast; 2658 2659 ASSERT(ire == NULL || ire->ire_generation != IRE_GENERATION_CONDEMNED); 2660 if (ire == NULL) 2661 ire = ire_blackhole(ill->ill_ipst, ill->ill_isv6); 2662 else 2663 ire_refhold(ire); 2664 return (ire); 2665 } 2666 2667 /* 2668 * Given an IRE return its nexthop IRE. The nexthop IRE is an IRE_ONLINK 2669 * that is an exact match (i.e., a /32 for IPv4 and /128 for IPv6). 2670 * This can return an RTF_REJECT|RTF_BLACKHOLE. 2671 * The returned IRE is held. 2672 * The assumption is that ip_select_route() has been called and returned the 2673 * IRE (thus ip_select_route would have set up the ire_dep* information.) 2674 * If some IRE is deleteted then ire_dep_remove() will have been called and 2675 * we might not find a nexthop IRE, in which case we return NULL. 2676 */ 2677 ire_t * 2678 ire_nexthop(ire_t *ire) 2679 { 2680 ip_stack_t *ipst = ire->ire_ipst; 2681 2682 /* Acquire lock to walk ire_dep_parent */ 2683 rw_enter(&ipst->ips_ire_dep_lock, RW_READER); 2684 while (ire != NULL) { 2685 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 2686 goto done; 2687 } 2688 /* 2689 * If we find an IRE_ONLINK we are done. This includes 2690 * the case of IRE_MULTICAST. 2691 * Note that in order to send packets we need a host-specific 2692 * IRE_IF_ALL first in the ire_dep_parent chain. Normally this 2693 * is done by inserting an IRE_IF_CLONE if the IRE_INTERFACE 2694 * was not host specific. 2695 * However, ip_rts_request doesn't want to send packets 2696 * hence doesn't want to allocate an IRE_IF_CLONE. Yet 2697 * it needs an IRE_IF_ALL to get to the ill. Thus 2698 * we return IRE_IF_ALL that are not host specific here. 2699 */ 2700 if (ire->ire_type & IRE_ONLINK) 2701 goto done; 2702 ire = ire->ire_dep_parent; 2703 } 2704 rw_exit(&ipst->ips_ire_dep_lock); 2705 return (NULL); 2706 2707 done: 2708 ire_refhold(ire); 2709 rw_exit(&ipst->ips_ire_dep_lock); 2710 return (ire); 2711 } 2712 2713 /* 2714 * Find the ill used to send packets. This will be NULL in case 2715 * of a reject or blackhole. 2716 * The returned ill is held; caller needs to do ill_refrele when done. 2717 */ 2718 ill_t * 2719 ire_nexthop_ill(ire_t *ire) 2720 { 2721 ill_t *ill; 2722 2723 ire = ire_nexthop(ire); 2724 if (ire == NULL) 2725 return (NULL); 2726 2727 /* ire_ill can not change for an existing ire */ 2728 ill = ire->ire_ill; 2729 if (ill != NULL) 2730 ill_refhold(ill); 2731 ire_refrele(ire); 2732 return (ill); 2733 } 2734 2735 #ifdef DEBUG 2736 static boolean_t 2737 parent_has_child(ire_t *parent, ire_t *child) 2738 { 2739 ire_t *ire; 2740 ire_t *prev; 2741 2742 ire = parent->ire_dep_children; 2743 prev = NULL; 2744 while (ire != NULL) { 2745 if (prev == NULL) { 2746 ASSERT(ire->ire_dep_sib_ptpn == 2747 &(parent->ire_dep_children)); 2748 } else { 2749 ASSERT(ire->ire_dep_sib_ptpn == 2750 &(prev->ire_dep_sib_next)); 2751 } 2752 if (ire == child) 2753 return (B_TRUE); 2754 prev = ire; 2755 ire = ire->ire_dep_sib_next; 2756 } 2757 return (B_FALSE); 2758 } 2759 2760 static void 2761 ire_dep_verify(ire_t *ire) 2762 { 2763 ire_t *parent = ire->ire_dep_parent; 2764 ire_t *child = ire->ire_dep_children; 2765 2766 ASSERT(ire->ire_ipversion == IPV4_VERSION || 2767 ire->ire_ipversion == IPV6_VERSION); 2768 if (parent != NULL) { 2769 ASSERT(parent->ire_ipversion == IPV4_VERSION || 2770 parent->ire_ipversion == IPV6_VERSION); 2771 ASSERT(parent->ire_refcnt >= 1); 2772 ASSERT(parent_has_child(parent, ire)); 2773 } 2774 if (child != NULL) { 2775 ASSERT(child->ire_ipversion == IPV4_VERSION || 2776 child->ire_ipversion == IPV6_VERSION); 2777 ASSERT(child->ire_dep_parent == ire); 2778 ASSERT(child->ire_dep_sib_ptpn != NULL); 2779 ASSERT(parent_has_child(ire, child)); 2780 } 2781 } 2782 #endif /* DEBUG */ 2783 2784 /* 2785 * Assumes ire_dep_parent is set. Remove this child from its parent's linkage. 2786 */ 2787 void 2788 ire_dep_remove(ire_t *ire) 2789 { 2790 ip_stack_t *ipst = ire->ire_ipst; 2791 ire_t *parent = ire->ire_dep_parent; 2792 ire_t *next; 2793 nce_t *nce; 2794 2795 ASSERT(RW_WRITE_HELD(&ipst->ips_ire_dep_lock)); 2796 ASSERT(ire->ire_dep_parent != NULL); 2797 ASSERT(ire->ire_dep_sib_ptpn != NULL); 2798 2799 #ifdef DEBUG 2800 ire_dep_verify(ire); 2801 ire_dep_verify(parent); 2802 #endif 2803 2804 next = ire->ire_dep_sib_next; 2805 if (next != NULL) 2806 next->ire_dep_sib_ptpn = ire->ire_dep_sib_ptpn; 2807 2808 ASSERT(*(ire->ire_dep_sib_ptpn) == ire); 2809 *(ire->ire_dep_sib_ptpn) = ire->ire_dep_sib_next; 2810 2811 ire->ire_dep_sib_ptpn = NULL; 2812 ire->ire_dep_sib_next = NULL; 2813 2814 mutex_enter(&ire->ire_lock); 2815 parent = ire->ire_dep_parent; 2816 ire->ire_dep_parent = NULL; 2817 mutex_exit(&ire->ire_lock); 2818 2819 /* 2820 * Make sure all our children, grandchildren, etc set 2821 * ire_dep_parent_generation to IRE_GENERATION_VERIFY since 2822 * we can no longer guarantee than the children have a current 2823 * ire_nce_cache and ire_nexthop_ill(). 2824 */ 2825 if (ire->ire_dep_children != NULL) 2826 ire_dep_invalidate_children(ire->ire_dep_children); 2827 2828 /* 2829 * Since the parent is gone we make sure we clear ire_nce_cache. 2830 * We can clear it under ire_lock even if the IRE is used 2831 */ 2832 mutex_enter(&ire->ire_lock); 2833 nce = ire->ire_nce_cache; 2834 ire->ire_nce_cache = NULL; 2835 mutex_exit(&ire->ire_lock); 2836 if (nce != NULL) 2837 nce_refrele(nce); 2838 2839 #ifdef DEBUG 2840 ire_dep_verify(ire); 2841 ire_dep_verify(parent); 2842 #endif 2843 2844 ire_refrele_notr(parent); 2845 ire_refrele_notr(ire); 2846 } 2847 2848 /* 2849 * Insert the child in the linkage of the parent 2850 */ 2851 static void 2852 ire_dep_parent_insert(ire_t *child, ire_t *parent) 2853 { 2854 ip_stack_t *ipst = child->ire_ipst; 2855 ire_t *next; 2856 2857 ASSERT(RW_WRITE_HELD(&ipst->ips_ire_dep_lock)); 2858 ASSERT(child->ire_dep_parent == NULL); 2859 2860 #ifdef DEBUG 2861 ire_dep_verify(child); 2862 ire_dep_verify(parent); 2863 #endif 2864 /* No parents => no siblings */ 2865 ASSERT(child->ire_dep_sib_ptpn == NULL); 2866 ASSERT(child->ire_dep_sib_next == NULL); 2867 2868 ire_refhold_notr(parent); 2869 ire_refhold_notr(child); 2870 2871 /* Head insertion */ 2872 next = parent->ire_dep_children; 2873 if (next != NULL) { 2874 ASSERT(next->ire_dep_sib_ptpn == &(parent->ire_dep_children)); 2875 child->ire_dep_sib_next = next; 2876 next->ire_dep_sib_ptpn = &(child->ire_dep_sib_next); 2877 } 2878 parent->ire_dep_children = child; 2879 child->ire_dep_sib_ptpn = &(parent->ire_dep_children); 2880 2881 mutex_enter(&child->ire_lock); 2882 child->ire_dep_parent = parent; 2883 mutex_exit(&child->ire_lock); 2884 2885 #ifdef DEBUG 2886 ire_dep_verify(child); 2887 ire_dep_verify(parent); 2888 #endif 2889 } 2890 2891 2892 /* 2893 * Given count worth of ires and generations, build ire_dep_* relationships 2894 * from ires[0] to ires[count-1]. Record generations[i+1] in 2895 * ire_dep_parent_generation for ires[i]. 2896 * We graft onto an existing parent chain by making sure that we don't 2897 * touch ire_dep_parent for ires[count-1]. 2898 * 2899 * We check for any condemned ire_generation count and return B_FALSE in 2900 * that case so that the caller can tear it apart. 2901 * 2902 * Note that generations[0] is not used. Caller handles that. 2903 */ 2904 boolean_t 2905 ire_dep_build(ire_t *ires[], uint_t generations[], uint_t count) 2906 { 2907 ire_t *ire = ires[0]; 2908 ip_stack_t *ipst; 2909 uint_t i; 2910 2911 ASSERT(count > 0); 2912 if (count == 1) { 2913 /* No work to do */ 2914 return (B_TRUE); 2915 } 2916 ipst = ire->ire_ipst; 2917 rw_enter(&ipst->ips_ire_dep_lock, RW_WRITER); 2918 /* 2919 * Do not remove the linkage for any existing parent chain i.e., 2920 * ires[count-1] is left alone. 2921 */ 2922 for (i = 0; i < count-1; i++) { 2923 /* Remove existing parent if we need to change it */ 2924 if (ires[i]->ire_dep_parent != NULL && 2925 ires[i]->ire_dep_parent != ires[i+1]) 2926 ire_dep_remove(ires[i]); 2927 } 2928 2929 for (i = 0; i < count - 1; i++) { 2930 ASSERT(ires[i]->ire_ipversion == IPV4_VERSION || 2931 ires[i]->ire_ipversion == IPV6_VERSION); 2932 /* Does it need to change? */ 2933 if (ires[i]->ire_dep_parent != ires[i+1]) 2934 ire_dep_parent_insert(ires[i], ires[i+1]); 2935 2936 mutex_enter(&ires[i+1]->ire_lock); 2937 if (IRE_IS_CONDEMNED(ires[i+1])) { 2938 mutex_exit(&ires[i+1]->ire_lock); 2939 rw_exit(&ipst->ips_ire_dep_lock); 2940 return (B_FALSE); 2941 } 2942 mutex_exit(&ires[i+1]->ire_lock); 2943 2944 mutex_enter(&ires[i]->ire_lock); 2945 ires[i]->ire_dep_parent_generation = generations[i+1]; 2946 mutex_exit(&ires[i]->ire_lock); 2947 } 2948 rw_exit(&ipst->ips_ire_dep_lock); 2949 return (B_TRUE); 2950 } 2951 2952 /* 2953 * Given count worth of ires, unbuild ire_dep_* relationships 2954 * from ires[0] to ires[count-1]. 2955 */ 2956 void 2957 ire_dep_unbuild(ire_t *ires[], uint_t count) 2958 { 2959 ip_stack_t *ipst; 2960 uint_t i; 2961 2962 if (count == 0) { 2963 /* No work to do */ 2964 return; 2965 } 2966 ipst = ires[0]->ire_ipst; 2967 rw_enter(&ipst->ips_ire_dep_lock, RW_WRITER); 2968 for (i = 0; i < count; i++) { 2969 ASSERT(ires[i]->ire_ipversion == IPV4_VERSION || 2970 ires[i]->ire_ipversion == IPV6_VERSION); 2971 if (ires[i]->ire_dep_parent != NULL) 2972 ire_dep_remove(ires[i]); 2973 mutex_enter(&ires[i]->ire_lock); 2974 ires[i]->ire_dep_parent_generation = IRE_GENERATION_VERIFY; 2975 mutex_exit(&ires[i]->ire_lock); 2976 } 2977 rw_exit(&ipst->ips_ire_dep_lock); 2978 } 2979 2980 /* 2981 * Both the forwarding and the outbound code paths can trip on 2982 * a condemned NCE, in which case we call this function. 2983 * We have two different behaviors: if the NCE was UNREACHABLE 2984 * it is an indication that something failed. In that case 2985 * we see if we should look for a different IRE (for example, 2986 * delete any matching redirect IRE, or try a different 2987 * IRE_DEFAULT (ECMP)). We mark the ire as bad so a hopefully 2988 * different IRE will be picked next time we send/forward. 2989 * 2990 * If we are called by the output path then fail_if_better is set 2991 * and we return NULL if there could be a better IRE. This is because the 2992 * output path retries the IRE lookup. (The input/forward path can not retry.) 2993 * 2994 * If the NCE was not unreachable then we pick/allocate a 2995 * new (most likely ND_INITIAL) NCE and proceed with it. 2996 * 2997 * ipha/ip6h are needed for multicast packets; ipha needs to be 2998 * set for IPv4 and ip6h needs to be set for IPv6 packets. 2999 */ 3000 nce_t * 3001 ire_handle_condemned_nce(nce_t *nce, ire_t *ire, ipha_t *ipha, ip6_t *ip6h, 3002 boolean_t fail_if_better) 3003 { 3004 if (nce->nce_common->ncec_state == ND_UNREACHABLE) { 3005 if (ire_no_good(ire) && fail_if_better) { 3006 /* 3007 * Did some changes, or ECMP likely to exist. 3008 * Make ip_output look for a different IRE 3009 */ 3010 return (NULL); 3011 } 3012 } 3013 if (ire_revalidate_nce(ire) == ENETUNREACH) { 3014 /* The ire_dep_parent chain went bad, or no memory? */ 3015 (void) ire_no_good(ire); 3016 return (NULL); 3017 } 3018 if (ire->ire_ipversion == IPV4_VERSION) { 3019 ASSERT(ipha != NULL); 3020 nce = ire_to_nce(ire, ipha->ipha_dst, NULL); 3021 } else { 3022 ASSERT(ip6h != NULL); 3023 nce = ire_to_nce(ire, INADDR_ANY, &ip6h->ip6_dst); 3024 } 3025 3026 if (nce == NULL) 3027 return (NULL); 3028 if (nce->nce_is_condemned) { 3029 nce_refrele(nce); 3030 return (NULL); 3031 } 3032 return (nce); 3033 } 3034 3035 /* 3036 * The caller has found that the ire is bad, either due to a reference to an NCE 3037 * in ND_UNREACHABLE state, or a MULTIRT route whose gateway can't be resolved. 3038 * We update things so a subsequent attempt to send to the destination 3039 * is likely to find different IRE, or that a new NCE would be created. 3040 * 3041 * Returns B_TRUE if it is likely that a subsequent ire_ftable_lookup would 3042 * find a different route (either due to having deleted a redirect, or there 3043 * being ECMP routes.) 3044 * 3045 * If we have a redirect (RTF_DYNAMIC) we delete it. 3046 * Otherwise we increment ire_badcnt and increment the generation number so 3047 * that a cached ixa_ire will redo the route selection. ire_badcnt is taken 3048 * into account in the route selection when we have multiple choices (multiple 3049 * default routes or ECMP in general). 3050 * Any time ip_select_route find an ire with a condemned ire_nce_cache 3051 * (e.g., if no equal cost route to the bad one) ip_select_route will make 3052 * sure the NCE is revalidated to avoid getting stuck on a 3053 * NCE_F_CONDMNED ncec that caused ire_no_good to be called. 3054 */ 3055 boolean_t 3056 ire_no_good(ire_t *ire) 3057 { 3058 ip_stack_t *ipst = ire->ire_ipst; 3059 ire_t *ire2; 3060 nce_t *nce; 3061 3062 if (ire->ire_flags & RTF_DYNAMIC) { 3063 ire_delete(ire); 3064 return (B_TRUE); 3065 } 3066 if (ire->ire_flags & RTF_INDIRECT) { 3067 /* Check if next IRE is a redirect */ 3068 rw_enter(&ipst->ips_ire_dep_lock, RW_READER); 3069 if (ire->ire_dep_parent != NULL && 3070 (ire->ire_dep_parent->ire_flags & RTF_DYNAMIC)) { 3071 ire2 = ire->ire_dep_parent; 3072 ire_refhold(ire2); 3073 } else { 3074 ire2 = NULL; 3075 } 3076 rw_exit(&ipst->ips_ire_dep_lock); 3077 if (ire2 != NULL) { 3078 ire_delete(ire2); 3079 ire_refrele(ire2); 3080 return (B_TRUE); 3081 } 3082 } 3083 /* 3084 * No redirect involved. Increment badcnt so that if we have ECMP 3085 * routes we are likely to pick a different one for the next packet. 3086 * 3087 * If the NCE is unreachable and condemned we should drop the reference 3088 * to it so that a new NCE can be created. 3089 * 3090 * Finally we increment the generation number so that any ixa_ire 3091 * cache will be revalidated. 3092 */ 3093 mutex_enter(&ire->ire_lock); 3094 ire->ire_badcnt++; 3095 ire->ire_last_badcnt = TICK_TO_SEC(lbolt64); 3096 nce = ire->ire_nce_cache; 3097 if (nce != NULL && nce->nce_is_condemned && 3098 nce->nce_common->ncec_state == ND_UNREACHABLE) 3099 ire->ire_nce_cache = NULL; 3100 else 3101 nce = NULL; 3102 mutex_exit(&ire->ire_lock); 3103 if (nce != NULL) 3104 nce_refrele(nce); 3105 3106 ire_increment_generation(ire); 3107 ire_dep_incr_generation(ire); 3108 3109 return (ire->ire_bucket->irb_ire_cnt > 1); 3110 } 3111 3112 /* 3113 * Walk ire_dep_parent chain and validate that ire_dep_parent->ire_generation == 3114 * ire_dep_parent_generation. 3115 * If they all match we just return ire_generation from the topmost IRE. 3116 * Otherwise we propagate the mismatch by setting all ire_dep_parent_generation 3117 * above the mismatch to IRE_GENERATION_VERIFY and also returning 3118 * IRE_GENERATION_VERIFY. 3119 */ 3120 uint_t 3121 ire_dep_validate_generations(ire_t *ire) 3122 { 3123 ip_stack_t *ipst = ire->ire_ipst; 3124 uint_t generation; 3125 ire_t *ire1; 3126 3127 rw_enter(&ipst->ips_ire_dep_lock, RW_READER); 3128 generation = ire->ire_generation; /* Assuming things match */ 3129 for (ire1 = ire; ire1 != NULL; ire1 = ire1->ire_dep_parent) { 3130 ASSERT(ire1->ire_ipversion == IPV4_VERSION || 3131 ire1->ire_ipversion == IPV6_VERSION); 3132 if (ire1->ire_dep_parent == NULL) 3133 break; 3134 if (ire1->ire_dep_parent_generation != 3135 ire1->ire_dep_parent->ire_generation) 3136 goto mismatch; 3137 } 3138 rw_exit(&ipst->ips_ire_dep_lock); 3139 return (generation); 3140 3141 mismatch: 3142 generation = IRE_GENERATION_VERIFY; 3143 /* Fill from top down to the mismatch with _VERIFY */ 3144 while (ire != ire1) { 3145 ASSERT(ire->ire_ipversion == IPV4_VERSION || 3146 ire->ire_ipversion == IPV6_VERSION); 3147 mutex_enter(&ire->ire_lock); 3148 ire->ire_dep_parent_generation = IRE_GENERATION_VERIFY; 3149 mutex_exit(&ire->ire_lock); 3150 ire = ire->ire_dep_parent; 3151 } 3152 rw_exit(&ipst->ips_ire_dep_lock); 3153 return (generation); 3154 } 3155 3156 /* 3157 * Used when we need to return an ire with ire_dep_parent, but we 3158 * know the chain is invalid for instance we didn't create an IRE_IF_CLONE 3159 * Using IRE_GENERATION_VERIFY means that next time we'll redo the 3160 * recursive lookup. 3161 */ 3162 void 3163 ire_dep_invalidate_generations(ire_t *ire) 3164 { 3165 ip_stack_t *ipst = ire->ire_ipst; 3166 3167 rw_enter(&ipst->ips_ire_dep_lock, RW_READER); 3168 while (ire != NULL) { 3169 ASSERT(ire->ire_ipversion == IPV4_VERSION || 3170 ire->ire_ipversion == IPV6_VERSION); 3171 mutex_enter(&ire->ire_lock); 3172 ire->ire_dep_parent_generation = IRE_GENERATION_VERIFY; 3173 mutex_exit(&ire->ire_lock); 3174 ire = ire->ire_dep_parent; 3175 } 3176 rw_exit(&ipst->ips_ire_dep_lock); 3177 } 3178 3179 /* Set _VERIFY ire_dep_parent_generation for all children recursively */ 3180 static void 3181 ire_dep_invalidate_children(ire_t *child) 3182 { 3183 ip_stack_t *ipst = child->ire_ipst; 3184 3185 ASSERT(RW_WRITE_HELD(&ipst->ips_ire_dep_lock)); 3186 /* Depth first */ 3187 if (child->ire_dep_children != NULL) 3188 ire_dep_invalidate_children(child->ire_dep_children); 3189 3190 while (child != NULL) { 3191 mutex_enter(&child->ire_lock); 3192 child->ire_dep_parent_generation = IRE_GENERATION_VERIFY; 3193 mutex_exit(&child->ire_lock); 3194 child = child->ire_dep_sib_next; 3195 } 3196 } 3197 3198 static void 3199 ire_dep_increment_children(ire_t *child) 3200 { 3201 ip_stack_t *ipst = child->ire_ipst; 3202 3203 ASSERT(RW_READ_HELD(&ipst->ips_ire_dep_lock)); 3204 /* Depth first */ 3205 if (child->ire_dep_children != NULL) 3206 ire_dep_increment_children(child->ire_dep_children); 3207 3208 while (child != NULL) { 3209 if (!IRE_IS_CONDEMNED(child)) 3210 ire_increment_generation(child); 3211 child = child->ire_dep_sib_next; 3212 } 3213 } 3214 3215 /* 3216 * Walk all the children of this ire recursively and increment their 3217 * generation number. 3218 */ 3219 void 3220 ire_dep_incr_generation(ire_t *parent) 3221 { 3222 ip_stack_t *ipst = parent->ire_ipst; 3223 3224 rw_enter(&ipst->ips_ire_dep_lock, RW_READER); 3225 if (parent->ire_dep_children != NULL) 3226 ire_dep_increment_children(parent->ire_dep_children); 3227 rw_exit(&ipst->ips_ire_dep_lock); 3228 } 3229 3230 /* 3231 * Get a new ire_nce_cache for this IRE as well as its nexthop. 3232 * Returns zero if it succeeds. Can fail due to lack of memory or when 3233 * the route has become unreachable. Returns ENOMEM and ENETUNREACH in those 3234 * cases. 3235 * 3236 * In the in.mpathd case, the ire will have ire_testhidden 3237 * set; so we should create the ncec for the underlying ill. 3238 * 3239 * Note that the error returned by ire_revalidate_nce() is ignored by most 3240 * callers except ire_handle_condemned_nce(), which handles the ENETUNREACH 3241 * error to mark potentially bad ire's. For all the other callers, an 3242 * error return could indicate a transient condition like ENOMEM, or could 3243 * be the result of an interface that is going down/unplumbing. In the former 3244 * case (transient error), we would leave the old stale ire/ire_nce_cache 3245 * in place, and possibly use incorrect link-layer information to send packets 3246 * but would eventually recover. In the latter case (ill down/replumb), 3247 * ire_revalidate_nce() might return a condemned nce back, but we would then 3248 * recover in the packet output path. 3249 */ 3250 int 3251 ire_revalidate_nce(ire_t *ire) 3252 { 3253 nce_t *nce, *old_nce; 3254 ire_t *nexthop; 3255 3256 /* 3257 * For multicast we conceptually have an NCE but we don't store it 3258 * in ire_nce_cache; when ire_to_nce is called we allocate the nce. 3259 */ 3260 if (ire->ire_type & IRE_MULTICAST) 3261 return (0); 3262 3263 /* ire_testhidden should only be set on under-interfaces */ 3264 ASSERT(!ire->ire_testhidden || !IS_IPMP(ire->ire_ill)); 3265 3266 nexthop = ire_nexthop(ire); 3267 if (nexthop == NULL) { 3268 /* The route is potentially bad */ 3269 (void) ire_no_good(ire); 3270 return (ENETUNREACH); 3271 } 3272 if (ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) { 3273 ASSERT(ire->ire_ill != NULL); 3274 3275 if (ire->ire_ipversion == IPV4_VERSION) 3276 nce = nce_lookup_v4(ire->ire_ill, &ire->ire_addr); 3277 else 3278 nce = nce_lookup_v6(ire->ire_ill, &ire->ire_addr_v6); 3279 } else { 3280 ASSERT(nexthop->ire_type & IRE_ONLINK); 3281 if (ire->ire_ipversion == IPV4_VERSION) { 3282 nce = arp_nce_init(nexthop->ire_ill, nexthop->ire_addr, 3283 nexthop->ire_type); 3284 } else { 3285 nce = ndp_nce_init(nexthop->ire_ill, 3286 &nexthop->ire_addr_v6, nexthop->ire_type); 3287 } 3288 } 3289 if (nce == NULL) { 3290 /* 3291 * Leave the old stale one in place to avoid a NULL 3292 * ire_nce_cache. 3293 */ 3294 ire_refrele(nexthop); 3295 return (ENOMEM); 3296 } 3297 3298 if (nexthop != ire) { 3299 /* Update the nexthop ire */ 3300 mutex_enter(&nexthop->ire_lock); 3301 old_nce = nexthop->ire_nce_cache; 3302 if (!IRE_IS_CONDEMNED(nexthop)) { 3303 nce_refhold(nce); 3304 nexthop->ire_nce_cache = nce; 3305 } else { 3306 nexthop->ire_nce_cache = NULL; 3307 } 3308 mutex_exit(&nexthop->ire_lock); 3309 if (old_nce != NULL) 3310 nce_refrele(old_nce); 3311 } 3312 ire_refrele(nexthop); 3313 3314 mutex_enter(&ire->ire_lock); 3315 old_nce = ire->ire_nce_cache; 3316 if (!IRE_IS_CONDEMNED(ire)) { 3317 nce_refhold(nce); 3318 ire->ire_nce_cache = nce; 3319 } else { 3320 ire->ire_nce_cache = NULL; 3321 } 3322 mutex_exit(&ire->ire_lock); 3323 if (old_nce != NULL) 3324 nce_refrele(old_nce); 3325 3326 nce_refrele(nce); 3327 return (0); 3328 } 3329 3330 /* 3331 * Get a held nce for a given ire. 3332 * In the common case this is just from ire_nce_cache. 3333 * For IRE_MULTICAST this needs to do an explicit lookup since we do not 3334 * have an IRE_MULTICAST per address. 3335 * Note that this explicitly returns CONDEMNED NCEs. The caller needs those 3336 * so they can check whether the NCE went unreachable (as opposed to was 3337 * condemned for some other reason). 3338 */ 3339 nce_t * 3340 ire_to_nce(ire_t *ire, ipaddr_t v4nexthop, const in6_addr_t *v6nexthop) 3341 { 3342 nce_t *nce; 3343 3344 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) 3345 return (NULL); 3346 3347 /* ire_testhidden should only be set on under-interfaces */ 3348 ASSERT(!ire->ire_testhidden || !IS_IPMP(ire->ire_ill)); 3349 3350 mutex_enter(&ire->ire_lock); 3351 nce = ire->ire_nce_cache; 3352 if (nce != NULL) { 3353 nce_refhold(nce); 3354 mutex_exit(&ire->ire_lock); 3355 return (nce); 3356 } 3357 mutex_exit(&ire->ire_lock); 3358 3359 if (ire->ire_type & IRE_MULTICAST) { 3360 ASSERT(ire->ire_ill != NULL); 3361 3362 if (ire->ire_ipversion == IPV4_VERSION) { 3363 ASSERT(v6nexthop == NULL); 3364 3365 nce = arp_nce_init(ire->ire_ill, v4nexthop, 3366 ire->ire_type); 3367 } else { 3368 ASSERT(v6nexthop != NULL); 3369 ASSERT(v4nexthop == 0); 3370 nce = ndp_nce_init(ire->ire_ill, v6nexthop, 3371 ire->ire_type); 3372 } 3373 return (nce); 3374 } 3375 return (NULL); 3376 } 3377 3378 nce_t * 3379 ire_to_nce_pkt(ire_t *ire, mblk_t *mp) 3380 { 3381 ipha_t *ipha; 3382 ip6_t *ip6h; 3383 3384 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 3385 ipha = (ipha_t *)mp->b_rptr; 3386 return (ire_to_nce(ire, ipha->ipha_dst, NULL)); 3387 } else { 3388 ip6h = (ip6_t *)mp->b_rptr; 3389 return (ire_to_nce(ire, INADDR_ANY, &ip6h->ip6_dst)); 3390 } 3391 } 3392 3393 /* 3394 * Given an IRE_INTERFACE (that matches more than one address) create 3395 * and return an IRE_IF_CLONE for the specific address. 3396 * Return the generation number. 3397 * Returns NULL is no memory for the IRE. 3398 * Handles both IPv4 and IPv6. 3399 */ 3400 ire_t * 3401 ire_create_if_clone(ire_t *ire_if, const in6_addr_t *addr, uint_t *generationp) 3402 { 3403 ire_t *ire; 3404 ire_t *nire; 3405 3406 if (ire_if->ire_ipversion == IPV4_VERSION) { 3407 ipaddr_t v4addr; 3408 ipaddr_t mask = IP_HOST_MASK; 3409 3410 ASSERT(IN6_IS_ADDR_V4MAPPED(addr)); 3411 IN6_V4MAPPED_TO_IPADDR(addr, v4addr); 3412 3413 ire = ire_create( 3414 (uchar_t *)&v4addr, /* dest address */ 3415 (uchar_t *)&mask, /* mask */ 3416 (uchar_t *)&ire_if->ire_gateway_addr, 3417 IRE_IF_CLONE, /* IRE type */ 3418 ire_if->ire_ill, 3419 ire_if->ire_zoneid, 3420 ire_if->ire_flags | RTF_HOST, 3421 NULL, /* No security attr for IRE_IF_ALL */ 3422 ire_if->ire_ipst); 3423 } else { 3424 ASSERT(!IN6_IS_ADDR_V4MAPPED(addr)); 3425 ire = ire_create_v6( 3426 addr, /* dest address */ 3427 &ipv6_all_ones, /* mask */ 3428 &ire_if->ire_gateway_addr_v6, /* gateway addr */ 3429 IRE_IF_CLONE, /* IRE type */ 3430 ire_if->ire_ill, 3431 ire_if->ire_zoneid, 3432 ire_if->ire_flags | RTF_HOST, 3433 NULL, /* No security attr for IRE_IF_ALL */ 3434 ire_if->ire_ipst); 3435 } 3436 if (ire == NULL) 3437 return (NULL); 3438 3439 /* Take the metrics, in particular the mtu, from the IRE_IF */ 3440 ire->ire_metrics = ire_if->ire_metrics; 3441 3442 nire = ire_add(ire); 3443 if (nire == NULL) /* Some failure */ 3444 return (NULL); 3445 3446 if (generationp != NULL) 3447 *generationp = nire->ire_generation; 3448 3449 /* 3450 * Make sure races don't add a duplicate by 3451 * catching the case when an identical was returned. 3452 */ 3453 if (nire != ire) { 3454 ASSERT(nire->ire_identical_ref > 1); 3455 ire_delete(nire); 3456 } 3457 return (nire); 3458 } 3459 3460 /* 3461 * The argument is an IRE_INTERFACE. Delete all of IRE_IF_CLONE in the 3462 * ire_dep_children (just walk the ire_dep_sib_next since they are all 3463 * immediate children.) 3464 * Since we hold a lock while we remove them we need to defer the actual 3465 * calls to ire_delete() until we have dropped the lock. This makes things 3466 * less efficient since we restart at the top after dropping the lock. But 3467 * we only run when an IRE_INTERFACE is deleted which is infrquent. 3468 * 3469 * Note that ire_dep_children can be any mixture of offlink routes and 3470 * IRE_IF_CLONE entries. 3471 */ 3472 void 3473 ire_dep_delete_if_clone(ire_t *parent) 3474 { 3475 ip_stack_t *ipst = parent->ire_ipst; 3476 ire_t *child, *next; 3477 3478 restart: 3479 rw_enter(&ipst->ips_ire_dep_lock, RW_READER); 3480 if (parent->ire_dep_children == NULL) { 3481 rw_exit(&ipst->ips_ire_dep_lock); 3482 return; 3483 } 3484 child = parent->ire_dep_children; 3485 while (child != NULL) { 3486 next = child->ire_dep_sib_next; 3487 if ((child->ire_type & IRE_IF_CLONE) && 3488 !IRE_IS_CONDEMNED(child)) { 3489 ire_refhold(child); 3490 rw_exit(&ipst->ips_ire_dep_lock); 3491 ire_delete(child); 3492 ASSERT(IRE_IS_CONDEMNED(child)); 3493 ire_refrele(child); 3494 goto restart; 3495 } 3496 child = next; 3497 } 3498 rw_exit(&ipst->ips_ire_dep_lock); 3499 } 3500 3501 /* 3502 * ire_pref() is used in recursive route-resolution for a destination to 3503 * determine the preference of an ire, where "preference" is determined 3504 * based on the level of indirection to the destination of the ire. 3505 * A higher preference indicates that fewer lookups are needed to complete 3506 * recursive route lookup. Thus 3507 * ire_pref(RTF_INDIRECT) < ire_pref(IRE_IF_RESOLVER) < ire_pref(IRE_PREF_CLONE) 3508 */ 3509 int 3510 ire_pref(ire_t *ire) 3511 { 3512 if (ire->ire_flags & RTF_INDIRECT) 3513 return (1); 3514 if (ire->ire_type & IRE_OFFLINK) 3515 return (2); 3516 if (ire->ire_type & (IRE_IF_RESOLVER|IRE_IF_NORESOLVER)) 3517 return (3); 3518 if (ire->ire_type & IRE_IF_CLONE) 3519 return (4); 3520 if (ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK|IRE_BROADCAST)) 3521 return (5); 3522 return (-1); /* unknown ire_type */ 3523 } 3524