要让两台运行 OpenWrt 的路由器通过 Ad-Hoc 模式实现互联互通,可以按照以下步骤操作:
- 确保无线网卡支持 Ad-Hoc 模式
在 OpenWrt 上执行以下命令,检查无线网卡是否支持 Ad-Hoc 模式:
iw list
在输出的 Supported interface modes 中寻找 IBSS,如果存在,则支持 Ad-Hoc 模式。
- 配置无线网络为 Ad-Hoc 模式
分别在两台路由器上修改无线配置文件 /etc/config/wireless。
示例配置:
假设两台路由器需要通过 adhoc 网络互联,网络名称为 adhoc-network,频率为 2412 MHz (信道 1),加密方式为 WPA2。
编辑 /etc/config/wireless,添加或修改以下内容:
config wifi-device ‘radio0’
option type ‘mac80211′
option hwmode ’11g’ # 或 ’11a’,根据你的设备支持的频段
option channel ‘1’ # 信道需两台保持一致
option htmode ‘HT20’ # 或其他支持的模式,如 VHT80
option country ‘US’
config wifi-iface
option device ‘radio0’
option network ‘adhoc’ # 网络接口名称
option mode ‘adhoc’
option ssid ‘adhoc-network’
option encryption ‘psk2’ # 设置加密类型
option key ‘password123’ # 密钥
- 配置网络接口
编辑 /etc/config/network,为 adhoc 接口分配静态 IP 地址。
示例配置:
在每台路由器中,为 Ad-Hoc 网络设置不同的静态 IP 地址,并确保它们在同一网段中。
路由器 1:
config interface ‘adhoc’
option proto ‘static’
option ipaddr ‘192.168.100.1’
option netmask ‘255.255.255.0’
路由器 2:
config interface ‘adhoc’
option proto ‘static’
option ipaddr ‘192.168.100.2’
option netmask ‘255.255.255.0’
- 重启网络服务
在两台路由器上执行以下命令:
/etc/init.d/network restart
- 测试连接
在任意一台路由器上尝试 ping 另一台的静态 IP 地址:
ping 192.168.100.2 # 从路由器 1 测试到路由器 2
ping 192.168.100.1 # 从路由器 2 测试到路由器 1
如果能成功 ping 通,说明 Ad-Hoc 网络已经建立,路由器互联互通。
- (可选)配置路由
如果需要让其他设备通过路由器访问 Ad-Hoc 网络,可以配置静态路由或启用 NAT 转发,具体步骤根据网络需求调整。
为了让两台路由器的 网口(LAN/WAN 口)通过 Ad-Hoc 模式实现互联互通,需要正确设置路由和防火墙规则,使两边的网口网络能够相互访问。以下是详细步骤:
假设网络拓扑
路由器 A:
LAN 网段:192.168.1.0/24(IP:192.168.1.1)
Ad-Hoc IP:192.168.100.1
路由器 B:
LAN 网段:192.168.2.0/24(IP:192.168.2.1)
Ad-Hoc IP:192.168.100.2
- 配置 Ad-Hoc 网络的静态路由
为了让两台路由器的 LAN 网段可以通过 Ad-Hoc 网络访问对方,必须在两边分别添加静态路由。
在路由器 A 上:
告诉路由器 A,如何通过 Ad-Hoc 网络访问路由器 B 的 LAN 网段:
uci add network route
uci set network.@route[-1].interface=’adhoc’
uci set network.@route[-1].target=’192.168.2.0′
uci set network.@route[-1].netmask=’255.255.255.0′
uci set network.@route[-1].gateway=’192.168.100.2′
uci commit network
/etc/init.d/network restart
在路由器 B 上:
告诉路由器 B,如何通过 Ad-Hoc 网络访问路由器 A 的 LAN 网段:
uci add network route
uci set network.@route[-1].interface=’adhoc’
uci set network.@route[-1].target=’192.168.1.0′
uci set network.@route[-1].netmask=’255.255.255.0′
uci set network.@route[-1].gateway=’192.168.100.1′
uci commit network
/etc/init.d/network restart
- 修改防火墙规则
确保 Ad-Hoc 网络和 LAN 网络之间允许相互访问。
在路由器 A 上:
编辑 /etc/config/firewall,添加以下规则:
config rule
option name ‘Allow-LAN-to-Adhoc’
option src ‘lan’
option dest ‘adhoc’
option target ‘ACCEPT’
config rule
option name ‘Allow-Adhoc-to-LAN’
option src ‘adhoc’
option dest ‘lan’
option target ‘ACCEPT’
在路由器 B 上:
类似地,编辑 /etc/config/firewall,添加:
config rule
option name ‘Allow-LAN-to-Adhoc’
option src ‘lan’
option dest ‘adhoc’
option target ‘ACCEPT’
config rule
option name ‘Allow-Adhoc-to-LAN’
option src ‘adhoc’
option dest ‘lan’
option target ‘ACCEPT’
应用防火墙配置:
在两台路由器上运行:
/etc/init.d/firewall restart
- 测试互联
测试 LAN 到 LAN 通信: 从路由器 A 的 LAN 网段中的设备尝试访问路由器 B 的 LAN 网段中的设备,例如:
ping 192.168.2.100 # 测试 B 的设备
测试路由器之间互通: 测试从路由器 A 到路由器 B 的 LAN IP 地址:
ping 192.168.2.1
- 其他注意事项
-
IP 地址规划:确保两台路由器的 LAN 网段不冲突。
-
广播和服务:如果需要跨网段访问广播服务(如共享打印机、DLNA),可能需要配置 IGMP Proxy 或 mDNS 中继。
-
性能调优:Ad-Hoc 网络性能较低,建议根据需求选择合适的信道和频宽。
通过以上配置,两台路由器的 LAN 网段可以通过 Ad-Hoc 网络实现互联互通。