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