Makesystem for kernel module

This commit is contained in:
Vadim Vetrov 2024-07-31 02:12:59 +03:00
parent 7490658708
commit 79d592a0ca
No known key found for this signature in database
GPG Key ID: E8A308689D7A73A5
12 changed files with 180 additions and 269 deletions

6
.editorconfig Normal file
View File

@ -0,0 +1,6 @@
root = true
[*]
indent_style = tab
indent_size = 8
tab_width = 8

15
.gitignore vendored
View File

@ -4,3 +4,18 @@ build
configure~
# Kernel module files
.Module.*
.modules.*
Module.*
modules.*
.youtubeKblock.*
youtubeKblock.ko
youtubeKblock.mod*
youtubeKblock.o
.mangle.*
mangle.o
.youtubeKUnblock.*
youtubeKUnblock.ko
youtubeKUnblock.mod*
youtubeKUnblock.o

3
Kbuild Normal file
View File

@ -0,0 +1,3 @@
obj-m := youtubeKUnblock.o
youtubeKUnblock-objs := youtubeKblock.o mangle.o
ccflags-y := -std=gnu11 -Wno-unused-variable -DKERNEL_SPACE

View File

@ -1,85 +1,14 @@
BUILD_DIR := $(CURDIR)/build
DEPSDIR := $(BUILD_DIR)/deps
USPACE_TARGETS := default all install uninstall dev run_dev
KMAKE_TARGETS := kmake kload kunload kreload
CC := gcc
LD := gcc
CFLAGS:=-Wall -Wpedantic -Wno-unused-variable -I$(DEPSDIR)/include -Os
LDFLAGS:=-L$(DEPSDIR)/lib -static
.PHONY: $(USPACE_TARGETS) $(KMAKE_TARGETS) clean
$(USPACE_TARGETS):
@$(MAKE) -f uspace.mk $@
# PREFIX is environment variable, if not set default to /usr/local
ifeq ($(PREFIX),)
PREFIX := /usr/local
else
PREFIX := $(DESTDIR)
endif
export CC LD CFLAGS LDFLAGS
APP:=$(BUILD_DIR)/youtubeUnblock
SRCS := youtubeUnblock.c mangle.c
OBJS := $(SRCS:%.c=$(BUILD_DIR)/%.o)
LIBNFNETLINK := $(DEPSDIR)/lib/libnfnetlink.a
LIBMNL := $(DEPSDIR)/lib/libmnl.a
LIBNETFILTER_QUEUE := $(DEPSDIR)/lib/libnetfilter_queue.a
.PHONY: default all dev dev_attrs prepare_dirs
default: all
run_dev: dev
bash -c "sudo $(APP) 537"
dev: dev_attrs all
dev_attrs:
$(eval CFLAGS := $(CFLAGS) -DDEBUG -ggdb -g3)
all: prepare_dirs $(APP)
prepare_dirs:
mkdir -p $(BUILD_DIR)
mkdir -p $(DEPSDIR)
$(LIBNFNETLINK):
cd deps/libnfnetlink && ./autogen.sh && ./configure --prefix=$(DEPSDIR) $(if $(CROSS_COMPILE_PLATFORM),--host=$(CROSS_COMPILE_PLATFORM),)
$(MAKE) -C deps/libnfnetlink
$(MAKE) install -C deps/libnfnetlink
$(LIBMNL):
cd deps/libmnl && ./autogen.sh && ./configure --prefix=$(DEPSDIR) $(if $(CROSS_COMPILE_PLATFORM),--host=$(CROSS_COMPILE_PLATFORM),)
$(MAKE) -C deps/libmnl
$(MAKE) install -C deps/libmnl
$(LIBNETFILTER_QUEUE): $(LIBNFNETLINK) $(LIBMNL)
cd deps/libnetfilter_queue && ./autogen.sh && ./configure --prefix=$(DEPSDIR) $(if $(CROSS_COMPILE_PLATFORM),--host=$(CROSS_COMPILE_PLATFORM),)
$(MAKE) -C deps/libnetfilter_queue
$(MAKE) install -C deps/libnetfilter_queue
$(APP): $(OBJS) $(LIBNETFILTER_QUEUE) $(LIBMNL)
@echo 'LD $(APP)'
@$(LD) $(OBJS) -o $(APP) -L$(DEPSDIR)/lib -lmnl -lnetfilter_queue
$(BUILD_DIR)/%.o: %.c $(LIBNETFILTER_QUEUE) $(LIBMNL)
@echo 'CC $@'
@$(CC) -c $(CFLAGS) $^ -o $@
install: all
install -d $(PREFIX)/bin/
install -m 755 $(APP) $(PREFIX)/bin/
install -d $(PREFIX)/lib/systemd/system/
@cp youtubeUnblock.service $(BUILD_DIR)
@sed -i 's/$$(PREFIX)/$(subst /,\/,$(PREFIX))/g' $(BUILD_DIR)/youtubeUnblock.service
install -m 644 $(BUILD_DIR)/youtubeUnblock.service $(PREFIX)/lib/systemd/system/
uninstall:
rm $(PREFIX)/bin/youtubeUnblock
rm $(PREFIX)/lib/systemd/system/youtubeUnblock.service
systemctl disable youtubeUnblock.service
$(KMAKE_TARGETS):
@$(MAKE) -f kmake.mk $@
clean:
rm -rf $(BUILD_DIR)
$(MAKE) distclean -C deps/libnetfilter_queue || true
$(MAKE) distclean -C deps/libmnl || true
$(MAKE) distclean -C deps/libnfnetlink || true
-@$(MAKE) -f kmake.mk kclean
@$(MAKE) -f uspace.mk clean

