我正在使用 xdp(使用 XDP_TX 操作)来构造一个 icmp resv 数据包。
linux:centos8
内核 4.18.0-80.el8.x86_64
llvm:7.0.1
clang:clang 版本 7.0.1 (tags/RELEASE_701/final)
这是我的代码:
SEC("xdp_icmp")
int _xdp_icmp(struct xdp_md *xdp)
{
void *data_end = (void *)(long)xdp->data_end;
void *data = (void *)(long)xdp->data;
struct ethhdr *eth = data;
struct iphdr *iph;
struct icmphdr *icmph;
__u16 h_proto;
__be32 raddr;
....
icmph = data + sizeof(*eth) + sizeof(*iph);
if (icmph + 1 > data_end)
return XDP_DROP;
if (icmph -> type != ICMP_ECHO)
{
return XDP_PASS;
}
if (handle_ipv4(xdp) != XDP_TX)
{
return XDP_PASS;
}
raddr = iph->saddr;
swap_src_dst_mac(data);
iph->saddr = iph->daddr;
iph->daddr = raddr;
icmph->type = ICMP_ECHOREPLY;
icmph->checksum = 0;
__u32 sum = 0;
sum = bpf_csum_diff(0, 0, icmph, ICMP_ECHO_LEN, 0);
icmph->checksum = csum_fold_helper(sum);
return XDP_TX;
}
但是编译结果告诉我“验证器失败”:
错误:
libbpf: load bpf program failed: Permission denied
libbpf: -- BEGIN DUMP LOG ---
libbpf:
...
48: (b7) r5 = 0
49: (85) call bpf_csum_diff#28
invalid access to packet, off=34 size=64, R3(id=0,off=34,r=42)
R3 offset is outside of the packet