-
Notifications
You must be signed in to change notification settings - Fork 2
/
routemap.go
360 lines (303 loc) · 9.05 KB
/
routemap.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
package netconf
import (
"fmt"
"net/netip"
"sort"
"strings"
"github.com/metal-stack/metal-go/api/models"
mn "github.com/metal-stack/metal-lib/pkg/net"
)
type importPrefix struct {
Prefix netip.Prefix
Policy AccessPolicy
SourceVRF string
}
type importRule struct {
TargetVRF string
ImportVRFs []string
ImportPrefixes []importPrefix
ImportPrefixesNoExport []importPrefix
}
type ImportSettings struct {
ImportPrefixes []importPrefix
ImportPrefixesNoExport []importPrefix
}
func (i *importRule) bySourceVrf() map[string]ImportSettings {
r := map[string]ImportSettings{}
for _, vrf := range i.ImportVRFs {
r[vrf] = ImportSettings{}
}
for _, pfx := range i.ImportPrefixes {
e := r[pfx.SourceVRF]
e.ImportPrefixes = append(e.ImportPrefixes, pfx)
r[pfx.SourceVRF] = e
}
for _, pfx := range i.ImportPrefixesNoExport {
e := r[pfx.SourceVRF]
e.ImportPrefixesNoExport = append(e.ImportPrefixesNoExport, pfx)
r[pfx.SourceVRF] = e
}
return r
}
func importRulesForNetwork(kb config, network *models.V1MachineNetwork) *importRule {
vrfName := vrfNameOf(network)
if network.Networktype == nil || *network.Networktype == mn.Underlay {
return nil
}
i := importRule{
TargetVRF: vrfName,
}
privatePrimaryNet := kb.getPrivatePrimaryNetwork()
externalNets := kb.GetNetworks(mn.External)
privateSecondarySharedNets := kb.GetNetworks(mn.PrivateSecondaryShared)
nt := *network.Networktype
switch nt {
case mn.PrivatePrimaryUnshared:
fallthrough
case mn.PrivatePrimaryShared:
// reach out from private network into public networks
i.ImportVRFs = vrfNamesOf(externalNets)
i.ImportPrefixes = getDestinationPrefixes(externalNets)
// deny public address of default network
defaultNet := kb.GetDefaultRouteNetwork()
if ip, err := netip.ParseAddr(defaultNet.Ips[0]); err == nil {
var bl = 32
if ip.Is6() {
bl = 128
}
i.ImportPrefixes = append(i.ImportPrefixes, importPrefix{
Prefix: netip.PrefixFrom(ip, bl),
Policy: Deny,
SourceVRF: vrfNameOf(defaultNet),
})
}
// permit external routes
i.ImportPrefixes = append(i.ImportPrefixes, prefixesOfNetworks(externalNets)...)
// reach out from private network into shared private networks
i.ImportVRFs = append(i.ImportVRFs, vrfNamesOf(privateSecondarySharedNets)...)
i.ImportPrefixes = append(i.ImportPrefixes, prefixesOfNetworks(privateSecondarySharedNets)...)
// reach out from private network to destination prefixes of private secondays shared networks
for _, n := range privateSecondarySharedNets {
for _, pfx := range n.Destinationprefixes {
ppfx := netip.MustParsePrefix(pfx)
isThere := false
for _, i := range i.ImportPrefixes {
if i.Prefix == ppfx {
isThere = true
}
}
if !isThere {
i.ImportPrefixes = append(i.ImportPrefixes, importPrefix{
Prefix: ppfx,
Policy: Permit,
SourceVRF: vrfNameOf(n),
})
}
}
}
case mn.PrivateSecondaryShared:
// reach out from private shared networks into private primary network
i.ImportVRFs = []string{vrfNameOf(privatePrimaryNet)}
i.ImportPrefixes = concatPfxSlices(prefixesOfNetwork(privatePrimaryNet, vrfNameOf(privatePrimaryNet)), prefixesOfNetwork(network, vrfNameOf(privatePrimaryNet)))
// import destination prefixes of dmz networks from external networks
if len(network.Destinationprefixes) > 0 {
for _, pfx := range network.Destinationprefixes {
for _, e := range externalNets {
importExternalNet := false
for _, epfx := range e.Destinationprefixes {
if pfx == epfx {
importExternalNet = true
i.ImportPrefixes = append(i.ImportPrefixes, importPrefix{
Prefix: netip.MustParsePrefix(pfx),
Policy: Permit,
SourceVRF: vrfNameOf(e),
})
}
}
if importExternalNet {
i.ImportVRFs = append(i.ImportVRFs, vrfNameOf(e))
i.ImportPrefixes = append(i.ImportPrefixes, prefixesOfNetwork(e, vrfNameOf(e))...)
}
}
}
}
case mn.External:
// reach out from public into private and other public networks
i.ImportVRFs = []string{vrfNameOf(privatePrimaryNet)}
i.ImportPrefixes = prefixesOfNetwork(network, vrfNameOf(privatePrimaryNet))
nets := []*models.V1MachineNetwork{privatePrimaryNet}
if containsDefaultRoute(network.Destinationprefixes) {
for _, r := range privateSecondarySharedNets {
if containsDefaultRoute(r.Destinationprefixes) {
nets = append(nets, r)
i.ImportVRFs = append(i.ImportVRFs, vrfNameOf(r))
}
}
}
i.ImportPrefixesNoExport = prefixesOfNetworks(nets)
}
return &i
}
func (i *importRule) prefixLists() []IPPrefixList {
var result []IPPrefixList
seed := IPPrefixListSeqSeed
afs := []AddressFamily{AddressFamilyIPv4, AddressFamilyIPv6}
for _, af := range afs {
pfxList := prefixLists(i.ImportPrefixesNoExport, af, false, seed, i.TargetVRF)
result = append(result, pfxList...)
seed = IPPrefixListSeqSeed + len(result)
result = append(result, prefixLists(i.ImportPrefixes, af, true, seed, i.TargetVRF)...)
}
return result
}
func prefixLists(
prefixes []importPrefix,
af AddressFamily,
isExported bool,
seed int,
vrf string,
) []IPPrefixList {
var result []IPPrefixList
for _, p := range prefixes {
if af == AddressFamilyIPv4 && !p.Prefix.Addr().Is4() {
continue
}
if af == AddressFamilyIPv6 && !p.Prefix.Addr().Is6() {
continue
}
specs := p.buildSpecs(seed)
for _, spec := range specs {
// self-importing prefixes is nonsense
if vrf == p.SourceVRF {
continue
}
name := p.name(vrf, isExported)
prefixList := IPPrefixList{
Name: name,
Spec: spec,
AddressFamily: af,
SourceVRF: p.SourceVRF,
}
result = append(result, prefixList)
}
seed++
}
return result
}
func concatPfxSlices(pfxSlices ...[]importPrefix) []importPrefix {
res := []importPrefix{}
for _, pfxSlice := range pfxSlices {
res = append(res, pfxSlice...)
}
return res
}
func stringSliceToIPPrefix(s []string, sourceVrf string) []importPrefix {
var result []importPrefix
for _, e := range s {
ipp, err := netip.ParsePrefix(e)
if err != nil {
continue
}
result = append(result, importPrefix{
Prefix: ipp,
Policy: Permit,
SourceVRF: sourceVrf,
})
}
return result
}
func getDestinationPrefixes(networks []*models.V1MachineNetwork) []importPrefix {
var result []importPrefix
for _, network := range networks {
result = append(result, stringSliceToIPPrefix(network.Destinationprefixes, vrfNameOf(network))...)
}
return result
}
func prefixesOfNetworks(networks []*models.V1MachineNetwork) []importPrefix {
var result []importPrefix
for _, network := range networks {
result = append(result, prefixesOfNetwork(network, vrfNameOf(network))...)
}
return result
}
func prefixesOfNetwork(network *models.V1MachineNetwork, sourceVrf string) []importPrefix {
return stringSliceToIPPrefix(network.Prefixes, sourceVrf)
}
func vrfNameOf(n *models.V1MachineNetwork) string {
return fmt.Sprintf("vrf%d", *n.Vrf)
}
func vrfNamesOf(networks []*models.V1MachineNetwork) []string {
var result []string
for _, n := range networks {
result = append(result, vrfNameOf(n))
}
return result
}
func byName(prefixLists []IPPrefixList) map[string]IPPrefixList {
byName := map[string]IPPrefixList{}
for _, prefixList := range prefixLists {
if _, isPresent := byName[prefixList.Name]; isPresent {
continue
}
byName[prefixList.Name] = prefixList
}
return byName
}
func (i *importRule) routeMaps() []RouteMap {
var result []RouteMap
order := RouteMapOrderSeed
byName := byName(i.prefixLists())
names := []string{}
for n := range byName {
names = append(names, n)
}
sort.Sort(sort.Reverse(sort.StringSlice(names)))
for _, n := range names {
prefixList := byName[n]
matchVrf := fmt.Sprintf("match source-vrf %s", prefixList.SourceVRF)
matchPfxList := fmt.Sprintf("match %s address prefix-list %s", prefixList.AddressFamily, n)
entries := []string{matchVrf, matchPfxList}
if strings.HasSuffix(n, IPPrefixListNoExportSuffix) {
entries = append(entries, "set community additive no-export")
}
routeMap := RouteMap{
Name: routeMapName(i.TargetVRF),
Policy: Permit.String(),
Order: order,
Entries: entries,
}
order += RouteMapOrderSeed
result = append(result, routeMap)
}
routeMap := RouteMap{
Name: routeMapName(i.TargetVRF),
Policy: Deny.String(),
Order: order,
}
result = append(result, routeMap)
return result
}
func routeMapName(vrfName string) string {
return vrfName + "-import-map"
}
func (i *importPrefix) buildSpecs(seq int) []string {
var result []string
var spec string
if i.Prefix.Bits() == 0 {
spec = fmt.Sprintf("%s %s", i.Policy, i.Prefix)
} else {
spec = fmt.Sprintf("seq %d %s %s le %d", seq, i.Policy, i.Prefix, i.Prefix.Addr().BitLen())
}
result = append(result, spec)
return result
}
func (i *importPrefix) name(targetVrf string, isExported bool) string {
suffix := ""
if i.Prefix.Addr().Is6() {
suffix = "-ipv6"
}
if !isExported {
suffix += IPPrefixListNoExportSuffix
}
return fmt.Sprintf("%s-import-from-%s%s", targetVrf, i.SourceVRF, suffix)
}