REM:

Init Projects
This commit is contained in:
黄昕 2019-06-11 11:21:35 +08:00
parent 6d861432b6
commit c5216fbefa
57 changed files with 20269 additions and 0 deletions

View File

543
Common/common.Makefile Executable file
View File

@ -0,0 +1,543 @@
# 调试,打印当前 MAKE 运行环境和配置信息
ifeq ($(SHOW_MAKE_EVENT), TRUE)
$(info MAKEFILES:$(MAKEFILES))
$(info MAKEFILES_LIST:$(MAKEFILES_LIST))
$(info SHELL:$(SHELL))
$(info MAKESHELL:$(MAKESHELL))
$(info MAKE:$(MAKE))
$(info MAKELEVEL:$(MAKELEVEL))
$(info MAKECMDGOALS:$(MAKECMDGOALS))
$(info SUFFIXES:$(SUFFIXES))
$(info LIBPATTERNS:$(.LIBPATTERNS))
$(info Make $(TARGET_OBJ): $(CURDIR))
endif
# 定义 Make 解析命令的 bash
SHELL := /bin/sh
# 构建信息开关,是否显示 gcc、ld 等命令执行的命令行消息,默认关闭
ENABLE_MAKE_LOG := FALSE
# 调试信息分离开关,默认开启
TARGET_STRIP ?= TRUE
# 定义各个平台编译工具,建议设置编译工具路径到 PATH 环境变量
# CC : Cross Build Tools
# LD : Cross Link Tools
CC := gcc
OBJCOPY := objcopy
STRIP := strip
AR := ar
ARM64_CROSS_CC := aarch64-fsl-linux-gcc
ARM64_CROSS_OBJCOPY := aarch64-fsl-linux-objcopy
ARM64_CROSS_STRIP := aarch64-fsl-linux-strip
ARM64_CROSS_AR := aarch64-fsl-linux-ar
ARM64_CROSS_AS := aarch64-fsl-linux-as
ifeq ($(TARGET_TYPE), KO)
ARM64_CROSS_LD := aarch64-fsl-linux-ld
LINUX_CROSS_LD := ld
else
ARM64_CROSS_LD := $(ARM64_CROSS_CC)
LINUX_CROSS_LD := $(CC)
endif
# 检查必要的环境变量是否设置
ifeq ($(PLAT_ARM64), TRUE)
# ARM64 SDK 环境变量
ifeq ($(SDKTARGETSYSROOT), )
$(error Unknown SDKTARGETSYSROOT environment Of [$(SDKTARGETSYSROOT)], It's must be set to current environment used <export SDKTARGETSYSROOT=...>)
else
ifneq ($(SDKTARGETSYSROOT), $(wildcard $(SDKTARGETSYSROOT)))
$(error Directory $(SDKTARGETSYSROOT) not exists, Please check it first.)
endif
endif
endif
ifeq ($(TARGET_OBJ), DRV)
# 华晨 ARM64 平台已经编译成功的内核路径,供编译驱动使用
ifeq ($(HUACHENG_ARM64_KERNEL), )
$(error Unknown HUACHENG_ARM64_KERNEL environment Of [$(HUACHENG_ARM64_KERNEL)], It's must be set to current environment used <export HUACHENG_ARM64_KERNEL=...>)
else
ifneq ($(HUACHENG_ARM64_KERNEL), $(wildcard $(HUACHENG_ARM64_KERNEL)))
$(error Directory $(HUACHENG_ARM64_KERNEL) not exists, Please check it first.)
endif
endif
# 华晨 LINUX 平台已经编译成功的内核路径,供编译驱动使用
ifeq ($(HUACHENG_LINUX_KERNEL), )
$(error Unknown HUACHENG_LINUX_KERNEL environment Of [$(HUACHENG_LINUX_KERNEL)], It's must be set to current environment used <export HUACHENG_LINUX_KERNEL=...>)
else
ifneq ($(HUACHENG_LINUX_KERNEL), $(wildcard $(HUACHENG_LINUX_KERNEL)))
$(error Directory $(HUACHENG_LINUX_KERNEL) not exists, Please check it first.)
endif
endif
endif
# @符号添加在执行的命令前,则该命令不会在控制台上打印这条命令执行的信息
ifeq ($(ENABLE_MAKE_LOG), TRUE)
MLOG :=
else
MLOG := @
endif
# 判断构建模式 debug/release
ifeq ($(DEBUG), TRUE)
TARGET_OUT_DIR ?= debug/
else
TARGET_OUT_DIR ?= release/
endif
# 设置安装路径的相对路径
ifeq ($(DIR), )
INSTALL_ROOT := $(addprefix ../,../_install/$(TARGET_OUT_DIR))
else
INSTALL_ROOT := $(addprefix ../,$(DIR)/$(TARGET_OUT_DIR))
endif
# 定义常用命令
RM = rm -rf
MKDIR = mkdir -p
CP = cp -f
ECHO_COLOR = echo
# 根据环境变量获取内核路径
ifeq ($(TARGET_OBJ), DRV)
ARM64_KERNEL := $(HUACHENG_ARM64_KERNEL)
LINUX_KERNEL := $(HUACHENG_LINUX_KERNEL)
endif
# 设置 ARM64 平台 SDK 头文件和库文件路径
ARM64_SDK_INCLUDE := $(SDKTARGETSYSROOT)/usr/include
ARM64_SDK_LIBDIR := $(SDKTARGETSYSROOT)/usr/lib
# 设置平台安装子目录
CPU_ARM64_DIR := ARM64
CPU_LINUX_DIR := LINUX
# 根据类型设置生成目标的扩展名
ifeq ($(TARGET_TYPE), KO)
TARGET_EXT := .ko
else ifeq ($(TARGET_TYPE), DLL)
TARGET_EXT := .so
else ifeq ($(TARGET_TYPE), SDK)
TARGET_EXT := .so
else ifeq ($(TARGET_TYPE), LIB)
TARGET_EXT := .a
else ifeq ($(TARGET_TYPE), EXE)
TARGET_EXT := .exe
else ifeq ($(TARGET_TYPE), SH)
UNMAKE_PROCESS := TRUE
else
$(error Unknown TARGET_TYPE Value Of [$(TARGET_TYPE)], It's Must Be Set Of KO/EXE/DLL/LIB/SDK)
endif
# 如果生成类型为库文件,则在生成目标的文件名前加上 lib 前缀
ifeq ($(TARGET_TYPE), LIB)
TARGET_PREFIX := lib
else ifeq ($(TARGET_TYPE), SDK)
TARGET_PREFIX := lib
else
TARGET_PREFIX :=
endif
# 定义各个类型关键字段控制台显示颜色
ifneq ($(MAKE_TARGET), )
MAKECMD_COLOR := \033[36;48m\033[1m$(shell env printf "%-10s" {$(MAKE_TARGET)}) \033[0m
else
MAKECMD_COLOR :=
endif
CLEAN_COLOR := $(MAKECMD_COLOR)\033[31;48m\033[1m[CLEAN]\033[0m
PATH_COLOR := \033[34;48m\033[1m($(CURDIR))\033[0m
INSTALL_PATH_COLOR := \033[34;48m\033[1m(TO $(INSTALL_ROOT))\033[0m
INSTALL_COLOR := \033[36;48m\033[1m[INSTALL]\033[0m
PRE_INSTALL_COLOR := \033[36;48m\033[1m[PRE INSTALL]\033[0m
INSTALL_USER_ITEM_COLOR := \033[36;48m\033[1m[INSTALL USER ITEMS]\033[0m
INSTALL_MULT_PLAT_COLOR := \033[36;48m\033[1m[INSTALL MULTPLATFORM ITEMS]\033[0m
INSTALL_IDK_ITEM_COLOR := \033[36;48m\033[1m[INSTALL IDK ITEMS]\033[0m
TARGET_TYPE_COLOR := \t\033[35;48m\033[1m$(shell env printf "%5s" [$(TARGET_TYPE)])\033[0m
TARGET_OBJ_COLOR := \t\033[34;48m\033[1m$(shell env printf "%5s" [$(TARGET_OBJ)])\033[0m
TARGET_MIX_COLOR := $(TARGET_TYPE_COLOR)$(TARGET_OBJ_COLOR)
PLAT_ARM64_COLOR := $(MAKECMD_COLOR)\033[32;48m\033[1m$(shell env printf "%-8s" [ARM64])\033[0m$(TARGET_MIX_COLOR)
PLAT_LINUX_COLOR := $(MAKECMD_COLOR)\033[32;48m\033[1m$(shell env printf "%-8s" [LINUX])\033[0m$(TARGET_MIX_COLOR)
# debug 模式 gcc 以 g3 参数添加调试信息
ifeq ($(DEBUG), TRUE)
DEBUG_CFLAGS := -g3
endif
# 设置 gcc 编译优化等级参数
DEBUG_CFLAGS += -O2
LINUX_EXTFLAG += $(DEBUG_CFLAGS)
# 如果视编译警告为错误,开启此开关
ifeq ($(DISABLE_WARRING), TRUE)
LINUX_EXTFLAG += -w
endif
TARGET_BOX ?= $(TARGET_NAME)
# 设置编译参数
ifeq ($(TARGET_OBJ), DRV)
ifeq ($(PLAT_LINUX), TRUE)
LINUX_SYSTEM = $(shell $(CC) -print-file-name=include)
endif
ifeq ($(PLAT_ARM64), TRUE)
ARM64_SYSTEM = $(shell $(ARM64_CROSS_CC) -print-file-name=include)
endif
# linux 内核模块编译参数,提取自 x86 模块编译命令行
LINUX_DRV_CFLAGS := -nostdinc -isystem $(LINUX_SYSTEM) \
-D__KERNEL__ -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing \
-fno-common -fshort-wchar -Werror-implicit-function-declaration -Wno-format-security \
-std=gnu89 -fno-PIE -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -m64 -falign-jumps=1 \
-falign-loops=1 -mno-80387 -mno-fp-ret-in-387 -mpreferred-stack-boundary=3 -mskip-rax-setup \
-mtune=generic -mno-red-zone -mcmodel=kernel -funit-at-a-time -maccumulate-outgoing-args \
-DCONFIG_X86_X32_ABI -DCONFIG_AS_CFI=1 -DCONFIG_AS_CFI_SIGNAL_FRAME=1 -DCONFIG_AS_CFI_SECTIONS=1 \
-DCONFIG_AS_FXSAVEQ=1 -DCONFIG_AS_SSSE3=1 -DCONFIG_AS_CRC32=1 -DCONFIG_AS_AVX=1 -DCONFIG_AS_AVX2=1 \
-DCONFIG_AS_AVX512=1 -DCONFIG_AS_SHA1_NI=1 -DCONFIG_AS_SHA256_NI=1 -pipe -Wno-sign-compare \
-fno-asynchronous-unwind-tables -mindirect-branch=thunk-extern -mindirect-branch-register \
-DRETPOLINE -fno-delete-null-pointer-checks -Wno-frame-address -Wno-format-truncation \
-Wno-format-overflow -Wno-int-in-bool-context -Wno-attribute-alias \
--param=allow-store-data-races=0 -DCC_HAVE_ASM_GOTO -Wframe-larger-than=1024 \
-fstack-protector-strong -Wno-unused-but-set-variable -Wno-unused-const-variable \
-fno-omit-frame-pointer -fno-optimize-sibling-calls -fno-var-tracking-assignments \
-g -gdwarf-4 -pg -mfentry -DCC_USING_FENTRY -Wdeclaration-after-statement -Wno-pointer-sign \
-fno-strict-overflow -fno-merge-all-constants -fmerge-constants -fno-stack-check -fconserve-stack \
-Werror=implicit-int -Werror=strict-prototypes -Werror=date-time -Werror=incompatible-pointer-types \
-DMODULE -DKBUILD_BASENAME='"$(TARGET_NAME).mod"' -DKBUILD_MODNAME='"$(TARGET_NAME)"'
LINUX_EXTFLAG += $(DEBUG_CFLAGS) $(LINUX_DRV_CFLAGS) \
-I$(LINUX_KERNEL)/arch/x86/include \
-I$(LINUX_KERNEL)/arch/x86/include/generated/uapi \
-I$(LINUX_KERNEL)/arch/x86/include/generated \
-I$(LINUX_KERNEL)/include -I$(LINUX_KERNEL)/arch/x86/include/uapi \
-I$(LINUX_KERNEL)/include/uapi \
-I$(LINUX_KERNEL)/include/generated/uapi \
-include $(LINUX_KERNEL)/include/linux/kconfig.h
# ARM64 linux 内核模块编译参数,提取自 arm 模块编译命令行
ARM64_DRV_CFLAGS := -nostdinc -isystem $(ARM64_SYSTEM) \
-D__KERNEL__ -mlittle-endian -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing \
-fno-common -fshort-wchar -Werror-implicit-function-declaration -Wno-format-security -std=gnu89 \
-fno-PIE -mgeneral-regs-only -DCONFIG_AS_LSE=1 -fno-asynchronous-unwind-tables -mpc-relative-literal-loads \
-fno-delete-null-pointer-checks -Wno-frame-address -Wno-format-truncation -Wno-format-overflow \
-Wno-int-in-bool-context -O2 --param=allow-store-data-races=0 -DCC_HAVE_ASM_GOTO -Wframe-larger-than=2048 \
-fno-stack-protector -Wno-unused-but-set-variable -Wno-unused-const-variable -fno-omit-frame-pointer \
-fno-optimize-sibling-calls -fno-var-tracking-assignments -g -Wdeclaration-after-statement -Wno-pointer-sign \
-fno-strict-overflow -fno-merge-all-constants -fmerge-constants -fno-stack-check -fconserve-stack \
-Werror=implicit-int -Werror=strict-prototypes -Werror=date-time -Werror=incompatible-pointer-types \
-DKBUILD_BASENAME='"$(TARGET_NAME).mod"' -DKBUILD_MODNAME='"$(TARGET_NAME)"' -DMODULE -mcmodel=large
ARM64_EXTFLAG += $(DEBUG_CFLAGS) $(ARM64_DRV_CFLAGS) \
-I$(ARM64_KERNEL)/arch/arm64/include \
-I$(ARM64_KERNEL)/arch/arm64/include/generated/uapi \
-I$(ARM64_KERNEL)/arch/arm64/include/generated \
-I$(ARM64_KERNEL)/include \
-I$(ARM64_KERNEL)/arch/arm64/include/uapi \
-I$(ARM64_KERNEL)/include/uapi \
-I$(ARM64_KERNEL)/include/generated/uapi \
-include $(ARM64_KERNEL)/include/linux/kconfig.h
# x86 Linux 内核模块链接参数,提取自 x86 模块连接命令行
LINUX_DRV_LDFLAGS = -r -m elf_x86_64 -z max-page-size=0x200000 -T $(LINUX_KERNEL)/scripts/module-common.lds --build-id
# arm64 Linux 内核模块链接参数,提取自 arm64 模块连接命令行
ARM64_DRV_LDFLAGS = -EL -r -T $(ARM64_KERNEL)/scripts/module-common.lds --build-id
else ifeq ($(TARGET_OBJ), APP)
# 用户态模块编译参数
ARM64_EXTFLAG += $(DEBUG_CFLAGS) -I$(ARM64_SDK_INCLUDE)
# show all warring
#CFLAGS += -Wall
# 设置 ARM64 交叉编译链接目录
ARM64_DRV_LDFLAGS := --sysroot=${SDKTARGETSYSROOT}
else ifeq ($(TARGET_OBJ), SH)
else
$(error Unknown TARGET_OBJ Value Of [$(TARGET_OBJ)], It's Must Be Set Of APP/DRV)
endif
# 根据 debug/release 模式设置目录名称
ifeq ($(DEBUG), TRUE)
BUILD_DIR := debug
else
BUILD_DIR := release
endif
# 编译生成中间文件存放临时子目录
OUTPUT_DIR := ./.build/
# 编译中间文件存放相对路径
PLAT_ARM64_DIR := $(OUTPUT_DIR)$(TARGET_NAME)_$(TARGET_TYPE)_$(TARGET_OBJ)_arm64_$(BUILD_DIR)
PLAT_LINUX_DIR := $(OUTPUT_DIR)$(TARGET_NAME)_$(TARGET_TYPE)_$(TARGET_OBJ)_linux_$(BUILD_DIR)
# 设置安装路径
ifeq ($(TARGET_TYPE), SDK)
PLAT_LINUX_PUBLISH_DIR := $(INSTALL_ROOT)/sdk/$(CPU_LINUX_DIR)/
PLAT_LINUX_PUBLISH_DEBUG_DIR := $(INSTALL_ROOT)/sdk.debug/$(CPU_LINUX_DIR)/
else
ifeq ($(TARGET_BOX), )
PLAT_ARM64_PUBLISH_DIR := $(INSTALL_ROOT)/targets/$(CPU_ARM64_DIR)/$(TARGET_BOX)/
PLAT_ARM64_PUBLISH_DIR := $(INSTALL_ROOT)/targets/$(CPU_ARM64_DIR)/$(TARGET_BOX)/
ifeq ($(LINUX_USER_INS_DIR), )
PLAT_LINUX_PUBLISH_DIR := $(INSTALL_ROOT)/targets/$(CPU_LINUX_DIR)/$(TARGET_BOX)/
else
PLAT_LINUX_PUBLISH_DIR := $(INSTALL_ROOT)/$(LINUX_USER_INS_DIR)/$(CPU_LINUX_DIR)/$(TARGET_BOX)/
endif
PLAT_ARM64_PUBLISH_DEBUG_DIR := $(INSTALL_ROOT)/targets.debug/$(CPU_ARM64_DIR)/$(TARGET_BOX)/
ifeq ($(LINUX_USER_INS_DIR), )
PLAT_LINUX_PUBLISH_DEBUG_DIR := $(INSTALL_ROOT)/targets.debug/$(CPU_LINUX_DIR)/$(TARGET_BOX)/
else
PLAT_LINUX_PUBLISH_DEBUG_DIR := $(INSTALL_ROOT)/$(LINUX_USER_INS_DIR).debug/$(CPU_LINUX_DIR)/$(TARGET_BOX)/
endif
else
PLAT_ARM64_PUBLISH_DIR := $(INSTALL_ROOT)/targets/$(CPU_ARM64_DIR)/$(TARGET_BOX)-arm64/
ifeq ($(LINUX_USER_INS_DIR), )
PLAT_LINUX_PUBLISH_DIR := $(INSTALL_ROOT)/targets/$(CPU_LINUX_DIR)/$(TARGET_BOX)-linux/
else
PLAT_LINUX_PUBLISH_DIR := $(INSTALL_ROOT)/$(LINUX_USER_INS_DIR)/$(CPU_LINUX_DIR)/$(TARGET_BOX)-linux/
endif
PLAT_ARM64_PUBLISH_DEBUG_DIR := $(INSTALL_ROOT)/targets.debug/$(CPU_ARM64_DIR)/$(TARGET_BOX)-arm64/
ifeq ($(LINUX_USER_INS_DIR), )
PLAT_LINUX_PUBLISH_DEBUG_DIR := $(INSTALL_ROOT)/targets.debug/$(CPU_LINUX_DIR)/$(TARGET_BOX)-linux/
else
PLAT_LINUX_PUBLISH_DEBUG_DIR := $(INSTALL_ROOT)/$(LINUX_USER_INS_DIR).debug/$(CPU_LINUX_DIR)/$(TARGET_BOX)-linux/
endif
endif
endif
# 定义编译目标
BUILD_TARGET :=
ifneq ($(UNMAKE_PROCESS), TRUE)
# 定义最终生成目标文件名
PLAT_ARM64_TARGET := $(TARGET_PREFIX)$(TARGET_NAME)-arm64$(TARGET_EXT)
PLAT_LINUX_TARGET := $(TARGET_PREFIX)$(TARGET_NAME)-linux$(TARGET_EXT)
# ARM64 平台生成目标、编译参数、链接参数、依赖库
ifeq ($(PLAT_ARM64), TRUE)
BUILD_TARGET += $(PLAT_ARM64_TARGET)
PLAT_ARM64_CFLAGS += -DPLATFORM_ARM64 $(ARM64_EXTFLAG)
PLAT_ARM64_LDFLAGS += $(ARM64_DRV_LDFLAGS)
PLAT_ARM64_LIBFLAGS := $(ARM64_LIBFLAGS)
PLAT_ARM64_LIBS := $(ARM64_LIBS)
endif
# ARM64 平台生成目标、编译参数、链接参数、依赖库
ifeq ($(PLAT_LINUX), TRUE)
BUILD_TARGET += $(PLAT_LINUX_TARGET)
PLAT_LINUX_CFLAGS += -DPLATFORM_LINUX $(LINUX_EXTFLAG)
PLAT_LINUX_LDFLAGS += $(LINUX_DRV_LDFLAGS)
PLAT_LINUX_LIBFLAGS := $(LINUX_LIBFLAGS)
PLAT_LINUX_LIBS := $(LINUX_LIBS)
endif
# 根据源文件推算出生成目标的依赖项
ifeq ($(PLAT_ARM64), TRUE)
PLAT_ARM64_OBJS := $(PLAT_ARM64_SRCS:%.c=$(PLAT_ARM64_DIR)/%.o)
PLAT_ARM64_OBJS += $(PLAT_ARM64_AS_SRCS:%.s=$(PLAT_ARM64_DIR)/%.o)
PLAT_ARM64_DEPS := $(PLAT_ARM64_SRCS:%.c=$(PLAT_ARM64_DIR)/%.d)
else
PLAT_ARM64_OBJS :=
PLAT_ARM64_DEPS :=
endif
ifeq ($(PLAT_LINUX), TRUE)
PLAT_LINUX_OBJS := $(PLAT_LINUX_SRCS:%.c=$(PLAT_LINUX_DIR)/%.o)
PLAT_LINUX_OBJS += $(PLAT_LINUX_AS_SRCS:%.s=$(PLAT_LINUX_DIR)/%.o)
PLAT_LINUX_DEPS := $(PLAT_LINUX_SRCS:%.c=$(PLAT_LINUX_DIR)/%.d)
else
PLAT_LINUX_OBJS :=
PLAT_LINUX_DEPS :=
endif
# 构建系统最终需要生成的目标,包含 ARM64 和 Linux 平台
ALL_OBJS += $(PLAT_ARM64_OBJS) $(PLAT_LINUX_OBJS)
ALL_DEPS += $(PLAT_ARM64_DEPS) $(PLAT_LINUX_DEPS)
ifneq ($(MAKECMDGOALS), clean)
ifneq ($(MAKECMDGOALS), cleanall)
ifneq ($(strip $(ALL_DEPS)),)
-include $(ALL_DEPS)
endif
endif
endif
# ifneq $(UNMAKE_PROCESS)
endif
ifeq ($(UNMAKE_PROCESS), TRUE)
BUILD_TARGET :=
endif
ifneq ($(OPT), clean)
ifneq ($(OPT), install)
ifneq ($(MAKECMDGOALS), clean)
ifneq ($(MAKECMDGOALS), cleanall)
ifeq ($(MAKE_TARGET), )
$(info ---------------------------------------------------------------------)
$(info | CPU | TYPE | OBJECT | Command | Source/Target |)
$(info ---------------------------------------------------------------------)
else
$(info ------------------------------------------------------------------------------)
$(info | Name | CPU | TYPE | OBJECT | Command | Source |)
$(info ------------------------------------------------------------------------------)
endif
endif
endif
endif
endif
.PHONY : all
all: $(BUILD_TARGET)
# Build ARM64
ifeq ($(PLAT_ARM64), TRUE)
$(PLAT_ARM64_TARGET): $(PLAT_ARM64_OBJS)
# 创建构建需要的目录结构
-@test -d $(TARGET_OUT_DIR) || $(MKDIR) $(TARGET_OUT_DIR)
ifeq ($(TARGET_TYPE), LIB)
# 打包生成库文件
@$(ECHO_COLOR) "$(PLAT_ARM64_COLOR) AR $@"
$(MLOG)$(ARM64_CROSS_AR) $(PLAT_ARM64_LIBFLAGS) $(TARGET_OUT_DIR)$@ $+ $(PLAT_ARM64_LIBS)
else
# 链接生成可执行文件
@$(ECHO_COLOR) "$(PLAT_ARM64_COLOR) LD $@"
$(MLOG)$(ARM64_CROSS_LD) -o $(TARGET_OUT_DIR)$@ $(PLAT_ARM64_LDFLAGS) $+ $(PLAT_ARM64_LIBS)
ifeq ($(TARGET_STRIP), TRUE)
# 将目标的调试信息复制到另外一个新文件中
@$(ECHO_COLOR) "$(PLAT_ARM64_COLOR) OBJCOPY $@.debug"
$(MLOG)$(ARM64_CROSS_OBJCOPY) --only-keep-debug $(TARGET_OUT_DIR)$@ $(TARGET_OUT_DIR)/$@.debug
# 清理掉目标中的调试信息
@$(ECHO_COLOR) "$(PLAT_ARM64_COLOR) STRIP $@"
$(MLOG)$(ARM64_CROSS_STRIP) -g $(TARGET_OUT_DIR)$@
# 建立目标文件和调试信息文件的管理,供 gdb 自动识别
@$(ECHO_COLOR) "$(PLAT_ARM64_COLOR) OBJCOPY $@"
$(MLOG)$(ARM64_CROSS_OBJCOPY) --add-gnu-debuglink=$(TARGET_OUT_DIR)$@.debug $(TARGET_OUT_DIR)$@
endif
endif
# C 源文件编译规则
$(PLAT_ARM64_DIR)/%.o : %.c
-@test -d $(dir $@) || $(MKDIR) $(dir $@)
@$(ECHO_COLOR) "$(PLAT_ARM64_COLOR) CC $<"
$(MLOG)$(ARM64_CROSS_CC) -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" $(PLAT_ARM64_CFLAGS) -o $@ -c $<
# 汇编文件编译规则
$(PLAT_ARM64_DIR)/%.o : %.s
@$(ECHO_COLOR) "$(PLAT_ARM64_COLOR) CC $<"
$(MLOG)$(ARM64_CROSS_CC) -c $< -o $@
endif
# Build LINUX Board
ifeq ($(PLAT_LINUX), TRUE)
$(PLAT_LINUX_TARGET): $(PLAT_LINUX_OBJS)
-@test -d $(TARGET_OUT_DIR) || $(MKDIR) $(TARGET_OUT_DIR)
ifeq ($(TARGET_TYPE), LIB)
# 打包生成库文件
@$(ECHO_COLOR) "$(PLAT_LINUX_COLOR) AR $@"
$(MLOG)$(AR) $(PLAT_LINUX_LIBFLAGS) $(TARGET_OUT_DIR)$@ $+ $(PLAT_LINUX_LIBS)
else
# 链接生成可执行文件
@$(ECHO_COLOR) "$(PLAT_LINUX_COLOR) LD $@"
$(MLOG)$(LINUX_CROSS_LD) -o $(TARGET_OUT_DIR)$@ $(PLAT_LINUX_LDFLAGS) $+ $(PLAT_LINUX_LIBS)
ifeq ($(TARGET_STRIP), TRUE)
# 将目标的调试信息复制到另外一个新文件中
@$(ECHO_COLOR) "$(PLAT_LINUX_COLOR) OBJCOPY $@.debug"
$(MLOG)$(OBJCOPY) --only-keep-debug $(TARGET_OUT_DIR)$@ $(TARGET_OUT_DIR)$@.debug
# 清理掉目标中的调试信息
@$(ECHO_COLOR) "$(PLAT_LINUX_COLOR) STRIP $@"
$(MLOG)$(STRIP) -g $(TARGET_OUT_DIR)$@
# 建立目标文件和调试信息文件的管理,供 gdb 自动识别
@$(ECHO_COLOR) "$(PLAT_LINUX_COLOR) OBJCOPY $@"
$(MLOG)$(OBJCOPY) --add-gnu-debuglink=$(TARGET_OUT_DIR)$@.debug $(TARGET_OUT_DIR)$@
endif
endif
# C 源文件编译规则
$(PLAT_LINUX_DIR)/%.o : %.c
-@test -d $(dir $@) || $(MKDIR) $(dir $@)
@$(ECHO_COLOR) "$(PLAT_LINUX_COLOR) CC $<"
$(MLOG)$(CC) -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" $(PLAT_LINUX_CFLAGS) -o $@ -c $<
# 汇编文件编译规则
$(PLAT_LINUX_DIR)/%.o : %.s
@$(ECHO_COLOR) "$(PLAT_LINUX_COLOR) CC $<"
$(MLOG)$(CC) -c $< -o $@
endif
# 安装功能
.PHONY : install
install:
ifeq ($(INSTALL_ROOT), )
$(error usage: make install DIR=<path>)
endif
ifneq ($(PLAT_ARM64_DONT_INSTALL), TRUE)
# ARM64 targets install
ifeq ($(PLAT_ARM64), TRUE)
-@test -d $(PLAT_ARM64_PUBLISH_DIR) || $(MKDIR) $(PLAT_ARM64_PUBLISH_DIR)
-@test -d $(PLAT_ARM64_PUBLISH_DEBUG_DIR) || $(MKDIR) $(PLAT_ARM64_PUBLISH_DEBUG_DIR)
ifneq ($(UNMAKE_PROCESS), TRUE)
@$(ECHO_COLOR) "$(INSTALL_COLOR) $(PLAT_ARM64_COLOR) $(TARGET_OUT_DIR)$(PLAT_ARM64_TARGET) $(PLAT_ARM64_TARGET).debug $(INSTALL_PATH_COLOR)"
@$(CP) $(TARGET_OUT_DIR)/$(PLAT_ARM64_TARGET) $(PLAT_ARM64_PUBLISH_DIR)
@$(CP) $(TARGET_OUT_DIR)/$(PLAT_ARM64_TARGET).debug $(PLAT_ARM64_PUBLISH_DEBUG_DIR)
endif
ifneq ($(PLAT_ARM64_USER_INS_ITEMS), )
@$(ECHO_COLOR) "$(INSTALL_USER_ITEM_COLOR) $(PLAT_ARM64_COLOR) $(notdir $(PLAT_ARM64_USER_INS_ITEMS)) $(INSTALL_PATH_COLOR)"
@$(CP) $(PLAT_ARM64_USER_INS_ITEMS) $(PLAT_ARM64_PUBLISH_DIR)
endif
endif
endif
ifneq ($(PLAT_LINUX_DONT_INSTALL), TRUE)
# LINUX board targets install
ifeq ($(PLAT_LINUX), TRUE)
-@test -d $(PLAT_LINUX_PUBLISH_DIR) || $(MKDIR) $(PLAT_LINUX_PUBLISH_DIR)
-@test -d $(PLAT_LINUX_PUBLISH_DEBUG_DIR) || $(MKDIR) $(PLAT_LINUX_PUBLISH_DEBUG_DIR)
ifneq ($(UNMAKE_PROCESS), TRUE)
@$(ECHO_COLOR) "$(INSTALL_COLOR) $(PLAT_LINUX_COLOR) $(TARGET_OUT_DIR)$(PLAT_LINUX_TARGET) $(PLAT_LINUX_TARGET).debug $(INSTALL_PATH_COLOR)"
@$(CP) $(TARGET_OUT_DIR)/$(PLAT_LINUX_TARGET) $(PLAT_LINUX_PUBLISH_DIR)
@$(CP) $(TARGET_OUT_DIR)/$(PLAT_LINUX_TARGET).debug $(PLAT_LINUX_PUBLISH_DEBUG_DIR)
endif
ifneq ($(PLAT_LINUX_USER_INS_ITEMS), )
@$(ECHO_COLOR) "$(INSTALL_USER_ITEM_COLOR) $(PLAT_LINUX_COLOR) $(notdir $(PLAT_LINUX_USER_INS_ITEMS)) $(INSTALL_PATH_COLOR)"
@$(CP) $(PLAT_LINUX_USER_INS_ITEMS) $(PLAT_LINUX_PUBLISH_DIR)
endif
ifneq ($(USER_MULTILE_PLATFORM_HEAD_ITEMS), )
@$(ECHO_COLOR) "$(INSTALL_MULT_PLAT_COLOR) $(PLAT_LINUX_COLOR) $(notdir $(USER_MULTILE_PLATFORM_HEAD_ITEMS)) $(INSTALL_PATH_COLOR)"
@$(CP) $(USER_MULTILE_PLATFORM_HEAD_ITEMS) $(SDK_HEADFILE_PUBLIST_DIR)
endif
endif
endif
# end of ($(DONT_INSTALL), TRUE)
# 清理功能
.PHONY : cleanall
cleanall:
@$(ECHO_COLOR) "$(CLEAN_COLOR) $(PATH_COLOR) $(OUTPUT_DIR) $(BUILD_TARGET) $(USER_CLEAN_ITEMS) $(notdir $(DEPEND_LIB)) $(TARGET_OUT_DIR) *.debug"
@-$(RM) $(TARGET_OUT_DIR) $(OUTPUT_DIR) $(BUILD_TARGET) $(notdir $(DEPEND_LIB)) $(USER_CLEAN_ITEMS) *.debug
.PHONY : clean
clean:
@$(ECHO_COLOR) "$(CLEAN_COLOR) $(PATH_COLOR) $(ALL_OBJS) $(BUILD_TARGET) $(ALL_DEPS) $(USER_CLEAN_ITEMS) $(notdir $(DEPEND_LIB)) $(TARGET_OUT_DIR) *.debug"
@-$(RM) $(TARGET_OUT_DIR) $(ALL_OBJS) $(BUILD_TARGET) $(ALL_DEPS) $(USER_CLEAN_ITEMS) $(notdir $(DEPEND_LIB)) *.debug

84
Common/commuapinl.h Executable file
View File

@ -0,0 +1,84 @@
#ifndef _UAPI__LINUX_COMMNETLINK_H
#define _UAPI__LINUX_COMMNETLINK_H
#include <linux/types.h>
#include <linux/netlink.h>
//#include <uapi/linux/netlink.h>
/* netlink proto*/
#define NETLINK_COMMMAX_MIN_FAMILY 21
#define NETLINK_COMMCFG 22
#define NETLINK_PDELIVERY 23
#define NETLINK_COMMMAX_MAX_FAMILY 24
/*NETLINK_PDELIVERY groups,keep userspace and kernel the same */
#define PDNLGRP_ALLRAW 0x01 /*上送DPI的报文*/
#define PDNLGRP_PKT1 0X02 /*保留*/
#define PDNLGRP_PKT2 0X04 /*保留*/
#define PDNLGRP_PKT3 0X08 /*保留*/
enum pdeliverynetlink_groups {
PDELIVERY_NLGRP_NONE=0,/*代表单播*/
#define PDELIVERY_NLGRP_NONE PDELIVERY_NLGRP_NONE
PDELIVERY_NLGRP_ALLRAW=1,
#define PDELIVERY_NLGRP_ALLRAW PDELIVERY_NLGRP_ALLRAW
PDELIVERY_NLGRP_PKT1,
#define PDELIVERY_NLGRP_PKT1 PDELIVERY_NLGRP_PKT1
PDELIVERY_NLGRP_PKT2,
#define PDELIVERY_NLGRP_PKT2 PDELIVERY_NLGRP_PKT2
PDELIVERY_NLGRP_PKT3,
#define PDELIVERY_NLGRP_PKT3 PDELIVERY_NLGRP_PKT3
__PDELIVERY_NLGRP_MAX,
};
#define PDELIVERY_NLGRP_MAX (__PDELIVERY_NLGRP_MAX - 1)
#define COMMLIBNLGRP_MAX 16 /*支持的最大组数量*/
/*netlink pdelivery msg type*/
enum pdelivmsgtype{
PDNL_BASE = 0x10,/*netlink 保留控制消息*/
PDNLGRP_REQUEST, /*用户态发送给内核态的请求消息用于查看pdiliv模块本身的状态及配置与业务无关*/
NLMSG_RECV_RAW_PKT,/*上送DPI的报文消息*/
NLMGS_PDELIVERY_MAX_TYPE,
};
/* PDELIVERY msg attr */
enum pdelivattr{
PDELIVERY_UNSPEC_ATTR,
PDELIVERY_PKT_ATTR,
PDELIVERY_DUMP_PKTNUM_ATTR,
__PDELIVERY_MAX_ATTR
};
#define PDELIVERY_MAX_MAX (__PDELIVERY_MAX_ATTR - 1)
/**********************************************************************/
/*becareful end :keep userspace and kernel the same . */
/**********************************************************************/
/****
* common cfg messages type.
****/
/* Types of messages */
enum {
//COMMMSGNL_BASE = NLMSG_MIN_TYPE,/*netlink 保留控制消息*/
COMMMSGNL_BASE = 0x10,/*netlink 保留控制消息*/
COMMNMSG_POLICYCONF,
COMMNMSG_USER,
COMMCFG_NLMSG_MAX_TYPE,
};
#endif /* _UAPI__LINUX_COMMNETLINK_H */

11
Common/compile.h Executable file
View File

@ -0,0 +1,11 @@
/* This file is auto generated,for vBRAS version info */
/* Used readelf to get this information form driver of application */
/* "readelf --debug-dump=macro <filename>" */
#define vBRAS_COMPILE_DATE "2019-05-30"
#define vBRAS_COMPILE_TIME "14:14:26"
#define vBRAS_COMPILE_MAJOR "20190530"
#define vBRAS_COMPILE_SUB "141426"
#define vBRAS_COMPILE_BY "hx"
#define vBRAS_COMPILE_HOST "esgwdev01"
#define vBRAS_GIT_TAGS ""
#define vBRAS_GIT_VERS ""

29
Common/policyconf.h Executable file
View File

@ -0,0 +1,29 @@
#ifndef _UAPI_LINUX_POLICYCONF_H_
#define _UAPI_LINUX_POLICYCONF_H_
#include <linux/types.h>
#include <linux/netlink.h>
struct policyconfmsg {
__u8 policy_id;
};
/* commoncfg netlink msg attr */
enum {
POLICYCONFA_UNSPEC,/* used in NLMSG_POLICYCONF*/
POLICYCONFA_SRCIP,/* used in NLMSG_POLICYCONF*/
POLICYCONFA_DSTIP,/* used in NLMSG_POLICYCONF*/
POLICYCONFA_SRCPORT,/* used in NLMSG_POLICYCONF*/
POLICYCONFA_DSTPORT,/* used in NLMSG_POLICYCONF*/
POLICYCONFA_PROTO, /* used in NLMSG_POLICYCONF*/
POLICYCONFA_ACTION,/* used in NLMSG_POLICYCONF*/
__POLICYCONFA_MAX
};
#define POLICYCONFA_MAX (__POLICYCONFA_MAX - 1)
#define POLICYCONFA_ALL -1
#endif /* _UAPI_LINUX_POLICYCONF_H_ */

86
Makefile Executable file
View File

@ -0,0 +1,86 @@
SHELL := /bin/sh
OUTPUT_MAKE_DEBUG = FALSE
ifeq ($(OUTPUT_MAKE_DEBUG), TRUE)
DIS_BUILD_WARRING ?= FALSE
EN_MAKE_MSG ?= TRUE
else
DIS_BUILD_WARRING ?= TRUE
EN_MAKE_MSG ?= FALSE
endif
ifeq ($(EN_MAKE_MSG), TRUE)
MAKE_FLAGS :=
else
MAKE_FLAGS := -s
endif
ifneq ($(OPT), clean)
ifneq ($(OPT), install)
MAKE_FLAGS += -j$(shell cat /proc/cpuinfo | grep processor | wc -l)
endif
endif
.PHONY : demo conntrack netlink
all: demo conntrack netlink
ifeq ($(OPT), install)
#$(shell `find ../release -name "*.zip" -delete`)
endif
ifeq ($(OPT), clean)
#
endif
$(shell chmod +x ./build_env.sh)
$(shell test -e ./Common/compile.h || ./build_env.sh)
demo:
ifeq ($(OPT), clean)
@make $(MAKE_FLAGS) -C Product/build -f module.demo.Makefile cleanall MAKE_TARGET=demo
@make $(MAKE_FLAGS) -C Product/build -f user.demo.Makefile cleanall MAKE_TARGET=demo
else ifeq ($(OPT), install)
@make $(MAKE_FLAGS) -C Product/build -f module.demo.Makefile install DIR=$(DIR) MAKE_TARGET=demo
@make $(MAKE_FLAGS) -C Product/build -f user.demo.Makefile install DIR=$(DIR) MAKE_TARGET=demo
else
@make all $(MAKE_FLAGS) -C Product/build -f module.demo.Makefile DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=demo
@make all $(MAKE_FLAGS) -C Product/build -f user.demo.Makefile DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=demo
endif
conntrack:
ifeq ($(OPT), clean)
@make $(MAKE_FLAGS) -C Platform/build -f module.conntrack.api.Makefile cleanall MAKE_TARGET=conntrack
@make $(MAKE_FLAGS) -C Platform/build -f module.conntrack.demoA.Makefile cleanall MAKE_TARGET=demoA
@make $(MAKE_FLAGS) -C Platform/build -f module.conntrack.test.Makefile cleanall MAKE_TARGET=test
else ifeq ($(OPT), install)
@make $(MAKE_FLAGS) -C Platform/build -f module.conntrack.api.Makefile install DIR=$(DIR) MAKE_TARGET=conntrack
@make $(MAKE_FLAGS) -C Platform/build -f module.conntrack.demoA.Makefile install DIR=$(DIR) MAKE_TARGET=demoA
@make $(MAKE_FLAGS) -C Platform/build -f module.conntrack.test.Makefile install DIR=$(DIR) MAKE_TARGET=conntrack
else
@make all $(MAKE_FLAGS) -C Platform/build -f module.conntrack.api.Makefile DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=conntrack
@make all $(MAKE_FLAGS) -C Platform/build -f module.conntrack.demoA.Makefile DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=demoA
@make all $(MAKE_FLAGS) -C Platform/build -f module.conntrack.test.Makefile DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=test
endif
netlink:
ifeq ($(OPT), clean)
@make $(MAKE_FLAGS) -C Platform/build -f module.cfgrcv.Makefile cleanall MAKE_TARGET=cfgrcv
@make $(MAKE_FLAGS) -C Platform/build -f module.pdelivery.Makefile cleanall MAKE_TARGET=pdeliv
@make $(MAKE_FLAGS) -C Platform/build -f user.cfg2kernel.Makefile cleanall MAKE_TARGET=cfg2kernel
@make $(MAKE_FLAGS) -C Platform/build -f user.pdeliv_u.Makefile cleanall MAKE_TARGET=pdeliv_u
@make $(MAKE_FLAGS) -C Platform/build -f user.netlink_uapi.Makefile cleanall MAKE_TARGET=netlink_uapi
else ifeq ($(OPT), install)
@make $(MAKE_FLAGS) -C Platform/build -f module.cfgrcv.Makefile install DIR=$(DIR) MAKE_TARGET=cfgrcv
@make $(MAKE_FLAGS) -C Platform/build -f module.pdelivery.Makefile install DIR=$(DIR) MAKE_TARGET=pdeliv
@make $(MAKE_FLAGS) -C Platform/build -f user.netlink_uapi.Makefile install DIR=$(DIR) MAKE_TARGET=netlink_uapi
@make $(MAKE_FLAGS) -C Platform/build -f user.cfg2kernel.Makefile install DIR=$(DIR) MAKE_TARGET=cfg2kernel
@make $(MAKE_FLAGS) -C Platform/build -f user.pdeliv_u.Makefile install DIR=$(DIR) MAKE_TARGET=pdeliv_u
else
@make all $(MAKE_FLAGS) -C Platform/build -f module.cfgrcv.Makefile DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=cfgrcv
@make all $(MAKE_FLAGS) -C Platform/build -f module.pdelivery.Makefile DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=pdeliv
@make all $(MAKE_FLAGS) -C Platform/build -f user.netlink_uapi.Makefile DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=netlink_uapi
@make all $(MAKE_FLAGS) -C Platform/build -f user.cfg2kernel.Makefile DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=cfg2kernel
@make all $(MAKE_FLAGS) -C Platform/build -f user.pdeliv_u.Makefile DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=pdeliv_u
endif

0
Platform/README Executable file
View File

0
Platform/build/README Executable file
View File

View File

@ -0,0 +1,52 @@
# target name, the target name must have the same name of c source file
TARGET_NAME=cfgrcv
# target
# for linux module driver: KO
# for application: EXE
# for dynamic library: DLL
TARGET_TYPE = KO
# target object
# for application: APP
# for device driver: DRV
TARGET_OBJ = DRV
# custom install dir
TARGET_BOX =
#debug mode or release mode
DEBUG = TRUE
PLAT_LINUX ?= TRUE
PLAT_ARM64 ?= TRUE
VPATH = ../modules
# source code
# set the source file, don't used .o because of ...
COMMON_SRCS = ./cfgrcv/cfgrcv_kinit.c \
./netlink_api/libnetlink_k.c \
./cfgrcv/cfgrcv_mod.c
# MRS Board Source Files
PLAT_LINUX_SRCS = $(COMMON_SRCS)
PLAT_ARM64_SRCS = $(COMMON_SRCS)
# gcc CFLAGS
PLAT_ARM64_CFLAGS := -I../modules/netlink_api/ -I../../Common
PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS)
# this line must be at below of thus, because of...
include ../../Common/common.Makefile
ifeq ($(MAKECMDGOALS), )
$(shell find ./ -name "$(TARGET)-*.ko" -delete)
else
ifeq ($(MAKECMDGOALS), all)
$(shell find ./ -name "$(TARGET)-*.ko" -delete)
endif
endif

View File

@ -0,0 +1,49 @@
# target name, the target name must have the same name of c source file
TARGET_NAME=conntrack_api
# target
# for linux module driver: KO
# for application: EXE
# for dynamic library: DLL
TARGET_TYPE = KO
# target object
# for application: APP
# for device driver: DRV
TARGET_OBJ = DRV
# custom install dir
TARGET_BOX =
#debug mode or release mode
DEBUG = TRUE
PLAT_LINUX ?= TRUE
PLAT_ARM64 ?= TRUE
VPATH = ../modules/conntrack_api/api
# source code
# set the source file, don't used .o because of ...
COMMON_SRCS = conntrack_api.c conntrack_mod.c
# MRS Board Source Files
PLAT_LINUX_SRCS = $(COMMON_SRCS)
PLAT_ARM64_SRCS = $(COMMON_SRCS)
# gcc CFLAGS
# this line must be at below of thus, because of...
include ../../Common/common.Makefile
ifeq ($(MAKECMDGOALS), )
$(shell find ./ -name "$(TARGET)-*.ko" -delete)
else
ifeq ($(MAKECMDGOALS), all)
$(shell find ./ -name "$(TARGET)-*.ko" -delete)
endif
endif

View File

@ -0,0 +1,49 @@
# target name, the target name must have the same name of c source file
TARGET_NAME=demoA
# target
# for linux module driver: KO
# for application: EXE
# for dynamic library: DLL
TARGET_TYPE = KO
# target object
# for application: APP
# for device driver: DRV
TARGET_OBJ = DRV
# custom install dir
TARGET_BOX =
#debug mode or release mode
DEBUG = TRUE
PLAT_LINUX ?= TRUE
PLAT_ARM64 ?= TRUE
VPATH = ../modules/conntrack_api/demoA
# source code
# set the source file, don't used .o because of ...
COMMON_SRCS = demoA.c demoA_mod.c
# MRS Board Source Files
PLAT_LINUX_SRCS = $(COMMON_SRCS)
PLAT_ARM64_SRCS = $(COMMON_SRCS)
# gcc CFLAGS
# this line must be at below of thus, because of...
include ../../Common/common.Makefile
ifeq ($(MAKECMDGOALS), )
$(shell find ./ -name "$(TARGET)-*.ko" -delete)
else
ifeq ($(MAKECMDGOALS), all)
$(shell find ./ -name "$(TARGET)-*.ko" -delete)
endif
endif

View File

@ -0,0 +1,49 @@
# target name, the target name must have the same name of c source file
TARGET_NAME=test
# target
# for linux module driver: KO
# for application: EXE
# for dynamic library: DLL
TARGET_TYPE = KO
# target object
# for application: APP
# for device driver: DRV
TARGET_OBJ = DRV
# custom install dir
TARGET_BOX =
#debug mode or release mode
DEBUG = TRUE
PLAT_LINUX ?= TRUE
PLAT_ARM64 ?= TRUE
VPATH = ../modules/conntrack_api/test
# source code
# set the source file, don't used .o because of ...
COMMON_SRCS = test.c test_mod.c
# MRS Board Source Files
PLAT_LINUX_SRCS = $(COMMON_SRCS)
PLAT_ARM64_SRCS = $(COMMON_SRCS)
# gcc CFLAGS
# this line must be at below of thus, because of...
include ../../Common/common.Makefile
ifeq ($(MAKECMDGOALS), )
$(shell find ./ -name "$(TARGET)-*.ko" -delete)
else
ifeq ($(MAKECMDGOALS), all)
$(shell find ./ -name "$(TARGET)-*.ko" -delete)
endif
endif

View File

@ -0,0 +1,52 @@
# target name, the target name must have the same name of c source file
TARGET_NAME=pdeliv
# target
# for linux module driver: KO
# for application: EXE
# for dynamic library: DLL
TARGET_TYPE = KO
# target object
# for application: APP
# for device driver: DRV
TARGET_OBJ = DRV
# custom install dir
TARGET_BOX =
#debug mode or release mode
DEBUG = TRUE
PLAT_LINUX ?= TRUE
PLAT_ARM64 ?= TRUE
VPATH = ../modules
# source code
# set the source file, don't used .o because of ...
COMMON_SRCS = ./pdelivery/pdeliverynl_kinit.c \
./netlink_api/libnetlink_k.c \
./pdelivery/pdeliverynl_mod.c
# MRS Board Source Files
PLAT_LINUX_SRCS = $(COMMON_SRCS)
PLAT_ARM64_SRCS = $(COMMON_SRCS)
# gcc CFLAGS
PLAT_ARM64_CFLAGS := -I../modules/netlink_api/ -I../../Common
PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS)
# this line must be at below of thus, because of...
include ../../Common/common.Makefile
ifeq ($(MAKECMDGOALS), )
$(shell find ./ -name "$(TARGET)-*.ko" -delete)
else
ifeq ($(MAKECMDGOALS), all)
$(shell find ./ -name "$(TARGET)-*.ko" -delete)
endif
endif

View File

@ -0,0 +1,78 @@
# target name, the target name must have the same name of c source file
TARGET_NAME=cfgchannel_sample
# target
# for linux module driver: KO
# for application: EXE
# for dynamic library: DLL
TARGET_TYPE = EXE
# target object
# for application: APP
# for device driver: DRV
TARGET_OBJ = APP
# custom install dir
TARGET_BOX =
#debug mode or release mode
DEBUG = TRUE
PLAT_LINUX ?= TRUE
PLAT_ARM64 ?= TRUE
VPATH = ../user/cfg2kernel
# source code
# set the source file, don't used .o because of ...
COMMON_SRCS = cfgchannel_sample.c
# MRS Board Source Files
PLAT_LINUX_SRCS = $(COMMON_SRCS)
PLAT_ARM64_SRCS = $(COMMON_SRCS)
# gcc CFLAGS
PLAT_ARM64_CFLAGS := -I../../Common -I../user/netlink_uapi
PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS)
PLAT_ARM64_LDFLAGS :=
PLAT_LINUX_LDFLAGS := $(PLAT_ARM64_LDFLAGS)
ARM64_LIBS := libnetlinku-arm64.so
LINUX_LIBS := libnetlinku-linux.so
ifeq ($(PLAT_ARM64), TRUE)
DEPEND_LIB += ./debug/libnetlinku-arm64.so
USER_CLEAN_ITEMS += ./libnetlinku-arm64.so
endif
ifeq ($(PLAT_LINUX), TRUE)
DEPEND_LIB += ./debug/libnetlinku-linux.so
USER_CLEAN_ITEMS += ./libnetlinku-linux.so
endif
# this line must be at below of thus, because of...
include ../../Common/common.Makefile
ifneq ($(MAKECMDGOALS), clean)
ifneq ($(MAKECMDGOALS), cleanall)
ifneq ($(notdir $(DEPEND_LIB)), $(wildcard $(DEPEND_LIB)))
$(shell $(CP) $(DEPEND_LIB) ./)
endif
endif
endif
ifeq ($(MAKECMDGOALS), )
$(shell find ./ -name "$(TARGET)-*.ko" -delete)
else
ifeq ($(MAKECMDGOALS), all)
$(shell find ./ -name "$(TARGET)-*.ko" -delete)
endif
endif

View File

@ -0,0 +1,54 @@
# target name, the target name must have the same name of c source file
TARGET_NAME=libnetlinku
# target
# for linux module driver: KO
# for application: EXE
# for dynamic library: DLL
TARGET_TYPE = DLL
# target object
# for application: APP
# for device driver: DRV
TARGET_OBJ = APP
# custom install dir
TARGET_BOX =
#debug mode or release mode
DEBUG = TRUE
PLAT_LINUX ?= TRUE
PLAT_ARM64 ?= TRUE
VPATH = ../user/netlink_uapi
# source code
# set the source file, don't used .o because of ...
COMMON_SRCS = libnetlinku.c
# MRS Board Source Files
PLAT_LINUX_SRCS = $(COMMON_SRCS)
PLAT_ARM64_SRCS = $(COMMON_SRCS)
# gcc CFLAGS
PLAT_ARM64_CFLAGS := -I../../Common -fPIC
PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS)
PLAT_ARM64_LDFLAGS := -fPIC -shared
PLAT_LINUX_LDFLAGS := $(PLAT_ARM64_LDFLAGS)
# this line must be at below of thus, because of...
include ../../Common/common.Makefile
ifeq ($(MAKECMDGOALS), )
$(shell find ./ -name "$(TARGET)-*.ko" -delete)
else
ifeq ($(MAKECMDGOALS), all)
$(shell find ./ -name "$(TARGET)-*.ko" -delete)
endif
endif

View File

@ -0,0 +1,78 @@
# target name, the target name must have the same name of c source file
TARGET_NAME=pdeliv
# target
# for linux module driver: KO
# for application: EXE
# for dynamic library: DLL
TARGET_TYPE = EXE
# target object
# for application: APP
# for device driver: DRV
TARGET_OBJ = APP
# custom install dir
TARGET_BOX =
#debug mode or release mode
DEBUG = TRUE
PLAT_LINUX ?= TRUE
PLAT_ARM64 ?= TRUE
VPATH = ../user/pdeliv_u
# source code
# set the source file, don't used .o because of ...
COMMON_SRCS = pdelivery_main.c
# MRS Board Source Files
PLAT_LINUX_SRCS = $(COMMON_SRCS)
PLAT_ARM64_SRCS = $(COMMON_SRCS)
# gcc CFLAGS
PLAT_ARM64_CFLAGS := -I../../Common -I../user/netlink_uapi
PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS)
PLAT_ARM64_LDFLAGS :=
PLAT_LINUX_LDFLAGS := $(PLAT_ARM64_LDFLAGS)
ARM64_LIBS := libnetlinku-arm64.so
LINUX_LIBS := libnetlinku-linux.so
ifeq ($(PLAT_ARM64), TRUE)
DEPEND_LIB += ./debug/libnetlinku-arm64.so
USER_CLEAN_ITEMS += ./libnetlinku-arm64.so
endif
ifeq ($(PLAT_LINUX), TRUE)
DEPEND_LIB += ./debug/libnetlinku-linux.so
USER_CLEAN_ITEMS += ./libnetlinku-linux.so
endif
# this line must be at below of thus, because of...
include ../../Common/common.Makefile
ifneq ($(MAKECMDGOALS), clean)
ifneq ($(MAKECMDGOALS), cleanall)
ifneq ($(notdir $(DEPEND_LIB)), $(wildcard $(DEPEND_LIB)))
$(shell $(CP) $(DEPEND_LIB) ./)
endif
endif
endif
ifeq ($(MAKECMDGOALS), )
$(shell find ./ -name "$(TARGET)-*.ko" -delete)
else
ifeq ($(MAKECMDGOALS), all)
$(shell find ./ -name "$(TARGET)-*.ko" -delete)
endif
endif

0
Platform/modules/README Executable file
View File

View File

@ -0,0 +1,17 @@
#Makefile 2.6
export HUACHENG_LINUX_KERNEL
DEFINES :=
INCLUDE := -I../lib -I../common
LIB :=
obj-m :=cfgrcv.o
cfgrcv-objs := cfgrcv_kinit.o
KERNEL :=$(HUACHENG_LINUX_KERNEL)
PWD :=$(shell pwd)
modules :
echo $HUACHENG_LINUX_KERNEL
$(MAKE) $(INCLUDE) -C $(KERNEL) M=$(PWD) modules
.PHONEY:clean
clean :
rm -rf *.o *.mod.c *.ko *.symvers *.order *.makers .*.cmd *.cmd .tmp_versions .cache.mk

View File

@ -0,0 +1,121 @@
#include <linux/module.h>
#include <linux/netlink.h>
#include <linux/netfilter.h>
#include <linux/ip.h>
#include <uapi/linux/netfilter_ipv4.h>
#include <uapi/linux/ip.h>
#include <net/netlink.h>
#include <net/net_namespace.h>
#include "../netlink_api/libnetlink_k.h"
#include "../../../Common/commuapinl.h"
struct netlinkk_cfg g_upmnlcfg;
#if 0
int nl_upm_data_ready(struct sk_buff *skb, struct nlmsghdr *nlh)
{
void *payload;
struct sk_buff *out_skb;
void *out_payload;
struct nlmsghdr *out_nlh;
int payload_len; // with padding, but ok for echo
printk(KERN_DEBUG "nl_upm_data_ready() begin.\n");
switch(nlh->nlmsg_type)
{
case COMMNMSG_POLICYCONF:/**/
payload = nlmsg_data(nlh);
payload_len = nlmsg_len(nlh);
printk(KERN_INFO "Recievid: %s, From: %d\n", (char *)payload, nlh->nlmsg_pid);
out_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); //分配足以存放默认大小的sk_buff
if (!out_skb) goto failure;
out_nlh = nlmsg_put(out_skb, 0, 0, COMMNMSG_POLICYCONF, payload_len, 0); //填充协议头数据
if (!out_nlh) goto failure;
//out_payload = nlmsg_data(out_nlh);
//nla_put_u32(out_skb,PDELIVERY_DUMP_PKTNUM_ATTR,g_nlcfg->pkt_delev_num);
commnl_unicast(g_upmnlcfg.sk, out_skb, nlh->nlmsg_pid);
break;
default:
printk(KERN_INFO "libnetlink Unknow msgtype recieved!\n");
return 0;
}
printk(KERN_DEBUG "nl_upm_data_ready() end.\n");
return 0;
failure:
printk(KERN_INFO " failed in fun dataready!\n");
return 0;
}
#endif
int upm_rcv_policycfg(struct sk_buff *skb, struct nlmsghdr *nlh)
{
printk(KERN_INFO "upm_rcv_policycfg, From pid: %d\n", nlh->nlmsg_pid);
//printk_mem((unsigned char*)nlh,nlh->nlmsg_len);
return 0;
}
int __init upm_init(void)
{
int ret = -1;
printk(KERN_CRIT "nl_upm initialed ok!\n");
/*init for pdelivery module*/
g_upmnlcfg.groups = 0;
g_upmnlcfg.subscriptions = NETLINK_COMMCFG;/*创建配置处理通道*/
ret = libnetlinkk_init_byproto(&g_upmnlcfg);
if(ret < 0)
{
printk (KERN_CRIT "upm_init netlink init fail!.\n");
return ret;
}
/*do msg process register*/
cfg_msgtype_register(COMMNMSG_POLICYCONF,upm_rcv_policycfg,NULL,NULL);
return 0;
}
void __exit upm_exit(void)
{
printk(KERN_CRIT "nl_upm existing...\n");
libnetlinkk_exit(&g_upmnlcfg);
return;
}
module_init(upm_init);
module_exit(upm_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("a simple example for upm(user policy manage) netlink protocal family");
MODULE_AUTHOR("RSLjdkt");

View File

@ -0,0 +1,28 @@
#include <linux/module.h>
#include <linux/vermagic.h>
#include <linux/compiler.h>
MODULE_INFO(vermagic, VERMAGIC_STRING);
MODULE_INFO(name, KBUILD_MODNAME);
__visible struct module __this_module
__attribute__((section(".gnu.linkonce.this_module"))) = {
.name = KBUILD_MODNAME,
.init = init_module,
#ifdef CONFIG_MODULE_UNLOAD
.exit = cleanup_module,
#endif
.arch = MODULE_ARCH_INIT,
};
#ifdef RETPOLINE
MODULE_INFO(retpoline, "Y");
#endif
static const char __module_depends[]
__used
__attribute__((section(".modinfo"))) =
"depends=";
MODULE_INFO(srcversion, "1A92196CEBDAFD5950E8BDF");

View File

@ -0,0 +1,13 @@
ifneq ($(KERNELRELEASE),)
obj-m += conntrack_api.o
else
PWD := $(shell pwd)
KVER := $(shell uname -r)
KDIR := $(HUACHENG_LINUX_KERNEL)
default:
$(MAKE) -C $(KDIR) M=$(PWD) modules
all:
make -C $(KDIR) M=$(PWD) modules
clean:
rm -rf *.o *.mod.c *.ko *.symvers *.order *.makers .*.cmd *.cmd .tmp_versions .cache.mk
endif

View File

@ -0,0 +1,568 @@
#include "conntrack_api.h"
/************************************************************
* :ct扩展字段中16
* :sk_buff, value(), type()
* :
* :
************************************************************/
int cmhi_set_conntrack_u16(const struct sk_buff *skb, uint16_t value, cmhi_ext_type type)
{
enum ip_conntrack_info ctinfo = {0};
struct nf_conn *ct = NULL;
if(!skb){
printk(KERN_ERR"[CT_API]set_conntrack_u16: skb is null\n");
return CMHI_EXT_ERR;
}
ct = nf_ct_get(skb, &ctinfo);
if(!ct){
printk(KERN_ERR"[CT_API]set-conntrak_u16: ct is not existed or ct is untracked\n");
return CMHI_EXT_ERR;
}
switch (type)
{
case USER_VERSION:
ct->cmhi.user_version = value;
break;
case APP_ID:
ct->cmhi.app_id = value;
break;
default: {
printk(KERN_INFO"[CT_API]set-conntrak_u16: value is not u16\n");
return CMHI_EXT_ERR;
}
}
return CMHI_EXT_OK;
}
/***********************************************************
* :ct扩展字段中32
* :sk_buff, value(), type()
* :
* :
***********************************************************/
int cmhi_set_conntrack_u32(const struct sk_buff *skb, uint32_t value, cmhi_ext_type type)
{
enum ip_conntrack_info ctinfo = {0};
struct nf_conn *ct = NULL;
if(!skb){
printk(KERN_ERR"[CT_API]set-conntrack_u32: skb is null\n");
return CMHI_EXT_ERR;
}
ct = nf_ct_get(skb, &ctinfo);
if(!ct){
printk(KERN_ERR"[CT_API]set-conntrak_u32: ct is not existed or ct is untracked\n");
return CMHI_EXT_ERR;
}
switch (type)
{
case USER_ID:
ct->cmhi.user_id = value;
break;
case NODE_INDEX:
ct->cmhi.node_index = value;
break;
case POLICY_VERSION:
ct->cmhi.policy_version = value;
break;
case ACTION:
ct->cmhi.action = value;
break;
default: {
printk(KERN_INFO"[CT_API]set-conntrak_u32: value is not u32\n");
return CMHI_EXT_ERR;
}
}
return CMHI_EXT_OK;
}
/**********************************************************
* :ct扩展字段中action字段
* :sk_buff, abit()
* :
* :
**********************************************************/
int cmhi_set_conntrack_action_by_bit(const struct sk_buff *skb, cmhi_ext_action_bit abit)
{
enum ip_conntrack_info ctinfo = {0};
struct nf_conn *ct = NULL;
if(!skb){
printk(KERN_ERR"[CT_API]set-conntrack_action_by_bit: skb is null\n");
return CMHI_EXT_ERR;
}
ct = nf_ct_get(skb, &ctinfo);
if(!ct){
printk(KERN_ERR"[CT_API]set-conntrak_action_by_bit: ct is not existed or ct is untracked\n");
return CMHI_EXT_ERR;
}
switch (abit)
{
case CMHI_EXT_PASS:
case CMHI_EXT_GOTO_DPI:
ct->cmhi.action |= abit;
break;
default: {
printk(KERN_INFO"[CT_API]set-conntrak_action_by_bit: wrong action bit\n");
return CMHI_EXT_ERR;
}
}
return CMHI_EXT_OK;
}
/**********************************************************
* :ct扩展字段中16
* :sk_buff, value(), type()
* :
* :
**********************************************************/
int cmhi_get_conntrack_u16(const struct sk_buff *skb, uint16_t *value, cmhi_ext_type type)
{
enum ip_conntrack_info ctinfo = {0};
struct nf_conn *ct = NULL;
if(!value){
printk(KERN_ERR"[CT_API]get-conntrak_u16: value is null\n");
return CMHI_EXT_ERR;
}
if(!skb){
printk(KERN_ERR"[CT_API]get-conntrack_u16: skb is null\n");
return CMHI_EXT_ERR;
}
ct = nf_ct_get(skb, &ctinfo);
if(!ct){
printk(KERN_ERR"[CT_API]get-conntrak_u16: ct is not existed or ct is untracked\n");
return CMHI_EXT_ERR;
}
switch (type)
{
case USER_VERSION:
*value = ct->cmhi.user_version;
break;
case APP_ID:
*value = ct->cmhi.app_id;
break;
default: {
printk(KERN_INFO"[CT_API]get-conntrak_u16: value is not u16\n");
return CMHI_EXT_ERR;
}
}
return CMHI_EXT_OK;
}
/**********************************************************
* :ct扩展字段中32
* :sk_buff, value(), type()
* :
* :
**********************************************************/
int cmhi_get_conntrack_u32(const struct sk_buff *skb, uint32_t *value, cmhi_ext_type type)
{
enum ip_conntrack_info ctinfo = {0};
struct nf_conn *ct = NULL;
if(!value){
printk(KERN_ERR"[CT_API]get-conntrak_u32: value is null\n");
return CMHI_EXT_ERR;
}
if(!skb){
printk(KERN_ERR"[CT_API]get-conntrack_u32: skb is null\n");
return CMHI_EXT_ERR;
}
ct = nf_ct_get(skb, &ctinfo);
if(!ct){
printk(KERN_ERR"[CT_API]get-conntrak_u32: ct is not existed or ct is untracked\n");
return CMHI_EXT_ERR;
}
switch (type)
{
case USER_ID:
*value = ct->cmhi.user_id;
break;
case NODE_INDEX:
*value = ct->cmhi.node_index;
break;
case POLICY_VERSION:
*value = ct->cmhi.policy_version;
break;
case ACTION:
*value = ct->cmhi.action;
break;
default: {
printk(KERN_INFO"[CT_API]get-conntrak_u32: value is not u32\n");
return CMHI_EXT_ERR;
}
}
return CMHI_EXT_OK;
}
/**********************************************************
* :ct扩展字段中的某个字段
* :sk_buff, type()
* :
* :
**********************************************************/
int cmhi_del_conntrack(const struct sk_buff *skb, cmhi_ext_type type)
{
enum ip_conntrack_info ctinfo = {0};
struct nf_conn *ct = NULL;
if(!skb){
printk(KERN_ERR"[CT_API]del-conntrack: skb is null\n");
return CMHI_EXT_ERR;
}
ct = nf_ct_get(skb, &ctinfo);
if(!ct){
printk(KERN_ERR"[CT_API]del-conntrak: ct is not existed or ct is untracked\n");
return CMHI_EXT_ERR;
}
switch (type)
{
case USER_ID:
ct->cmhi.user_id = 0;
break;
case USER_VERSION:
ct->cmhi.user_version = 0;
break;
case NODE_INDEX:
ct->cmhi.node_index = 0;
break;
case APP_ID:
ct->cmhi.app_id = 0;
break;
case POLICY_VERSION:
ct->cmhi.policy_version = 0;
break;
case ACTION:
ct->cmhi.action = 0;
break;
default: {
printk(KERN_INFO"[CT_API]del-conntrak: wrong type\n");
return CMHI_EXT_ERR;
}
}
return CMHI_EXT_OK;
}
/**********************************************************
* : ct扩展字段中的action字段的某个比特位
* : sk_buff, abit()
* :
* :
**********************************************************/
int cmhi_del_conntrack_action_by_bit(const struct sk_buff *skb, cmhi_ext_action_bit abit)
{
enum ip_conntrack_info ctinfo = {0};
struct nf_conn *ct = NULL;
if(!skb){
printk(KERN_ERR"[CT_API]del-conntrack_action_by_bit: skb is null\n");
return CMHI_EXT_ERR;
}
ct = nf_ct_get(skb, &ctinfo);
if(!ct){
printk(KERN_ERR"[CT_API]del-conntrack_action_by_bit: ct is not existed or ct is untracked\n");
return CMHI_EXT_ERR;
}
switch (abit)
{
case CMHI_EXT_PASS:
case CMHI_EXT_GOTO_DPI:
ct->cmhi.action &= (~abit);
break;
default: {
printk(KERN_INFO"[CT_API]del-conntrack_action_by_bit: wrong action bit\n");
return CMHI_EXT_ERR;
}
}
return CMHI_EXT_OK;
}
/**********************************************************
* : tuple获取hash值
* : const struct nf_conntrack_tuple *
* : u32 hash值
* : u32 hash值
**********************************************************/
static u32 cmhi_hash_conntrack_raw(const struct nf_conntrack_tuple *tuple)
{
unsigned int n;
u32 seed;
seed = cmhi_seed;
n = (sizeof(tuple->src) + sizeof(tuple->dst.u3)) / sizeof(u32);
return jhash2((u32 *)tuple, n, seed ^
(((__force __u16)tuple->dst.u.all << 16) |
tuple->dst.protonum));
}
/*
static
int cmhi_net_eq(unsigned long ulnet1, unsigned long ulnet2)
{
return ulnet1 == ulnet2;
}
*/
/**********************************************************
* : tuple和查询到的ct中的tuple是否相同
* ct中的bit位是否置为IPS_CONFIRMED_BIT
* : ct中存的tuple的全局hash链表:struct nf_conntrack_tuple_hash *
* DPI传入的tuple: const struct nf_conntrack_tuple *
* :
* : bool类型 true or false
**********************************************************/
static bool
cmhi_nf_ct_key_equal(struct nf_conntrack_tuple_hash *h,
const struct nf_conntrack_tuple *tuple)
{
struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
/* A conntrack can be recreated with the equal tuple,
* so we need to check that the conntrack is confirmed
*/
return nf_ct_tuple_equal(tuple, &h->tuple) &&
//nf_ct_zone_equal(ct, zone, NF_CT_DIRECTION(h)) &&
//nf_ct_is_confirmed(ct) &&
//cmhi_net_eq((unsigned long)(nf_ct_net(ct)));
nf_ct_is_confirmed(ct);
}
/**********************************************************
* :ct是否是expired并设置ct_general
* :struct nf_conn *
* :
* :
**********************************************************/
static void cmhi_nf_ct_gc_expired(struct nf_conn *ct)
{
if (!atomic_inc_not_zero(&ct->ct_general.use))
return;
if (nf_ct_should_gc(ct))
nf_ct_kill(ct);
nf_ct_put(ct);
}
/*
* Warning :
* - Caller must take a reference on returned object
* and recheck nf_ct_tuple_equal(tuple, &h->tuple)
*/
static struct nf_conntrack_tuple_hash *
cmhi_nf_conntrack_find(
const struct nf_conntrack_tuple *tuple, u32 hash)
{
struct nf_conntrack_tuple_hash *h;
struct hlist_nulls_head *ct_hash;
struct hlist_nulls_node *n;
unsigned int bucket, hsize;
begin:
nf_conntrack_get_ht(&ct_hash, &hsize);
bucket = reciprocal_scale(hash, hsize);
//printk(KERN_INFO"[CT_API]###bucket=%d\n", bucket);
hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[bucket], hnnode) {
struct nf_conn *ct;
ct = nf_ct_tuplehash_to_ctrack(h);
if (nf_ct_is_expired(ct)) {
cmhi_nf_ct_gc_expired(ct);
continue;
}
if (nf_ct_is_dying(ct))
continue;
if (cmhi_nf_ct_key_equal(h, tuple))
return h;
}
/*
* if the nulls value we got at the end of this lookup is
* not the expected one, we must restart lookup.
* We probably met an item that was moved to another chain.
*/
if (get_nulls_value(n) != bucket) {
//printk(KERN_INFO"[CT_API]nulls_value != bucket, bucket=%d\n\n\n\n", bucket);
goto begin;
}
return NULL;
}
/**********************************************************
* : DPI信息构造的tuple和hash值查找tuple所在hash
* ,
* : tuple:const struct nf_conntrack_tuple *
* tuple生成的hash值:u32
* : tuple所在的hashmap的首指针
* : tuple所在的hashmap的首指针
**********************************************************/
static struct nf_conntrack_tuple_hash *
cmhi_nf_conntrack_find_get(
const struct nf_conntrack_tuple *tuple, u32 hash)
{
struct nf_conntrack_tuple_hash *h;
struct nf_conn *ct;
rcu_read_lock();
begin:
h = cmhi_nf_conntrack_find(tuple, hash);
if (h) {
ct = nf_ct_tuplehash_to_ctrack(h);
if (unlikely(nf_ct_is_dying(ct) ||
!atomic_inc_not_zero(&ct->ct_general.use)))
h = NULL;
else {
if (unlikely(!cmhi_nf_ct_key_equal(h, tuple))) {
nf_ct_put(ct);
goto begin;
}
}
}
rcu_read_unlock();
return h;
}
/**********************************************************
* : DPI tuple获取链接跟踪指针ct
* : DPI五元组tuple地址: struct dpi_tuple *
* : struct nf_conn *
* : struct nf_conn *
**********************************************************/
struct nf_conn *get_conntrack_from_tuple(struct dpi_tuple *dpi_tuple)
{
u32 hash;
struct nf_conntrack_tuple tuple = {0};
struct nf_conntrack_tuple_hash *h= NULL;
struct nf_conn *ct = NULL;
if(!dpi_tuple){
printk(KERN_ERR"[CT_API]get-conntrack-from-tuple: dpi_tuple is null.\n");
return NULL;
}
tuple.src.l3num = 2;
tuple.src.u3.ip = dpi_tuple->sip;
tuple.dst.u3.ip = dpi_tuple->dip;
tuple.dst.protonum = dpi_tuple->protonum;
tuple.dst.dir = IP_CT_DIR_ORIGINAL;
/* UDP */
if(dpi_tuple->protonum == IPPROTO_UDP){
tuple.src.u.udp.port = dpi_tuple->sport;
tuple.dst.u.udp.port = dpi_tuple->dport;
}
/* TCP */
if(dpi_tuple->protonum == IPPROTO_TCP){
tuple.src.u.tcp.port = dpi_tuple->sport;
tuple.dst.u.tcp.port = dpi_tuple->dport;
}
printk(KERN_INFO"[CT_API]<L3XXX>:src.l3num=%d, src.u3.ip=%x, dst.u3.ip=%x, dst.protonum=%d, dst.dir=%d,dst.u.all=%d\n",
tuple.src.l3num, tuple.src.u3.ip, tuple.dst.u3.ip, tuple.dst.protonum, tuple.dst.dir, tuple.dst.u.all);
hash = cmhi_hash_conntrack_raw(&tuple);
printk(KERN_INFO"[CT_API]get-conntrack-from-tuple:hash=%d\n", hash);
h = cmhi_nf_conntrack_find_get(&tuple, hash);
if (!h) {
printk(KERN_ERR"[CT_API]get-conntrack-from-tuple: h is null.\n");
return NULL;
}
ct = nf_ct_tuplehash_to_ctrack(h);
return ct;
}
/**********************************************************
* : id aid设置到相应的链接跟踪ct中
* : struct nf_conn *id aid
* :
* : 0-1
**********************************************************/
int __set_aid_by_dpi_tuple(struct nf_conn *ct, uint16_t aid)
{
if(!ct){
printk(KERN_ERR"[CT_API]__set-appid-by-dpituple:input ct is null.\n");
return CMHI_EXT_ERR;
}
if(!ct){
printk(KERN_ERR"[CT_API]__set-appid-by-dpituple: ct is not existed or ct is untracked\n");
return CMHI_EXT_ERR;
}
ct->cmhi.app_id = aid;
return CMHI_EXT_OK;
}
/**********************************************************
* : id aid设置到相应的链接跟踪ct中
* : id aid
* :
* : 0-1
**********************************************************/
int set_aid_by_dpi_tuple(struct dpi *dpi)
{
int ret;
struct nf_conn *ct;
if(!dpi){
printk(KERN_ERR"[CT_API]set-appid-by-dpituple: dpi is null.\n");
return CMHI_EXT_ERR;
}
ct = get_conntrack_from_tuple(&dpi->tuple);
if(!ct){
printk(KERN_ERR"[CT_API]set-appid-by-dpituple:find ct failed, now return.\n");
return CMHI_EXT_ERR;
}
printk(KERN_INFO"[CT_API]set-appid-by-dpituple: Find ct !!!\n");
ret = __set_aid_by_dpi_tuple(ct, dpi->aid);
if(CMHI_EXT_OK != ret){
printk(KERN_ERR"[CT_API]set-appid-by-dpituple:set aid failed.\n");
return CMHI_EXT_ERR;
}
printk(KERN_INFO"[CT_API]set-appid-by-dpituple: set aid ok\n");
return CMHI_EXT_OK;
}
EXPORT_SYMBOL(cmhi_set_conntrack_u16);
EXPORT_SYMBOL(cmhi_set_conntrack_u32);
EXPORT_SYMBOL(cmhi_set_conntrack_action_by_bit);
EXPORT_SYMBOL(cmhi_get_conntrack_u16);
EXPORT_SYMBOL(cmhi_get_conntrack_u32);
EXPORT_SYMBOL(cmhi_del_conntrack);
EXPORT_SYMBOL(cmhi_del_conntrack_action_by_bit);
EXPORT_SYMBOL(get_conntrack_from_tuple);
EXPORT_SYMBOL(set_aid_by_dpi_tuple);
static int __init API_init(void)
{
printk(KERN_INFO"[API]module init.\n");
return 0;
}
static void __exit API_exit(void)
{
printk(KERN_INFO"[API]module exit.\n");
return;
}
module_init(API_init);
module_exit(API_exit);
MODULE_DESCRIPTION("CMHI API");
MODULE_VERSION("0.1");
MODULE_LICENSE("GPL");

View File

@ -0,0 +1,54 @@
#ifndef _CONNTRACK_API_H
#define _CONNTRACK_API_H
#include <net/netfilter/nf_conntrack.h>
extern seqcount_t nf_conntrack_generation;
extern u32 cmhi_seed;
#define CMHI_EXT_ERR -1
#define CMHI_EXT_OK 0
typedef enum{
USER_ID,
USER_VERSION,
NODE_INDEX,
APP_ID,
POLICY_VERSION,
ACTION,
}cmhi_ext_type;
//modify cmhi_ext_info u_int32_t status
typedef enum{
/*skb pass or not*/
//CMHI_EXT_PASS_BIT = 0,
CMHI_EXT_PASS = (1 << 0),
/*skb go to dpi or not*/
//CMHI_EXT_GOTO_DPI_BIT = 1,
CMHI_EXT_GOTO_DPI = (1 << 1),
}cmhi_ext_action_bit;
struct dpi_tuple{
__be32 sip;
__be32 dip;
__be16 sport;
__be16 dport;
u_int8_t protonum;
};
struct dpi{
struct dpi_tuple tuple; /* dpi???a¡Á¨¦ */
uint16_t aid; /* app id */
};
int cmhi_set_conntrack_u16(const struct sk_buff *skb, uint16_t value, cmhi_ext_type type);
int cmhi_set_conntrack_u32(const struct sk_buff *skb, uint32_t value, cmhi_ext_type type);
int cmhi_set_conntrack_action_by_bit(const struct sk_buff *skb, cmhi_ext_action_bit abit);
int cmhi_get_conntrack_u16(const struct sk_buff *skb, uint16_t *value, cmhi_ext_type type);
int cmhi_get_conntrack_u32(const struct sk_buff *skb, uint32_t *value, cmhi_ext_type type);
int cmhi_del_conntrack(const struct sk_buff *skb, cmhi_ext_type type);
int cmhi_del_conntrack_action_by_bit(const struct sk_buff *skb, cmhi_ext_action_bit abit);
struct nf_conn *get_conntrack_from_tuple(struct dpi_tuple *dpi_tuple);
int set_aid_by_dpi_tuple(struct dpi *dpi);
#endif /* _CONNTRACK_API_H */

View File

@ -0,0 +1,28 @@
#include <linux/module.h>
#include <linux/vermagic.h>
#include <linux/compiler.h>
MODULE_INFO(vermagic, VERMAGIC_STRING);
MODULE_INFO(name, KBUILD_MODNAME);
__visible struct module __this_module
__attribute__((section(".gnu.linkonce.this_module"))) = {
.name = KBUILD_MODNAME,
.init = init_module,
#ifdef CONFIG_MODULE_UNLOAD
.exit = cleanup_module,
#endif
.arch = MODULE_ARCH_INIT,
};
#ifdef RETPOLINE
MODULE_INFO(retpoline, "Y");
#endif
static const char __module_depends[]
__used
__attribute__((section(".modinfo"))) =
"depends=nf_conntrack";
MODULE_INFO(srcversion, "0A5F318A837DEF6E6F82ABC");

View File

@ -0,0 +1,15 @@
ifneq ($(KERNELRELEASE),)
obj-m += demoA.o
else
PWD := $(shell pwd)
KVER := $(shell uname -r)
KDIR := $(HUACHENG_LINUX_KERNEL)
KBUILD_EXTRA_SYMBOLS += ../api/Module.symvers
export KBUILD_EXTRA_SYMBOLS
default:
$(MAKE) -C $(KDIR) M=$(PWD) modules
all:
make -C $(KDIR) M=$(PWD) modules
clean:
rm -rf *.o *.mod.c *.ko *.symvers *.order *.makers .*.cmd *.cmd .tmp_versions .cache.mk
endif

View File

@ -0,0 +1,412 @@
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/init.h>
#include <linux/inet.h>
#include <linux/moduleparam.h>
#include <linux/skbuff.h>
#include <linux/netdevice.h>
#include <linux/net.h>
#include "../api/conntrack_api.h"
static unsigned int test_set_correct_value(void *priv, struct sk_buff *skb, const struct nf_hook_state *state)
{
int ret;
printk(KERN_INFO"[test_set_correct_value]begin!!!!\n");
printk(KERN_INFO"[test_set_correct_value] test set user_version 10\n");
ret = cmhi_set_conntrack_u16(skb, 10, USER_VERSION);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_set_correct_value]set user_version failed\n");
goto out;
}
printk(KERN_INFO"[test_set_correct_value] set user_version successed\n");
printk(KERN_INFO"[test_set_correct_value] test set app_id 20\n");
ret = cmhi_set_conntrack_u16(skb, 20, APP_ID);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_set_correct_value] set app_id failed\n");
goto out;
}
printk(KERN_INFO"[test_set_correct_value] set app_id successed\n");
printk(KERN_INFO"[test_set_correct_value] test set user_id 30\n");
ret = cmhi_set_conntrack_u32(skb, 30, USER_ID);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_set_correct_value] set user_id failed\n");
goto out;
}
printk(KERN_INFO"[test_set_correct_value] set user_id successed\n");
printk(KERN_INFO"[test_set_correct_value] test set node_index 40\n");
ret = cmhi_set_conntrack_u32(skb, 40, NODE_INDEX);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_set_correct_value]set node_index failed\n");
goto out;
}
printk(KERN_INFO"[test_set_correct_value] set node_index successed\n");
printk(KERN_INFO"[test_set_correct_value] test set policy_version 50\n");
ret = cmhi_set_conntrack_u32(skb, 50, POLICY_VERSION);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_set_correct_value] set policy_version failed\n");
goto out;
}
printk(KERN_INFO"[test_set_correct_value] set policy_version successed\n");
ret = cmhi_set_conntrack_action_by_bit(skb, CMHI_EXT_PASS);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_set_correct_value] set action failed\n");
goto out;
}
printk(KERN_INFO"[test_set_correct_value] set action successed\n");
goto out;
out:
return NF_ACCEPT;
}
static unsigned int test_set_value_by_wrong_interface(void *priv, struct sk_buff *skb, const struct nf_hook_state *state)
{
int ret;
printk(KERN_INFO"[test_set_value_by_wrong_interface]begin!!!!\n");
printk(KERN_INFO"[test_set_value_by_wrong_interface] test set user_version 10\n");
ret = cmhi_set_conntrack_u32(skb, 10, USER_VERSION);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_set_value_by_wrong_interface]set user_version failed\n");
goto out;
}
printk(KERN_INFO"[test_set_value_by_wrong_interface] set user_version successed\n");
printk(KERN_INFO"[test_set_value_by_wrong_interface] test set app_id 20\n");
ret = cmhi_set_conntrack_u32(skb, 20, APP_ID);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_set_value_by_wrong_interface] set app_id failed\n");
goto out;
}
printk(KERN_INFO"[test_set_value_by_wrong_interface] set app_id successed\n");
printk(KERN_INFO"[test_set_value_by_wrong_interface] test set user_id 30\n");
ret = cmhi_set_conntrack_u16(skb, 30, USER_ID);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_set_value_by_wrong_interface] set user_id failed\n");
goto out;
}
printk(KERN_INFO"[test_set_value_by_wrong_interface] set user_id successed\n");
printk(KERN_INFO"[test_set_value_by_wrong_interface] test set node_index 40\n");
ret = cmhi_set_conntrack_u16(skb, 40, NODE_INDEX);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_set_value_by_wrong_interface]set node_index failed\n");
goto out;
}
printk(KERN_INFO"[test_set_value_by_wrong_interface] set node_index successed\n");
printk(KERN_INFO"[test_set_value_by_wrong_interface] test set policy_version 50\n");
ret = cmhi_set_conntrack_u16(skb, 50, POLICY_VERSION);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_set_value_by_wrong_interface] set policy_version failed\n");
goto out;
}
printk(KERN_INFO"[test_set_value_by_wrong_interface] set policy_version successed\n");
goto out;
out:
return NF_ACCEPT;
}
static unsigned int test_get_value_by_correct_interface(void *priv, struct sk_buff *skb, const struct nf_hook_state *state)
{
int ret;
u_int32_t user_id;
u_int16_t user_version;
u_int32_t node_index;
u_int16_t app_id;
u_int32_t policy_version;
u_int32_t action;
printk(KERN_INFO"[test_get_value_by_correct_interface]begin!!!!\n");
ret = cmhi_get_conntrack_u32(skb, &user_id, USER_ID);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_get_value_by_correct_interface]: get user_id failed\n");
goto out;
}
printk(KERN_INFO"[test_get_value_by_correct_interface]: get usr_id: %d successed\n", user_id);
ret = cmhi_get_conntrack_u16(skb, &user_version, USER_VERSION);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_get_value_by_correct_interface]: get user_version failed\n");
goto out;
}
printk(KERN_INFO"[test_get_value_by_correct_interface]: get user_version: %d successed\n", user_version);
ret = cmhi_get_conntrack_u32(skb, &node_index, NODE_INDEX);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_get_value_by_correct_interface]: get node_index failed\n");
goto out;
}
printk(KERN_INFO"[test_get_value_by_correct_interface]: get node_index: %d successed\n", node_index);
ret = cmhi_get_conntrack_u16(skb, &app_id, APP_ID);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_get_value_by_correct_interface]: get app_id failed\n");
goto out;
}
printk(KERN_INFO"[test_get_value_by_correct_interface]: get app_id: %d successed\n", app_id);
ret = cmhi_get_conntrack_u32(skb, &policy_version, POLICY_VERSION);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_get_value_by_correct_interface]: get policy_version failed\n");
goto out;
}
printk(KERN_INFO"[test_get_value_by_correct_interface]: get policy_version: %d successed\n", policy_version);
ret = cmhi_get_conntrack_u32(skb, &action, ACTION);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_get_value_by_correct_interface]: get action failed\n");
goto out;
}
printk(KERN_INFO"[test_get_value_by_correct_interface]: get action: %d successed\n", action);
goto out;
out:
return NF_ACCEPT;
}
static unsigned int test_get_value_by_wrong_interface(void *priv, struct sk_buff *skb, const struct nf_hook_state *state)
{
int ret;
u_int16_t user_id;
u_int32_t user_version;
u_int16_t node_index;
u_int32_t app_id;
u_int16_t policy_version;
u_int16_t action;
printk(KERN_INFO"[test_get_value_by_wrong_interface]begin!!!!\n");
ret = cmhi_get_conntrack_u16(skb, &user_id, USER_ID);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_get_value_by_wrong_interface]: get user_id failed\n");
goto out;
}
printk(KERN_INFO"[test_get_value_by_wrong_interface]: get usr_id: %d successed\n", user_id);
ret = cmhi_get_conntrack_u32(skb, &user_version, USER_VERSION);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_get_value_by_wrong_interface]: get user_version failed\n");
goto out;
}
printk(KERN_INFO"[test_get_value_by_wrong_interface]: get user_version: %d successed\n", user_version);
ret = cmhi_get_conntrack_u16(skb, &node_index, NODE_INDEX);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_get_value_by_wrong_interface]: get node_index failed\n");
goto out;
}
printk(KERN_INFO"[test_get_value_by_wrong_interface]: get node_index: %d successed\n", node_index);
ret = cmhi_get_conntrack_u32(skb, &app_id, APP_ID);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_get_value_by_wrong_interface]: get app_id failed\n");
goto out;
}
printk(KERN_INFO"[test_get_value_by_wrong_interface]: get app_id: %d successed\n", app_id);
ret = cmhi_get_conntrack_u16(skb, &policy_version, POLICY_VERSION);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_get_value_by_wrong_interface]: get policy_version failed\n");
goto out;
}
printk(KERN_INFO"[test_get_value_by_wrong_interface]: get policy_version: %d successed\n", policy_version);
ret = cmhi_get_conntrack_u16(skb, &action, ACTION);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_get_value_by_wrong_interface]: get action failed\n");
goto out;
}
printk(KERN_INFO"[test_get_value_by_wrong_interface]: get action: %d successed\n", action);
goto out;
out:
return NF_ACCEPT;
}
static unsigned int test_get_null_value(void *priv, struct sk_buff *skb, const struct nf_hook_state *state)
{
int ret;
printk(KERN_INFO"[test_get_null_value]begin!!!!\n");
ret = cmhi_get_conntrack_u32(skb, NULL, USER_ID);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_get_null_value]: get user_id failed\n");
goto out;
}
printk(KERN_INFO"[test_get_null_value]: get usr_id successed\n");
ret = cmhi_get_conntrack_u16(skb, NULL, USER_VERSION);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_get_null_value]: get user_version failed\n");
goto out;
}
printk(KERN_INFO"[test_get_null_value]: get user_version successed\n");
ret = cmhi_get_conntrack_u32(skb, NULL, NODE_INDEX);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_get_null_value]: get node_index failed\n");
goto out;
}
printk(KERN_INFO"[test_get_null_value]: get node_index successed\n");
ret = cmhi_get_conntrack_u16(skb, NULL, APP_ID);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_get_null_value]: get app_id failed\n");
goto out;
}
printk(KERN_INFO"[test_get_null_value]: get app_id successed\n");
ret = cmhi_get_conntrack_u32(skb, NULL, POLICY_VERSION);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_get_null_value]: get policy_version failed\n");
goto out;
}
printk(KERN_INFO"[test_get_null_value]: get policy_version successed\n");
ret = cmhi_get_conntrack_u32(skb, NULL, ACTION);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_get_null_value]: get action failed\n");
goto out;
}
printk(KERN_INFO"[test_get_null_value]: get action successed\n");
goto out;
out:
return NF_ACCEPT;
}
static unsigned int test_del_value(void *priv, struct sk_buff *skb, const struct nf_hook_state *state)
{
int ret;
printk(KERN_INFO"[test_del_value]begin!!!!\n");
ret = cmhi_del_conntrack(skb, USER_ID);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_del_value]: del user_id failed\n");
goto out;
}
printk(KERN_INFO"[test_del_value]: del usr_id successed\n");
ret = cmhi_del_conntrack(skb, USER_VERSION);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_del_value]: del USER_VERSION failed\n");
goto out;
}
printk(KERN_INFO"[test_del_value]: del USER_VERSION successed\n");
ret = cmhi_del_conntrack(skb, APP_ID);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_del_value]: del APP_ID failed\n");
goto out;
}
printk(KERN_INFO"[test_del_value]: del APP_ID successed\n");
ret = cmhi_del_conntrack(skb, NODE_INDEX);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_del_value]: del NODE_INDEX failed\n");
goto out;
}
printk(KERN_INFO"[test_del_value]: del NODE_INDEX successed\n");
ret = cmhi_del_conntrack(skb, POLICY_VERSION);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test_del_value]: del POLICY_VERSION failed\n");
goto out;
}
printk(KERN_INFO"[test_del_value]: del POLICY_VERSION successed\n");
printk(KERN_INFO"DomoB: test del action by bit\n");
ret = cmhi_del_conntrack_action_by_bit(skb, CMHI_EXT_PASS);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"DemoB_test: del action by bit failed");
goto out;
}
printk(KERN_INFO"DemoB_test: del action by bit successed\n");
goto out;
out:
return NF_ACCEPT;
}
static struct nf_hook_ops nfho[] __read_mostly = {
{
.hook = (nf_hookfn *)test_set_correct_value,
.hooknum = NF_INET_PRE_ROUTING,
.pf = NFPROTO_IPV4,
.priority = NF_IP_PRI_FILTER,
},
{
.hook = (nf_hookfn *)test_set_value_by_wrong_interface,
.hooknum = NF_INET_PRE_ROUTING,
.pf = NFPROTO_IPV4,
.priority = NF_IP_PRI_FILTER+1,
},
{
.hook = (nf_hookfn *)test_get_value_by_correct_interface,
.hooknum = NF_INET_PRE_ROUTING,
.pf = NFPROTO_IPV4,
.priority = NF_IP_PRI_FILTER+2,
},
{
.hook = (nf_hookfn *)test_get_value_by_wrong_interface,
.hooknum = NF_INET_PRE_ROUTING,
.pf = NFPROTO_IPV4,
.priority = NF_IP_PRI_FILTER+3,
},
{
.hook = (nf_hookfn *)test_get_null_value,
.hooknum = NF_INET_PRE_ROUTING,
.pf = NFPROTO_IPV4,
.priority = NF_IP_PRI_FILTER+4,
},
{
.hook = (nf_hookfn *)test_del_value,
.hooknum = NF_INET_PRE_ROUTING,
.pf = NFPROTO_IPV4,
.priority = NF_IP_PRI_FILTER+5,
},
};
static int __init DemoA_init(void)
{
nf_register_net_hooks(&init_net, nfho, ARRAY_SIZE(nfho));
printk(KERN_INFO"[DemoA] Module_init\n");
return 0;
}
static void __exit DemoA_clean(void)
{
nf_unregister_net_hooks(&init_net, nfho, ARRAY_SIZE(nfho));
printk(KERN_INFO"[DemoA]Module_clean\n");
return;
}
module_init(DemoA_init);
module_exit(DemoA_clean);
MODULE_LICENSE("GPL");

