/
custom_chain.go
252 lines (215 loc) · 5.42 KB
/
custom_chain.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package iptables
import (
"fmt"
"strings"
"github.com/mattn/go-shellwords"
"github.com/cilium/cilium/pkg/option"
)
type customChain struct {
name string
table string
hook string
feederArgs []string
ipv6 bool // ip6tables chain in addition to iptables chain
}
// ciliumChains is the list of custom iptables chain used by Cilium. Custom
// chains are used to allow for simple replacements of all rules.
//
// WARNING: If you change or remove any of the feeder rules you have to ensure
// that the old feeder rules is also removed on agent start, otherwise,
// flushing and removing the custom chains will fail.
var ciliumChains = []customChain{
{
name: ciliumInputChain,
table: "filter",
hook: "INPUT",
feederArgs: []string{""},
ipv6: true,
},
{
name: ciliumOutputChain,
table: "filter",
hook: "OUTPUT",
feederArgs: []string{""},
ipv6: true,
},
{
name: ciliumOutputRawChain,
table: "raw",
hook: "OUTPUT",
feederArgs: []string{""},
ipv6: true,
},
{
name: ciliumPostNatChain,
table: "nat",
hook: "POSTROUTING",
feederArgs: []string{""},
ipv6: true,
},
{
name: ciliumOutputNatChain,
table: "nat",
hook: "OUTPUT",
feederArgs: []string{""},
},
{
name: ciliumPreNatChain,
table: "nat",
hook: "PREROUTING",
feederArgs: []string{""},
},
{
name: ciliumPostMangleChain,
table: "mangle",
hook: "POSTROUTING",
feederArgs: []string{""},
},
{
name: ciliumPreMangleChain,
table: "mangle",
hook: "PREROUTING",
feederArgs: []string{""},
ipv6: true,
},
{
name: ciliumPreRawChain,
table: "raw",
hook: "PREROUTING",
feederArgs: []string{""},
ipv6: true,
},
{
name: ciliumForwardChain,
table: "filter",
hook: "FORWARD",
feederArgs: []string{""},
ipv6: true,
},
}
func (c *customChain) exists(prog iptablesInterface) (bool, error) {
args := []string{"-t", c.table, "-L", c.name}
output, err := prog.runProgOutput(args)
if err != nil {
if strings.Contains(err.Error(), "No chain/target/match by that name.") {
return false, nil
}
return false, fmt.Errorf("unable to list %s chain: %s (%w)", c.name, string(output), err)
}
return true, nil
}
func (c *customChain) doAdd(prog iptablesInterface) error {
args := []string{"-t", c.table, "-N", c.name}
output, err := prog.runProgOutput(args)
if err != nil {
return fmt.Errorf("unable to add %s chain: %s (%w)", c.name, string(output), err)
}
return nil
}
func (c *customChain) add(ipv4, ipv6 bool) error {
if ipv4 {
if err := c.doAdd(ip4tables); err != nil {
return err
}
}
if ipv6 && c.ipv6 {
if err := c.doAdd(ip6tables); err != nil {
return err
}
}
return nil
}
func (c *customChain) doRename(prog iptablesInterface, newName string) error {
if exists, err := c.exists(prog); err != nil {
return err
} else if !exists {
return nil
}
args := []string{"-t", c.table, "-E", c.name, newName}
output, err := prog.runProgOutput(args)
if err != nil {
return fmt.Errorf("unable to rename %s chain to %s: %s (%w)", c.name, newName, string(output), err)
}
return nil
}
func (c *customChain) rename(ipv4, ipv6 bool, name string) error {
if ipv4 {
if err := c.doRename(ip4tables, name); err != nil {
return err
}
}
if ipv6 && c.ipv6 {
if err := c.doRename(ip6tables, name); err != nil {
return nil
}
}
return nil
}
func (c *customChain) doRemove(prog iptablesInterface) error {
if exists, err := c.exists(prog); err != nil {
return err
} else if !exists {
return nil
}
args := []string{"-t", c.table, "-F", c.name}
output, err := prog.runProgOutput(args)
if err != nil {
return fmt.Errorf("unable to flush %s chain: %s (%w)", c.name, string(output), err)
}
args = []string{"-t", c.table, "-X", c.name}
output, err = prog.runProgOutput(args)
if err != nil {
return fmt.Errorf("unable to remove %s chain: %s (%w)", c.name, string(output), err)
}
return nil
}
func (c *customChain) remove(ipv4, ipv6 bool) error {
if ipv4 {
if err := c.doRemove(ip4tables); err != nil {
return err
}
}
if ipv6 && c.ipv6 {
if err := c.doRemove(ip6tables); err != nil {
return err
}
}
return nil
}
func (c *customChain) doInstallFeeder(prog iptablesInterface, feedArgs string) error {
installMode := "-A"
if option.Config.PrependIptablesChains {
installMode = "-I"
}
feedRule := []string{"-m", "comment", "--comment", feederDescription + " " + c.name, "-j", c.name}
if feedArgs != "" {
argsList, err := shellwords.Parse(feedArgs)
if err != nil {
return fmt.Errorf("cannot parse '%s' rule into argument slice: %w", feedArgs, err)
}
feedRule = append(argsList, feedRule...)
}
args := append([]string{"-t", c.table, installMode, c.hook}, feedRule...)
output, err := prog.runProgOutput(args)
if err != nil {
return fmt.Errorf("unable to install feeder rule for %s chain: %s (%w)", c.name, string(output), err)
}
return nil
}
func (c *customChain) installFeeder(ipv4, ipv6 bool) error {
for _, feedArgs := range c.feederArgs {
if ipv4 {
if err := c.doInstallFeeder(ip4tables, feedArgs); err != nil {
return err
}
}
if ipv6 && c.ipv6 == true {
if err := c.doInstallFeeder(ip6tables, feedArgs); err != nil {
return err
}
}
}
return nil
}