-
Notifications
You must be signed in to change notification settings - Fork 51
/
aclcache.go
89 lines (69 loc) · 1.76 KB
/
aclcache.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
package acls
import (
"errors"
"go.aporeto.io/trireme-lib/policy"
)
var catchAllPolicy = &policy.FlowPolicy{Action: policy.Reject, PolicyID: "default", ServiceID: "default"}
// ACLCache holds all the ACLS in an internal DB
// map[prefixes][subnets] -> list of ports with their actions
type ACLCache struct {
reject *acl
accept *acl
observe *acl
}
type prefixRules struct {
mask uint32
rules map[uint32]portActionList
}
// NewACLCache creates a new ACL cache
func NewACLCache() *ACLCache {
return &ACLCache{
reject: newACL(),
accept: newACL(),
observe: newACL(),
}
}
// AddRule adds a single rule to the ACL Cache
func (c *ACLCache) AddRule(rule policy.IPRule) (err error) {
if rule.Policy.ObserveAction.ObserveApply() {
return c.observe.addRule(rule)
}
if rule.Policy.Action.Accepted() {
return c.accept.addRule(rule)
}
return c.reject.addRule(rule)
}
// AddRuleList adds a list of rules to the cache
func (c *ACLCache) AddRuleList(rules policy.IPRuleList) (err error) {
for _, rule := range rules {
if err = c.AddRule(rule); err != nil {
return
}
}
c.reject.reverseSort()
c.accept.reverseSort()
c.observe.reverseSort()
return
}
// GetMatchingAction gets the matching action
func (c *ACLCache) GetMatchingAction(ip []byte, port uint16) (report *policy.FlowPolicy, packet *policy.FlowPolicy, err error) {
report, packet, err = c.reject.getMatchingAction(ip, port, report)
if err == nil {
return
}
report, packet, err = c.accept.getMatchingAction(ip, port, report)
if err == nil {
return
}
report, packet, err = c.observe.getMatchingAction(ip, port, report)
if err == nil {
return
}
if report == nil {
report = catchAllPolicy
}
if packet == nil {
packet = catchAllPolicy
}
return report, packet, errors.New("no match")
}