View File

@ -0,0 +1,28 @@
#include <linux/module.h>
#include <linux/vermagic.h>
#include <linux/compiler.h>
MODULE_INFO(vermagic, VERMAGIC_STRING);
MODULE_INFO(name, KBUILD_MODNAME);
__visible struct module __this_module
__attribute__((section(".gnu.linkonce.this_module"))) = {
.name = KBUILD_MODNAME,
.init = init_module,
#ifdef CONFIG_MODULE_UNLOAD
.exit = cleanup_module,
#endif
.arch = MODULE_ARCH_INIT,
};
#ifdef RETPOLINE
MODULE_INFO(retpoline, "Y");
#endif
static const char __module_depends[]
__used
__attribute__((section(".modinfo"))) =
"depends=";
MODULE_INFO(srcversion, "FA4CBF739E142A77FC9D2B5");

View File

@ -0,0 +1,15 @@
ifneq ($(KERNELRELEASE),)
obj-m += test.o
else
PWD := $(shell pwd)
KVER := $(shell uname -r)
KDIR := $(HUACHENG_LINUX_KERNEL)
KBUILD_EXTRA_SYMBOLS += ../api/Module.symvers
export KBUILD_EXTRA_SYMBOLS
default:
$(MAKE) -C $(KDIR) M=$(PWD) modules
all:
make -C $(KDIR) M=$(PWD) modules
clean:
rm -rf *.o *.mod.c *.ko *.symvers *.order *.makers .*.cmd *.cmd .tmp_versions .cache.mk
endif

