-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathattribute.go
274 lines (216 loc) · 7.07 KB
/
attribute.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package netfilter
import (
"encoding/binary"
"fmt"
"github.com/mdlayher/netlink"
)
// NewAttributeDecoder instantiates a new netlink.AttributeDecoder
// configured with a Big Endian byte order.
func NewAttributeDecoder(b []byte) (*netlink.AttributeDecoder, error) {
ad, err := netlink.NewAttributeDecoder(b)
if err != nil {
return nil, err
}
// All Netfilter attribute payloads are big-endian. (network byte order)
ad.ByteOrder = binary.BigEndian
return ad, nil
}
// NewAttributeEncoder instantiates a new netlink.AttributeEncoder
// configured with a Big Endian byte order.
func NewAttributeEncoder() *netlink.AttributeEncoder {
ae := netlink.NewAttributeEncoder()
// All Netfilter attribute payloads are big-endian. (network byte order)
ae.ByteOrder = binary.BigEndian
return ae
}
// An Attribute is a copy of a netlink.Attribute that can be nested.
type Attribute struct {
// The type of this Attribute, typically matched to a constant.
Type uint16
// An arbitrary payload which is specified by Type.
Data []byte
// Whether the attribute's data contains nested attributes.
Nested bool
Children []Attribute
// Whether the attribute's data is in network (true) or native (false) byte order.
NetByteOrder bool
}
func (a Attribute) String() string {
if a.Nested {
return fmt.Sprintf("<Length %d, Type %d, Nested %t, %d Children (%v)>",
len(a.Data), a.Type, a.Nested, len(a.Children), a.Children)
}
return fmt.Sprintf("<Length %d, Type %d, Nested %t, NetByteOrder %t, %v>",
len(a.Data), a.Type, a.Nested, a.NetByteOrder, a.Data)
}
// Uint16 interprets a non-nested Netfilter attribute in network byte order as a uint16.
func (a Attribute) Uint16() uint16 {
if a.Nested {
panic("Uint16: unexpected Nested attribute")
}
if l := len(a.Data); l != 2 {
panic(fmt.Sprintf("Uint16: unexpected byte slice length: %d", l))
}
return binary.BigEndian.Uint16(a.Data)
}
// PutUint16 sets the Attribute's data field to a Uint16 encoded in net byte order.
func (a *Attribute) PutUint16(v uint16) {
if len(a.Data) != 2 {
a.Data = make([]byte, 2)
}
binary.BigEndian.PutUint16(a.Data, v)
}
// Uint32 interprets a non-nested Netfilter attribute in network byte order as a uint32.
func (a Attribute) Uint32() uint32 {
if a.Nested {
panic("Uint32: unexpected Nested attribute")
}
if l := len(a.Data); l != 4 {
panic(fmt.Sprintf("Uint32: unexpected byte slice length: %d", l))
}
return binary.BigEndian.Uint32(a.Data)
}
// PutUint32 sets the Attribute's data field to a Uint32 encoded in net byte order.
func (a *Attribute) PutUint32(v uint32) {
if len(a.Data) != 4 {
a.Data = make([]byte, 4)
}
binary.BigEndian.PutUint32(a.Data, v)
}
// Int32 converts the result of Uint16() to an int32.
func (a Attribute) Int32() int32 {
return int32(a.Uint32())
}
// Uint64 interprets a non-nested Netfilter attribute in network byte order as a uint64.
func (a Attribute) Uint64() uint64 {
if a.Nested {
panic("Uint64: unexpected Nested attribute")
}
if l := len(a.Data); l != 8 {
panic(fmt.Sprintf("Uint64: unexpected byte slice length: %d", l))
}
return binary.BigEndian.Uint64(a.Data)
}
// PutUint64 sets the Attribute's data field to a Uint64 encoded in net byte order.
func (a *Attribute) PutUint64(v uint64) {
if len(a.Data) != 8 {
a.Data = make([]byte, 8)
}
binary.BigEndian.PutUint64(a.Data, v)
}
// Int64 converts the result of Uint16() to an int64.
func (a Attribute) Int64() int64 {
return int64(a.Uint64())
}
// Uint16Bytes gets the big-endian 2-byte representation of a uint16.
func Uint16Bytes(u uint16) []byte {
d := make([]byte, 2)
binary.BigEndian.PutUint16(d, u)
return d
}
// Uint32Bytes gets the big-endian 4-byte representation of a uint32.
func Uint32Bytes(u uint32) []byte {
d := make([]byte, 4)
binary.BigEndian.PutUint32(d, u)
return d
}
// Uint64Bytes gets the big-endian 8-byte representation of a uint64.
func Uint64Bytes(u uint64) []byte {
d := make([]byte, 8)
binary.BigEndian.PutUint64(d, u)
return d
}
// decode fills the Attribute's Children field with Attributes
// obtained by exhausting ad.
func (a *Attribute) decode(ad *netlink.AttributeDecoder) error {
for ad.Next() {
// Copy the netlink attribute's fields into the netfilter attribute.
nfa := Attribute{
// Only consider the rightmost 14 bits for Type.
// ad.Type implicitly masks the Nested and NetByteOrder bits.
Type: ad.Type(),
Data: ad.Bytes(),
}
// Boolean flags extracted from the two leftmost bits of Type.
nfa.Nested = ad.TypeFlags()&netlink.Nested != 0
nfa.NetByteOrder = ad.TypeFlags()&netlink.NetByteOrder != 0
if nfa.NetByteOrder && nfa.Nested {
return errInvalidAttributeFlags
}
// Unmarshal recursively if the netlink Nested flag is set.
if nfa.Nested {
ad.Nested(nfa.decode)
}
a.Children = append(a.Children, nfa)
}
return ad.Err()
}
// encode returns a function that takes an AttributeEncoder and returns error.
// This function can be passed to AttributeEncoder.Nested for recursively
// encoding Attributes.
func (a *Attribute) encode(attrs []Attribute) func(*netlink.AttributeEncoder) error {
return func(ae *netlink.AttributeEncoder) error {
for _, nfa := range attrs {
if nfa.NetByteOrder && nfa.Nested {
return errInvalidAttributeFlags
}
if nfa.Nested {
ae.Nested(nfa.Type, nfa.encode(nfa.Children))
continue
}
// Manually set the NetByteOrder flag, since ae.Bytes() can't.
if nfa.NetByteOrder {
nfa.Type |= netlink.NetByteOrder
}
ae.Bytes(nfa.Type, nfa.Data)
}
return nil
}
}
// decodeAttributes returns an array of netfilter.Attributes decoded from
// a byte array. This byte array should be taken from the netlink.Message's
// Data payload after the nfHeaderLen offset.
func decodeAttributes(ad *netlink.AttributeDecoder) ([]Attribute, error) {
// Use the Children element of the Attribute to decode into.
// Attribute already has nested decoding implemented on the type.
var a Attribute
// Pre-allocate backing array when there are netlink attributes to decode.
if ad.Len() != 0 {
a.Children = make([]Attribute, 0, ad.Len())
}
// Catch any errors encountered parsing netfilter structures.
if err := a.decode(ad); err != nil {
return nil, err
}
return a.Children, nil
}
// encodeAttributes encodes a list of Attributes into the given netlink.AttributeEncoder.
func encodeAttributes(ae *netlink.AttributeEncoder, attrs []Attribute) error {
if ae == nil {
return errNilAttributeEncoder
}
attr := Attribute{}
return attr.encode(attrs)(ae)
}
// MarshalAttributes marshals a nested attribute structure into a byte slice.
// This byte slice can then be copied into a netlink.Message's Data field after
// the nfHeaderLen offset.
func MarshalAttributes(attrs []Attribute) ([]byte, error) {
ae := NewAttributeEncoder()
if err := encodeAttributes(ae, attrs); err != nil {
return nil, err
}
b, err := ae.Encode()
if err != nil {
return nil, err
}
return b, nil
}
// UnmarshalAttributes unmarshals a byte slice into a list of Attributes.
func UnmarshalAttributes(b []byte) ([]Attribute, error) {
ad, err := NewAttributeDecoder(b)
if err != nil {
return nil, err
}
return decodeAttributes(ad)
}