| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package header |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"encoding/binary" |
| 20 |
"errors" |
| 21 |
"fmt" |
| 22 |
"io" |
| 23 |
"math" |
| 24 |
"time" |
| 25 |
|
| 26 |
"inet.af/netstack/tcpip" |
| 27 |
) |
| 28 |
|
| 29 |
|
| 30 |
type ndpOptionIdentifier uint8 |
| 31 |
|
| 32 |
const ( |
| 33 |
|
| 34 |
|
| 35 |
ndpSourceLinkLayerAddressOptionType ndpOptionIdentifier = 1 |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
ndpTargetLinkLayerAddressOptionType ndpOptionIdentifier = 2 |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
ndpPrefixInformationType ndpOptionIdentifier = 3 |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
ndpNonceOptionType ndpOptionIdentifier = 14 |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
ndpRecursiveDNSServerOptionType ndpOptionIdentifier = 25 |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
ndpDNSSearchListOptionType ndpOptionIdentifier = 31 |
| 56 |
) |
| 57 |
|
| 58 |
const ( |
| 59 |
|
| 60 |
|
| 61 |
NDPLinkLayerAddressSize = 8 |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
ndpPrefixInformationLength = 30 |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
ndpPrefixInformationPrefixLengthOffset = 0 |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
ndpPrefixInformationFlagsOffset = 1 |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
ndpPrefixInformationOnLinkFlagMask = 1 << 7 |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
ndpPrefixInformationAutoAddrConfFlagMask = 1 << 6 |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
ndpPrefixInformationReserved1FlagsMask = 63 |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
ndpPrefixInformationValidLifetimeOffset = 2 |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
ndpPrefixInformationPreferredLifetimeOffset = 6 |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
ndpPrefixInformationReserved2Offset = 10 |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
ndpPrefixInformationReserved2Length = 4 |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
ndpPrefixInformationPrefixOffset = 14 |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
ndpRecursiveDNSServerLifetimeOffset = 2 |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
ndpRecursiveDNSServerAddressesOffset = 6 |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
minNDPRecursiveDNSServerBodySize = 22 |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
ndpDNSSearchListLifetimeOffset = 2 |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
ndpDNSSearchListDomainNamesOffset = 6 |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
minNDPDNSSearchListBodySize = 14 |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
maxDomainNameLabelLength = 63 |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
maxDomainNameLength = 255 |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
lengthByteUnits = 8 |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
NDPInfiniteLifetime = time.Second * math.MaxUint32 |
| 156 |
) |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
type NDPOptionIterator struct { |
| 166 |
opts *bytes.Buffer |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
var ( |
| 171 |
ErrNDPOptMalformedBody = errors.New("NDP option has a malformed body") |
| 172 |
ErrNDPOptMalformedHeader = errors.New("NDP option has a malformed header") |
| 173 |
) |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
func (i *NDPOptionIterator) Next() (NDPOption, bool, error) { |
| 181 |
for { |
| 182 |
|
| 183 |
if i.opts.Len() == 0 { |
| 184 |
return nil, true, nil |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
temp, err := i.opts.ReadByte() |
| 189 |
if err != nil { |
| 190 |
if err != io.EOF { |
| 191 |
|
| 192 |
panic(fmt.Sprintf("unexpected error when reading the option's Type field: %s", err)) |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
return nil, true, fmt.Errorf("unexpectedly exhausted buffer when reading the option's Type field: %w", io.ErrUnexpectedEOF) |
| 199 |
} |
| 200 |
kind := ndpOptionIdentifier(temp) |
| 201 |
|
| 202 |
|
| 203 |
length, err := i.opts.ReadByte() |
| 204 |
if err != nil { |
| 205 |
if err != io.EOF { |
| 206 |
panic(fmt.Sprintf("unexpected error when reading the option's Length field for %s: %s", kind, err)) |
| 207 |
} |
| 208 |
|
| 209 |
return nil, true, fmt.Errorf("unexpectedly exhausted buffer when reading the option's Length field for %s: %w", kind, io.ErrUnexpectedEOF) |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
if length == 0 { |
| 215 |
return nil, true, fmt.Errorf("zero valued Length field for %s: %w", kind, ErrNDPOptMalformedHeader) |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
numBytes := int(length) * lengthByteUnits |
| 220 |
numBodyBytes := numBytes - 2 |
| 221 |
body := i.opts.Next(numBodyBytes) |
| 222 |
if len(body) < numBodyBytes { |
| 223 |
return nil, true, fmt.Errorf("unexpectedly exhausted buffer when reading the option's Body for %s: %w", kind, io.ErrUnexpectedEOF) |
| 224 |
} |
| 225 |
|
| 226 |
switch kind { |
| 227 |
case ndpSourceLinkLayerAddressOptionType: |
| 228 |
return NDPSourceLinkLayerAddressOption(body), false, nil |
| 229 |
|
| 230 |
case ndpTargetLinkLayerAddressOptionType: |
| 231 |
return NDPTargetLinkLayerAddressOption(body), false, nil |
| 232 |
|
| 233 |
case ndpNonceOptionType: |
| 234 |
return NDPNonceOption(body), false, nil |
| 235 |
|
| 236 |
case ndpRouteInformationType: |
| 237 |
if numBodyBytes > ndpRouteInformationMaxLength { |
| 238 |
return nil, true, fmt.Errorf("got %d bytes for NDP Route Information option's body, expected at max %d bytes: %w", numBodyBytes, ndpRouteInformationMaxLength, ErrNDPOptMalformedBody) |
| 239 |
} |
| 240 |
opt := NDPRouteInformation(body) |
| 241 |
if err := opt.hasError(); err != nil { |
| 242 |
return nil, true, err |
| 243 |
} |
| 244 |
|
| 245 |
return opt, false, nil |
| 246 |
|
| 247 |
case ndpPrefixInformationType: |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
if numBodyBytes != ndpPrefixInformationLength { |
| 252 |
return nil, true, fmt.Errorf("got %d bytes for NDP Prefix Information option's body, expected %d bytes: %w", numBodyBytes, ndpPrefixInformationLength, ErrNDPOptMalformedBody) |
| 253 |
} |
| 254 |
|
| 255 |
return NDPPrefixInformation(body), false, nil |
| 256 |
|
| 257 |
case ndpRecursiveDNSServerOptionType: |
| 258 |
opt := NDPRecursiveDNSServer(body) |
| 259 |
if err := opt.checkAddresses(); err != nil { |
| 260 |
return nil, true, err |
| 261 |
} |
| 262 |
|
| 263 |
return opt, false, nil |
| 264 |
|
| 265 |
case ndpDNSSearchListOptionType: |
| 266 |
opt := NDPDNSSearchList(body) |
| 267 |
if err := opt.checkDomainNames(); err != nil { |
| 268 |
return nil, true, err |
| 269 |
} |
| 270 |
|
| 271 |
return opt, false, nil |
| 272 |
|
| 273 |
default: |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
type NDPOptions []byte |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
func (b NDPOptions) Iter(check bool) (NDPOptionIterator, error) { |
| 295 |
it := NDPOptionIterator{ |
| 296 |
opts: bytes.NewBuffer(b), |
| 297 |
} |
| 298 |
|
| 299 |
if check { |
| 300 |
it2 := NDPOptionIterator{ |
| 301 |
opts: bytes.NewBuffer(b), |
| 302 |
} |
| 303 |
|
| 304 |
for { |
| 305 |
if _, done, err := it2.Next(); err != nil || done { |
| 306 |
return it, err |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
return it, nil |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
func (b NDPOptions) Serialize(s NDPOptionsSerializer) int { |
| 323 |
done := 0 |
| 324 |
|
| 325 |
for _, o := range s { |
| 326 |
l := paddedLength(o) |
| 327 |
|
| 328 |
if l == 0 { |
| 329 |
continue |
| 330 |
} |
| 331 |
|
| 332 |
b[0] = byte(o.kind()) |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
b[1] = uint8(l / lengthByteUnits) |
| 337 |
|
| 338 |
|
| 339 |
used := o.serializeInto(b[2:]) |
| 340 |
|
| 341 |
|
| 342 |
for i := used + 2; i < l; i++ { |
| 343 |
b[i] = 0 |
| 344 |
} |
| 345 |
|
| 346 |
b = b[l:] |
| 347 |
done += l |
| 348 |
} |
| 349 |
|
| 350 |
return done |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
type NDPOption interface { |
| 355 |
fmt.Stringer |
| 356 |
|
| 357 |
|
| 358 |
kind() ndpOptionIdentifier |
| 359 |
|
| 360 |
|
| 361 |
length() int |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
serializeInto([]byte) int |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
func paddedLength(o NDPOption) int { |
| 381 |
l := o.length() |
| 382 |
|
| 383 |
if l == 0 { |
| 384 |
return 0 |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
l += 2 |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
mask := lengthByteUnits - 1 |
| 397 |
l += mask |
| 398 |
l &^= mask |
| 399 |
|
| 400 |
if l/lengthByteUnits > 255 { |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
return 0 |
| 413 |
} |
| 414 |
|
| 415 |
return l |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
type NDPOptionsSerializer []NDPOption |
| 420 |
|
| 421 |
|
| 422 |
func (b NDPOptionsSerializer) Length() int { |
| 423 |
l := 0 |
| 424 |
|
| 425 |
for _, o := range b { |
| 426 |
l += paddedLength(o) |
| 427 |
} |
| 428 |
|
| 429 |
return l |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
type NDPNonceOption []byte |
| 437 |
|
| 438 |
|
| 439 |
func (o NDPNonceOption) kind() ndpOptionIdentifier { |
| 440 |
return ndpNonceOptionType |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
func (o NDPNonceOption) length() int { |
| 445 |
return len(o) |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
func (o NDPNonceOption) serializeInto(b []byte) int { |
| 450 |
return copy(b, o) |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
func (o NDPNonceOption) String() string { |
| 455 |
return fmt.Sprintf("%T(%x)", o, []byte(o)) |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
func (o NDPNonceOption) Nonce() []byte { |
| 460 |
return o |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
type NDPSourceLinkLayerAddressOption tcpip.LinkAddress |
| 469 |
|
| 470 |
|
| 471 |
func (o NDPSourceLinkLayerAddressOption) kind() ndpOptionIdentifier { |
| 472 |
return ndpSourceLinkLayerAddressOptionType |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
func (o NDPSourceLinkLayerAddressOption) length() int { |
| 477 |
return len(o) |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
func (o NDPSourceLinkLayerAddressOption) serializeInto(b []byte) int { |
| 482 |
return copy(b, o) |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
func (o NDPSourceLinkLayerAddressOption) String() string { |
| 487 |
return fmt.Sprintf("%T(%s)", o, tcpip.LinkAddress(o)) |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
func (o NDPSourceLinkLayerAddressOption) EthernetAddress() tcpip.LinkAddress { |
| 496 |
if len(o) >= EthernetAddressSize { |
| 497 |
return tcpip.LinkAddress(o[:EthernetAddressSize]) |
| 498 |
} |
| 499 |
|
| 500 |
return tcpip.LinkAddress([]byte(nil)) |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
type NDPTargetLinkLayerAddressOption tcpip.LinkAddress |
| 509 |
|
| 510 |
|
| 511 |
func (o NDPTargetLinkLayerAddressOption) kind() ndpOptionIdentifier { |
| 512 |
return ndpTargetLinkLayerAddressOptionType |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
func (o NDPTargetLinkLayerAddressOption) length() int { |
| 517 |
return len(o) |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
func (o NDPTargetLinkLayerAddressOption) serializeInto(b []byte) int { |
| 522 |
return copy(b, o) |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
func (o NDPTargetLinkLayerAddressOption) String() string { |
| 527 |
return fmt.Sprintf("%T(%s)", o, tcpip.LinkAddress(o)) |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
func (o NDPTargetLinkLayerAddressOption) EthernetAddress() tcpip.LinkAddress { |
| 536 |
if len(o) >= EthernetAddressSize { |
| 537 |
return tcpip.LinkAddress(o[:EthernetAddressSize]) |
| 538 |
} |
| 539 |
|
| 540 |
return tcpip.LinkAddress([]byte(nil)) |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
type NDPPrefixInformation []byte |
| 549 |
|
| 550 |
|
| 551 |
func (o NDPPrefixInformation) kind() ndpOptionIdentifier { |
| 552 |
return ndpPrefixInformationType |
| 553 |
} |
| 554 |
|
| 555 |
|
| 556 |
func (o NDPPrefixInformation) length() int { |
| 557 |
return ndpPrefixInformationLength |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
func (o NDPPrefixInformation) serializeInto(b []byte) int { |
| 562 |
used := copy(b, o) |
| 563 |
|
| 564 |
|
| 565 |
b[ndpPrefixInformationFlagsOffset] &^= ndpPrefixInformationReserved1FlagsMask |
| 566 |
|
| 567 |
|
| 568 |
reserved2 := b[ndpPrefixInformationReserved2Offset:][:ndpPrefixInformationReserved2Length] |
| 569 |
for i := range reserved2 { |
| 570 |
reserved2[i] = 0 |
| 571 |
} |
| 572 |
|
| 573 |
return used |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
func (o NDPPrefixInformation) String() string { |
| 578 |
return fmt.Sprintf("%T(O=%t, A=%t, PL=%s, VL=%s, Prefix=%s)", |
| 579 |
o, |
| 580 |
o.OnLinkFlag(), |
| 581 |
o.AutonomousAddressConfigurationFlag(), |
| 582 |
o.PreferredLifetime(), |
| 583 |
o.ValidLifetime(), |
| 584 |
o.Subnet()) |
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
func (o NDPPrefixInformation) PrefixLength() uint8 { |
| 593 |
return o[ndpPrefixInformationPrefixLengthOffset] |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
func (o NDPPrefixInformation) OnLinkFlag() bool { |
| 605 |
return o[ndpPrefixInformationFlagsOffset]&ndpPrefixInformationOnLinkFlagMask != 0 |
| 606 |
} |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
func (o NDPPrefixInformation) AutonomousAddressConfigurationFlag() bool { |
| 611 |
return o[ndpPrefixInformationFlagsOffset]&ndpPrefixInformationAutoAddrConfFlagMask != 0 |
| 612 |
} |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
func (o NDPPrefixInformation) ValidLifetime() time.Duration { |
| 622 |
|
| 623 |
return time.Second * time.Duration(binary.BigEndian.Uint32(o[ndpPrefixInformationValidLifetimeOffset:])) |
| 624 |
} |
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
func (o NDPPrefixInformation) PreferredLifetime() time.Duration { |
| 639 |
|
| 640 |
return time.Second * time.Duration(binary.BigEndian.Uint32(o[ndpPrefixInformationPreferredLifetimeOffset:])) |
| 641 |
} |
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
func (o NDPPrefixInformation) Prefix() tcpip.Address { |
| 650 |
return tcpip.Address(o[ndpPrefixInformationPrefixOffset:][:IPv6AddressSize]) |
| 651 |
} |
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
func (o NDPPrefixInformation) Subnet() tcpip.Subnet { |
| 656 |
addrWithPrefix := tcpip.AddressWithPrefix{ |
| 657 |
Address: o.Prefix(), |
| 658 |
PrefixLen: int(o.PrefixLength()), |
| 659 |
} |
| 660 |
return addrWithPrefix.Subnet() |
| 661 |
} |
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
type NDPRecursiveDNSServer []byte |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
func (NDPRecursiveDNSServer) kind() ndpOptionIdentifier { |
| 676 |
return ndpRecursiveDNSServerOptionType |
| 677 |
} |
| 678 |
|
| 679 |
|
| 680 |
func (o NDPRecursiveDNSServer) length() int { |
| 681 |
return len(o) |
| 682 |
} |
| 683 |
|
| 684 |
|
| 685 |
func (o NDPRecursiveDNSServer) serializeInto(b []byte) int { |
| 686 |
used := copy(b, o) |
| 687 |
|
| 688 |
|
| 689 |
for i := 0; i < ndpRecursiveDNSServerLifetimeOffset; i++ { |
| 690 |
b[i] = 0 |
| 691 |
} |
| 692 |
|
| 693 |
return used |
| 694 |
} |
| 695 |
|
| 696 |
|
| 697 |
func (o NDPRecursiveDNSServer) String() string { |
| 698 |
lt := o.Lifetime() |
| 699 |
addrs, err := o.Addresses() |
| 700 |
if err != nil { |
| 701 |
return fmt.Sprintf("%T([] valid for %s; err = %s)", o, lt, err) |
| 702 |
} |
| 703 |
return fmt.Sprintf("%T(%s valid for %s)", o, addrs, lt) |
| 704 |
} |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
func (o NDPRecursiveDNSServer) Lifetime() time.Duration { |
| 715 |
|
| 716 |
return time.Second * time.Duration(binary.BigEndian.Uint32(o[ndpRecursiveDNSServerLifetimeOffset:])) |
| 717 |
} |
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
func (o NDPRecursiveDNSServer) Addresses() ([]tcpip.Address, error) { |
| 724 |
var addrs []tcpip.Address |
| 725 |
return addrs, o.iterAddresses(func(addr tcpip.Address) { addrs = append(addrs, addr) }) |
| 726 |
} |
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
func (o NDPRecursiveDNSServer) checkAddresses() error { |
| 731 |
return o.iterAddresses(nil) |
| 732 |
} |
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
func (o NDPRecursiveDNSServer) iterAddresses(fn func(tcpip.Address)) error { |
| 739 |
if l := len(o); l < minNDPRecursiveDNSServerBodySize { |
| 740 |
return fmt.Errorf("got %d bytes for NDP Recursive DNS Server option's body, expected at least %d bytes: %w", l, minNDPRecursiveDNSServerBodySize, io.ErrUnexpectedEOF) |
| 741 |
} |
| 742 |
|
| 743 |
o = o[ndpRecursiveDNSServerAddressesOffset:] |
| 744 |
l := len(o) |
| 745 |
if l%IPv6AddressSize != 0 { |
| 746 |
return fmt.Errorf("NDP Recursive DNS Server option's body ends in the middle of an IPv6 address (addresses body size = %d bytes): %w", l, ErrNDPOptMalformedBody) |
| 747 |
} |
| 748 |
|
| 749 |
for i := 0; len(o) != 0; i++ { |
| 750 |
addr := tcpip.Address(o[:IPv6AddressSize]) |
| 751 |
if !IsV6UnicastAddress(addr) { |
| 752 |
return fmt.Errorf("%d-th address (%s) in NDP Recursive DNS Server option is not a valid unicast IPv6 address: %w", i, addr, ErrNDPOptMalformedBody) |
| 753 |
} |
| 754 |
|
| 755 |
if fn != nil { |
| 756 |
fn(addr) |
| 757 |
} |
| 758 |
|
| 759 |
o = o[IPv6AddressSize:] |
| 760 |
} |
| 761 |
|
| 762 |
return nil |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
type NDPDNSSearchList []byte |
| 768 |
|
| 769 |
|
| 770 |
func (o NDPDNSSearchList) kind() ndpOptionIdentifier { |
| 771 |
return ndpDNSSearchListOptionType |
| 772 |
} |
| 773 |
|
| 774 |
|
| 775 |
func (o NDPDNSSearchList) length() int { |
| 776 |
return len(o) |
| 777 |
} |
| 778 |
|
| 779 |
|
| 780 |
func (o NDPDNSSearchList) serializeInto(b []byte) int { |
| 781 |
used := copy(b, o) |
| 782 |
|
| 783 |
|
| 784 |
for i := 0; i < ndpDNSSearchListLifetimeOffset; i++ { |
| 785 |
b[i] = 0 |
| 786 |
} |
| 787 |
|
| 788 |
return used |
| 789 |
} |
| 790 |
|
| 791 |
|
| 792 |
func (o NDPDNSSearchList) String() string { |
| 793 |
lt := o.Lifetime() |
| 794 |
domainNames, err := o.DomainNames() |
| 795 |
if err != nil { |
| 796 |
return fmt.Sprintf("%T([] valid for %s; err = %s)", o, lt, err) |
| 797 |
} |
| 798 |
return fmt.Sprintf("%T(%s valid for %s)", o, domainNames, lt) |
| 799 |
} |
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
func (o NDPDNSSearchList) Lifetime() time.Duration { |
| 807 |
|
| 808 |
return time.Second * time.Duration(binary.BigEndian.Uint32(o[ndpDNSSearchListLifetimeOffset:])) |
| 809 |
} |
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
func (o NDPDNSSearchList) DomainNames() ([]string, error) { |
| 816 |
var domainNames []string |
| 817 |
return domainNames, o.iterDomainNames(func(domainName string) { domainNames = append(domainNames, domainName) }) |
| 818 |
} |
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
func (o NDPDNSSearchList) checkDomainNames() error { |
| 823 |
return o.iterDomainNames(nil) |
| 824 |
} |
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
func (o NDPDNSSearchList) iterDomainNames(fn func(string)) error { |
| 829 |
if l := len(o); l < minNDPDNSSearchListBodySize { |
| 830 |
return fmt.Errorf("got %d bytes for NDP DNS Search List option's body, expected at least %d bytes: %w", l, minNDPDNSSearchListBodySize, io.ErrUnexpectedEOF) |
| 831 |
} |
| 832 |
|
| 833 |
var searchList bytes.Reader |
| 834 |
searchList.Reset(o[ndpDNSSearchListDomainNamesOffset:]) |
| 835 |
|
| 836 |
var scratch [maxDomainNameLength]byte |
| 837 |
domainName := bytes.NewBuffer(scratch[:]) |
| 838 |
|
| 839 |
|
| 840 |
for searchList.Len() != 0 { |
| 841 |
domainName.Reset() |
| 842 |
|
| 843 |
|
| 844 |
for { |
| 845 |
|
| 846 |
labelLenByte, err := searchList.ReadByte() |
| 847 |
if err != nil { |
| 848 |
if err != io.EOF { |
| 849 |
|
| 850 |
panic(fmt.Sprintf("unexpected error when reading a label's length: %s", err)) |
| 851 |
} |
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
return fmt.Errorf("unexpected exhausted buffer while parsing a new label for a domain from NDP Search List option: %w", io.ErrUnexpectedEOF) |
| 857 |
} |
| 858 |
labelLen := int(labelLenByte) |
| 859 |
|
| 860 |
|
| 861 |
if labelLen == 0 { |
| 862 |
|
| 863 |
|
| 864 |
if domainName.Len() == 0 || fn == nil { |
| 865 |
break |
| 866 |
} |
| 867 |
|
| 868 |
|
| 869 |
domainName.Truncate(domainName.Len() - 1) |
| 870 |
fn(domainName.String()) |
| 871 |
break |
| 872 |
} |
| 873 |
|
| 874 |
|
| 875 |
if labelLen > maxDomainNameLabelLength { |
| 876 |
return fmt.Errorf("label length of %d bytes is greater than the max label length of %d bytes for an NDP Search List option: %w", labelLen, maxDomainNameLabelLength, ErrNDPOptMalformedBody) |
| 877 |
} |
| 878 |
|
| 879 |
|
| 880 |
if labelLen+1 > domainName.Cap()-domainName.Len() { |
| 881 |
return fmt.Errorf("label would make an NDP Search List option's domain name longer than the max domain name length of %d bytes: %w", maxDomainNameLength, ErrNDPOptMalformedBody) |
| 882 |
} |
| 883 |
|
| 884 |
|
| 885 |
for i := 0; i < labelLen; i++ { |
| 886 |
b, err := searchList.ReadByte() |
| 887 |
if err != nil { |
| 888 |
if err != io.EOF { |
| 889 |
panic(fmt.Sprintf("unexpected error when reading domain name's label: %s", err)) |
| 890 |
} |
| 891 |
|
| 892 |
return fmt.Errorf("read %d out of %d bytes for a domain name's label from NDP Search List option: %w", i, labelLen, io.ErrUnexpectedEOF) |
| 893 |
} |
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
if !isLetter(b) { |
| 902 |
if i == 0 { |
| 903 |
return fmt.Errorf("first character of a domain name's label in an NDP Search List option must be a letter, got character code = %d: %w", b, ErrNDPOptMalformedBody) |
| 904 |
} |
| 905 |
|
| 906 |
if b == '-' { |
| 907 |
if i == labelLen-1 { |
| 908 |
return fmt.Errorf("last character of a domain name's label in an NDP Search List option must not be a hyphen (-): %w", ErrNDPOptMalformedBody) |
| 909 |
} |
| 910 |
} else if !isDigit(b) { |
| 911 |
return fmt.Errorf("domain name's label in an NDP Search List option may only contain letters, digits and hyphens, got character code = %d: %w", b, ErrNDPOptMalformedBody) |
| 912 |
} |
| 913 |
} |
| 914 |
|
| 915 |
|
| 916 |
if isUpperLetter(b) { |
| 917 |
b = b - 'A' + 'a' |
| 918 |
} |
| 919 |
|
| 920 |
if err := domainName.WriteByte(b); err != nil { |
| 921 |
panic(fmt.Sprintf("unexpected error writing label to domain name buffer: %s", err)) |
| 922 |
} |
| 923 |
} |
| 924 |
if err := domainName.WriteByte('.'); err != nil { |
| 925 |
panic(fmt.Sprintf("unexpected error writing trailing period to domain name buffer: %s", err)) |
| 926 |
} |
| 927 |
} |
| 928 |
} |
| 929 |
|
| 930 |
return nil |
| 931 |
} |
| 932 |
|
| 933 |
func isLetter(b byte) bool { |
| 934 |
return b >= 'a' && b <= 'z' || isUpperLetter(b) |
| 935 |
} |
| 936 |
|
| 937 |
func isUpperLetter(b byte) bool { |
| 938 |
return b >= 'A' && b <= 'Z' |
| 939 |
} |
| 940 |
|
| 941 |
func isDigit(b byte) bool { |
| 942 |
return b >= '0' && b <= '9' |
| 943 |
} |
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
const ( |
| 974 |
ndpRouteInformationType = ndpOptionIdentifier(24) |
| 975 |
ndpRouteInformationMaxLength = 22 |
| 976 |
|
| 977 |
ndpRouteInformationPrefixLengthIdx = 0 |
| 978 |
ndpRouteInformationFlagsIdx = 1 |
| 979 |
ndpRouteInformationPrfShift = 3 |
| 980 |
ndpRouteInformationPrfMask = 3 << ndpRouteInformationPrfShift |
| 981 |
ndpRouteInformationRouteLifetimeIdx = 2 |
| 982 |
ndpRouteInformationRoutePrefixIdx = 6 |
| 983 |
) |
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
type NDPRouteInformation []byte |
| 988 |
|
| 989 |
func (NDPRouteInformation) kind() ndpOptionIdentifier { |
| 990 |
return ndpRouteInformationType |
| 991 |
} |
| 992 |
|
| 993 |
func (o NDPRouteInformation) length() int { |
| 994 |
return len(o) |
| 995 |
} |
| 996 |
|
| 997 |
func (o NDPRouteInformation) serializeInto(b []byte) int { |
| 998 |
return copy(b, o) |
| 999 |
} |
| 1000 |
|
| 1001 |
|
| 1002 |
func (o NDPRouteInformation) String() string { |
| 1003 |
return fmt.Sprintf("%T", o) |
| 1004 |
} |
| 1005 |
|
| 1006 |
|
| 1007 |
func (o NDPRouteInformation) PrefixLength() uint8 { |
| 1008 |
return o[ndpRouteInformationPrefixLengthIdx] |
| 1009 |
} |
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
func (o NDPRouteInformation) RoutePreference() NDPRoutePreference { |
| 1014 |
return NDPRoutePreference((o[ndpRouteInformationFlagsIdx] & ndpRouteInformationPrfMask) >> ndpRouteInformationPrfShift) |
| 1015 |
} |
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
func (o NDPRouteInformation) RouteLifetime() time.Duration { |
| 1022 |
return time.Second * time.Duration(binary.BigEndian.Uint32(o[ndpRouteInformationRouteLifetimeIdx:])) |
| 1023 |
} |
| 1024 |
|
| 1025 |
|
| 1026 |
func (o NDPRouteInformation) Prefix() (tcpip.Subnet, error) { |
| 1027 |
prefixLength := int(o.PrefixLength()) |
| 1028 |
if max := IPv6AddressSize * 8; prefixLength > max { |
| 1029 |
return tcpip.Subnet{}, fmt.Errorf("got prefix length = %d, want <= %d", prefixLength, max) |
| 1030 |
} |
| 1031 |
|
| 1032 |
prefix := o[ndpRouteInformationRoutePrefixIdx:] |
| 1033 |
var addrBytes [IPv6AddressSize]byte |
| 1034 |
if n := copy(addrBytes[:], prefix); n != len(prefix) { |
| 1035 |
panic(fmt.Sprintf("got copy(addrBytes, prefix) = %d, want = %d", n, len(prefix))) |
| 1036 |
} |
| 1037 |
|
| 1038 |
return tcpip.AddressWithPrefix{ |
| 1039 |
Address: tcpip.Address(addrBytes[:]), |
| 1040 |
PrefixLen: prefixLength, |
| 1041 |
}.Subnet(), nil |
| 1042 |
} |
| 1043 |
|
| 1044 |
func (o NDPRouteInformation) hasError() error { |
| 1045 |
l := len(o) |
| 1046 |
if l < ndpRouteInformationRoutePrefixIdx { |
| 1047 |
return fmt.Errorf("%T too small, got = %d bytes: %w", o, l, ErrNDPOptMalformedBody) |
| 1048 |
} |
| 1049 |
|
| 1050 |
prefixLength := int(o.PrefixLength()) |
| 1051 |
if max := IPv6AddressSize * 8; prefixLength > max { |
| 1052 |
return fmt.Errorf("got prefix length = %d, want <= %d: %w", prefixLength, max, ErrNDPOptMalformedBody) |
| 1053 |
} |
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
l += 2 |
| 1063 |
lengthField := l / lengthByteUnits |
| 1064 |
if prefixLength > 64 { |
| 1065 |
if lengthField != 3 { |
| 1066 |
return fmt.Errorf("Length field must be 3 when Prefix Length (%d) is > 64 (got = %d): %w", prefixLength, lengthField, ErrNDPOptMalformedBody) |
| 1067 |
} |
| 1068 |
} else if prefixLength > 0 { |
| 1069 |
if lengthField != 2 && lengthField != 3 { |
| 1070 |
return fmt.Errorf("Length field must be 2 or 3 when Prefix Length (%d) is between 0 and 64 (got = %d): %w", prefixLength, lengthField, ErrNDPOptMalformedBody) |
| 1071 |
} |
| 1072 |
} else if lengthField == 0 || lengthField > 3 { |
| 1073 |
return fmt.Errorf("Length field must be 1, 2, or 3 when Prefix Length is zero (got = %d): %w", lengthField, ErrNDPOptMalformedBody) |
| 1074 |
} |
| 1075 |
|
| 1076 |
return nil |
| 1077 |
} |