View File

@ -0,0 +1,265 @@
#include "../api/conntrack_api.h"
/*
: :
1,,
ct的,ct,ct里的aid
tcp,,ESTABLISHED
ct不会老化.
2ct之后,
ct,aid查找出来.
1conntrack_api.ko
2tcp三次握手,ESTABLISHED
3test中的设置aid的函数dpi_set_aid_test_tcp放在init中
,get_aid_test放在netfilter的钩子
,
4test.ko(test为在init函数中只设置一次aid,
tcp三次握手
,test找不到ct,aid失败;
dpi_set_aid_test_tcp放在netfilter的钩子中,,
test.ko和链接跟踪创建的时序就无所谓了)
keypoint: test的加载顺序,aid
init,,;aid在netfilter
,,,,
,
tcp程序,server端一直循环recv,close fd,
client端也不close fd,send;ct,
send触发.
TCP报文记录链接跟踪的前提是建立三次握手成功,
.UDP可以单方面打流来创建ct.
:
:
: 0-1
*/
enum test_proto_type{
TEST_PROTO_TCP,
TEST_PROTO_UDP,
};
/**********************************************************
* :aid,netfilter的hook上
* :void *, struct sk_buff *, const struct nf_hook_state *
* :
* :
**********************************************************/
static unsigned int get_aid_test(void *priv, struct sk_buff *skb, const struct nf_hook_state *state)
{
int ret;
uint16_t aid;
printk(KERN_INFO"[test]: test get app_id\n");
ret = cmhi_get_conntrack_u16(skb, &aid, APP_ID);
if(ret == CMHI_EXT_ERR){
printk(KERN_INFO"[test]: get app_id failed\n");
goto out;
}
printk(KERN_INFO"[test]: get app_id: %d successed\n", aid);
goto out;
out:
return NF_ACCEPT;
}
/**********************************************************
* :TCP的DPI五元组获取链接跟踪指针ct
* :
* :
* : 0-1
**********************************************************/
int dpi_get_ct_test_tcp(void)
{
int ret;
struct nf_conn *ct;
struct dpi_tuple dpi_tuple;
dpi_tuple.sip = htonl(0xc0a840bc);//192.168.64.188
dpi_tuple.dip = htonl(0xc0a840c2);//192.168.64.194
dpi_tuple.protonum = IPPROTO_TCP;
dpi_tuple.sport = htons(179);
dpi_tuple.dport = htons(10020);
printk(KERN_INFO"[Test-tcp]dpi_tuple sip=%u, dip=%u, proto=%d, sport=%du, dport=%du\n",
dpi_tuple.sip, dpi_tuple.dip, dpi_tuple.protonum, dpi_tuple.sport, dpi_tuple.dport);
ct = get_conntrack_from_tuple(&dpi_tuple);
if(!ct){
printk(KERN_ERR"[Test-tcp]get ct failed!!\n");
ret = CMHI_EXT_ERR;
}
else {
printk(KERN_INFO"[Test-tcp]Find it!!\n");
ret = CMHI_EXT_OK;
}
goto out;
out:
return ret;
}
/**********************************************************
* :UDP的DPI五元组获取链接跟踪指针ct
* :
* :
* : 0-1
**********************************************************/
int dpi_get_ct_test_udp(void)
{
int ret;
struct nf_conn *ct;
struct dpi_tuple dpi_tuple;
dpi_tuple.sip = htonl(0xc0a840c7);//192.168.64.199
dpi_tuple.dip = htonl(0xc0a840c2);//192.168.64.194
dpi_tuple.protonum = IPPROTO_UDP;
dpi_tuple.sport = htons(7);
dpi_tuple.dport = htons(520);
printk(KERN_INFO"[Test-udp]dpi_tuple sip=%x, dip=%x, proto=%d, sport=%x, dport=%x\n",
dpi_tuple.sip, dpi_tuple.dip, dpi_tuple.protonum, dpi_tuple.sport, dpi_tuple.dport);
ct = get_conntrack_from_tuple(&dpi_tuple);
if(!ct){
printk(KERN_ERR"[Test-udp]get ct failed!!\n");
ret = CMHI_EXT_ERR;
}
else {
printk(KERN_INFO"[Test-udp]Find it!!\n");
ret = CMHI_EXT_OK;
}
goto out;
out:
return ret;
}
/**********************************************************
* :TCP的DPI五元组设置链接跟踪ct里的aid
* :
* :
* : 0-1
**********************************************************/
int dpi_set_aid_test_tcp(void)
{
int ret;
// uint16_t aid;
struct dpi dpi;
dpi.tuple.sip = htonl(0xc0a840bc);//192.168.64.188
dpi.tuple.dip = htonl(0xc0a840c2);//192.168.64.194
dpi.tuple.protonum = IPPROTO_TCP;
dpi.tuple.sport = htons(179);
dpi.tuple.dport = htons(10020);
dpi.aid = 3;
printk(KERN_INFO"[Test]dpi_tuple sip=%x, dip=%x, proto=%d, sport=%du, dport=%du,aid=%d\n",
dpi.tuple.sip, dpi.tuple.dip, dpi.tuple.protonum, dpi.tuple.sport, dpi.tuple.dport, dpi.aid);
ret = set_aid_by_dpi_tuple(&dpi);
if(CMHI_EXT_OK != ret){
printk(KERN_ERR"[Test]set_aid_by_dpi_tuple failed.\n");
return CMHI_EXT_ERR;
}
return CMHI_EXT_OK;
}
/**********************************************************
* :UDP的DPI五元组设置链接跟踪ct里的aid
* :
* :
* : 0-1
**********************************************************/
int dpi_set_aid_test_udp(void)
{
int ret;
// uint16_t aid;
struct dpi dpi;
dpi.tuple.sip = htonl(0xc0a840c7);//192.168.64.199
dpi.tuple.dip = htonl(0xc0a840c2);//192.168.64.194
dpi.tuple.protonum = IPPROTO_UDP;
dpi.tuple.sport = htons(7);
dpi.tuple.dport = htons(520);
dpi.aid = 5;
printk(KERN_INFO"[Test]dpi_set_aid_test_udp: dpi_tuple sip=%x, dip=%x, proto=%d, sport=%d, dport=%d,aid=%d\n",
dpi.tuple.sip, dpi.tuple.dip, dpi.tuple.protonum, dpi.tuple.sport, dpi.tuple.dport, dpi.aid);
ret = set_aid_by_dpi_tuple(&dpi);
if(CMHI_EXT_OK != ret){
printk(KERN_ERR"[Test]dpi_set_aid_test_udp: set_aid_by_dpi_tuple failed.\n");
return CMHI_EXT_ERR;
}
return CMHI_EXT_OK;
}
/**********************************************************
* :TCP还是UDP来调用TCP或UDP的设置
* aid的函数
* : enum test_proto_type
* :
* :
**********************************************************/
void dpi_set_aid_test(enum test_proto_type type)
{
if(TEST_PROTO_TCP == type){
dpi_set_aid_test_tcp();
}
else if(TEST_PROTO_UDP == type){
dpi_set_aid_test_udp();
}
return;
}
/**********************************************************
* :TCP还是UDP来调用TCP或UDP的获取
* ct
* : enum test_proto_type
* :
* :
**********************************************************/
void dpi_get_ct_test(enum test_proto_type type)
{
if(TEST_PROTO_TCP == type){
dpi_get_ct_test_tcp();
}
else if(TEST_PROTO_UDP == type){
dpi_get_ct_test_udp();
}
return;
}
static struct nf_hook_ops nfho[] __read_mostly = {
//{
//.hook = (nf_hookfn *)demoB,
//.hooknum = NF_INET_PRE_ROUTING,
//.pf = NFPROTO_IPV4,
//.priority = (NF_IP_PRI_FILTER-1),
//},
{
.hook = (nf_hookfn *)get_aid_test,
.hooknum = NF_INET_PRE_ROUTING,
.pf = NFPROTO_IPV4,
.priority = NF_IP_PRI_FILTER,
},
};
static int __init test_init(void)
{
dpi_set_aid_test(TEST_PROTO_TCP);
nf_register_net_hooks(&init_net, nfho, ARRAY_SIZE(nfho));
printk(KERN_INFO"[Test] Module_init\n");
return 0;
}
static void __exit test_clean(void)
{
nf_unregister_net_hooks(&init_net, nfho, ARRAY_SIZE(nfho));
printk(KERN_INFO"[Test]Module_clean\n");
return;
}
module_init(test_init);
module_exit(test_clean);
MODULE_LICENSE("GPL");

