forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump_nat_vppcalls.go
572 lines (503 loc) · 16.2 KB
/
dump_nat_vppcalls.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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
// Copyright (c) 2019 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 vpp2001_379
import (
"fmt"
"net"
"sort"
"strings"
"github.com/gogo/protobuf/proto"
ifs "github.com/ligato/vpp-agent/api/models/vpp/interfaces"
nat "github.com/ligato/vpp-agent/api/models/vpp/nat"
vpp_nat "github.com/ligato/vpp-agent/plugins/vpp/binapi/vpp2001_379/nat"
"github.com/ligato/vpp-agent/plugins/vpp/ifplugin/ifaceidx"
)
// DNATs sorted by tags
type dnatMap map[string]*nat.DNat44
// static mappings sorted by tags
type stMappingMap map[string][]*nat.DNat44_StaticMapping
// identity mappings sorted by tags
type idMappingMap map[string][]*nat.DNat44_IdentityMapping
// Nat44GlobalConfigDump dumps global NAT44 config in NB format.
func (h *NatVppHandler) Nat44GlobalConfigDump() (*nat.Nat44Global, error) {
isEnabled, err := h.isNat44ForwardingEnabled()
if err != nil {
return nil, err
}
natInterfaces, err := h.nat44InterfaceDump()
if err != nil {
return nil, err
}
natAddressPool, err := h.nat44AddressDump()
if err != nil {
return nil, err
}
vrIPv4, _, err := h.virtualReassemblyDump()
if err != nil {
return nil, err
}
// combine into the global NAT configuration
return &nat.Nat44Global{
Forwarding: isEnabled,
NatInterfaces: natInterfaces,
AddressPool: natAddressPool,
VirtualReassembly: vrIPv4,
}, nil
}
// DNat44Dump dumps all configured DNAT-44 configurations ordered by label.
func (h *NatVppHandler) DNat44Dump() (dnats []*nat.DNat44, err error) {
dnatMap := make(dnatMap)
// Static mappings
natStMappings, err := h.nat44StaticMappingDump()
if err != nil {
return nil, fmt.Errorf("failed to dump NAT44 static mappings: %v", err)
}
for label, mappings := range natStMappings {
dnat := getOrCreateDNAT(dnatMap, label)
dnat.StMappings = append(dnat.StMappings, mappings...)
}
// Static mappings with load balancer
natStLbMappings, err := h.nat44StaticMappingLbDump()
if err != nil {
return nil, fmt.Errorf("failed to dump NAT44 static mappings with load balancer: %v", err)
}
for label, mappings := range natStLbMappings {
dnat := getOrCreateDNAT(dnatMap, label)
dnat.StMappings = append(dnat.StMappings, mappings...)
}
// Identity mappings
natIDMappings, err := h.nat44IdentityMappingDump()
if err != nil {
return nil, fmt.Errorf("failed to dump NAT44 identity mappings: %v", err)
}
for label, mappings := range natIDMappings {
dnat := getOrCreateDNAT(dnatMap, label)
dnat.IdMappings = append(dnat.IdMappings, mappings...)
}
// Convert map of DNAT configurations into a list.
for _, dnat := range dnatMap {
dnats = append(dnats, dnat)
}
// sort to simplify testing
sort.Slice(dnats, func(i, j int) bool { return dnats[i].Label < dnats[j].Label })
return dnats, nil
}
// nat44AddressDump returns NAT44 address pool configured in the VPP.
func (h *NatVppHandler) nat44AddressDump() (addressPool []*nat.Nat44Global_Address, err error) {
req := &vpp_nat.Nat44AddressDump{}
reqContext := h.callsChannel.SendMultiRequest(req)
for {
msg := &vpp_nat.Nat44AddressDetails{}
stop, err := reqContext.ReceiveReply(msg)
if err != nil {
return nil, fmt.Errorf("failed to dump NAT44 Address pool: %v", err)
}
if stop {
break
}
addressPool = append(addressPool, &nat.Nat44Global_Address{
Address: net.IP(msg.IPAddress[:]).String(),
VrfId: msg.VrfID,
TwiceNat: getNat44Flags(msg.Flags).isTwiceNat,
})
}
return
}
// virtualReassemblyDump returns current NAT virtual-reassembly configuration.
func (h *NatVppHandler) virtualReassemblyDump() (vrIPv4 *nat.VirtualReassembly, vrIPv6 *nat.VirtualReassembly, err error) {
req := &vpp_nat.NatGetReass{}
reply := &vpp_nat.NatGetReassReply{}
if err := h.callsChannel.SendRequest(req).ReceiveReply(reply); err != nil {
return nil, nil, fmt.Errorf("failed to get NAT virtual reassembly configuration: %v", err)
}
vrIPv4 = &nat.VirtualReassembly{
Timeout: reply.IP4Timeout,
MaxReassemblies: uint32(reply.IP4MaxReass),
MaxFragments: uint32(reply.IP4MaxFrag),
DropFragments: uintToBool(reply.IP4DropFrag),
}
vrIPv6 = &nat.VirtualReassembly{
Timeout: reply.IP6Timeout,
MaxReassemblies: uint32(reply.IP6MaxReass),
MaxFragments: uint32(reply.IP6MaxFrag),
DropFragments: uintToBool(reply.IP6DropFrag),
}
return
}
// nat44StaticMappingDump returns a map of NAT44 static mappings sorted by tags
func (h *NatVppHandler) nat44StaticMappingDump() (entries stMappingMap, err error) {
entries = make(stMappingMap)
childMappings := make(stMappingMap)
req := &vpp_nat.Nat44StaticMappingDump{}
reqContext := h.callsChannel.SendMultiRequest(req)
for {
msg := &vpp_nat.Nat44StaticMappingDetails{}
stop, err := reqContext.ReceiveReply(msg)
if err != nil {
return nil, fmt.Errorf("failed to dump NAT44 static mapping: %v", err)
}
if stop {
break
}
lcIPAddress := net.IP(msg.LocalIPAddress[:]).String()
exIPAddress := net.IP(msg.ExternalIPAddress[:]).String()
// Parse tag (DNAT label)
tag := strings.TrimRight(msg.Tag, "\x00")
if _, hasTag := entries[tag]; !hasTag {
entries[tag] = []*nat.DNat44_StaticMapping{}
childMappings[tag] = []*nat.DNat44_StaticMapping{}
}
// resolve interface name
var (
found bool
extIfaceName string
extIfaceMeta *ifaceidx.IfaceMetadata
)
if msg.ExternalSwIfIndex != NoInterface {
extIfaceName, extIfaceMeta, found = h.ifIndexes.LookupBySwIfIndex(uint32(msg.ExternalSwIfIndex))
if !found {
h.log.Warnf("Interface with index %v not found in the mapping", msg.ExternalSwIfIndex)
continue
}
}
flags := getNat44Flags(msg.Flags)
// Add mapping into the map.
mapping := &nat.DNat44_StaticMapping{
ExternalInterface: extIfaceName,
ExternalIp: exIPAddress,
ExternalPort: uint32(msg.ExternalPort),
LocalIps: []*nat.DNat44_StaticMapping_LocalIP{ // single-value
{
VrfId: msg.VrfID,
LocalIp: lcIPAddress,
LocalPort: uint32(msg.LocalPort),
},
},
Protocol: h.protocolNumberToNBValue(msg.Protocol),
TwiceNat: h.getTwiceNatMode(flags.isTwiceNat, flags.isSelfTwiceNat),
// if there is only one backend the affinity can not be set
SessionAffinity: 0,
}
entries[tag] = append(entries[tag], mapping)
if msg.ExternalSwIfIndex != NoInterface {
// collect auto-generated "child" mappings (interface replaced with every assigned IP address)
for _, ipAddr := range h.getInterfaceIPAddresses(extIfaceName, extIfaceMeta) {
childMapping := proto.Clone(mapping).(*nat.DNat44_StaticMapping)
childMapping.ExternalIp = ipAddr
childMapping.ExternalInterface = ""
childMappings[tag] = append(childMappings[tag], childMapping)
}
}
}
// do not dump auto-generated child mappings
for tag, mappings := range entries {
var filtered []*nat.DNat44_StaticMapping
for _, mapping := range mappings {
isChild := false
for _, child := range childMappings[tag] {
if proto.Equal(mapping, child) {
isChild = true
break
}
}
if !isChild {
filtered = append(filtered, mapping)
}
}
entries[tag] = filtered
}
return entries, nil
}
// nat44StaticMappingLbDump returns a map of NAT44 static mapping with load balancing sorted by tags.
func (h *NatVppHandler) nat44StaticMappingLbDump() (entries stMappingMap, err error) {
entries = make(stMappingMap)
req := &vpp_nat.Nat44LbStaticMappingDump{}
reqContext := h.callsChannel.SendMultiRequest(req)
for {
msg := &vpp_nat.Nat44LbStaticMappingDetails{}
stop, err := reqContext.ReceiveReply(msg)
if err != nil {
return nil, fmt.Errorf("failed to dump NAT44 lb-static mapping: %v", err)
}
if stop {
break
}
// Parse tag (DNAT label)
tag := strings.TrimRight(msg.Tag, "\x00")
if _, hasTag := entries[tag]; !hasTag {
entries[tag] = []*nat.DNat44_StaticMapping{}
}
// Prepare localIPs
var locals []*nat.DNat44_StaticMapping_LocalIP
for _, localIPVal := range msg.Locals {
locals = append(locals, &nat.DNat44_StaticMapping_LocalIP{
VrfId: localIPVal.VrfID,
LocalIp: net.IP(localIPVal.Addr[:]).String(),
LocalPort: uint32(localIPVal.Port),
Probability: uint32(localIPVal.Probability),
})
}
exIPAddress := net.IP(msg.ExternalAddr[:]).String()
flags := getNat44Flags(msg.Flags)
// Add mapping into the map.
mapping := &nat.DNat44_StaticMapping{
ExternalIp: exIPAddress,
ExternalPort: uint32(msg.ExternalPort),
LocalIps: locals,
Protocol: h.protocolNumberToNBValue(msg.Protocol),
TwiceNat: h.getTwiceNatMode(flags.isTwiceNat, flags.isSelfTwiceNat),
SessionAffinity: msg.Affinity,
}
entries[tag] = append(entries[tag], mapping)
}
return entries, nil
}
// nat44IdentityMappingDump returns a map of NAT44 identity mappings sorted by tags.
func (h *NatVppHandler) nat44IdentityMappingDump() (entries idMappingMap, err error) {
entries = make(idMappingMap)
childMappings := make(idMappingMap)
req := &vpp_nat.Nat44IdentityMappingDump{}
reqContext := h.callsChannel.SendMultiRequest(req)
for {
msg := &vpp_nat.Nat44IdentityMappingDetails{}
stop, err := reqContext.ReceiveReply(msg)
if err != nil {
return nil, fmt.Errorf("failed to dump NAT44 identity mapping: %v", err)
}
if stop {
break
}
// Parse tag (DNAT label)
tag := strings.TrimRight(msg.Tag, "\x00")
if _, hasTag := entries[tag]; !hasTag {
entries[tag] = []*nat.DNat44_IdentityMapping{}
childMappings[tag] = []*nat.DNat44_IdentityMapping{}
}
// resolve interface name
var (
found bool
ifaceName string
ifaceMeta *ifaceidx.IfaceMetadata
)
if msg.SwIfIndex != NoInterface {
ifaceName, ifaceMeta, found = h.ifIndexes.LookupBySwIfIndex(uint32(msg.SwIfIndex))
if !found {
h.log.Warnf("Interface with index %v not found in the mapping", msg.SwIfIndex)
continue
}
}
// Add mapping into the map.
mapping := &nat.DNat44_IdentityMapping{
IpAddress: net.IP(msg.IPAddress[:]).String(),
VrfId: msg.VrfID,
Interface: ifaceName,
Port: uint32(msg.Port),
Protocol: h.protocolNumberToNBValue(msg.Protocol),
}
entries[tag] = append(entries[tag], mapping)
if msg.SwIfIndex != NoInterface {
// collect auto-generated "child" mappings (interface replaced with every assigned IP address)
for _, ipAddr := range h.getInterfaceIPAddresses(ifaceName, ifaceMeta) {
childMapping := proto.Clone(mapping).(*nat.DNat44_IdentityMapping)
childMapping.IpAddress = ipAddr
childMapping.Interface = ""
childMappings[tag] = append(childMappings[tag], childMapping)
}
}
}
// do not dump auto-generated child mappings
for tag, mappings := range entries {
var filtered []*nat.DNat44_IdentityMapping
for _, mapping := range mappings {
isChild := false
for _, child := range childMappings[tag] {
if proto.Equal(mapping, child) {
isChild = true
break
}
}
if !isChild {
filtered = append(filtered, mapping)
}
}
entries[tag] = filtered
}
return entries, nil
}
// nat44InterfaceDump dumps NAT44 interface features.
func (h *NatVppHandler) nat44InterfaceDump() (interfaces []*nat.Nat44Global_Interface, err error) {
/* dump non-Output interfaces first */
req1 := &vpp_nat.Nat44InterfaceDump{}
reqContext := h.callsChannel.SendMultiRequest(req1)
for {
msg := &vpp_nat.Nat44InterfaceDetails{}
stop, err := reqContext.ReceiveReply(msg)
if err != nil {
return nil, fmt.Errorf("failed to dump NAT44 interface: %v", err)
}
if stop {
break
}
// Find interface name
ifName, _, found := h.ifIndexes.LookupBySwIfIndex(uint32(msg.SwIfIndex))
if !found {
h.log.Warnf("Interface with index %d not found in the mapping", msg.SwIfIndex)
continue
}
flags := getNat44Flags(msg.Flags)
if flags.isInside {
interfaces = append(interfaces, &nat.Nat44Global_Interface{
Name: ifName,
IsInside: true,
})
} else {
interfaces = append(interfaces, &nat.Nat44Global_Interface{
Name: ifName,
IsInside: false,
})
}
}
/* dump Output interfaces next */
req2 := &vpp_nat.Nat44InterfaceOutputFeatureDump{}
reqContext = h.callsChannel.SendMultiRequest(req2)
for {
msg := &vpp_nat.Nat44InterfaceOutputFeatureDetails{}
stop, err := reqContext.ReceiveReply(msg)
if err != nil {
return nil, fmt.Errorf("failed to dump NAT44 interface output feature: %v", err)
}
if stop {
break
}
// Find interface name
ifName, _, found := h.ifIndexes.LookupBySwIfIndex(uint32(msg.SwIfIndex))
if !found {
h.log.Warnf("Interface with index %d not found in the mapping", msg.SwIfIndex)
continue
}
flags := getNat44Flags(msg.Flags)
interfaces = append(interfaces, &nat.Nat44Global_Interface{
Name: ifName,
IsInside: flags.isInside,
OutputFeature: true,
})
}
return interfaces, nil
}
// Nat44IsForwardingEnabled checks if the NAT44 forwarding is enabled.
func (h *NatVppHandler) isNat44ForwardingEnabled() (isEnabled bool, err error) {
req := &vpp_nat.Nat44ForwardingIsEnabled{}
reply := &vpp_nat.Nat44ForwardingIsEnabledReply{}
if err := h.callsChannel.SendRequest(req).ReceiveReply(reply); err != nil {
return false, fmt.Errorf("failed to dump NAT44 forwarding: %v", err)
}
isEnabled = reply.Enabled
return isEnabled, nil
}
func (h *NatVppHandler) getInterfaceIPAddresses(ifaceName string, ifaceMeta *ifaceidx.IfaceMetadata) (ipAddrs []string) {
ipAddrNets := ifaceMeta.IPAddresses
dhcpLease, hasDHCPLease := h.dhcpIndex.GetValue(ifaceName)
if hasDHCPLease {
lease := dhcpLease.(*ifs.DHCPLease)
ipAddrNets = append(ipAddrNets, lease.HostIpAddress)
}
for _, ipAddrNet := range ipAddrNets {
ipAddr := strings.Split(ipAddrNet, "/")[0]
ipAddrs = append(ipAddrs, ipAddr)
}
return ipAddrs
}
// protocolNumberToNBValue converts protocol numeric representation into the corresponding enum
// enum value from the NB model.
func (h *NatVppHandler) protocolNumberToNBValue(protocol uint8) (proto nat.DNat44_Protocol) {
switch protocol {
case TCP:
return nat.DNat44_TCP
case UDP:
return nat.DNat44_UDP
case ICMP:
return nat.DNat44_ICMP
default:
h.log.Warnf("Unknown protocol %v", protocol)
return 0
}
}
// protocolNBValueToNumber converts protocol enum value from the NB model into the
// corresponding numeric representation.
func (h *NatVppHandler) protocolNBValueToNumber(protocol nat.DNat44_Protocol) (proto uint8) {
switch protocol {
case nat.DNat44_TCP:
return TCP
case nat.DNat44_UDP:
return UDP
case nat.DNat44_ICMP:
return ICMP
default:
h.log.Warnf("Unknown protocol %v, defaulting to TCP", protocol)
return TCP
}
}
func (h *NatVppHandler) getTwiceNatMode(twiceNat, selfTwiceNat bool) nat.DNat44_StaticMapping_TwiceNatMode {
if twiceNat {
if selfTwiceNat {
h.log.Warnf("Both TwiceNAT and self-TwiceNAT are enabled")
return 0
}
return nat.DNat44_StaticMapping_ENABLED
}
if selfTwiceNat {
return nat.DNat44_StaticMapping_SELF
}
return nat.DNat44_StaticMapping_DISABLED
}
func getOrCreateDNAT(dnats dnatMap, label string) *nat.DNat44 {
if _, created := dnats[label]; !created {
dnats[label] = &nat.DNat44{Label: label}
}
return dnats[label]
}
func getNat44Flags(flags vpp_nat.NatConfigFlags) *nat44Flags {
natFlags := &nat44Flags{}
if flags&vpp_nat.NAT_IS_EXT_HOST_VALID != 0 {
natFlags.isExtHostValid = true
}
if flags&vpp_nat.NAT_IS_STATIC != 0 {
natFlags.isStatic = true
}
if flags&vpp_nat.NAT_IS_INSIDE != 0 {
natFlags.isInside = true
}
if flags&vpp_nat.NAT_IS_OUTSIDE != 0 {
natFlags.isOutside = true
}
if flags&vpp_nat.NAT_IS_ADDR_ONLY != 0 {
natFlags.isAddrOnly = true
}
if flags&vpp_nat.NAT_IS_OUT2IN_ONLY != 0 {
natFlags.isOut2In = true
}
if flags&vpp_nat.NAT_IS_SELF_TWICE_NAT != 0 {
natFlags.isSelfTwiceNat = true
}
if flags&vpp_nat.NAT_IS_TWICE_NAT != 0 {
natFlags.isTwiceNat = true
}
return natFlags
}
func uintToBool(value uint8) bool {
if value == 0 {
return false
}
return true
}