Sun's Blog

openwrt 实现IPv6 netmap (1:1nat)

时间: 分类: 闲言碎语

首先,确保你能获得 IPv6-PD 地址块 查看wan_6接口
IPv6-PD: 2409:8a70:x:x::/60
如果你看到如上类似的,那么就可以.

netmap可以实现如下效果
私有IP fd01::1 公网IP 2409:8a70::1
那么给设备分配fd01::1 通过netmap 访问2409:8a70::1 就等于完全访问fd01::1.

这样我们可以实现一些特殊需求,比如策略路由等等 还可以每个设备拥有独立的IPv6地址。

第一步,安装对应的iptables NAT模块

opkg install kmod-ip6tables ip6tables-mod-nat iptables-mod-nat-extra

第二步 在全局网络选项
IPv6 ULA 前缀 输入自定义的私有地址 ,
因为通常情况下 宽带会获得 /60的IPv6-PD 地址块
所以我们给私有地址设置/64的掩码
例如fd01::/64

第三步,添加脚本
脚本需要下面两个软件包

opkg install ubus jq

在/etc/hotplug.d/iface目录 添加以下脚本,文件名任意 文件权限755

#!/bin/sh
[ "$ACTION" = ifup ] || exit 0
iface_dhcp=wan_6
iface_wan=pppoe-wan
[ -z "$iface_dhcp" -o "$INTERFACE" = "$iface_dhcp" ] || exit 0

ip6tables -t nat -F
ip6tables -t nat -A POSTROUTING -o $iface_wan -j NETMAP --to `ubus call network.interface.wan_6 status | jq -r '.["ipv6-prefix"][0].address'`/64 -s `uci get network.globals.ula_prefix`
ip6tables -t nat -A PREROUTING -i $iface_wan -j NETMAP -d `ubus call network.interface.wan_6 status | jq -r '.["ipv6-prefix"][0].address'`/64 --to `uci get network.globals.ula_prefix`
gw=$(ip -6 route show default | grep $iface_wan | sed 's/from [^ ]* //' | head -n1)
status=$(ip -6 route add $gw 2>&1)
logger -t IPv6 "Done: $status"

设置脚本后,重启wan接口。
执行ip6tables -t nat -nL --line 看到类似以下内容说明配置成功,并测试是否可以访问IPv6网络

Chain PREROUTING (policy ACCEPT)
num  target     prot opt source               destination         
1    NETMAP     all      ::/0                 2409:8a70::/64  to:fd01::/64

Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
num  target     prot opt source               destination         
1    NETMAP     all      fd01::/64  ::/0                 to:2409:8a70::/64
Powered by Typecho | Web ui: Layui