Просмотр исходного кода

fix(devices): flag locally administered MACs from the address itself (#1818) (#1820)

Nacho 1 месяц назад
Родитель
Сommit
2403dae170

+ 1 - 0
CHANGELOG.md

@@ -32,6 +32,7 @@ y este proyecto se adhiere a [Versionado Semántico](https://semver.org/lang/es/
 
 ### Fixed
 
+- 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.
 - The pre-flight checks speak the language of the UI (#1811). Their detail text was written by the server in Spanish, so an English interface showed "3393 MB libres" next to a translated label. Each check now publishes a stable code with its numbers and the frontend translates it, leaving the server text as a fallback for older clients.

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

@@ -3592,7 +3592,11 @@ func (l *Live) buildDevices(polled map[string]*routerPolled, wanPort string) []D
 				d.Type = GuessDeviceType(d.Name, d.Manufacturer, vendorClass, clientID, lldpCapsByMac[mac])
 			}
 		}
-		if randomMACs[mac] {
+		// #1818: el bit localmente administrado se calcula aquí. La lista del
+		// agente es una foto del escaneo de discovery, así que una MAC
+		// efímera (el NMRP de Netgear al reiniciar para actualizar firmware)
+		// vive en el FDB unos segundos y nunca entra en ella.
+		if randomMACs[mac] || isLocallyAdministeredMAC(mac) {
 			d.RandomMAC = true
 		}
 		// mDNS-based type refinement (#338): if the hostname/DHCP classification

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

@@ -0,0 +1,26 @@
+// macbits.go — clasificación de una MAC por sus bits de control.
+package adapters
+
+import (
+	"strconv"
+	"strings"
+)
+
+// isLocallyAdministeredMAC informa de si la MAC tiene el bit localmente
+// administrado (0x02 del primer octeto). Esas direcciones no las asigna un
+// fabricante: las genera el propio equipo (privacidad de iOS y Android, MACs
+// de VM) o un firmware para una función temporal, como el NMRP de Netgear
+// mientras reinicia para actualizar (#1818).
+//
+// Una MAC mal formada devuelve false: no se clasifica lo que no se entiende.
+func isLocallyAdministeredMAC(mac string) bool {
+	m := strings.TrimSpace(mac)
+	if len(m) < 2 {
+		return false
+	}
+	first, err := strconv.ParseUint(m[:2], 16, 8)
+	if err != nil {
+		return false
+	}
+	return first&0x02 != 0
+}

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

@@ -0,0 +1,56 @@
+package adapters
+
+import "testing"
+
+// #1818: la MAC del NMRP de Netgear (D2:...) es localmente administrada y el
+// servidor debe reconocerlo sin depender del escaneo de discovery.
+func TestIsLocallyAdministeredMAC(t *testing.T) {
+	tests := []struct {
+		mac  string
+		want bool
+	}{
+		{"D2:95:DE:65:C5:B0", true}, // NMRP de Netgear (el del issue)
+		{"9E:11:22:33:44:55", true}, // bit 0x02 puesto
+		{"02:00:00:00:00:01", true}, // mínimo del rango local
+		{"d2:95:de:65:c5:b0", true}, // minúsculas
+		{"  D2:95:DE:65:C5:B0  ", true},
+		{"04:F4:1C:F3:46:65", false}, // OUI de fabricante (MikroTik)
+		{"94:83:C4:00:00:02", false}, // GL.iNet
+		{"BC:24:11:0A:0B:0C", false}, // OUI registrado de Proxmox
+		{"00:00:00:00:00:00", false},
+		{"", false},
+		{"ZZ:95:DE:65:C5:B0", false}, // no parseable
+		{"D", false},
+	}
+	for _, tt := range tests {
+		if got := isLocallyAdministeredMAC(tt.mac); got != tt.want {
+			t.Errorf("isLocallyAdministeredMAC(%q) = %v, esperaba %v", tt.mac, got, tt.want)
+		}
+	}
+}
+
+// #1818: una MAC efímera vista solo en el FDB sale marcada como RandomMAC,
+// aunque el agente no la haya reportado en su escaneo de discovery.
+func TestBuildDevicesMarksLocallyAdministeredFromFDB(t *testing.T) {
+	l := NewLive(nil, nil, nil, nil)
+	polled := map[string]*routerPolled{
+		"gw": {
+			cfg:      RouterConfig{ID: "gw", IsGateway: true},
+			fdb:      map[string]string{"D2:95:DE:65:C5:B0": "lan2", "04:F4:1C:F3:46:65": "lan3"},
+			arp:      map[string]string{},
+			arpStale: map[string]bool{},
+		},
+	}
+	byMAC := map[string]Device{}
+	for _, d := range l.buildDevices(polled, "") {
+		byMAC[d.MAC] = d
+	}
+	if d, ok := byMAC["D2:95:DE:65:C5:B0"]; !ok {
+		t.Fatal("la MAC del NMRP no aparece como dispositivo")
+	} else if !d.RandomMAC {
+		t.Error("la MAC localmente administrada no quedó marcada como RandomMAC")
+	}
+	if d, ok := byMAC["04:F4:1C:F3:46:65"]; ok && d.RandomMAC {
+		t.Error("una MAC de fabricante quedó marcada como RandomMAC")
+	}
+}