|
openwrt 无线中继(二级路由)模式通过nat6插件实现路由器下所有设备支持ipv6
理论上此方法可解决openwrt设置作为二级路由时无法获得ipv6前缀时路由器下设备无法访问ipv6网站的问题,包括有线中继 与无线中继 方式。
以下为实现步骤:
1、确保openwrt本身已支持ipv6并且已安装以下插件:kmod-ipt-nat6 ,odhcp6c , odhcpd-ipv6only (odhcpd亦可)。如果没有请手动安装以上插件即可。楼主使用的是以下R3G固件:https://www.right.com.cn/forum/thread-5214936-1-1.html 已集成上述插件。
2、在 网络-接口 中,将 IPv6 ULA 前缀 的第一个字母 f 改为 d 。如下图所示:
3、设置LAN口dhcp 。在ssh中输入以下命令:
- uci set dhcp.lan.ra_default='1'
- uci commit dhcp
复制代码
4、新建NAT6服务 。在 /etc/init.d/ 目录下,新建一个 nat6 文件,文件内容如下:
- #!/bin/sh /etc/rc.common
- # NAT6 init script for OpenWrt // Depends on package: kmod-ipt-nat6
- START=55
- # Options
- # -------
- # Use temporary addresses (IPv6 privacy extensions) for outgoing connections? Yes: 1 / No: 0
- PRIVACY=1
- # Maximum number of attempts before this script will stop in case no IPv6 route is available
- # This limits the execution time of the IPv6 route lookup to (MAX_TRIES+1)*(MAX_TRIES/2) seconds. The default (15) equals 120 seconds.
- MAX_TRIES=15
- # An initial delay (in seconds) helps to avoid looking for the IPv6 network too early. Ideally, the first probe is successful.
- # This would be the case if the time passed between the system log messages "Probing IPv6 route" and "Setting up NAT6" is 1 second.
- DELAY=10
- # Logical interface name of outbound IPv6 connection
- # There should be no need to modify this, unless you changed the default network interface names
- # Edit by Vincent: I never changed my default network interface names, but still I have to change the WAN6_NAME to "wan" instead of "wan6"
- WAN6_NAME="wan6"
- # ---------------------------------------------------
- # Options end here - no need to change anything below
- boot() {
- [ $DELAY -gt 0 ] && sleep $DELAY
- logger -t NAT6 "Probing IPv6 route"
- PROBE=0
- COUNT=1
- while [ $PROBE -eq 0 ]
- do
- if [ $COUNT -gt $MAX_TRIES ]
- then
- logger -t NAT6 "Fatal error: No IPv6 route found (reached retry limit)" && exit 1
- fi
- sleep $COUNT
- COUNT=$((COUNT+1))
- PROBE=$(route -A inet6 | grep -c '::/0')
- done
- logger -t NAT6 "Setting up NAT6"
- WAN6_INTERFACE=$(uci get "network.$WAN6_NAME.ifname")
- if [ -z "$WAN6_INTERFACE" ] || [ ! -e "/sys/class/net/$WAN6_INTERFACE/" ] ; then
- logger -t NAT6 "Fatal error: Lookup of $WAN6_NAME interface failed. Were the default interface names changed?" && exit 1
- fi
- WAN6_GATEWAY=$(route -A inet6 -e | grep "$WAN6_INTERFACE" | awk '/::\/0/{print $2; exit}')
- if [ -z "$WAN6_GATEWAY" ] ; then
- logger -t NAT6 "Fatal error: No IPv6 gateway for $WAN6_INTERFACE found" && exit 1
- fi
- LAN_ULA_PREFIX=$(uci get network.globals.ula_prefix)
- if [ $(echo "$LAN_ULA_PREFIX" | grep -c -E "^([0-9a-fA-F]{4}):([0-9a-fA-F]{0,4}):") -ne 1 ] ; then
- logger -t NAT6 "Fatal error: IPv6 ULA prefix $LAN_ULA_PREFIX seems invalid. Please verify that a prefix is set and valid." && exit 1
- fi
- ip6tables -t nat -I POSTROUTING -s "$LAN_ULA_PREFIX" -o "$WAN6_INTERFACE" -j MASQUERADE
- if [ $? -eq 0 ] ; then
- logger -t NAT6 "Added IPv6 masquerading rule to the firewall (Src: $LAN_ULA_PREFIX - Dst: $WAN6_INTERFACE)"
- else
- logger -t NAT6 "Fatal error: Failed to add IPv6 masquerading rule to the firewall (Src: $LAN_ULA_PREFIX - Dst: $WAN6_INTERFACE)" && exit 1
- fi
- route -A inet6 add 2000::/3 gw "$WAN6_GATEWAY" dev "$WAN6_INTERFACE"
- if [ $? -eq 0 ] ; then
- logger -t NAT6 "Added $WAN6_GATEWAY to routing table as gateway on $WAN6_INTERFACE for outgoing connections"
- else
- logger -t NAT6 "Error: Failed to add $WAN6_GATEWAY to routing table as gateway on $WAN6_INTERFACE for outgoing connections"
- fi
- if [ $PRIVACY -eq 1 ] ; then
- echo 2 > "/proc/sys/net/ipv6/conf/$WAN6_INTERFACE/accept_ra"
- if [ $? -eq 0 ] ; then
- logger -t NAT6 "Accepting router advertisements on $WAN6_INTERFACE even if forwarding is enabled (required for temporary addresses)"
- else
- logger -t NAT6 "Error: Failed to change router advertisements accept policy on $WAN6_INTERFACE (required for temporary addresses)"
- fi
- echo 2 > "/proc/sys/net/ipv6/conf/$WAN6_INTERFACE/use_tempaddr"
- if [ $? -eq 0 ] ; then
- logger -t NAT6 "Using temporary addresses for outgoing connections on interface $WAN6_INTERFACE"
- else
- logger -t NAT6 "Error: Failed to enable temporary addresses for outgoing connections on interface $WAN6_INTERFACE"
- fi
- fi
- exit 0
- }
复制代码
5、启动NAT6服务 。命令如下:
- chmod +x /etc/init.d/nat6
- /etc/init.d/nat6 enable
复制代码
6、修改 /etc/sysctl.conf 文件,添加以下内容(如果没有的话):
- net.ipv6.conf.default.forwarding=2
- net.ipv6.conf.all.forwarding=2
- net.ipv6.conf.default.accept_ra=2
- net.ipv6.conf.all.accept_ra=2
复制代码
7、修改防火墙自定义规则。首先使用 ifconfig 命令,确认 路由器获得ipv6地址的接口名称。如果使用无线中继,接口名称可能是 wlan1 ;如果是有线中继,接口名称可能是 eth0.2 。
在网络-防火墙- 自定义规则中,添加以下内容(注意使用正确的接口名称):
- ip6tables -t nat -A POSTROUTING -o wlan1 -j MASQUERADE
复制代码
8、重启路由器,测试路由器下设备能否正常使用ipv6。如果还有问题可尝试在ssh输入以下命令(一般情况下可省略此步骤):
- uci set firewall.@rule["$(uci show firewall | grep 'Allow-ICMPv6-Forward' | cut -d'[' -f2 | cut -d']' -f1)"].enabled='0'
- uci commit firewall
复制代码
本文参考了以下文章:
①https://www.jianshu.com/p/723f3086352a
②https://www.right.com.cn/forum/thread-2661027-1-1.html
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|