View File

@ -15,3 +15,4 @@ Makefile.in
/stamp-h1
/*.pc
doxygen.cfg

View File

@ -1,180 +0,0 @@
DOXYFILE_ENCODING = UTF-8
PROJECT_NAME = libnfnetlink
PROJECT_NUMBER = 1.0.2
OUTPUT_DIRECTORY = doxygen
CREATE_SUBDIRS = NO
OUTPUT_LANGUAGE = English
BRIEF_MEMBER_DESC = YES
REPEAT_BRIEF = YES
ABBREVIATE_BRIEF =
ALWAYS_DETAILED_SEC = NO
INLINE_INHERITED_MEMB = NO
FULL_PATH_NAMES = NO
STRIP_FROM_PATH =
STRIP_FROM_INC_PATH =
SHORT_NAMES = NO
JAVADOC_AUTOBRIEF = NO
QT_AUTOBRIEF = NO
MULTILINE_CPP_IS_BRIEF = NO
INHERIT_DOCS = YES
SEPARATE_MEMBER_PAGES = NO
TAB_SIZE = 8
ALIASES =
OPTIMIZE_OUTPUT_FOR_C = YES
OPTIMIZE_OUTPUT_JAVA = NO
OPTIMIZE_FOR_FORTRAN = NO
OPTIMIZE_OUTPUT_VHDL = NO
BUILTIN_STL_SUPPORT = NO
CPP_CLI_SUPPORT = NO
SIP_SUPPORT = NO
DISTRIBUTE_GROUP_DOC = NO
SUBGROUPING = YES
TYPEDEF_HIDES_STRUCT = NO
EXTRACT_ALL = NO
EXTRACT_PRIVATE = NO
EXTRACT_STATIC = NO
EXTRACT_LOCAL_CLASSES = YES
EXTRACT_LOCAL_METHODS = NO
EXTRACT_ANON_NSPACES = NO
HIDE_UNDOC_MEMBERS = NO
HIDE_UNDOC_CLASSES = NO
HIDE_FRIEND_COMPOUNDS = NO
HIDE_IN_BODY_DOCS = NO
INTERNAL_DOCS = NO
CASE_SENSE_NAMES = YES
HIDE_SCOPE_NAMES = NO
SHOW_INCLUDE_FILES = YES
INLINE_INFO = YES
SORT_MEMBER_DOCS = YES
SORT_BRIEF_DOCS = NO
SORT_GROUP_NAMES = NO
SORT_BY_SCOPE_NAME = NO
GENERATE_TODOLIST = YES
GENERATE_TESTLIST = YES
GENERATE_BUGLIST = YES
GENERATE_DEPRECATEDLIST= YES
ENABLED_SECTIONS =
MAX_INITIALIZER_LINES = 30
SHOW_USED_FILES = YES
FILE_VERSION_FILTER =
QUIET = NO
WARNINGS = YES
WARN_IF_UNDOCUMENTED = YES
WARN_IF_DOC_ERROR = YES
WARN_NO_PARAMDOC = NO
WARN_FORMAT = "$file:$line: $text"
WARN_LOGFILE =
INPUT = .
INPUT_ENCODING = UTF-8
FILE_PATTERNS = *.c libnfnetlink.h
RECURSIVE = YES
EXCLUDE =
EXCLUDE_SYMLINKS = NO
EXCLUDE_PATTERNS = */.git/* .*.d
EXCLUDE_SYMBOLS = EXPORT_SYMBOL
EXAMPLE_PATH =
EXAMPLE_PATTERNS =
EXAMPLE_RECURSIVE = NO
IMAGE_PATH =
INPUT_FILTER =
FILTER_PATTERNS =
FILTER_SOURCE_FILES = NO
SOURCE_BROWSER = YES
INLINE_SOURCES = NO
STRIP_CODE_COMMENTS = YES
REFERENCED_BY_RELATION = NO
REFERENCES_RELATION = NO
REFERENCES_LINK_SOURCE = YES
USE_HTAGS = NO
VERBATIM_HEADERS = YES
ALPHABETICAL_INDEX = NO
COLS_IN_ALPHA_INDEX = 5
IGNORE_PREFIX =
GENERATE_HTML = YES
HTML_OUTPUT = html
HTML_FILE_EXTENSION = .html
HTML_HEADER =
HTML_STYLESHEET =
GENERATE_HTMLHELP = NO
GENERATE_DOCSET = NO
DOCSET_FEEDNAME = "Doxygen generated docs"
DOCSET_BUNDLE_ID = org.doxygen.Project
HTML_DYNAMIC_SECTIONS = NO
CHM_FILE =
HHC_LOCATION =
GENERATE_CHI = NO
BINARY_TOC = NO
TOC_EXPAND = NO
DISABLE_INDEX = NO
ENUM_VALUES_PER_LINE = 4
GENERATE_TREEVIEW = NO
TREEVIEW_WIDTH = 250
GENERATE_LATEX = NO
LATEX_OUTPUT = latex
LATEX_CMD_NAME = latex
MAKEINDEX_CMD_NAME = makeindex
COMPACT_LATEX = NO
PAPER_TYPE = a4wide
EXTRA_PACKAGES =
LATEX_HEADER =
PDF_HYPERLINKS = YES
USE_PDFLATEX = YES
LATEX_BATCHMODE = NO
LATEX_HIDE_INDICES = NO
GENERATE_RTF = NO
RTF_OUTPUT = rtf
COMPACT_RTF = NO
RTF_HYPERLINKS = NO
RTF_STYLESHEET_FILE =
RTF_EXTENSIONS_FILE =
GENERATE_MAN = YES
MAN_OUTPUT = man
MAN_EXTENSION = .3
MAN_LINKS = NO
GENERATE_XML = NO
XML_OUTPUT = xml
XML_PROGRAMLISTING = YES
GENERATE_AUTOGEN_DEF = NO
GENERATE_PERLMOD = NO
PERLMOD_LATEX = NO
PERLMOD_PRETTY = YES
PERLMOD_MAKEVAR_PREFIX =
ENABLE_PREPROCESSING = YES
MACRO_EXPANSION = NO
EXPAND_ONLY_PREDEF = NO
SEARCH_INCLUDES = YES
INCLUDE_PATH =
INCLUDE_FILE_PATTERNS =
PREDEFINED =
EXPAND_AS_DEFINED =
SKIP_FUNCTION_MACROS = YES
TAGFILES =
GENERATE_TAGFILE =
ALLEXTERNALS = NO
EXTERNAL_GROUPS = YES
PERL_PATH = /usr/bin/perl
CLASS_DIAGRAMS = YES
MSCGEN_PATH =
HIDE_UNDOC_RELATIONS = YES
HAVE_DOT = YES
CLASS_GRAPH = YES
COLLABORATION_GRAPH = YES
GROUP_GRAPHS = YES
UML_LOOK = NO
TEMPLATE_RELATIONS = NO
INCLUDE_GRAPH = YES
INCLUDED_BY_GRAPH = YES
CALL_GRAPH = NO
CALLER_GRAPH = NO
GRAPHICAL_HIERARCHY = YES
DIRECTORY_GRAPH = YES
DOT_IMAGE_FORMAT = png
DOT_PATH =
DOTFILE_DIRS =
DOT_GRAPH_MAX_NODES = 50
MAX_DOT_GRAPH_DEPTH = 0
DOT_TRANSPARENT = YES
DOT_MULTI_TARGETS = NO
GENERATE_LEGEND = YES
DOT_CLEANUP = YES
SEARCHENGINE = NO

26
kmake.mk Normal file
View File

@ -0,0 +1,26 @@
#Kernel module makes here
PWD := $(CURDIR)
override CC := $(OCC)
override LD := $(OLD)
override CFLAGS :=
override LDFLAGS :=
export CC LD CFLAGS LDFLAGS
.PHONY: kmake kload kunload kreload kclean
kmake:
$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
kload:
insmod youtubeKUnblock.ko
kunload:
-rmmod youtubeKUnblock
kreload: kunload kload
kclean:
$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

View File

@ -1,5 +1,4 @@
#include "mangle.h"
#include <endian.h>
#ifdef KERNEL_SPACE
static __u16 nfq_checksum(__u32 sum, __u16 *buf, int size)
@ -9,7 +8,7 @@ static __u16 nfq_checksum(__u32 sum, __u16 *buf, int size)
size -= sizeof(__u16);
}
if (size) {
#if __BYTE_ORDER == __BIG_ENDIAN
#ifdef __LITTLE_ENDIAN
sum += (uint16_t)*(uint8_t *)buf << 8;
#else
sum += (__u16)*(__u8 *)buf;

View File

@ -16,10 +16,10 @@ typedef __u32 uint32_t;
#include <linux/tcp.h>
#include <asm/byteorder.h>
#define ntohs(x) __constant_ntohs(x)
#define ntohl(x) __constant_ntohl(x)
#define htons(x) __constant_htons(x)
#define htonl(x) __constant_htonl(x)
// #define ntohs(x) __constant_ntohs(x)
// #define ntohl(x) __constant_ntohl(x)
// #define htons(x) __constant_htons(x)
// #define htonl(x) __constant_htonl(x)
#define IP_RF 0x8000 /* reserved fragment flag */
#define IP_DF 0x4000 /* dont fragment flag */