View File

@ -0,0 +1,28 @@
#include <linux/module.h>
#include <linux/vermagic.h>
#include <linux/compiler.h>
MODULE_INFO(vermagic, VERMAGIC_STRING);
MODULE_INFO(name, KBUILD_MODNAME);
__visible struct module __this_module
__attribute__((section(".gnu.linkonce.this_module"))) = {
.name = KBUILD_MODNAME,
.init = init_module,
#ifdef CONFIG_MODULE_UNLOAD
.exit = cleanup_module,
#endif
.arch = MODULE_ARCH_INIT,
};
#ifdef RETPOLINE
MODULE_INFO(retpoline, "Y");
#endif
static const char __module_depends[]
__used
__attribute__((section(".modinfo"))) =
"depends=";
MODULE_INFO(srcversion, "C1E0D681AD12831025523D4");

View File

@ -0,0 +1,289 @@
#include <linux/module.h>
#include <linux/netlink.h>
#include <linux/netfilter.h>
#include <linux/ip.h>
#include <uapi/linux/netfilter_ipv4.h>
#include <uapi/linux/ip.h>
#include <net/netlink.h>
#include <net/net_namespace.h>
#include "libnetlink_k.h"
#include "../../../Common/commuapinl.h"
struct commnl_msgtype_process pdelivnl_msg_handlers[NLMGS_PDELIVERY_MAX_TYPE];
struct commnl_msgtype_process cfgnl_msg_handlers[COMMCFG_NLMSG_MAX_TYPE];
void printk_mem(unsigned char * p,int len)
{
int num = 0;
for(num = 0; num<len; num++)
{
printk(KERN_DEBUG"%02x ",*(p+num));
if(((num+1)%16 == 0) || ((num+1 ) == len))
{
printk(KERN_DEBUG"\r\n");
}
}
return;
}
int commnl_register(int protocol, int msgtype,
commnl_doit_func doit, commnl_dumpit_func dumpit,
commnl_calcit_func calcit)
{
switch (protocol)
{
case NETLINK_PDELIVERY:
if(msgtype >= NLMGS_PDELIVERY_MAX_TYPE )
{
printk(KERN_ERR"commnl_register invalid msgtype %d,protocl pdeliv.\r\n",msgtype);
return -1;
}
if(NULL != doit)
pdelivnl_msg_handlers[msgtype].doit = doit;
if(NULL != dumpit)
pdelivnl_msg_handlers[msgtype].dumpit = dumpit;
if(NULL != calcit)
pdelivnl_msg_handlers[msgtype].calcit = calcit;
break;
case NETLINK_COMMCFG:
if(msgtype >= COMMCFG_NLMSG_MAX_TYPE )
{
printk(KERN_ERR"commnl_register invalid msgtype %d,protocl conncfg.\r\n",msgtype);
return -1;
}
if(NULL != doit)
cfgnl_msg_handlers[msgtype].doit = doit;
if(NULL != dumpit)
cfgnl_msg_handlers[msgtype].dumpit = dumpit;
if(NULL != calcit)
cfgnl_msg_handlers[msgtype].calcit = calcit;
break;
default:
printk(KERN_ERR"commnl_register invalid protocl %d",protocol);
return -1;
break;
}
printk(KERN_DEBUG"commnl_register sucess msgtype %d,protocl %d.\r\n",msgtype,protocol);
return 0;
}
int commnl_unregister(int protocol, int msgtype)
{
switch (protocol)
{
case NETLINK_PDELIVERY:
if(msgtype >= NLMGS_PDELIVERY_MAX_TYPE )
{
printk(KERN_ERR"commnl_unregister invalid msgtype %d,protocl pdeliv.\r\n",msgtype);
return -1;
}
pdelivnl_msg_handlers[msgtype].doit = NULL;
pdelivnl_msg_handlers[msgtype].dumpit = NULL;
pdelivnl_msg_handlers[msgtype].calcit = NULL;
break;
case NETLINK_COMMCFG:
if(msgtype >= COMMCFG_NLMSG_MAX_TYPE )
{
printk(KERN_ERR"commnl_unregister invalid msgtype %d,protocl conncfg.\r\n",msgtype);
return -1;
}
cfgnl_msg_handlers[msgtype].doit = NULL;
cfgnl_msg_handlers[msgtype].dumpit = NULL;
cfgnl_msg_handlers[msgtype].calcit = NULL;
break;
default:
printk(KERN_ERR"commnl_unregister failed,invalid protocl %d\r\n",protocol);
return -1;
break;
}
printk(KERN_DEBUG"commnl_unregister sucess msgtype %d,protocl %d.\r\n",msgtype,protocol);
return 0;
}
int cfg_msgtype_register( int msgtype,commnl_doit_func doit,
commnl_dumpit_func dumpit,commnl_calcit_func calcit)
{
return(commnl_register(NETLINK_COMMCFG, msgtype,
doit, dumpit,calcit));
}
int pdeliv_msgtype_register( int msgtype,commnl_doit_func doit,
commnl_dumpit_func dumpit,commnl_calcit_func calcit)
{
return(commnl_register(NETLINK_PDELIVERY, msgtype,
doit, dumpit,calcit));
}
int cfg_msgtype_unregister(int msgtype)
{
return(commnl_unregister(NETLINK_COMMCFG,msgtype));
}
int pdeliv_msgtype_unregister(int msgtype)
{
return(commnl_unregister(NETLINK_PDELIVERY,msgtype));
}
void printk_ipaddress(unsigned int address)
{
unsigned int ipa = 0x00000000000000ff;
unsigned int ipb = 0x000000000000ff00;
unsigned int ipc = 0x0000000000ff0000;
unsigned int ipd = 0x00000000ff000000;
printk(KERN_INFO "%u.%u.%u.%u",address&ipa,(address&ipb)>>8,(address&ipc)>>16,(address&ipd)>>24);
return;
}
int nl_cfg_data_ready(struct sk_buff *skb, struct nlmsghdr *nlh, struct netlink_ext_ack *extack)
{
printk(KERN_DEBUG "nl_cfg_data_ready() nlmsg_type = %d begin.\n",nlh->nlmsg_type);
if(NULL != cfgnl_msg_handlers[nlh->nlmsg_type].doit)
{
cfgnl_msg_handlers[nlh->nlmsg_type].doit(skb, nlh);
}
else
{
printk(KERN_WARNING "no doit fun register with nlmsg_type = %d .\n",nlh->nlmsg_type);
}
printk(KERN_DEBUG "nl_cfg_data_ready() nlmsg_type = %d end.\n",nlh->nlmsg_type);
return 0;
}
int nl_pdeliv_data_ready(struct sk_buff *skb, struct nlmsghdr *nlh, struct netlink_ext_ack *extack)
{
printk(KERN_DEBUG "nl_pdeliv_data_ready() nlmsg_type = %d begin.\n",nlh->nlmsg_type);
if(NULL != pdelivnl_msg_handlers[nlh->nlmsg_type].doit)
{
pdelivnl_msg_handlers[nlh->nlmsg_type].doit(skb, nlh);
}
else
{
printk(KERN_WARNING "no doit fun register with nlmsg_type = %d .\n",nlh->nlmsg_type);
}
printk(KERN_DEBUG "nl_pdeliv_data_ready() nlmsg_type = %d end.\n",nlh->nlmsg_type);
return 0;
}
static void libcfgnl_rcv(struct sk_buff *skb)
{
printk(KERN_DEBUG "libcfgnl_rcv:\n");
netlink_rcv_skb(skb, &nl_cfg_data_ready);
}
static void libpdelivnl_rcv(struct sk_buff *skb)
{
printk(KERN_DEBUG "libpdelivnl_rcv:\n");
netlink_rcv_skb(skb, &nl_pdeliv_data_ready);
}
int libnetlinkk_init_byproto(struct netlinkk_cfg *g_nlcfg)
{
printk(KERN_CRIT "libnetlinkk_init:\n");
if(g_nlcfg->subscriptions == NETLINK_PDELIVERY)
{
g_nlcfg->cfg.input = libpdelivnl_rcv;
}
else if(g_nlcfg->subscriptions == NETLINK_COMMCFG )
{
g_nlcfg->cfg.input = libcfgnl_rcv;
}
else
{
printk(KERN_INFO"libnetlinkk_init_byproto invalid subscriptions %d",g_nlcfg->subscriptions);
return -1;
}
g_nlcfg->cfg.flags = NL_CFG_F_NONROOT_RECV;
g_nlcfg->user_pid = -1;
//g_nlcfg->sk = -1;
g_nlcfg->pkt_delev_num = 0;
g_nlcfg->sk = netlink_kernel_create(&init_net, g_nlcfg->subscriptions, &(g_nlcfg->cfg));
if (!g_nlcfg->sk)
{
printk(KERN_CRIT "pcapk_nl create init socket faild!\n");
return 0;
}
return 0;
}
void libnetlinkk_exit(struct netlinkk_cfg *g_nlcfg)
{
printk(KERN_CRIT "libnetlinkk_exit...\n");
netlink_kernel_release(g_nlcfg->sk);
return;
}
/**
* nlmsg_unicast - unicast a netlink message
* @sk: netlink socket to spread message to
* @skb: netlink message as socket buffer
* @portid: netlink portid of the destination socket
*/
int commnl_unicast(struct sock *sk, struct sk_buff *skb, u32 portid)
{
int err;
err = netlink_unicast(sk, skb, portid, MSG_DONTWAIT);
if (err > 0)
err = 0;
return err;
}

