Repository navigation
Expand file tree
/
Copy pathmanager.go
More file actions
309 lines (257 loc) · 7.07 KB
/
Copy pathmanager.go
File metadata and controls
309 lines (257 loc) · 7.07 KB
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
package dockeripv6nat
import (
"errors"
"net"
"strconv"
"github.com/coreos/go-iptables/iptables"
)
type managedNetwork struct {
id string
bridge string
subnet net.IPNet
icc bool
masquerade bool
internal bool
binding net.IP
}
type managedContainer struct {
id string
bridge string
address net.IP
ports []managedPort
}
type managedPort struct {
port uint16
proto string
hostAddress net.IP
hostPort uint16
}
type manager struct {
fw *firewall
hairpinMode bool
}
func NewManager() (*manager, error) {
hairpinMode, err := detectHairpinMode()
if err != nil {
return nil, err
}
fw, err := NewFirewall()
if err != nil {
return nil, err
}
if err := fw.EnsureTableChains(getCustomTableChains()); err != nil {
return nil, err
}
if err := fw.EnsureRules(getBaseRules(hairpinMode)); err != nil {
return nil, err
}
return &manager{
fw: fw,
hairpinMode: hairpinMode,
}, nil
}
func detectHairpinMode() (bool, error) {
// Use the IPv4 firewall to detect if the docker daemon is started with --userland-proxy=false.
ipt, err := iptables.NewWithProtocol(iptables.ProtocolIPv4)
if err != nil {
return false, err
}
hairpinModeOffRulespec := []string{
"!", "-d", "127.0.0.0/8",
"-m", "addrtype",
"--dst-type", "LOCAL",
"-j", "DOCKER",
}
hairpinModeOnRulespec := hairpinModeOffRulespec[3:]
hairpinModeOn, err := ipt.Exists(TableNat, ChainOutput, hairpinModeOnRulespec...)
if err != nil {
return false, err
} else if hairpinModeOn {
return true, nil
}
hairpinModeOff, err := ipt.Exists(TableNat, ChainOutput, hairpinModeOffRulespec...)
if err != nil {
return false, err
} else if hairpinModeOff {
return false, nil
}
return false, errors.New("unable to detect hairpin mode (is the docker daemon running?)")
}
func (m *manager) Cleanup() error {
if err := m.fw.RemoveRules(getBaseRules(m.hairpinMode)); err != nil {
return err
}
if err := m.fw.RemoveTableChains(getCustomTableChains()); err != nil {
return err
}
return nil
}
func (m *manager) ReplaceNetwork(oldNetwork, newNetwork *managedNetwork) error {
return m.applyRules(getRulesForNetwork(oldNetwork, m.hairpinMode), getRulesForNetwork(newNetwork, m.hairpinMode))
}
func (m *manager) ReplaceContainer(oldContainer, newContainer *managedContainer) error {
return m.applyRules(getRulesForContainer(oldContainer, m.hairpinMode), getRulesForContainer(newContainer, m.hairpinMode))
}
func (m *manager) EnsureInterconnectionRules(network *managedNetwork, otherNetworks []*managedNetwork) error {
return m.fw.EnsureRules(getInterconnectionRules(network, otherNetworks))
}
func (m *manager) RemoveInterconnectionRules(network *managedNetwork, otherNetworks []*managedNetwork) error {
return m.fw.RemoveRules(getInterconnectionRules(network, otherNetworks))
}
func (m *manager) applyRules(oldRules, newRules *Ruleset) error {
oldRules = oldRules.Diff(newRules)
if err := m.fw.EnsureRules(newRules); err != nil {
return err
}
if err := m.fw.RemoveRules(oldRules); err != nil {
return err
}
return nil
}
func getCustomTableChains() []TableChain {
return []TableChain{
TableChain{TableFilter, ChainDocker},
TableChain{TableFilter, ChainDockerIsolation},
TableChain{TableNat, ChainDocker},
}
}
func getBaseRules(hairpinMode bool) *Ruleset {
outputRule := NewRule(TableNat, ChainOutput,
"-m", "addrtype",
"--dst-type", "LOCAL",
"-j", ChainDocker)
if !hairpinMode {
outputRule.spec = append(outputRule.spec, "!", "-d", "::1")
}
return &Ruleset{
NewPrependRule(TableFilter, ChainForward,
"-j", ChainDockerIsolation),
NewRule(TableFilter, ChainDockerIsolation,
"-j", "RETURN"),
NewRule(TableNat, ChainPrerouting,
"-m", "addrtype",
"--dst-type", "LOCAL",
"-j", ChainDocker),
outputRule,
}
}
func getRulesForNetwork(network *managedNetwork, hairpinMode bool) *Ruleset {
if network == nil {
return &Ruleset{}
}
if network.internal {
return &Ruleset{
NewPrependRule(TableFilter, ChainDockerIsolation,
"!", "-s", network.subnet.String(),
"-o", network.bridge,
"-j", "DROP"),
NewPrependRule(TableFilter, ChainDockerIsolation,
"!", "-d", network.subnet.String(),
"-i", network.bridge,
"-j", "DROP"),
}
}
iccAction := "ACCEPT"
if !network.icc {
iccAction = "DROP"
}
rs := Ruleset{
NewRule(TableFilter, ChainForward,
"-o", network.bridge,
"-j", ChainDocker),
NewRule(TableFilter, ChainForward,
"-o", network.bridge,
"-m", "conntrack",
"--ctstate", "RELATED,ESTABLISHED",
"-j", "ACCEPT"),
NewRule(TableFilter, ChainForward,
"-i", network.bridge,
"!", "-o", network.bridge,
"-j", "ACCEPT"),
NewRule(TableFilter, ChainForward,
"-i", network.bridge,
"-o", network.bridge,
"-j", iccAction),
}
if network.masquerade {
rs = append(rs, NewPrependRule(TableNat, ChainPostrouting,
"-s", network.subnet.String(),
"!", "-o", network.bridge,
"-j", "MASQUERADE"))
}
if !hairpinMode {
rs = append(rs, NewPrependRule(TableNat, ChainDocker,
"-i", network.bridge,
"-j", "RETURN"))
}
return &rs
}
func getRulesForContainer(container *managedContainer, hairpinMode bool) *Ruleset {
if container == nil {
return &Ruleset{}
}
rs := make(Ruleset, 0, len(container.ports)*3)
for _, port := range container.ports {
rs = append(rs, *getRulesForPort(&port, container, hairpinMode)...)
}
return &rs
}
func getRulesForPort(port *managedPort, container *managedContainer, hairpinMode bool) *Ruleset {
containerPortString := strconv.Itoa(int(port.port))
hostPortString := strconv.Itoa(int(port.hostPort))
hostAddressString := "0/0"
if !port.hostAddress.IsUnspecified() {
hostAddressString = port.hostAddress.String()
}
dnatRule := NewRule(TableNat, ChainDocker,
"-d", hostAddressString,
"-p", port.proto,
"-m", port.proto,
"--dport", hostPortString,
"-j", "DNAT",
"--to-destination", net.JoinHostPort(container.address.String(), containerPortString))
if !hairpinMode {
dnatRule.spec = append(dnatRule.spec, "!", "-i", container.bridge)
}
return &Ruleset{
NewRule(TableFilter, ChainDocker,
"-d", container.address.String(),
"!", "-i", container.bridge,
"-o", container.bridge,
"-p", port.proto,
"-m", port.proto,
"--dport", containerPortString,
"-j", "ACCEPT"),
NewRule(TableNat, ChainPostrouting,
"-s", container.address.String(),
"-d", container.address.String(),
"-p", port.proto,
"-m", port.proto,
"--dport", containerPortString,
"-j", "MASQUERADE"),
dnatRule,
}
}
func getInterconnectionRules(network *managedNetwork, otherNetworks []*managedNetwork) *Ruleset {
if network.internal {
return &Ruleset{}
}
rs := make(Ruleset, 0, len(otherNetworks)*2)
for _, otherNetwork := range otherNetworks {
if otherNetwork.id == network.id {
continue
}
if otherNetwork.internal {
continue
}
rs = append(rs, NewPrependRule(TableFilter, ChainDockerIsolation,
"-i", network.bridge,
"-o", otherNetwork.bridge,
"-j", "DROP"))
rs = append(rs, NewPrependRule(TableFilter, ChainDockerIsolation,
"-i", otherNetwork.bridge,
"-o", network.bridge,
"-j", "DROP"))
}
return &rs
}