@@ -2,25 +2,24 @@ package nebula
2
2
3
3
import (
4
4
"fmt"
5
- "net"
5
+ "net/netip "
6
6
"regexp"
7
7
8
- "github.com/slackhq/nebula/cidr "
8
+ "github.com/gaissmai/bart "
9
9
"github.com/slackhq/nebula/config"
10
- "github.com/slackhq/nebula/iputil"
11
10
)
12
11
13
12
type AllowList struct {
14
13
// The values of this cidrTree are `bool`, signifying allow/deny
15
- cidrTree * cidr. Tree6 [bool ]
14
+ cidrTree * bart. Table [bool ]
16
15
}
17
16
18
17
type RemoteAllowList struct {
19
18
AllowList * AllowList
20
19
21
20
// Inside Range Specific, keys of this tree are inside CIDRs and values
22
21
// are *AllowList
23
- insideAllowLists * cidr. Tree6 [* AllowList ]
22
+ insideAllowLists * bart. Table [* AllowList ]
24
23
}
25
24
26
25
type LocalAllowList struct {
@@ -88,7 +87,7 @@ func newAllowList(k string, raw interface{}, handleKey func(key string, value in
88
87
return nil , fmt .Errorf ("config `%s` has invalid type: %T" , k , raw )
89
88
}
90
89
91
- tree := cidr . NewTree6 [bool ]( )
90
+ tree := new (bart. Table [bool ])
92
91
93
92
// Keep track of the rules we have added for both ipv4 and ipv6
94
93
type allowListRules struct {
@@ -122,18 +121,20 @@ func newAllowList(k string, raw interface{}, handleKey func(key string, value in
122
121
return nil , fmt .Errorf ("config `%s` has invalid value (type %T): %v" , k , rawValue , rawValue )
123
122
}
124
123
125
- _ , ipNet , err := net . ParseCIDR (rawCIDR )
124
+ ipNet , err := netip . ParsePrefix (rawCIDR )
126
125
if err != nil {
127
- return nil , fmt .Errorf ("config `%s` has invalid CIDR: %s" , k , rawCIDR )
126
+ return nil , fmt .Errorf ("config `%s` has invalid CIDR: %s. %w " , k , rawCIDR , err )
128
127
}
129
128
129
+ ipNet = netip .PrefixFrom (ipNet .Addr ().Unmap (), ipNet .Bits ())
130
+
130
131
// TODO: should we error on duplicate CIDRs in the config?
131
- tree .AddCIDR (ipNet , value )
132
+ tree .Insert (ipNet , value )
132
133
133
- maskBits , maskSize := ipNet .Mask . Size ()
134
+ maskBits := ipNet .Bits ()
134
135
135
136
var rules * allowListRules
136
- if maskSize == 32 {
137
+ if ipNet . Addr (). Is4 () {
137
138
rules = & rules4
138
139
} else {
139
140
rules = & rules6
@@ -156,17 +157,15 @@ func newAllowList(k string, raw interface{}, handleKey func(key string, value in
156
157
157
158
if ! rules4 .defaultSet {
158
159
if rules4 .allValuesMatch {
159
- _ , zeroCIDR , _ := net .ParseCIDR ("0.0.0.0/0" )
160
- tree .AddCIDR (zeroCIDR , ! rules4 .allValues )
160
+ tree .Insert (netip .PrefixFrom (netip .IPv4Unspecified (), 0 ), ! rules4 .allValues )
161
161
} else {
162
162
return nil , fmt .Errorf ("config `%s` contains both true and false rules, but no default set for 0.0.0.0/0" , k )
163
163
}
164
164
}
165
165
166
166
if ! rules6 .defaultSet {
167
167
if rules6 .allValuesMatch {
168
- _ , zeroCIDR , _ := net .ParseCIDR ("::/0" )
169
- tree .AddCIDR (zeroCIDR , ! rules6 .allValues )
168
+ tree .Insert (netip .PrefixFrom (netip .IPv6Unspecified (), 0 ), ! rules6 .allValues )
170
169
} else {
171
170
return nil , fmt .Errorf ("config `%s` contains both true and false rules, but no default set for ::/0" , k )
172
171
}
@@ -218,13 +217,13 @@ func getAllowListInterfaces(k string, v interface{}) ([]AllowListNameRule, error
218
217
return nameRules , nil
219
218
}
220
219
221
- func getRemoteAllowRanges (c * config.C , k string ) (* cidr. Tree6 [* AllowList ], error ) {
220
+ func getRemoteAllowRanges (c * config.C , k string ) (* bart. Table [* AllowList ], error ) {
222
221
value := c .Get (k )
223
222
if value == nil {
224
223
return nil , nil
225
224
}
226
225
227
- remoteAllowRanges := cidr . NewTree6 [* AllowList ]( )
226
+ remoteAllowRanges := new (bart. Table [* AllowList ])
228
227
229
228
rawMap , ok := value .(map [interface {}]interface {})
230
229
if ! ok {
@@ -241,45 +240,27 @@ func getRemoteAllowRanges(c *config.C, k string) (*cidr.Tree6[*AllowList], error
241
240
return nil , err
242
241
}
243
242
244
- _ , ipNet , err := net . ParseCIDR (rawCIDR )
243
+ ipNet , err := netip . ParsePrefix (rawCIDR )
245
244
if err != nil {
246
- return nil , fmt .Errorf ("config `%s` has invalid CIDR: %s" , k , rawCIDR )
245
+ return nil , fmt .Errorf ("config `%s` has invalid CIDR: %s. %w " , k , rawCIDR , err )
247
246
}
248
247
249
- remoteAllowRanges .AddCIDR ( ipNet , allowList )
248
+ remoteAllowRanges .Insert ( netip . PrefixFrom ( ipNet . Addr (). Unmap (), ipNet . Bits ()) , allowList )
250
249
}
251
250
252
251
return remoteAllowRanges , nil
253
252
}
254
253
255
- func (al * AllowList ) Allow (ip net.IP ) bool {
256
- if al == nil {
257
- return true
258
- }
259
-
260
- _ , result := al .cidrTree .MostSpecificContains (ip )
261
- return result
262
- }
263
-
264
- func (al * AllowList ) AllowIpV4 (ip iputil.VpnIp ) bool {
265
- if al == nil {
266
- return true
267
- }
268
-
269
- _ , result := al .cidrTree .MostSpecificContainsIpV4 (ip )
270
- return result
271
- }
272
-
273
- func (al * AllowList ) AllowIpV6 (hi , lo uint64 ) bool {
254
+ func (al * AllowList ) Allow (ip netip.Addr ) bool {
274
255
if al == nil {
275
256
return true
276
257
}
277
258
278
- _ , result := al .cidrTree .MostSpecificContainsIpV6 ( hi , lo )
259
+ result , _ := al .cidrTree .Lookup ( ip )
279
260
return result
280
261
}
281
262
282
- func (al * LocalAllowList ) Allow (ip net. IP ) bool {
263
+ func (al * LocalAllowList ) Allow (ip netip. Addr ) bool {
283
264
if al == nil {
284
265
return true
285
266
}
@@ -301,43 +282,23 @@ func (al *LocalAllowList) AllowName(name string) bool {
301
282
return ! al .nameRules [0 ].Allow
302
283
}
303
284
304
- func (al * RemoteAllowList ) AllowUnknownVpnIp (ip net. IP ) bool {
285
+ func (al * RemoteAllowList ) AllowUnknownVpnIp (ip netip. Addr ) bool {
305
286
if al == nil {
306
287
return true
307
288
}
308
289
return al .AllowList .Allow (ip )
309
290
}
310
291
311
- func (al * RemoteAllowList ) Allow (vpnIp iputil. VpnIp , ip net. IP ) bool {
292
+ func (al * RemoteAllowList ) Allow (vpnIp netip. Addr , ip netip. Addr ) bool {
312
293
if ! al .getInsideAllowList (vpnIp ).Allow (ip ) {
313
294
return false
314
295
}
315
296
return al .AllowList .Allow (ip )
316
297
}
317
298
318
- func (al * RemoteAllowList ) AllowIpV4 (vpnIp iputil.VpnIp , ip iputil.VpnIp ) bool {
319
- if al == nil {
320
- return true
321
- }
322
- if ! al .getInsideAllowList (vpnIp ).AllowIpV4 (ip ) {
323
- return false
324
- }
325
- return al .AllowList .AllowIpV4 (ip )
326
- }
327
-
328
- func (al * RemoteAllowList ) AllowIpV6 (vpnIp iputil.VpnIp , hi , lo uint64 ) bool {
329
- if al == nil {
330
- return true
331
- }
332
- if ! al .getInsideAllowList (vpnIp ).AllowIpV6 (hi , lo ) {
333
- return false
334
- }
335
- return al .AllowList .AllowIpV6 (hi , lo )
336
- }
337
-
338
- func (al * RemoteAllowList ) getInsideAllowList (vpnIp iputil.VpnIp ) * AllowList {
299
+ func (al * RemoteAllowList ) getInsideAllowList (vpnIp netip.Addr ) * AllowList {
339
300
if al .insideAllowLists != nil {
340
- ok , inside := al .insideAllowLists .MostSpecificContainsIpV4 (vpnIp )
301
+ inside , ok := al .insideAllowLists .Lookup (vpnIp )
341
302
if ok {
342
303
return inside
343
304
}
0 commit comments