mirror of
https://github.com/netbirdio/netbird.git
synced 2026-03-31 06:24:18 -04:00
37 lines
647 B
Go
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
|
|
}
|