diff --git a/.clang-format b/.clang-format index 036dc3c..dbe226a 100644 --- a/.clang-format +++ b/.clang-format @@ -1,4 +1,7 @@ --- +Language: Cpp +BasedOnStyle: LLVM + # Use tabs for indentation UseTab: Always TabWidth: 4 diff --git a/src/main.c b/bpf/bpf_dns.c similarity index 97% rename from src/main.c rename to bpf/bpf_dns.c index 4ff0ef4..ae7f022 100644 --- a/src/main.c +++ b/bpf/bpf_dns.c @@ -11,8 +11,9 @@ #include #include -#include "dns.h" -#include "xdp_map.h" +#include "lib/dns.h" +#include "lib/consts.h" +#include "lib/maps.h" #define memcpy __builtin_memcpy @@ -167,14 +168,14 @@ static int parse_dns_query(struct xdp_md *ctx, void *query_start, // Fill dns_query.name with zero bytes // Not doing so will make the verifier complain when dns_query is used as a // key in bpf_map_lookup - for (i = 0; i < MAX_DNS_NAME_LENGTH; i++) { + for (i = 0; i < MAX_DNS_NAME_LEN; i++) { q->name[i] = 0; } // Fill record_type and class with default values to satisfy verifier q->record_type = 0; q->record_class = 0; - for (i = 0; i < MAX_DNS_NAME_LENGTH; i++) { + for (i = 0; i < MAX_DNS_NAME_LEN; i++) { // Boundary check of cursor. Verifier requires a +1 here. // Probably because we are advancing the pointer at the end of the loop if (cursor + 1 > data_end) { diff --git a/src/xdp_struct.h b/bpf/lib/client.h similarity index 82% rename from src/xdp_struct.h rename to bpf/lib/client.h index b46f178..9849bac 100644 --- a/src/xdp_struct.h +++ b/bpf/lib/client.h @@ -1,5 +1,5 @@ -#ifndef __XDP_STRUCTS_H -#define __XDP_STRUCTS_H +#ifndef __LIB_CLIENT_H +#define __LIB_CLIENT_H #include #include diff --git a/src/xdp_consts.h b/bpf/lib/consts.h similarity index 54% rename from src/xdp_consts.h rename to bpf/lib/consts.h index b10d6ae..d2618ff 100644 --- a/src/xdp_consts.h +++ b/bpf/lib/consts.h @@ -1,9 +1,9 @@ -#ifndef __XDP_CONSTS_H -#define __XDP_CONSTS_H +#ifndef __LIB_CONSTS_H +#define __LIB_CONSTS_H #define MAX_MAP_ENTRIES 1024 #define MAX_SEGMENTLIST_ENTRIES 10 -#define MAX_DOMAIN_NAME_LEN 256 +#define MAX_DNS_NAME_LEN 256 #define UDP_P_DNS 53 diff --git a/src/dns.h b/bpf/lib/dns.h similarity index 85% rename from src/dns.h rename to bpf/lib/dns.h index 9d4595b..c2ed291 100644 --- a/src/dns.h +++ b/bpf/lib/dns.h @@ -1,9 +1,9 @@ -#ifndef __DNS_H -#define __DNS_H +#ifndef __LIB_DNS_H +#define __LIB_DNS_H #include -#define MAX_DNS_NAME_LENGTH 256 +#include "consts.h" struct dns_hdr { __u16 transaction_id; @@ -26,7 +26,7 @@ struct dns_hdr { struct dns_query { __u16 record_type; __u16 record_class; - char name[MAX_DNS_NAME_LENGTH]; + char name[MAX_DNS_NAME_LEN]; } __attribute__((packed)); struct dns_answer { diff --git a/src/xdp_map.h b/bpf/lib/maps.h similarity index 77% rename from src/xdp_map.h rename to bpf/lib/maps.h index 408fc43..14dcbda 100644 --- a/src/xdp_map.h +++ b/bpf/lib/maps.h @@ -1,7 +1,7 @@ -#ifndef __XDP_MAPS_H -#define __XDP_MAPS_H -#include "xdp_consts.h" -#include "xdp_struct.h" +#ifndef __LIB_MAPS_H +#define __LIB_MAPS_H +#include "client.h" +#include "consts.h" #include #include #include @@ -10,7 +10,7 @@ struct { __uint(type, BPF_MAP_TYPE_LRU_HASH); __uint(max_entries, MAX_MAP_ENTRIES); - __type(key, char[MAX_DOMAIN_NAME_LEN]); + __type(key, char[MAX_DNS_NAME_LEN]); __type(value, struct client_data); } client_map SEC(".maps"); diff --git a/out/bin/hawkwing b/out/bin/hawkwing index 1e0322a..a88eedb 100755 Binary files a/out/bin/hawkwing and b/out/bin/hawkwing differ diff --git a/pkg/bpf/bpf.go b/pkg/bpf/bpf.go index 7fa5133..9ac4912 100644 --- a/pkg/bpf/bpf.go +++ b/pkg/bpf/bpf.go @@ -8,7 +8,7 @@ import ( "github.com/cilium/ebpf" ) -//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -no-global-types -cc $BPF_CLANG -cflags $BPF_CFLAGS bpf ../../src/main.c -- -I../../src +//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -no-global-types -cc $BPF_CLANG -cflags $BPF_CFLAGS bpf ../../bpf/bpf_dns.c -- -I../../bpf type ClientData struct { DstPort uint16 diff --git a/pkg/bpf/bpf_bpfeb.o b/pkg/bpf/bpf_bpfeb.o index 2b730cc..bc33dfd 100644 Binary files a/pkg/bpf/bpf_bpfeb.o and b/pkg/bpf/bpf_bpfeb.o differ diff --git a/pkg/bpf/bpf_bpfel.o b/pkg/bpf/bpf_bpfel.o index 2c746ad..68a6bab 100644 Binary files a/pkg/bpf/bpf_bpfel.o and b/pkg/bpf/bpf_bpfel.o differ