forked from florianl/go-tc
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathematch.go
More file actions
185 lines (164 loc) · 4.23 KB
/
ematch.go
File metadata and controls
185 lines (164 loc) · 4.23 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
package tc
import (
"fmt"
"github.com/mdlayher/netlink"
)
type EmatchLayer uint8
const (
EmatchLayerLink = EmatchLayer(0)
EmatchLayerNetwork = EmatchLayer(1)
EmatchLayerTransport = EmatchLayer(2)
)
type EmatchOpnd uint8
const (
EmatchOpndEq = EmatchOpnd(0)
EmatchOpndGt = EmatchOpnd(1)
EmatchOpndLt = EmatchOpnd(2)
)
const (
tcaEmatchTreeUnspec = iota
tcaEmatchTreeHdr
tcaEmatchTreeList
)
type EmatchKind uint16
// Various Ematch kinds
const (
EmatchContainer = EmatchKind(0)
EmatchCmp = EmatchKind(1)
EmatchNByte = EmatchKind(2)
EmatchU32 = EmatchKind(3)
EmatchMeta = EmatchKind(4)
EmatchText = EmatchKind(5)
EmatchVLan = EmatchKind(6)
EmatchCanID = EmatchKind(7)
EmatchIPSet = EmatchKind(8)
EmatchIPT = EmatchKind(9)
)
// Ematch contains attributes of the ematch discipline
// https://man7.org/linux/man-pages/man8/tc-ematch.8.html
type Ematch struct {
Hdr *EmatchTreeHdr
Matches *[]EmatchMatch
}
// tcf_ematch_tree_hdr from include/uapi/linux/pkt_cls.h
type EmatchTreeHdr struct {
NMatches uint16
ProgID uint16
}
// tcf_ematch_hdr from include/uapi/linux/pkt_cls.h
type EmatchHdr struct {
MatchID uint16
Kind EmatchKind
Flags uint16
Pad uint16
}
type EmatchMatch struct {
Hdr EmatchHdr
U32Match *U32Match
CmpMatch *CmpMatch
}
// unmarshalEmatch parses the Ematch-encoded data and stores the result in the value pointed to by info.
func unmarshalEmatch(data []byte, info *Ematch) error {
ad, err := netlink.NewAttributeDecoder(data)
if err != nil {
return err
}
ad.ByteOrder = nativeEndian
for ad.Next() {
switch ad.Type() {
case tcaEmatchTreeHdr:
hdr := &EmatchTreeHdr{}
if err := unmarshalStruct(ad.Bytes(), hdr); err != nil {
return err
}
info.Hdr = hdr
case tcaEmatchTreeList:
list := []EmatchMatch{}
if err := unmarshalEmatchTreeList(ad.Bytes(), &list); err != nil {
return err
}
info.Matches = &list
default:
return fmt.Errorf("UnmarshalEmatch()\t%d\n\t%v", ad.Type(), ad.Bytes())
}
}
return ad.Err()
}
// marshalEmatch returns the binary encoding of Ematch
func marshalEmatch(info *Ematch) ([]byte, error) {
options := []tcOption{}
if info == nil {
return []byte{}, fmt.Errorf("Ematch: %w", ErrNoArg)
}
if info.Hdr != nil {
data, err := marshalStruct(info.Hdr)
if err != nil {
return []byte{}, err
}
options = append(options, tcOption{Interpretation: vtBytes, Type: tcaEmatchTreeHdr, Data: data})
}
if info.Matches != nil {
data, err := marshalEmatchTreeList(info.Matches)
if err != nil {
return []byte{}, err
}
options = append(options, tcOption{Interpretation: vtBytes, Type: tcaEmatchTreeList | nlaFNnested, Data: data})
}
return marshalAttributes(options)
}
func unmarshalEmatchTreeList(data []byte, info *[]EmatchMatch) error {
ad, err := netlink.NewAttributeDecoder(data)
if err != nil {
return err
}
ad.ByteOrder = nativeEndian
for ad.Next() {
match := EmatchMatch{}
tmp := ad.Bytes()
if err := unmarshalStruct(tmp[:8], &match.Hdr); err != nil {
return err
}
switch match.Hdr.Kind {
case EmatchU32:
expr := &U32Match{}
if err := unmarshalU32Match(tmp[8:], expr); err != nil {
return err
}
match.U32Match = expr
case EmatchCmp:
expr := &CmpMatch{}
if err := unmarshalCmpMatch(tmp[8:], expr); err != nil {
return err
}
match.CmpMatch = expr
default:
return fmt.Errorf("unmarshalEmatchTreeList() kind %d is not yet implemented", match.Hdr.Kind)
}
*info = append(*info, match)
}
return ad.Err()
}
func marshalEmatchTreeList(info *[]EmatchMatch) ([]byte, error) {
options := []tcOption{}
for i, m := range *info {
payload, err := marshalStruct(m.Hdr)
if err != nil {
return []byte{}, err
}
var expr []byte
switch m.Hdr.Kind {
case EmatchU32:
expr, err = marshalU32Match(m.U32Match)
case EmatchCmp:
expr, err = marshalCmpMatch(m.CmpMatch)
default:
return []byte{}, fmt.Errorf("marshalEmatchTreeList() kind %d is not yet implemented", m.Hdr.Kind)
}
if err != nil {
return []byte{}, fmt.Errorf("marshalEmatchTreeList(): %v", err)
}
payload = append(payload, expr...)
options = append(options, tcOption{Interpretation: vtBytes, Type: uint16(i + 1), Data: payload})
}
return marshalAttributes(options)
}