| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package ipv6 |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
"time" |
| 20 |
|
| 21 |
"inet.af/netstack/sync" |
| 22 |
"inet.af/netstack/tcpip" |
| 23 |
"inet.af/netstack/tcpip/buffer" |
| 24 |
"inet.af/netstack/tcpip/header" |
| 25 |
"inet.af/netstack/tcpip/network/internal/ip" |
| 26 |
"inet.af/netstack/tcpip/stack" |
| 27 |
) |
| 28 |
|
| 29 |
const ( |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
defaultMaxRtrSolicitations = 3 |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
defaultRtrSolicitationInterval = 4 * time.Second |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
defaultMaxRtrSolicitationDelay = time.Second |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
defaultHandleRAs = HandlingRAsEnabledWhenForwardingDisabled |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
defaultDiscoverDefaultRouters = true |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
defaultDiscoverMoreSpecificRoutes = true |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
defaultDiscoverOnLinkPrefixes = true |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
defaultAutoGenGlobalAddresses = true |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
minimumRtrSolicitationInterval = 500 * time.Millisecond |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
minimumMaxRtrSolicitationDelay = 0 |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
MaxDiscoveredOffLinkRoutes = 10 |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
MaxDiscoveredOnLinkPrefixes = 10 |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
validPrefixLenForAutoGen = 64 |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
defaultAutoGenTempGlobalAddresses = true |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
defaultMaxTempAddrValidLifetime = 7 * 24 * time.Hour |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
defaultMaxTempAddrPreferredLifetime = 24 * time.Hour |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
defaultRegenAdvanceDuration = 5 * time.Second |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
minRegenAdvanceDuration = time.Duration(0) |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
maxSLAACAddrLocalRegenAttempts = 10 |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
MinPrefixInformationValidLifetimeForUpdate = 2 * time.Hour |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
MaxDesyncFactor = 10 * time.Minute |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
MinMaxTempAddrPreferredLifetime = defaultRegenAdvanceDuration + MaxDesyncFactor + time.Hour |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
MinMaxTempAddrValidLifetime = 2 * time.Hour |
| 164 |
) |
| 165 |
|
| 166 |
|
| 167 |
type NDPEndpoint interface { |
| 168 |
|
| 169 |
SetNDPConfigurations(NDPConfigurations) |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
type DHCPv6ConfigurationFromNDPRA int |
| 175 |
|
| 176 |
const ( |
| 177 |
_ DHCPv6ConfigurationFromNDPRA = iota |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
DHCPv6NoConfiguration |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
DHCPv6ManagedAddress |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
DHCPv6OtherConfigurations |
| 196 |
) |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
type NDPDispatcher interface { |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
OnDuplicateAddressDetectionResult(tcpip.NICID, tcpip.Address, stack.DADResult) |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
OnOffLinkRouteUpdated(tcpip.NICID, tcpip.Subnet, tcpip.Address, header.NDPRoutePreference) |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
OnOffLinkRouteInvalidated(tcpip.NICID, tcpip.Subnet, tcpip.Address) |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
OnOnLinkPrefixDiscovered(tcpip.NICID, tcpip.Subnet) |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
OnOnLinkPrefixInvalidated(tcpip.NICID, tcpip.Subnet) |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
OnAutoGenAddress(tcpip.NICID, tcpip.AddressWithPrefix) |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
OnAutoGenAddressDeprecated(tcpip.NICID, tcpip.AddressWithPrefix) |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
OnAutoGenAddressInvalidated(tcpip.NICID, tcpip.AddressWithPrefix) |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
OnRecursiveDNSServerOption(tcpip.NICID, []tcpip.Address, time.Duration) |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
OnDNSSearchListOption(tcpip.NICID, []string, time.Duration) |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
OnDHCPv6Configuration(tcpip.NICID, DHCPv6ConfigurationFromNDPRA) |
| 285 |
} |
| 286 |
|
| 287 |
var _ fmt.Stringer = HandleRAsConfiguration(0) |
| 288 |
|
| 289 |
|
| 290 |
type HandleRAsConfiguration int |
| 291 |
|
| 292 |
const ( |
| 293 |
|
| 294 |
|
| 295 |
HandlingRAsDisabled HandleRAsConfiguration = iota |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
HandlingRAsEnabledWhenForwardingDisabled |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
HandlingRAsAlwaysEnabled |
| 304 |
) |
| 305 |
|
| 306 |
|
| 307 |
func (c HandleRAsConfiguration) String() string { |
| 308 |
switch c { |
| 309 |
case HandlingRAsDisabled: |
| 310 |
return "HandlingRAsDisabled" |
| 311 |
case HandlingRAsEnabledWhenForwardingDisabled: |
| 312 |
return "HandlingRAsEnabledWhenForwardingDisabled" |
| 313 |
case HandlingRAsAlwaysEnabled: |
| 314 |
return "HandlingRAsAlwaysEnabled" |
| 315 |
default: |
| 316 |
return fmt.Sprintf("HandleRAsConfiguration(%d)", c) |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
func (c HandleRAsConfiguration) enabled(forwarding bool) bool { |
| 323 |
switch c { |
| 324 |
case HandlingRAsDisabled: |
| 325 |
return false |
| 326 |
case HandlingRAsEnabledWhenForwardingDisabled: |
| 327 |
return !forwarding |
| 328 |
case HandlingRAsAlwaysEnabled: |
| 329 |
return true |
| 330 |
default: |
| 331 |
panic(fmt.Sprintf("unhandled HandleRAsConfiguration = %d", c)) |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
type NDPConfigurations struct { |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
MaxRtrSolicitations uint8 |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
RtrSolicitationInterval time.Duration |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
MaxRtrSolicitationDelay time.Duration |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
HandleRAs HandleRAsConfiguration |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
DiscoverDefaultRouters bool |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
DiscoverMoreSpecificRoutes bool |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
DiscoverOnLinkPrefixes bool |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
AutoGenGlobalAddresses bool |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
AutoGenAddressConflictRetries uint8 |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
AutoGenTempGlobalAddresses bool |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
MaxTempAddrValidLifetime time.Duration |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
MaxTempAddrPreferredLifetime time.Duration |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
RegenAdvanceDuration time.Duration |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
func DefaultNDPConfigurations() NDPConfigurations { |
| 416 |
return NDPConfigurations{ |
| 417 |
MaxRtrSolicitations: defaultMaxRtrSolicitations, |
| 418 |
RtrSolicitationInterval: defaultRtrSolicitationInterval, |
| 419 |
MaxRtrSolicitationDelay: defaultMaxRtrSolicitationDelay, |
| 420 |
HandleRAs: defaultHandleRAs, |
| 421 |
DiscoverDefaultRouters: defaultDiscoverDefaultRouters, |
| 422 |
DiscoverMoreSpecificRoutes: defaultDiscoverMoreSpecificRoutes, |
| 423 |
DiscoverOnLinkPrefixes: defaultDiscoverOnLinkPrefixes, |
| 424 |
AutoGenGlobalAddresses: defaultAutoGenGlobalAddresses, |
| 425 |
AutoGenTempGlobalAddresses: defaultAutoGenTempGlobalAddresses, |
| 426 |
MaxTempAddrValidLifetime: defaultMaxTempAddrValidLifetime, |
| 427 |
MaxTempAddrPreferredLifetime: defaultMaxTempAddrPreferredLifetime, |
| 428 |
RegenAdvanceDuration: defaultRegenAdvanceDuration, |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
func (c *NDPConfigurations) validate() { |
| 435 |
if c.RtrSolicitationInterval < minimumRtrSolicitationInterval { |
| 436 |
c.RtrSolicitationInterval = defaultRtrSolicitationInterval |
| 437 |
} |
| 438 |
|
| 439 |
if c.MaxRtrSolicitationDelay < minimumMaxRtrSolicitationDelay { |
| 440 |
c.MaxRtrSolicitationDelay = defaultMaxRtrSolicitationDelay |
| 441 |
} |
| 442 |
|
| 443 |
if c.MaxTempAddrValidLifetime < MinMaxTempAddrValidLifetime { |
| 444 |
c.MaxTempAddrValidLifetime = MinMaxTempAddrValidLifetime |
| 445 |
} |
| 446 |
|
| 447 |
if c.MaxTempAddrPreferredLifetime < MinMaxTempAddrPreferredLifetime || c.MaxTempAddrPreferredLifetime > c.MaxTempAddrValidLifetime { |
| 448 |
c.MaxTempAddrPreferredLifetime = MinMaxTempAddrPreferredLifetime |
| 449 |
} |
| 450 |
|
| 451 |
if c.RegenAdvanceDuration < minRegenAdvanceDuration { |
| 452 |
c.RegenAdvanceDuration = minRegenAdvanceDuration |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
type timer struct { |
| 457 |
|
| 458 |
done *bool |
| 459 |
|
| 460 |
timer tcpip.Timer |
| 461 |
} |
| 462 |
|
| 463 |
type offLinkRoute struct { |
| 464 |
dest tcpip.Subnet |
| 465 |
router tcpip.Address |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
type ndpState struct { |
| 470 |
|
| 471 |
_ sync.NoCopy |
| 472 |
|
| 473 |
|
| 474 |
ep *endpoint |
| 475 |
|
| 476 |
|
| 477 |
configs NDPConfigurations |
| 478 |
|
| 479 |
|
| 480 |
dad ip.DAD |
| 481 |
|
| 482 |
|
| 483 |
offLinkRoutes map[offLinkRoute]offLinkRouteState |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
rtrSolicitTimer timer |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
onLinkPrefixes map[tcpip.Subnet]onLinkPrefixState |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
slaacPrefixes map[tcpip.Subnet]slaacPrefixState |
| 498 |
|
| 499 |
|
| 500 |
dhcpv6Configuration DHCPv6ConfigurationFromNDPRA |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
temporaryIIDHistory [header.IIDSize]byte |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
temporaryAddressDesyncFactor time.Duration |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
type offLinkRouteState struct { |
| 514 |
prf header.NDPRoutePreference |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
invalidationJob *tcpip.Job |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
type onLinkPrefixState struct { |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
invalidationJob *tcpip.Job |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
type tempSLAACAddrState struct { |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
deprecationJob *tcpip.Job |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
invalidationJob *tcpip.Job |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
regenJob *tcpip.Job |
| 548 |
|
| 549 |
createdAt tcpip.MonotonicTime |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
addressEndpoint stack.AddressEndpoint |
| 555 |
|
| 556 |
|
| 557 |
regenerated bool |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
type slaacPrefixState struct { |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
deprecationJob *tcpip.Job |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
invalidationJob *tcpip.Job |
| 571 |
|
| 572 |
|
| 573 |
validUntil *tcpip.MonotonicTime |
| 574 |
|
| 575 |
|
| 576 |
preferredUntil *tcpip.MonotonicTime |
| 577 |
|
| 578 |
|
| 579 |
stableAddr struct { |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
addressEndpoint stack.AddressEndpoint |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
localGenerationFailures uint8 |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
tempAddrs map[tcpip.Address]tempSLAACAddrState |
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
generationAttempts uint8 |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
maxGenerationAttempts uint8 |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
func (ndp *ndpState) startDuplicateAddressDetection(addr tcpip.Address, addressEndpoint stack.AddressEndpoint) tcpip.Error { |
| 617 |
|
| 618 |
if !header.IsV6UnicastAddress(addr) { |
| 619 |
return &tcpip.ErrAddressFamilyNotSupported{} |
| 620 |
} |
| 621 |
|
| 622 |
if addressEndpoint.GetKind() != stack.PermanentTentative { |
| 623 |
|
| 624 |
panic(fmt.Sprintf("ndpdad: addr %s is not tentative on NIC(%d)", addr, ndp.ep.nic.ID())) |
| 625 |
} |
| 626 |
|
| 627 |
ret := ndp.dad.CheckDuplicateAddressLocked(addr, func(r stack.DADResult) { |
| 628 |
if addressEndpoint.GetKind() != stack.PermanentTentative { |
| 629 |
|
| 630 |
|
| 631 |
panic(fmt.Sprintf("ndpdad: addr %s is no longer tentative on NIC(%d)", addr, ndp.ep.nic.ID())) |
| 632 |
} |
| 633 |
|
| 634 |
var dadSucceeded bool |
| 635 |
switch r.(type) { |
| 636 |
case *stack.DADAborted, *stack.DADError, *stack.DADDupAddrDetected: |
| 637 |
dadSucceeded = false |
| 638 |
case *stack.DADSucceeded: |
| 639 |
dadSucceeded = true |
| 640 |
default: |
| 641 |
panic(fmt.Sprintf("unrecognized DAD result = %T", r)) |
| 642 |
} |
| 643 |
|
| 644 |
if dadSucceeded { |
| 645 |
addressEndpoint.SetKind(stack.Permanent) |
| 646 |
} |
| 647 |
|
| 648 |
if ndpDisp := ndp.ep.protocol.options.NDPDisp; ndpDisp != nil { |
| 649 |
ndpDisp.OnDuplicateAddressDetectionResult(ndp.ep.nic.ID(), addr, r) |
| 650 |
} |
| 651 |
|
| 652 |
if dadSucceeded { |
| 653 |
if addressEndpoint.ConfigType() == stack.AddressConfigSlaac { |
| 654 |
|
| 655 |
|
| 656 |
ndp.regenerateTempSLAACAddr(addressEndpoint.AddressWithPrefix().Subnet(), true ) |
| 657 |
} |
| 658 |
|
| 659 |
ndp.ep.onAddressAssignedLocked(addr) |
| 660 |
} |
| 661 |
}) |
| 662 |
|
| 663 |
switch ret { |
| 664 |
case stack.DADStarting: |
| 665 |
case stack.DADAlreadyRunning: |
| 666 |
panic(fmt.Sprintf("ndpdad: already performing DAD for addr %s on NIC(%d)", addr, ndp.ep.nic.ID())) |
| 667 |
case stack.DADDisabled: |
| 668 |
addressEndpoint.SetKind(stack.Permanent) |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
if ndpDisp := ndp.ep.protocol.options.NDPDisp; ndpDisp != nil { |
| 673 |
ndpDisp.OnDuplicateAddressDetectionResult(ndp.ep.nic.ID(), addr, &stack.DADSucceeded{}) |
| 674 |
} |
| 675 |
|
| 676 |
ndp.ep.onAddressAssignedLocked(addr) |
| 677 |
} |
| 678 |
|
| 679 |
return nil |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
func (ndp *ndpState) stopDuplicateAddressDetection(addr tcpip.Address, reason stack.DADResult) { |
| 691 |
ndp.dad.StopLocked(addr, reason) |
| 692 |
} |
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
func (ndp *ndpState) handleRA(ip tcpip.Address, ra header.NDPRouterAdvert) { |
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
if !ndp.configs.HandleRAs.enabled(ndp.ep.Forwarding()) { |
| 706 |
ndp.ep.stats.localStats.UnhandledRouterAdvertisements.Increment() |
| 707 |
return |
| 708 |
} |
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
if ndpDisp := ndp.ep.protocol.options.NDPDisp; ndpDisp != nil { |
| 714 |
var configuration DHCPv6ConfigurationFromNDPRA |
| 715 |
switch { |
| 716 |
case ra.ManagedAddrConfFlag(): |
| 717 |
configuration = DHCPv6ManagedAddress |
| 718 |
|
| 719 |
case ra.OtherConfFlag(): |
| 720 |
configuration = DHCPv6OtherConfigurations |
| 721 |
|
| 722 |
default: |
| 723 |
configuration = DHCPv6NoConfiguration |
| 724 |
} |
| 725 |
|
| 726 |
if ndp.dhcpv6Configuration != configuration { |
| 727 |
ndp.dhcpv6Configuration = configuration |
| 728 |
ndpDisp.OnDHCPv6Configuration(ndp.ep.nic.ID(), configuration) |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
|
| 733 |
if ndp.configs.DiscoverDefaultRouters { |
| 734 |
prf := ra.DefaultRouterPreference() |
| 735 |
if prf == header.ReservedRoutePreference { |
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
prf = header.MediumRoutePreference |
| 745 |
} |
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
ndp.handleOffLinkRouteDiscovery(offLinkRoute{dest: header.IPv6EmptySubnet, router: ip}, ra.RouterLifetime(), prf) |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
it, _ := ra.Options().Iter(false) |
| 759 |
for opt, done, _ := it.Next(); !done; opt, done, _ = it.Next() { |
| 760 |
switch opt := opt.(type) { |
| 761 |
case header.NDPRecursiveDNSServer: |
| 762 |
if ndp.ep.protocol.options.NDPDisp == nil { |
| 763 |
continue |
| 764 |
} |
| 765 |
|
| 766 |
addrs, _ := opt.Addresses() |
| 767 |
ndp.ep.protocol.options.NDPDisp.OnRecursiveDNSServerOption(ndp.ep.nic.ID(), addrs, opt.Lifetime()) |
| 768 |
|
| 769 |
case header.NDPDNSSearchList: |
| 770 |
if ndp.ep.protocol.options.NDPDisp == nil { |
| 771 |
continue |
| 772 |
} |
| 773 |
|
| 774 |
domainNames, _ := opt.DomainNames() |
| 775 |
ndp.ep.protocol.options.NDPDisp.OnDNSSearchListOption(ndp.ep.nic.ID(), domainNames, opt.Lifetime()) |
| 776 |
|
| 777 |
case header.NDPPrefixInformation: |
| 778 |
prefix := opt.Subnet() |
| 779 |
|
| 780 |
|
| 781 |
if header.IsV6LinkLocalUnicastAddress(prefix.ID()) { |
| 782 |
|
| 783 |
|
| 784 |
continue |
| 785 |
} |
| 786 |
|
| 787 |
|
| 788 |
if prefix.Prefix() == 0 { |
| 789 |
|
| 790 |
|
| 791 |
continue |
| 792 |
} |
| 793 |
|
| 794 |
if opt.OnLinkFlag() { |
| 795 |
ndp.handleOnLinkPrefixInformation(opt) |
| 796 |
} |
| 797 |
|
| 798 |
if opt.AutonomousAddressConfigurationFlag() { |
| 799 |
ndp.handleAutonomousPrefixInformation(opt) |
| 800 |
} |
| 801 |
|
| 802 |
case header.NDPRouteInformation: |
| 803 |
if !ndp.configs.DiscoverMoreSpecificRoutes { |
| 804 |
continue |
| 805 |
} |
| 806 |
|
| 807 |
dest, err := opt.Prefix() |
| 808 |
if err != nil { |
| 809 |
panic(fmt.Sprintf("%T.Prefix(): %s", opt, err)) |
| 810 |
} |
| 811 |
|
| 812 |
prf := opt.RoutePreference() |
| 813 |
if prf == header.ReservedRoutePreference { |
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
continue |
| 824 |
} |
| 825 |
|
| 826 |
ndp.handleOffLinkRouteDiscovery(offLinkRoute{dest: dest, router: ip}, opt.RouteLifetime(), prf) |
| 827 |
} |
| 828 |
|
| 829 |
|
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
func (ndp *ndpState) invalidateOffLinkRoute(route offLinkRoute) { |
| 837 |
state, ok := ndp.offLinkRoutes[route] |
| 838 |
if !ok { |
| 839 |
return |
| 840 |
} |
| 841 |
|
| 842 |
state.invalidationJob.Cancel() |
| 843 |
delete(ndp.offLinkRoutes, route) |
| 844 |
|
| 845 |
|
| 846 |
if ndpDisp := ndp.ep.protocol.options.NDPDisp; ndpDisp != nil { |
| 847 |
ndpDisp.OnOffLinkRouteInvalidated(ndp.ep.nic.ID(), route.dest, route.router) |
| 848 |
} |
| 849 |
} |
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
func (ndp *ndpState) handleOffLinkRouteDiscovery(route offLinkRoute, lifetime time.Duration, prf header.NDPRoutePreference) { |
| 855 |
ndpDisp := ndp.ep.protocol.options.NDPDisp |
| 856 |
if ndpDisp == nil { |
| 857 |
return |
| 858 |
} |
| 859 |
|
| 860 |
state, ok := ndp.offLinkRoutes[route] |
| 861 |
switch { |
| 862 |
case !ok && lifetime != 0: |
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
if len(ndp.offLinkRoutes) < MaxDiscoveredOffLinkRoutes { |
| 868 |
|
| 869 |
ndpDisp.OnOffLinkRouteUpdated(ndp.ep.nic.ID(), route.dest, route.router, prf) |
| 870 |
|
| 871 |
state := offLinkRouteState{ |
| 872 |
prf: prf, |
| 873 |
invalidationJob: tcpip.NewJob(ndp.ep.protocol.stack.Clock(), &ndp.ep.mu, func() { |
| 874 |
ndp.invalidateOffLinkRoute(route) |
| 875 |
}), |
| 876 |
} |
| 877 |
|
| 878 |
state.invalidationJob.Schedule(lifetime) |
| 879 |
|
| 880 |
ndp.offLinkRoutes[route] = state |
| 881 |
} |
| 882 |
|
| 883 |
case ok && lifetime != 0: |
| 884 |
|
| 885 |
state.invalidationJob.Cancel() |
| 886 |
state.invalidationJob.Schedule(lifetime) |
| 887 |
|
| 888 |
if prf != state.prf { |
| 889 |
state.prf = prf |
| 890 |
|
| 891 |
|
| 892 |
ndpDisp.OnOffLinkRouteUpdated(ndp.ep.nic.ID(), route.dest, route.router, prf) |
| 893 |
} |
| 894 |
|
| 895 |
ndp.offLinkRoutes[route] = state |
| 896 |
|
| 897 |
case ok && lifetime == 0: |
| 898 |
|
| 899 |
|
| 900 |
ndp.invalidateOffLinkRoute(route) |
| 901 |
} |
| 902 |
} |
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
func (ndp *ndpState) rememberOnLinkPrefix(prefix tcpip.Subnet, l time.Duration) { |
| 911 |
ndpDisp := ndp.ep.protocol.options.NDPDisp |
| 912 |
if ndpDisp == nil { |
| 913 |
return |
| 914 |
} |
| 915 |
|
| 916 |
|
| 917 |
ndpDisp.OnOnLinkPrefixDiscovered(ndp.ep.nic.ID(), prefix) |
| 918 |
|
| 919 |
state := onLinkPrefixState{ |
| 920 |
invalidationJob: tcpip.NewJob(ndp.ep.protocol.stack.Clock(), &ndp.ep.mu, func() { |
| 921 |
ndp.invalidateOnLinkPrefix(prefix) |
| 922 |
}), |
| 923 |
} |
| 924 |
|
| 925 |
if l < header.NDPInfiniteLifetime { |
| 926 |
state.invalidationJob.Schedule(l) |
| 927 |
} |
| 928 |
|
| 929 |
ndp.onLinkPrefixes[prefix] = state |
| 930 |
} |
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
func (ndp *ndpState) invalidateOnLinkPrefix(prefix tcpip.Subnet) { |
| 936 |
s, ok := ndp.onLinkPrefixes[prefix] |
| 937 |
|
| 938 |
|
| 939 |
if !ok { |
| 940 |
|
| 941 |
return |
| 942 |
} |
| 943 |
|
| 944 |
s.invalidationJob.Cancel() |
| 945 |
delete(ndp.onLinkPrefixes, prefix) |
| 946 |
|
| 947 |
|
| 948 |
if ndpDisp := ndp.ep.protocol.options.NDPDisp; ndpDisp != nil { |
| 949 |
ndpDisp.OnOnLinkPrefixInvalidated(ndp.ep.nic.ID(), prefix) |
| 950 |
} |
| 951 |
} |
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
func (ndp *ndpState) handleOnLinkPrefixInformation(pi header.NDPPrefixInformation) { |
| 961 |
prefix := pi.Subnet() |
| 962 |
prefixState, ok := ndp.onLinkPrefixes[prefix] |
| 963 |
vl := pi.ValidLifetime() |
| 964 |
|
| 965 |
if !ok && vl == 0 { |
| 966 |
|
| 967 |
|
| 968 |
return |
| 969 |
} |
| 970 |
|
| 971 |
if !ok && vl != 0 { |
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
if ndp.configs.DiscoverOnLinkPrefixes && len(ndp.onLinkPrefixes) < MaxDiscoveredOnLinkPrefixes { |
| 977 |
ndp.rememberOnLinkPrefix(prefix, vl) |
| 978 |
} |
| 979 |
return |
| 980 |
} |
| 981 |
|
| 982 |
if ok && vl == 0 { |
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
ndp.invalidateOnLinkPrefix(prefix) |
| 987 |
return |
| 988 |
} |
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
prefixState.invalidationJob.Cancel() |
| 996 |
|
| 997 |
if vl < header.NDPInfiniteLifetime { |
| 998 |
|
| 999 |
|
| 1000 |
prefixState.invalidationJob.Schedule(vl) |
| 1001 |
} |
| 1002 |
|
| 1003 |
ndp.onLinkPrefixes[prefix] = prefixState |
| 1004 |
} |
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
func (ndp *ndpState) handleAutonomousPrefixInformation(pi header.NDPPrefixInformation) { |
| 1014 |
vl := pi.ValidLifetime() |
| 1015 |
pl := pi.PreferredLifetime() |
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
if pl > vl { |
| 1021 |
return |
| 1022 |
} |
| 1023 |
|
| 1024 |
prefix := pi.Subnet() |
| 1025 |
|
| 1026 |
|
| 1027 |
if state, ok := ndp.slaacPrefixes[prefix]; ok { |
| 1028 |
|
| 1029 |
ndp.refreshSLAACPrefixLifetimes(prefix, &state, pl, vl) |
| 1030 |
ndp.slaacPrefixes[prefix] = state |
| 1031 |
return |
| 1032 |
} |
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
if !ndp.configs.AutoGenGlobalAddresses { |
| 1037 |
return |
| 1038 |
} |
| 1039 |
|
| 1040 |
ndp.doSLAAC(prefix, pl, vl) |
| 1041 |
} |
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
func (ndp *ndpState) doSLAAC(prefix tcpip.Subnet, pl, vl time.Duration) { |
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
if vl == 0 { |
| 1054 |
return |
| 1055 |
} |
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
if prefix.Prefix() != validPrefixLenForAutoGen { |
| 1061 |
return |
| 1062 |
} |
| 1063 |
|
| 1064 |
state := slaacPrefixState{ |
| 1065 |
deprecationJob: tcpip.NewJob(ndp.ep.protocol.stack.Clock(), &ndp.ep.mu, func() { |
| 1066 |
state, ok := ndp.slaacPrefixes[prefix] |
| 1067 |
if !ok { |
| 1068 |
panic(fmt.Sprintf("ndp: must have a slaacPrefixes entry for the deprecated SLAAC prefix %s", prefix)) |
| 1069 |
} |
| 1070 |
|
| 1071 |
ndp.deprecateSLAACAddress(state.stableAddr.addressEndpoint) |
| 1072 |
}), |
| 1073 |
invalidationJob: tcpip.NewJob(ndp.ep.protocol.stack.Clock(), &ndp.ep.mu, func() { |
| 1074 |
state, ok := ndp.slaacPrefixes[prefix] |
| 1075 |
if !ok { |
| 1076 |
panic(fmt.Sprintf("ndp: must have a slaacPrefixes entry for the invalidated SLAAC prefix %s", prefix)) |
| 1077 |
} |
| 1078 |
|
| 1079 |
ndp.invalidateSLAACPrefix(prefix, state) |
| 1080 |
}), |
| 1081 |
tempAddrs: make(map[tcpip.Address]tempSLAACAddrState), |
| 1082 |
maxGenerationAttempts: ndp.configs.AutoGenAddressConflictRetries + 1, |
| 1083 |
} |
| 1084 |
|
| 1085 |
now := ndp.ep.protocol.stack.Clock().NowMonotonic() |
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
if pl < header.NDPInfiniteLifetime { |
| 1090 |
t := now.Add(pl) |
| 1091 |
state.preferredUntil = &t |
| 1092 |
} |
| 1093 |
|
| 1094 |
if !ndp.generateSLAACAddr(prefix, &state) { |
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
return |
| 1099 |
} |
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
if pl < header.NDPInfiniteLifetime && pl != 0 { |
| 1104 |
state.deprecationJob.Schedule(pl) |
| 1105 |
} |
| 1106 |
|
| 1107 |
if vl < header.NDPInfiniteLifetime { |
| 1108 |
state.invalidationJob.Schedule(vl) |
| 1109 |
t := now.Add(vl) |
| 1110 |
state.validUntil = &t |
| 1111 |
} |
| 1112 |
|
| 1113 |
|
| 1114 |
if state.stableAddr.addressEndpoint.GetKind() == stack.Permanent { |
| 1115 |
|
| 1116 |
|
| 1117 |
ndp.generateTempSLAACAddr(prefix, &state, true ) |
| 1118 |
} |
| 1119 |
|
| 1120 |
ndp.slaacPrefixes[prefix] = state |
| 1121 |
} |
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
func (ndp *ndpState) addAndAcquireSLAACAddr(addr tcpip.AddressWithPrefix, configType stack.AddressConfigType, deprecated bool) stack.AddressEndpoint { |
| 1127 |
|
| 1128 |
ndpDisp := ndp.ep.protocol.options.NDPDisp |
| 1129 |
if ndpDisp == nil { |
| 1130 |
return nil |
| 1131 |
} |
| 1132 |
|
| 1133 |
addressEndpoint, err := ndp.ep.addAndAcquirePermanentAddressLocked(addr, stack.AddressProperties{ |
| 1134 |
PEB: stack.FirstPrimaryEndpoint, |
| 1135 |
ConfigType: configType, |
| 1136 |
Deprecated: deprecated, |
| 1137 |
}) |
| 1138 |
if err != nil { |
| 1139 |
panic(fmt.Sprintf("ndp: error when adding SLAAC address %+v: %s", addr, err)) |
| 1140 |
} |
| 1141 |
|
| 1142 |
ndpDisp.OnAutoGenAddress(ndp.ep.nic.ID(), addr) |
| 1143 |
|
| 1144 |
return addressEndpoint |
| 1145 |
} |
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
func (ndp *ndpState) generateSLAACAddr(prefix tcpip.Subnet, state *slaacPrefixState) bool { |
| 1155 |
if addressEndpoint := state.stableAddr.addressEndpoint; addressEndpoint != nil { |
| 1156 |
panic(fmt.Sprintf("ndp: SLAAC prefix %s already has a permenant address %s", prefix, addressEndpoint.AddressWithPrefix())) |
| 1157 |
} |
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
if state.generationAttempts == state.maxGenerationAttempts { |
| 1162 |
return false |
| 1163 |
} |
| 1164 |
|
| 1165 |
var generatedAddr tcpip.AddressWithPrefix |
| 1166 |
addrBytes := []byte(prefix.ID()) |
| 1167 |
|
| 1168 |
for i := 0; ; i++ { |
| 1169 |
|
| 1170 |
|
| 1171 |
if i == maxSLAACAddrLocalRegenAttempts { |
| 1172 |
return false |
| 1173 |
} |
| 1174 |
|
| 1175 |
dadCounter := state.generationAttempts + state.stableAddr.localGenerationFailures |
| 1176 |
if oIID := ndp.ep.protocol.options.OpaqueIIDOpts; oIID.NICNameFromID != nil { |
| 1177 |
addrBytes = header.AppendOpaqueInterfaceIdentifier( |
| 1178 |
addrBytes[:header.IIDOffsetInIPv6Address], |
| 1179 |
prefix, |
| 1180 |
oIID.NICNameFromID(ndp.ep.nic.ID(), ndp.ep.nic.Name()), |
| 1181 |
dadCounter, |
| 1182 |
oIID.SecretKey, |
| 1183 |
) |
| 1184 |
} else if dadCounter == 0 { |
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
linkAddr := ndp.ep.nic.LinkAddress() |
| 1194 |
if !header.IsValidUnicastEthernetAddress(linkAddr) { |
| 1195 |
return false |
| 1196 |
} |
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
header.EthernetAdddressToModifiedEUI64IntoBuf(linkAddr, addrBytes[header.IIDOffsetInIPv6Address:]) |
| 1201 |
} else { |
| 1202 |
|
| 1203 |
|
| 1204 |
return false |
| 1205 |
} |
| 1206 |
|
| 1207 |
generatedAddr = tcpip.AddressWithPrefix{ |
| 1208 |
Address: tcpip.Address(addrBytes), |
| 1209 |
PrefixLen: validPrefixLenForAutoGen, |
| 1210 |
} |
| 1211 |
|
| 1212 |
if !ndp.ep.hasPermanentAddressRLocked(generatedAddr.Address) { |
| 1213 |
break |
| 1214 |
} |
| 1215 |
|
| 1216 |
state.stableAddr.localGenerationFailures++ |
| 1217 |
} |
| 1218 |
|
| 1219 |
deprecated := state.preferredUntil != nil && !state.preferredUntil.After(ndp.ep.protocol.stack.Clock().NowMonotonic()) |
| 1220 |
if addressEndpoint := ndp.addAndAcquireSLAACAddr(generatedAddr, stack.AddressConfigSlaac, deprecated); addressEndpoint != nil { |
| 1221 |
state.stableAddr.addressEndpoint = addressEndpoint |
| 1222 |
state.generationAttempts++ |
| 1223 |
return true |
| 1224 |
} |
| 1225 |
|
| 1226 |
return false |
| 1227 |
} |
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
func (ndp *ndpState) regenerateSLAACAddr(prefix tcpip.Subnet) { |
| 1235 |
state, ok := ndp.slaacPrefixes[prefix] |
| 1236 |
if !ok { |
| 1237 |
panic(fmt.Sprintf("ndp: SLAAC prefix state not found to regenerate address for %s", prefix)) |
| 1238 |
} |
| 1239 |
|
| 1240 |
if ndp.generateSLAACAddr(prefix, &state) { |
| 1241 |
ndp.slaacPrefixes[prefix] = state |
| 1242 |
return |
| 1243 |
} |
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
ndp.invalidateSLAACPrefix(prefix, state) |
| 1249 |
} |
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
func (ndp *ndpState) generateTempSLAACAddr(prefix tcpip.Subnet, prefixState *slaacPrefixState, resetGenAttempts bool) bool { |
| 1257 |
|
| 1258 |
|
| 1259 |
if !ndp.configs.AutoGenTempGlobalAddresses || prefix == header.IPv6LinkLocalPrefix.Subnet() { |
| 1260 |
return false |
| 1261 |
} |
| 1262 |
|
| 1263 |
if resetGenAttempts { |
| 1264 |
prefixState.generationAttempts = 0 |
| 1265 |
prefixState.maxGenerationAttempts = ndp.configs.AutoGenAddressConflictRetries + 1 |
| 1266 |
} |
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
if prefixState.generationAttempts == prefixState.maxGenerationAttempts { |
| 1271 |
return false |
| 1272 |
} |
| 1273 |
|
| 1274 |
stableAddr := prefixState.stableAddr.addressEndpoint.AddressWithPrefix().Address |
| 1275 |
now := ndp.ep.protocol.stack.Clock().NowMonotonic() |
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
vl := ndp.configs.MaxTempAddrValidLifetime |
| 1281 |
if prefixState.validUntil != nil { |
| 1282 |
if prefixVL := prefixState.validUntil.Sub(now); vl > prefixVL { |
| 1283 |
vl = prefixVL |
| 1284 |
} |
| 1285 |
} |
| 1286 |
|
| 1287 |
if vl <= 0 { |
| 1288 |
|
| 1289 |
return false |
| 1290 |
} |
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
pl := ndp.configs.MaxTempAddrPreferredLifetime - ndp.temporaryAddressDesyncFactor |
| 1297 |
if prefixState.preferredUntil != nil { |
| 1298 |
if prefixPL := prefixState.preferredUntil.Sub(now); pl > prefixPL { |
| 1299 |
|
| 1300 |
|
| 1301 |
pl = prefixPL |
| 1302 |
} |
| 1303 |
} |
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
if pl <= ndp.configs.RegenAdvanceDuration { |
| 1310 |
return false |
| 1311 |
} |
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
var generatedAddr tcpip.AddressWithPrefix |
| 1316 |
for i := 0; ; i++ { |
| 1317 |
|
| 1318 |
|
| 1319 |
if i == maxSLAACAddrLocalRegenAttempts { |
| 1320 |
return false |
| 1321 |
} |
| 1322 |
|
| 1323 |
generatedAddr = header.GenerateTempIPv6SLAACAddr(ndp.temporaryIIDHistory[:], stableAddr) |
| 1324 |
if !ndp.ep.hasPermanentAddressRLocked(generatedAddr.Address) { |
| 1325 |
break |
| 1326 |
} |
| 1327 |
} |
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
addressEndpoint := ndp.addAndAcquireSLAACAddr(generatedAddr, stack.AddressConfigSlaacTemp, false ) |
| 1333 |
if addressEndpoint == nil { |
| 1334 |
return false |
| 1335 |
} |
| 1336 |
|
| 1337 |
state := tempSLAACAddrState{ |
| 1338 |
deprecationJob: tcpip.NewJob(ndp.ep.protocol.stack.Clock(), &ndp.ep.mu, func() { |
| 1339 |
prefixState, ok := ndp.slaacPrefixes[prefix] |
| 1340 |
if !ok { |
| 1341 |
panic(fmt.Sprintf("ndp: must have a slaacPrefixes entry for %s to deprecate temporary address %s", prefix, generatedAddr)) |
| 1342 |
} |
| 1343 |
|
| 1344 |
tempAddrState, ok := prefixState.tempAddrs[generatedAddr.Address] |
| 1345 |
if !ok { |
| 1346 |
panic(fmt.Sprintf("ndp: must have a tempAddr entry to deprecate temporary address %s", generatedAddr)) |
| 1347 |
} |
| 1348 |
|
| 1349 |
ndp.deprecateSLAACAddress(tempAddrState.addressEndpoint) |
| 1350 |
}), |
| 1351 |
invalidationJob: tcpip.NewJob(ndp.ep.protocol.stack.Clock(), &ndp.ep.mu, func() { |
| 1352 |
prefixState, ok := ndp.slaacPrefixes[prefix] |
| 1353 |
if !ok { |
| 1354 |
panic(fmt.Sprintf("ndp: must have a slaacPrefixes entry for %s to invalidate temporary address %s", prefix, generatedAddr)) |
| 1355 |
} |
| 1356 |
|
| 1357 |
tempAddrState, ok := prefixState.tempAddrs[generatedAddr.Address] |
| 1358 |
if !ok { |
| 1359 |
panic(fmt.Sprintf("ndp: must have a tempAddr entry to invalidate temporary address %s", generatedAddr)) |
| 1360 |
} |
| 1361 |
|
| 1362 |
ndp.invalidateTempSLAACAddr(prefixState.tempAddrs, generatedAddr.Address, tempAddrState) |
| 1363 |
}), |
| 1364 |
regenJob: tcpip.NewJob(ndp.ep.protocol.stack.Clock(), &ndp.ep.mu, func() { |
| 1365 |
prefixState, ok := ndp.slaacPrefixes[prefix] |
| 1366 |
if !ok { |
| 1367 |
panic(fmt.Sprintf("ndp: must have a slaacPrefixes entry for %s to regenerate temporary address after %s", prefix, generatedAddr)) |
| 1368 |
} |
| 1369 |
|
| 1370 |
tempAddrState, ok := prefixState.tempAddrs[generatedAddr.Address] |
| 1371 |
if !ok { |
| 1372 |
panic(fmt.Sprintf("ndp: must have a tempAddr entry to regenerate temporary address after %s", generatedAddr)) |
| 1373 |
} |
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
if tempAddrState.regenerated { |
| 1378 |
return |
| 1379 |
} |
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
tempAddrState.regenerated = ndp.generateTempSLAACAddr(prefix, &prefixState, true ) |
| 1384 |
prefixState.tempAddrs[generatedAddr.Address] = tempAddrState |
| 1385 |
ndp.slaacPrefixes[prefix] = prefixState |
| 1386 |
}), |
| 1387 |
createdAt: now, |
| 1388 |
addressEndpoint: addressEndpoint, |
| 1389 |
} |
| 1390 |
|
| 1391 |
state.deprecationJob.Schedule(pl) |
| 1392 |
state.invalidationJob.Schedule(vl) |
| 1393 |
state.regenJob.Schedule(pl - ndp.configs.RegenAdvanceDuration) |
| 1394 |
|
| 1395 |
prefixState.generationAttempts++ |
| 1396 |
prefixState.tempAddrs[generatedAddr.Address] = state |
| 1397 |
|
| 1398 |
return true |
| 1399 |
} |
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
func (ndp *ndpState) regenerateTempSLAACAddr(prefix tcpip.Subnet, resetGenAttempts bool) { |
| 1405 |
state, ok := ndp.slaacPrefixes[prefix] |
| 1406 |
if !ok { |
| 1407 |
panic(fmt.Sprintf("ndp: SLAAC prefix state not found to regenerate temporary address for %s", prefix)) |
| 1408 |
} |
| 1409 |
|
| 1410 |
ndp.generateTempSLAACAddr(prefix, &state, resetGenAttempts) |
| 1411 |
ndp.slaacPrefixes[prefix] = state |
| 1412 |
} |
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
func (ndp *ndpState) refreshSLAACPrefixLifetimes(prefix tcpip.Subnet, prefixState *slaacPrefixState, pl, vl time.Duration) { |
| 1420 |
|
| 1421 |
deprecated := pl == 0 |
| 1422 |
if deprecated { |
| 1423 |
ndp.deprecateSLAACAddress(prefixState.stableAddr.addressEndpoint) |
| 1424 |
} else { |
| 1425 |
prefixState.stableAddr.addressEndpoint.SetDeprecated(false) |
| 1426 |
} |
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
prefixState.deprecationJob.Cancel() |
| 1431 |
|
| 1432 |
now := ndp.ep.protocol.stack.Clock().NowMonotonic() |
| 1433 |
|
| 1434 |
|
| 1435 |
if pl < header.NDPInfiniteLifetime { |
| 1436 |
if !deprecated { |
| 1437 |
prefixState.deprecationJob.Schedule(pl) |
| 1438 |
} |
| 1439 |
t := now.Add(pl) |
| 1440 |
prefixState.preferredUntil = &t |
| 1441 |
} else { |
| 1442 |
prefixState.preferredUntil = nil |
| 1443 |
} |
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
if vl >= header.NDPInfiniteLifetime { |
| 1457 |
|
| 1458 |
|
| 1459 |
prefixState.invalidationJob.Cancel() |
| 1460 |
prefixState.validUntil = nil |
| 1461 |
} else { |
| 1462 |
var effectiveVl time.Duration |
| 1463 |
var rl time.Duration |
| 1464 |
|
| 1465 |
|
| 1466 |
|
| 1467 |
if prefixState.validUntil == nil { |
| 1468 |
rl = header.NDPInfiniteLifetime |
| 1469 |
} else { |
| 1470 |
rl = prefixState.validUntil.Sub(now) |
| 1471 |
} |
| 1472 |
|
| 1473 |
if vl > MinPrefixInformationValidLifetimeForUpdate || vl > rl { |
| 1474 |
effectiveVl = vl |
| 1475 |
} else if rl > MinPrefixInformationValidLifetimeForUpdate { |
| 1476 |
effectiveVl = MinPrefixInformationValidLifetimeForUpdate |
| 1477 |
} |
| 1478 |
|
| 1479 |
if effectiveVl != 0 { |
| 1480 |
prefixState.invalidationJob.Cancel() |
| 1481 |
prefixState.invalidationJob.Schedule(effectiveVl) |
| 1482 |
t := now.Add(effectiveVl) |
| 1483 |
prefixState.validUntil = &t |
| 1484 |
} |
| 1485 |
} |
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
if prefixState.stableAddr.addressEndpoint.GetKind() != stack.Permanent { |
| 1490 |
return |
| 1491 |
} |
| 1492 |
|
| 1493 |
|
| 1494 |
|
| 1495 |
var regenForAddr tcpip.Address |
| 1496 |
allAddressesRegenerated := true |
| 1497 |
for tempAddr, tempAddrState := range prefixState.tempAddrs { |
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
validUntil := tempAddrState.createdAt.Add(ndp.configs.MaxTempAddrValidLifetime) |
| 1503 |
if prefixState.validUntil != nil && prefixState.validUntil.Before(validUntil) { |
| 1504 |
validUntil = *prefixState.validUntil |
| 1505 |
} |
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
newValidLifetime := validUntil.Sub(now) |
| 1510 |
if newValidLifetime <= 0 { |
| 1511 |
ndp.invalidateTempSLAACAddr(prefixState.tempAddrs, tempAddr, tempAddrState) |
| 1512 |
continue |
| 1513 |
} |
| 1514 |
tempAddrState.invalidationJob.Cancel() |
| 1515 |
tempAddrState.invalidationJob.Schedule(newValidLifetime) |
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
|
| 1521 |
|
| 1522 |
preferredUntil := tempAddrState.createdAt.Add(ndp.configs.MaxTempAddrPreferredLifetime - ndp.temporaryAddressDesyncFactor) |
| 1523 |
if prefixState.preferredUntil != nil && prefixState.preferredUntil.Before(preferredUntil) { |
| 1524 |
preferredUntil = *prefixState.preferredUntil |
| 1525 |
} |
| 1526 |
|
| 1527 |
|
| 1528 |
|
| 1529 |
newPreferredLifetime := preferredUntil.Sub(now) |
| 1530 |
tempAddrState.deprecationJob.Cancel() |
| 1531 |
|
| 1532 |
if newPreferredLifetime <= 0 { |
| 1533 |
ndp.deprecateSLAACAddress(tempAddrState.addressEndpoint) |
| 1534 |
} else { |
| 1535 |
tempAddrState.addressEndpoint.SetDeprecated(false) |
| 1536 |
tempAddrState.deprecationJob.Schedule(newPreferredLifetime) |
| 1537 |
} |
| 1538 |
|
| 1539 |
tempAddrState.regenJob.Cancel() |
| 1540 |
if tempAddrState.regenerated { |
| 1541 |
} else { |
| 1542 |
allAddressesRegenerated = false |
| 1543 |
|
| 1544 |
if newPreferredLifetime <= ndp.configs.RegenAdvanceDuration { |
| 1545 |
|
| 1546 |
|
| 1547 |
|
| 1548 |
regenForAddr = tempAddr |
| 1549 |
} else { |
| 1550 |
tempAddrState.regenJob.Schedule(newPreferredLifetime - ndp.configs.RegenAdvanceDuration) |
| 1551 |
} |
| 1552 |
} |
| 1553 |
} |
| 1554 |
|
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
|
| 1560 |
|
| 1561 |
|
| 1562 |
if len(regenForAddr) != 0 || allAddressesRegenerated { |
| 1563 |
|
| 1564 |
|
| 1565 |
if state, ok := prefixState.tempAddrs[regenForAddr]; ndp.generateTempSLAACAddr(prefix, prefixState, true ) && ok { |
| 1566 |
state.regenerated = true |
| 1567 |
prefixState.tempAddrs[regenForAddr] = state |
| 1568 |
} |
| 1569 |
} |
| 1570 |
} |
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
|
| 1575 |
|
| 1576 |
|
| 1577 |
|
| 1578 |
func (ndp *ndpState) deprecateSLAACAddress(addressEndpoint stack.AddressEndpoint) { |
| 1579 |
if addressEndpoint.Deprecated() { |
| 1580 |
return |
| 1581 |
} |
| 1582 |
|
| 1583 |
addressEndpoint.SetDeprecated(true) |
| 1584 |
if ndpDisp := ndp.ep.protocol.options.NDPDisp; ndpDisp != nil { |
| 1585 |
ndpDisp.OnAutoGenAddressDeprecated(ndp.ep.nic.ID(), addressEndpoint.AddressWithPrefix()) |
| 1586 |
} |
| 1587 |
} |
| 1588 |
|
| 1589 |
|
| 1590 |
|
| 1591 |
|
| 1592 |
func (ndp *ndpState) invalidateSLAACPrefix(prefix tcpip.Subnet, state slaacPrefixState) { |
| 1593 |
ndp.cleanupSLAACPrefixResources(prefix, state) |
| 1594 |
|
| 1595 |
if addressEndpoint := state.stableAddr.addressEndpoint; addressEndpoint != nil { |
| 1596 |
if ndpDisp := ndp.ep.protocol.options.NDPDisp; ndpDisp != nil { |
| 1597 |
ndpDisp.OnAutoGenAddressInvalidated(ndp.ep.nic.ID(), addressEndpoint.AddressWithPrefix()) |
| 1598 |
} |
| 1599 |
|
| 1600 |
if err := ndp.ep.removePermanentEndpointInnerLocked(addressEndpoint, &stack.DADAborted{}); err != nil { |
| 1601 |
panic(fmt.Sprintf("ndp: error removing stable SLAAC address %s: %s", addressEndpoint.AddressWithPrefix(), err)) |
| 1602 |
} |
| 1603 |
} |
| 1604 |
} |
| 1605 |
|
| 1606 |
|
| 1607 |
|
| 1608 |
|
| 1609 |
|
| 1610 |
func (ndp *ndpState) cleanupSLAACAddrResourcesAndNotify(addr tcpip.AddressWithPrefix, invalidatePrefix bool) { |
| 1611 |
if ndpDisp := ndp.ep.protocol.options.NDPDisp; ndpDisp != nil { |
| 1612 |
ndpDisp.OnAutoGenAddressInvalidated(ndp.ep.nic.ID(), addr) |
| 1613 |
} |
| 1614 |
|
| 1615 |
prefix := addr.Subnet() |
| 1616 |
state, ok := ndp.slaacPrefixes[prefix] |
| 1617 |
if !ok || state.stableAddr.addressEndpoint == nil || addr.Address != state.stableAddr.addressEndpoint.AddressWithPrefix().Address { |
| 1618 |
return |
| 1619 |
} |
| 1620 |
|
| 1621 |
if !invalidatePrefix { |
| 1622 |
|
| 1623 |
|
| 1624 |
state.stableAddr.addressEndpoint.DecRef() |
| 1625 |
state.stableAddr.addressEndpoint = nil |
| 1626 |
ndp.slaacPrefixes[prefix] = state |
| 1627 |
return |
| 1628 |
} |
| 1629 |
|
| 1630 |
ndp.cleanupSLAACPrefixResources(prefix, state) |
| 1631 |
} |
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
|
| 1638 |
func (ndp *ndpState) cleanupSLAACPrefixResources(prefix tcpip.Subnet, state slaacPrefixState) { |
| 1639 |
|
| 1640 |
for tempAddr, tempAddrState := range state.tempAddrs { |
| 1641 |
ndp.invalidateTempSLAACAddr(state.tempAddrs, tempAddr, tempAddrState) |
| 1642 |
} |
| 1643 |
|
| 1644 |
if state.stableAddr.addressEndpoint != nil { |
| 1645 |
state.stableAddr.addressEndpoint.DecRef() |
| 1646 |
state.stableAddr.addressEndpoint = nil |
| 1647 |
} |
| 1648 |
state.deprecationJob.Cancel() |
| 1649 |
state.invalidationJob.Cancel() |
| 1650 |
delete(ndp.slaacPrefixes, prefix) |
| 1651 |
} |
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 1655 |
|
| 1656 |
func (ndp *ndpState) invalidateTempSLAACAddr(tempAddrs map[tcpip.Address]tempSLAACAddrState, tempAddr tcpip.Address, tempAddrState tempSLAACAddrState) { |
| 1657 |
ndp.cleanupTempSLAACAddrResourcesAndNotifyInner(tempAddrs, tempAddr, tempAddrState) |
| 1658 |
|
| 1659 |
if err := ndp.ep.removePermanentEndpointInnerLocked(tempAddrState.addressEndpoint, &stack.DADAborted{}); err != nil { |
| 1660 |
panic(fmt.Sprintf("error removing temporary SLAAC address %s: %s", tempAddrState.addressEndpoint.AddressWithPrefix(), err)) |
| 1661 |
} |
| 1662 |
} |
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
|
| 1669 |
func (ndp *ndpState) cleanupTempSLAACAddrResourcesAndNotify(addr tcpip.AddressWithPrefix) { |
| 1670 |
prefix := addr.Subnet() |
| 1671 |
state, ok := ndp.slaacPrefixes[prefix] |
| 1672 |
if !ok { |
| 1673 |
panic(fmt.Sprintf("ndp: must have a slaacPrefixes entry to clean up temp addr %s resources", addr)) |
| 1674 |
} |
| 1675 |
|
| 1676 |
tempAddrState, ok := state.tempAddrs[addr.Address] |
| 1677 |
if !ok { |
| 1678 |
panic(fmt.Sprintf("ndp: must have a tempAddr entry to clean up temp addr %s resources", addr)) |
| 1679 |
} |
| 1680 |
|
| 1681 |
ndp.cleanupTempSLAACAddrResourcesAndNotifyInner(state.tempAddrs, addr.Address, tempAddrState) |
| 1682 |
} |
| 1683 |
|
| 1684 |
|
| 1685 |
|
| 1686 |
|
| 1687 |
|
| 1688 |
|
| 1689 |
func (ndp *ndpState) cleanupTempSLAACAddrResourcesAndNotifyInner(tempAddrs map[tcpip.Address]tempSLAACAddrState, tempAddr tcpip.Address, tempAddrState tempSLAACAddrState) { |
| 1690 |
if ndpDisp := ndp.ep.protocol.options.NDPDisp; ndpDisp != nil { |
| 1691 |
ndpDisp.OnAutoGenAddressInvalidated(ndp.ep.nic.ID(), tempAddrState.addressEndpoint.AddressWithPrefix()) |
| 1692 |
} |
| 1693 |
|
| 1694 |
tempAddrState.addressEndpoint.DecRef() |
| 1695 |
tempAddrState.addressEndpoint = nil |
| 1696 |
tempAddrState.deprecationJob.Cancel() |
| 1697 |
tempAddrState.invalidationJob.Cancel() |
| 1698 |
tempAddrState.regenJob.Cancel() |
| 1699 |
delete(tempAddrs, tempAddr) |
| 1700 |
} |
| 1701 |
|
| 1702 |
|
| 1703 |
|
| 1704 |
|
| 1705 |
|
| 1706 |
|
| 1707 |
|
| 1708 |
func (ndp *ndpState) cleanupState() { |
| 1709 |
for prefix, state := range ndp.slaacPrefixes { |
| 1710 |
ndp.invalidateSLAACPrefix(prefix, state) |
| 1711 |
} |
| 1712 |
|
| 1713 |
for prefix := range ndp.onLinkPrefixes { |
| 1714 |
ndp.invalidateOnLinkPrefix(prefix) |
| 1715 |
} |
| 1716 |
|
| 1717 |
if got := len(ndp.onLinkPrefixes); got != 0 { |
| 1718 |
panic(fmt.Sprintf("ndp: still have discovered on-link prefixes after cleaning up; found = %d", got)) |
| 1719 |
} |
| 1720 |
|
| 1721 |
for route := range ndp.offLinkRoutes { |
| 1722 |
ndp.invalidateOffLinkRoute(route) |
| 1723 |
} |
| 1724 |
|
| 1725 |
if got := len(ndp.offLinkRoutes); got != 0 { |
| 1726 |
panic(fmt.Sprintf("ndp: still have discovered off-link routes after cleaning up; found = %d", got)) |
| 1727 |
} |
| 1728 |
|
| 1729 |
ndp.dhcpv6Configuration = 0 |
| 1730 |
} |
| 1731 |
|
| 1732 |
|
| 1733 |
|
| 1734 |
|
| 1735 |
|
| 1736 |
|
| 1737 |
|
| 1738 |
|
| 1739 |
|
| 1740 |
func (ndp *ndpState) startSolicitingRouters() { |
| 1741 |
if ndp.rtrSolicitTimer.timer != nil { |
| 1742 |
|
| 1743 |
return |
| 1744 |
} |
| 1745 |
|
| 1746 |
remaining := ndp.configs.MaxRtrSolicitations |
| 1747 |
if remaining == 0 { |
| 1748 |
return |
| 1749 |
} |
| 1750 |
|
| 1751 |
if !ndp.configs.HandleRAs.enabled(ndp.ep.Forwarding()) { |
| 1752 |
return |
| 1753 |
} |
| 1754 |
|
| 1755 |
|
| 1756 |
|
| 1757 |
var delay time.Duration |
| 1758 |
if ndp.configs.MaxRtrSolicitationDelay > 0 { |
| 1759 |
delay = time.Duration(ndp.ep.protocol.stack.Rand().Int63n(int64(ndp.configs.MaxRtrSolicitationDelay))) |
| 1760 |
} |
| 1761 |
|
| 1762 |
|
| 1763 |
done := false |
| 1764 |
|
| 1765 |
ndp.rtrSolicitTimer = timer{ |
| 1766 |
done: &done, |
| 1767 |
timer: ndp.ep.protocol.stack.Clock().AfterFunc(delay, func() { |
| 1768 |
|
| 1769 |
|
| 1770 |
|
| 1771 |
|
| 1772 |
|
| 1773 |
|
| 1774 |
|
| 1775 |
localAddr := header.IPv6Any |
| 1776 |
if addressEndpoint := ndp.ep.AcquireOutgoingPrimaryAddress(header.IPv6AllRoutersLinkLocalMulticastAddress, false); addressEndpoint != nil { |
| 1777 |
localAddr = addressEndpoint.AddressWithPrefix().Address |
| 1778 |
addressEndpoint.DecRef() |
| 1779 |
} |
| 1780 |
|
| 1781 |
|
| 1782 |
|
| 1783 |
|
| 1784 |
|
| 1785 |
|
| 1786 |
|
| 1787 |
|
| 1788 |
var optsSerializer header.NDPOptionsSerializer |
| 1789 |
linkAddress := ndp.ep.nic.LinkAddress() |
| 1790 |
if localAddr != header.IPv6Any && header.IsValidUnicastEthernetAddress(linkAddress) { |
| 1791 |
optsSerializer = header.NDPOptionsSerializer{ |
| 1792 |
header.NDPSourceLinkLayerAddressOption(linkAddress), |
| 1793 |
} |
| 1794 |
} |
| 1795 |
payloadSize := header.ICMPv6HeaderSize + header.NDPRSMinimumSize + optsSerializer.Length() |
| 1796 |
icmpData := header.ICMPv6(buffer.NewView(payloadSize)) |
| 1797 |
icmpData.SetType(header.ICMPv6RouterSolicit) |
| 1798 |
rs := header.NDPRouterSolicit(icmpData.MessageBody()) |
| 1799 |
rs.Options().Serialize(optsSerializer) |
| 1800 |
icmpData.SetChecksum(header.ICMPv6Checksum(header.ICMPv6ChecksumParams{ |
| 1801 |
Header: icmpData, |
| 1802 |
Src: localAddr, |
| 1803 |
Dst: header.IPv6AllRoutersLinkLocalMulticastAddress, |
| 1804 |
})) |
| 1805 |
|
| 1806 |
pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ |
| 1807 |
ReserveHeaderBytes: int(ndp.ep.MaxHeaderLength()), |
| 1808 |
Data: buffer.View(icmpData).ToVectorisedView(), |
| 1809 |
}) |
| 1810 |
defer pkt.DecRef() |
| 1811 |
|
| 1812 |
sent := ndp.ep.stats.icmp.packetsSent |
| 1813 |
if err := addIPHeader(localAddr, header.IPv6AllRoutersLinkLocalMulticastAddress, pkt, stack.NetworkHeaderParams{ |
| 1814 |
Protocol: header.ICMPv6ProtocolNumber, |
| 1815 |
TTL: header.NDPHopLimit, |
| 1816 |
}, nil ); err != nil { |
| 1817 |
panic(fmt.Sprintf("failed to add IP header: %s", err)) |
| 1818 |
} |
| 1819 |
|
| 1820 |
if err := ndp.ep.nic.WritePacketToRemote(header.EthernetAddressFromMulticastIPv6Address(header.IPv6AllRoutersLinkLocalMulticastAddress), ProtocolNumber, pkt); err != nil { |
| 1821 |
sent.dropped.Increment() |
| 1822 |
|
| 1823 |
remaining = 0 |
| 1824 |
} else { |
| 1825 |
sent.routerSolicit.Increment() |
| 1826 |
remaining-- |
| 1827 |
} |
| 1828 |
|
| 1829 |
ndp.ep.mu.Lock() |
| 1830 |
defer ndp.ep.mu.Unlock() |
| 1831 |
|
| 1832 |
if done { |
| 1833 |
|
| 1834 |
return |
| 1835 |
} |
| 1836 |
|
| 1837 |
if remaining == 0 { |
| 1838 |
|
| 1839 |
ndp.stopSolicitingRouters() |
| 1840 |
return |
| 1841 |
} |
| 1842 |
|
| 1843 |
ndp.rtrSolicitTimer.timer.Reset(ndp.configs.RtrSolicitationInterval) |
| 1844 |
}), |
| 1845 |
} |
| 1846 |
} |
| 1847 |
|
| 1848 |
|
| 1849 |
|
| 1850 |
|
| 1851 |
|
| 1852 |
|
| 1853 |
|
| 1854 |
|
| 1855 |
func (ndp *ndpState) forwardingChanged(forwarding bool) { |
| 1856 |
if forwarding { |
| 1857 |
if ndp.configs.HandleRAs.enabled(forwarding) { |
| 1858 |
return |
| 1859 |
} |
| 1860 |
|
| 1861 |
ndp.stopSolicitingRouters() |
| 1862 |
return |
| 1863 |
} |
| 1864 |
|
| 1865 |
|
| 1866 |
|
| 1867 |
|
| 1868 |
|
| 1869 |
if ndp.ep.Enabled() { |
| 1870 |
ndp.startSolicitingRouters() |
| 1871 |
} |
| 1872 |
} |
| 1873 |
|
| 1874 |
|
| 1875 |
|
| 1876 |
|
| 1877 |
|
| 1878 |
func (ndp *ndpState) stopSolicitingRouters() { |
| 1879 |
if ndp.rtrSolicitTimer.timer == nil { |
| 1880 |
|
| 1881 |
return |
| 1882 |
} |
| 1883 |
|
| 1884 |
ndp.rtrSolicitTimer.timer.Stop() |
| 1885 |
*ndp.rtrSolicitTimer.done = true |
| 1886 |
ndp.rtrSolicitTimer = timer{} |
| 1887 |
} |
| 1888 |
|
| 1889 |
func (ndp *ndpState) init(ep *endpoint, dadOptions ip.DADOptions) { |
| 1890 |
if ndp.offLinkRoutes != nil { |
| 1891 |
panic("attempted to initialize NDP state twice") |
| 1892 |
} |
| 1893 |
|
| 1894 |
ndp.ep = ep |
| 1895 |
ndp.configs = ep.protocol.options.NDPConfigs |
| 1896 |
ndp.dad.Init(&ndp.ep.mu, ep.protocol.options.DADConfigs, dadOptions) |
| 1897 |
ndp.offLinkRoutes = make(map[offLinkRoute]offLinkRouteState) |
| 1898 |
ndp.onLinkPrefixes = make(map[tcpip.Subnet]onLinkPrefixState) |
| 1899 |
ndp.slaacPrefixes = make(map[tcpip.Subnet]slaacPrefixState) |
| 1900 |
|
| 1901 |
header.InitialTempIID(ndp.temporaryIIDHistory[:], ndp.ep.protocol.options.TempIIDSeed, ndp.ep.nic.ID()) |
| 1902 |
ndp.temporaryAddressDesyncFactor = time.Duration(ep.protocol.stack.Rand().Int63n(int64(MaxDesyncFactor))) |
| 1903 |
} |
| 1904 |
|
| 1905 |
func (ndp *ndpState) SendDADMessage(addr tcpip.Address, nonce []byte) tcpip.Error { |
| 1906 |
snmc := header.SolicitedNodeAddr(addr) |
| 1907 |
return ndp.ep.sendNDPNS(header.IPv6Any, snmc, addr, header.EthernetAddressFromMulticastIPv6Address(snmc), header.NDPOptionsSerializer{ |
| 1908 |
header.NDPNonceOption(nonce), |
| 1909 |
}) |
| 1910 |
} |
| 1911 |
|
| 1912 |
func (e *endpoint) sendNDPNS(srcAddr, dstAddr, targetAddr tcpip.Address, remoteLinkAddr tcpip.LinkAddress, opts header.NDPOptionsSerializer) tcpip.Error { |
| 1913 |
icmp := header.ICMPv6(buffer.NewView(header.ICMPv6NeighborSolicitMinimumSize + opts.Length())) |
| 1914 |
icmp.SetType(header.ICMPv6NeighborSolicit) |
| 1915 |
ns := header.NDPNeighborSolicit(icmp.MessageBody()) |
| 1916 |
ns.SetTargetAddress(targetAddr) |
| 1917 |
ns.Options().Serialize(opts) |
| 1918 |
icmp.SetChecksum(header.ICMPv6Checksum(header.ICMPv6ChecksumParams{ |
| 1919 |
Header: icmp, |
| 1920 |
Src: srcAddr, |
| 1921 |
Dst: dstAddr, |
| 1922 |
})) |
| 1923 |
|
| 1924 |
pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ |
| 1925 |
ReserveHeaderBytes: int(e.MaxHeaderLength()), |
| 1926 |
Data: buffer.View(icmp).ToVectorisedView(), |
| 1927 |
}) |
| 1928 |
defer pkt.DecRef() |
| 1929 |
|
| 1930 |
if err := addIPHeader(srcAddr, dstAddr, pkt, stack.NetworkHeaderParams{ |
| 1931 |
Protocol: header.ICMPv6ProtocolNumber, |
| 1932 |
TTL: header.NDPHopLimit, |
| 1933 |
}, nil ); err != nil { |
| 1934 |
panic(fmt.Sprintf("failed to add IP header: %s", err)) |
| 1935 |
} |
| 1936 |
|
| 1937 |
sent := e.stats.icmp.packetsSent |
| 1938 |
err := e.nic.WritePacketToRemote(remoteLinkAddr, ProtocolNumber, pkt) |
| 1939 |
if err != nil { |
| 1940 |
sent.dropped.Increment() |
| 1941 |
} else { |
| 1942 |
sent.neighborSolicit.Increment() |
| 1943 |
} |
| 1944 |
return err |
| 1945 |
} |