View File

@ -0,0 +1,77 @@
#include <linux/module.h>
#include <linux/netlink.h>
#include <linux/netfilter.h>
#include <linux/ip.h>
#include <uapi/linux/netfilter_ipv4.h>
#include <uapi/linux/ip.h>
#include <net/netlink.h>
#include <net/net_namespace.h>
struct netlinkk_cfg
{
struct netlink_kernel_cfg cfg;
struct sock *sk; //内核端socket
int user_pid;/*用户态pid*/
unsigned int subscriptions;/*netlink总线netlink协议号*/
unsigned int groups;/*netlink 组播组*/
//struct upmnl_link *upmnl_msg_handlers[UPMNL_FAMILY_MAX + 1];
unsigned int pkt_delev_num;/*上送的包数量*/
};
typedef int (*commnl_doit_func)(struct sk_buff *, struct nlmsghdr *);
typedef int (*commnl_dumpit_func)(struct sk_buff *, struct netlink_callback *);
typedef u16 (*commnl_calcit_func)(struct sk_buff *, struct nlmsghdr *);
struct commnl_msgtype_process
{
int msgtype;
commnl_doit_func doit;
commnl_dumpit_func dumpit;
commnl_calcit_func calcit;
};
void printk_mem(unsigned char * p,int len);
void printk_ipaddress(unsigned int address);
int pdeliv_msgtype_register( int msgtype,commnl_doit_func doit,
commnl_dumpit_func dumpit,commnl_calcit_func calcit);
/****************************************************************/
/*函数功能:注册内核态配置接收消息处理函数。 */
/*输入参数: */
/*msgtype:注册的消息类型。*/
/*doit:对应的消息接收处理函数;*/
/*dumpit,calcit:框架预留接口暂未使用。填NULL.*/
/*输出参数: WU */
/*返回值0注册成功< 0,失败 */
/****************************************************************/
int cfg_msgtype_register( int msgtype,commnl_doit_func doit,
commnl_dumpit_func dumpit,commnl_calcit_func calcit);
int cfg_msgtype_unregister(int msgtype);
int pdeliv_msgtype_unregister(int msgtype);
/****************************************************************/
/*函数功能:在内核态创建报文上送通道,上送的 */
/*报文内容从链路层头开始。一个netlink协议号只能创建一个通道。 */
/*输入参数g_nlcfg */
/*输出参数: struct upmnl_handle * upmh ,存放创建的通道相关信息。*/
/*返回值0通道创建成果< 0,失败 */
/****************************************************************/
int libnetlinkk_init_byproto(struct netlinkk_cfg *g_nlcfg);
void libnetlinkk_exit(struct netlinkk_cfg *g_nlcfg);
int commnl_unicast(struct sock *sk, struct sk_buff *skb, u32 portid);

