Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

export VERSION = 1.2.1
export VERSION = 1.2.2
RELEASE ?= .tl3
export RELEASE

targets := droptrace nodetrace src legacy
targets := nodetrace src legacy
targets-call = for i in $1; do make $2 -C $$i $@; done
man-target := script/zh_CN/nettrace.8

Expand Down Expand Up @@ -45,7 +45,7 @@ install:
done

@mkdir -p $(BCOMP); cd $(BCOMP); cp $(SCRIPT)/bash-completion.sh \
./nettrace; ln -s nettrace droptrace
./nettrace

pack:
@make clean
Expand Down
134 changes: 99 additions & 35 deletions README.md

Large diffs are not rendered by default.

49 changes: 8 additions & 41 deletions common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ KERNEL_CFLAGS += $(NOSTDINC_FLAGS) $(LINUXINCLUDE) \
cmd_download = @if [ ! -f $(1) ]; then wget -O $(1) $(REMOTE_ROOT)/$(2); fi
cmd_exist = $(if $(wildcard $(1)),$(2),$(3))
cmd_or_exist = $(call cmd_exist,$(1),$(1),$(2))
ifeq ("$(wildcard $(HEADERS))$(wildcard $(BTF))",)
ifeq ("$(wildcard $(HEADERS))$(wildcard $(BTF))","")
$(error BTF is not found in your system, please install kernel headers)
endif

Expand All @@ -56,24 +56,12 @@ ifeq ($(if $(VMLINUX),$(wildcard $(VMLINUX)),"pass"),)
$(error vmlinux path not exist)
endif

# preferred to generate drop reason from
ifeq ("$(KERNEL)$(VMLINUX)","")
DROP_REASON := $(call cmd_or_exist,$(HEADERS)/include/net/dropreason.h,\
$(call cmd_or_exist,$(HEADERS)/include/linux/skbuff.h,\
$(call cmd_exist,$(BTF),vmlinux.h,)))
endif

# preferred to compile from kernel headers, then BTF
mode := $(if $(VMLINUX),'btf',$(call cmd_exist,$(HEADERS),'kernel','btf'))
ifeq ($(mode),'btf')
DROP_REASON ?= vmlinux.h
kheaders_cmd := ln -s vmlinux.h kheaders.h
kheaders_dep := vmlinux.h
else
ifndef DROP_REASON
DROP_REASON := $(call cmd_or_exist,$(HEADERS)/include/net/dropreason.h,\
$(call cmd_or_exist,$(HEADERS)/include/linux/skbuff.h,))
endif
kheaders_cmd := ln -s vmlinux_header.h kheaders.h
BPF_CFLAGS += $(KERNEL_CFLAGS)
endif
Expand All @@ -92,45 +80,24 @@ vmlinux.h:
kheaders.h: $(kheaders_dep)
$(call kheaders_cmd)

drop_reason.h: $(DROP_REASON)
rm -rf $@
ifneq ($(strip $(DROP_REASON)),)
@awk 'BEGIN{ print "#ifndef _H_SKB_DROP_REASON"; \
print "#define _H_SKB_DROP_REASON\n";\
system("sed -e \"/enum skb_drop_reason {/,/}/!d\" $< >> $@");\
print "\n#define __DEFINE_SKB_REASON(FN) \\";\
}\
/^enum skb_drop/ { dr=1; }\
/^\};/ { dr=0; }\
/^\tSKB_DROP_REASON_/ {\
if (dr) {\
sub(/SKB_DROP_REASON_/, "", $$1);\
sub(/,/, "", $$1);\
printf "\tFN(%s)\t\\\n", $$1;\
}\
}\
END{ print "\n#endif" }' $< >> $@
@echo generated drop_reason.h
else
touch $@
@echo drop reason not supported, skips
endif

progs/%.o: progs/%.c kheaders.h
clang -O2 -c -g -S -Wall -Wno-pointer-sign -Wno-unused-value \
-Wno-incompatible-pointer-types-discards-qualifiers \
-fno-asynchronous-unwind-tables \
$< -emit-llvm -Wno-unknown-attributes $(BPF_CFLAGS) -o - | \
$< -emit-llvm -Wno-unknown-attributes $(BPF_CFLAGS) -Xclang \
-disable-llvm-passes -o - | \
opt -O2 -mtriple=bpf-pc-linux | \
llvm-dis | \
llc -march=bpf -filetype=obj -o $@
@file $@ | grep debug_info > /dev/null || (rm $@ && exit 1)

%.skel.h: %.o
$(BPFTOOL) gen skeleton $< > $@
$(BPFTOOL) gen skeleton $< $(SKEL_FLAGS) > $@

$(bpf_progs): %: %.skel.h
@echo "bpf compile success"
@:

bpf: $(bpf_progs)
bpf: $(bpf_progs) $(bpf_progs_ext)

$(progs): %: %.c bpf
@if [ -n "$(prog-$@)" ]; then \
Expand Down
7 changes: 7 additions & 0 deletions component/arg_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ int parse_args(int argc, char *argv[], arg_config_t *config,
}
S_SET(bool, true);
break;
case OPTION_IPV6:
if (ipv6toi(optarg, item->dest)) {
printf("invalid ip address: %s\n", optarg);
goto err;
}
S_SET(bool, true);
break;
case OPTION_HELP:
goto help;
case OPTION_PROTO: {
Expand Down
1 change: 1 addition & 0 deletions component/arg_parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ enum option_type {
OPTION_U32,
OPTION_INT,
OPTION_IPV4,
OPTION_IPV6,
OPTION_HELP,
OPTION_BLANK,
OPTION_PROTO,
Expand Down
39 changes: 39 additions & 0 deletions component/net_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include "net_utils.h"
#include "arg_parse.h"

#define _LINUX_IN_H
#include <netinet/in.h>

typedef struct {
char *name;
int val;
Expand Down Expand Up @@ -143,3 +146,39 @@ int proto2i(char *proto, int *dest)
return 4;
return 0;
}

void i2ipv6(char *dest, __u8 ip[])
{
int i = 0, offset = 0;

for (; i < 16; i += 2) {
if (ip[i] || ip[i + 1])
offset += sprintf(dest + offset, "%02x%02x:",
ip[i], ip[i + 1]);
else
offset += sprintf(dest + offset, ":");
}
*(dest + offset - 1) = '\0';
}

int ipv6toi(char *ip, __u8 *dest)
{
u16 *c = (u16 *)dest;
u32 t[8] = {}, i = 0;

if (sscanf(ip, "%x:%x:%x:%x:%x:%x:%x:%x", t, t + 1, t + 2,
t + 3, t + 4, t + 5, t + 6, t + 7) == 8)
goto is_valid;

memset(t, 0, sizeof(t));
if (sscanf(ip, "%x::%x:%x:%x:%x", t, t + 4, t + 5, t + 6,
t + 7) == 5)
goto is_valid;

return -1;

is_valid:
for (i = 0; i < 8; i++)
c[i] = htons(t[i]);
return 0;
}
9 changes: 6 additions & 3 deletions component/net_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ typedef __u32 u32;
typedef __s64 s64;
typedef __u64 u64;

extern char *l4_proto_names[];

static inline void i2ip(char *dest, __u32 ip)
{
u8 *t = (u8 *)&ip;
Expand All @@ -48,12 +50,13 @@ static inline int ip2i(char *ip, __u32 *dest)
return 0;
}

int proto2i(char *proto, int *dest);

extern char *l4_proto_names[];
static inline char *i2l4(u8 num)
{
return l4_proto_names[num];
}

int proto2i(char *proto, int *dest);
void i2ipv6(char *dest, u8 ip[]);
int ipv6toi(char *ip, u8 *dest);

#endif
23 changes: 23 additions & 0 deletions component/sys_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <string.h>
#include <stdlib.h>
#include <sys/resource.h>
#include <sys/utsname.h>

#include "sys_utils.h"

Expand Down Expand Up @@ -57,3 +58,25 @@ int liberate_l()
struct rlimit lim = {RLIM_INFINITY, RLIM_INFINITY};
return setrlimit(RLIMIT_MEMLOCK, &lim);
}

bool fsearch(FILE *f, char *target)
{
char tmp[128];

while (fscanf(f, "%s", tmp) == 1) {
if (strstr(tmp, target))
return true;
}
return false;
}

int kernel_version()
{
int major, minor, patch;
struct utsname buf;

uname(&buf);
sscanf(buf.release, "%d.%d.%d", &major, &minor, &patch);

return kv_to_num(major, minor, patch);
}
23 changes: 20 additions & 3 deletions component/sys_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

extern int log_level;

int execf(char *output, char *fmt, ...);
int exec(char *cmd, char *output);
int liberate_l();
int execf(char *output, char *fmt, ...);
int exec(char *cmd, char *output);
int liberate_l();
bool fsearch(FILE *f, char *target);
int kernel_version();

static inline int simple_exec(char *cmd)
{
Expand All @@ -23,6 +29,17 @@ static inline bool file_exist(char *path)
return access(path, F_OK) == 0;
}

static inline int kv_to_num(int major, int minor, int patch)
{
return (major << 16) + (minor << 8) + patch;
}

/* compare current kernel version with the provided one */
static inline int kv_compare(int major, int minor, int patch)
{
return kernel_version() - kv_to_num(major, minor, patch);
}

#define pr_level(level, target, fmt, args...) \
do { \
if (level <= log_level) \
Expand Down
5 changes: 0 additions & 5 deletions droptrace/.gitignore

This file was deleted.

31 changes: 0 additions & 31 deletions droptrace/Makefile

This file was deleted.

1 change: 0 additions & 1 deletion droptrace/README.md

This file was deleted.

Loading