Skip to content

Instantly share code, notes, and snippets.

@amiremohamadi
Created September 7, 2021 07:43
Show Gist options
  • Save amiremohamadi/fb62c22f7fb6a15fa4fa0b50ab4f230d to your computer and use it in GitHub Desktop.
Save amiremohamadi/fb62c22f7fb6a15fa4fa0b50ab4f230d to your computer and use it in GitHub Desktop.
#include <linux/bpf.h>
#include <linux/if_ether.h>
#include <linux/in.h>
#include <linux/ip.h>
#include <linux/ipv6.h>
#include <linux/pkt_cls.h>
#include <linux/tcp.h>
/* struct bpf_map_def reuseport_map = { */
/* .type = BPF_MAP_TYPE_ARRAY, */
/* .key_size = sizeof(__u32), */
/* .value_size = sizeof(__u64), */
/* .max_entries = 16, */
/* }; */
BPF_HASH(reuse_map, u32, u64, 1024);
int handler(struct sk_reuseport_md *ctx)
{
int err;
u32 key=12;
u32 value = 0, *val;
reuse_map.lookup(&key);
void *data = ctx->data;
void *data_end = ctx->data_end;
u32 skb_addrs[8];
bpf_skb_load_bytes_relative(ctx, offsetof(struct iphdr, saddr), skb_addrs, 8, BPF_HDR_START_NET);
return SK_DROP;
}
from bcc import BPF
device = "wg0"
flags = 0
flags |= (1 << 1)
b = BPF(src_file="./bpf.c")
fn = b.load_func("handler", BPF.XDP, None)
b.attach_xdp(device, fn, flags)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment