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

最后,放上測試圖。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM