/
legacyacls.go
160 lines (147 loc) · 4.64 KB
/
legacyacls.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
package iptablesctrl
// legacyProxyRules creates all the proxy specific rules.
import (
"text/template"
"go.aporeto.io/trireme-lib/common"
"go.aporeto.io/trireme-lib/policy"
"go.uber.org/zap"
)
// This refers to the pu chain rules for pus in older distros like RH 6.9/Ubuntu 14.04. The rules
// consider source ports to identify packets from the process.
func (i *iptables) legacyPuChainRules(contextID, appChain string, netChain string, mark string, tcpPorts, udpPorts string, proxyPort string, proxyPortSetName string,
appSection, netSection string, puType common.PUType, dnsProxyPort string, dnsServerIP string) [][]string {
iptableCgroupSection := appSection
iptableNetSection := netSection
rules := [][]string{}
if tcpPorts != "0" {
rules = append(rules, [][]string{
{
appPacketIPTableContext,
iptableCgroupSection,
"-p", tcpProto,
"-m", "multiport",
"--source-ports", tcpPorts,
"-m", "comment", "--comment", "Server-specific-chain",
"-j", "MARK", "--set-mark", mark,
},
{
appPacketIPTableContext,
iptableCgroupSection,
"-p", tcpProto,
"-m", "multiport",
"--source-ports", tcpPorts,
"-m", "comment", "--comment", "Server-specific-chain",
"-j", appChain,
},
{
netPacketIPTableContext,
iptableNetSection,
"-p", tcpProto,
"-m", "multiport",
"--destination-ports", tcpPorts,
"-m", "comment", "--comment", "Container-specific-chain",
"-j", netChain,
}}...)
}
if udpPorts != "0" {
rules = append(rules, [][]string{
{
appPacketIPTableContext,
iptableCgroupSection,
"-p", udpProto,
"-m", "multiport",
"--source-ports", udpPorts,
"-m", "comment", "--comment", "Server-specific-chain",
"-j", "MARK", "--set-mark", mark,
},
{
appPacketIPTableContext,
iptableCgroupSection,
"-p", udpProto, "-m", "mark", "--mark", mark,
"-m", "addrtype", "--src-type", "LOCAL",
"-m", "addrtype", "--dst-type", "LOCAL",
"-m", "state", "--state", "NEW",
"-j", "NFLOG", "--nflog-group", "10",
"--nflog-prefix", policy.DefaultAcceptLogPrefix(contextID),
},
{
appPacketIPTableContext,
iptableCgroupSection,
"-m", "comment", "--comment", "traffic-same-pu",
"-p", udpProto, "-m", "mark", "--mark", mark,
"-m", "addrtype", "--src-type", "LOCAL",
"-m", "addrtype", "--dst-type", "LOCAL",
"-j", "ACCEPT",
},
{
appPacketIPTableContext,
iptableCgroupSection,
"-p", udpProto,
"-m", "multiport",
"--source-ports", udpPorts,
"-m", "comment", "--comment", "Server-specific-chain",
"-j", appChain,
},
{
netPacketIPTableContext,
iptableNetSection,
"-m", "comment", "--comment", "traffic-same-pu",
"-p", udpProto, "-m", "mark", "--mark", mark,
"-m", "addrtype", "--src-type", "LOCAL",
"-m", "addrtype", "--dst-type", "LOCAL",
"-j", "ACCEPT",
},
{
netPacketIPTableContext,
iptableNetSection,
"-p", udpProto,
"-m", "multiport",
"--destination-ports", udpPorts,
"-m", "comment", "--comment", "Container-specific-chain",
"-j", netChain,
}}...)
}
if puType == common.HostPU {
// Add a capture all traffic rule for host pu. This traps all traffic going out
// of the box.
rules = append(rules, []string{
appPacketIPTableContext,
iptableCgroupSection,
"-m", "comment", "--comment", "capture all outgoing traffic",
"-j", appChain,
})
}
return append(rules, i.legacyProxyRules(tcpPorts, proxyPort, proxyPortSetName, mark, dnsProxyPort, dnsServerIP)...)
}
func (i *iptables) legacyProxyRules(tcpPorts string, proxyPort string, proxyPortSetName string, cgroupMark string, dnsProxyPort string, dnsServerIP string) [][]string {
destSetName, srvSetName := i.getSetNames(proxyPortSetName)
aclInfo := ACLInfo{
MangleTable: appPacketIPTableContext,
NatTable: appProxyIPTableContext,
MangleProxyAppChain: proxyOutputChain,
MangleProxyNetChain: proxyInputChain,
NatProxyNetChain: natProxyInputChain,
NatProxyAppChain: natProxyOutputChain,
CgroupMark: cgroupMark,
DestIPSet: destSetName,
SrvIPSet: srvSetName,
ProxyPort: proxyPort,
ProxyMark: proxyMark,
TCPPorts: tcpPorts,
DNSProxyPort: dnsProxyPort,
DNSServerIP: dnsServerIP,
}
tmpl := template.Must(template.New(legacyProxyRules).Funcs(template.FuncMap{
"isCgroupSet": func() bool {
return cgroupMark != ""
},
"enableDNSProxy": func() bool {
return dnsServerIP != ""
},
}).Parse(legacyProxyRules))
rules, err := extractRulesFromTemplate(tmpl, aclInfo)
if err != nil {
zap.L().Warn("unable to extract rules", zap.Error(err))
}
return rules
}