invert conditionals, removing crazy nesting
All checks were successful
continuous-integration/drone/push Build is passing

Thank you code aesthetic
This commit is contained in:
Steven Polley 2024-05-09 20:17:02 -06:00
parent 390fabe1b4
commit b318bcb3c1

View File

@ -40,15 +40,27 @@ int xdp_prog_func(struct xdp_md *ctx) {
// parse ethernet header // parse ethernet header
struct ethhdr *eth = data; struct ethhdr *eth = data;
if ((void *)eth + sizeof(*eth) <= data_end) { if ((void *)eth + sizeof(*eth) > data_end) {
return XDP_PASS;
}
// parse IP header // parse IP header
struct iphdr *ip = data + sizeof(*eth); struct iphdr *ip = data + sizeof(*eth);
if ((void *)ip + sizeof(*ip) <= data_end) { if ((void *)ip + sizeof(*ip) > data_end) {
if (ip->protocol == IPPROTO_UDP) { return XDP_PASS;
}
// Ensure IP header protocol field is UDP (protocol 17)
if (ip->protocol != IPPROTO_UDP) {
return XDP_PASS;
}
// parse UDP header // parse UDP header
struct udphdr *udp = (void *)ip + sizeof(*ip); struct udphdr *udp = (void *)ip + sizeof(*ip);
if ((void *)udp + sizeof(*udp) <= data_end) if ((void *)udp + sizeof(*udp) > data_end) {
{ return XDP_PASS;
}
// pack into knock structure and send to userspace // pack into knock structure and send to userspace
struct knock_data knock = { struct knock_data knock = {
.srcip = bpf_ntohl(ip->saddr), .srcip = bpf_ntohl(ip->saddr),
@ -56,10 +68,7 @@ int xdp_prog_func(struct xdp_md *ctx) {
.pad = 0 .pad = 0
}; };
bpf_ringbuf_output(&rb, &knock, sizeof(knock), BPF_RB_FORCE_WAKEUP); bpf_ringbuf_output(&rb, &knock, sizeof(knock), BPF_RB_FORCE_WAKEUP);
}
}
}
}
// We send everything to XDP_PASS // We send everything to XDP_PASS
return XDP_PASS; return XDP_PASS;
} }