This repository has been archived by the owner on Mar 20, 2024. It is now read-only.
forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
l4.go
660 lines (579 loc) · 20.8 KB
/
l4.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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
// Copyright 2016-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 policy
import (
"bytes"
"encoding/json"
"fmt"
"sort"
"strconv"
"sync/atomic"
"unsafe"
"github.com/cilium/cilium/api/v1/models"
"github.com/cilium/cilium/pkg/identity"
"github.com/cilium/cilium/pkg/labels"
"github.com/cilium/cilium/pkg/lock"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/policy/api"
"github.com/cilium/cilium/pkg/policy/trafficdirection"
"github.com/cilium/cilium/pkg/u8proto"
"github.com/sirupsen/logrus"
)
// L7DataMap contains a map of L7 rules per endpoint where key is a CachedSelector
type L7DataMap map[CachedSelector]api.L7Rules
func (l7 L7DataMap) MarshalJSON() ([]byte, error) {
if len(l7) == 0 {
return []byte("[]"), nil
}
/* First, create a sorted slice of the selectors so we can get
* consistent JSON output */
selectors := make(CachedSelectorSlice, 0, len(l7))
for cs := range l7 {
selectors = append(selectors, cs)
}
sort.Sort(selectors)
/* Now we can iterate the slice and generate JSON entries. */
var err error
buffer := bytes.NewBufferString("[")
for _, cs := range selectors {
buffer.WriteString("{\"")
buffer.WriteString(cs.String())
buffer.WriteString("\":")
b, err := json.Marshal(l7[cs])
if err == nil {
buffer.Write(b)
} else {
buffer.WriteString("\"L7DataMap error: ")
buffer.WriteString(err.Error())
buffer.WriteString("\"")
}
buffer.WriteString("},")
}
buffer.Truncate(buffer.Len() - 1) // Drop the final ","
buffer.WriteString("]")
return buffer.Bytes(), err
}
// L7ParserType is the type used to indicate what L7 parser to use.
// Consts are defined for all well known L7 parsers.
// Unknown string values are created for key-value pair policies, which
// are then transparently used in redirect configuration.
type L7ParserType string
func (l7 L7ParserType) String() string {
return (string)(l7)
}
const (
// ParserTypeNone represents the case where no parser type is provided.
ParserTypeNone L7ParserType = ""
// ParserTypeHTTP specifies a HTTP parser type
ParserTypeHTTP L7ParserType = "http"
// ParserTypeKafka specifies a Kafka parser type
ParserTypeKafka L7ParserType = "kafka"
// ParserTypeDNS specifies a DNS parser type
ParserTypeDNS L7ParserType = "dns"
)
// L4Filter represents the policy (allowed remote sources / destinations of
// traffic) that applies at a specific L4 port/protocol combination (including
// all ports and protocols), at either ingress or egress. The policy here is
// specified in terms of selectors that are mapped to security identities via
// the selector cache.
type L4Filter struct {
// Port is the destination port to allow. Port 0 indicates that all traffic
// is allowed at L4.
Port int `json:"port"`
// Protocol is the L4 protocol to allow or NONE
Protocol api.L4Proto `json:"protocol"`
// U8Proto is the Protocol in numeric format, or 0 for NONE
U8Proto u8proto.U8proto `json:"-"`
// allowsAllAtL3 indicates whether this filter allows all traffic at L3.
// This can be determined by checking whether 'Endpoints' contains
// 'wildcardCachedSelector', but caching this information instead is
// much more performant.
allowsAllAtL3 bool
// CachedSelectors limits the labels for allowing traffic (to / from).
// This includes selectors for destinations affected by entity-based
// and CIDR-based policy.
// Holds references to the CachedSelectors, which must be released!
CachedSelectors CachedSelectorSlice `json:"-"`
// L7Parser specifies the L7 protocol parser (optional). If specified as
// an empty string, then means that no L7 proxy redirect is performed.
L7Parser L7ParserType `json:"-"`
// L7RulesPerEp is a list of L7 rules per endpoint passed to the L7 proxy (optional)
L7RulesPerEp L7DataMap `json:"l7-rules,omitempty"`
// Ingress is true if filter applies at ingress; false if it applies at egress.
Ingress bool `json:"-"`
// The rule labels of this Filter
DerivedFromRules labels.LabelArrayList `json:"-"`
// This reference is circular, but it is cleaned up at Detach()
policy unsafe.Pointer // *L4Policy
}
// AllowsAllAtL3 returns whether this L4Filter applies to all endpoints at L3.
func (l4 *L4Filter) AllowsAllAtL3() bool {
return l4.allowsAllAtL3
}
// HasL3DependentL7Rules returns true if this L4Filter is created from rules
// that require an L3 match as well as specific L7 rules.
func (l4 *L4Filter) HasL3DependentL7Rules() bool {
switch len(l4.L7RulesPerEp) {
case 0:
// No L7 rules.
return false
case 1:
// loop to get access to the first and only key in the map
for cs := range l4.L7RulesPerEp {
// If L3 is wildcarded, this filter corresponds to L4-only rule(s).
return !cs.IsWildcard()
}
}
return true
}
// ToKeys converts filter into a list of Keys.
func (l4 *L4Filter) ToKeys(direction trafficdirection.TrafficDirection) []Key {
keysToAdd := []Key{}
port := uint16(l4.Port)
proto := uint8(l4.U8Proto)
if l4.AllowsAllAtL3() {
if l4.Port == 0 {
// Allow-all
log.WithFields(logrus.Fields{
logfields.TrafficDirection: direction,
}).Debug("ToKeys: allow all")
keyToAdd := Key{
DestPort: 0,
Nexthdr: 0,
TrafficDirection: direction.Uint8(),
}
keysToAdd = append(keysToAdd, keyToAdd)
} else {
// L4 allow
log.WithFields(logrus.Fields{
logfields.Port: port,
logfields.Protocol: proto,
logfields.TrafficDirection: direction,
}).Debug("ToKeys: L4 allow all")
keyToAdd := Key{
Identity: 0,
// NOTE: Port is in host byte-order!
DestPort: port,
Nexthdr: proto,
TrafficDirection: direction.Uint8(),
}
keysToAdd = append(keysToAdd, keyToAdd)
}
if !l4.HasL3DependentL7Rules() {
return keysToAdd
} // else we need to calculate all L3-dependent L4 peers below.
}
for _, cs := range l4.CachedSelectors {
identities := cs.GetSelections()
log.WithFields(logrus.Fields{
logfields.TrafficDirection: direction,
logfields.EndpointSelector: cs,
logfields.PolicyID: identities,
}).Debug("ToKeys: Allowed remote IDs")
for _, id := range identities {
srcID := id.Uint32()
keyToAdd := Key{
Identity: srcID,
// NOTE: Port is in host byte-order!
DestPort: port,
Nexthdr: proto,
TrafficDirection: direction.Uint8(),
}
keysToAdd = append(keysToAdd, keyToAdd)
}
}
return keysToAdd
}
// IdentitySelectionUpdated implements CachedSelectionUser interface
// This call is made while holding selector cache lock, must beware of deadlocking!
//
// The caller is responsible for making sure the same identity is not
// present in both 'added' and 'deleted'.
func (l4 *L4Filter) IdentitySelectionUpdated(selector CachedSelector, selections, added, deleted []identity.NumericIdentity) {
log.WithFields(logrus.Fields{
logfields.EndpointSelector: selector,
logfields.PolicyID: selections,
logfields.AddedPolicyID: added,
logfields.DeletedPolicyID: deleted,
}).Debug("identities selected by L4Filter updated")
// Skip updates on filter that wildcards L3.
// This logic mirrors the one in ToKeys().
if l4.AllowsAllAtL3() && !l4.HasL3DependentL7Rules() {
return
}
// Push endpoint policy changes.
//
// `l4.policy` is set to nil when the filter is detached so
// that we could not push updates on a stale policy.
l4Policy := (*L4Policy)(atomic.LoadPointer(&l4.policy))
if l4Policy != nil {
direction := trafficdirection.Egress
if l4.Ingress {
direction = trafficdirection.Ingress
}
l4Policy.AccumulateMapChanges(added, deleted, uint16(l4.Port), uint8(l4.U8Proto), direction)
}
}
func (l4 *L4Filter) cacheIdentitySelector(sel api.EndpointSelector, selectorCache *SelectorCache) CachedSelector {
cs, added := selectorCache.AddIdentitySelector(l4, sel)
if added {
l4.CachedSelectors = append(l4.CachedSelectors, cs)
}
return cs
}
func (l4 *L4Filter) cacheIdentitySelectors(selectors api.EndpointSelectorSlice, selectorCache *SelectorCache) {
for _, sel := range selectors {
l4.cacheIdentitySelector(sel, selectorCache)
}
}
func (l4 *L4Filter) cacheFQDNSelectors(selectors api.FQDNSelectorSlice, selectorCache *SelectorCache) {
for _, fqdnSel := range selectors {
l4.cacheFQDNSelector(fqdnSel, selectorCache)
}
}
func (l4 *L4Filter) cacheFQDNSelector(sel api.FQDNSelector, selectorCache *SelectorCache) CachedSelector {
cs, added := selectorCache.AddFQDNSelector(l4, sel)
if added {
l4.CachedSelectors = append(l4.CachedSelectors, cs)
}
return cs
}
// GetRelevantRulesForKafka returns the relevant rules based on the remote numeric identity.
func (l7 L7DataMap) GetRelevantRulesForKafka(nid identity.NumericIdentity) []api.PortRuleKafka {
var rules []api.PortRuleKafka
for cs, r := range l7 {
if cs.IsWildcard() || cs.Selects(nid) {
rules = append(rules, r.Kafka...)
}
}
return rules
}
func (l7 L7DataMap) addRulesForEndpoints(rules api.L7Rules, endpoints []CachedSelector) {
if rules.Len() == 0 {
return
}
for _, epsel := range endpoints {
l7[epsel] = rules
}
}
// createL4Filter creates a filter for L4 policy that applies to the specified
// endpoints and port/protocol, with reference to the original rules that the
// filter is derived from. This filter may be associated with a series of L7
// rules via the `rule` parameter.
// Not called with an empty peerEndpoints.
func createL4Filter(peerEndpoints api.EndpointSelectorSlice, rule api.PortRule, port api.PortProtocol,
protocol api.L4Proto, ruleLabels labels.LabelArray, ingress bool, selectorCache *SelectorCache, fqdns api.FQDNSelectorSlice) *L4Filter {
// already validated via PortRule.Validate()
p, _ := strconv.ParseUint(port.Port, 0, 16)
// already validated via L4Proto.Validate()
u8p, _ := u8proto.ParseProtocol(string(protocol))
l4 := &L4Filter{
Port: int(p),
Protocol: protocol,
U8Proto: u8p,
L7RulesPerEp: make(L7DataMap),
DerivedFromRules: labels.LabelArrayList{ruleLabels},
Ingress: ingress,
}
if peerEndpoints.SelectsAllEndpoints() {
l4.cacheIdentitySelector(api.WildcardEndpointSelector, selectorCache)
l4.allowsAllAtL3 = true
} else {
l4.CachedSelectors = make(CachedSelectorSlice, 0, len(peerEndpoints))
l4.cacheIdentitySelectors(peerEndpoints, selectorCache)
l4.cacheFQDNSelectors(fqdns, selectorCache)
}
if protocol == api.ProtoTCP && rule.Rules != nil {
switch {
case len(rule.Rules.HTTP) > 0:
l4.L7Parser = ParserTypeHTTP
case len(rule.Rules.Kafka) > 0:
l4.L7Parser = ParserTypeKafka
case rule.Rules.L7Proto != "":
l4.L7Parser = (L7ParserType)(rule.Rules.L7Proto)
}
if !rule.Rules.IsEmpty() {
l4.L7RulesPerEp.addRulesForEndpoints(*rule.Rules, l4.CachedSelectors)
}
}
// we need this to redirect DNS UDP (or ANY, which is more useful)
if !rule.Rules.IsEmpty() && len(rule.Rules.DNS) > 0 {
l4.L7Parser = ParserTypeDNS
l4.L7RulesPerEp.addRulesForEndpoints(*rule.Rules, l4.CachedSelectors)
}
return l4
}
// detach releases the references held in the L4Filter and must be called before
// the filter is left to be garbage collected.
func (l4 *L4Filter) detach(selectorCache *SelectorCache) {
selectorCache.RemoveSelectors(l4.CachedSelectors, l4)
l4.attach(nil)
}
func (l4 *L4Filter) attach(l4Policy *L4Policy) {
atomic.StorePointer(&l4.policy, unsafe.Pointer(l4Policy))
}
// createL4IngressFilter creates a filter for L4 policy that applies to the
// specified endpoints and port/protocol for ingress traffic, with reference
// to the original rules that the filter is derived from. This filter may be
// associated with a series of L7 rules via the `rule` parameter.
//
// hostWildcardL7 determines if L7 traffic from Host should be
// wildcarded (in the relevant daemon mode).
func createL4IngressFilter(fromEndpoints api.EndpointSelectorSlice, hostWildcardL7 bool, rule api.PortRule, port api.PortProtocol,
protocol api.L4Proto, ruleLabels labels.LabelArray, selectorCache *SelectorCache) *L4Filter {
filter := createL4Filter(fromEndpoints, rule, port, protocol, ruleLabels, true, selectorCache, nil)
// If the filter would apply L7 rules for the Host, when we should accept everything from host,
// then wildcard Host at L7.
if !rule.Rules.IsEmpty() && hostWildcardL7 {
for _, cs := range filter.CachedSelectors {
if cs.Selects(identity.ReservedIdentityHost) {
hostSelector := api.ReservedEndpointSelectors[labels.IDNameHost]
hcs := filter.cacheIdentitySelector(hostSelector, selectorCache)
filter.L7RulesPerEp[hcs] = api.L7Rules{}
}
}
}
return filter
}
// createL4EgressFilter creates a filter for L4 policy that applies to the
// specified endpoints and port/protocol for egress traffic, with reference
// to the original rules that the filter is derived from. This filter may be
// associated with a series of L7 rules via the `rule` parameter.
func createL4EgressFilter(toEndpoints api.EndpointSelectorSlice, rule api.PortRule, port api.PortProtocol,
protocol api.L4Proto, ruleLabels labels.LabelArray, selectorCache *SelectorCache, fqdns api.FQDNSelectorSlice) *L4Filter {
return createL4Filter(toEndpoints, rule, port, protocol, ruleLabels, false, selectorCache, fqdns)
}
// IsRedirect returns true if the L4 filter contains a port redirection
func (l4 *L4Filter) IsRedirect() bool {
return l4.L7Parser != ParserTypeNone
}
// MarshalIndent returns the `L4Filter` in indented JSON string.
func (l4 *L4Filter) MarshalIndent() string {
b, err := json.MarshalIndent(l4, "", " ")
if err != nil {
b = []byte("\"L4Filter error: " + err.Error() + "\"")
}
return string(b)
}
// String returns the `L4Filter` in a human-readable string.
func (l4 *L4Filter) String() string {
b, err := json.Marshal(l4)
if err != nil {
return err.Error()
}
return string(b)
}
// Note: Only used for policy tracing
func (l4 *L4Filter) matchesLabels(labels labels.LabelArray) bool {
if l4.AllowsAllAtL3() {
return true
} else if len(labels) == 0 {
return false
}
for _, sel := range l4.CachedSelectors {
// slow, but OK for tracing
if idSel, ok := sel.(*labelIdentitySelector); ok && idSel.xxxMatches(labels) {
return true
}
}
return false
}
// L4PolicyMap is a list of L4 filters indexable by protocol/port
// key format: "port/proto"
type L4PolicyMap map[string]*L4Filter
// Detach removes the cached selectors held by L4PolicyMap from the
// selectorCache, allowing the map to be garbage collected when there
// are no more references to it.
func (l4 L4PolicyMap) Detach(selectorCache *SelectorCache) {
for _, f := range l4 {
f.detach(selectorCache)
}
}
// Attach makes all the L4Filters to point back to the L4Policy that contains them.
func (l4 L4PolicyMap) Attach(l4Policy *L4Policy) {
for _, f := range l4 {
f.attach(l4Policy)
}
}
// HasRedirect returns true if at least one L4 filter contains a port
// redirection
func (l4 L4PolicyMap) HasRedirect() bool {
for _, f := range l4 {
if f.IsRedirect() {
return true
}
}
return false
}
// containsAllL3L4 checks if the L4PolicyMap contains all L4 ports in `ports`.
// For L4Filters that specify ToEndpoints or FromEndpoints, uses `labels` to
// determine whether the policy allows L4 communication between the corresponding
// endpoints.
// Returns api.Denied in the following conditions:
// * If a single port is not present in the `L4PolicyMap` and is not allowed
// by the distilled L3 policy
// * If a port is present in the `L4PolicyMap`, but it applies ToEndpoints or
// FromEndpoints constraints that require labels not present in `labels`.
// Otherwise, returns api.Allowed.
//
// Note: Only used for policy tracing
func (l4 L4PolicyMap) containsAllL3L4(labels labels.LabelArray, ports []*models.Port) api.Decision {
if len(l4) == 0 {
return api.Allowed
}
// Check L3-only filters first.
filter, match := l4[api.PortProtocolAny]
if match && filter.matchesLabels(labels) {
return api.Allowed
}
for _, l4Ctx := range ports {
lwrProtocol := l4Ctx.Protocol
switch lwrProtocol {
case "", models.PortProtocolANY:
tcpPort := fmt.Sprintf("%d/TCP", l4Ctx.Port)
tcpFilter, tcpmatch := l4[tcpPort]
if tcpmatch {
tcpmatch = tcpFilter.matchesLabels(labels)
}
udpPort := fmt.Sprintf("%d/UDP", l4Ctx.Port)
udpFilter, udpmatch := l4[udpPort]
if udpmatch {
udpmatch = udpFilter.matchesLabels(labels)
}
if !tcpmatch && !udpmatch {
return api.Denied
}
default:
port := fmt.Sprintf("%d/%s", l4Ctx.Port, lwrProtocol)
filter, match := l4[port]
if !match || !filter.matchesLabels(labels) {
return api.Denied
}
}
}
return api.Allowed
}
type L4Policy struct {
Ingress L4PolicyMap
Egress L4PolicyMap
// Revision is the repository revision used to generate this policy.
Revision uint64
// Endpoint policies using this L4Policy
// These are circular references, cleaned up in Detach()
mutex lock.RWMutex
users map[*EndpointPolicy]struct{}
}
// NewL4Policy creates a new L4Policy
func NewL4Policy(revision uint64) *L4Policy {
return &L4Policy{
Ingress: L4PolicyMap{},
Egress: L4PolicyMap{},
Revision: revision,
users: make(map[*EndpointPolicy]struct{}),
}
}
// insertUser adds a user to the L4Policy so that incremental
// updates of the L4Policy may be forwarded to the users of it.
func (l4 *L4Policy) insertUser(user *EndpointPolicy) {
l4.mutex.Lock()
// 'users' is set to nil when the policy is detached. This
// happens to the old policy when it is being replaced with a
// new one, or when the last endpoint using this policy is
// removed.
// In the case of an policy update it is possible that an
// endpoint has started regeneration before the policy was
// updated, and that the policy was updated before the said
// endpoint reached this point. In this case the endpoint's
// policy is going to be recomputed soon after and we do
// nothing here.
if l4.users != nil {
l4.users[user] = struct{}{}
}
l4.mutex.Unlock()
}
// AccumulateMapChanges distributes the given changes to the registered users.
//
// The caller is responsible for making sure the same identity is not
// present in both 'adds' and 'deletes'.
func (l4 *L4Policy) AccumulateMapChanges(adds, deletes []identity.NumericIdentity,
port uint16, proto uint8, direction trafficdirection.TrafficDirection) {
l4.mutex.RLock()
for epPolicy := range l4.users {
epPolicy.PolicyMapChanges.AccumulateMapChanges(adds, deletes, port, proto, direction)
}
l4.mutex.RUnlock()
}
// Detach makes the L4Policy ready for garbage collection, removing
// circular pointer references.
// Note that the L4Policy itself is not modified in any way, so that it may still
// be used concurrently.
func (l4 *L4Policy) Detach(selectorCache *SelectorCache) {
l4.Ingress.Detach(selectorCache)
l4.Egress.Detach(selectorCache)
l4.mutex.Lock()
l4.users = nil
l4.mutex.Unlock()
}
// Attach makes all the L4Filters to point back to the L4Policy that contains them.
// This is done before the L4Policy is exposed to concurrent access.
func (l4 *L4Policy) Attach() {
l4.Ingress.Attach(l4)
l4.Egress.Attach(l4)
}
// IngressCoversContext checks if the receiver's ingress L4Policy contains
// all `dPorts` and `labels`.
//
// Note: Only used for policy tracing
func (l4 *L4PolicyMap) IngressCoversContext(ctx *SearchContext) api.Decision {
return l4.containsAllL3L4(ctx.From, ctx.DPorts)
}
// EgressCoversContext checks if the receiver's egress L4Policy contains
// all `dPorts` and `labels`.
//
// Note: Only used for policy tracing
func (l4 *L4PolicyMap) EgressCoversContext(ctx *SearchContext) api.Decision {
return l4.containsAllL3L4(ctx.To, ctx.DPorts)
}
// HasRedirect returns true if the L4 policy contains at least one port redirection
func (l4 *L4Policy) HasRedirect() bool {
return l4 != nil && (l4.Ingress.HasRedirect() || l4.Egress.HasRedirect())
}
// RequiresConntrack returns true if if the L4 configuration requires
// connection tracking to be enabled.
func (l4 *L4Policy) RequiresConntrack() bool {
return l4 != nil && (len(l4.Ingress) > 0 || len(l4.Egress) > 0)
}
func (l4 *L4Policy) GetModel() *models.L4Policy {
if l4 == nil {
return nil
}
ingress := []*models.PolicyRule{}
for _, v := range l4.Ingress {
ingress = append(ingress, &models.PolicyRule{
Rule: v.MarshalIndent(),
DerivedFromRules: v.DerivedFromRules.GetModel(),
})
}
egress := []*models.PolicyRule{}
for _, v := range l4.Egress {
egress = append(egress, &models.PolicyRule{
Rule: v.MarshalIndent(),
DerivedFromRules: v.DerivedFromRules.GetModel(),
})
}
return &models.L4Policy{
Ingress: ingress,
Egress: egress,
}
}