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 2008 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/ddi.h> 35 #include <sys/cmn_err.h> 36 #include <sys/policy.h> 37 38 #include <sys/systm.h> 39 #include <sys/kmem.h> 40 #include <sys/param.h> 41 #include <sys/socket.h> 42 #include <net/if.h> 43 #include <net/route.h> 44 #include <netinet/in.h> 45 #include <net/if_dl.h> 46 #include <netinet/ip6.h> 47 #include <netinet/icmp6.h> 48 49 #include <inet/common.h> 50 #include <inet/mi.h> 51 #include <inet/ip.h> 52 #include <inet/ip6.h> 53 #include <inet/ip_ndp.h> 54 #include <inet/arp.h> 55 #include <inet/ip_if.h> 56 #include <inet/ip_ire.h> 57 #include <inet/ip_ftable.h> 58 #include <inet/ip_rts.h> 59 #include <inet/nd.h> 60 61 #include <net/pfkeyv2.h> 62 #include <inet/ipsec_info.h> 63 #include <inet/sadb.h> 64 #include <sys/kmem.h> 65 #include <inet/tcp.h> 66 #include <inet/ipclassifier.h> 67 #include <sys/zone.h> 68 #include <sys/cpuvar.h> 69 70 #include <sys/tsol/label.h> 71 #include <sys/tsol/tnet.h> 72 73 struct kmem_cache *rt_entry_cache; 74 75 /* 76 * Synchronization notes: 77 * 78 * The fields of the ire_t struct are protected in the following way : 79 * 80 * ire_next/ire_ptpn 81 * 82 * - bucket lock of the respective tables (cache or forwarding tables). 83 * 84 * ire_mp, ire_rfq, ire_stq, ire_u *except* ire_gateway_addr[v6], ire_mask, 85 * ire_type, ire_create_time, ire_masklen, ire_ipversion, ire_flags, ire_ipif, 86 * ire_ihandle, ire_phandle, ire_nce, ire_bucket, ire_in_ill, ire_in_src_addr 87 * 88 * - Set in ire_create_v4/v6 and never changes after that. Thus, 89 * we don't need a lock whenever these fields are accessed. 90 * 91 * - ire_bucket and ire_masklen (also set in ire_create) is set in 92 * ire_add_v4/ire_add_v6 before inserting in the bucket and never 93 * changes after that. Thus we don't need a lock whenever these 94 * fields are accessed. 95 * 96 * ire_gateway_addr_v4[v6] 97 * 98 * - ire_gateway_addr_v4[v6] is set during ire_create and later modified 99 * by rts_setgwr[v6]. As ire_gateway_addr is a uint32_t, updates to 100 * it assumed to be atomic and hence the other parts of the code 101 * does not use any locks. ire_gateway_addr_v6 updates are not atomic 102 * and hence any access to it uses ire_lock to get/set the right value. 103 * 104 * ire_ident, ire_refcnt 105 * 106 * - Updated atomically using atomic_add_32 107 * 108 * ire_ssthresh, ire_rtt_sd, ire_rtt, ire_ib_pkt_count, ire_ob_pkt_count 109 * 110 * - Assumes that 32 bit writes are atomic. No locks. ire_lock is 111 * used to serialize updates to ire_ssthresh, ire_rtt_sd, ire_rtt. 112 * 113 * ire_max_frag, ire_frag_flag 114 * 115 * - ire_lock is used to set/read both of them together. 116 * 117 * ire_tire_mark 118 * 119 * - Set in ire_create and updated in ire_expire, which is called 120 * by only one function namely ip_trash_timer_expire. Thus only 121 * one function updates and examines the value. 122 * 123 * ire_marks 124 * - bucket lock protects this. 125 * 126 * ire_ipsec_overhead/ire_ll_hdr_length 127 * 128 * - Place holder for returning the information to the upper layers 129 * when IRE_DB_REQ comes down. 130 * 131 * 132 * ipv6_ire_default_count is protected by the bucket lock of 133 * ip_forwarding_table_v6[0][0]. 134 * 135 * ipv6_ire_default_index is not protected as it is just a hint 136 * at which default gateway to use. There is nothing 137 * wrong in using the same gateway for two different connections. 138 * 139 * As we always hold the bucket locks in all the places while accessing 140 * the above values, it is natural to use them for protecting them. 141 * 142 * We have a separate cache table and forwarding table for IPv4 and IPv6. 143 * Cache table (ip_cache_table/ip_cache_table_v6) is a pointer to an 144 * array of irb_t structures. The IPv6 forwarding table 145 * (ip_forwarding_table_v6) is an array of pointers to arrays of irb_t 146 * structure. ip_forwarding_table_v6 is allocated dynamically in 147 * ire_add_v6. ire_ft_init_lock is used to serialize multiple threads 148 * initializing the same bucket. Once a bucket is initialized, it is never 149 * de-alloacted. This assumption enables us to access 150 * ip_forwarding_table_v6[i] without any locks. 151 * 152 * The forwarding table for IPv4 is a radix tree whose leaves 153 * are rt_entry structures containing the irb_t for the rt_dst. The irb_t 154 * for IPv4 is dynamically allocated and freed. 155 * 156 * Each irb_t - ire bucket structure has a lock to protect 157 * a bucket and the ires residing in the bucket have a back pointer to 158 * the bucket structure. It also has a reference count for the number 159 * of threads walking the bucket - irb_refcnt which is bumped up 160 * using the macro IRB_REFHOLD macro. The flags irb_flags can be 161 * set to IRE_MARK_CONDEMNED indicating that there are some ires 162 * in this bucket that are marked with IRE_MARK_CONDEMNED and the 163 * last thread to leave the bucket should delete the ires. Usually 164 * this is done by the IRB_REFRELE macro which is used to decrement 165 * the reference count on a bucket. See comments above irb_t structure 166 * definition in ip.h for further details. 167 * 168 * IRE_REFHOLD/IRE_REFRELE macros operate on the ire which increments/ 169 * decrements the reference count, ire_refcnt, atomically on the ire. 170 * ire_refcnt is modified only using this macro. Operations on the IRE 171 * could be described as follows : 172 * 173 * CREATE an ire with reference count initialized to 1. 174 * 175 * ADDITION of an ire holds the bucket lock, checks for duplicates 176 * and then adds the ire. ire_add_v4/ire_add_v6 returns the ire after 177 * bumping up once more i.e the reference count is 2. This is to avoid 178 * an extra lookup in the functions calling ire_add which wants to 179 * work with the ire after adding. 180 * 181 * LOOKUP of an ire bumps up the reference count using IRE_REFHOLD 182 * macro. It is valid to bump up the referece count of the IRE, 183 * after the lookup has returned an ire. Following are the lookup 184 * functions that return an HELD ire : 185 * 186 * ire_lookup_local[_v6], ire_ctable_lookup[_v6], ire_ftable_lookup[_v6], 187 * ire_cache_lookup[_v6], ire_lookup_multi[_v6], ire_route_lookup[_v6], 188 * ipif_to_ire[_v6]. 189 * 190 * DELETION of an ire holds the bucket lock, removes it from the list 191 * and then decrements the reference count for having removed from the list 192 * by using the IRE_REFRELE macro. If some other thread has looked up 193 * the ire, the reference count would have been bumped up and hence 194 * this ire will not be freed once deleted. It will be freed once the 195 * reference count drops to zero. 196 * 197 * Add and Delete acquires the bucket lock as RW_WRITER, while all the 198 * lookups acquire the bucket lock as RW_READER. 199 * 200 * NOTE : The only functions that does the IRE_REFRELE when an ire is 201 * passed as an argument are : 202 * 203 * 1) ip_wput_ire : This is because it IRE_REFHOLD/RELEs the 204 * broadcast ires it looks up internally within 205 * the function. Currently, for simplicity it does 206 * not differentiate the one that is passed in and 207 * the ones it looks up internally. It always 208 * IRE_REFRELEs. 209 * 2) ire_send 210 * ire_send_v6 : As ire_send calls ip_wput_ire and other functions 211 * that take ire as an argument, it has to selectively 212 * IRE_REFRELE the ire. To maintain symmetry, 213 * ire_send_v6 does the same. 214 * 215 * Otherwise, the general rule is to do the IRE_REFRELE in the function 216 * that is passing the ire as an argument. 217 * 218 * In trying to locate ires the following points are to be noted. 219 * 220 * IRE_MARK_CONDEMNED signifies that the ire has been logically deleted and is 221 * to be ignored when walking the ires using ire_next. 222 * 223 * IRE_MARK_HIDDEN signifies that the ire is a special ire typically for the 224 * benefit of in.mpathd which needs to probe interfaces for failures. Normal 225 * applications should not be seeing this ire and hence this ire is ignored 226 * in most cases in the search using ire_next. 227 * 228 * Zones note: 229 * Walking IREs within a given zone also walks certain ires in other 230 * zones. This is done intentionally. IRE walks with a specified 231 * zoneid are used only when doing informational reports, and 232 * zone users want to see things that they can access. See block 233 * comment in ire_walk_ill_match(). 234 */ 235 236 /* 237 * The minimum size of IRE cache table. It will be recalcuated in 238 * ip_ire_init(). 239 * Setable in /etc/system 240 */ 241 uint32_t ip_cache_table_size = IP_CACHE_TABLE_SIZE; 242 uint32_t ip6_cache_table_size = IP6_CACHE_TABLE_SIZE; 243 244 /* 245 * The size of the forwarding table. We will make sure that it is a 246 * power of 2 in ip_ire_init(). 247 * Setable in /etc/system 248 */ 249 uint32_t ip6_ftable_hash_size = IP6_FTABLE_HASH_SIZE; 250 251 struct kmem_cache *ire_cache; 252 static ire_t ire_null; 253 254 /* 255 * The threshold number of IRE in a bucket when the IREs are 256 * cleaned up. This threshold is calculated later in ip_open() 257 * based on the speed of CPU and available memory. This default 258 * value is the maximum. 259 * 260 * We have two kinds of cached IRE, temporary and 261 * non-temporary. Temporary IREs are marked with 262 * IRE_MARK_TEMPORARY. They are IREs created for non 263 * TCP traffic and for forwarding purposes. All others 264 * are non-temporary IREs. We don't mark IRE created for 265 * TCP as temporary because TCP is stateful and there are 266 * info stored in the IRE which can be shared by other TCP 267 * connections to the same destination. For connected 268 * endpoint, we also don't want to mark the IRE used as 269 * temporary because the same IRE will be used frequently, 270 * otherwise, the app should not do a connect(). We change 271 * the marking at ip_bind_connected_*() if necessary. 272 * 273 * We want to keep the cache IRE hash bucket length reasonably 274 * short, otherwise IRE lookup functions will take "forever." 275 * We use the "crude" function that the IRE bucket 276 * length should be based on the CPU speed, which is 1 entry 277 * per x MHz, depending on the shift factor ip_ire_cpu_ratio 278 * (n). This means that with a 750MHz CPU, the max bucket 279 * length can be (750 >> n) entries. 280 * 281 * Note that this threshold is separate for temp and non-temp 282 * IREs. This means that the actual bucket length can be 283 * twice as that. And while we try to keep temporary IRE 284 * length at most at the threshold value, we do not attempt to 285 * make the length for non-temporary IREs fixed, for the 286 * reason stated above. Instead, we start trying to find 287 * "unused" non-temporary IREs when the bucket length reaches 288 * this threshold and clean them up. 289 * 290 * We also want to limit the amount of memory used by 291 * IREs. So if we are allowed to use ~3% of memory (M) 292 * for those IREs, each bucket should not have more than 293 * 294 * M / num of cache bucket / sizeof (ire_t) 295 * 296 * Again the above memory uses are separate for temp and 297 * non-temp cached IREs. 298 * 299 * We may also want the limit to be a function of the number 300 * of interfaces and number of CPUs. Doing the initialization 301 * in ip_open() means that every time an interface is plumbed, 302 * the max is re-calculated. Right now, we don't do anything 303 * different. In future, when we have more experience, we 304 * may want to change this behavior. 305 */ 306 uint32_t ip_ire_max_bucket_cnt = 10; /* Setable in /etc/system */ 307 uint32_t ip6_ire_max_bucket_cnt = 10; 308 uint32_t ip_ire_cleanup_cnt = 2; 309 310 /* 311 * The minimum of the temporary IRE bucket count. We do not want 312 * the length of each bucket to be too short. This may hurt 313 * performance of some apps as the temporary IREs are removed too 314 * often. 315 */ 316 uint32_t ip_ire_min_bucket_cnt = 3; /* /etc/system - not used */ 317 uint32_t ip6_ire_min_bucket_cnt = 3; 318 319 /* 320 * The ratio of memory consumed by IRE used for temporary to available 321 * memory. This is a shift factor, so 6 means the ratio 1 to 64. This 322 * value can be changed in /etc/system. 6 is a reasonable number. 323 */ 324 uint32_t ip_ire_mem_ratio = 6; /* /etc/system */ 325 /* The shift factor for CPU speed to calculate the max IRE bucket length. */ 326 uint32_t ip_ire_cpu_ratio = 7; /* /etc/system */ 327 328 typedef struct nce_clookup_s { 329 ipaddr_t ncecl_addr; 330 boolean_t ncecl_found; 331 } nce_clookup_t; 332 333 /* 334 * The maximum number of buckets in IRE cache table. In future, we may 335 * want to make it a dynamic hash table. For the moment, we fix the 336 * size and allocate the table in ip_ire_init() when IP is first loaded. 337 * We take into account the amount of memory a system has. 338 */ 339 #define IP_MAX_CACHE_TABLE_SIZE 4096 340 341 /* Setable in /etc/system */ 342 static uint32_t ip_max_cache_table_size = IP_MAX_CACHE_TABLE_SIZE; 343 static uint32_t ip6_max_cache_table_size = IP_MAX_CACHE_TABLE_SIZE; 344 345 #define NUM_ILLS 2 /* To build the ILL list to unlock */ 346 347 /* Zero iulp_t for initialization. */ 348 const iulp_t ire_uinfo_null = { 0 }; 349 350 static int ire_add_v4(ire_t **ire_p, queue_t *q, mblk_t *mp, 351 ipsq_func_t func, boolean_t); 352 static void ire_delete_v4(ire_t *ire); 353 static void ire_walk_ipvers(pfv_t func, void *arg, uchar_t vers, 354 zoneid_t zoneid, ip_stack_t *); 355 static void ire_walk_ill_ipvers(uint_t match_flags, uint_t ire_type, 356 pfv_t func, void *arg, uchar_t vers, ill_t *ill); 357 static void ire_cache_cleanup(irb_t *irb, uint32_t threshold, 358 ire_t *ref_ire); 359 static void ip_nce_clookup_and_delete(nce_t *nce, void *arg); 360 #ifdef DEBUG 361 static void ire_trace_cleanup(const ire_t *); 362 #endif 363 364 /* 365 * To avoid bloating the code, we call this function instead of 366 * using the macro IRE_REFRELE. Use macro only in performance 367 * critical paths. 368 * 369 * Must not be called while holding any locks. Otherwise if this is 370 * the last reference to be released there is a chance of recursive mutex 371 * panic due to ire_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying 372 * to restart an ioctl. The one exception is when the caller is sure that 373 * this is not the last reference to be released. Eg. if the caller is 374 * sure that the ire has not been deleted and won't be deleted. 375 */ 376 void 377 ire_refrele(ire_t *ire) 378 { 379 IRE_REFRELE(ire); 380 } 381 382 void 383 ire_refrele_notr(ire_t *ire) 384 { 385 IRE_REFRELE_NOTR(ire); 386 } 387 388 /* 389 * kmem_cache_alloc constructor for IRE in kma space. 390 * Note that when ire_mp is set the IRE is stored in that mblk and 391 * not in this cache. 392 */ 393 /* ARGSUSED */ 394 static int 395 ip_ire_constructor(void *buf, void *cdrarg, int kmflags) 396 { 397 ire_t *ire = buf; 398 399 ire->ire_nce = NULL; 400 401 return (0); 402 } 403 404 /* ARGSUSED1 */ 405 static void 406 ip_ire_destructor(void *buf, void *cdrarg) 407 { 408 ire_t *ire = buf; 409 410 ASSERT(ire->ire_nce == NULL); 411 } 412 413 /* 414 * This function is associated with the IP_IOC_IRE_ADVISE_NO_REPLY 415 * IOCTL. It is used by TCP (or other ULPs) to supply revised information 416 * for an existing CACHED IRE. 417 */ 418 /* ARGSUSED */ 419 int 420 ip_ire_advise(queue_t *q, mblk_t *mp, cred_t *ioc_cr) 421 { 422 uchar_t *addr_ucp; 423 ipic_t *ipic; 424 ire_t *ire; 425 ipaddr_t addr; 426 in6_addr_t v6addr; 427 irb_t *irb; 428 zoneid_t zoneid; 429 ip_stack_t *ipst = CONNQ_TO_IPST(q); 430 431 ASSERT(q->q_next == NULL); 432 zoneid = Q_TO_CONN(q)->conn_zoneid; 433 434 /* 435 * Check privilege using the ioctl credential; if it is NULL 436 * then this is a kernel message and therefor privileged. 437 */ 438 if (ioc_cr != NULL && secpolicy_ip_config(ioc_cr, B_FALSE) != 0) 439 return (EPERM); 440 441 ipic = (ipic_t *)mp->b_rptr; 442 if (!(addr_ucp = mi_offset_param(mp, ipic->ipic_addr_offset, 443 ipic->ipic_addr_length))) { 444 return (EINVAL); 445 } 446 if (!OK_32PTR(addr_ucp)) 447 return (EINVAL); 448 switch (ipic->ipic_addr_length) { 449 case IP_ADDR_LEN: { 450 /* Extract the destination address. */ 451 addr = *(ipaddr_t *)addr_ucp; 452 /* Find the corresponding IRE. */ 453 ire = ire_cache_lookup(addr, zoneid, NULL, ipst); 454 break; 455 } 456 case IPV6_ADDR_LEN: { 457 /* Extract the destination address. */ 458 v6addr = *(in6_addr_t *)addr_ucp; 459 /* Find the corresponding IRE. */ 460 ire = ire_cache_lookup_v6(&v6addr, zoneid, NULL, ipst); 461 break; 462 } 463 default: 464 return (EINVAL); 465 } 466 467 if (ire == NULL) 468 return (ENOENT); 469 /* 470 * Update the round trip time estimate and/or the max frag size 471 * and/or the slow start threshold. 472 * 473 * We serialize multiple advises using ire_lock. 474 */ 475 mutex_enter(&ire->ire_lock); 476 if (ipic->ipic_rtt) { 477 /* 478 * If there is no old cached values, initialize them 479 * conservatively. Set them to be (1.5 * new value). 480 */ 481 if (ire->ire_uinfo.iulp_rtt != 0) { 482 ire->ire_uinfo.iulp_rtt = (ire->ire_uinfo.iulp_rtt + 483 ipic->ipic_rtt) >> 1; 484 } else { 485 ire->ire_uinfo.iulp_rtt = ipic->ipic_rtt + 486 (ipic->ipic_rtt >> 1); 487 } 488 if (ire->ire_uinfo.iulp_rtt_sd != 0) { 489 ire->ire_uinfo.iulp_rtt_sd = 490 (ire->ire_uinfo.iulp_rtt_sd + 491 ipic->ipic_rtt_sd) >> 1; 492 } else { 493 ire->ire_uinfo.iulp_rtt_sd = ipic->ipic_rtt_sd + 494 (ipic->ipic_rtt_sd >> 1); 495 } 496 } 497 if (ipic->ipic_max_frag) 498 ire->ire_max_frag = MIN(ipic->ipic_max_frag, IP_MAXPACKET); 499 if (ipic->ipic_ssthresh != 0) { 500 if (ire->ire_uinfo.iulp_ssthresh != 0) 501 ire->ire_uinfo.iulp_ssthresh = 502 (ipic->ipic_ssthresh + 503 ire->ire_uinfo.iulp_ssthresh) >> 1; 504 else 505 ire->ire_uinfo.iulp_ssthresh = ipic->ipic_ssthresh; 506 } 507 /* 508 * Don't need the ire_lock below this. ire_type does not change 509 * after initialization. ire_marks is protected by irb_lock. 510 */ 511 mutex_exit(&ire->ire_lock); 512 513 if (ipic->ipic_ire_marks != 0 && ire->ire_type == IRE_CACHE) { 514 /* 515 * Only increment the temporary IRE count if the original 516 * IRE is not already marked temporary. 517 */ 518 irb = ire->ire_bucket; 519 rw_enter(&irb->irb_lock, RW_WRITER); 520 if ((ipic->ipic_ire_marks & IRE_MARK_TEMPORARY) && 521 !(ire->ire_marks & IRE_MARK_TEMPORARY)) { 522 irb->irb_tmp_ire_cnt++; 523 } 524 ire->ire_marks |= ipic->ipic_ire_marks; 525 rw_exit(&irb->irb_lock); 526 } 527 528 ire_refrele(ire); 529 return (0); 530 } 531 532 /* 533 * This function is associated with the IP_IOC_IRE_DELETE[_NO_REPLY] 534 * IOCTL[s]. The NO_REPLY form is used by TCP to delete a route IRE 535 * for a host that is not responding. This will force an attempt to 536 * establish a new route, if available, and flush out the ARP entry so 537 * it will re-resolve. Management processes may want to use the 538 * version that generates a reply. 539 * 540 * This function does not support IPv6 since Neighbor Unreachability Detection 541 * means that negative advise like this is useless. 542 */ 543 /* ARGSUSED */ 544 int 545 ip_ire_delete(queue_t *q, mblk_t *mp, cred_t *ioc_cr) 546 { 547 uchar_t *addr_ucp; 548 ipaddr_t addr; 549 ire_t *ire; 550 ipid_t *ipid; 551 boolean_t routing_sock_info = B_FALSE; /* Sent info? */ 552 zoneid_t zoneid; 553 ire_t *gire = NULL; 554 ill_t *ill; 555 mblk_t *arp_mp; 556 ip_stack_t *ipst; 557 558 ASSERT(q->q_next == NULL); 559 zoneid = Q_TO_CONN(q)->conn_zoneid; 560 ipst = CONNQ_TO_IPST(q); 561 562 /* 563 * Check privilege using the ioctl credential; if it is NULL 564 * then this is a kernel message and therefor privileged. 565 */ 566 if (ioc_cr != NULL && secpolicy_ip_config(ioc_cr, B_FALSE) != 0) 567 return (EPERM); 568 569 ipid = (ipid_t *)mp->b_rptr; 570 571 /* Only actions on IRE_CACHEs are acceptable at present. */ 572 if (ipid->ipid_ire_type != IRE_CACHE) 573 return (EINVAL); 574 575 addr_ucp = mi_offset_param(mp, ipid->ipid_addr_offset, 576 ipid->ipid_addr_length); 577 if (addr_ucp == NULL || !OK_32PTR(addr_ucp)) 578 return (EINVAL); 579 switch (ipid->ipid_addr_length) { 580 case IP_ADDR_LEN: 581 /* addr_ucp points at IP addr */ 582 break; 583 case sizeof (sin_t): { 584 sin_t *sin; 585 /* 586 * got complete (sockaddr) address - increment addr_ucp to point 587 * at the ip_addr field. 588 */ 589 sin = (sin_t *)addr_ucp; 590 addr_ucp = (uchar_t *)&sin->sin_addr.s_addr; 591 break; 592 } 593 default: 594 return (EINVAL); 595 } 596 /* Extract the destination address. */ 597 bcopy(addr_ucp, &addr, IP_ADDR_LEN); 598 599 /* Try to find the CACHED IRE. */ 600 ire = ire_cache_lookup(addr, zoneid, NULL, ipst); 601 602 /* Nail it. */ 603 if (ire) { 604 /* Allow delete only on CACHE entries */ 605 if (ire->ire_type != IRE_CACHE) { 606 ire_refrele(ire); 607 return (EINVAL); 608 } 609 610 /* 611 * Verify that the IRE has been around for a while. 612 * This is to protect against transport protocols 613 * that are too eager in sending delete messages. 614 */ 615 if (gethrestime_sec() < 616 ire->ire_create_time + ipst->ips_ip_ignore_delete_time) { 617 ire_refrele(ire); 618 return (EINVAL); 619 } 620 /* 621 * Now we have a potentially dead cache entry. We need 622 * to remove it. 623 * If this cache entry is generated from a 624 * default route (i.e., ire_cmask == 0), 625 * search the default list and mark it dead and some 626 * background process will try to activate it. 627 */ 628 if ((ire->ire_gateway_addr != 0) && (ire->ire_cmask == 0)) { 629 /* 630 * Make sure that we pick a different 631 * IRE_DEFAULT next time. 632 */ 633 ire_t *gw_ire; 634 irb_t *irb = NULL; 635 uint_t match_flags; 636 637 match_flags = (MATCH_IRE_DEFAULT | MATCH_IRE_RJ_BHOLE); 638 639 gire = ire_ftable_lookup(ire->ire_addr, 640 ire->ire_cmask, 0, 0, 641 ire->ire_ipif, NULL, zoneid, 0, NULL, match_flags, 642 ipst); 643 644 ip3dbg(("ire_ftable_lookup() returned gire %p\n", 645 (void *)gire)); 646 647 if (gire != NULL) { 648 irb = gire->ire_bucket; 649 650 /* 651 * We grab it as writer just to serialize 652 * multiple threads trying to bump up 653 * irb_rr_origin 654 */ 655 rw_enter(&irb->irb_lock, RW_WRITER); 656 if ((gw_ire = irb->irb_rr_origin) == NULL) { 657 rw_exit(&irb->irb_lock); 658 goto done; 659 } 660 661 DTRACE_PROBE1(ip__ire__del__origin, 662 (ire_t *), gw_ire); 663 664 /* Skip past the potentially bad gateway */ 665 if (ire->ire_gateway_addr == 666 gw_ire->ire_gateway_addr) { 667 ire_t *next = gw_ire->ire_next; 668 669 DTRACE_PROBE2(ip__ire__del, 670 (ire_t *), gw_ire, (irb_t *), irb); 671 IRE_FIND_NEXT_ORIGIN(next); 672 irb->irb_rr_origin = next; 673 } 674 rw_exit(&irb->irb_lock); 675 } 676 } 677 done: 678 if (gire != NULL) 679 IRE_REFRELE(gire); 680 /* report the bad route to routing sockets */ 681 ip_rts_change(RTM_LOSING, ire->ire_addr, ire->ire_gateway_addr, 682 ire->ire_mask, ire->ire_src_addr, 0, 0, 0, 683 (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_IFA), ipst); 684 routing_sock_info = B_TRUE; 685 686 /* 687 * TCP is really telling us to start over completely, and it 688 * expects that we'll resend the ARP query. Tell ARP to 689 * discard the entry, if this is a local destination. 690 * 691 * But, if the ARP entry is permanent then it shouldn't be 692 * deleted, so we set ARED_F_PRESERVE_PERM. 693 */ 694 ill = ire->ire_stq->q_ptr; 695 if (ire->ire_gateway_addr == 0 && 696 (arp_mp = ill_ared_alloc(ill, addr)) != NULL) { 697 ared_t *ared = (ared_t *)arp_mp->b_rptr; 698 699 ASSERT(ared->ared_cmd == AR_ENTRY_DELETE); 700 ared->ared_flags |= ARED_F_PRESERVE_PERM; 701 putnext(ill->ill_rq, arp_mp); 702 } 703 704 ire_delete(ire); 705 ire_refrele(ire); 706 } 707 /* 708 * Also look for an IRE_HOST type redirect ire and 709 * remove it if present. 710 */ 711 ire = ire_route_lookup(addr, 0, 0, IRE_HOST, NULL, NULL, 712 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 713 714 /* Nail it. */ 715 if (ire != NULL) { 716 if (ire->ire_flags & RTF_DYNAMIC) { 717 if (!routing_sock_info) { 718 ip_rts_change(RTM_LOSING, ire->ire_addr, 719 ire->ire_gateway_addr, ire->ire_mask, 720 ire->ire_src_addr, 0, 0, 0, 721 (RTA_DST | RTA_GATEWAY | 722 RTA_NETMASK | RTA_IFA), 723 ipst); 724 } 725 ire_delete(ire); 726 } 727 ire_refrele(ire); 728 } 729 return (0); 730 } 731 732 733 /* 734 * ip_ire_req is called by ip_wput when an IRE_DB_REQ_TYPE message is handed 735 * down from the Upper Level Protocol to request a copy of the IRE (to check 736 * its type or to extract information like round-trip time estimates or the 737 * MTU.) 738 * The address is assumed to be in the ire_addr field. If no IRE is found 739 * an IRE is returned with ire_type being zero. 740 * Note that the upper lavel protocol has to check for broadcast 741 * (IRE_BROADCAST) and multicast (CLASSD(addr)). 742 * If there is a b_cont the resulting IRE_DB_TYPE mblk is placed at the 743 * end of the returned message. 744 * 745 * TCP sends down a message of this type with a connection request packet 746 * chained on. UDP and ICMP send it down to verify that a route exists for 747 * the destination address when they get connected. 748 */ 749 void 750 ip_ire_req(queue_t *q, mblk_t *mp) 751 { 752 ire_t *inire; 753 ire_t *ire; 754 mblk_t *mp1; 755 ire_t *sire = NULL; 756 zoneid_t zoneid = Q_TO_CONN(q)->conn_zoneid; 757 ip_stack_t *ipst = CONNQ_TO_IPST(q); 758 759 ASSERT(q->q_next == NULL); 760 761 if ((mp->b_wptr - mp->b_rptr) < sizeof (ire_t) || 762 !OK_32PTR(mp->b_rptr)) { 763 freemsg(mp); 764 return; 765 } 766 inire = (ire_t *)mp->b_rptr; 767 /* 768 * Got it, now take our best shot at an IRE. 769 */ 770 if (inire->ire_ipversion == IPV6_VERSION) { 771 ire = ire_route_lookup_v6(&inire->ire_addr_v6, 0, 0, 0, 772 NULL, &sire, zoneid, NULL, 773 (MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT), ipst); 774 } else { 775 ASSERT(inire->ire_ipversion == IPV4_VERSION); 776 ire = ire_route_lookup(inire->ire_addr, 0, 0, 0, 777 NULL, &sire, zoneid, NULL, 778 (MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT), ipst); 779 } 780 781 /* 782 * We prevent returning IRES with source address INADDR_ANY 783 * as these were temporarily created for sending packets 784 * from endpoints that have conn_unspec_src set. 785 */ 786 if (ire == NULL || 787 (ire->ire_ipversion == IPV4_VERSION && 788 ire->ire_src_addr == INADDR_ANY) || 789 (ire->ire_ipversion == IPV6_VERSION && 790 IN6_IS_ADDR_UNSPECIFIED(&ire->ire_src_addr_v6))) { 791 inire->ire_type = 0; 792 } else { 793 bcopy(ire, inire, sizeof (ire_t)); 794 /* Copy the route metrics from the parent. */ 795 if (sire != NULL) { 796 bcopy(&(sire->ire_uinfo), &(inire->ire_uinfo), 797 sizeof (iulp_t)); 798 } 799 800 /* 801 * As we don't lookup global policy here, we may not 802 * pass the right size if per-socket policy is not 803 * present. For these cases, path mtu discovery will 804 * do the right thing. 805 */ 806 inire->ire_ipsec_overhead = conn_ipsec_length(Q_TO_CONN(q)); 807 808 /* Pass the latest setting of the ip_path_mtu_discovery */ 809 inire->ire_frag_flag |= 810 (ipst->ips_ip_path_mtu_discovery) ? IPH_DF : 0; 811 } 812 if (ire != NULL) 813 ire_refrele(ire); 814 if (sire != NULL) 815 ire_refrele(sire); 816 mp->b_wptr = &mp->b_rptr[sizeof (ire_t)]; 817 mp->b_datap->db_type = IRE_DB_TYPE; 818 819 /* Put the IRE_DB_TYPE mblk last in the chain */ 820 mp1 = mp->b_cont; 821 if (mp1 != NULL) { 822 mp->b_cont = NULL; 823 linkb(mp1, mp); 824 mp = mp1; 825 } 826 qreply(q, mp); 827 } 828 829 /* 830 * Send a packet using the specified IRE. 831 * If ire_src_addr_v6 is all zero then discard the IRE after 832 * the packet has been sent. 833 */ 834 static void 835 ire_send(queue_t *q, mblk_t *pkt, ire_t *ire) 836 { 837 mblk_t *ipsec_mp; 838 boolean_t is_secure; 839 uint_t ifindex; 840 ill_t *ill; 841 zoneid_t zoneid = ire->ire_zoneid; 842 ip_stack_t *ipst = ire->ire_ipst; 843 844 ASSERT(ire->ire_ipversion == IPV4_VERSION); 845 ASSERT(!(ire->ire_type & IRE_LOCAL)); /* Has different ire_zoneid */ 846 ipsec_mp = pkt; 847 is_secure = (pkt->b_datap->db_type == M_CTL); 848 if (is_secure) { 849 ipsec_out_t *io; 850 851 pkt = pkt->b_cont; 852 io = (ipsec_out_t *)ipsec_mp->b_rptr; 853 if (io->ipsec_out_type == IPSEC_OUT) 854 zoneid = io->ipsec_out_zoneid; 855 } 856 857 /* If the packet originated externally then */ 858 if (pkt->b_prev) { 859 ire_refrele(ire); 860 /* 861 * Extract the ifindex from b_prev (set in ip_rput_noire). 862 * Look up interface to see if it still exists (it could have 863 * been unplumbed by the time the reply came back from ARP) 864 */ 865 ifindex = (uint_t)(uintptr_t)pkt->b_prev; 866 ill = ill_lookup_on_ifindex(ifindex, B_FALSE, 867 NULL, NULL, NULL, NULL, ipst); 868 if (ill == NULL) { 869 pkt->b_prev = NULL; 870 pkt->b_next = NULL; 871 freemsg(ipsec_mp); 872 return; 873 } 874 q = ill->ill_rq; 875 pkt->b_prev = NULL; 876 /* 877 * This packet has not gone through IPSEC processing 878 * and hence we should not have any IPSEC message 879 * prepended. 880 */ 881 ASSERT(ipsec_mp == pkt); 882 put(q, pkt); 883 ill_refrele(ill); 884 } else if (pkt->b_next) { 885 /* Packets from multicast router */ 886 pkt->b_next = NULL; 887 /* 888 * We never get the IPSEC_OUT while forwarding the 889 * packet for multicast router. 890 */ 891 ASSERT(ipsec_mp == pkt); 892 ip_rput_forward(ire, (ipha_t *)pkt->b_rptr, ipsec_mp, NULL); 893 ire_refrele(ire); 894 } else { 895 /* Locally originated packets */ 896 boolean_t delete_ire = B_FALSE; 897 ipha_t *ipha = (ipha_t *)pkt->b_rptr; 898 899 /* 900 * If this IRE shouldn't be kept in the table (because its 901 * source address is unspecified), hold a reference to it so 902 * we can delete it even after e.g. ip_wput_ire() has dropped 903 * its reference. 904 */ 905 if (!(ire->ire_marks & IRE_MARK_NOADD) && 906 ire->ire_src_addr == INADDR_ANY) { 907 delete_ire = B_TRUE; 908 IRE_REFHOLD(ire); 909 } 910 911 /* 912 * If we were resolving a router we can not use the 913 * routers IRE for sending the packet (since it would 914 * violate the uniqness of the IP idents) thus we 915 * make another pass through ip_wput to create the IRE_CACHE 916 * for the destination. 917 * When IRE_MARK_NOADD is set, ire_add() is not called. 918 * Thus ip_wput() will never find a ire and result in an 919 * infinite loop. Thus we check whether IRE_MARK_NOADD is 920 * is set. This also implies that IRE_MARK_NOADD can only be 921 * used to send packets to directly connected hosts. 922 */ 923 if (ipha->ipha_dst != ire->ire_addr && 924 !(ire->ire_marks & IRE_MARK_NOADD)) { 925 ire_refrele(ire); /* Held in ire_add */ 926 if (CONN_Q(q)) { 927 (void) ip_output(Q_TO_CONN(q), ipsec_mp, q, 928 IRE_SEND); 929 } else { 930 (void) ip_output((void *)(uintptr_t)zoneid, 931 ipsec_mp, q, IRE_SEND); 932 } 933 } else { 934 if (is_secure) { 935 ipsec_out_t *oi; 936 ipha_t *ipha; 937 938 oi = (ipsec_out_t *)ipsec_mp->b_rptr; 939 ipha = (ipha_t *)ipsec_mp->b_cont->b_rptr; 940 if (oi->ipsec_out_proc_begin) { 941 /* 942 * This is the case where 943 * ip_wput_ipsec_out could not find 944 * the IRE and recreated a new one. 945 * As ip_wput_ipsec_out does ire 946 * lookups, ire_refrele for the extra 947 * bump in ire_add. 948 */ 949 ire_refrele(ire); 950 ip_wput_ipsec_out(q, ipsec_mp, ipha, 951 NULL, NULL); 952 } else { 953 /* 954 * IRE_REFRELE will be done in 955 * ip_wput_ire. 956 */ 957 ip_wput_ire(q, ipsec_mp, ire, NULL, 958 IRE_SEND, zoneid); 959 } 960 } else { 961 /* 962 * IRE_REFRELE will be done in ip_wput_ire. 963 */ 964 ip_wput_ire(q, ipsec_mp, ire, NULL, 965 IRE_SEND, zoneid); 966 } 967 } 968 /* 969 * Special code to support sending a single packet with 970 * conn_unspec_src using an IRE which has no source address. 971 * The IRE is deleted here after sending the packet to avoid 972 * having other code trip on it. But before we delete the 973 * ire, somebody could have looked up this ire. 974 * We prevent returning/using this IRE by the upper layers 975 * by making checks to NULL source address in other places 976 * like e.g ip_ire_append, ip_ire_req and ip_bind_connected. 977 * Though this does not completely prevent other threads 978 * from using this ire, this should not cause any problems. 979 */ 980 if (delete_ire) { 981 ip1dbg(("ire_send: delete IRE\n")); 982 ire_delete(ire); 983 ire_refrele(ire); /* Held above */ 984 } 985 } 986 } 987 988 /* 989 * Send a packet using the specified IRE. 990 * If ire_src_addr_v6 is all zero then discard the IRE after 991 * the packet has been sent. 992 */ 993 static void 994 ire_send_v6(queue_t *q, mblk_t *pkt, ire_t *ire) 995 { 996 mblk_t *ipsec_mp; 997 boolean_t secure; 998 uint_t ifindex; 999 zoneid_t zoneid = ire->ire_zoneid; 1000 ip_stack_t *ipst = ire->ire_ipst; 1001 1002 ASSERT(ire->ire_ipversion == IPV6_VERSION); 1003 ASSERT(!(ire->ire_type & IRE_LOCAL)); /* Has different ire_zoneid */ 1004 if (pkt->b_datap->db_type == M_CTL) { 1005 ipsec_out_t *io; 1006 1007 ipsec_mp = pkt; 1008 pkt = pkt->b_cont; 1009 secure = B_TRUE; 1010 io = (ipsec_out_t *)ipsec_mp->b_rptr; 1011 if (io->ipsec_out_type == IPSEC_OUT) 1012 zoneid = io->ipsec_out_zoneid; 1013 } else { 1014 ipsec_mp = pkt; 1015 secure = B_FALSE; 1016 } 1017 1018 /* If the packet originated externally then */ 1019 if (pkt->b_prev) { 1020 ill_t *ill; 1021 /* 1022 * Extract the ifindex from b_prev (set in ip_rput_data_v6). 1023 * Look up interface to see if it still exists (it could have 1024 * been unplumbed by the time the reply came back from the 1025 * resolver). 1026 */ 1027 ifindex = (uint_t)(uintptr_t)pkt->b_prev; 1028 ill = ill_lookup_on_ifindex(ifindex, B_TRUE, 1029 NULL, NULL, NULL, NULL, ipst); 1030 if (ill == NULL) { 1031 pkt->b_prev = NULL; 1032 pkt->b_next = NULL; 1033 freemsg(ipsec_mp); 1034 ire_refrele(ire); /* Held in ire_add */ 1035 return; 1036 } 1037 q = ill->ill_rq; 1038 pkt->b_prev = NULL; 1039 /* 1040 * This packet has not gone through IPSEC processing 1041 * and hence we should not have any IPSEC message 1042 * prepended. 1043 */ 1044 ASSERT(ipsec_mp == pkt); 1045 put(q, pkt); 1046 ill_refrele(ill); 1047 } else if (pkt->b_next) { 1048 /* Packets from multicast router */ 1049 pkt->b_next = NULL; 1050 /* 1051 * We never get the IPSEC_OUT while forwarding the 1052 * packet for multicast router. 1053 */ 1054 ASSERT(ipsec_mp == pkt); 1055 /* 1056 * XXX TODO IPv6. 1057 */ 1058 freemsg(pkt); 1059 #ifdef XXX 1060 ip_rput_forward(ire, (ipha_t *)pkt->b_rptr, pkt, NULL); 1061 #endif 1062 } else { 1063 if (secure) { 1064 ipsec_out_t *oi; 1065 ip6_t *ip6h; 1066 1067 oi = (ipsec_out_t *)ipsec_mp->b_rptr; 1068 ip6h = (ip6_t *)ipsec_mp->b_cont->b_rptr; 1069 if (oi->ipsec_out_proc_begin) { 1070 /* 1071 * This is the case where 1072 * ip_wput_ipsec_out could not find 1073 * the IRE and recreated a new one. 1074 */ 1075 ip_wput_ipsec_out_v6(q, ipsec_mp, ip6h, 1076 NULL, NULL); 1077 } else { 1078 if (CONN_Q(q)) { 1079 (void) ip_output_v6(Q_TO_CONN(q), 1080 ipsec_mp, q, IRE_SEND); 1081 } else { 1082 (void) ip_output_v6( 1083 (void *)(uintptr_t)zoneid, 1084 ipsec_mp, q, IRE_SEND); 1085 } 1086 } 1087 } else { 1088 /* 1089 * Send packets through ip_output_v6 so that any 1090 * ip6_info header can be processed again. 1091 */ 1092 if (CONN_Q(q)) { 1093 (void) ip_output_v6(Q_TO_CONN(q), ipsec_mp, q, 1094 IRE_SEND); 1095 } else { 1096 (void) ip_output_v6((void *)(uintptr_t)zoneid, 1097 ipsec_mp, q, IRE_SEND); 1098 } 1099 } 1100 /* 1101 * Special code to support sending a single packet with 1102 * conn_unspec_src using an IRE which has no source address. 1103 * The IRE is deleted here after sending the packet to avoid 1104 * having other code trip on it. But before we delete the 1105 * ire, somebody could have looked up this ire. 1106 * We prevent returning/using this IRE by the upper layers 1107 * by making checks to NULL source address in other places 1108 * like e.g ip_ire_append_v6, ip_ire_req and 1109 * ip_bind_connected_v6. Though, this does not completely 1110 * prevent other threads from using this ire, this should 1111 * not cause any problems. 1112 */ 1113 if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_src_addr_v6)) { 1114 ip1dbg(("ire_send_v6: delete IRE\n")); 1115 ire_delete(ire); 1116 } 1117 } 1118 ire_refrele(ire); /* Held in ire_add */ 1119 } 1120 1121 /* 1122 * Make sure that IRE bucket does not get too long. 1123 * This can cause lock up because ire_cache_lookup() 1124 * may take "forever" to finish. 1125 * 1126 * We only remove a maximum of cnt IREs each time. This 1127 * should keep the bucket length approximately constant, 1128 * depending on cnt. This should be enough to defend 1129 * against DoS attack based on creating temporary IREs 1130 * (for forwarding and non-TCP traffic). 1131 * 1132 * We also pass in the address of the newly created IRE 1133 * as we do not want to remove this straight after adding 1134 * it. New IREs are normally added at the tail of the 1135 * bucket. This means that we are removing the "oldest" 1136 * temporary IREs added. Only if there are IREs with 1137 * the same ire_addr, do we not add it at the tail. Refer 1138 * to ire_add_v*(). It should be OK for our purpose. 1139 * 1140 * For non-temporary cached IREs, we make sure that they 1141 * have not been used for some time (defined below), they 1142 * are non-local destinations, and there is no one using 1143 * them at the moment (refcnt == 1). 1144 * 1145 * The above means that the IRE bucket length may become 1146 * very long, consisting of mostly non-temporary IREs. 1147 * This can happen when the hash function does a bad job 1148 * so that most TCP connections cluster to a specific bucket. 1149 * This "hopefully" should never happen. It can also 1150 * happen if most TCP connections have very long lives. 1151 * Even with the minimal hash table size of 256, there 1152 * has to be a lot of such connections to make the bucket 1153 * length unreasonably long. This should probably not 1154 * happen either. The third can when this can happen is 1155 * when the machine is under attack, such as SYN flooding. 1156 * TCP should already have the proper mechanism to protect 1157 * that. So we should be safe. 1158 * 1159 * This function is called by ire_add_then_send() after 1160 * a new IRE is added and the packet is sent. 1161 * 1162 * The idle cutoff interval is set to 60s. It can be 1163 * changed using /etc/system. 1164 */ 1165 uint32_t ire_idle_cutoff_interval = 60000; 1166 1167 static void 1168 ire_cache_cleanup(irb_t *irb, uint32_t threshold, ire_t *ref_ire) 1169 { 1170 ire_t *ire; 1171 clock_t cut_off = drv_usectohz(ire_idle_cutoff_interval * 1000); 1172 int cnt = ip_ire_cleanup_cnt; 1173 1174 /* 1175 * Try to remove cnt temporary IREs first. 1176 */ 1177 for (ire = irb->irb_ire; cnt > 0 && ire != NULL; ire = ire->ire_next) { 1178 if (ire == ref_ire) 1179 continue; 1180 if (ire->ire_marks & IRE_MARK_CONDEMNED) 1181 continue; 1182 if (ire->ire_marks & IRE_MARK_TEMPORARY) { 1183 ASSERT(ire->ire_type == IRE_CACHE); 1184 ire_delete(ire); 1185 cnt--; 1186 } 1187 } 1188 if (cnt == 0) 1189 return; 1190 1191 /* 1192 * If we didn't satisfy our removal target from temporary IREs 1193 * we see how many non-temporary IREs are currently in the bucket. 1194 * If this quantity is above the threshold then we see if there are any 1195 * candidates for removal. We are still limited to removing a maximum 1196 * of cnt IREs. 1197 */ 1198 if ((irb->irb_ire_cnt - irb->irb_tmp_ire_cnt) > threshold) { 1199 for (ire = irb->irb_ire; cnt > 0 && ire != NULL; 1200 ire = ire->ire_next) { 1201 if (ire == ref_ire) 1202 continue; 1203 if (ire->ire_type != IRE_CACHE) 1204 continue; 1205 if (ire->ire_marks & IRE_MARK_CONDEMNED) 1206 continue; 1207 if ((ire->ire_refcnt == 1) && 1208 (lbolt - ire->ire_last_used_time > cut_off)) { 1209 ire_delete(ire); 1210 cnt--; 1211 } 1212 } 1213 } 1214 } 1215 1216 /* 1217 * ire_add_then_send is called when a new IRE has been created in order to 1218 * route an outgoing packet. Typically, it is called from ip_wput when 1219 * a response comes back down from a resolver. We add the IRE, and then 1220 * possibly run the packet through ip_wput or ip_rput, as appropriate. 1221 * However, we do not add the newly created IRE in the cache when 1222 * IRE_MARK_NOADD is set in the IRE. IRE_MARK_NOADD is set at 1223 * ip_newroute_ipif(). The ires with IRE_MARK_NOADD are ire_refrele'd by 1224 * ip_wput_ire() and get deleted. 1225 * Multirouting support: the packet is silently discarded when the new IRE 1226 * holds the RTF_MULTIRT flag, but is not the first IRE to be added with the 1227 * RTF_MULTIRT flag for the same destination address. 1228 * In this case, we just want to register this additional ire without 1229 * sending the packet, as it has already been replicated through 1230 * existing multirt routes in ip_wput(). 1231 */ 1232 void 1233 ire_add_then_send(queue_t *q, ire_t *ire, mblk_t *mp) 1234 { 1235 irb_t *irb; 1236 boolean_t drop = B_FALSE; 1237 /* LINTED : set but not used in function */ 1238 boolean_t mctl_present; 1239 mblk_t *first_mp = NULL; 1240 mblk_t *save_mp = NULL; 1241 ire_t *dst_ire; 1242 ipha_t *ipha; 1243 ip6_t *ip6h; 1244 ip_stack_t *ipst = ire->ire_ipst; 1245 int ire_limit; 1246 1247 if (mp != NULL) { 1248 /* 1249 * We first have to retrieve the destination address carried 1250 * by the packet. 1251 * We can't rely on ire as it can be related to a gateway. 1252 * The destination address will help in determining if 1253 * other RTF_MULTIRT ires are already registered. 1254 * 1255 * We first need to know where we are going : v4 or V6. 1256 * the ire version is enough, as there is no risk that 1257 * we resolve an IPv6 address with an IPv4 ire 1258 * or vice versa. 1259 */ 1260 if (ire->ire_ipversion == IPV4_VERSION) { 1261 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 1262 ipha = (ipha_t *)mp->b_rptr; 1263 save_mp = mp; 1264 mp = first_mp; 1265 1266 dst_ire = ire_cache_lookup(ipha->ipha_dst, 1267 ire->ire_zoneid, MBLK_GETLABEL(mp), ipst); 1268 } else { 1269 ASSERT(ire->ire_ipversion == IPV6_VERSION); 1270 /* 1271 * Get a pointer to the beginning of the IPv6 header. 1272 * Ignore leading IPsec control mblks. 1273 */ 1274 first_mp = mp; 1275 if (mp->b_datap->db_type == M_CTL) { 1276 mp = mp->b_cont; 1277 } 1278 ip6h = (ip6_t *)mp->b_rptr; 1279 save_mp = mp; 1280 mp = first_mp; 1281 dst_ire = ire_cache_lookup_v6(&ip6h->ip6_dst, 1282 ire->ire_zoneid, MBLK_GETLABEL(mp), ipst); 1283 } 1284 if (dst_ire != NULL) { 1285 if (dst_ire->ire_flags & RTF_MULTIRT) { 1286 /* 1287 * At least one resolved multirt route 1288 * already exists for the destination, 1289 * don't sent this packet: either drop it 1290 * or complete the pending resolution, 1291 * depending on the ire. 1292 */ 1293 drop = B_TRUE; 1294 } 1295 ip1dbg(("ire_add_then_send: dst_ire %p " 1296 "[dst %08x, gw %08x], drop %d\n", 1297 (void *)dst_ire, 1298 (dst_ire->ire_ipversion == IPV4_VERSION) ? \ 1299 ntohl(dst_ire->ire_addr) : \ 1300 ntohl(V4_PART_OF_V6(dst_ire->ire_addr_v6)), 1301 (dst_ire->ire_ipversion == IPV4_VERSION) ? \ 1302 ntohl(dst_ire->ire_gateway_addr) : \ 1303 ntohl(V4_PART_OF_V6( 1304 dst_ire->ire_gateway_addr_v6)), 1305 drop)); 1306 ire_refrele(dst_ire); 1307 } 1308 } 1309 1310 if (!(ire->ire_marks & IRE_MARK_NOADD)) { 1311 /* Regular packets with cache bound ires are here. */ 1312 (void) ire_add(&ire, NULL, NULL, NULL, B_FALSE); 1313 1314 if (ire == NULL) { 1315 mp->b_prev = NULL; 1316 mp->b_next = NULL; 1317 MULTIRT_DEBUG_UNTAG(mp); 1318 freemsg(mp); 1319 return; 1320 } 1321 if (mp == NULL) { 1322 ire_refrele(ire); /* Held in ire_add_v4/v6 */ 1323 return; 1324 } 1325 } 1326 if (drop) { 1327 /* 1328 * If we're adding an RTF_MULTIRT ire, the resolution 1329 * is over: we just drop the packet. 1330 */ 1331 if (ire->ire_flags & RTF_MULTIRT) { 1332 if (save_mp) { 1333 save_mp->b_prev = NULL; 1334 save_mp->b_next = NULL; 1335 } 1336 MULTIRT_DEBUG_UNTAG(mp); 1337 freemsg(mp); 1338 } else { 1339 /* 1340 * Otherwise, we're adding the ire to a gateway 1341 * for a multirt route. 1342 * Invoke ip_newroute() to complete the resolution 1343 * of the route. We will then come back here and 1344 * finally drop this packet in the above code. 1345 */ 1346 if (ire->ire_ipversion == IPV4_VERSION) { 1347 /* 1348 * TODO: in order for CGTP to work in non-global 1349 * zones, ip_newroute() must create the IRE 1350 * cache in the zone indicated by 1351 * ire->ire_zoneid. 1352 */ 1353 ip_newroute(q, mp, ipha->ipha_dst, 1354 (CONN_Q(q) ? Q_TO_CONN(q) : NULL), 1355 ire->ire_zoneid, ipst); 1356 } else { 1357 ASSERT(ire->ire_ipversion == IPV6_VERSION); 1358 ip_newroute_v6(q, mp, &ip6h->ip6_dst, NULL, 1359 NULL, ire->ire_zoneid, ipst); 1360 } 1361 } 1362 1363 ire_refrele(ire); /* As done by ire_send(). */ 1364 return; 1365 } 1366 /* 1367 * Need to remember ire_bucket here as ire_send*() may delete 1368 * the ire so we cannot reference it after that. 1369 */ 1370 irb = ire->ire_bucket; 1371 if (ire->ire_ipversion == IPV4_VERSION) { 1372 ire_send(q, mp, ire); 1373 ire_limit = ip_ire_max_bucket_cnt; 1374 } else { 1375 ire_send_v6(q, mp, ire); 1376 ire_limit = ip6_ire_max_bucket_cnt; 1377 } 1378 1379 /* 1380 * irb is NULL if the IRE was not added to the hash. This happens 1381 * when IRE_MARK_NOADD is set and when IREs are returned from 1382 * ire_update_srcif_v4(). 1383 */ 1384 if (irb != NULL) { 1385 IRB_REFHOLD(irb); 1386 if (irb->irb_ire_cnt > ire_limit) 1387 ire_cache_cleanup(irb, ire_limit, ire); 1388 IRB_REFRELE(irb); 1389 } 1390 } 1391 1392 /* 1393 * Initialize the ire that is specific to IPv4 part and call 1394 * ire_init_common to finish it. 1395 */ 1396 ire_t * 1397 ire_init(ire_t *ire, uchar_t *addr, uchar_t *mask, uchar_t *src_addr, 1398 uchar_t *gateway, uint_t *max_fragp, nce_t *src_nce, queue_t *rfq, 1399 queue_t *stq, ushort_t type, ipif_t *ipif, ipaddr_t cmask, uint32_t phandle, 1400 uint32_t ihandle, uint32_t flags, const iulp_t *ulp_info, tsol_gc_t *gc, 1401 tsol_gcgrp_t *gcgrp, ip_stack_t *ipst) 1402 { 1403 ASSERT(type != IRE_CACHE || stq != NULL); 1404 /* 1405 * Reject IRE security attribute creation/initialization 1406 * if system is not running in Trusted mode. 1407 */ 1408 if ((gc != NULL || gcgrp != NULL) && !is_system_labeled()) 1409 return (NULL); 1410 1411 1412 BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_alloced); 1413 1414 if (addr != NULL) 1415 bcopy(addr, &ire->ire_addr, IP_ADDR_LEN); 1416 if (src_addr != NULL) 1417 bcopy(src_addr, &ire->ire_src_addr, IP_ADDR_LEN); 1418 if (mask != NULL) { 1419 bcopy(mask, &ire->ire_mask, IP_ADDR_LEN); 1420 ire->ire_masklen = ip_mask_to_plen(ire->ire_mask); 1421 } 1422 if (gateway != NULL) { 1423 bcopy(gateway, &ire->ire_gateway_addr, IP_ADDR_LEN); 1424 } 1425 1426 if (type == IRE_CACHE) 1427 ire->ire_cmask = cmask; 1428 1429 /* ire_init_common will free the mblks upon encountering any failure */ 1430 if (!ire_init_common(ire, max_fragp, src_nce, rfq, stq, type, ipif, 1431 phandle, ihandle, flags, IPV4_VERSION, ulp_info, gc, gcgrp, ipst)) 1432 return (NULL); 1433 1434 return (ire); 1435 } 1436 1437 /* 1438 * Similar to ire_create except that it is called only when 1439 * we want to allocate ire as an mblk e.g. we have an external 1440 * resolver ARP. 1441 */ 1442 ire_t * 1443 ire_create_mp(uchar_t *addr, uchar_t *mask, uchar_t *src_addr, uchar_t *gateway, 1444 uint_t max_frag, nce_t *src_nce, queue_t *rfq, queue_t *stq, ushort_t type, 1445 ipif_t *ipif, ipaddr_t cmask, uint32_t phandle, uint32_t ihandle, 1446 uint32_t flags, const iulp_t *ulp_info, tsol_gc_t *gc, tsol_gcgrp_t *gcgrp, 1447 ip_stack_t *ipst) 1448 { 1449 ire_t *ire, *buf; 1450 ire_t *ret_ire; 1451 mblk_t *mp; 1452 size_t bufsize; 1453 frtn_t *frtnp; 1454 ill_t *ill; 1455 1456 bufsize = sizeof (ire_t) + sizeof (frtn_t); 1457 buf = kmem_alloc(bufsize, KM_NOSLEEP); 1458 if (buf == NULL) { 1459 ip1dbg(("ire_create_mp: alloc failed\n")); 1460 return (NULL); 1461 } 1462 frtnp = (frtn_t *)(buf + 1); 1463 frtnp->free_arg = (caddr_t)buf; 1464 frtnp->free_func = ire_freemblk; 1465 1466 /* 1467 * Allocate the new IRE. The ire created will hold a ref on 1468 * an nce_t after ire_nce_init, and this ref must either be 1469 * (a) transferred to the ire_cache entry created when ire_add_v4 1470 * is called after successful arp resolution, or, 1471 * (b) released, when arp resolution fails 1472 * Case (b) is handled in ire_freemblk() which will be called 1473 * when mp is freed as a result of failed arp. 1474 */ 1475 mp = esballoc((unsigned char *)buf, bufsize, BPRI_MED, frtnp); 1476 if (mp == NULL) { 1477 ip1dbg(("ire_create_mp: alloc failed\n")); 1478 kmem_free(buf, bufsize); 1479 return (NULL); 1480 } 1481 ire = (ire_t *)mp->b_rptr; 1482 mp->b_wptr = (uchar_t *)&ire[1]; 1483 1484 /* Start clean. */ 1485 *ire = ire_null; 1486 ire->ire_mp = mp; 1487 mp->b_datap->db_type = IRE_DB_TYPE; 1488 ire->ire_marks |= IRE_MARK_UNCACHED; 1489 1490 ret_ire = ire_init(ire, addr, mask, src_addr, gateway, NULL, src_nce, 1491 rfq, stq, type, ipif, cmask, phandle, ihandle, flags, ulp_info, gc, 1492 gcgrp, ipst); 1493 1494 ill = (ill_t *)(stq->q_ptr); 1495 if (ret_ire == NULL) { 1496 /* ire_freemblk needs these set */ 1497 ire->ire_stq_ifindex = ill->ill_phyint->phyint_ifindex; 1498 ire->ire_stackid = ipst->ips_netstack->netstack_stackid; 1499 ire->ire_ipst = ipst; 1500 freeb(ire->ire_mp); 1501 return (NULL); 1502 } 1503 ret_ire->ire_stq_ifindex = ill->ill_phyint->phyint_ifindex; 1504 ret_ire->ire_stackid = ipst->ips_netstack->netstack_stackid; 1505 ASSERT(ret_ire == ire); 1506 ASSERT(ret_ire->ire_ipst == ipst); 1507 /* 1508 * ire_max_frag is normally zero here and is atomically set 1509 * under the irebucket lock in ire_add_v[46] except for the 1510 * case of IRE_MARK_NOADD. In that event the the ire_max_frag 1511 * is non-zero here. 1512 */ 1513 ire->ire_max_frag = max_frag; 1514 return (ire); 1515 } 1516 1517 /* 1518 * ire_create is called to allocate and initialize a new IRE. 1519 * 1520 * NOTE : This is called as writer sometimes though not required 1521 * by this function. 1522 */ 1523 ire_t * 1524 ire_create(uchar_t *addr, uchar_t *mask, uchar_t *src_addr, uchar_t *gateway, 1525 uint_t *max_fragp, nce_t *src_nce, queue_t *rfq, queue_t *stq, 1526 ushort_t type, ipif_t *ipif, ipaddr_t cmask, uint32_t phandle, 1527 uint32_t ihandle, uint32_t flags, const iulp_t *ulp_info, tsol_gc_t *gc, 1528 tsol_gcgrp_t *gcgrp, ip_stack_t *ipst) 1529 { 1530 ire_t *ire; 1531 ire_t *ret_ire; 1532 1533 ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP); 1534 if (ire == NULL) { 1535 ip1dbg(("ire_create: alloc failed\n")); 1536 return (NULL); 1537 } 1538 *ire = ire_null; 1539 1540 ret_ire = ire_init(ire, addr, mask, src_addr, gateway, max_fragp, 1541 src_nce, rfq, stq, type, ipif, cmask, phandle, ihandle, flags, 1542 ulp_info, gc, gcgrp, ipst); 1543 1544 if (ret_ire == NULL) { 1545 kmem_cache_free(ire_cache, ire); 1546 return (NULL); 1547 } 1548 ASSERT(ret_ire == ire); 1549 return (ire); 1550 } 1551 1552 1553 /* 1554 * Common to IPv4 and IPv6 1555 */ 1556 boolean_t 1557 ire_init_common(ire_t *ire, uint_t *max_fragp, nce_t *src_nce, queue_t *rfq, 1558 queue_t *stq, ushort_t type, ipif_t *ipif, uint32_t phandle, 1559 uint32_t ihandle, uint32_t flags, uchar_t ipversion, const iulp_t *ulp_info, 1560 tsol_gc_t *gc, tsol_gcgrp_t *gcgrp, ip_stack_t *ipst) 1561 { 1562 ire->ire_max_fragp = max_fragp; 1563 ire->ire_frag_flag |= (ipst->ips_ip_path_mtu_discovery) ? IPH_DF : 0; 1564 1565 #ifdef DEBUG 1566 if (ipif != NULL) { 1567 if (ipif->ipif_isv6) 1568 ASSERT(ipversion == IPV6_VERSION); 1569 else 1570 ASSERT(ipversion == IPV4_VERSION); 1571 } 1572 #endif /* DEBUG */ 1573 1574 /* 1575 * Create/initialize IRE security attribute only in Trusted mode; 1576 * if the passed in gc/gcgrp is non-NULL, we expect that the caller 1577 * has held a reference to it and will release it when this routine 1578 * returns a failure, otherwise we own the reference. We do this 1579 * prior to initializing the rest IRE fields. 1580 * 1581 * Don't allocate ire_gw_secattr for the resolver case to prevent 1582 * memory leak (in case of external resolution failure). We'll 1583 * allocate it after a successful external resolution, in ire_add(). 1584 * Note that ire->ire_mp != NULL here means this ire is headed 1585 * to an external resolver. 1586 */ 1587 if (is_system_labeled()) { 1588 if ((type & (IRE_LOCAL | IRE_LOOPBACK | IRE_BROADCAST | 1589 IRE_INTERFACE)) != 0) { 1590 /* release references on behalf of caller */ 1591 if (gc != NULL) 1592 GC_REFRELE(gc); 1593 if (gcgrp != NULL) 1594 GCGRP_REFRELE(gcgrp); 1595 } else if ((ire->ire_mp == NULL) && 1596 tsol_ire_init_gwattr(ire, ipversion, gc, gcgrp) != 0) { 1597 return (B_FALSE); 1598 } 1599 } 1600 1601 ire->ire_stq = stq; 1602 ire->ire_rfq = rfq; 1603 ire->ire_type = type; 1604 ire->ire_flags = RTF_UP | flags; 1605 ire->ire_ident = TICK_TO_MSEC(lbolt); 1606 bcopy(ulp_info, &ire->ire_uinfo, sizeof (iulp_t)); 1607 1608 ire->ire_tire_mark = ire->ire_ob_pkt_count + ire->ire_ib_pkt_count; 1609 ire->ire_last_used_time = lbolt; 1610 ire->ire_create_time = (uint32_t)gethrestime_sec(); 1611 1612 /* 1613 * If this IRE is an IRE_CACHE, inherit the handles from the 1614 * parent IREs. For others in the forwarding table, assign appropriate 1615 * new ones. 1616 * 1617 * The mutex protecting ire_handle is because ire_create is not always 1618 * called as a writer. 1619 */ 1620 if (ire->ire_type & IRE_OFFSUBNET) { 1621 mutex_enter(&ipst->ips_ire_handle_lock); 1622 ire->ire_phandle = (uint32_t)ipst->ips_ire_handle++; 1623 mutex_exit(&ipst->ips_ire_handle_lock); 1624 } else if (ire->ire_type & IRE_INTERFACE) { 1625 mutex_enter(&ipst->ips_ire_handle_lock); 1626 ire->ire_ihandle = (uint32_t)ipst->ips_ire_handle++; 1627 mutex_exit(&ipst->ips_ire_handle_lock); 1628 } else if (ire->ire_type == IRE_CACHE) { 1629 ire->ire_phandle = phandle; 1630 ire->ire_ihandle = ihandle; 1631 } 1632 ire->ire_ipif = ipif; 1633 if (ipif != NULL) { 1634 ire->ire_ipif_seqid = ipif->ipif_seqid; 1635 ire->ire_zoneid = ipif->ipif_zoneid; 1636 } else { 1637 ire->ire_zoneid = GLOBAL_ZONEID; 1638 } 1639 ire->ire_ipversion = ipversion; 1640 mutex_init(&ire->ire_lock, NULL, MUTEX_DEFAULT, NULL); 1641 if (ipversion == IPV4_VERSION) { 1642 /* 1643 * IPv6 initializes the ire_nce in ire_add_v6, which expects 1644 * to find the ire_nce to be null when it is called. 1645 */ 1646 if (ire_nce_init(ire, src_nce) != 0) { 1647 /* some failure occurred. propagate error back */ 1648 return (B_FALSE); 1649 } 1650 } 1651 ire->ire_refcnt = 1; 1652 ire->ire_ipst = ipst; /* No netstack_hold */ 1653 ire->ire_trace_disable = B_FALSE; 1654 1655 return (B_TRUE); 1656 } 1657 1658 /* 1659 * This routine is called repeatedly by ipif_up to create broadcast IREs. 1660 * It is passed a pointer to a slot in an IRE pointer array into which to 1661 * place the pointer to the new IRE, if indeed we create one. If the 1662 * IRE corresponding to the address passed in would be a duplicate of an 1663 * existing one, we don't create the new one. irep is incremented before 1664 * return only if we do create a new IRE. (Always called as writer.) 1665 * 1666 * Note that with the "match_flags" parameter, we can match on either 1667 * a particular logical interface (MATCH_IRE_IPIF) or for all logical 1668 * interfaces for a given physical interface (MATCH_IRE_ILL). Currently, 1669 * we only create broadcast ire's on a per physical interface basis. If 1670 * someone is going to be mucking with logical interfaces, it is important 1671 * to call "ipif_check_bcast_ires()" to make sure that any change to a 1672 * logical interface will not cause critical broadcast IRE's to be deleted. 1673 */ 1674 ire_t ** 1675 ire_check_and_create_bcast(ipif_t *ipif, ipaddr_t addr, ire_t **irep, 1676 int match_flags) 1677 { 1678 ire_t *ire; 1679 uint64_t check_flags = IPIF_DEPRECATED | IPIF_NOLOCAL | IPIF_ANYCAST; 1680 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst; 1681 1682 /* 1683 * No broadcast IREs for the LOOPBACK interface 1684 * or others such as point to point and IPIF_NOXMIT. 1685 */ 1686 if (!(ipif->ipif_flags & IPIF_BROADCAST) || 1687 (ipif->ipif_flags & IPIF_NOXMIT)) 1688 return (irep); 1689 1690 /* If this would be a duplicate, don't bother. */ 1691 if ((ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ipif, 1692 ipif->ipif_zoneid, NULL, match_flags, ipst)) != NULL) { 1693 /* 1694 * We look for non-deprecated (and non-anycast, non-nolocal) 1695 * ipifs as the best choice. ipifs with check_flags matching 1696 * (deprecated, etc) are used only if non-deprecated ipifs 1697 * are not available. if the existing ire's ipif is deprecated 1698 * and the new ipif is non-deprecated, switch to the new ipif 1699 */ 1700 if ((!(ire->ire_ipif->ipif_flags & check_flags)) || 1701 (ipif->ipif_flags & check_flags)) { 1702 ire_refrele(ire); 1703 return (irep); 1704 } 1705 /* 1706 * Bcast ires exist in pairs. Both have to be deleted, 1707 * Since we are exclusive we can make the above assertion. 1708 * The 1st has to be refrele'd since it was ctable_lookup'd. 1709 */ 1710 ASSERT(IAM_WRITER_IPIF(ipif)); 1711 ASSERT(ire->ire_next->ire_addr == ire->ire_addr); 1712 ire_delete(ire->ire_next); 1713 ire_delete(ire); 1714 ire_refrele(ire); 1715 } 1716 1717 irep = ire_create_bcast(ipif, addr, irep); 1718 1719 return (irep); 1720 } 1721 1722 uint_t ip_loopback_mtu = IP_LOOPBACK_MTU; 1723 1724 /* 1725 * This routine is called from ipif_check_bcast_ires and ire_check_bcast. 1726 * It leaves all the verifying and deleting to those routines. So it always 1727 * creates 2 bcast ires and chains them into the ire array passed in. 1728 */ 1729 ire_t ** 1730 ire_create_bcast(ipif_t *ipif, ipaddr_t addr, ire_t **irep) 1731 { 1732 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst; 1733 1734 *irep++ = ire_create( 1735 (uchar_t *)&addr, /* dest addr */ 1736 (uchar_t *)&ip_g_all_ones, /* mask */ 1737 (uchar_t *)&ipif->ipif_src_addr, /* source addr */ 1738 NULL, /* no gateway */ 1739 &ipif->ipif_mtu, /* max frag */ 1740 NULL, /* no src nce */ 1741 ipif->ipif_rq, /* recv-from queue */ 1742 ipif->ipif_wq, /* send-to queue */ 1743 IRE_BROADCAST, 1744 ipif, 1745 0, 1746 0, 1747 0, 1748 0, 1749 &ire_uinfo_null, 1750 NULL, 1751 NULL, 1752 ipst); 1753 1754 *irep++ = ire_create( 1755 (uchar_t *)&addr, /* dest address */ 1756 (uchar_t *)&ip_g_all_ones, /* mask */ 1757 (uchar_t *)&ipif->ipif_src_addr, /* source address */ 1758 NULL, /* no gateway */ 1759 &ip_loopback_mtu, /* max frag size */ 1760 NULL, /* no src_nce */ 1761 ipif->ipif_rq, /* recv-from queue */ 1762 NULL, /* no send-to queue */ 1763 IRE_BROADCAST, /* Needed for fanout in wput */ 1764 ipif, 1765 0, 1766 0, 1767 0, 1768 0, 1769 &ire_uinfo_null, 1770 NULL, 1771 NULL, 1772 ipst); 1773 1774 return (irep); 1775 } 1776 1777 /* 1778 * ire_walk routine to delete or update any IRE_CACHE that might contain 1779 * stale information. 1780 * The flags state which entries to delete or update. 1781 * Garbage collection is done separately using kmem alloc callbacks to 1782 * ip_trash_ire_reclaim. 1783 * Used for both IPv4 and IPv6. However, IPv6 only uses FLUSH_MTU_TIME 1784 * since other stale information is cleaned up using NUD. 1785 */ 1786 void 1787 ire_expire(ire_t *ire, char *arg) 1788 { 1789 ire_expire_arg_t *ieap = (ire_expire_arg_t *)(uintptr_t)arg; 1790 ill_t *stq_ill; 1791 int flush_flags = ieap->iea_flush_flag; 1792 ip_stack_t *ipst = ieap->iea_ipst; 1793 1794 if ((flush_flags & FLUSH_REDIRECT_TIME) && 1795 (ire->ire_flags & RTF_DYNAMIC)) { 1796 /* Make sure we delete the corresponding IRE_CACHE */ 1797 ip1dbg(("ire_expire: all redirects\n")); 1798 ip_rts_rtmsg(RTM_DELETE, ire, 0, ipst); 1799 ire_delete(ire); 1800 atomic_dec_32(&ipst->ips_ip_redirect_cnt); 1801 return; 1802 } 1803 if (ire->ire_type != IRE_CACHE) 1804 return; 1805 1806 if (flush_flags & FLUSH_ARP_TIME) { 1807 /* 1808 * Remove all IRE_CACHE except IPv4 multicast ires. These 1809 * ires will be deleted by ip_trash_ire_reclaim_stack() 1810 * when system runs low in memory. 1811 * Verify that create time is more than ip_ire_arp_interval 1812 * milliseconds ago. 1813 */ 1814 1815 if (!(ire->ire_ipversion == IPV4_VERSION && 1816 CLASSD(ire->ire_addr)) && NCE_EXPIRED(ire->ire_nce, ipst)) { 1817 ire_delete(ire); 1818 return; 1819 } 1820 } 1821 1822 if (ipst->ips_ip_path_mtu_discovery && (flush_flags & FLUSH_MTU_TIME) && 1823 (ire->ire_ipif != NULL)) { 1824 /* Increase pmtu if it is less than the interface mtu */ 1825 mutex_enter(&ire->ire_lock); 1826 /* 1827 * If the ipif is a vni (whose mtu is 0, since it's virtual) 1828 * get the mtu from the sending interfaces' ipif 1829 */ 1830 if (IS_VNI(ire->ire_ipif->ipif_ill)) { 1831 stq_ill = ire->ire_stq->q_ptr; 1832 ire->ire_max_frag = MIN(stq_ill->ill_ipif->ipif_mtu, 1833 IP_MAXPACKET); 1834 } else { 1835 ire->ire_max_frag = MIN(ire->ire_ipif->ipif_mtu, 1836 IP_MAXPACKET); 1837 } 1838 ire->ire_frag_flag |= IPH_DF; 1839 mutex_exit(&ire->ire_lock); 1840 } 1841 } 1842 1843 /* 1844 * Return any local address. We use this to target ourselves 1845 * when the src address was specified as 'default'. 1846 * Preference for IRE_LOCAL entries. 1847 */ 1848 ire_t * 1849 ire_lookup_local(zoneid_t zoneid, ip_stack_t *ipst) 1850 { 1851 ire_t *ire; 1852 irb_t *irb; 1853 ire_t *maybe = NULL; 1854 int i; 1855 1856 for (i = 0; i < ipst->ips_ip_cache_table_size; i++) { 1857 irb = &ipst->ips_ip_cache_table[i]; 1858 if (irb->irb_ire == NULL) 1859 continue; 1860 rw_enter(&irb->irb_lock, RW_READER); 1861 for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) { 1862 if ((ire->ire_marks & IRE_MARK_CONDEMNED) || 1863 (ire->ire_zoneid != zoneid && 1864 ire->ire_zoneid != ALL_ZONES)) 1865 continue; 1866 switch (ire->ire_type) { 1867 case IRE_LOOPBACK: 1868 if (maybe == NULL) { 1869 IRE_REFHOLD(ire); 1870 maybe = ire; 1871 } 1872 break; 1873 case IRE_LOCAL: 1874 if (maybe != NULL) { 1875 ire_refrele(maybe); 1876 } 1877 IRE_REFHOLD(ire); 1878 rw_exit(&irb->irb_lock); 1879 return (ire); 1880 } 1881 } 1882 rw_exit(&irb->irb_lock); 1883 } 1884 return (maybe); 1885 } 1886 1887 /* 1888 * If the specified IRE is associated with a particular ILL, return 1889 * that ILL pointer (May be called as writer.). 1890 * 1891 * NOTE : This is not a generic function that can be used always. 1892 * This function always returns the ill of the outgoing packets 1893 * if this ire is used. 1894 */ 1895 ill_t * 1896 ire_to_ill(const ire_t *ire) 1897 { 1898 ill_t *ill = NULL; 1899 1900 /* 1901 * 1) For an IRE_CACHE, ire_ipif is the one where it obtained 1902 * the source address from. ire_stq is the one where the 1903 * packets will be sent out on. We return that here. 1904 * 1905 * 2) IRE_BROADCAST normally has a loopback and a non-loopback 1906 * copy and they always exist next to each other with loopback 1907 * copy being the first one. If we are called on the non-loopback 1908 * copy, return the one pointed by ire_stq. If it was called on 1909 * a loopback copy, we still return the one pointed by the next 1910 * ire's ire_stq pointer i.e the one pointed by the non-loopback 1911 * copy. We don't want use ire_ipif as it might represent the 1912 * source address (if we borrow source addresses for 1913 * IRE_BROADCASTS in the future). 1914 * However if an interface is currently coming up, the above 1915 * condition may not hold during that period since the ires 1916 * are added one at a time. Thus one of the pair could have been 1917 * added and the other not yet added. 1918 * 3) For many other IREs (e.g., IRE_LOCAL), ire_rfq indicates the ill. 1919 * 4) For all others return the ones pointed by ire_ipif->ipif_ill. 1920 * That handles IRE_LOOPBACK. 1921 */ 1922 1923 if (ire->ire_type == IRE_CACHE) { 1924 ill = (ill_t *)ire->ire_stq->q_ptr; 1925 } else if (ire->ire_type == IRE_BROADCAST) { 1926 if (ire->ire_stq != NULL) { 1927 ill = (ill_t *)ire->ire_stq->q_ptr; 1928 } else { 1929 ire_t *ire_next; 1930 1931 ire_next = ire->ire_next; 1932 if (ire_next != NULL && 1933 ire_next->ire_type == IRE_BROADCAST && 1934 ire_next->ire_addr == ire->ire_addr && 1935 ire_next->ire_ipif == ire->ire_ipif) { 1936 ill = (ill_t *)ire_next->ire_stq->q_ptr; 1937 } 1938 } 1939 } else if (ire->ire_rfq != NULL) { 1940 ill = ire->ire_rfq->q_ptr; 1941 } else if (ire->ire_ipif != NULL) { 1942 ill = ire->ire_ipif->ipif_ill; 1943 } 1944 return (ill); 1945 } 1946 1947 /* Arrange to call the specified function for every IRE in the world. */ 1948 void 1949 ire_walk(pfv_t func, void *arg, ip_stack_t *ipst) 1950 { 1951 ire_walk_ipvers(func, arg, 0, ALL_ZONES, ipst); 1952 } 1953 1954 void 1955 ire_walk_v4(pfv_t func, void *arg, zoneid_t zoneid, ip_stack_t *ipst) 1956 { 1957 ire_walk_ipvers(func, arg, IPV4_VERSION, zoneid, ipst); 1958 } 1959 1960 void 1961 ire_walk_v6(pfv_t func, void *arg, zoneid_t zoneid, ip_stack_t *ipst) 1962 { 1963 ire_walk_ipvers(func, arg, IPV6_VERSION, zoneid, ipst); 1964 } 1965 1966 /* 1967 * Walk a particular version. version == 0 means both v4 and v6. 1968 */ 1969 static void 1970 ire_walk_ipvers(pfv_t func, void *arg, uchar_t vers, zoneid_t zoneid, 1971 ip_stack_t *ipst) 1972 { 1973 if (vers != IPV6_VERSION) { 1974 /* 1975 * ip_forwarding_table variable doesn't matter for IPv4 since 1976 * ire_walk_ill_tables uses ips_ip_ftable for IPv4. 1977 */ 1978 ire_walk_ill_tables(0, 0, func, arg, IP_MASK_TABLE_SIZE, 1979 0, NULL, 1980 ipst->ips_ip_cache_table_size, ipst->ips_ip_cache_table, 1981 NULL, zoneid, ipst); 1982 } 1983 if (vers != IPV4_VERSION) { 1984 ire_walk_ill_tables(0, 0, func, arg, IP6_MASK_TABLE_SIZE, 1985 ipst->ips_ip6_ftable_hash_size, 1986 ipst->ips_ip_forwarding_table_v6, 1987 ipst->ips_ip6_cache_table_size, 1988 ipst->ips_ip_cache_table_v6, NULL, zoneid, ipst); 1989 } 1990 } 1991 1992 /* 1993 * Arrange to call the specified function for every IRE that matches the ill. 1994 */ 1995 void 1996 ire_walk_ill(uint_t match_flags, uint_t ire_type, pfv_t func, void *arg, 1997 ill_t *ill) 1998 { 1999 uchar_t vers = (ill->ill_isv6 ? IPV6_VERSION : IPV4_VERSION); 2000 2001 ire_walk_ill_ipvers(match_flags, ire_type, func, arg, vers, ill); 2002 } 2003 2004 void 2005 ire_walk_ill_v4(uint_t match_flags, uint_t ire_type, pfv_t func, void *arg, 2006 ill_t *ill) 2007 { 2008 ire_walk_ill_ipvers(match_flags, ire_type, func, arg, IPV4_VERSION, 2009 ill); 2010 } 2011 2012 void 2013 ire_walk_ill_v6(uint_t match_flags, uint_t ire_type, pfv_t func, void *arg, 2014 ill_t *ill) 2015 { 2016 ire_walk_ill_ipvers(match_flags, ire_type, func, arg, IPV6_VERSION, 2017 ill); 2018 } 2019 2020 /* 2021 * Walk a particular ill and version. 2022 */ 2023 static void 2024 ire_walk_ill_ipvers(uint_t match_flags, uint_t ire_type, pfv_t func, 2025 void *arg, uchar_t vers, ill_t *ill) 2026 { 2027 ip_stack_t *ipst = ill->ill_ipst; 2028 2029 if (vers == IPV4_VERSION) { 2030 ire_walk_ill_tables(match_flags, ire_type, func, arg, 2031 IP_MASK_TABLE_SIZE, 0, 2032 NULL, ipst->ips_ip_cache_table_size, 2033 ipst->ips_ip_cache_table, ill, ALL_ZONES, ipst); 2034 } else if (vers == IPV6_VERSION) { 2035 ire_walk_ill_tables(match_flags, ire_type, func, arg, 2036 IP6_MASK_TABLE_SIZE, ipst->ips_ip6_ftable_hash_size, 2037 ipst->ips_ip_forwarding_table_v6, 2038 ipst->ips_ip6_cache_table_size, 2039 ipst->ips_ip_cache_table_v6, ill, ALL_ZONES, ipst); 2040 } 2041 } 2042 2043 boolean_t 2044 ire_walk_ill_match(uint_t match_flags, uint_t ire_type, ire_t *ire, 2045 ill_t *ill, zoneid_t zoneid, ip_stack_t *ipst) 2046 { 2047 ill_t *ire_stq_ill = NULL; 2048 ill_t *ire_ipif_ill = NULL; 2049 ill_group_t *ire_ill_group = NULL; 2050 2051 ASSERT(match_flags != 0 || zoneid != ALL_ZONES); 2052 /* 2053 * MATCH_IRE_ILL/MATCH_IRE_ILL_GROUP : We match both on ill 2054 * pointed by ire_stq and ire_ipif. Only in the case of 2055 * IRE_CACHEs can ire_stq and ire_ipif be pointing to 2056 * different ills. But we want to keep this function generic 2057 * enough for future use. So, we always try to match on both. 2058 * The only caller of this function ire_walk_ill_tables, will 2059 * call "func" after we return from this function. We expect 2060 * "func" to do the right filtering of ires in this case. 2061 * 2062 * NOTE : In the case of MATCH_IRE_ILL_GROUP, groups 2063 * pointed by ire_stq and ire_ipif should always be the same. 2064 * So, we just match on only one of them. 2065 */ 2066 if (match_flags & (MATCH_IRE_ILL|MATCH_IRE_ILL_GROUP)) { 2067 if (ire->ire_stq != NULL) 2068 ire_stq_ill = (ill_t *)ire->ire_stq->q_ptr; 2069 if (ire->ire_ipif != NULL) 2070 ire_ipif_ill = ire->ire_ipif->ipif_ill; 2071 if (ire_stq_ill != NULL) 2072 ire_ill_group = ire_stq_ill->ill_group; 2073 if ((ire_ill_group == NULL) && (ire_ipif_ill != NULL)) 2074 ire_ill_group = ire_ipif_ill->ill_group; 2075 } 2076 2077 if (zoneid != ALL_ZONES) { 2078 /* 2079 * We're walking the IREs for a specific zone. The only relevant 2080 * IREs are: 2081 * - all IREs with a matching ire_zoneid 2082 * - all IRE_OFFSUBNETs as they're shared across all zones 2083 * - IRE_INTERFACE IREs for interfaces with a usable source addr 2084 * with a matching zone 2085 * - IRE_DEFAULTs with a gateway reachable from the zone 2086 * We should really match on IRE_OFFSUBNETs and IRE_DEFAULTs 2087 * using the same rule; but the above rules are consistent with 2088 * the behavior of ire_ftable_lookup[_v6]() so that all the 2089 * routes that can be matched during lookup are also matched 2090 * here. 2091 */ 2092 if (zoneid != ire->ire_zoneid && ire->ire_zoneid != ALL_ZONES) { 2093 /* 2094 * Note, IRE_INTERFACE can have the stq as NULL. For 2095 * example, if the default multicast route is tied to 2096 * the loopback address. 2097 */ 2098 if ((ire->ire_type & IRE_INTERFACE) && 2099 (ire->ire_stq != NULL)) { 2100 ire_stq_ill = (ill_t *)ire->ire_stq->q_ptr; 2101 if (ire->ire_ipversion == IPV4_VERSION) { 2102 if (!ipif_usesrc_avail(ire_stq_ill, 2103 zoneid)) 2104 /* No usable src addr in zone */ 2105 return (B_FALSE); 2106 } else if (ire_stq_ill->ill_usesrc_ifindex 2107 != 0) { 2108 /* 2109 * For IPv6 use ipif_select_source_v6() 2110 * so the right scope selection is done 2111 */ 2112 ipif_t *src_ipif; 2113 src_ipif = 2114 ipif_select_source_v6(ire_stq_ill, 2115 &ire->ire_addr_v6, RESTRICT_TO_NONE, 2116 IPV6_PREFER_SRC_DEFAULT, 2117 zoneid); 2118 if (src_ipif != NULL) { 2119 ipif_refrele(src_ipif); 2120 } else { 2121 return (B_FALSE); 2122 } 2123 } else { 2124 return (B_FALSE); 2125 } 2126 2127 } else if (!(ire->ire_type & IRE_OFFSUBNET)) { 2128 return (B_FALSE); 2129 } 2130 } 2131 2132 /* 2133 * Match all default routes from the global zone, irrespective 2134 * of reachability. For a non-global zone only match those 2135 * where ire_gateway_addr has a IRE_INTERFACE for the zoneid. 2136 */ 2137 if (ire->ire_type == IRE_DEFAULT && zoneid != GLOBAL_ZONEID) { 2138 int ire_match_flags = 0; 2139 in6_addr_t gw_addr_v6; 2140 ire_t *rire; 2141 2142 ire_match_flags |= MATCH_IRE_TYPE; 2143 if (ire->ire_ipif != NULL) { 2144 ire_match_flags |= MATCH_IRE_ILL_GROUP; 2145 } 2146 if (ire->ire_ipversion == IPV4_VERSION) { 2147 rire = ire_route_lookup(ire->ire_gateway_addr, 2148 0, 0, IRE_INTERFACE, ire->ire_ipif, NULL, 2149 zoneid, NULL, ire_match_flags, ipst); 2150 } else { 2151 ASSERT(ire->ire_ipversion == IPV6_VERSION); 2152 mutex_enter(&ire->ire_lock); 2153 gw_addr_v6 = ire->ire_gateway_addr_v6; 2154 mutex_exit(&ire->ire_lock); 2155 rire = ire_route_lookup_v6(&gw_addr_v6, 2156 NULL, NULL, IRE_INTERFACE, ire->ire_ipif, 2157 NULL, zoneid, NULL, ire_match_flags, ipst); 2158 } 2159 if (rire == NULL) { 2160 return (B_FALSE); 2161 } 2162 ire_refrele(rire); 2163 } 2164 } 2165 2166 if (((!(match_flags & MATCH_IRE_TYPE)) || 2167 (ire->ire_type & ire_type)) && 2168 ((!(match_flags & MATCH_IRE_ILL)) || 2169 (ire_stq_ill == ill || ire_ipif_ill == ill)) && 2170 ((!(match_flags & MATCH_IRE_ILL_GROUP)) || 2171 (ire_stq_ill == ill) || (ire_ipif_ill == ill) || 2172 (ire_ill_group != NULL && 2173 ire_ill_group == ill->ill_group))) { 2174 return (B_TRUE); 2175 } 2176 return (B_FALSE); 2177 } 2178 2179 int 2180 rtfunc(struct radix_node *rn, void *arg) 2181 { 2182 struct rtfuncarg *rtf = arg; 2183 struct rt_entry *rt; 2184 irb_t *irb; 2185 ire_t *ire; 2186 boolean_t ret; 2187 2188 rt = (struct rt_entry *)rn; 2189 ASSERT(rt != NULL); 2190 irb = &rt->rt_irb; 2191 for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) { 2192 if ((rtf->rt_match_flags != 0) || 2193 (rtf->rt_zoneid != ALL_ZONES)) { 2194 ret = ire_walk_ill_match(rtf->rt_match_flags, 2195 rtf->rt_ire_type, ire, 2196 rtf->rt_ill, rtf->rt_zoneid, rtf->rt_ipst); 2197 } else 2198 ret = B_TRUE; 2199 if (ret) 2200 (*rtf->rt_func)(ire, rtf->rt_arg); 2201 } 2202 return (0); 2203 } 2204 2205 /* 2206 * Walk the ftable and the ctable entries that match the ill. 2207 */ 2208 void 2209 ire_walk_ill_tables(uint_t match_flags, uint_t ire_type, pfv_t func, 2210 void *arg, size_t ftbl_sz, size_t htbl_sz, irb_t **ipftbl, 2211 size_t ctbl_sz, irb_t *ipctbl, ill_t *ill, zoneid_t zoneid, 2212 ip_stack_t *ipst) 2213 { 2214 irb_t *irb_ptr; 2215 irb_t *irb; 2216 ire_t *ire; 2217 int i, j; 2218 boolean_t ret; 2219 struct rtfuncarg rtfarg; 2220 2221 ASSERT((!(match_flags & (MATCH_IRE_ILL | 2222 MATCH_IRE_ILL_GROUP))) || (ill != NULL)); 2223 ASSERT(!(match_flags & MATCH_IRE_TYPE) || (ire_type != 0)); 2224 /* 2225 * Optimize by not looking at the forwarding table if there 2226 * is a MATCH_IRE_TYPE specified with no IRE_FORWARDTABLE 2227 * specified in ire_type. 2228 */ 2229 if (!(match_flags & MATCH_IRE_TYPE) || 2230 ((ire_type & IRE_FORWARDTABLE) != 0)) { 2231 /* knobs such that routine is called only for v6 case */ 2232 if (ipftbl == ipst->ips_ip_forwarding_table_v6) { 2233 for (i = (ftbl_sz - 1); i >= 0; i--) { 2234 if ((irb_ptr = ipftbl[i]) == NULL) 2235 continue; 2236 for (j = 0; j < htbl_sz; j++) { 2237 irb = &irb_ptr[j]; 2238 if (irb->irb_ire == NULL) 2239 continue; 2240 2241 IRB_REFHOLD(irb); 2242 for (ire = irb->irb_ire; ire != NULL; 2243 ire = ire->ire_next) { 2244 if (match_flags == 0 && 2245 zoneid == ALL_ZONES) { 2246 ret = B_TRUE; 2247 } else { 2248 ret = 2249 ire_walk_ill_match( 2250 match_flags, 2251 ire_type, ire, ill, 2252 zoneid, ipst); 2253 } 2254 if (ret) 2255 (*func)(ire, arg); 2256 } 2257 IRB_REFRELE(irb); 2258 } 2259 } 2260 } else { 2261 (void) memset(&rtfarg, 0, sizeof (rtfarg)); 2262 rtfarg.rt_func = func; 2263 rtfarg.rt_arg = arg; 2264 if (match_flags != 0) { 2265 rtfarg.rt_match_flags = match_flags; 2266 } 2267 rtfarg.rt_ire_type = ire_type; 2268 rtfarg.rt_ill = ill; 2269 rtfarg.rt_zoneid = zoneid; 2270 rtfarg.rt_ipst = ipst; /* No netstack_hold */ 2271 (void) ipst->ips_ip_ftable->rnh_walktree_mt( 2272 ipst->ips_ip_ftable, 2273 rtfunc, &rtfarg, irb_refhold_rn, irb_refrele_rn); 2274 } 2275 } 2276 2277 /* 2278 * Optimize by not looking at the cache table if there 2279 * is a MATCH_IRE_TYPE specified with no IRE_CACHETABLE 2280 * specified in ire_type. 2281 */ 2282 if (!(match_flags & MATCH_IRE_TYPE) || 2283 ((ire_type & IRE_CACHETABLE) != 0)) { 2284 for (i = 0; i < ctbl_sz; i++) { 2285 irb = &ipctbl[i]; 2286 if (irb->irb_ire == NULL) 2287 continue; 2288 IRB_REFHOLD(irb); 2289 for (ire = irb->irb_ire; ire != NULL; 2290 ire = ire->ire_next) { 2291 if (match_flags == 0 && zoneid == ALL_ZONES) { 2292 ret = B_TRUE; 2293 } else { 2294 ret = ire_walk_ill_match( 2295 match_flags, ire_type, 2296 ire, ill, zoneid, ipst); 2297 } 2298 if (ret) 2299 (*func)(ire, arg); 2300 } 2301 IRB_REFRELE(irb); 2302 } 2303 } 2304 } 2305 2306 /* 2307 * This function takes a mask and returns 2308 * number of bits set in the mask. If no 2309 * bit is set it returns 0. 2310 * Assumes a contiguous mask. 2311 */ 2312 int 2313 ip_mask_to_plen(ipaddr_t mask) 2314 { 2315 return (mask == 0 ? 0 : IP_ABITS - (ffs(ntohl(mask)) -1)); 2316 } 2317 2318 /* 2319 * Convert length for a mask to the mask. 2320 */ 2321 ipaddr_t 2322 ip_plen_to_mask(uint_t masklen) 2323 { 2324 return (htonl(IP_HOST_MASK << (IP_ABITS - masklen))); 2325 } 2326 2327 void 2328 ire_atomic_end(irb_t *irb_ptr, ire_t *ire) 2329 { 2330 ill_t *ill_list[NUM_ILLS]; 2331 ip_stack_t *ipst = ire->ire_ipst; 2332 2333 ill_list[0] = ire->ire_stq != NULL ? ire->ire_stq->q_ptr : NULL; 2334 ill_list[1] = ire->ire_ipif != NULL ? ire->ire_ipif->ipif_ill : NULL; 2335 ill_unlock_ills(ill_list, NUM_ILLS); 2336 rw_exit(&irb_ptr->irb_lock); 2337 rw_exit(&ipst->ips_ill_g_usesrc_lock); 2338 } 2339 2340 /* 2341 * ire_add_v[46] atomically make sure that the ipif or ill associated 2342 * with the new ire being added is stable and not IPIF_CHANGING or ILL_CHANGING 2343 * before adding the ire to the table. This ensures that we don't create 2344 * new IRE_CACHEs with stale values for parameters that are passed to 2345 * ire_create such as ire_max_frag. Note that ire_create() is passed a pointer 2346 * to the ipif_mtu, and not the value. The actual value is derived from the 2347 * parent ire or ipif under the bucket lock. 2348 */ 2349 int 2350 ire_atomic_start(irb_t *irb_ptr, ire_t *ire, queue_t *q, mblk_t *mp, 2351 ipsq_func_t func) 2352 { 2353 ill_t *stq_ill; 2354 ill_t *ipif_ill; 2355 ill_t *ill_list[NUM_ILLS]; 2356 int cnt = NUM_ILLS; 2357 int error = 0; 2358 ill_t *ill = NULL; 2359 ip_stack_t *ipst = ire->ire_ipst; 2360 2361 ill_list[0] = stq_ill = ire->ire_stq != 2362 NULL ? ire->ire_stq->q_ptr : NULL; 2363 ill_list[1] = ipif_ill = ire->ire_ipif != 2364 NULL ? ire->ire_ipif->ipif_ill : NULL; 2365 2366 ASSERT((q != NULL && mp != NULL && func != NULL) || 2367 (q == NULL && mp == NULL && func == NULL)); 2368 rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER); 2369 GRAB_CONN_LOCK(q); 2370 rw_enter(&irb_ptr->irb_lock, RW_WRITER); 2371 ill_lock_ills(ill_list, cnt); 2372 2373 /* 2374 * While the IRE is in the process of being added, a user may have 2375 * invoked the ifconfig usesrc option on the stq_ill to make it a 2376 * usesrc client ILL. Check for this possibility here, if it is true 2377 * then we fail adding the IRE_CACHE. Another check is to make sure 2378 * that an ipif_ill of an IRE_CACHE being added is not part of a usesrc 2379 * group. The ill_g_usesrc_lock is released in ire_atomic_end 2380 */ 2381 if ((ire->ire_type & IRE_CACHE) && 2382 (ire->ire_marks & IRE_MARK_USESRC_CHECK)) { 2383 if (stq_ill->ill_usesrc_ifindex != 0) { 2384 ASSERT(stq_ill->ill_usesrc_grp_next != NULL); 2385 if ((ipif_ill->ill_phyint->phyint_ifindex != 2386 stq_ill->ill_usesrc_ifindex) || 2387 (ipif_ill->ill_usesrc_grp_next == NULL) || 2388 (ipif_ill->ill_usesrc_ifindex != 0)) { 2389 error = EINVAL; 2390 goto done; 2391 } 2392 } else if (ipif_ill->ill_usesrc_grp_next != NULL) { 2393 error = EINVAL; 2394 goto done; 2395 } 2396 } 2397 2398 /* 2399 * IPMP flag settings happen without taking the exclusive route 2400 * in ip_sioctl_flags. So we need to make an atomic check here 2401 * for FAILED/OFFLINE/INACTIVE flags or if it has hit the 2402 * FAILBACK=no case. 2403 */ 2404 if ((stq_ill != NULL) && !IAM_WRITER_ILL(stq_ill)) { 2405 if (stq_ill->ill_state_flags & ILL_CHANGING) { 2406 ill = stq_ill; 2407 error = EAGAIN; 2408 } else if ((stq_ill->ill_phyint->phyint_flags & PHYI_OFFLINE) || 2409 (ill_is_probeonly(stq_ill) && 2410 !(ire->ire_marks & IRE_MARK_HIDDEN))) { 2411 error = EINVAL; 2412 } 2413 goto done; 2414 } 2415 2416 /* 2417 * We don't check for OFFLINE/FAILED in this case because 2418 * the source address selection logic (ipif_select_source) 2419 * may still select a source address from such an ill. The 2420 * assumption is that these addresses will be moved by in.mpathd 2421 * soon. (i.e. this is a race). However link local addresses 2422 * will not move and hence ipif_select_source_v6 tries to avoid 2423 * FAILED ills. Please see ipif_select_source_v6 for more info 2424 */ 2425 if ((ipif_ill != NULL) && !IAM_WRITER_ILL(ipif_ill) && 2426 (ipif_ill->ill_state_flags & ILL_CHANGING)) { 2427 ill = ipif_ill; 2428 error = EAGAIN; 2429 goto done; 2430 } 2431 2432 if ((ire->ire_ipif != NULL) && !IAM_WRITER_IPIF(ire->ire_ipif) && 2433 (ire->ire_ipif->ipif_state_flags & IPIF_CHANGING)) { 2434 ill = ire->ire_ipif->ipif_ill; 2435 ASSERT(ill != NULL); 2436 error = EAGAIN; 2437 goto done; 2438 } 2439 2440 done: 2441 if (error == EAGAIN && ILL_CAN_WAIT(ill, q)) { 2442 ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq; 2443 mutex_enter(&ipsq->ipsq_lock); 2444 ire_atomic_end(irb_ptr, ire); 2445 ipsq_enq(ipsq, q, mp, func, NEW_OP, ill); 2446 mutex_exit(&ipsq->ipsq_lock); 2447 error = EINPROGRESS; 2448 } else if (error != 0) { 2449 ire_atomic_end(irb_ptr, ire); 2450 } 2451 2452 RELEASE_CONN_LOCK(q); 2453 return (error); 2454 } 2455 2456 /* 2457 * Add a fully initialized IRE to an appropriate table based on 2458 * ire_type. 2459 * 2460 * allow_unresolved == B_FALSE indicates a legacy code-path call 2461 * that has prohibited the addition of incomplete ire's. If this 2462 * parameter is set, and we find an nce that is in a state other 2463 * than ND_REACHABLE, we fail the add. Note that nce_state could be 2464 * something other than ND_REACHABLE if the nce had just expired and 2465 * the ire_create preceding the ire_add added a new ND_INITIAL nce. 2466 */ 2467 int 2468 ire_add(ire_t **irep, queue_t *q, mblk_t *mp, ipsq_func_t func, 2469 boolean_t allow_unresolved) 2470 { 2471 ire_t *ire1; 2472 ill_t *stq_ill = NULL; 2473 ill_t *ill; 2474 ipif_t *ipif = NULL; 2475 ill_walk_context_t ctx; 2476 ire_t *ire = *irep; 2477 int error; 2478 boolean_t ire_is_mblk = B_FALSE; 2479 tsol_gcgrp_t *gcgrp = NULL; 2480 tsol_gcgrp_addr_t ga; 2481 ip_stack_t *ipst = ire->ire_ipst; 2482 2483 /* get ready for the day when original ire is not created as mblk */ 2484 if (ire->ire_mp != NULL) { 2485 ire_is_mblk = B_TRUE; 2486 /* Copy the ire to a kmem_alloc'ed area */ 2487 ire1 = kmem_cache_alloc(ire_cache, KM_NOSLEEP); 2488 if (ire1 == NULL) { 2489 ip1dbg(("ire_add: alloc failed\n")); 2490 ire_delete(ire); 2491 *irep = NULL; 2492 return (ENOMEM); 2493 } 2494 ire->ire_marks &= ~IRE_MARK_UNCACHED; 2495 *ire1 = *ire; 2496 ire1->ire_mp = NULL; 2497 ire1->ire_stq_ifindex = 0; 2498 freeb(ire->ire_mp); 2499 ire = ire1; 2500 } 2501 if (ire->ire_stq != NULL) 2502 stq_ill = (ill_t *)ire->ire_stq->q_ptr; 2503 2504 if (ire->ire_type == IRE_CACHE) { 2505 /* 2506 * If this interface is FAILED, or INACTIVE or has hit 2507 * the FAILBACK=no case, we create IRE_CACHES marked 2508 * HIDDEN for some special cases e.g. bind to 2509 * IPIF_NOFAILOVER address etc. So, if this interface 2510 * is FAILED/INACTIVE/hit FAILBACK=no case, and we are 2511 * not creating hidden ires, we should not allow that. 2512 * This happens because the state of the interface 2513 * changed while we were waiting in ARP. If this is the 2514 * daemon sending probes, the next probe will create 2515 * HIDDEN ires and we will create an ire then. This 2516 * cannot happen with NDP currently because IRE is 2517 * never queued in NDP. But it can happen in the 2518 * future when we have external resolvers with IPv6. 2519 * If the interface gets marked with OFFLINE while we 2520 * are waiting in ARP, don't add the ire. 2521 */ 2522 if ((stq_ill->ill_phyint->phyint_flags & PHYI_OFFLINE) || 2523 (ill_is_probeonly(stq_ill) && 2524 !(ire->ire_marks & IRE_MARK_HIDDEN))) { 2525 /* 2526 * We don't know whether it is a valid ipif or not. 2527 * unless we do the check below. So, set it to NULL. 2528 */ 2529 ire->ire_ipif = NULL; 2530 ire_delete(ire); 2531 *irep = NULL; 2532 return (EINVAL); 2533 } 2534 } 2535 2536 if (stq_ill != NULL && ire->ire_type == IRE_CACHE && 2537 stq_ill->ill_net_type == IRE_IF_RESOLVER) { 2538 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 2539 ill = ILL_START_WALK_ALL(&ctx, ipst); 2540 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 2541 mutex_enter(&ill->ill_lock); 2542 if (ill->ill_state_flags & ILL_CONDEMNED) { 2543 mutex_exit(&ill->ill_lock); 2544 continue; 2545 } 2546 /* 2547 * We need to make sure that the ipif is a valid one 2548 * before adding the IRE_CACHE. This happens only 2549 * with IRE_CACHE when there is an external resolver. 2550 * 2551 * We can unplumb a logical interface while the 2552 * packet is waiting in ARP with the IRE. Then, 2553 * later on when we feed the IRE back, the ipif 2554 * has to be re-checked. This can't happen with 2555 * NDP currently, as we never queue the IRE with 2556 * the packet. We always try to recreate the IRE 2557 * when the resolution is completed. But, we do 2558 * it for IPv6 also here so that in future if 2559 * we have external resolvers, it will work without 2560 * any change. 2561 */ 2562 ipif = ipif_lookup_seqid(ill, ire->ire_ipif_seqid); 2563 if (ipif != NULL) { 2564 ipif_refhold_locked(ipif); 2565 mutex_exit(&ill->ill_lock); 2566 break; 2567 } 2568 mutex_exit(&ill->ill_lock); 2569 } 2570 rw_exit(&ipst->ips_ill_g_lock); 2571 if (ipif == NULL || 2572 (ipif->ipif_isv6 && 2573 !IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6, 2574 &ipif->ipif_v6src_addr)) || 2575 (!ipif->ipif_isv6 && 2576 ire->ire_src_addr != ipif->ipif_src_addr) || 2577 ire->ire_zoneid != ipif->ipif_zoneid) { 2578 2579 if (ipif != NULL) 2580 ipif_refrele(ipif); 2581 ire->ire_ipif = NULL; 2582 ire_delete(ire); 2583 *irep = NULL; 2584 return (EINVAL); 2585 } 2586 2587 2588 ASSERT(ill != NULL); 2589 /* 2590 * If this group was dismantled while this packets was 2591 * queued in ARP, don't add it here. 2592 */ 2593 if (ire->ire_ipif->ipif_ill->ill_group != ill->ill_group) { 2594 /* We don't want ire_inactive bump stats for this */ 2595 ipif_refrele(ipif); 2596 ire->ire_ipif = NULL; 2597 ire_delete(ire); 2598 *irep = NULL; 2599 return (EINVAL); 2600 } 2601 2602 /* 2603 * Since we didn't attach label security attributes to the 2604 * ire for the resolver case, we need to add it now. (only 2605 * for v4 resolver and v6 xresolv case). 2606 */ 2607 if (is_system_labeled() && ire_is_mblk) { 2608 if (ire->ire_ipversion == IPV4_VERSION) { 2609 ga.ga_af = AF_INET; 2610 IN6_IPADDR_TO_V4MAPPED(ire->ire_gateway_addr != 2611 INADDR_ANY ? ire->ire_gateway_addr : 2612 ire->ire_addr, &ga.ga_addr); 2613 } else { 2614 ga.ga_af = AF_INET6; 2615 ga.ga_addr = IN6_IS_ADDR_UNSPECIFIED( 2616 &ire->ire_gateway_addr_v6) ? 2617 ire->ire_addr_v6 : 2618 ire->ire_gateway_addr_v6; 2619 } 2620 gcgrp = gcgrp_lookup(&ga, B_FALSE); 2621 error = tsol_ire_init_gwattr(ire, ire->ire_ipversion, 2622 NULL, gcgrp); 2623 if (error != 0) { 2624 if (gcgrp != NULL) { 2625 GCGRP_REFRELE(gcgrp); 2626 gcgrp = NULL; 2627 } 2628 ipif_refrele(ipif); 2629 ire->ire_ipif = NULL; 2630 ire_delete(ire); 2631 *irep = NULL; 2632 return (error); 2633 } 2634 } 2635 } 2636 2637 /* 2638 * In case ire was changed 2639 */ 2640 *irep = ire; 2641 if (ire->ire_ipversion == IPV6_VERSION) 2642 error = ire_add_v6(irep, q, mp, func); 2643 else 2644 error = ire_add_v4(irep, q, mp, func, allow_unresolved); 2645 if (ipif != NULL) 2646 ipif_refrele(ipif); 2647 return (error); 2648 } 2649 2650 /* 2651 * Add an initialized IRE to an appropriate table based on ire_type. 2652 * 2653 * The forward table contains IRE_PREFIX/IRE_HOST and 2654 * IRE_IF_RESOLVER/IRE_IF_NORESOLVER and IRE_DEFAULT. 2655 * 2656 * The cache table contains IRE_BROADCAST/IRE_LOCAL/IRE_LOOPBACK 2657 * and IRE_CACHE. 2658 * 2659 * NOTE : This function is called as writer though not required 2660 * by this function. 2661 */ 2662 static int 2663 ire_add_v4(ire_t **ire_p, queue_t *q, mblk_t *mp, ipsq_func_t func, 2664 boolean_t allow_unresolved) 2665 { 2666 ire_t *ire1; 2667 irb_t *irb_ptr; 2668 ire_t **irep; 2669 int flags; 2670 ire_t *pire = NULL; 2671 ill_t *stq_ill; 2672 ire_t *ire = *ire_p; 2673 int error; 2674 boolean_t need_refrele = B_FALSE; 2675 nce_t *nce; 2676 ip_stack_t *ipst = ire->ire_ipst; 2677 2678 if (ire->ire_ipif != NULL) 2679 ASSERT(!MUTEX_HELD(&ire->ire_ipif->ipif_ill->ill_lock)); 2680 if (ire->ire_stq != NULL) 2681 ASSERT(!MUTEX_HELD( 2682 &((ill_t *)(ire->ire_stq->q_ptr))->ill_lock)); 2683 ASSERT(ire->ire_ipversion == IPV4_VERSION); 2684 ASSERT(ire->ire_mp == NULL); /* Calls should go through ire_add */ 2685 2686 /* Find the appropriate list head. */ 2687 switch (ire->ire_type) { 2688 case IRE_HOST: 2689 ire->ire_mask = IP_HOST_MASK; 2690 ire->ire_masklen = IP_ABITS; 2691 if ((ire->ire_flags & RTF_SETSRC) == 0) 2692 ire->ire_src_addr = 0; 2693 break; 2694 case IRE_CACHE: 2695 case IRE_BROADCAST: 2696 case IRE_LOCAL: 2697 case IRE_LOOPBACK: 2698 ire->ire_mask = IP_HOST_MASK; 2699 ire->ire_masklen = IP_ABITS; 2700 break; 2701 case IRE_PREFIX: 2702 if ((ire->ire_flags & RTF_SETSRC) == 0) 2703 ire->ire_src_addr = 0; 2704 break; 2705 case IRE_DEFAULT: 2706 if ((ire->ire_flags & RTF_SETSRC) == 0) 2707 ire->ire_src_addr = 0; 2708 break; 2709 case IRE_IF_RESOLVER: 2710 case IRE_IF_NORESOLVER: 2711 break; 2712 default: 2713 ip0dbg(("ire_add_v4: ire %p has unrecognized IRE type (%d)\n", 2714 (void *)ire, ire->ire_type)); 2715 ire_delete(ire); 2716 *ire_p = NULL; 2717 return (EINVAL); 2718 } 2719 2720 /* Make sure the address is properly masked. */ 2721 ire->ire_addr &= ire->ire_mask; 2722 2723 /* 2724 * ip_newroute/ip_newroute_multi are unable to prevent the deletion 2725 * of the interface route while adding an IRE_CACHE for an on-link 2726 * destination in the IRE_IF_RESOLVER case, since the ire has to 2727 * go to ARP and return. We can't do a REFHOLD on the 2728 * associated interface ire for fear of ARP freeing the message. 2729 * Here we look up the interface ire in the forwarding table and 2730 * make sure that the interface route has not been deleted. 2731 */ 2732 if (ire->ire_type == IRE_CACHE && ire->ire_gateway_addr == 0 && 2733 ((ill_t *)ire->ire_stq->q_ptr)->ill_net_type == IRE_IF_RESOLVER) { 2734 2735 ASSERT(ire->ire_max_fragp == NULL); 2736 if (CLASSD(ire->ire_addr) && !(ire->ire_flags & RTF_SETSRC)) { 2737 /* 2738 * The ihandle that we used in ip_newroute_multi 2739 * comes from the interface route corresponding 2740 * to ire_ipif. Lookup here to see if it exists 2741 * still. 2742 * If the ire has a source address assigned using 2743 * RTF_SETSRC, ire_ipif is the logical interface holding 2744 * this source address, so we can't use it to check for 2745 * the existence of the interface route. Instead we rely 2746 * on the brute force ihandle search in 2747 * ire_ihandle_lookup_onlink() below. 2748 */ 2749 pire = ipif_to_ire(ire->ire_ipif); 2750 if (pire == NULL) { 2751 ire_delete(ire); 2752 *ire_p = NULL; 2753 return (EINVAL); 2754 } else if (pire->ire_ihandle != ire->ire_ihandle) { 2755 ire_refrele(pire); 2756 ire_delete(ire); 2757 *ire_p = NULL; 2758 return (EINVAL); 2759 } 2760 } else { 2761 pire = ire_ihandle_lookup_onlink(ire); 2762 if (pire == NULL) { 2763 ire_delete(ire); 2764 *ire_p = NULL; 2765 return (EINVAL); 2766 } 2767 } 2768 /* Prevent pire from getting deleted */ 2769 IRB_REFHOLD(pire->ire_bucket); 2770 /* Has it been removed already ? */ 2771 if (pire->ire_marks & IRE_MARK_CONDEMNED) { 2772 IRB_REFRELE(pire->ire_bucket); 2773 ire_refrele(pire); 2774 ire_delete(ire); 2775 *ire_p = NULL; 2776 return (EINVAL); 2777 } 2778 } else { 2779 ASSERT(ire->ire_max_fragp != NULL); 2780 } 2781 flags = (MATCH_IRE_MASK | MATCH_IRE_TYPE | MATCH_IRE_GW); 2782 2783 if (ire->ire_ipif != NULL) { 2784 /* 2785 * We use MATCH_IRE_IPIF while adding IRE_CACHES only 2786 * for historic reasons and to maintain symmetry with 2787 * IPv6 code path. Historically this was used by 2788 * multicast code to create multiple IRE_CACHES on 2789 * a single ill with different ipifs. This was used 2790 * so that multicast packets leaving the node had the 2791 * right source address. This is no longer needed as 2792 * ip_wput initializes the address correctly. 2793 */ 2794 flags |= MATCH_IRE_IPIF; 2795 /* 2796 * If we are creating hidden ires, make sure we search on 2797 * this ill (MATCH_IRE_ILL) and a hidden ire, 2798 * while we are searching for duplicates below. Otherwise we 2799 * could potentially find an IRE on some other interface 2800 * and it may not be a IRE marked with IRE_MARK_HIDDEN. We 2801 * shouldn't do this as this will lead to an infinite loop 2802 * (if we get to ip_wput again) eventually we need an hidden 2803 * ire for this packet to go out. MATCH_IRE_ILL is explicitly 2804 * done below. 2805 */ 2806 if (ire->ire_type == IRE_CACHE && 2807 (ire->ire_marks & IRE_MARK_HIDDEN)) 2808 flags |= (MATCH_IRE_MARK_HIDDEN); 2809 } 2810 if ((ire->ire_type & IRE_CACHETABLE) == 0) { 2811 irb_ptr = ire_get_bucket(ire); 2812 need_refrele = B_TRUE; 2813 if (irb_ptr == NULL) { 2814 /* 2815 * This assumes that the ire has not added 2816 * a reference to the ipif. 2817 */ 2818 ire->ire_ipif = NULL; 2819 ire_delete(ire); 2820 if (pire != NULL) { 2821 IRB_REFRELE(pire->ire_bucket); 2822 ire_refrele(pire); 2823 } 2824 *ire_p = NULL; 2825 return (EINVAL); 2826 } 2827 } else { 2828 irb_ptr = &(ipst->ips_ip_cache_table[IRE_ADDR_HASH( 2829 ire->ire_addr, ipst->ips_ip_cache_table_size)]); 2830 } 2831 2832 /* 2833 * Start the atomic add of the ire. Grab the ill locks, 2834 * ill_g_usesrc_lock and the bucket lock. Check for condemned 2835 * 2836 * If ipif or ill is changing ire_atomic_start() may queue the 2837 * request and return EINPROGRESS. 2838 * To avoid lock order problems, get the ndp4->ndp_g_lock. 2839 */ 2840 mutex_enter(&ipst->ips_ndp4->ndp_g_lock); 2841 error = ire_atomic_start(irb_ptr, ire, q, mp, func); 2842 if (error != 0) { 2843 mutex_exit(&ipst->ips_ndp4->ndp_g_lock); 2844 /* 2845 * We don't know whether it is a valid ipif or not. 2846 * So, set it to NULL. This assumes that the ire has not added 2847 * a reference to the ipif. 2848 */ 2849 ire->ire_ipif = NULL; 2850 ire_delete(ire); 2851 if (pire != NULL) { 2852 IRB_REFRELE(pire->ire_bucket); 2853 ire_refrele(pire); 2854 } 2855 *ire_p = NULL; 2856 if (need_refrele) 2857 IRB_REFRELE(irb_ptr); 2858 return (error); 2859 } 2860 /* 2861 * To avoid creating ires having stale values for the ire_max_frag 2862 * we get the latest value atomically here. For more details 2863 * see the block comment in ip_sioctl_mtu and in DL_NOTE_SDU_CHANGE 2864 * in ip_rput_dlpi_writer 2865 */ 2866 if (ire->ire_max_fragp == NULL) { 2867 if (CLASSD(ire->ire_addr)) 2868 ire->ire_max_frag = ire->ire_ipif->ipif_mtu; 2869 else 2870 ire->ire_max_frag = pire->ire_max_frag; 2871 } else { 2872 uint_t max_frag; 2873 2874 max_frag = *ire->ire_max_fragp; 2875 ire->ire_max_fragp = NULL; 2876 ire->ire_max_frag = max_frag; 2877 } 2878 /* 2879 * Atomically check for duplicate and insert in the table. 2880 */ 2881 for (ire1 = irb_ptr->irb_ire; ire1 != NULL; ire1 = ire1->ire_next) { 2882 if (ire1->ire_marks & IRE_MARK_CONDEMNED) 2883 continue; 2884 if (ire->ire_ipif != NULL) { 2885 /* 2886 * We do MATCH_IRE_ILL implicitly here for IREs 2887 * with a non-null ire_ipif, including IRE_CACHEs. 2888 * As ire_ipif and ire_stq could point to two 2889 * different ills, we can't pass just ire_ipif to 2890 * ire_match_args and get a match on both ills. 2891 * This is just needed for duplicate checks here and 2892 * so we don't add an extra argument to 2893 * ire_match_args for this. Do it locally. 2894 * 2895 * NOTE : Currently there is no part of the code 2896 * that asks for both MATH_IRE_IPIF and MATCH_IRE_ILL 2897 * match for IRE_CACHEs. Thus we don't want to 2898 * extend the arguments to ire_match_args. 2899 */ 2900 if (ire1->ire_stq != ire->ire_stq) 2901 continue; 2902 /* 2903 * Multiroute IRE_CACHEs for a given destination can 2904 * have the same ire_ipif, typically if their source 2905 * address is forced using RTF_SETSRC, and the same 2906 * send-to queue. We differentiate them using the parent 2907 * handle. 2908 */ 2909 if (ire->ire_type == IRE_CACHE && 2910 (ire1->ire_flags & RTF_MULTIRT) && 2911 (ire->ire_flags & RTF_MULTIRT) && 2912 (ire1->ire_phandle != ire->ire_phandle)) 2913 continue; 2914 } 2915 if (ire1->ire_zoneid != ire->ire_zoneid) 2916 continue; 2917 if (ire_match_args(ire1, ire->ire_addr, ire->ire_mask, 2918 ire->ire_gateway_addr, ire->ire_type, ire->ire_ipif, 2919 ire->ire_zoneid, 0, NULL, flags)) { 2920 /* 2921 * Return the old ire after doing a REFHOLD. 2922 * As most of the callers continue to use the IRE 2923 * after adding, we return a held ire. This will 2924 * avoid a lookup in the caller again. If the callers 2925 * don't want to use it, they need to do a REFRELE. 2926 */ 2927 ip1dbg(("found dup ire existing %p new %p", 2928 (void *)ire1, (void *)ire)); 2929 IRE_REFHOLD(ire1); 2930 ire_atomic_end(irb_ptr, ire); 2931 mutex_exit(&ipst->ips_ndp4->ndp_g_lock); 2932 ire_delete(ire); 2933 if (pire != NULL) { 2934 /* 2935 * Assert that it is not removed from the 2936 * list yet. 2937 */ 2938 ASSERT(pire->ire_ptpn != NULL); 2939 IRB_REFRELE(pire->ire_bucket); 2940 ire_refrele(pire); 2941 } 2942 *ire_p = ire1; 2943 if (need_refrele) 2944 IRB_REFRELE(irb_ptr); 2945 return (0); 2946 } 2947 } 2948 if (ire->ire_type & IRE_CACHE) { 2949 ASSERT(ire->ire_stq != NULL); 2950 nce = ndp_lookup_v4(ire_to_ill(ire), 2951 ((ire->ire_gateway_addr != INADDR_ANY) ? 2952 &ire->ire_gateway_addr : &ire->ire_addr), 2953 B_TRUE); 2954 if (nce != NULL) 2955 mutex_enter(&nce->nce_lock); 2956 /* 2957 * if the nce is NCE_F_CONDEMNED, or if it is not ND_REACHABLE 2958 * and the caller has prohibited the addition of incomplete 2959 * ire's, we fail the add. Note that nce_state could be 2960 * something other than ND_REACHABLE if the nce had 2961 * just expired and the ire_create preceding the 2962 * ire_add added a new ND_INITIAL nce. 2963 */ 2964 if ((nce == NULL) || 2965 (nce->nce_flags & NCE_F_CONDEMNED) || 2966 (!allow_unresolved && 2967 (nce->nce_state != ND_REACHABLE))) { 2968 if (nce != NULL) { 2969 DTRACE_PROBE1(ire__bad__nce, nce_t *, nce); 2970 mutex_exit(&nce->nce_lock); 2971 } 2972 ire_atomic_end(irb_ptr, ire); 2973 mutex_exit(&ipst->ips_ndp4->ndp_g_lock); 2974 if (nce != NULL) 2975 NCE_REFRELE(nce); 2976 DTRACE_PROBE1(ire__no__nce, ire_t *, ire); 2977 ire_delete(ire); 2978 if (pire != NULL) { 2979 IRB_REFRELE(pire->ire_bucket); 2980 ire_refrele(pire); 2981 } 2982 *ire_p = NULL; 2983 if (need_refrele) 2984 IRB_REFRELE(irb_ptr); 2985 return (EINVAL); 2986 } else { 2987 ire->ire_nce = nce; 2988 mutex_exit(&nce->nce_lock); 2989 /* 2990 * We are associating this nce to the ire, so 2991 * change the nce ref taken in ndp_lookup_v4() from 2992 * NCE_REFHOLD to NCE_REFHOLD_NOTR 2993 */ 2994 NCE_REFHOLD_TO_REFHOLD_NOTR(ire->ire_nce); 2995 } 2996 } 2997 /* 2998 * Make it easy for ip_wput_ire() to hit multiple broadcast ires by 2999 * grouping identical addresses together on the hash chain. We also 3000 * don't want to send multiple copies out if there are two ills part 3001 * of the same group. Thus we group the ires with same addr and same 3002 * ill group together so that ip_wput_ire can easily skip all the 3003 * ires with same addr and same group after sending the first copy. 3004 * We do this only for IRE_BROADCASTs as ip_wput_ire is currently 3005 * interested in such groupings only for broadcasts. 3006 * 3007 * NOTE : If the interfaces are brought up first and then grouped, 3008 * illgrp_insert will handle it. We come here when the interfaces 3009 * are already in group and we are bringing them UP. 3010 * 3011 * Find the first entry that matches ire_addr. *irep will be null 3012 * if no match. 3013 * 3014 * Note: the loopback and non-loopback broadcast entries for an 3015 * interface MUST be added before any MULTIRT entries. 3016 */ 3017 irep = (ire_t **)irb_ptr; 3018 while ((ire1 = *irep) != NULL && ire->ire_addr != ire1->ire_addr) 3019 irep = &ire1->ire_next; 3020 if (ire->ire_type == IRE_BROADCAST && *irep != NULL) { 3021 /* 3022 * We found some ire (i.e *irep) with a matching addr. We 3023 * want to group ires with same addr and same ill group 3024 * together. 3025 * 3026 * First get to the entry that matches our address and 3027 * ill group i.e stop as soon as we find the first ire 3028 * matching the ill group and address. If there is only 3029 * an address match, we should walk and look for some 3030 * group match. These are some of the possible scenarios : 3031 * 3032 * 1) There are no groups at all i.e all ire's ill_group 3033 * are NULL. In that case we will essentially group 3034 * all the ires with the same addr together. Same as 3035 * the "else" block of this "if". 3036 * 3037 * 2) There are some groups and this ire's ill_group is 3038 * NULL. In this case, we will first find the group 3039 * that matches the address and a NULL group. Then 3040 * we will insert the ire at the end of that group. 3041 * 3042 * 3) There are some groups and this ires's ill_group is 3043 * non-NULL. In this case we will first find the group 3044 * that matches the address and the ill_group. Then 3045 * we will insert the ire at the end of that group. 3046 */ 3047 for (;;) { 3048 ire1 = *irep; 3049 if ((ire1->ire_next == NULL) || 3050 (ire1->ire_next->ire_addr != ire->ire_addr) || 3051 (ire1->ire_type != IRE_BROADCAST) || 3052 (ire1->ire_flags & RTF_MULTIRT) || 3053 (ire1->ire_ipif->ipif_ill->ill_group == 3054 ire->ire_ipif->ipif_ill->ill_group)) 3055 break; 3056 irep = &ire1->ire_next; 3057 } 3058 ASSERT(*irep != NULL); 3059 /* 3060 * The ire will be added before *irep, so 3061 * if irep is a MULTIRT ire, just break to 3062 * ire insertion code. 3063 */ 3064 if (((*irep)->ire_flags & RTF_MULTIRT) != 0) 3065 goto insert_ire; 3066 3067 irep = &((*irep)->ire_next); 3068 3069 /* 3070 * Either we have hit the end of the list or the address 3071 * did not match or the group *matched*. If we found 3072 * a match on the group, skip to the end of the group. 3073 */ 3074 while (*irep != NULL) { 3075 ire1 = *irep; 3076 if ((ire1->ire_addr != ire->ire_addr) || 3077 (ire1->ire_type != IRE_BROADCAST) || 3078 (ire1->ire_ipif->ipif_ill->ill_group != 3079 ire->ire_ipif->ipif_ill->ill_group)) 3080 break; 3081 if (ire1->ire_ipif->ipif_ill->ill_group == NULL && 3082 ire1->ire_ipif == ire->ire_ipif) { 3083 irep = &ire1->ire_next; 3084 break; 3085 } 3086 irep = &ire1->ire_next; 3087 } 3088 } else if (*irep != NULL) { 3089 /* 3090 * Find the last ire which matches ire_addr. 3091 * Needed to do tail insertion among entries with the same 3092 * ire_addr. 3093 */ 3094 while (ire->ire_addr == ire1->ire_addr) { 3095 irep = &ire1->ire_next; 3096 ire1 = *irep; 3097 if (ire1 == NULL) 3098 break; 3099 } 3100 } 3101 3102 insert_ire: 3103 /* Insert at *irep */ 3104 ire1 = *irep; 3105 if (ire1 != NULL) 3106 ire1->ire_ptpn = &ire->ire_next; 3107 ire->ire_next = ire1; 3108 /* Link the new one in. */ 3109 ire->ire_ptpn = irep; 3110 3111 /* 3112 * ire_walk routines de-reference ire_next without holding 3113 * a lock. Before we point to the new ire, we want to make 3114 * sure the store that sets the ire_next of the new ire 3115 * reaches global visibility, so that ire_walk routines 3116 * don't see a truncated list of ires i.e if the ire_next 3117 * of the new ire gets set after we do "*irep = ire" due 3118 * to re-ordering, the ire_walk thread will see a NULL 3119 * once it accesses the ire_next of the new ire. 3120 * membar_producer() makes sure that the following store 3121 * happens *after* all of the above stores. 3122 */ 3123 membar_producer(); 3124 *irep = ire; 3125 ire->ire_bucket = irb_ptr; 3126 /* 3127 * We return a bumped up IRE above. Keep it symmetrical 3128 * so that the callers will always have to release. This 3129 * helps the callers of this function because they continue 3130 * to use the IRE after adding and hence they don't have to 3131 * lookup again after we return the IRE. 3132 * 3133 * NOTE : We don't have to use atomics as this is appearing 3134 * in the list for the first time and no one else can bump 3135 * up the reference count on this yet. 3136 */ 3137 IRE_REFHOLD_LOCKED(ire); 3138 BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted); 3139 3140 irb_ptr->irb_ire_cnt++; 3141 if (irb_ptr->irb_marks & IRB_MARK_FTABLE) 3142 irb_ptr->irb_nire++; 3143 3144 if (ire->ire_marks & IRE_MARK_TEMPORARY) 3145 irb_ptr->irb_tmp_ire_cnt++; 3146 3147 if (ire->ire_ipif != NULL) { 3148 DTRACE_PROBE3(ipif__incr__cnt, (ipif_t *), ire->ire_ipif, 3149 (char *), "ire", (void *), ire); 3150 ire->ire_ipif->ipif_ire_cnt++; 3151 if (ire->ire_stq != NULL) { 3152 stq_ill = (ill_t *)ire->ire_stq->q_ptr; 3153 DTRACE_PROBE3(ill__incr__cnt, (ill_t *), stq_ill, 3154 (char *), "ire", (void *), ire); 3155 stq_ill->ill_ire_cnt++; 3156 } 3157 } else { 3158 ASSERT(ire->ire_stq == NULL); 3159 } 3160 3161 ire_atomic_end(irb_ptr, ire); 3162 mutex_exit(&ipst->ips_ndp4->ndp_g_lock); 3163 3164 if (pire != NULL) { 3165 /* Assert that it is not removed from the list yet */ 3166 ASSERT(pire->ire_ptpn != NULL); 3167 IRB_REFRELE(pire->ire_bucket); 3168 ire_refrele(pire); 3169 } 3170 3171 if (ire->ire_type != IRE_CACHE) { 3172 /* 3173 * For ire's with host mask see if there is an entry 3174 * in the cache. If there is one flush the whole cache as 3175 * there might be multiple entries due to RTF_MULTIRT (CGTP). 3176 * If no entry is found than there is no need to flush the 3177 * cache. 3178 */ 3179 if (ire->ire_mask == IP_HOST_MASK) { 3180 ire_t *lire; 3181 lire = ire_ctable_lookup(ire->ire_addr, NULL, IRE_CACHE, 3182 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 3183 if (lire != NULL) { 3184 ire_refrele(lire); 3185 ire_flush_cache_v4(ire, IRE_FLUSH_ADD); 3186 } 3187 } else { 3188 ire_flush_cache_v4(ire, IRE_FLUSH_ADD); 3189 } 3190 } 3191 /* 3192 * We had to delay the fast path probe until the ire is inserted 3193 * in the list. Otherwise the fast path ack won't find the ire in 3194 * the table. 3195 */ 3196 if (ire->ire_type == IRE_CACHE || 3197 (ire->ire_type == IRE_BROADCAST && ire->ire_stq != NULL)) { 3198 ASSERT(ire->ire_nce != NULL); 3199 if (ire->ire_nce->nce_state == ND_REACHABLE) 3200 nce_fastpath(ire->ire_nce); 3201 } 3202 if (ire->ire_ipif != NULL) 3203 ASSERT(!MUTEX_HELD(&ire->ire_ipif->ipif_ill->ill_lock)); 3204 *ire_p = ire; 3205 if (need_refrele) { 3206 IRB_REFRELE(irb_ptr); 3207 } 3208 return (0); 3209 } 3210 3211 /* 3212 * IRB_REFRELE is the only caller of the function. ire_unlink calls to 3213 * do the final cleanup for this ire. 3214 */ 3215 void 3216 ire_cleanup(ire_t *ire) 3217 { 3218 ire_t *ire_next; 3219 ip_stack_t *ipst = ire->ire_ipst; 3220 3221 ASSERT(ire != NULL); 3222 3223 while (ire != NULL) { 3224 ire_next = ire->ire_next; 3225 if (ire->ire_ipversion == IPV4_VERSION) { 3226 ire_delete_v4(ire); 3227 BUMP_IRE_STATS(ipst->ips_ire_stats_v4, 3228 ire_stats_deleted); 3229 } else { 3230 ASSERT(ire->ire_ipversion == IPV6_VERSION); 3231 ire_delete_v6(ire); 3232 BUMP_IRE_STATS(ipst->ips_ire_stats_v6, 3233 ire_stats_deleted); 3234 } 3235 /* 3236 * Now it's really out of the list. Before doing the 3237 * REFRELE, set ire_next to NULL as ire_inactive asserts 3238 * so. 3239 */ 3240 ire->ire_next = NULL; 3241 IRE_REFRELE_NOTR(ire); 3242 ire = ire_next; 3243 } 3244 } 3245 3246 /* 3247 * IRB_REFRELE is the only caller of the function. It calls to unlink 3248 * all the CONDEMNED ires from this bucket. 3249 */ 3250 ire_t * 3251 ire_unlink(irb_t *irb) 3252 { 3253 ire_t *ire; 3254 ire_t *ire1; 3255 ire_t **ptpn; 3256 ire_t *ire_list = NULL; 3257 3258 ASSERT(RW_WRITE_HELD(&irb->irb_lock)); 3259 ASSERT(((irb->irb_marks & IRB_MARK_FTABLE) && irb->irb_refcnt == 1) || 3260 (irb->irb_refcnt == 0)); 3261 ASSERT(irb->irb_marks & IRB_MARK_CONDEMNED); 3262 ASSERT(irb->irb_ire != NULL); 3263 3264 for (ire = irb->irb_ire; ire != NULL; ire = ire1) { 3265 ip_stack_t *ipst = ire->ire_ipst; 3266 3267 ire1 = ire->ire_next; 3268 if (ire->ire_marks & IRE_MARK_CONDEMNED) { 3269 ptpn = ire->ire_ptpn; 3270 ire1 = ire->ire_next; 3271 if (ire1) 3272 ire1->ire_ptpn = ptpn; 3273 *ptpn = ire1; 3274 ire->ire_ptpn = NULL; 3275 ire->ire_next = NULL; 3276 if (ire->ire_type == IRE_DEFAULT) { 3277 /* 3278 * IRE is out of the list. We need to adjust 3279 * the accounting before the caller drops 3280 * the lock. 3281 */ 3282 if (ire->ire_ipversion == IPV6_VERSION) { 3283 ASSERT(ipst-> 3284 ips_ipv6_ire_default_count != 3285 0); 3286 ipst->ips_ipv6_ire_default_count--; 3287 } 3288 } 3289 /* 3290 * We need to call ire_delete_v4 or ire_delete_v6 3291 * to clean up the cache or the redirects pointing at 3292 * the default gateway. We need to drop the lock 3293 * as ire_flush_cache/ire_delete_host_redircts require 3294 * so. But we can't drop the lock, as ire_unlink needs 3295 * to atomically remove the ires from the list. 3296 * So, create a temporary list of CONDEMNED ires 3297 * for doing ire_delete_v4/ire_delete_v6 operations 3298 * later on. 3299 */ 3300 ire->ire_next = ire_list; 3301 ire_list = ire; 3302 } 3303 } 3304 irb->irb_marks &= ~IRB_MARK_CONDEMNED; 3305 return (ire_list); 3306 } 3307 3308 /* 3309 * Delete all the cache entries with this 'addr'. When IP gets a gratuitous 3310 * ARP message on any of its interface queue, it scans the nce table and 3311 * deletes and calls ndp_delete() for the appropriate nce. This action 3312 * also deletes all the neighbor/ire cache entries for that address. 3313 * This function is called from ip_arp_news in ip.c and also for 3314 * ARP ioctl processing in ip_if.c. ip_ire_clookup_and_delete returns 3315 * true if it finds a nce entry which is used by ip_arp_news to determine if 3316 * it needs to do an ire_walk_v4. The return value is also used for the 3317 * same purpose by ARP IOCTL processing * in ip_if.c when deleting 3318 * ARP entries. For SIOC*IFARP ioctls in addition to the address, 3319 * ip_if->ipif_ill also needs to be matched. 3320 */ 3321 boolean_t 3322 ip_ire_clookup_and_delete(ipaddr_t addr, ipif_t *ipif, ip_stack_t *ipst) 3323 { 3324 ill_t *ill; 3325 nce_t *nce; 3326 3327 ill = (ipif ? ipif->ipif_ill : NULL); 3328 3329 if (ill != NULL) { 3330 /* 3331 * clean up the nce (and any relevant ire's) that matches 3332 * on addr and ill. 3333 */ 3334 nce = ndp_lookup_v4(ill, &addr, B_FALSE); 3335 if (nce != NULL) { 3336 ndp_delete(nce); 3337 return (B_TRUE); 3338 } 3339 } else { 3340 /* 3341 * ill is wildcard. clean up all nce's and 3342 * ire's that match on addr 3343 */ 3344 nce_clookup_t cl; 3345 3346 cl.ncecl_addr = addr; 3347 cl.ncecl_found = B_FALSE; 3348 3349 ndp_walk_common(ipst->ips_ndp4, NULL, 3350 (pfi_t)ip_nce_clookup_and_delete, (uchar_t *)&cl, B_TRUE); 3351 3352 /* 3353 * ncecl_found would be set by ip_nce_clookup_and_delete if 3354 * we found a matching nce. 3355 */ 3356 return (cl.ncecl_found); 3357 } 3358 return (B_FALSE); 3359 3360 } 3361 3362 /* Delete the supplied nce if its nce_addr matches the supplied address */ 3363 static void 3364 ip_nce_clookup_and_delete(nce_t *nce, void *arg) 3365 { 3366 nce_clookup_t *cl = (nce_clookup_t *)arg; 3367 ipaddr_t nce_addr; 3368 3369 IN6_V4MAPPED_TO_IPADDR(&nce->nce_addr, nce_addr); 3370 if (nce_addr == cl->ncecl_addr) { 3371 cl->ncecl_found = B_TRUE; 3372 /* clean up the nce (and any relevant ire's) */ 3373 ndp_delete(nce); 3374 } 3375 } 3376 3377 /* 3378 * Clean up the radix node for this ire. Must be called by IRB_REFRELE 3379 * when there are no ire's left in the bucket. Returns TRUE if the bucket 3380 * is deleted and freed. 3381 */ 3382 boolean_t 3383 irb_inactive(irb_t *irb) 3384 { 3385 struct rt_entry *rt; 3386 struct radix_node *rn; 3387 ip_stack_t *ipst = irb->irb_ipst; 3388 3389 ASSERT(irb->irb_ipst != NULL); 3390 3391 rt = IRB2RT(irb); 3392 rn = (struct radix_node *)rt; 3393 3394 /* first remove it from the radix tree. */ 3395 RADIX_NODE_HEAD_WLOCK(ipst->ips_ip_ftable); 3396 rw_enter(&irb->irb_lock, RW_WRITER); 3397 if (irb->irb_refcnt == 1 && irb->irb_nire == 0) { 3398 rn = ipst->ips_ip_ftable->rnh_deladdr(rn->rn_key, rn->rn_mask, 3399 ipst->ips_ip_ftable); 3400 DTRACE_PROBE1(irb__free, rt_t *, rt); 3401 ASSERT((void *)rn == (void *)rt); 3402 Free(rt, rt_entry_cache); 3403 /* irb_lock is freed */ 3404 RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable); 3405 return (B_TRUE); 3406 } 3407 rw_exit(&irb->irb_lock); 3408 RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable); 3409 return (B_FALSE); 3410 } 3411 3412 /* 3413 * Delete the specified IRE. 3414 */ 3415 void 3416 ire_delete(ire_t *ire) 3417 { 3418 ire_t *ire1; 3419 ire_t **ptpn; 3420 irb_t *irb; 3421 ip_stack_t *ipst = ire->ire_ipst; 3422 3423 if ((irb = ire->ire_bucket) == NULL) { 3424 /* 3425 * It was never inserted in the list. Should call REFRELE 3426 * to free this IRE. 3427 */ 3428 IRE_REFRELE_NOTR(ire); 3429 return; 3430 } 3431 3432 rw_enter(&irb->irb_lock, RW_WRITER); 3433 3434 if (irb->irb_rr_origin == ire) { 3435 irb->irb_rr_origin = NULL; 3436 } 3437 3438 /* 3439 * In case of V4 we might still be waiting for fastpath ack. 3440 */ 3441 if (ire->ire_ipversion == IPV4_VERSION && 3442 (ire->ire_type == IRE_CACHE || 3443 (ire->ire_type == IRE_BROADCAST && ire->ire_stq != NULL))) { 3444 ASSERT(ire->ire_nce != NULL); 3445 nce_fastpath_list_delete(ire->ire_nce); 3446 } 3447 3448 if (ire->ire_ptpn == NULL) { 3449 /* 3450 * Some other thread has removed us from the list. 3451 * It should have done the REFRELE for us. 3452 */ 3453 rw_exit(&irb->irb_lock); 3454 return; 3455 } 3456 3457 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 3458 irb->irb_ire_cnt--; 3459 ire->ire_marks |= IRE_MARK_CONDEMNED; 3460 if (ire->ire_marks & IRE_MARK_TEMPORARY) { 3461 irb->irb_tmp_ire_cnt--; 3462 ire->ire_marks &= ~IRE_MARK_TEMPORARY; 3463 } 3464 } 3465 3466 if (irb->irb_refcnt != 0) { 3467 /* 3468 * The last thread to leave this bucket will 3469 * delete this ire. 3470 */ 3471 irb->irb_marks |= IRB_MARK_CONDEMNED; 3472 rw_exit(&irb->irb_lock); 3473 return; 3474 } 3475 3476 /* 3477 * Normally to delete an ire, we walk the bucket. While we 3478 * walk the bucket, we normally bump up irb_refcnt and hence 3479 * we return from above where we mark CONDEMNED and the ire 3480 * gets deleted from ire_unlink. This case is where somebody 3481 * knows the ire e.g by doing a lookup, and wants to delete the 3482 * IRE. irb_refcnt would be 0 in this case if nobody is walking 3483 * the bucket. 3484 */ 3485 ptpn = ire->ire_ptpn; 3486 ire1 = ire->ire_next; 3487 if (ire1 != NULL) 3488 ire1->ire_ptpn = ptpn; 3489 ASSERT(ptpn != NULL); 3490 *ptpn = ire1; 3491 ire->ire_ptpn = NULL; 3492 ire->ire_next = NULL; 3493 if (ire->ire_ipversion == IPV6_VERSION) { 3494 BUMP_IRE_STATS(ipst->ips_ire_stats_v6, ire_stats_deleted); 3495 } else { 3496 BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_deleted); 3497 } 3498 /* 3499 * ip_wput/ip_wput_v6 checks this flag to see whether 3500 * it should still use the cached ire or not. 3501 */ 3502 if (ire->ire_type == IRE_DEFAULT) { 3503 /* 3504 * IRE is out of the list. We need to adjust the 3505 * accounting before we drop the lock. 3506 */ 3507 if (ire->ire_ipversion == IPV6_VERSION) { 3508 ASSERT(ipst->ips_ipv6_ire_default_count != 0); 3509 ipst->ips_ipv6_ire_default_count--; 3510 } 3511 } 3512 rw_exit(&irb->irb_lock); 3513 3514 if (ire->ire_ipversion == IPV6_VERSION) { 3515 ire_delete_v6(ire); 3516 } else { 3517 ire_delete_v4(ire); 3518 } 3519 /* 3520 * We removed it from the list. Decrement the 3521 * reference count. 3522 */ 3523 IRE_REFRELE_NOTR(ire); 3524 } 3525 3526 /* 3527 * Delete the specified IRE. 3528 * All calls should use ire_delete(). 3529 * Sometimes called as writer though not required by this function. 3530 * 3531 * NOTE : This function is called only if the ire was added 3532 * in the list. 3533 */ 3534 static void 3535 ire_delete_v4(ire_t *ire) 3536 { 3537 ip_stack_t *ipst = ire->ire_ipst; 3538 3539 ASSERT(ire->ire_refcnt >= 1); 3540 ASSERT(ire->ire_ipversion == IPV4_VERSION); 3541 3542 if (ire->ire_type != IRE_CACHE) 3543 ire_flush_cache_v4(ire, IRE_FLUSH_DELETE); 3544 if (ire->ire_type == IRE_DEFAULT) { 3545 /* 3546 * when a default gateway is going away 3547 * delete all the host redirects pointing at that 3548 * gateway. 3549 */ 3550 ire_delete_host_redirects(ire->ire_gateway_addr, ipst); 3551 } 3552 } 3553 3554 /* 3555 * IRE_REFRELE/ire_refrele are the only caller of the function. It calls 3556 * to free the ire when the reference count goes to zero. 3557 */ 3558 void 3559 ire_inactive(ire_t *ire) 3560 { 3561 nce_t *nce; 3562 ill_t *ill = NULL; 3563 ill_t *stq_ill = NULL; 3564 ipif_t *ipif; 3565 boolean_t need_wakeup = B_FALSE; 3566 irb_t *irb; 3567 ip_stack_t *ipst = ire->ire_ipst; 3568 3569 ASSERT(ire->ire_refcnt == 0); 3570 ASSERT(ire->ire_ptpn == NULL); 3571 ASSERT(ire->ire_next == NULL); 3572 3573 if (ire->ire_gw_secattr != NULL) { 3574 ire_gw_secattr_free(ire->ire_gw_secattr); 3575 ire->ire_gw_secattr = NULL; 3576 } 3577 3578 if (ire->ire_mp != NULL) { 3579 ASSERT(ire->ire_bucket == NULL); 3580 mutex_destroy(&ire->ire_lock); 3581 BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_freed); 3582 if (ire->ire_nce != NULL) 3583 NCE_REFRELE_NOTR(ire->ire_nce); 3584 freeb(ire->ire_mp); 3585 return; 3586 } 3587 3588 if ((nce = ire->ire_nce) != NULL) { 3589 NCE_REFRELE_NOTR(nce); 3590 ire->ire_nce = NULL; 3591 } 3592 3593 if (ire->ire_ipif == NULL) 3594 goto end; 3595 3596 ipif = ire->ire_ipif; 3597 ill = ipif->ipif_ill; 3598 3599 if (ire->ire_bucket == NULL) { 3600 /* The ire was never inserted in the table. */ 3601 goto end; 3602 } 3603 3604 /* 3605 * ipif_ire_cnt on this ipif goes down by 1. If the ire_stq is 3606 * non-null ill_ire_count also goes down by 1. 3607 * 3608 * The ipif that is associated with an ire is ire->ire_ipif and 3609 * hence when the ire->ire_ipif->ipif_ire_cnt drops to zero we call 3610 * ipif_ill_refrele_tail. Usually stq_ill is null or the same as 3611 * ire->ire_ipif->ipif_ill. So nothing more needs to be done. Only 3612 * in the case of IRE_CACHES when IPMP is used, stq_ill can be 3613 * different. If this is different from ire->ire_ipif->ipif_ill and 3614 * if the ill_ire_cnt on the stq_ill also has dropped to zero, we call 3615 * ipif_ill_refrele_tail on the stq_ill. 3616 */ 3617 3618 if (ire->ire_stq != NULL) 3619 stq_ill = (ill_t *)ire->ire_stq->q_ptr; 3620 3621 if (stq_ill == NULL || stq_ill == ill) { 3622 /* Optimize the most common case */ 3623 mutex_enter(&ill->ill_lock); 3624 ASSERT(ipif->ipif_ire_cnt != 0); 3625 DTRACE_PROBE3(ipif__decr__cnt, (ipif_t *), ipif, 3626 (char *), "ire", (void *), ire); 3627 ipif->ipif_ire_cnt--; 3628 if (IPIF_DOWN_OK(ipif)) 3629 need_wakeup = B_TRUE; 3630 if (stq_ill != NULL) { 3631 ASSERT(stq_ill->ill_ire_cnt != 0); 3632 DTRACE_PROBE3(ill__decr__cnt, (ill_t *), stq_ill, 3633 (char *), "ire", (void *), ire); 3634 stq_ill->ill_ire_cnt--; 3635 if (ILL_DOWN_OK(stq_ill)) 3636 need_wakeup = B_TRUE; 3637 } 3638 if (need_wakeup) { 3639 /* Drops the ill lock */ 3640 ipif_ill_refrele_tail(ill); 3641 } else { 3642 mutex_exit(&ill->ill_lock); 3643 } 3644 } else { 3645 /* 3646 * We can't grab all the ill locks at the same time. 3647 * It can lead to recursive lock enter in the call to 3648 * ipif_ill_refrele_tail and later. Instead do it 1 at 3649 * a time. 3650 */ 3651 mutex_enter(&ill->ill_lock); 3652 ASSERT(ipif->ipif_ire_cnt != 0); 3653 DTRACE_PROBE3(ipif__decr__cnt, (ipif_t *), ipif, 3654 (char *), "ire", (void *), ire); 3655 ipif->ipif_ire_cnt--; 3656 if (IPIF_DOWN_OK(ipif)) { 3657 /* Drops the lock */ 3658 ipif_ill_refrele_tail(ill); 3659 } else { 3660 mutex_exit(&ill->ill_lock); 3661 } 3662 if (stq_ill != NULL) { 3663 mutex_enter(&stq_ill->ill_lock); 3664 ASSERT(stq_ill->ill_ire_cnt != 0); 3665 DTRACE_PROBE3(ill__decr__cnt, (ill_t *), stq_ill, 3666 (char *), "ire", (void *), ire); 3667 stq_ill->ill_ire_cnt--; 3668 if (ILL_DOWN_OK(stq_ill)) { 3669 /* Drops the ill lock */ 3670 ipif_ill_refrele_tail(stq_ill); 3671 } else { 3672 mutex_exit(&stq_ill->ill_lock); 3673 } 3674 } 3675 } 3676 end: 3677 /* This should be true for both V4 and V6 */ 3678 3679 if ((ire->ire_type & IRE_FORWARDTABLE) && 3680 (ire->ire_ipversion == IPV4_VERSION) && 3681 ((irb = ire->ire_bucket) != NULL)) { 3682 rw_enter(&irb->irb_lock, RW_WRITER); 3683 irb->irb_nire--; 3684 /* 3685 * Instead of examining the conditions for freeing 3686 * the radix node here, we do it by calling 3687 * IRB_REFRELE which is a single point in the code 3688 * that embeds that logic. Bump up the refcnt to 3689 * be able to call IRB_REFRELE 3690 */ 3691 IRB_REFHOLD_LOCKED(irb); 3692 rw_exit(&irb->irb_lock); 3693 IRB_REFRELE(irb); 3694 } 3695 ire->ire_ipif = NULL; 3696 3697 #ifdef DEBUG 3698 ire_trace_cleanup(ire); 3699 #endif 3700 mutex_destroy(&ire->ire_lock); 3701 if (ire->ire_ipversion == IPV6_VERSION) { 3702 BUMP_IRE_STATS(ipst->ips_ire_stats_v6, ire_stats_freed); 3703 } else { 3704 BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_freed); 3705 } 3706 ASSERT(ire->ire_mp == NULL); 3707 /* Has been allocated out of the cache */ 3708 kmem_cache_free(ire_cache, ire); 3709 } 3710 3711 /* 3712 * ire_walk routine to delete all IRE_CACHE/IRE_HOST types redirect 3713 * entries that have a given gateway address. 3714 */ 3715 void 3716 ire_delete_cache_gw(ire_t *ire, char *cp) 3717 { 3718 ipaddr_t gw_addr; 3719 3720 if (!(ire->ire_type & IRE_CACHE) && 3721 !(ire->ire_flags & RTF_DYNAMIC)) 3722 return; 3723 3724 bcopy(cp, &gw_addr, sizeof (gw_addr)); 3725 if (ire->ire_gateway_addr == gw_addr) { 3726 ip1dbg(("ire_delete_cache_gw: deleted 0x%x type %d to 0x%x\n", 3727 (int)ntohl(ire->ire_addr), ire->ire_type, 3728 (int)ntohl(ire->ire_gateway_addr))); 3729 ire_delete(ire); 3730 } 3731 } 3732 3733 /* 3734 * Remove all IRE_CACHE entries that match the ire specified. 3735 * 3736 * The flag argument indicates if the flush request is due to addition 3737 * of new route (IRE_FLUSH_ADD) or deletion of old route (IRE_FLUSH_DELETE). 3738 * 3739 * This routine takes only the IREs from the forwarding table and flushes 3740 * the corresponding entries from the cache table. 3741 * 3742 * When flushing due to the deletion of an old route, it 3743 * just checks the cache handles (ire_phandle and ire_ihandle) and 3744 * deletes the ones that match. 3745 * 3746 * When flushing due to the creation of a new route, it checks 3747 * if a cache entry's address matches the one in the IRE and 3748 * that the cache entry's parent has a less specific mask than the 3749 * one in IRE. The destination of such a cache entry could be the 3750 * gateway for other cache entries, so we need to flush those as 3751 * well by looking for gateway addresses matching the IRE's address. 3752 */ 3753 void 3754 ire_flush_cache_v4(ire_t *ire, int flag) 3755 { 3756 int i; 3757 ire_t *cire; 3758 irb_t *irb; 3759 ip_stack_t *ipst = ire->ire_ipst; 3760 3761 if (ire->ire_type & IRE_CACHE) 3762 return; 3763 3764 /* 3765 * If a default is just created, there is no point 3766 * in going through the cache, as there will not be any 3767 * cached ires. 3768 */ 3769 if (ire->ire_type == IRE_DEFAULT && flag == IRE_FLUSH_ADD) 3770 return; 3771 if (flag == IRE_FLUSH_ADD) { 3772 /* 3773 * This selective flush is due to the addition of 3774 * new IRE. 3775 */ 3776 for (i = 0; i < ipst->ips_ip_cache_table_size; i++) { 3777 irb = &ipst->ips_ip_cache_table[i]; 3778 if ((cire = irb->irb_ire) == NULL) 3779 continue; 3780 IRB_REFHOLD(irb); 3781 for (cire = irb->irb_ire; cire != NULL; 3782 cire = cire->ire_next) { 3783 if (cire->ire_type != IRE_CACHE) 3784 continue; 3785 /* 3786 * If 'cire' belongs to the same subnet 3787 * as the new ire being added, and 'cire' 3788 * is derived from a prefix that is less 3789 * specific than the new ire being added, 3790 * we need to flush 'cire'; for instance, 3791 * when a new interface comes up. 3792 */ 3793 if (((cire->ire_addr & ire->ire_mask) == 3794 (ire->ire_addr & ire->ire_mask)) && 3795 (ip_mask_to_plen(cire->ire_cmask) <= 3796 ire->ire_masklen)) { 3797 ire_delete(cire); 3798 continue; 3799 } 3800 /* 3801 * This is the case when the ire_gateway_addr 3802 * of 'cire' belongs to the same subnet as 3803 * the new ire being added. 3804 * Flushing such ires is sometimes required to 3805 * avoid misrouting: say we have a machine with 3806 * two interfaces (I1 and I2), a default router 3807 * R on the I1 subnet, and a host route to an 3808 * off-link destination D with a gateway G on 3809 * the I2 subnet. 3810 * Under normal operation, we will have an 3811 * on-link cache entry for G and an off-link 3812 * cache entry for D with G as ire_gateway_addr, 3813 * traffic to D will reach its destination 3814 * through gateway G. 3815 * If the administrator does 'ifconfig I2 down', 3816 * the cache entries for D and G will be 3817 * flushed. However, G will now be resolved as 3818 * an off-link destination using R (the default 3819 * router) as gateway. Then D will also be 3820 * resolved as an off-link destination using G 3821 * as gateway - this behavior is due to 3822 * compatibility reasons, see comment in 3823 * ire_ihandle_lookup_offlink(). Traffic to D 3824 * will go to the router R and probably won't 3825 * reach the destination. 3826 * The administrator then does 'ifconfig I2 up'. 3827 * Since G is on the I2 subnet, this routine 3828 * will flush its cache entry. It must also 3829 * flush the cache entry for D, otherwise 3830 * traffic will stay misrouted until the IRE 3831 * times out. 3832 */ 3833 if ((cire->ire_gateway_addr & ire->ire_mask) == 3834 (ire->ire_addr & ire->ire_mask)) { 3835 ire_delete(cire); 3836 continue; 3837 } 3838 } 3839 IRB_REFRELE(irb); 3840 } 3841 } else { 3842 /* 3843 * delete the cache entries based on 3844 * handle in the IRE as this IRE is 3845 * being deleted/changed. 3846 */ 3847 for (i = 0; i < ipst->ips_ip_cache_table_size; i++) { 3848 irb = &ipst->ips_ip_cache_table[i]; 3849 if ((cire = irb->irb_ire) == NULL) 3850 continue; 3851 IRB_REFHOLD(irb); 3852 for (cire = irb->irb_ire; cire != NULL; 3853 cire = cire->ire_next) { 3854 if (cire->ire_type != IRE_CACHE) 3855 continue; 3856 if ((cire->ire_phandle == 0 || 3857 cire->ire_phandle != ire->ire_phandle) && 3858 (cire->ire_ihandle == 0 || 3859 cire->ire_ihandle != ire->ire_ihandle)) 3860 continue; 3861 ire_delete(cire); 3862 } 3863 IRB_REFRELE(irb); 3864 } 3865 } 3866 } 3867 3868 /* 3869 * Matches the arguments passed with the values in the ire. 3870 * 3871 * Note: for match types that match using "ipif" passed in, ipif 3872 * must be checked for non-NULL before calling this routine. 3873 */ 3874 boolean_t 3875 ire_match_args(ire_t *ire, ipaddr_t addr, ipaddr_t mask, ipaddr_t gateway, 3876 int type, const ipif_t *ipif, zoneid_t zoneid, uint32_t ihandle, 3877 const ts_label_t *tsl, int match_flags) 3878 { 3879 ill_t *ire_ill = NULL, *dst_ill; 3880 ill_t *ipif_ill = NULL; 3881 ill_group_t *ire_ill_group = NULL; 3882 ill_group_t *ipif_ill_group = NULL; 3883 3884 ASSERT(ire->ire_ipversion == IPV4_VERSION); 3885 ASSERT((ire->ire_addr & ~ire->ire_mask) == 0); 3886 ASSERT((!(match_flags & (MATCH_IRE_ILL|MATCH_IRE_ILL_GROUP))) || 3887 (ipif != NULL && !ipif->ipif_isv6)); 3888 3889 /* 3890 * HIDDEN cache entries have to be looked up specifically with 3891 * MATCH_IRE_MARK_HIDDEN. MATCH_IRE_MARK_HIDDEN is usually set 3892 * when the interface is FAILED or INACTIVE. In that case, 3893 * any IRE_CACHES that exists should be marked with 3894 * IRE_MARK_HIDDEN. So, we don't really need to match below 3895 * for IRE_MARK_HIDDEN. But we do so for consistency. 3896 */ 3897 if (!(match_flags & MATCH_IRE_MARK_HIDDEN) && 3898 (ire->ire_marks & IRE_MARK_HIDDEN)) 3899 return (B_FALSE); 3900 3901 /* 3902 * MATCH_IRE_MARK_PRIVATE_ADDR is set when IP_NEXTHOP option 3903 * is used. In that case the routing table is bypassed and the 3904 * packets are sent directly to the specified nexthop. The 3905 * IRE_CACHE entry representing this route should be marked 3906 * with IRE_MARK_PRIVATE_ADDR. 3907 */ 3908 3909 if (!(match_flags & MATCH_IRE_MARK_PRIVATE_ADDR) && 3910 (ire->ire_marks & IRE_MARK_PRIVATE_ADDR)) 3911 return (B_FALSE); 3912 3913 if (zoneid != ALL_ZONES && zoneid != ire->ire_zoneid && 3914 ire->ire_zoneid != ALL_ZONES) { 3915 /* 3916 * If MATCH_IRE_ZONEONLY has been set and the supplied zoneid is 3917 * valid and does not match that of ire_zoneid, a failure to 3918 * match is reported at this point. Otherwise, since some IREs 3919 * that are available in the global zone can be used in local 3920 * zones, additional checks need to be performed: 3921 * 3922 * IRE_BROADCAST, IRE_CACHE and IRE_LOOPBACK 3923 * entries should never be matched in this situation. 3924 * 3925 * IRE entries that have an interface associated with them 3926 * should in general not match unless they are an IRE_LOCAL 3927 * or in the case when MATCH_IRE_DEFAULT has been set in 3928 * the caller. In the case of the former, checking of the 3929 * other fields supplied should take place. 3930 * 3931 * In the case where MATCH_IRE_DEFAULT has been set, 3932 * all of the ipif's associated with the IRE's ill are 3933 * checked to see if there is a matching zoneid. If any 3934 * one ipif has a matching zoneid, this IRE is a 3935 * potential candidate so checking of the other fields 3936 * takes place. 3937 * 3938 * In the case where the IRE_INTERFACE has a usable source 3939 * address (indicated by ill_usesrc_ifindex) in the 3940 * correct zone then it's permitted to return this IRE 3941 */ 3942 if (match_flags & MATCH_IRE_ZONEONLY) 3943 return (B_FALSE); 3944 if (ire->ire_type & (IRE_BROADCAST | IRE_CACHE | IRE_LOOPBACK)) 3945 return (B_FALSE); 3946 /* 3947 * Note, IRE_INTERFACE can have the stq as NULL. For 3948 * example, if the default multicast route is tied to 3949 * the loopback address. 3950 */ 3951 if ((ire->ire_type & IRE_INTERFACE) && 3952 (ire->ire_stq != NULL)) { 3953 dst_ill = (ill_t *)ire->ire_stq->q_ptr; 3954 /* 3955 * If there is a usable source address in the 3956 * zone, then it's ok to return an 3957 * IRE_INTERFACE 3958 */ 3959 if (ipif_usesrc_avail(dst_ill, zoneid)) { 3960 ip3dbg(("ire_match_args: dst_ill %p match %d\n", 3961 (void *)dst_ill, 3962 (ire->ire_addr == (addr & mask)))); 3963 } else { 3964 ip3dbg(("ire_match_args: src_ipif NULL" 3965 " dst_ill %p\n", (void *)dst_ill)); 3966 return (B_FALSE); 3967 } 3968 } 3969 if (ire->ire_ipif != NULL && ire->ire_type != IRE_LOCAL && 3970 !(ire->ire_type & IRE_INTERFACE)) { 3971 ipif_t *tipif; 3972 3973 if ((match_flags & MATCH_IRE_DEFAULT) == 0) { 3974 return (B_FALSE); 3975 } 3976 mutex_enter(&ire->ire_ipif->ipif_ill->ill_lock); 3977 for (tipif = ire->ire_ipif->ipif_ill->ill_ipif; 3978 tipif != NULL; tipif = tipif->ipif_next) { 3979 if (IPIF_CAN_LOOKUP(tipif) && 3980 (tipif->ipif_flags & IPIF_UP) && 3981 (tipif->ipif_zoneid == zoneid || 3982 tipif->ipif_zoneid == ALL_ZONES)) 3983 break; 3984 } 3985 mutex_exit(&ire->ire_ipif->ipif_ill->ill_lock); 3986 if (tipif == NULL) { 3987 return (B_FALSE); 3988 } 3989 } 3990 } 3991 3992 /* 3993 * For IRE_CACHES, MATCH_IRE_ILL/ILL_GROUP really means that 3994 * somebody wants to send out on a particular interface which 3995 * is given by ire_stq and hence use ire_stq to derive the ill 3996 * value. ire_ipif for IRE_CACHES is just the means of getting 3997 * a source address i.e ire_src_addr = ire->ire_ipif->ipif_src_addr. 3998 * ire_to_ill does the right thing for this. 3999 */ 4000 if (match_flags & (MATCH_IRE_ILL|MATCH_IRE_ILL_GROUP)) { 4001 ire_ill = ire_to_ill(ire); 4002 if (ire_ill != NULL) 4003 ire_ill_group = ire_ill->ill_group; 4004 ipif_ill = ipif->ipif_ill; 4005 ipif_ill_group = ipif_ill->ill_group; 4006 } 4007 4008 if ((ire->ire_addr == (addr & mask)) && 4009 ((!(match_flags & MATCH_IRE_GW)) || 4010 (ire->ire_gateway_addr == gateway)) && 4011 ((!(match_flags & MATCH_IRE_TYPE)) || 4012 (ire->ire_type & type)) && 4013 ((!(match_flags & MATCH_IRE_SRC)) || 4014 (ire->ire_src_addr == ipif->ipif_src_addr)) && 4015 ((!(match_flags & MATCH_IRE_IPIF)) || 4016 (ire->ire_ipif == ipif)) && 4017 ((!(match_flags & MATCH_IRE_MARK_HIDDEN)) || 4018 (ire->ire_type != IRE_CACHE || 4019 ire->ire_marks & IRE_MARK_HIDDEN)) && 4020 ((!(match_flags & MATCH_IRE_MARK_PRIVATE_ADDR)) || 4021 (ire->ire_type != IRE_CACHE || 4022 ire->ire_marks & IRE_MARK_PRIVATE_ADDR)) && 4023 ((!(match_flags & MATCH_IRE_ILL)) || 4024 (ire_ill == ipif_ill)) && 4025 ((!(match_flags & MATCH_IRE_IHANDLE)) || 4026 (ire->ire_ihandle == ihandle)) && 4027 ((!(match_flags & MATCH_IRE_MASK)) || 4028 (ire->ire_mask == mask)) && 4029 ((!(match_flags & MATCH_IRE_ILL_GROUP)) || 4030 (ire_ill == ipif_ill) || 4031 (ire_ill_group != NULL && 4032 ire_ill_group == ipif_ill_group)) && 4033 ((!(match_flags & MATCH_IRE_SECATTR)) || 4034 (!is_system_labeled()) || 4035 (tsol_ire_match_gwattr(ire, tsl) == 0))) { 4036 /* We found the matched IRE */ 4037 return (B_TRUE); 4038 } 4039 return (B_FALSE); 4040 } 4041 4042 4043 /* 4044 * Lookup for a route in all the tables 4045 */ 4046 ire_t * 4047 ire_route_lookup(ipaddr_t addr, ipaddr_t mask, ipaddr_t gateway, 4048 int type, const ipif_t *ipif, ire_t **pire, zoneid_t zoneid, 4049 const ts_label_t *tsl, int flags, ip_stack_t *ipst) 4050 { 4051 ire_t *ire = NULL; 4052 4053 /* 4054 * ire_match_args() will dereference ipif MATCH_IRE_SRC or 4055 * MATCH_IRE_ILL is set. 4056 */ 4057 if ((flags & (MATCH_IRE_SRC | MATCH_IRE_ILL | MATCH_IRE_ILL_GROUP)) && 4058 (ipif == NULL)) 4059 return (NULL); 4060 4061 /* 4062 * might be asking for a cache lookup, 4063 * This is not best way to lookup cache, 4064 * user should call ire_cache_lookup directly. 4065 * 4066 * If MATCH_IRE_TYPE was set, first lookup in the cache table and then 4067 * in the forwarding table, if the applicable type flags were set. 4068 */ 4069 if ((flags & MATCH_IRE_TYPE) == 0 || (type & IRE_CACHETABLE) != 0) { 4070 ire = ire_ctable_lookup(addr, gateway, type, ipif, zoneid, 4071 tsl, flags, ipst); 4072 if (ire != NULL) 4073 return (ire); 4074 } 4075 if ((flags & MATCH_IRE_TYPE) == 0 || (type & IRE_FORWARDTABLE) != 0) { 4076 ire = ire_ftable_lookup(addr, mask, gateway, type, ipif, pire, 4077 zoneid, 0, tsl, flags, ipst); 4078 } 4079 return (ire); 4080 } 4081 4082 4083 /* 4084 * Delete the IRE cache for the gateway and all IRE caches whose 4085 * ire_gateway_addr points to this gateway, and allow them to 4086 * be created on demand by ip_newroute. 4087 */ 4088 void 4089 ire_clookup_delete_cache_gw(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst) 4090 { 4091 irb_t *irb; 4092 ire_t *ire; 4093 4094 irb = &ipst->ips_ip_cache_table[IRE_ADDR_HASH(addr, 4095 ipst->ips_ip_cache_table_size)]; 4096 IRB_REFHOLD(irb); 4097 for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) { 4098 if (ire->ire_marks & IRE_MARK_CONDEMNED) 4099 continue; 4100 4101 ASSERT(ire->ire_mask == IP_HOST_MASK); 4102 if (ire_match_args(ire, addr, ire->ire_mask, 0, IRE_CACHE, 4103 NULL, zoneid, 0, NULL, MATCH_IRE_TYPE)) { 4104 ire_delete(ire); 4105 } 4106 } 4107 IRB_REFRELE(irb); 4108 4109 ire_walk_v4(ire_delete_cache_gw, &addr, zoneid, ipst); 4110 } 4111 4112 /* 4113 * Looks up cache table for a route. 4114 * specific lookup can be indicated by 4115 * passing the MATCH_* flags and the 4116 * necessary parameters. 4117 */ 4118 ire_t * 4119 ire_ctable_lookup(ipaddr_t addr, ipaddr_t gateway, int type, const ipif_t *ipif, 4120 zoneid_t zoneid, const ts_label_t *tsl, int flags, ip_stack_t *ipst) 4121 { 4122 irb_t *irb_ptr; 4123 ire_t *ire; 4124 4125 /* 4126 * ire_match_args() will dereference ipif MATCH_IRE_SRC or 4127 * MATCH_IRE_ILL is set. 4128 */ 4129 if ((flags & (MATCH_IRE_SRC | MATCH_IRE_ILL | MATCH_IRE_ILL_GROUP)) && 4130 (ipif == NULL)) 4131 return (NULL); 4132 4133 irb_ptr = &ipst->ips_ip_cache_table[IRE_ADDR_HASH(addr, 4134 ipst->ips_ip_cache_table_size)]; 4135 rw_enter(&irb_ptr->irb_lock, RW_READER); 4136 for (ire = irb_ptr->irb_ire; ire != NULL; ire = ire->ire_next) { 4137 if (ire->ire_marks & IRE_MARK_CONDEMNED) 4138 continue; 4139 ASSERT(ire->ire_mask == IP_HOST_MASK); 4140 if (ire_match_args(ire, addr, ire->ire_mask, gateway, type, 4141 ipif, zoneid, 0, tsl, flags)) { 4142 IRE_REFHOLD(ire); 4143 rw_exit(&irb_ptr->irb_lock); 4144 return (ire); 4145 } 4146 } 4147 rw_exit(&irb_ptr->irb_lock); 4148 return (NULL); 4149 } 4150 4151 /* 4152 * Check whether the IRE_LOCAL and the IRE potentially used to transmit 4153 * (could be an IRE_CACHE, IRE_BROADCAST, or IRE_INTERFACE) are part of 4154 * the same ill group. 4155 */ 4156 boolean_t 4157 ire_local_same_ill_group(ire_t *ire_local, ire_t *xmit_ire) 4158 { 4159 ill_t *recv_ill, *xmit_ill; 4160 ill_group_t *recv_group, *xmit_group; 4161 4162 ASSERT(ire_local->ire_type & (IRE_LOCAL|IRE_LOOPBACK)); 4163 ASSERT(xmit_ire->ire_type & (IRE_CACHETABLE|IRE_INTERFACE)); 4164 4165 recv_ill = ire_to_ill(ire_local); 4166 xmit_ill = ire_to_ill(xmit_ire); 4167 4168 ASSERT(recv_ill != NULL); 4169 ASSERT(xmit_ill != NULL); 4170 4171 if (recv_ill == xmit_ill) 4172 return (B_TRUE); 4173 4174 recv_group = recv_ill->ill_group; 4175 xmit_group = xmit_ill->ill_group; 4176 4177 if (recv_group != NULL && recv_group == xmit_group) 4178 return (B_TRUE); 4179 4180 return (B_FALSE); 4181 } 4182 4183 /* 4184 * Check if the IRE_LOCAL uses the same ill (group) as another route would use. 4185 * If there is no alternate route, or the alternate is a REJECT or BLACKHOLE, 4186 * then we don't allow this IRE_LOCAL to be used. 4187 */ 4188 boolean_t 4189 ire_local_ok_across_zones(ire_t *ire_local, zoneid_t zoneid, void *addr, 4190 const ts_label_t *tsl, ip_stack_t *ipst) 4191 { 4192 ire_t *alt_ire; 4193 boolean_t rval; 4194 4195 if (ire_local->ire_ipversion == IPV4_VERSION) { 4196 alt_ire = ire_ftable_lookup(*((ipaddr_t *)addr), 0, 0, 0, NULL, 4197 NULL, zoneid, 0, tsl, 4198 MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 4199 MATCH_IRE_RJ_BHOLE, ipst); 4200 } else { 4201 alt_ire = ire_ftable_lookup_v6((in6_addr_t *)addr, NULL, NULL, 4202 0, NULL, NULL, zoneid, 0, tsl, 4203 MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 4204 MATCH_IRE_RJ_BHOLE, ipst); 4205 } 4206 4207 if (alt_ire == NULL) 4208 return (B_FALSE); 4209 4210 if (alt_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 4211 ire_refrele(alt_ire); 4212 return (B_FALSE); 4213 } 4214 rval = ire_local_same_ill_group(ire_local, alt_ire); 4215 4216 ire_refrele(alt_ire); 4217 return (rval); 4218 } 4219 4220 /* 4221 * Lookup cache. Don't return IRE_MARK_HIDDEN entries. Callers 4222 * should use ire_ctable_lookup with MATCH_IRE_MARK_HIDDEN to get 4223 * to the hidden ones. 4224 * 4225 * In general the zoneid has to match (where ALL_ZONES match all of them). 4226 * But for IRE_LOCAL we also need to handle the case where L2 should 4227 * conceptually loop back the packet. This is necessary since neither 4228 * Ethernet drivers nor Ethernet hardware loops back packets sent to their 4229 * own MAC address. This loopback is needed when the normal 4230 * routes (ignoring IREs with different zoneids) would send out the packet on 4231 * the same ill (or ill group) as the ill with which this IRE_LOCAL is 4232 * associated. 4233 * 4234 * Earlier versions of this code always matched an IRE_LOCAL independently of 4235 * the zoneid. We preserve that earlier behavior when 4236 * ip_restrict_interzone_loopback is turned off. 4237 */ 4238 ire_t * 4239 ire_cache_lookup(ipaddr_t addr, zoneid_t zoneid, const ts_label_t *tsl, 4240 ip_stack_t *ipst) 4241 { 4242 irb_t *irb_ptr; 4243 ire_t *ire; 4244 4245 irb_ptr = &ipst->ips_ip_cache_table[IRE_ADDR_HASH(addr, 4246 ipst->ips_ip_cache_table_size)]; 4247 rw_enter(&irb_ptr->irb_lock, RW_READER); 4248 for (ire = irb_ptr->irb_ire; ire != NULL; ire = ire->ire_next) { 4249 if (ire->ire_marks & (IRE_MARK_CONDEMNED | 4250 IRE_MARK_HIDDEN | IRE_MARK_PRIVATE_ADDR)) { 4251 continue; 4252 } 4253 if (ire->ire_addr == addr) { 4254 /* 4255 * Finally, check if the security policy has any 4256 * restriction on using this route for the specified 4257 * message. 4258 */ 4259 if (tsl != NULL && 4260 ire->ire_gw_secattr != NULL && 4261 tsol_ire_match_gwattr(ire, tsl) != 0) { 4262 continue; 4263 } 4264 4265 if (zoneid == ALL_ZONES || ire->ire_zoneid == zoneid || 4266 ire->ire_zoneid == ALL_ZONES) { 4267 IRE_REFHOLD(ire); 4268 rw_exit(&irb_ptr->irb_lock); 4269 return (ire); 4270 } 4271 4272 if (ire->ire_type == IRE_LOCAL) { 4273 if (ipst->ips_ip_restrict_interzone_loopback && 4274 !ire_local_ok_across_zones(ire, zoneid, 4275 &addr, tsl, ipst)) 4276 continue; 4277 4278 IRE_REFHOLD(ire); 4279 rw_exit(&irb_ptr->irb_lock); 4280 return (ire); 4281 } 4282 } 4283 } 4284 rw_exit(&irb_ptr->irb_lock); 4285 return (NULL); 4286 } 4287 4288 /* 4289 * Locate the interface ire that is tied to the cache ire 'cire' via 4290 * cire->ire_ihandle. 4291 * 4292 * We are trying to create the cache ire for an offlink destn based 4293 * on the cache ire of the gateway in 'cire'. 'pire' is the prefix ire 4294 * as found by ip_newroute(). We are called from ip_newroute() in 4295 * the IRE_CACHE case. 4296 */ 4297 ire_t * 4298 ire_ihandle_lookup_offlink(ire_t *cire, ire_t *pire) 4299 { 4300 ire_t *ire; 4301 int match_flags; 4302 ipaddr_t gw_addr; 4303 ipif_t *gw_ipif; 4304 ip_stack_t *ipst = cire->ire_ipst; 4305 4306 ASSERT(cire != NULL && pire != NULL); 4307 4308 /* 4309 * We don't need to specify the zoneid to ire_ftable_lookup() below 4310 * because the ihandle refers to an ipif which can be in only one zone. 4311 */ 4312 match_flags = MATCH_IRE_TYPE | MATCH_IRE_IHANDLE | MATCH_IRE_MASK; 4313 /* 4314 * ip_newroute calls ire_ftable_lookup with MATCH_IRE_ILL only 4315 * for on-link hosts. We should never be here for onlink. 4316 * Thus, use MATCH_IRE_ILL_GROUP. 4317 */ 4318 if (pire->ire_ipif != NULL) 4319 match_flags |= MATCH_IRE_ILL_GROUP; 4320 /* 4321 * We know that the mask of the interface ire equals cire->ire_cmask. 4322 * (When ip_newroute() created 'cire' for the gateway it set its 4323 * cmask from the interface ire's mask) 4324 */ 4325 ire = ire_ftable_lookup(cire->ire_addr, cire->ire_cmask, 0, 4326 IRE_INTERFACE, pire->ire_ipif, NULL, ALL_ZONES, cire->ire_ihandle, 4327 NULL, match_flags, ipst); 4328 if (ire != NULL) 4329 return (ire); 4330 /* 4331 * If we didn't find an interface ire above, we can't declare failure. 4332 * For backwards compatibility, we need to support prefix routes 4333 * pointing to next hop gateways that are not on-link. 4334 * 4335 * Assume we are trying to ping some offlink destn, and we have the 4336 * routing table below. 4337 * 4338 * Eg. default - gw1 <--- pire (line 1) 4339 * gw1 - gw2 (line 2) 4340 * gw2 - hme0 (line 3) 4341 * 4342 * If we already have a cache ire for gw1 in 'cire', the 4343 * ire_ftable_lookup above would have failed, since there is no 4344 * interface ire to reach gw1. We will fallthru below. 4345 * 4346 * Here we duplicate the steps that ire_ftable_lookup() did in 4347 * getting 'cire' from 'pire', in the MATCH_IRE_RECURSIVE case. 4348 * The differences are the following 4349 * i. We want the interface ire only, so we call ire_ftable_lookup() 4350 * instead of ire_route_lookup() 4351 * ii. We look for only prefix routes in the 1st call below. 4352 * ii. We want to match on the ihandle in the 2nd call below. 4353 */ 4354 match_flags = MATCH_IRE_TYPE; 4355 if (pire->ire_ipif != NULL) 4356 match_flags |= MATCH_IRE_ILL_GROUP; 4357 ire = ire_ftable_lookup(pire->ire_gateway_addr, 0, 0, IRE_OFFSUBNET, 4358 pire->ire_ipif, NULL, ALL_ZONES, 0, NULL, match_flags, ipst); 4359 if (ire == NULL) 4360 return (NULL); 4361 /* 4362 * At this point 'ire' corresponds to the entry shown in line 2. 4363 * gw_addr is 'gw2' in the example above. 4364 */ 4365 gw_addr = ire->ire_gateway_addr; 4366 gw_ipif = ire->ire_ipif; 4367 ire_refrele(ire); 4368 4369 match_flags |= MATCH_IRE_IHANDLE; 4370 ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, 4371 gw_ipif, NULL, ALL_ZONES, cire->ire_ihandle, NULL, match_flags, 4372 ipst); 4373 return (ire); 4374 } 4375 4376 /* 4377 * Return the IRE_LOOPBACK, IRE_IF_RESOLVER or IRE_IF_NORESOLVER 4378 * ire associated with the specified ipif. 4379 * 4380 * This might occasionally be called when IPIF_UP is not set since 4381 * the IP_MULTICAST_IF as well as creating interface routes 4382 * allows specifying a down ipif (ipif_lookup* match ipifs that are down). 4383 * 4384 * Note that if IPIF_NOLOCAL, IPIF_NOXMIT, or IPIF_DEPRECATED is set on 4385 * the ipif, this routine might return NULL. 4386 */ 4387 ire_t * 4388 ipif_to_ire(const ipif_t *ipif) 4389 { 4390 ire_t *ire; 4391 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst; 4392 4393 ASSERT(!ipif->ipif_isv6); 4394 if (ipif->ipif_ire_type == IRE_LOOPBACK) { 4395 ire = ire_ctable_lookup(ipif->ipif_lcl_addr, 0, IRE_LOOPBACK, 4396 ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF), 4397 ipst); 4398 } else if (ipif->ipif_flags & IPIF_POINTOPOINT) { 4399 /* In this case we need to lookup destination address. */ 4400 ire = ire_ftable_lookup(ipif->ipif_pp_dst_addr, IP_HOST_MASK, 0, 4401 IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL, 4402 (MATCH_IRE_TYPE | MATCH_IRE_IPIF | MATCH_IRE_MASK), ipst); 4403 } else { 4404 ire = ire_ftable_lookup(ipif->ipif_subnet, 4405 ipif->ipif_net_mask, 0, IRE_INTERFACE, ipif, NULL, 4406 ALL_ZONES, 0, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF | 4407 MATCH_IRE_MASK), ipst); 4408 } 4409 return (ire); 4410 } 4411 4412 /* 4413 * ire_walk function. 4414 * Count the number of IRE_CACHE entries in different categories. 4415 */ 4416 void 4417 ire_cache_count(ire_t *ire, char *arg) 4418 { 4419 ire_cache_count_t *icc = (ire_cache_count_t *)arg; 4420 4421 if (ire->ire_type != IRE_CACHE) 4422 return; 4423 4424 icc->icc_total++; 4425 4426 if (ire->ire_ipversion == IPV6_VERSION) { 4427 mutex_enter(&ire->ire_lock); 4428 if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_gateway_addr_v6)) { 4429 mutex_exit(&ire->ire_lock); 4430 icc->icc_onlink++; 4431 return; 4432 } 4433 mutex_exit(&ire->ire_lock); 4434 } else { 4435 if (ire->ire_gateway_addr == 0) { 4436 icc->icc_onlink++; 4437 return; 4438 } 4439 } 4440 4441 ASSERT(ire->ire_ipif != NULL); 4442 if (ire->ire_max_frag < ire->ire_ipif->ipif_mtu) 4443 icc->icc_pmtu++; 4444 else if (ire->ire_tire_mark != ire->ire_ob_pkt_count + 4445 ire->ire_ib_pkt_count) 4446 icc->icc_offlink++; 4447 else 4448 icc->icc_unused++; 4449 } 4450 4451 /* 4452 * ire_walk function called by ip_trash_ire_reclaim(). 4453 * Free a fraction of the IRE_CACHE cache entries. The fractions are 4454 * different for different categories of IRE_CACHE entries. 4455 * A fraction of zero means to not free any in that category. 4456 * Use the hash bucket id plus lbolt as a random number. Thus if the fraction 4457 * is N then every Nth hash bucket chain will be freed. 4458 */ 4459 void 4460 ire_cache_reclaim(ire_t *ire, char *arg) 4461 { 4462 ire_cache_reclaim_t *icr = (ire_cache_reclaim_t *)arg; 4463 uint_t rand; 4464 ip_stack_t *ipst = icr->icr_ipst; 4465 4466 if (ire->ire_type != IRE_CACHE) 4467 return; 4468 4469 if (ire->ire_ipversion == IPV6_VERSION) { 4470 rand = (uint_t)lbolt + 4471 IRE_ADDR_HASH_V6(ire->ire_addr_v6, 4472 ipst->ips_ip6_cache_table_size); 4473 mutex_enter(&ire->ire_lock); 4474 if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_gateway_addr_v6)) { 4475 mutex_exit(&ire->ire_lock); 4476 if (icr->icr_onlink != 0 && 4477 (rand/icr->icr_onlink)*icr->icr_onlink == rand) { 4478 ire_delete(ire); 4479 return; 4480 } 4481 goto done; 4482 } 4483 mutex_exit(&ire->ire_lock); 4484 } else { 4485 rand = (uint_t)lbolt + 4486 IRE_ADDR_HASH(ire->ire_addr, ipst->ips_ip_cache_table_size); 4487 if (ire->ire_gateway_addr == 0) { 4488 if (icr->icr_onlink != 0 && 4489 (rand/icr->icr_onlink)*icr->icr_onlink == rand) { 4490 ire_delete(ire); 4491 return; 4492 } 4493 goto done; 4494 } 4495 } 4496 /* Not onlink IRE */ 4497 ASSERT(ire->ire_ipif != NULL); 4498 if (ire->ire_max_frag < ire->ire_ipif->ipif_mtu) { 4499 /* Use ptmu fraction */ 4500 if (icr->icr_pmtu != 0 && 4501 (rand/icr->icr_pmtu)*icr->icr_pmtu == rand) { 4502 ire_delete(ire); 4503 return; 4504 } 4505 } else if (ire->ire_tire_mark != ire->ire_ob_pkt_count + 4506 ire->ire_ib_pkt_count) { 4507 /* Use offlink fraction */ 4508 if (icr->icr_offlink != 0 && 4509 (rand/icr->icr_offlink)*icr->icr_offlink == rand) { 4510 ire_delete(ire); 4511 return; 4512 } 4513 } else { 4514 /* Use unused fraction */ 4515 if (icr->icr_unused != 0 && 4516 (rand/icr->icr_unused)*icr->icr_unused == rand) { 4517 ire_delete(ire); 4518 return; 4519 } 4520 } 4521 done: 4522 /* 4523 * Update tire_mark so that those that haven't been used since this 4524 * reclaim will be considered unused next time we reclaim. 4525 */ 4526 ire->ire_tire_mark = ire->ire_ob_pkt_count + ire->ire_ib_pkt_count; 4527 } 4528 4529 static void 4530 power2_roundup(uint32_t *value) 4531 { 4532 int i; 4533 4534 for (i = 1; i < 31; i++) { 4535 if (*value <= (1 << i)) 4536 break; 4537 } 4538 *value = (1 << i); 4539 } 4540 4541 /* Global init for all zones */ 4542 void 4543 ip_ire_g_init() 4544 { 4545 /* 4546 * Create ire caches, ire_reclaim() 4547 * will give IRE_CACHE back to system when needed. 4548 * This needs to be done here before anything else, since 4549 * ire_add() expects the cache to be created. 4550 */ 4551 ire_cache = kmem_cache_create("ire_cache", 4552 sizeof (ire_t), 0, ip_ire_constructor, 4553 ip_ire_destructor, ip_trash_ire_reclaim, NULL, NULL, 0); 4554 4555 rt_entry_cache = kmem_cache_create("rt_entry", 4556 sizeof (struct rt_entry), 0, NULL, NULL, NULL, NULL, NULL, 0); 4557 4558 /* 4559 * Have radix code setup kmem caches etc. 4560 */ 4561 rn_init(); 4562 } 4563 4564 void 4565 ip_ire_init(ip_stack_t *ipst) 4566 { 4567 int i; 4568 uint32_t mem_cnt; 4569 uint32_t cpu_cnt; 4570 uint32_t min_cnt; 4571 pgcnt_t mem_avail; 4572 4573 /* 4574 * ip_ire_max_bucket_cnt is sized below based on the memory 4575 * size and the cpu speed of the machine. This is upper 4576 * bounded by the compile time value of ip_ire_max_bucket_cnt 4577 * and is lower bounded by the compile time value of 4578 * ip_ire_min_bucket_cnt. Similar logic applies to 4579 * ip6_ire_max_bucket_cnt. 4580 * 4581 * We calculate this for each IP Instances in order to use 4582 * the kmem_avail and ip_ire_{min,max}_bucket_cnt that are 4583 * in effect when the zone is booted. 4584 */ 4585 mem_avail = kmem_avail(); 4586 mem_cnt = (mem_avail >> ip_ire_mem_ratio) / 4587 ip_cache_table_size / sizeof (ire_t); 4588 cpu_cnt = CPU->cpu_type_info.pi_clock >> ip_ire_cpu_ratio; 4589 4590 min_cnt = MIN(cpu_cnt, mem_cnt); 4591 if (min_cnt < ip_ire_min_bucket_cnt) 4592 min_cnt = ip_ire_min_bucket_cnt; 4593 if (ip_ire_max_bucket_cnt > min_cnt) { 4594 ip_ire_max_bucket_cnt = min_cnt; 4595 } 4596 4597 mem_cnt = (mem_avail >> ip_ire_mem_ratio) / 4598 ip6_cache_table_size / sizeof (ire_t); 4599 min_cnt = MIN(cpu_cnt, mem_cnt); 4600 if (min_cnt < ip6_ire_min_bucket_cnt) 4601 min_cnt = ip6_ire_min_bucket_cnt; 4602 if (ip6_ire_max_bucket_cnt > min_cnt) { 4603 ip6_ire_max_bucket_cnt = min_cnt; 4604 } 4605 4606 mutex_init(&ipst->ips_ire_ft_init_lock, NULL, MUTEX_DEFAULT, 0); 4607 mutex_init(&ipst->ips_ire_handle_lock, NULL, MUTEX_DEFAULT, NULL); 4608 4609 (void) rn_inithead((void **)&ipst->ips_ip_ftable, 32); 4610 4611 4612 /* Calculate the IPv4 cache table size. */ 4613 ipst->ips_ip_cache_table_size = MAX(ip_cache_table_size, 4614 ((mem_avail >> ip_ire_mem_ratio) / sizeof (ire_t) / 4615 ip_ire_max_bucket_cnt)); 4616 if (ipst->ips_ip_cache_table_size > ip_max_cache_table_size) 4617 ipst->ips_ip_cache_table_size = ip_max_cache_table_size; 4618 /* 4619 * Make sure that the table size is always a power of 2. The 4620 * hash macro IRE_ADDR_HASH() depends on that. 4621 */ 4622 power2_roundup(&ipst->ips_ip_cache_table_size); 4623 4624 ipst->ips_ip_cache_table = kmem_zalloc(ipst->ips_ip_cache_table_size * 4625 sizeof (irb_t), KM_SLEEP); 4626 4627 for (i = 0; i < ipst->ips_ip_cache_table_size; i++) { 4628 rw_init(&ipst->ips_ip_cache_table[i].irb_lock, NULL, 4629 RW_DEFAULT, NULL); 4630 } 4631 4632 /* Calculate the IPv6 cache table size. */ 4633 ipst->ips_ip6_cache_table_size = MAX(ip6_cache_table_size, 4634 ((mem_avail >> ip_ire_mem_ratio) / sizeof (ire_t) / 4635 ip6_ire_max_bucket_cnt)); 4636 if (ipst->ips_ip6_cache_table_size > ip6_max_cache_table_size) 4637 ipst->ips_ip6_cache_table_size = ip6_max_cache_table_size; 4638 /* 4639 * Make sure that the table size is always a power of 2. The 4640 * hash macro IRE_ADDR_HASH_V6() depends on that. 4641 */ 4642 power2_roundup(&ipst->ips_ip6_cache_table_size); 4643 4644 ipst->ips_ip_cache_table_v6 = kmem_zalloc( 4645 ipst->ips_ip6_cache_table_size * sizeof (irb_t), KM_SLEEP); 4646 4647 for (i = 0; i < ipst->ips_ip6_cache_table_size; i++) { 4648 rw_init(&ipst->ips_ip_cache_table_v6[i].irb_lock, NULL, 4649 RW_DEFAULT, NULL); 4650 } 4651 4652 /* 4653 * Make sure that the forwarding table size is a power of 2. 4654 * The IRE*_ADDR_HASH() macroes depend on that. 4655 */ 4656 ipst->ips_ip6_ftable_hash_size = ip6_ftable_hash_size; 4657 power2_roundup(&ipst->ips_ip6_ftable_hash_size); 4658 4659 ipst->ips_ire_handle = 1; 4660 } 4661 4662 void 4663 ip_ire_g_fini(void) 4664 { 4665 kmem_cache_destroy(ire_cache); 4666 kmem_cache_destroy(rt_entry_cache); 4667 4668 rn_fini(); 4669 } 4670 4671 void 4672 ip_ire_fini(ip_stack_t *ipst) 4673 { 4674 int i; 4675 4676 /* 4677 * Delete all IREs - assumes that the ill/ipifs have 4678 * been removed so what remains are just the ftable and IRE_CACHE. 4679 */ 4680 ire_walk(ire_delete, NULL, ipst); 4681 4682 rn_freehead(ipst->ips_ip_ftable); 4683 ipst->ips_ip_ftable = NULL; 4684 4685 mutex_destroy(&ipst->ips_ire_ft_init_lock); 4686 mutex_destroy(&ipst->ips_ire_handle_lock); 4687 4688 for (i = 0; i < ipst->ips_ip_cache_table_size; i++) { 4689 ASSERT(ipst->ips_ip_cache_table[i].irb_ire == NULL); 4690 rw_destroy(&ipst->ips_ip_cache_table[i].irb_lock); 4691 } 4692 kmem_free(ipst->ips_ip_cache_table, 4693 ipst->ips_ip_cache_table_size * sizeof (irb_t)); 4694 ipst->ips_ip_cache_table = NULL; 4695 4696 for (i = 0; i < ipst->ips_ip6_cache_table_size; i++) { 4697 ASSERT(ipst->ips_ip_cache_table_v6[i].irb_ire == NULL); 4698 rw_destroy(&ipst->ips_ip_cache_table_v6[i].irb_lock); 4699 } 4700 kmem_free(ipst->ips_ip_cache_table_v6, 4701 ipst->ips_ip6_cache_table_size * sizeof (irb_t)); 4702 ipst->ips_ip_cache_table_v6 = NULL; 4703 4704 for (i = 0; i < IP6_MASK_TABLE_SIZE; i++) { 4705 irb_t *ptr; 4706 int j; 4707 4708 if ((ptr = ipst->ips_ip_forwarding_table_v6[i]) == NULL) 4709 continue; 4710 4711 for (j = 0; j < ipst->ips_ip6_ftable_hash_size; j++) { 4712 ASSERT(ptr[j].irb_ire == NULL); 4713 rw_destroy(&ptr[j].irb_lock); 4714 } 4715 mi_free(ptr); 4716 ipst->ips_ip_forwarding_table_v6[i] = NULL; 4717 } 4718 } 4719 4720 /* 4721 * Check if another multirt route resolution is needed. 4722 * B_TRUE is returned is there remain a resolvable route, 4723 * or if no route for that dst is resolved yet. 4724 * B_FALSE is returned if all routes for that dst are resolved 4725 * or if the remaining unresolved routes are actually not 4726 * resolvable. 4727 * This only works in the global zone. 4728 */ 4729 boolean_t 4730 ire_multirt_need_resolve(ipaddr_t dst, const ts_label_t *tsl, ip_stack_t *ipst) 4731 { 4732 ire_t *first_fire; 4733 ire_t *first_cire; 4734 ire_t *fire; 4735 ire_t *cire; 4736 irb_t *firb; 4737 irb_t *cirb; 4738 int unres_cnt = 0; 4739 boolean_t resolvable = B_FALSE; 4740 4741 /* Retrieve the first IRE_HOST that matches the destination */ 4742 first_fire = ire_ftable_lookup(dst, IP_HOST_MASK, 0, IRE_HOST, NULL, 4743 NULL, ALL_ZONES, 0, tsl, 4744 MATCH_IRE_MASK | MATCH_IRE_TYPE | MATCH_IRE_SECATTR, ipst); 4745 4746 /* No route at all */ 4747 if (first_fire == NULL) { 4748 return (B_TRUE); 4749 } 4750 4751 firb = first_fire->ire_bucket; 4752 ASSERT(firb != NULL); 4753 4754 /* Retrieve the first IRE_CACHE ire for that destination. */ 4755 first_cire = ire_cache_lookup(dst, GLOBAL_ZONEID, tsl, ipst); 4756 4757 /* No resolved route. */ 4758 if (first_cire == NULL) { 4759 ire_refrele(first_fire); 4760 return (B_TRUE); 4761 } 4762 4763 /* 4764 * At least one route is resolved. Here we look through the forward 4765 * and cache tables, to compare the number of declared routes 4766 * with the number of resolved routes. The search for a resolvable 4767 * route is performed only if at least one route remains 4768 * unresolved. 4769 */ 4770 cirb = first_cire->ire_bucket; 4771 ASSERT(cirb != NULL); 4772 4773 /* Count the number of routes to that dest that are declared. */ 4774 IRB_REFHOLD(firb); 4775 for (fire = first_fire; fire != NULL; fire = fire->ire_next) { 4776 if (!(fire->ire_flags & RTF_MULTIRT)) 4777 continue; 4778 if (fire->ire_addr != dst) 4779 continue; 4780 unres_cnt++; 4781 } 4782 IRB_REFRELE(firb); 4783 4784 /* Then subtract the number of routes to that dst that are resolved */ 4785 IRB_REFHOLD(cirb); 4786 for (cire = first_cire; cire != NULL; cire = cire->ire_next) { 4787 if (!(cire->ire_flags & RTF_MULTIRT)) 4788 continue; 4789 if (cire->ire_addr != dst) 4790 continue; 4791 if (cire->ire_marks & (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN)) 4792 continue; 4793 unres_cnt--; 4794 } 4795 IRB_REFRELE(cirb); 4796 4797 /* At least one route is unresolved; search for a resolvable route. */ 4798 if (unres_cnt > 0) 4799 resolvable = ire_multirt_lookup(&first_cire, &first_fire, 4800 MULTIRT_USESTAMP | MULTIRT_CACHEGW, tsl, ipst); 4801 4802 if (first_fire != NULL) 4803 ire_refrele(first_fire); 4804 4805 if (first_cire != NULL) 4806 ire_refrele(first_cire); 4807 4808 return (resolvable); 4809 } 4810 4811 4812 /* 4813 * Explore a forward_table bucket, starting from fire_arg. 4814 * fire_arg MUST be an IRE_HOST entry. 4815 * 4816 * Return B_TRUE and update *ire_arg and *fire_arg 4817 * if at least one resolvable route is found. *ire_arg 4818 * is the IRE entry for *fire_arg's gateway. 4819 * 4820 * Return B_FALSE otherwise (all routes are resolved or 4821 * the remaining unresolved routes are all unresolvable). 4822 * 4823 * The IRE selection relies on a priority mechanism 4824 * driven by the flags passed in by the caller. 4825 * The caller, such as ip_newroute_ipif(), can get the most 4826 * relevant ire at each stage of a multiple route resolution. 4827 * 4828 * The rules are: 4829 * 4830 * - if MULTIRT_CACHEGW is specified in flags, IRE_CACHETABLE 4831 * ires are preferred for the gateway. This gives the highest 4832 * priority to routes that can be resolved without using 4833 * a resolver. 4834 * 4835 * - if MULTIRT_CACHEGW is not specified, or if MULTIRT_CACHEGW 4836 * is specified but no IRE_CACHETABLE ire entry for the gateway 4837 * is found, the following rules apply. 4838 * 4839 * - if MULTIRT_USESTAMP is specified in flags, IRE_INTERFACE 4840 * ires for the gateway, that have not been tried since 4841 * a configurable amount of time, are preferred. 4842 * This applies when a resolver must be invoked for 4843 * a missing route, but we don't want to use the resolver 4844 * upon each packet emission. If no such resolver is found, 4845 * B_FALSE is returned. 4846 * The MULTIRT_USESTAMP flag can be combined with 4847 * MULTIRT_CACHEGW. 4848 * 4849 * - if MULTIRT_USESTAMP is not specified in flags, the first 4850 * unresolved but resolvable route is selected. 4851 * 4852 * - Otherwise, there is no resolvalble route, and 4853 * B_FALSE is returned. 4854 * 4855 * At last, MULTIRT_SETSTAMP can be specified in flags to 4856 * request the timestamp of unresolvable routes to 4857 * be refreshed. This prevents the useless exploration 4858 * of those routes for a while, when MULTIRT_USESTAMP is used. 4859 * 4860 * This only works in the global zone. 4861 */ 4862 boolean_t 4863 ire_multirt_lookup(ire_t **ire_arg, ire_t **fire_arg, uint32_t flags, 4864 const ts_label_t *tsl, ip_stack_t *ipst) 4865 { 4866 clock_t delta; 4867 ire_t *best_fire = NULL; 4868 ire_t *best_cire = NULL; 4869 ire_t *first_fire; 4870 ire_t *first_cire; 4871 ire_t *fire; 4872 ire_t *cire; 4873 irb_t *firb = NULL; 4874 irb_t *cirb = NULL; 4875 ire_t *gw_ire; 4876 boolean_t already_resolved; 4877 boolean_t res; 4878 ipaddr_t dst; 4879 ipaddr_t gw; 4880 4881 ip2dbg(("ire_multirt_lookup: *ire_arg %p, *fire_arg %p, flags %04x\n", 4882 (void *)*ire_arg, (void *)*fire_arg, flags)); 4883 4884 ASSERT(ire_arg != NULL); 4885 ASSERT(fire_arg != NULL); 4886 4887 /* Not an IRE_HOST ire; give up. */ 4888 if ((*fire_arg == NULL) || ((*fire_arg)->ire_type != IRE_HOST)) { 4889 return (B_FALSE); 4890 } 4891 4892 /* This is the first IRE_HOST ire for that destination. */ 4893 first_fire = *fire_arg; 4894 firb = first_fire->ire_bucket; 4895 ASSERT(firb != NULL); 4896 4897 dst = first_fire->ire_addr; 4898 4899 ip2dbg(("ire_multirt_lookup: dst %08x\n", ntohl(dst))); 4900 4901 /* 4902 * Retrieve the first IRE_CACHE ire for that destination; 4903 * if we don't find one, no route for that dest is 4904 * resolved yet. 4905 */ 4906 first_cire = ire_cache_lookup(dst, GLOBAL_ZONEID, tsl, ipst); 4907 if (first_cire != NULL) { 4908 cirb = first_cire->ire_bucket; 4909 } 4910 4911 ip2dbg(("ire_multirt_lookup: first_cire %p\n", (void *)first_cire)); 4912 4913 /* 4914 * Search for a resolvable route, giving the top priority 4915 * to routes that can be resolved without any call to the resolver. 4916 */ 4917 IRB_REFHOLD(firb); 4918 4919 if (!CLASSD(dst)) { 4920 /* 4921 * For all multiroute IRE_HOST ires for that destination, 4922 * check if the route via the IRE_HOST's gateway is 4923 * resolved yet. 4924 */ 4925 for (fire = first_fire; fire != NULL; fire = fire->ire_next) { 4926 4927 if (!(fire->ire_flags & RTF_MULTIRT)) 4928 continue; 4929 if (fire->ire_addr != dst) 4930 continue; 4931 4932 if (fire->ire_gw_secattr != NULL && 4933 tsol_ire_match_gwattr(fire, tsl) != 0) { 4934 continue; 4935 } 4936 4937 gw = fire->ire_gateway_addr; 4938 4939 ip2dbg(("ire_multirt_lookup: fire %p, " 4940 "ire_addr %08x, ire_gateway_addr %08x\n", 4941 (void *)fire, ntohl(fire->ire_addr), ntohl(gw))); 4942 4943 already_resolved = B_FALSE; 4944 4945 if (first_cire != NULL) { 4946 ASSERT(cirb != NULL); 4947 4948 IRB_REFHOLD(cirb); 4949 /* 4950 * For all IRE_CACHE ires for that 4951 * destination. 4952 */ 4953 for (cire = first_cire; 4954 cire != NULL; 4955 cire = cire->ire_next) { 4956 4957 if (!(cire->ire_flags & RTF_MULTIRT)) 4958 continue; 4959 if (cire->ire_addr != dst) 4960 continue; 4961 if (cire->ire_marks & 4962 (IRE_MARK_CONDEMNED | 4963 IRE_MARK_HIDDEN)) 4964 continue; 4965 4966 if (cire->ire_gw_secattr != NULL && 4967 tsol_ire_match_gwattr(cire, 4968 tsl) != 0) { 4969 continue; 4970 } 4971 4972 /* 4973 * Check if the IRE_CACHE's gateway 4974 * matches the IRE_HOST's gateway. 4975 */ 4976 if (cire->ire_gateway_addr == gw) { 4977 already_resolved = B_TRUE; 4978 break; 4979 } 4980 } 4981 IRB_REFRELE(cirb); 4982 } 4983 4984 /* 4985 * This route is already resolved; 4986 * proceed with next one. 4987 */ 4988 if (already_resolved) { 4989 ip2dbg(("ire_multirt_lookup: found cire %p, " 4990 "already resolved\n", (void *)cire)); 4991 continue; 4992 } 4993 4994 /* 4995 * The route is unresolved; is it actually 4996 * resolvable, i.e. is there a cache or a resolver 4997 * for the gateway? 4998 */ 4999 gw_ire = ire_route_lookup(gw, 0, 0, 0, NULL, NULL, 5000 ALL_ZONES, tsl, 5001 MATCH_IRE_RECURSIVE | MATCH_IRE_SECATTR, ipst); 5002 5003 ip2dbg(("ire_multirt_lookup: looked up gw_ire %p\n", 5004 (void *)gw_ire)); 5005 5006 /* 5007 * If gw_ire is typed IRE_CACHETABLE, 5008 * this route can be resolved without any call to the 5009 * resolver. If the MULTIRT_CACHEGW flag is set, 5010 * give the top priority to this ire and exit the 5011 * loop. 5012 * This is typically the case when an ARP reply 5013 * is processed through ip_wput_nondata(). 5014 */ 5015 if ((flags & MULTIRT_CACHEGW) && 5016 (gw_ire != NULL) && 5017 (gw_ire->ire_type & IRE_CACHETABLE)) { 5018 ASSERT(gw_ire->ire_nce == NULL || 5019 gw_ire->ire_nce->nce_state == ND_REACHABLE); 5020 /* 5021 * Release the resolver associated to the 5022 * previous candidate best ire, if any. 5023 */ 5024 if (best_cire != NULL) { 5025 ire_refrele(best_cire); 5026 ASSERT(best_fire != NULL); 5027 } 5028 5029 best_fire = fire; 5030 best_cire = gw_ire; 5031 5032 ip2dbg(("ire_multirt_lookup: found top prio " 5033 "best_fire %p, best_cire %p\n", 5034 (void *)best_fire, (void *)best_cire)); 5035 break; 5036 } 5037 5038 /* 5039 * Compute the time elapsed since our preceding 5040 * attempt to resolve that route. 5041 * If the MULTIRT_USESTAMP flag is set, we take that 5042 * route into account only if this time interval 5043 * exceeds ip_multirt_resolution_interval; 5044 * this prevents us from attempting to resolve a 5045 * broken route upon each sending of a packet. 5046 */ 5047 delta = lbolt - fire->ire_last_used_time; 5048 delta = TICK_TO_MSEC(delta); 5049 5050 res = (boolean_t)((delta > 5051 ipst->ips_ip_multirt_resolution_interval) || 5052 (!(flags & MULTIRT_USESTAMP))); 5053 5054 ip2dbg(("ire_multirt_lookup: fire %p, delta %lu, " 5055 "res %d\n", 5056 (void *)fire, delta, res)); 5057 5058 if (res) { 5059 /* 5060 * We are here if MULTIRT_USESTAMP flag is set 5061 * and the resolver for fire's gateway 5062 * has not been tried since 5063 * ip_multirt_resolution_interval, or if 5064 * MULTIRT_USESTAMP is not set but gw_ire did 5065 * not fill the conditions for MULTIRT_CACHEGW, 5066 * or if neither MULTIRT_USESTAMP nor 5067 * MULTIRT_CACHEGW are set. 5068 */ 5069 if (gw_ire != NULL) { 5070 if (best_fire == NULL) { 5071 ASSERT(best_cire == NULL); 5072 5073 best_fire = fire; 5074 best_cire = gw_ire; 5075 5076 ip2dbg(("ire_multirt_lookup:" 5077 "found candidate " 5078 "best_fire %p, " 5079 "best_cire %p\n", 5080 (void *)best_fire, 5081 (void *)best_cire)); 5082 5083 /* 5084 * If MULTIRT_CACHEGW is not 5085 * set, we ignore the top 5086 * priority ires that can 5087 * be resolved without any 5088 * call to the resolver; 5089 * In that case, there is 5090 * actually no need 5091 * to continue the loop. 5092 */ 5093 if (!(flags & 5094 MULTIRT_CACHEGW)) { 5095 break; 5096 } 5097 continue; 5098 } 5099 } else { 5100 /* 5101 * No resolver for the gateway: the 5102 * route is not resolvable. 5103 * If the MULTIRT_SETSTAMP flag is 5104 * set, we stamp the IRE_HOST ire, 5105 * so we will not select it again 5106 * during this resolution interval. 5107 */ 5108 if (flags & MULTIRT_SETSTAMP) 5109 fire->ire_last_used_time = 5110 lbolt; 5111 } 5112 } 5113 5114 if (gw_ire != NULL) 5115 ire_refrele(gw_ire); 5116 } 5117 } else { /* CLASSD(dst) */ 5118 5119 for (fire = first_fire; 5120 fire != NULL; 5121 fire = fire->ire_next) { 5122 5123 if (!(fire->ire_flags & RTF_MULTIRT)) 5124 continue; 5125 if (fire->ire_addr != dst) 5126 continue; 5127 5128 if (fire->ire_gw_secattr != NULL && 5129 tsol_ire_match_gwattr(fire, tsl) != 0) { 5130 continue; 5131 } 5132 5133 already_resolved = B_FALSE; 5134 5135 gw = fire->ire_gateway_addr; 5136 5137 gw_ire = ire_ftable_lookup(gw, 0, 0, IRE_INTERFACE, 5138 NULL, NULL, ALL_ZONES, 0, tsl, 5139 MATCH_IRE_RECURSIVE | MATCH_IRE_TYPE | 5140 MATCH_IRE_SECATTR, ipst); 5141 5142 /* No resolver for the gateway; we skip this ire. */ 5143 if (gw_ire == NULL) { 5144 continue; 5145 } 5146 ASSERT(gw_ire->ire_nce == NULL || 5147 gw_ire->ire_nce->nce_state == ND_REACHABLE); 5148 5149 if (first_cire != NULL) { 5150 5151 IRB_REFHOLD(cirb); 5152 /* 5153 * For all IRE_CACHE ires for that 5154 * destination. 5155 */ 5156 for (cire = first_cire; 5157 cire != NULL; 5158 cire = cire->ire_next) { 5159 5160 if (!(cire->ire_flags & RTF_MULTIRT)) 5161 continue; 5162 if (cire->ire_addr != dst) 5163 continue; 5164 if (cire->ire_marks & 5165 (IRE_MARK_CONDEMNED | 5166 IRE_MARK_HIDDEN)) 5167 continue; 5168 5169 if (cire->ire_gw_secattr != NULL && 5170 tsol_ire_match_gwattr(cire, 5171 tsl) != 0) { 5172 continue; 5173 } 5174 5175 /* 5176 * Cache entries are linked to the 5177 * parent routes using the parent handle 5178 * (ire_phandle). If no cache entry has 5179 * the same handle as fire, fire is 5180 * still unresolved. 5181 */ 5182 ASSERT(cire->ire_phandle != 0); 5183 if (cire->ire_phandle == 5184 fire->ire_phandle) { 5185 already_resolved = B_TRUE; 5186 break; 5187 } 5188 } 5189 IRB_REFRELE(cirb); 5190 } 5191 5192 /* 5193 * This route is already resolved; proceed with 5194 * next one. 5195 */ 5196 if (already_resolved) { 5197 ire_refrele(gw_ire); 5198 continue; 5199 } 5200 5201 /* 5202 * Compute the time elapsed since our preceding 5203 * attempt to resolve that route. 5204 * If the MULTIRT_USESTAMP flag is set, we take 5205 * that route into account only if this time 5206 * interval exceeds ip_multirt_resolution_interval; 5207 * this prevents us from attempting to resolve a 5208 * broken route upon each sending of a packet. 5209 */ 5210 delta = lbolt - fire->ire_last_used_time; 5211 delta = TICK_TO_MSEC(delta); 5212 5213 res = (boolean_t)((delta > 5214 ipst->ips_ip_multirt_resolution_interval) || 5215 (!(flags & MULTIRT_USESTAMP))); 5216 5217 ip3dbg(("ire_multirt_lookup: fire %p, delta %lx, " 5218 "flags %04x, res %d\n", 5219 (void *)fire, delta, flags, res)); 5220 5221 if (res) { 5222 if (best_cire != NULL) { 5223 /* 5224 * Release the resolver associated 5225 * to the preceding candidate best 5226 * ire, if any. 5227 */ 5228 ire_refrele(best_cire); 5229 ASSERT(best_fire != NULL); 5230 } 5231 best_fire = fire; 5232 best_cire = gw_ire; 5233 continue; 5234 } 5235 5236 ire_refrele(gw_ire); 5237 } 5238 } 5239 5240 if (best_fire != NULL) { 5241 IRE_REFHOLD(best_fire); 5242 } 5243 IRB_REFRELE(firb); 5244 5245 /* Release the first IRE_CACHE we initially looked up, if any. */ 5246 if (first_cire != NULL) 5247 ire_refrele(first_cire); 5248 5249 /* Found a resolvable route. */ 5250 if (best_fire != NULL) { 5251 ASSERT(best_cire != NULL); 5252 5253 if (*fire_arg != NULL) 5254 ire_refrele(*fire_arg); 5255 if (*ire_arg != NULL) 5256 ire_refrele(*ire_arg); 5257 5258 /* 5259 * Update the passed-in arguments with the 5260 * resolvable multirt route we found. 5261 */ 5262 *fire_arg = best_fire; 5263 *ire_arg = best_cire; 5264 5265 ip2dbg(("ire_multirt_lookup: returning B_TRUE, " 5266 "*fire_arg %p, *ire_arg %p\n", 5267 (void *)best_fire, (void *)best_cire)); 5268 5269 return (B_TRUE); 5270 } 5271 5272 ASSERT(best_cire == NULL); 5273 5274 ip2dbg(("ire_multirt_lookup: returning B_FALSE, *fire_arg %p, " 5275 "*ire_arg %p\n", 5276 (void *)*fire_arg, (void *)*ire_arg)); 5277 5278 /* No resolvable route. */ 5279 return (B_FALSE); 5280 } 5281 5282 /* 5283 * IRE iterator for inbound and loopback broadcast processing. 5284 * Given an IRE_BROADCAST ire, walk the ires with the same destination 5285 * address, but skip over the passed-in ire. Returns the next ire without 5286 * a hold - assumes that the caller holds a reference on the IRE bucket. 5287 */ 5288 ire_t * 5289 ire_get_next_bcast_ire(ire_t *curr, ire_t *ire) 5290 { 5291 ill_t *ill; 5292 5293 if (curr == NULL) { 5294 for (curr = ire->ire_bucket->irb_ire; curr != NULL; 5295 curr = curr->ire_next) { 5296 if (curr->ire_addr == ire->ire_addr) 5297 break; 5298 } 5299 } else { 5300 curr = curr->ire_next; 5301 } 5302 ill = ire_to_ill(ire); 5303 for (; curr != NULL; curr = curr->ire_next) { 5304 if (curr->ire_addr != ire->ire_addr) { 5305 /* 5306 * All the IREs to a given destination are contiguous; 5307 * break out once the address doesn't match. 5308 */ 5309 break; 5310 } 5311 if (curr == ire) { 5312 /* skip over the passed-in ire */ 5313 continue; 5314 } 5315 if ((curr->ire_stq != NULL && ire->ire_stq == NULL) || 5316 (curr->ire_stq == NULL && ire->ire_stq != NULL)) { 5317 /* 5318 * If the passed-in ire is loopback, skip over 5319 * non-loopback ires and vice versa. 5320 */ 5321 continue; 5322 } 5323 if (ire_to_ill(curr) != ill) { 5324 /* skip over IREs going through a different interface */ 5325 continue; 5326 } 5327 if (curr->ire_marks & IRE_MARK_CONDEMNED) { 5328 /* skip over deleted IREs */ 5329 continue; 5330 } 5331 return (curr); 5332 } 5333 return (NULL); 5334 } 5335 5336 #ifdef DEBUG 5337 void 5338 ire_trace_ref(ire_t *ire) 5339 { 5340 mutex_enter(&ire->ire_lock); 5341 if (ire->ire_trace_disable) { 5342 mutex_exit(&ire->ire_lock); 5343 return; 5344 } 5345 5346 if (th_trace_ref(ire, ire->ire_ipst)) { 5347 mutex_exit(&ire->ire_lock); 5348 } else { 5349 ire->ire_trace_disable = B_TRUE; 5350 mutex_exit(&ire->ire_lock); 5351 ire_trace_cleanup(ire); 5352 } 5353 } 5354 5355 void 5356 ire_untrace_ref(ire_t *ire) 5357 { 5358 mutex_enter(&ire->ire_lock); 5359 if (!ire->ire_trace_disable) 5360 th_trace_unref(ire); 5361 mutex_exit(&ire->ire_lock); 5362 } 5363 5364 static void 5365 ire_trace_cleanup(const ire_t *ire) 5366 { 5367 th_trace_cleanup(ire, ire->ire_trace_disable); 5368 } 5369 #endif /* DEBUG */ 5370 5371 /* 5372 * Generate a message chain with an arp request to resolve the in_ire. 5373 * It is assumed that in_ire itself is currently in the ire cache table, 5374 * so we create a fake_ire filled with enough information about ire_addr etc. 5375 * to retrieve in_ire when the DL_UNITDATA response from the resolver 5376 * comes back. The fake_ire itself is created by calling esballoc with 5377 * the fr_rtnp (free routine) set to ire_freemblk. This routine will be 5378 * invoked when the mblk containing fake_ire is freed. 5379 */ 5380 void 5381 ire_arpresolve(ire_t *in_ire, ill_t *dst_ill) 5382 { 5383 areq_t *areq; 5384 ipaddr_t *addrp; 5385 mblk_t *ire_mp, *areq_mp; 5386 ire_t *ire, *buf; 5387 size_t bufsize; 5388 frtn_t *frtnp; 5389 ill_t *ill; 5390 ip_stack_t *ipst = dst_ill->ill_ipst; 5391 5392 /* 5393 * Construct message chain for the resolver 5394 * of the form: 5395 * ARP_REQ_MBLK-->IRE_MBLK 5396 * 5397 * NOTE : If the response does not 5398 * come back, ARP frees the packet. For this reason, 5399 * we can't REFHOLD the bucket of save_ire to prevent 5400 * deletions. We may not be able to REFRELE the bucket 5401 * if the response never comes back. Thus, before 5402 * adding the ire, ire_add_v4 will make sure that the 5403 * interface route does not get deleted. This is the 5404 * only case unlike ip_newroute_v6, ip_newroute_ipif_v6 5405 * where we can always prevent deletions because of 5406 * the synchronous nature of adding IRES i.e 5407 * ire_add_then_send is called after creating the IRE. 5408 */ 5409 5410 /* 5411 * We use esballoc to allocate the second part(the ire_t size mblk) 5412 * of the message chain depicted above. THis mblk will be freed 5413 * by arp when there is a timeout, and otherwise passed to IP 5414 * and IP will * free it after processing the ARP response. 5415 */ 5416 5417 bufsize = sizeof (ire_t) + sizeof (frtn_t); 5418 buf = kmem_alloc(bufsize, KM_NOSLEEP); 5419 if (buf == NULL) { 5420 ip1dbg(("ire_arpresolver:alloc buffer failed\n ")); 5421 return; 5422 } 5423 frtnp = (frtn_t *)(buf + 1); 5424 frtnp->free_arg = (caddr_t)buf; 5425 frtnp->free_func = ire_freemblk; 5426 5427 ire_mp = esballoc((unsigned char *)buf, bufsize, BPRI_MED, frtnp); 5428 5429 if (ire_mp == NULL) { 5430 ip1dbg(("ire_arpresolve: esballoc failed\n")); 5431 kmem_free(buf, bufsize); 5432 return; 5433 } 5434 ASSERT(in_ire->ire_nce != NULL); 5435 areq_mp = copyb(dst_ill->ill_resolver_mp); 5436 if (areq_mp == NULL) { 5437 kmem_free(buf, bufsize); 5438 return; 5439 } 5440 5441 ire_mp->b_datap->db_type = IRE_ARPRESOLVE_TYPE; 5442 ire = (ire_t *)buf; 5443 /* 5444 * keep enough info in the fake ire so that we can pull up 5445 * the incomplete ire (in_ire) after result comes back from 5446 * arp and make it complete. 5447 */ 5448 *ire = ire_null; 5449 ire->ire_u = in_ire->ire_u; 5450 ire->ire_ipif_seqid = in_ire->ire_ipif_seqid; 5451 ire->ire_ipif = in_ire->ire_ipif; 5452 ire->ire_stq = in_ire->ire_stq; 5453 ill = ire_to_ill(ire); 5454 ire->ire_stq_ifindex = ill->ill_phyint->phyint_ifindex; 5455 ire->ire_zoneid = in_ire->ire_zoneid; 5456 ire->ire_stackid = ipst->ips_netstack->netstack_stackid; 5457 ire->ire_ipst = ipst; 5458 5459 /* 5460 * ire_freemblk will be called when ire_mp is freed, both for 5461 * successful and failed arp resolution. IRE_MARK_UNCACHED will be set 5462 * when the arp resolution failed. 5463 */ 5464 ire->ire_marks |= IRE_MARK_UNCACHED; 5465 ire->ire_mp = ire_mp; 5466 ire_mp->b_wptr = (uchar_t *)&ire[1]; 5467 ire_mp->b_cont = NULL; 5468 linkb(areq_mp, ire_mp); 5469 5470 /* 5471 * Fill in the source and dest addrs for the resolver. 5472 * NOTE: this depends on memory layouts imposed by 5473 * ill_init(). 5474 */ 5475 areq = (areq_t *)areq_mp->b_rptr; 5476 addrp = (ipaddr_t *)((char *)areq + areq->areq_sender_addr_offset); 5477 *addrp = ire->ire_src_addr; 5478 5479 addrp = (ipaddr_t *)((char *)areq + areq->areq_target_addr_offset); 5480 if (ire->ire_gateway_addr != INADDR_ANY) { 5481 *addrp = ire->ire_gateway_addr; 5482 } else { 5483 *addrp = ire->ire_addr; 5484 } 5485 5486 /* Up to the resolver. */ 5487 if (canputnext(dst_ill->ill_rq)) { 5488 putnext(dst_ill->ill_rq, areq_mp); 5489 } else { 5490 freemsg(areq_mp); 5491 } 5492 } 5493 5494 /* 5495 * Esballoc free function for AR_ENTRY_QUERY request to clean up any 5496 * unresolved ire_t and/or nce_t structures when ARP resolution fails. 5497 * 5498 * This function can be called by ARP via free routine for ire_mp or 5499 * by IPv4(both host and forwarding path) via ire_delete 5500 * in case ARP resolution fails. 5501 * NOTE: Since IP is MT, ARP can call into IP but not vice versa 5502 * (for IP to talk to ARP, it still has to send AR* messages). 5503 * 5504 * Note that the ARP/IP merge should replace the functioanlity by providing 5505 * direct function calls to clean up unresolved entries in ire/nce lists. 5506 */ 5507 5508 void 5509 ire_freemblk(ire_t *ire_mp) 5510 { 5511 nce_t *nce = NULL; 5512 ill_t *ill; 5513 ip_stack_t *ipst; 5514 netstack_t *ns = NULL; 5515 5516 ASSERT(ire_mp != NULL); 5517 5518 if ((ire_mp->ire_addr == NULL) && (ire_mp->ire_gateway_addr == NULL)) { 5519 ip1dbg(("ire_freemblk(0x%p) ire_addr is NULL\n", 5520 (void *)ire_mp)); 5521 goto cleanup; 5522 } 5523 if ((ire_mp->ire_marks & IRE_MARK_UNCACHED) == 0) { 5524 goto cleanup; /* everything succeeded. just free and return */ 5525 } 5526 5527 /* 5528 * the arp information corresponding to this ire_mp was not 5529 * transferred to an ire_cache entry. Need 5530 * to clean up incomplete ire's and nce, if necessary. 5531 */ 5532 ASSERT(ire_mp->ire_stq != NULL); 5533 ASSERT(ire_mp->ire_stq_ifindex != 0); 5534 ASSERT(ire_mp->ire_ipst != NULL); 5535 5536 ns = netstack_find_by_stackid(ire_mp->ire_stackid); 5537 ipst = (ns ? ns->netstack_ip : NULL); 5538 if (ipst == NULL || ipst != ire_mp->ire_ipst) /* Disapeared on us */ 5539 goto cleanup; 5540 5541 /* 5542 * Get any nce's corresponding to this ire_mp. We first have to 5543 * make sure that the ill is still around. 5544 */ 5545 ill = ill_lookup_on_ifindex(ire_mp->ire_stq_ifindex, 5546 B_FALSE, NULL, NULL, NULL, NULL, ipst); 5547 if (ill == NULL || (ire_mp->ire_stq != ill->ill_wq) || 5548 (ill->ill_state_flags & ILL_CONDEMNED)) { 5549 /* 5550 * ill went away. no nce to clean up. 5551 * Note that the ill_state_flags could be set to 5552 * ILL_CONDEMNED after this point, but if we know 5553 * that it is CONDEMNED now, we just bail out quickly. 5554 */ 5555 if (ill != NULL) 5556 ill_refrele(ill); 5557 goto cleanup; 5558 } 5559 nce = ndp_lookup_v4(ill, 5560 ((ire_mp->ire_gateway_addr != INADDR_ANY) ? 5561 &ire_mp->ire_gateway_addr : &ire_mp->ire_addr), 5562 B_FALSE); 5563 ill_refrele(ill); 5564 5565 if ((nce != NULL) && (nce->nce_state != ND_REACHABLE)) { 5566 /* 5567 * some incomplete nce was found. 5568 */ 5569 DTRACE_PROBE2(ire__freemblk__arp__resolv__fail, 5570 nce_t *, nce, ire_t *, ire_mp); 5571 /* 5572 * Send the icmp_unreachable messages for the queued mblks in 5573 * ire->ire_nce->nce_qd_mp, since ARP resolution failed 5574 * for this ire 5575 */ 5576 arp_resolv_failed(nce); 5577 /* 5578 * Delete the nce and clean up all ire's pointing at this nce 5579 * in the cachetable 5580 */ 5581 ndp_delete(nce); 5582 } 5583 if (nce != NULL) 5584 NCE_REFRELE(nce); /* release the ref taken by ndp_lookup_v4 */ 5585 5586 cleanup: 5587 if (ns != NULL) 5588 netstack_rele(ns); 5589 /* 5590 * Get rid of the ire buffer 5591 * We call kmem_free here(instead of ire_delete()), since 5592 * this is the freeb's callback. 5593 */ 5594 kmem_free(ire_mp, sizeof (ire_t) + sizeof (frtn_t)); 5595 } 5596 5597 /* 5598 * find, or create if needed, a neighbor cache entry nce_t for IRE_CACHE and 5599 * non-loopback IRE_BROADCAST ire's. 5600 * 5601 * If a neighbor-cache entry has to be created (i.e., one does not already 5602 * exist in the nce list) the nce_res_mp and nce_state of the neighbor cache 5603 * entry are initialized in ndp_add_v4(). These values are picked from 5604 * the src_nce, if one is passed in. Otherwise (if src_nce == NULL) the 5605 * ire->ire_type and the outgoing interface (ire_to_ill(ire)) values 5606 * determine the {nce_state, nce_res_mp} of the nce_t created. All 5607 * IRE_BROADCAST entries have nce_state = ND_REACHABLE, and the nce_res_mp 5608 * is set to the ill_bcast_mp of the outgoing inerface. For unicast ire 5609 * entries, 5610 * - if the outgoing interface is of type IRE_IF_RESOLVER, a newly created 5611 * nce_t will have a null nce_res_mp, and will be in the ND_INITIAL state. 5612 * - if the outgoing interface is a IRE_IF_NORESOLVER interface, no link 5613 * layer resolution is necessary, so that the nce_t will be in the 5614 * ND_REACHABLE state and the nce_res_mp will have a copy of the 5615 * ill_resolver_mp of the outgoing interface. 5616 * 5617 * The link layer information needed for broadcast addresses, and for 5618 * packets sent on IRE_IF_NORESOLVER interfaces is a constant mapping that 5619 * never needs re-verification for the lifetime of the nce_t. These are 5620 * therefore marked NCE_F_PERMANENT, and never allowed to expire via 5621 * NCE_EXPIRED. 5622 * 5623 * IRE_CACHE ire's contain the information for the nexthop (ire_gateway_addr) 5624 * in the case of indirect routes, and for the dst itself (ire_addr) in the 5625 * case of direct routes, with the nce_res_mp containing a template 5626 * DL_UNITDATA request. 5627 * 5628 * The actual association of the ire_nce to the nce created here is 5629 * typically done in ire_add_v4 for IRE_CACHE entries. Exceptions 5630 * to this rule are SO_DONTROUTE ire's (IRE_MARK_NO_ADD), for which 5631 * the ire_nce assignment is done in ire_add_then_send. 5632 */ 5633 int 5634 ire_nce_init(ire_t *ire, nce_t *src_nce) 5635 { 5636 in_addr_t addr4; 5637 int err; 5638 nce_t *nce = NULL; 5639 ill_t *ire_ill; 5640 uint16_t nce_flags = 0; 5641 ip_stack_t *ipst; 5642 5643 if (ire->ire_stq == NULL) 5644 return (0); /* no need to create nce for local/loopback */ 5645 5646 switch (ire->ire_type) { 5647 case IRE_CACHE: 5648 if (ire->ire_gateway_addr != INADDR_ANY) 5649 addr4 = ire->ire_gateway_addr; /* 'G' route */ 5650 else 5651 addr4 = ire->ire_addr; /* direct route */ 5652 break; 5653 case IRE_BROADCAST: 5654 addr4 = ire->ire_addr; 5655 nce_flags |= (NCE_F_PERMANENT|NCE_F_BCAST); 5656 break; 5657 default: 5658 return (0); 5659 } 5660 5661 /* 5662 * ire_ipif is picked based on RTF_SETSRC, usesrc etc. 5663 * rules in ire_forward_src_ipif. We want the dlureq_mp 5664 * for the outgoing interface, which we get from the ire_stq. 5665 */ 5666 ire_ill = ire_to_ill(ire); 5667 ipst = ire_ill->ill_ipst; 5668 5669 /* 5670 * IRE_IF_NORESOLVER entries never need re-verification and 5671 * do not expire, so we mark them as NCE_F_PERMANENT. 5672 */ 5673 if (ire_ill->ill_net_type == IRE_IF_NORESOLVER) 5674 nce_flags |= NCE_F_PERMANENT; 5675 5676 retry_nce: 5677 err = ndp_lookup_then_add_v4(ire_ill, &addr4, nce_flags, 5678 &nce, src_nce); 5679 5680 if (err == EEXIST && NCE_EXPIRED(nce, ipst)) { 5681 /* 5682 * We looked up an expired nce. 5683 * Go back and try to create one again. 5684 */ 5685 ndp_delete(nce); 5686 NCE_REFRELE(nce); 5687 nce = NULL; 5688 goto retry_nce; 5689 } 5690 5691 ip1dbg(("ire 0x%p addr 0x%lx type 0x%x; found nce 0x%p err %d\n", 5692 (void *)ire, (ulong_t)addr4, ire->ire_type, (void *)nce, err)); 5693 5694 switch (err) { 5695 case 0: 5696 case EEXIST: 5697 /* 5698 * return a pointer to a newly created or existing nce_t; 5699 * note that the ire-nce mapping is many-one, i.e., 5700 * multiple ire's could point to the same nce_t. 5701 */ 5702 break; 5703 default: 5704 DTRACE_PROBE2(nce__init__fail, ill_t *, ire_ill, int, err); 5705 return (EINVAL); 5706 } 5707 if (ire->ire_type == IRE_BROADCAST) { 5708 /* 5709 * Two bcast ires are created for each interface; 5710 * 1. loopback copy (which does not have an 5711 * ire_stq, and therefore has no ire_nce), and, 5712 * 2. the non-loopback copy, which has the nce_res_mp 5713 * initialized to a copy of the ill_bcast_mp, and 5714 * is marked as ND_REACHABLE at this point. 5715 * This nce does not undergo any further state changes, 5716 * and exists as long as the interface is plumbed. 5717 * Note: we do the ire_nce assignment here for IRE_BROADCAST 5718 * because some functions like ill_mark_bcast() inline the 5719 * ire_add functionality. 5720 */ 5721 ire->ire_nce = nce; 5722 /* 5723 * We are associating this nce to the ire, 5724 * so change the nce ref taken in 5725 * ndp_lookup_then_add_v4() from 5726 * NCE_REFHOLD to NCE_REFHOLD_NOTR 5727 */ 5728 NCE_REFHOLD_TO_REFHOLD_NOTR(ire->ire_nce); 5729 } else { 5730 /* 5731 * We are not using this nce_t just yet so release 5732 * the ref taken in ndp_lookup_then_add_v4() 5733 */ 5734 NCE_REFRELE(nce); 5735 } 5736 return (0); 5737 } 5738