forked from aquasecurity/libbpfgo
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tchook.go
161 lines (128 loc) · 3.17 KB
/
tchook.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
package libbpfgo
/*
#cgo LDFLAGS: -lelf -lz
#include "libbpfgo.h"
*/
import "C"
import (
"fmt"
"net"
"syscall"
)
//
// TcHook
//
type TcHook struct {
hook *C.struct_bpf_tc_hook
}
func (hook *TcHook) SetInterfaceByIndex(ifaceIdx int) {
hook.hook.ifindex = C.int(ifaceIdx)
}
func (hook *TcHook) SetInterfaceByName(ifaceName string) error {
iface, err := net.InterfaceByName(ifaceName)
if err != nil {
return err
}
hook.hook.ifindex = C.int(iface.Index)
return nil
}
func (hook *TcHook) GetInterfaceIndex() int {
return int(hook.hook.ifindex)
}
func (hook *TcHook) SetAttachPoint(attachPoint TcAttachPoint) {
hook.hook.attach_point = uint32(attachPoint)
}
func (hook *TcHook) SetParent(a int, b int) {
parent := (((a) << 16) & 0xFFFF0000) | ((b) & 0x0000FFFF)
hook.hook.parent = C.uint(parent)
}
func (hook *TcHook) Create() error {
retC := C.bpf_tc_hook_create(hook.hook)
if retC < 0 {
return fmt.Errorf("failed to create tc hook: %w", syscall.Errno(-retC))
}
return nil
}
func (hook *TcHook) Destroy() error {
retC := C.bpf_tc_hook_destroy(hook.hook)
if retC < 0 {
return fmt.Errorf("failed to destroy tc hook: %w", syscall.Errno(-retC))
}
C.cgo_bpf_tc_hook_free(hook.hook)
return nil
}
type TcOpts struct {
ProgFd int
Flags TcFlags
ProgId uint
Handle uint
Priority uint
}
func tcOptsToC(tcOpts *TcOpts) (*C.struct_bpf_tc_opts, error) {
if tcOpts == nil {
return nil, nil
}
optsC, errno := C.cgo_bpf_tc_opts_new(
C.int(tcOpts.ProgFd),
C.uint(tcOpts.Flags),
C.uint(tcOpts.ProgId),
C.uint(tcOpts.Handle),
C.uint(tcOpts.Priority),
)
if optsC == nil {
return nil, fmt.Errorf("failed to create bpf_tc_opts: %w", errno)
}
return optsC, nil
}
func tcOptsFromC(tcOpts *TcOpts, optsC *C.struct_bpf_tc_opts) {
if optsC == nil {
return
}
tcOpts.ProgFd = int(C.cgo_bpf_tc_opts_prog_fd(optsC))
tcOpts.Flags = TcFlags(C.cgo_bpf_tc_opts_flags(optsC))
tcOpts.ProgId = uint(C.cgo_bpf_tc_opts_prog_id(optsC))
tcOpts.Handle = uint(C.cgo_bpf_tc_opts_handle(optsC))
tcOpts.Priority = uint(C.cgo_bpf_tc_opts_priority(optsC))
}
func (hook *TcHook) Attach(tcOpts *TcOpts) error {
optsC, err := tcOptsToC(tcOpts)
if err != nil {
return err
}
defer C.cgo_bpf_tc_opts_free(optsC)
retC := C.bpf_tc_attach(hook.hook, optsC)
if retC < 0 {
return fmt.Errorf("failed to attach tc hook: %w", syscall.Errno(-retC))
}
// update tcOpts with the values from the libbpf
tcOptsFromC(tcOpts, optsC)
return nil
}
func (hook *TcHook) Detach(tcOpts *TcOpts) error {
optsC, err := tcOptsToC(tcOpts)
if err != nil {
return err
}
defer C.cgo_bpf_tc_opts_free(optsC)
retC := C.bpf_tc_detach(hook.hook, optsC)
if retC < 0 {
return fmt.Errorf("failed to detach tc hook: %w", syscall.Errno(-retC))
}
// update tcOpts with the values from the libbpf
tcOptsFromC(tcOpts, optsC)
return nil
}
func (hook *TcHook) Query(tcOpts *TcOpts) error {
optsC, err := tcOptsToC(tcOpts)
if err != nil {
return err
}
defer C.cgo_bpf_tc_opts_free(optsC)
retC := C.bpf_tc_query(hook.hook, optsC)
if retC < 0 {
return fmt.Errorf("failed to query tc hook: %w", syscall.Errno(-retC))
}
// update tcOpts with the values from the libbpf
tcOptsFromC(tcOpts, optsC)
return nil
}