Files
netbird/ebpf/filter.go
braginini b78ec082a3 ebpf test
2021-12-05 18:17:13 +01:00

37 lines
647 B
Go

package main
import (
"github.com/cilium/ebpf"
"net"
"syscall"
)
// Filter represents a classic BPF filter program that can be applied to a socket
type Filter struct {
*ebpf.ProgramSpec
}
// ApplyTo applies the current filter onto the provided UDPConn
func (filter Filter) ApplyTo(conn *net.UDPConn) error {
file, err := conn.File()
if err != nil {
return err
}
p, err := ebpf.NewProgramWithOptions(filter.ProgramSpec, ebpf.ProgramOptions{
LogLevel: 6,
})
if err != nil {
return err
}
if err := syscall.SetsockoptInt(int(file.Fd()), syscall.SOL_SOCKET, SO_ATTACH_BPF, p.FD()); err != nil {
return err
}
return nil
}