-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbase.go
216 lines (197 loc) · 5.54 KB
/
base.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
package whalewall
import (
"errors"
"fmt"
"syscall"
"github.com/google/nftables"
"github.com/google/nftables/expr"
"go.uber.org/zap"
)
const (
filterTableName = "filter"
dockerChainName = "DOCKER-USER"
inputChainName = "INPUT"
outputChainName = "OUTPUT"
whalewallChainName = "whalewall"
containerAddrSetName = "whalewall-container-addrs"
)
var (
filterTable = &nftables.Table{
Name: filterTableName,
Family: nftables.TableFamilyIPv4,
}
whalewallChain = &nftables.Chain{
Name: whalewallChainName,
Table: filterTable,
Type: nftables.ChainTypeFilter,
}
containerAddrSet = &nftables.Set{
Table: filterTable,
Name: containerAddrSetName,
IsMap: true,
KeyType: nftables.TypeIPAddr,
DataType: nftables.TypeVerdict,
}
srcJumpRule = &nftables.Rule{
Table: filterTable,
Chain: whalewallChain,
Exprs: []expr.Any{
// [ payload load 4b @ network header + 12 => reg 1 ]
&expr.Payload{
OperationType: expr.PayloadLoad,
Len: 4,
Base: expr.PayloadBaseNetworkHeader,
Offset: 12,
DestRegister: 1,
},
// [ lookup reg 1 set ... dreg 0 0x0 ]
&expr.Lookup{
SourceRegister: 1,
SetName: containerAddrSetName,
DestRegister: 0,
IsDestRegSet: true,
},
},
}
dstJumpRule = &nftables.Rule{
Table: filterTable,
Chain: whalewallChain,
Exprs: []expr.Any{
// [ payload load 4b @ network header + 16 => reg 1 ]
&expr.Payload{
OperationType: expr.PayloadLoad,
Len: 4,
Base: expr.PayloadBaseNetworkHeader,
Offset: 16,
DestRegister: 1,
},
// [ lookup reg 1 set ... dreg 0 0x0 ]
&expr.Lookup{
SourceRegister: 1,
SetName: containerAddrSetName,
DestRegister: 0,
IsDestRegSet: true,
},
},
}
)
func (r *RuleManager) createBaseRules() error {
nfc, err := r.newFirewallClient()
if err != nil {
return fmt.Errorf("error creating netlink connection: %w", err)
}
chains, err := nfc.ListChainsOfTableFamily(nftables.TableFamilyIPv4)
if err != nil {
return fmt.Errorf("error listing IPv4 chains: %w", err)
}
var (
whalewallChainFound bool
dockerChain *nftables.Chain
inputChain *nftables.Chain
outputChain *nftables.Chain
)
for _, c := range chains {
if c.Table.Name != filterTableName {
continue
}
switch c.Name {
case dockerChainName:
dockerChain = c
case whalewallChainName:
whalewallChainFound = true
case inputChainName:
inputChain = c
case outputChainName:
outputChain = c
}
}
if dockerChain == nil {
return errors.New("couldn't find required Docker chain, is Docker running?")
}
// get or create whalewall chain
var mainChainRules []*nftables.Rule
var addContainerJumpRules bool
if !whalewallChainFound {
nfc.AddChain(whalewallChain)
addContainerJumpRules = true
} else {
mainChainRules, err = nfc.GetRules(filterTable, whalewallChain)
if err != nil {
return fmt.Errorf("error listing rules of %q chain: %w", whalewallChainName, err)
}
if len(mainChainRules) == 0 {
addContainerJumpRules = true
}
}
// add rule to jump from DOCKER-USER chain to whalewall chain
dockerRules, err := nfc.GetRules(filterTable, dockerChain)
if err != nil {
return fmt.Errorf("error listing rules of %q chain: %w", dockerChainName, err)
}
dockerUserJumpRule := createJumpRule(dockerChain, whalewallChainName)
if !findRule(r.logger, dockerUserJumpRule, dockerRules) {
nfc.InsertRule(dockerUserJumpRule)
}
// add rule to jump from INPUT/OUTPUT chains to whalewall chain
handleMainChain := func(name string, hook *nftables.ChainHook, mainChain *nftables.Chain) error {
if mainChain == nil {
r.logger.Debug("creating chain", zap.String("chain.name", name))
// INPUT and OUTPUT sometimes don't exist in nftables
mainChain = &nftables.Chain{
Name: name,
Table: filterTable,
Hooknum: hook,
Priority: nftables.ChainPriorityFilter,
Type: nftables.ChainTypeFilter,
Policy: ref(nftables.ChainPolicyAccept),
}
nfc.AddChain(mainChain)
if err := ignoringErr(nfc.Flush, syscall.EEXIST); err != nil {
return fmt.Errorf("error creating chain: %w", err)
}
}
rules, err := nfc.GetRules(filterTable, mainChain)
if err != nil {
return fmt.Errorf("error listing rules of %q chain: %w", name, err)
}
jumpRule := createJumpRule(mainChain, whalewallChainName)
if !findRule(r.logger, jumpRule, rules) {
nfc.InsertRule(jumpRule)
}
return nil
}
if err := handleMainChain(inputChainName, nftables.ChainHookInput, inputChain); err != nil {
return err
}
if err := handleMainChain(outputChainName, nftables.ChainHookOutput, outputChain); err != nil {
return err
}
// create a map that maps container IPs to their respective chain
if err := nfc.AddSet(containerAddrSet, nil); err != nil {
return fmt.Errorf("error adding set %q: %w", containerAddrSetName, err)
}
// create rules to jump to container chain if packet is from/to a container
if addContainerJumpRules || !findRule(r.logger, srcJumpRule, mainChainRules) {
nfc.AddRule(srcJumpRule)
}
if addContainerJumpRules || !findRule(r.logger, dstJumpRule, mainChainRules) {
nfc.AddRule(dstJumpRule)
}
if err := nfc.Flush(); err != nil {
return fmt.Errorf("error flushing nftables commands: %w", err)
}
return nil
}
func createJumpRule(srcChain *nftables.Chain, dstChainName string) *nftables.Rule {
return &nftables.Rule{
Table: filterTable,
Chain: srcChain,
Exprs: []expr.Any{
&expr.Counter{},
&expr.Verdict{
Kind: expr.VerdictJump,
Chain: dstChainName,
},
},
}
}