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 2009 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/tun.h> 69 #include <inet/ipclassifier.h> 70 #include <inet/sctp_ip.h> 71 72 #include <sys/tsol/tndb.h> 73 #include <sys/tsol/tnet.h> 74 75 static in6_addr_t ipv6_ll_template = 76 {(uint32_t)V6_LINKLOCAL, 0x0, 0x0, 0x0}; 77 78 static ipif_t * 79 ipif_lookup_interface_v6(const in6_addr_t *if_addr, const in6_addr_t *dst, 80 queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst); 81 82 /* 83 * These two functions, ipif_lookup_group_v6() and ill_lookup_group_v6(), 84 * are called when an application does not specify an interface to be 85 * 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 ipif_t * 95 ipif_lookup_group_v6(const in6_addr_t *group, zoneid_t zoneid, ip_stack_t *ipst) 96 { 97 ire_t *ire; 98 ipif_t *ipif; 99 100 ire = ire_lookup_multi_v6(group, zoneid, ipst); 101 if (ire != NULL) { 102 ipif = ire->ire_ipif; 103 ipif_refhold(ipif); 104 ire_refrele(ire); 105 return (ipif); 106 } 107 108 return (ipif_lookup_multicast(ipst, zoneid, B_TRUE)); 109 } 110 111 ill_t * 112 ill_lookup_group_v6(const in6_addr_t *group, zoneid_t zoneid, ip_stack_t *ipst) 113 { 114 ire_t *ire; 115 ill_t *ill; 116 ipif_t *ipif; 117 118 ire = ire_lookup_multi_v6(group, zoneid, ipst); 119 if (ire != NULL) { 120 ill = ire->ire_ipif->ipif_ill; 121 ill_refhold(ill); 122 ire_refrele(ire); 123 return (ill); 124 } 125 126 ipif = ipif_lookup_multicast(ipst, zoneid, B_TRUE); 127 if (ipif == NULL) 128 return (NULL); 129 130 ill = ipif->ipif_ill; 131 ill_refhold(ill); 132 ipif_refrele(ipif); 133 return (ill); 134 } 135 136 /* 137 * Look for an ipif with the specified interface address and destination. 138 * The destination address is used only for matching point-to-point interfaces. 139 */ 140 static ipif_t * 141 ipif_lookup_interface_v6(const in6_addr_t *if_addr, const in6_addr_t *dst, 142 queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst) 143 { 144 ipif_t *ipif; 145 ill_t *ill; 146 ipsq_t *ipsq; 147 ill_walk_context_t ctx; 148 149 if (error != NULL) 150 *error = 0; 151 152 /* 153 * First match all the point-to-point interfaces 154 * before looking at non-point-to-point interfaces. 155 * This is done to avoid returning non-point-to-point 156 * ipif instead of unnumbered point-to-point ipif. 157 */ 158 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 159 ill = ILL_START_WALK_V6(&ctx, ipst); 160 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 161 GRAB_CONN_LOCK(q); 162 mutex_enter(&ill->ill_lock); 163 for (ipif = ill->ill_ipif; ipif != NULL; 164 ipif = ipif->ipif_next) { 165 /* Allow the ipif to be down */ 166 if ((ipif->ipif_flags & IPIF_POINTOPOINT) && 167 (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr, 168 if_addr)) && 169 (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, 170 dst))) { 171 if (IPIF_CAN_LOOKUP(ipif)) { 172 ipif_refhold_locked(ipif); 173 mutex_exit(&ill->ill_lock); 174 RELEASE_CONN_LOCK(q); 175 rw_exit(&ipst->ips_ill_g_lock); 176 return (ipif); 177 } else if (IPIF_CAN_WAIT(ipif, q)) { 178 ipsq = ill->ill_phyint->phyint_ipsq; 179 mutex_enter(&ipsq->ipsq_lock); 180 mutex_enter(&ipsq->ipsq_xop->ipx_lock); 181 mutex_exit(&ill->ill_lock); 182 rw_exit(&ipst->ips_ill_g_lock); 183 ipsq_enq(ipsq, q, mp, func, NEW_OP, 184 ill); 185 mutex_exit(&ipsq->ipsq_xop->ipx_lock); 186 mutex_exit(&ipsq->ipsq_lock); 187 RELEASE_CONN_LOCK(q); 188 if (error != NULL) 189 *error = EINPROGRESS; 190 return (NULL); 191 } 192 } 193 } 194 mutex_exit(&ill->ill_lock); 195 RELEASE_CONN_LOCK(q); 196 } 197 rw_exit(&ipst->ips_ill_g_lock); 198 /* lookup the ipif based on interface address */ 199 ipif = ipif_lookup_addr_v6(if_addr, NULL, ALL_ZONES, q, mp, func, 200 error, ipst); 201 ASSERT(ipif == NULL || ipif->ipif_isv6); 202 return (ipif); 203 } 204 205 /* 206 * Common function for ipif_lookup_addr_v6() and ipif_lookup_addr_exact_v6(). 207 */ 208 static ipif_t * 209 ipif_lookup_addr_common_v6(const in6_addr_t *addr, ill_t *match_ill, 210 boolean_t match_illgrp, zoneid_t zoneid, queue_t *q, mblk_t *mp, 211 ipsq_func_t func, int *error, ip_stack_t *ipst) 212 { 213 ipif_t *ipif; 214 ill_t *ill; 215 boolean_t ptp = B_FALSE; 216 ipsq_t *ipsq; 217 ill_walk_context_t ctx; 218 219 if (error != NULL) 220 *error = 0; 221 222 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 223 /* 224 * Repeat twice, first based on local addresses and 225 * next time for pointopoint. 226 */ 227 repeat: 228 ill = ILL_START_WALK_V6(&ctx, ipst); 229 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 230 if (match_ill != NULL && ill != match_ill && 231 (!match_illgrp || !IS_IN_SAME_ILLGRP(ill, match_ill))) { 232 continue; 233 } 234 GRAB_CONN_LOCK(q); 235 mutex_enter(&ill->ill_lock); 236 for (ipif = ill->ill_ipif; ipif != NULL; 237 ipif = ipif->ipif_next) { 238 if (zoneid != ALL_ZONES && 239 ipif->ipif_zoneid != zoneid && 240 ipif->ipif_zoneid != ALL_ZONES) 241 continue; 242 /* Allow the ipif to be down */ 243 if ((!ptp && (IN6_ARE_ADDR_EQUAL( 244 &ipif->ipif_v6lcl_addr, addr) && 245 (ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) || 246 (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) && 247 IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, 248 addr))) { 249 if (IPIF_CAN_LOOKUP(ipif)) { 250 ipif_refhold_locked(ipif); 251 mutex_exit(&ill->ill_lock); 252 RELEASE_CONN_LOCK(q); 253 rw_exit(&ipst->ips_ill_g_lock); 254 return (ipif); 255 } else if (IPIF_CAN_WAIT(ipif, q)) { 256 ipsq = ill->ill_phyint->phyint_ipsq; 257 mutex_enter(&ipsq->ipsq_lock); 258 mutex_enter(&ipsq->ipsq_xop->ipx_lock); 259 mutex_exit(&ill->ill_lock); 260 rw_exit(&ipst->ips_ill_g_lock); 261 ipsq_enq(ipsq, q, mp, func, NEW_OP, 262 ill); 263 mutex_exit(&ipsq->ipsq_xop->ipx_lock); 264 mutex_exit(&ipsq->ipsq_lock); 265 RELEASE_CONN_LOCK(q); 266 if (error != NULL) 267 *error = EINPROGRESS; 268 return (NULL); 269 } 270 } 271 } 272 mutex_exit(&ill->ill_lock); 273 RELEASE_CONN_LOCK(q); 274 } 275 276 /* If we already did the ptp case, then we are done */ 277 if (ptp) { 278 rw_exit(&ipst->ips_ill_g_lock); 279 if (error != NULL) 280 *error = ENXIO; 281 return (NULL); 282 } 283 ptp = B_TRUE; 284 goto repeat; 285 } 286 287 boolean_t 288 ip_addr_exists_v6(const in6_addr_t *addr, zoneid_t zoneid, 289 ip_stack_t *ipst) 290 { 291 ipif_t *ipif; 292 ill_t *ill; 293 ill_walk_context_t ctx; 294 295 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 296 297 ill = ILL_START_WALK_V6(&ctx, ipst); 298 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 299 mutex_enter(&ill->ill_lock); 300 for (ipif = ill->ill_ipif; ipif != NULL; 301 ipif = ipif->ipif_next) { 302 if (zoneid != ALL_ZONES && 303 ipif->ipif_zoneid != zoneid && 304 ipif->ipif_zoneid != ALL_ZONES) 305 continue; 306 /* Allow the ipif to be down */ 307 if (((IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr, 308 addr) && 309 (ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) || 310 ((ipif->ipif_flags & IPIF_POINTOPOINT) && 311 IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, 312 addr))) { 313 mutex_exit(&ill->ill_lock); 314 rw_exit(&ipst->ips_ill_g_lock); 315 return (B_TRUE); 316 } 317 } 318 mutex_exit(&ill->ill_lock); 319 } 320 321 rw_exit(&ipst->ips_ill_g_lock); 322 return (B_FALSE); 323 } 324 325 /* 326 * Lookup an ipif with the specified address. For point-to-point links we 327 * look for matches on either the destination address or the local address, 328 * but we skip the local address check if IPIF_UNNUMBERED is set. If the 329 * `match_ill' argument is non-NULL, the lookup is restricted to that ill 330 * (or illgrp if `match_ill' is in an IPMP group). 331 */ 332 ipif_t * 333 ipif_lookup_addr_v6(const in6_addr_t *addr, ill_t *match_ill, zoneid_t zoneid, 334 queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst) 335 { 336 return (ipif_lookup_addr_common_v6(addr, match_ill, B_TRUE, zoneid, q, 337 mp, func, error, ipst)); 338 } 339 340 /* 341 * Special abbreviated version of ipif_lookup_addr_v6() that doesn't match 342 * `match_ill' across the IPMP group. This function is only needed in some 343 * corner-cases; almost everything should use ipif_lookup_addr_v6(). 344 */ 345 ipif_t * 346 ipif_lookup_addr_exact_v6(const in6_addr_t *addr, ill_t *match_ill, 347 ip_stack_t *ipst) 348 { 349 ASSERT(match_ill != NULL); 350 return (ipif_lookup_addr_common_v6(addr, match_ill, B_FALSE, ALL_ZONES, 351 NULL, NULL, NULL, NULL, ipst)); 352 } 353 354 /* 355 * Look for an ipif with the specified address. For point-point links 356 * we look for matches on either the destination address and the local 357 * address, but we ignore the check on the local address if IPIF_UNNUMBERED 358 * is set. 359 * If the `match_ill' argument is non-NULL, the lookup is restricted to that 360 * ill (or illgrp if `match_ill' is in an IPMP group). 361 * Return the zoneid for the ipif. ALL_ZONES if none found. 362 */ 363 zoneid_t 364 ipif_lookup_addr_zoneid_v6(const in6_addr_t *addr, ill_t *match_ill, 365 ip_stack_t *ipst) 366 { 367 ipif_t *ipif; 368 ill_t *ill; 369 boolean_t ptp = B_FALSE; 370 ill_walk_context_t ctx; 371 zoneid_t zoneid; 372 373 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 374 /* 375 * Repeat twice, first based on local addresses and 376 * next time for pointopoint. 377 */ 378 repeat: 379 ill = ILL_START_WALK_V6(&ctx, ipst); 380 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 381 if (match_ill != NULL && ill != match_ill && 382 !IS_IN_SAME_ILLGRP(ill, match_ill)) { 383 continue; 384 } 385 mutex_enter(&ill->ill_lock); 386 for (ipif = ill->ill_ipif; ipif != NULL; 387 ipif = ipif->ipif_next) { 388 /* Allow the ipif to be down */ 389 if ((!ptp && (IN6_ARE_ADDR_EQUAL( 390 &ipif->ipif_v6lcl_addr, addr) && 391 (ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) || 392 (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) && 393 IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, 394 addr)) && 395 !(ipif->ipif_state_flags & IPIF_CONDEMNED)) { 396 zoneid = ipif->ipif_zoneid; 397 mutex_exit(&ill->ill_lock); 398 rw_exit(&ipst->ips_ill_g_lock); 399 /* 400 * If ipif_zoneid was ALL_ZONES then we have 401 * a trusted extensions shared IP address. 402 * In that case GLOBAL_ZONEID works to send. 403 */ 404 if (zoneid == ALL_ZONES) 405 zoneid = GLOBAL_ZONEID; 406 return (zoneid); 407 } 408 } 409 mutex_exit(&ill->ill_lock); 410 } 411 412 /* If we already did the ptp case, then we are done */ 413 if (ptp) { 414 rw_exit(&ipst->ips_ill_g_lock); 415 return (ALL_ZONES); 416 } 417 ptp = B_TRUE; 418 goto repeat; 419 } 420 421 /* 422 * Perform various checks to verify that an address would make sense as a local 423 * interface address. This is currently only called when an attempt is made 424 * to set a local address. 425 * 426 * Does not allow a v4-mapped address, an address that equals the subnet 427 * anycast address, ... a multicast address, ... 428 */ 429 boolean_t 430 ip_local_addr_ok_v6(const in6_addr_t *addr, const in6_addr_t *subnet_mask) 431 { 432 in6_addr_t subnet; 433 434 if (IN6_IS_ADDR_UNSPECIFIED(addr)) 435 return (B_TRUE); /* Allow all zeros */ 436 437 /* 438 * Don't allow all zeroes or host part, but allow 439 * all ones netmask. 440 */ 441 V6_MASK_COPY(*addr, *subnet_mask, subnet); 442 if (IN6_IS_ADDR_V4MAPPED(addr) || 443 (IN6_ARE_ADDR_EQUAL(addr, &subnet) && 444 !IN6_ARE_ADDR_EQUAL(subnet_mask, &ipv6_all_ones)) || 445 (IN6_IS_ADDR_V4COMPAT(addr) && CLASSD(V4_PART_OF_V6((*addr)))) || 446 IN6_IS_ADDR_MULTICAST(addr)) 447 return (B_FALSE); 448 449 return (B_TRUE); 450 } 451 452 /* 453 * Perform various checks to verify that an address would make sense as a 454 * remote/subnet interface address. 455 */ 456 boolean_t 457 ip_remote_addr_ok_v6(const in6_addr_t *addr, const in6_addr_t *subnet_mask) 458 { 459 in6_addr_t subnet; 460 461 if (IN6_IS_ADDR_UNSPECIFIED(addr)) 462 return (B_TRUE); /* Allow all zeros */ 463 464 V6_MASK_COPY(*addr, *subnet_mask, subnet); 465 if (IN6_IS_ADDR_V4MAPPED(addr) || 466 (IN6_ARE_ADDR_EQUAL(addr, &subnet) && 467 !IN6_ARE_ADDR_EQUAL(subnet_mask, &ipv6_all_ones)) || 468 IN6_IS_ADDR_MULTICAST(addr) || 469 (IN6_IS_ADDR_V4COMPAT(addr) && CLASSD(V4_PART_OF_V6((*addr))))) 470 return (B_FALSE); 471 472 return (B_TRUE); 473 } 474 475 /* 476 * ip_rt_add_v6 is called to add an IPv6 route to the forwarding table. 477 * ipif_arg is passed in to associate it with the correct interface 478 * (for link-local destinations and gateways). 479 */ 480 /* ARGSUSED1 */ 481 int 482 ip_rt_add_v6(const in6_addr_t *dst_addr, const in6_addr_t *mask, 483 const in6_addr_t *gw_addr, const in6_addr_t *src_addr, int flags, 484 ipif_t *ipif_arg, ire_t **ire_arg, queue_t *q, mblk_t *mp, ipsq_func_t func, 485 struct rtsa_s *sp, ip_stack_t *ipst) 486 { 487 ire_t *ire; 488 ire_t *gw_ire = NULL; 489 ipif_t *ipif; 490 boolean_t ipif_refheld = B_FALSE; 491 uint_t type; 492 int match_flags = MATCH_IRE_TYPE; 493 int error; 494 tsol_gc_t *gc = NULL; 495 tsol_gcgrp_t *gcgrp = NULL; 496 boolean_t gcgrp_xtraref = B_FALSE; 497 498 if (ire_arg != NULL) 499 *ire_arg = NULL; 500 501 /* 502 * Prevent routes with a zero gateway from being created (since 503 * interfaces can currently be plumbed and brought up with no assigned 504 * address). 505 */ 506 if (IN6_IS_ADDR_UNSPECIFIED(gw_addr)) 507 return (ENETUNREACH); 508 509 /* 510 * If this is the case of RTF_HOST being set, then we set the netmask 511 * to all ones (regardless if one was supplied). 512 */ 513 if (flags & RTF_HOST) 514 mask = &ipv6_all_ones; 515 516 /* 517 * Get the ipif, if any, corresponding to the gw_addr 518 */ 519 ipif = ipif_lookup_interface_v6(gw_addr, dst_addr, q, mp, func, 520 &error, ipst); 521 if (ipif != NULL) 522 ipif_refheld = B_TRUE; 523 else if (error == EINPROGRESS) { 524 ip1dbg(("ip_rt_add_v6: null and EINPROGRESS")); 525 return (error); 526 } 527 528 /* 529 * GateD will attempt to create routes with a loopback interface 530 * address as the gateway and with RTF_GATEWAY set. We allow 531 * these routes to be added, but create them as interface routes 532 * since the gateway is an interface address. 533 */ 534 if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) { 535 flags &= ~RTF_GATEWAY; 536 if (IN6_ARE_ADDR_EQUAL(gw_addr, &ipv6_loopback) && 537 IN6_ARE_ADDR_EQUAL(dst_addr, &ipv6_loopback) && 538 IN6_ARE_ADDR_EQUAL(mask, &ipv6_all_ones)) { 539 ire = ire_ctable_lookup_v6(dst_addr, 0, IRE_LOOPBACK, 540 ipif, ALL_ZONES, NULL, match_flags, ipst); 541 if (ire != NULL) { 542 ire_refrele(ire); 543 if (ipif_refheld) 544 ipif_refrele(ipif); 545 return (EEXIST); 546 } 547 ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x" 548 "for 0x%x\n", (void *)ipif, 549 ipif->ipif_ire_type, 550 ntohl(ipif->ipif_lcl_addr))); 551 ire = ire_create_v6( 552 dst_addr, 553 mask, 554 &ipif->ipif_v6src_addr, 555 NULL, 556 &ipif->ipif_mtu, 557 NULL, 558 NULL, 559 NULL, 560 ipif->ipif_net_type, 561 ipif, 562 NULL, 563 0, 564 0, 565 flags, 566 &ire_uinfo_null, 567 NULL, 568 NULL, 569 ipst); 570 if (ire == NULL) { 571 if (ipif_refheld) 572 ipif_refrele(ipif); 573 return (ENOMEM); 574 } 575 error = ire_add(&ire, q, mp, func, B_FALSE); 576 if (error == 0) 577 goto save_ire; 578 /* 579 * In the result of failure, ire_add() will have already 580 * deleted the ire in question, so there is no need to 581 * do that here. 582 */ 583 if (ipif_refheld) 584 ipif_refrele(ipif); 585 return (error); 586 } 587 } 588 589 /* 590 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set 591 * and the gateway address provided is one of the system's interface 592 * addresses. By using the routing socket interface and supplying an 593 * RTA_IFP sockaddr with an interface index, an alternate method of 594 * specifying an interface route to be created is available which uses 595 * the interface index that specifies the outgoing interface rather than 596 * the address of an outgoing interface (which may not be able to 597 * uniquely identify an interface). When coupled with the RTF_GATEWAY 598 * flag, routes can be specified which not only specify the next-hop to 599 * be used when routing to a certain prefix, but also which outgoing 600 * interface should be used. 601 * 602 * Previously, interfaces would have unique addresses assigned to them 603 * and so the address assigned to a particular interface could be used 604 * to identify a particular interface. One exception to this was the 605 * case of an unnumbered interface (where IPIF_UNNUMBERED was set). 606 * 607 * With the advent of IPv6 and its link-local addresses, this 608 * restriction was relaxed and interfaces could share addresses between 609 * themselves. In fact, typically all of the link-local interfaces on 610 * an IPv6 node or router will have the same link-local address. In 611 * order to differentiate between these interfaces, the use of an 612 * interface index is necessary and this index can be carried inside a 613 * RTA_IFP sockaddr (which is actually a sockaddr_dl). One restriction 614 * of using the interface index, however, is that all of the ipif's that 615 * are part of an ill have the same index and so the RTA_IFP sockaddr 616 * cannot be used to differentiate between ipif's (or logical 617 * interfaces) that belong to the same ill (physical interface). 618 * 619 * For example, in the following case involving IPv4 interfaces and 620 * logical interfaces 621 * 622 * 192.0.2.32 255.255.255.224 192.0.2.33 U if0 623 * 192.0.2.32 255.255.255.224 192.0.2.34 U if0:1 624 * 192.0.2.32 255.255.255.224 192.0.2.35 U if0:2 625 * 626 * the ipif's corresponding to each of these interface routes can be 627 * uniquely identified by the "gateway" (actually interface address). 628 * 629 * In this case involving multiple IPv6 default routes to a particular 630 * link-local gateway, the use of RTA_IFP is necessary to specify which 631 * default route is of interest: 632 * 633 * default fe80::123:4567:89ab:cdef U if0 634 * default fe80::123:4567:89ab:cdef U if1 635 */ 636 637 /* RTF_GATEWAY not set */ 638 if (!(flags & RTF_GATEWAY)) { 639 queue_t *stq; 640 641 if (sp != NULL) { 642 ip2dbg(("ip_rt_add_v6: gateway security attributes " 643 "cannot be set with interface route\n")); 644 if (ipif_refheld) 645 ipif_refrele(ipif); 646 return (EINVAL); 647 } 648 649 /* 650 * As the interface index specified with the RTA_IFP sockaddr is 651 * the same for all ipif's off of an ill, the matching logic 652 * below uses MATCH_IRE_ILL if such an index was specified. 653 * This means that routes sharing the same prefix when added 654 * using a RTA_IFP sockaddr must have distinct interface 655 * indices (namely, they must be on distinct ill's). 656 * 657 * On the other hand, since the gateway address will usually be 658 * different for each ipif on the system, the matching logic 659 * uses MATCH_IRE_IPIF in the case of a traditional interface 660 * route. This means that interface routes for the same prefix 661 * can be created if they belong to distinct ipif's and if a 662 * RTA_IFP sockaddr is not present. 663 */ 664 if (ipif_arg != NULL) { 665 if (ipif_refheld) { 666 ipif_refrele(ipif); 667 ipif_refheld = B_FALSE; 668 } 669 ipif = ipif_arg; 670 match_flags |= MATCH_IRE_ILL; 671 } else { 672 /* 673 * Check the ipif corresponding to the gw_addr 674 */ 675 if (ipif == NULL) 676 return (ENETUNREACH); 677 match_flags |= MATCH_IRE_IPIF; 678 } 679 680 ASSERT(ipif != NULL); 681 /* 682 * We check for an existing entry at this point. 683 */ 684 match_flags |= MATCH_IRE_MASK; 685 ire = ire_ftable_lookup_v6(dst_addr, mask, 0, IRE_INTERFACE, 686 ipif, NULL, ALL_ZONES, 0, NULL, match_flags, ipst); 687 if (ire != NULL) { 688 ire_refrele(ire); 689 if (ipif_refheld) 690 ipif_refrele(ipif); 691 return (EEXIST); 692 } 693 694 stq = (ipif->ipif_net_type == IRE_IF_RESOLVER) 695 ? ipif->ipif_rq : ipif->ipif_wq; 696 697 /* 698 * Create a copy of the IRE_LOOPBACK, IRE_IF_NORESOLVER or 699 * IRE_IF_RESOLVER with the modified address and netmask. 700 */ 701 ire = ire_create_v6( 702 dst_addr, 703 mask, 704 &ipif->ipif_v6src_addr, 705 NULL, 706 &ipif->ipif_mtu, 707 NULL, 708 NULL, 709 stq, 710 ipif->ipif_net_type, 711 ipif, 712 NULL, 713 0, 714 0, 715 flags, 716 &ire_uinfo_null, 717 NULL, 718 NULL, 719 ipst); 720 if (ire == NULL) { 721 if (ipif_refheld) 722 ipif_refrele(ipif); 723 return (ENOMEM); 724 } 725 726 /* 727 * Some software (for example, GateD and Sun Cluster) attempts 728 * to create (what amount to) IRE_PREFIX routes with the 729 * loopback address as the gateway. This is primarily done to 730 * set up prefixes with the RTF_REJECT flag set (for example, 731 * when generating aggregate routes). We also OR in the 732 * RTF_BLACKHOLE flag as these interface routes, by 733 * definition, can only be that. 734 * 735 * If the IRE type (as defined by ipif->ipif_net_type) is 736 * IRE_LOOPBACK, then we map the request into a 737 * IRE_IF_NORESOLVER. 738 * 739 * Needless to say, the real IRE_LOOPBACK is NOT created by this 740 * routine, but rather using ire_create_v6() directly. 741 */ 742 if (ipif->ipif_net_type == IRE_LOOPBACK) { 743 ire->ire_type = IRE_IF_NORESOLVER; 744 ire->ire_flags |= RTF_BLACKHOLE; 745 } 746 error = ire_add(&ire, q, mp, func, B_FALSE); 747 if (error == 0) 748 goto save_ire; 749 /* 750 * In the result of failure, ire_add() will have already 751 * deleted the ire in question, so there is no need to 752 * do that here. 753 */ 754 if (ipif_refheld) 755 ipif_refrele(ipif); 756 return (error); 757 } 758 if (ipif_refheld) { 759 ipif_refrele(ipif); 760 ipif_refheld = B_FALSE; 761 } 762 763 /* 764 * Get an interface IRE for the specified gateway. 765 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the 766 * gateway, it is currently unreachable and we fail the request 767 * accordingly. 768 */ 769 ipif = ipif_arg; 770 if (ipif_arg != NULL) 771 match_flags |= MATCH_IRE_ILL; 772 gw_ire = ire_ftable_lookup_v6(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, 773 NULL, ALL_ZONES, 0, NULL, match_flags, ipst); 774 if (gw_ire == NULL) 775 return (ENETUNREACH); 776 777 /* 778 * We create one of three types of IREs as a result of this request 779 * based on the netmask. A netmask of all ones (which is automatically 780 * assumed when RTF_HOST is set) results in an IRE_HOST being created. 781 * An all zeroes netmask implies a default route so an IRE_DEFAULT is 782 * created. Otherwise, an IRE_PREFIX route is created for the 783 * destination prefix. 784 */ 785 if (IN6_ARE_ADDR_EQUAL(mask, &ipv6_all_ones)) 786 type = IRE_HOST; 787 else if (IN6_IS_ADDR_UNSPECIFIED(mask)) 788 type = IRE_DEFAULT; 789 else 790 type = IRE_PREFIX; 791 792 /* check for a duplicate entry */ 793 ire = ire_ftable_lookup_v6(dst_addr, mask, gw_addr, type, ipif_arg, 794 NULL, ALL_ZONES, 0, NULL, 795 match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst); 796 if (ire != NULL) { 797 ire_refrele(gw_ire); 798 ire_refrele(ire); 799 return (EEXIST); 800 } 801 802 /* Security attribute exists */ 803 if (sp != NULL) { 804 tsol_gcgrp_addr_t ga; 805 806 /* find or create the gateway credentials group */ 807 ga.ga_af = AF_INET6; 808 ga.ga_addr = *gw_addr; 809 810 /* we hold reference to it upon success */ 811 gcgrp = gcgrp_lookup(&ga, B_TRUE); 812 if (gcgrp == NULL) { 813 ire_refrele(gw_ire); 814 return (ENOMEM); 815 } 816 817 /* 818 * Create and add the security attribute to the group; a 819 * reference to the group is made upon allocating a new 820 * entry successfully. If it finds an already-existing 821 * entry for the security attribute in the group, it simply 822 * returns it and no new reference is made to the group. 823 */ 824 gc = gc_create(sp, gcgrp, &gcgrp_xtraref); 825 if (gc == NULL) { 826 /* release reference held by gcgrp_lookup */ 827 GCGRP_REFRELE(gcgrp); 828 ire_refrele(gw_ire); 829 return (ENOMEM); 830 } 831 } 832 833 /* Create the IRE. */ 834 ire = ire_create_v6( 835 dst_addr, /* dest address */ 836 mask, /* mask */ 837 /* src address assigned by the caller? */ 838 (((flags & RTF_SETSRC) && !IN6_IS_ADDR_UNSPECIFIED(src_addr)) ? 839 src_addr : NULL), 840 gw_addr, /* gateway address */ 841 &gw_ire->ire_max_frag, 842 NULL, /* no src nce */ 843 NULL, /* no recv-from queue */ 844 NULL, /* no send-to queue */ 845 (ushort_t)type, /* IRE type */ 846 ipif_arg, 847 NULL, 848 0, 849 0, 850 flags, 851 &gw_ire->ire_uinfo, /* Inherit ULP info from gw */ 852 gc, /* security attribute */ 853 NULL, 854 ipst); 855 856 /* 857 * The ire holds a reference to the 'gc' and the 'gc' holds a 858 * reference to the 'gcgrp'. We can now release the extra reference 859 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used. 860 */ 861 if (gcgrp_xtraref) 862 GCGRP_REFRELE(gcgrp); 863 if (ire == NULL) { 864 if (gc != NULL) 865 GC_REFRELE(gc); 866 ire_refrele(gw_ire); 867 return (ENOMEM); 868 } 869 870 /* 871 * POLICY: should we allow an RTF_HOST with address INADDR_ANY? 872 * SUN/OS socket stuff does but do we really want to allow ::0 ? 873 */ 874 875 /* Add the new IRE. */ 876 error = ire_add(&ire, q, mp, func, B_FALSE); 877 /* 878 * In the result of failure, ire_add() will have already 879 * deleted the ire in question, so there is no need to 880 * do that here. 881 */ 882 if (error != 0) { 883 ire_refrele(gw_ire); 884 return (error); 885 } 886 887 if (flags & RTF_MULTIRT) { 888 /* 889 * Invoke the CGTP (multirouting) filtering module 890 * to add the dst address in the filtering database. 891 * Replicated inbound packets coming from that address 892 * will be filtered to discard the duplicates. 893 * It is not necessary to call the CGTP filter hook 894 * when the dst address is a multicast, because an 895 * IP source address cannot be a multicast. 896 */ 897 if (ipst->ips_ip_cgtp_filter_ops != NULL && 898 !IN6_IS_ADDR_MULTICAST(&(ire->ire_addr_v6))) { 899 int res; 900 901 res = ipst->ips_ip_cgtp_filter_ops->cfo_add_dest_v6( 902 ipst->ips_netstack->netstack_stackid, 903 &ire->ire_addr_v6, 904 &ire->ire_gateway_addr_v6, 905 &ire->ire_src_addr_v6, 906 &gw_ire->ire_src_addr_v6); 907 if (res != 0) { 908 ire_refrele(gw_ire); 909 ire_delete(ire); 910 return (res); 911 } 912 } 913 } 914 915 /* 916 * Now that the prefix IRE entry has been created, delete any 917 * existing gateway IRE cache entries as well as any IRE caches 918 * using the gateway, and force them to be created through 919 * ip_newroute_v6. 920 */ 921 if (gc != NULL) { 922 ASSERT(gcgrp != NULL); 923 ire_clookup_delete_cache_gw_v6(gw_addr, ALL_ZONES, ipst); 924 } 925 926 save_ire: 927 if (gw_ire != NULL) { 928 ire_refrele(gw_ire); 929 } 930 if (ipif != NULL) { 931 mblk_t *save_mp; 932 933 /* 934 * Save enough information so that we can recreate the IRE if 935 * the interface goes down and then up. The metrics associated 936 * with the route will be saved as well when rts_setmetrics() is 937 * called after the IRE has been created. In the case where 938 * memory cannot be allocated, none of this information will be 939 * saved. 940 */ 941 save_mp = allocb(sizeof (ifrt_t), BPRI_MED); 942 if (save_mp != NULL) { 943 ifrt_t *ifrt; 944 945 save_mp->b_wptr += sizeof (ifrt_t); 946 ifrt = (ifrt_t *)save_mp->b_rptr; 947 bzero(ifrt, sizeof (ifrt_t)); 948 ifrt->ifrt_type = ire->ire_type; 949 ifrt->ifrt_v6addr = ire->ire_addr_v6; 950 mutex_enter(&ire->ire_lock); 951 ifrt->ifrt_v6gateway_addr = ire->ire_gateway_addr_v6; 952 ifrt->ifrt_v6src_addr = ire->ire_src_addr_v6; 953 mutex_exit(&ire->ire_lock); 954 ifrt->ifrt_v6mask = ire->ire_mask_v6; 955 ifrt->ifrt_flags = ire->ire_flags; 956 ifrt->ifrt_max_frag = ire->ire_max_frag; 957 mutex_enter(&ipif->ipif_saved_ire_lock); 958 save_mp->b_cont = ipif->ipif_saved_ire_mp; 959 ipif->ipif_saved_ire_mp = save_mp; 960 ipif->ipif_saved_ire_cnt++; 961 mutex_exit(&ipif->ipif_saved_ire_lock); 962 } 963 } 964 if (ire_arg != NULL) { 965 /* 966 * Store the ire that was successfully added into where ire_arg 967 * points to so that callers don't have to look it up 968 * themselves (but they are responsible for ire_refrele()ing 969 * the ire when they are finished with it). 970 */ 971 *ire_arg = ire; 972 } else { 973 ire_refrele(ire); /* Held in ire_add */ 974 } 975 if (ipif_refheld) 976 ipif_refrele(ipif); 977 return (0); 978 } 979 980 /* 981 * ip_rt_delete_v6 is called to delete an IPv6 route. 982 * ipif_arg is passed in to associate it with the correct interface 983 * (for link-local destinations and gateways). 984 */ 985 /* ARGSUSED4 */ 986 int 987 ip_rt_delete_v6(const in6_addr_t *dst_addr, const in6_addr_t *mask, 988 const in6_addr_t *gw_addr, uint_t rtm_addrs, int flags, ipif_t *ipif_arg, 989 queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst) 990 { 991 ire_t *ire = NULL; 992 ipif_t *ipif; 993 uint_t type; 994 uint_t match_flags = MATCH_IRE_TYPE; 995 int err = 0; 996 boolean_t ipif_refheld = B_FALSE; 997 998 /* 999 * If this is the case of RTF_HOST being set, then we set the netmask 1000 * to all ones. Otherwise, we use the netmask if one was supplied. 1001 */ 1002 if (flags & RTF_HOST) { 1003 mask = &ipv6_all_ones; 1004 match_flags |= MATCH_IRE_MASK; 1005 } else if (rtm_addrs & RTA_NETMASK) { 1006 match_flags |= MATCH_IRE_MASK; 1007 } 1008 1009 /* 1010 * Note that RTF_GATEWAY is never set on a delete, therefore 1011 * we check if the gateway address is one of our interfaces first, 1012 * and fall back on RTF_GATEWAY routes. 1013 * 1014 * This makes it possible to delete an original 1015 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1. 1016 * 1017 * As the interface index specified with the RTA_IFP sockaddr is the 1018 * same for all ipif's off of an ill, the matching logic below uses 1019 * MATCH_IRE_ILL if such an index was specified. This means a route 1020 * sharing the same prefix and interface index as the the route 1021 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr 1022 * is specified in the request. 1023 * 1024 * On the other hand, since the gateway address will usually be 1025 * different for each ipif on the system, the matching logic 1026 * uses MATCH_IRE_IPIF in the case of a traditional interface 1027 * route. This means that interface routes for the same prefix can be 1028 * uniquely identified if they belong to distinct ipif's and if a 1029 * RTA_IFP sockaddr is not present. 1030 * 1031 * For more detail on specifying routes by gateway address and by 1032 * interface index, see the comments in ip_rt_add_v6(). 1033 */ 1034 ipif = ipif_lookup_interface_v6(gw_addr, dst_addr, q, mp, func, &err, 1035 ipst); 1036 if (ipif != NULL) { 1037 ipif_refheld = B_TRUE; 1038 if (ipif_arg != NULL) { 1039 ipif_refrele(ipif); 1040 ipif_refheld = B_FALSE; 1041 ipif = ipif_arg; 1042 match_flags |= MATCH_IRE_ILL; 1043 } else { 1044 match_flags |= MATCH_IRE_IPIF; 1045 } 1046 1047 if (ipif->ipif_ire_type == IRE_LOOPBACK) 1048 ire = ire_ctable_lookup_v6(dst_addr, 0, IRE_LOOPBACK, 1049 ipif, ALL_ZONES, NULL, match_flags, ipst); 1050 if (ire == NULL) 1051 ire = ire_ftable_lookup_v6(dst_addr, mask, 0, 1052 IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL, 1053 match_flags, ipst); 1054 } else if (err == EINPROGRESS) { 1055 return (err); 1056 } else { 1057 err = 0; 1058 } 1059 if (ire == NULL) { 1060 /* 1061 * At this point, the gateway address is not one of our own 1062 * addresses or a matching interface route was not found. We 1063 * set the IRE type to lookup based on whether 1064 * this is a host route, a default route or just a prefix. 1065 * 1066 * If an ipif_arg was passed in, then the lookup is based on an 1067 * interface index so MATCH_IRE_ILL is added to match_flags. 1068 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is 1069 * set as the route being looked up is not a traditional 1070 * interface route. 1071 */ 1072 match_flags &= ~MATCH_IRE_IPIF; 1073 match_flags |= MATCH_IRE_GW; 1074 if (ipif_arg != NULL) 1075 match_flags |= MATCH_IRE_ILL; 1076 if (IN6_ARE_ADDR_EQUAL(mask, &ipv6_all_ones)) 1077 type = IRE_HOST; 1078 else if (IN6_IS_ADDR_UNSPECIFIED(mask)) 1079 type = IRE_DEFAULT; 1080 else 1081 type = IRE_PREFIX; 1082 ire = ire_ftable_lookup_v6(dst_addr, mask, gw_addr, type, 1083 ipif_arg, NULL, ALL_ZONES, 0, NULL, match_flags, ipst); 1084 } 1085 1086 if (ipif_refheld) { 1087 ipif_refrele(ipif); 1088 ipif_refheld = B_FALSE; 1089 } 1090 if (ire == NULL) 1091 return (ESRCH); 1092 1093 if (ire->ire_flags & RTF_MULTIRT) { 1094 /* 1095 * Invoke the CGTP (multirouting) filtering module 1096 * to remove the dst address from the filtering database. 1097 * Packets coming from that address will no longer be 1098 * filtered to remove duplicates. 1099 */ 1100 if (ipst->ips_ip_cgtp_filter_ops != NULL) { 1101 err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v6( 1102 ipst->ips_netstack->netstack_stackid, 1103 &ire->ire_addr_v6, &ire->ire_gateway_addr_v6); 1104 } 1105 } 1106 1107 ipif = ire->ire_ipif; 1108 if (ipif != NULL) { 1109 mblk_t **mpp; 1110 mblk_t *mp; 1111 ifrt_t *ifrt; 1112 in6_addr_t gw_addr_v6; 1113 1114 /* Remove from ipif_saved_ire_mp list if it is there */ 1115 mutex_enter(&ire->ire_lock); 1116 gw_addr_v6 = ire->ire_gateway_addr_v6; 1117 mutex_exit(&ire->ire_lock); 1118 mutex_enter(&ipif->ipif_saved_ire_lock); 1119 for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL; 1120 mpp = &(*mpp)->b_cont) { 1121 /* 1122 * On a given ipif, the triple of address, gateway and 1123 * mask is unique for each saved IRE (in the case of 1124 * ordinary interface routes, the gateway address is 1125 * all-zeroes). 1126 */ 1127 mp = *mpp; 1128 ifrt = (ifrt_t *)mp->b_rptr; 1129 if (IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6addr, 1130 &ire->ire_addr_v6) && 1131 IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6gateway_addr, 1132 &gw_addr_v6) && 1133 IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6mask, 1134 &ire->ire_mask_v6)) { 1135 *mpp = mp->b_cont; 1136 ipif->ipif_saved_ire_cnt--; 1137 freeb(mp); 1138 break; 1139 } 1140 } 1141 mutex_exit(&ipif->ipif_saved_ire_lock); 1142 } 1143 ire_delete(ire); 1144 ire_refrele(ire); 1145 return (err); 1146 } 1147 1148 /* 1149 * Derive a token from the link layer address. 1150 */ 1151 boolean_t 1152 ill_setdefaulttoken(ill_t *ill) 1153 { 1154 int i; 1155 in6_addr_t v6addr, v6mask; 1156 1157 if (!MEDIA_V6INTFID(ill->ill_media, ill, &v6addr)) 1158 return (B_FALSE); 1159 1160 (void) ip_plen_to_mask_v6(IPV6_TOKEN_LEN, &v6mask); 1161 1162 for (i = 0; i < 4; i++) 1163 v6mask.s6_addr32[i] = v6mask.s6_addr32[i] ^ 1164 (uint32_t)0xffffffff; 1165 1166 V6_MASK_COPY(v6addr, v6mask, ill->ill_token); 1167 ill->ill_token_length = IPV6_TOKEN_LEN; 1168 return (B_TRUE); 1169 } 1170 1171 /* 1172 * Create a link-local address from a token. 1173 */ 1174 static void 1175 ipif_get_linklocal(in6_addr_t *dest, const in6_addr_t *token) 1176 { 1177 int i; 1178 1179 for (i = 0; i < 4; i++) { 1180 dest->s6_addr32[i] = 1181 token->s6_addr32[i] | ipv6_ll_template.s6_addr32[i]; 1182 } 1183 } 1184 1185 /* 1186 * Set a nice default address for either automatic tunnels tsrc/96 or 1187 * 6to4 tunnels 2002:<tsrc>::1/64 1188 */ 1189 static void 1190 ipif_set_tun_auto_addr(ipif_t *ipif, struct iftun_req *ta) 1191 { 1192 sin6_t sin6; 1193 sin_t *sin; 1194 ill_t *ill = ipif->ipif_ill; 1195 tun_t *tp = (tun_t *)ill->ill_wq->q_next->q_ptr; 1196 1197 if (ta->ifta_saddr.ss_family != AF_INET || 1198 (ipif->ipif_flags & IPIF_UP) || !ipif->ipif_isv6 || 1199 (ta->ifta_flags & IFTUN_SRC) == 0) 1200 return; 1201 1202 /* 1203 * Check the tunnel type by examining q_next->q_ptr 1204 */ 1205 if (tp->tun_flags & TUN_AUTOMATIC) { 1206 /* this is an automatic tunnel */ 1207 (void) ip_plen_to_mask_v6(IPV6_ABITS - IP_ABITS, 1208 &ipif->ipif_v6net_mask); 1209 bzero(&sin6, sizeof (sin6_t)); 1210 sin = (sin_t *)&ta->ifta_saddr; 1211 V4_PART_OF_V6(sin6.sin6_addr) = sin->sin_addr.s_addr; 1212 sin6.sin6_family = AF_INET6; 1213 (void) ip_sioctl_addr(ipif, (sin_t *)&sin6, 1214 NULL, NULL, NULL, NULL); 1215 } else if (tp->tun_flags & TUN_6TO4) { 1216 /* this is a 6to4 tunnel */ 1217 (void) ip_plen_to_mask_v6(IPV6_PREFIX_LEN, 1218 &ipif->ipif_v6net_mask); 1219 sin = (sin_t *)&ta->ifta_saddr; 1220 /* create a 6to4 address from the IPv4 tsrc */ 1221 IN6_V4ADDR_TO_6TO4(&sin->sin_addr, &sin6.sin6_addr); 1222 sin6.sin6_family = AF_INET6; 1223 (void) ip_sioctl_addr(ipif, (sin_t *)&sin6, 1224 NULL, NULL, NULL, NULL); 1225 } else { 1226 ip1dbg(("ipif_set_tun_auto_addr: Unknown tunnel type")); 1227 return; 1228 } 1229 } 1230 1231 /* 1232 * Set link local for ipif_id 0 of a configured tunnel based on the 1233 * tsrc or tdst parameter 1234 * For tunnels over IPv4 use the IPv4 address prepended with 32 zeros as 1235 * the token. 1236 * For tunnels over IPv6 use the low-order 64 bits of the "inner" IPv6 address 1237 * as the token for the "outer" link. 1238 */ 1239 void 1240 ipif_set_tun_llink(ill_t *ill, struct iftun_req *ta) 1241 { 1242 ipif_t *ipif; 1243 sin_t *sin; 1244 in6_addr_t *s6addr; 1245 1246 ASSERT(IAM_WRITER_ILL(ill)); 1247 1248 /* The first ipif must be id zero. */ 1249 ipif = ill->ill_ipif; 1250 ASSERT(ipif->ipif_id == 0); 1251 1252 /* no link local for automatic tunnels */ 1253 if (!(ipif->ipif_flags & IPIF_POINTOPOINT)) { 1254 ipif_set_tun_auto_addr(ipif, ta); 1255 return; 1256 } 1257 1258 if ((ta->ifta_flags & IFTUN_DST) && 1259 IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)) { 1260 sin6_t sin6; 1261 1262 ASSERT(!(ipif->ipif_flags & IPIF_UP)); 1263 bzero(&sin6, sizeof (sin6_t)); 1264 if ((ta->ifta_saddr.ss_family == AF_INET)) { 1265 sin = (sin_t *)&ta->ifta_daddr; 1266 V4_PART_OF_V6(sin6.sin6_addr) = 1267 sin->sin_addr.s_addr; 1268 } else { 1269 s6addr = 1270 &((sin6_t *)&ta->ifta_daddr)->sin6_addr; 1271 sin6.sin6_addr.s6_addr32[3] = s6addr->s6_addr32[3]; 1272 sin6.sin6_addr.s6_addr32[2] = s6addr->s6_addr32[2]; 1273 } 1274 ipif_get_linklocal(&ipif->ipif_v6pp_dst_addr, 1275 &sin6.sin6_addr); 1276 ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr; 1277 } 1278 if ((ta->ifta_flags & IFTUN_SRC)) { 1279 ASSERT(!(ipif->ipif_flags & IPIF_UP)); 1280 1281 /* Set the token if it isn't already set */ 1282 if (IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token)) { 1283 if ((ta->ifta_saddr.ss_family == AF_INET)) { 1284 sin = (sin_t *)&ta->ifta_saddr; 1285 V4_PART_OF_V6(ill->ill_token) = 1286 sin->sin_addr.s_addr; 1287 } else { 1288 s6addr = 1289 &((sin6_t *)&ta->ifta_saddr)->sin6_addr; 1290 ill->ill_token.s6_addr32[3] = 1291 s6addr->s6_addr32[3]; 1292 ill->ill_token.s6_addr32[2] = 1293 s6addr->s6_addr32[2]; 1294 } 1295 ill->ill_token_length = IPV6_TOKEN_LEN; 1296 } 1297 /* 1298 * Attempt to set the link local address if it isn't set. 1299 */ 1300 if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) 1301 (void) ipif_setlinklocal(ipif); 1302 } 1303 } 1304 1305 /* 1306 * Is it not possible to set the link local address? 1307 * The address can be set if the token is set, and the token 1308 * isn't too long. 1309 * Return B_TRUE if the address can't be set, or B_FALSE if it can. 1310 */ 1311 boolean_t 1312 ipif_cant_setlinklocal(ipif_t *ipif) 1313 { 1314 ill_t *ill = ipif->ipif_ill; 1315 1316 if (IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token) || 1317 ill->ill_token_length > IPV6_ABITS - IPV6_LL_PREFIXLEN) 1318 return (B_TRUE); 1319 1320 return (B_FALSE); 1321 } 1322 1323 /* 1324 * Generate a link-local address from the token. 1325 * Return zero if the address was set, or non-zero if it couldn't be set. 1326 */ 1327 int 1328 ipif_setlinklocal(ipif_t *ipif) 1329 { 1330 ill_t *ill = ipif->ipif_ill; 1331 in6_addr_t ov6addr; 1332 1333 ASSERT(IAM_WRITER_ILL(ill)); 1334 1335 if (ipif_cant_setlinklocal(ipif)) 1336 return (-1); 1337 1338 ov6addr = ipif->ipif_v6lcl_addr; 1339 ipif_get_linklocal(&ipif->ipif_v6lcl_addr, &ill->ill_token); 1340 sctp_update_ipif_addr(ipif, ov6addr); 1341 (void) ip_plen_to_mask_v6(IPV6_LL_PREFIXLEN, &ipif->ipif_v6net_mask); 1342 V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask, 1343 ipif->ipif_v6subnet); 1344 1345 if (ipif->ipif_flags & IPIF_NOLOCAL) { 1346 ipif->ipif_v6src_addr = ipv6_all_zeros; 1347 } else { 1348 ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr; 1349 } 1350 return (0); 1351 } 1352 1353 /* 1354 * This function sets up the multicast mappings in NDP. 1355 * Unlike ARP, there are no mapping_mps here. We delete the 1356 * mapping nces and add a new one. 1357 * 1358 * Returns non-zero on error and 0 on success. 1359 */ 1360 int 1361 ipif_ndp_setup_multicast(ipif_t *ipif, nce_t **ret_nce) 1362 { 1363 ill_t *ill = ipif->ipif_ill; 1364 in6_addr_t v6_mcast_addr = {(uint32_t)V6_MCAST, 0, 0, 0}; 1365 in6_addr_t v6_mcast_mask = {(uint32_t)V6_MCAST, 0, 0, 0}; 1366 in6_addr_t v6_extract_mask; 1367 uchar_t *phys_addr, *bphys_addr, *alloc_phys; 1368 nce_t *mnce = NULL; 1369 int err = 0; 1370 phyint_t *phyi = ill->ill_phyint; 1371 uint32_t hw_extract_start; 1372 dl_unitdata_req_t *dlur; 1373 ip_stack_t *ipst = ill->ill_ipst; 1374 1375 if (ret_nce != NULL) 1376 *ret_nce = NULL; 1377 1378 /* 1379 * IPMP meta-interfaces don't have any inherent multicast mappings, 1380 * and instead use the ones on the underlying interfaces. 1381 */ 1382 if (IS_IPMP(ill)) 1383 return (0); 1384 1385 /* 1386 * Delete the mapping nce. Normally these should not exist 1387 * as a previous ipif_down -> ipif_ndp_down should have deleted 1388 * all the nces. But they can exist if ip_rput_dlpi_writer 1389 * calls this when PHYI_MULTI_BCAST is set. Mappings are always 1390 * tied to the underlying ill, so don't match across the illgrp. 1391 */ 1392 mnce = ndp_lookup_v6(ill, B_FALSE, &v6_mcast_addr, B_FALSE); 1393 if (mnce != NULL) { 1394 ndp_delete(mnce); 1395 NCE_REFRELE(mnce); 1396 mnce = NULL; 1397 } 1398 1399 /* 1400 * Get media specific v6 mapping information. Note that 1401 * nd_lla_len can be 0 for tunnels. 1402 */ 1403 alloc_phys = kmem_alloc(ill->ill_nd_lla_len, KM_NOSLEEP); 1404 if ((alloc_phys == NULL) && (ill->ill_nd_lla_len != 0)) 1405 return (ENOMEM); 1406 /* 1407 * Determine the broadcast address. 1408 */ 1409 dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr; 1410 if (ill->ill_sap_length < 0) 1411 bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset; 1412 else 1413 bphys_addr = (uchar_t *)dlur + 1414 dlur->dl_dest_addr_offset + ill->ill_sap_length; 1415 1416 /* 1417 * Check PHYI_MULTI_BCAST and possible length of physical 1418 * address to determine if we use the mapping or the 1419 * broadcast address. 1420 */ 1421 if ((phyi->phyint_flags & PHYI_MULTI_BCAST) || 1422 (!MEDIA_V6MINFO(ill->ill_media, ill->ill_nd_lla_len, 1423 bphys_addr, alloc_phys, &hw_extract_start, 1424 &v6_extract_mask))) { 1425 if (ill->ill_phys_addr_length > IP_MAX_HW_LEN) { 1426 kmem_free(alloc_phys, ill->ill_nd_lla_len); 1427 return (E2BIG); 1428 } 1429 /* Use the link-layer broadcast address for MULTI_BCAST */ 1430 phys_addr = bphys_addr; 1431 bzero(&v6_extract_mask, sizeof (v6_extract_mask)); 1432 hw_extract_start = ill->ill_nd_lla_len; 1433 } else { 1434 phys_addr = alloc_phys; 1435 } 1436 if ((ipif->ipif_flags & IPIF_BROADCAST) || 1437 (ill->ill_flags & ILLF_MULTICAST) || 1438 (phyi->phyint_flags & PHYI_MULTI_BCAST)) { 1439 mutex_enter(&ipst->ips_ndp6->ndp_g_lock); 1440 err = ndp_add_v6(ill, 1441 phys_addr, 1442 &v6_mcast_addr, /* v6 address */ 1443 &v6_mcast_mask, /* v6 mask */ 1444 &v6_extract_mask, 1445 hw_extract_start, 1446 NCE_F_MAPPING | NCE_F_PERMANENT | NCE_F_NONUD, 1447 ND_REACHABLE, 1448 &mnce); 1449 mutex_exit(&ipst->ips_ndp6->ndp_g_lock); 1450 if (err == 0) { 1451 if (ret_nce != NULL) { 1452 *ret_nce = mnce; 1453 } else { 1454 NCE_REFRELE(mnce); 1455 } 1456 } 1457 } 1458 kmem_free(alloc_phys, ill->ill_nd_lla_len); 1459 return (err); 1460 } 1461 1462 /* 1463 * Get the resolver set up for a new ipif. (Always called as writer.) 1464 */ 1465 int 1466 ipif_ndp_up(ipif_t *ipif, boolean_t initial) 1467 { 1468 ill_t *ill = ipif->ipif_ill; 1469 int err = 0; 1470 nce_t *nce = NULL; 1471 nce_t *mnce = NULL; 1472 boolean_t added_ipif = B_FALSE; 1473 1474 ASSERT(IAM_WRITER_ILL(ill)); 1475 ip1dbg(("ipif_ndp_up(%s:%u)\n", ill->ill_name, ipif->ipif_id)); 1476 1477 /* 1478 * ND not supported on XRESOLV interfaces. If ND support (multicast) 1479 * added later, take out this check. 1480 */ 1481 if ((ill->ill_flags & ILLF_XRESOLV) || 1482 IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) || 1483 (!(ill->ill_net_type & IRE_INTERFACE))) { 1484 ipif->ipif_addr_ready = 1; 1485 return (0); 1486 } 1487 1488 /* 1489 * Need to setup multicast mapping only when the first 1490 * interface is coming UP. 1491 */ 1492 if (ill->ill_ipif_up_count == 0 && 1493 (ill->ill_flags & ILLF_MULTICAST)) { 1494 /* 1495 * We set the multicast before setting up the mapping for 1496 * local address because ipif_ndp_setup_multicast does 1497 * ndp_walk to delete nces which will delete the mapping 1498 * for local address also if we added the mapping for 1499 * local address first. 1500 */ 1501 err = ipif_ndp_setup_multicast(ipif, &mnce); 1502 if (err != 0) 1503 return (err); 1504 } 1505 1506 if ((ipif->ipif_flags & (IPIF_UNNUMBERED|IPIF_NOLOCAL)) == 0) { 1507 uint16_t flags; 1508 uint16_t state; 1509 uchar_t *hw_addr = NULL; 1510 ill_t *bound_ill; 1511 ipmp_illgrp_t *illg = ill->ill_grp; 1512 1513 /* Permanent entries don't need NUD */ 1514 flags = NCE_F_PERMANENT | NCE_F_NONUD; 1515 if (ill->ill_flags & ILLF_ROUTER) 1516 flags |= NCE_F_ISROUTER; 1517 1518 if (ipif->ipif_flags & IPIF_ANYCAST) 1519 flags |= NCE_F_ANYCAST; 1520 1521 if (IS_IPMP(ill)) { 1522 ASSERT(ill->ill_net_type == IRE_IF_RESOLVER); 1523 /* 1524 * If we're here via ipif_up(), then the ipif won't be 1525 * bound yet -- add it to the group, which will bind 1526 * it if possible. (We would add it in ipif_up(), but 1527 * deleting on failure there is gruesome.) If we're 1528 * here via ipmp_ill_bind_ipif(), then the ipif has 1529 * already been added to the group and we just need to 1530 * use the binding. 1531 */ 1532 if ((bound_ill = ipmp_ipif_bound_ill(ipif)) == NULL) { 1533 bound_ill = ipmp_illgrp_add_ipif(illg, ipif); 1534 if (bound_ill == NULL) { 1535 /* 1536 * We couldn't bind the ipif to an ill 1537 * yet, so we have nothing to publish. 1538 * Set ipif_addr_ready so that this 1539 * address can be used locally for now. 1540 * The routing socket message will be 1541 * sent from ipif_up_done_v6(). 1542 */ 1543 ipif->ipif_addr_ready = 1; 1544 return (0); 1545 } 1546 added_ipif = B_TRUE; 1547 } 1548 hw_addr = bound_ill->ill_nd_lla; 1549 } else { 1550 bound_ill = ill; 1551 if (ill->ill_net_type == IRE_IF_RESOLVER) 1552 hw_addr = ill->ill_nd_lla; 1553 } 1554 1555 /* 1556 * If this is an initial bring-up (or the ipif was never 1557 * completely brought up), do DAD. Otherwise, we're here 1558 * because IPMP has rebound an address to this ill: send 1559 * unsolicited advertisements to inform others. 1560 */ 1561 if (initial || !ipif->ipif_addr_ready) { 1562 state = ND_PROBE; 1563 } else { 1564 state = ND_REACHABLE; 1565 flags |= NCE_F_UNSOL_ADV; 1566 } 1567 /* 1568 * Create an nce for the local address. We pass a match_illgrp 1569 * of B_TRUE because the local address must be unique across 1570 * the illgrp, and the existence of an nce with nce_ill set 1571 * to any ill in the group is indicative of a duplicate address 1572 */ 1573 err = ndp_lookup_then_add_v6(bound_ill, 1574 B_TRUE, 1575 hw_addr, 1576 &ipif->ipif_v6lcl_addr, 1577 &ipv6_all_ones, 1578 &ipv6_all_zeros, 1579 0, 1580 flags, 1581 state, 1582 &nce); 1583 switch (err) { 1584 case 0: 1585 ip1dbg(("ipif_ndp_up: NCE created for %s\n", 1586 ill->ill_name)); 1587 ipif->ipif_addr_ready = 1; 1588 ipif->ipif_added_nce = 1; 1589 break; 1590 case EINPROGRESS: 1591 ip1dbg(("ipif_ndp_up: running DAD now for %s\n", 1592 ill->ill_name)); 1593 ipif->ipif_added_nce = 1; 1594 break; 1595 case EEXIST: 1596 NCE_REFRELE(nce); 1597 ip1dbg(("ipif_ndp_up: NCE already exists for %s\n", 1598 ill->ill_name)); 1599 goto fail; 1600 default: 1601 ip1dbg(("ipif_ndp_up: NCE creation failed for %s\n", 1602 ill->ill_name)); 1603 goto fail; 1604 } 1605 } else { 1606 /* No local NCE for this entry */ 1607 ipif->ipif_addr_ready = 1; 1608 } 1609 if (nce != NULL) 1610 NCE_REFRELE(nce); 1611 if (mnce != NULL) 1612 NCE_REFRELE(mnce); 1613 return (0); 1614 fail: 1615 if (mnce != NULL) { 1616 ndp_delete(mnce); 1617 NCE_REFRELE(mnce); 1618 } 1619 if (added_ipif) 1620 ipmp_illgrp_del_ipif(ill->ill_grp, ipif); 1621 1622 return (err); 1623 } 1624 1625 /* Remove all cache entries for this logical interface */ 1626 void 1627 ipif_ndp_down(ipif_t *ipif) 1628 { 1629 nce_t *nce; 1630 ill_t *ill = ipif->ipif_ill; 1631 1632 ASSERT(IAM_WRITER_ILL(ill)); 1633 1634 if (ipif->ipif_isv6) { 1635 ill_t *bound_ill; 1636 1637 if (IS_IPMP(ill)) 1638 bound_ill = ipmp_ipif_bound_ill(ipif); 1639 else 1640 bound_ill = ill; 1641 1642 if (bound_ill != NULL && ipif->ipif_added_nce) { 1643 nce = ndp_lookup_v6(bound_ill, 1644 B_TRUE, 1645 &ipif->ipif_v6lcl_addr, 1646 B_FALSE); 1647 if (nce != NULL) { 1648 ndp_delete(nce); 1649 NCE_REFRELE(nce); 1650 } 1651 ipif->ipif_added_nce = 0; 1652 } 1653 1654 /* 1655 * Make IPMP aware of the deleted data address. 1656 */ 1657 if (IS_IPMP(ill)) 1658 ipmp_illgrp_del_ipif(ill->ill_grp, ipif); 1659 } 1660 1661 /* 1662 * Remove mapping and all other nces dependent on this ill 1663 * when the last ipif is going away. 1664 */ 1665 if (ill->ill_ipif_up_count == 0) 1666 ndp_walk(ill, (pfi_t)ndp_delete_per_ill, ill, ill->ill_ipst); 1667 } 1668 1669 /* 1670 * Used when an interface comes up to recreate any extra routes on this 1671 * interface. 1672 */ 1673 static ire_t ** 1674 ipif_recover_ire_v6(ipif_t *ipif) 1675 { 1676 mblk_t *mp; 1677 ire_t **ipif_saved_irep; 1678 ire_t **irep; 1679 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst; 1680 1681 ip1dbg(("ipif_recover_ire_v6(%s:%u)", ipif->ipif_ill->ill_name, 1682 ipif->ipif_id)); 1683 1684 ASSERT(ipif->ipif_isv6); 1685 1686 mutex_enter(&ipif->ipif_saved_ire_lock); 1687 ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) * 1688 ipif->ipif_saved_ire_cnt, KM_NOSLEEP); 1689 if (ipif_saved_irep == NULL) { 1690 mutex_exit(&ipif->ipif_saved_ire_lock); 1691 return (NULL); 1692 } 1693 1694 irep = ipif_saved_irep; 1695 1696 for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) { 1697 ire_t *ire; 1698 queue_t *rfq; 1699 queue_t *stq; 1700 ifrt_t *ifrt; 1701 in6_addr_t *src_addr; 1702 in6_addr_t *gateway_addr; 1703 char buf[INET6_ADDRSTRLEN]; 1704 ushort_t type; 1705 1706 /* 1707 * When the ire was initially created and then added in 1708 * ip_rt_add_v6(), it was created either using 1709 * ipif->ipif_net_type in the case of a traditional interface 1710 * route, or as one of the IRE_OFFSUBNET types (with the 1711 * exception of IRE_HOST type redirect ire which is created by 1712 * icmp_redirect_v6() and which we don't need to save or 1713 * recover). In the case where ipif->ipif_net_type was 1714 * IRE_LOOPBACK, ip_rt_add_v6() will update the ire_type to 1715 * IRE_IF_NORESOLVER before calling ire_add_v6() to satisfy 1716 * software like GateD and Sun Cluster which creates routes 1717 * using the the loopback interface's address as a gateway. 1718 * 1719 * As ifrt->ifrt_type reflects the already updated ire_type, 1720 * ire_create_v6() will be called in the same way here as in 1721 * ip_rt_add_v6(), namely using ipif->ipif_net_type when the 1722 * route looks like a traditional interface route (where 1723 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise 1724 * using the saved ifrt->ifrt_type. This means that in 1725 * the case where ipif->ipif_net_type is IRE_LOOPBACK, 1726 * the ire created by ire_create_v6() will be an IRE_LOOPBACK, 1727 * it will then be turned into an IRE_IF_NORESOLVER and then 1728 * added by ire_add_v6(). 1729 */ 1730 ifrt = (ifrt_t *)mp->b_rptr; 1731 if (ifrt->ifrt_type & IRE_INTERFACE) { 1732 rfq = NULL; 1733 stq = (ipif->ipif_net_type == IRE_IF_RESOLVER) 1734 ? ipif->ipif_rq : ipif->ipif_wq; 1735 src_addr = (ifrt->ifrt_flags & RTF_SETSRC) 1736 ? &ifrt->ifrt_v6src_addr 1737 : &ipif->ipif_v6src_addr; 1738 gateway_addr = NULL; 1739 type = ipif->ipif_net_type; 1740 } else { 1741 rfq = NULL; 1742 stq = NULL; 1743 src_addr = (ifrt->ifrt_flags & RTF_SETSRC) 1744 ? &ifrt->ifrt_v6src_addr : NULL; 1745 gateway_addr = &ifrt->ifrt_v6gateway_addr; 1746 type = ifrt->ifrt_type; 1747 } 1748 1749 /* 1750 * Create a copy of the IRE with the saved address and netmask. 1751 */ 1752 ip1dbg(("ipif_recover_ire_v6: creating IRE %s (%d) for %s/%d\n", 1753 ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type, 1754 inet_ntop(AF_INET6, &ifrt->ifrt_v6addr, buf, sizeof (buf)), 1755 ip_mask_to_plen_v6(&ifrt->ifrt_v6mask))); 1756 ire = ire_create_v6( 1757 &ifrt->ifrt_v6addr, 1758 &ifrt->ifrt_v6mask, 1759 src_addr, 1760 gateway_addr, 1761 &ifrt->ifrt_max_frag, 1762 NULL, 1763 rfq, 1764 stq, 1765 type, 1766 ipif, 1767 NULL, 1768 0, 1769 0, 1770 ifrt->ifrt_flags, 1771 &ifrt->ifrt_iulp_info, 1772 NULL, 1773 NULL, 1774 ipst); 1775 if (ire == NULL) { 1776 mutex_exit(&ipif->ipif_saved_ire_lock); 1777 kmem_free(ipif_saved_irep, 1778 ipif->ipif_saved_ire_cnt * sizeof (ire_t *)); 1779 return (NULL); 1780 } 1781 1782 /* 1783 * Some software (for example, GateD and Sun Cluster) attempts 1784 * to create (what amount to) IRE_PREFIX routes with the 1785 * loopback address as the gateway. This is primarily done to 1786 * set up prefixes with the RTF_REJECT flag set (for example, 1787 * when generating aggregate routes.) 1788 * 1789 * If the IRE type (as defined by ipif->ipif_net_type) is 1790 * IRE_LOOPBACK, then we map the request into a 1791 * IRE_IF_NORESOLVER. 1792 */ 1793 if (ipif->ipif_net_type == IRE_LOOPBACK) 1794 ire->ire_type = IRE_IF_NORESOLVER; 1795 /* 1796 * ire held by ire_add, will be refreled' in ipif_up_done 1797 * towards the end 1798 */ 1799 (void) ire_add(&ire, NULL, NULL, NULL, B_FALSE); 1800 *irep = ire; 1801 irep++; 1802 ip1dbg(("ipif_recover_ire_v6: added ire %p\n", (void *)ire)); 1803 } 1804 mutex_exit(&ipif->ipif_saved_ire_lock); 1805 return (ipif_saved_irep); 1806 } 1807 1808 /* 1809 * Return the scope of the given IPv6 address. If the address is an 1810 * IPv4 mapped IPv6 address, return the scope of the corresponding 1811 * IPv4 address. 1812 */ 1813 in6addr_scope_t 1814 ip_addr_scope_v6(const in6_addr_t *addr) 1815 { 1816 static in6_addr_t ipv6loopback = IN6ADDR_LOOPBACK_INIT; 1817 1818 if (IN6_IS_ADDR_V4MAPPED(addr)) { 1819 in_addr_t v4addr_h = ntohl(V4_PART_OF_V6((*addr))); 1820 if ((v4addr_h >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 1821 (v4addr_h & IN_AUTOCONF_MASK) == IN_AUTOCONF_NET) 1822 return (IP6_SCOPE_LINKLOCAL); 1823 if ((v4addr_h & IN_PRIVATE8_MASK) == IN_PRIVATE8_NET || 1824 (v4addr_h & IN_PRIVATE12_MASK) == IN_PRIVATE12_NET || 1825 (v4addr_h & IN_PRIVATE16_MASK) == IN_PRIVATE16_NET) 1826 return (IP6_SCOPE_SITELOCAL); 1827 return (IP6_SCOPE_GLOBAL); 1828 } 1829 1830 if (IN6_IS_ADDR_MULTICAST(addr)) 1831 return (IN6_ADDR_MC_SCOPE(addr)); 1832 1833 /* link-local and loopback addresses are of link-local scope */ 1834 if (IN6_IS_ADDR_LINKLOCAL(addr) || 1835 IN6_ARE_ADDR_EQUAL(addr, &ipv6loopback)) 1836 return (IP6_SCOPE_LINKLOCAL); 1837 if (IN6_IS_ADDR_SITELOCAL(addr)) 1838 return (IP6_SCOPE_SITELOCAL); 1839 return (IP6_SCOPE_GLOBAL); 1840 } 1841 1842 1843 /* 1844 * Returns the length of the common prefix of a1 and a2, as per 1845 * CommonPrefixLen() defined in RFC 3484. 1846 */ 1847 static int 1848 ip_common_prefix_v6(const in6_addr_t *a1, const in6_addr_t *a2) 1849 { 1850 int i; 1851 uint32_t a1val, a2val, mask; 1852 1853 for (i = 0; i < 4; i++) { 1854 if ((a1val = a1->s6_addr32[i]) != (a2val = a2->s6_addr32[i])) { 1855 a1val ^= a2val; 1856 i *= 32; 1857 mask = 0x80000000u; 1858 while (!(a1val & mask)) { 1859 mask >>= 1; 1860 i++; 1861 } 1862 return (i); 1863 } 1864 } 1865 return (IPV6_ABITS); 1866 } 1867 1868 #define IPIF_VALID_IPV6_SOURCE(ipif) \ 1869 (((ipif)->ipif_flags & IPIF_UP) && \ 1870 !((ipif)->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST)) && \ 1871 (ipif)->ipif_addr_ready) 1872 1873 /* source address candidate */ 1874 typedef struct candidate { 1875 ipif_t *cand_ipif; 1876 /* The properties of this candidate */ 1877 boolean_t cand_isdst; 1878 boolean_t cand_isdst_set; 1879 in6addr_scope_t cand_scope; 1880 boolean_t cand_scope_set; 1881 boolean_t cand_isdeprecated; 1882 boolean_t cand_isdeprecated_set; 1883 boolean_t cand_ispreferred; 1884 boolean_t cand_ispreferred_set; 1885 boolean_t cand_matchedinterface; 1886 boolean_t cand_matchedinterface_set; 1887 boolean_t cand_matchedlabel; 1888 boolean_t cand_matchedlabel_set; 1889 boolean_t cand_istmp; 1890 boolean_t cand_istmp_set; 1891 int cand_common_pref; 1892 boolean_t cand_common_pref_set; 1893 boolean_t cand_pref_eq; 1894 boolean_t cand_pref_eq_set; 1895 int cand_pref_len; 1896 boolean_t cand_pref_len_set; 1897 } cand_t; 1898 #define cand_srcaddr cand_ipif->ipif_v6lcl_addr 1899 #define cand_mask cand_ipif->ipif_v6net_mask 1900 #define cand_flags cand_ipif->ipif_flags 1901 #define cand_ill cand_ipif->ipif_ill 1902 #define cand_zoneid cand_ipif->ipif_zoneid 1903 1904 /* information about the destination for source address selection */ 1905 typedef struct dstinfo { 1906 const in6_addr_t *dst_addr; 1907 ill_t *dst_ill; 1908 uint_t dst_restrict_ill; 1909 boolean_t dst_prefer_src_tmp; 1910 in6addr_scope_t dst_scope; 1911 char *dst_label; 1912 } dstinfo_t; 1913 1914 /* 1915 * The following functions are rules used to select a source address in 1916 * ipif_select_source_v6(). Each rule compares a current candidate (cc) 1917 * against the best candidate (bc). Each rule has three possible outcomes; 1918 * the candidate is preferred over the best candidate (CAND_PREFER), the 1919 * candidate is not preferred over the best candidate (CAND_AVOID), or the 1920 * candidate is of equal value as the best candidate (CAND_TIE). 1921 * 1922 * These rules are part of a greater "Default Address Selection for IPv6" 1923 * sheme, which is standards based work coming out of the IETF ipv6 working 1924 * group. The IETF document defines both IPv6 source address selection and 1925 * destination address ordering. The rules defined here implement the IPv6 1926 * source address selection. Destination address ordering is done by 1927 * libnsl, and uses a similar set of rules to implement the sorting. 1928 * 1929 * Most of the rules are defined by the RFC and are not typically altered. The 1930 * last rule, number 8, has language that allows for local preferences. In the 1931 * scheme below, this means that new Solaris rules should normally go between 1932 * rule_ifprefix and rule_prefix. 1933 */ 1934 typedef enum {CAND_AVOID, CAND_TIE, CAND_PREFER} rule_res_t; 1935 typedef rule_res_t (*rulef_t)(cand_t *, cand_t *, const dstinfo_t *, 1936 ip_stack_t *); 1937 1938 /* Prefer an address if it is equal to the destination address. */ 1939 /* ARGSUSED3 */ 1940 static rule_res_t 1941 rule_isdst(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, ip_stack_t *ipst) 1942 { 1943 if (!bc->cand_isdst_set) { 1944 bc->cand_isdst = 1945 IN6_ARE_ADDR_EQUAL(&bc->cand_srcaddr, dstinfo->dst_addr); 1946 bc->cand_isdst_set = B_TRUE; 1947 } 1948 1949 cc->cand_isdst = 1950 IN6_ARE_ADDR_EQUAL(&cc->cand_srcaddr, dstinfo->dst_addr); 1951 cc->cand_isdst_set = B_TRUE; 1952 1953 if (cc->cand_isdst == bc->cand_isdst) 1954 return (CAND_TIE); 1955 else if (cc->cand_isdst) 1956 return (CAND_PREFER); 1957 else 1958 return (CAND_AVOID); 1959 } 1960 1961 /* 1962 * Prefer addresses that are of closest scope to the destination. Always 1963 * prefer addresses that are of greater scope than the destination over 1964 * those that are of lesser scope than the destination. 1965 */ 1966 /* ARGSUSED3 */ 1967 static rule_res_t 1968 rule_scope(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, ip_stack_t *ipst) 1969 { 1970 if (!bc->cand_scope_set) { 1971 bc->cand_scope = ip_addr_scope_v6(&bc->cand_srcaddr); 1972 bc->cand_scope_set = B_TRUE; 1973 } 1974 1975 cc->cand_scope = ip_addr_scope_v6(&cc->cand_srcaddr); 1976 cc->cand_scope_set = B_TRUE; 1977 1978 if (cc->cand_scope < bc->cand_scope) { 1979 if (cc->cand_scope < dstinfo->dst_scope) 1980 return (CAND_AVOID); 1981 else 1982 return (CAND_PREFER); 1983 } else if (bc->cand_scope < cc->cand_scope) { 1984 if (bc->cand_scope < dstinfo->dst_scope) 1985 return (CAND_PREFER); 1986 else 1987 return (CAND_AVOID); 1988 } else { 1989 return (CAND_TIE); 1990 } 1991 } 1992 1993 /* 1994 * Prefer non-deprecated source addresses. 1995 */ 1996 /* ARGSUSED2 */ 1997 static rule_res_t 1998 rule_deprecated(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, 1999 ip_stack_t *ipst) 2000 { 2001 if (!bc->cand_isdeprecated_set) { 2002 bc->cand_isdeprecated = 2003 ((bc->cand_flags & IPIF_DEPRECATED) != 0); 2004 bc->cand_isdeprecated_set = B_TRUE; 2005 } 2006 2007 cc->cand_isdeprecated = ((cc->cand_flags & IPIF_DEPRECATED) != 0); 2008 cc->cand_isdeprecated_set = B_TRUE; 2009 2010 if (bc->cand_isdeprecated == cc->cand_isdeprecated) 2011 return (CAND_TIE); 2012 else if (cc->cand_isdeprecated) 2013 return (CAND_AVOID); 2014 else 2015 return (CAND_PREFER); 2016 } 2017 2018 /* 2019 * Prefer source addresses that have the IPIF_PREFERRED flag set. This 2020 * rule must be before rule_interface because the flag could be set on any 2021 * interface, not just the interface being used for outgoing packets (for 2022 * example, the IFF_PREFERRED could be set on an address assigned to the 2023 * loopback interface). 2024 */ 2025 /* ARGSUSED2 */ 2026 static rule_res_t 2027 rule_preferred(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, 2028 ip_stack_t *ipst) 2029 { 2030 if (!bc->cand_ispreferred_set) { 2031 bc->cand_ispreferred = ((bc->cand_flags & IPIF_PREFERRED) != 0); 2032 bc->cand_ispreferred_set = B_TRUE; 2033 } 2034 2035 cc->cand_ispreferred = ((cc->cand_flags & IPIF_PREFERRED) != 0); 2036 cc->cand_ispreferred_set = B_TRUE; 2037 2038 if (bc->cand_ispreferred == cc->cand_ispreferred) 2039 return (CAND_TIE); 2040 else if (cc->cand_ispreferred) 2041 return (CAND_PREFER); 2042 else 2043 return (CAND_AVOID); 2044 } 2045 2046 /* 2047 * Prefer source addresses that are assigned to the outgoing interface. 2048 */ 2049 /* ARGSUSED3 */ 2050 static rule_res_t 2051 rule_interface(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, 2052 ip_stack_t *ipst) 2053 { 2054 ill_t *dstill = dstinfo->dst_ill; 2055 2056 /* 2057 * If dstinfo->dst_restrict_ill is set, this rule is unnecessary 2058 * since we know all candidates will be on the same link. 2059 */ 2060 if (dstinfo->dst_restrict_ill) 2061 return (CAND_TIE); 2062 2063 if (!bc->cand_matchedinterface_set) { 2064 bc->cand_matchedinterface = bc->cand_ill == dstill; 2065 bc->cand_matchedinterface_set = B_TRUE; 2066 } 2067 2068 cc->cand_matchedinterface = cc->cand_ill == dstill; 2069 cc->cand_matchedinterface_set = B_TRUE; 2070 2071 if (bc->cand_matchedinterface == cc->cand_matchedinterface) 2072 return (CAND_TIE); 2073 else if (cc->cand_matchedinterface) 2074 return (CAND_PREFER); 2075 else 2076 return (CAND_AVOID); 2077 } 2078 2079 /* 2080 * Prefer source addresses whose label matches the destination's label. 2081 */ 2082 static rule_res_t 2083 rule_label(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, ip_stack_t *ipst) 2084 { 2085 char *label; 2086 2087 if (!bc->cand_matchedlabel_set) { 2088 label = ip6_asp_lookup(&bc->cand_srcaddr, NULL, ipst); 2089 bc->cand_matchedlabel = 2090 ip6_asp_labelcmp(label, dstinfo->dst_label); 2091 bc->cand_matchedlabel_set = B_TRUE; 2092 } 2093 2094 label = ip6_asp_lookup(&cc->cand_srcaddr, NULL, ipst); 2095 cc->cand_matchedlabel = ip6_asp_labelcmp(label, dstinfo->dst_label); 2096 cc->cand_matchedlabel_set = B_TRUE; 2097 2098 if (bc->cand_matchedlabel == cc->cand_matchedlabel) 2099 return (CAND_TIE); 2100 else if (cc->cand_matchedlabel) 2101 return (CAND_PREFER); 2102 else 2103 return (CAND_AVOID); 2104 } 2105 2106 /* 2107 * Prefer public addresses over temporary ones. An application can reverse 2108 * the logic of this rule and prefer temporary addresses by using the 2109 * IPV6_SRC_PREFERENCES socket option. 2110 */ 2111 /* ARGSUSED3 */ 2112 static rule_res_t 2113 rule_temporary(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, 2114 ip_stack_t *ipst) 2115 { 2116 if (!bc->cand_istmp_set) { 2117 bc->cand_istmp = ((bc->cand_flags & IPIF_TEMPORARY) != 0); 2118 bc->cand_istmp_set = B_TRUE; 2119 } 2120 2121 cc->cand_istmp = ((cc->cand_flags & IPIF_TEMPORARY) != 0); 2122 cc->cand_istmp_set = B_TRUE; 2123 2124 if (bc->cand_istmp == cc->cand_istmp) 2125 return (CAND_TIE); 2126 2127 if (dstinfo->dst_prefer_src_tmp && cc->cand_istmp) 2128 return (CAND_PREFER); 2129 else if (!dstinfo->dst_prefer_src_tmp && !cc->cand_istmp) 2130 return (CAND_PREFER); 2131 else 2132 return (CAND_AVOID); 2133 } 2134 2135 /* 2136 * Prefer source addresses with longer matching prefix with the destination 2137 * under the interface mask. This gets us on the same subnet before applying 2138 * any Solaris-specific rules. 2139 */ 2140 /* ARGSUSED3 */ 2141 static rule_res_t 2142 rule_ifprefix(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, 2143 ip_stack_t *ipst) 2144 { 2145 if (!bc->cand_pref_eq_set) { 2146 bc->cand_pref_eq = V6_MASK_EQ_2(bc->cand_srcaddr, 2147 bc->cand_mask, *dstinfo->dst_addr); 2148 bc->cand_pref_eq_set = B_TRUE; 2149 } 2150 2151 cc->cand_pref_eq = V6_MASK_EQ_2(cc->cand_srcaddr, cc->cand_mask, 2152 *dstinfo->dst_addr); 2153 cc->cand_pref_eq_set = B_TRUE; 2154 2155 if (bc->cand_pref_eq) { 2156 if (cc->cand_pref_eq) { 2157 if (!bc->cand_pref_len_set) { 2158 bc->cand_pref_len = 2159 ip_mask_to_plen_v6(&bc->cand_mask); 2160 bc->cand_pref_len_set = B_TRUE; 2161 } 2162 cc->cand_pref_len = ip_mask_to_plen_v6(&cc->cand_mask); 2163 cc->cand_pref_len_set = B_TRUE; 2164 if (bc->cand_pref_len == cc->cand_pref_len) 2165 return (CAND_TIE); 2166 else if (bc->cand_pref_len > cc->cand_pref_len) 2167 return (CAND_AVOID); 2168 else 2169 return (CAND_PREFER); 2170 } else { 2171 return (CAND_AVOID); 2172 } 2173 } else { 2174 if (cc->cand_pref_eq) 2175 return (CAND_PREFER); 2176 else 2177 return (CAND_TIE); 2178 } 2179 } 2180 2181 /* 2182 * Prefer to use zone-specific addresses when possible instead of all-zones 2183 * addresses. 2184 */ 2185 /* ARGSUSED2 */ 2186 static rule_res_t 2187 rule_zone_specific(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, 2188 ip_stack_t *ipst) 2189 { 2190 if ((bc->cand_zoneid == ALL_ZONES) == 2191 (cc->cand_zoneid == ALL_ZONES)) 2192 return (CAND_TIE); 2193 else if (cc->cand_zoneid == ALL_ZONES) 2194 return (CAND_AVOID); 2195 else 2196 return (CAND_PREFER); 2197 } 2198 2199 /* 2200 * Prefer to use DHCPv6 (first) and static addresses (second) when possible 2201 * instead of statelessly autoconfigured addresses. 2202 * 2203 * This is done after trying all other preferences (and before the final tie 2204 * breaker) so that, if all else is equal, we select addresses configured by 2205 * DHCPv6 over other addresses. We presume that DHCPv6 addresses, unlike 2206 * stateless autoconfigured addresses, are deliberately configured by an 2207 * administrator, and thus are correctly set up in DNS and network packet 2208 * filters. 2209 */ 2210 /* ARGSUSED2 */ 2211 static rule_res_t 2212 rule_addr_type(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, 2213 ip_stack_t *ipst) 2214 { 2215 #define ATYPE(x) \ 2216 ((x) & IPIF_DHCPRUNNING) ? 1 : ((x) & IPIF_ADDRCONF) ? 3 : 2 2217 int bcval = ATYPE(bc->cand_flags); 2218 int ccval = ATYPE(cc->cand_flags); 2219 #undef ATYPE 2220 2221 if (bcval == ccval) 2222 return (CAND_TIE); 2223 else if (ccval < bcval) 2224 return (CAND_PREFER); 2225 else 2226 return (CAND_AVOID); 2227 } 2228 2229 /* 2230 * Prefer source addresses with longer matching prefix with the destination. 2231 * We do the longest matching prefix calculation by doing an xor of both 2232 * addresses with the destination, and pick the address with the longest string 2233 * of leading zeros, as per CommonPrefixLen() defined in RFC 3484. 2234 */ 2235 /* ARGSUSED3 */ 2236 static rule_res_t 2237 rule_prefix(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, ip_stack_t *ipst) 2238 { 2239 /* 2240 * For IPMP, we always want to choose a random source address from 2241 * among any equally usable addresses, so always report a tie. 2242 */ 2243 if (IS_IPMP(dstinfo->dst_ill)) 2244 return (CAND_TIE); 2245 2246 if (!bc->cand_common_pref_set) { 2247 bc->cand_common_pref = ip_common_prefix_v6(&bc->cand_srcaddr, 2248 dstinfo->dst_addr); 2249 bc->cand_common_pref_set = B_TRUE; 2250 } 2251 2252 cc->cand_common_pref = ip_common_prefix_v6(&cc->cand_srcaddr, 2253 dstinfo->dst_addr); 2254 cc->cand_common_pref_set = B_TRUE; 2255 2256 if (bc->cand_common_pref == cc->cand_common_pref) 2257 return (CAND_TIE); 2258 else if (bc->cand_common_pref > cc->cand_common_pref) 2259 return (CAND_AVOID); 2260 else 2261 return (CAND_PREFER); 2262 } 2263 2264 /* 2265 * Last rule: we must pick something, so just prefer the current best 2266 * candidate. 2267 */ 2268 /* ARGSUSED */ 2269 static rule_res_t 2270 rule_must_be_last(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, 2271 ip_stack_t *ipst) 2272 { 2273 return (CAND_AVOID); 2274 } 2275 2276 /* 2277 * Determine the best source address given a destination address and a 2278 * destination ill. If no suitable source address is found, it returns 2279 * NULL. If there is a usable address pointed to by the usesrc 2280 * (i.e ill_usesrc_ifindex != 0) then return that first since it is more 2281 * fine grained (i.e per interface) 2282 * 2283 * This implementation is based on the "Default Address Selection for IPv6" 2284 * specification produced by the IETF IPv6 working group. It has been 2285 * implemented so that the list of addresses is only traversed once (the 2286 * specification's algorithm could traverse the list of addresses once for 2287 * every rule). 2288 * 2289 * The restrict_ill argument restricts the algorithm to choose a source 2290 * address that is assigned to the destination ill. This is used when 2291 * the destination address is a link-local or multicast address, and when 2292 * ipv6_strict_dst_multihoming is turned on. 2293 * 2294 * src_prefs is the caller's set of source address preferences. If source 2295 * address selection is being called to determine the source address of a 2296 * connected socket (from ip_bind_connected_v6()), then the preferences are 2297 * taken from conn_src_preferences. These preferences can be set on a 2298 * per-socket basis using the IPV6_SRC_PREFERENCES socket option. The only 2299 * preference currently implemented is for rfc3041 temporary addresses. 2300 */ 2301 ipif_t * 2302 ipif_select_source_v6(ill_t *dstill, const in6_addr_t *dst, 2303 boolean_t restrict_ill, uint32_t src_prefs, zoneid_t zoneid) 2304 { 2305 dstinfo_t dstinfo; 2306 char dstr[INET6_ADDRSTRLEN]; 2307 char sstr[INET6_ADDRSTRLEN]; 2308 ipif_t *ipif, *start_ipif, *next_ipif; 2309 ill_t *ill, *usesrc_ill = NULL, *ipmp_ill = NULL; 2310 ill_walk_context_t ctx; 2311 cand_t best_c; /* The best candidate */ 2312 cand_t curr_c; /* The current candidate */ 2313 uint_t index; 2314 boolean_t first_candidate = B_TRUE; 2315 rule_res_t rule_result; 2316 tsol_tpc_t *src_rhtp, *dst_rhtp; 2317 ip_stack_t *ipst = dstill->ill_ipst; 2318 2319 /* 2320 * The list of ordering rules. They are applied in the order they 2321 * appear in the list. 2322 * 2323 * Solaris doesn't currently support Mobile IPv6, so there's no 2324 * rule_mipv6 corresponding to rule 4 in the specification. 2325 */ 2326 rulef_t rules[] = { 2327 rule_isdst, 2328 rule_scope, 2329 rule_deprecated, 2330 rule_preferred, 2331 rule_interface, 2332 rule_label, 2333 rule_temporary, 2334 rule_ifprefix, /* local rules after this */ 2335 rule_zone_specific, 2336 rule_addr_type, 2337 rule_prefix, /* local rules before this */ 2338 rule_must_be_last, /* must always be last */ 2339 NULL 2340 }; 2341 2342 ASSERT(dstill->ill_isv6); 2343 ASSERT(!IN6_IS_ADDR_V4MAPPED(dst)); 2344 2345 /* 2346 * Check if there is a usable src address pointed to by the 2347 * usesrc ifindex. This has higher precedence since it is 2348 * finer grained (i.e per interface) v/s being system wide. 2349 */ 2350 if (dstill->ill_usesrc_ifindex != 0) { 2351 if ((usesrc_ill = 2352 ill_lookup_on_ifindex(dstill->ill_usesrc_ifindex, B_TRUE, 2353 NULL, NULL, NULL, NULL, ipst)) != NULL) { 2354 dstinfo.dst_ill = usesrc_ill; 2355 } else { 2356 return (NULL); 2357 } 2358 } else if (IS_UNDER_IPMP(dstill)) { 2359 /* 2360 * Test addresses should never be used for source address 2361 * selection, so if we were passed an underlying ill, switch 2362 * to the IPMP meta-interface. 2363 */ 2364 if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(dstill)) != NULL) 2365 dstinfo.dst_ill = ipmp_ill; 2366 else 2367 return (NULL); 2368 } else { 2369 dstinfo.dst_ill = dstill; 2370 } 2371 2372 /* 2373 * If we're dealing with an unlabeled destination on a labeled system, 2374 * make sure that we ignore source addresses that are incompatible with 2375 * the destination's default label. That destination's default label 2376 * must dominate the minimum label on the source address. 2377 * 2378 * (Note that this has to do with Trusted Solaris. It's not related to 2379 * the labels described by ip6_asp_lookup.) 2380 */ 2381 dst_rhtp = NULL; 2382 if (is_system_labeled()) { 2383 dst_rhtp = find_tpc(dst, IPV6_VERSION, B_FALSE); 2384 if (dst_rhtp == NULL) 2385 return (NULL); 2386 if (dst_rhtp->tpc_tp.host_type != UNLABELED) { 2387 TPC_RELE(dst_rhtp); 2388 dst_rhtp = NULL; 2389 } 2390 } 2391 2392 dstinfo.dst_addr = dst; 2393 dstinfo.dst_scope = ip_addr_scope_v6(dst); 2394 dstinfo.dst_label = ip6_asp_lookup(dst, NULL, ipst); 2395 dstinfo.dst_prefer_src_tmp = ((src_prefs & IPV6_PREFER_SRC_TMP) != 0); 2396 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 2397 /* 2398 * Section three of the I-D states that for multicast and 2399 * link-local destinations, the candidate set must be restricted to 2400 * an interface that is on the same link as the outgoing interface. 2401 * Also, when ipv6_strict_dst_multihoming is turned on, always 2402 * restrict the source address to the destination link as doing 2403 * otherwise will almost certainly cause problems. 2404 */ 2405 if (IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst) || 2406 ipst->ips_ipv6_strict_dst_multihoming || usesrc_ill != NULL) { 2407 dstinfo.dst_restrict_ill = B_TRUE; 2408 } else { 2409 dstinfo.dst_restrict_ill = restrict_ill; 2410 } 2411 2412 bzero(&best_c, sizeof (cand_t)); 2413 2414 /* 2415 * Take a pass through the list of IPv6 interfaces to choose the best 2416 * possible source address. If restrict_ill is set, just use dst_ill. 2417 */ 2418 if (dstinfo.dst_restrict_ill) 2419 ill = dstinfo.dst_ill; 2420 else 2421 ill = ILL_START_WALK_V6(&ctx, ipst); 2422 2423 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 2424 ASSERT(ill->ill_isv6); 2425 2426 /* 2427 * Test addresses should never be used for source address 2428 * selection, so ignore underlying ills. 2429 */ 2430 if (IS_UNDER_IPMP(ill)) 2431 continue; 2432 2433 /* 2434 * For source address selection, we treat the ipif list as 2435 * circular and continue until we get back to where we 2436 * started. This allows IPMP to vary source address selection 2437 * (which improves inbound load spreading) by caching its last 2438 * ending point and starting from there. NOTE: we don't have 2439 * to worry about ill_src_ipif changing ills since that can't 2440 * happen on the IPMP ill. 2441 */ 2442 start_ipif = ill->ill_ipif; 2443 if (IS_IPMP(ill) && ill->ill_src_ipif != NULL) 2444 start_ipif = ill->ill_src_ipif; 2445 2446 ipif = start_ipif; 2447 do { 2448 if ((next_ipif = ipif->ipif_next) == NULL) 2449 next_ipif = ill->ill_ipif; 2450 2451 if (!IPIF_VALID_IPV6_SOURCE(ipif)) 2452 continue; 2453 2454 if (zoneid != ALL_ZONES && 2455 ipif->ipif_zoneid != zoneid && 2456 ipif->ipif_zoneid != ALL_ZONES) 2457 continue; 2458 2459 /* 2460 * Check compatibility of local address for 2461 * destination's default label if we're on a labeled 2462 * system. Incompatible addresses can't be used at 2463 * all and must be skipped over. 2464 */ 2465 if (dst_rhtp != NULL) { 2466 boolean_t incompat; 2467 2468 src_rhtp = find_tpc(&ipif->ipif_v6lcl_addr, 2469 IPV6_VERSION, B_FALSE); 2470 if (src_rhtp == NULL) 2471 continue; 2472 incompat = 2473 src_rhtp->tpc_tp.host_type != SUN_CIPSO || 2474 src_rhtp->tpc_tp.tp_doi != 2475 dst_rhtp->tpc_tp.tp_doi || 2476 (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label, 2477 &src_rhtp->tpc_tp.tp_sl_range_cipso) && 2478 !blinlset(&dst_rhtp->tpc_tp.tp_def_label, 2479 src_rhtp->tpc_tp.tp_sl_set_cipso)); 2480 TPC_RELE(src_rhtp); 2481 if (incompat) 2482 continue; 2483 } 2484 2485 if (first_candidate) { 2486 /* 2487 * This is first valid address in the list. 2488 * It is automatically the best candidate 2489 * so far. 2490 */ 2491 best_c.cand_ipif = ipif; 2492 first_candidate = B_FALSE; 2493 continue; 2494 } 2495 2496 bzero(&curr_c, sizeof (cand_t)); 2497 curr_c.cand_ipif = ipif; 2498 2499 /* 2500 * Compare this current candidate (curr_c) with the 2501 * best candidate (best_c) by applying the 2502 * comparison rules in order until one breaks the 2503 * tie. 2504 */ 2505 for (index = 0; rules[index] != NULL; index++) { 2506 /* Apply a comparison rule. */ 2507 rule_result = (rules[index])(&best_c, &curr_c, 2508 &dstinfo, ipst); 2509 if (rule_result == CAND_AVOID) { 2510 /* 2511 * The best candidate is still the 2512 * best candidate. Forget about 2513 * this current candidate and go on 2514 * to the next one. 2515 */ 2516 break; 2517 } else if (rule_result == CAND_PREFER) { 2518 /* 2519 * This candidate is prefered. It 2520 * becomes the best candidate so 2521 * far. Go on to the next address. 2522 */ 2523 best_c = curr_c; 2524 break; 2525 } 2526 /* We have a tie, apply the next rule. */ 2527 } 2528 2529 /* 2530 * The last rule must be a tie breaker rule and 2531 * must never produce a tie. At this point, the 2532 * candidate should have either been rejected, or 2533 * have been prefered as the best candidate so far. 2534 */ 2535 ASSERT(rule_result != CAND_TIE); 2536 } while ((ipif = next_ipif) != start_ipif); 2537 2538 /* 2539 * For IPMP, update the source ipif rotor to the next ipif, 2540 * provided we can look it up. (We must not use it if it's 2541 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after 2542 * ipif_free() checked ill_src_ipif.) 2543 */ 2544 if (IS_IPMP(ill) && ipif != NULL) { 2545 mutex_enter(&ipif->ipif_ill->ill_lock); 2546 next_ipif = ipif->ipif_next; 2547 if (next_ipif != NULL && IPIF_CAN_LOOKUP(next_ipif)) 2548 ill->ill_src_ipif = next_ipif; 2549 else 2550 ill->ill_src_ipif = NULL; 2551 mutex_exit(&ipif->ipif_ill->ill_lock); 2552 } 2553 2554 /* 2555 * Only one ill to consider if dst_restrict_ill is set. 2556 */ 2557 if (dstinfo.dst_restrict_ill) 2558 break; 2559 } 2560 2561 ipif = best_c.cand_ipif; 2562 ip1dbg(("ipif_select_source_v6(%s, %s) -> %s\n", 2563 dstinfo.dst_ill->ill_name, 2564 inet_ntop(AF_INET6, dstinfo.dst_addr, dstr, sizeof (dstr)), 2565 (ipif == NULL ? "NULL" : 2566 inet_ntop(AF_INET6, &ipif->ipif_v6lcl_addr, sstr, sizeof (sstr))))); 2567 2568 if (usesrc_ill != NULL) 2569 ill_refrele(usesrc_ill); 2570 2571 if (ipmp_ill != NULL) 2572 ill_refrele(ipmp_ill); 2573 2574 if (dst_rhtp != NULL) 2575 TPC_RELE(dst_rhtp); 2576 2577 if (ipif == NULL) { 2578 rw_exit(&ipst->ips_ill_g_lock); 2579 return (NULL); 2580 } 2581 2582 mutex_enter(&ipif->ipif_ill->ill_lock); 2583 if (IPIF_CAN_LOOKUP(ipif)) { 2584 ipif_refhold_locked(ipif); 2585 mutex_exit(&ipif->ipif_ill->ill_lock); 2586 rw_exit(&ipst->ips_ill_g_lock); 2587 return (ipif); 2588 } 2589 mutex_exit(&ipif->ipif_ill->ill_lock); 2590 rw_exit(&ipst->ips_ill_g_lock); 2591 ip1dbg(("ipif_select_source_v6 cannot lookup ipif %p" 2592 " returning null \n", (void *)ipif)); 2593 2594 return (NULL); 2595 } 2596 2597 /* 2598 * If old_ipif is not NULL, see if ipif was derived from old 2599 * ipif and if so, recreate the interface route by re-doing 2600 * source address selection. This happens when ipif_down -> 2601 * ipif_update_other_ipifs calls us. 2602 * 2603 * If old_ipif is NULL, just redo the source address selection 2604 * if needed. This happens when ipif_up_done_v6 calls us. 2605 */ 2606 void 2607 ipif_recreate_interface_routes_v6(ipif_t *old_ipif, ipif_t *ipif) 2608 { 2609 ire_t *ire; 2610 ire_t *ipif_ire; 2611 queue_t *stq; 2612 ill_t *ill; 2613 ipif_t *nipif = NULL; 2614 boolean_t nipif_refheld = B_FALSE; 2615 boolean_t ip6_asp_table_held = B_FALSE; 2616 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst; 2617 2618 ill = ipif->ipif_ill; 2619 2620 if (!(ipif->ipif_flags & 2621 (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) { 2622 /* 2623 * Can't possibly have borrowed the source 2624 * from old_ipif. 2625 */ 2626 return; 2627 } 2628 2629 /* 2630 * Is there any work to be done? No work if the address 2631 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST ( 2632 * ipif_select_source_v6() does not borrow addresses from 2633 * NOLOCAL and ANYCAST interfaces). 2634 */ 2635 if ((old_ipif != NULL) && 2636 ((IN6_IS_ADDR_UNSPECIFIED(&old_ipif->ipif_v6lcl_addr)) || 2637 (old_ipif->ipif_ill->ill_wq == NULL) || 2638 (old_ipif->ipif_flags & 2639 (IPIF_NOLOCAL|IPIF_ANYCAST)))) { 2640 return; 2641 } 2642 2643 /* 2644 * Perform the same checks as when creating the 2645 * IRE_INTERFACE in ipif_up_done_v6. 2646 */ 2647 if (!(ipif->ipif_flags & IPIF_UP)) 2648 return; 2649 2650 if ((ipif->ipif_flags & IPIF_NOXMIT)) 2651 return; 2652 2653 if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet) && 2654 IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask)) 2655 return; 2656 2657 /* 2658 * We know that ipif uses some other source for its 2659 * IRE_INTERFACE. Is it using the source of this 2660 * old_ipif? 2661 */ 2662 ipif_ire = ipif_to_ire_v6(ipif); 2663 if (ipif_ire == NULL) 2664 return; 2665 2666 if (old_ipif != NULL && 2667 !IN6_ARE_ADDR_EQUAL(&old_ipif->ipif_v6lcl_addr, 2668 &ipif_ire->ire_src_addr_v6)) { 2669 ire_refrele(ipif_ire); 2670 return; 2671 } 2672 2673 if (ip_debug > 2) { 2674 /* ip1dbg */ 2675 pr_addr_dbg("ipif_recreate_interface_routes_v6: deleting IRE" 2676 " for src %s\n", AF_INET6, &ipif_ire->ire_src_addr_v6); 2677 } 2678 2679 stq = ipif_ire->ire_stq; 2680 2681 /* 2682 * Can't use our source address. Select a different source address 2683 * for the IRE_INTERFACE. We restrict interface route source 2684 * address selection to ipif's assigned to the same link as the 2685 * interface. 2686 */ 2687 if (ip6_asp_can_lookup(ipst)) { 2688 ip6_asp_table_held = B_TRUE; 2689 nipif = ipif_select_source_v6(ill, &ipif->ipif_v6subnet, 2690 B_TRUE, IPV6_PREFER_SRC_DEFAULT, ipif->ipif_zoneid); 2691 } 2692 if (nipif == NULL) { 2693 /* Last resort - all ipif's have IPIF_NOLOCAL */ 2694 nipif = ipif; 2695 } else { 2696 nipif_refheld = B_TRUE; 2697 } 2698 2699 ire = ire_create_v6( 2700 &ipif->ipif_v6subnet, /* dest pref */ 2701 &ipif->ipif_v6net_mask, /* mask */ 2702 &nipif->ipif_v6src_addr, /* src addr */ 2703 NULL, /* no gateway */ 2704 &ipif->ipif_mtu, /* max frag */ 2705 NULL, /* no src nce */ 2706 NULL, /* no recv from queue */ 2707 stq, /* send-to queue */ 2708 ill->ill_net_type, /* IF_[NO]RESOLVER */ 2709 ipif, 2710 NULL, 2711 0, 2712 0, 2713 0, 2714 &ire_uinfo_null, 2715 NULL, 2716 NULL, 2717 ipst); 2718 2719 if (ire != NULL) { 2720 ire_t *ret_ire; 2721 int error; 2722 2723 /* 2724 * We don't need ipif_ire anymore. We need to delete 2725 * before we add so that ire_add does not detect 2726 * duplicates. 2727 */ 2728 ire_delete(ipif_ire); 2729 ret_ire = ire; 2730 error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE); 2731 ASSERT(error == 0); 2732 ASSERT(ret_ire == ire); 2733 if (ret_ire != NULL) { 2734 /* Held in ire_add */ 2735 ire_refrele(ret_ire); 2736 } 2737 } 2738 /* 2739 * Either we are falling through from above or could not 2740 * allocate a replacement. 2741 */ 2742 ire_refrele(ipif_ire); 2743 if (ip6_asp_table_held) 2744 ip6_asp_table_refrele(ipst); 2745 if (nipif_refheld) 2746 ipif_refrele(nipif); 2747 } 2748 2749 /* 2750 * This old_ipif is going away. 2751 * 2752 * Determine if any other ipif's are using our address as 2753 * ipif_v6lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or 2754 * IPIF_DEPRECATED). 2755 * Find the IRE_INTERFACE for such ipif's and recreate them 2756 * to use an different source address following the rules in 2757 * ipif_up_done_v6. 2758 */ 2759 void 2760 ipif_update_other_ipifs_v6(ipif_t *old_ipif) 2761 { 2762 ipif_t *ipif; 2763 ill_t *ill; 2764 char buf[INET6_ADDRSTRLEN]; 2765 2766 ASSERT(IAM_WRITER_IPIF(old_ipif)); 2767 2768 ill = old_ipif->ipif_ill; 2769 2770 ip1dbg(("ipif_update_other_ipifs_v6(%s, %s)\n", 2771 ill->ill_name, 2772 inet_ntop(AF_INET6, &old_ipif->ipif_v6lcl_addr, 2773 buf, sizeof (buf)))); 2774 2775 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) { 2776 if (ipif != old_ipif) 2777 ipif_recreate_interface_routes_v6(old_ipif, ipif); 2778 } 2779 } 2780 2781 /* 2782 * Perform an attach and bind to get phys addr plus info_req for 2783 * the physical device. 2784 * q and mp represents an ioctl which will be queued waiting for 2785 * completion of the DLPI message exchange. 2786 * MUST be called on an ill queue. Can not set conn_pending_ill for that 2787 * reason thus the DL_PHYS_ADDR_ACK code does not assume ill_pending_q. 2788 * 2789 * Returns EINPROGRESS when mp has been consumed by queueing it on 2790 * ill_pending_mp and the ioctl will complete in ip_rput. 2791 */ 2792 int 2793 ill_dl_phys(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q) 2794 { 2795 mblk_t *v6token_mp = NULL; 2796 mblk_t *v6lla_mp = NULL; 2797 mblk_t *phys_mp = NULL; 2798 mblk_t *info_mp = NULL; 2799 mblk_t *attach_mp = NULL; 2800 mblk_t *bind_mp = NULL; 2801 mblk_t *unbind_mp = NULL; 2802 mblk_t *notify_mp = NULL; 2803 2804 ip1dbg(("ill_dl_phys(%s:%u)\n", ill->ill_name, ipif->ipif_id)); 2805 ASSERT(ill->ill_dlpi_style_set); 2806 ASSERT(WR(q)->q_next != NULL); 2807 2808 if (ill->ill_isv6) { 2809 v6token_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) + 2810 sizeof (t_scalar_t), DL_PHYS_ADDR_REQ); 2811 if (v6token_mp == NULL) 2812 goto bad; 2813 ((dl_phys_addr_req_t *)v6token_mp->b_rptr)->dl_addr_type = 2814 DL_IPV6_TOKEN; 2815 2816 v6lla_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) + 2817 sizeof (t_scalar_t), DL_PHYS_ADDR_REQ); 2818 if (v6lla_mp == NULL) 2819 goto bad; 2820 ((dl_phys_addr_req_t *)v6lla_mp->b_rptr)->dl_addr_type = 2821 DL_IPV6_LINK_LAYER_ADDR; 2822 } 2823 2824 /* 2825 * Allocate a DL_NOTIFY_REQ and set the notifications we want. 2826 */ 2827 notify_mp = ip_dlpi_alloc(sizeof (dl_notify_req_t) + sizeof (long), 2828 DL_NOTIFY_REQ); 2829 if (notify_mp == NULL) 2830 goto bad; 2831 ((dl_notify_req_t *)notify_mp->b_rptr)->dl_notifications = 2832 (DL_NOTE_PHYS_ADDR | DL_NOTE_SDU_SIZE | DL_NOTE_FASTPATH_FLUSH | 2833 DL_NOTE_LINK_UP | DL_NOTE_LINK_DOWN | DL_NOTE_CAPAB_RENEG | 2834 DL_NOTE_REPLUMB); 2835 2836 phys_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) + 2837 sizeof (t_scalar_t), DL_PHYS_ADDR_REQ); 2838 if (phys_mp == NULL) 2839 goto bad; 2840 ((dl_phys_addr_req_t *)phys_mp->b_rptr)->dl_addr_type = 2841 DL_CURR_PHYS_ADDR; 2842 2843 info_mp = ip_dlpi_alloc( 2844 sizeof (dl_info_req_t) + sizeof (dl_info_ack_t), 2845 DL_INFO_REQ); 2846 if (info_mp == NULL) 2847 goto bad; 2848 2849 bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long), 2850 DL_BIND_REQ); 2851 if (bind_mp == NULL) 2852 goto bad; 2853 ((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap; 2854 ((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS; 2855 2856 unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ); 2857 if (unbind_mp == NULL) 2858 goto bad; 2859 2860 /* If we need to attach, pre-alloc and initialize the mblk */ 2861 if (ill->ill_needs_attach) { 2862 attach_mp = ip_dlpi_alloc(sizeof (dl_attach_req_t), 2863 DL_ATTACH_REQ); 2864 if (attach_mp == NULL) 2865 goto bad; 2866 ((dl_attach_req_t *)attach_mp->b_rptr)->dl_ppa = ill->ill_ppa; 2867 } 2868 2869 /* 2870 * Here we are going to delay the ioctl ack until after 2871 * ACKs from DL_PHYS_ADDR_REQ. So need to save the 2872 * original ioctl message before sending the requests 2873 */ 2874 mutex_enter(&ill->ill_lock); 2875 /* ipsq_pending_mp_add won't fail since we pass in a NULL connp */ 2876 (void) ipsq_pending_mp_add(NULL, ipif, ill->ill_wq, mp, 0); 2877 /* 2878 * Set ill_phys_addr_pend to zero. It will be set to the addr_type of 2879 * the DL_PHYS_ADDR_REQ in ill_dlpi_send() and ill_dlpi_done(). It will 2880 * be used to track which DL_PHYS_ADDR_REQ is being ACK'd/NAK'd. 2881 */ 2882 ill->ill_phys_addr_pend = 0; 2883 mutex_exit(&ill->ill_lock); 2884 2885 if (attach_mp != NULL) { 2886 ip1dbg(("ill_dl_phys: attach\n")); 2887 ill_dlpi_send(ill, attach_mp); 2888 } 2889 ill_dlpi_send(ill, bind_mp); 2890 ill_dlpi_send(ill, info_mp); 2891 if (ill->ill_isv6) { 2892 ill_dlpi_send(ill, v6token_mp); 2893 ill_dlpi_send(ill, v6lla_mp); 2894 } 2895 ill_dlpi_send(ill, phys_mp); 2896 ill_dlpi_send(ill, notify_mp); 2897 ill_dlpi_send(ill, unbind_mp); 2898 2899 /* 2900 * This operation will complete in ip_rput_dlpi_writer with either 2901 * a DL_PHYS_ADDR_ACK or DL_ERROR_ACK. 2902 */ 2903 return (EINPROGRESS); 2904 bad: 2905 freemsg(v6token_mp); 2906 freemsg(v6lla_mp); 2907 freemsg(phys_mp); 2908 freemsg(info_mp); 2909 freemsg(attach_mp); 2910 freemsg(bind_mp); 2911 freemsg(unbind_mp); 2912 freemsg(notify_mp); 2913 return (ENOMEM); 2914 } 2915 2916 uint_t ip_loopback_mtu_v6plus = IP_LOOPBACK_MTU + IPV6_HDR_LEN + 20; 2917 2918 /* 2919 * DLPI is up. 2920 * Create all the IREs associated with an interface bring up multicast. 2921 * Set the interface flag and finish other initialization 2922 * that potentially had to be differed to after DL_BIND_ACK. 2923 */ 2924 int 2925 ipif_up_done_v6(ipif_t *ipif) 2926 { 2927 ire_t *ire_array[20]; 2928 ire_t **irep = ire_array; 2929 ire_t **irep1; 2930 ill_t *ill = ipif->ipif_ill; 2931 queue_t *stq; 2932 in6_addr_t v6addr; 2933 in6_addr_t route_mask; 2934 ipif_t *src_ipif = NULL; 2935 ipif_t *tmp_ipif; 2936 boolean_t flush_ire_cache = B_TRUE; 2937 int err; 2938 char buf[INET6_ADDRSTRLEN]; 2939 ire_t **ipif_saved_irep = NULL; 2940 int ipif_saved_ire_cnt; 2941 int cnt; 2942 boolean_t src_ipif_held = B_FALSE; 2943 boolean_t loopback = B_FALSE; 2944 boolean_t ip6_asp_table_held = B_FALSE; 2945 ip_stack_t *ipst = ill->ill_ipst; 2946 2947 ip1dbg(("ipif_up_done_v6(%s:%u)\n", 2948 ipif->ipif_ill->ill_name, ipif->ipif_id)); 2949 2950 /* Check if this is a loopback interface */ 2951 if (ipif->ipif_ill->ill_wq == NULL) 2952 loopback = B_TRUE; 2953 2954 ASSERT(ipif->ipif_isv6); 2955 ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock)); 2956 2957 /* 2958 * If all other interfaces for this ill are down or DEPRECATED, 2959 * or otherwise unsuitable for source address selection, remove 2960 * any IRE_CACHE entries for this ill to make sure source 2961 * address selection gets to take this new ipif into account. 2962 * No need to hold ill_lock while traversing the ipif list since 2963 * we are writer 2964 */ 2965 for (tmp_ipif = ill->ill_ipif; tmp_ipif; 2966 tmp_ipif = tmp_ipif->ipif_next) { 2967 if (((tmp_ipif->ipif_flags & 2968 (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) || 2969 !(tmp_ipif->ipif_flags & IPIF_UP)) || 2970 (tmp_ipif == ipif)) 2971 continue; 2972 /* first useable pre-existing interface */ 2973 flush_ire_cache = B_FALSE; 2974 break; 2975 } 2976 if (flush_ire_cache) 2977 ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE, 2978 IRE_CACHE, ill_ipif_cache_delete, ill, ill); 2979 2980 /* 2981 * Figure out which way the send-to queue should go. Only 2982 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER should show up here. 2983 */ 2984 switch (ill->ill_net_type) { 2985 case IRE_IF_RESOLVER: 2986 stq = ill->ill_rq; 2987 break; 2988 case IRE_IF_NORESOLVER: 2989 case IRE_LOOPBACK: 2990 stq = ill->ill_wq; 2991 break; 2992 default: 2993 return (EINVAL); 2994 } 2995 2996 if (IS_LOOPBACK(ill)) { 2997 /* 2998 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in 2999 * ipif_lookup_on_name(), but in the case of zones we can have 3000 * several loopback addresses on lo0. So all the interfaces with 3001 * loopback addresses need to be marked IRE_LOOPBACK. 3002 */ 3003 if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr, &ipv6_loopback)) 3004 ipif->ipif_ire_type = IRE_LOOPBACK; 3005 else 3006 ipif->ipif_ire_type = IRE_LOCAL; 3007 } 3008 3009 if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST) || 3010 ((ipif->ipif_flags & IPIF_DEPRECATED) && 3011 !(ipif->ipif_flags & IPIF_NOFAILOVER))) { 3012 /* 3013 * Can't use our source address. Select a different 3014 * source address for the IRE_INTERFACE and IRE_LOCAL 3015 */ 3016 if (ip6_asp_can_lookup(ipst)) { 3017 ip6_asp_table_held = B_TRUE; 3018 src_ipif = ipif_select_source_v6(ipif->ipif_ill, 3019 &ipif->ipif_v6subnet, B_FALSE, 3020 IPV6_PREFER_SRC_DEFAULT, ipif->ipif_zoneid); 3021 } 3022 if (src_ipif == NULL) 3023 src_ipif = ipif; /* Last resort */ 3024 else 3025 src_ipif_held = B_TRUE; 3026 } else { 3027 src_ipif = ipif; 3028 } 3029 3030 if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) && 3031 !(ipif->ipif_flags & IPIF_NOLOCAL)) { 3032 3033 /* 3034 * If we're on a labeled system then make sure that zone- 3035 * private addresses have proper remote host database entries. 3036 */ 3037 if (is_system_labeled() && 3038 ipif->ipif_ire_type != IRE_LOOPBACK) { 3039 if (ip6opt_ls == 0) { 3040 cmn_err(CE_WARN, "IPv6 not enabled " 3041 "via /etc/system"); 3042 return (EINVAL); 3043 } 3044 if (!tsol_check_interface_address(ipif)) 3045 return (EINVAL); 3046 } 3047 3048 /* Register the source address for __sin6_src_id */ 3049 err = ip_srcid_insert(&ipif->ipif_v6lcl_addr, 3050 ipif->ipif_zoneid, ipst); 3051 if (err != 0) { 3052 ip0dbg(("ipif_up_done_v6: srcid_insert %d\n", err)); 3053 if (src_ipif_held) 3054 ipif_refrele(src_ipif); 3055 if (ip6_asp_table_held) 3056 ip6_asp_table_refrele(ipst); 3057 return (err); 3058 } 3059 /* 3060 * If the interface address is set, create the LOCAL 3061 * or LOOPBACK IRE. 3062 */ 3063 ip1dbg(("ipif_up_done_v6: creating IRE %d for %s\n", 3064 ipif->ipif_ire_type, 3065 inet_ntop(AF_INET6, &ipif->ipif_v6lcl_addr, 3066 buf, sizeof (buf)))); 3067 3068 *irep++ = ire_create_v6( 3069 &ipif->ipif_v6lcl_addr, /* dest address */ 3070 &ipv6_all_ones, /* mask */ 3071 &src_ipif->ipif_v6src_addr, /* source address */ 3072 NULL, /* no gateway */ 3073 &ip_loopback_mtu_v6plus, /* max frag size */ 3074 NULL, 3075 ipif->ipif_rq, /* recv-from queue */ 3076 NULL, /* no send-to queue */ 3077 ipif->ipif_ire_type, /* LOCAL or LOOPBACK */ 3078 ipif, /* interface */ 3079 NULL, 3080 0, 3081 0, 3082 (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0, 3083 &ire_uinfo_null, 3084 NULL, 3085 NULL, 3086 ipst); 3087 } 3088 3089 /* 3090 * Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. 3091 * Note that atun interfaces have an all-zero ipif_v6subnet. 3092 * Thus we allow a zero subnet as long as the mask is non-zero. 3093 */ 3094 if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) && 3095 !(IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet) && 3096 IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))) { 3097 /* ipif_v6subnet is ipif_v6pp_dst_addr for pt-pt */ 3098 v6addr = ipif->ipif_v6subnet; 3099 3100 if (ipif->ipif_flags & IPIF_POINTOPOINT) { 3101 route_mask = ipv6_all_ones; 3102 } else { 3103 route_mask = ipif->ipif_v6net_mask; 3104 } 3105 3106 ip1dbg(("ipif_up_done_v6: creating if IRE %d for %s\n", 3107 ill->ill_net_type, 3108 inet_ntop(AF_INET6, &v6addr, buf, sizeof (buf)))); 3109 3110 *irep++ = ire_create_v6( 3111 &v6addr, /* dest pref */ 3112 &route_mask, /* mask */ 3113 &src_ipif->ipif_v6src_addr, /* src addr */ 3114 NULL, /* no gateway */ 3115 &ipif->ipif_mtu, /* max frag */ 3116 NULL, /* no src nce */ 3117 NULL, /* no recv from queue */ 3118 stq, /* send-to queue */ 3119 ill->ill_net_type, /* IF_[NO]RESOLVER */ 3120 ipif, 3121 NULL, 3122 0, 3123 0, 3124 (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0, 3125 &ire_uinfo_null, 3126 NULL, 3127 NULL, 3128 ipst); 3129 } 3130 3131 /* 3132 * Setup 2002::/16 route, if this interface is a 6to4 tunnel 3133 */ 3134 if (IN6_IS_ADDR_6TO4(&ipif->ipif_v6lcl_addr) && 3135 (ill->ill_is_6to4tun)) { 3136 /* 3137 * Destination address is 2002::/16 3138 */ 3139 #ifdef _BIG_ENDIAN 3140 const in6_addr_t prefix_addr = { 0x20020000U, 0, 0, 0 }; 3141 const in6_addr_t prefix_mask = { 0xffff0000U, 0, 0, 0 }; 3142 #else 3143 const in6_addr_t prefix_addr = { 0x00000220U, 0, 0, 0 }; 3144 const in6_addr_t prefix_mask = { 0x0000ffffU, 0, 0, 0 }; 3145 #endif /* _BIG_ENDIAN */ 3146 char buf2[INET6_ADDRSTRLEN]; 3147 ire_t *isdup; 3148 in6_addr_t *first_addr = &ill->ill_ipif->ipif_v6lcl_addr; 3149 3150 /* 3151 * check to see if this route has already been added for 3152 * this tunnel interface. 3153 */ 3154 isdup = ire_ftable_lookup_v6(first_addr, &prefix_mask, 0, 3155 IRE_IF_NORESOLVER, ill->ill_ipif, NULL, ALL_ZONES, 0, NULL, 3156 (MATCH_IRE_SRC | MATCH_IRE_MASK), ipst); 3157 3158 if (isdup == NULL) { 3159 ip1dbg(("ipif_up_done_v6: creating if IRE %d for %s", 3160 IRE_IF_NORESOLVER, inet_ntop(AF_INET6, &v6addr, 3161 buf2, sizeof (buf2)))); 3162 3163 *irep++ = ire_create_v6( 3164 &prefix_addr, /* 2002:: */ 3165 &prefix_mask, /* ffff:: */ 3166 &ipif->ipif_v6lcl_addr, /* src addr */ 3167 NULL, /* gateway */ 3168 &ipif->ipif_mtu, /* max_frag */ 3169 NULL, /* no src nce */ 3170 NULL, /* no rfq */ 3171 ill->ill_wq, /* stq */ 3172 IRE_IF_NORESOLVER, /* type */ 3173 ipif, /* interface */ 3174 NULL, /* v6cmask */ 3175 0, 3176 0, 3177 RTF_UP, 3178 &ire_uinfo_null, 3179 NULL, 3180 NULL, 3181 ipst); 3182 } else { 3183 ire_refrele(isdup); 3184 } 3185 } 3186 3187 /* If an earlier ire_create failed, get out now */ 3188 for (irep1 = irep; irep1 > ire_array; ) { 3189 irep1--; 3190 if (*irep1 == NULL) { 3191 ip1dbg(("ipif_up_done_v6: NULL ire found in" 3192 " ire_array\n")); 3193 err = ENOMEM; 3194 goto bad; 3195 } 3196 } 3197 3198 ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock)); 3199 3200 /* 3201 * Need to atomically check for IP address availability under 3202 * ip_addr_avail_lock. ill_g_lock is held as reader to ensure no new 3203 * ills or new ipifs can be added while we are checking availability. 3204 */ 3205 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 3206 mutex_enter(&ipst->ips_ip_addr_avail_lock); 3207 ill->ill_ipif_up_count++; 3208 ipif->ipif_flags |= IPIF_UP; 3209 err = ip_addr_availability_check(ipif); 3210 mutex_exit(&ipst->ips_ip_addr_avail_lock); 3211 rw_exit(&ipst->ips_ill_g_lock); 3212 3213 if (err != 0) { 3214 /* 3215 * Our address may already be up on the same ill. In this case, 3216 * the external resolver entry for our ipif replaced the one for 3217 * the other ipif. So we don't want to delete it (otherwise the 3218 * other ipif would be unable to send packets). 3219 * ip_addr_availability_check() identifies this case for us and 3220 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL 3221 * which is the expected error code. 3222 * 3223 * Note that, for the non-XRESOLV case, ipif_ndp_down() will 3224 * only delete an nce in the case when one was actually created 3225 * by ipif_ndp_up(), as indicated by the ipif_added_nce bit. 3226 */ 3227 if (err == EADDRINUSE) { 3228 if (ipif->ipif_ill->ill_flags & ILLF_XRESOLV) { 3229 freemsg(ipif->ipif_arp_del_mp); 3230 ipif->ipif_arp_del_mp = NULL; 3231 } 3232 err = EADDRNOTAVAIL; 3233 } 3234 ill->ill_ipif_up_count--; 3235 ipif->ipif_flags &= ~IPIF_UP; 3236 goto bad; 3237 } 3238 3239 /* 3240 * Add in all newly created IREs. 3241 * 3242 * NOTE : We refrele the ire though we may branch to "bad" 3243 * later on where we do ire_delete. This is okay 3244 * because nobody can delete it as we are running 3245 * exclusively. 3246 */ 3247 for (irep1 = irep; irep1 > ire_array; ) { 3248 irep1--; 3249 /* Shouldn't be adding any bcast ire's */ 3250 ASSERT((*irep1)->ire_type != IRE_BROADCAST); 3251 ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock)); 3252 /* 3253 * refheld by ire_add. refele towards the end of the func 3254 */ 3255 (void) ire_add(irep1, NULL, NULL, NULL, B_FALSE); 3256 } 3257 if (ip6_asp_table_held) { 3258 ip6_asp_table_refrele(ipst); 3259 ip6_asp_table_held = B_FALSE; 3260 } 3261 3262 /* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */ 3263 ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt; 3264 ipif_saved_irep = ipif_recover_ire_v6(ipif); 3265 3266 if (ill->ill_need_recover_multicast) { 3267 /* 3268 * Need to recover all multicast memberships in the driver. 3269 * This had to be deferred until we had attached. 3270 */ 3271 ill_recover_multicast(ill); 3272 } 3273 3274 if (ill->ill_ipif_up_count == 1) { 3275 /* 3276 * Since the interface is now up, it may now be active. 3277 */ 3278 if (IS_UNDER_IPMP(ill)) 3279 ipmp_ill_refresh_active(ill); 3280 } 3281 3282 /* Join the allhosts multicast address and the solicited node MC */ 3283 ipif_multicast_up(ipif); 3284 3285 /* 3286 * See if anybody else would benefit from our new ipif. 3287 */ 3288 if (!loopback && 3289 !(ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) { 3290 ill_update_source_selection(ill); 3291 } 3292 3293 for (irep1 = irep; irep1 > ire_array; ) { 3294 irep1--; 3295 if (*irep1 != NULL) { 3296 /* was held in ire_add */ 3297 ire_refrele(*irep1); 3298 } 3299 } 3300 3301 cnt = ipif_saved_ire_cnt; 3302 for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) { 3303 if (*irep1 != NULL) { 3304 /* was held in ire_add */ 3305 ire_refrele(*irep1); 3306 } 3307 } 3308 3309 if (ipif->ipif_addr_ready) 3310 ipif_up_notify(ipif); 3311 3312 if (ipif_saved_irep != NULL) { 3313 kmem_free(ipif_saved_irep, 3314 ipif_saved_ire_cnt * sizeof (ire_t *)); 3315 } 3316 3317 if (src_ipif_held) 3318 ipif_refrele(src_ipif); 3319 3320 return (0); 3321 3322 bad: 3323 if (ip6_asp_table_held) 3324 ip6_asp_table_refrele(ipst); 3325 3326 while (irep > ire_array) { 3327 irep--; 3328 if (*irep != NULL) 3329 ire_delete(*irep); 3330 } 3331 (void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst); 3332 3333 if (ipif_saved_irep != NULL) { 3334 kmem_free(ipif_saved_irep, 3335 ipif_saved_ire_cnt * sizeof (ire_t *)); 3336 } 3337 if (src_ipif_held) 3338 ipif_refrele(src_ipif); 3339 3340 ipif_ndp_down(ipif); 3341 ipif_resolver_down(ipif); 3342 3343 return (err); 3344 } 3345 3346 /* 3347 * Delete an ND entry and the corresponding IRE_CACHE entry if it exists. 3348 */ 3349 /* ARGSUSED */ 3350 int 3351 ip_siocdelndp_v6(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp, 3352 ip_ioctl_cmd_t *ipip, void *dummy_ifreq) 3353 { 3354 sin6_t *sin6; 3355 nce_t *nce; 3356 struct lifreq *lifr; 3357 lif_nd_req_t *lnr; 3358 ill_t *ill = ipif->ipif_ill; 3359 ire_t *ire; 3360 3361 lifr = (struct lifreq *)mp->b_cont->b_cont->b_rptr; 3362 lnr = &lifr->lifr_nd; 3363 /* Only allow for logical unit zero i.e. not on "le0:17" */ 3364 if (ipif->ipif_id != 0) 3365 return (EINVAL); 3366 3367 if (!ipif->ipif_isv6) 3368 return (EINVAL); 3369 3370 if (lnr->lnr_addr.ss_family != AF_INET6) 3371 return (EAFNOSUPPORT); 3372 3373 sin6 = (sin6_t *)&lnr->lnr_addr; 3374 3375 /* 3376 * Since ND mappings must be consistent across an IPMP group, prohibit 3377 * deleting ND mappings on underlying interfaces. Also, since ND 3378 * mappings for IPMP data addresses are owned by IP itself, prohibit 3379 * deleting them. 3380 */ 3381 if (IS_UNDER_IPMP(ill)) 3382 return (EPERM); 3383 3384 if (IS_IPMP(ill)) { 3385 ire = ire_ctable_lookup_v6(&sin6->sin6_addr, NULL, IRE_LOCAL, 3386 ipif, ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, 3387 ill->ill_ipst); 3388 if (ire != NULL) { 3389 ire_refrele(ire); 3390 return (EPERM); 3391 } 3392 } 3393 3394 /* See comment in ndp_query() regarding IS_IPMP(ill) usage */ 3395 nce = ndp_lookup_v6(ill, IS_IPMP(ill), &sin6->sin6_addr, B_FALSE); 3396 if (nce == NULL) 3397 return (ESRCH); 3398 ndp_delete(nce); 3399 NCE_REFRELE(nce); 3400 return (0); 3401 } 3402 3403 /* 3404 * Return nbr cache info. 3405 */ 3406 /* ARGSUSED */ 3407 int 3408 ip_siocqueryndp_v6(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp, 3409 ip_ioctl_cmd_t *ipip, void *dummy_ifreq) 3410 { 3411 ill_t *ill = ipif->ipif_ill; 3412 struct lifreq *lifr; 3413 lif_nd_req_t *lnr; 3414 3415 lifr = (struct lifreq *)mp->b_cont->b_cont->b_rptr; 3416 lnr = &lifr->lifr_nd; 3417 /* Only allow for logical unit zero i.e. not on "le0:17" */ 3418 if (ipif->ipif_id != 0) 3419 return (EINVAL); 3420 3421 if (!ipif->ipif_isv6) 3422 return (EINVAL); 3423 3424 if (lnr->lnr_addr.ss_family != AF_INET6) 3425 return (EAFNOSUPPORT); 3426 3427 if (ill->ill_phys_addr_length > sizeof (lnr->lnr_hdw_addr)) 3428 return (EINVAL); 3429 3430 return (ndp_query(ill, lnr)); 3431 } 3432 3433 /* 3434 * Perform an update of the nd entry for the specified address. 3435 */ 3436 /* ARGSUSED */ 3437 int 3438 ip_siocsetndp_v6(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp, 3439 ip_ioctl_cmd_t *ipip, void *dummy_ifreq) 3440 { 3441 sin6_t *sin6; 3442 ill_t *ill = ipif->ipif_ill; 3443 struct lifreq *lifr; 3444 lif_nd_req_t *lnr; 3445 ire_t *ire; 3446 3447 lifr = (struct lifreq *)mp->b_cont->b_cont->b_rptr; 3448 lnr = &lifr->lifr_nd; 3449 /* Only allow for logical unit zero i.e. not on "le0:17" */ 3450 if (ipif->ipif_id != 0) 3451 return (EINVAL); 3452 3453 if (!ipif->ipif_isv6) 3454 return (EINVAL); 3455 3456 if (lnr->lnr_addr.ss_family != AF_INET6) 3457 return (EAFNOSUPPORT); 3458 3459 sin6 = (sin6_t *)&lnr->lnr_addr; 3460 3461 /* 3462 * Since ND mappings must be consistent across an IPMP group, prohibit 3463 * updating ND mappings on underlying interfaces. Also, since ND 3464 * mappings for IPMP data addresses are owned by IP itself, prohibit 3465 * updating them. 3466 */ 3467 if (IS_UNDER_IPMP(ill)) 3468 return (EPERM); 3469 3470 if (IS_IPMP(ill)) { 3471 ire = ire_ctable_lookup_v6(&sin6->sin6_addr, NULL, IRE_LOCAL, 3472 ipif, ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, 3473 ill->ill_ipst); 3474 if (ire != NULL) { 3475 ire_refrele(ire); 3476 return (EPERM); 3477 } 3478 } 3479 3480 return (ndp_sioc_update(ill, lnr)); 3481 } 3482