2
0

calculated_remote.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package nebula
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. "math"
  6. "net"
  7. "net/netip"
  8. "strconv"
  9. "github.com/gaissmai/bart"
  10. "github.com/slackhq/nebula/config"
  11. )
  12. // This allows us to "guess" what the remote might be for a host while we wait
  13. // for the lighthouse response. See "lighthouse.calculated_remotes" in the
  14. // example config file.
  15. type calculatedRemote struct {
  16. ipNet netip.Prefix
  17. mask netip.Prefix
  18. port uint32
  19. }
  20. func newCalculatedRemote(maskCidr netip.Prefix, port int) (*calculatedRemote, error) {
  21. masked := maskCidr.Masked()
  22. if port < 0 || port > math.MaxUint16 {
  23. return nil, fmt.Errorf("invalid port: %d", port)
  24. }
  25. return &calculatedRemote{
  26. ipNet: maskCidr,
  27. mask: masked,
  28. port: uint32(port),
  29. }, nil
  30. }
  31. func (c *calculatedRemote) String() string {
  32. return fmt.Sprintf("CalculatedRemote(mask=%v port=%d)", c.ipNet, c.port)
  33. }
  34. func (c *calculatedRemote) Apply(ip netip.Addr) *Ip4AndPort {
  35. // Combine the masked bytes of the "mask" IP with the unmasked bytes
  36. // of the overlay IP
  37. if c.ipNet.Addr().Is4() {
  38. return c.apply4(ip)
  39. }
  40. return c.apply6(ip)
  41. }
  42. func (c *calculatedRemote) apply4(ip netip.Addr) *Ip4AndPort {
  43. //TODO: IPV6-WORK this can be less crappy
  44. maskb := net.CIDRMask(c.mask.Bits(), c.mask.Addr().BitLen())
  45. mask := binary.BigEndian.Uint32(maskb[:])
  46. b := c.mask.Addr().As4()
  47. maskIp := binary.BigEndian.Uint32(b[:])
  48. b = ip.As4()
  49. intIp := binary.BigEndian.Uint32(b[:])
  50. return &Ip4AndPort{(maskIp & mask) | (intIp & ^mask), c.port}
  51. }
  52. func (c *calculatedRemote) apply6(ip netip.Addr) *Ip4AndPort {
  53. //TODO: IPV6-WORK
  54. panic("Can not calculate ipv6 remote addresses")
  55. }
  56. func NewCalculatedRemotesFromConfig(c *config.C, k string) (*bart.Table[[]*calculatedRemote], error) {
  57. value := c.Get(k)
  58. if value == nil {
  59. return nil, nil
  60. }
  61. calculatedRemotes := new(bart.Table[[]*calculatedRemote])
  62. rawMap, ok := value.(map[any]any)
  63. if !ok {
  64. return nil, fmt.Errorf("config `%s` has invalid type: %T", k, value)
  65. }
  66. for rawKey, rawValue := range rawMap {
  67. rawCIDR, ok := rawKey.(string)
  68. if !ok {
  69. return nil, fmt.Errorf("config `%s` has invalid key (type %T): %v", k, rawKey, rawKey)
  70. }
  71. cidr, err := netip.ParsePrefix(rawCIDR)
  72. if err != nil {
  73. return nil, fmt.Errorf("config `%s` has invalid CIDR: %s", k, rawCIDR)
  74. }
  75. //TODO: IPV6-WORK this does not verify that rawValue contains the same bits as cidr here
  76. entry, err := newCalculatedRemotesListFromConfig(rawValue)
  77. if err != nil {
  78. return nil, fmt.Errorf("config '%s.%s': %w", k, rawCIDR, err)
  79. }
  80. calculatedRemotes.Insert(cidr, entry)
  81. }
  82. return calculatedRemotes, nil
  83. }
  84. func newCalculatedRemotesListFromConfig(raw any) ([]*calculatedRemote, error) {
  85. rawList, ok := raw.([]any)
  86. if !ok {
  87. return nil, fmt.Errorf("calculated_remotes entry has invalid type: %T", raw)
  88. }
  89. var l []*calculatedRemote
  90. for _, e := range rawList {
  91. c, err := newCalculatedRemotesEntryFromConfig(e)
  92. if err != nil {
  93. return nil, fmt.Errorf("calculated_remotes entry: %w", err)
  94. }
  95. l = append(l, c)
  96. }
  97. return l, nil
  98. }
  99. func newCalculatedRemotesEntryFromConfig(raw any) (*calculatedRemote, error) {
  100. rawMap, ok := raw.(map[any]any)
  101. if !ok {
  102. return nil, fmt.Errorf("invalid type: %T", raw)
  103. }
  104. rawValue := rawMap["mask"]
  105. if rawValue == nil {
  106. return nil, fmt.Errorf("missing mask: %v", rawMap)
  107. }
  108. rawMask, ok := rawValue.(string)
  109. if !ok {
  110. return nil, fmt.Errorf("invalid mask (type %T): %v", rawValue, rawValue)
  111. }
  112. maskCidr, err := netip.ParsePrefix(rawMask)
  113. if err != nil {
  114. return nil, fmt.Errorf("invalid mask: %s", rawMask)
  115. }
  116. var port int
  117. rawValue = rawMap["port"]
  118. if rawValue == nil {
  119. return nil, fmt.Errorf("missing port: %v", rawMap)
  120. }
  121. switch v := rawValue.(type) {
  122. case int:
  123. port = v
  124. case string:
  125. port, err = strconv.Atoi(v)
  126. if err != nil {
  127. return nil, fmt.Errorf("invalid port: %s: %w", v, err)
  128. }
  129. default:
  130. return nil, fmt.Errorf("invalid port (type %T): %v", rawValue, rawValue)
  131. }
  132. return newCalculatedRemote(maskCidr, port)
  133. }