Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions docs/modules/net.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,10 @@ relay 立即让位,PD 消失后下一次事件恢复。WAN 变化、关闭开
流量;之后约一到两分钟(下一次 cron 探测并代理)可达。路由器能学到主机的全局地址,是因为主机访问 `br-lan` 上的 relay 地址
(例如它通告的 DNS)时用全局源地址发 NS,所以该前缀按 on-link 通告。

**已知限制**:relay 的 `/64` 在上游也是 on-link 网段,LAN 主机把整个 `/64` 当作本地链路,因此不能直接访问上游同一 `/64`
里的其它主机(上游路由器的全局地址、上游网段里的其它设备);互联网和其它网段不受影响。
**反向代理(LAN 访问上游同 `/64` 主机)**:relay 的 `/64` 在 LAN 上以 on-link 通告,LAN 主机对上游同 `/64` 内的地址会直接发 NS。
为此路由器同时在 `br-lan` 上开启 `proxy_ndp`,把 WAN 邻居表和 WAN 本机全局地址中落在 relay `/64` 内的地址(排除 `br-lan` 本机地址与已代理的
LAN 主机地址)反向代理到 `br-lan`(同样最多 64 个条目,已有条目优先保留,记录在 `/run/mini-router/ipv6-relay.json` 的 `lan_proxies`)。
这样 LAN 主机即可访问路由器已在上游学到的同 `/64` 主机(如上游路由器的全局地址);尚未出现在 WAN 邻居表中的静默上游主机仍需先产生一次经路由器的流量或邻居发现后才会被代理。

**探测与结论**:只有 crond 每分钟的 `mr net ipv6-relay --cron` 做主动探测(`ping6 -c 1 -W 3 -I <WAN> <地址>`,再读 WAN 邻居表
里该地址的状态)。有 echo 应答、或邻居状态为 REACHABLE / STALE / DELAY / PROBE / PERMANENT(上游主机回应了 NS,即使它忽略 ping)
Expand Down
85 changes: 79 additions & 6 deletions mr/mod_net_ipv6_relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ type ipv6RelayRoute struct {
}

type ipv6RelayState struct {
Prefixes []ipv6RelayPrefix `json:"prefixes"`
Proxies []ipv6RelayProxy `json:"proxies"`
Routes []ipv6RelayRoute `json:"routes,omitempty"`
Misses map[string]int `json:"misses,omitempty"`
LANAddrs []string `json:"lan_addrs"`
Prefixes []ipv6RelayPrefix `json:"prefixes"`
Proxies []ipv6RelayProxy `json:"proxies"`
LANProxies []ipv6RelayProxy `json:"lan_proxies,omitempty"`
Routes []ipv6RelayRoute `json:"routes,omitempty"`
Misses map[string]int `json:"misses,omitempty"`
LANAddrs []string `json:"lan_addrs"`
}

