This repository has been archived by the owner on Aug 3, 2020. It is now read-only.
forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_resync.go
292 lines (276 loc) · 11.3 KB
/
data_resync.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
// Copyright (c) 2018 Cisco and/or its affiliates.
//
// 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 ipsecplugin
import (
"github.com/go-errors/errors"
"github.com/ligato/vpp-agent/plugins/vpp/ipsecplugin/vppcalls"
"github.com/ligato/vpp-agent/plugins/vpp/model/interfaces"
"github.com/ligato/vpp-agent/plugins/vpp/model/ipsec"
)
// IP address of unset field
const defaultIP = "0.0.0.0"
// Resync writes missing IPSec configs to the VPP and removes obsolete ones.
func (c *IPSecConfigurator) Resync(nbSpds []*ipsec.SecurityPolicyDatabases_SPD, nbSas []*ipsec.SecurityAssociations_SA, nbTunnels []*ipsec.TunnelInterfaces_Tunnel) error {
c.clearMapping()
// Read VPP IPSec data
vppSas, err := c.ipSecHandler.DumpIPSecSA()
if err != nil {
return errors.Errorf("IPSec resync: failed to dump security associations: %v", err)
}
vppSpds, err := c.ipSecHandler.DumpIPSecSPD()
if err != nil {
return errors.Errorf("IPSec resync: failed to dump security policy databases: %v", err)
}
vppTunnels, err := c.ipSecHandler.DumpIPSecTunnelInterfaces()
if err != nil {
return errors.Errorf("IPSec resync: failed to dump tunnel interfaces: %v", err)
}
// Remove all security policy databases before manipulating security associations.
// TODO since IPSec interface dump is missing, all SPDs will be removed since diff cannot be calculated
for _, vppSpdDetails := range vppSpds {
// First register all policy entries
vppSpd := vppSpdDetails.Spd
for _, spdPolicyEntry := range vppSpdDetails.Spd.PolicyEntries {
// Find ID for given policy
meta, ok := vppSpdDetails.PolicyMeta[spdPolicyEntry.Sa]
if !ok {
c.log.Warnf("Metadata for SPD gen name %s not found", spdPolicyEntry.Sa)
continue
}
vppSpd.Name = "<unknown>"
c.spdIndexes.RegisterName(vppSpd.Name, meta.SpdID, nil)
}
if err := c.DeleteSPD(vppSpd); err != nil {
return errors.Errorf("IPSec resync: failed to remove VPP SPD (sp: %s): %v", vppSpd.Name, err)
}
c.log.Debugf("IPSec resync: removed VPP SPD %s", vppSpd.Name)
}
// Resolve security associations
if err := c.synchronizeSA(vppSas, nbSas); err != nil {
return err
}
// Configure all NB SPDs
for _, nbSpd := range nbSpds {
if err := c.ConfigureSPD(nbSpd); err != nil {
return errors.Errorf("IPSec resync: failed to configure VPP SPD: %v", err)
}
c.log.Debugf("IPSec resync: configured VPP SPD %s", nbSpd.Name)
}
// Sync tunnel interfaces
resolvedVppIfs := make(map[uint32]string)
for _, nbTun := range nbTunnels {
var correlated bool
for _, vppTun := range vppTunnels {
if nbTun.Name == vppTun.Tunnel.Name {
if c.isIfModified(nbTun, vppTun.Tunnel) {
if err := c.ModifyTunnel(vppTun.Tunnel, nbTun); err != nil {
return errors.Errorf("IPSec resync: failed to modify tunnel interface %s: %v", nbTun.Name, err)
}
} else {
// Update metadata for tunnel interface. Tunnel was registered during interface plugin resync.
c.ifIndexes.UpdateMetadata(nbTun.Name, &interfaces.Interfaces_Interface{
Name: nbTun.Name,
Enabled: nbTun.Enabled,
IpAddresses: nbTun.IpAddresses,
Vrf: nbTun.Vrf,
})
}
correlated = true
resolvedVppIfs[vppTun.Meta.SwIfIndex] = vppTun.Tunnel.Name
}
}
if !correlated {
if err := c.ConfigureTunnel(nbTun); err != nil {
return errors.Errorf("IPSec resync: failed to configure tunnel interface %s: %v", nbTun.Name, err)
}
}
}
// Remove obsolete tunnels
for _, vppTun := range vppTunnels {
_, ok := resolvedVppIfs[vppTun.Meta.SwIfIndex]
if !ok {
if err := c.DeleteTunnel(vppTun.Tunnel); err != nil {
return errors.Errorf("IPSec resync: failed to remove tunnel interface %s: %v", vppTun.Tunnel.Name, err)
}
}
}
c.log.Debug("IPSec resync done")
return nil
}
// Correlates security associations, removes obsolete and configures new ones
func (c *IPSecConfigurator) synchronizeSA(vppSAs []*vppcalls.IPSecSaDetails, nbSAs []*ipsec.SecurityAssociations_SA) error {
for _, nbSa := range nbSAs {
var found bool
c.log.Debugf("looking for SA %s in the VPP", nbSa.Name)
// Look for VPP security association
for _, vppSaDetails := range vppSAs {
vppSa := vppSaDetails.Sa
if nbSa.GetSpi() != vppSa.GetSpi() {
c.log.Debugf("SA comparison: different SPI (nb: %d vs vpp: %d)", nbSa.GetSpi(), vppSa.GetSpi())
continue
}
if nbSa.GetCryptoKey() != vppSa.GetCryptoKey() {
c.log.Debugf("SA comparison: different crypto key (nb: %s vs vpp: %s)", nbSa.GetCryptoKey(), vppSa.GetCryptoKey())
continue
}
if nbSa.GetCryptoAlg() != vppSa.GetCryptoAlg() {
c.log.Debugf("SA comparison: different crypto alg (nb: %v vs vpp: %v)", nbSa.GetEnableUdpEncap(), vppSa.GetEnableUdpEncap())
continue
}
if nbSa.GetIntegKey() != vppSa.GetIntegKey() {
c.log.Debugf("SA comparison: different integ key (nb: %s vs vpp: %s)", nbSa.GetIntegKey(), vppSa.GetIntegKey())
continue
}
if nbSa.GetIntegAlg() != vppSa.GetIntegAlg() {
c.log.Debugf("SA comparison: different integ alg (nb: %d vs vpp: %d)", nbSa.GetIntegAlg(), vppSa.GetIntegAlg())
continue
}
if nbSa.GetTunnelSrcAddr() == "" && vppSa.GetTunnelSrcAddr() != defaultIP {
c.log.Debugf("SA comparison: tunnel src IP not set for nb, but is %s for vpp)", vppSa.GetTunnelSrcAddr())
continue
} else if nbSa.GetTunnelSrcAddr() != "" && nbSa.GetTunnelSrcAddr() != vppSa.GetTunnelSrcAddr() {
c.log.Debugf("SA comparison: different tunnel src IP (nb: %s vs vpp: %s)", nbSa.GetTunnelSrcAddr(), vppSa.GetTunnelSrcAddr())
continue
}
if nbSa.GetTunnelDstAddr() == "" && vppSa.GetTunnelDstAddr() != defaultIP {
c.log.Debugf("SA comparison: tunnel dst IP not set for nb, but is %s for vpp)", vppSa.GetTunnelSrcAddr())
continue
} else if nbSa.GetTunnelDstAddr() != "" && nbSa.GetTunnelDstAddr() != vppSa.GetTunnelDstAddr() {
c.log.Debugf("SA comparison: different tunnel dst IP (nb: %s vs vpp: %s)", nbSa.GetTunnelDstAddr(), vppSa.GetTunnelDstAddr())
continue
}
if nbSa.GetUseAntiReplay() != vppSa.GetUseAntiReplay() {
c.log.Debugf("SA comparison: different use anti replay (nb: %v vs vpp: %v)", nbSa.GetUseAntiReplay(), vppSa.GetTunnelDstAddr())
continue
}
if nbSa.GetUseEsn() != vppSa.GetUseEsn() {
c.log.Debugf("SA comparison: different use ESN (nb: %v vs vpp: %v)", nbSa.GetUseEsn(), vppSa.GetUseEsn())
continue
}
if nbSa.GetEnableUdpEncap() != vppSa.GetEnableUdpEncap() {
c.log.Debugf("SA comparison: different enable UDP encap (nb: %v vs vpp: %v)", nbSa.GetEnableUdpEncap(), vppSa.GetEnableUdpEncap())
continue
}
if nbSa.GetProtocol() != vppSa.GetProtocol() {
c.log.Debugf("SA comparison: different protocol (nb: %d vs vpp: %d)", nbSa.GetProtocol(), vppSa.GetProtocol())
continue
}
found = true
vppSa.Name = nbSa.Name // So it can be identified
break
}
if !found {
if err := c.ConfigureSA(nbSa); err != nil {
return errors.Errorf("IPSec resync: failed to configure VPP SA %s: %v", nbSa.Name, err)
}
c.log.Debugf("IPSec resync: configured VPP SA %s", nbSa.Name)
} else {
c.saIndexes.RegisterName(nbSa.Name, c.saIndexSeq, nil)
c.saIndexSeq++
c.log.Debugf("SA %s registered without additional changes", nbSa.Name)
}
}
for _, vppSaDetails := range vppSAs {
vppSa := vppSaDetails.Sa
// Remove all without name
if vppSa.Name == "" {
vppSa.Name = "<unknown>"
c.saIndexes.RegisterName(vppSa.Name, vppSaDetails.Meta.SaID, nil)
if err := c.DeleteSA(vppSa); err != nil {
return errors.Errorf("IPSec resync: failed to remove VPP SA (sp: %d): %v", vppSa.Spi, err)
}
c.log.Debugf("IPSec resync: removed VPP SA (spi: %d)", vppSa.Spi)
}
}
return nil
}
// Returns true if provided tunnel interfaces are different, false if they are equal
func (c *IPSecConfigurator) isIfModified(vppTun, nbTun *ipsec.TunnelInterfaces_Tunnel) bool {
if nbTun.GetName() != vppTun.GetName() {
c.log.Debugf("Tunnel comparison: different name (nb: %s vs vpp: %s)", nbTun.GetName(), vppTun.GetName())
return true
}
if nbTun.GetEsn() != vppTun.GetEsn() {
c.log.Debugf("Tunnel comparison: different esn (nb: %v vs vpp: %v)", nbTun.GetEsn(), vppTun.GetEsn())
return true
}
if nbTun.GetAntiReplay() != vppTun.GetAntiReplay() {
c.log.Debugf("Tunnel comparison: different anti replay (nb: %v vs vpp: %v)", nbTun.GetAntiReplay(), vppTun.GetAntiReplay())
return true
}
if c.localRemoteIPSpiIsDifferent(nbTun, vppTun) {
return true
}
if nbTun.GetCryptoAlg() != vppTun.GetCryptoAlg() {
c.log.Debugf("Tunnel comparison: different crypto alg (nb: %v vs vpp: %v)", nbTun.GetCryptoAlg(), vppTun.GetCryptoAlg())
return true
}
if nbTun.GetIntegAlg() != vppTun.GetIntegAlg() {
c.log.Debugf("Tunnel comparison: different integ alg (nb: %v vs vpp: %v)", nbTun.GetIntegAlg(), vppTun.GetIntegAlg())
return true
}
if len(nbTun.GetIpAddresses()) != len(vppTun.GetIpAddresses()) {
c.log.Debugf("Tunnel comparison: different IP address count (nb: %v vs vpp: %v)", len(nbTun.GetIpAddresses()), len(vppTun.GetIpAddresses()))
return true
}
if c.ipAddressesAreDifferent(nbTun.GetIpAddresses(), vppTun.GetIpAddresses()) {
return true
}
if nbTun.GetVrf() != vppTun.GetVrf() {
c.log.Debugf("SA comparison: different protocol (nb: %d vs vpp: %d)", nbTun.GetVrf(), vppTun.GetVrf())
return true
}
return false
}
func (c *IPSecConfigurator) localRemoteIPSpiIsDifferent(nbTun, vppTun *ipsec.TunnelInterfaces_Tunnel) bool {
if nbTun.GetLocalIp() == vppTun.GetLocalIp() && nbTun.GetLocalSpi() == vppTun.GetLocalSpi() {
if nbTun.GetRemoteIp() != vppTun.GetRemoteIp() {
c.log.Debugf("Tunnel comparison: different remote IP (nb: %s vs vpp: %s)", nbTun.GetRemoteIp(), vppTun.GetRemoteIp())
return true
}
if nbTun.GetRemoteSpi() != vppTun.GetRemoteSpi() {
c.log.Debugf("Tunnel comparison: different remote spi (nb: %d vs vpp: %d)", nbTun.GetRemoteSpi(), vppTun.GetRemoteSpi())
return true
}
} else if nbTun.GetLocalIp() == vppTun.GetRemoteIp() && nbTun.GetLocalSpi() == vppTun.GetRemoteSpi() {
if nbTun.GetRemoteIp() != vppTun.GetLocalIp() {
c.log.Debugf("Tunnel comparison: different remote IP (nb: %s vs vpp: %s)", nbTun.GetRemoteIp(), vppTun.GetRemoteIp())
return true
}
if nbTun.GetRemoteSpi() != vppTun.GetLocalSpi() {
c.log.Debugf("Tunnel comparison: different remote spi (nb: %d vs vpp: %d)", nbTun.GetRemoteSpi(), vppTun.GetRemoteSpi())
return true
}
} else {
c.log.Debugf("Tunnel comparison: different local ip/spi (nb: %s/%d vs vpp: %s/%d)",
nbTun.GetLocalIp(), nbTun.GetLocalSpi(), vppTun.GetLocalIp(), vppTun.GetLocalSpi())
return true
}
return false
}
func (c *IPSecConfigurator) ipAddressesAreDifferent(nbIPs, vppIPs []string) bool {
for _, nbIP := range nbIPs {
var found bool
for _, vppIP := range vppIPs {
if nbIP == vppIP {
found = true
}
}
if !found {
c.log.Debugf("SA comparison: IP address %s is missing on vpp tunnel", nbIP)
return true
}
}
return false
}