ipcalc.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2018-present the CoreDHCP Authors. All rights reserved
  2. // This source code is licensed under the MIT license found in the
  3. // LICENSE file in the root directory of this source tree.
  4. // Provides functions to add/subtract ipv6 addresses, for use in offset
  5. // calculations in allocators
  6. package allocators
  7. import (
  8. "bytes"
  9. "encoding/binary"
  10. "errors"
  11. "math/bits"
  12. "net"
  13. )
  14. // ErrOverflow is returned when arithmetic operations on IPs carry bits
  15. // over/under the 0th or 128th bit respectively
  16. var ErrOverflow = errors.New("Operation overflows")
  17. // Offset returns the absolute distance between addresses `a` and `b` in units
  18. // of /`prefixLength` subnets.
  19. // Both addresses will have a /`prefixLength` mask applied to them, any
  20. // differences of less than that will be discarded
  21. // If the distance is larger than 2^64 units of /`prefixLength` an error is returned
  22. //
  23. // This function is used in allocators to index bitmaps by an offset from the
  24. // first ip of the range
  25. func Offset(a, b net.IP, prefixLength int) (uint64, error) {
  26. if prefixLength > 128 || prefixLength < 0 {
  27. return 0, errors.New("prefix out of range")
  28. }
  29. reverse := bytes.Compare(a, b)
  30. if reverse == 0 {
  31. return 0, nil
  32. } else if reverse < 0 {
  33. a, b = b, a
  34. }
  35. // take an example of [a:b:c:d:e:f:g:h] [1:2:3:4:5:6:7:8]
  36. // Cut the addresses as such: [a:b:c:d|e:f:g:h] [1:2:3:4|5:6:7:8] so we can use
  37. // native integers for computation
  38. ah, bh := binary.BigEndian.Uint64(a[:8]), binary.BigEndian.Uint64(b[:8])
  39. if prefixLength <= 64 {
  40. // [(a:b:c):d|e:f:g:h] - [(1:2:3):4|5:6:7:8]
  41. // Only the high bits matter, so the distance always fits within 64 bits.
  42. // We shift to remove anything to the right of the cut
  43. // [(a:b:c):d] => [0:a:b:c]
  44. return (ah - bh) >> (64 - uint(prefixLength)), nil
  45. }
  46. // General case where both high and low bits matter
  47. al, bl := binary.BigEndian.Uint64(a[8:]), binary.BigEndian.Uint64(b[8:])
  48. distanceLow, borrow := bits.Sub64(al, bl, 0)
  49. // This is the distance between the high bits. depending on the prefix unit, we
  50. // will shift this distance left or right
  51. distanceHigh, _ := bits.Sub64(ah, bh, borrow) // [a:b:c:d] - [1:2:3:4]
  52. // [a:b:c:(d|e:f:g):h] - [1:2:3:(4|5:6:7):8]
  53. // we cut in the low bits (eg. between the parentheses)
  54. // To ensure we stay within 64 bits, we need to ensure [a:b:c:d] - [1:2:3:4] = [0:0:0:d-4]
  55. // so that we don't overflow when adding to the low bits
  56. if distanceHigh >= (1 << (128 - uint(prefixLength))) {
  57. return 0, ErrOverflow
  58. }
  59. // Schema of the carry and shifts:
  60. // [a:b:c:(d]
  61. // [e:f:g):h]
  62. // <---------------> prefixLen
  63. // <-> 128 - prefixLen (cut right)
  64. // <-----> prefixLen - 64 (cut left)
  65. //
  66. // [a:b:c:(d] => [d:0:0:0]
  67. distanceHigh <<= uint(prefixLength) - 64
  68. // [e:f:g):h] => [0:e:f:g]
  69. distanceLow >>= 128 - uint(prefixLength)
  70. // [d:0:0:0] + [0:e:f:g] = (d:e:f:g)
  71. return distanceHigh + distanceLow, nil
  72. }
  73. // AddPrefixes returns the `n`th /`unit` subnet after the `ip` base subnet. It
  74. // is the converse operation of Offset(), used to retrieve a prefix from the
  75. // index within the allocator table
  76. func AddPrefixes(ip net.IP, n, unit uint64) (net.IP, error) {
  77. if unit == 0 && n != 0 {
  78. return net.IP{}, ErrOverflow
  79. } else if n == 0 {
  80. return ip, nil
  81. }
  82. if len(ip) != 16 {
  83. // We don't actually care if they're true v6 or v4-mapped,
  84. // but they need to be 128-bit to handle as 64-bit ints
  85. return net.IP{}, errors.New("AddPrefixes needs 128-bit IPs")
  86. }
  87. // Compute as pairs of uint64 for easier operations
  88. // This could all be 1 function call if go had 128-bit integers
  89. iph, ipl := binary.BigEndian.Uint64(ip[:8]), binary.BigEndian.Uint64(ip[8:])
  90. // Compute `n` /`unit` subnets as uint64 pair
  91. var offh, offl uint64
  92. if unit <= 64 {
  93. offh = n << (64 - unit)
  94. } else {
  95. offh, offl = bits.Mul64(n, 1<<(128-unit))
  96. }
  97. // Now add the 2, check for overflow
  98. ipl, carry := bits.Add64(offl, ipl, 0)
  99. iph, carry = bits.Add64(offh, iph, carry)
  100. if carry != 0 {
  101. return net.IP{}, ErrOverflow
  102. }
  103. // Finally convert back to net.IP
  104. ret := make(net.IP, net.IPv6len)
  105. binary.BigEndian.PutUint64(ret[:8], iph)
  106. binary.BigEndian.PutUint64(ret[8:], ipl)
  107. return ret, nil
  108. }