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