-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolicy.go
173 lines (140 loc) · 3.6 KB
/
policy.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
package yafw
import (
"encoding/json"
"github.com/google/nftables"
"github.com/google/nftables/expr"
)
type PolicyAction int
const (
PolicyAccept PolicyAction = iota
PolicyDrop
)
func (action PolicyAction) MarshalJSON() ([]byte, error) {
switch action {
case PolicyAccept:
return json.Marshal("accept")
case PolicyDrop:
return json.Marshal("drop")
default:
return json.Marshal("(unknown)")
}
}
func (action *PolicyAction) UnmarshalJSON(data []byte) error {
text := ""
if err := json.Unmarshal(data, &text); err != nil {
return err
}
switch text {
case "accept":
*action = PolicyAccept
case "drop":
*action = PolicyDrop
}
return nil
}
type Policy struct {
ID int `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Log bool `json:"log"`
Action PolicyAction `json:"action"`
Source *Address `json:"source"`
SourceZone string `json:"source_zone"`
Destination *Address `json:"destination"`
DestinationZone string `json:"destination_zone"`
Service *Service `json:"service"`
artifact *PolicyArtifact
}
type PolicyArtifact struct {
Source *nftables.Set
SourceZone *nftables.Set
Destination *nftables.Set
DestinationZone *nftables.Set
}
func (r *Router) Policies() (ret []*Policy) {
ret = make([]*Policy, 0)
for _, entry := range r.policyEntries.All() {
ret = append(ret, entry.(*Policy))
}
return ret
}
// the following contents implement Entry in entry.go
func (policy *Policy) buildArtifact(router *Router) error {
artifact := &PolicyArtifact{}
// if policy.SourceZone != "" {
// zone := router.zones.FindZone(policy.SourceZone)
// if zone != nil {
// artifact.SourceZone = zone.set
// } else {
// return fmt.Errorf("zone \"%s\" not found", policy.SourceZone)
// }
// }
// if policy.DestinationZone != "" {
// zone := router.zones.FindZone(policy.DestinationZone)
// if zone != nil {
// artifact.DestinationZone = zone.set
// } else {
// return fmt.Errorf("zone \"%s\" not found", policy.DestinationZone)
// }
// }
if policy.Source != nil {
set, err := router.addressToSet(policy.Source)
if err != nil {
return err
}
artifact.Source = set
}
if policy.Destination != nil {
set, err := router.addressToSet(policy.Destination)
if err != nil {
return err
}
artifact.Destination = set
}
policy.artifact = artifact
return nil
}
func (policy *Policy) Index() int {
return policy.ID
}
func (policy *Policy) SetIndex(index int) {
policy.ID = index
}
func (policy *Policy) ToRules() []*nftables.Rule {
builder := &ExprBuilder{}
artifact := policy.artifact
if artifact != nil {
if policy.SourceZone != "" && artifact.SourceZone != nil {
builder.MetaIngressInterface(1).LookupSet(1, artifact.SourceZone)
}
if policy.DestinationZone != "" && artifact.DestinationZone != nil {
builder.MetaIngressInterface(1).LookupSet(1, artifact.DestinationZone)
}
if policy.Source != nil && artifact.Source != nil {
builder.PayloadIPSource(1).LookupSet(1, artifact.Source)
}
if policy.Destination != nil && artifact.Destination != nil {
builder.PayloadIPDestination(1).LookupSet(1, artifact.Destination)
}
}
if policy.Service != nil {
builder.AppendGroup(policy.Service.Exprs())
}
if policy.Log {
builder.Append(&expr.Log{
Data: []byte("yafw-policy"),
Flags: expr.LogFlagsIPOpt | expr.LogFlagsTCPOpt,
})
}
switch policy.Action {
case PolicyAccept:
builder.VerdictAccept()
case PolicyDrop:
builder.VerdictDrop()
}
return []*nftables.Rule{
{
Exprs: builder.Exprs(),
},
}
}