Procházet zdrojové kódy

fix(devices): stop remembering locally administered MACs without evidence (#1818) (#1821)

Nacho před 1 měsícem
rodič
revize
a30593b966

+ 1 - 0
CHANGELOG.md

@@ -32,6 +32,7 @@ y este proyecto se adhiere a [Versionado Semántico](https://semver.org/lang/es/
 
 ### Fixed
 
+- A locally administered MAC with no address and no lease is no longer remembered (#1818). The NMRP address a Netgear unit announces while it reboots to flash firmware never asks for an IP, so nothing but the memory of the previous cycle kept it in the client list with a lease that did not exist. The entry is still persisted as soon as it has an address, a DHCP lease or a previous record, so virtual machines and phones with MAC randomisation are untouched.
 - Locally administered MACs are recognised by the server itself (#1818). The flag only came from the agent discovery scan, a snapshot taken at one moment, so a MAC that lives in the switch FDB for a few seconds (the NMRP address a Netgear unit announces while it reboots to flash firmware) was never marked and entered the client list as an unknown device. The bit is now read from the address, so those entries are identifiable even when discovery never saw them.
 - Rotating the agent token now reaches the configuration the agent actually reads (#1816). The packaged init prefers the UCI config and only falls back to the environment file when the UCI token is empty, while the rotation rewrote the environment file alone. A router that still carried a token from an older install kept pushing with it and got a 401 on every cycle, forever, while SSH polling made it look healthy. The hot rotation now writes server, slug and token through `uci` when the config file is present, before restarting the service.
 - The served installer is always LF (#1812). A checkout with `core.autocrlf=true`, which is what Git for Windows picks by default, put CRLF in the embedded `install-agent.sh`, and the busybox ash on the router rejected the script with a syntax error. The embedded copy is normalised before it is served, and `.gitattributes` now forces LF on the shell files that travel to a router. The copy check compares both sides normalised, so it still catches a real divergence on a Windows checkout.

+ 4 - 1
server-go/internal/adapters/live.go

@@ -3148,7 +3148,10 @@ func (l *Live) buildDevices(polled map[string]*routerPolled, wanPort string) []D
 					continue
 				}
 				seen[mac] = seenInfo{routerID: routerID, band: band}
-				if l.db != nil {
+				// #1818: una MAC local sin IP ni lease no se recuerda.
+				_, hasLocalLease := leasesByMac[mac]
+				_, localWasKnown := known[mac]
+				if l.db != nil && persistLocalMAC(mac, arpByMac[mac], hasLocalLease, localWasKnown) {
 					// Sin signal_dbm en el DO UPDATE: el hint no trae RSSI y
 					// pisar con NULL el último bueno de una asociación real
 					// rompería la serie de señal del dispositivo.

+ 14 - 0
server-go/internal/adapters/macbits.go

@@ -24,3 +24,17 @@ func isLocallyAdministeredMAC(mac string) bool {
 	}
 	return first&0x02 != 0
 }
+
+// persistLocalMAC decide si una MAC debe recordarse en device_attrib (#1818).
+// Una dirección localmente administrada sin IP ni lease es una identidad
+// transitoria: el NMRP que anuncia un Netgear mientras reinicia para flashear
+// vive segundos y no pide dirección. Recordarla es lo que la mantenía en la
+// lista de clientes después de desaparecer. Todo lo demás se persiste igual.
+func persistLocalMAC(mac, ip string, hasLease, wasKnown bool) bool {
+	if !isLocallyAdministeredMAC(mac) {
+		return true
+	}
+	// Una MAC que ya estaba en el inventario se sigue actualizando: el filtro
+	// solo evita estrenar una identidad transitoria.
+	return ip != "" || hasLease || wasKnown
+}

+ 24 - 0
server-go/internal/adapters/macbits_1818_test.go

@@ -54,3 +54,27 @@ func TestBuildDevicesMarksLocallyAdministeredFromFDB(t *testing.T) {
 		t.Error("una MAC de fabricante quedó marcada como RandomMAC")
 	}
 }
+
+// #1818: la MAC local sin IP ni lease no se recuerda; con cualquiera de los
+// dos, o si es de fabricante, sí.
+func TestPersistLocalMAC(t *testing.T) {
+	cases := []struct {
+		name     string
+		mac      string
+		ip       string
+		hasLease bool
+		wasKnown bool
+		want     bool
+	}{
+		{"NMRP sin IP ni lease", "D2:95:DE:65:C5:B0", "", false, false, false},
+		{"local con IP", "D2:95:DE:65:C5:B0", "192.168.1.61", false, false, true},
+		{"local con lease", "D2:95:DE:65:C5:B0", "", true, false, true},
+		{"local ya conocida", "D2:95:DE:65:C5:B0", "", false, true, true},
+		{"fabricante sin nada", "04:F4:1C:F3:46:65", "", false, false, true},
+	}
+	for _, c := range cases {
+		if got := persistLocalMAC(c.mac, c.ip, c.hasLease, c.wasKnown); got != c.want {
+			t.Errorf("%s: persistLocalMAC = %v, esperaba %v", c.name, got, c.want)
+		}
+	}
+}