-
Notifications
You must be signed in to change notification settings - Fork 48
/
filter.go
164 lines (143 loc) · 4.09 KB
/
filter.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
package tc
import (
"errors"
"fmt"
"github.com/florianl/go-tc/internal/unix"
"github.com/mdlayher/netlink"
)
// Filter represents the filtering part of rtnetlink
type Filter struct {
Tc
}
// Filter allows to read and alter filters
func (tc *Tc) Filter() *Filter {
return &Filter{*tc}
}
// Add create a new filter
func (f *Filter) Add(info *Object) error {
if info == nil {
return ErrNoArg
}
options, err := validateFilterObject(unix.RTM_NEWTFILTER, info)
if err != nil {
return err
}
return f.action(unix.RTM_NEWTFILTER, netlink.Create|netlink.Excl, &info.Msg, options)
}
// Replace add/remove a filter. If the node does not exist yet it is created
func (f *Filter) Replace(info *Object) error {
if info == nil {
return ErrNoArg
}
options, err := validateFilterObject(unix.RTM_NEWTFILTER, info)
if err != nil {
return err
}
return f.action(unix.RTM_NEWTFILTER, netlink.Create, &info.Msg, options)
}
// Delete removes a filter
func (f *Filter) Delete(info *Object) error {
if info == nil {
return ErrNoArg
}
options, err := validateFilterObject(unix.RTM_DELTFILTER, info)
if err != nil {
return err
}
return f.action(unix.RTM_DELTFILTER, netlink.HeaderFlags(0), &info.Msg, options)
}
// Get fetches all filters
func (f *Filter) Get(i *Msg) ([]Object, error) {
if i == nil {
return []Object{}, ErrNoArg
}
return f.get(unix.RTM_GETTFILTER, i)
}
func marshalFilterOptions(kind string, info *Object) ([]byte, error) {
var data []byte
var err error
switch kind {
case "bpf":
data, err = marshalBpf(info.BPF)
case "basic":
data, err = marshalBasic(info.Basic)
case "cgroup":
data, err = marshalCgroup(info.Cgroup)
case "flow":
data, err = marshalFlow(info.Flow)
case "flower":
data, err = marshalFlower(info.Flower)
case "fw":
data, err = marshalFw(info.Fw)
case "route4":
data, err = marshalRoute4(info.Route4)
case "rsvp":
data, err = marshalRsvp(info.Rsvp)
case "u32":
data, err = marshalU32(info.U32)
case "matchall":
data, err = marshalMatchall(info.Matchall)
case "tcindex":
data, err = marshalTcIndex(info.TcIndex)
default:
return []byte{}, fmt.Errorf("can't marshal %s: %w", kind, ErrNotImplemented)
}
return data, err
}
func validateFilterObject(action int, info *Object) ([]tcOption, error) {
options := []tcOption{}
if info.Ifindex == 0 {
return options, ErrInvalidDev
}
if !isFilter(info.Kind) && !isChainAction(action) {
return options, ErrInvalidArg
}
if info.Stats != nil || info.XStats != nil || info.Stats2 != nil {
return options, ErrInvalidArg
}
if info.EgressBlock != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaEgressBlock, Data: uint32Value(info.EgressBlock)})
}
if info.IngressBlock != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaIngressBlock, Data: uint32Value(info.IngressBlock)})
}
if info.HwOffload != nil {
options = append(options, tcOption{Interpretation: vtUint8, Type: tcaHwOffload, Data: uint8Value(info.HwOffload)})
}
if info.Chain != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaChain, Data: uint32Value(info.Chain)})
}
options = append(options, tcOption{Interpretation: vtString, Type: tcaKind, Data: info.Kind})
var data []byte
var err error
if !isChainAction(action) {
data, err = marshalFilterOptions(info.Kind, info)
}
if err != nil {
if !errors.Is(err, ErrNoArg) && action != unix.RTM_DELTFILTER {
return options, err
}
}
if len(data) < 1 && !isDelAction(action) && !isChainAction(action) {
return options, ErrNoArg
}
options = append(options, tcOption{Interpretation: vtBytes, Type: tcaOptions, Data: data})
return options, nil
}
func isFilter(f string) bool {
for _, filter := range []string{"basic", "bpf", "cgroup", "flow", "flower", "fw", "matchall", "route4", "rsvp", "u32", "tcindex"} {
if f == filter {
return true
}
}
return false
}
func isChainAction(action int) bool {
return action == unix.RTM_NEWCHAIN ||
action == unix.RTM_GETCHAIN ||
action == unix.RTM_DELCHAIN
}
func isDelAction(action int) bool {
return action == unix.RTM_DELTFILTER ||
action == unix.RTM_DELTCLASS
}