mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-20 01:06:45 +00:00
52 lines
973 B
Go
52 lines
973 B
Go
package ebpf
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"net"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
const (
|
|
mapKeyDNSIP uint32 = 0
|
|
mapKeyDNSPort uint32 = 1
|
|
)
|
|
|
|
func (tf *Manager) LoadDNSFwd(ip string, dnsPort int) error {
|
|
log.Debugf("load ebpf DNS forwarder: address: %s:%d", ip, dnsPort)
|
|
tf.lock.Lock()
|
|
defer tf.lock.Unlock()
|
|
|
|
err := tf.loadXdp()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = tf.bpfObjs.NbMapDnsIp.Put(mapKeyDNSIP, ip2int(ip))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = tf.bpfObjs.NbMapDnsPort.Put(mapKeyDNSPort, uint16(dnsPort))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
tf.setFeatureFlag(featureFlagDnsForwarder)
|
|
err = tf.bpfObjs.NbFeatures.Put(mapKeyFeatures, tf.featureFlags)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (tf *Manager) FreeDNSFwd() error {
|
|
log.Debugf("free ebpf DNS forwarder")
|
|
return tf.unsetFeatureFlag(featureFlagDnsForwarder)
|
|
}
|
|
|
|
func ip2int(ipString string) uint32 {
|
|
ip := net.ParseIP(ipString)
|
|
return binary.BigEndian.Uint32(ip.To4())
|
|
}
|