const (
Expand Down Expand Up @@ -513,12 +514,24 @@ func ipv6RelaySyncWait(c *Config, wait bool) error {
for _, ip := range ipv6RelayLocalAddrs(c.LAN.Bridge) {
local[ip.String()] = true
}
wanNeigh := map[string][]netip.Addr{}
wanLocal := map[string][]netip.Addr{}
wanSets := map[string]map[string]bool{}
oldProxy := map[ipv6RelayProxy]bool{}
for _, p := range old.Proxies {
oldProxy[p] = true
}
oldLANProxy := map[ipv6RelayProxy]bool{}
for _, p := range old.LANProxies {
oldLANProxy[p] = true
}
if len(cur.Prefixes) > 0 {
if err := ipv6RelaySetProxyNDP(c.LAN.Bridge, true); err != nil {
return fmt.Errorf("IPv6 relay proxy_ndp %s: %v", c.LAN.Bridge, err)
}
}
var eligible []ipv6RelayProxy
var lanEligible []ipv6RelayProxy
for _, p := range cur.Prefixes {
// The same /64 is connected on br-lan and the WAN. Prefer the WAN for destinations
// that are not proxied LAN hosts; the /128 LAN host routes below keep those on the bridge.
Expand All @@ -527,8 +540,13 @@ func ipv6RelaySyncWait(c *Config, wait bool) error {
return fmt.Errorf("IPv6 relay proxy_ndp %s: %v", p.Dev, err)
}
if wanSets[p.Dev] == nil {
wanNeigh[p.Dev] = ipv6RelayNeighbours(p.Dev)
wanLocal[p.Dev] = ipv6RelayLocalAddrs(p.Dev)
wanSets[p.Dev] = map[string]bool{}
for _, ip := range append(ipv6RelayNeighbours(p.Dev), ipv6RelayLocalAddrs(p.Dev)...) {
for _, ip := range wanNeigh[p.Dev] {
wanSets[p.Dev][ip.String()] = true
}
for _, ip := range wanLocal[p.Dev] {
wanSets[p.Dev][ip.String()] = true
}
}
Expand All @@ -540,6 +558,13 @@ func ipv6RelaySyncWait(c *Config, wait bool) error {
}
eligible = append(eligible, ipv6RelayProxy{Addr: addr, Dev: p.Dev})
}
for _, ip := range append(append([]netip.Addr(nil), wanNeigh[p.Dev]...), wanLocal[p.Dev]...) {
addr := ip.String()
if !prefix.Contains(ip) || addr == p.Addr || local[addr] {
continue
}
lanEligible = append(lanEligible, ipv6RelayProxy{Addr: addr, Dev: c.LAN.Bridge})
}
}
// the cap keeps entries that are already proxied first, so new addresses cannot push them out
sort.SliceStable(eligible, func(i, j int) bool { return oldProxy[eligible[i]] && !oldProxy[eligible[j]] })
Expand All @@ -548,6 +573,12 @@ func ipv6RelaySyncWait(c *Config, wait bool) error {
cur.Proxies = append(cur.Proxies, x)
}
}
sort.SliceStable(lanEligible, func(i, j int) bool { return oldLANProxy[lanEligible[i]] && !oldLANProxy[lanEligible[j]] })
for _, x := range lanEligible {
if len(cur.LANProxies) < ipv6RelayMaxProxies && !ipv6RelayHasProxy(cur.LANProxies, x) {
cur.LANProxies = append(cur.LANProxies, x)
}
}
// A proxied host that dropped out of the LAN neighbour table keeps its entry for a few scans
// (hysteresis against reply gaps). Every other reason to stop — no unexpired free verdict, an
// occupied verdict, the WAN or the router has the address, the prefix is gone — removes it now.
Expand Down Expand Up @@ -579,6 +610,40 @@ func ipv6RelaySyncWait(c *Config, wait bool) error {
sort.Slice(cur.Proxies, func(i, j int) bool {
return cur.Proxies[i].Dev+cur.Proxies[i].Addr < cur.Proxies[j].Dev+cur.Proxies[j].Addr
})
proxiedLANHost := map[string]bool{}
for _, p := range cur.Proxies {
proxiedLANHost[p.Addr] = true
}
inLANPrefix := func(x ipv6RelayProxy) bool {
if x.Dev != c.LAN.Bridge || local[x.Addr] || proxiedLANHost[x.Addr] {
return false
}
ip, err := netip.ParseAddr(x.Addr)
if err != nil {
return false
}
for _, p := range cur.Prefixes {
if netip.MustParsePrefix(p.Prefix).Contains(ip) && p.Addr != x.Addr {
return true
}
}
return false
}
for _, p := range old.LANProxies {
if ipv6RelayHasProxy(cur.LANProxies, p) {
continue
}
key := p.Dev + "\x00" + p.Addr
if inLANPrefix(p) && len(cur.LANProxies) < ipv6RelayMaxProxies && old.Misses[key] < ipv6RelayProxyGrace {
cur.LANProxies = append(cur.LANProxies, p)
cur.Misses[key] = old.Misses[key] + 1
continue
}
ipv6RelayRun("ip", "-6", "neigh", "del", "proxy", p.Addr, "dev", p.Dev)
}
sort.Slice(cur.LANProxies, func(i, j int) bool {
return cur.LANProxies[i].Dev+cur.LANProxies[i].Addr < cur.LANProxies[j].Dev+cur.LANProxies[j].Addr
})
for _, p := range cur.Proxies {
cur.Routes = append(cur.Routes, ipv6RelayRoute{Addr: p.Addr, Dev: c.LAN.Bridge})
}
Expand Down Expand Up @@ -624,11 +689,19 @@ func ipv6RelaySyncWait(c *Config, wait bool) error {
ipv6RelaySetProxyNDP(p.Dev, false)
}
}
if len(cur.Prefixes) == 0 && (len(old.Prefixes) > 0 || len(old.LANProxies) > 0) {
ipv6RelaySetProxyNDP(c.LAN.Bridge, false)
}
for _, p := range cur.Proxies {
if !oldProxy[p] {
ipv6RelayRun("ip", "-6", "neigh", "replace", "proxy", p.Addr, "dev", p.Dev)
}
}
for _, p := range cur.LANProxies {
if !oldLANProxy[p] {
ipv6RelayRun("ip", "-6", "neigh", "replace", "proxy", p.Addr, "dev", p.Dev)
}
}
if prefixesChanged {
// dnsmasq's constructor: range notices the new address, but a reload makes the
// first RA immediate and emits a zero-lifetime RA when a prefix is removed.
Expand Down
67 changes: 67 additions & 0 deletions mr/mod_net_ipv6_relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,3 +563,70 @@ func TestIPv6RelayCapKeepsExistingProxies(t *testing.T) {
t.Fatalf("cap %d, existing proxy kept: %v (%d proxies)", ipv6RelayMaxProxies, ipv6RelayHasProxy(st.Proxies, ipv6RelayProxy{Addr: last, Dev: "wan"}), len(st.Proxies))
}
}

func TestIPv6RelayProxiesUpstreamOnLAN(t *testing.T) {
wanNeigh := `[{"dst":"2001:db8:1::1","state":["REACHABLE"]},{"dst":"2001:db8:99::1","state":["STALE"]},{"dst":"fe80::1","state":["REACHABLE"]}]`
var calls []string
relayTestEnv(t, func(name string, args ...string) (string, error) {
line := name + " " + strings.Join(args, " ")
calls = append(calls, line)
switch {
case strings.Contains(line, "addr show dev wan"):
return `[{"addr_info":[{"family":"inet6","local":"2001:db8:1::10","prefixlen":64}]}]`, nil
case strings.Contains(line, "neigh show dev wan"):
return wanNeigh, nil
}
return "", nil
})
ipv6RelaySetProxyNDP = func(dev string, enabled bool) error {
calls = append(calls, "proxy_ndp "+dev+" "+fmt.Sprint(enabled))
return nil
}
c := relayConfig()
if err := ipv6RelaySync(c); err != nil {
t.Fatal(err)
}
joined := strings.Join(calls, "\n")
for _, want := range []string{"proxy_ndp br-lan true", "neigh replace proxy 2001:db8:1::1 dev br-lan", "neigh replace proxy 2001:db8:1::10 dev br-lan"} {
if !strings.Contains(joined, want) {
t.Errorf("missing %q:\n%s", want, joined)
}
}
for _, bad := range []string{"2001:db8:99::1", "fe80::1", "route replace 2001:db8:1::1/128"} {
if strings.Contains(joined, bad) {
t.Errorf("unexpected %q:\n%s", bad, joined)
}
}

// Upstream neighbour disappears: grace period keeps it, then removes it.
wanNeigh = `[]`
calls = nil
if err := ipv6RelaySync(c); err != nil {
t.Fatal(err)
}
if strings.Contains(strings.Join(calls, "\n"), "neigh del proxy 2001:db8:1::1 dev br-lan") {
t.Fatalf("upstream proxy removed before grace period:\n%s", strings.Join(calls, "\n"))
}
for i := 0; i < ipv6RelayProxyGrace; i++ {
calls = nil
if err := ipv6RelaySync(c); err != nil {
t.Fatal(err)
}
}
if !strings.Contains(strings.Join(calls, "\n"), "neigh del proxy 2001:db8:1::1 dev br-lan") {
t.Fatalf("missing upstream proxy was not removed after grace:\n%s", strings.Join(calls, "\n"))
}

// Disabling relay removes remaining LAN proxies and disables proxy_ndp on br-lan.
c.WAN[0].IPv6Relay = false
calls = nil
if err := ipv6RelaySync(c); err != nil {
t.Fatal(err)
}
joined = strings.Join(calls, "\n")
for _, want := range []string{"neigh del proxy 2001:db8:1::10 dev br-lan", "proxy_ndp br-lan false"} {
if !strings.Contains(joined, want) {
t.Errorf("disable missing %q:\n%s", want, joined)
}
}
}
1 change: 1 addition & 0 deletions tools/lab/scenarios/08-dhcp-slaac-relay.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ check "cron probed the client's address: verdict free" verdict_is "$client_v6" f
check "the client is proxied on the WAN" retry 70 proxied "$client_v6"
info lab console "$ID" 'ip -6 route; ip -6 neigh show proxy; cat /run/mini-router/ipv6-relay.json; echo; cat /run/mini-router/ipv6-relay-probe.json'
check "LAN client pings the ISP-side IPv6 endpoint" lab lan "$ID" exec "ping6 -c 2 -W 3 2001:db8:$ID:99::2"
check "LAN client pings the ISP router inside the relayed /64" lab lan "$ID" exec "ping6 -c 2 -W 3 $P::1"
# from the ISP's other /64: hosts of the relayed /64 itself are on-link for LAN clients (docs: limitation)
check "the ISP side reaches the LAN client (echo through the proxy)" ip netns exec "$isp_ns" ping -6 -c 2 -W 3 -I "2001:db8:$ID:99::2" "$client_v6"

Expand Down
Loading