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 2010 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 /* 26 * Copyright (c) 1990 Mentat Inc. 27 */ 28 29 /* 30 * This file contains the interface control functions for IPv6. 31 */ 32 33 #include <sys/types.h> 34 #include <sys/sysmacros.h> 35 #include <sys/stream.h> 36 #include <sys/dlpi.h> 37 #include <sys/stropts.h> 38 #include <sys/ddi.h> 39 #include <sys/cmn_err.h> 40 #include <sys/kstat.h> 41 #include <sys/debug.h> 42 #include <sys/zone.h> 43 #include <sys/policy.h> 44 45 #include <sys/systm.h> 46 #include <sys/param.h> 47 #include <sys/socket.h> 48 #include <sys/isa_defs.h> 49 #include <net/if.h> 50 #include <net/if_dl.h> 51 #include <net/route.h> 52 #include <netinet/in.h> 53 #include <netinet/igmp_var.h> 54 #include <netinet/ip6.h> 55 #include <netinet/icmp6.h> 56 57 #include <inet/common.h> 58 #include <inet/nd.h> 59 #include <inet/mib2.h> 60 #include <inet/ip.h> 61 #include <inet/ip6.h> 62 #include <inet/ip_multi.h> 63 #include <inet/ip_ire.h> 64 #include <inet/ip_rts.h> 65 #include <inet/ip_ndp.h> 66 #include <inet/ip_if.h> 67 #include <inet/ip6_asp.h> 68 #include <inet/ipclassifier.h> 69 #include <inet/sctp_ip.h> 70 71 #include <sys/tsol/tndb.h> 72 #include <sys/tsol/tnet.h> 73 74 static in6_addr_t ipv6_ll_template = 75 {(uint32_t)V6_LINKLOCAL, 0x0, 0x0, 0x0}; 76 77 static ipif_t * 78 ipif_lookup_interface_v6(const in6_addr_t *if_addr, const in6_addr_t *dst, 79 ip_stack_t *ipst); 80 81 static int ipif_add_ires_v6(ipif_t *, boolean_t); 82 83 /* 84 * This function is called when an application does not specify an interface 85 * to be used for multicast traffic. It calls ire_lookup_multi_v6() to look 86 * for an interface route for the specified multicast group. Doing 87 * this allows the administrator to add prefix routes for multicast to 88 * indicate which interface to be used for multicast traffic in the above 89 * scenario. The route could be for all multicast (ff00::/8), for a single 90 * multicast group (a /128 route) or anything in between. If there is no 91 * such multicast route, we just find any multicast capable interface and 92 * return it. 93 * 94 * We support MULTIRT and RTF_SETSRC on the multicast routes added to the 95 * unicast table. This is used by CGTP. 96 */ 97 ill_t * 98 ill_lookup_group_v6(const in6_addr_t *group, zoneid_t zoneid, ip_stack_t *ipst, 99 boolean_t *multirtp, in6_addr_t *setsrcp) 100 { 101 ill_t *ill; 102 103 ill = ire_lookup_multi_ill_v6(group, zoneid, ipst, multirtp, setsrcp); 104 if (ill != NULL) 105 return (ill); 106 107 return (ill_lookup_multicast(ipst, zoneid, B_TRUE)); 108 } 109 110 /* 111 * Look for an ipif with the specified interface address and destination. 112 * The destination address is used only for matching point-to-point interfaces. 113 */ 114 static ipif_t * 115 ipif_lookup_interface_v6(const in6_addr_t *if_addr, const in6_addr_t *dst, 116 ip_stack_t *ipst) 117 { 118 ipif_t *ipif; 119 ill_t *ill; 120 ill_walk_context_t ctx; 121 122 /* 123 * First match all the point-to-point interfaces 124 * before looking at non-point-to-point interfaces. 125 * This is done to avoid returning non-point-to-point 126 * ipif instead of unnumbered point-to-point ipif. 127 */ 128 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 129 ill = ILL_START_WALK_V6(&ctx, ipst); 130 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 131 mutex_enter(&ill->ill_lock); 132 for (ipif = ill->ill_ipif; ipif != NULL; 133 ipif = ipif->ipif_next) { 134 /* Allow the ipif to be down */ 135 if ((ipif->ipif_flags & IPIF_POINTOPOINT) && 136 (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr, 137 if_addr)) && 138 (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, 139 dst))) { 140 if (!IPIF_IS_CONDEMNED(ipif)) { 141 ipif_refhold_locked(ipif); 142 mutex_exit(&ill->ill_lock); 143 rw_exit(&ipst->ips_ill_g_lock); 144 return (ipif); 145 } 146 } 147 } 148 mutex_exit(&ill->ill_lock); 149 } 150 rw_exit(&ipst->ips_ill_g_lock); 151 /* lookup the ipif based on interface address */ 152 ipif = ipif_lookup_addr_v6(if_addr, NULL, ALL_ZONES, ipst); 153 ASSERT(ipif == NULL || ipif->ipif_isv6); 154 return (ipif); 155 } 156 157 /* 158 * Common function for ipif_lookup_addr_v6() and ipif_lookup_addr_exact_v6(). 159 */ 160 static ipif_t * 161 ipif_lookup_addr_common_v6(const in6_addr_t *addr, ill_t *match_ill, 162 uint32_t match_flags, zoneid_t zoneid, ip_stack_t *ipst) 163 { 164 ipif_t *ipif; 165 ill_t *ill; 166 boolean_t ptp = B_FALSE; 167 ill_walk_context_t ctx; 168 boolean_t match_illgrp = (match_flags & IPIF_MATCH_ILLGRP); 169 boolean_t no_duplicate = (match_flags & IPIF_MATCH_NONDUP); 170 171 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 172 /* 173 * Repeat twice, first based on local addresses and 174 * next time for pointopoint. 175 */ 176 repeat: 177 ill = ILL_START_WALK_V6(&ctx, ipst); 178 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 179 if (match_ill != NULL && ill != match_ill && 180 (!match_illgrp || !IS_IN_SAME_ILLGRP(ill, match_ill))) { 181 continue; 182 } 183 mutex_enter(&ill->ill_lock); 184 for (ipif = ill->ill_ipif; ipif != NULL; 185 ipif = ipif->ipif_next) { 186 if (zoneid != ALL_ZONES && 187 ipif->ipif_zoneid != zoneid && 188 ipif->ipif_zoneid != ALL_ZONES) 189 continue; 190 191 if (no_duplicate && 192 !(ipif->ipif_flags & IPIF_UP)) { 193 continue; 194 } 195 196 /* Allow the ipif to be down */ 197 if ((!ptp && (IN6_ARE_ADDR_EQUAL( 198 &ipif->ipif_v6lcl_addr, addr) && 199 (ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) || 200 (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) && 201 IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, 202 addr))) { 203 if (!IPIF_IS_CONDEMNED(ipif)) { 204 ipif_refhold_locked(ipif); 205 mutex_exit(&ill->ill_lock); 206 rw_exit(&ipst->ips_ill_g_lock); 207 return (ipif); 208 } 209 } 210 } 211 mutex_exit(&ill->ill_lock); 212 } 213 214 /* If we already did the ptp case, then we are done */ 215 if (ptp) { 216 rw_exit(&ipst->ips_ill_g_lock); 217 return (NULL); 218 } 219 ptp = B_TRUE; 220 goto repeat; 221 } 222 223 /* 224 * Lookup an ipif with the specified address. For point-to-point links we 225 * look for matches on either the destination address or the local address, 226 * but we skip the local address check if IPIF_UNNUMBERED is set. If the 227 * `match_ill' argument is non-NULL, the lookup is restricted to that ill 228 * (or illgrp if `match_ill' is in an IPMP group). 229 */ 230 ipif_t * 231 ipif_lookup_addr_v6(const in6_addr_t *addr, ill_t *match_ill, zoneid_t zoneid, 232 ip_stack_t *ipst) 233 { 234 return (ipif_lookup_addr_common_v6(addr, match_ill, IPIF_MATCH_ILLGRP, 235 zoneid, ipst)); 236 } 237 238 /* 239 * Lookup an ipif with the specified address. Similar to ipif_lookup_addr, 240 * except that we will only return an address if it is not marked as 241 * IPIF_DUPLICATE 242 */ 243 ipif_t * 244 ipif_lookup_addr_nondup_v6(const in6_addr_t *addr, ill_t *match_ill, 245 zoneid_t zoneid, ip_stack_t *ipst) 246 { 247 return (ipif_lookup_addr_common_v6(addr, match_ill, 248 (IPIF_MATCH_ILLGRP | IPIF_MATCH_NONDUP), zoneid, 249 ipst)); 250 } 251 252 /* 253 * Special abbreviated version of ipif_lookup_addr_v6() that doesn't match 254 * `match_ill' across the IPMP group. This function is only needed in some 255 * corner-cases; almost everything should use ipif_lookup_addr_v6(). 256 */ 257 ipif_t * 258 ipif_lookup_addr_exact_v6(const in6_addr_t *addr, ill_t *match_ill, 259 ip_stack_t *ipst) 260 { 261 ASSERT(match_ill != NULL); 262 return (ipif_lookup_addr_common_v6(addr, match_ill, 0, ALL_ZONES, 263 ipst)); 264 } 265 266 /* 267 * Look for an ipif with the specified address. For point-point links 268 * we look for matches on either the destination address and the local 269 * address, but we ignore the check on the local address if IPIF_UNNUMBERED 270 * is set. 271 * If the `match_ill' argument is non-NULL, the lookup is restricted to that 272 * ill (or illgrp if `match_ill' is in an IPMP group). 273 * Return the zoneid for the ipif. ALL_ZONES if none found. 274 */ 275 zoneid_t 276 ipif_lookup_addr_zoneid_v6(const in6_addr_t *addr, ill_t *match_ill, 277 ip_stack_t *ipst) 278 { 279 ipif_t *ipif; 280 ill_t *ill; 281 boolean_t ptp = B_FALSE; 282 ill_walk_context_t ctx; 283 zoneid_t zoneid; 284 285 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 286 /* 287 * Repeat twice, first based on local addresses and 288 * next time for pointopoint. 289 */ 290 repeat: 291 ill = ILL_START_WALK_V6(&ctx, ipst); 292 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 293 if (match_ill != NULL && ill != match_ill && 294 !IS_IN_SAME_ILLGRP(ill, match_ill)) { 295 continue; 296 } 297 mutex_enter(&ill->ill_lock); 298 for (ipif = ill->ill_ipif; ipif != NULL; 299 ipif = ipif->ipif_next) { 300 /* Allow the ipif to be down */ 301 if ((!ptp && (IN6_ARE_ADDR_EQUAL( 302 &ipif->ipif_v6lcl_addr, addr) && 303 (ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) || 304 (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) && 305 IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, 306 addr)) && 307 !(ipif->ipif_state_flags & IPIF_CONDEMNED)) { 308 zoneid = ipif->ipif_zoneid; 309 mutex_exit(&ill->ill_lock); 310 rw_exit(&ipst->ips_ill_g_lock); 311 /* 312 * If ipif_zoneid was ALL_ZONES then we have 313 * a trusted extensions shared IP address. 314 * In that case GLOBAL_ZONEID works to send. 315 */ 316 if (zoneid == ALL_ZONES) 317 zoneid = GLOBAL_ZONEID; 318 return (zoneid); 319 } 320 } 321 mutex_exit(&ill->ill_lock); 322 } 323 324 /* If we already did the ptp case, then we are done */ 325 if (ptp) { 326 rw_exit(&ipst->ips_ill_g_lock); 327 return (ALL_ZONES); 328 } 329 ptp = B_TRUE; 330 goto repeat; 331 } 332 333 /* 334 * Perform various checks to verify that an address would make sense as a local 335 * interface address. This is currently only called when an attempt is made 336 * to set a local address. 337 * 338 * Does not allow a v4-mapped address, an address that equals the subnet 339 * anycast address, ... a multicast address, ... 340 */ 341 boolean_t 342 ip_local_addr_ok_v6(const in6_addr_t *addr, const in6_addr_t *subnet_mask) 343 { 344 in6_addr_t subnet; 345 346 if (IN6_IS_ADDR_UNSPECIFIED(addr)) 347 return (B_TRUE); /* Allow all zeros */ 348 349 /* 350 * Don't allow all zeroes or host part, but allow 351 * all ones netmask. 352 */ 353 V6_MASK_COPY(*addr, *subnet_mask, subnet); 354 if (IN6_IS_ADDR_V4MAPPED(addr) || 355 (IN6_ARE_ADDR_EQUAL(addr, &subnet) && 356 !IN6_ARE_ADDR_EQUAL(subnet_mask, &ipv6_all_ones)) || 357 (IN6_IS_ADDR_V4COMPAT(addr) && CLASSD(V4_PART_OF_V6((*addr)))) || 358 IN6_IS_ADDR_MULTICAST(addr)) 359 return (B_FALSE); 360 361 return (B_TRUE); 362 } 363 364 /* 365 * Perform various checks to verify that an address would make sense as a 366 * remote/subnet interface address. 367 */ 368 boolean_t 369 ip_remote_addr_ok_v6(const in6_addr_t *addr, const in6_addr_t *subnet_mask) 370 { 371 in6_addr_t subnet; 372 373 if (IN6_IS_ADDR_UNSPECIFIED(addr)) 374 return (B_TRUE); /* Allow all zeros */ 375 376 V6_MASK_COPY(*addr, *subnet_mask, subnet); 377 if (IN6_IS_ADDR_V4MAPPED(addr) || 378 (IN6_ARE_ADDR_EQUAL(addr, &subnet) && 379 !IN6_ARE_ADDR_EQUAL(subnet_mask, &ipv6_all_ones)) || 380 IN6_IS_ADDR_MULTICAST(addr) || 381 (IN6_IS_ADDR_V4COMPAT(addr) && CLASSD(V4_PART_OF_V6((*addr))))) 382 return (B_FALSE); 383 384 return (B_TRUE); 385 } 386 387 /* 388 * ip_rt_add_v6 is called to add an IPv6 route to the forwarding table. 389 * ill is passed in to associate it with the correct interface 390 * (for link-local destinations and gateways). 391 * If ire_arg is set, then we return the held IRE in that location. 392 */ 393 /* ARGSUSED1 */ 394 int 395 ip_rt_add_v6(const in6_addr_t *dst_addr, const in6_addr_t *mask, 396 const in6_addr_t *gw_addr, const in6_addr_t *src_addr, int flags, 397 ill_t *ill, ire_t **ire_arg, struct rtsa_s *sp, ip_stack_t *ipst, 398 zoneid_t zoneid) 399 { 400 ire_t *ire, *nire; 401 ire_t *gw_ire = NULL; 402 ipif_t *ipif; 403 uint_t type; 404 int match_flags = MATCH_IRE_TYPE; 405 tsol_gc_t *gc = NULL; 406 tsol_gcgrp_t *gcgrp = NULL; 407 boolean_t gcgrp_xtraref = B_FALSE; 408 boolean_t unbound = B_FALSE; 409 410 if (ire_arg != NULL) 411 *ire_arg = NULL; 412 413 /* 414 * Prevent routes with a zero gateway from being created (since 415 * interfaces can currently be plumbed and brought up with no assigned 416 * address). 417 */ 418 if (IN6_IS_ADDR_UNSPECIFIED(gw_addr)) 419 return (ENETUNREACH); 420 421 /* 422 * If this is the case of RTF_HOST being set, then we set the netmask 423 * to all ones (regardless if one was supplied). 424 */ 425 if (flags & RTF_HOST) 426 mask = &ipv6_all_ones; 427 428 /* 429 * Get the ipif, if any, corresponding to the gw_addr 430 * If -ifp was specified we restrict ourselves to the ill, otherwise 431 * we match on the gatway and destination to handle unnumbered pt-pt 432 * interfaces. 433 */ 434 if (ill != NULL) 435 ipif = ipif_lookup_addr_v6(gw_addr, ill, ALL_ZONES, ipst); 436 else 437 ipif = ipif_lookup_interface_v6(gw_addr, dst_addr, ipst); 438 if (ipif != NULL) { 439 if (IS_VNI(ipif->ipif_ill)) { 440 ipif_refrele(ipif); 441 return (EINVAL); 442 } 443 } 444 445 /* 446 * GateD will attempt to create routes with a loopback interface 447 * address as the gateway and with RTF_GATEWAY set. We allow 448 * these routes to be added, but create them as interface routes 449 * since the gateway is an interface address. 450 */ 451 if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) { 452 flags &= ~RTF_GATEWAY; 453 if (IN6_ARE_ADDR_EQUAL(gw_addr, &ipv6_loopback) && 454 IN6_ARE_ADDR_EQUAL(dst_addr, &ipv6_loopback) && 455 IN6_ARE_ADDR_EQUAL(mask, &ipv6_all_ones)) { 456 ire = ire_ftable_lookup_v6(dst_addr, 0, 0, IRE_LOOPBACK, 457 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst, 458 NULL); 459 if (ire != NULL) { 460 ire_refrele(ire); 461 ipif_refrele(ipif); 462 return (EEXIST); 463 } 464 ip1dbg(("ip_rt_add_v6: 0x%p creating IRE 0x%x" 465 "for 0x%x\n", (void *)ipif, 466 ipif->ipif_ire_type, 467 ntohl(ipif->ipif_lcl_addr))); 468 ire = ire_create_v6( 469 dst_addr, 470 mask, 471 NULL, 472 ipif->ipif_ire_type, /* LOOPBACK */ 473 ipif->ipif_ill, 474 zoneid, 475 (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0, 476 NULL, 477 ipst); 478 479 if (ire == NULL) { 480 ipif_refrele(ipif); 481 return (ENOMEM); 482 } 483 /* src address assigned by the caller? */ 484 if ((flags & RTF_SETSRC) && 485 !IN6_IS_ADDR_UNSPECIFIED(src_addr)) 486 ire->ire_setsrc_addr_v6 = *src_addr; 487 488 nire = ire_add(ire); 489 if (nire == NULL) { 490 /* 491 * In the result of failure, ire_add() will have 492 * already deleted the ire in question, so there 493 * is no need to do that here. 494 */ 495 ipif_refrele(ipif); 496 return (ENOMEM); 497 } 498 /* 499 * Check if it was a duplicate entry. This handles 500 * the case of two racing route adds for the same route 501 */ 502 if (nire != ire) { 503 ASSERT(nire->ire_identical_ref > 1); 504 ire_delete(nire); 505 ire_refrele(nire); 506 ipif_refrele(ipif); 507 return (EEXIST); 508 } 509 ire = nire; 510 goto save_ire; 511 } 512 } 513 514 /* 515 * The routes for multicast with CGTP are quite special in that 516 * the gateway is the local interface address, yet RTF_GATEWAY 517 * is set. We turn off RTF_GATEWAY to provide compatibility with 518 * this undocumented and unusual use of multicast routes. 519 */ 520 if ((flags & RTF_MULTIRT) && ipif != NULL) 521 flags &= ~RTF_GATEWAY; 522 523 /* 524 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set 525 * and the gateway address provided is one of the system's interface 526 * addresses. By using the routing socket interface and supplying an 527 * RTA_IFP sockaddr with an interface index, an alternate method of 528 * specifying an interface route to be created is available which uses 529 * the interface index that specifies the outgoing interface rather than 530 * the address of an outgoing interface (which may not be able to 531 * uniquely identify an interface). When coupled with the RTF_GATEWAY 532 * flag, routes can be specified which not only specify the next-hop to 533 * be used when routing to a certain prefix, but also which outgoing 534 * interface should be used. 535 * 536 * Previously, interfaces would have unique addresses assigned to them 537 * and so the address assigned to a particular interface could be used 538 * to identify a particular interface. One exception to this was the 539 * case of an unnumbered interface (where IPIF_UNNUMBERED was set). 540 * 541 * With the advent of IPv6 and its link-local addresses, this 542 * restriction was relaxed and interfaces could share addresses between 543 * themselves. In fact, typically all of the link-local interfaces on 544 * an IPv6 node or router will have the same link-local address. In 545 * order to differentiate between these interfaces, the use of an 546 * interface index is necessary and this index can be carried inside a 547 * RTA_IFP sockaddr (which is actually a sockaddr_dl). One restriction 548 * of using the interface index, however, is that all of the ipif's that 549 * are part of an ill have the same index and so the RTA_IFP sockaddr 550 * cannot be used to differentiate between ipif's (or logical 551 * interfaces) that belong to the same ill (physical interface). 552 * 553 * For example, in the following case involving IPv4 interfaces and 554 * logical interfaces 555 * 556 * 192.0.2.32 255.255.255.224 192.0.2.33 U if0 557 * 192.0.2.32 255.255.255.224 192.0.2.34 U if0 558 * 192.0.2.32 255.255.255.224 192.0.2.35 U if0 559 * 560 * the ipif's corresponding to each of these interface routes can be 561 * uniquely identified by the "gateway" (actually interface address). 562 * 563 * In this case involving multiple IPv6 default routes to a particular 564 * link-local gateway, the use of RTA_IFP is necessary to specify which 565 * default route is of interest: 566 * 567 * default fe80::123:4567:89ab:cdef U if0 568 * default fe80::123:4567:89ab:cdef U if1 569 */ 570 571 /* RTF_GATEWAY not set */ 572 if (!(flags & RTF_GATEWAY)) { 573 if (sp != NULL) { 574 ip2dbg(("ip_rt_add_v6: gateway security attributes " 575 "cannot be set with interface route\n")); 576 if (ipif != NULL) 577 ipif_refrele(ipif); 578 return (EINVAL); 579 } 580 581 /* 582 * Whether or not ill (RTA_IFP) is set, we require that 583 * the gateway is one of our local addresses. 584 */ 585 if (ipif == NULL) 586 return (ENETUNREACH); 587 588 /* 589 * We use MATCH_IRE_ILL here. If the caller specified an 590 * interface (from the RTA_IFP sockaddr) we use it, otherwise 591 * we use the ill derived from the gateway address. 592 * We can always match the gateway address since we record it 593 * in ire_gateway_addr. 594 * We don't allow RTA_IFP to specify a different ill than the 595 * one matching the ipif to make sure we can delete the route. 596 */ 597 match_flags |= MATCH_IRE_GW | MATCH_IRE_ILL; 598 if (ill == NULL) { 599 ill = ipif->ipif_ill; 600 } else if (ill != ipif->ipif_ill) { 601 ipif_refrele(ipif); 602 return (EINVAL); 603 } 604 605 /* 606 * We check for an existing entry at this point. 607 */ 608 match_flags |= MATCH_IRE_MASK; 609 ire = ire_ftable_lookup_v6(dst_addr, mask, gw_addr, 610 IRE_INTERFACE, ill, ALL_ZONES, NULL, match_flags, 0, ipst, 611 NULL); 612 if (ire != NULL) { 613 ire_refrele(ire); 614 ipif_refrele(ipif); 615 return (EEXIST); 616 } 617 618 /* 619 * Create a copy of the IRE_LOOPBACK, IRE_IF_NORESOLVER or 620 * IRE_IF_RESOLVER with the modified address, netmask, and 621 * gateway. 622 */ 623 ire = ire_create_v6( 624 dst_addr, 625 mask, 626 gw_addr, 627 ill->ill_net_type, 628 ill, 629 zoneid, 630 flags, 631 NULL, 632 ipst); 633 if (ire == NULL) { 634 ipif_refrele(ipif); 635 return (ENOMEM); 636 } 637 638 /* 639 * Some software (for example, GateD and Sun Cluster) attempts 640 * to create (what amount to) IRE_PREFIX routes with the 641 * loopback address as the gateway. This is primarily done to 642 * set up prefixes with the RTF_REJECT flag set (for example, 643 * when generating aggregate routes). We also OR in the 644 * RTF_BLACKHOLE flag as these interface routes, by 645 * definition, can only be that. 646 * 647 * If the IRE type (as defined by ill->ill_net_type) is 648 * IRE_LOOPBACK, then we map the request into a 649 * IRE_IF_NORESOLVER. 650 * 651 * Needless to say, the real IRE_LOOPBACK is NOT created by this 652 * routine, but rather using ire_create_v6() directly. 653 */ 654 if (ill->ill_net_type == IRE_LOOPBACK) { 655 ire->ire_type = IRE_IF_NORESOLVER; 656 ire->ire_flags |= RTF_BLACKHOLE; 657 } 658 /* src address assigned by the caller? */ 659 if ((flags & RTF_SETSRC) && !IN6_IS_ADDR_UNSPECIFIED(src_addr)) 660 ire->ire_setsrc_addr_v6 = *src_addr; 661 662 nire = ire_add(ire); 663 if (nire == NULL) { 664 /* 665 * In the result of failure, ire_add() will have 666 * already deleted the ire in question, so there 667 * is no need to do that here. 668 */ 669 ipif_refrele(ipif); 670 return (ENOMEM); 671 } 672 /* 673 * Check if it was a duplicate entry. This handles 674 * the case of two racing route adds for the same route 675 */ 676 if (nire != ire) { 677 ASSERT(nire->ire_identical_ref > 1); 678 ire_delete(nire); 679 ire_refrele(nire); 680 ipif_refrele(ipif); 681 return (EEXIST); 682 } 683 ire = nire; 684 goto save_ire; 685 } 686 687 /* 688 * Get an interface IRE for the specified gateway. 689 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the 690 * gateway, it is currently unreachable and we fail the request 691 * accordingly. We reject any RTF_GATEWAY routes where the gateway 692 * is an IRE_LOCAL or IRE_LOOPBACK. 693 * If RTA_IFP was specified we look on that particular ill. 694 */ 695 if (ill != NULL) 696 match_flags |= MATCH_IRE_ILL; 697 698 /* Check whether the gateway is reachable. */ 699 again: 700 type = IRE_INTERFACE | IRE_LOCAL | IRE_LOOPBACK; 701 if (flags & RTF_INDIRECT) 702 type |= IRE_OFFLINK; 703 704 gw_ire = ire_ftable_lookup_v6(gw_addr, 0, 0, type, ill, 705 ALL_ZONES, NULL, match_flags, 0, ipst, NULL); 706 if (gw_ire == NULL) { 707 /* 708 * With IPMP, we allow host routes to influence in.mpathd's 709 * target selection. However, if the test addresses are on 710 * their own network, the above lookup will fail since the 711 * underlying IRE_INTERFACEs are marked hidden. So allow 712 * hidden test IREs to be found and try again. 713 */ 714 if (!(match_flags & MATCH_IRE_TESTHIDDEN)) { 715 match_flags |= MATCH_IRE_TESTHIDDEN; 716 goto again; 717 } 718 if (ipif != NULL) 719 ipif_refrele(ipif); 720 return (ENETUNREACH); 721 } 722 if (gw_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) { 723 ire_refrele(gw_ire); 724 if (ipif != NULL) 725 ipif_refrele(ipif); 726 return (ENETUNREACH); 727 } 728 if (ill == NULL && !(flags & RTF_INDIRECT)) { 729 unbound = B_TRUE; 730 if (ipst->ips_ipv6_strict_src_multihoming > 0) 731 ill = gw_ire->ire_ill; 732 } 733 734 /* 735 * We create one of three types of IREs as a result of this request 736 * based on the netmask. A netmask of all ones (which is automatically 737 * assumed when RTF_HOST is set) results in an IRE_HOST being created. 738 * An all zeroes netmask implies a default route so an IRE_DEFAULT is 739 * created. Otherwise, an IRE_PREFIX route is created for the 740 * destination prefix. 741 */ 742 if (IN6_ARE_ADDR_EQUAL(mask, &ipv6_all_ones)) 743 type = IRE_HOST; 744 else if (IN6_IS_ADDR_UNSPECIFIED(mask)) 745 type = IRE_DEFAULT; 746 else 747 type = IRE_PREFIX; 748 749 /* check for a duplicate entry */ 750 ire = ire_ftable_lookup_v6(dst_addr, mask, gw_addr, type, ill, 751 ALL_ZONES, NULL, 752 match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, 0, ipst, NULL); 753 if (ire != NULL) { 754 if (ipif != NULL) 755 ipif_refrele(ipif); 756 ire_refrele(gw_ire); 757 ire_refrele(ire); 758 return (EEXIST); 759 } 760 761 /* Security attribute exists */ 762 if (sp != NULL) { 763 tsol_gcgrp_addr_t ga; 764 765 /* find or create the gateway credentials group */ 766 ga.ga_af = AF_INET6; 767 ga.ga_addr = *gw_addr; 768 769 /* we hold reference to it upon success */ 770 gcgrp = gcgrp_lookup(&ga, B_TRUE); 771 if (gcgrp == NULL) { 772 if (ipif != NULL) 773 ipif_refrele(ipif); 774 ire_refrele(gw_ire); 775 return (ENOMEM); 776 } 777 778 /* 779 * Create and add the security attribute to the group; a 780 * reference to the group is made upon allocating a new 781 * entry successfully. If it finds an already-existing 782 * entry for the security attribute in the group, it simply 783 * returns it and no new reference is made to the group. 784 */ 785 gc = gc_create(sp, gcgrp, &gcgrp_xtraref); 786 if (gc == NULL) { 787 /* release reference held by gcgrp_lookup */ 788 GCGRP_REFRELE(gcgrp); 789 if (ipif != NULL) 790 ipif_refrele(ipif); 791 ire_refrele(gw_ire); 792 return (ENOMEM); 793 } 794 } 795 796 /* Create the IRE. */ 797 ire = ire_create_v6( 798 dst_addr, /* dest address */ 799 mask, /* mask */ 800 gw_addr, /* gateway address */ 801 (ushort_t)type, /* IRE type */ 802 ill, 803 zoneid, 804 flags, 805 gc, /* security attribute */ 806 ipst); 807 808 /* 809 * The ire holds a reference to the 'gc' and the 'gc' holds a 810 * reference to the 'gcgrp'. We can now release the extra reference 811 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used. 812 */ 813 if (gcgrp_xtraref) 814 GCGRP_REFRELE(gcgrp); 815 if (ire == NULL) { 816 if (gc != NULL) 817 GC_REFRELE(gc); 818 if (ipif != NULL) 819 ipif_refrele(ipif); 820 ire_refrele(gw_ire); 821 return (ENOMEM); 822 } 823 824 /* src address assigned by the caller? */ 825 if ((flags & RTF_SETSRC) && !IN6_IS_ADDR_UNSPECIFIED(src_addr)) 826 ire->ire_setsrc_addr_v6 = *src_addr; 827 828 ire->ire_unbound = unbound; 829 830 /* 831 * POLICY: should we allow an RTF_HOST with address INADDR_ANY? 832 * SUN/OS socket stuff does but do we really want to allow ::0 ? 833 */ 834 835 /* Add the new IRE. */ 836 nire = ire_add(ire); 837 if (nire == NULL) { 838 /* 839 * In the result of failure, ire_add() will have 840 * already deleted the ire in question, so there 841 * is no need to do that here. 842 */ 843 if (ipif != NULL) 844 ipif_refrele(ipif); 845 ire_refrele(gw_ire); 846 return (ENOMEM); 847 } 848 /* 849 * Check if it was a duplicate entry. This handles 850 * the case of two racing route adds for the same route 851 */ 852 if (nire != ire) { 853 ASSERT(nire->ire_identical_ref > 1); 854 ire_delete(nire); 855 ire_refrele(nire); 856 if (ipif != NULL) 857 ipif_refrele(ipif); 858 ire_refrele(gw_ire); 859 return (EEXIST); 860 } 861 ire = nire; 862 863 if (flags & RTF_MULTIRT) { 864 /* 865 * Invoke the CGTP (multirouting) filtering module 866 * to add the dst address in the filtering database. 867 * Replicated inbound packets coming from that address 868 * will be filtered to discard the duplicates. 869 * It is not necessary to call the CGTP filter hook 870 * when the dst address is a multicast, because an 871 * IP source address cannot be a multicast. 872 */ 873 if (ipst->ips_ip_cgtp_filter_ops != NULL && 874 !IN6_IS_ADDR_MULTICAST(&(ire->ire_addr_v6))) { 875 int res; 876 ipif_t *src_ipif; 877 878 /* Find the source address corresponding to gw_ire */ 879 src_ipif = ipif_lookup_addr_v6( 880 &gw_ire->ire_gateway_addr_v6, NULL, zoneid, ipst); 881 if (src_ipif != NULL) { 882 res = ipst->ips_ip_cgtp_filter_ops-> 883 cfo_add_dest_v6( 884 ipst->ips_netstack->netstack_stackid, 885 &ire->ire_addr_v6, 886 &ire->ire_gateway_addr_v6, 887 &ire->ire_setsrc_addr_v6, 888 &src_ipif->ipif_v6lcl_addr); 889 ipif_refrele(src_ipif); 890 } else { 891 res = EADDRNOTAVAIL; 892 } 893 if (res != 0) { 894 if (ipif != NULL) 895 ipif_refrele(ipif); 896 ire_refrele(gw_ire); 897 ire_delete(ire); 898 ire_refrele(ire); /* Held in ire_add */ 899 return (res); 900 } 901 } 902 } 903 904 save_ire: 905 if (gw_ire != NULL) { 906 ire_refrele(gw_ire); 907 gw_ire = NULL; 908 } 909 if (ire->ire_ill != NULL) { 910 /* 911 * Save enough information so that we can recreate the IRE if 912 * the ILL goes down and then up. The metrics associated 913 * with the route will be saved as well when rts_setmetrics() is 914 * called after the IRE has been created. In the case where 915 * memory cannot be allocated, none of this information will be 916 * saved. 917 */ 918 ill_save_ire(ire->ire_ill, ire); 919 } 920 921 if (ire_arg != NULL) { 922 /* 923 * Store the ire that was successfully added into where ire_arg 924 * points to so that callers don't have to look it up 925 * themselves (but they are responsible for ire_refrele()ing 926 * the ire when they are finished with it). 927 */ 928 *ire_arg = ire; 929 } else { 930 ire_refrele(ire); /* Held in ire_add */ 931 } 932 if (ipif != NULL) 933 ipif_refrele(ipif); 934 return (0); 935 } 936 937 /* 938 * ip_rt_delete_v6 is called to delete an IPv6 route. 939 * ill is passed in to associate it with the correct interface. 940 * (for link-local destinations and gateways). 941 */ 942 /* ARGSUSED4 */ 943 int 944 ip_rt_delete_v6(const in6_addr_t *dst_addr, const in6_addr_t *mask, 945 const in6_addr_t *gw_addr, uint_t rtm_addrs, int flags, ill_t *ill, 946 ip_stack_t *ipst, zoneid_t zoneid) 947 { 948 ire_t *ire = NULL; 949 ipif_t *ipif; 950 uint_t type; 951 uint_t match_flags = MATCH_IRE_TYPE; 952 int err = 0; 953 954 /* 955 * If this is the case of RTF_HOST being set, then we set the netmask 956 * to all ones. Otherwise, we use the netmask if one was supplied. 957 */ 958 if (flags & RTF_HOST) { 959 mask = &ipv6_all_ones; 960 match_flags |= MATCH_IRE_MASK; 961 } else if (rtm_addrs & RTA_NETMASK) { 962 match_flags |= MATCH_IRE_MASK; 963 } 964 965 /* 966 * Note that RTF_GATEWAY is never set on a delete, therefore 967 * we check if the gateway address is one of our interfaces first, 968 * and fall back on RTF_GATEWAY routes. 969 * 970 * This makes it possible to delete an original 971 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1. 972 * However, we have RTF_KERNEL set on the ones created by ipif_up 973 * and those can not be deleted here. 974 * 975 * We use MATCH_IRE_ILL if we know the interface. If the caller 976 * specified an interface (from the RTA_IFP sockaddr) we use it, 977 * otherwise we use the ill derived from the gateway address. 978 * We can always match the gateway address since we record it 979 * in ire_gateway_addr. 980 * 981 * For more detail on specifying routes by gateway address and by 982 * interface index, see the comments in ip_rt_add_v6(). 983 */ 984 ipif = ipif_lookup_interface_v6(gw_addr, dst_addr, ipst); 985 if (ipif != NULL) { 986 ill_t *ill_match; 987 988 if (ill != NULL) 989 ill_match = ill; 990 else 991 ill_match = ipif->ipif_ill; 992 993 match_flags |= MATCH_IRE_ILL; 994 if (ipif->ipif_ire_type == IRE_LOOPBACK) { 995 ire = ire_ftable_lookup_v6(dst_addr, 0, 0, IRE_LOOPBACK, 996 ill_match, ALL_ZONES, NULL, match_flags, 0, ipst, 997 NULL); 998 } 999 if (ire == NULL) { 1000 match_flags |= MATCH_IRE_GW; 1001 ire = ire_ftable_lookup_v6(dst_addr, mask, gw_addr, 1002 IRE_INTERFACE, ill_match, ALL_ZONES, NULL, 1003 match_flags, 0, ipst, NULL); 1004 } 1005 /* Avoid deleting routes created by kernel from an ipif */ 1006 if (ire != NULL && (ire->ire_flags & RTF_KERNEL)) { 1007 ire_refrele(ire); 1008 ire = NULL; 1009 } 1010 1011 /* Restore in case we didn't find a match */ 1012 match_flags &= ~(MATCH_IRE_GW|MATCH_IRE_ILL); 1013 } 1014 1015 if (ire == NULL) { 1016 /* 1017 * At this point, the gateway address is not one of our own 1018 * addresses or a matching interface route was not found. We 1019 * set the IRE type to lookup based on whether 1020 * this is a host route, a default route or just a prefix. 1021 * 1022 * If an ill was passed in, then the lookup is based on an 1023 * interface index so MATCH_IRE_ILL is added to match_flags. 1024 */ 1025 match_flags |= MATCH_IRE_GW; 1026 if (ill != NULL) 1027 match_flags |= MATCH_IRE_ILL; 1028 if (IN6_ARE_ADDR_EQUAL(mask, &ipv6_all_ones)) 1029 type = IRE_HOST; 1030 else if (IN6_IS_ADDR_UNSPECIFIED(mask)) 1031 type = IRE_DEFAULT; 1032 else 1033 type = IRE_PREFIX; 1034 ire = ire_ftable_lookup_v6(dst_addr, mask, gw_addr, type, 1035 ill, ALL_ZONES, NULL, match_flags, 0, ipst, NULL); 1036 } 1037 1038 if (ipif != NULL) { 1039 ipif_refrele(ipif); 1040 ipif = NULL; 1041 } 1042 if (ire == NULL) 1043 return (ESRCH); 1044 1045 if (ire->ire_flags & RTF_MULTIRT) { 1046 /* 1047 * Invoke the CGTP (multirouting) filtering module 1048 * to remove the dst address from the filtering database. 1049 * Packets coming from that address will no longer be 1050 * filtered to remove duplicates. 1051 */ 1052 if (ipst->ips_ip_cgtp_filter_ops != NULL) { 1053 err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v6( 1054 ipst->ips_netstack->netstack_stackid, 1055 &ire->ire_addr_v6, &ire->ire_gateway_addr_v6); 1056 } 1057 } 1058 1059 ill = ire->ire_ill; 1060 if (ill != NULL) 1061 ill_remove_saved_ire(ill, ire); 1062 ire_delete(ire); 1063 ire_refrele(ire); 1064 return (err); 1065 } 1066 1067 /* 1068 * Derive an interface id from the link layer address. 1069 */ 1070 void 1071 ill_setdefaulttoken(ill_t *ill) 1072 { 1073 if (!ill->ill_manual_token) { 1074 bzero(&ill->ill_token, sizeof (ill->ill_token)); 1075 MEDIA_V6INTFID(ill->ill_media, ill, &ill->ill_token); 1076 ill->ill_token_length = IPV6_TOKEN_LEN; 1077 } 1078 } 1079 1080 void 1081 ill_setdesttoken(ill_t *ill) 1082 { 1083 bzero(&ill->ill_dest_token, sizeof (ill->ill_dest_token)); 1084 MEDIA_V6DESTINTFID(ill->ill_media, ill, &ill->ill_dest_token); 1085 } 1086 1087 /* 1088 * Create a link-local address from a token. 1089 */ 1090 static void 1091 ipif_get_linklocal(in6_addr_t *dest, const in6_addr_t *token) 1092 { 1093 int i; 1094 1095 for (i = 0; i < 4; i++) { 1096 dest->s6_addr32[i] = 1097 token->s6_addr32[i] | ipv6_ll_template.s6_addr32[i]; 1098 } 1099 } 1100 1101 /* 1102 * Set a default IPv6 address for a 6to4 tunnel interface 2002:<tsrc>::1/16 1103 */ 1104 static void 1105 ipif_set6to4addr(ipif_t *ipif) 1106 { 1107 ill_t *ill = ipif->ipif_ill; 1108 struct in_addr v4phys; 1109 1110 ASSERT(ill->ill_mactype == DL_6TO4); 1111 ASSERT(ill->ill_phys_addr_length == sizeof (struct in_addr)); 1112 ASSERT(ipif->ipif_isv6); 1113 1114 if (ipif->ipif_flags & IPIF_UP) 1115 return; 1116 1117 (void) ip_plen_to_mask_v6(16, &ipif->ipif_v6net_mask); 1118 bcopy(ill->ill_phys_addr, &v4phys, sizeof (struct in_addr)); 1119 IN6_V4ADDR_TO_6TO4(&v4phys, &ipif->ipif_v6lcl_addr); 1120 V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask, 1121 ipif->ipif_v6subnet); 1122 } 1123 1124 /* 1125 * Is it not possible to set the link local address? 1126 * The address can be set if the token is set, and the token 1127 * isn't too long. 1128 * Return B_TRUE if the address can't be set, or B_FALSE if it can. 1129 */ 1130 boolean_t 1131 ipif_cant_setlinklocal(ipif_t *ipif) 1132 { 1133 ill_t *ill = ipif->ipif_ill; 1134 1135 if (IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token) || 1136 ill->ill_token_length > IPV6_ABITS - IPV6_LL_PREFIXLEN) 1137 return (B_TRUE); 1138 1139 return (B_FALSE); 1140 } 1141 1142 /* 1143 * Generate a link-local address from the token. 1144 */ 1145 void 1146 ipif_setlinklocal(ipif_t *ipif) 1147 { 1148 ill_t *ill = ipif->ipif_ill; 1149 in6_addr_t ov6addr; 1150 1151 ASSERT(IAM_WRITER_ILL(ill)); 1152 1153 /* 1154 * ill_manual_linklocal is set when the link-local address was 1155 * manually configured. 1156 */ 1157 if (ill->ill_manual_linklocal) 1158 return; 1159 1160 /* 1161 * IPv6 interfaces over 6to4 tunnels are special. They do not have 1162 * link-local addresses, but instead have a single automatically 1163 * generated global address. 1164 */ 1165 if (ill->ill_mactype == DL_6TO4) { 1166 ipif_set6to4addr(ipif); 1167 return; 1168 } 1169 1170 if (ipif_cant_setlinklocal(ipif)) 1171 return; 1172 1173 ov6addr = ipif->ipif_v6lcl_addr; 1174 ipif_get_linklocal(&ipif->ipif_v6lcl_addr, &ill->ill_token); 1175 sctp_update_ipif_addr(ipif, ov6addr); 1176 (void) ip_plen_to_mask_v6(IPV6_LL_PREFIXLEN, &ipif->ipif_v6net_mask); 1177 if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)) { 1178 V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask, 1179 ipif->ipif_v6subnet); 1180 } 1181 1182 ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT); 1183 } 1184 1185 /* 1186 * Generate a destination link-local address for a point-to-point IPv6 1187 * interface with a destination interface id (IP tunnels are such interfaces) 1188 * based on the destination token. 1189 */ 1190 void 1191 ipif_setdestlinklocal(ipif_t *ipif) 1192 { 1193 ill_t *ill = ipif->ipif_ill; 1194 1195 ASSERT(IAM_WRITER_ILL(ill)); 1196 1197 if (ill->ill_manual_dst_linklocal) 1198 return; 1199 1200 if (IN6_IS_ADDR_UNSPECIFIED(&ill->ill_dest_token)) 1201 return; 1202 1203 ipif_get_linklocal(&ipif->ipif_v6pp_dst_addr, &ill->ill_dest_token); 1204 ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr; 1205 } 1206 1207 /* 1208 * Get the resolver set up for a new ipif. (Always called as writer.) 1209 */ 1210 int 1211 ipif_ndp_up(ipif_t *ipif, boolean_t initial) 1212 { 1213 ill_t *ill = ipif->ipif_ill; 1214 int err = 0; 1215 nce_t *nce = NULL; 1216 boolean_t added_ipif = B_FALSE; 1217 1218 DTRACE_PROBE3(ipif__downup, char *, "ipif_ndp_up", 1219 ill_t *, ill, ipif_t *, ipif); 1220 ip1dbg(("ipif_ndp_up(%s:%u)\n", ill->ill_name, ipif->ipif_id)); 1221 1222 if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) || 1223 (!(ill->ill_net_type & IRE_INTERFACE))) { 1224 ipif->ipif_addr_ready = 1; 1225 return (0); 1226 } 1227 1228 if ((ipif->ipif_flags & (IPIF_UNNUMBERED|IPIF_NOLOCAL)) == 0) { 1229 uint16_t flags; 1230 uint16_t state; 1231 uchar_t *hw_addr; 1232 ill_t *bound_ill; 1233 ipmp_illgrp_t *illg = ill->ill_grp; 1234 uint_t hw_addr_len; 1235 1236 flags = NCE_F_MYADDR | NCE_F_NONUD | NCE_F_PUBLISH | 1237 NCE_F_AUTHORITY; 1238 if (ill->ill_flags & ILLF_ROUTER) 1239 flags |= NCE_F_ISROUTER; 1240 1241 if (ipif->ipif_flags & IPIF_ANYCAST) 1242 flags |= NCE_F_ANYCAST; 1243 1244 if (IS_IPMP(ill)) { 1245 ASSERT(ill->ill_net_type == IRE_IF_RESOLVER); 1246 /* 1247 * If we're here via ipif_up(), then the ipif won't be 1248 * bound yet -- add it to the group, which will bind 1249 * it if possible. (We would add it in ipif_up(), but 1250 * deleting on failure there is gruesome.) If we're 1251 * here via ipmp_ill_bind_ipif(), then the ipif has 1252 * already been added to the group and we just need to 1253 * use the binding. 1254 */ 1255 if ((bound_ill = ipmp_ipif_bound_ill(ipif)) == NULL) { 1256 bound_ill = ipmp_illgrp_add_ipif(illg, ipif); 1257 if (bound_ill == NULL) { 1258 /* 1259 * We couldn't bind the ipif to an ill 1260 * yet, so we have nothing to publish. 1261 * Set ipif_addr_ready so that this 1262 * address can be used locally for now. 1263 * The routing socket message will be 1264 * sent from ipif_up_done_v6(). 1265 */ 1266 ipif->ipif_addr_ready = 1; 1267 return (0); 1268 } 1269 added_ipif = B_TRUE; 1270 } 1271 hw_addr = bound_ill->ill_nd_lla; 1272 hw_addr_len = bound_ill->ill_phys_addr_length; 1273 } else { 1274 bound_ill = ill; 1275 hw_addr = ill->ill_nd_lla; 1276 hw_addr_len = ill->ill_phys_addr_length; 1277 } 1278 1279 /* 1280 * If this is an initial bring-up (or the ipif was never 1281 * completely brought up), do DAD. Otherwise, we're here 1282 * because IPMP has rebound an address to this ill: send 1283 * unsolicited advertisements to inform others. 1284 */ 1285 if (initial || !ipif->ipif_addr_ready) { 1286 /* Causes Duplicate Address Detection to run */ 1287 state = ND_PROBE; 1288 } else { 1289 state = ND_REACHABLE; 1290 flags |= NCE_F_UNSOL_ADV; 1291 } 1292 1293 retry: 1294 err = nce_lookup_then_add_v6(ill, hw_addr, hw_addr_len, 1295 &ipif->ipif_v6lcl_addr, flags, state, &nce); 1296 switch (err) { 1297 case 0: 1298 ip1dbg(("ipif_ndp_up: NCE created for %s\n", 1299 ill->ill_name)); 1300 ipif->ipif_addr_ready = 1; 1301 ipif->ipif_added_nce = 1; 1302 nce->nce_ipif_cnt++; 1303 break; 1304 case EINPROGRESS: 1305 ip1dbg(("ipif_ndp_up: running DAD now for %s\n", 1306 ill->ill_name)); 1307 ipif->ipif_added_nce = 1; 1308 nce->nce_ipif_cnt++; 1309 break; 1310 case EEXIST: 1311 ip1dbg(("ipif_ndp_up: NCE already exists for %s\n", 1312 ill->ill_name)); 1313 if (!NCE_MYADDR(nce->nce_common)) { 1314 /* 1315 * A leftover nce from before this address 1316 * existed 1317 */ 1318 ncec_delete(nce->nce_common); 1319 nce_refrele(nce); 1320 nce = NULL; 1321 goto retry; 1322 } 1323 if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) { 1324 nce_refrele(nce); 1325 nce = NULL; 1326 ip1dbg(("ipif_ndp_up: NCE already exists " 1327 "for %s\n", ill->ill_name)); 1328 goto fail; 1329 } 1330 /* 1331 * Duplicate local addresses are permissible for 1332 * IPIF_POINTOPOINT interfaces which will get marked 1333 * IPIF_UNNUMBERED later in 1334 * ip_addr_availability_check(). 1335 * 1336 * The nce_ipif_cnt field tracks the number of 1337 * ipifs that have nce_addr as their local address. 1338 */ 1339 ipif->ipif_addr_ready = 1; 1340 ipif->ipif_added_nce = 1; 1341 nce->nce_ipif_cnt++; 1342 err = 0; 1343 break; 1344 default: 1345 ip1dbg(("ipif_ndp_up: NCE creation failed for %s\n", 1346 ill->ill_name)); 1347 goto fail; 1348 } 1349 } else { 1350 /* No local NCE for this entry */ 1351 ipif->ipif_addr_ready = 1; 1352 } 1353 if (nce != NULL) 1354 nce_refrele(nce); 1355 return (0); 1356 fail: 1357 if (added_ipif) 1358 ipmp_illgrp_del_ipif(ill->ill_grp, ipif); 1359 1360 return (err); 1361 } 1362 1363 /* Remove all cache entries for this logical interface */ 1364 void 1365 ipif_ndp_down(ipif_t *ipif) 1366 { 1367 ipif_nce_down(ipif); 1368 } 1369 1370 /* 1371 * Return the scope of the given IPv6 address. If the address is an 1372 * IPv4 mapped IPv6 address, return the scope of the corresponding 1373 * IPv4 address. 1374 */ 1375 in6addr_scope_t 1376 ip_addr_scope_v6(const in6_addr_t *addr) 1377 { 1378 static in6_addr_t ipv6loopback = IN6ADDR_LOOPBACK_INIT; 1379 1380 if (IN6_IS_ADDR_V4MAPPED(addr)) { 1381 in_addr_t v4addr_h = ntohl(V4_PART_OF_V6((*addr))); 1382 if ((v4addr_h >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 1383 (v4addr_h & IN_AUTOCONF_MASK) == IN_AUTOCONF_NET) 1384 return (IP6_SCOPE_LINKLOCAL); 1385 if ((v4addr_h & IN_PRIVATE8_MASK) == IN_PRIVATE8_NET || 1386 (v4addr_h & IN_PRIVATE12_MASK) == IN_PRIVATE12_NET || 1387 (v4addr_h & IN_PRIVATE16_MASK) == IN_PRIVATE16_NET) 1388 return (IP6_SCOPE_SITELOCAL); 1389 return (IP6_SCOPE_GLOBAL); 1390 } 1391 1392 if (IN6_IS_ADDR_MULTICAST(addr)) 1393 return (IN6_ADDR_MC_SCOPE(addr)); 1394 1395 /* link-local and loopback addresses are of link-local scope */ 1396 if (IN6_IS_ADDR_LINKLOCAL(addr) || 1397 IN6_ARE_ADDR_EQUAL(addr, &ipv6loopback)) 1398 return (IP6_SCOPE_LINKLOCAL); 1399 if (IN6_IS_ADDR_SITELOCAL(addr)) 1400 return (IP6_SCOPE_SITELOCAL); 1401 return (IP6_SCOPE_GLOBAL); 1402 } 1403 1404 1405 /* 1406 * Returns the length of the common prefix of a1 and a2, as per 1407 * CommonPrefixLen() defined in RFC 3484. 1408 */ 1409 static int 1410 ip_common_prefix_v6(const in6_addr_t *a1, const in6_addr_t *a2) 1411 { 1412 int i; 1413 uint32_t a1val, a2val, mask; 1414 1415 for (i = 0; i < 4; i++) { 1416 if ((a1val = a1->s6_addr32[i]) != (a2val = a2->s6_addr32[i])) { 1417 a1val ^= a2val; 1418 i *= 32; 1419 mask = 0x80000000u; 1420 while (!(a1val & mask)) { 1421 mask >>= 1; 1422 i++; 1423 } 1424 return (i); 1425 } 1426 } 1427 return (IPV6_ABITS); 1428 } 1429 1430 #define IPIF_VALID_IPV6_SOURCE(ipif) \ 1431 (((ipif)->ipif_flags & IPIF_UP) && \ 1432 !((ipif)->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST)) && \ 1433 !((ipif)->ipif_ill->ill_flags & ILLF_NOACCEPT)) 1434 1435 /* source address candidate */ 1436 typedef struct candidate { 1437 ipif_t *cand_ipif; 1438 /* The properties of this candidate */ 1439 boolean_t cand_isdst; 1440 boolean_t cand_isdst_set; 1441 in6addr_scope_t cand_scope; 1442 boolean_t cand_scope_set; 1443 boolean_t cand_isdeprecated; 1444 boolean_t cand_isdeprecated_set; 1445 boolean_t cand_ispreferred; 1446 boolean_t cand_ispreferred_set; 1447 boolean_t cand_matchedinterface; 1448 boolean_t cand_matchedinterface_set; 1449 boolean_t cand_matchedlabel; 1450 boolean_t cand_matchedlabel_set; 1451 boolean_t cand_istmp; 1452 boolean_t cand_istmp_set; 1453 int cand_common_pref; 1454 boolean_t cand_common_pref_set; 1455 boolean_t cand_pref_eq; 1456 boolean_t cand_pref_eq_set; 1457 int cand_pref_len; 1458 boolean_t cand_pref_len_set; 1459 } cand_t; 1460 #define cand_srcaddr cand_ipif->ipif_v6lcl_addr 1461 #define cand_mask cand_ipif->ipif_v6net_mask 1462 #define cand_flags cand_ipif->ipif_flags 1463 #define cand_ill cand_ipif->ipif_ill 1464 #define cand_zoneid cand_ipif->ipif_zoneid 1465 1466 /* information about the destination for source address selection */ 1467 typedef struct dstinfo { 1468 const in6_addr_t *dst_addr; 1469 ill_t *dst_ill; 1470 uint_t dst_restrict_ill; 1471 boolean_t dst_prefer_src_tmp; 1472 in6addr_scope_t dst_scope; 1473 char *dst_label; 1474 } dstinfo_t; 1475 1476 /* 1477 * The following functions are rules used to select a source address in 1478 * ipif_select_source_v6(). Each rule compares a current candidate (cc) 1479 * against the best candidate (bc). Each rule has three possible outcomes; 1480 * the candidate is preferred over the best candidate (CAND_PREFER), the 1481 * candidate is not preferred over the best candidate (CAND_AVOID), or the 1482 * candidate is of equal value as the best candidate (CAND_TIE). 1483 * 1484 * These rules are part of a greater "Default Address Selection for IPv6" 1485 * sheme, which is standards based work coming out of the IETF ipv6 working 1486 * group. The IETF document defines both IPv6 source address selection and 1487 * destination address ordering. The rules defined here implement the IPv6 1488 * source address selection. Destination address ordering is done by 1489 * libnsl, and uses a similar set of rules to implement the sorting. 1490 * 1491 * Most of the rules are defined by the RFC and are not typically altered. The 1492 * last rule, number 8, has language that allows for local preferences. In the 1493 * scheme below, this means that new Solaris rules should normally go between 1494 * rule_ifprefix and rule_prefix. 1495 */ 1496 typedef enum {CAND_AVOID, CAND_TIE, CAND_PREFER} rule_res_t; 1497 typedef rule_res_t (*rulef_t)(cand_t *, cand_t *, const dstinfo_t *, 1498 ip_stack_t *); 1499 1500 /* Prefer an address if it is equal to the destination address. */ 1501 /* ARGSUSED3 */ 1502 static rule_res_t 1503 rule_isdst(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, ip_stack_t *ipst) 1504 { 1505 if (!bc->cand_isdst_set) { 1506 bc->cand_isdst = 1507 IN6_ARE_ADDR_EQUAL(&bc->cand_srcaddr, dstinfo->dst_addr); 1508 bc->cand_isdst_set = B_TRUE; 1509 } 1510 1511 cc->cand_isdst = 1512 IN6_ARE_ADDR_EQUAL(&cc->cand_srcaddr, dstinfo->dst_addr); 1513 cc->cand_isdst_set = B_TRUE; 1514 1515 if (cc->cand_isdst == bc->cand_isdst) 1516 return (CAND_TIE); 1517 else if (cc->cand_isdst) 1518 return (CAND_PREFER); 1519 else 1520 return (CAND_AVOID); 1521 } 1522 1523 /* 1524 * Prefer addresses that are of closest scope to the destination. Always 1525 * prefer addresses that are of greater scope than the destination over 1526 * those that are of lesser scope than the destination. 1527 */ 1528 /* ARGSUSED3 */ 1529 static rule_res_t 1530 rule_scope(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, ip_stack_t *ipst) 1531 { 1532 if (!bc->cand_scope_set) { 1533 bc->cand_scope = ip_addr_scope_v6(&bc->cand_srcaddr); 1534 bc->cand_scope_set = B_TRUE; 1535 } 1536 1537 cc->cand_scope = ip_addr_scope_v6(&cc->cand_srcaddr); 1538 cc->cand_scope_set = B_TRUE; 1539 1540 if (cc->cand_scope < bc->cand_scope) { 1541 if (cc->cand_scope < dstinfo->dst_scope) 1542 return (CAND_AVOID); 1543 else 1544 return (CAND_PREFER); 1545 } else if (bc->cand_scope < cc->cand_scope) { 1546 if (bc->cand_scope < dstinfo->dst_scope) 1547 return (CAND_PREFER); 1548 else 1549 return (CAND_AVOID); 1550 } else { 1551 return (CAND_TIE); 1552 } 1553 } 1554 1555 /* 1556 * Prefer non-deprecated source addresses. 1557 */ 1558 /* ARGSUSED2 */ 1559 static rule_res_t 1560 rule_deprecated(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, 1561 ip_stack_t *ipst) 1562 { 1563 if (!bc->cand_isdeprecated_set) { 1564 bc->cand_isdeprecated = 1565 ((bc->cand_flags & IPIF_DEPRECATED) != 0); 1566 bc->cand_isdeprecated_set = B_TRUE; 1567 } 1568 1569 cc->cand_isdeprecated = ((cc->cand_flags & IPIF_DEPRECATED) != 0); 1570 cc->cand_isdeprecated_set = B_TRUE; 1571 1572 if (bc->cand_isdeprecated == cc->cand_isdeprecated) 1573 return (CAND_TIE); 1574 else if (cc->cand_isdeprecated) 1575 return (CAND_AVOID); 1576 else 1577 return (CAND_PREFER); 1578 } 1579 1580 /* 1581 * Prefer source addresses that have the IPIF_PREFERRED flag set. This 1582 * rule must be before rule_interface because the flag could be set on any 1583 * interface, not just the interface being used for outgoing packets (for 1584 * example, the IFF_PREFERRED could be set on an address assigned to the 1585 * loopback interface). 1586 */ 1587 /* ARGSUSED2 */ 1588 static rule_res_t 1589 rule_preferred(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, 1590 ip_stack_t *ipst) 1591 { 1592 if (!bc->cand_ispreferred_set) { 1593 bc->cand_ispreferred = ((bc->cand_flags & IPIF_PREFERRED) != 0); 1594 bc->cand_ispreferred_set = B_TRUE; 1595 } 1596 1597 cc->cand_ispreferred = ((cc->cand_flags & IPIF_PREFERRED) != 0); 1598 cc->cand_ispreferred_set = B_TRUE; 1599 1600 if (bc->cand_ispreferred == cc->cand_ispreferred) 1601 return (CAND_TIE); 1602 else if (cc->cand_ispreferred) 1603 return (CAND_PREFER); 1604 else 1605 return (CAND_AVOID); 1606 } 1607 1608 /* 1609 * Prefer source addresses that are assigned to the outgoing interface. 1610 */ 1611 /* ARGSUSED3 */ 1612 static rule_res_t 1613 rule_interface(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, 1614 ip_stack_t *ipst) 1615 { 1616 ill_t *dstill = dstinfo->dst_ill; 1617 1618 /* 1619 * If dstinfo->dst_restrict_ill is set, this rule is unnecessary 1620 * since we know all candidates will be on the same link. 1621 */ 1622 if (dstinfo->dst_restrict_ill) 1623 return (CAND_TIE); 1624 1625 if (!bc->cand_matchedinterface_set) { 1626 bc->cand_matchedinterface = bc->cand_ill == dstill; 1627 bc->cand_matchedinterface_set = B_TRUE; 1628 } 1629 1630 cc->cand_matchedinterface = cc->cand_ill == dstill; 1631 cc->cand_matchedinterface_set = B_TRUE; 1632 1633 if (bc->cand_matchedinterface == cc->cand_matchedinterface) 1634 return (CAND_TIE); 1635 else if (cc->cand_matchedinterface) 1636 return (CAND_PREFER); 1637 else 1638 return (CAND_AVOID); 1639 } 1640 1641 /* 1642 * Prefer source addresses whose label matches the destination's label. 1643 */ 1644 static rule_res_t 1645 rule_label(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, ip_stack_t *ipst) 1646 { 1647 char *label; 1648 1649 if (!bc->cand_matchedlabel_set) { 1650 label = ip6_asp_lookup(&bc->cand_srcaddr, NULL, ipst); 1651 bc->cand_matchedlabel = 1652 ip6_asp_labelcmp(label, dstinfo->dst_label); 1653 bc->cand_matchedlabel_set = B_TRUE; 1654 } 1655 1656 label = ip6_asp_lookup(&cc->cand_srcaddr, NULL, ipst); 1657 cc->cand_matchedlabel = ip6_asp_labelcmp(label, dstinfo->dst_label); 1658 cc->cand_matchedlabel_set = B_TRUE; 1659 1660 if (bc->cand_matchedlabel == cc->cand_matchedlabel) 1661 return (CAND_TIE); 1662 else if (cc->cand_matchedlabel) 1663 return (CAND_PREFER); 1664 else 1665 return (CAND_AVOID); 1666 } 1667 1668 /* 1669 * Prefer public addresses over temporary ones. An application can reverse 1670 * the logic of this rule and prefer temporary addresses by using the 1671 * IPV6_SRC_PREFERENCES socket option. 1672 */ 1673 /* ARGSUSED3 */ 1674 static rule_res_t 1675 rule_temporary(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, 1676 ip_stack_t *ipst) 1677 { 1678 if (!bc->cand_istmp_set) { 1679 bc->cand_istmp = ((bc->cand_flags & IPIF_TEMPORARY) != 0); 1680 bc->cand_istmp_set = B_TRUE; 1681 } 1682 1683 cc->cand_istmp = ((cc->cand_flags & IPIF_TEMPORARY) != 0); 1684 cc->cand_istmp_set = B_TRUE; 1685 1686 if (bc->cand_istmp == cc->cand_istmp) 1687 return (CAND_TIE); 1688 1689 if (dstinfo->dst_prefer_src_tmp && cc->cand_istmp) 1690 return (CAND_PREFER); 1691 else if (!dstinfo->dst_prefer_src_tmp && !cc->cand_istmp) 1692 return (CAND_PREFER); 1693 else 1694 return (CAND_AVOID); 1695 } 1696 1697 /* 1698 * Prefer source addresses with longer matching prefix with the destination 1699 * under the interface mask. This gets us on the same subnet before applying 1700 * any Solaris-specific rules. 1701 */ 1702 /* ARGSUSED3 */ 1703 static rule_res_t 1704 rule_ifprefix(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, 1705 ip_stack_t *ipst) 1706 { 1707 if (!bc->cand_pref_eq_set) { 1708 bc->cand_pref_eq = V6_MASK_EQ_2(bc->cand_srcaddr, 1709 bc->cand_mask, *dstinfo->dst_addr); 1710 bc->cand_pref_eq_set = B_TRUE; 1711 } 1712 1713 cc->cand_pref_eq = V6_MASK_EQ_2(cc->cand_srcaddr, cc->cand_mask, 1714 *dstinfo->dst_addr); 1715 cc->cand_pref_eq_set = B_TRUE; 1716 1717 if (bc->cand_pref_eq) { 1718 if (cc->cand_pref_eq) { 1719 if (!bc->cand_pref_len_set) { 1720 bc->cand_pref_len = 1721 ip_mask_to_plen_v6(&bc->cand_mask); 1722 bc->cand_pref_len_set = B_TRUE; 1723 } 1724 cc->cand_pref_len = ip_mask_to_plen_v6(&cc->cand_mask); 1725 cc->cand_pref_len_set = B_TRUE; 1726 if (bc->cand_pref_len == cc->cand_pref_len) 1727 return (CAND_TIE); 1728 else if (bc->cand_pref_len > cc->cand_pref_len) 1729 return (CAND_AVOID); 1730 else 1731 return (CAND_PREFER); 1732 } else { 1733 return (CAND_AVOID); 1734 } 1735 } else { 1736 if (cc->cand_pref_eq) 1737 return (CAND_PREFER); 1738 else 1739 return (CAND_TIE); 1740 } 1741 } 1742 1743 /* 1744 * Prefer to use zone-specific addresses when possible instead of all-zones 1745 * addresses. 1746 */ 1747 /* ARGSUSED2 */ 1748 static rule_res_t 1749 rule_zone_specific(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, 1750 ip_stack_t *ipst) 1751 { 1752 if ((bc->cand_zoneid == ALL_ZONES) == 1753 (cc->cand_zoneid == ALL_ZONES)) 1754 return (CAND_TIE); 1755 else if (cc->cand_zoneid == ALL_ZONES) 1756 return (CAND_AVOID); 1757 else 1758 return (CAND_PREFER); 1759 } 1760 1761 /* 1762 * Prefer to use DHCPv6 (first) and static addresses (second) when possible 1763 * instead of statelessly autoconfigured addresses. 1764 * 1765 * This is done after trying all other preferences (and before the final tie 1766 * breaker) so that, if all else is equal, we select addresses configured by 1767 * DHCPv6 over other addresses. We presume that DHCPv6 addresses, unlike 1768 * stateless autoconfigured addresses, are deliberately configured by an 1769 * administrator, and thus are correctly set up in DNS and network packet 1770 * filters. 1771 */ 1772 /* ARGSUSED2 */ 1773 static rule_res_t 1774 rule_addr_type(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, 1775 ip_stack_t *ipst) 1776 { 1777 #define ATYPE(x) \ 1778 ((x) & IPIF_DHCPRUNNING) ? 1 : ((x) & IPIF_ADDRCONF) ? 3 : 2 1779 int bcval = ATYPE(bc->cand_flags); 1780 int ccval = ATYPE(cc->cand_flags); 1781 #undef ATYPE 1782 1783 if (bcval == ccval) 1784 return (CAND_TIE); 1785 else if (ccval < bcval) 1786 return (CAND_PREFER); 1787 else 1788 return (CAND_AVOID); 1789 } 1790 1791 /* 1792 * Prefer source addresses with longer matching prefix with the destination. 1793 * We do the longest matching prefix calculation by doing an xor of both 1794 * addresses with the destination, and pick the address with the longest string 1795 * of leading zeros, as per CommonPrefixLen() defined in RFC 3484. 1796 */ 1797 /* ARGSUSED3 */ 1798 static rule_res_t 1799 rule_prefix(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, ip_stack_t *ipst) 1800 { 1801 if (!bc->cand_common_pref_set) { 1802 bc->cand_common_pref = ip_common_prefix_v6(&bc->cand_srcaddr, 1803 dstinfo->dst_addr); 1804 bc->cand_common_pref_set = B_TRUE; 1805 } 1806 1807 cc->cand_common_pref = ip_common_prefix_v6(&cc->cand_srcaddr, 1808 dstinfo->dst_addr); 1809 cc->cand_common_pref_set = B_TRUE; 1810 1811 if (bc->cand_common_pref == cc->cand_common_pref) 1812 return (CAND_TIE); 1813 else if (bc->cand_common_pref > cc->cand_common_pref) 1814 return (CAND_AVOID); 1815 else 1816 return (CAND_PREFER); 1817 } 1818 1819 /* 1820 * Last rule: we must pick something, so just prefer the current best 1821 * candidate. 1822 */ 1823 /* ARGSUSED */ 1824 static rule_res_t 1825 rule_must_be_last(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, 1826 ip_stack_t *ipst) 1827 { 1828 return (CAND_AVOID); 1829 } 1830 1831 /* 1832 * Determine the best source address given a destination address and a 1833 * destination ill. If no suitable source address is found, it returns 1834 * NULL. If there is a usable address pointed to by the usesrc 1835 * (i.e ill_usesrc_ifindex != 0) then return that first since it is more 1836 * fine grained (i.e per interface) 1837 * 1838 * This implementation is based on the "Default Address Selection for IPv6" 1839 * specification produced by the IETF IPv6 working group. It has been 1840 * implemented so that the list of addresses is only traversed once (the 1841 * specification's algorithm could traverse the list of addresses once for 1842 * every rule). 1843 * 1844 * The restrict_ill argument restricts the algorithm to choose a source 1845 * address that is assigned to the destination ill. This is used when 1846 * the destination address is a link-local or multicast address, and when 1847 * ipv6_strict_dst_multihoming is turned on. 1848 * 1849 * src_prefs is the caller's set of source address preferences. If source 1850 * address selection is being called to determine the source address of a 1851 * connected socket (from ip_set_destination_v6()), then the preferences are 1852 * taken from conn_ixa->ixa_src_preferences. These preferences can be set on a 1853 * per-socket basis using the IPV6_SRC_PREFERENCES socket option. The only 1854 * preference currently implemented is for rfc3041 temporary addresses. 1855 */ 1856 ipif_t * 1857 ipif_select_source_v6(ill_t *dstill, const in6_addr_t *dst, 1858 boolean_t restrict_ill, uint32_t src_prefs, zoneid_t zoneid, 1859 boolean_t allow_usesrc, boolean_t *notreadyp) 1860 { 1861 dstinfo_t dstinfo; 1862 char dstr[INET6_ADDRSTRLEN]; 1863 char sstr[INET6_ADDRSTRLEN]; 1864 ipif_t *ipif, *start_ipif, *next_ipif; 1865 ill_t *ill, *usesrc_ill = NULL, *ipmp_ill = NULL; 1866 ill_walk_context_t ctx; 1867 cand_t best_c; /* The best candidate */ 1868 cand_t curr_c; /* The current candidate */ 1869 uint_t index; 1870 boolean_t first_candidate = B_TRUE; 1871 rule_res_t rule_result; 1872 tsol_tpc_t *src_rhtp, *dst_rhtp; 1873 ip_stack_t *ipst = dstill->ill_ipst; 1874 1875 /* 1876 * The list of ordering rules. They are applied in the order they 1877 * appear in the list. 1878 * 1879 * Solaris doesn't currently support Mobile IPv6, so there's no 1880 * rule_mipv6 corresponding to rule 4 in the specification. 1881 */ 1882 rulef_t rules[] = { 1883 rule_isdst, 1884 rule_scope, 1885 rule_deprecated, 1886 rule_preferred, 1887 rule_interface, 1888 rule_label, 1889 rule_temporary, 1890 rule_ifprefix, /* local rules after this */ 1891 rule_zone_specific, 1892 rule_addr_type, 1893 rule_prefix, /* local rules before this */ 1894 rule_must_be_last, /* must always be last */ 1895 NULL 1896 }; 1897 1898 ASSERT(dstill->ill_isv6); 1899 ASSERT(!IN6_IS_ADDR_V4MAPPED(dst)); 1900 1901 /* 1902 * Check if there is a usable src address pointed to by the 1903 * usesrc ifindex. This has higher precedence since it is 1904 * finer grained (i.e per interface) v/s being system wide. 1905 */ 1906 if (dstill->ill_usesrc_ifindex != 0 && allow_usesrc) { 1907 if ((usesrc_ill = 1908 ill_lookup_on_ifindex(dstill->ill_usesrc_ifindex, B_TRUE, 1909 ipst)) != NULL) { 1910 dstinfo.dst_ill = usesrc_ill; 1911 } else { 1912 return (NULL); 1913 } 1914 } else if (IS_UNDER_IPMP(dstill)) { 1915 /* 1916 * Test addresses should never be used for source address 1917 * selection, so if we were passed an underlying ill, switch 1918 * to the IPMP meta-interface. 1919 */ 1920 if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(dstill)) != NULL) 1921 dstinfo.dst_ill = ipmp_ill; 1922 else 1923 return (NULL); 1924 } else { 1925 dstinfo.dst_ill = dstill; 1926 } 1927 1928 /* 1929 * If we're dealing with an unlabeled destination on a labeled system, 1930 * make sure that we ignore source addresses that are incompatible with 1931 * the destination's default label. That destination's default label 1932 * must dominate the minimum label on the source address. 1933 * 1934 * (Note that this has to do with Trusted Solaris. It's not related to 1935 * the labels described by ip6_asp_lookup.) 1936 */ 1937 dst_rhtp = NULL; 1938 if (is_system_labeled()) { 1939 dst_rhtp = find_tpc(dst, IPV6_VERSION, B_FALSE); 1940 if (dst_rhtp == NULL) 1941 return (NULL); 1942 if (dst_rhtp->tpc_tp.host_type != UNLABELED) { 1943 TPC_RELE(dst_rhtp); 1944 dst_rhtp = NULL; 1945 } 1946 } 1947 1948 dstinfo.dst_addr = dst; 1949 dstinfo.dst_scope = ip_addr_scope_v6(dst); 1950 dstinfo.dst_label = ip6_asp_lookup(dst, NULL, ipst); 1951 dstinfo.dst_prefer_src_tmp = ((src_prefs & IPV6_PREFER_SRC_TMP) != 0); 1952 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 1953 /* 1954 * Section three of the I-D states that for multicast and 1955 * link-local destinations, the candidate set must be restricted to 1956 * an interface that is on the same link as the outgoing interface. 1957 * Also, when ipv6_strict_dst_multihoming is turned on, always 1958 * restrict the source address to the destination link as doing 1959 * otherwise will almost certainly cause problems. 1960 */ 1961 if (IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst) || 1962 ipst->ips_ipv6_strict_dst_multihoming || usesrc_ill != NULL) { 1963 dstinfo.dst_restrict_ill = B_TRUE; 1964 } else { 1965 dstinfo.dst_restrict_ill = restrict_ill; 1966 } 1967 1968 bzero(&best_c, sizeof (cand_t)); 1969 1970 /* 1971 * Take a pass through the list of IPv6 interfaces to choose the best 1972 * possible source address. If restrict_ill is set, just use dst_ill. 1973 */ 1974 if (dstinfo.dst_restrict_ill) 1975 ill = dstinfo.dst_ill; 1976 else 1977 ill = ILL_START_WALK_V6(&ctx, ipst); 1978 1979 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 1980 ASSERT(ill->ill_isv6); 1981 1982 /* 1983 * Test addresses should never be used for source address 1984 * selection, so ignore underlying ills. 1985 */ 1986 if (IS_UNDER_IPMP(ill)) 1987 continue; 1988 1989 if (ill->ill_ipif == NULL) 1990 continue; 1991 /* 1992 * For source address selection, we treat the ipif list as 1993 * circular and continue until we get back to where we 1994 * started. This allows IPMP to vary source address selection 1995 * (which improves inbound load spreading) by caching its last 1996 * ending point and starting from there. NOTE: we don't have 1997 * to worry about ill_src_ipif changing ills since that can't 1998 * happen on the IPMP ill. 1999 */ 2000 start_ipif = ill->ill_ipif; 2001 if (IS_IPMP(ill) && ill->ill_src_ipif != NULL) 2002 start_ipif = ill->ill_src_ipif; 2003 2004 ipif = start_ipif; 2005 do { 2006 if ((next_ipif = ipif->ipif_next) == NULL) 2007 next_ipif = ill->ill_ipif; 2008 2009 if (!IPIF_VALID_IPV6_SOURCE(ipif)) 2010 continue; 2011 2012 if (!ipif->ipif_addr_ready) { 2013 if (notreadyp != NULL) 2014 *notreadyp = B_TRUE; 2015 continue; 2016 } 2017 2018 if (zoneid != ALL_ZONES && 2019 ipif->ipif_zoneid != zoneid && 2020 ipif->ipif_zoneid != ALL_ZONES) 2021 continue; 2022 2023 /* 2024 * Check compatibility of local address for 2025 * destination's default label if we're on a labeled 2026 * system. Incompatible addresses can't be used at 2027 * all and must be skipped over. 2028 */ 2029 if (dst_rhtp != NULL) { 2030 boolean_t incompat; 2031 2032 src_rhtp = find_tpc(&ipif->ipif_v6lcl_addr, 2033 IPV6_VERSION, B_FALSE); 2034 if (src_rhtp == NULL) 2035 continue; 2036 incompat = 2037 src_rhtp->tpc_tp.host_type != SUN_CIPSO || 2038 src_rhtp->tpc_tp.tp_doi != 2039 dst_rhtp->tpc_tp.tp_doi || 2040 (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label, 2041 &src_rhtp->tpc_tp.tp_sl_range_cipso) && 2042 !blinlset(&dst_rhtp->tpc_tp.tp_def_label, 2043 src_rhtp->tpc_tp.tp_sl_set_cipso)); 2044 TPC_RELE(src_rhtp); 2045 if (incompat) 2046 continue; 2047 } 2048 2049 if (first_candidate) { 2050 /* 2051 * This is first valid address in the list. 2052 * It is automatically the best candidate 2053 * so far. 2054 */ 2055 best_c.cand_ipif = ipif; 2056 first_candidate = B_FALSE; 2057 continue; 2058 } 2059 2060 bzero(&curr_c, sizeof (cand_t)); 2061 curr_c.cand_ipif = ipif; 2062 2063 /* 2064 * Compare this current candidate (curr_c) with the 2065 * best candidate (best_c) by applying the 2066 * comparison rules in order until one breaks the 2067 * tie. 2068 */ 2069 for (index = 0; rules[index] != NULL; index++) { 2070 /* Apply a comparison rule. */ 2071 rule_result = (rules[index])(&best_c, &curr_c, 2072 &dstinfo, ipst); 2073 if (rule_result == CAND_AVOID) { 2074 /* 2075 * The best candidate is still the 2076 * best candidate. Forget about 2077 * this current candidate and go on 2078 * to the next one. 2079 */ 2080 break; 2081 } else if (rule_result == CAND_PREFER) { 2082 /* 2083 * This candidate is prefered. It 2084 * becomes the best candidate so 2085 * far. Go on to the next address. 2086 */ 2087 best_c = curr_c; 2088 break; 2089 } 2090 /* We have a tie, apply the next rule. */ 2091 } 2092 2093 /* 2094 * The last rule must be a tie breaker rule and 2095 * must never produce a tie. At this point, the 2096 * candidate should have either been rejected, or 2097 * have been prefered as the best candidate so far. 2098 */ 2099 ASSERT(rule_result != CAND_TIE); 2100 } while ((ipif = next_ipif) != start_ipif); 2101 2102 /* 2103 * For IPMP, update the source ipif rotor to the next ipif, 2104 * provided we can look it up. (We must not use it if it's 2105 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after 2106 * ipif_free() checked ill_src_ipif.) 2107 */ 2108 if (IS_IPMP(ill) && ipif != NULL) { 2109 mutex_enter(&ipif->ipif_ill->ill_lock); 2110 next_ipif = ipif->ipif_next; 2111 if (next_ipif != NULL && !IPIF_IS_CONDEMNED(next_ipif)) 2112 ill->ill_src_ipif = next_ipif; 2113 else 2114 ill->ill_src_ipif = NULL; 2115 mutex_exit(&ipif->ipif_ill->ill_lock); 2116 } 2117 2118 /* 2119 * Only one ill to consider if dst_restrict_ill is set. 2120 */ 2121 if (dstinfo.dst_restrict_ill) 2122 break; 2123 } 2124 2125 ipif = best_c.cand_ipif; 2126 ip1dbg(("ipif_select_source_v6(%s, %s) -> %s\n", 2127 dstinfo.dst_ill->ill_name, 2128 inet_ntop(AF_INET6, dstinfo.dst_addr, dstr, sizeof (dstr)), 2129 (ipif == NULL ? "NULL" : 2130 inet_ntop(AF_INET6, &ipif->ipif_v6lcl_addr, sstr, sizeof (sstr))))); 2131 2132 if (usesrc_ill != NULL) 2133 ill_refrele(usesrc_ill); 2134 2135 if (ipmp_ill != NULL) 2136 ill_refrele(ipmp_ill); 2137 2138 if (dst_rhtp != NULL) 2139 TPC_RELE(dst_rhtp); 2140 2141 if (ipif == NULL) { 2142 rw_exit(&ipst->ips_ill_g_lock); 2143 return (NULL); 2144 } 2145 2146 mutex_enter(&ipif->ipif_ill->ill_lock); 2147 if (!IPIF_IS_CONDEMNED(ipif)) { 2148 ipif_refhold_locked(ipif); 2149 mutex_exit(&ipif->ipif_ill->ill_lock); 2150 rw_exit(&ipst->ips_ill_g_lock); 2151 return (ipif); 2152 } 2153 mutex_exit(&ipif->ipif_ill->ill_lock); 2154 rw_exit(&ipst->ips_ill_g_lock); 2155 ip1dbg(("ipif_select_source_v6 cannot lookup ipif %p" 2156 " returning null \n", (void *)ipif)); 2157 2158 return (NULL); 2159 } 2160 2161 /* 2162 * Pick a source address based on the destination ill and an optional setsrc 2163 * address. 2164 * The result is stored in srcp. If generation is set, then put the source 2165 * generation number there before we look for the source address (to avoid 2166 * missing changes in the set of source addresses. 2167 * If flagsp is set, then us it to pass back ipif_flags. 2168 * 2169 * If the caller wants to cache the returned source address and detect when 2170 * that might be stale, the caller should pass in a generation argument, 2171 * which the caller can later compare against ips_src_generation 2172 * 2173 * The precedence order for selecting an IPv6 source address is: 2174 * - RTF_SETSRC on the first ire in the recursive lookup always wins. 2175 * - If usrsrc is set, swap the ill to be the usesrc one. 2176 * - If IPMP is used on the ill, select a random address from the most 2177 * preferred ones below: 2178 * That is followed by the long list of IPv6 source address selection rules 2179 * starting with rule_isdst(), rule_scope(), etc. 2180 * 2181 * We have lower preference for ALL_ZONES IP addresses, 2182 * as they pose problems with unlabeled destinations. 2183 * 2184 * Note that when multiple IP addresses match e.g., with rule_scope() we pick 2185 * the first one if IPMP is not in use. With IPMP we randomize. 2186 */ 2187 int 2188 ip_select_source_v6(ill_t *ill, const in6_addr_t *setsrc, const in6_addr_t *dst, 2189 zoneid_t zoneid, ip_stack_t *ipst, uint_t restrict_ill, uint32_t src_prefs, 2190 in6_addr_t *srcp, uint32_t *generation, uint64_t *flagsp) 2191 { 2192 ipif_t *ipif; 2193 boolean_t notready = B_FALSE; /* Set if !ipif_addr_ready found */ 2194 2195 if (flagsp != NULL) 2196 *flagsp = 0; 2197 2198 /* 2199 * Need to grab the generation number before we check to 2200 * avoid a race with a change to the set of local addresses. 2201 * No lock needed since the thread which updates the set of local 2202 * addresses use ipif/ill locks and exit those (hence a store memory 2203 * barrier) before doing the atomic increase of ips_src_generation. 2204 */ 2205 if (generation != NULL) { 2206 *generation = ipst->ips_src_generation; 2207 } 2208 2209 /* Was RTF_SETSRC set on the first IRE in the recursive lookup? */ 2210 if (setsrc != NULL && !IN6_IS_ADDR_UNSPECIFIED(setsrc)) { 2211 *srcp = *setsrc; 2212 return (0); 2213 } 2214 2215 ipif = ipif_select_source_v6(ill, dst, restrict_ill, src_prefs, zoneid, 2216 B_TRUE, ¬ready); 2217 if (ipif == NULL) { 2218 if (notready) 2219 return (ENETDOWN); 2220 else 2221 return (EADDRNOTAVAIL); 2222 } 2223 *srcp = ipif->ipif_v6lcl_addr; 2224 if (flagsp != NULL) 2225 *flagsp = ipif->ipif_flags; 2226 ipif_refrele(ipif); 2227 return (0); 2228 } 2229 2230 /* 2231 * Perform an attach and bind to get phys addr plus info_req for 2232 * the physical device. 2233 * q and mp represents an ioctl which will be queued waiting for 2234 * completion of the DLPI message exchange. 2235 * MUST be called on an ill queue. 2236 * 2237 * Returns EINPROGRESS when mp has been consumed by queueing it. 2238 * The ioctl will complete in ip_rput. 2239 */ 2240 int 2241 ill_dl_phys(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q) 2242 { 2243 mblk_t *v6token_mp = NULL; 2244 mblk_t *v6lla_mp = NULL; 2245 mblk_t *dest_mp = NULL; 2246 mblk_t *phys_mp = NULL; 2247 mblk_t *info_mp = NULL; 2248 mblk_t *attach_mp = NULL; 2249 mblk_t *bind_mp = NULL; 2250 mblk_t *unbind_mp = NULL; 2251 mblk_t *notify_mp = NULL; 2252 mblk_t *capab_mp = NULL; 2253 2254 ip1dbg(("ill_dl_phys(%s:%u)\n", ill->ill_name, ipif->ipif_id)); 2255 ASSERT(ill->ill_dlpi_style_set); 2256 ASSERT(WR(q)->q_next != NULL); 2257 2258 if (ill->ill_isv6) { 2259 v6token_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) + 2260 sizeof (t_scalar_t), DL_PHYS_ADDR_REQ); 2261 if (v6token_mp == NULL) 2262 goto bad; 2263 ((dl_phys_addr_req_t *)v6token_mp->b_rptr)->dl_addr_type = 2264 DL_IPV6_TOKEN; 2265 2266 v6lla_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) + 2267 sizeof (t_scalar_t), DL_PHYS_ADDR_REQ); 2268 if (v6lla_mp == NULL) 2269 goto bad; 2270 ((dl_phys_addr_req_t *)v6lla_mp->b_rptr)->dl_addr_type = 2271 DL_IPV6_LINK_LAYER_ADDR; 2272 } 2273 2274 if (ill->ill_mactype == DL_IPV4 || ill->ill_mactype == DL_IPV6) { 2275 dest_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) + 2276 sizeof (t_scalar_t), DL_PHYS_ADDR_REQ); 2277 if (dest_mp == NULL) 2278 goto bad; 2279 ((dl_phys_addr_req_t *)dest_mp->b_rptr)->dl_addr_type = 2280 DL_CURR_DEST_ADDR; 2281 } 2282 2283 /* 2284 * Allocate a DL_NOTIFY_REQ and set the notifications we want. 2285 */ 2286 notify_mp = ip_dlpi_alloc(sizeof (dl_notify_req_t) + sizeof (long), 2287 DL_NOTIFY_REQ); 2288 if (notify_mp == NULL) 2289 goto bad; 2290 ((dl_notify_req_t *)notify_mp->b_rptr)->dl_notifications = 2291 (DL_NOTE_PHYS_ADDR | DL_NOTE_SDU_SIZE | DL_NOTE_FASTPATH_FLUSH | 2292 DL_NOTE_LINK_UP | DL_NOTE_LINK_DOWN | DL_NOTE_CAPAB_RENEG | 2293 DL_NOTE_PROMISC_ON_PHYS | DL_NOTE_PROMISC_OFF_PHYS | 2294 DL_NOTE_REPLUMB); 2295 2296 phys_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) + 2297 sizeof (t_scalar_t), DL_PHYS_ADDR_REQ); 2298 if (phys_mp == NULL) 2299 goto bad; 2300 ((dl_phys_addr_req_t *)phys_mp->b_rptr)->dl_addr_type = 2301 DL_CURR_PHYS_ADDR; 2302 2303 info_mp = ip_dlpi_alloc( 2304 sizeof (dl_info_req_t) + sizeof (dl_info_ack_t), 2305 DL_INFO_REQ); 2306 if (info_mp == NULL) 2307 goto bad; 2308 2309 ASSERT(ill->ill_dlpi_capab_state == IDCS_UNKNOWN); 2310 capab_mp = ip_dlpi_alloc(sizeof (dl_capability_req_t), 2311 DL_CAPABILITY_REQ); 2312 if (capab_mp == NULL) 2313 goto bad; 2314 2315 bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long), 2316 DL_BIND_REQ); 2317 if (bind_mp == NULL) 2318 goto bad; 2319 ((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap; 2320 ((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS; 2321 2322 unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ); 2323 if (unbind_mp == NULL) 2324 goto bad; 2325 2326 /* If we need to attach, pre-alloc and initialize the mblk */ 2327 if (ill->ill_needs_attach) { 2328 attach_mp = ip_dlpi_alloc(sizeof (dl_attach_req_t), 2329 DL_ATTACH_REQ); 2330 if (attach_mp == NULL) 2331 goto bad; 2332 ((dl_attach_req_t *)attach_mp->b_rptr)->dl_ppa = ill->ill_ppa; 2333 } 2334 2335 /* 2336 * Here we are going to delay the ioctl ack until after 2337 * ACKs from DL_PHYS_ADDR_REQ. So need to save the 2338 * original ioctl message before sending the requests 2339 */ 2340 mutex_enter(&ill->ill_lock); 2341 /* ipsq_pending_mp_add won't fail since we pass in a NULL connp */ 2342 (void) ipsq_pending_mp_add(NULL, ipif, ill->ill_wq, mp, 0); 2343 /* 2344 * Set ill_phys_addr_pend to zero. It will be set to the addr_type of 2345 * the DL_PHYS_ADDR_REQ in ill_dlpi_send() and ill_dlpi_done(). It will 2346 * be used to track which DL_PHYS_ADDR_REQ is being ACK'd/NAK'd. 2347 */ 2348 ill->ill_phys_addr_pend = 0; 2349 mutex_exit(&ill->ill_lock); 2350 2351 if (attach_mp != NULL) { 2352 ip1dbg(("ill_dl_phys: attach\n")); 2353 ill_dlpi_send(ill, attach_mp); 2354 } 2355 ill_dlpi_send(ill, bind_mp); 2356 ill_dlpi_send(ill, info_mp); 2357 2358 /* 2359 * Send the capability request to get the VRRP capability information. 2360 */ 2361 ill_capability_send(ill, capab_mp); 2362 2363 if (v6token_mp != NULL) 2364 ill_dlpi_send(ill, v6token_mp); 2365 if (v6lla_mp != NULL) 2366 ill_dlpi_send(ill, v6lla_mp); 2367 if (dest_mp != NULL) 2368 ill_dlpi_send(ill, dest_mp); 2369 ill_dlpi_send(ill, phys_mp); 2370 ill_dlpi_send(ill, notify_mp); 2371 ill_dlpi_send(ill, unbind_mp); 2372 2373 /* 2374 * This operation will complete in ip_rput_dlpi_writer with either 2375 * a DL_PHYS_ADDR_ACK or DL_ERROR_ACK. 2376 */ 2377 return (EINPROGRESS); 2378 bad: 2379 freemsg(v6token_mp); 2380 freemsg(v6lla_mp); 2381 freemsg(dest_mp); 2382 freemsg(phys_mp); 2383 freemsg(info_mp); 2384 freemsg(attach_mp); 2385 freemsg(bind_mp); 2386 freemsg(capab_mp); 2387 freemsg(unbind_mp); 2388 freemsg(notify_mp); 2389 return (ENOMEM); 2390 } 2391 2392 /* Add room for tcp+ip headers */ 2393 uint_t ip_loopback_mtu_v6plus = IP_LOOPBACK_MTU + IPV6_HDR_LEN + 20; 2394 2395 /* 2396 * DLPI is up. 2397 * Create all the IREs associated with an interface bring up multicast. 2398 * Set the interface flag and finish other initialization 2399 * that potentially had to be differed to after DL_BIND_ACK. 2400 */ 2401 int 2402 ipif_up_done_v6(ipif_t *ipif) 2403 { 2404 ill_t *ill = ipif->ipif_ill; 2405 int err; 2406 boolean_t loopback = B_FALSE; 2407 2408 ip1dbg(("ipif_up_done_v6(%s:%u)\n", 2409 ipif->ipif_ill->ill_name, ipif->ipif_id)); 2410 DTRACE_PROBE3(ipif__downup, char *, "ipif_up_done_v6", 2411 ill_t *, ill, ipif_t *, ipif); 2412 2413 /* Check if this is a loopback interface */ 2414 if (ipif->ipif_ill->ill_wq == NULL) 2415 loopback = B_TRUE; 2416 2417 ASSERT(ipif->ipif_isv6); 2418 ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock)); 2419 2420 if (IS_LOOPBACK(ill) || ill->ill_net_type == IRE_IF_NORESOLVER) { 2421 nce_t *loop_nce = NULL; 2422 uint16_t flags = (NCE_F_MYADDR | NCE_F_NONUD | NCE_F_AUTHORITY); 2423 2424 /* 2425 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in 2426 * ipif_lookup_on_name(), but in the case of zones we can have 2427 * several loopback addresses on lo0. So all the interfaces with 2428 * loopback addresses need to be marked IRE_LOOPBACK. 2429 */ 2430 if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr, &ipv6_loopback)) 2431 ipif->ipif_ire_type = IRE_LOOPBACK; 2432 else 2433 ipif->ipif_ire_type = IRE_LOCAL; 2434 if (ill->ill_net_type != IRE_LOOPBACK) 2435 flags |= NCE_F_PUBLISH; 2436 err = nce_lookup_then_add_v6(ill, NULL, 2437 ill->ill_phys_addr_length, 2438 &ipif->ipif_v6lcl_addr, flags, ND_REACHABLE, &loop_nce); 2439 2440 /* A shared-IP zone sees EEXIST for lo0:N */ 2441 if (err == 0 || err == EEXIST) { 2442 ipif->ipif_added_nce = 1; 2443 loop_nce->nce_ipif_cnt++; 2444 nce_refrele(loop_nce); 2445 err = 0; 2446 } else { 2447 ASSERT(loop_nce == NULL); 2448 return (err); 2449 } 2450 } 2451 2452 err = ipif_add_ires_v6(ipif, loopback); 2453 if (err != 0) { 2454 /* 2455 * See comments about return value from 2456 * ipif_addr_availability_check() in ipif_add_ires_v6(). 2457 */ 2458 if (err != EADDRINUSE) { 2459 ipif_ndp_down(ipif); 2460 } else { 2461 /* 2462 * Make IPMP aware of the deleted ipif so that 2463 * the needed ipmp cleanup (e.g., of ipif_bound_ill) 2464 * can be completed. Note that we do not want to 2465 * destroy the nce that was created on the ipmp_ill 2466 * for the active copy of the duplicate address in 2467 * use. 2468 */ 2469 if (IS_IPMP(ill)) 2470 ipmp_illgrp_del_ipif(ill->ill_grp, ipif); 2471 err = EADDRNOTAVAIL; 2472 } 2473 return (err); 2474 } 2475 2476 if (ill->ill_ipif_up_count == 1 && !loopback) { 2477 /* Recover any additional IREs entries for this ill */ 2478 (void) ill_recover_saved_ire(ill); 2479 } 2480 2481 if (ill->ill_need_recover_multicast) { 2482 /* 2483 * Need to recover all multicast memberships in the driver. 2484 * This had to be deferred until we had attached. 2485 */ 2486 ill_recover_multicast(ill); 2487 } 2488 2489 if (ill->ill_ipif_up_count == 1) { 2490 /* 2491 * Since the interface is now up, it may now be active. 2492 */ 2493 if (IS_UNDER_IPMP(ill)) 2494 ipmp_ill_refresh_active(ill); 2495 } 2496 2497 /* Join the allhosts multicast address and the solicited node MC */ 2498 ipif_multicast_up(ipif); 2499 2500 /* Perhaps ilgs should use this ill */ 2501 update_conn_ill(NULL, ill->ill_ipst); 2502 2503 if (ipif->ipif_addr_ready) 2504 ipif_up_notify(ipif); 2505 2506 return (0); 2507 } 2508 2509 /* 2510 * Add the IREs associated with the ipif. 2511 * Those MUST be explicitly removed in ipif_delete_ires_v6. 2512 */ 2513 static int 2514 ipif_add_ires_v6(ipif_t *ipif, boolean_t loopback) 2515 { 2516 ill_t *ill = ipif->ipif_ill; 2517 ip_stack_t *ipst = ill->ill_ipst; 2518 in6_addr_t v6addr; 2519 in6_addr_t route_mask; 2520 int err; 2521 char buf[INET6_ADDRSTRLEN]; 2522 ire_t *ire_local = NULL; /* LOCAL or LOOPBACK */ 2523 ire_t *ire_if = NULL; 2524 in6_addr_t *gw; 2525 2526 if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) && 2527 !(ipif->ipif_flags & IPIF_NOLOCAL)) { 2528 2529 /* 2530 * If we're on a labeled system then make sure that zone- 2531 * private addresses have proper remote host database entries. 2532 */ 2533 if (is_system_labeled() && 2534 ipif->ipif_ire_type != IRE_LOOPBACK) { 2535 if (ip6opt_ls == 0) { 2536 cmn_err(CE_WARN, "IPv6 not enabled " 2537 "via /etc/system"); 2538 return (EINVAL); 2539 } 2540 if (!tsol_check_interface_address(ipif)) 2541 return (EINVAL); 2542 } 2543 2544 if (loopback) 2545 gw = &ipif->ipif_v6lcl_addr; 2546 else 2547 gw = NULL; 2548 2549 /* Register the source address for __sin6_src_id */ 2550 err = ip_srcid_insert(&ipif->ipif_v6lcl_addr, 2551 ipif->ipif_zoneid, ipst); 2552 if (err != 0) { 2553 ip0dbg(("ipif_add_ires_v6: srcid_insert %d\n", err)); 2554 return (err); 2555 } 2556 /* 2557 * If the interface address is set, create the LOCAL 2558 * or LOOPBACK IRE. 2559 */ 2560 ip1dbg(("ipif_add_ires_v6: creating IRE %d for %s\n", 2561 ipif->ipif_ire_type, 2562 inet_ntop(AF_INET6, &ipif->ipif_v6lcl_addr, 2563 buf, sizeof (buf)))); 2564 2565 ire_local = ire_create_v6( 2566 &ipif->ipif_v6lcl_addr, /* dest address */ 2567 &ipv6_all_ones, /* mask */ 2568 gw, /* gateway */ 2569 ipif->ipif_ire_type, /* LOCAL or LOOPBACK */ 2570 ipif->ipif_ill, /* interface */ 2571 ipif->ipif_zoneid, 2572 ((ipif->ipif_flags & IPIF_PRIVATE) ? 2573 RTF_PRIVATE : 0) | RTF_KERNEL, 2574 NULL, 2575 ipst); 2576 if (ire_local == NULL) { 2577 ip1dbg(("ipif_up_done_v6: NULL ire_local\n")); 2578 err = ENOMEM; 2579 goto bad; 2580 } 2581 } 2582 2583 /* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */ 2584 if (!loopback && !(ipif->ipif_flags & IPIF_NOXMIT) && 2585 !(IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet) && 2586 IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))) { 2587 /* ipif_v6subnet is ipif_v6pp_dst_addr for pt-pt */ 2588 v6addr = ipif->ipif_v6subnet; 2589 2590 if (ipif->ipif_flags & IPIF_POINTOPOINT) { 2591 route_mask = ipv6_all_ones; 2592 } else { 2593 route_mask = ipif->ipif_v6net_mask; 2594 } 2595 2596 ip1dbg(("ipif_add_ires_v6: creating if IRE %d for %s\n", 2597 ill->ill_net_type, 2598 inet_ntop(AF_INET6, &v6addr, buf, sizeof (buf)))); 2599 2600 ire_if = ire_create_v6( 2601 &v6addr, /* dest pref */ 2602 &route_mask, /* mask */ 2603 &ipif->ipif_v6lcl_addr, /* gateway */ 2604 ill->ill_net_type, /* IF_[NO]RESOLVER */ 2605 ipif->ipif_ill, 2606 ipif->ipif_zoneid, 2607 ((ipif->ipif_flags & IPIF_PRIVATE) ? 2608 RTF_PRIVATE : 0) | RTF_KERNEL, 2609 NULL, 2610 ipst); 2611 if (ire_if == NULL) { 2612 ip1dbg(("ipif_up_done: NULL ire_if\n")); 2613 err = ENOMEM; 2614 goto bad; 2615 } 2616 } 2617 2618 /* 2619 * Need to atomically check for IP address availability under 2620 * ip_addr_avail_lock. ill_g_lock is held as reader to ensure no new 2621 * ills or new ipifs can be added while we are checking availability. 2622 */ 2623 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 2624 mutex_enter(&ipst->ips_ip_addr_avail_lock); 2625 ill->ill_ipif_up_count++; 2626 ipif->ipif_flags |= IPIF_UP; 2627 err = ip_addr_availability_check(ipif); 2628 mutex_exit(&ipst->ips_ip_addr_avail_lock); 2629 rw_exit(&ipst->ips_ill_g_lock); 2630 2631 if (err != 0) { 2632 /* 2633 * Our address may already be up on the same ill. In this case, 2634 * the external resolver entry for our ipif replaced the one for 2635 * the other ipif. So we don't want to delete it (otherwise the 2636 * other ipif would be unable to send packets). 2637 * ip_addr_availability_check() identifies this case for us and 2638 * returns EADDRINUSE; Caller must turn it into EADDRNOTAVAIL 2639 * which is the expected error code. 2640 * 2641 * Note that ipif_ndp_down() will only delete the nce in the 2642 * case when the nce_ipif_cnt drops to 0. 2643 */ 2644 ill->ill_ipif_up_count--; 2645 ipif->ipif_flags &= ~IPIF_UP; 2646 goto bad; 2647 } 2648 2649 /* 2650 * Add in all newly created IREs. 2651 * We add the IRE_INTERFACE before the IRE_LOCAL to ensure 2652 * that lookups find the IRE_LOCAL even if the IRE_INTERFACE is 2653 * a /128 route. 2654 */ 2655 if (ire_if != NULL) { 2656 ire_if = ire_add(ire_if); 2657 if (ire_if == NULL) { 2658 err = ENOMEM; 2659 goto bad2; 2660 } 2661 #ifdef DEBUG 2662 ire_refhold_notr(ire_if); 2663 ire_refrele(ire_if); 2664 #endif 2665 } 2666 if (ire_local != NULL) { 2667 ire_local = ire_add(ire_local); 2668 if (ire_local == NULL) { 2669 err = ENOMEM; 2670 goto bad2; 2671 } 2672 #ifdef DEBUG 2673 ire_refhold_notr(ire_local); 2674 ire_refrele(ire_local); 2675 #endif 2676 } 2677 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER); 2678 if (ire_local != NULL) 2679 ipif->ipif_ire_local = ire_local; 2680 if (ire_if != NULL) 2681 ipif->ipif_ire_if = ire_if; 2682 rw_exit(&ipst->ips_ill_g_lock); 2683 ire_local = NULL; 2684 ire_if = NULL; 2685 2686 if (ipif->ipif_addr_ready) 2687 ipif_up_notify(ipif); 2688 return (0); 2689 2690 bad2: 2691 ill->ill_ipif_up_count--; 2692 ipif->ipif_flags &= ~IPIF_UP; 2693 2694 bad: 2695 if (ire_local != NULL) 2696 ire_delete(ire_local); 2697 if (ire_if != NULL) 2698 ire_delete(ire_if); 2699 2700 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER); 2701 ire_local = ipif->ipif_ire_local; 2702 ipif->ipif_ire_local = NULL; 2703 ire_if = ipif->ipif_ire_if; 2704 ipif->ipif_ire_if = NULL; 2705 rw_exit(&ipst->ips_ill_g_lock); 2706 if (ire_local != NULL) { 2707 ire_delete(ire_local); 2708 ire_refrele_notr(ire_local); 2709 } 2710 if (ire_if != NULL) { 2711 ire_delete(ire_if); 2712 ire_refrele_notr(ire_if); 2713 } 2714 (void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst); 2715 2716 return (err); 2717 } 2718 2719 /* Remove all the IREs created by ipif_add_ires_v6 */ 2720 void 2721 ipif_delete_ires_v6(ipif_t *ipif) 2722 { 2723 ill_t *ill = ipif->ipif_ill; 2724 ip_stack_t *ipst = ill->ill_ipst; 2725 ire_t *ire; 2726 2727 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER); 2728 ire = ipif->ipif_ire_local; 2729 ipif->ipif_ire_local = NULL; 2730 rw_exit(&ipst->ips_ill_g_lock); 2731 if (ire != NULL) { 2732 /* 2733 * Move count to ipif so we don't loose the count due to 2734 * a down/up dance. 2735 */ 2736 atomic_add_32(&ipif->ipif_ib_pkt_count, ire->ire_ib_pkt_count); 2737 2738 ire_delete(ire); 2739 ire_refrele_notr(ire); 2740 } 2741 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER); 2742 ire = ipif->ipif_ire_if; 2743 ipif->ipif_ire_if = NULL; 2744 rw_exit(&ipst->ips_ill_g_lock); 2745 if (ire != NULL) { 2746 ire_delete(ire); 2747 ire_refrele_notr(ire); 2748 } 2749 } 2750 2751 /* 2752 * Delete an ND entry if it exists. 2753 */ 2754 /* ARGSUSED */ 2755 int 2756 ip_siocdelndp_v6(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp, 2757 ip_ioctl_cmd_t *ipip, void *dummy_ifreq) 2758 { 2759 sin6_t *sin6; 2760 struct lifreq *lifr; 2761 lif_nd_req_t *lnr; 2762 ill_t *ill = ipif->ipif_ill; 2763 nce_t *nce; 2764 2765 lifr = (struct lifreq *)mp->b_cont->b_cont->b_rptr; 2766 lnr = &lifr->lifr_nd; 2767 /* Only allow for logical unit zero i.e. not on "le0:17" */ 2768 if (ipif->ipif_id != 0) 2769 return (EINVAL); 2770 2771 if (!ipif->ipif_isv6) 2772 return (EINVAL); 2773 2774 if (lnr->lnr_addr.ss_family != AF_INET6) 2775 return (EAFNOSUPPORT); 2776 2777 sin6 = (sin6_t *)&lnr->lnr_addr; 2778 2779 /* 2780 * Since ND mappings must be consistent across an IPMP group, prohibit 2781 * deleting ND mappings on underlying interfaces. 2782 * Don't allow deletion of mappings for local addresses. 2783 */ 2784 if (IS_UNDER_IPMP(ill)) 2785 return (EPERM); 2786 2787 nce = nce_lookup_v6(ill, &sin6->sin6_addr); 2788 if (nce == NULL) 2789 return (ESRCH); 2790 2791 if (NCE_MYADDR(nce->nce_common)) { 2792 nce_refrele(nce); 2793 return (EPERM); 2794 } 2795 2796 /* 2797 * delete the nce_common which will also delete the nces on any 2798 * under_ill in the case of ipmp. 2799 */ 2800 ncec_delete(nce->nce_common); 2801 nce_refrele(nce); 2802 return (0); 2803 } 2804 2805 /* 2806 * Return nbr cache info. 2807 */ 2808 /* ARGSUSED */ 2809 int 2810 ip_siocqueryndp_v6(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp, 2811 ip_ioctl_cmd_t *ipip, void *dummy_ifreq) 2812 { 2813 ill_t *ill = ipif->ipif_ill; 2814 struct lifreq *lifr; 2815 lif_nd_req_t *lnr; 2816 2817 lifr = (struct lifreq *)mp->b_cont->b_cont->b_rptr; 2818 lnr = &lifr->lifr_nd; 2819 /* Only allow for logical unit zero i.e. not on "le0:17" */ 2820 if (ipif->ipif_id != 0) 2821 return (EINVAL); 2822 2823 if (!ipif->ipif_isv6) 2824 return (EINVAL); 2825 2826 if (lnr->lnr_addr.ss_family != AF_INET6) 2827 return (EAFNOSUPPORT); 2828 2829 if (ill->ill_phys_addr_length > sizeof (lnr->lnr_hdw_addr)) 2830 return (EINVAL); 2831 2832 return (ndp_query(ill, lnr)); 2833 } 2834 2835 /* 2836 * Perform an update of the nd entry for the specified address. 2837 */ 2838 /* ARGSUSED */ 2839 int 2840 ip_siocsetndp_v6(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp, 2841 ip_ioctl_cmd_t *ipip, void *dummy_ifreq) 2842 { 2843 sin6_t *sin6; 2844 ill_t *ill = ipif->ipif_ill; 2845 struct lifreq *lifr; 2846 lif_nd_req_t *lnr; 2847 ire_t *ire; 2848 2849 lifr = (struct lifreq *)mp->b_cont->b_cont->b_rptr; 2850 lnr = &lifr->lifr_nd; 2851 /* Only allow for logical unit zero i.e. not on "le0:17" */ 2852 if (ipif->ipif_id != 0) 2853 return (EINVAL); 2854 2855 if (!ipif->ipif_isv6) 2856 return (EINVAL); 2857 2858 if (lnr->lnr_addr.ss_family != AF_INET6) 2859 return (EAFNOSUPPORT); 2860 2861 sin6 = (sin6_t *)&lnr->lnr_addr; 2862 2863 /* 2864 * Since ND mappings must be consistent across an IPMP group, prohibit 2865 * updating ND mappings on underlying interfaces. Also, since ND 2866 * mappings for IPMP data addresses are owned by IP itself, prohibit 2867 * updating them. 2868 */ 2869 if (IS_UNDER_IPMP(ill)) 2870 return (EPERM); 2871 2872 if (IS_IPMP(ill)) { 2873 ire = ire_ftable_lookup_v6(&sin6->sin6_addr, NULL, NULL, 2874 IRE_LOCAL, ill, ALL_ZONES, NULL, 2875 MATCH_IRE_TYPE | MATCH_IRE_ILL, 0, ill->ill_ipst, NULL); 2876 if (ire != NULL) { 2877 ire_refrele(ire); 2878 return (EPERM); 2879 } 2880 } 2881 2882 return (ndp_sioc_update(ill, lnr)); 2883 } 2884