找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
广告投放联系QQ68610888
查看: 10131|回复: 38

[R3G] openwrt 无线中继(二级路由)模式通过nat6插件实现路由器下所有设备支持ipv6

  [复制链接]
发表于 2021-8-4 06:36 | 显示全部楼层 |阅读模式
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中输入以下命令:
  1. uci set dhcp.lan.ra_default='1'
  2. uci commit dhcp
复制代码


4、新建NAT6服务 。在 /etc/init.d/ 目录下,新建一个 nat6  文件,文件内容如下:
  1. #!/bin/sh /etc/rc.common
  2. # NAT6 init script for OpenWrt // Depends on package: kmod-ipt-nat6

  3. START=55

  4. # Options
  5. # -------

  6. # Use temporary addresses (IPv6 privacy extensions) for outgoing connections? Yes: 1 / No: 0
  7. PRIVACY=1

  8. # Maximum number of attempts before this script will stop in case no IPv6 route is available
  9. # This limits the execution time of the IPv6 route lookup to (MAX_TRIES+1)*(MAX_TRIES/2) seconds. The default (15) equals 120 seconds.
  10. MAX_TRIES=15

  11. # An initial delay (in seconds) helps to avoid looking for the IPv6 network too early. Ideally, the first probe is successful.
  12. # This would be the case if the time passed between the system log messages "Probing IPv6 route" and "Setting up NAT6" is 1 second.
  13. DELAY=10

  14. # Logical interface name of outbound IPv6 connection
  15. # There should be no need to modify this, unless you changed the default network interface names
  16. # 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"
  17. WAN6_NAME="wan6"

  18. # ---------------------------------------------------
  19. # Options end here - no need to change anything below

  20. boot() {
  21.         [ $DELAY -gt 0 ] && sleep $DELAY
  22.         logger -t NAT6 "Probing IPv6 route"
  23.         PROBE=0
  24.         COUNT=1
  25.         while [ $PROBE -eq 0 ]
  26.         do
  27.                 if [ $COUNT -gt $MAX_TRIES ]
  28.                 then
  29.                         logger -t NAT6 "Fatal error: No IPv6 route found (reached retry limit)" && exit 1
  30.                 fi
  31.                 sleep $COUNT
  32.                 COUNT=$((COUNT+1))
  33.                 PROBE=$(route -A inet6 | grep -c '::/0')
  34.         done

  35.         logger -t NAT6 "Setting up NAT6"

  36.         WAN6_INTERFACE=$(uci get "network.$WAN6_NAME.ifname")
  37.         if [ -z "$WAN6_INTERFACE" ] || [ ! -e "/sys/class/net/$WAN6_INTERFACE/" ] ; then
  38.                 logger -t NAT6 "Fatal error: Lookup of $WAN6_NAME interface failed. Were the default interface names changed?" && exit 1
  39.         fi
  40.         WAN6_GATEWAY=$(route -A inet6 -e | grep "$WAN6_INTERFACE" | awk '/::\/0/{print $2; exit}')
  41.         if [ -z "$WAN6_GATEWAY" ] ; then
  42.                 logger -t NAT6 "Fatal error: No IPv6 gateway for $WAN6_INTERFACE found" && exit 1
  43.         fi
  44.         LAN_ULA_PREFIX=$(uci get network.globals.ula_prefix)
  45.         if [ $(echo "$LAN_ULA_PREFIX" | grep -c -E "^([0-9a-fA-F]{4}):([0-9a-fA-F]{0,4}):") -ne 1 ] ; then
  46.                 logger -t NAT6 "Fatal error: IPv6 ULA prefix $LAN_ULA_PREFIX seems invalid. Please verify that a prefix is set and valid." && exit 1
  47.         fi

  48.         ip6tables -t nat -I POSTROUTING -s "$LAN_ULA_PREFIX" -o "$WAN6_INTERFACE" -j MASQUERADE
  49.         if [ $? -eq 0 ] ; then
  50.                 logger -t NAT6 "Added IPv6 masquerading rule to the firewall (Src: $LAN_ULA_PREFIX - Dst: $WAN6_INTERFACE)"
  51.         else
  52.                 logger -t NAT6 "Fatal error: Failed to add IPv6 masquerading rule to the firewall (Src: $LAN_ULA_PREFIX - Dst: $WAN6_INTERFACE)" && exit 1
  53.         fi

  54.         route -A inet6 add 2000::/3 gw "$WAN6_GATEWAY" dev "$WAN6_INTERFACE"
  55.         if [ $? -eq 0 ] ; then
  56.                 logger -t NAT6 "Added $WAN6_GATEWAY to routing table as gateway on $WAN6_INTERFACE for outgoing connections"
  57.         else
  58.                 logger -t NAT6 "Error: Failed to add $WAN6_GATEWAY to routing table as gateway on $WAN6_INTERFACE for outgoing connections"
  59.         fi

  60.         if [ $PRIVACY -eq 1 ] ; then
  61.                 echo 2 > "/proc/sys/net/ipv6/conf/$WAN6_INTERFACE/accept_ra"
  62.                 if [ $? -eq 0 ] ; then
  63.                         logger -t NAT6 "Accepting router advertisements on $WAN6_INTERFACE even if forwarding is enabled (required for temporary addresses)"
  64.                 else
  65.                         logger -t NAT6 "Error: Failed to change router advertisements accept policy on $WAN6_INTERFACE (required for temporary addresses)"
  66.                 fi
  67.                 echo 2 > "/proc/sys/net/ipv6/conf/$WAN6_INTERFACE/use_tempaddr"
  68.                 if [ $? -eq 0 ] ; then
  69.                         logger -t NAT6 "Using temporary addresses for outgoing connections on interface $WAN6_INTERFACE"
  70.                 else
  71.                         logger -t NAT6 "Error: Failed to enable temporary addresses for outgoing connections on interface $WAN6_INTERFACE"
  72.                 fi
  73.         fi

  74.         exit 0
  75. }
复制代码


5、启动NAT6服务 。命令如下:
  1. chmod +x /etc/init.d/nat6
  2. /etc/init.d/nat6 enable
复制代码


6、修改 /etc/sysctl.conf 文件,添加以下内容(如果没有的话):
  1. net.ipv6.conf.default.forwarding=2
  2. net.ipv6.conf.all.forwarding=2
  3. net.ipv6.conf.default.accept_ra=2
  4. net.ipv6.conf.all.accept_ra=2
复制代码


7、修改防火墙自定义规则。首先使用 ifconfig 命令,确认 路由器获得ipv6地址的接口名称。如果使用无线中继,接口名称可能是 wlan1 ;如果是有线中继,接口名称可能是 eth0.2 。
在网络-防火墙- 自定义规则中,添加以下内容(注意使用正确的接口名称):
  1. ip6tables -t nat -A POSTROUTING -o wlan1 -j MASQUERADE
复制代码


8、重启路由器,测试路由器下设备能否正常使用ipv6。如果还有问题可尝试在ssh输入以下命令(一般情况下可省略此步骤):
  1. uci set firewall.@rule["$(uci show firewall | grep 'Allow-ICMPv6-Forward' | cut -d'[' -f2 | cut -d']' -f1)"].enabled='0'
  2. uci commit firewall
复制代码


本文参考了以下文章:
https://www.jianshu.com/p/723f3086352a
https://www.right.com.cn/forum/thread-2661027-1-1.html


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
我的恩山、我的无线 The best wifi forum is right here.
发表于 2021-8-5 14:32 | 显示全部楼层
谢谢楼主!
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2021-8-6 07:32 | 显示全部楼层
楼主666,感谢分享
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2021-8-9 17:49 | 显示全部楼层
我好像没改什么就可以
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2021-8-10 20:35 | 显示全部楼层
你好,大神,感谢分享!!
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2021-8-12 00:19 | 显示全部楼层
原来如此复杂,有点难度,最怕固件不兼容
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2021-8-21 10:12 | 显示全部楼层
谢谢分享!!!
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2021-8-21 23:30 | 显示全部楼层

谢谢楼主!
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2021-8-29 20:09 | 显示全部楼层
学学经验方法
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2021-8-30 18:41 | 显示全部楼层
谢谢分享,支持一下。
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2021-9-2 11:49 | 显示全部楼层
楼主666,感谢分享
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2021-9-2 20:10 | 显示全部楼层
感谢你的分享!
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2021-9-3 16:34 | 显示全部楼层
学习一下 学习一下 学习一下
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2021-9-5 21:21 | 显示全部楼层
感谢楼主分享
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2021-9-5 21:28 | 显示全部楼层
IPV6还需要插件吗,不能像硬路由那样直接DHCP分配?这教程好复杂 啊,小白看不太懂。

点评

这个是给不能自动分配IPV6的用的  详情 回复 发表于 2022-10-18 16:07
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

有疑问请添加管理员QQ86788181|手机版|小黑屋|Archiver|恩山无线论坛(常州市恩山计算机开发有限公司版权所有) ( 苏ICP备05084872号 )

GMT+8, 2024-3-29 01:33

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

| 江苏省互联网有害信息举报中心 举报信箱:js12377 | @jischina.com.cn 举报电话:025-88802724 本站不良内容举报信箱:68610888@qq.com 举报电话:0519-86695797

快速回复 返回顶部 返回列表