Skip to content

Commit

Permalink
set v4 and more debug
Browse files Browse the repository at this point in the history
  • Loading branch information
boratanrikulu committed Jul 27, 2024
1 parent 8a1846c commit 812e016
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
22 changes: 22 additions & 0 deletions internal/ebpf-c/xdp.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ int xdp_durdur_func(struct xdp_md *ctx)

if (data + sizeof(struct ethhdr) + sizeof(struct iphdr) > data_end)
{
printk("[ERROR] Packet too short for Ethernet and IP headers");
return XDP_PASS;
}

Expand All @@ -129,6 +130,12 @@ int xdp_durdur_func(struct xdp_md *ctx)

struct iphdr *ip = data + sizeof(struct ethhdr);

if ((void *)(ip + 1) > data_end)
{
printk("[ERROR] Packet too short for IP header");
return XDP_PASS;
}

__u32 ip_src = ip->saddr;
long *pkt_count = bpf_map_lookup_elem(&drop_src_addrs, &ip_src);
if (pkt_count)
Expand All @@ -142,25 +149,40 @@ int xdp_durdur_func(struct xdp_md *ctx)
struct udphdr *udp;
if (data + sizeof(struct ethhdr) + sizeof(struct iphdr) + sizeof(struct udphdr) > data_end)
{
printk("[ERROR] Packet too short for UDP header");
return XDP_PASS;
}

udp = data + sizeof(struct ethhdr) + sizeof(struct iphdr);
if ((void *)(udp + 1) > data_end)
{
printk("[ERROR] Packet too short for UDP header");
return XDP_PASS;
}

if (udp->source == bpf_htons(53))
{
if (data + sizeof(*eth) + sizeof(*ip) + sizeof(*udp) + sizeof(struct dnshdr) > data_end)
{
printk("[ERROR] Packet too short for DNS header");
return XDP_PASS;
}

struct dnshdr *dns = data + sizeof(*eth) + sizeof(*ip) + sizeof(*udp);
if ((void *)(dns + 1) > data_end)
{
printk("[ERROR] Packet too short for DNS header");
return XDP_PASS;
}

if (dns->opcode == 0) // it's a dns query.
{
void *query_start = (void *)dns + sizeof(struct dnshdr);

struct dnsquery query;
if (!parse_query(data_end, query_start, &query))
{
printk("[ERROR] Failed to parse DNS query");
return XDP_PASS;
}

Expand Down
4 changes: 2 additions & 2 deletions internal/ebpf/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (tw *tWrap) Run(c *qt.C, until string, f func(e *EBPF)) {

// TTCPWrite tests the TCP connection through the address.
func TTCPWrite(c *qt.C, address string, ok bool) {
conn, err := net.DialTimeout("tcp", address, 2*time.Second)
conn, err := net.DialTimeout("tcp4", address, 2*time.Second)
if !ok {
c.Assert(err, qt.ErrorMatches, ".* i/o timeout")
return
Expand All @@ -114,7 +114,7 @@ func TDNSLookup(c *qt.C, dns string, ok bool) {
},
}

_, err := r.LookupIP(context.Background(), "ip", dns)
_, err := r.LookupIP(context.Background(), "ip4", dns)
if !ok {
c.Assert(err, qt.IsNotNil)
return
Expand Down

0 comments on commit 812e016

Please sign in to comment.