View File

@ -0,0 +1,17 @@
#Makefile 2.6
export HUACHENG_LINUX_KERNEL
DEFINES :=
INCLUDE := -I../lib -I../common
LIB :=
obj-m :=pdeliv.o
pdeliv-objs := pdeliverynl_kinit.o
KERNEL :=$(HUACHENG_LINUX_KERNEL)
PWD :=$(shell pwd)
modules :
echo $HUACHENG_LINUX_KERNEL
$(MAKE) $(INCLUDE) -C $(KERNEL) M=$(PWD) modules
.PHONEY:clean
clean :
rm -rf *.o *.mod.c *.ko *.symvers *.order *.makers .*.cmd *.cmd .tmp_versions .cache.mk

View File

@ -0,0 +1,163 @@
#include <linux/module.h>
#include <linux/netlink.h>
#include <linux/netfilter.h>
#include <linux/ip.h>
#include <uapi/linux/netfilter_ipv4.h>
#include <uapi/linux/ip.h>
#include <net/netlink.h>
#include <net/net_namespace.h>
#include "../netlink_api/libnetlink_k.h"
#include "../../../Common/commuapinl.h"
unsigned int pdelivery_hook_func(void *priv,
struct sk_buff *skb,
const struct nf_hook_state *state);
struct netlinkk_cfg g_nlcfg = {0};
static struct nf_hook_ops upm_nfho = {
.hook = pdelivery_hook_func,
.hooknum = 2, /* should be NF_IP_FORWARD,use NF_IP_LOCAL_IN=1 for test */
.pf = PF_INET,
.priority = NF_IP_PRI_FILTER,
};
int pdeliv_rcv_stat(struct sk_buff *skb, struct nlmsghdr *nlh)
{
printk(KERN_INFO "pdeliv_rcv_stat, From: %d\n", nlh->nlmsg_pid);
return 0;
}
int __init pdelivery_init(void)
{
int ret = -1;
printk(KERN_CRIT "nl_upm initialed ok!\n");
/*init for pdelivery module*/
g_nlcfg.groups = PDELIVERY_NLGRP_MAX;
g_nlcfg.subscriptions = NETLINK_PDELIVERY;
//g_nlcfg.cfg.input = libnetlink_rcv;
ret = libnetlinkk_init_byproto(&g_nlcfg);
if(ret < 0)
{
printk (KERN_CRIT "pdelivery_init netlink init fail!.\n");
return ret;
}
/*init the netfilter hook for upm*/
printk (KERN_INFO "upm register netfilter module.\n");
nf_register_net_hook (&init_net,&upm_nfho);
/*do msg process register*/
pdeliv_msgtype_register(PDNLGRP_REQUEST,pdeliv_rcv_stat,NULL,NULL);
return 0;
}
void __exit pdelivery_exit(void)
{
printk(KERN_CRIT "nl_upm existing...\n");
libnetlinkk_exit(&g_nlcfg);
/*init the netfilter hook for upm*/
printk (KERN_INFO "upm unregister netfilter module.\n");
nf_unregister_net_hook (&init_net,&upm_nfho);
return;
}
/****************************************************************/
/*函数功能pdelivery模块注册的netfilter钩子回调函数用于从 */
/*netfilter框架接收到报文然后调用netlink发送接口将报文上送到*/
/*用户态。报文内容从链路层头开始。 */
/*输入参数priv优先级skb 报文statenetfilter钩子状态。 */
/*输出参数: 无*/
/*返回值固定返回NF_ACCEPT表示放行报文。 */
/****************************************************************/
unsigned int pdelivery_hook_func(void *priv,
struct sk_buff *skb,
const struct nf_hook_state *state)
{
void *payload;
struct sk_buff *out_skb;
void *out_payload;
struct nlmsghdr *out_nlh;
int payload_len; // with padding, but ok for echo
struct iphdr *iph;
int ret = -1;
iph = ip_hdr(skb);
printk(KERN_INFO "pdelivery_hook_func:pktlen=%d,mac_header = %d IP:",skb->len,skb->mac_len);
printk_ipaddress(iph->saddr);
printk(KERN_INFO "-->");
printk_ipaddress(iph->daddr);
payload = skb_mac_header(skb);
payload_len = skb->len + skb->mac_len;/**/
out_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); //分配足以存放默认大小的sk_buff
if (!out_skb) goto failure;
out_nlh = nlmsg_put(out_skb, 0, 0, NLMSG_RECV_RAW_PKT, payload_len, 0); //填充协议头数据
if (!out_nlh) goto failure;
out_payload = nlmsg_data(out_nlh);
memcpy(out_payload, payload,payload_len);/**/
#if 1
printk(KERN_INFO "%02x %02x %02x %02x %02x %02x %02x %02x\r\n",
*((char*)out_payload),*((char*)out_payload+1),
*((char*)out_payload+2),*((char*)out_payload+3),
*((char*)out_payload+4),*((char*)out_payload+5),
*((char*)out_payload+6),*((char*)out_payload+7));
#endif
ret = nlmsg_multicast(g_nlcfg.sk,out_skb,0,PDNLGRP_ALLRAW,0);
if (ret < 0)
{
printk(KERN_INFO "Error while sending pkt to user, err id: %d\n", ret);
}
g_nlcfg.pkt_delev_num ++;
printk(KERN_INFO "pdelivery_hook_func() end.\n");
return NF_ACCEPT;/*must return a value*/
failure:
printk(KERN_INFO " failed in pdelivery_hook_func!\n");
return NF_ACCEPT;/*must return a value*/
}
module_init(pdelivery_init);
module_exit(pdelivery_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("a simple example for upm(user policy manage) netlink protocal family");
MODULE_AUTHOR("RSLjdkt");

View File

@ -0,0 +1,28 @@
#include <linux/module.h>
#include <linux/vermagic.h>
#include <linux/compiler.h>
MODULE_INFO(vermagic, VERMAGIC_STRING);
MODULE_INFO(name, KBUILD_MODNAME);
__visible struct module __this_module
__attribute__((section(".gnu.linkonce.this_module"))) = {
.name = KBUILD_MODNAME,
.init = init_module,
#ifdef CONFIG_MODULE_UNLOAD
.exit = cleanup_module,
#endif
.arch = MODULE_ARCH_INIT,
};
#ifdef RETPOLINE
MODULE_INFO(retpoline, "Y");
#endif
static const char __module_depends[]
__used
__attribute__((section(".modinfo"))) =
"depends=";
MODULE_INFO(srcversion, "AFBB76601AC352A20DE7026");

0
Platform/user/README Executable file
View File

View File

@ -0,0 +1,44 @@
######################################
#
######################################
#source file
#源文件,自动找所有.c和.cpp文件并将目标定义为同名.o文件
SOURCE := $(wildcard *.c) $(wildcard *.cpp)
OBJS := $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCE)))
#target you can change test to what you want
#目标文件名,输入任意你想要的执行文件名
TARGET := cfgchannel_sample
#compile and lib parameter
#编译参数
CC := gcc
LIBS := -L../lib -lnetlinku
LDFLAGS :=
DEFINES :=
INCLUDE := -I../lib -I../../common
CFLAGS := -g -Wall -O3 $(DEFINES) $(INCLUDE)
CXXFLAGS:= $(CFLAGS) -DHAVE_CONFIG_H
#i think you should do anything here
#下面的基本上不需要做任何改动了
.PHONY : everything objs clean veryclean rebuild
everything : $(TARGET)
all : $(TARGET)
objs : $(OBJS)
rebuild: veryclean everything
clean :
rm -fr *.so
rm -fr *.o
veryclean : clean
rm -fr $(TARGET)
$(TARGET) : $(OBJS)
$(CC) $(CXXFLAGS) -o $@ $(OBJS) $(LDFLAGS) $(LIBS)

View File

