Die*_*ino 4 networking ipv6 linux-containers
我需要从网络命名空间访问外部 IPv6 地址。
在我的主机中,我设置了一个 SIT 隧道 (IPv6-in-IPv4),它对 IPv6 数据包进行隧道传输并通过默认接口 (eth0) 发送它。SIT 隧道依赖于 Hurricane Electric 隧道代理服务。我可以从主机 ping 外部 IPv6 地址。
$ ping6 ipv6.google.com
PING ipv6.google.com(lis01s13-in-x0e.1e100.net) 56 data bytes
64 bytes from lis01s13-in-x0e.1e100.net: icmp_seq=1 ttl=57 time=98.1 ms
64 bytes from lis01s13-in-x0e.1e100.net: icmp_seq=2 ttl=57 time=98.7 ms
Run Code Online (Sandbox Code Playgroud)
以下是有关隧道的一些详细信息:
$ ip -6 route sh
2001:470:1f14:10be::/64 dev he-ipv6 proto kernel metric 256
default dev he-ipv6 metric 1024
Run Code Online (Sandbox Code Playgroud)
有趣的部分来了。由于超出此问题范围的原因,我需要从网络命名空间内执行相同的操作(ping ipv6.google.com)。
以下是我创建和设置网络命名空间的方法:
ns1.sh
#!/bin/bash
set -x
if [[ $EUID -ne 0 ]]; then
echo "You must run this script as root."
exit 1
fi
# Create network namespace 'ns1'.
ip netns del ns1 &>/dev/null
ip netns add ns1
# Create veth pair.
ip li add name veth1 type veth peer name vpeer1
# Setup veth1 (host).
ip -6 addr add fc00::1/64 dev veth1
ip li set dev veth1 up
# Setup vpeer1 (network namespace).
ip li set dev vpeer1 netns ns1
ip netns exec ns1 ip li set dev lo up
ip netns exec ns1 ip -6 addr add fc00::2/64 dev vpeer1
ip netns exec ns1 ip li set vpeer1 up
# Make vpeer1 default gw.
ip netns exec ns1 ip -6 route add default dev vpeer1
# Get into ns1.
ip netns exec ns1 /bin/bash --rcfile <(echo "PS1=\"ns1> \"")
Run Code Online (Sandbox Code Playgroud)
然后我ns1.sh从“ns1”运行并 ping veth1 (fc00::1) vpeer1 (fc00::2)。
ns1> ping6 fc00::1
PING fc00::1(fc00::1) 56 data bytes
64 bytes from fc00::1: icmp_seq=1 ttl=64 time=0.075 ms
^C
ns1> ping6 fc00::2
PING fc00::2(fc00::2) 56 data bytes
64 bytes from fc00::2: icmp_seq=1 ttl=64 time=0.056 ms
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试 ping 外部 IPv6 地址:
ns1> ping6 2a00:1450:4004:801::200e
PING 2a00:1450:4004:801::200e(2a00:1450:4004:801::200e) 56 data bytes
^C
--- 2a00:1450:4004:801::200e ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1008ms
Run Code Online (Sandbox Code Playgroud)
所有数据包均丢失。
我用 tcpdump 打开veth1并检查发生了什么。我看到的是邻居请求数据包正在到达接口。这些数据包尝试解析 IPv6 目标地址的 MAC 地址:
$ sudo tcpdump -qns 0 -e -i veth1
IPv6, length 86: fc00::2 > ff02::1:ff00:200e: ICMP6, neighbor solicitation,
who has 2a00:1450:4004:801::200e, length 32
IPv6, length 86: fc00::2 > ff02::1:ff00:200e: ICMP6, neighbor solicitation,
who has 2a00:1450:4004:801::200e, length 32
Run Code Online (Sandbox Code Playgroud)
我真的不明白为什么会发生这种情况。我也在主机中启用了 IPv6 转发,但没有效果:
$ sudo sysctl -w net.ipv6.conf.default.forwarding=1
Run Code Online (Sandbox Code Playgroud)
感谢您阅读这个问题。欢迎提出建议:)
编辑:
主机中的路由表:
2001:470:1f14:10be::/64 dev he-ipv6 proto kernel metric 256
fc00::/64 dev veth1 proto kernel metric 256
default dev he-ipv6 metric 1024
Run Code Online (Sandbox Code Playgroud)
我在主机中添加了 NDP 代理,以解决 NDP 请求。仍然无法从 nsnet 访问该地址(对此进行研究):
sudo sysctl -w net.ipv6.conf.all.proxy_ndp=1
sudo ip -6 neigh add proxy 2a00:1450:4004:801::200e dev veth1
Run Code Online (Sandbox Code Playgroud)
您已为网络名称空间指定了唯一的本地地址 (fc00::2):此 IP 地址在全球互联网中不可路由,只能在您的本地网络中路由。
当您的 ISP 收到来自该地址的 ICMP 数据包时,它将丢弃它。即使此数据包成功到达 ipv6.google.com,它也不可能将答案发送回给您,因为该 IP 地址没有公布的路由。
您会因为以下行而收到 NDP 通知:
ip netns exec ns1 ip -6 route add default dev vpeer1
Run Code Online (Sandbox Code Playgroud)
它告诉内核(在netns中)所有IP地址都直接连接在接口上vpeer1。内核认为该 IP 地址存在于以太网链路上:这就是它尝试使用 NDP 解析其 MAC 地址的原因。
相反,您想说它们可以通过给定的路由器访问(在您的情况下,路由器是您的主机):
ip netns exec ns1 ip -6 route add default dev vpeer1 via $myipv6
Run Code Online (Sandbox Code Playgroud)
您可以:
将公共 IPv6 地址(公共 IPv6 前缀)关联到您的 netns,并在主机上为此地址设置 NDP 代理;
对您的 IPv6 前缀进行子网划分,并将子网路由到您的主机(如果可以的话);
使用 NAT(不好、丑陋,不要这样做)。
您应该能够使用如下方法实现第一个:
#!/bin/bash
set -x
myipv6=2001:470:1f14:10be::42
peeripv6=2001:470:1f14:10be::43
#Create the netns:
ip netns add ns1
# Create and configure the local veth:
ip link add name veth1 type veth peer name vpeer1
ip -6 address add $myipv6/128 dev veth1
ip -6 route add $peeripv6/128 dev veth1
ip li set dev veth1 up
# Setup vpeer1 in the netns:
ip link set dev vpeer1 netns ns1
ip netns exec ns1 ip link set dev lo up
ip netns exec ns1 ip -6 address add $peeripv6/128 dev vpeer1
ip netns exec ns1 ip -6 route add $myipv6/128 dev vpeer1
ip netns exec ns1 ip link set vpeer1 up
ip netns exec ns1 ip -6 route add default dev vpeer1 via $peeripv6
# IP forwarding
sysctl -w net.ipv6.conf.default.forwarding=1
# NDP proxy for the netns
ip -6 neigh add proxy $myipv6 dev veth1
Run Code Online (Sandbox Code Playgroud)