mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
fix: remove deref_to_depth on single depth pointers
This commit is contained in:
@ -241,7 +241,11 @@ def get_operand_value(
|
|||||||
var_type = var.type
|
var_type = var.type
|
||||||
base_type, depth = get_base_type_and_depth(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}")
|
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
|
return val
|
||||||
else:
|
else:
|
||||||
# Check if it's a vmlinux enum/constant
|
# Check if it's a vmlinux enum/constant
|
||||||
|
|||||||
@ -1,38 +1,36 @@
|
|||||||
|
#include "vmlinux.h"
|
||||||
|
#include <bpf/bpf_helpers.h>
|
||||||
#include <linux/bpf.h>
|
#include <linux/bpf.h>
|
||||||
#include <linux/if_ether.h>
|
#include <linux/if_ether.h>
|
||||||
#include <linux/ip.h>
|
#include <linux/ip.h>
|
||||||
#include <bpf/bpf_helpers.h>
|
|
||||||
|
|
||||||
struct fake_iphdr {
|
struct fake_iphdr {
|
||||||
unsigned short useless;
|
unsigned short useless;
|
||||||
unsigned short tot_len;
|
unsigned short tot_len;
|
||||||
unsigned short id;
|
unsigned short id;
|
||||||
unsigned short frag_off;
|
unsigned short frag_off;
|
||||||
unsigned char ttl;
|
unsigned char ttl;
|
||||||
unsigned char protocol;
|
unsigned char protocol;
|
||||||
unsigned short check;
|
unsigned short check;
|
||||||
unsigned int saddr;
|
unsigned int saddr;
|
||||||
unsigned int daddr;
|
unsigned int daddr;
|
||||||
};
|
};
|
||||||
|
|
||||||
SEC("xdp")
|
SEC("xdp")
|
||||||
int xdp_prog(struct xdp_md *ctx)
|
int xdp_prog(struct xdp_md *ctx) {
|
||||||
{
|
unsigned long data = ctx->data;
|
||||||
void *data_end = (void *)(long)ctx->data_end;
|
unsigned long data_end = ctx->data_end;
|
||||||
void *data = (void *)(long)ctx->data;
|
|
||||||
|
|
||||||
struct ethhdr *eth = data;
|
if (data + sizeof(struct ethhdr) + sizeof(struct fake_iphdr) <= data_end) {
|
||||||
if ((void *)(eth + 1) > data_end)
|
struct fake_iphdr *iph = (void *)data + sizeof(struct ethhdr);
|
||||||
return XDP_ABORTED;
|
|
||||||
if (eth->h_proto != __constant_htons(ETH_P_IP))
|
|
||||||
return XDP_PASS;
|
|
||||||
|
|
||||||
struct fake_iphdr *iph = (struct fake_iphdr *)(eth + 1);
|
|
||||||
if ((void *)(iph + 1) > data_end)
|
|
||||||
return XDP_ABORTED;
|
|
||||||
bpf_printk("%d", iph->saddr);
|
bpf_printk("%d", iph->saddr);
|
||||||
|
|
||||||
return XDP_PASS;
|
return XDP_PASS;
|
||||||
|
} else {
|
||||||
|
return XDP_ABORTED;
|
||||||
|
}
|
||||||
|
struct task_struct * a = btf_bpf_get_current_task_btf();
|
||||||
}
|
}
|
||||||
|
|
||||||
char _license[] SEC("license") = "GPL";
|
char _license[] SEC("license") = "GPL";
|
||||||
|
|||||||
@ -1,10 +1,9 @@
|
|||||||
from vmlinux import XDP_PASS, XDP_DROP
|
from vmlinux import XDP_PASS, XDP_ABORTED
|
||||||
from vmlinux import (
|
from vmlinux import (
|
||||||
struct_xdp_md,
|
struct_xdp_md,
|
||||||
struct_ethhdr,
|
|
||||||
)
|
)
|
||||||
from pythonbpf import bpf, section, bpfglobal, compile, compile_to_ir, struct
|
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
|
@bpf
|
||||||
@ -24,19 +23,15 @@ class iphdr:
|
|||||||
@bpf
|
@bpf
|
||||||
@section("xdp")
|
@section("xdp")
|
||||||
def ip_detector(ctx: struct_xdp_md) -> c_int64:
|
def ip_detector(ctx: struct_xdp_md) -> c_int64:
|
||||||
data = ctx.data
|
data = c_void_p(ctx.data)
|
||||||
data_end = ctx.data_end
|
data_end = c_void_p(ctx.data_end)
|
||||||
if data + 14 > data_end:
|
if data + 34 < data_end:
|
||||||
return c_int64(XDP_DROP)
|
hdr = data + 14
|
||||||
|
iph = iphdr(hdr)
|
||||||
eth = struct_ethhdr(data)
|
addr = iph.saddr
|
||||||
nh = data + 14
|
print(f"ipaddress: {addr}")
|
||||||
if nh + 20 > data_end:
|
else:
|
||||||
return c_int64(XDP_DROP)
|
return c_int64(XDP_ABORTED)
|
||||||
|
|
||||||
iph = iphdr(nh)
|
|
||||||
|
|
||||||
print(f"ipaddress: {iph.saddr}")
|
|
||||||
|
|
||||||
return c_int64(XDP_PASS)
|
return c_int64(XDP_PASS)
|
||||||
|
|
||||||
|
|||||||
@ -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 pythonbpf.helper import comm
|
||||||
|
|
||||||
from ctypes import c_void_p, c_int64
|
from ctypes import c_void_p, c_int64
|
||||||
@ -26,3 +26,6 @@ def hello(ctx: c_void_p) -> c_int64:
|
|||||||
@bpfglobal
|
@bpfglobal
|
||||||
def LICENSE() -> str:
|
def LICENSE() -> str:
|
||||||
return "GPL"
|
return "GPL"
|
||||||
|
|
||||||
|
|
||||||
|
compile()
|
||||||
|
|||||||
Reference in New Issue
Block a user