@ -0,0 +1,136 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <linux/types.h>
#include <errno.h>
#include "libnetlinku.h"
#include "policyconf.h"
#include "commuapinl.h"
struct upmnl_handle cfgnlh;
#if 0
static int process_msg(struct pdelivnl_ctrl_data *ctrl,
struct nlmsghdr *n, void *arg)
{
// FILE *fp = arg;
printf("pdelivery main process_msg begin:\r\n");
//if (timestamp)
//print_timestamp(fp);
switch (n->nlmsg_type) {
case NLMSG_RECV_RAW_PKT:
printf("netlink msg type RAW_pkt:\r\n");
printf_pkt(NLMSG_DATA(n),n->nlmsg_len-NLMSG_HDRLEN);
break;
default:
return 0;
}
return 0;
}
#endif
int set_user_policy()
{
unsigned char srcip[64]={"192.168.1.1"};/*需要发送的数据*/
int srcip_len = strlen((const char *)srcip);
struct {
struct nlmsghdr n;
struct policyconfmsg ncm;
char buf[1024];
} req = {
.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct policyconfmsg)),
.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
.n.nlmsg_type = COMMNMSG_POLICYCONF,
.ncm.policy_id = 17,
.n.nlmsg_pid = getpid(),
};
/*可选属性*/
commnl_addattr_l(&req.n, sizeof(req), POLICYCONFA_SRCIP, srcip, srcip_len);
printf("srcip_len =%d.req.n.nlmsg_type =%d\r\n",srcip_len,req.n.nlmsg_type);
/*发送组装好的netlink消息*/
if (commcfg_talk(&req.n, NULL) < 0)
return -2;
return 0;
}
int set_user_policy2()
{
unsigned char srcip[64]={"192.168.1.1"};/*需要发送的数据*/
int srcip_len = strlen((const char *)srcip);
struct {
struct nlmsghdr n;
struct policyconfmsg ncm;
char buf[1024];
} req = {
.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct policyconfmsg)),
.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
.n.nlmsg_type = COMMNMSG_POLICYCONF,
.ncm.policy_id = 17,
};
/*可选属性*/
commnl_addattr_l(&req.n, sizeof(req), POLICYCONFA_SRCIP, srcip, srcip_len);
printf("srcip_len =%d.req.n.nlmsg_type =%d\r\n",srcip_len,req.n.nlmsg_type);
/*发送组装好的netlink消息*/
if (commcfg_talk(&req.n, NULL) < 0)
return -2;
return 0;
}
int main( int argc, char **argv)
{
int ret = -1;
printf("cfgchannel main begin:\r\n");
/*创建通道*/
ret = commcfgnl_open();
if(ret < 0)
{
printf("pdelivnl_open fail,exit.\r\n");
return -1;
}
/*下发配置到内核态*/
ret = set_user_policy();
if(ret < 0)
{
printf("set_user_policy failed.\r\n");
return -1;
}
/*关闭netlink通道*/
commcfgnl_close();
printf("cfgchannel main exit!\r\n");
return 0;
}

View File

@ -0,0 +1,17 @@
LIBDIR = -L/usr/local/lib
PTHREAD = -lpthread
INC = -I/usr/local/include -I../../common
CC = gcc
CFLAGS = -Isub -Iadd
OBJS = libnetlinku.o
TARGET = libnetlinku.so
RM = rm -f
$(TARGET):$(OBJS)
$(CC) $(INC) -fPIC -shared -o $(TARGET) $(OBJS) $(CFLAGS)
@echo 'copy libnetlinku.so to /usr/lib'
cp libnetlinku.so /usr/lib
$(OBJS):%.o:%.c
$(CC) $(INC) -fPIC -c $(CFLAGS) $< -o $@
clean:
-$(RM) $(TARGET) $(OBJS)

View File

@ -0,0 +1,718 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <fcntl.h>
#include <net/if_arp.h>
#include <linux/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <sys/uio.h>
#include "libnetlinku.h"
#include "commuapinl.h"
#define NETLINK_COMM_FAMILY_NUM (NETLINK_COMMMAX_MAX_FAMILY - NETLINK_COMMMAX_MIN_FAMILY -1)
#define NETLINK_COMM_FAMILY_INDEX(family) (family - NETLINK_COMMMAX_MIN_FAMILY -1)
struct upmnl_handle * gcommnl[NETLINK_COMM_FAMILY_NUM][COMMLIBNLGRP_MAX] = {0};/*包含单播和组播一起组0是单播1开始是组播*/
int rcvbuf = 1024 * 1024;
struct upmnl_handle * commnl_get_handle(int protocol,unsigned int group)
{
struct upmnl_handle * handle = NULL;
handle = gcommnl[NETLINK_COMM_FAMILY_INDEX(protocol)][group];
//printf("get handle proto %d,group%d,handle = %p.\r\n",NETLINK_COMM_FAMILY_INDEX(protocol),group,handle);
return handle;
}
void commnl_del_handle(int protocol,unsigned int group)
{
gcommnl[NETLINK_COMM_FAMILY_INDEX(protocol)][group] = NULL;
return;
}
int commnl_add_handle(struct upmnl_handle * handle,int protocol,unsigned int group)
{
if(commnl_get_handle(protocol,group) != NULL)
{
printf("handel already exist,proto:%d,group%d.\r\n",protocol,group);
return -1;
}
//printf("add handle %p proto %d,group%d\r\n",handle,NETLINK_COMM_FAMILY_INDEX(protocol),group);
gcommnl[NETLINK_COMM_FAMILY_INDEX(protocol)][group]= handle;
return 0;
}
void printf_mem(unsigned char * p,int len)
{
int num = 0;
for(num = 0; num<len; num++)
{
printf("%02x ",*(p+num));
if(((num+1)%16 == 0) || ((num+1 ) == len))
{
printf("\r\n");
}
}
return;
}
void test_handle_add_get()
{
struct upmnl_handle handle ={
.proto = NETLINK_COMMCFG};
struct upmnl_handle *phandle =NULL;
int ret = -1;
/*test*/
ret = commnl_add_handle(&handle,NETLINK_COMMCFG,0);
if(ret < 0)
printf("commnl_add_handle failed.\r\n");
phandle = commnl_get_handle(NETLINK_COMMCFG,0);
if(phandle == NULL)
printf("commnl_get_handle faild\r\n.");
commnl_del_handle(NETLINK_COMMCFG,0);
phandle = NULL;
phandle = commnl_get_handle(NETLINK_COMMCFG,0);
if(phandle == NULL)
printf("commnl_get_handle faild\r\n.");
return ;
}
int commnl_addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
int alen)
{
int len = RTA_LENGTH(alen);
struct rtattr *rta;
if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
fprintf(stderr,
"addattr_l ERROR: message exceeded bound of %d\n",
maxlen);
return -1;
}
rta = NLMSG_TAIL(n);
rta->rta_type = type;
rta->rta_len = len;
if (alen)
memcpy(RTA_DATA(rta), data, alen);
n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
return 0;
}
int commnl_addattr(struct nlmsghdr *n, int maxlen, int type)
{
return commnl_addattr_l(n, maxlen, type, NULL, 0);
}
int commnl_addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data)
{
return commnl_addattr_l(n, maxlen, type, &data, sizeof(__u8));
}
int commnl_addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data)
{
return commnl_addattr_l(n, maxlen, type, &data, sizeof(__u16));
}
int commnl_addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data)
{
return commnl_addattr_l(n, maxlen, type, &data, sizeof(__u32));
}
int commnl_addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data)
{
return commnl_addattr_l(n, maxlen, type, &data, sizeof(__u64));
}
int commnl_addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *str)
{
return commnl_addattr_l(n, maxlen, type, str, strlen(str)+1);
}
int commnl_open_byproto( unsigned int subscriptions,int protocol)
{
socklen_t addr_len;
int sndbuf = 32768;
int one = 1;
struct upmnl_handle * upmh = NULL;
struct timeval timeout={1,10000};//10ms
printf("commnl_open_byproto begin:protocol=%d,subscriptions=%d\r\n",protocol,subscriptions);
upmh = calloc(1,sizeof(struct upmnl_handle) );
if(upmh == NULL)
{
printf("commnl_new_handle calloc mem error protocol:%d,group:%d.\r\n",protocol,subscriptions);
return -1;
}
upmh->local.nl_family = AF_NETLINK;
upmh->local.nl_groups = subscriptions;
upmh->local.nl_pid = getpid();
upmh->proto = protocol;
upmh->fd = socket(AF_NETLINK, SOCK_RAW, protocol);
if (upmh->fd < 0) {
perror("Cannot open netlink socket");
goto ERR;
}
if (setsockopt(upmh->fd, SOL_SOCKET, SO_SNDBUF,
&sndbuf, sizeof(sndbuf)) < 0) {
perror("SO_SNDBUF");
goto ERR;
}
if (setsockopt(upmh->fd, SOL_SOCKET, SO_RCVBUF,
&rcvbuf, sizeof(rcvbuf)) < 0) {
perror("SO_RCVBUF");
goto ERR;
}
if (setsockopt(upmh->fd, SOL_SOCKET, SO_RCVTIMEO,
&timeout, sizeof(timeout)) < 0) {
perror("SO_RCVTIMEO");
goto ERR;
}
#if 0
/* Older kernels may no support extended ACK reporting */
setsockopt(upmh->fd, SOL_NETLINK, NETLINK_EXT_ACK,
&one, sizeof(one));
#endif
memset(&upmh->local, 0, sizeof(upmh->local));
upmh->local.nl_family = AF_NETLINK;
upmh->local.nl_pid = getpid();
upmh->local.nl_groups = subscriptions;
if (bind(upmh->fd, (struct sockaddr *)&(upmh->local),
sizeof(upmh->local)) < 0) {
perror("Cannot bind netlink socket");
//printf("Cannot bind netlink socket\r\n");
goto ERR;
}
addr_len = sizeof(upmh->local);
if (getsockname(upmh->fd, (struct sockaddr *)&upmh->local,
&addr_len) < 0) {
perror("Cannot getsockname");
goto ERR;
}
if (addr_len != sizeof(upmh->local)) {
fprintf(stderr, "Wrong address length %d\n", addr_len);
goto ERR;
}
if (upmh->local.nl_family != AF_NETLINK) {
fprintf(stderr, "Wrong address family %d\n",
upmh->local.nl_family);
goto ERR;
}
upmh->seq = time(NULL);
commnl_add_handle(upmh, protocol, subscriptions);
printf("commnl_open_byproto succes.\r\n");
return 0;
ERR:
if (upmh->fd >= 0) {
close(upmh->fd);
upmh->fd = -1;
}
if(NULL != upmh){
free(upmh);
}
commnl_del_handle(protocol, subscriptions);
return -1;
}
void commnl_close( unsigned int subscriptions,int protocol)
{
struct upmnl_handle *upmh = commnl_get_handle(protocol,subscriptions);
if (upmh->fd >= 0) {
close(upmh->fd);
upmh->fd = -1;
}
commnl_del_handle(upmh->proto, upmh->local.nl_groups);
free(upmh);
return;
}
int commcfgnl_open()
{
return commnl_open_byproto(0, NETLINK_COMMCFG);
}
void commcfgnl_close()
{
commnl_close(0,NETLINK_COMMCFG);
return;
}
int pdelivnl_open()
{
return commnl_open_byproto(PDNLGRP_ALLRAW, NETLINK_PDELIVERY);
}
void pdelivnl_close()
{
commnl_close(PDNLGRP_ALLRAW,NETLINK_PDELIVERY);
return;
}
int commnl_send(struct upmnl_handle *nl, struct nlmsghdr *n)
{
return 0;
}
static int __commnl_recvmsg(int fd, struct msghdr *msg, int flags)
{
int len;
unsigned int num = COMMNL_TALKRCV_TIMEOUT;/*10秒超时*/
do {
len = recvmsg(fd, msg, flags);
num --;
if(num ==0)
break;
printf("__rtnl_recvmsg return num %d.\r\n",num);
} while (len < 0 && (errno == EINTR || errno == EAGAIN));/*超时*/
if (len < 0) {
fprintf(stderr, "netlink receive error %s (%d)\n",
strerror(errno), errno);
return -errno;
}
if (len == 0) {
fprintf(stderr, "EOF on netlink\n");
return -ENODATA;
}
return len;
}
static int commnl_recvmsg(int fd, struct msghdr *msg, char **answer)
{
struct iovec *iov = msg->msg_iov;
char *buf;
int len;
iov->iov_base = NULL;
iov->iov_len = 0;
len = __commnl_recvmsg(fd, msg, MSG_PEEK | MSG_TRUNC);
if (len < 0)
return len;
if (len < 32768)
len = 32768;
buf = malloc(len);
if (!buf) {
fprintf(stderr, "malloc error: not enough buffer\n");
return -ENOMEM;
}
iov->iov_base = buf;
iov->iov_len = len;
len = __commnl_recvmsg(fd, msg, 0);
if (len < 0) {
free(buf);
return len;
}
if (answer)
*answer = buf;
else
free(buf);
return len;
}
static int __commnl_talk_iov(struct upmnl_handle *rtnl, struct iovec *iov,
size_t iovlen, struct nlmsghdr **answer,
bool show_rtnl_err, nl_ext_ack_fn_t errfn)
{
struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
struct iovec riov;
struct msghdr msg = {
.msg_name = &nladdr,
.msg_namelen = sizeof(nladdr),
.msg_iov = iov,
.msg_iovlen = iovlen,
};
unsigned int seq = 0;
struct nlmsghdr *h;
int i, status;
char *buf;
for (i = 0; i < iovlen; i++) {
h = iov[i].iov_base;
h->nlmsg_seq = seq = ++rtnl->seq;
if (answer == NULL)
h->nlmsg_flags |= NLM_F_ACK;
}
status = sendmsg(rtnl->fd, &msg, 0);
if (status < 0) {
perror("Cannot talk to rtnetlink");
return -1;
}
/* change msg to use the response iov */
msg.msg_iov = &riov;
msg.msg_iovlen = 1;
i = 0;
while (1) {
next:
status = commnl_recvmsg(rtnl->fd, &msg, &buf);
++i;
if (status < 0)
return status;
if (msg.msg_namelen != sizeof(nladdr)) {
fprintf(stderr,
"sender address length == %d\n",
msg.msg_namelen);
exit(1);
}
for (h = (struct nlmsghdr *)buf; status >= sizeof(*h); ) {
int len = h->nlmsg_len;
int l = len - sizeof(*h);
if (l < 0 || len > status) {
if (msg.msg_flags & MSG_TRUNC) {
fprintf(stderr, "Truncated message\n");
free(buf);
return -1;
}
fprintf(stderr,
"!!!malformed message: len=%d\n",
len);
exit(1);
}
/*单播消息pid必须正确*/
if (nladdr.nl_pid != 0 ||
h->nlmsg_pid != rtnl->local.nl_pid ||
h->nlmsg_seq > seq || h->nlmsg_seq < seq - iovlen) {
/* Don't forget to skip that message. */
status -= NLMSG_ALIGN(len);
h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
continue;
}
if (h->nlmsg_type == NLMSG_ERROR) {
struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h);
int error = err->error;
if (l < sizeof(struct nlmsgerr)) {
fprintf(stderr, "ERROR truncated\n");
free(buf);
return -1;
}
if (!error) {
/* check messages from kernel */
//nl_dump_ext_ack(h, errfn);
} else {
errno = -error;
//if (rtnl->proto != NETLINK_SOCK_DIAG && show_rtnl_err)
//rtnl_talk_error(h, err, errfn);
}
if (answer)
*answer = (struct nlmsghdr *)buf;
else
free(buf);
if (i < iovlen)
goto next;
return error ? -i : 0;
}
if (answer) {
*answer = (struct nlmsghdr *)buf;
return 0;
}
fprintf(stderr, "Unexpected reply!!!\n");
status -= NLMSG_ALIGN(len);
h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
}
free(buf);
if (msg.msg_flags & MSG_TRUNC) {
fprintf(stderr, "Message truncated\n");
continue;
}
if (status) {
fprintf(stderr, "!!!Remnant of size %d\n", status);
exit(1);
}
}
}
static int __commnl_talk(struct upmnl_handle *nl, struct nlmsghdr *n,
struct nlmsghdr **answer,
bool show_rtnl_err, nl_ext_ack_fn_t errfn)
{
struct iovec iov = {
.iov_base = n,
.iov_len = n->nlmsg_len
};
return __commnl_talk_iov(nl, &iov, 1, answer, show_rtnl_err, errfn);
}
int commnl_talk(struct upmnl_handle *nl, struct nlmsghdr *n,
struct nlmsghdr **answer)
{
return __commnl_talk(nl, n, answer, true, NULL);
}
int pdeliv_talk(int group,struct nlmsghdr *n, struct nlmsghdr **answer)
{
struct upmnl_handle *nl = NULL;
nl = commnl_get_handle(NETLINK_PDELIVERY,group);
if(nl == NULL)
{
printf("pdeliv_talk faid,pdeliv netlink has not open.\r\n");
return -1;
}
return(commnl_talk(nl,n,answer));
}
int commcfg_talk(struct nlmsghdr *n, struct nlmsghdr **answer)
{
struct upmnl_handle *nl = NULL;
nl = commnl_get_handle(NETLINK_COMMCFG,0);
if(nl == NULL)
{
printf("commcfg_talk faid,commcfg netlink has not open.\r\n");
return -1;
}
return(commnl_talk(nl,n,answer));
}
int pdelivnl_listen(int group,
pdelivnl_listen_filter_t handler,
void *jarg)
{
struct upmnl_handle *nl = NULL;
nl = commnl_get_handle(NETLINK_PDELIVERY,group);
if(nl == NULL)
{
printf("pdelivnl_listen faid,pdeliv netlink has not open.\r\n");
return -1;
}
return(conmnl_listen(nl,handler,jarg));
}
int conmnl_listen(struct upmnl_handle *nl,
pdelivnl_listen_filter_t handler,
void *jarg)
{
int status;
struct nlmsghdr *h;
struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
struct iovec iov;
struct msghdr msg = {
.msg_name = &nladdr,
.msg_namelen = sizeof(nladdr),
.msg_iov = &iov,
.msg_iovlen = 1,
};
char buf[16384];
char cmsgbuf[BUFSIZ];
printf("pdelivnl_listen begin.\r\n");
#if 0
if (nl->flags & RTNL_HANDLE_F_LISTEN_ALL_NSID) {
msg.msg_control = &cmsgbuf;
msg.msg_controllen = sizeof(cmsgbuf);
}
#endif
iov.iov_base = buf;
while (1) {
struct pdelivnl_ctrl_data ctrl;
struct cmsghdr *cmsg;
printf("pdelivnl_listen recvmsg....\r\n");
iov.iov_len = sizeof(buf);
status = recvmsg(nl->fd, &msg, MSG_WAITALL);
if (status < 0) {
if (errno == EINTR || errno == EAGAIN)/*超时*/
continue;
fprintf(stderr, "netlink receive error %s (%d)\n",
strerror(errno), errno);
if (errno == ENOBUFS)
continue;
return -1;
}
if (status == 0) {
fprintf(stderr, "EOF on netlink\n");
return -1;
}
if (msg.msg_namelen != sizeof(nladdr)) {
fprintf(stderr,
"Sender address length == %d\n",
msg.msg_namelen);
exit(1);
}
#if 0
if (nl->flags & RTNL_HANDLE_F_LISTEN_ALL_NSID) {
memset(&ctrl, 0, sizeof(ctrl));
ctrl.nsid = -1;
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
cmsg = CMSG_NXTHDR(&msg, cmsg))
if (cmsg->cmsg_level == SOL_NETLINK &&
cmsg->cmsg_type == NETLINK_LISTEN_ALL_NSID &&
cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
int *data = (int *)CMSG_DATA(cmsg);
ctrl.nsid = *data;
}
}
#endif
#if 1
printf("pdelivnl_listen prase msg:\r\n");
for (h = (struct nlmsghdr *)buf; status >= sizeof(*h); ) {
int err;
int len = h->nlmsg_len;
int l = len - sizeof(*h);
if (l < 0 || len > status) {
//if (msg.msg_flags & MSG_TRUNC) {
//fprintf(stderr, "Truncated message\n");
//return -1;
//}
fprintf(stderr,
"!!!malformed message: len=%d\n",
len);
exit(1);
}
err = handler(&ctrl, h, jarg);
if (err < 0)
return err;
status -= NLMSG_ALIGN(len);
h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
}
#endif
//if (msg.msg_flags & MSG_TRUNC) {
//fprintf(stderr, "Message truncated\n");
//continue;
//}
if (status) {
fprintf(stderr, "!!!Remnant of size %d\n", status);
exit(1);
}
}
}
#if 0
int upnl_dump_type(struct upmnl_handle *upmh, unsigned int type)
{
struct {
struct nlmsghdr nlh;
//struct rtgenmsg g;
} req;
struct sockaddr_nl nladdr;
memset(&nladdr, 0, sizeof(nladdr));
memset(&req, 0, sizeof(req));
nladdr.nl_family = AF_NETLINK;
req.nlh.nlmsg_len = sizeof(req);
req.nlh.nlmsg_type = type;
req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
req.nlh.nlmsg_pid = 0;
//req.nlh.nlmsg_seq = upmh->rtnl_dump = ++(upmh->rtnl_seq);
req.nlh.nlmsg_seq = 1;
//req.g.rtgen_family = AF_INET;
return sendto(upmh->fd, &req, sizeof(req), 0,
(struct sockaddr*)&nladdr, sizeof(nladdr));
}
#endif
int pdeliv_main(pdelivnl_listen_filter_t process_pkt)
{
int ret = -1;
printf("pdeliv_main begin:\r\n");
/*创建通道*/
ret = pdelivnl_open();
if(ret < 0)
{
printf("pdelivnl_open fail,exit.\r\n");
return -1;
}
/*接收报文*/
pdelivnl_listen(PDNLGRP_ALLRAW,process_pkt,NULL);
/*关闭netlink通道*/
pdelivnl_close();
printf("pdeliv_main exit!\r\n");
return 0;
}

