TSG-7784 PacketAdapter支持CI自动构建RPM; 修改代码结构
This commit is contained in:
111
.gitlab-ci.yml
Normal file
111
.gitlab-ci.yml
Normal file
@@ -0,0 +1,111 @@
|
||||
image: "git.mesalab.cn:7443/mesa_platform/build-env:master"
|
||||
variables:
|
||||
GIT_STRATEGY: "clone"
|
||||
BUILD_PADDING_PREFIX: /tmp/padding_for_CPACK_RPM_BUILD_SOURCE_DIRS_PREFIX_PREFIX_PREFIX_PREFIX_PREFIX_PREFIX/
|
||||
INSTALL_PREFIX: "/opt/tsg/packetadapter"
|
||||
TESTING_VERSION_BUILD: 0
|
||||
|
||||
stages:
|
||||
- build
|
||||
|
||||
.build_by_travis:
|
||||
before_script:
|
||||
- mkdir -p $BUILD_PADDING_PREFIX/$CI_PROJECT_NAMESPACE/
|
||||
- ln -s $CI_PROJECT_DIR $BUILD_PADDING_PREFIX/$CI_PROJECT_PATH
|
||||
- cd $BUILD_PADDING_PREFIX/$CI_PROJECT_PATH
|
||||
- chmod +x ./ci/travis.sh
|
||||
script:
|
||||
- yum makecache
|
||||
- ./ci/travis.sh
|
||||
tags:
|
||||
- share
|
||||
|
||||
branch_build_debug:
|
||||
stage: build
|
||||
extends: .build_by_travis
|
||||
variables:
|
||||
BUILD_TYPE: Debug
|
||||
except:
|
||||
- /^develop-.*$/i
|
||||
- /^release-.*$/i
|
||||
- tags
|
||||
|
||||
branch_build_release:
|
||||
stage: build
|
||||
variables:
|
||||
BUILD_TYPE: RelWithDebInfo
|
||||
extends: .build_by_travis
|
||||
except:
|
||||
- /^develop-.*$/i
|
||||
- /^release-.*$/i
|
||||
- tags
|
||||
|
||||
develop_build_debug:
|
||||
stage: build
|
||||
extends: .build_by_travis
|
||||
variables:
|
||||
TESTING_VERSION_BUILD: 1
|
||||
UPLOAD_SYMBOL_FILES: 1
|
||||
BUILD_TYPE: Debug
|
||||
ASAN_OPTION: ADDRESS
|
||||
PACKAGE: 1
|
||||
PULP3_REPO_NAME: tsg-testing-x86_64.el7
|
||||
PULP3_DIST_NAME: tsg-testing-x86_64.el7
|
||||
artifacts:
|
||||
name: "packetadapter-develop-$CI_COMMIT_REF_NAME-debug"
|
||||
paths:
|
||||
- build/*.rpm
|
||||
only:
|
||||
- /^develop-.*$/i
|
||||
- /^release-.*$/i
|
||||
|
||||
develop_build_release:
|
||||
stage: build
|
||||
extends: .build_by_travis
|
||||
variables:
|
||||
TESTING_VERSION_BUILD: 1
|
||||
UPLOAD_SYMBOL_FILES: 1
|
||||
# ASAN_OPTION: ADDRESS
|
||||
BUILD_TYPE: RelWithDebInfo
|
||||
PACKAGE: 1
|
||||
PULP3_REPO_NAME: tsg-testing-x86_64.el7
|
||||
PULP3_DIST_NAME: tsg-testing-x86_64.el7
|
||||
artifacts:
|
||||
name: "packetadapter-develop-$CI_COMMIT_REF_NAME-release"
|
||||
paths:
|
||||
- build/*.rpm
|
||||
only:
|
||||
- /^develop-.*$/i
|
||||
- /^release-.*$/i
|
||||
|
||||
release_build_debug:
|
||||
stage: build
|
||||
variables:
|
||||
UPLOAD_SYMBOL_FILES: 1
|
||||
BUILD_TYPE: Debug
|
||||
PACKAGE: 1
|
||||
PULP3_REPO_NAME: tsg-stable-x86_64.el7
|
||||
PULP3_DIST_NAME: tsg-stable-x86_64.el7
|
||||
extends: .build_by_travis
|
||||
artifacts:
|
||||
name: "packetadapter-install-$CI_COMMIT_REF_NAME-debug"
|
||||
paths:
|
||||
- build/*.rpm
|
||||
only:
|
||||
- tags
|
||||
|
||||
release_build_release:
|
||||
stage: build
|
||||
variables:
|
||||
BUILD_TYPE: RelWithDebInfo
|
||||
UPLOAD_SYMBOL_FILES: 1
|
||||
PACKAGE: 1
|
||||
PULP3_REPO_NAME: tsg-stable-x86_64.el7
|
||||
PULP3_DIST_NAME: tsg-stable-x86_64.el7
|
||||
extends: .build_by_travis
|
||||
artifacts:
|
||||
name: "packetadapter-install-$CI_COMMIT_REF_NAME-release"
|
||||
paths:
|
||||
- build/*.rpm
|
||||
only:
|
||||
- tags
|
||||
61
CMakeLists.txt
Normal file
61
CMakeLists.txt
Normal file
@@ -0,0 +1,61 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
project(packetadapter)
|
||||
|
||||
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
|
||||
include(Version)
|
||||
include(Package)
|
||||
|
||||
add_definitions(-D_GNU_SOURCE)
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
|
||||
if(NOT CMAKE_BUILD_TYPE)
|
||||
set(CMAKE_BUILD_TYPE RelWithDebInfo)
|
||||
endif()
|
||||
|
||||
if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
|
||||
set (CMAKE_INSTALL_PREFIX "/opt/tsg/packetadapter" CACHE PATH "default install path" FORCE )
|
||||
endif()
|
||||
|
||||
# Global compile options
|
||||
option(ENABLE_PIC "Generate position independent code (necessary for shared libraries)" TRUE)
|
||||
option(ENABLE_WARNING_ALL "Enable all optional warnings which are desirable for normal code" TRUE)
|
||||
|
||||
if(NOT ASAN_OPTION)
|
||||
option(ENABLE_SANITIZE_ADDRESS "Enable AddressSanitizer" FALSE)
|
||||
else()
|
||||
option(ENABLE_SANITIZE_ADDRESS "Enable AddressSanitizer" TRUE)
|
||||
endif()
|
||||
|
||||
option(ENABLE_SANITIZE_THREAD "Enable ThreadSanitizer" FALSE)
|
||||
|
||||
if(ENABLE_PIC)
|
||||
set(CMAKE_POSITION_INDEPENDENT_CODE 1)
|
||||
endif()
|
||||
|
||||
if(ENABLE_WARNING_ALL)
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
|
||||
endif()
|
||||
|
||||
if(ENABLE_SANITIZE_ADDRESS)
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
|
||||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lasan")
|
||||
elseif(ENABLE_SANITIZE_THREAD)
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread -fno-omit-frame-pointer")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread -fno-omit-frame-pointer")
|
||||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lasan")
|
||||
endif()
|
||||
|
||||
if(ENABLE_SANITIZE_ADDRESS AND ENABLE_SANITIZE_THREAD)
|
||||
message(WARNING "Both ENABLE_SANITIZE_ADDRESS and ENABLE_SANITIZE_THREAD set, only ENABLE_SANITIZE_ADDRESS effected.")
|
||||
endif()
|
||||
|
||||
add_custom_target("install-program" COMMAND ${CMAKE_COMMAND} ARGS -DCOMPONENT=Program -P cmake_install.cmake)
|
||||
add_custom_target("install-profile" COMMAND ${CMAKE_COMMAND} ARGS -DCOMPONENT=Profile -P cmake_install.cmake)
|
||||
|
||||
enable_testing()
|
||||
add_subdirectory(common)
|
||||
add_subdirectory(platform)
|
||||
add_subdirectory(script)
|
||||
2
Makefile
2
Makefile
@@ -1,2 +0,0 @@
|
||||
all:
|
||||
gcc -Wall -o nfq_filter_gtp *.c -lnfnetlink -lnetfilter_queue
|
||||
87
README.md
87
README.md
@@ -1,70 +1,73 @@
|
||||
## 功能
|
||||
# PacketAdapter -- Packet Filtering & Adaptation tools
|
||||
|
||||
并联环境下实现GTP路由封堵功能。
|
||||
## 简介
|
||||
|
||||
**实现**
|
||||
PacketAdapter 是一个基于 iptables 的数据包过滤,转换/适配工具。
|
||||
PacketAdapter 并不会凭空产生数据包,而是将 iptables 过滤的数据包重新转换/适配后再回注到网络中。
|
||||
可用于 Overlay networks 中 Packet encapsulation and decapsulation,屏蔽端到端协议层之间的差异。
|
||||
|
||||
将 “=> MAC => IPv4/IPv6 => UDP => GTP1 => IPv4/IPv6 => TCP/UDP“ 中的 "IPv4/IPv6 => UDP => GTP1" 协议层剥离。
|
||||
## 应用 -- 实现 GTP Overlay 数据包的解封装
|
||||
|
||||
**例子**
|
||||
PacketAdapter 通过 iptables 将 Firewall 发送的 GTP RST 包进行过滤,然后将 GTP 数据解封装后回注到网络中。
|
||||
例如:将 “MAC/IPv4 or IPv6/UDP/GTP1/IPv4 or IPv6/TCP or UDP“ 中的 "/IPv4 or IPv6/UDP/GTP1" 协议层剥离。
|
||||
|
||||
原数据包为:
|
||||
|
||||
``` shel
|
||||
Frame 1: 90 bytes on wire (720 bits), 90 bytes captured (720 bits)
|
||||
Ethernet II, Src: JuniperN_4d:d3:51 (08:81:f4:4d:d3:51), Dst: c8:67:d9:18:80:c3 (c8:67:d9:18:80:c3)
|
||||
Internet Protocol Version 4, Src: 10.166.20.10, Dst: 10.2.3.35
|
||||
User Datagram Protocol, Src Port: 2152, Dst Port: 2152
|
||||
GPRS Tunneling Protocol
|
||||
Internet Protocol Version 4, Src: 10.58.121.62, Dst: 217.76.78.112
|
||||
Transmission Control Protocol, Src Port: 52144, Dst Port: 443, Seq: 1, Ack: 1, Len: 0
|
||||
```
|
||||
+-----------+ +-----------+
|
||||
| TCP/UDP | | TCP/UDP |
|
||||
+-----------+ +-----------+
|
||||
| IPv4/IPv6 | | IPv4/IPv6 |
|
||||
+-----------+ +-----------+
|
||||
| GTP1 | | |
|
||||
+-----------+ | |
|
||||
| UDP | ==> | |
|
||||
+-----------+ | |
|
||||
| IPv4/IPv6 | | |
|
||||
+-----------+ | |
|
||||
| MAC | | MAC |
|
||||
+-----------+ +-----------+
|
||||
```
|
||||
|
||||
经过 NFQ 过滤后变为:
|
||||
注意:
|
||||
* /MAC/IPv6 的 first next header 必须为 UDP。
|
||||
* 目前不支持 GTP 扩展头。
|
||||
|
||||
``` shel
|
||||
Frame 1: 90 bytes on wire (720 bits), 90 bytes captured (720 bits)
|
||||
Ethernet II, Src: JuniperN_4d:d3:51 (08:81:f4:4d:d3:51), Dst: c8:67:d9:18:80:c3 (c8:67:d9:18:80:c3)
|
||||
Internet Protocol Version 4, Src: 10.58.121.62, Dst: 217.76.78.112
|
||||
Transmission Control Protocol, Src Port: 52144, Dst Port: 443, Seq: 1, Ack: 1, Len: 0
|
||||
```
|
||||
|
||||
## 构造测试环境
|
||||
## 运行环境
|
||||
|
||||
``` shell
|
||||
# yum install --downloadonly --downloaddir=./ libnetfilter_queue.x86_64
|
||||
# yum install --downloadonly --downloaddir=./ libnetfilter_queue-devel.x86_64
|
||||
# 安装 libnetfilter_queue
|
||||
yum install -y libnetfilter_queue
|
||||
|
||||
# 清空 iptables
|
||||
iptables -F -t nat
|
||||
iptables -F -t filter
|
||||
iptables -F -t mangle
|
||||
iptables -F -t raw
|
||||
iptables -F -t nat
|
||||
iptables -F -t filter
|
||||
iptables -F -t mangle
|
||||
iptables -F -t raw
|
||||
|
||||
ip6tables -F -t nat
|
||||
ip6tables -F -t filter
|
||||
ip6tables -F -t mangle
|
||||
ip6tables -F -t raw
|
||||
|
||||
# 增加 iptables
|
||||
/usr/sbin/iptables -A OUTPUT -o eno2 -p udp --dport 2152 -j NFQUEUE --queue-num 1
|
||||
/usr/sbin/iptables -A OUTPUT -o eno2 -p udp --dport 2152 -j NFQUEUE --queue-num 1
|
||||
/usr/sbin/ip6tables -A OUTPUT -o eno2 -p udp --dport 2152 -j NFQUEUE --queue-num 1
|
||||
|
||||
# 删除 iptables
|
||||
/usr/sbin/iptables -D OUTPUT -o eno2 -p udp --dport 2152 -j NFQUEUE --queue-num 1
|
||||
/usr/sbin/iptables -D OUTPUT -o eno2 -p udp --dport 2152 -j NFQUEUE --queue-num 1
|
||||
/usr/sbin/ip6tables -D OUTPUT -o eno2 -p udp --dport 2152 -j NFQUEUE --queue-num 1
|
||||
|
||||
# 调试 iptables
|
||||
/usr/sbin/iptables -A OUTPUT -o eno2 -j LOG
|
||||
/usr/sbin/ip6tables -A OUTPUT -o eno2 -j LOG
|
||||
# /usr/sbin/iptables -A OUTPUT -o eno2 -j LOG
|
||||
# /usr/sbin/ip6tables -A OUTPUT -o eno2 -j LOG
|
||||
|
||||
# make
|
||||
yum install -y libnetfilter_queue-devel
|
||||
make
|
||||
./nfq_filter_gtp
|
||||
# 启动服务
|
||||
systemctl enable nfq_filter_gtp
|
||||
systemctl start nfq_filter_gtp
|
||||
```
|
||||
|
||||
## 局限
|
||||
|
||||
/MAC/IPv6 的 first next header 必须为 UDP,否则跳过。
|
||||
|
||||
## TODO
|
||||
|
||||
// support service
|
||||
// support filestat
|
||||
* support service
|
||||
* support filestat
|
||||
1268
autorevision.sh
Normal file
1268
autorevision.sh
Normal file
File diff suppressed because it is too large
Load Diff
48
ci/get-nprocessors.sh
Normal file
48
ci/get-nprocessors.sh
Normal file
@@ -0,0 +1,48 @@
|
||||
#!/usr/bin/env bash
|
||||
# Copyright 2017 Google Inc.
|
||||
# All Rights Reserved.
|
||||
#
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are
|
||||
# met:
|
||||
#
|
||||
# * Redistributions of source code must retain the above copyright
|
||||
# notice, this list of conditions and the following disclaimer.
|
||||
# * Redistributions in binary form must reproduce the above
|
||||
# copyright notice, this list of conditions and the following disclaimer
|
||||
# in the documentation and/or other materials provided with the
|
||||
# distribution.
|
||||
# * Neither the name of Google Inc. nor the names of its
|
||||
# contributors may be used to endorse or promote products derived from
|
||||
# this software without specific prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
# This file is typically sourced by another script.
|
||||
# if possible, ask for the precise number of processors,
|
||||
# otherwise take 2 processors as reasonable default; see
|
||||
# https://docs.travis-ci.com/user/speeding-up-the-build/#Makefile-optimization
|
||||
if [ -x /usr/bin/getconf ]; then
|
||||
NPROCESSORS=$(/usr/bin/getconf _NPROCESSORS_ONLN)
|
||||
else
|
||||
NPROCESSORS=2
|
||||
fi
|
||||
|
||||
# as of 2017-09-04 Travis CI reports 32 processors, but GCC build
|
||||
# crashes if parallelized too much (maybe memory consumption problem),
|
||||
# so limit to 4 processors for the time being.
|
||||
if [ $NPROCESSORS -gt 4 ] ; then
|
||||
echo "$0:Note: Limiting processors to use by make from $NPROCESSORS to 4."
|
||||
NPROCESSORS=4
|
||||
fi
|
||||
3
ci/perpare_pulp3_netrc.sh
Normal file
3
ci/perpare_pulp3_netrc.sh
Normal file
@@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env sh
|
||||
set -evx
|
||||
echo "machine ${PULP3_SERVER_URL}\nlogin ${PULP3_SERVER_LOGIN}\npassword ${PULP3_SERVER_PASSWORD}\n" > ~/.netrc
|
||||
65
ci/travis.sh
Normal file
65
ci/travis.sh
Normal file
@@ -0,0 +1,65 @@
|
||||
#!/usr/bin/env sh
|
||||
set -evx
|
||||
|
||||
chmod +x ci/get-nprocessors.sh
|
||||
. ci/get-nprocessors.sh
|
||||
|
||||
# if possible, ask for the precise number of processors,
|
||||
# otherwise take 2 processors as reasonable default; see
|
||||
# https://docs.travis-ci.com/user/speeding-up-the-build/#Makefile-optimization
|
||||
if [ -x /usr/bin/getconf ]; then
|
||||
NPROCESSORS=$(/usr/bin/getconf _NPROCESSORS_ONLN)
|
||||
else
|
||||
NPROCESSORS=2
|
||||
fi
|
||||
|
||||
# as of 2017-09-04 Travis CI reports 32 processors, but GCC build
|
||||
# crashes if parallelized too much (maybe memory consumption problem),
|
||||
# so limit to 4 processors for the time being.
|
||||
if [ $NPROCESSORS -gt 4 ] ; then
|
||||
echo "$0:Note: Limiting processors to use by make from $NPROCESSORS to 4."
|
||||
NPROCESSORS=4
|
||||
fi
|
||||
|
||||
# Tell make to use the processors. No preceding '-' required.
|
||||
MAKEFLAGS="j${NPROCESSORS}"
|
||||
export MAKEFLAGS
|
||||
|
||||
env | sort
|
||||
|
||||
# Set default values to OFF for these variables if not specified.
|
||||
: "${NO_EXCEPTION:=OFF}"
|
||||
: "${NO_RTTI:=OFF}"
|
||||
: "${COMPILER_IS_GNUCXX:=OFF}"
|
||||
|
||||
# Install dependency from YUM
|
||||
yum install -y libasan
|
||||
#yum install -y libmnl-devel libnfnetlink-devel
|
||||
yum install -y libnetfilter_queue-devel
|
||||
|
||||
if [ $ASAN_OPTION ];then
|
||||
source /opt/rh/devtoolset-7/enable
|
||||
fi
|
||||
|
||||
mkdir build || true
|
||||
cd build
|
||||
|
||||
cmake3 -DCMAKE_CXX_FLAGS=$CXX_FLAGS \
|
||||
-DCMAKE_BUILD_TYPE=$BUILD_TYPE \
|
||||
-DASAN_OPTION=$ASAN_OPTION \
|
||||
-DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX \
|
||||
-DVERSION_DAILY_BUILD=$TESTING_VERSION_BUILD \
|
||||
..
|
||||
make
|
||||
|
||||
if [ -n "${PACKAGE}" ]; then
|
||||
make package
|
||||
cp ~/rpm_upload_tools.py ./
|
||||
python3 rpm_upload_tools.py ${PULP3_REPO_NAME} ${PULP3_DIST_NAME} *.rpm
|
||||
fi
|
||||
|
||||
if [ -n "${UPLOAD_SYMBOL_FILES}" ]; then
|
||||
rpm -i packetadapter*debuginfo*.rpm
|
||||
cp /usr/lib/debug/opt/tsg/packetadapter/bin/packetadapter.debug /tmp/packetadapter.debuginfo.${CI_COMMIT_SHORT_SHA}
|
||||
sentry-cli upload-dif -t elf /tmp/packetadapter.debuginfo.${CI_COMMIT_SHORT_SHA}
|
||||
fi
|
||||
39
cmake/FindNFNETLINK.cmake
Normal file
39
cmake/FindNFNETLINK.cmake
Normal file
@@ -0,0 +1,39 @@
|
||||
# - Find nfnetlinkDaemon
|
||||
# Find the nfnetlink daemon library
|
||||
#
|
||||
# This module defines the following variables:
|
||||
# NFNETLINK_FOUND - True if library and include directory are found
|
||||
# If set to TRUE, the following are also defined:
|
||||
# NFNETLINK_INCLUDE_DIRS - The directory where to find the header file
|
||||
# NFNETLINK_LIBRARIES - Where to find the library file
|
||||
#
|
||||
# For conveniance, these variables are also set. They have the same values
|
||||
# than the variables above. The user can thus choose his/her prefered way
|
||||
# to write them.
|
||||
# NFNETLINK_LIBRARY
|
||||
# NFNETLINK_INCLUDE_DIR
|
||||
#
|
||||
# This file is in the public domain
|
||||
|
||||
include(FindPkgConfig)
|
||||
pkg_check_modules(NFNETLINK libnfnetlink)
|
||||
|
||||
if(NOT NFNETLINK_FOUND)
|
||||
find_path(NFNETLINK_INCLUDE_DIRS NAMES nlibnfnetlink/libnfnetlink.h
|
||||
DOC "The nfnetlink include directory")
|
||||
|
||||
find_library(NFNETLINK_LIBRARIES NAMES libnfnetlink
|
||||
DOC "The nfnetlink library")
|
||||
|
||||
# Use some standard module to handle the QUIETLY and REQUIRED arguments, and
|
||||
# set NFNETLINK_FOUND to TRUE if these two variables are set.
|
||||
include(FindPackageHandleStandardArgs)
|
||||
find_package_handle_standard_args(NFNETLINK REQUIRED_VARS NFNETLINK_LIBRARIES NFNETLINK_INCLUDE_DIRS)
|
||||
|
||||
if(NFNETLINK_FOUND)
|
||||
set(NFNETLINK_LIBRARY ${NFNETLINK_LIBRARIES})
|
||||
set(NFNETLINK_INCLUDE_DIR ${NFNETLINK_INCLUDE_DIRS})
|
||||
endif()
|
||||
endif()
|
||||
|
||||
mark_as_advanced(NFNETLINK_INCLUDE_DIRS NFNETLINK_LIBRARIES)
|
||||
31
cmake/Package.cmake
Normal file
31
cmake/Package.cmake
Normal file
@@ -0,0 +1,31 @@
|
||||
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||
set(CPACK_PACKAGE_NAME "packetadapter-debug")
|
||||
else()
|
||||
set(CPACK_PACKAGE_NAME "packetadapter")
|
||||
endif()
|
||||
|
||||
message(STATUS "Package: ${CPACK_PACKAGE_NAME}")
|
||||
|
||||
set(CPACK_PACKAGE_VENDOR "MESASOFT")
|
||||
set(CPACK_PACKAGE_VERSION_MAJOR "${VERSION_MAJOR}")
|
||||
set(CPACK_PACKAGE_VERSION_MINOR "${VERSION_MINOR}")
|
||||
set(CPACK_PACKAGE_VERSION_PATCH "${VERSION_PATCH}.${DESCRIBE}")
|
||||
set(CPACK_PACKAGING_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX})
|
||||
|
||||
# RPM Build
|
||||
set(CPACK_GENERATOR "RPM")
|
||||
set(CPACK_RPM_AUTO_GENERATED_FILE_NAME ON)
|
||||
set(CPACK_RPM_FILE_NAME "RPM-DEFAULT")
|
||||
set(CPACK_RPM_PACKAGE_AUTOREQPROV "no")
|
||||
set(CPACK_RPM_PACKAGE_RELEASE_DIST on)
|
||||
set(CPACK_RPM_DEBUGINFO_PACKAGE on)
|
||||
set(CPACK_RPM_POST_INSTALL_SCRIPT_FILE ${CMAKE_SOURCE_DIR}/cmake/PostInstall.in)
|
||||
set(CPACK_RPM_POST_UNINSTALL_SCRIPT_FILE ${CMAKE_SOURCE_DIR}/cmake/PostUninstall.in)
|
||||
set(CPACK_RPM_PRE_UNINSTALL_SCRIPT_FILE ${CMAKE_SOURCE_DIR}/cmake/PreUninstall.in)
|
||||
|
||||
# Must uninstall the debug package before install release package
|
||||
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||
set(CPACK_RPM_PACKAGE_CONFLICTS "packetadapter")
|
||||
else()
|
||||
set(CPACK_RPM_PACKAGE_CONFLICTS "packetadapter-debug")
|
||||
endif()
|
||||
2
cmake/PostInstall.in
Normal file
2
cmake/PostInstall.in
Normal file
@@ -0,0 +1,2 @@
|
||||
%systemd_post packetadapter.service
|
||||
/sbin/ldconfig
|
||||
2
cmake/PostUninstall.in
Normal file
2
cmake/PostUninstall.in
Normal file
@@ -0,0 +1,2 @@
|
||||
%systemd_postun_with_restart packetadapter.service
|
||||
/sbin/ldconfig
|
||||
1
cmake/PreUninstall.in
Normal file
1
cmake/PreUninstall.in
Normal file
@@ -0,0 +1 @@
|
||||
%systemd_preun packetadapter.service
|
||||
49
cmake/Version.cmake
Normal file
49
cmake/Version.cmake
Normal file
@@ -0,0 +1,49 @@
|
||||
|
||||
# Using autorevision.sh to generate version information
|
||||
|
||||
set(__SOURCE_AUTORESIVISION ${CMAKE_SOURCE_DIR}/autorevision.sh)
|
||||
set(__AUTORESIVISION ${CMAKE_BINARY_DIR}/autorevision.sh)
|
||||
set(__VERSION_CACHE ${CMAKE_SOURCE_DIR}/version.txt)
|
||||
set(__VERSION_CONFIG ${CMAKE_BINARY_DIR}/version.cmake)
|
||||
|
||||
file(COPY ${__SOURCE_AUTORESIVISION} DESTINATION ${CMAKE_BINARY_DIR}
|
||||
FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE
|
||||
WORLD_READ WORLD_EXECUTE)
|
||||
|
||||
# execute autorevision.sh to generate version information
|
||||
execute_process(COMMAND ${__AUTORESIVISION} -t cmake -o ${__VERSION_CACHE} OUTPUT_FILE ${__VERSION_CONFIG})
|
||||
include(${__VERSION_CONFIG})
|
||||
|
||||
# extract major, minor, patch version from git tag
|
||||
string(REGEX REPLACE "^v([0-9]+)\\..*" "\\1" VERSION_MAJOR "${VCS_TAG}")
|
||||
string(REGEX REPLACE "^v[0-9]+\\.([0-9]+).*" "\\1" VERSION_MINOR "${VCS_TAG}")
|
||||
string(REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" VERSION_PATCH "${VCS_TAG}")
|
||||
string(REGEX REPLACE "[T\\:\\+\\-]" "" VERSION_DATE "${VCS_DATE}")
|
||||
|
||||
if(VERSION_DAILY_BUILD)
|
||||
set(VERSION_PATCH ${VERSION_PATCH}.${VERSION_DATE})
|
||||
endif()
|
||||
|
||||
if(NOT VERSION_MAJOR)
|
||||
set(VERSION_MAJOR 1)
|
||||
endif()
|
||||
|
||||
if(NOT VERSION_MINOR)
|
||||
set(VERSION_MINOR 0)
|
||||
endif()
|
||||
|
||||
if(NOT VERSION_PATCH)
|
||||
set(VERSION_PATCH 0)
|
||||
endif()
|
||||
|
||||
set(DESCRIBE "${VCS_SHORT_HASH}")
|
||||
set(VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
|
||||
set(GIT_VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}-${DESCRIBE}")
|
||||
|
||||
# Replace .- with _
|
||||
string(REGEX REPLACE "[\\.\\-]" "_" VAR_VERSION "${GIT_VERSION}")
|
||||
|
||||
# print information
|
||||
message(STATUS "Welcome to Packet Adapter, Version: ${GIT_VERSION}")
|
||||
add_definitions(-DGIT_VERSION=\"${GIT_VERSION}\")
|
||||
add_definitions(-DVAR_VERSION=${VAR_VERSION})
|
||||
2
common/CMakeLists.txt
Normal file
2
common/CMakeLists.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
add_library(common src/decode_ipv4.c src/decode_ipv6.c src/decode_tcp.c src/decode_udp.c src/decode_gtp.c)
|
||||
target_include_directories(common PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include)
|
||||
36
common/include/decode_gtp.h
Normal file
36
common/include/decode_gtp.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#ifndef _DECODE_GTP_H
|
||||
#define _DECODE_GTP_H
|
||||
|
||||
#ifdef __cpluscplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#include "public.h"
|
||||
|
||||
/* According to 3GPP TS 29.060. */
|
||||
typedef struct gtp1_header_s
|
||||
{
|
||||
uint8_t flags;
|
||||
uint8_t type;
|
||||
uint16_t length;
|
||||
uint32_t tid;
|
||||
} __attribute__((packed)) gtp1_header_t;
|
||||
|
||||
typedef struct gtp_info_s
|
||||
{
|
||||
gtp1_header_t *hdr;
|
||||
uint8_t *payload;
|
||||
|
||||
uint32_t hdr_len;
|
||||
uint32_t payload_len;
|
||||
} gtp_info_t;
|
||||
|
||||
int decode_gtp(gtp_info_t *packet, const uint8_t *data, uint32_t len);
|
||||
void dump_gtp_info(uint32_t pkt_id, gtp_info_t *packet);
|
||||
|
||||
#ifdef __cpluscplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
55
common/include/decode_ipv4.h
Normal file
55
common/include/decode_ipv4.h
Normal file
@@ -0,0 +1,55 @@
|
||||
#ifndef _DECODE_IPV4_H
|
||||
#define _DECODE_IPV4_H
|
||||
|
||||
#ifdef __cpluscplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#include "public.h"
|
||||
|
||||
#define IPV4_HEADER_LEN 20
|
||||
|
||||
typedef struct ipv4_header_s
|
||||
{
|
||||
uint8_t ip_verhl; // version & header length
|
||||
uint8_t ip_tos;
|
||||
uint16_t ip_len;
|
||||
uint16_t ip_id;
|
||||
uint16_t ip_off;
|
||||
uint8_t ip_ttl;
|
||||
uint8_t ip_proto;
|
||||
uint16_t ip_csum;
|
||||
union
|
||||
{
|
||||
struct
|
||||
{
|
||||
struct in_addr ip_src;
|
||||
struct in_addr ip_dst;
|
||||
} ip4_un1;
|
||||
uint16_t ip_addrs[4];
|
||||
} ip4_hdrun1;
|
||||
} __attribute__((__packed__)) ipv4_header_t;
|
||||
|
||||
typedef struct ipv4_info_s
|
||||
{
|
||||
char src_addr[INET_ADDRSTRLEN];
|
||||
char dst_addr[INET_ADDRSTRLEN];
|
||||
|
||||
ipv4_header_t *hdr;
|
||||
uint8_t *payload;
|
||||
uint8_t next_protocol;
|
||||
|
||||
uint32_t hdr_len;
|
||||
uint32_t opts_len;
|
||||
uint32_t payload_len;
|
||||
} ipv4_info_t;
|
||||
|
||||
int decode_ipv4(ipv4_info_t *packet, const uint8_t *data, uint32_t len);
|
||||
void dump_ipv4_info(uint32_t pkt_id, ipv4_info_t *packet);
|
||||
|
||||
#ifdef __cpluscplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
58
common/include/decode_ipv6.h
Normal file
58
common/include/decode_ipv6.h
Normal file
@@ -0,0 +1,58 @@
|
||||
#ifndef _DECODE_IPV6_H
|
||||
#define _DECODE_IPV6_H
|
||||
|
||||
#ifdef __cpluscplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#include "public.h"
|
||||
|
||||
#define IPV6_HEADER_LEN 40
|
||||
|
||||
typedef struct ipv6_header_s
|
||||
{
|
||||
union
|
||||
{
|
||||
struct ip6_un1_
|
||||
{
|
||||
uint32_t ip6_un1_flow; /* 20 bits of flow-ID */
|
||||
uint16_t ip6_un1_plen; /* payload length */
|
||||
uint8_t ip6_un1_nxt; /* next header */
|
||||
uint8_t ip6_un1_hlim; /* hop limit */
|
||||
} ip6_un1;
|
||||
uint8_t ip6_un2_vfc; /* 4 bits version, top 4 bits class */
|
||||
} ip6_hdrun;
|
||||
|
||||
union
|
||||
{
|
||||
struct
|
||||
{
|
||||
uint32_t ip6_src[4];
|
||||
uint32_t ip6_dst[4];
|
||||
} ip6_un2;
|
||||
uint16_t ip6_addrs[16];
|
||||
} ip6_hdrun2;
|
||||
} __attribute__((__packed__)) ipv6_header_t;
|
||||
|
||||
typedef struct ipv6_info_s
|
||||
{
|
||||
char src_addr[INET6_ADDRSTRLEN];
|
||||
char dst_addr[INET6_ADDRSTRLEN];
|
||||
|
||||
ipv6_header_t *hdr;
|
||||
uint8_t *payload;
|
||||
uint8_t next_protocol;
|
||||
|
||||
uint32_t hdr_len;
|
||||
uint32_t payload_len;
|
||||
} ipv6_info_t;
|
||||
|
||||
int decode_ipv6(ipv6_info_t *packet, const uint8_t *data, uint32_t len);
|
||||
void dump_ipv6_info(uint32_t pkt_id, ipv6_info_t *packet);
|
||||
|
||||
#ifdef __cpluscplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
46
common/include/decode_tcp.h
Normal file
46
common/include/decode_tcp.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#ifndef _DECODE_TCP_H
|
||||
#define _DECODE_TCP_H
|
||||
|
||||
#ifdef __cpluscplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#include "public.h"
|
||||
|
||||
#define TCP_HEADER_LEN 20
|
||||
|
||||
typedef struct tcp_header_s
|
||||
{
|
||||
uint16_t th_sport; /**< source port */
|
||||
uint16_t th_dport; /**< destination port */
|
||||
uint32_t th_seq; /**< sequence number */
|
||||
uint32_t th_ack; /**< acknowledgement number */
|
||||
uint8_t th_offx2; /**< offset and reserved */
|
||||
uint8_t th_flags; /**< pkt flags */
|
||||
uint16_t th_win; /**< pkt window */
|
||||
uint16_t th_sum; /**< checksum */
|
||||
uint16_t th_urp; /**< urgent pointer */
|
||||
} __attribute__((__packed__)) tcp_header_t;
|
||||
|
||||
typedef struct tcp_info_s
|
||||
{
|
||||
uint16_t src_port;
|
||||
uint16_t dst_port;
|
||||
|
||||
tcp_header_t *hdr;
|
||||
uint8_t *payload;
|
||||
|
||||
uint32_t opt_len;
|
||||
uint32_t hdr_len;
|
||||
uint32_t payload_len;
|
||||
} tcp_info_t;
|
||||
|
||||
int decode_tcp(tcp_info_t *packet, const uint8_t *data, uint32_t len);
|
||||
void dump_tcp_info(uint32_t pkt_id, tcp_info_t *packet);
|
||||
|
||||
#ifdef __cpluscplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
40
common/include/decode_udp.h
Normal file
40
common/include/decode_udp.h
Normal file
@@ -0,0 +1,40 @@
|
||||
#ifndef _DECODE_UDP_H
|
||||
#define _DECODE_UDP_H
|
||||
|
||||
#ifdef __cpluscplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#include "public.h"
|
||||
|
||||
#define UDP_HEADER_LEN 8
|
||||
|
||||
typedef struct udp_header_s
|
||||
{
|
||||
uint16_t udp_src_port;
|
||||
uint16_t udp_dst_port;
|
||||
uint16_t udp_len;
|
||||
uint16_t udp_sum;
|
||||
} __attribute__((__packed__)) udp_header_t;
|
||||
|
||||
typedef struct udp_info_s
|
||||
{
|
||||
uint16_t src_port;
|
||||
uint16_t dst_port;
|
||||
|
||||
udp_header_t *hdr;
|
||||
uint8_t *payload;
|
||||
|
||||
uint32_t hdr_len;
|
||||
uint32_t payload_len;
|
||||
} udp_info_t;
|
||||
|
||||
int decode_udp(udp_info_t *packet, const uint8_t *data, uint32_t len);
|
||||
void dump_udp_info(uint32_t pkt_id, udp_info_t *packet);
|
||||
|
||||
#ifdef __cpluscplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef _LOG_H
|
||||
#define _LOG_H
|
||||
#ifndef _PUBLIC_H
|
||||
#define _PUBLIC_H
|
||||
|
||||
#ifdef __cpluscplus
|
||||
extern "C"
|
||||
@@ -7,8 +7,16 @@ extern "C"
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
|
||||
#define IP_GET_RAW_VER(raw_pkt) ((((raw_pkt)[0] & 0xf0) >> 4))
|
||||
|
||||
#define PRINT_FILE_INFO 0
|
||||
|
||||
56
common/src/decode_gtp.c
Normal file
56
common/src/decode_gtp.c
Normal file
@@ -0,0 +1,56 @@
|
||||
#include "decode_gtp.h"
|
||||
|
||||
#define GTP_TPDU 255
|
||||
#define GTP1U_PORT 2152
|
||||
#define GTP1_F_MASK 0x07
|
||||
|
||||
#define GTP1_GET_TYPE(gtp1_hdr) ((gtp1_hdr)->type)
|
||||
#define GTP1_GET_FLAGS(gtp1_hdr) ((gtp1_hdr)->flags >> 5)
|
||||
#define GTP1_GET_HLEN(gtp1_hdr) (((gtp1_hdr)->flags & GTP1_F_MASK) > 0 ? 12 : 8)
|
||||
|
||||
enum gtp_version_e
|
||||
{
|
||||
GTP_V0 = 0,
|
||||
GTP_V1,
|
||||
};
|
||||
|
||||
int decode_gtp(gtp_info_t *packet, const uint8_t *data, uint32_t len)
|
||||
{
|
||||
if (len < sizeof(gtp1_header_t))
|
||||
{
|
||||
LOG_ERROR("Parser GTP Header: packet length too small %d", len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
packet->hdr = (gtp1_header_t *)data;
|
||||
if (GTP1_GET_FLAGS(packet->hdr) != GTP_V1)
|
||||
{
|
||||
LOG_ERROR("Parser GTP Header: invalid gtp flags %d", GTP1_GET_FLAGS(packet->hdr));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (GTP1_GET_TYPE(packet->hdr) != GTP_TPDU)
|
||||
{
|
||||
LOG_ERROR("Parser GTP Header: invalid gtp type %d", GTP1_GET_TYPE(packet->hdr));
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* From 29.060: "This field shall be present if and only if any one or
|
||||
* more of the S, PN and E flags are set.".
|
||||
*
|
||||
* If any of the bit is set, then the remaining ones also have to be set.
|
||||
*/
|
||||
packet->hdr_len = GTP1_GET_HLEN(packet->hdr);
|
||||
packet->payload = (uint8_t *)data + packet->hdr_len;
|
||||
packet->payload_len = len - packet->hdr_len;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void dump_gtp_info(uint32_t pkt_id, gtp_info_t *packet)
|
||||
{
|
||||
LOG_DEBUG("id: %u, gtp_info: {hdr_len: %u, data_len: %u}",
|
||||
pkt_id,
|
||||
packet->hdr_len,
|
||||
packet->payload_len);
|
||||
}
|
||||
68
common/src/decode_ipv4.c
Normal file
68
common/src/decode_ipv4.c
Normal file
@@ -0,0 +1,68 @@
|
||||
#include "decode_ipv4.h"
|
||||
|
||||
#define IPV4_GET_HLEN(ip4_hdr) (((ip4_hdr)->ip_verhl & 0x0f) << 2)
|
||||
#define IPV4_GET_IPPROTO(ip4_hdr) ((ip4_hdr)->ip_proto)
|
||||
#define IPV4_GET_IPLEN(ip4_hdr) ((uint16_t)ntohs((ip4_hdr)->ip_len))
|
||||
#define IPV4_GET_SRC_ADDR(ip4_hdr) ((ip4_hdr)->ip4_hdrun1.ip4_un1.ip_src)
|
||||
#define IPV4_GET_DST_ADDR(ip4_hdr) ((ip4_hdr)->ip4_hdrun1.ip4_un1.ip_dst)
|
||||
|
||||
int decode_ipv4(ipv4_info_t *packet, const uint8_t *data, uint32_t len)
|
||||
{
|
||||
// 检查包长是否大于 IPv4 header
|
||||
if (len < IPV4_HEADER_LEN)
|
||||
{
|
||||
LOG_ERROR("Parser IPv4 Header: packet length too small %d", len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 检查 IPv4 header version
|
||||
if (IP_GET_RAW_VER(data) != 4)
|
||||
{
|
||||
LOG_ERROR("Parser IPv4 Header: invalid IP version %d", IP_GET_RAW_VER(data));
|
||||
return -1;
|
||||
}
|
||||
|
||||
packet->hdr = (ipv4_header_t *)data;
|
||||
// 检查 IPv4 header length
|
||||
if (IPV4_GET_HLEN(packet->hdr) < IPV4_HEADER_LEN)
|
||||
{
|
||||
LOG_ERROR("Parser IPv4 Header: invalid IP header length %d", IPV4_GET_HLEN(packet->hdr));
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 检查 IPv4 header total length
|
||||
if (IPV4_GET_IPLEN(packet->hdr) < IPV4_GET_HLEN(packet->hdr))
|
||||
{
|
||||
LOG_ERROR("Parser IPv4 Header: invalid IP header total length %d", IPV4_GET_IPLEN(packet->hdr));
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 检查是否 IP 分片
|
||||
if (len < IPV4_GET_IPLEN(packet->hdr))
|
||||
{
|
||||
LOG_ERROR("Parser IPv4 Header: trunc packet");
|
||||
return -1;
|
||||
}
|
||||
|
||||
inet_ntop(AF_INET, &IPV4_GET_SRC_ADDR(packet->hdr), packet->src_addr, sizeof(packet->src_addr));
|
||||
inet_ntop(AF_INET, &IPV4_GET_DST_ADDR(packet->hdr), packet->dst_addr, sizeof(packet->dst_addr));
|
||||
|
||||
packet->next_protocol = IPV4_GET_IPPROTO(packet->hdr);
|
||||
packet->hdr_len = IPV4_GET_HLEN(packet->hdr);
|
||||
packet->opts_len = packet->hdr_len - IPV4_HEADER_LEN;
|
||||
packet->payload_len = len - packet->hdr_len;
|
||||
packet->payload = (uint8_t *)data + packet->hdr_len;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void dump_ipv4_info(uint32_t pkt_id, ipv4_info_t *packet)
|
||||
{
|
||||
LOG_DEBUG("id: %u, ipv4_info: {src_addr: %s, dst_addr: %s, hdr_len: %u, opt_len: %u, data_len: %u}",
|
||||
pkt_id,
|
||||
packet->src_addr,
|
||||
packet->dst_addr,
|
||||
packet->hdr_len,
|
||||
packet->opts_len,
|
||||
packet->payload_len);
|
||||
}
|
||||
55
common/src/decode_ipv6.c
Normal file
55
common/src/decode_ipv6.c
Normal file
@@ -0,0 +1,55 @@
|
||||
#include "decode_ipv6.h"
|
||||
|
||||
#define IPV6_GET_PLEN(ip6_hdr) ((uint16_t)ntohs((ip6_hdr)->ip6_hdrun.ip6_un1.ip6_un1_plen))
|
||||
#define IPV6_GET_NH(ip6_hdr) ((ip6_hdr)->ip6_hdrun.ip6_un1.ip6_un1_nxt)
|
||||
#define IPV6_GET_SRC_ADDR(ip6_hdr) ((ip6_hdr)->ip6_hdrun2.ip6_un2.ip6_src)
|
||||
#define IPV6_GET_DST_ADDR(ip6_hdr) ((ip6_hdr)->ip6_hdrun2.ip6_un2.ip6_dst)
|
||||
|
||||
int decode_ipv6(ipv6_info_t *packet, const uint8_t *data, uint32_t len)
|
||||
{
|
||||
if (len < IPV6_HEADER_LEN)
|
||||
{
|
||||
LOG_ERROR("Parser IPv6 Header: packet length too small %d", len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 检查 IPv6 header version
|
||||
if (IP_GET_RAW_VER(data) != 6)
|
||||
{
|
||||
LOG_ERROR("Parser IPv6 Header: invalid IP version %d", IP_GET_RAW_VER(data));
|
||||
return -1;
|
||||
}
|
||||
|
||||
packet->hdr = (ipv6_header_t *)data;
|
||||
if (len < (IPV6_HEADER_LEN + IPV6_GET_PLEN(packet->hdr)))
|
||||
{
|
||||
LOG_ERROR("Parser IPv6 Header: trunc packet");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (len != (IPV6_HEADER_LEN + IPV6_GET_PLEN(packet->hdr)))
|
||||
{
|
||||
LOG_ERROR("Parser IPv6 Header: invalid payload length %d", IPV6_GET_PLEN(packet->hdr));
|
||||
return -1;
|
||||
}
|
||||
|
||||
inet_ntop(AF_INET6, &IPV6_GET_SRC_ADDR(packet->hdr), packet->src_addr, sizeof(packet->src_addr));
|
||||
inet_ntop(AF_INET6, &IPV6_GET_DST_ADDR(packet->hdr), packet->dst_addr, sizeof(packet->dst_addr));
|
||||
|
||||
packet->next_protocol = IPV6_GET_NH(packet->hdr);
|
||||
packet->hdr_len = IPV6_HEADER_LEN;
|
||||
packet->payload = (uint8_t *)data + packet->hdr_len;
|
||||
packet->payload_len = len - packet->hdr_len;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void dump_ipv6_info(uint32_t pkt_id, ipv6_info_t *packet)
|
||||
{
|
||||
LOG_DEBUG("id: %u, ipv6_info: {src_addr: %s, dst_addr: %s, hdr_len: %u, data_len: %u}",
|
||||
pkt_id,
|
||||
packet->src_addr,
|
||||
packet->dst_addr,
|
||||
packet->hdr_len,
|
||||
packet->payload_len);
|
||||
}
|
||||
52
common/src/decode_tcp.c
Normal file
52
common/src/decode_tcp.c
Normal file
@@ -0,0 +1,52 @@
|
||||
#include "decode_tcp.h"
|
||||
|
||||
#define TCP_OPTLENMAX 40
|
||||
|
||||
#define TCP_GET_HLEN(tcp_hdr) ((((tcp_hdr)->th_offx2 & 0xf0) >> 4) << 2)
|
||||
#define TCP_GET_SRC_PORT(tcp_hdr) ((uint16_t)ntohs((tcp_hdr)->th_sport))
|
||||
#define TCP_GET_DST_PORT(tcp_hdr) ((uint16_t)ntohs((tcp_hdr)->th_dport))
|
||||
|
||||
int decode_tcp(tcp_info_t *packet, const uint8_t *data, uint32_t len)
|
||||
{
|
||||
if (len < TCP_HEADER_LEN)
|
||||
{
|
||||
LOG_ERROR("Parser TCP Header: packet length too small %d", len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
packet->hdr = (tcp_header_t *)data;
|
||||
uint8_t hlen = TCP_GET_HLEN(packet->hdr);
|
||||
if (len < hlen)
|
||||
{
|
||||
LOG_ERROR("Parser TCP Header: TCP packet too small %d", len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint8_t tcp_opt_len = hlen - TCP_HEADER_LEN;
|
||||
if (tcp_opt_len > TCP_OPTLENMAX)
|
||||
{
|
||||
LOG_ERROR("Parser TCP Header: invalid opt length %d", tcp_opt_len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
packet->opt_len = tcp_opt_len;
|
||||
packet->src_port = TCP_GET_SRC_PORT(packet->hdr);
|
||||
packet->dst_port = TCP_GET_DST_PORT(packet->hdr);
|
||||
|
||||
packet->hdr_len = hlen;
|
||||
packet->payload = (uint8_t *)data + packet->hdr_len;
|
||||
packet->payload_len = len - packet->hdr_len;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void dump_tcp_info(uint32_t pkt_id, tcp_info_t *packet)
|
||||
{
|
||||
LOG_DEBUG("id: %u, tcp_info: {src_port: %u, dst_port: %u, hdr_len: %u, opt_len: %u, data_len:%u}",
|
||||
pkt_id,
|
||||
packet->src_port,
|
||||
packet->dst_port,
|
||||
packet->hdr_len,
|
||||
packet->opt_len,
|
||||
packet->payload_len);
|
||||
}
|
||||
48
common/src/decode_udp.c
Normal file
48
common/src/decode_udp.c
Normal file
@@ -0,0 +1,48 @@
|
||||
#include "decode_udp.h"
|
||||
|
||||
#define UDP_GET_LEN(udp_hdr) ((uint16_t)ntohs((udp_hdr)->udp_len))
|
||||
#define UDP_GET_SRC_PORT(udp_hdr) ((uint16_t)ntohs((udp_hdr)->udp_src_port))
|
||||
#define UDP_GET_DST_PORT(udp_hdr) ((uint16_t)ntohs((udp_hdr)->udp_dst_port))
|
||||
|
||||
int decode_udp(udp_info_t *packet, const uint8_t *data, uint32_t len)
|
||||
{
|
||||
if (len < UDP_HEADER_LEN)
|
||||
{
|
||||
LOG_ERROR("Parser UDP Header: packet length too small %d", len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
packet->hdr = (udp_header_t *)data;
|
||||
// 检查 UDP header len
|
||||
if (len < UDP_GET_LEN(packet->hdr))
|
||||
{
|
||||
LOG_ERROR("Parser UDP Header: UDP packet too small %d", len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 检查 UDP header len
|
||||
if (len != UDP_GET_LEN(packet->hdr))
|
||||
{
|
||||
LOG_ERROR("Parser UDP Header: invalid UDP header length %d", UDP_GET_LEN(packet->hdr));
|
||||
return -1;
|
||||
}
|
||||
|
||||
packet->src_port = UDP_GET_SRC_PORT(packet->hdr);
|
||||
packet->dst_port = UDP_GET_DST_PORT(packet->hdr);
|
||||
|
||||
packet->hdr_len = UDP_HEADER_LEN;
|
||||
packet->payload = (uint8_t *)data + UDP_HEADER_LEN;
|
||||
packet->payload_len = len - UDP_HEADER_LEN;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void dump_udp_info(uint32_t pkt_id, udp_info_t *packet)
|
||||
{
|
||||
LOG_DEBUG("id: %u, udp_info: {src_port: %u, dst_port: %u, hdr_len: %u, data_len: %u}",
|
||||
pkt_id,
|
||||
packet->src_port,
|
||||
packet->dst_port,
|
||||
packet->hdr_len,
|
||||
packet->payload_len);
|
||||
}
|
||||
869
nfqnl_test.c
869
nfqnl_test.c
@@ -1,869 +0,0 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include <netinet/ip.h>
|
||||
#include <netinet/ip6.h>
|
||||
|
||||
#include <linux/tcp.h>
|
||||
#include <linux/netfilter.h> // for NF_ACCEPT
|
||||
#include <libnetfilter_queue/libnetfilter_queue.h>
|
||||
|
||||
#include "log.h"
|
||||
|
||||
#define GTP_TPDU 255
|
||||
#define GTP1U_PORT 2152
|
||||
#define GTP1_F_MASK 0x07
|
||||
|
||||
#define UDP_HEADER_LEN 8
|
||||
#define TCP_HEADER_LEN 20
|
||||
#define TCP_OPTLENMAX 40
|
||||
|
||||
#define IPV4_HEADER_LEN 20
|
||||
#define IPV6_HEADER_LEN 40
|
||||
|
||||
#define IP_GET_RAW_VER(raw_pkt) ((((raw_pkt)[0] & 0xf0) >> 4))
|
||||
|
||||
#define IPV6_GET_PLEN(ip6_hdr) ((uint16_t)ntohs((ip6_hdr)->ip6_hdrun.ip6_un1.ip6_un1_plen))
|
||||
#define IPV6_GET_NH(ip6_hdr) ((ip6_hdr)->ip6_hdrun.ip6_un1.ip6_un1_nxt)
|
||||
#define IPV6_GET_SRC_ADDR(ip6_hdr) ((ip6_hdr)->ip6_hdrun2.ip6_un2.ip6_src)
|
||||
#define IPV6_GET_DST_ADDR(ip6_hdr) ((ip6_hdr)->ip6_hdrun2.ip6_un2.ip6_dst)
|
||||
|
||||
#define IPV4_GET_HLEN(ip4_hdr) (((ip4_hdr)->ip_verhl & 0x0f) << 2)
|
||||
#define IPV4_GET_IPPROTO(ip4_hdr) ((ip4_hdr)->ip_proto)
|
||||
#define IPV4_GET_IPLEN(ip4_hdr) ((uint16_t)ntohs((ip4_hdr)->ip_len))
|
||||
#define IPV4_GET_SRC_ADDR(ip4_hdr) ((ip4_hdr)->ip4_hdrun1.ip4_un1.ip_src)
|
||||
#define IPV4_GET_DST_ADDR(ip4_hdr) ((ip4_hdr)->ip4_hdrun1.ip4_un1.ip_dst)
|
||||
|
||||
#define UDP_GET_LEN(udp_hdr) ((uint16_t)ntohs((udp_hdr)->udp_len))
|
||||
#define UDP_GET_SRC_PORT(udp_hdr) ((uint16_t)ntohs((udp_hdr)->udp_src_port))
|
||||
#define UDP_GET_DST_PORT(udp_hdr) ((uint16_t)ntohs((udp_hdr)->udp_dst_port))
|
||||
|
||||
#define TCP_GET_HLEN(tcp_hdr) ((((tcp_hdr)->th_offx2 & 0xf0) >> 4) << 2)
|
||||
#define TCP_GET_SRC_PORT(tcp_hdr) ((uint16_t)ntohs((tcp_hdr)->th_sport))
|
||||
#define TCP_GET_DST_PORT(tcp_hdr) ((uint16_t)ntohs((tcp_hdr)->th_dport))
|
||||
|
||||
#define GTP1_GET_TYPE(gtp1_hdr) ((gtp1_hdr)->type)
|
||||
#define GTP1_GET_FLAGS(gtp1_hdr) ((gtp1_hdr)->flags >> 5)
|
||||
#define GTP1_GET_HLEN(gtp1_hdr) (((gtp1_hdr)->flags & GTP1_F_MASK) > 0 ? 12 : 8)
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// IPv4 IPv6 UDP GPT Header Struct
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
typedef struct ipv6_header_s
|
||||
{
|
||||
union
|
||||
{
|
||||
struct ip6_un1_
|
||||
{
|
||||
uint32_t ip6_un1_flow; /* 20 bits of flow-ID */
|
||||
uint16_t ip6_un1_plen; /* payload length */
|
||||
uint8_t ip6_un1_nxt; /* next header */
|
||||
uint8_t ip6_un1_hlim; /* hop limit */
|
||||
} ip6_un1;
|
||||
uint8_t ip6_un2_vfc; /* 4 bits version, top 4 bits class */
|
||||
} ip6_hdrun;
|
||||
|
||||
union
|
||||
{
|
||||
struct
|
||||
{
|
||||
uint32_t ip6_src[4];
|
||||
uint32_t ip6_dst[4];
|
||||
} ip6_un2;
|
||||
uint16_t ip6_addrs[16];
|
||||
} ip6_hdrun2;
|
||||
} ipv6_header_t;
|
||||
|
||||
typedef struct ipv4_header_s
|
||||
{
|
||||
uint8_t ip_verhl; // version & header length
|
||||
uint8_t ip_tos;
|
||||
uint16_t ip_len;
|
||||
uint16_t ip_id;
|
||||
uint16_t ip_off;
|
||||
uint8_t ip_ttl;
|
||||
uint8_t ip_proto;
|
||||
uint16_t ip_csum;
|
||||
union
|
||||
{
|
||||
struct
|
||||
{
|
||||
struct in_addr ip_src;
|
||||
struct in_addr ip_dst;
|
||||
} ip4_un1;
|
||||
uint16_t ip_addrs[4];
|
||||
} ip4_hdrun1;
|
||||
} ipv4_header_t;
|
||||
|
||||
typedef struct tcp_header_s
|
||||
{
|
||||
uint16_t th_sport; /**< source port */
|
||||
uint16_t th_dport; /**< destination port */
|
||||
uint32_t th_seq; /**< sequence number */
|
||||
uint32_t th_ack; /**< acknowledgement number */
|
||||
uint8_t th_offx2; /**< offset and reserved */
|
||||
uint8_t th_flags; /**< pkt flags */
|
||||
uint16_t th_win; /**< pkt window */
|
||||
uint16_t th_sum; /**< checksum */
|
||||
uint16_t th_urp; /**< urgent pointer */
|
||||
} __attribute__((__packed__)) tcp_header_t;
|
||||
|
||||
typedef struct udp_header_s
|
||||
{
|
||||
uint16_t udp_src_port;
|
||||
uint16_t udp_dst_port;
|
||||
uint16_t udp_len;
|
||||
uint16_t udp_sum;
|
||||
} __attribute__((__packed__)) udp_header_t;
|
||||
|
||||
enum gtp_version_e
|
||||
{
|
||||
GTP_V0 = 0,
|
||||
GTP_V1,
|
||||
};
|
||||
|
||||
/* According to 3GPP TS 29.060. */
|
||||
typedef struct gtp1_header_s
|
||||
{
|
||||
uint8_t flags;
|
||||
uint8_t type;
|
||||
uint16_t length;
|
||||
uint32_t tid;
|
||||
} __attribute__((packed)) gtp1_header_t;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// IPv4 IPv6 UDP GPT Header Parser
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
typedef enum network_mode_s
|
||||
{
|
||||
NONE = 0,
|
||||
IPv4 = 1,
|
||||
IPv6 = 2,
|
||||
TCP = 3,
|
||||
UDP = 4,
|
||||
} network_mode_t;
|
||||
|
||||
typedef struct pkt_info_s
|
||||
{
|
||||
uint32_t id;
|
||||
uint8_t *payload;
|
||||
uint32_t payload_len;
|
||||
} pkt_info_t;
|
||||
|
||||
typedef struct ipv4_info_s
|
||||
{
|
||||
char src_addr[INET_ADDRSTRLEN];
|
||||
char dst_addr[INET_ADDRSTRLEN];
|
||||
|
||||
ipv4_header_t *hdr;
|
||||
uint8_t *payload;
|
||||
|
||||
uint32_t hdr_len;
|
||||
uint32_t opts_len;
|
||||
uint32_t payload_len;
|
||||
} ipv4_info_t;
|
||||
|
||||
typedef struct ipv6_info_s
|
||||
{
|
||||
char src_addr[INET6_ADDRSTRLEN];
|
||||
char dst_addr[INET6_ADDRSTRLEN];
|
||||
|
||||
ipv6_header_t *hdr;
|
||||
uint8_t *payload;
|
||||
|
||||
uint32_t hdr_len;
|
||||
uint32_t payload_len;
|
||||
} ipv6_info_t;
|
||||
|
||||
typedef struct udp_info_s
|
||||
{
|
||||
uint16_t src_port;
|
||||
uint16_t dst_port;
|
||||
|
||||
udp_header_t *hdr;
|
||||
uint8_t *payload;
|
||||
|
||||
uint32_t hdr_len;
|
||||
uint32_t payload_len;
|
||||
} udp_info_t;
|
||||
|
||||
typedef struct tcp_info_s
|
||||
{
|
||||
uint16_t src_port;
|
||||
uint16_t dst_port;
|
||||
|
||||
tcp_header_t *hdr;
|
||||
uint8_t *payload;
|
||||
|
||||
uint32_t opt_len;
|
||||
uint32_t hdr_len;
|
||||
uint32_t payload_len;
|
||||
} tcp_info_t;
|
||||
|
||||
typedef struct gtp_info_s
|
||||
{
|
||||
gtp1_header_t *hdr;
|
||||
uint8_t *payload;
|
||||
|
||||
uint32_t hdr_len;
|
||||
uint32_t payload_len;
|
||||
} gtp_info_t;
|
||||
|
||||
typedef struct pkt_paser_s
|
||||
{
|
||||
pkt_info_t raw_pkt;
|
||||
|
||||
ipv4_info_t external_ipv4;
|
||||
ipv6_info_t external_ipv6;
|
||||
udp_info_t external_udp;
|
||||
gtp_info_t external_gtp;
|
||||
ipv4_info_t internal_ipv4;
|
||||
ipv6_info_t internal_ipv6;
|
||||
udp_info_t internal_udp;
|
||||
tcp_info_t internal_tcp;
|
||||
|
||||
network_mode_t external_ip_version;
|
||||
network_mode_t external_l4_version;
|
||||
network_mode_t internal_ip_version;
|
||||
network_mode_t internal_l4_version;
|
||||
|
||||
} pkt_paser_t;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Packet Parser API
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static void dump_info(pkt_paser_t *pkt_parser)
|
||||
{
|
||||
|
||||
LOG_DEBUG("raw_pkt: {id: %u, data_len: %u}", pkt_parser->raw_pkt.id, pkt_parser->raw_pkt.payload_len);
|
||||
|
||||
if (pkt_parser->external_ipv4.hdr)
|
||||
{
|
||||
LOG_DEBUG("id: %u, external_ipv4: {src_addr:%s,dst_addr:%s,hdr_len: %u, opt_len: %u, data_len: %u}",
|
||||
pkt_parser->raw_pkt.id,
|
||||
pkt_parser->external_ipv4.src_addr,
|
||||
pkt_parser->external_ipv4.dst_addr,
|
||||
pkt_parser->external_ipv4.hdr_len,
|
||||
pkt_parser->external_ipv4.opts_len,
|
||||
pkt_parser->external_ipv4.payload_len);
|
||||
}
|
||||
if (pkt_parser->external_ipv6.hdr)
|
||||
{
|
||||
LOG_DEBUG("id: %u, external_ipv6: {src_addr:%s,dst_addr:%s,hdr_len: %u, data_len: %u}",
|
||||
pkt_parser->raw_pkt.id,
|
||||
pkt_parser->external_ipv6.src_addr,
|
||||
pkt_parser->external_ipv6.dst_addr,
|
||||
pkt_parser->external_ipv6.hdr_len,
|
||||
pkt_parser->external_ipv6.payload_len);
|
||||
}
|
||||
if (pkt_parser->external_udp.hdr)
|
||||
{
|
||||
LOG_DEBUG("id: %u, external_udp: {src_port: %u, dst_port: %u, hdr_len: %u, data_len: %u}",
|
||||
pkt_parser->raw_pkt.id,
|
||||
pkt_parser->external_udp.src_port,
|
||||
pkt_parser->external_udp.dst_port,
|
||||
pkt_parser->external_udp.hdr_len,
|
||||
pkt_parser->external_udp.payload_len);
|
||||
}
|
||||
|
||||
if (pkt_parser->external_gtp.hdr)
|
||||
{
|
||||
LOG_DEBUG("id: %u, external_gtp: {hdr_len: %u, data_len: %u}",
|
||||
pkt_parser->raw_pkt.id,
|
||||
pkt_parser->external_gtp.hdr_len,
|
||||
pkt_parser->external_gtp.payload_len);
|
||||
}
|
||||
if (pkt_parser->internal_ipv4.hdr)
|
||||
{
|
||||
LOG_DEBUG("id: %u, internal_ipv4: {src_addr:%s,dst_addr:%s,hdr_len: %u, opt_len: %u, data_len: %u}",
|
||||
pkt_parser->raw_pkt.id,
|
||||
pkt_parser->internal_ipv4.src_addr,
|
||||
pkt_parser->internal_ipv4.dst_addr,
|
||||
pkt_parser->internal_ipv4.hdr_len,
|
||||
pkt_parser->internal_ipv4.opts_len,
|
||||
pkt_parser->internal_ipv4.payload_len);
|
||||
}
|
||||
if (pkt_parser->internal_ipv6.hdr)
|
||||
{
|
||||
LOG_DEBUG("id: %u, interna_ipv6: {src_addr:%s,dst_addr:%s,hdr_len: %u, data_len: %u}",
|
||||
pkt_parser->raw_pkt.id,
|
||||
pkt_parser->internal_ipv6.src_addr,
|
||||
pkt_parser->internal_ipv6.dst_addr,
|
||||
pkt_parser->internal_ipv6.hdr_len,
|
||||
pkt_parser->internal_ipv6.payload_len);
|
||||
}
|
||||
if (pkt_parser->internal_udp.hdr)
|
||||
{
|
||||
LOG_DEBUG("id: %u, internal_udp: {src_port: %u, dst_port: %u, hdr_len: %u, data_len: %u}",
|
||||
pkt_parser->raw_pkt.id,
|
||||
pkt_parser->internal_udp.src_port,
|
||||
pkt_parser->internal_udp.dst_port,
|
||||
pkt_parser->internal_udp.hdr_len,
|
||||
pkt_parser->internal_udp.payload_len);
|
||||
}
|
||||
if (pkt_parser->internal_tcp.hdr)
|
||||
{
|
||||
LOG_DEBUG("id: %u, internal_tcp: {src_port: %u, dst_port: %u, hdr_len: %u, data_len:%u}",
|
||||
pkt_parser->raw_pkt.id,
|
||||
pkt_parser->internal_tcp.src_port,
|
||||
pkt_parser->internal_tcp.dst_port,
|
||||
pkt_parser->internal_tcp.hdr_len,
|
||||
pkt_parser->internal_tcp.payload_len);
|
||||
}
|
||||
}
|
||||
|
||||
static int decode_gtp(gtp_info_t *packet, const uint8_t *data, uint32_t len)
|
||||
{
|
||||
if (len < sizeof(gtp1_header_t))
|
||||
{
|
||||
LOG_ERROR("Parser GTP Header: packet length too small %d", len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
packet->hdr = (gtp1_header_t *)data;
|
||||
if (GTP1_GET_FLAGS(packet->hdr) != GTP_V1)
|
||||
{
|
||||
LOG_ERROR("Parser GTP Header: invalid gtp flags %d", GTP1_GET_FLAGS(packet->hdr));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (GTP1_GET_TYPE(packet->hdr) != GTP_TPDU)
|
||||
{
|
||||
LOG_ERROR("Parser GTP Header: invalid gtp type %d", GTP1_GET_TYPE(packet->hdr));
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* From 29.060: "This field shall be present if and only if any one or
|
||||
* more of the S, PN and E flags are set.".
|
||||
*
|
||||
* If any of the bit is set, then the remaining ones also have to be set.
|
||||
*/
|
||||
packet->hdr_len = GTP1_GET_HLEN(packet->hdr);
|
||||
packet->payload = (uint8_t *)data + packet->hdr_len;
|
||||
packet->payload_len = len - packet->hdr_len;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int decode_udp(udp_info_t *packet, const uint8_t *data, uint32_t len)
|
||||
{
|
||||
if (len < UDP_HEADER_LEN)
|
||||
{
|
||||
LOG_ERROR("Parser UDP Header: packet length too small %d", len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
packet->hdr = (udp_header_t *)data;
|
||||
// 检查 UDP header len
|
||||
if (len < UDP_GET_LEN(packet->hdr))
|
||||
{
|
||||
LOG_ERROR("Parser UDP Header: UDP packet too small %d", len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 检查 UDP header len
|
||||
if (len != UDP_GET_LEN(packet->hdr))
|
||||
{
|
||||
LOG_ERROR("Parser UDP Header: invalid UDP header length %d", UDP_GET_LEN(packet->hdr));
|
||||
return -1;
|
||||
}
|
||||
|
||||
packet->src_port = UDP_GET_SRC_PORT(packet->hdr);
|
||||
packet->dst_port = UDP_GET_DST_PORT(packet->hdr);
|
||||
|
||||
packet->hdr_len = UDP_HEADER_LEN;
|
||||
packet->payload = (uint8_t *)data + UDP_HEADER_LEN;
|
||||
packet->payload_len = len - UDP_HEADER_LEN;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int decode_tcp(tcp_info_t *packet, const uint8_t *data, uint32_t len)
|
||||
{
|
||||
if (len < TCP_HEADER_LEN)
|
||||
{
|
||||
LOG_ERROR("Parser TCP Header: packet length too small %d", len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
packet->hdr = (tcp_header_t *)data;
|
||||
uint8_t hlen = TCP_GET_HLEN(packet->hdr);
|
||||
if (len < hlen)
|
||||
{
|
||||
LOG_ERROR("Parser TCP Header: TCP packet too small %d", len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint8_t tcp_opt_len = hlen - TCP_HEADER_LEN;
|
||||
if (tcp_opt_len > TCP_OPTLENMAX)
|
||||
{
|
||||
LOG_ERROR("Parser TCP Header: invalid opt length %d", tcp_opt_len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
packet->opt_len = tcp_opt_len;
|
||||
packet->src_port = TCP_GET_SRC_PORT(packet->hdr);
|
||||
packet->dst_port = TCP_GET_DST_PORT(packet->hdr);
|
||||
|
||||
packet->hdr_len = hlen;
|
||||
packet->payload = (uint8_t *)data + packet->hdr_len;
|
||||
packet->payload_len = len = packet->hdr_len;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int decode_ipv6(ipv6_info_t *packet, const uint8_t *data, uint32_t len)
|
||||
{
|
||||
if (len < IPV6_HEADER_LEN)
|
||||
{
|
||||
LOG_ERROR("Parser IPv6 Header: packet length too small %d", len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 检查 IPv6 header version
|
||||
if (IP_GET_RAW_VER(data) != 6)
|
||||
{
|
||||
LOG_ERROR("Parser IPv6 Header: unknown IP version %d", IP_GET_RAW_VER(data));
|
||||
return -1;
|
||||
}
|
||||
|
||||
packet->hdr = (ipv6_header_t *)data;
|
||||
if (len < (IPV6_HEADER_LEN + IPV6_GET_PLEN(packet->hdr)))
|
||||
{
|
||||
LOG_ERROR("Parser IPv6 Header: trunc packet");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (len != (IPV6_HEADER_LEN + IPV6_GET_PLEN(packet->hdr)))
|
||||
{
|
||||
LOG_ERROR("Parser IPv6 Header: invalid payload length %d", IPV6_GET_PLEN(packet->hdr));
|
||||
return -1;
|
||||
}
|
||||
|
||||
inet_ntop(AF_INET6, &IPV6_GET_SRC_ADDR(packet->hdr), packet->src_addr, sizeof(packet->src_addr));
|
||||
inet_ntop(AF_INET6, &IPV6_GET_DST_ADDR(packet->hdr), packet->dst_addr, sizeof(packet->dst_addr));
|
||||
|
||||
packet->hdr_len = IPV6_HEADER_LEN;
|
||||
packet->payload = (uint8_t *)data + packet->hdr_len;
|
||||
packet->payload_len = len - packet->hdr_len;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int decode_ipv4(ipv4_info_t *packet, const uint8_t *data, uint32_t len)
|
||||
{
|
||||
// 检查包长是否大于 IPv4 header
|
||||
if (len < IPV4_HEADER_LEN)
|
||||
{
|
||||
LOG_ERROR("Parser IPv4 Header: packet length too small %d", len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 检查 IPv4 header version
|
||||
if (IP_GET_RAW_VER(data) != 4)
|
||||
{
|
||||
LOG_ERROR("Parser IPv4 Header: unknown IP version %d", IP_GET_RAW_VER(data));
|
||||
return -1;
|
||||
}
|
||||
|
||||
packet->hdr = (ipv4_header_t *)data;
|
||||
// 检查 IPv4 header length
|
||||
if (IPV4_GET_HLEN(packet->hdr) < IPV4_HEADER_LEN)
|
||||
{
|
||||
LOG_ERROR("Parser IPv4 Header: invalid IP header length %d", IPV4_GET_HLEN(packet->hdr));
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 检查 IPv4 header total length
|
||||
if (IPV4_GET_IPLEN(packet->hdr) < IPV4_GET_HLEN(packet->hdr))
|
||||
{
|
||||
LOG_ERROR("Parser IPv4 Header: invalid IP header total length %d", IPV4_GET_IPLEN(packet->hdr));
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 检查是否 IP 分片
|
||||
if (len < IPV4_GET_IPLEN(packet->hdr))
|
||||
{
|
||||
LOG_ERROR("Parser IPv4 Header: trunc packet");
|
||||
return -1;
|
||||
}
|
||||
|
||||
inet_ntop(AF_INET, &IPV4_GET_SRC_ADDR(packet->hdr), packet->src_addr, sizeof(packet->src_addr));
|
||||
inet_ntop(AF_INET, &IPV4_GET_DST_ADDR(packet->hdr), packet->dst_addr, sizeof(packet->dst_addr));
|
||||
|
||||
packet->hdr_len = IPV4_GET_HLEN(packet->hdr);
|
||||
packet->opts_len = packet->hdr_len - IPV4_HEADER_LEN;
|
||||
packet->payload_len = len - packet->hdr_len;
|
||||
packet->payload = (uint8_t *)data + packet->hdr_len;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int decode_internal_ip_pkt(pkt_paser_t *pkt_parser, const uint8_t *data, uint32_t len)
|
||||
{
|
||||
int next_protocol = 0;
|
||||
uint8_t *payload = NULL;
|
||||
uint32_t payload_len = 0;
|
||||
|
||||
// IPv4/IPv6
|
||||
if (len < IPV4_HEADER_LEN)
|
||||
{
|
||||
LOG_ERROR("Parser Internal Raw header: packet length too small %d", len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (IP_GET_RAW_VER(data) == 4)
|
||||
{
|
||||
if (decode_ipv4(&(pkt_parser->internal_ipv4), data, len) == -1)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
pkt_parser->internal_ip_version = IPv4;
|
||||
payload = pkt_parser->internal_ipv4.payload;
|
||||
payload_len = pkt_parser->internal_ipv4.payload_len;
|
||||
next_protocol = IPV4_GET_IPPROTO(pkt_parser->internal_ipv4.hdr);
|
||||
}
|
||||
else if (IP_GET_RAW_VER(data) == 6)
|
||||
{
|
||||
if (decode_ipv6(&(pkt_parser->internal_ipv6), data, len) == -1)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
pkt_parser->internal_ip_version = IPv6;
|
||||
payload = pkt_parser->internal_ipv6.payload;
|
||||
payload_len = pkt_parser->internal_ipv6.payload_len;
|
||||
next_protocol = IPV6_GET_NH(pkt_parser->internal_ipv6.hdr);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_ERROR("Unknown Internal IP version %d", IP_GET_RAW_VER(data));
|
||||
return -1;
|
||||
}
|
||||
|
||||
// TCP/UDP
|
||||
if (next_protocol == IPPROTO_UDP)
|
||||
{
|
||||
if (decode_udp(&(pkt_parser->internal_udp), payload, payload_len) == -1)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
pkt_parser->internal_l4_version = UDP;
|
||||
return 0;
|
||||
}
|
||||
else if (next_protocol == IPPROTO_TCP)
|
||||
{
|
||||
if (decode_tcp(&(pkt_parser->internal_tcp), payload, payload_len) == -1)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
pkt_parser->internal_l4_version = TCP;
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_ERROR("Unknown Internal L4 next_protocol version %d", next_protocol);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
static int decode_external_ip_pkt(pkt_paser_t *pkt_parser, const uint8_t *data, uint32_t len)
|
||||
{
|
||||
int next_protocol = 0;
|
||||
uint8_t *payload = NULL;
|
||||
uint32_t payload_len = 0;
|
||||
|
||||
// IPv4/IPv6
|
||||
if (len < IPV4_HEADER_LEN)
|
||||
{
|
||||
LOG_ERROR("Parser External Raw header: packet length too small %d", len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (IP_GET_RAW_VER(data) == 4)
|
||||
{
|
||||
if (decode_ipv4(&(pkt_parser->external_ipv4), data, len) == -1)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
pkt_parser->external_ip_version = IPv4;
|
||||
payload = pkt_parser->external_ipv4.payload;
|
||||
payload_len = pkt_parser->external_ipv4.payload_len;
|
||||
next_protocol = IPV4_GET_IPPROTO(pkt_parser->external_ipv4.hdr);
|
||||
}
|
||||
else if (IP_GET_RAW_VER(data) == 6)
|
||||
{
|
||||
if (decode_ipv6(&(pkt_parser->external_ipv6), data, len) == -1)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
pkt_parser->external_ip_version = IPv6;
|
||||
payload = pkt_parser->external_ipv6.payload;
|
||||
payload_len = pkt_parser->external_ipv6.payload_len;
|
||||
next_protocol = IPV6_GET_NH(pkt_parser->external_ipv6.hdr);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_ERROR("Unknown External IP version %d", IP_GET_RAW_VER(data));
|
||||
return -1;
|
||||
}
|
||||
|
||||
// TCP/UDP
|
||||
if (next_protocol == IPPROTO_UDP)
|
||||
{
|
||||
if (decode_udp(&(pkt_parser->external_udp), payload, payload_len) == -1)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
pkt_parser->external_l4_version = UDP;
|
||||
}
|
||||
else if (next_protocol == IPPROTO_TCP)
|
||||
{
|
||||
pkt_parser->external_l4_version = TCP;
|
||||
LOG_ERROR("Unknown External L4 next_protocol version %d", next_protocol);
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_ERROR("Unknown External L4 next_protocol version %d", next_protocol);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// GTP
|
||||
if (decode_gtp(&(pkt_parser->external_gtp), pkt_parser->external_udp.payload, pkt_parser->external_udp.payload_len) == -1)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// NFQ API
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/*
|
||||
* nfmsg : message objetc that contains the packet
|
||||
* nfa : Netlink packet data handle
|
||||
*/
|
||||
static int packet_handler_cb(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg, struct nfq_data *nfa, void *data)
|
||||
{
|
||||
int offest = 0;
|
||||
int raw_ip_fd = 0;
|
||||
int packet_len = 0;
|
||||
pkt_paser_t pkt_parser = {0};
|
||||
unsigned char *packet_data = NULL;
|
||||
struct nfqnl_msg_packet_hdr *packet_hdr = NULL;
|
||||
|
||||
packet_hdr = nfq_get_msg_packet_hdr(nfa);
|
||||
if (packet_hdr == NULL)
|
||||
{
|
||||
LOG_ERROR("Failed at nfq_get_msg_packet_hdr()");
|
||||
goto end;
|
||||
}
|
||||
|
||||
packet_len = nfq_get_payload(nfa, &packet_data);
|
||||
if (packet_len <= 0)
|
||||
{
|
||||
LOG_ERROR("Failed at nfq_get_payload()");
|
||||
goto end;
|
||||
}
|
||||
|
||||
pkt_parser.raw_pkt.id = ntohl(packet_hdr->packet_id);
|
||||
pkt_parser.raw_pkt.payload = packet_data;
|
||||
pkt_parser.raw_pkt.payload_len = packet_len;
|
||||
if (decode_external_ip_pkt(&pkt_parser, pkt_parser.raw_pkt.payload, pkt_parser.raw_pkt.payload_len) == -1)
|
||||
{
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (decode_internal_ip_pkt(&pkt_parser, pkt_parser.external_gtp.payload, pkt_parser.external_gtp.payload_len) == -1)
|
||||
{
|
||||
goto end;
|
||||
}
|
||||
|
||||
/*
|
||||
* NF_DROP : discarded the packet
|
||||
* NF_ACCEPT : the packet passes, continue iterations
|
||||
* NF_QUEUE : inject the packet into a different queue (the target queue number is in the high 16 bits of the verdict)
|
||||
* NF_REPEAT : iterate the same cycle once more
|
||||
* NF_STOP : accept, but don't continue iterations
|
||||
*/
|
||||
// nfq_set_verdict()
|
||||
// nfq_set_verdict2()
|
||||
// nfq_set_verdict_batch()
|
||||
// nfq_set_verdict_batch2()
|
||||
// nfq_set_verdict_mark()
|
||||
|
||||
if (pkt_parser.external_ip_version == IPv4)
|
||||
{
|
||||
offest += pkt_parser.external_ipv4.hdr_len;
|
||||
}
|
||||
if (pkt_parser.external_ip_version == IPv6)
|
||||
{
|
||||
offest += pkt_parser.external_ipv6.hdr_len;
|
||||
}
|
||||
|
||||
offest += pkt_parser.external_udp.hdr_len;
|
||||
offest += pkt_parser.external_gtp.hdr_len;
|
||||
|
||||
dump_info(&pkt_parser);
|
||||
LOG_DEBUG("Offset : %d", offest);
|
||||
|
||||
if (offest > 0)
|
||||
{
|
||||
if ((pkt_parser.external_ip_version == IPv4 && pkt_parser.internal_ip_version == IPv4) || (pkt_parser.external_ip_version == IPv6 && pkt_parser.internal_ip_version == IPv6))
|
||||
{
|
||||
return nfq_set_verdict(qh, pkt_parser.raw_pkt.id, NF_ACCEPT, packet_len - offest, packet_data + offest);
|
||||
}
|
||||
if (pkt_parser.external_ip_version == IPv4 && pkt_parser.internal_ip_version == IPv6)
|
||||
{
|
||||
struct sockaddr_in6 saddr6 = {0};
|
||||
saddr6.sin6_family = PF_INET6;
|
||||
memcpy(saddr6.sin6_addr.s6_addr, pkt_parser.internal_ipv6.hdr->ip6_hdrun2.ip6_un2.ip6_dst, sizeof(struct in6_addr));
|
||||
raw_ip_fd = socket(PF_INET6, SOCK_RAW, IPPROTO_RAW);
|
||||
if (sendto(raw_ip_fd, packet_data + offest, packet_len - offest, 0, (struct sockaddr *)&saddr6, sizeof(saddr6)) == -1)
|
||||
{
|
||||
LOG_ERROR("Failed at send(), %d: %s", errno, strerror(errno));
|
||||
close(raw_ip_fd);
|
||||
goto end;
|
||||
}
|
||||
close(raw_ip_fd);
|
||||
}
|
||||
else if (pkt_parser.external_ip_version == IPv6 && pkt_parser.internal_ip_version == IPv4)
|
||||
{
|
||||
struct sockaddr_in saddr4 = {0};
|
||||
saddr4.sin_family = PF_INET;
|
||||
saddr4.sin_addr.s_addr = inet_addr(pkt_parser.internal_ipv4.dst_addr);
|
||||
raw_ip_fd = socket(PF_INET, SOCK_RAW, IPPROTO_RAW);
|
||||
if (sendto(raw_ip_fd, packet_data + offest, packet_len - offest, 0, (struct sockaddr *)&saddr4, sizeof(saddr4)) == -1)
|
||||
{
|
||||
LOG_ERROR("Failed at send(), %d: %s", errno, strerror(errno));
|
||||
close(raw_ip_fd);
|
||||
goto end;
|
||||
}
|
||||
close(raw_ip_fd);
|
||||
}
|
||||
return nfq_set_verdict(qh, pkt_parser.raw_pkt.id, NF_DROP, 0, NULL);
|
||||
}
|
||||
|
||||
end:
|
||||
return nfq_set_verdict(qh, pkt_parser.raw_pkt.id, NF_ACCEPT, 0, NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* doc : http://www.netfilter.org/projects/libnetfilter_queue/doxygen/html/
|
||||
* Library setup : http://www.netfilter.org/projects/libnetfilter_queue/doxygen/html/group__LibrarySetup.html
|
||||
* Queue handling : http://www.netfilter.org/projects/libnetfilter_queue/doxygen/html/group__Queue.html
|
||||
* Message parsing : http://www.netfilter.org/projects/libnetfilter_queue/doxygen/html/group__Parsing.html
|
||||
*/
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int fd;
|
||||
int rv;
|
||||
uint32_t queue = 1;
|
||||
struct nfq_handle *handle;
|
||||
struct nfq_q_handle *q_handle;
|
||||
char buf[4096] __attribute__((aligned));
|
||||
|
||||
if (argc == 2)
|
||||
{
|
||||
queue = atoi(argv[1]);
|
||||
if (queue > 65535)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s [<0-65535>]\n", argv[0]);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
LOG_DEBUG("Using queue: %d", queue);
|
||||
|
||||
handle = nfq_open();
|
||||
if (handle == NULL)
|
||||
{
|
||||
LOG_ERROR("Failed at nfq_open(), %d: %s", errno, strerror(errno));
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (nfq_unbind_pf(handle, AF_INET) < 0)
|
||||
{
|
||||
LOG_ERROR("Failed at nfq_unbind_pf(), %d: %s", errno, strerror(errno));
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (nfq_bind_pf(handle, AF_INET) < 0)
|
||||
{
|
||||
LOG_ERROR("Failed at nfq_bind_pf(), %d: %s", errno, strerror(errno));
|
||||
goto error;
|
||||
}
|
||||
|
||||
q_handle = nfq_create_queue(handle, queue, &packet_handler_cb, NULL);
|
||||
if (q_handle == NULL)
|
||||
{
|
||||
LOG_ERROR("Failed at nfq_create_queue(), %d: %s", errno, strerror(errno));
|
||||
goto error;
|
||||
}
|
||||
|
||||
/*
|
||||
* NFQNL_COPY_NONE - noop, do not use it
|
||||
* NFQNL_COPY_META - copy only packet metadata
|
||||
* NFQNL_COPY_PACKET - copy entire packet
|
||||
*/
|
||||
if (nfq_set_mode(q_handle, NFQNL_COPY_PACKET, 0xffff) < 0)
|
||||
{
|
||||
LOG_ERROR("Failed at nfq_set_mode(NFQNL_COPY_PACKET), %d: %s", errno, strerror(errno));
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (nfq_set_queue_maxlen(q_handle, 65535) < 0)
|
||||
{
|
||||
LOG_ERROR("Failed at nfq_set_queue_maxlen(65535), %d: %s", errno, strerror(errno));
|
||||
goto error;
|
||||
}
|
||||
|
||||
LOG_DEBUG("Waiting for packets...");
|
||||
|
||||
fd = nfq_fd(handle);
|
||||
for (;;)
|
||||
{
|
||||
if ((rv = recv(fd, buf, sizeof(buf), 0)) >= 0)
|
||||
{
|
||||
nfq_handle_packet(handle, buf, rv);
|
||||
continue;
|
||||
}
|
||||
/*
|
||||
* if your application is too slow to digest the packets that
|
||||
* are sent from kernel-space, the socket buffer that we use
|
||||
* to enqueue packets may fill up returning ENOBUFS. Depending
|
||||
* on your application, this error may be ignored. Please, see
|
||||
* the doxygen documentation of this library on how to improve
|
||||
* this situation.
|
||||
*/
|
||||
if (rv < 0 && errno == ENOBUFS)
|
||||
{
|
||||
LOG_ERROR("Losing packets !!!");
|
||||
continue;
|
||||
}
|
||||
LOG_ERROR("Failed at recv(), %d: %s", errno, strerror(errno));
|
||||
break;
|
||||
}
|
||||
|
||||
error:
|
||||
if (q_handle)
|
||||
{
|
||||
nfq_destroy_queue(q_handle);
|
||||
}
|
||||
|
||||
if (handle)
|
||||
{
|
||||
nfq_close(handle);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
10
platform/CMakeLists.txt
Normal file
10
platform/CMakeLists.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
find_package(NFNETLINK REQUIRED)
|
||||
|
||||
add_executable(packetadapter src/inject_pkt.c src/system.c src/packet_adapter.c)
|
||||
|
||||
target_include_directories(packetadapter PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include/)
|
||||
|
||||
target_link_libraries(packetadapter common)
|
||||
target_link_libraries(packetadapter netfilter_queue)
|
||||
|
||||
install(TARGETS packetadapter RUNTIME DESTINATION bin COMPONENT Program)
|
||||
18
platform/include/inject_pkt.h
Normal file
18
platform/include/inject_pkt.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef _INJECT_PKT_H
|
||||
#define _INJECT_PKT_H
|
||||
|
||||
#ifdef __cpluscplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#include "public.h"
|
||||
|
||||
int inject_ipv4_pkt(char *ip4_addr, uint8_t *data, uint32_t len);
|
||||
int inject_ipv6_pkt(char *ip6_addr, uint8_t *data, uint32_t len);
|
||||
|
||||
#ifdef __cpluscplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
17
platform/include/system.h
Normal file
17
platform/include/system.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef _SYSTEM_H
|
||||
#define _SYSTEM_H
|
||||
|
||||
#ifdef __cpluscplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#include "public.h"
|
||||
|
||||
int run_daemon(void);
|
||||
|
||||
#ifdef __cpluscplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
53
platform/src/inject_pkt.c
Normal file
53
platform/src/inject_pkt.c
Normal file
@@ -0,0 +1,53 @@
|
||||
#include "inject_pkt.h"
|
||||
|
||||
int inject_ipv4_pkt(char *ip4_addr, uint8_t *data, uint32_t len)
|
||||
{
|
||||
int fd = 0;
|
||||
struct sockaddr_in saddr4 = {0};
|
||||
|
||||
saddr4.sin_family = PF_INET;
|
||||
saddr4.sin_addr.s_addr = inet_addr(ip4_addr);
|
||||
|
||||
fd = socket(PF_INET, SOCK_RAW, IPPROTO_RAW);
|
||||
if (fd == -1)
|
||||
{
|
||||
LOG_ERROR("Failed at socket(PF_INET, SOCK_RAW), %d: %s", errno, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (sendto(fd, data, len, 0, (struct sockaddr *)&saddr4, sizeof(saddr4)) == -1)
|
||||
{
|
||||
LOG_ERROR("Failed at send(), %d: %s", errno, strerror(errno));
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int inject_ipv6_pkt(char *ip6_addr, uint8_t *data, uint32_t len)
|
||||
{
|
||||
int fd = 0;
|
||||
struct sockaddr_in6 saddr6 = {0};
|
||||
|
||||
saddr6.sin6_family = PF_INET6;
|
||||
inet_pton(AF_INET6, ip6_addr, &saddr6.sin6_addr);
|
||||
|
||||
fd = socket(PF_INET6, SOCK_RAW, IPPROTO_RAW);
|
||||
if (fd == -1)
|
||||
{
|
||||
LOG_ERROR("Failed at socket(PF_INET6, SOCK_RAW), %d: %s", errno, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (sendto(fd, data, len, 0, (struct sockaddr *)&saddr6, sizeof(saddr6)) == -1)
|
||||
{
|
||||
LOG_ERROR("Failed at send(), %d: %s", errno, strerror(errno));
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
449
platform/src/packet_adapter.c
Normal file
449
platform/src/packet_adapter.c
Normal file
@@ -0,0 +1,449 @@
|
||||
#include "decode_ipv4.h"
|
||||
#include "decode_ipv6.h"
|
||||
#include "decode_tcp.h"
|
||||
#include "decode_udp.h"
|
||||
#include "decode_gtp.h"
|
||||
#include "inject_pkt.h"
|
||||
#include "system.h"
|
||||
|
||||
#include <linux/netfilter.h> // for NF_ACCEPT
|
||||
#include <libnetfilter_queue/libnetfilter_queue.h>
|
||||
|
||||
#ifdef Packet_Adapter_GIT_VERSION
|
||||
static __attribute__((__used__)) const char *Packet_Adapter_Version = Packet_Adapter_GIT_VERSION;
|
||||
#else
|
||||
static __attribute__((__used__)) const char *Packet_Adapter_Version = "Unknown";
|
||||
#endif
|
||||
|
||||
typedef struct pkt_info_s
|
||||
{
|
||||
uint32_t id; // unique ID of packet in queue
|
||||
uint16_t protocol; // hw protocol
|
||||
uint8_t hook; // netfilter hook
|
||||
u_int32_t mark;
|
||||
u_int32_t indev;
|
||||
u_int32_t outdev;
|
||||
u_int32_t phys_indev;
|
||||
u_int32_t phys_outdev;
|
||||
|
||||
uint8_t *payload;
|
||||
uint32_t payload_len;
|
||||
|
||||
char src_addr[512];
|
||||
} pkt_info_t;
|
||||
|
||||
typedef struct union_info_s
|
||||
{
|
||||
ipv4_info_t ipv4;
|
||||
ipv6_info_t ipv6;
|
||||
tcp_info_t tcp;
|
||||
udp_info_t udp;
|
||||
} union_info_t;
|
||||
|
||||
typedef struct pkt_paser_s
|
||||
{
|
||||
pkt_info_t raw;
|
||||
union_info_t external;
|
||||
gtp_info_t gtp;
|
||||
union_info_t internal;
|
||||
} pkt_paser_t;
|
||||
|
||||
static void dump_info(pkt_paser_t *parser)
|
||||
{
|
||||
uint32_t pkt_id = parser->raw.id;
|
||||
LOG_DEBUG("raw: {id: %u, protocol: %u, hook: %u, mark: %u, indev: %u, outdev: %u, phys_indev: %u, phys_outdev: %u, src_addr: %s, data_len: %u}",
|
||||
parser->raw.id,
|
||||
parser->raw.protocol,
|
||||
parser->raw.hook,
|
||||
parser->raw.mark,
|
||||
parser->raw.indev,
|
||||
parser->raw.outdev,
|
||||
parser->raw.phys_indev,
|
||||
parser->raw.phys_outdev,
|
||||
parser->raw.src_addr,
|
||||
parser->raw.payload_len);
|
||||
|
||||
// external
|
||||
if (parser->external.ipv4.hdr)
|
||||
{
|
||||
dump_ipv4_info(pkt_id, &(parser->external.ipv4));
|
||||
}
|
||||
if (parser->external.ipv6.hdr)
|
||||
{
|
||||
dump_ipv6_info(pkt_id, &(parser->external.ipv6));
|
||||
}
|
||||
if (parser->external.udp.hdr)
|
||||
{
|
||||
dump_udp_info(pkt_id, &(parser->external.udp));
|
||||
}
|
||||
if (parser->external.tcp.hdr)
|
||||
{
|
||||
dump_tcp_info(pkt_id, &(parser->external.tcp));
|
||||
}
|
||||
|
||||
// gtp
|
||||
if (parser->gtp.hdr)
|
||||
{
|
||||
dump_gtp_info(pkt_id, &(parser->gtp));
|
||||
}
|
||||
|
||||
// internal
|
||||
if (parser->internal.ipv4.hdr)
|
||||
{
|
||||
dump_ipv4_info(pkt_id, &(parser->internal.ipv4));
|
||||
}
|
||||
if (parser->internal.ipv6.hdr)
|
||||
{
|
||||
dump_ipv6_info(pkt_id, &(parser->internal.ipv6));
|
||||
}
|
||||
if (parser->internal.udp.hdr)
|
||||
{
|
||||
dump_udp_info(pkt_id, &(parser->internal.udp));
|
||||
}
|
||||
if (parser->internal.tcp.hdr)
|
||||
{
|
||||
dump_tcp_info(pkt_id, &(parser->internal.tcp));
|
||||
}
|
||||
}
|
||||
|
||||
static int decode_ip_tcp_udp(union_info_t *parser, const uint8_t *data, uint32_t len)
|
||||
{
|
||||
int next_protocol = 0;
|
||||
uint8_t *payload = NULL;
|
||||
uint32_t payload_len = 0;
|
||||
|
||||
if (len < IPV4_HEADER_LEN)
|
||||
{
|
||||
LOG_ERROR("Parser IP header: packet length too small %d", len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (IP_GET_RAW_VER(data) == 4)
|
||||
{
|
||||
if (decode_ipv4(&(parser->ipv4), data, len) == -1)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
payload = parser->ipv4.payload;
|
||||
payload_len = parser->ipv4.payload_len;
|
||||
next_protocol = parser->ipv4.next_protocol;
|
||||
}
|
||||
else if (IP_GET_RAW_VER(data) == 6)
|
||||
{
|
||||
if (decode_ipv6(&(parser->ipv6), data, len) == -1)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
payload = parser->ipv6.payload;
|
||||
payload_len = parser->ipv6.payload_len;
|
||||
next_protocol = parser->ipv6.next_protocol;
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_ERROR("Unknown IP version %d", IP_GET_RAW_VER(data));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (next_protocol == IPPROTO_UDP)
|
||||
{
|
||||
if (decode_udp(&(parser->udp), payload, payload_len) == -1)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
else if (next_protocol == IPPROTO_TCP)
|
||||
{
|
||||
if (decode_tcp(&(parser->tcp), payload, payload_len) == -1)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_ERROR("Unknown Internal L4 next_protocol version %d", next_protocol);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// NFQ API
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static int decode_pkt(pkt_info_t *packet, struct nfgenmsg *nfmsg, struct nfq_data *nfa)
|
||||
{
|
||||
struct nfqnl_msg_packet_hdr *packet_hdr = NULL;
|
||||
struct nfqnl_msg_packet_hw *packet_hw = NULL;
|
||||
|
||||
packet_hdr = nfq_get_msg_packet_hdr(nfa);
|
||||
if (packet_hdr == NULL)
|
||||
{
|
||||
LOG_ERROR("Failed at nfq_get_msg_packet_hdr()");
|
||||
return 0;
|
||||
}
|
||||
packet->id = ntohl(packet_hdr->packet_id);
|
||||
|
||||
packet->payload_len = nfq_get_payload(nfa, &packet->payload);
|
||||
if (packet->payload_len <= 0)
|
||||
{
|
||||
LOG_ERROR("Failed at nfq_get_payload()");
|
||||
return packet->id;
|
||||
}
|
||||
packet->protocol = ntohs(packet_hdr->hw_protocol);
|
||||
packet->hook = packet_hdr->hook;
|
||||
|
||||
packet_hw = nfq_get_packet_hw(nfa);
|
||||
if (packet_hw)
|
||||
{
|
||||
int i = 0;
|
||||
int offset = 0;
|
||||
int len = sizeof(packet->src_addr);
|
||||
int hlen = ntohs(packet_hw->hw_addrlen);
|
||||
|
||||
for (i = 0; i < hlen - 1; i++)
|
||||
{
|
||||
offset += snprintf(packet->src_addr + offset, len - offset, "%02x:", packet_hw->hw_addr[i]);
|
||||
}
|
||||
snprintf(packet->src_addr + offset, len - offset, "%02x", packet_hw->hw_addr[hlen - 1]);
|
||||
}
|
||||
|
||||
packet->mark = nfq_get_nfmark(nfa);
|
||||
packet->indev = nfq_get_indev(nfa);
|
||||
packet->outdev = nfq_get_outdev(nfa);
|
||||
packet->phys_indev = nfq_get_physindev(nfa);
|
||||
packet->phys_outdev = nfq_get_physoutdev(nfa);
|
||||
|
||||
return packet->id;
|
||||
}
|
||||
/*
|
||||
* nfmsg : message objetc that contains the packet
|
||||
* nfa : Netlink packet data handle
|
||||
*/
|
||||
static int packet_handler_cb(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg, struct nfq_data *nfa, void *data)
|
||||
{
|
||||
int offest = 0;
|
||||
pkt_paser_t parser = {0};
|
||||
int packet_id = decode_pkt(&(parser.raw), nfmsg, nfa);
|
||||
|
||||
// external
|
||||
if (decode_ip_tcp_udp(&(parser.external), parser.raw.payload, parser.raw.payload_len) == -1)
|
||||
{
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (parser.external.udp.hdr == NULL)
|
||||
{
|
||||
LOG_ERROR("External L4 protocol not UDP");
|
||||
goto end;
|
||||
}
|
||||
|
||||
// decode GTP
|
||||
if (decode_gtp(&(parser.gtp), parser.external.udp.payload, parser.external.udp.payload_len) == -1)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
// internal
|
||||
if (decode_ip_tcp_udp(&(parser.internal), parser.gtp.payload, parser.gtp.payload_len) == -1)
|
||||
{
|
||||
goto end;
|
||||
}
|
||||
|
||||
/*
|
||||
* NF_DROP : discarded the packet
|
||||
* NF_ACCEPT : the packet passes, continue iterations
|
||||
* NF_QUEUE : inject the packet into a different queue (the target queue number is in the high 16 bits of the verdict)
|
||||
* NF_REPEAT : iterate the same cycle once more
|
||||
* NF_STOP : accept, but don't continue iterations
|
||||
*/
|
||||
// nfq_set_verdict()
|
||||
// nfq_set_verdict2()
|
||||
// nfq_set_verdict_batch()
|
||||
// nfq_set_verdict_batch2()
|
||||
// nfq_set_verdict_mark()
|
||||
|
||||
if (parser.external.ipv4.hdr)
|
||||
{
|
||||
offest += parser.external.ipv4.hdr_len;
|
||||
}
|
||||
if (parser.external.ipv6.hdr)
|
||||
{
|
||||
offest += parser.external.ipv6.hdr_len;
|
||||
}
|
||||
|
||||
offest += parser.external.udp.hdr_len;
|
||||
offest += parser.gtp.hdr_len;
|
||||
|
||||
dump_info(&parser);
|
||||
LOG_DEBUG("Offset : %d", offest);
|
||||
|
||||
uint8_t *inject_data = parser.raw.payload + offest;
|
||||
uint32_t inject_data_len = parser.raw.payload_len - offest;
|
||||
|
||||
if (offest > 0)
|
||||
{
|
||||
if ((parser.external.ipv4.hdr && parser.internal.ipv4.hdr) || (parser.external.ipv6.hdr && parser.internal.ipv6.hdr))
|
||||
{
|
||||
return nfq_set_verdict(qh, packet_id, NF_ACCEPT, inject_data_len, inject_data);
|
||||
}
|
||||
|
||||
if (parser.external.ipv4.hdr && parser.internal.ipv6.hdr)
|
||||
{
|
||||
if (inject_ipv6_pkt(parser.internal.ipv6.dst_addr, inject_data, inject_data_len) == -1)
|
||||
{
|
||||
goto end;
|
||||
}
|
||||
return nfq_set_verdict(qh, packet_id, NF_DROP, 0, NULL);
|
||||
}
|
||||
|
||||
if (parser.external.ipv6.hdr && parser.internal.ipv4.hdr)
|
||||
{
|
||||
if (inject_ipv4_pkt(parser.internal.ipv4.dst_addr, inject_data, inject_data_len) == -1)
|
||||
{
|
||||
goto end;
|
||||
}
|
||||
return nfq_set_verdict(qh, packet_id, NF_DROP, 0, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
end:
|
||||
return nfq_set_verdict(qh, packet_id, NF_ACCEPT, 0, NULL);
|
||||
}
|
||||
|
||||
static void usage(char *cmd)
|
||||
{
|
||||
fprintf(stderr, "USAGE: %s [OPTIONS]\n", cmd);
|
||||
fprintf(stderr, " -v -- show version\n");
|
||||
fprintf(stderr, " -i id -- set queue id\n");
|
||||
fprintf(stderr, " -d -- run daemon\n");
|
||||
fprintf(stderr, " -h -- show help\n");
|
||||
}
|
||||
|
||||
/*
|
||||
* doc : http://www.netfilter.org/projects/libnetfilter_queue/doxygen/html/
|
||||
* Library setup : http://www.netfilter.org/projects/libnetfilter_queue/doxygen/html/group__LibrarySetup.html
|
||||
* Queue handling : http://www.netfilter.org/projects/libnetfilter_queue/doxygen/html/group__Queue.html
|
||||
* Message parsing : http://www.netfilter.org/projects/libnetfilter_queue/doxygen/html/group__Parsing.html
|
||||
*/
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int fd;
|
||||
int rv;
|
||||
int opt;
|
||||
uint16_t queue = 1;
|
||||
struct nfq_handle *handle;
|
||||
struct nfq_q_handle *q_handle;
|
||||
char buf[65535] __attribute__((aligned));
|
||||
|
||||
while ((opt = getopt(argc, argv, "vi:dh")) != -1)
|
||||
{
|
||||
switch (opt)
|
||||
{
|
||||
case 'v':
|
||||
fprintf(stderr, "Packet Adapter Version: %s\n", Packet_Adapter_Version);
|
||||
return 0;
|
||||
case 'i':
|
||||
queue = atoi(optarg);
|
||||
if (queue < 0 || queue > 65535)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s queueid %d out of range [0, 65535]\n", argv[0], queue);
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
case 'd':
|
||||
run_daemon();
|
||||
break;
|
||||
case 'h': /* fall through */
|
||||
default:
|
||||
usage(argv[0]);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
LOG_DEBUG("Using queue: %d", queue);
|
||||
|
||||
handle = nfq_open();
|
||||
if (handle == NULL)
|
||||
{
|
||||
LOG_ERROR("Failed at nfq_open(), %d: %s", errno, strerror(errno));
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (nfq_unbind_pf(handle, AF_INET) < 0)
|
||||
{
|
||||
LOG_ERROR("Failed at nfq_unbind_pf(), %d: %s", errno, strerror(errno));
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (nfq_bind_pf(handle, AF_INET) < 0)
|
||||
{
|
||||
LOG_ERROR("Failed at nfq_bind_pf(), %d: %s", errno, strerror(errno));
|
||||
goto error;
|
||||
}
|
||||
|
||||
q_handle = nfq_create_queue(handle, queue, &packet_handler_cb, NULL);
|
||||
if (q_handle == NULL)
|
||||
{
|
||||
LOG_ERROR("Failed at nfq_create_queue(), %d: %s", errno, strerror(errno));
|
||||
goto error;
|
||||
}
|
||||
|
||||
/*
|
||||
* NFQNL_COPY_NONE - noop, do not use it
|
||||
* NFQNL_COPY_META - copy only packet metadata
|
||||
* NFQNL_COPY_PACKET - copy entire packet
|
||||
*/
|
||||
if (nfq_set_mode(q_handle, NFQNL_COPY_PACKET, 0xffff) < 0)
|
||||
{
|
||||
LOG_ERROR("Failed at nfq_set_mode(NFQNL_COPY_PACKET), %d: %s", errno, strerror(errno));
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (nfq_set_queue_maxlen(q_handle, 65535) < 0)
|
||||
{
|
||||
LOG_ERROR("Failed at nfq_set_queue_maxlen(65535), %d: %s", errno, strerror(errno));
|
||||
goto error;
|
||||
}
|
||||
|
||||
LOG_DEBUG("Waiting for packets...");
|
||||
|
||||
fd = nfq_fd(handle);
|
||||
for (;;)
|
||||
{
|
||||
if ((rv = recv(fd, buf, sizeof(buf), 0)) >= 0)
|
||||
{
|
||||
nfq_handle_packet(handle, buf, rv);
|
||||
continue;
|
||||
}
|
||||
/*
|
||||
* if your application is too slow to digest the packets that
|
||||
* are sent from kernel-space, the socket buffer that we use
|
||||
* to enqueue packets may fill up returning ENOBUFS. Depending
|
||||
* on your application, this error may be ignored. Please, see
|
||||
* the doxygen documentation of this library on how to improve
|
||||
* this situation.
|
||||
*/
|
||||
if (rv < 0 && errno == ENOBUFS)
|
||||
{
|
||||
LOG_ERROR("Losing packets !!!");
|
||||
continue;
|
||||
}
|
||||
|
||||
LOG_ERROR("Failed at recv(), %d: %s", errno, strerror(errno));
|
||||
}
|
||||
|
||||
error:
|
||||
if (q_handle)
|
||||
{
|
||||
nfq_destroy_queue(q_handle);
|
||||
}
|
||||
|
||||
if (handle)
|
||||
{
|
||||
nfq_close(handle);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
69
platform/src/system.c
Normal file
69
platform/src/system.c
Normal file
@@ -0,0 +1,69 @@
|
||||
#include "system.h"
|
||||
|
||||
int run_daemon(void)
|
||||
{
|
||||
int fd;
|
||||
|
||||
switch (fork())
|
||||
{
|
||||
// 失败
|
||||
case -1:
|
||||
LOG_ERROR("Failed at fork(), %d: %s", errno, strerror(errno));
|
||||
return -1;
|
||||
// 子进程
|
||||
case 0:
|
||||
break;
|
||||
// 父进程
|
||||
default:
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if (setsid() == -1)
|
||||
{
|
||||
LOG_ERROR("Failed at setsid(), %d: %s", errno, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
umask(0);
|
||||
|
||||
// 以读写模式打开 /dev/null
|
||||
fd = open("/dev/null", O_RDWR);
|
||||
if (fd == -1)
|
||||
{
|
||||
LOG_ERROR("Failed at open(/dev/null), %d: %s", errno, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 将标准输入关联到 /dev/null
|
||||
if (dup2(fd, STDIN_FILENO) == -1)
|
||||
{
|
||||
LOG_ERROR("Failed at dup2(STDIN_FILENO), %d: %s", errno, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 将标准输出关联到 /dev/null
|
||||
if (dup2(fd, STDOUT_FILENO) == -1)
|
||||
{
|
||||
LOG_ERROR("Failed at dup2(STDOUT_FILENO), %d: %s", errno, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 将标准错误关联到 /dev/null
|
||||
if (dup2(fd, STDERR_FILENO) == -1)
|
||||
{
|
||||
LOG_ERROR("Failed at dup2(STDERR_FILENO), %d: %s", errno, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 关闭 /dev/null 的文件句柄
|
||||
if (fd > STDERR_FILENO)
|
||||
{
|
||||
if (close(fd) == -1)
|
||||
{
|
||||
LOG_ERROR("Failed at close(), %d: %s", errno, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
1
script/CMakeLists.txt
Normal file
1
script/CMakeLists.txt
Normal file
@@ -0,0 +1 @@
|
||||
install(FILES service/packetadapter.service DESTINATION /usr/lib/systemd/system/ COMPONENT Program)
|
||||
10
script/service/packetadapter.service
Normal file
10
script/service/packetadapter.service
Normal file
@@ -0,0 +1,10 @@
|
||||
[Unit]
|
||||
Description=Packet Adapter
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
ExecStartPre=/usr/sbin/iptables -A OUTPUT -o eno4 -p udp --dport 2152 -j NFQUEUE --queue-num 1
|
||||
ExecStartPre=/usr/sbin/ip6tables -A OUTPUT -o eno4 -p udp --dport 2152 -j NFQUEUE --queue-num 1
|
||||
ExecStart=/opt/tsg/packetadapter/bin/packetadapter
|
||||
ExecStopPost=/usr/sbin/iptables -D OUTPUT -o eno4 -p udp --dport 2152 -j NFQUEUE --queue-num 1
|
||||
ExecStopPost=/usr/sbin/ip6tables -D OUTPUT -o eno4 -p udp --dport 2152 -j NFQUEUE --queue-num 1
|
||||
Reference in New Issue
Block a user