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 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 /* 29 * This file contains consumer routines of the IPv4 forwarding engine 30 */ 31 32 #include <sys/types.h> 33 #include <sys/stream.h> 34 #include <sys/stropts.h> 35 #include <sys/strlog.h> 36 #include <sys/dlpi.h> 37 #include <sys/ddi.h> 38 #include <sys/cmn_err.h> 39 #include <sys/policy.h> 40 41 #include <sys/systm.h> 42 #include <sys/strsun.h> 43 #include <sys/kmem.h> 44 #include <sys/param.h> 45 #include <sys/socket.h> 46 #include <sys/strsubr.h> 47 #include <sys/pattr.h> 48 #include <net/if.h> 49 #include <net/route.h> 50 #include <netinet/in.h> 51 #include <net/if_dl.h> 52 #include <netinet/ip6.h> 53 #include <netinet/icmp6.h> 54 55 #include <inet/common.h> 56 #include <inet/mi.h> 57 #include <inet/mib2.h> 58 #include <inet/ip.h> 59 #include <inet/ip_impl.h> 60 #include <inet/ip6.h> 61 #include <inet/ip_ndp.h> 62 #include <inet/arp.h> 63 #include <inet/ip_if.h> 64 #include <inet/ip_ire.h> 65 #include <inet/ip_ftable.h> 66 #include <inet/ip_rts.h> 67 #include <inet/nd.h> 68 69 #include <net/pfkeyv2.h> 70 #include <inet/ipsec_info.h> 71 #include <inet/sadb.h> 72 #include <sys/kmem.h> 73 #include <inet/tcp.h> 74 #include <inet/ipclassifier.h> 75 #include <sys/zone.h> 76 #include <net/radix.h> 77 #include <sys/tsol/label.h> 78 #include <sys/tsol/tnet.h> 79 80 #define IS_DEFAULT_ROUTE(ire) \ 81 (((ire)->ire_type & IRE_DEFAULT) || \ 82 (((ire)->ire_type & IRE_INTERFACE) && ((ire)->ire_addr == 0))) 83 84 /* 85 * structure for passing args between ire_ftable_lookup and ire_find_best_route 86 */ 87 typedef struct ire_ftable_args_s { 88 ipaddr_t ift_addr; 89 ipaddr_t ift_mask; 90 ipaddr_t ift_gateway; 91 int ift_type; 92 const ipif_t *ift_ipif; 93 zoneid_t ift_zoneid; 94 uint32_t ift_ihandle; 95 const ts_label_t *ift_tsl; 96 int ift_flags; 97 ire_t *ift_best_ire; 98 } ire_ftable_args_t; 99 100 static ire_t *route_to_dst(const struct sockaddr *, zoneid_t, ip_stack_t *); 101 static ire_t *ire_round_robin(irb_t *, zoneid_t, ire_ftable_args_t *, 102 ip_stack_t *); 103 static void ire_del_host_redir(ire_t *, char *); 104 static boolean_t ire_find_best_route(struct radix_node *, void *); 105 static int ip_send_align_hcksum_flags(mblk_t *, ill_t *); 106 107 /* 108 * Lookup a route in forwarding table. A specific lookup is indicated by 109 * passing the required parameters and indicating the match required in the 110 * flag field. 111 * 112 * Looking for default route can be done in three ways 113 * 1) pass mask as 0 and set MATCH_IRE_MASK in flags field 114 * along with other matches. 115 * 2) pass type as IRE_DEFAULT and set MATCH_IRE_TYPE in flags 116 * field along with other matches. 117 * 3) if the destination and mask are passed as zeros. 118 * 119 * A request to return a default route if no route 120 * is found, can be specified by setting MATCH_IRE_DEFAULT 121 * in flags. 122 * 123 * It does not support recursion more than one level. It 124 * will do recursive lookup only when the lookup maps to 125 * a prefix or default route and MATCH_IRE_RECURSIVE flag is passed. 126 * 127 * If the routing table is setup to allow more than one level 128 * of recursion, the cleaning up cache table will not work resulting 129 * in invalid routing. 130 * 131 * Supports IP_BOUND_IF by following the ipif/ill when recursing. 132 * 133 * NOTE : When this function returns NULL, pire has already been released. 134 * pire is valid only when this function successfully returns an 135 * ire. 136 */ 137 ire_t * 138 ire_ftable_lookup(ipaddr_t addr, ipaddr_t mask, ipaddr_t gateway, 139 int type, const ipif_t *ipif, ire_t **pire, zoneid_t zoneid, 140 uint32_t ihandle, const ts_label_t *tsl, int flags, ip_stack_t *ipst) 141 { 142 ire_t *ire = NULL; 143 ipaddr_t gw_addr; 144 struct rt_sockaddr rdst, rmask; 145 struct rt_entry *rt; 146 ire_ftable_args_t margs; 147 boolean_t found_incomplete = B_FALSE; 148 149 ASSERT(ipif == NULL || !ipif->ipif_isv6); 150 ASSERT(!(flags & MATCH_IRE_WQ)); 151 152 /* 153 * When we return NULL from this function, we should make 154 * sure that *pire is NULL so that the callers will not 155 * wrongly REFRELE the pire. 156 */ 157 if (pire != NULL) 158 *pire = NULL; 159 /* 160 * ire_match_args() will dereference ipif MATCH_IRE_SRC or 161 * MATCH_IRE_ILL is set. 162 */ 163 if ((flags & (MATCH_IRE_SRC | MATCH_IRE_ILL | MATCH_IRE_ILL_GROUP)) && 164 (ipif == NULL)) 165 return (NULL); 166 167 (void) memset(&rdst, 0, sizeof (rdst)); 168 rdst.rt_sin_len = sizeof (rdst); 169 rdst.rt_sin_family = AF_INET; 170 rdst.rt_sin_addr.s_addr = addr; 171 172 (void) memset(&rmask, 0, sizeof (rmask)); 173 rmask.rt_sin_len = sizeof (rmask); 174 rmask.rt_sin_family = AF_INET; 175 rmask.rt_sin_addr.s_addr = mask; 176 177 (void) memset(&margs, 0, sizeof (margs)); 178 margs.ift_addr = addr; 179 margs.ift_mask = mask; 180 margs.ift_gateway = gateway; 181 margs.ift_type = type; 182 margs.ift_ipif = ipif; 183 margs.ift_zoneid = zoneid; 184 margs.ift_ihandle = ihandle; 185 margs.ift_tsl = tsl; 186 margs.ift_flags = flags; 187 188 /* 189 * The flags argument passed to ire_ftable_lookup may cause the 190 * search to return, not the longest matching prefix, but the 191 * "best matching prefix", i.e., the longest prefix that also 192 * satisfies constraints imposed via the permutation of flags 193 * passed in. To achieve this, we invoke ire_match_args() on 194 * each matching leaf in the radix tree. ire_match_args is 195 * invoked by the callback function ire_find_best_route() 196 * We hold the global tree lock in read mode when calling 197 * rn_match_args.Before dropping the global tree lock, ensure 198 * that the radix node can't be deleted by incrementing ire_refcnt. 199 */ 200 RADIX_NODE_HEAD_RLOCK(ipst->ips_ip_ftable); 201 rt = (struct rt_entry *)ipst->ips_ip_ftable->rnh_matchaddr_args(&rdst, 202 ipst->ips_ip_ftable, ire_find_best_route, &margs); 203 ire = margs.ift_best_ire; 204 RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable); 205 206 if (rt == NULL) { 207 return (NULL); 208 } else { 209 ASSERT(ire != NULL); 210 } 211 212 DTRACE_PROBE2(ire__found, ire_ftable_args_t *, &margs, ire_t *, ire); 213 214 if (!IS_DEFAULT_ROUTE(ire)) 215 goto found_ire_held; 216 /* 217 * If default route is found, see if default matching criteria 218 * are satisfied. 219 */ 220 if (flags & MATCH_IRE_MASK) { 221 /* 222 * we were asked to match a 0 mask, and came back with 223 * a default route. Ok to return it. 224 */ 225 goto found_default_ire; 226 } 227 if ((flags & MATCH_IRE_TYPE) && 228 (type & (IRE_DEFAULT | IRE_INTERFACE))) { 229 /* 230 * we were asked to match a default ire type. Ok to return it. 231 */ 232 goto found_default_ire; 233 } 234 if (flags & MATCH_IRE_DEFAULT) { 235 goto found_default_ire; 236 } 237 /* 238 * we found a default route, but default matching criteria 239 * are not specified and we are not explicitly looking for 240 * default. 241 */ 242 IRE_REFRELE(ire); 243 return (NULL); 244 found_default_ire: 245 /* 246 * round-robin only if we have more than one route in the bucket. 247 */ 248 if ((ire->ire_bucket->irb_ire_cnt > 1) && 249 IS_DEFAULT_ROUTE(ire) && 250 ((flags & (MATCH_IRE_DEFAULT | MATCH_IRE_MASK)) == 251 MATCH_IRE_DEFAULT)) { 252 ire_t *next_ire; 253 254 next_ire = ire_round_robin(ire->ire_bucket, zoneid, &margs, 255 ipst); 256 IRE_REFRELE(ire); 257 if (next_ire != NULL) { 258 ire = next_ire; 259 } else { 260 /* no route */ 261 return (NULL); 262 } 263 } 264 found_ire_held: 265 if ((flags & MATCH_IRE_RJ_BHOLE) && 266 (ire->ire_flags & (RTF_BLACKHOLE | RTF_REJECT))) { 267 return (ire); 268 } 269 /* 270 * At this point, IRE that was found must be an IRE_FORWARDTABLE 271 * type. If this is a recursive lookup and an IRE_INTERFACE type was 272 * found, return that. If it was some other IRE_FORWARDTABLE type of 273 * IRE (one of the prefix types), then it is necessary to fill in the 274 * parent IRE pointed to by pire, and then lookup the gateway address of 275 * the parent. For backwards compatiblity, if this lookup returns an 276 * IRE other than a IRE_CACHETABLE or IRE_INTERFACE, then one more level 277 * of lookup is done. 278 */ 279 if (flags & MATCH_IRE_RECURSIVE) { 280 ipif_t *gw_ipif; 281 int match_flags = MATCH_IRE_DSTONLY; 282 ire_t *save_ire; 283 284 if (ire->ire_type & IRE_INTERFACE) 285 return (ire); 286 if (pire != NULL) 287 *pire = ire; 288 /* 289 * If we can't find an IRE_INTERFACE or the caller has not 290 * asked for pire, we need to REFRELE the save_ire. 291 */ 292 save_ire = ire; 293 294 /* 295 * Currently MATCH_IRE_ILL is never used with 296 * (MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT) while 297 * sending out packets as MATCH_IRE_ILL is used only 298 * for communicating with on-link hosts. We can't assert 299 * that here as RTM_GET calls this function with 300 * MATCH_IRE_ILL | MATCH_IRE_DEFAULT | MATCH_IRE_RECURSIVE. 301 * We have already used the MATCH_IRE_ILL in determining 302 * the right prefix route at this point. To match the 303 * behavior of how we locate routes while sending out 304 * packets, we don't want to use MATCH_IRE_ILL below 305 * while locating the interface route. 306 * 307 * ire_ftable_lookup may end up with an incomplete IRE_CACHE 308 * entry for the gateway (i.e., one for which the 309 * ire_nce->nce_state is not yet ND_REACHABLE). If the caller 310 * has specified MATCH_IRE_COMPLETE, such entries will not 311 * be returned; instead, we return the IF_RESOLVER ire. 312 */ 313 if (ire->ire_ipif != NULL) 314 match_flags |= MATCH_IRE_ILL_GROUP; 315 316 ire = ire_route_lookup(ire->ire_gateway_addr, 0, 0, 0, 317 ire->ire_ipif, NULL, zoneid, tsl, match_flags, ipst); 318 DTRACE_PROBE2(ftable__route__lookup1, (ire_t *), ire, 319 (ire_t *), save_ire); 320 if (ire == NULL || 321 ((ire->ire_type & IRE_CACHE) && ire->ire_nce && 322 ire->ire_nce->nce_state != ND_REACHABLE && 323 (flags & MATCH_IRE_COMPLETE))) { 324 /* 325 * Do not release the parent ire if MATCH_IRE_PARENT 326 * is set. Also return it via ire. 327 */ 328 if (ire != NULL) { 329 ire_refrele(ire); 330 ire = NULL; 331 found_incomplete = B_TRUE; 332 } 333 if (flags & MATCH_IRE_PARENT) { 334 if (pire != NULL) { 335 /* 336 * Need an extra REFHOLD, if the parent 337 * ire is returned via both ire and 338 * pire. 339 */ 340 IRE_REFHOLD(save_ire); 341 } 342 ire = save_ire; 343 } else { 344 ire_refrele(save_ire); 345 if (pire != NULL) 346 *pire = NULL; 347 } 348 if (!found_incomplete) 349 return (ire); 350 } 351 if (ire->ire_type & (IRE_CACHETABLE | IRE_INTERFACE)) { 352 /* 353 * If the caller did not ask for pire, release 354 * it now. 355 */ 356 if (pire == NULL) { 357 ire_refrele(save_ire); 358 } 359 return (ire); 360 } 361 match_flags |= MATCH_IRE_TYPE; 362 gw_addr = ire->ire_gateway_addr; 363 gw_ipif = ire->ire_ipif; 364 ire_refrele(ire); 365 ire = ire_route_lookup(gw_addr, 0, 0, 366 (found_incomplete? IRE_INTERFACE : 367 (IRE_CACHETABLE | IRE_INTERFACE)), 368 gw_ipif, NULL, zoneid, tsl, match_flags, ipst); 369 DTRACE_PROBE2(ftable__route__lookup2, (ire_t *), ire, 370 (ire_t *), save_ire); 371 if (ire == NULL || 372 ((ire->ire_type & IRE_CACHE) && ire->ire_nce && 373 ire->ire_nce->nce_state != ND_REACHABLE && 374 (flags & MATCH_IRE_COMPLETE))) { 375 /* 376 * Do not release the parent ire if MATCH_IRE_PARENT 377 * is set. Also return it via ire. 378 */ 379 if (ire != NULL) { 380 ire_refrele(ire); 381 ire = NULL; 382 } 383 if (flags & MATCH_IRE_PARENT) { 384 if (pire != NULL) { 385 /* 386 * Need an extra REFHOLD, if the 387 * parent ire is returned via both 388 * ire and pire. 389 */ 390 IRE_REFHOLD(save_ire); 391 } 392 ire = save_ire; 393 } else { 394 ire_refrele(save_ire); 395 if (pire != NULL) 396 *pire = NULL; 397 } 398 return (ire); 399 } else if (pire == NULL) { 400 /* 401 * If the caller did not ask for pire, release 402 * it now. 403 */ 404 ire_refrele(save_ire); 405 } 406 return (ire); 407 } 408 ASSERT(pire == NULL || *pire == NULL); 409 return (ire); 410 } 411 412 413 /* 414 * Find an IRE_OFFSUBNET IRE entry for the multicast address 'group' 415 * that goes through 'ipif'. As a fallback, a route that goes through 416 * ipif->ipif_ill can be returned. 417 */ 418 ire_t * 419 ipif_lookup_multi_ire(ipif_t *ipif, ipaddr_t group) 420 { 421 ire_t *ire; 422 ire_t *save_ire = NULL; 423 ire_t *gw_ire; 424 irb_t *irb; 425 ipaddr_t gw_addr; 426 int match_flags = MATCH_IRE_TYPE | MATCH_IRE_ILL; 427 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst; 428 429 ASSERT(CLASSD(group)); 430 431 ire = ire_ftable_lookup(group, 0, 0, 0, NULL, NULL, ALL_ZONES, 0, 432 NULL, MATCH_IRE_DEFAULT, ipst); 433 434 if (ire == NULL) 435 return (NULL); 436 437 irb = ire->ire_bucket; 438 ASSERT(irb); 439 440 IRB_REFHOLD(irb); 441 ire_refrele(ire); 442 for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) { 443 if (ire->ire_addr != group || 444 ipif->ipif_zoneid != ire->ire_zoneid && 445 ire->ire_zoneid != ALL_ZONES) { 446 continue; 447 } 448 449 switch (ire->ire_type) { 450 case IRE_DEFAULT: 451 case IRE_PREFIX: 452 case IRE_HOST: 453 gw_addr = ire->ire_gateway_addr; 454 gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, 455 ipif, NULL, ALL_ZONES, 0, NULL, match_flags, ipst); 456 457 if (gw_ire != NULL) { 458 if (save_ire != NULL) { 459 ire_refrele(save_ire); 460 } 461 IRE_REFHOLD(ire); 462 if (gw_ire->ire_ipif == ipif) { 463 ire_refrele(gw_ire); 464 465 IRB_REFRELE(irb); 466 return (ire); 467 } 468 ire_refrele(gw_ire); 469 save_ire = ire; 470 } 471 break; 472 case IRE_IF_NORESOLVER: 473 case IRE_IF_RESOLVER: 474 if (ire->ire_ipif == ipif) { 475 if (save_ire != NULL) { 476 ire_refrele(save_ire); 477 } 478 IRE_REFHOLD(ire); 479 480 IRB_REFRELE(irb); 481 return (ire); 482 } 483 break; 484 } 485 } 486 IRB_REFRELE(irb); 487 488 return (save_ire); 489 } 490 491 /* 492 * Find an IRE_INTERFACE for the multicast group. 493 * Allows different routes for multicast addresses 494 * in the unicast routing table (akin to 224.0.0.0 but could be more specific) 495 * which point at different interfaces. This is used when IP_MULTICAST_IF 496 * isn't specified (when sending) and when IP_ADD_MEMBERSHIP doesn't 497 * specify the interface to join on. 498 * 499 * Supports IP_BOUND_IF by following the ipif/ill when recursing. 500 */ 501 ire_t * 502 ire_lookup_multi(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst) 503 { 504 ire_t *ire; 505 ipif_t *ipif = NULL; 506 int match_flags = MATCH_IRE_TYPE; 507 ipaddr_t gw_addr; 508 509 ire = ire_ftable_lookup(group, 0, 0, 0, NULL, NULL, zoneid, 510 0, NULL, MATCH_IRE_DEFAULT, ipst); 511 512 /* We search a resolvable ire in case of multirouting. */ 513 if ((ire != NULL) && (ire->ire_flags & RTF_MULTIRT)) { 514 ire_t *cire = NULL; 515 /* 516 * If the route is not resolvable, the looked up ire 517 * may be changed here. In that case, ire_multirt_lookup() 518 * IRE_REFRELE the original ire and change it. 519 */ 520 (void) ire_multirt_lookup(&cire, &ire, MULTIRT_CACHEGW, 521 NULL, ipst); 522 if (cire != NULL) 523 ire_refrele(cire); 524 } 525 if (ire == NULL) 526 return (NULL); 527 /* 528 * Make sure we follow ire_ipif. 529 * 530 * We need to determine the interface route through 531 * which the gateway will be reached. We don't really 532 * care which interface is picked if the interface is 533 * part of a group. 534 */ 535 if (ire->ire_ipif != NULL) { 536 ipif = ire->ire_ipif; 537 match_flags |= MATCH_IRE_ILL_GROUP; 538 } 539 540 switch (ire->ire_type) { 541 case IRE_DEFAULT: 542 case IRE_PREFIX: 543 case IRE_HOST: 544 gw_addr = ire->ire_gateway_addr; 545 ire_refrele(ire); 546 ire = ire_ftable_lookup(gw_addr, 0, 0, 547 IRE_INTERFACE, ipif, NULL, zoneid, 0, 548 NULL, match_flags, ipst); 549 return (ire); 550 case IRE_IF_NORESOLVER: 551 case IRE_IF_RESOLVER: 552 return (ire); 553 default: 554 ire_refrele(ire); 555 return (NULL); 556 } 557 } 558 559 /* 560 * Delete the passed in ire if the gateway addr matches 561 */ 562 void 563 ire_del_host_redir(ire_t *ire, char *gateway) 564 { 565 if ((ire->ire_flags & RTF_DYNAMIC) && 566 (ire->ire_gateway_addr == *(ipaddr_t *)gateway)) 567 ire_delete(ire); 568 } 569 570 /* 571 * Search for all HOST REDIRECT routes that are 572 * pointing at the specified gateway and 573 * delete them. This routine is called only 574 * when a default gateway is going away. 575 */ 576 void 577 ire_delete_host_redirects(ipaddr_t gateway, ip_stack_t *ipst) 578 { 579 struct rtfuncarg rtfarg; 580 581 (void) memset(&rtfarg, 0, sizeof (rtfarg)); 582 rtfarg.rt_func = ire_del_host_redir; 583 rtfarg.rt_arg = (void *)&gateway; 584 (void) ipst->ips_ip_ftable->rnh_walktree_mt(ipst->ips_ip_ftable, 585 rtfunc, &rtfarg, irb_refhold_rn, irb_refrele_rn); 586 } 587 588 struct ihandle_arg { 589 uint32_t ihandle; 590 ire_t *ire; 591 }; 592 593 static int 594 ire_ihandle_onlink_match(struct radix_node *rn, void *arg) 595 { 596 struct rt_entry *rt; 597 irb_t *irb; 598 ire_t *ire; 599 struct ihandle_arg *ih = arg; 600 601 rt = (struct rt_entry *)rn; 602 ASSERT(rt != NULL); 603 irb = &rt->rt_irb; 604 for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) { 605 if ((ire->ire_type & IRE_INTERFACE) && 606 (ire->ire_ihandle == ih->ihandle)) { 607 ih->ire = ire; 608 IRE_REFHOLD(ire); 609 return (1); 610 } 611 } 612 return (0); 613 } 614 615 /* 616 * Locate the interface ire that is tied to the cache ire 'cire' via 617 * cire->ire_ihandle. 618 * 619 * We are trying to create the cache ire for an onlink destn. or 620 * gateway in 'cire'. We are called from ire_add_v4() in the IRE_IF_RESOLVER 621 * case, after the ire has come back from ARP. 622 */ 623 ire_t * 624 ire_ihandle_lookup_onlink(ire_t *cire) 625 { 626 ire_t *ire; 627 int match_flags; 628 struct ihandle_arg ih; 629 ip_stack_t *ipst; 630 631 ASSERT(cire != NULL); 632 ipst = cire->ire_ipst; 633 634 /* 635 * We don't need to specify the zoneid to ire_ftable_lookup() below 636 * because the ihandle refers to an ipif which can be in only one zone. 637 */ 638 match_flags = MATCH_IRE_TYPE | MATCH_IRE_IHANDLE | MATCH_IRE_MASK; 639 /* 640 * We know that the mask of the interface ire equals cire->ire_cmask. 641 * (When ip_newroute() created 'cire' for an on-link destn. it set its 642 * cmask from the interface ire's mask) 643 */ 644 ire = ire_ftable_lookup(cire->ire_addr, cire->ire_cmask, 0, 645 IRE_INTERFACE, NULL, NULL, ALL_ZONES, cire->ire_ihandle, 646 NULL, match_flags, ipst); 647 if (ire != NULL) 648 return (ire); 649 /* 650 * If we didn't find an interface ire above, we can't declare failure. 651 * For backwards compatibility, we need to support prefix routes 652 * pointing to next hop gateways that are not on-link. 653 * 654 * In the resolver/noresolver case, ip_newroute() thinks it is creating 655 * the cache ire for an onlink destination in 'cire'. But 'cire' is 656 * not actually onlink, because ire_ftable_lookup() cheated it, by 657 * doing ire_route_lookup() twice and returning an interface ire. 658 * 659 * Eg. default - gw1 (line 1) 660 * gw1 - gw2 (line 2) 661 * gw2 - hme0 (line 3) 662 * 663 * In the above example, ip_newroute() tried to create the cache ire 664 * 'cire' for gw1, based on the interface route in line 3. The 665 * ire_ftable_lookup() above fails, because there is no interface route 666 * to reach gw1. (it is gw2). We fall thru below. 667 * 668 * Do a brute force search based on the ihandle in a subset of the 669 * forwarding tables, corresponding to cire->ire_cmask. Otherwise 670 * things become very complex, since we don't have 'pire' in this 671 * case. (Also note that this method is not possible in the offlink 672 * case because we don't know the mask) 673 */ 674 (void) memset(&ih, 0, sizeof (ih)); 675 ih.ihandle = cire->ire_ihandle; 676 (void) ipst->ips_ip_ftable->rnh_walktree_mt(ipst->ips_ip_ftable, 677 ire_ihandle_onlink_match, &ih, irb_refhold_rn, irb_refrele_rn); 678 return (ih.ire); 679 } 680 681 /* 682 * IRE iterator used by ire_ftable_lookup[_v6]() to process multiple default 683 * routes. Given a starting point in the hash list (ire_origin), walk the IREs 684 * in the bucket skipping default interface routes and deleted entries. 685 * Returns the next IRE (unheld), or NULL when we're back to the starting point. 686 * Assumes that the caller holds a reference on the IRE bucket. 687 */ 688 ire_t * 689 ire_get_next_default_ire(ire_t *ire, ire_t *ire_origin) 690 { 691 ASSERT(ire_origin->ire_bucket != NULL); 692 ASSERT(ire != NULL); 693 694 do { 695 ire = ire->ire_next; 696 if (ire == NULL) 697 ire = ire_origin->ire_bucket->irb_ire; 698 if (ire == ire_origin) 699 return (NULL); 700 } while ((ire->ire_type & IRE_INTERFACE) || 701 (ire->ire_marks & IRE_MARK_CONDEMNED)); 702 ASSERT(ire != NULL); 703 return (ire); 704 } 705 706 static ipif_t * 707 ire_forward_src_ipif(ipaddr_t dst, ire_t *sire, ire_t *ire, ill_t *dst_ill, 708 int zoneid, ushort_t *marks) 709 { 710 ipif_t *src_ipif; 711 ip_stack_t *ipst = dst_ill->ill_ipst; 712 713 /* 714 * Pick the best source address from dst_ill. 715 * 716 * 1) If it is part of a multipathing group, we would 717 * like to spread the inbound packets across different 718 * interfaces. ipif_select_source picks a random source 719 * across the different ills in the group. 720 * 721 * 2) If it is not part of a multipathing group, we try 722 * to pick the source address from the destination 723 * route. Clustering assumes that when we have multiple 724 * prefixes hosted on an interface, the prefix of the 725 * source address matches the prefix of the destination 726 * route. We do this only if the address is not 727 * DEPRECATED. 728 * 729 * 3) If the conn is in a different zone than the ire, we 730 * need to pick a source address from the right zone. 731 * 732 * NOTE : If we hit case (1) above, the prefix of the source 733 * address picked may not match the prefix of the 734 * destination routes prefix as ipif_select_source 735 * does not look at "dst" while picking a source 736 * address. 737 * If we want the same behavior as (2), we will need 738 * to change the behavior of ipif_select_source. 739 */ 740 741 if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) { 742 /* 743 * The RTF_SETSRC flag is set in the parent ire (sire). 744 * Check that the ipif matching the requested source 745 * address still exists. 746 */ 747 src_ipif = ipif_lookup_addr(sire->ire_src_addr, NULL, 748 zoneid, NULL, NULL, NULL, NULL, ipst); 749 return (src_ipif); 750 } 751 *marks |= IRE_MARK_USESRC_CHECK; 752 if ((dst_ill->ill_group != NULL) || 753 (ire->ire_ipif->ipif_flags & IPIF_DEPRECATED) || 754 (dst_ill->ill_usesrc_ifindex != 0)) { 755 src_ipif = ipif_select_source(dst_ill, dst, zoneid); 756 if (src_ipif == NULL) 757 return (NULL); 758 759 } else { 760 src_ipif = ire->ire_ipif; 761 ASSERT(src_ipif != NULL); 762 /* hold src_ipif for uniformity */ 763 ipif_refhold(src_ipif); 764 } 765 return (src_ipif); 766 } 767 768 /* 769 * This function is called by ip_rput_noire() and ip_fast_forward() 770 * to resolve the route of incoming packet that needs to be forwarded. 771 * If the ire of the nexthop is not already in the cachetable, this 772 * routine will insert it to the table, but won't trigger ARP resolution yet. 773 * Thus unlike ip_newroute, this function adds incomplete ires to 774 * the cachetable. ARP resolution for these ires are delayed until 775 * after all of the packet processing is completed and its ready to 776 * be sent out on the wire, Eventually, the packet transmit routine 777 * ip_xmit_v4() attempts to send a packet to the driver. If it finds 778 * that there is no link layer information, it will do the arp 779 * resolution and queue the packet in ire->ire_nce->nce_qd_mp and 780 * then send it out once the arp resolution is over 781 * (see ip_xmit_v4()->ire_arpresolve()). This scheme is similar to 782 * the model of BSD/SunOS 4 783 * 784 * In future, the insertion of incomplete ires in the cachetable should 785 * be implemented in hostpath as well, as doing so will greatly reduce 786 * the existing complexity for code paths that depend on the context of 787 * the sender (such as IPsec). 788 * 789 * Thus this scheme of adding incomplete ires in cachetable in forwarding 790 * path can be used as a template for simplifying the hostpath. 791 */ 792 793 ire_t * 794 ire_forward(ipaddr_t dst, boolean_t *check_multirt, ire_t *supplied_ire, 795 ire_t *supplied_sire, const struct ts_label_s *tsl, ip_stack_t *ipst) 796 { 797 ipaddr_t gw = 0; 798 ire_t *ire = NULL; 799 ire_t *sire = NULL, *save_ire; 800 ill_t *dst_ill = NULL; 801 int error; 802 zoneid_t zoneid; 803 ipif_t *src_ipif = NULL; 804 mblk_t *res_mp; 805 ushort_t ire_marks = 0; 806 tsol_gcgrp_t *gcgrp = NULL; 807 tsol_gcgrp_addr_t ga; 808 809 zoneid = GLOBAL_ZONEID; 810 811 if (supplied_ire != NULL) { 812 /* We have arrived here from ipfil_sendpkt */ 813 ire = supplied_ire; 814 sire = supplied_sire; 815 goto create_irecache; 816 } 817 818 ire = ire_ftable_lookup(dst, 0, 0, 0, NULL, &sire, zoneid, 0, 819 tsl, MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 820 MATCH_IRE_RJ_BHOLE | MATCH_IRE_PARENT|MATCH_IRE_SECATTR, ipst); 821 822 if (ire == NULL) { 823 ip_rts_change(RTM_MISS, dst, 0, 0, 0, 0, 0, 0, RTA_DST, ipst); 824 goto icmp_err_ret; 825 } 826 827 /* 828 * If we encounter CGTP, we should have the caller use 829 * ip_newroute to resolve multirt instead of this function. 830 * CGTP specs explicitly state that it can't be used with routers. 831 * This essentially prevents insertion of incomplete RTF_MULTIRT 832 * ires in cachetable. 833 */ 834 if (ipst->ips_ip_cgtp_filter && 835 ((ire->ire_flags & RTF_MULTIRT) || 836 ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)))) { 837 ip3dbg(("ire_forward: packet is to be multirouted- " 838 "handing it to ip_newroute\n")); 839 if (sire != NULL) 840 ire_refrele(sire); 841 ire_refrele(ire); 842 /* 843 * Inform caller about encountering of multirt so that 844 * ip_newroute() can be called. 845 */ 846 *check_multirt = B_TRUE; 847 return (NULL); 848 } 849 850 *check_multirt = B_FALSE; 851 852 /* 853 * Verify that the returned IRE does not have either 854 * the RTF_REJECT or RTF_BLACKHOLE flags set and that the IRE is 855 * either an IRE_CACHE, IRE_IF_NORESOLVER or IRE_IF_RESOLVER. 856 */ 857 if ((ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) || 858 (ire->ire_type & (IRE_CACHE | IRE_INTERFACE)) == 0) { 859 ip3dbg(("ire 0x%p is not cache/resolver/noresolver\n", 860 (void *)ire)); 861 goto icmp_err_ret; 862 } 863 864 /* 865 * If we already have a fully resolved IRE CACHE of the 866 * nexthop router, just hand over the cache entry 867 * and we are done. 868 */ 869 870 if (ire->ire_type & IRE_CACHE) { 871 872 /* 873 * If we are using this ire cache entry as a 874 * gateway to forward packets, chances are we 875 * will be using it again. So turn off 876 * the temporary flag, thus reducing its 877 * chances of getting deleted frequently. 878 */ 879 if (ire->ire_marks & IRE_MARK_TEMPORARY) { 880 irb_t *irb = ire->ire_bucket; 881 rw_enter(&irb->irb_lock, RW_WRITER); 882 ire->ire_marks &= ~IRE_MARK_TEMPORARY; 883 irb->irb_tmp_ire_cnt--; 884 rw_exit(&irb->irb_lock); 885 } 886 887 if (sire != NULL) { 888 UPDATE_OB_PKT_COUNT(sire); 889 sire->ire_last_used_time = lbolt; 890 ire_refrele(sire); 891 } 892 return (ire); 893 } 894 create_irecache: 895 /* 896 * Increment the ire_ob_pkt_count field for ire if it is an 897 * INTERFACE (IF_RESOLVER or IF_NORESOLVER) IRE type, and 898 * increment the same for the parent IRE, sire, if it is some 899 * sort of prefix IRE (which includes DEFAULT, PREFIX, and HOST). 900 */ 901 if ((ire->ire_type & IRE_INTERFACE) != 0) { 902 UPDATE_OB_PKT_COUNT(ire); 903 ire->ire_last_used_time = lbolt; 904 } 905 906 /* 907 * sire must be either IRE_CACHETABLE OR IRE_INTERFACE type 908 */ 909 if (sire != NULL) { 910 gw = sire->ire_gateway_addr; 911 ASSERT((sire->ire_type & 912 (IRE_CACHETABLE | IRE_INTERFACE)) == 0); 913 UPDATE_OB_PKT_COUNT(sire); 914 sire->ire_last_used_time = lbolt; 915 } 916 917 /* Obtain dst_ill */ 918 dst_ill = ip_newroute_get_dst_ill(ire->ire_ipif->ipif_ill); 919 if (dst_ill == NULL) { 920 ip2dbg(("ire_forward no dst ill; ire 0x%p\n", (void *)ire)); 921 goto icmp_err_ret; 922 } 923 924 ASSERT(src_ipif == NULL); 925 /* Now obtain the src_ipif */ 926 src_ipif = ire_forward_src_ipif(dst, sire, ire, dst_ill, 927 zoneid, &ire_marks); 928 if (src_ipif == NULL) 929 goto icmp_err_ret; 930 931 switch (ire->ire_type) { 932 case IRE_IF_NORESOLVER: 933 /* create ire_cache for ire_addr endpoint */ 934 case IRE_IF_RESOLVER: 935 /* 936 * We have the IRE_IF_RESOLVER of the nexthop gateway 937 * and now need to build a IRE_CACHE for it. 938 * In this case, we have the following : 939 * 940 * 1) src_ipif - used for getting a source address. 941 * 942 * 2) dst_ill - from which we derive ire_stq/ire_rfq. This 943 * means packets using the IRE_CACHE that we will build 944 * here will go out on dst_ill. 945 * 946 * 3) sire may or may not be NULL. But, the IRE_CACHE that is 947 * to be created will only be tied to the IRE_INTERFACE 948 * that was derived from the ire_ihandle field. 949 * 950 * If sire is non-NULL, it means the destination is 951 * off-link and we will first create the IRE_CACHE for the 952 * gateway. 953 */ 954 res_mp = dst_ill->ill_resolver_mp; 955 if (ire->ire_type == IRE_IF_RESOLVER && 956 (!OK_RESOLVER_MP(res_mp))) { 957 ire_refrele(ire); 958 ire = NULL; 959 goto out; 960 } 961 /* 962 * To be at this point in the code with a non-zero gw 963 * means that dst is reachable through a gateway that 964 * we have never resolved. By changing dst to the gw 965 * addr we resolve the gateway first. 966 */ 967 if (gw != INADDR_ANY) { 968 /* 969 * The source ipif that was determined above was 970 * relative to the destination address, not the 971 * gateway's. If src_ipif was not taken out of 972 * the IRE_IF_RESOLVER entry, we'll need to call 973 * ipif_select_source() again. 974 */ 975 if (src_ipif != ire->ire_ipif) { 976 ipif_refrele(src_ipif); 977 src_ipif = ipif_select_source(dst_ill, 978 gw, zoneid); 979 if (src_ipif == NULL) 980 goto icmp_err_ret; 981 } 982 dst = gw; 983 gw = INADDR_ANY; 984 } 985 /* 986 * dst has been set to the address of the nexthop. 987 * 988 * TSol note: get security attributes of the nexthop; 989 * Note that the nexthop may either be a gateway, or the 990 * packet destination itself; Detailed explanation of 991 * issues involved is provided in the IRE_IF_NORESOLVER 992 * logic in ip_newroute(). 993 */ 994 ga.ga_af = AF_INET; 995 IN6_IPADDR_TO_V4MAPPED(dst, &ga.ga_addr); 996 gcgrp = gcgrp_lookup(&ga, B_FALSE); 997 998 if (ire->ire_type == IRE_IF_NORESOLVER) 999 dst = ire->ire_addr; /* ire_cache for tunnel endpoint */ 1000 1001 save_ire = ire; 1002 /* 1003 * create an incomplete IRE_CACHE. 1004 * An areq_mp will be generated in ire_arpresolve() for 1005 * RESOLVER interfaces. 1006 */ 1007 ire = ire_create( 1008 (uchar_t *)&dst, /* dest address */ 1009 (uchar_t *)&ip_g_all_ones, /* mask */ 1010 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 1011 (uchar_t *)&gw, /* gateway address */ 1012 (save_ire->ire_type == IRE_IF_RESOLVER ? NULL: 1013 &save_ire->ire_max_frag), 1014 NULL, 1015 dst_ill->ill_rq, /* recv-from queue */ 1016 dst_ill->ill_wq, /* send-to queue */ 1017 IRE_CACHE, /* IRE type */ 1018 src_ipif, 1019 ire->ire_mask, /* Parent mask */ 1020 0, 1021 ire->ire_ihandle, /* Interface handle */ 1022 0, 1023 &(ire->ire_uinfo), 1024 NULL, 1025 gcgrp, 1026 ipst); 1027 ip1dbg(("incomplete ire_cache 0x%p\n", (void *)ire)); 1028 if (ire != NULL) { 1029 gcgrp = NULL; /* reference now held by IRE */ 1030 ire->ire_marks |= ire_marks; 1031 /* add the incomplete ire: */ 1032 error = ire_add(&ire, NULL, NULL, NULL, B_TRUE); 1033 if (error == 0 && ire != NULL) { 1034 ire->ire_max_frag = save_ire->ire_max_frag; 1035 ip1dbg(("setting max_frag to %d in ire 0x%p\n", 1036 ire->ire_max_frag, (void *)ire)); 1037 } else { 1038 ire_refrele(save_ire); 1039 goto icmp_err_ret; 1040 } 1041 } else { 1042 if (gcgrp != NULL) { 1043 GCGRP_REFRELE(gcgrp); 1044 gcgrp = NULL; 1045 } 1046 } 1047 1048 ire_refrele(save_ire); 1049 break; 1050 default: 1051 break; 1052 } 1053 1054 out: 1055 if (sire != NULL) 1056 ire_refrele(sire); 1057 if (dst_ill != NULL) 1058 ill_refrele(dst_ill); 1059 if (src_ipif != NULL) 1060 ipif_refrele(src_ipif); 1061 return (ire); 1062 icmp_err_ret: 1063 if (src_ipif != NULL) 1064 ipif_refrele(src_ipif); 1065 if (dst_ill != NULL) 1066 ill_refrele(dst_ill); 1067 if (sire != NULL) 1068 ire_refrele(sire); 1069 if (ire != NULL) { 1070 ire_refrele(ire); 1071 } 1072 /* caller needs to send icmp error message */ 1073 return (NULL); 1074 1075 } 1076 1077 /* 1078 * Obtain the rt_entry and rt_irb for the route to be added to 1079 * the ips_ip_ftable. 1080 * First attempt to add a node to the radix tree via rn_addroute. If the 1081 * route already exists, return the bucket for the existing route. 1082 * 1083 * Locking notes: Need to hold the global radix tree lock in write mode to 1084 * add a radix node. To prevent the node from being deleted, ire_get_bucket() 1085 * returns with a ref'ed irb_t. The ire itself is added in ire_add_v4() 1086 * while holding the irb_lock, but not the radix tree lock. 1087 */ 1088 irb_t * 1089 ire_get_bucket(ire_t *ire) 1090 { 1091 struct radix_node *rn; 1092 struct rt_entry *rt; 1093 struct rt_sockaddr rmask, rdst; 1094 irb_t *irb = NULL; 1095 ip_stack_t *ipst = ire->ire_ipst; 1096 1097 ASSERT(ipst->ips_ip_ftable != NULL); 1098 1099 /* first try to see if route exists (based on rtalloc1) */ 1100 (void) memset(&rdst, 0, sizeof (rdst)); 1101 rdst.rt_sin_len = sizeof (rdst); 1102 rdst.rt_sin_family = AF_INET; 1103 rdst.rt_sin_addr.s_addr = ire->ire_addr; 1104 1105 (void) memset(&rmask, 0, sizeof (rmask)); 1106 rmask.rt_sin_len = sizeof (rmask); 1107 rmask.rt_sin_family = AF_INET; 1108 rmask.rt_sin_addr.s_addr = ire->ire_mask; 1109 1110 /* 1111 * add the route. based on BSD's rtrequest1(RTM_ADD) 1112 */ 1113 R_Malloc(rt, rt_entry_cache, sizeof (*rt)); 1114 (void) memset(rt, 0, sizeof (*rt)); 1115 rt->rt_nodes->rn_key = (char *)&rt->rt_dst; 1116 rt->rt_dst = rdst; 1117 irb = &rt->rt_irb; 1118 irb->irb_marks |= IRB_MARK_FTABLE; /* dynamically allocated/freed */ 1119 irb->irb_ipst = ipst; 1120 rw_init(&irb->irb_lock, NULL, RW_DEFAULT, NULL); 1121 RADIX_NODE_HEAD_WLOCK(ipst->ips_ip_ftable); 1122 rn = ipst->ips_ip_ftable->rnh_addaddr(&rt->rt_dst, &rmask, 1123 ipst->ips_ip_ftable, (struct radix_node *)rt); 1124 if (rn == NULL) { 1125 RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable); 1126 Free(rt, rt_entry_cache); 1127 rt = NULL; 1128 irb = NULL; 1129 RADIX_NODE_HEAD_RLOCK(ipst->ips_ip_ftable); 1130 rn = ipst->ips_ip_ftable->rnh_lookup(&rdst, &rmask, 1131 ipst->ips_ip_ftable); 1132 if (rn != NULL && ((rn->rn_flags & RNF_ROOT) == 0)) { 1133 /* found a non-root match */ 1134 rt = (struct rt_entry *)rn; 1135 } 1136 } 1137 if (rt != NULL) { 1138 irb = &rt->rt_irb; 1139 IRB_REFHOLD(irb); 1140 } 1141 RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable); 1142 return (irb); 1143 } 1144 1145 /* 1146 * This function is used when the caller wants to know the outbound 1147 * interface for a packet given only the address. 1148 * If this is a offlink IP address and there are multiple 1149 * routes to this destination, this routine will utilise the 1150 * first route it finds to IP address 1151 * Return values: 1152 * 0 - FAILURE 1153 * nonzero - ifindex 1154 */ 1155 uint_t 1156 ifindex_lookup(const struct sockaddr *ipaddr, zoneid_t zoneid) 1157 { 1158 uint_t ifindex = 0; 1159 ire_t *ire; 1160 ill_t *ill; 1161 netstack_t *ns; 1162 ip_stack_t *ipst; 1163 1164 if (zoneid == ALL_ZONES) 1165 ns = netstack_find_by_zoneid(GLOBAL_ZONEID); 1166 else 1167 ns = netstack_find_by_zoneid(zoneid); 1168 ASSERT(ns != NULL); 1169 1170 /* 1171 * For exclusive stacks we set the zoneid to zero 1172 * since IP uses the global zoneid in the exclusive stacks. 1173 */ 1174 if (ns->netstack_stackid != GLOBAL_NETSTACKID) 1175 zoneid = GLOBAL_ZONEID; 1176 ipst = ns->netstack_ip; 1177 1178 ASSERT(ipaddr->sa_family == AF_INET || ipaddr->sa_family == AF_INET6); 1179 1180 if ((ire = route_to_dst(ipaddr, zoneid, ipst)) != NULL) { 1181 ill = ire_to_ill(ire); 1182 if (ill != NULL) 1183 ifindex = ill->ill_phyint->phyint_ifindex; 1184 ire_refrele(ire); 1185 } 1186 netstack_rele(ns); 1187 return (ifindex); 1188 } 1189 1190 /* 1191 * Routine to find the route to a destination. If a ifindex is supplied 1192 * it tries to match the the route to the corresponding ipif for the ifindex 1193 */ 1194 static ire_t * 1195 route_to_dst(const struct sockaddr *dst_addr, zoneid_t zoneid, ip_stack_t *ipst) 1196 { 1197 ire_t *ire = NULL; 1198 int match_flags; 1199 1200 match_flags = (MATCH_IRE_DSTONLY | MATCH_IRE_DEFAULT | 1201 MATCH_IRE_RECURSIVE | MATCH_IRE_RJ_BHOLE); 1202 1203 /* XXX pass NULL tsl for now */ 1204 1205 if (dst_addr->sa_family == AF_INET) { 1206 ire = ire_route_lookup( 1207 ((struct sockaddr_in *)dst_addr)->sin_addr.s_addr, 1208 0, 0, 0, NULL, NULL, zoneid, NULL, match_flags, ipst); 1209 } else { 1210 ire = ire_route_lookup_v6( 1211 &((struct sockaddr_in6 *)dst_addr)->sin6_addr, 1212 0, 0, 0, NULL, NULL, zoneid, NULL, match_flags, ipst); 1213 } 1214 return (ire); 1215 } 1216 1217 /* 1218 * This routine is called by IP Filter to send a packet out on the wire 1219 * to a specified V4 dst (which may be onlink or offlink). The ifindex may or 1220 * may not be 0. A non-null ifindex indicates IP Filter has stipulated 1221 * an outgoing interface and requires the nexthop to be on that interface. 1222 * IP WILL NOT DO the following to the data packet before sending it out: 1223 * a. manipulate ttl 1224 * b. ipsec work 1225 * c. fragmentation 1226 * 1227 * If the packet has been prepared for hardware checksum then it will be 1228 * passed off to ip_send_align_cksum() to check that the flags set on the 1229 * packet are in alignment with the capabilities of the new outgoing NIC. 1230 * 1231 * Return values: 1232 * 0: IP was able to send of the data pkt 1233 * ECOMM: Could not send packet 1234 * ENONET No route to dst. It is up to the caller 1235 * to send icmp unreachable error message, 1236 * EINPROGRESS The macaddr of the onlink dst or that 1237 * of the offlink dst's nexthop needs to get 1238 * resolved before packet can be sent to dst. 1239 * Thus transmission is not guaranteed. 1240 * 1241 */ 1242 1243 int 1244 ipfil_sendpkt(const struct sockaddr *dst_addr, mblk_t *mp, uint_t ifindex, 1245 zoneid_t zoneid) 1246 { 1247 ire_t *ire = NULL, *sire = NULL; 1248 ire_t *ire_cache = NULL; 1249 boolean_t check_multirt = B_FALSE; 1250 int value; 1251 int match_flags; 1252 ipaddr_t dst; 1253 netstack_t *ns; 1254 ip_stack_t *ipst; 1255 1256 ASSERT(mp != NULL); 1257 1258 if (zoneid == ALL_ZONES) 1259 ns = netstack_find_by_zoneid(GLOBAL_ZONEID); 1260 else 1261 ns = netstack_find_by_zoneid(zoneid); 1262 ASSERT(ns != NULL); 1263 1264 /* 1265 * For exclusive stacks we set the zoneid to zero 1266 * since IP uses the global zoneid in the exclusive stacks. 1267 */ 1268 if (ns->netstack_stackid != GLOBAL_NETSTACKID) 1269 zoneid = GLOBAL_ZONEID; 1270 ipst = ns->netstack_ip; 1271 1272 ASSERT(dst_addr->sa_family == AF_INET || 1273 dst_addr->sa_family == AF_INET6); 1274 1275 if (dst_addr->sa_family == AF_INET) { 1276 dst = ((struct sockaddr_in *)dst_addr)->sin_addr.s_addr; 1277 } else { 1278 /* 1279 * We dont have support for V6 yet. It will be provided 1280 * once RFE 6399103 has been delivered. 1281 * Until then, for V6 dsts, IP Filter will not call 1282 * this function. Instead the netinfo framework provides 1283 * its own code path, in ip_inject_impl(), to achieve 1284 * what it needs to do, for the time being. 1285 */ 1286 ip1dbg(("ipfil_sendpkt: no V6 support \n")); 1287 value = ECOMM; 1288 freemsg(mp); 1289 goto discard; 1290 } 1291 1292 /* 1293 * Lets get the ire. We might get the ire cache entry, 1294 * or the ire,sire pair needed to create the cache entry. 1295 * XXX pass NULL tsl for now. 1296 */ 1297 1298 if (ifindex == 0) { 1299 /* There is no supplied index. So use the FIB info */ 1300 1301 match_flags = (MATCH_IRE_DSTONLY | MATCH_IRE_DEFAULT | 1302 MATCH_IRE_RECURSIVE | MATCH_IRE_RJ_BHOLE); 1303 ire = ire_route_lookup(dst, 1304 0, 0, 0, NULL, &sire, zoneid, MBLK_GETLABEL(mp), 1305 match_flags, ipst); 1306 } else { 1307 ipif_t *supplied_ipif; 1308 ill_t *ill; 1309 1310 match_flags = (MATCH_IRE_DSTONLY | MATCH_IRE_DEFAULT | 1311 MATCH_IRE_RECURSIVE| MATCH_IRE_RJ_BHOLE| 1312 MATCH_IRE_SECATTR); 1313 1314 /* 1315 * If supplied ifindex is non-null, the only valid 1316 * nexthop is one off of the interface or group corresponding 1317 * to the specified ifindex. 1318 */ 1319 ill = ill_lookup_on_ifindex(ifindex, B_FALSE, 1320 NULL, NULL, NULL, NULL, ipst); 1321 if (ill != NULL) { 1322 match_flags |= MATCH_IRE_ILL; 1323 } else { 1324 /* Fallback to group names if hook_emulation set */ 1325 if (ipst->ips_ipmp_hook_emulation) { 1326 ill = ill_group_lookup_on_ifindex(ifindex, 1327 B_FALSE, ipst); 1328 } 1329 if (ill == NULL) { 1330 ip1dbg(("ipfil_sendpkt: Could not find" 1331 " route to dst\n")); 1332 value = ECOMM; 1333 freemsg(mp); 1334 goto discard; 1335 } 1336 match_flags |= MATCH_IRE_ILL_GROUP; 1337 } 1338 supplied_ipif = ipif_get_next_ipif(NULL, ill); 1339 1340 ire = ire_route_lookup(dst, 0, 0, 0, supplied_ipif, 1341 &sire, zoneid, MBLK_GETLABEL(mp), match_flags, ipst); 1342 ipif_refrele(supplied_ipif); 1343 ill_refrele(ill); 1344 } 1345 1346 /* 1347 * Verify that the returned IRE is non-null and does 1348 * not have either the RTF_REJECT or RTF_BLACKHOLE 1349 * flags set and that the IRE is either an IRE_CACHE, 1350 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER. 1351 */ 1352 if (ire == NULL || 1353 ((ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) || 1354 (ire->ire_type & (IRE_CACHE | IRE_INTERFACE)) == 0)) { 1355 /* 1356 * Either ire could not be found or we got 1357 * an invalid one 1358 */ 1359 ip1dbg(("ipfil_sendpkt: Could not find route to dst\n")); 1360 value = ENONET; 1361 freemsg(mp); 1362 goto discard; 1363 } 1364 1365 /* IP Filter and CGTP dont mix. So bail out if CGTP is on */ 1366 if (ipst->ips_ip_cgtp_filter && 1367 ((ire->ire_flags & RTF_MULTIRT) || 1368 ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)))) { 1369 ip1dbg(("ipfil_sendpkt: IPFilter does not work with CGTP\n")); 1370 value = ECOMM; 1371 freemsg(mp); 1372 goto discard; 1373 } 1374 1375 ASSERT(ire->ire_type != IRE_CACHE || ire->ire_nce != NULL); 1376 1377 /* 1378 * If needed, we will create the ire cache entry for the 1379 * nexthop, resolve its link-layer address and then send 1380 * the packet out without ttl or IPSec processing. 1381 */ 1382 switch (ire->ire_type) { 1383 case IRE_IF_NORESOLVER: 1384 case IRE_CACHE: 1385 if (sire != NULL) { 1386 UPDATE_OB_PKT_COUNT(sire); 1387 sire->ire_last_used_time = lbolt; 1388 ire_refrele(sire); 1389 } 1390 ire_cache = ire; 1391 break; 1392 case IRE_IF_RESOLVER: 1393 /* 1394 * Call ire_forward(). This function 1395 * will, create the ire cache entry of the 1396 * the nexthop and adds this incomplete ire 1397 * to the ire cache table 1398 */ 1399 ire_cache = ire_forward(dst, &check_multirt, ire, sire, 1400 MBLK_GETLABEL(mp), ipst); 1401 if (ire_cache == NULL) { 1402 ip1dbg(("ipfil_sendpkt: failed to create the" 1403 " ire cache entry \n")); 1404 value = ENONET; 1405 freemsg(mp); 1406 sire = NULL; 1407 ire = NULL; 1408 goto discard; 1409 } 1410 break; 1411 } 1412 1413 if (DB_CKSUMFLAGS(mp)) { 1414 if (ip_send_align_hcksum_flags(mp, ire_to_ill(ire_cache))) 1415 goto cleanup; 1416 } 1417 1418 /* 1419 * Now that we have the ire cache entry of the nexthop, call 1420 * ip_xmit_v4() to trigger mac addr resolution 1421 * if necessary and send it once ready. 1422 */ 1423 1424 value = ip_xmit_v4(mp, ire_cache, NULL, B_FALSE); 1425 cleanup: 1426 ire_refrele(ire_cache); 1427 /* 1428 * At this point, the reference for these have already been 1429 * released within ire_forward() and/or ip_xmit_v4(). So we set 1430 * them to NULL to make sure we dont drop the references 1431 * again in case ip_xmit_v4() returns with either SEND_FAILED 1432 * or LLHDR_RESLV_FAILED 1433 */ 1434 sire = NULL; 1435 ire = NULL; 1436 1437 switch (value) { 1438 case SEND_FAILED: 1439 ip1dbg(("ipfil_sendpkt: Send failed\n")); 1440 value = ECOMM; 1441 break; 1442 case LLHDR_RESLV_FAILED: 1443 ip1dbg(("ipfil_sendpkt: Link-layer resolution" 1444 " failed\n")); 1445 value = ECOMM; 1446 break; 1447 case LOOKUP_IN_PROGRESS: 1448 netstack_rele(ns); 1449 return (EINPROGRESS); 1450 case SEND_PASSED: 1451 netstack_rele(ns); 1452 return (0); 1453 } 1454 discard: 1455 if (dst_addr->sa_family == AF_INET) { 1456 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 1457 } else { 1458 BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsOutDiscards); 1459 } 1460 if (ire != NULL) 1461 ire_refrele(ire); 1462 if (sire != NULL) 1463 ire_refrele(sire); 1464 netstack_rele(ns); 1465 return (value); 1466 } 1467 1468 1469 /* 1470 * We don't check for dohwcksum in here because it should be being used 1471 * elsewhere to control what flags are being set on the mblk. That is, 1472 * if DB_CKSUMFLAGS() is non-zero then we assume dohwcksum to be true 1473 * for this packet. 1474 * 1475 * This function assumes that it is *only* being called for TCP or UDP 1476 * packets and nothing else. 1477 */ 1478 static int 1479 ip_send_align_hcksum_flags(mblk_t *mp, ill_t *ill) 1480 { 1481 int illhckflags; 1482 int mbhckflags; 1483 uint16_t *up; 1484 uint32_t cksum; 1485 ipha_t *ipha; 1486 ip6_t *ip6; 1487 int proto; 1488 int ipversion; 1489 int length; 1490 int start; 1491 ip6_pkt_t ipp; 1492 1493 mbhckflags = DB_CKSUMFLAGS(mp); 1494 ASSERT(mbhckflags != 0); 1495 ASSERT(mp->b_datap->db_type == M_DATA); 1496 /* 1497 * Since this function only knows how to manage the hardware checksum 1498 * issue, reject and packets that have flags set on the aside from 1499 * checksum related attributes as we cannot necessarily safely map 1500 * that packet onto the new NIC. Packets that can be potentially 1501 * dropped here include those marked for LSO. 1502 */ 1503 if ((mbhckflags & 1504 ~(HCK_FULLCKSUM|HCK_PARTIALCKSUM|HCK_IPV4_HDRCKSUM)) != 0) { 1505 DTRACE_PROBE2(pbr__incapable, (mblk_t *), mp, (ill_t *), ill); 1506 freemsg(mp); 1507 return (-1); 1508 } 1509 1510 ipha = (ipha_t *)mp->b_rptr; 1511 1512 /* 1513 * Find out what the new NIC is capable of, if anything, and 1514 * only allow it to be used with M_DATA mblks being sent out. 1515 */ 1516 if (ILL_HCKSUM_CAPABLE(ill)) { 1517 illhckflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 1518 } else { 1519 /* 1520 * No capabilities, so turn off everything. 1521 */ 1522 illhckflags = 0; 1523 (void) hcksum_assoc(mp, NULL, NULL, 0, 0, 0, 0, 0, 0); 1524 mp->b_datap->db_struioflag &= ~STRUIO_IP; 1525 } 1526 1527 DTRACE_PROBE4(pbr__info__a, (mblk_t *), mp, (ill_t *), ill, 1528 uint32_t, illhckflags, uint32_t, mbhckflags); 1529 /* 1530 * This block of code that looks for the position of the TCP/UDP 1531 * checksum is early in this function because we need to know 1532 * what needs to be blanked out for the hardware checksum case. 1533 * 1534 * That we're in this function implies that the packet is either 1535 * TCP or UDP on Solaris, so checks are made for one protocol and 1536 * if that fails, the other is therefore implied. 1537 */ 1538 ipversion = IPH_HDR_VERSION(ipha); 1539 1540 if (ipversion == IPV4_VERSION) { 1541 proto = ipha->ipha_protocol; 1542 if (proto == IPPROTO_TCP) { 1543 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 1544 } else { 1545 up = IPH_UDPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 1546 } 1547 } else { 1548 uint8_t lasthdr; 1549 1550 /* 1551 * Nothing I've seen indicates that IPv6 checksum'ing 1552 * precludes the presence of extension headers, so we 1553 * can't just look at the next header value in the IPv6 1554 * packet header to see if it is TCP/UDP. 1555 */ 1556 ip6 = (ip6_t *)ipha; 1557 (void) memset(&ipp, 0, sizeof (ipp)); 1558 start = ip_find_hdr_v6(mp, ip6, &ipp, &lasthdr); 1559 proto = lasthdr; 1560 1561 if (proto == IPPROTO_TCP) { 1562 up = IPH_TCPH_CHECKSUMP(ipha, start); 1563 } else { 1564 up = IPH_UDPH_CHECKSUMP(ipha, start); 1565 } 1566 } 1567 1568 /* 1569 * The first case here is easiest: 1570 * mblk hasn't asked for full checksum, but the card supports it. 1571 * 1572 * In addition, check for IPv4 header capability. Note that only 1573 * the mblk flag is checked and not ipversion. 1574 */ 1575 if ((((illhckflags & HCKSUM_INET_FULL_V4) && (ipversion == 4)) || 1576 (((illhckflags & HCKSUM_INET_FULL_V6) && (ipversion == 6)))) && 1577 ((mbhckflags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) != 0)) { 1578 int newflags = HCK_FULLCKSUM; 1579 1580 if ((mbhckflags & HCK_IPV4_HDRCKSUM) != 0) { 1581 if ((illhckflags & HCKSUM_IPHDRCKSUM) != 0) { 1582 newflags |= HCK_IPV4_HDRCKSUM; 1583 } else { 1584 /* 1585 * Rather than call a function, just inline 1586 * the computation of the basic IPv4 header. 1587 */ 1588 cksum = (ipha->ipha_dst >> 16) + 1589 (ipha->ipha_dst & 0xFFFF) + 1590 (ipha->ipha_src >> 16) + 1591 (ipha->ipha_src & 0xFFFF); 1592 IP_HDR_CKSUM(ipha, cksum, 1593 ((uint32_t *)ipha)[0], 1594 ((uint16_t *)ipha)[4]); 1595 } 1596 } 1597 1598 *up = 0; 1599 (void) hcksum_assoc(mp, NULL, NULL, 0, 0, 0, 0, 1600 newflags, 0); 1601 return (0); 1602 } 1603 1604 DTRACE_PROBE2(pbr__info__b, int, ipversion, int, proto); 1605 1606 /* 1607 * Start calculating the pseudo checksum over the IP packet header. 1608 * Although the final pseudo checksum used by TCP/UDP consists of 1609 * more than just the address fields, we can use the result of 1610 * adding those together a little bit further down for IPv4. 1611 */ 1612 if (ipversion == IPV4_VERSION) { 1613 cksum = (ipha->ipha_dst >> 16) + (ipha->ipha_dst & 0xFFFF) + 1614 (ipha->ipha_src >> 16) + (ipha->ipha_src & 0xFFFF); 1615 start = IP_SIMPLE_HDR_LENGTH; 1616 length = ntohs(ipha->ipha_length); 1617 DTRACE_PROBE3(pbr__info__e, uint32_t, ipha->ipha_src, 1618 uint32_t, ipha->ipha_dst, int, cksum); 1619 } else { 1620 uint16_t *pseudo; 1621 1622 pseudo = (uint16_t *)&ip6->ip6_src; 1623 1624 /* calculate pseudo-header checksum */ 1625 cksum = pseudo[0] + pseudo[1] + pseudo[2] + pseudo[3] + 1626 pseudo[4] + pseudo[5] + pseudo[6] + pseudo[7] + 1627 pseudo[8] + pseudo[9] + pseudo[10] + pseudo[11] + 1628 pseudo[12] + pseudo[13] + pseudo[14] + pseudo[15]; 1629 1630 length = ntohs(ip6->ip6_plen) + sizeof (ip6_t); 1631 } 1632 1633 /* Fold the initial sum */ 1634 cksum = (cksum & 0xffff) + (cksum >> 16); 1635 1636 /* 1637 * If the packet was asking for an IPv4 header checksum to be 1638 * calculated but the interface doesn't support that, fill it in 1639 * using our pseudo checksum as a starting point. 1640 */ 1641 if (((mbhckflags & HCK_IPV4_HDRCKSUM) != 0) && 1642 ((illhckflags & HCKSUM_IPHDRCKSUM) == 0)) { 1643 /* 1644 * IP_HDR_CKSUM uses the 2rd arg to the macro in a destructive 1645 * way so pass in a copy of the checksum calculated thus far. 1646 */ 1647 uint32_t ipsum = cksum; 1648 1649 DB_CKSUMFLAGS(mp) &= ~HCK_IPV4_HDRCKSUM; 1650 1651 IP_HDR_CKSUM(ipha, ipsum, ((uint32_t *)ipha)[0], 1652 ((uint16_t *)ipha)[4]); 1653 } 1654 1655 DTRACE_PROBE3(pbr__info__c, int, start, int, length, int, cksum); 1656 1657 if (proto == IPPROTO_TCP) { 1658 cksum += IP_TCP_CSUM_COMP; 1659 } else { 1660 cksum += IP_UDP_CSUM_COMP; 1661 } 1662 cksum += htons(length - start); 1663 cksum = (cksum & 0xffff) + (cksum >> 16); 1664 1665 /* 1666 * For TCP/UDP, we either want to setup the packet for partial 1667 * checksum or we want to do it all ourselves because the NIC 1668 * offers no support for either partial or full checksum. 1669 */ 1670 if ((illhckflags & HCKSUM_INET_PARTIAL) != 0) { 1671 /* 1672 * The only case we care about here is if the mblk was 1673 * previously set for full checksum offload. If it was 1674 * marked for partial (and the NIC does partial), then 1675 * we have nothing to do. Similarly if the packet was 1676 * not set for partial or full, we do nothing as this 1677 * is cheaper than more work to set something up. 1678 */ 1679 if ((mbhckflags & HCK_FULLCKSUM) != 0) { 1680 uint32_t offset; 1681 1682 if (proto == IPPROTO_TCP) { 1683 offset = TCP_CHECKSUM_OFFSET; 1684 } else { 1685 offset = UDP_CHECKSUM_OFFSET; 1686 } 1687 *up = cksum; 1688 1689 DTRACE_PROBE3(pbr__info__f, int, length - start, int, 1690 cksum, int, offset); 1691 1692 (void) hcksum_assoc(mp, NULL, NULL, start, 1693 start + offset, length, 0, 1694 DB_CKSUMFLAGS(mp) | HCK_PARTIALCKSUM, 0); 1695 } 1696 1697 } else if (mbhckflags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) { 1698 DB_CKSUMFLAGS(mp) &= ~(HCK_PARTIALCKSUM|HCK_FULLCKSUM); 1699 1700 *up = 0; 1701 *up = IP_CSUM(mp, start, cksum); 1702 } 1703 1704 DTRACE_PROBE4(pbr__info__d, (mblk_t *), mp, (ipha_t *), ipha, 1705 (uint16_t *), up, int, cksum); 1706 return (0); 1707 } 1708 1709 1710 /* ire_walk routine invoked for ip_ire_report for each IRE. */ 1711 void 1712 ire_report_ftable(ire_t *ire, char *m) 1713 { 1714 char buf1[16]; 1715 char buf2[16]; 1716 char buf3[16]; 1717 char buf4[16]; 1718 uint_t fo_pkt_count; 1719 uint_t ib_pkt_count; 1720 int ref; 1721 uint_t print_len, buf_len; 1722 mblk_t *mp = (mblk_t *)m; 1723 1724 if (ire->ire_type & IRE_CACHETABLE) 1725 return; 1726 buf_len = mp->b_datap->db_lim - mp->b_wptr; 1727 if (buf_len <= 0) 1728 return; 1729 1730 /* Number of active references of this ire */ 1731 ref = ire->ire_refcnt; 1732 /* "inbound" to a non local address is a forward */ 1733 ib_pkt_count = ire->ire_ib_pkt_count; 1734 fo_pkt_count = 0; 1735 if (!(ire->ire_type & (IRE_LOCAL|IRE_BROADCAST))) { 1736 fo_pkt_count = ib_pkt_count; 1737 ib_pkt_count = 0; 1738 } 1739 print_len = snprintf((char *)mp->b_wptr, buf_len, 1740 MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR "%5d " 1741 "%s %s %s %s %05d %05ld %06ld %08d %03d %06d %09d %09d %06d %08d " 1742 "%04d %08d %08d %d/%d/%d %s\n", 1743 (void *)ire, (void *)ire->ire_rfq, (void *)ire->ire_stq, 1744 (int)ire->ire_zoneid, 1745 ip_dot_addr(ire->ire_addr, buf1), ip_dot_addr(ire->ire_mask, buf2), 1746 ip_dot_addr(ire->ire_src_addr, buf3), 1747 ip_dot_addr(ire->ire_gateway_addr, buf4), 1748 ire->ire_max_frag, ire->ire_uinfo.iulp_rtt, 1749 ire->ire_uinfo.iulp_rtt_sd, 1750 ire->ire_uinfo.iulp_ssthresh, ref, 1751 ire->ire_uinfo.iulp_rtomax, 1752 (ire->ire_uinfo.iulp_tstamp_ok ? 1: 0), 1753 (ire->ire_uinfo.iulp_wscale_ok ? 1: 0), 1754 (ire->ire_uinfo.iulp_ecn_ok ? 1: 0), 1755 (ire->ire_uinfo.iulp_pmtud_ok ? 1: 0), 1756 ire->ire_uinfo.iulp_sack, 1757 ire->ire_uinfo.iulp_spipe, ire->ire_uinfo.iulp_rpipe, 1758 ib_pkt_count, ire->ire_ob_pkt_count, fo_pkt_count, 1759 ip_nv_lookup(ire_nv_tbl, (int)ire->ire_type)); 1760 if (print_len < buf_len) { 1761 mp->b_wptr += print_len; 1762 } else { 1763 mp->b_wptr += buf_len; 1764 } 1765 } 1766 1767 /* 1768 * callback function provided by ire_ftable_lookup when calling 1769 * rn_match_args(). Invoke ire_match_args on each matching leaf node in 1770 * the radix tree. 1771 */ 1772 boolean_t 1773 ire_find_best_route(struct radix_node *rn, void *arg) 1774 { 1775 struct rt_entry *rt = (struct rt_entry *)rn; 1776 irb_t *irb_ptr; 1777 ire_t *ire; 1778 ire_ftable_args_t *margs = arg; 1779 ipaddr_t match_mask; 1780 1781 ASSERT(rt != NULL); 1782 1783 irb_ptr = &rt->rt_irb; 1784 1785 if (irb_ptr->irb_ire_cnt == 0) 1786 return (B_FALSE); 1787 1788 rw_enter(&irb_ptr->irb_lock, RW_READER); 1789 for (ire = irb_ptr->irb_ire; ire != NULL; ire = ire->ire_next) { 1790 if (ire->ire_marks & IRE_MARK_CONDEMNED) 1791 continue; 1792 if (margs->ift_flags & MATCH_IRE_MASK) 1793 match_mask = margs->ift_mask; 1794 else 1795 match_mask = ire->ire_mask; 1796 1797 if (ire_match_args(ire, margs->ift_addr, match_mask, 1798 margs->ift_gateway, margs->ift_type, margs->ift_ipif, 1799 margs->ift_zoneid, margs->ift_ihandle, margs->ift_tsl, 1800 margs->ift_flags)) { 1801 IRE_REFHOLD(ire); 1802 rw_exit(&irb_ptr->irb_lock); 1803 margs->ift_best_ire = ire; 1804 return (B_TRUE); 1805 } 1806 } 1807 rw_exit(&irb_ptr->irb_lock); 1808 return (B_FALSE); 1809 } 1810 1811 /* 1812 * ftable irb_t structures are dynamically allocated, and we need to 1813 * check if the irb_t (and associated ftable tree attachment) needs to 1814 * be cleaned up when the irb_refcnt goes to 0. The conditions that need 1815 * be verified are: 1816 * - no other walkers of the irebucket, i.e., quiescent irb_refcnt, 1817 * - no other threads holding references to ire's in the bucket, 1818 * i.e., irb_nire == 0 1819 * - no active ire's in the bucket, i.e., irb_ire_cnt == 0 1820 * - need to hold the global tree lock and irb_lock in write mode. 1821 */ 1822 void 1823 irb_refrele_ftable(irb_t *irb) 1824 { 1825 for (;;) { 1826 rw_enter(&irb->irb_lock, RW_WRITER); 1827 ASSERT(irb->irb_refcnt != 0); 1828 if (irb->irb_refcnt != 1) { 1829 /* 1830 * Someone has a reference to this radix node 1831 * or there is some bucket walker. 1832 */ 1833 irb->irb_refcnt--; 1834 rw_exit(&irb->irb_lock); 1835 return; 1836 } else { 1837 /* 1838 * There is no other walker, nor is there any 1839 * other thread that holds a direct ref to this 1840 * radix node. Do the clean up if needed. Call 1841 * to ire_unlink will clear the IRB_MARK_CONDEMNED flag 1842 */ 1843 if (irb->irb_marks & IRB_MARK_CONDEMNED) { 1844 ire_t *ire_list; 1845 1846 ire_list = ire_unlink(irb); 1847 rw_exit(&irb->irb_lock); 1848 1849 if (ire_list != NULL) 1850 ire_cleanup(ire_list); 1851 /* 1852 * more CONDEMNED entries could have 1853 * been added while we dropped the lock, 1854 * so we have to re-check. 1855 */ 1856 continue; 1857 } 1858 1859 /* 1860 * Now check if there are still any ires 1861 * associated with this radix node. 1862 */ 1863 if (irb->irb_nire != 0) { 1864 /* 1865 * someone is still holding on 1866 * to ires in this bucket 1867 */ 1868 irb->irb_refcnt--; 1869 rw_exit(&irb->irb_lock); 1870 return; 1871 } else { 1872 /* 1873 * Everything is clear. Zero walkers, 1874 * Zero threads with a ref to this 1875 * radix node, Zero ires associated with 1876 * this radix node. Due to lock order, 1877 * check the above conditions again 1878 * after grabbing all locks in the right order 1879 */ 1880 rw_exit(&irb->irb_lock); 1881 if (irb_inactive(irb)) 1882 return; 1883 /* 1884 * irb_inactive could not free the irb. 1885 * See if there are any walkers, if not 1886 * try to clean up again. 1887 */ 1888 } 1889 } 1890 } 1891 } 1892 1893 /* 1894 * IRE iterator used by ire_ftable_lookup() to process multiple default 1895 * routes. Given a starting point in the hash list (ire_origin), walk the IREs 1896 * in the bucket skipping default interface routes and deleted entries. 1897 * Returns the next IRE (unheld), or NULL when we're back to the starting point. 1898 * Assumes that the caller holds a reference on the IRE bucket. 1899 * 1900 * In the absence of good IRE_DEFAULT routes, this function will return 1901 * the first IRE_INTERFACE route found (if any). 1902 */ 1903 ire_t * 1904 ire_round_robin(irb_t *irb_ptr, zoneid_t zoneid, ire_ftable_args_t *margs, 1905 ip_stack_t *ipst) 1906 { 1907 ire_t *ire_origin; 1908 ire_t *ire, *maybe_ire = NULL; 1909 1910 rw_enter(&irb_ptr->irb_lock, RW_WRITER); 1911 ire_origin = irb_ptr->irb_rr_origin; 1912 if (ire_origin != NULL) { 1913 ire_origin = ire_origin->ire_next; 1914 IRE_FIND_NEXT_ORIGIN(ire_origin); 1915 } 1916 1917 if (ire_origin == NULL) { 1918 /* 1919 * first time through routine, or we dropped off the end 1920 * of list. 1921 */ 1922 ire_origin = irb_ptr->irb_ire; 1923 IRE_FIND_NEXT_ORIGIN(ire_origin); 1924 } 1925 irb_ptr->irb_rr_origin = ire_origin; 1926 IRB_REFHOLD_LOCKED(irb_ptr); 1927 rw_exit(&irb_ptr->irb_lock); 1928 1929 DTRACE_PROBE2(ire__rr__origin, (irb_t *), irb_ptr, 1930 (ire_t *), ire_origin); 1931 1932 /* 1933 * Round-robin the routers list looking for a route that 1934 * matches the passed in parameters. 1935 * We start with the ire we found above and we walk the hash 1936 * list until we're back where we started. It doesn't matter if 1937 * routes are added or deleted by other threads - we know this 1938 * ire will stay in the list because we hold a reference on the 1939 * ire bucket. 1940 */ 1941 ire = ire_origin; 1942 while (ire != NULL) { 1943 int match_flags = MATCH_IRE_TYPE | MATCH_IRE_SECATTR; 1944 ire_t *rire; 1945 1946 if (ire->ire_marks & IRE_MARK_CONDEMNED) 1947 goto next_ire; 1948 1949 if (!ire_match_args(ire, margs->ift_addr, (ipaddr_t)0, 1950 margs->ift_gateway, margs->ift_type, margs->ift_ipif, 1951 margs->ift_zoneid, margs->ift_ihandle, margs->ift_tsl, 1952 margs->ift_flags)) 1953 goto next_ire; 1954 1955 if (ire->ire_type & IRE_INTERFACE) { 1956 /* 1957 * keep looking to see if there is a non-interface 1958 * default ire, but save this one as a last resort. 1959 */ 1960 if (maybe_ire == NULL) 1961 maybe_ire = ire; 1962 goto next_ire; 1963 } 1964 1965 if (zoneid == ALL_ZONES) { 1966 IRE_REFHOLD(ire); 1967 IRB_REFRELE(irb_ptr); 1968 return (ire); 1969 } 1970 /* 1971 * When we're in a non-global zone, we're only 1972 * interested in routers that are 1973 * reachable through ipifs within our zone. 1974 */ 1975 if (ire->ire_ipif != NULL) { 1976 match_flags |= MATCH_IRE_ILL_GROUP; 1977 } 1978 rire = ire_route_lookup(ire->ire_gateway_addr, 0, 0, 1979 IRE_INTERFACE, ire->ire_ipif, NULL, zoneid, margs->ift_tsl, 1980 match_flags, ipst); 1981 if (rire != NULL) { 1982 ire_refrele(rire); 1983 IRE_REFHOLD(ire); 1984 IRB_REFRELE(irb_ptr); 1985 return (ire); 1986 } 1987 next_ire: 1988 ire = (ire->ire_next ? ire->ire_next : irb_ptr->irb_ire); 1989 if (ire == ire_origin) 1990 break; 1991 } 1992 if (maybe_ire != NULL) 1993 IRE_REFHOLD(maybe_ire); 1994 IRB_REFRELE(irb_ptr); 1995 return (maybe_ire); 1996 } 1997 1998 void 1999 irb_refhold_rn(struct radix_node *rn) 2000 { 2001 if ((rn->rn_flags & RNF_ROOT) == 0) 2002 IRB_REFHOLD(&((rt_t *)(rn))->rt_irb); 2003 } 2004 2005 void 2006 irb_refrele_rn(struct radix_node *rn) 2007 { 2008 if ((rn->rn_flags & RNF_ROOT) == 0) 2009 irb_refrele_ftable(&((rt_t *)(rn))->rt_irb); 2010 } 2011