ARM WIFI AP 模式 使用 iptables nat 转发 通过 LAN 网线上网

编译内核,支持 iptables 和 forward 和 nat。

 

 

编译内核,使用新内核启动arm 开发板。

编译内核支持 iptables
-> Networking support (NET [=y])
  -> Networking options
    -> Network packet filtering framework (Netfilter) (NETFILTER [=y])
      -> IP: Netfilter Configuration
      
本开发板是通过 LAN 有线连接到 路由器上。路由器网段为 192.168.1.1
      
首先要配置 LAN 的网关,DNS 等 ,因为之前已经配置好 dhcp
如果你不是使用 NFS 文件系统可以这样做。
dhclient eth0

使用 NFS 文件系统,因为已经连接上,在执行 dhcp 就会断开重新获取 IP ,文件系统是在 NFS 上,所以就会死机。
#手动设定IP
ifconfig eth0 192.168.1.10 netmask 255.255.255.0
#网关
route add default gw 192.168.1.1
#DNS
echo nameserver 192.168.1.1 >/etc/resolv.conf      

ping qq.com 测试

LAN 网络正常,配置好了。

手工给 WLAN0 分配一个 IP
ifconfig wlan0 192.168.100.1 netmask 255.255.255.0

启动 DHCP 服务
dhcpd -cf /etc/dhcpd.conf wlan0
要注意 DHCP 配置文件中有 wlan0 的网段
cat /etc/dhcpd.conf
subnet 192.168.100.0 netmask 255.255.255.0 {
  range 192.168.100.10 192.168.100.100;
  option domain-name-servers 192.168.1.1,8.8.8.8,8.8.4.4;
  option routers 192.168.100.1;
}

启用 wlan0 AP
hostapd -B /etc/myhostapd.conf

 

关键的 iptables

编译  iptables
下载地址 下载最新的 1.6.1
wget ftp://ftp.netfilter.org/pub/iptables/iptables-1.6.1.tar.bz2
tar xvf iptables-1.6.1.tar.bz2
cd iptables-1.6.1
./configure --prefix=$PWD/tmp --host=arm-linux

有错误
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 也没关系,就不装了
./configure --prefix=$PWD/tmp --host=arm-linux --disable-nftables
make
libebt_log.c: In function 'brlog_parse':
libebt_log.c:147: error: 'EBT_LOG_IP6' undeclared (first use in this function)
libebt_log.c:147: error: (Each undeclared identifier is reported only once
libebt_log.c:147: error: for each function it appears in.)
libebt_log.c: In function 'brlog_print':
libebt_log.c:174: error: 'EBT_LOG_IP6' undeclared (first use in this function)
make[2]: *** [libebt_log.oo] 错误 1
make[2]:正在离开目录 `/home/iptables-1.6.1/extensions'
make[1]: *** [all-recursive] 错误 1
make[1]:正在离开目录 `/home/iptables-1.6.1'
make: *** [all] 错误 2

打开发现没有宏定义,从 LINUX 源码中找到了一个
#define EBT_LOG_IP6 0x08
添加到 libebt_log.c 文件中

make install
cd tmp

复制库 和 可执行文件到 ARM 板
cp lib/* -rfd /home/nfs/fs2440/lib
cp bin/* -rfd /home/nfs/fs2440/usr/bin
cp sbin/* -rfd /home/nfs/fs2440/usr/bin

复制 ubunto 的 配置文件到 ARM 文件系统中
cp /etc/sysctl.conf /home/nfs/fs2440/etc

编辑 vi /home/nfs/fs/etc/sysctl.conf
net.ipv4.ip_forward=1 去掉前面的 # 启用 内核转发
经过测试,发现 无效
cat /proc/sys/net/ipv4/ip_forward
手动开启
echo "1" > /proc/sys/net/ipv4/ip_forward

ARM 上执行配置 iptables
iptables -F
iptables -t nat -F
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE

/ # iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
iptables v1.4.12.1: Couldn't load target `MASQUERADE':No such file or directory
报错,我在这里还测试了 1.4.12 的版本

查看源码 iptables-1.4.12.1/iptables/xtables.c +727
#ifndef NO_SHARED_LIBS
    if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
        ptr = load_extension(xtables_libdir, afinfo->libprefix,
              name, true);

        if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
            xt_params->exit_err(PARAMETER_PROBLEM,
                   "Couldn't load target `%s':%s\n",
                   name, strerror(errno));
    }
看样子,不使用动态库就不会执行这里
void xtables_init(void)
{
    xtables_libdir = getenv("XTABLES_LIBDIR");
    if (xtables_libdir != NULL)
        return;

解决方法,定义一个 XTABLES_LIBDIR 变量 或是编译为静态的。

执行 iptables 前先设定库        
export XTABLES_LIBDIR=/lib/xtables

重新启动ARM
把上面的初始化命令重新整理一下,开机后执行下面所有命令
ifconfig eth0 192.168.1.10 netmask 255.255.255.0
route add default gw 192.168.1.1
echo nameserver 192.168.1.1 >/etc/resolv.conf
ifconfig wlan0 192.168.100.1 netmask 255.255.255.0
dhcpd -cf /etc/dhcpd.conf wlan0
hostapd -B /etc/myhostapd.conf
echo "1" > /proc/sys/net/ipv4/ip_forward
export XTABLES_LIBDIR=/lib/xtables
iptables -F
iptables -t nat -F
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

最后,放上测试图。

 

posted @ 2017-05-10 10:40  宁次  阅读(3660)  评论(0编辑  收藏  举报