View File

@ -0,0 +1,172 @@
#include <stdio.h>
#include <string.h>
#include <asm/types.h>
#include <linux/netlink.h>
/*
Generic structure for encapsulation of optional route information.
It is reminiscent of sockaddr, but with sa_family replaced
with attribute type.
*/
#define COMMNL_TALKRCV_TIMEOUT (1) /*超时时间10毫秒*1次*/
struct rtattr {
unsigned short rta_len;
unsigned short rta_type;
};
/* Macros to handle rtattributes */
#define RTA_ALIGNTO 4U
#define RTA_ALIGN(len) ( ((len)+RTA_ALIGNTO-1) & ~(RTA_ALIGNTO-1) )
#define RTA_OK(rta,len) ((len) >= (int)sizeof(struct rtattr) && \
(rta)->rta_len >= sizeof(struct rtattr) && \
(rta)->rta_len <= (len))
#define RTA_NEXT(rta,attrlen) ((attrlen) -= RTA_ALIGN((rta)->rta_len), \
(struct rtattr*)(((char*)(rta)) + RTA_ALIGN((rta)->rta_len)))
#define RTA_LENGTH(len) (RTA_ALIGN(sizeof(struct rtattr)) + (len))
#define RTA_SPACE(len) RTA_ALIGN(RTA_LENGTH(len))
#define RTA_DATA(rta) ((void*)(((char*)(rta)) + RTA_LENGTH(0)))
#define RTA_PAYLOAD(rta) ((int)((rta)->rta_len) - RTA_LENGTH(0))
#define NLMSG_TAIL(nmsg) \
((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
#define NEW_NL_MSG(msg_type,datasize) \
malloc();
typedef int (*nl_ext_ack_fn_t)(const char *errmsg, uint32_t off,
const struct nlmsghdr *inner_nlh);
struct pdelivnl_ctrl_data {
int nsid;
};
struct upmnl_handle {
int fd;
struct sockaddr_nl local;/*里面包含协议号加入的组pid*/
struct sockaddr_nl peer;
int proto;
__u32 seq;/*目前发送消息的序列号*/
//__u32 dump;
//FILE *dump_fp;
//#define RTNL_HANDLE_F_LISTEN_ALL_NSID 0x01
//#define RTNL_HANDLE_F_SUPPRESS_NLERR 0x02
//#define RTNL_HANDLE_F_STRICT_CHK 0x04
int flags;
};
/*应用模块提供的报文处理回调函数原型*/
typedef int (*pdelivnl_listen_filter_t)(struct pdelivnl_ctrl_data *,
struct nlmsghdr *n, void *);
void printf_mem(unsigned char * p,int len);
/****************************************************************/
/*函数功能给netlink消息添加可选的ATTR属性。*/
/*输入参数: */
/*n需要添加attr属性的netlink消息头*/
/*maxlen整个netlink消息可以空间的大小*/
/*type添加的attr的类型*/
/*data添加的attr的值*/
/*输出参数: 无*/
/*返回值0通道创建成果< 0,失败 */
/****************************************************************/
int commnl_addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
int alen);
int commnl_addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data);
int commnl_addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data);
int commnl_addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data);
int commnl_addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data);
int commnl_addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *str);
/****************************************************************/
/*函数功能:创建一个新的内核配置下发通道,给内核态的安全接入模块下发配置, */
/*可以支持多实例,即多个用户态模块可以同时给内核态的安全接入模块下发配置。*/
/*输入参数:无 */
/*输出参数: struct upmnl_handle * upmh ,存放创建的通道相关信息。*/
/*返回值0通道创建成果< 0,失败 */
/****************************************************************/
int commcfgnl_open();
void commcfgnl_close();
/****************************************************************/
/*函数功能创建一个新的包接收通道接收需要上送DPI的原始报文 */
/*报文内容从链路层头开始。可以支持多实例,即多个收包模块可以同时*/
/*收到同一份报文的拷贝。 */
/*输入参数:无 */
/*输出参数: struct upmnl_handle * upmh ,存放创建的通道相关信息。*/
/*返回值0通道创建成果< 0,失败 */
/****************************************************************/
int pdelivnl_open();
void pdelivnl_close();
//int upnl_dump_type(struct upmnl_handle *upmh, unsigned int type);
/****************************************************************/
/*函数功能:在已经创建好的包接收通道上,循环接收报文。*/
/*输入参数:*/
/*nl监听的通道所属的组。 */
/*handler报文处理回调函数在接收到报文时调用。 */
/*jarg :用户自定义上下文信息在报文处理回调函数调用是会将jarg当做参数传入。*/
/*输出参数: 无*/
/*返回值0成功< 0,失败 */
/****************************************************************/
int pdelivnl_listen(int group,
pdelivnl_listen_filter_t handler,
void *jarg);
/****************************************************************/
/*函数功能:在已经创建好的包接收通道上,循环接收报文。*/
/*输入参数:*/
/*nl监听的通道。 */
/*handler报文处理回调函数在接收到报文时调用。 */
/*jarg :用户自定义上下文信息在报文处理回调函数调用是会将jarg当做参数传入。*/
/*输出参数: 无*/
/*返回值0成功< 0,失败 */
/****************************************************************/
int conmnl_listen(struct upmnl_handle *nl,
pdelivnl_listen_filter_t handler,
void *jarg);
/****************************************************************/
/*函数功能:发送配置消息,并同步等待内核应答。*/
/*输入参数:*/
/*nl监听的通道。 */
/*n:需要发送的配置消息。 */
/*输出参数: */
/*answer :接收到内核应答消息。*/
/*返回值0成功< 0,失败 */
/****************************************************************/
int commnl_talk(struct upmnl_handle *nl, struct nlmsghdr *n,
struct nlmsghdr **answer);
int commcfg_talk(struct nlmsghdr *n, struct nlmsghdr **answer);
/****************************************************************/
/*函数功能:发送配置消息,不等待内核应答。*/
/*输入参数:*/
/*nl发送的通道。 */
/*n:需要发送的配置消息。 */
/*输出参数: */
/*返回值0成功< 0,失败 */
/****************************************************************/
int commnl_send(struct upmnl_handle *nl, struct nlmsghdr *n);
int pdeliv_main(pdelivnl_listen_filter_t process_pkt);

View File

@ -0,0 +1,44 @@
######################################
#
######################################
#source file
#源文件,自动找所有.c和.cpp文件并将目标定义为同名.o文件
SOURCE := $(wildcard *.c) $(wildcard *.cpp)
OBJS := $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCE)))
#target you can change test to what you want
#目标文件名,输入任意你想要的执行文件名
TARGET := pdeliv_main
#compile and lib parameter
#编译参数
CC := gcc
LIBS := -L../lib -lnetlinku
LDFLAGS :=
DEFINES :=
INCLUDE := -I../lib -I../../common
CFLAGS := -g -Wall -O3 $(DEFINES) $(INCLUDE)
CXXFLAGS:= $(CFLAGS) -DHAVE_CONFIG_H
#i think you should do anything here
#下面的基本上不需要做任何改动了
.PHONY : everything objs clean veryclean rebuild
everything : $(TARGET)
all : $(TARGET)
objs : $(OBJS)
rebuild: veryclean everything
clean :
rm -fr *.so
rm -fr *.o
veryclean : clean
rm -fr $(TARGET)
$(TARGET) : $(OBJS)
$(CC) $(CXXFLAGS) -o $@ $(OBJS) $(LDFLAGS) $(LIBS)

View File

@ -0,0 +1,47 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <linux/types.h>
#include <errno.h>
#include "libnetlinku.h"
#include "commuapinl.h"
static int process_pkt(struct pdelivnl_ctrl_data *ctrl,
struct nlmsghdr *n, void *arg)
{
printf("pdelivery main process_msg begin:\r\n");
//if (timestamp)
//print_timestamp(fp);
switch (n->nlmsg_type) {
case NLMSG_RECV_RAW_PKT:
printf("netlink msg type RAW_pkt:\r\n");
printf_mem(NLMSG_DATA(n),n->nlmsg_len-NLMSG_HDRLEN);
break;
default:
return 0;
}
return 0;
}
int main( int argc, char **argv)
{
int ret = -1;
ret = pdeliv_main(process_pkt);
}

11
Product/build/Readme.md Executable file
View File

@ -0,0 +1,11 @@
# **编译配置文件以及脚本文件夹**
+ **功能简介**
&nbsp;
&emsp;&emsp;存放 Makefile 编译脚本
&nbsp;
&nbsp;
+ **目录结构**
&nbsp;
+ **编译方法**
&nbsp;
&nbsp;

View File

@ -0,0 +1,49 @@
# target name, the target name must have the same name of c source file
TARGET_NAME=demo
# target
# for linux module driver: KO
# for application: EXE
# for dynamic library: DLL
TARGET_TYPE = KO
# target object
# for application: APP
# for device driver: DRV
TARGET_OBJ = DRV
# custom install dir
TARGET_BOX =
#debug mode or release mode
DEBUG = TRUE
PLAT_LINUX ?= TRUE
PLAT_ARM64 ?= TRUE
VPATH = ../modules/demo
# source code
# set the source file, don't used .o because of ...
COMMON_SRCS = main.c
# MRS Board Source Files
PLAT_LINUX_SRCS = $(COMMON_SRCS)
PLAT_ARM64_SRCS = $(COMMON_SRCS)
# gcc CFLAGS
# this line must be at below of thus, because of...
include ../../Common/common.Makefile
ifeq ($(MAKECMDGOALS), )
$(shell find ./ -name "$(TARGET)-*.ko" -delete)
else
ifeq ($(MAKECMDGOALS), all)
$(shell find ./ -name "$(TARGET)-*.ko" -delete)
endif
endif

View File

@ -0,0 +1,55 @@
# target name, the target name must have the same name of c source file
TARGET_NAME=demo
# target
# for linux module driver: KO
# for application: EXE
# for dynamic library: DLL
TARGET_TYPE = EXE
# target object
# for application: APP
# for device driver: DRV
TARGET_OBJ = APP
# custom install dir
TARGET_BOX =
#debug mode or release mode
DEBUG = TRUE
PLAT_LINUX ?= TRUE
PLAT_ARM64 ?= TRUE
VPATH = ../user/demo
# source code
# set the source file, don't used .o because of ...
# MRS Board Source Files
PLAT_LINUX_SRCS := main.c
PLAT_ARM64_SRCS := $(PLAT_LINUX_SRCS)
# gcc CFLAGS
PLAT_LINUX_CFLAGS += -DVERSION=\"v0.0.0.1\"
PLAT_ARM64_CFLAGS += -DVERSION=\"v0.0.0.1\"
# this line must be at below of thus, because of...
include ../../Common/common.Makefile
ifneq ($(MAKECMDGOALS), clean)
ifneq ($(MAKECMDGOALS), cleanall)
ifneq ($(notdir $(DEPEND_LIB)), $(wildcard $(DEPEND_LIB)))
$(shell $(CP) $(DEPEND_LIB) ./)
endif
endif
endif
ifeq ($(MAKECMDGOALS), )
$(shell find ./ -name $(TARGET)-*.exe -delete)
else
ifeq ($(MAKECMDGOALS), all)
$(shell find ./ -name "$(TARGET)-*.exe" -delete)
endif
endif

11
Product/common/Readme.md Executable file
View File

@ -0,0 +1,11 @@
# **安全网关产品侧公共内容文件夹**
+ **功能简介**
&nbsp;
&emsp;&emsp;存放安全网关产品侧用户态、内核态公共代码和配置内容
&nbsp;
&nbsp;
+ **目录结构**
&nbsp;
+ **编译方法**
&nbsp;
&nbsp;

11
Product/modules/Readme.md Executable file
View File

@ -0,0 +1,11 @@
# **安全网关产品侧内核态相关内容文件夹**
+ **功能简介**
&nbsp;
&emsp;&emsp;存放安全网关产品内核态项目
&nbsp;
&nbsp;
+ **目录结构**
&nbsp;
+ **编译方法**
&nbsp;
&nbsp;

11
Product/modules/demo/Makefile Executable file
View File

@ -0,0 +1,11 @@
ifneq ($(KERNELRELEASE), )
obj-m := main.o
else
#KDIR ?= /opt/fsl-kernel/x86/linux-4.9.140
KDIR ?= /opt/fsl-kernel/arm64/linux-4.9.140
#PWD := $(shell pwd)
all:
make -C $(KDIR) M=$(PWD) modules
clean:
make -C $(KDIR) M=$(PWD) clean
endif

22
Product/modules/demo/main.c Executable file
View File

@ -0,0 +1,22 @@
#ifdef __KERNEL__
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
#define VERSION ("v0.0.0.1")
static int demo_init(void)
{
printk(KERN_ALERT "Hello vBRAS Product user demo version: %s\n", VERSION);
return 0;
}
module_init(demo_init);
static void __exit demo_exit(void)
{
printk(KERN_ALERT "Bye vBRAS Product user demo version: %s\n", VERSION);
}
module_exit(demo_exit);
#endif // __KERNEL__

11
Product/user/Readme.md Executable file
View File

@ -0,0 +1,11 @@
# **编译配置文件以及脚本文件夹**
+ **功能简介**
&nbsp;
&emsp;&emsp;存放 Makefile 编译脚本
&nbsp;
&nbsp;
+ **目录结构**
&nbsp;
+ **编译方法**
&nbsp;
&nbsp;

7
Product/user/demo/main.c Executable file
View File

@ -0,0 +1,7 @@
#include <stdio.h>
int main(int argc, char** argv)
{
fprintf(stdout, "Hello vBRAS Product user demo version: %s\n", VERSION);
return 0;
}

100
Readme.md Executable file
View File

@ -0,0 +1,100 @@
# **安全网关项目产品代码仓库**
## **简介**
&emsp;&emsp;安全网关项目产品侧仓库,主要存放应用与安全网关设备上的应用、内核等相关功能的代码。
## **目录结构**
<pre>
Product 安全网关产品源代码主目录
├── build Makefile和编译脚本目录
├── common 公共代码/头文件/配置文件 目录
├── modules linux内核态源代码目录
│ └── demo 内核态驱动程序 demo 目录
└── user 用户态源代码目录
└── demo 用户态应用程序 demo 目录
</pre>
>#### **注意事项:**
>1. 内核态功能相关代码存放于 modules 目录下
>2. 用户态功能相关代码存放与 user 目录下
>3. 每个独立的功能以子目录方式存放到适当的目录下
>4. user 与 modules 目录中共有的源代码和头文件以及配置文件必须存放到 common 目录中
>5. 不得把相同功能、相同定义、相同配置的内容独立放到各自的 user 和 modules 目录中
## **编译方法**
#### 1. 从仓库获取最新代码
<code>git clone https://git.komect.net:SDX/vBRAS.git</code>
<code>git pull</code>
#### 2. 安装必要软件(UBuntu)
<code>sudo ./fsl-qoriq-glibc-x86_64-fsl-toolchain-aarch64-toolchain-2.4.1.sh
sudo apt-get -y install git u-boot-tools device-tree-compiler autoconf curl flex
sudo apt-get -y install automake dh-autoreconf libssl-dev openssl libpcap-dev bc
sudo apt-get -y install python-pip qemu-utils libncurses5-dev python-crypto bison
</code>
#### 3. 安装 linux 内核源代码
<code>sudo mkdir -p /opt/fsl-kernel /opt/fsl-kernel/arm64 /opt/fsl-kernel/x86
sudo chmod 777 /opt/fsl-kernel -R
cp ./vBRAS/Platform/kernel/linux-4.9.140.tar.gz /opt/fsl-kernel
cd /opt/fsl-kernel
tar -xvaf ./linux-4.9.140.tar.gz ./
cp ./linux-4.9.140 ./x86
cp ./linux-4.9.140 ./arm64
</code>
#### 4. 设置环境变量
在 ~/.bashrc 文件末尾加上以下几行配置
<code>export HUACHENG_LINUX_KERNEL=/opt/fsl-kernel/x86/linux-4.9.140
export HUACHENG_ARM64_KERNEL=/opt/fsl-kernel/arm64/linux-4.9.140
export SDKTARGETSYSROOT=/opt/fsl-qoriq/2.4.1/sysroots/aarch64-fsl-linux
export PATH=/opt/fsl-qoriq/2.4.1/sysroots/x86_64-fslsdk-linux/usr/bin:/opt/fsl-qoriq/2.4.1/sysroots/x86_64-fslsdk-linux/usr/sbin:/opt/fsl-qoriq/2.4.1/sysroots/x86_64-fslsdk-linux/bin:/opt/fsl-qoriq/2.4.1/sysroots/x86_64-fslsdk-linux/sbin:/opt/fsl-qoriq/2.4.1/sysroots/x86_64-fslsdk-linux/usr/bin/../x86_64-fslsdk-linux/bin:/opt/fsl-qoriq/2.4.1/sysroots/x86_64-fslsdk-linux/usr/bin/aarch64-fsl-linux:/opt/fsl-qoriq/2.4.1/sysroots/x86_64-fslsdk-linux/usr/bin/aarch64-fsl-linux-musl:$PATH
source ~/.bashrc
</code>
#### 5. 编译内核
+ x64_86
<code>cd /opt/fsl-kernel/x86/linux-4.9.140 && make -j</code>
+ arm64
<code>cd /opt/fsl-kernel/arm64/linux-4.9.140 && unset LDFLAS</code>
修改 Makefile 文件第 257、258 两行
<code>ARCH ?= $(SUBARCH)
CROSS_COMPILE ?= $(CONFIG_CROSS_COMPILE:"%"=%)</code>
<code>ARCH ?= arm64
CROSS_COMPILE ?= aarch64-fsl-linux-</code>
然后运行 make 命令进行编译
#### 6. 构建系统
+ 构建
<code>cd vBRAS/Product
make</code>
</code>
+ 清理
<code>make OPT=clean</code>
+ 安装
<code>make OPT=install [DIR=../_install]</code>
DIR 环境变量指定了最终安装默认,默认为 ../_install。支持绝对路径以及相对路径。
+ 安装目录结构
<pre>
_install
└── debug 系统构建类型 debug/release
├── targets 构建可执行程序、驱动安装目录
│   ├── ARM64 ARM64 平台的可执行程序、驱动
│   └── LINUX Linux 平台的可执行程序、驱动
└── targets.debug 构建可执行程序、驱动的对应调试符号信息安装目录
├── ARM64 ARM64 平台的可执行程序、驱动调试符号信息
└── LINUX ARM64 平台的可执行程序、驱动调试符号信息
</pre>
+ 编译信息
执行 make 命令进行编译结束后vBRAS/Common/compile.h 文件中保存了当前系统编译信息,可以在代码中直接引用。
<code>#define vBRAS_COMPILE_DATE "2019-05-22"
#define vBRAS_COMPILE_TIME "11:19:58"
#define vBRAS_COMPILE_MAJOR "20190522"
#define vBRAS_COMPILE_SUB "111958"
#define vBRAS_COMPILE_BY "hx"
#define vBRAS_COMPILE_HOST "hx-ubuntu"
#define vBRAS_GIT_TAGS "vBRAS1.0-20181213-stable-159-g1b4c69da2-dev"
#define vBRAS_GIT_VERS "1b4c69da2d9cd6d133075f2fd96fbe0ac220fb72"
</code>
vBRAS_GIT_TAGS 记录了当前源码在 gitlab 服务器上面的分支信息
vBRAS_GIT_VERS 记录了当前源码在 gitlab 服务器上面的版本信息

21
build_env.sh Executable file
View File

@ -0,0 +1,21 @@
#!/bin/sh
UTS_LEN=64
UTS_TRUNCATE="cut -b -$UTS_LEN"
VERSION_FILE=./Common/compile.h
(
echo /\* This file is auto generated,for vBRAS version info \*/
echo /\* Used readelf to get this information form driver of application \*/
echo /\* \"readelf --debug-dump=macro \<filename\>\" \*/
echo \#define vBRAS_COMPILE_DATE \"`date +%F`\"
echo \#define vBRAS_COMPILE_TIME \"`date +%T`\"
echo \#define vBRAS_COMPILE_MAJOR \"`date +%Y%m%d`\"
echo \#define vBRAS_COMPILE_SUB \"`date +%H%M%S`\"
echo \#define vBRAS_COMPILE_BY \"`whoami`\"
echo \#define vBRAS_COMPILE_HOST \"`hostname | $UTS_TRUNCATE`\"
echo \#define vBRAS_GIT_TAGS \"`git describe --tags --always --dirty="-dev"`\"
echo \#define vBRAS_GIT_VERS \"`git rev-parse HEAD`\"
) > $VERSION_FILE

6133
kernel/arm64_config Executable file

File diff suppressed because it is too large Load Diff

BIN
kernel/linux-4.14.83.tar.gz Executable file

Binary file not shown.

9269
kernel/x86.config Executable file

File diff suppressed because it is too large Load Diff