Debian Codegraph @

calculated_remote.go

raw
1 package nebula
2
3 import (
4 "encoding/binary"
5 "fmt"
6 "math"
7 "net"
8 "net/netip"
9 "strconv"
10
11 "github.com/gaissmai/bart"
12 "github.com/slackhq/nebula/config"
13 )
14
15 // This allows us to "guess" what the remote might be for a host while we wait
16 // for the lighthouse response. See "lighthouse.calculated_remotes" in the
17 // example config file.
18 type calculatedRemote struct {
19 ipNet netip.Prefix
20 mask netip.Prefix
21 port uint32
22 }
23
24 func newCalculatedRemote(cidr, maskCidr netip.Prefix, port int) (*calculatedRemote, error) {
25 if maskCidr.Addr().BitLen() != cidr.Addr().BitLen() {
26 return nil, fmt.Errorf("invalid mask: %s for cidr: %s", maskCidr, cidr)
27 }
28
29 masked := maskCidr.Masked()
30 if port < 0 || port > math.MaxUint16 {
31 return nil, fmt.Errorf("invalid port: %d", port)
32 }
33
34 return &calculatedRemote{
35 ipNet: maskCidr,
36 mask: masked,
37 port: uint32(port),
38 }, nil
39 }
40
41 func (c *calculatedRemote) String() string {
42 return fmt.Sprintf("CalculatedRemote(mask=%v port=%d)", c.ipNet, c.port)
43 }
44
45 func (c *calculatedRemote) ApplyV4(addr netip.Addr) *V4AddrPort {
46 // Combine the masked bytes of the "mask" IP with the unmasked bytes of the overlay IP
47 maskb := net.CIDRMask(c.mask.Bits(), c.mask.Addr().BitLen())
48 mask := binary.BigEndian.Uint32(maskb[:])
49
50 b := c.mask.Addr().As4()
51 maskAddr := binary.BigEndian.Uint32(b[:])
52
53 b = addr.As4()
54 intAddr := binary.BigEndian.Uint32(b[:])
55
56 return &V4AddrPort{(maskAddr & mask) | (intAddr & ^mask), c.port}
57 }
58
59 func (c *calculatedRemote) ApplyV6(addr netip.Addr) *V6AddrPort {
60 mask := net.CIDRMask(c.mask.Bits(), c.mask.Addr().BitLen())
61 maskAddr := c.mask.Addr().As16()
62 calcAddr := addr.As16()
63
64 ap := V6AddrPort{Port: c.port}
65
66 maskb := binary.BigEndian.Uint64(mask[:8])
67 maskAddrb := binary.BigEndian.Uint64(maskAddr[:8])
68 calcAddrb := binary.BigEndian.Uint64(calcAddr[:8])
69 ap.Hi = (maskAddrb & maskb) | (calcAddrb & ^maskb)
70
71 maskb = binary.BigEndian.Uint64(mask[8:])
72 maskAddrb = binary.BigEndian.Uint64(maskAddr[8:])
73 calcAddrb = binary.BigEndian.Uint64(calcAddr[8:])
74 ap.Lo = (maskAddrb & maskb) | (calcAddrb & ^maskb)
75
76 return &ap
77 }
78
79 func NewCalculatedRemotesFromConfig(c *config.C, k string) (*bart.Table[[]*calculatedRemote], error) {
80 value := c.Get(k)
81 if value == nil {
82 return nil, nil
83 }
84
85 calculatedRemotes := new(bart.Table[[]*calculatedRemote])
86
87 rawMap, ok := value.(map[string]any)
88 if !ok {
89 return nil, fmt.Errorf("config `%s` has invalid type: %T", k, value)
90 }
91 for rawCIDR, rawValue := range rawMap {
92 cidr, err := netip.ParsePrefix(rawCIDR)
93 if err != nil {
94 return nil, fmt.Errorf("config `%s` has invalid CIDR: %s", k, rawCIDR)
95 }
96
97 entry, err := newCalculatedRemotesListFromConfig(cidr, rawValue)
98 if err != nil {
99 return nil, fmt.Errorf("config '%s.%s': %w", k, rawCIDR, err)
100 }
101
102 calculatedRemotes.Insert(cidr, entry)
103 }
104
105 return calculatedRemotes, nil
106 }
107
108 func newCalculatedRemotesListFromConfig(cidr netip.Prefix, raw any) ([]*calculatedRemote, error) {
109 rawList, ok := raw.([]any)
110 if !ok {
111 return nil, fmt.Errorf("calculated_remotes entry has invalid type: %T", raw)
112 }
113
114 var l []*calculatedRemote
115 for _, e := range rawList {
116 c, err := newCalculatedRemotesEntryFromConfig(cidr, e)
117 if err != nil {
118 return nil, fmt.Errorf("calculated_remotes entry: %w", err)
119 }
120 l = append(l, c)
121 }
122
123 return l, nil
124 }
125
126 func newCalculatedRemotesEntryFromConfig(cidr netip.Prefix, raw any) (*calculatedRemote, error) {
127 rawMap, ok := raw.(map[string]any)
128 if !ok {
129 return nil, fmt.Errorf("invalid type: %T", raw)
130 }
131
132 rawValue := rawMap["mask"]
133 if rawValue == nil {
134 return nil, fmt.Errorf("missing mask: %v", rawMap)
135 }
136 rawMask, ok := rawValue.(string)
137 if !ok {
138 return nil, fmt.Errorf("invalid mask (type %T): %v", rawValue, rawValue)
139 }
140 maskCidr, err := netip.ParsePrefix(rawMask)
141 if err != nil {
142 return nil, fmt.Errorf("invalid mask: %s", rawMask)
143 }
144
145 var port int
146 rawValue = rawMap["port"]
147 if rawValue == nil {
148 return nil, fmt.Errorf("missing port: %v", rawMap)
149 }
150 switch v := rawValue.(type) {
151 case int:
152 port = v
153 case string:
154 port, err = strconv.Atoi(v)
155 if err != nil {
156 return nil, fmt.Errorf("invalid port: %s: %w", v, err)
157 }
158 default:
159 return nil, fmt.Errorf("invalid port (type %T): %v", rawValue, rawValue)
160 }
161
162 return newCalculatedRemote(cidr, maskCidr, port)
163 }