Skip to content

Commit

Permalink
Allow DNS tracking to use configurable port for tracking
Browse files Browse the repository at this point in the history
Today DNS tracking uses only port 53 which is the
port defined for DNS service and it get mapped
to different port at the container level to
avoid root access. This change wil allow
tracking at the pod level as well

Signed-off-by: Mohamed Mahmoud <[email protected]>
  • Loading branch information
msherif1234 committed Aug 6, 2024
1 parent 7b7dc40 commit 45824dd
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 16 deletions.
1 change: 1 addition & 0 deletions bpf/configs.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ volatile const u8 enable_rtt = 0;
volatile const u8 enable_pca = 0;
volatile const u8 enable_dns_tracking = 0;
volatile const u8 enable_flows_filtering = 0;
volatile const u32 dns_port = 0;
#endif //__CONFIGS_H__
3 changes: 1 addition & 2 deletions bpf/dns_tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#define __DNS_TRACKER_H__
#include "utils.h"

#define DNS_PORT 53
#define DNS_QR_FLAG 0x8000
#define UDP_MAXMSG 512
#define EINVAL 22
Expand Down Expand Up @@ -67,7 +66,7 @@ static __always_inline u8 calc_dns_header_offset(pkt_info *pkt, void *data_end)

static __always_inline int track_dns_packet(struct __sk_buff *skb, pkt_info *pkt) {
void *data_end = (void *)(long)skb->data_end;
if (pkt->id->dst_port == DNS_PORT || pkt->id->src_port == DNS_PORT) {
if (pkt->id->dst_port == dns_port || pkt->id->src_port == dns_port) {
dns_flow_id dns_req;

u8 len = calc_dns_header_offset(pkt, data_end);
Expand Down
1 change: 1 addition & 0 deletions pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ func FlowsAgent(cfg *Config) (*Flows, error) {
CacheMaxSize: cfg.CacheMaxFlows,
PktDrops: cfg.EnablePktDrops,
DNSTracker: cfg.EnableDNSTracking,
DNSTrackerPort: cfg.DNSTrackingPort,
EnableRTT: cfg.EnableRTT,
EnableFlowFilter: cfg.EnableFlowFilter,
FilterConfig: &ebpf.FilterConfig{
Expand Down
3 changes: 3 additions & 0 deletions pkg/agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ type Config struct {
EnablePktDrops bool `env:"ENABLE_PKT_DROPS" envDefault:"false"`
// EnableDNSTracking enable DNS tracking eBPF hook to track dns query/response flows
EnableDNSTracking bool `env:"ENABLE_DNS_TRACKING" envDefault:"false"`
// DNSTrackingPort used to define which port the DNS service is mapped to at the pod level,
// so we can track DNS at the pod level
DNSTrackingPort int `env:"DNS_TRACKING_PORT" envDefault:"53"`
// StaleEntriesEvictTimeout specifies the maximum duration that stale entries are kept
// before being deleted, default is 5 seconds.
StaleEntriesEvictTimeout time.Duration `env:"STALE_ENTRIES_EVICT_TIMEOUT" envDefault:"5s"`
Expand Down
Binary file modified pkg/ebpf/bpf_arm64_bpfel.o
Binary file not shown.
Binary file modified pkg/ebpf/bpf_powerpc_bpfel.o
Binary file not shown.
Binary file modified pkg/ebpf/bpf_s390_bpfeb.o
Binary file not shown.
Binary file modified pkg/ebpf/bpf_x86_bpfel.o
Binary file not shown.
9 changes: 9 additions & 0 deletions pkg/ebpf/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const (
constTraceMessages = "trace_messages"
constEnableRtt = "enable_rtt"
constEnableDNSTracking = "enable_dns_tracking"
constDNSTrackingPort = "dns_port"
dnsDefaultPort = 53
constEnableFlowFiltering = "enable_flows_filtering"
pktDropHook = "kfree_skb"
constPcaEnable = "enable_pca"
Expand Down Expand Up @@ -79,12 +81,14 @@ type FlowFetcherConfig struct {
CacheMaxSize int
PktDrops bool
DNSTracker bool
DNSTrackerPort int
EnableRTT bool
EnableFlowFilter bool
EnablePCA bool
FilterConfig *FilterConfig
}

// nolint:cyclop
func NewFlowFetcher(cfg *FlowFetcherConfig) (*FlowFetcher, error) {
if err := rlimit.RemoveMemlock(); err != nil {
log.WithError(err).
Expand All @@ -110,8 +114,12 @@ func NewFlowFetcher(cfg *FlowFetcherConfig) (*FlowFetcher, error) {
}

enableDNSTracking := 0
dnsTrackerPort := dnsDefaultPort
if cfg.DNSTracker {
enableDNSTracking = 1
if cfg.DNSTrackerPort != 0 {
dnsTrackerPort = cfg.DNSTrackerPort
}
}

if enableDNSTracking == 0 {
Expand All @@ -128,6 +136,7 @@ func NewFlowFetcher(cfg *FlowFetcherConfig) (*FlowFetcher, error) {
constTraceMessages: uint8(traceMsgs),
constEnableRtt: uint8(enableRtt),
constEnableDNSTracking: uint8(enableDNSTracking),
constDNSTrackingPort: uint32(dnsTrackerPort),
constEnableFlowFiltering: uint8(enableFlowFiltering),
}); err != nil {
return nil, fmt.Errorf("rewriting BPF constants definition: %w", err)
Expand Down
25 changes: 18 additions & 7 deletions pkg/pbflow/flow_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 18 additions & 7 deletions pkg/pbpacket/packet_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 45824dd

Please sign in to comment.