89
uspace.mk Normal file
View File

@ -0,0 +1,89 @@
#Userspace app makes here
BUILD_DIR := $(CURDIR)/build
DEPSDIR := $(BUILD_DIR)/deps
CC:=gcc
LD:=gcc
CFLAGS:=-Wall -Wpedantic -Wno-unused-variable -I$(DEPSDIR)/include -Os
LDFLAGS:=-L$(DEPSDIR)/lib -static
export CC LD CFLAGS LDFLAGS
# PREFIX is environment variable, if not set default to /usr/local
ifeq ($(PREFIX),)
PREFIX := /usr/local
else
PREFIX := $(DESTDIR)
endif
APP:=$(BUILD_DIR)/youtubeUnblock
SRCS := youtubeUnblock.c mangle.c
OBJS := $(SRCS:%.c=$(BUILD_DIR)/%.o)
LIBNFNETLINK := $(DEPSDIR)/lib/libnfnetlink.a
LIBMNL := $(DEPSDIR)/lib/libmnl.a
LIBNETFILTER_QUEUE := $(DEPSDIR)/lib/libnetfilter_queue.a
.PHONY: default all dev dev_attrs prepare_dirs
default: all
run_dev: dev
bash -c "sudo $(APP) 537"
dev: dev_attrs all
dev_attrs:
$(eval CFLAGS := $(CFLAGS) -DDEBUG -ggdb -g3)
all: prepare_dirs $(APP)
prepare_dirs:
mkdir -p $(BUILD_DIR)
mkdir -p $(DEPSDIR)
$(LIBNFNETLINK):
cd deps/libnfnetlink && ./autogen.sh && ./configure --prefix=$(DEPSDIR) $(if $(CROSS_COMPILE_PLATFORM),--host=$(CROSS_COMPILE_PLATFORM),)
$(MAKE) -C deps/libnfnetlink
$(MAKE) install -C deps/libnfnetlink
$(LIBMNL):
cd deps/libmnl && ./autogen.sh && ./configure --prefix=$(DEPSDIR) $(if $(CROSS_COMPILE_PLATFORM),--host=$(CROSS_COMPILE_PLATFORM),)
$(MAKE) -C deps/libmnl
$(MAKE) install -C deps/libmnl
$(LIBNETFILTER_QUEUE): $(LIBNFNETLINK) $(LIBMNL)
cd deps/libnetfilter_queue && ./autogen.sh && ./configure --prefix=$(DEPSDIR) $(if $(CROSS_COMPILE_PLATFORM),--host=$(CROSS_COMPILE_PLATFORM),)
$(MAKE) -C deps/libnetfilter_queue
$(MAKE) install -C deps/libnetfilter_queue
$(APP): $(OBJS) $(LIBNETFILTER_QUEUE) $(LIBMNL)
@echo 'LD $(APP)'
@$(LD) $(OBJS) -o $(APP) -L$(DEPSDIR)/lib -lmnl -lnetfilter_queue
$(BUILD_DIR)/%.o: %.c $(LIBNETFILTER_QUEUE) $(LIBMNL)
@echo 'CC $@'
@$(CC) -c $(CFLAGS) $^ -o $@
install: all
install -d $(PREFIX)/bin/
install -m 755 $(APP) $(PREFIX)/bin/
install -d $(PREFIX)/lib/systemd/system/
@cp youtubeUnblock.service $(BUILD_DIR)
@sed -i 's/$$(PREFIX)/$(subst /,\/,$(PREFIX))/g' $(BUILD_DIR)/youtubeUnblock.service
install -m 644 $(BUILD_DIR)/youtubeUnblock.service $(PREFIX)/lib/systemd/system/
uninstall:
rm $(PREFIX)/bin/youtubeUnblock
rm $(PREFIX)/lib/systemd/system/youtubeUnblock.service
systemctl disable youtubeUnblock.service
clean:
rm -rf $(BUILD_DIR)
$(MAKE) distclean -C deps/libnetfilter_queue || true
$(MAKE) distclean -C deps/libmnl || true
$(MAKE) distclean -C deps/libnfnetlink || true

25
youtubeKblock.c Normal file
View File

@ -0,0 +1,25 @@
// Kernel module for youtubeUnblock.
#include <linux/module.h>
#include <linux/init.h>
#include <linux/printk.h>
#include "mangle.h"
static int __init ykb_init(void) {
pr_info("youtubeUnblock kernel module started.\n");
return 0;
}
static void __exit ykb_destroy(void) {
pr_info("youtubeUnblock kernel module destroyed.\n");
}
MODULE_LICENSE("GPL");
MODULE_VERSION("0.1");
MODULE_AUTHOR("Vadim Vetrov <vetrovvd@gmail.com>");
MODULE_DESCRIPTION("Linux kernel module for youtube unblock");
module_init(ykb_init);
module_exit(ykb_destroy);

View File

@ -316,13 +316,11 @@ static int process_packet(const struct packet_data packet, struct queue_data qda
const struct tcphdr *tcph = (const void *)(raw_payload + iph_len);
if ((const uint8_t *)tcph + 20 > raw_payload + raw_payload_len) {
printf("LZ\n");
goto fallback;
}
int tcph_len = tcph->doff * 4;
if ((const uint8_t *)tcph + tcph_len > raw_payload + raw_payload_len) {
printf("LZ\n");
goto fallback;
}