/
iptables_builder_impl.go
246 lines (219 loc) · 8.18 KB
/
iptables_builder_impl.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
// Copyright Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package builder
import (
"fmt"
"strings"
"istio.io/istio/pkg/util/sets"
"istio.io/istio/tools/istio-iptables/pkg/config"
"istio.io/istio/tools/istio-iptables/pkg/constants"
"istio.io/istio/tools/istio-iptables/pkg/log"
)
// Rule represents iptables rule - chain, table and options
type Rule struct {
chain string
table string
params []string
}
// Rules represents iptables for V4 and V6
type Rules struct {
rulesv4 []*Rule
rulesv6 []*Rule
}
// IptablesRuleBuilder is an implementation for IptablesRuleBuilder interface
type IptablesRuleBuilder struct {
rules Rules
cfg *config.Config
}
// NewIptablesBuilders creates a new IptablesRuleBuilder
func NewIptablesRuleBuilder(cfg *config.Config) *IptablesRuleBuilder {
if cfg == nil {
cfg = &config.Config{}
}
return &IptablesRuleBuilder{
rules: Rules{
rulesv4: []*Rule{},
rulesv6: []*Rule{},
},
cfg: cfg,
}
}
func (rb *IptablesRuleBuilder) InsertRule(command log.Command, chain string, table string, position int, params ...string) *IptablesRuleBuilder {
rb.InsertRuleV4(command, chain, table, position, params...)
rb.InsertRuleV6(command, chain, table, position, params...)
return rb
}
// nolint lll
func (rb *IptablesRuleBuilder) insertInternal(ipt *[]*Rule, command log.Command, chain string, table string, position int, params ...string) *IptablesRuleBuilder {
rules := params
*ipt = append(*ipt, &Rule{
chain: chain,
table: table,
params: append([]string{"-I", chain, fmt.Sprint(position)}, rules...),
})
idx := indexOf("-j", params)
// We have identified the type of command this is and logging is enabled. Insert a rule to log this chain was hit.
// Since this is insert we do this *after* the real chain, which will result in it bumping it forward
if rb.cfg.TraceLogging && idx >= 0 && command != log.UndefinedCommand {
match := params[:idx]
// 1337 group is just a random constant to be matched on the log reader side
// Size of 20 allows reading the IPv4 IP header.
match = append(match, "-j", "NFLOG", "--nflog-prefix", fmt.Sprintf(`%q`, command.Identifier), "--nflog-group", "1337", "--nflog-size", "20")
*ipt = append(*ipt, &Rule{
chain: chain,
table: table,
params: append([]string{"-I", chain, fmt.Sprint(position)}, match...),
})
}
return rb
}
func (rb *IptablesRuleBuilder) InsertRuleV4(command log.Command, chain string, table string, position int, params ...string) *IptablesRuleBuilder {
return rb.insertInternal(&rb.rules.rulesv4, command, chain, table, position, params...)
}
func (rb *IptablesRuleBuilder) InsertRuleV6(command log.Command, chain string, table string, position int, params ...string) *IptablesRuleBuilder {
if !rb.cfg.EnableInboundIPv6 {
return rb
}
return rb.insertInternal(&rb.rules.rulesv6, command, chain, table, position, params...)
}
func indexOf(element string, data []string) int {
for k, v := range data {
if element == v {
return k
}
}
return -1 // not found.
}
func (rb *IptablesRuleBuilder) appendInternal(ipt *[]*Rule, command log.Command, chain string, table string, params ...string) *IptablesRuleBuilder {
idx := indexOf("-j", params)
// We have identified the type of command this is and logging is enabled. Appending a rule to log this chain will be hit
if rb.cfg.TraceLogging && idx >= 0 && command != log.UndefinedCommand {
match := params[:idx]
// 1337 group is just a random constant to be matched on the log reader side
// Size of 20 allows reading the IPv4 IP header.
match = append(match, "-j", "NFLOG", "--nflog-prefix", fmt.Sprintf(`%q`, command.Identifier), "--nflog-group", "1337", "--nflog-size", "20")
*ipt = append(*ipt, &Rule{
chain: chain,
table: table,
params: append([]string{"-A", chain}, match...),
})
}
rules := params
*ipt = append(*ipt, &Rule{
chain: chain,
table: table,
params: append([]string{"-A", chain}, rules...),
})
return rb
}
func (rb *IptablesRuleBuilder) AppendRuleV4(command log.Command, chain string, table string, params ...string) *IptablesRuleBuilder {
return rb.appendInternal(&rb.rules.rulesv4, command, chain, table, params...)
}
func (rb *IptablesRuleBuilder) AppendRule(command log.Command, chain string, table string, params ...string) *IptablesRuleBuilder {
rb.AppendRuleV4(command, chain, table, params...)
rb.AppendRuleV6(command, chain, table, params...)
return rb
}
func (rb *IptablesRuleBuilder) AppendRuleV6(command log.Command, chain string, table string, params ...string) *IptablesRuleBuilder {
if !rb.cfg.EnableInboundIPv6 {
return rb
}
return rb.appendInternal(&rb.rules.rulesv6, command, chain, table, params...)
}
func (rb *IptablesRuleBuilder) buildRules(rules []*Rule) [][]string {
output := make([][]string, 0)
chainTableLookupSet := sets.New[string]()
for _, r := range rules {
chainTable := fmt.Sprintf("%s:%s", r.chain, r.table)
// Create new chain if key: `chainTable` isn't present in map
if !chainTableLookupSet.Contains(chainTable) {
// Ignore chain creation for built-in chains for iptables
if _, present := constants.BuiltInChainsMap[r.chain]; !present {
cmd := []string{"-t", r.table, "-N", r.chain}
output = append(output, cmd)
chainTableLookupSet.Insert(chainTable)
}
}
}
for _, r := range rules {
cmd := append([]string{"-t", r.table}, r.params...)
output = append(output, cmd)
}
return output
}
func (rb *IptablesRuleBuilder) BuildV4() [][]string {
return rb.buildRules(rb.rules.rulesv4)
}
func (rb *IptablesRuleBuilder) BuildV6() [][]string {
return rb.buildRules(rb.rules.rulesv6)
}
func (rb *IptablesRuleBuilder) constructIptablesRestoreContents(tableRulesMap map[string][]string) string {
var b strings.Builder
for table, rules := range tableRulesMap {
if len(rules) > 0 {
_, _ = fmt.Fprintln(&b, "*", table)
for _, r := range rules {
_, _ = fmt.Fprintln(&b, r)
}
_, _ = fmt.Fprintln(&b, "COMMIT")
}
}
return b.String()
}
func (rb *IptablesRuleBuilder) buildRestore(rules []*Rule) string {
tableRulesMap := map[string][]string{
constants.FILTER: {},
constants.NAT: {},
constants.MANGLE: {},
}
chainTableLookupMap := sets.New[string]()
for _, r := range rules {
chainTable := fmt.Sprintf("%s:%s", r.chain, r.table)
// Create new chain if key: `chainTable` isn't present in map
if !chainTableLookupMap.Contains(chainTable) {
// Ignore chain creation for built-in chains for iptables
if _, present := constants.BuiltInChainsMap[r.chain]; !present {
tableRulesMap[r.table] = append(tableRulesMap[r.table], fmt.Sprintf("-N %s", r.chain))
chainTableLookupMap.Insert(chainTable)
}
}
}
for _, r := range rules {
tableRulesMap[r.table] = append(tableRulesMap[r.table], strings.Join(r.params, " "))
}
return rb.constructIptablesRestoreContents(tableRulesMap)
}
func (rb *IptablesRuleBuilder) BuildV4Restore() string {
return rb.buildRestore(rb.rules.rulesv4)
}
func (rb *IptablesRuleBuilder) BuildV6Restore() string {
return rb.buildRestore(rb.rules.rulesv6)
}
// AppendVersionedRule is a wrapper around AppendRule that substitutes an ipv4/ipv6 specific value
// in place in the params. This allows appending a dual-stack rule that has an IP value in it.
func (rb *IptablesRuleBuilder) AppendVersionedRule(ipv4 string, ipv6 string, command log.Command, chain string, table string, params ...string) {
rb.AppendRuleV4(command, chain, table, replaceVersionSpecific(ipv4, params...)...)
rb.AppendRuleV6(command, chain, table, replaceVersionSpecific(ipv6, params...)...)
}
func replaceVersionSpecific(contents string, inputs ...string) []string {
res := make([]string, 0, len(inputs))
for _, i := range inputs {
if i == constants.IPVersionSpecific {
res = append(res, contents)
} else {
res = append(res, i)
}
}
return res
}