From d718795f8173af6a66aeea3bf93e15ac2c95046a Mon Sep 17 00:00:00 2001 From: Rayan Salhab Date: Tue, 28 Jul 2026 19:57:27 +0300 Subject: [PATCH] net/netmon: report disappeared interface IPs (#20621) InterfaceIPDisappeared is documented as true when an address existed in the old network state and is absent from the new network state. The non-nil-state predicate was reversed, so it reported addresses that appeared instead. Fix the predicate and add table coverage for disappeared, appeared, unchanged, and unknown-state cases. Fixes #20614 Change-Id: Ieb7a8c5e13d23e868e3ffe0b99e20fb14ba2245e Signed-off-by: cyphercodes Co-authored-by: cyphercodes (cherry picked from commit 5927c1864f4babb3214d32bf93910582beba6b41) --- net/netmon/netmon.go | 2 +- net/netmon/netmon_test.go | 62 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/net/netmon/netmon.go b/net/netmon/netmon.go index 46b5de37c..087cf4162 100644 --- a/net/netmon/netmon.go +++ b/net/netmon/netmon.go @@ -254,7 +254,7 @@ func (cd *ChangeDelta) InterfaceIPDisappeared(ip netip.Addr) bool { if cd.new == nil && cd.old.HasIP(ip) { return true } - return cd.new.HasIP(ip) && !cd.old.HasIP(ip) + return cd.old.HasIP(ip) && !cd.new.HasIP(ip) } // AnyInterfaceUp reports whether any interfaces are up in the new state. diff --git a/net/netmon/netmon_test.go b/net/netmon/netmon_test.go index a3ba4e03e..8177b623d 100644 --- a/net/netmon/netmon_test.go +++ b/net/netmon/netmon_test.go @@ -157,6 +157,68 @@ func TestMonitorMode(t *testing.T) { } } +func TestInterfaceIPDisappeared(t *testing.T) { + ip := netip.MustParseAddr("192.0.2.1") + + stateWithIP := func(ip netip.Addr) *State { + return &State{InterfaceIPs: map[string][]netip.Prefix{ + "eth0": {netip.PrefixFrom(ip, ip.BitLen())}, + }} + } + stateWithoutIP := func() *State { + return &State{InterfaceIPs: map[string][]netip.Prefix{ + "eth0": {netip.MustParsePrefix("198.51.100.1/32")}, + }} + } + + tests := []struct { + name string + old *State + new *State + want bool + }{ + { + name: "initial_state", + new: stateWithIP(ip), + }, + { + name: "disappeared", + old: stateWithIP(ip), + new: stateWithoutIP(), + want: true, + }, + { + name: "unchanged_present", + old: stateWithIP(ip), + new: stateWithIP(ip), + }, + { + name: "appeared", + old: stateWithoutIP(), + new: stateWithIP(ip), + }, + { + name: "unchanged_absent", + old: stateWithoutIP(), + new: stateWithoutIP(), + }, + { + name: "new_unknown", + old: stateWithIP(ip), + want: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cd := &ChangeDelta{old: tt.old, new: tt.new} + if got := cd.InterfaceIPDisappeared(ip); got != tt.want { + t.Fatalf("InterfaceIPDisappeared(%v) = %v; want %v", ip, got, tt.want) + } + }) + } +} + // tests (*ChangeDelta).RebindRequired func TestRebindRequired(t *testing.T) { // s1 must not be nil by definition