iptables的轉發實現ip地址實際訪問地址的轉發。
路由器功能,看做將一個網卡作為wan口,一個網卡作為lan口。wan口配置到外網,實現互聯網的接入。lan口重新分配本地ip段,實現本地連接。
再將lan口的ip通過iptables轉發到wan口,實現lan口接入互聯網。
1. iptables功能的內核支持:
第一步,選中Networking support (按Y)
第二步,回車進入Networking support ,再進入Networking options
第三步,選中下面的選項(按Y)
第四步,按回車進入到core Netfilter configuration (按Y)
第五步,core Netfilter configuration選擇如下選項:
第六步,進入到 IP:Netfilter Configuration
第七步,選擇IP:Netfilter Configuration 的如下選項:
注意:千萬不要將這些選項一股腦全部勾選,有些功能用不到,而且選多了會造成內核不穩定,突然就跑飛了的情況。親歷過那種找問題的過程。
到此內配置完畢,需要重新編譯內核移植到開發板。
2. 下載iptables命令源碼,移植一個iptables指令。
我下載的是1.4.7版本。
編譯之后,iptables指令是連接到iptables-multi的,直接拷貝iptables-multi指令,作為iptables使用。
3. 板端的實際配置和應用。
兩個wlan設備,wlan0和wlan1 。
我這里wlan1 作為wan口,配置其連接到外網,從外網端分配的IP地址為192.168.0.133
waln0作為lan口,hostapd起AP模式,udhcpd配置自動分配IP。
可以看到wan口,lan口的ip完全不在同一個網段。
配置lan口能上網,此時需要運行如下兩條指令:
#開啟ip_forward轉發功能
echo 1 > /proc/sys/net/ipv4/ip_forward
#lan口ip轉發到wan口
/apps/iptables -t nat -A POSTROUTING -s 172.16.11.0/24 -j SNAT --to 192.168.0.133
到此可以ping通wan口網關,192.168.0.1
但是還是不能上網。
需要配置DNS服務器:我配置的是如下兩個,可以配置到udhcpd.conf文件里面,自動給接入lan口的設備,分配DNS
114.114.114.114
8.8.8.8
到此接入lan口的設備可以通過arm板訪問外網。板也實現了一個簡單的路由功能。
錯誤log信息,解決方法:
# iptables -t nat -nL 提示
iptables v1.3.5: 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(new)
-> 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)
-> IP tables support (required for filtering/masq/NAT)
-> Full NAT
-> MASQUERADE target support
-> REDIRECT target support