/
chains.go
241 lines (211 loc) · 5.44 KB
/
chains.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
package utils
import (
"fmt"
"github.com/google/nftables"
)
var defaultDropPolicy = nftables.ChainPolicyDrop
// IsChainExists checks whether a chain exists.
func IsChainExists(v, tableName, chainName string) (bool, error) {
if err := isSupportedIPVersion(v); err != nil {
return false, err
}
conn, err := initNftConn()
if err != nil {
return false, err
}
chains, err := conn.ListChains()
if err != nil {
return false, err
}
for _, chain := range chains {
if chain == nil {
continue
}
if chain.Name != chainName {
continue
}
if chain.Table.Name != tableName {
continue
}
if v == "4" {
if chain.Table.Family != nftables.TableFamilyIPv4 {
continue
}
} else {
if chain.Table.Family != nftables.TableFamilyIPv6 {
continue
}
}
return true, nil
}
return false, nil
}
// CreateNatPostRoutingChain creates a postrouting chain in nat table.
//
// NF_INET_POST_ROUTING: this hook in the ipfinishoutput() function
// before they leave the computer.
func CreateNatPostRoutingChain(v, tableName, chainName string) error {
return CreateChain(v, tableName, chainName, "nat", "postrouting", "snat")
}
// CreateNatPreRoutingChain creates a prerouting chain in nat table.
//
// NF_INET_PRE_ROUTING: incoming packets pass this hook in the ip_rcv()
// (linux/net/ipv4/ip_input.c) function before they are processed
// by the routing code.
func CreateNatPreRoutingChain(v, tableName, chainName string) error {
return CreateChain(v, tableName, chainName, "nat", "prerouting", "dnat")
}
// CreateNatOutputChain creates an output chain in nat table.
//
// NF_INET_LOCAL_OUT: all outgoing packets created in the local
// computer pass this hook in the function ip_build_and_send_pkt().
func CreateNatOutputChain(v, tableName, chainName string) error {
return CreateChain(v, tableName, chainName, "nat", "output", "dnat")
}
// CreateNatInputChain creates an input chain in nat table.
//
// NF_INET_LOCAL_IN: all incoming packets addressed to the local
// computer pass this hook in the function ip_local_deliver().
func CreateNatInputChain(v, tableName, chainName string) error {
return CreateChain(v, tableName, chainName, "nat", "input", "snat")
}
// CreateRawPreRoutingChain creates a prerouting chain in raw table.
func CreateRawPreRoutingChain(v, tableName, chainName string) error {
return CreateChain(v, tableName, chainName, "filter", "prerouting", "raw")
}
// CreateChain creates NAT chain of a specific type.
func CreateChain(v, tableName, chainName, chainType, chainHookType, chainPriority string) error {
if err := isSupportedIPVersion(v); err != nil {
return err
}
conn, err := initNftConn()
if err != nil {
return err
}
tb := &nftables.Table{
Name: tableName,
}
if v == "4" {
tb.Family = nftables.TableFamilyIPv4
} else {
tb.Family = nftables.TableFamilyIPv6
}
ch := &nftables.Chain{
Name: chainName,
Table: tb,
}
switch chainType {
case "nat":
ch.Type = nftables.ChainTypeNAT
case "filter":
ch.Type = nftables.ChainTypeFilter
case "route":
ch.Type = nftables.ChainTypeRoute
default:
if chainType != "none" {
return fmt.Errorf("unsupported table type: %s", chainType)
}
}
switch chainHookType {
case "input":
ch.Hooknum = nftables.ChainHookInput
case "forward":
ch.Hooknum = nftables.ChainHookForward
case "output":
ch.Hooknum = nftables.ChainHookOutput
case "prerouting":
ch.Hooknum = nftables.ChainHookPrerouting
case "postrouting":
ch.Hooknum = nftables.ChainHookPostrouting
default:
if chainHookType != "none" {
return fmt.Errorf("unsupported chain type: %s", chainHookType)
}
}
switch chainPriority {
case "dnat":
ch.Priority = nftables.ChainPriorityNATDest
case "snat":
ch.Priority = nftables.ChainPriorityNATSource
case "raw":
ch.Priority = nftables.ChainPriorityRaw
case "filter":
ch.Priority = nftables.ChainPriorityFilter
case "none":
// do nothing
default:
if chainPriority != "none" {
return fmt.Errorf("unsupported chain priority: %s", chainPriority)
}
}
conn.AddChain(ch)
if err = conn.Flush(); err != nil {
return err
}
return nil
}
// CreateFilterForwardChain creates forward chain in filter table.
func CreateFilterForwardChain(v, tableName, chainName string) error {
if err := isSupportedIPVersion(v); err != nil {
return err
}
conn, err := initNftConn()
if err != nil {
return err
}
tb := &nftables.Table{
Name: tableName,
}
if v == "4" {
tb.Family = nftables.TableFamilyIPv4
} else {
tb.Family = nftables.TableFamilyIPv6
}
ch := &nftables.Chain{
Name: chainName,
Table: tb,
Type: nftables.ChainTypeFilter,
Hooknum: nftables.ChainHookForward,
Priority: nftables.ChainPriorityFilter,
Policy: &defaultDropPolicy,
}
conn.AddChain(ch)
if err = conn.Flush(); err != nil {
return err
}
if err := addLogDenyRule(v, tableName, chainName); err != nil {
return err
}
return nil
}
// DeleteChain deletes a particular chain.
func DeleteChain(v, tableName, chainName string) error {
if err := isSupportedIPVersion(v); err != nil {
return err
}
conn, err := initNftConn()
if err != nil {
return err
}
tb := &nftables.Table{
Name: tableName,
}
if v == "4" {
tb.Family = nftables.TableFamilyIPv4
} else {
tb.Family = nftables.TableFamilyIPv6
}
ch := &nftables.Chain{
Name: chainName,
Table: tb,
}
conn.FlushChain(ch)
conn.DelChain(ch)
if err := conn.Flush(); err != nil {
return fmt.Errorf(
"error deleting %s chain in %s table: %s",
chainName, tableName, err,
)
}
return nil
}