CentOS 7 iptables Full Cone
为什么折腾
《switch主机UDP联机方案》讲到主机加速需要 NAT A 级别才能有最好的网络质量,但是 iptables 本身 NAT 只有 B,要达到 A 需要打一个补丁 https://github.com/Chion82/netfilter-full-cone-nat
开始安装
昨天测试时候用了香港服务器,IP 是在美国申请广播过去,结果老任识别为美国 IP 把我扔到美服,延迟很难受,因此这次搞了在东京的服务器带本地 IP 做出口。为确保国际网络稳定,出口机器要安装一个 BBR,先安装 el-repo 内核然后开启 BBR。
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
yum -y install epel-release
yum -y install https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm
yum --enablerepo=elrepo-kernel install kernel-ml kernel-ml-headers kernel-ml-devel kernel-ml-tools kernel-ml-tools-libs
grub2-set-default 0
reboot
echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf
sysctl -p
搞定内核之后准备安装上面的内核模块,虽然是 iptables 的模块但是它还需要内核源码,因此上面安装了 kernel-ml-devel 用于编译。并且还要求高版本 gcc,CentOS 7 自带的并不行,这里切换到 gcc8。
yum install gcc gcc-c++ autoconf autogen libmnl libmnl-devel libtool-devel libtool -y
yum install centos-release-scl -y
yum install devtoolset-8-gcc* -y
scl enable devtoolset-8 bash
禁掉 Firewalld 改用 iptables
systemctl disable firewalld
systemctl stop firewalld
yum -y install iptables-services
systemctl enable iptables
iptables -F
service iptables save
systemctl start iptables
下载源码
git clone git://git.netfilter.org/libnftnl.git
git clone git://git.netfilter.org/iptables.git
git clone https://github.com/Chion82/netfilter-full-cone-nat.git
以下路径直接用 cd 演示,需替换为实际路径
编译 libnftnl
yum install libmnl-devel -y
cd libnftnl
git checkout a71599dc0ae1a808e970e96850ea83d77f452994
sh autogen.sh
./configure
make
make install
编译 netfilter-full-cone-nat 模块
cd netfilter-full-cone-nat
make
modprobe nf_nat
insmod xt_FULLCONENAT.ko
编译 iptables 1.8.4
cp netfilter-full-cone-nat/libipt_FULLCONENAT.c iptables/extensions/
cd iptables
git checkout 2b506c6681c7b01803f06b258a39e9da9012e5c5
ln -sfv /usr/sbin/xtables-multi /usr/bin/iptables-xml
./autogen.sh
PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
./configure
make
make install
替换 iptables
systemctl stop iptables
rm -rf /sbin/iptables
rm -rf /sbin/iptables-restore
rm -rf /sbin/iptables-save
cd /usr/local/sbin
cp /usr/local/sbin/iptables /sbin/
cp /usr/local/sbin/iptables-restore /sbin/
cp /usr/local/sbin/iptables-save /sbin/
iptables -V
最后一步是检查版本号,是 1.8.4 就表示替换成功。
最后要开机自动加载这个模块。
kernel=`uname -r`
cp /root/netfilter-full-cone-nat/xt_FULLCONENAT.ko /lib/modules/$kernel/
depmod
echo "modprobe xt_FULLCONENAT" > /etc/sysconfig/modules/xt_FULLCONENAT.modules
chmod 755 /etc/sysconfig/modules/xt_FULLCONENAT.modules
reboot
重启后用 lsmod | grep xt_FULLCONENAT
确认是否有加载模块,有就代表成功。
最后上一篇的 iptables 命令里的 MASQUERADE 替换为 FULLCONENAT 就全部搞定啦!
xt_FULLCONENAT: loading out-of-tree module taints kernel.
xt_FULLCONENAT: module verification failed: signature and/or required key missing – tainting kernel
楼主我这边运行一段时间之后会被内核踢掉,不知道上面原因,你看看你的会这样吗?
能直接提供内核吧,我这边一直编不了,检查了很多会不知道哪里出了问题
我用 kernel-ml-5.14.9 能够编译成功
wget https://dl.lamp.sh/kernel/el7/kernel-ml-5.14.9-1.el7.elrepo.x86_64.rpm
wget https://dl.lamp.sh/kernel/el7/kernel-ml-headers-5.14.9-1.el7.elrepo.x86_64.rpm
wget https://dl.lamp.sh/kernel/el7/kernel-ml-devel-5.14.9-1.el7.elrepo.x86_64.rpm
wget https://dl.lamp.sh/kernel/el7/kernel-ml-tools-5.14.9-1.el7.elrepo.x86_64.rpm
wget https://dl.lamp.sh/kernel/el7/kernel-ml-tools-libs-5.14.9-1.el7.elrepo.x86_64.rpm
然后再rpm安装