forked from florianl/go-tc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
filter.go
152 lines (136 loc) · 3.75 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
package tc
import (
"errors"
"fmt"
"github.com/bestateless/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)
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 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)})
}
var data []byte
var err error
options = append(options, tcOption{Interpretation: vtString, Type: tcaKind, Data: info.Kind})
if isFilter(info.Kind) {
data, err = marshalFilterOptions(info.Kind, info)
if err != nil {
return nil, err
}
}
if err != nil {
if errors.Is(err, ErrNoArg) && action == unix.RTM_DELTFILTER {
return options, nil
}
return options, err
}
if len(data) < 1 {
if action == unix.RTM_NEWTFILTER {
return options, ErrNoArg
}
} else {
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", "route4", "rsvp", "u32", "matchall"} {
if f == filter {
return true
}
}
return false
}