-
Notifications
You must be signed in to change notification settings - Fork 0
/
iptables.go
351 lines (291 loc) · 10.9 KB
/
iptables.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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package main
import (
"context"
"fmt"
"net"
"github.com/coreos/go-iptables/iptables"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
pb "github.com/lovi-cloud/teleskop/protoc/agent"
)
const (
maxChainNameLength = 29
tableFilter = "filter"
chainINPUT = "INPUT"
chainFORWARD = "FORWARD"
chainCallistoSGFallback = "callisto-sg-fallback"
chainCallistoSG = "callisto-sg-chain"
chainCallistoINPUT = "callisto-INPUT"
chainCallistoFORWARD = "callisto-FORWARD"
chainCallistoINPUTPrefix = "callisto-i"
chainCallistoOUTPUPrefix = "callisto-o"
chainCallistoSOURCEPrefix = "callisto-s"
actionACCEPT = "ACCEPT"
actionDROP = "DROP"
actionRETURN = "RETURN"
)
var (
setupFunctions = []setupFunction{
setupSGFallbackChain,
setupSGChain,
setupINPUT,
setupFORWARD,
}
addFunctions = []addFunction{
addSOURCESGRules,
addOUTPUTSGRules,
addINPUTSGRules,
addSGRules,
addINPUTRules,
addFORWARDRules,
}
ruleSGFallback = []string{
"-m", "comment", "--comment", "Default drop rule for unmatched traffic.", "-j", actionDROP,
}
ruleINPUT = []string{
"-j", chainCallistoINPUT,
}
ruleFORWARD = []string{
"-j", chainCallistoFORWARD,
}
)
type setupFunction func(ctx context.Context, client *iptables.IPTables) error
type addFunction func(ctx context.Context, client *iptables.IPTables, intf link) error
type link struct {
Name string
IPAddress net.IP
MACAddress net.HardwareAddr
}
func (a *agent) GetIPTables(ctx context.Context, req *pb.GetIPTablesRequest) (*pb.GetIPTablesResponse, error) {
return &pb.GetIPTablesResponse{}, nil
}
func (a *agent) SetupDefaultSecurityGroup(ctx context.Context, req *pb.SetupDefaultSecurityGroupRequest) (*pb.SetupDefaultSecurityGroupResponse, error) {
client, err := iptables.New()
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to create iptables client: %+v", err)
}
if err := setupDefaultSecurityGroup(ctx, client); err != nil {
return nil, err
}
return &pb.SetupDefaultSecurityGroupResponse{}, nil
}
func (a *agent) AddSecurityGroup(ctx context.Context, req *pb.AddSecurityGroupRequest) (*pb.AddSecurityGroupResponse, error) {
client, err := iptables.New()
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to create iptables client: %+v", err)
}
ipAddr := net.ParseIP(req.IpAddress)
if ipAddr == nil {
return nil, status.Errorf(codes.InvalidArgument, "failed to parse request IP address: %s", "")
}
macAddr, err := net.ParseMAC(req.MacAddress)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "failed to parse request MAC address: %+v", err)
}
err = addSecurityGroup(ctx, client, link{
Name: req.Interface,
IPAddress: ipAddr,
MACAddress: macAddr,
})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to add security group: %+v", err)
}
return &pb.AddSecurityGroupResponse{}, nil
}
func setupDefaultSecurityGroup(ctx context.Context, client *iptables.IPTables) error {
for _, fn := range setupFunctions {
if err := fn(ctx, client); err != nil {
return err
}
}
return nil
}
func addSecurityGroup(ctx context.Context, client *iptables.IPTables, intf link) error {
for _, fn := range addFunctions {
if err := fn(ctx, client, intf); err != nil {
return err
}
}
return nil
}
func setupChain(ctx context.Context, client *iptables.IPTables, name string) error {
if err := client.NewChain(tableFilter, name); err != nil {
return status.Errorf(codes.Internal, "failed to create new chain: %+v", err)
}
return nil
}
func setupSGFallbackChain(ctx context.Context, client *iptables.IPTables) error {
if err := setupChain(ctx, client, chainCallistoSGFallback); err != nil {
return err
}
if err := client.AppendUnique(tableFilter, chainCallistoSGFallback, ruleSGFallback...); err != nil {
return status.Errorf(codes.Internal, "failed to append new rule: %+v", err)
}
return nil
}
func setupSGChain(ctx context.Context, client *iptables.IPTables) error {
if err := setupChain(ctx, client, chainCallistoSG); err != nil {
return err
}
if err := client.AppendUnique(tableFilter, chainCallistoSG,
"-j", actionACCEPT,
); err != nil {
return status.Errorf(codes.Internal, "failed to append new rule: %+v", err)
}
return nil
}
func setupINPUT(ctx context.Context, client *iptables.IPTables) error {
if err := setupChain(ctx, client, chainCallistoINPUT); err != nil {
return err
}
if err := client.AppendUnique(tableFilter, chainINPUT, ruleINPUT...); err != nil {
return status.Errorf(codes.Internal, "failed to append new rule: %+v", err)
}
return nil
}
func setupFORWARD(ctx context.Context, client *iptables.IPTables) error {
if err := setupChain(ctx, client, chainCallistoFORWARD); err != nil {
return err
}
if err := client.AppendUnique(tableFilter, chainFORWARD, ruleFORWARD...); err != nil {
return status.Errorf(codes.Internal, "failed to append new rule: %+v", err)
}
return nil
}
func addSOURCESGRules(ctx context.Context, client *iptables.IPTables, intf link) error {
var err error
chain := getSOURCEChainName(intf)
if err := setupChain(ctx, client, chain); err != nil {
return err
}
rules := [][]string{
{"-s", fmt.Sprintf("%s/32", intf.IPAddress), "-m", "mac", "--mac-source", intf.MACAddress.String(), "-m", "comment", "--comment", "Allow traffic from defined IP/MAC pairs.", "-j", actionRETURN},
{"-m", "comment", "--comment", "Drop traffic without an IP/MAC allow rule.", "-j", actionDROP},
}
for i, rule := range rules {
err = client.Insert(tableFilter, chain, i+1, rule...)
if err != nil {
client.ClearChain(tableFilter, chain)
client.DeleteChain(tableFilter, chain)
return status.Errorf(codes.Internal, "failed to append new %s rule: %+v", chain, err)
}
}
return nil
}
func addINPUTSGRules(ctx context.Context, client *iptables.IPTables, intf link) error {
var err error
chain := getINPUTChainName(intf)
if err := setupChain(ctx, client, chain); err != nil {
return err
}
rules := [][]string{
{"-m", "state", "--state", "RELATED,ESTABLISHED", "-m", "comment", "--comment", "Direct packets associated with a known session to the RETURN chain.", "-j", actionRETURN},
{"-p", "udp", "-m", "udp", "--sport", "67", "--dport", "68", "-j", actionRETURN},
{"-p", "tcp", "-m", "tcp", "-m", "multiport", "--dports", "1:65535", "-j", actionRETURN},
{"-p", "udp", "-m", "udp", "-m", "multiport", "--dports", "1:65535", "-j", actionRETURN},
{"-p", "icmp", "-j", actionRETURN},
{"-m", "comment", "--comment", "Send unmatched traffic to the fallback chain.", "-j", chainCallistoSGFallback},
}
for i, rule := range rules {
err = client.Insert(tableFilter, chain, i+1, rule...)
if err != nil {
client.ClearChain(tableFilter, chain)
client.DeleteChain(tableFilter, chain)
return status.Errorf(codes.Internal, "failed to append new %s rule: %+v", chain, err)
}
}
return nil
}
func addOUTPUTSGRules(ctx context.Context, client *iptables.IPTables, intf link) error {
var err error
chain := getOUTPUTChainName(intf)
if err := setupChain(ctx, client, chain); err != nil {
return err
}
rules := [][]string{
{"-p", "udp", "-m", "udp", "--sport", "68", "--dport", "67", "-m", "comment", "--comment", "Allow DHCP client traffic.", "-j", actionRETURN},
{"-j", getSOURCEChainName(intf)},
{"-p", "udp", "-m", "udp", "--sport", "67", "--dport", "68", "-m", "comment", "--comment", "Prevent DHCP Spoofing by VM.", "-j", actionDROP},
{"-m", "state", "--state", "RELATED,ESTABLISHED", "-m", "comment", "--comment", "Direct packets associated with a known session to the RETURN chain.", "-j", actionRETURN},
{"-p", "tcp", "-m", "tcp", "-m", "multiport", "--dports", "1:65535", "-j", actionRETURN},
{"-p", "udp", "-m", "udp", "-m", "multiport", "--dports", "1:65535", "-j", actionRETURN},
{"-p", "icmp", "-j", actionRETURN},
{"-j", actionRETURN},
{"-m", "comment", "--comment", "Send unmatched traffic to the fallback chain.", "-j", chainCallistoSGFallback},
}
for i, rule := range rules {
err = client.Insert(tableFilter, chain, i+1, rule...)
if err != nil {
client.ClearChain(tableFilter, chain)
client.DeleteChain(tableFilter, chain)
return status.Errorf(codes.Internal, "failed to append new %s rule: %+v", chain, err)
}
}
return nil
}
func addSGRules(ctx context.Context, client *iptables.IPTables, intf link) error {
var err error
chain := chainCallistoSG
rules := [][]string{
{"-m", "physdev", "--physdev-out", intf.Name, "--physdev-is-bridged", "-m", "comment", "--comment", "Jump to the VM specific chain.", "-j", getINPUTChainName(intf)},
{"-m", "physdev", "--physdev-in", intf.Name, "--physdev-is-bridged", "-m", "comment", "--comment", "Jump to the VM specific chain.", "-j", getOUTPUTChainName(intf)},
}
for i, rule := range rules {
err = client.Insert(tableFilter, chain, i+1, rule...)
if err != nil {
client.ClearChain(tableFilter, chain)
client.DeleteChain(tableFilter, chain)
return status.Errorf(codes.Internal, "failed to append new %s rule: %+v", chain, err)
}
}
return nil
}
func addINPUTRules(ctx context.Context, client *iptables.IPTables, intf link) error {
var err error
chain := chainCallistoFORWARD
rules := [][]string{
{"-m", "physdev", "--physdev-in", intf.Name, "--physdev-is-bridged", "-m", "comment", "--comment", "Direct incoming traffic from VM to the security group chain.", "-j", getOUTPUTChainName(intf)},
}
for i, rule := range rules {
err = client.Insert(tableFilter, chain, i+1, rule...)
if err != nil {
client.ClearChain(tableFilter, chain)
client.DeleteChain(tableFilter, chain)
return status.Errorf(codes.Internal, "failed to append new %s rule: %+v", chain, err)
}
}
return nil
}
func addFORWARDRules(ctx context.Context, client *iptables.IPTables, intf link) error {
var err error
chain := chainCallistoFORWARD
rules := [][]string{
{"-m", "physdev", "--physdev-out", intf.Name, "--physdev-is-bridged", "-m", "comment", "--comment", "Direct traffic from the VM interface to the security group chain.", "-j", chainCallistoSG},
{"-m", "physdev", "--physdev-in", intf.Name, "--physdev-is-bridged", "-m", "comment", "--comment", "Direct traffic from the VM interface to the security group chain.", "-j", chainCallistoSG},
}
for i, rule := range rules {
err = client.Insert(tableFilter, chain, i+1, rule...)
if err != nil {
client.ClearChain(tableFilter, chain)
client.DeleteChain(tableFilter, chain)
return status.Errorf(codes.Internal, "failed to append new %s rule: %+v", chain, err)
}
}
return nil
}
func getINPUTChainName(intf link) string {
return getValidChainName(chainCallistoINPUTPrefix, intf.Name)
}
func getOUTPUTChainName(intf link) string {
return getValidChainName(chainCallistoOUTPUPrefix, intf.Name)
}
func getSOURCEChainName(intf link) string {
return getValidChainName(chainCallistoSOURCEPrefix, intf.Name)
}
func getValidChainName(prefix, val string) string {
tmp := fmt.Sprintf("%s%s", prefix, val)
if len(tmp) > maxChainNameLength {
return tmp[:maxChainNameLength]
}
return tmp
}