-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserdata.go
More file actions
247 lines (213 loc) · 5.24 KB
/
Copy pathuserdata.go
File metadata and controls
247 lines (213 loc) · 5.24 KB
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
package nftnl
import (
"encoding/binary"
"fmt"
"strings"
)
func udPut(buf []byte, typ uint8, val []byte) []byte {
buf = append(buf, typ, uint8(len(val)))
return append(buf, val...)
}
func udPutString(buf []byte, typ uint8, s *string) []byte {
if s == nil {
return buf
}
return udPut(buf, typ, append([]byte(*s), 0))
}
func udString(val []byte) *string {
return new(strings.TrimRight(string(val), "\x00"))
}
func udPutUint64BE(buf []byte, typ uint8, v *uint64) []byte {
if v == nil {
return buf
}
var b [8]byte
binary.BigEndian.PutUint64(b[:], *v)
return udPut(buf, typ, b[:])
}
func udUint64BE(val []byte) *uint64 {
if len(val) != 8 {
return nil
}
return new(binary.BigEndian.Uint64(val))
}
func udEach(data []byte, fn func(typ uint8, val []byte)) error {
for len(data) >= 2 {
typ := data[0]
l := int(data[1])
if len(data) < 2+l {
return fmt.Errorf("nftnl: userdata TLV truncated at type %d", typ)
}
fn(typ, data[2:2+l])
data = data[2+l:]
}
return nil
}
const (
udTableComment uint8 = 0
udTableNFTVer uint8 = 1
udTableNFTBld uint8 = 2
)
// NFTVersion is the nft release that created the table.
type NFTVersion struct {
Major uint8
Minor uint8
Patch uint8
}
func udPutNFTVersion(buf []byte, typ uint8, v *NFTVersion) []byte {
if v == nil {
return buf
}
return udPut(buf, typ, []byte{v.Major, v.Minor, v.Patch, 0})
}
func udNFTVersion(val []byte) *NFTVersion {
if len(val) != 4 {
return nil
}
return &NFTVersion{Major: val[0], Minor: val[1], Patch: val[2]}
}
// Table userdata
//
// https://git.netfilter.org/libnftnl/tree/include/libnftnl/udata.h?id=0f48d7638800bb48c863e6b7e5f2fe92972bb31c#n12
type TableUserData struct {
// Table comment
Comment *string
// nft version that created the table
NFTVer *NFTVersion
// Timestamp in seconds of the nft build that created the table
NFTBld *uint64
}
func (a *TableUserData) marshal() []byte {
if a == nil {
return nil
}
var buf []byte
buf = udPutString(buf, udTableComment, a.Comment)
buf = udPutNFTVersion(buf, udTableNFTVer, a.NFTVer)
buf = udPutUint64BE(buf, udTableNFTBld, a.NFTBld)
return buf
}
func (a *TableUserData) unmarshal(data []byte) error {
return udEach(data, func(typ uint8, val []byte) {
switch typ {
case udTableComment:
a.Comment = udString(val)
case udTableNFTVer:
a.NFTVer = udNFTVersion(val)
case udTableNFTBld:
a.NFTBld = udUint64BE(val)
}
})
}
const udChainComment uint8 = 0
// Chain userdata
//
// https://git.netfilter.org/libnftnl/tree/include/libnftnl/udata.h?id=0f48d7638800bb48c863e6b7e5f2fe92972bb31c#n20
type ChainUserData struct {
// Chain comment
Comment *string
}
func (a *ChainUserData) marshal() []byte {
if a == nil {
return nil
}
return udPutString(nil, udChainComment, a.Comment)
}
func (a *ChainUserData) unmarshal(data []byte) error {
return udEach(data, func(typ uint8, val []byte) {
if typ == udChainComment {
a.Comment = udString(val)
}
})
}
const (
udRuleComment uint8 = 0
)
// Rule userdata
//
// https://git.netfilter.org/libnftnl/tree/include/libnftnl/udata.h?id=0f48d7638800bb48c863e6b7e5f2fe92972bb31c#n26
type RuleUserData struct {
// Rule comment
Comment *string
// TODO: investigate whether the rest of the fields are useful to expose.
}
func (a *RuleUserData) marshal() []byte {
if a == nil {
return nil
}
var buf []byte
buf = udPutString(buf, udRuleComment, a.Comment)
return buf
}
func (a *RuleUserData) unmarshal(data []byte) error {
return udEach(data, func(typ uint8, val []byte) {
if typ == udRuleComment {
a.Comment = udString(val)
}
})
}
const udObjComment uint8 = 0
// Object userdata
//
// https://git.netfilter.org/libnftnl/tree/include/libnftnl/udata.h?id=0f48d7638800bb48c863e6b7e5f2fe92972bb31c#n33
type ObjUserData struct {
// Object comment
Comment *string
}
func (a *ObjUserData) marshal() []byte {
if a == nil {
return nil
}
return udPutString(nil, udObjComment, a.Comment)
}
func (a *ObjUserData) unmarshal(data []byte) error {
return udEach(data, func(typ uint8, val []byte) {
if typ == udObjComment {
a.Comment = udString(val)
}
})
}
const udSetComment uint8 = 7
// Set userdata
//
// https://git.netfilter.org/libnftnl/tree/include/libnftnl/udata.h?id=0f48d7638800bb48c863e6b7e5f2fe92972bb31c#n41
type SetUserData struct {
// Set comment
Comment *string
// TODO: investigate whether the rest of the fields are useful to expose.
}
func (a *SetUserData) marshal() []byte {
if a == nil {
return nil
}
return udPutString(nil, udSetComment, a.Comment)
}
func (a *SetUserData) unmarshal(data []byte) error {
return udEach(data, func(typ uint8, val []byte) {
if typ == udSetComment {
a.Comment = udString(val)
}
})
}
const udSetElemComment uint8 = 0
// Set element userdata
//
// https://git.netfilter.org/libnftnl/tree/include/libnftnl/udata.h?id=0f48d7638800bb48c863e6b7e5f2fe92972bb31c#n61
type SetElemUserData struct {
// Set element comment
Comment *string
// TODO: investigate whether the rest of the fields are useful to expose.
}
func (a *SetElemUserData) marshal() []byte {
if a == nil {
return nil
}
return udPutString(nil, udSetElemComment, a.Comment)
}
func (a *SetElemUserData) unmarshal(data []byte) error {
return udEach(data, func(typ uint8, val []byte) {
if typ == udSetElemComment {
a.Comment = udString(val)
}
})
}