armLinux的iptables转发,实现路由器功能

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

 

posted @ 2021-10-22 16:44  被使用的昵称&  阅读(1017)  评论(0编辑  收藏  举报