fix: remove deref_to_depth on single depth pointers

This commit is contained in:
2025-11-27 22:59:34 +05:30
parent f135cdbcc0
commit de8c486461
4 changed files with 40 additions and 40 deletions

View File

@ -241,7 +241,11 @@ def get_operand_value(
var_type = var.type
base_type, depth = get_base_type_and_depth(var_type)
logger.info(f"var is {var}, base_type is {base_type}, depth is {depth}")
val = deref_to_depth(func, builder, var, depth)
if depth == 1:
val = builder.load(var)
return val
else:
val = deref_to_depth(func, builder, var, depth)
return val
else:
# Check if it's a vmlinux enum/constant

View File

@ -1,38 +1,36 @@
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <linux/bpf.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <bpf/bpf_helpers.h>
struct fake_iphdr {
unsigned short useless;
unsigned short tot_len;
unsigned short id;
unsigned short frag_off;
unsigned char ttl;
unsigned char protocol;
unsigned short check;
unsigned int saddr;
unsigned int daddr;
unsigned short useless;
unsigned short tot_len;
unsigned short id;
unsigned short frag_off;
unsigned char ttl;
unsigned char protocol;
unsigned short check;
unsigned int saddr;
unsigned int daddr;
};
SEC("xdp")
int xdp_prog(struct xdp_md *ctx)
{
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
int xdp_prog(struct xdp_md *ctx) {
unsigned long data = ctx->data;
unsigned long data_end = ctx->data_end;
struct ethhdr *eth = data;
if ((void *)(eth + 1) > data_end)
return XDP_ABORTED;
if (eth->h_proto != __constant_htons(ETH_P_IP))
return XDP_PASS;
if (data + sizeof(struct ethhdr) + sizeof(struct fake_iphdr) <= data_end) {
struct fake_iphdr *iph = (void *)data + sizeof(struct ethhdr);
struct fake_iphdr *iph = (struct fake_iphdr *)(eth + 1);
if ((void *)(iph + 1) > data_end)
return XDP_ABORTED;
bpf_printk("%d", iph->saddr);
return XDP_PASS;
} else {
return XDP_ABORTED;
}
struct task_struct * a = btf_bpf_get_current_task_btf();
}
char _license[] SEC("license") = "GPL";

View File

@ -1,10 +1,9 @@
from vmlinux import XDP_PASS, XDP_DROP
from vmlinux import XDP_PASS, XDP_ABORTED
from vmlinux import (
struct_xdp_md,
struct_ethhdr,
)
from pythonbpf import bpf, section, bpfglobal, compile, compile_to_ir, struct
from ctypes import c_int64, c_ubyte, c_ushort, c_uint32
from ctypes import c_int64, c_ubyte, c_ushort, c_uint32, c_void_p
@bpf
@ -24,19 +23,15 @@ class iphdr:
@bpf
@section("xdp")
def ip_detector(ctx: struct_xdp_md) -> c_int64:
data = ctx.data
data_end = ctx.data_end
if data + 14 > data_end:
return c_int64(XDP_DROP)
eth = struct_ethhdr(data)
nh = data + 14
if nh + 20 > data_end:
return c_int64(XDP_DROP)
iph = iphdr(nh)
print(f"ipaddress: {iph.saddr}")
data = c_void_p(ctx.data)
data_end = c_void_p(ctx.data_end)
if data + 34 < data_end:
hdr = data + 14
iph = iphdr(hdr)
addr = iph.saddr
print(f"ipaddress: {addr}")
else:
return c_int64(XDP_ABORTED)
return c_int64(XDP_PASS)

View File

@ -1,4 +1,4 @@
from pythonbpf import bpf, struct, section, bpfglobal
from pythonbpf import bpf, struct, section, bpfglobal, compile
from pythonbpf.helper import comm
from ctypes import c_void_p, c_int64
@ -26,3 +26,6 @@ def hello(ctx: c_void_p) -> c_int64:
@bpfglobal
def LICENSE() -> str:
return "GPL"
compile()