-
Notifications
You must be signed in to change notification settings - Fork 0
/
chain.go
177 lines (158 loc) · 3.62 KB
/
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
// +build linux
package nftlib
import (
"github.com/google/nftables"
"github.com/google/nftables/binaryutil"
)
const (
ChainHookInput chainHook = "input"
ChainHookOutput chainHook = "output"
ChainHookForward chainHook = "forward"
ChainTypeFilter chainType = "filter"
ChainTypeRoute chainType = "route"
ChainTypeNat chainType = "nat"
ChainPolicyAccept chainPolicy = "accept"
ChainPolicyDrop chainPolicy = "drop"
)
type Chain struct {
Conn *Conn `json:"-"`
Table *Table `json:"table,omitempty"`
Name string `json:"name,omitempty"`
Priority int32 `json:"priority"`
Hook chainHook `json:"hook,omitempty"`
Type chainType `json:"type,omitempty"`
Policy chainPolicy `json:"policy,omitempty"`
}
type (
chainHook string
chainType string
chainPolicy string
)
func (d *Chain) NewRule() *Rule {
return &Rule{Chain: d, conn: d.Conn}
}
func (d *Chain) ClearRule() {
d.Conn.FlushChain(d.toNch())
}
func (d *Chain) AddRule(rule *Rule, handle ...uint64) error {
rule.conn = d.Conn
rule.Chain = d
nrule, err := rule.toNRule(handle...)
if err != nil {
return err
}
d.Conn.AddRule(nrule)
return nil
}
func (d *Chain) InsertRule(rule *Rule, handle ...uint64) error {
rule.conn = d.Conn
rule.Chain = d
nrule, err := rule.toNRule(handle...)
if err != nil {
return err
}
d.Conn.InsertRule(nrule)
return nil
}
func (d *Chain) DelRule(rule *Rule) error {
rule.conn = d.Conn
rule.Chain = d
nrule, err := rule.toNRule()
if err != nil {
return err
}
err = d.Conn.DelRule(nrule)
if err != nil {
return err
}
return nil
}
func (d *Chain) ReplaceRule(rule *Rule) error {
rule.conn = d.Conn
rule.Chain = d
nrule, err := rule.toNRule()
if err != nil {
return err
}
d.Conn.ReplaceRule(nrule)
return nil
}
func (d *Chain) ListRule() ([]*Rule, error) {
var r []*Rule
nrlist, err := d.Conn.GetRule(d.Table.toNTable(), d.toNch())
if err != nil {
return nil, err
}
for _, nr := range nrlist {
rule := &Rule{Chain: d, conn: d.Conn}
err = rule.toRule(*nr)
if err != nil {
return nil, err
}
r = append(r, rule)
}
return r, nil
}
func (d *Chain) Commit() error {
return d.Conn.Commit()
}
func (d *Chain) toCh(nch nftables.Chain) {
d.Name = nch.Name
switch nch.Type {
case nftables.ChainTypeFilter:
d.Type = ChainTypeFilter
case nftables.ChainTypeRoute:
d.Type = ChainTypeRoute
case nftables.ChainTypeNAT:
d.Type = ChainTypeNat
}
switch nch.Hooknum {
case nftables.ChainHookInput:
d.Hook = ChainHookInput
case nftables.ChainHookOutput:
d.Hook = ChainHookOutput
case nftables.ChainHookForward:
d.Hook = ChainHookForward
}
if nch.Policy != nil {
plc := *nch.Policy
plcb := binaryutil.BigEndian.PutUint32(uint32(plc))
plcn := binaryutil.NativeEndian.Uint32(plcb)
switch nftables.ChainPolicy(plcn) {
case nftables.ChainPolicyAccept:
d.Policy = ChainPolicyAccept
case nftables.ChainPolicyDrop:
d.Policy = ChainPolicyDrop
}
}
}
func (d *Chain) toNch() *nftables.Chain {
var nch = new(nftables.Chain)
nch.Name = d.Name
nch.Table = d.Table.toNTable()
switch d.Type {
case ChainTypeFilter:
nch.Type = nftables.ChainTypeFilter
case ChainTypeRoute:
nch.Type = nftables.ChainTypeRoute
case ChainTypeNat:
nch.Type = nftables.ChainTypeNAT
}
switch d.Hook {
case ChainHookOutput:
nch.Hooknum = nftables.ChainHookOutput
case ChainHookInput:
nch.Hooknum = nftables.ChainHookInput
case ChainHookForward:
nch.Hooknum = nftables.ChainHookForward
}
switch d.Policy {
case ChainPolicyAccept:
plc := nftables.ChainPolicyAccept
nch.Policy = &plc
case ChainPolicyDrop:
plc := nftables.ChainPolicyDrop
nch.Policy = &plc
}
return nch
}