Skip to content

Instantly share code, notes, and snippets.

@DRiKE
Created July 16, 2020 14:08
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 DRiKE/adec28bbdc11b99406eb9215e4e1a9b5 to your computer and use it in GitHub Desktop.
Save DRiKE/adec28bbdc11b99406eb9215e4e1a9b5 to your computer and use it in GitHub Desktop.
XDP blog, post 1 gist 2
struct dnshdr {
uint16_t id;
union {
struct {
uint8_t rd : 1;
uint8_t tc : 1;
uint8_t aa : 1;
uint8_t opcode : 4;
uint8_t qr : 1;
uint8_t rcode : 4;
uint8_t cd : 1;
uint8_t ad : 1;
uint8_t z : 1;
uint8_t ra : 1;
} as_bits_and_pieces;
uint16_t as_value;
} flags;
uint16_t qdcount;
uint16_t ancount;
uint16_t nscount;
uint16_t arcount;
};
static __always_inline
int udp_dns_reply(struct cursor *c)
{
struct udphdr *udp;
struct dnshdr *dns;
if (!(udp = parse_udphdr(c))|| udp->dest != __bpf_htons(DNS_PORT)
|| !(dns = parse_dnshdr(c)))
return -1;
uint16_t old_val = dns->flags.as_value;
dns->flags.as_bits_and_pieces.ad = 0;
dns->flags.as_bits_and_pieces.qr = 1;
dns->flags.as_bits_and_pieces.rcode = RCODE_REFUSED;
update_checksum(&udp->check, old_val, dns->flags.as_value);
udp->dest = udp->source;
udp->source = __bpf_htons(DNS_PORT);
return 0;
}
@DRiKE
Copy link
Author

DRiKE commented Mar 23, 2021

@Wqrld, these are just snippets (embedded in blog posts). Please checkout https://github.com/NLnetLabs/XDPeriments/ for the full code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment