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