-
Notifications
You must be signed in to change notification settings - Fork 48
/
m_ipt.go
98 lines (89 loc) · 2.28 KB
/
m_ipt.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
package tc
import (
"fmt"
"github.com/mdlayher/netlink"
)
const (
tcaIptUnspec = iota
tcaIptTable
tcaIptHook
tcaIptIndex
tcaIptCnt
tcaIptTm
tcaIptTarg
tcaIptPad
)
// Ipt contains attribute of the ipt discipline
type Ipt struct {
Table *string
Hook *uint32
Index *uint32
Cnt *IptCnt
Tm *Tcft
}
// IptCnt as tc_cnt from include/uapi/linux/pkt_cls.h
type IptCnt struct {
RefCnt uint32
BindCnt uint32
}
// unmarshalIpt parses the ipt-encoded data and stores the result in the value pointed to by info.
func unmarshalIpt(data []byte, info *Ipt) error {
ad, err := netlink.NewAttributeDecoder(data)
if err != nil {
return err
}
var multiError error
for ad.Next() {
switch ad.Type() {
case tcaIptTm:
tcft := &Tcft{}
err = unmarshalStruct(ad.Bytes(), tcft)
multiError = concatError(multiError, err)
info.Tm = tcft
case tcaIptTable:
info.Table = stringPtr(ad.String())
case tcaIptHook:
info.Hook = uint32Ptr(ad.Uint32())
case tcaIptIndex:
info.Index = uint32Ptr(ad.Uint32())
case tcaIptCnt:
tmp := &IptCnt{}
err = unmarshalStruct(ad.Bytes(), tmp)
multiError = concatError(multiError, err)
info.Cnt = tmp
case tcaIptPad:
// padding does not contain data, we just skip it
default:
return fmt.Errorf("UnmarshalIpt()\t%d\n\t%v", ad.Type(), ad.Bytes())
}
}
return concatError(multiError, ad.Err())
}
// marshalIpt returns the binary encoding of Ipt
func marshalIpt(info *Ipt) ([]byte, error) {
options := []tcOption{}
if info == nil {
return []byte{}, fmt.Errorf("Ipt: %w", ErrNoArg)
}
// TODO: improve logic and check combinations
if info.Tm != nil {
return []byte{}, ErrNoArgAlter
}
if info.Table != nil {
options = append(options, tcOption{Interpretation: vtString, Type: tcaIptTable, Data: stringValue(info.Table)})
}
if info.Hook != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaIptHook, Data: uint32Value(info.Hook)})
}
if info.Index != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaIptIndex, Data: uint32Value(info.Index)})
}
if info.Cnt != nil {
data, err := marshalStruct(info.Cnt)
if err != nil {
return []byte{}, err
}
options = append(options, tcOption{Interpretation: vtBytes, Type: tcaIptCnt, Data: data})
}
return marshalAttributes(options)
}