Skip to content

Instantly share code, notes, and snippets.

@DRiKE
Created July 16, 2020 13:47
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/18168d9d2cfda58457645a3fc4b21ed5 to your computer and use it in GitHub Desktop.
Save DRiKE/18168d9d2cfda58457645a3fc4b21ed5 to your computer and use it in GitHub Desktop.
XDP blog, post 1 gist 1
SEC("xdp-dns-says-no-v1")
int xdp_dns_says_no(struct xdp_md *ctx)
{
struct cursor c;
struct ethhdr *eth;
uint16_t eth_proto;
struct iphdr *ipv4;
struct ipv6hdr *ipv6;
cursor_init(&c, ctx);
if (!(eth = parse_eth(&c, &eth_proto)))
return XDP_PASS;
if (eth_proto == __bpf_htons(ETH_P_IP)) {
if (!(ipv4 = parse_iphdr(&c))
|| ipv4->protocol != IPPROTO_UDP
|| udp_dns_reply(&c))
return XDP_PASS;
uint32_t swap_ipv4 = ipv4->daddr;
ipv4->daddr = ipv4->saddr;
ipv4->saddr = swap_ipv4;
} else if (eth_proto == __bpf_htons(ETH_P_IPV6)) {
if (!(ipv6 = parse_ipv6hdr(&c))
|| ipv6->nexthdr != IPPROTO_UDP
|| udp_dns_reply(&c))
return XDP_PASS;
struct in6_addr swap_ipv6 = ipv6->daddr;
ipv6->daddr = ipv6->saddr;
ipv6->saddr = swap_ipv6;
} else
return XDP_PASS;
uint8_t swap_eth[ETH_ALEN];
memcpy(swap_eth, eth->h_dest, ETH_ALEN);
memcpy(eth->h_dest, eth->h_source, ETH_ALEN);
memcpy(eth->h_source, swap_eth, ETH_ALEN);
return XDP_TX;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment