Skip to content

Instantly share code, notes, and snippets.

@TCY16
Last active October 5, 2020 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TCY16/f2ada48bec4070e649d668b63b1da677 to your computer and use it in GitHub Desktop.
Save TCY16/f2ada48bec4070e649d668b63b1da677 to your computer and use it in GitHub Desktop.
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