【转发】 通过openwrt的NAT6转发,使后端设备获得ipv6网络

校园网原生ipv6,路由器是K2,之前一直在研究如何才能用上ipv6。试了各种方法都不稳定,最后终于找到了解决办法。

新装的路由器固件,只需按照下述步骤进行就可以(需使用putty登陆路由器)。潘多拉,openwrt,lede原版按照此方法都可以。


1.Install the package kmod-ipt-nat6 #安装kmod-ipt-nat6包

opkg update && opkg install kmod-ipt-nat6

2.Change the first letter of the “IPv6 ULA Prefix” from f to d

uci set network.globals.ula_prefix="$(uci get network.globals.ula_prefix | sed 's/^./d/')"
uci commit network

3.Set the DHCP server to “Always announce default router”

uci set dhcp.lan.ra_default='1'
uci commit dhcp

4.Add an init script for NAT6 by creating a new file /etc/init.d/nat6 and paste the code from the section Init Script into it #生成nat6脚本,脚本内容见最后!!

touch /etc/init.d/nat6
vi /etc/init.d/nat6

5.Make the script executable and enable it #修改权限,并生效

chmod +x /etc/init.d/nat6
/etc/init.d/nat6 enable

6.Reboot your router - your client devices should now be able to establish IPv6 connections #重启,这时候电脑就该能获得ipv6连接了

reboot

7.In addition, you may now disable the default firewall rule “Allow-ICMPv6-Forward” since it’s not needed when masquerading is enabled #接下来关闭默认的ipv6规则,因为我们安装了新的

uci set firewall.@rule["$(uci show firewall | grep 'Allow-ICMPv6-Forward' | cut -d'[' -f2 | cut -d']' -f1)"].enabled='0'
uci commit firewall

8.修改/etc/sysctl.conf,把文件中相关内容改为以下内容,没有的话就添加

vi /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

9.加入转发规则,编辑/etc/firewall.user

vi /etc/firewall.user
ip6tables -t nat -I POSTROUTING -s $(uci get network.globals.ula_prefix) -j MASQUERADE

10.重启路由器,配置正确的话就可以用了。



注意!
除了opkg命令外没有返回值。

以下为第4步使用到的源码:

#!/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=5

# 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
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在OpenWrt中配置NAT转发,可以按照以下步骤进行操作: 1. 登录到OpenWrt设备的Web界面或通过SSH登录到设备的命令行界面。 2. 找到并编辑 `/etc/config/firewall` 文件,您可以使用文本编辑器如vi或nano进行编辑。 3. 在文件中找到 `config defaults` 部分,通常在文件的开头。 4. 在 `config defaults` 部分中,确保 `option input`、`option output` 和 `option forward` 的值都设置为 `ACCEPT`。这将允许转发和访问控制规则。 5. 在文件中找到或添加一个名为 `config redirect` 的新配置部分,用于定义NAT转发规则。 6. 在 `config redirect` 部分中,设置以下选项来定义转发规则: - `option name`: 规则的名称,可以随意指定。 - `option src`: 源地址,可以设置为 `wan` 表示从WAN接口接收的流量。 - `option dest`: 目标地址,可以设置为 `lan` 表示将流量转发到LAN接口。 - `option proto`: 协议,可以设置为具体的协议类型如TCP或UDP。 - `option src_dport`: 源端口,可以设置为具体的端口号或端口范围。 - `option dest_ip`: 目标IP地址,可以设置为要转发到的目标设备的IP地址。 - `option dest_port`: 目标端口,可以设置为要转发到的目标设备的端口号。 - `option target`: 目标,可以设置为 `DNAT` 表示进行目标地址转换。 7. 保存并关闭文件。 8. 重新启动防火墙服务,可以通过执行以下命令来实现:`/etc/init.d/firewall restart` 通过这些步骤,您可以配置OpenWrt设备上的NAT转发规则。确保根据您的需求正确设置源地址、目标地址、协议、端口和目标设备的IP地址和端口。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值