-
-
Save TCY16/f2ada48bec4070e649d668b63b1da677 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static __always_inline | |
int udp_dns_reply_v4(struct cursor *c, uint32_t key) | |
{ | |
struct udphdr *udp; | |
struct dnshdr *dns; | |
// check that we have a DNS packet | |
if (!(udp = parse_udphdr(c)) || udp->dest != __bpf_htons(DNS_PORT) | |
|| !(dns = parse_dnshdr(c))) | |
return 1; | |
// get the rrl bucket from the map by IPv4 address | |
struct bucket *b = bpf_map_lookup_elem(&state_map, &key); | |
// did we see this IPv4 address before? | |
if (b) | |
return do_rate_limit(udp, dns, b); | |
// create new starting bucket for this IPv4 address | |
struct bucket new_bucket; | |
new_bucket.start_time = bpf_ktime_get_ns(); | |
new_bucket.n_packets = 0; | |
// store the bucket and pass the packet | |
bpf_map_update_elem(&state_map, &key, &new_bucket, BPF_ANY); | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment