【原】iptables 交叉编译

    防火墙在做数据包过滤决定时,有一套遵循和组成的规则,这些规则存储在专用的数据包过滤表中,而这些表集成在 Linux 内核中。在数据包过滤表中,规则被分组放在我们所谓的链(chain)中。而netfilter/iptables IP 数据包过滤系统是一款功能强大的工具,可用于添加、编辑和移除规则。
虽然 netfilter/iptables IP 信息包过滤系统被称为单个实体,但它实际上由两个组件netfilter 和 iptables 组成。
netfilter 组件也称为内核空间(kernelspace),是内核的一部分,由一些信息包过滤表组成,这些表包含内核用来控制信息包过滤处理的规则集。
iptables 组件是一种工具,也称为用户空间(userspace),它使插入、修改和除去信息包过滤表中的规则变得容易。


1、下载iptables
https://git.netfilter.org/iptables/
git clone git://git.netfilter.org/iptables


2、进入下载包,执行autogen.sh,得到configure文件。


3、配置交叉编译器
./configure --enable-static --disable-shared --prefix=$PWD/bin CC=/opt/Xilinx/pkg2018.2/petalinux/tools/linux-i386/aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc --host=arm-none-linux-gnueabi
说明:
1)解析参考./configure --help
2)/opt/Xilinx/pkg2018.2/petalinux/tools/linux-i386/aarch64-linux-gnu/bin/aarch64-linux-gnu-为编译器路径。
3)--enable-static --disable-shared 指定静态编译。
4)--prefix=$PWD/bin指定make install后的安装目录,这里设置为当前目录下的bin目录。
5)执行时会出现下述错误,缺少libmnl。
    checking for libmnl... no
         *** Error: No suitable libmnl found. ***
        Please install the 'libmnl' package
        Or consider --disable-nftables to skip
        iptables-compat over nftables support.
这里有说明,可以去掉 感觉这个和 NAT 也没关系,就不装了。
6)完整命令: ./configure --enable-static --disable-shared --prefix=$PWD/bin CC=/opt/Xilinx/pkg2018.2/petalinux/tools/linux-i386/aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc --host=arm-none-linux-gnueabi --disable-nftables


4、编译:make


5、安装:make install


6、 编译出来的文件在当前路径下的bin/sbin里:最终文件是xtables-legacy-multi,但是不能直接运行xtables-legacy-multi,必须建立软连接或者重命名。否则出现以下异常信息:
      /bin # xtables-legacy-multi
      ERROR: No valid subcommand given.
      Valid subcommands:
      * iptables
      * main4
      * iptables-save
      * save4
      * iptables-restore
      * restore4
      * iptables-xml
      * xml
      * ip6tables
      * main6
      * ip6tables-save
      * save6
      * ip6tables-restore
      * restore6
      /bin #


7、把xtables-legacy-multi上传到设备上,并重名为iptables。

8、查看nat表
iptables -t nat -L

提示:iptables v1.8.3 (legacy): can't initialize iptables table `nat': Table does not exist (do you need to insmod?)

Perhaps iptables or your kernel needs to be upgraded.

解决方法: 编译内核时加入以下模块

Linux Kernel Configuration
    -> Networking support
        -> Networking options
            -> Network packet filtering framework(netfilter)
                -> Core netfilter configuration
                    -> Netfilter connection tracking support
                    -> NetBIOS name service protocal support
                    -> Netfilter Xtables support (required for ip_tables)


Linux Kernel Configuration
    -> Networking support
        -> Networking options
            -> Network packet filtering framework(netfilter)
                -> IP: Netfilter Configuration
                    -> IPv4 connection tracking support (require for NAT) 
                    -> IPv4 NAT
                    -> IP tables support (required for filtering/masq/NAT)
                    -> iptables NAT support
                    -> raw table support (required for NOTRACK/TRACE)

参考:
1、https://blog.csdn.net/dean_gdp/article/details/25879081
2、https://www.cnblogs.com/ningci/p/6834640.html

3、https://blog.csdn.net/seven407/article/details/7667421

原文地址:https://www.cnblogs.com/eleclsc/p/11686287.html