-
Notifications
You must be signed in to change notification settings - Fork 10
/
normalize-services.go
296 lines (269 loc) · 7.11 KB
/
normalize-services.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
package pass1
import (
"net/netip"
)
//#############################################################################
// Purpose : Expand auto interface to one or more real interfaces
// with respect to list of destination objects.
// Note : Different destination objects may lead to different result lists.
// Result : An array of pairs:
// 1. List of real interfaces.
// 2. Those objects from dst_list that lead to result in 1.
type expAutoPair struct {
srcList srvObjList
dstList groupObjList
}
type pathOrAuto interface{}
func (c *spoc) pathAutoInterfaces(
src *autoIntf, dst pathOrAuto, origDst groupObj) intfList {
managed := src.managed
srcPath := src.getPathNode()
var dstPath pathStore
var toList []pathStore
switch x := dst.(type) {
case *autoIntf:
dstPath = x.getPathNode()
toList = addPathrestictedIntfs(dstPath, x.object)
case pathStore:
dstPath = x
toList = []pathStore{x}
}
if srcPath == dstPath {
return nil
}
result := c.findAutoInterfaces(
srcPath, dstPath, toList, src.name, origDst.String(), src.object)
if managed {
j := 0
for _, intf := range result {
if intf.router.managed != "" {
result[j] = intf
j++
}
}
result = result[:j]
}
return result
}
func (c *spoc) expandAutoIntfWithDstList(
a *autoIntf, dstList groupObjList, ctx string) []*expAutoPair {
path2pair := make(map[pathOrAuto]*expAutoPair)
var result []*expAutoPair
for _, dst := range dstList {
var path pathOrAuto
switch x := dst.(type) {
case someObj:
path = x.getPathNode()
case *host:
path = x.getPathNode()
case *autoIntf:
path = x
}
pair, found := path2pair[path]
if !found {
var real srvObjList
for _, intf := range c.pathAutoInterfaces(a, path, dst) {
switch intf.ipType {
case shortIP:
c.err("%s without IP address (from .[auto])\n"+
" must not be used in rule of %s", intf, ctx)
case unnumberedIP:
// Ignore unnumbered interfaces.
default:
real.push(intf)
}
}
if real != nil {
// If identical result already was found with other destination,
// then share this result for both destinations.
PAIR:
for _, pair2 := range result {
if len(pair2.srcList) != len(real) {
continue
}
for i, intf := range pair2.srcList {
if real[i] != intf {
continue PAIR
}
pair = pair2
break PAIR
}
}
if pair == nil {
pair = &expAutoPair{srcList: real}
result = append(result, pair)
}
}
// Store nil for empty list of interfaces.
path2pair[path] = pair
}
if pair != nil {
pair.dstList.push(dst)
}
}
return result
}
func (c *spoc) substituteAutoIntf(
srcList, dstList groupObjList, ctx string) (srvObjList, []*expAutoPair) {
var convertedSrc srvObjList
var resultPairList []*expAutoPair
for _, src := range srcList {
var a *autoIntf
switch x := src.(type) {
case srvObj:
convertedSrc.push(x)
continue
case *autoIntf:
a = x
}
pairList := c.expandAutoIntfWithDstList(a, dstList, ctx)
// All elements of dstList lead to same result list of interfaces.
if len(pairList) == 1 {
for _, intf := range pairList[0].srcList {
convertedSrc.push(intf)
}
continue
}
// Different destination objects lead to different result sets.
// Skip auto interface in convertedSrc,
// but add src/dst pairs to second return value.
resultPairList = append(resultPairList, pairList...)
}
return convertedSrc, resultPairList
}
func (c *spoc) normalizeSrcDstList(
r *unexpRule, l groupObjList, s *service) [][2]srvObjList {
ctx := s.name
ipv6 := s.ipV6
c.userObj.elements = l
srcList := c.expandGroupInRule(r.src, "src of rule in "+ctx, ipv6)
dstList := c.expandGroupInRule(r.dst, "dst of rule in "+ctx, ipv6)
c.userObj.elements = nil
// Expand auto interfaces in srcList.
expSrcList, extraSrcDst := c.substituteAutoIntf(srcList, dstList, ctx)
var extraResult [][2]srvObjList
toGrp := func(l srvObjList) groupObjList {
result := make(groupObjList, len(l))
for i, obj := range l {
result[i] = obj.(groupObj)
}
return result
}
toSrv := func(l groupObjList) srvObjList {
result := make(srvObjList, len(l))
for i, obj := range l {
result[i] = obj.(srvObj)
}
return result
}
addExtra := func(extraDstSrc []*expAutoPair) {
for _, pair := range extraDstSrc {
extraResult = append(
extraResult, [2]srvObjList{toSrv(pair.dstList), pair.srcList})
}
}
// Expand auto interfaces in dst of extraSrcDst.
for _, pair := range extraSrcDst {
sList, dList := pair.srcList, pair.dstList
expDstList, extraDstSrc :=
c.substituteAutoIntf(dList, toGrp(sList), ctx)
extraResult = append(extraResult, [2]srvObjList{sList, expDstList})
addExtra(extraDstSrc)
}
// Expand auto interfaces in dstList.
expDstList, extraDstSrc :=
c.substituteAutoIntf(dstList, toGrp(expSrcList), ctx)
addExtra(extraDstSrc)
return append([][2]srvObjList{{expSrcList, expDstList}}, extraResult...)
}
func (c *spoc) normalizeServiceRules(s *service, sRules *serviceRules) {
user := c.expandUser(s)
s.expandedUser = user
hasRules := false
for _, uRule := range s.rules {
deny := uRule.action == "deny"
var store *serviceRuleList
if deny {
store = &sRules.deny
} else {
store = &sRules.permit
}
log := uRule.log
prtList := uRule.prt
if prtList == nil {
continue
}
simplePrtList, complexPrtList := classifyProtocols(prtList)
process := func(elt groupObjList) {
srcDstListPairs := c.normalizeSrcDstList(uRule, elt, s)
for _, srcDstList := range srcDstListPairs {
srcList, dstList := srcDstList[0], srcDstList[1]
if srcList != nil || dstList != nil {
hasRules = true
}
if srcList == nil || dstList == nil || s.disabled {
continue
}
rule := serviceRule{
deny: deny,
src: srcList,
dst: dstList,
prt: simplePrtList,
log: log,
rule: uRule,
}
if simplePrtList != nil {
store.push(&rule)
}
for _, c := range complexPrtList {
prt, srcRange := c.prt, c.src
r2 := rule
if c.modifiers != nil {
r2.modifiers = *c.modifiers
if c.modifiers.reversed {
r2.src, r2.dst = rule.dst, rule.src
}
}
r2.prt = protoList{prt}
r2.srcRange = srcRange
r2.statelessICMP = prt.statelessICMP
store.push(&r2)
}
}
}
if s.foreach {
// Must not split aggregate set of zone cluster.
// Otherwise we would get wrong result for interface[user].[all].
var cluster *zone
var ipp netip.Prefix
clusterIdx := 0
for i, elt := range user {
if n, ok := elt.(*network); ok {
cl := n.zone.cluster[0]
if cl == cluster && n.ipp == ipp {
continue
} else {
cluster = cl
ipp = n.ipp
}
}
process(user[clusterIdx:i])
clusterIdx = i
}
process(user[clusterIdx:])
} else {
process(user)
}
}
if !hasRules && len(user) == 0 {
c.warn("Must not define %s with empty users and empty rules", s.name)
}
}
func (c *spoc) normalizeServices() *serviceRules {
c.progress("Normalizing services")
sRules := new(serviceRules)
for _, sv := range c.ascendingServices {
c.normalizeServiceRules(sv, sRules)
}
return sRules
}