-
Notifications
You must be signed in to change notification settings - Fork 51
/
aclcache.go
141 lines (110 loc) · 3.35 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
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
package acls
import (
"errors"
"net"
"go.aporeto.io/enforcerd/trireme-lib/policy"
)
// 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
}
// NewACLCache 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
}
}
return
}
// RemoveRulesForAddress is going to remove all rules for the provided address, protocol and ports.
func (c *ACLCache) RemoveRulesForAddress(address *Address, protocol string, ports []string, policy *policy.FlowPolicy) error {
if err := c.reject.removeFromCache(address.IP, address.Mask, address.NoMatch, protocol, ports, policy); err != nil {
return err
}
if err := c.accept.removeFromCache(address.IP, address.Mask, address.NoMatch, protocol, ports, policy); err != nil {
return err
}
if err := c.observe.removeFromCache(address.IP, address.Mask, address.NoMatch, protocol, ports, policy); err != nil {
return err
}
return nil
}
// RemoveIPMask removes the entries indexed with (ip, mask). This is an idempotent operation
// and thus does not returns an error
func (c *ACLCache) RemoveIPMask(ip net.IP, mask int) {
c.reject.removeIPMask(ip, mask)
c.accept.removeIPMask(ip, mask)
c.observe.removeIPMask(ip, mask)
}
// GetMatchingAction gets the action from the acl cache
func (c *ACLCache) GetMatchingAction(ip net.IP, port uint16, proto uint8, defaultFlowPolicy *policy.FlowPolicy) (report *policy.FlowPolicy, packet *policy.FlowPolicy, err error) {
report, packet, err = c.reject.getMatchingAction(ip, port, proto, report)
if err == nil {
return
}
report, packet, err = c.accept.getMatchingAction(ip, port, proto, report)
if err == nil {
return
}
report, packet, err = c.observe.getMatchingAction(ip, port, proto, report)
if err == nil {
return
}
if report == nil {
report = defaultFlowPolicy
}
if packet == nil {
packet = defaultFlowPolicy
}
if defaultFlowPolicy.Action.Accepted() {
return report, packet, nil
}
return report, packet, errors.New("no match")
}
// GetMatchingICMPAction gets the action based on icmp policy
func (c *ACLCache) GetMatchingICMPAction(ip net.IP, icmpType, icmpCode int8, defaultFlowPolicy *policy.FlowPolicy) (report *policy.FlowPolicy, packet *policy.FlowPolicy, err error) {
report, packet, err = c.reject.matchICMPRule(ip, icmpType, icmpCode)
if err == nil {
return
}
report, packet, err = c.accept.matchICMPRule(ip, icmpType, icmpCode)
if err == nil {
return
}
report, packet, err = c.observe.matchICMPRule(ip, icmpType, icmpCode)
if err == nil {
return
}
if report == nil {
report = defaultFlowPolicy
}
if packet == nil {
packet = defaultFlowPolicy
}
if defaultFlowPolicy.Action.Accepted() {
return report, packet, nil
}
return report, packet, errors.New("no match")
}