-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
78 lines (63 loc) · 2.38 KB
/
Makefile
File metadata and controls
78 lines (63 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
BPFTOOL ?= bpftool
CLANG ?= clang -g
LLVM_STRIP ?= llvm-strip
INTERFACE ?= eth0
SOURCES = ./modules/base.c
OBJECTS = $(SOURCES:.c=.o)
#KERNEL_HEADERS = /usr/src/linux-headers-$(shell uname -r)
BPF_HEADERS = ./libbpf/src
#CFLAGS = -O2 -Wall -Werror -Wno-address-of-packed-member -I$(KERNEL_HEADERS)/include -I$(KERNEL_HEADERS)/arch/x86/include -I$(BPF_HEADERS)
CFLAGS = -O2 -Wall -Werror -Wno-address-of-packed-member -Wno-unused-variable -I$(BPF_HEADERS)
.PHONY: all clean load unload attach detach ip
all: $(OBJECTS)
%.o: %.c
$(CLANG) -target bpf -c $(CFLAGS) $< -o $@
$(LLVM_STRIP) -g $@
clean:
rm -f $(OBJECTS)
load: $(OBJECTS)
$(foreach obj, $(OBJECTS), \
sudo $(BPFTOOL) prog load $(obj) /sys/fs/bpf/$(basename $(notdir $(obj))); \
)
unload: $(OBJECTS)
$(foreach obj, $(OBJECTS), \
sudo rm -rf /sys/fs/bpf/$(basename $(notdir $(obj))); \
)
attach: $(OBJECTS)
$(foreach obj, $(OBJECTS), \
sudo $(BPFTOOL) net attach xdp name tinyxdp_$(basename $(notdir $(obj))) dev $(INTERFACE); \
)
detach:
$(foreach obj, $(OBJECTS), \
sudo $(BPFTOOL) net detach xdp dev $(INTERFACE); \
)
ip:
@/bin/bash -c ' \
source ./ip_utils.sh; \
if [ -z "$(ACTION)" ] || [ -z "$(NETWORK)" ] || [ -z "$(MASK)" ]; then \
echo "Usage: make ip ACTION=<add|remove> NETWORK=<NETWORK_ADDRESS> MASK=<SUBNET_MASK>"; \
exit 1; \
fi; \
map_id=$$(sudo bpftool map show | grep -w whitelist_map | awk "{print \$$1}" | cut -d: -f1); \
if [ -z "$$map_id" ]; then \
echo "whitelist_map not found"; \
exit 1; \
fi; \
network=$$(ip_to_hex $(NETWORK)); \
prefix_len=$$(mask_to_prefix $(MASK)); \
key="0x$$(printf "%02x" $$prefix_len) 0x00 0x00 0x00 0x$${network:0:2} 0x$${network:2:2} 0x$${network:4:2} 0x$${network:6:2}"; \
echo "Debug: map_id=$$map_id"; \
echo "Debug: key=$$key"; \
if [ "$(ACTION)" = "add" ]; then \
echo "Debug: Executing command:"; \
echo "sudo bpftool map update id $$map_id key $$key value 0x01 0x00 0x00 0x00"; \
sudo bpftool map update id $$map_id key $$key value 0x01 0x00 0x00 0x00; \
echo "Added network $(NETWORK)/$$prefix_len to whitelist"; \
elif [ "$(ACTION)" = "remove" ]; then \
echo "Debug: Executing command:"; \
echo "sudo bpftool map delete id $$map_id key $$key"; \
sudo bpftool map delete id $$map_id key $$key; \
echo "Removed network $(NETWORK)/$$prefix_len from whitelist"; \
else \
echo "Invalid action: $(ACTION). Use '\''add'\'' or '\''remove'\''."; \
fi'