/
ipcache.go
521 lines (457 loc) · 16.8 KB
/
ipcache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
// Copyright 2018-2019 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ipcache
import (
"fmt"
"net"
"github.com/cilium/cilium/pkg/identity"
"github.com/cilium/cilium/pkg/lock"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/option"
"github.com/cilium/cilium/pkg/source"
"github.com/sirupsen/logrus"
)
var (
// IPIdentityCache caches the mapping of endpoint IPs to their corresponding
// security identities across the entire cluster in which this instance of
// Cilium is running.
IPIdentityCache = NewIPCache()
)
// Identity is the identity representation of an IP<->Identity cache.
type Identity struct {
// ID is the numeric identity
ID identity.NumericIdentity
// Source is the source of the identity in the cache
Source source.Source
// shadowed determines if another entry overlaps with this one.
// Shadowed identities are not propagated to listeners by default.
// Most commonly set for Identity with Source = source.Generated when
// a pod IP (other source) has the same IP.
shadowed bool
}
// IPKeyPair is the (IP, key) pair used of the identity
type IPKeyPair struct {
IP net.IP
Key uint8
}
// IPCache is a collection of mappings:
// - mapping of endpoint IP or CIDR to security identities of all endpoints
// which are part of the same cluster, and vice-versa
// - mapping of endpoint IP or CIDR to host IP (maybe nil)
type IPCache struct {
mutex lock.SemaphoredMutex
ipToIdentityCache map[string]Identity
identityToIPCache map[identity.NumericIdentity]map[string]struct{}
ipToHostIPCache map[string]IPKeyPair
// prefixLengths reference-count the number of CIDRs that use
// particular prefix lengths for the mask.
v4PrefixLengths map[int]int
v6PrefixLengths map[int]int
listeners []IPIdentityMappingListener
}
// Implementation represents a concrete datapath implementation of the IPCache
// which may restrict the ability to apply IPCache mappings, depending on the
// underlying details of that implementation.
type Implementation interface {
GetMaxPrefixLengths(ipv6 bool) int
}
// NewIPCache returns a new IPCache with the mappings of endpoint IP to security
// identity (and vice-versa) initialized.
func NewIPCache() *IPCache {
return &IPCache{
mutex: lock.NewSemaphoredMutex(),
ipToIdentityCache: map[string]Identity{},
identityToIPCache: map[identity.NumericIdentity]map[string]struct{}{},
ipToHostIPCache: map[string]IPKeyPair{},
v4PrefixLengths: map[int]int{},
v6PrefixLengths: map[int]int{},
}
}
// Lock locks the IPCache's mutex.
func (ipc *IPCache) Lock() {
ipc.mutex.Lock()
}
// Unlock unlocks the IPCache's mutex.
func (ipc *IPCache) Unlock() {
ipc.mutex.Unlock()
}
// RLock RLocks the IPCache's mutex.
func (ipc *IPCache) RLock() {
ipc.mutex.RLock()
}
// RUnlock RUnlocks the IPCache's mutex.
func (ipc *IPCache) RUnlock() {
ipc.mutex.RUnlock()
}
// SetListeners sets the listeners for this IPCache.
func (ipc *IPCache) SetListeners(listeners []IPIdentityMappingListener) {
ipc.mutex.Lock()
ipc.listeners = listeners
ipc.mutex.Unlock()
}
// AddListener adds a listener for this IPCache.
func (ipc *IPCache) AddListener(listener IPIdentityMappingListener) {
// We need to acquire the semaphored mutex as we Write Lock as we are
// modifying the listeners slice.
ipc.mutex.Lock()
ipc.listeners = append(ipc.listeners, listener)
// We will release the semaphore mutex with UnlockToRLock, *and not Unlock*
// because want to prevent a race across an Upsert or Delete. By doing this
// we are sure no other writers are performing any operation while we are
// still reading.
ipc.mutex.UnlockToRLock()
defer ipc.mutex.RUnlock()
// Initialize new listener with the current mappings
ipc.DumpToListenerLocked(listener)
}
func checkPrefixLengthsAgainstMap(impl Implementation, prefixes []*net.IPNet, existingPrefixes map[int]int, isIPv6 bool) error {
prefixLengths := make(map[int]struct{})
for i := range existingPrefixes {
prefixLengths[i] = struct{}{}
}
for _, prefix := range prefixes {
ones, bits := prefix.Mask.Size()
if _, ok := prefixLengths[ones]; !ok {
if bits == net.IPv6len*8 && isIPv6 || bits == net.IPv4len*8 && !isIPv6 {
prefixLengths[ones] = struct{}{}
}
}
}
maxPrefixLengths := impl.GetMaxPrefixLengths(isIPv6)
if len(prefixLengths) > maxPrefixLengths {
existingPrefixLengths := len(existingPrefixes)
return fmt.Errorf("adding specified CIDR prefixes would result in too many prefix lengths (current: %d, result: %d, max: %d)",
existingPrefixLengths, len(prefixLengths), maxPrefixLengths)
}
return nil
}
// checkPrefixes ensures that we will reject rules if the import of those
// rules would cause the underlying implementation of the ipcache to exceed
// the maximum number of supported CIDR prefix lengths.
func checkPrefixes(impl Implementation, prefixes []*net.IPNet) (err error) {
IPIdentityCache.RLock()
defer IPIdentityCache.RUnlock()
if err = checkPrefixLengthsAgainstMap(impl, prefixes, IPIdentityCache.v4PrefixLengths, false); err != nil {
return
}
return checkPrefixLengthsAgainstMap(impl, prefixes, IPIdentityCache.v6PrefixLengths, true)
}
// refPrefixLength adds one reference to the prefix length in the map.
func refPrefixLength(prefixLengths map[int]int, length int) {
if _, ok := prefixLengths[length]; ok {
prefixLengths[length]++
} else {
prefixLengths[length] = 1
}
}
// refPrefixLength removes one reference from the prefix length in the map.
func unrefPrefixLength(prefixLengths map[int]int, length int) {
value := prefixLengths[length]
if value <= 1 {
delete(prefixLengths, length)
} else {
prefixLengths[length]--
}
}
// endpointIPToCIDR converts the endpoint IP into an equivalent full CIDR.
func endpointIPToCIDR(ip net.IP) *net.IPNet {
bits := net.IPv6len * 8
if ip.To4() != nil {
bits = net.IPv4len * 8
}
return &net.IPNet{
IP: ip,
Mask: net.CIDRMask(bits, bits),
}
}
func (ipc *IPCache) getHostIPCache(ip string) (net.IP, uint8) {
ipKeyPair := ipc.ipToHostIPCache[ip]
return ipKeyPair.IP, ipKeyPair.Key
}
// Upsert adds / updates the provided IP (endpoint or CIDR prefix) and identity
// into the IPCache.
//
// Returns false if the entry is not owned by the self declared source, i.e.
// returns false if the kubernetes layer is trying to upsert an entry now
// managed by the kvstore layer. See source.AllowOverwrite() for rules on
// ownership. hostIP is the location of the given IP. It is optional (may be
// nil) and is propagated to the listeners.
func (ipc *IPCache) Upsert(ip string, hostIP net.IP, hostKey uint8, newIdentity Identity) bool {
scopedLog := log
if option.Config.Debug {
scopedLog = log.WithFields(logrus.Fields{
logfields.IPAddr: ip,
logfields.Identity: newIdentity,
logfields.Key: hostKey,
})
}
ipc.mutex.Lock()
defer ipc.mutex.Unlock()
var cidr *net.IPNet
var oldIdentity *identity.NumericIdentity
callbackListeners := true
oldHostIP, oldHostKey := ipc.getHostIPCache(ip)
cachedIdentity, found := ipc.ipToIdentityCache[ip]
if found {
if !source.AllowOverwrite(cachedIdentity.Source, newIdentity.Source) {
return false
}
// Skip update if IP is already mapped to the given identity
// and the host IP hasn't changed.
if cachedIdentity == newIdentity && oldHostIP.Equal(hostIP) && hostKey == oldHostKey {
return true
}
oldIdentity = &cachedIdentity.ID
}
// Endpoint IP identities take precedence over CIDR identities, so if the
// IP is a full CIDR prefix and there's an existing equivalent endpoint IP,
// don't notify the listeners.
var err error
if _, cidr, err = net.ParseCIDR(ip); err == nil {
// Add a reference for the prefix length if this is a CIDR.
pl, bits := cidr.Mask.Size()
switch bits {
case net.IPv6len * 8:
refPrefixLength(ipc.v6PrefixLengths, pl)
case net.IPv4len * 8:
refPrefixLength(ipc.v4PrefixLengths, pl)
}
ones, bits := cidr.Mask.Size()
if ones == bits {
if _, endpointIPFound := ipc.ipToIdentityCache[cidr.IP.String()]; endpointIPFound {
scopedLog.Debug("Ignoring CIDR to identity mapping as it is shadowed by an endpoint IP")
// Skip calling back the listeners, since the endpoint IP has
// precedence over the new CIDR.
newIdentity.shadowed = true
}
}
} else if endpointIP := net.ParseIP(ip); endpointIP != nil { // Endpoint IP.
cidr = endpointIPToCIDR(endpointIP)
// Check whether the upserted endpoint IP will shadow that CIDR, and
// replace its mapping with the listeners if that was the case.
if !found {
cidrStr := cidr.String()
if cidrIdentity, cidrFound := ipc.ipToIdentityCache[cidrStr]; cidrFound {
oldHostIP, _ = ipc.getHostIPCache(cidrStr)
if cidrIdentity.ID != newIdentity.ID || !oldHostIP.Equal(hostIP) {
scopedLog.Debug("New endpoint IP started shadowing existing CIDR to identity mapping")
cidrIdentity.shadowed = true
ipc.ipToIdentityCache[cidrStr] = cidrIdentity
oldIdentity = &cidrIdentity.ID
} else {
// The endpoint IP and the CIDR are associated with the
// same identity and host IP. Nothing changes for the
// listeners.
callbackListeners = false
}
}
}
} else {
log.WithFields(logrus.Fields{
logfields.IPAddr: ip,
logfields.Identity: newIdentity,
logfields.Key: hostKey,
}).Error("Attempt to upsert invalid IP into ipcache layer")
return false
}
scopedLog.Debug("Upserting IP into ipcache layer")
// Update both maps.
ipc.ipToIdentityCache[ip] = newIdentity
// Delete the old identity, if any.
if found {
delete(ipc.identityToIPCache[cachedIdentity.ID], ip)
if len(ipc.identityToIPCache[cachedIdentity.ID]) == 0 {
delete(ipc.identityToIPCache, cachedIdentity.ID)
}
}
if _, ok := ipc.identityToIPCache[newIdentity.ID]; !ok {
ipc.identityToIPCache[newIdentity.ID] = map[string]struct{}{}
}
ipc.identityToIPCache[newIdentity.ID][ip] = struct{}{}
if hostIP == nil {
delete(ipc.ipToHostIPCache, ip)
} else {
ipc.ipToHostIPCache[ip] = IPKeyPair{IP: hostIP, Key: hostKey}
}
if callbackListeners && !newIdentity.shadowed {
for _, listener := range ipc.listeners {
listener.OnIPIdentityCacheChange(Upsert, *cidr, oldHostIP, hostIP, oldIdentity, newIdentity.ID, hostKey)
}
}
return true
}
// DumpToListenerLocked dumps the entire contents of the IPCache by triggering
// the listener's "OnIPIdentityCacheChange" method for each entry in the cache.
func (ipc *IPCache) DumpToListenerLocked(listener IPIdentityMappingListener) {
for ip, identity := range ipc.ipToIdentityCache {
if identity.shadowed {
continue
}
hostIP, encryptKey := ipc.getHostIPCache(ip)
_, cidr, err := net.ParseCIDR(ip)
if err != nil {
endpointIP := net.ParseIP(ip)
cidr = endpointIPToCIDR(endpointIP)
}
listener.OnIPIdentityCacheChange(Upsert, *cidr, nil, hostIP, nil, identity.ID, encryptKey)
}
}
// deleteLocked removes the provided IP-to-security-identity mapping
// from ipc with the assumption that the IPCache's mutex is held.
func (ipc *IPCache) deleteLocked(ip string, source source.Source) {
scopedLog := log.WithFields(logrus.Fields{
logfields.IPAddr: ip,
})
cachedIdentity, found := ipc.ipToIdentityCache[ip]
if !found {
scopedLog.Debug("Attempt to remove non-existing IP from ipcache layer")
return
}
if cachedIdentity.Source != source {
scopedLog.WithField("source", cachedIdentity.Source).
Debugf("Skipping delete of identity from source %s", source)
return
}
var cidr *net.IPNet
cacheModification := Delete
oldHostIP, encryptKey := ipc.getHostIPCache(ip)
var newHostIP net.IP
var oldIdentity *identity.NumericIdentity
newIdentity := cachedIdentity
callbackListeners := true
var err error
if _, cidr, err = net.ParseCIDR(ip); err == nil {
// Remove a reference for the prefix length if this is a CIDR.
pl, bits := cidr.Mask.Size()
switch bits {
case net.IPv6len * 8:
unrefPrefixLength(ipc.v6PrefixLengths, pl)
case net.IPv4len * 8:
unrefPrefixLength(ipc.v4PrefixLengths, pl)
}
// Check whether the deleted CIDR was shadowed by an endpoint IP. In
// this case, skip calling back the listeners since they don't know
// about its mapping.
if _, endpointIPFound := ipc.ipToIdentityCache[cidr.IP.String()]; endpointIPFound {
scopedLog.Debug("Deleting CIDR shadowed by endpoint IP")
callbackListeners = false
}
} else if endpointIP := net.ParseIP(ip); endpointIP != nil { // Endpoint IP.
// Convert the endpoint IP into an equivalent full CIDR.
bits := net.IPv6len * 8
if endpointIP.To4() != nil {
bits = net.IPv4len * 8
}
cidr = &net.IPNet{
IP: endpointIP,
Mask: net.CIDRMask(bits, bits),
}
// Check whether the deleted endpoint IP was shadowing that CIDR, and
// restore its mapping with the listeners if that was the case.
cidrStr := cidr.String()
if cidrIdentity, cidrFound := ipc.ipToIdentityCache[cidrStr]; cidrFound {
newHostIP, _ = ipc.getHostIPCache(cidrStr)
if cidrIdentity.ID != cachedIdentity.ID || !oldHostIP.Equal(newHostIP) {
scopedLog.Debug("Removal of endpoint IP revives shadowed CIDR to identity mapping")
cacheModification = Upsert
cidrIdentity.shadowed = false
ipc.ipToIdentityCache[cidrStr] = cidrIdentity
oldIdentity = &cachedIdentity.ID
newIdentity = cidrIdentity
} else {
// The endpoint IP and the CIDR were associated with the same
// identity and host IP. Nothing changes for the listeners.
callbackListeners = false
}
}
} else {
scopedLog.Error("Attempt to delete invalid IP from ipcache layer")
return
}
scopedLog.Debug("Deleting IP from ipcache layer")
delete(ipc.ipToIdentityCache, ip)
delete(ipc.identityToIPCache[cachedIdentity.ID], ip)
if len(ipc.identityToIPCache[cachedIdentity.ID]) == 0 {
delete(ipc.identityToIPCache, cachedIdentity.ID)
}
delete(ipc.ipToHostIPCache, ip)
if callbackListeners {
for _, listener := range ipc.listeners {
listener.OnIPIdentityCacheChange(cacheModification, *cidr, oldHostIP, newHostIP,
oldIdentity, newIdentity.ID, encryptKey)
}
}
}
// Delete removes the provided IP-to-security-identity mapping from the IPCache.
func (ipc *IPCache) Delete(IP string, source source.Source) {
ipc.mutex.Lock()
defer ipc.mutex.Unlock()
ipc.deleteLocked(IP, source)
}
// LookupByIP returns the corresponding security identity that endpoint IP maps
// to within the provided IPCache, as well as if the corresponding entry exists
// in the IPCache.
func (ipc *IPCache) LookupByIP(IP string) (Identity, bool) {
ipc.mutex.RLock()
defer ipc.mutex.RUnlock()
return ipc.LookupByIPRLocked(IP)
}
// LookupByIPRLocked returns the corresponding security identity that endpoint IP maps
// to within the provided IPCache, as well as if the corresponding entry exists
// in the IPCache.
func (ipc *IPCache) LookupByIPRLocked(IP string) (Identity, bool) {
identity, exists := ipc.ipToIdentityCache[IP]
return identity, exists
}
// LookupByPrefixRLocked looks for either the specified CIDR prefix, or if the
// prefix is fully specified (ie, w.x.y.z/32 for IPv4), find the host for the
// identity in the provided IPCache, and returns the corresponding security
// identity as well as whether the entry exists in the IPCache.
func (ipc *IPCache) LookupByPrefixRLocked(prefix string) (identity Identity, exists bool) {
if _, cidr, err := net.ParseCIDR(prefix); err == nil {
// If it's a fully specfied prefix, attempt to find the host
ones, bits := cidr.Mask.Size()
if ones == bits {
identity, exists = ipc.ipToIdentityCache[cidr.IP.String()]
if exists {
return
}
}
}
identity, exists = ipc.ipToIdentityCache[prefix]
return
}
// LookupByPrefix returns the corresponding security identity that endpoint IP
// maps to within the provided IPCache, as well as if the corresponding entry
// exists in the IPCache.
func (ipc *IPCache) LookupByPrefix(IP string) (Identity, bool) {
ipc.mutex.RLock()
defer ipc.mutex.RUnlock()
return ipc.LookupByPrefixRLocked(IP)
}
// LookupByIdentity returns the set of IPs (endpoint or CIDR prefix) that have
// security identity ID, as well as whether the corresponding entry exists in
// the IPCache.
func (ipc *IPCache) LookupByIdentity(id identity.NumericIdentity) (map[string]struct{}, bool) {
ipc.mutex.RLock()
defer ipc.mutex.RUnlock()
ips, exists := ipc.identityToIPCache[id]
return ips, exists
}
// GetIPIdentityMapModel returns all known endpoint IP to security identity mappings
// stored in the key-value store.
func GetIPIdentityMapModel() {
// TODO (ianvernon) return model of ip to identity mapping. For use in CLI.
// see GH-2555
}