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

feat(agent): Broadcom wl tier for wireless clients, radio bands and channel width on dhd (#1531)

Broadcom dhd (Asuswrt/Merlin RT-AX92U, GT-BE98 Pro) exposes no stations and
no channel over nl80211, so a radio with real clients looks empty and its
clients only exist as a wireless-side hint with band "—". Add a last-resort
tier that reads the same facts from the Broadcom `wl` binary: band, channel,
channel width (from `wl chanspec`, e.g. "48/80" -> 80 MHz, bare "11" -> 20),
assoclist and per-station RSSI, in a single spawn (no `command -v`, which the
Merlin ash lacks).

Wireless-side band tags now resolve on dhd radios, and the radio panel shows
band + channel + width instead of blanks. Verified on a live RT-AX92U (5 GHz
ch48 80 MHz, 2.4 GHz ch11 20 MHz) and GT-BE98 Pro (5 GHz 160 MHz).

Co-authored-by: Bann-DEV <bann.dev@gmail.com>
Co-authored-by: gnacho <gnacho@domatix.com>
Bann-DEV 1 месяц назад
Родитель
Сommit
4eaa5e83d1
4 измененных файлов с 247 добавлено и 1 удалено
  1. 1 1
      agent/VERSION
  2. 33 0
      agent/probe/local.go
  3. 124 0
      agent/probe/wl.go
  4. 89 0
      agent/probe/wl_test.go

+ 1 - 1
agent/VERSION

@@ -1 +1 @@
-3.0.28
+3.0.29

+ 33 - 0
agent/probe/local.go

@@ -567,6 +567,39 @@ func (p *Prober) probeWireless(ctx context.Context, full bool) *WirelessData {
 			}
 		}
 	}
+	// Broadcom dhd (RT-AX92U con Merlin): nl80211 no expone NADA util — ni
+	// estaciones ni canal — y la unidad parece vacía aunque tenga clientes
+	// asociados (visibles en el panel del firmware). `wl` sí los sabe. Solo
+	// corre cuando los tiers anteriores dejaron huérfanos los clientes o
+	// ninguna radio trae banda.
+	{
+		needWl := !any || len(wd.Radios) == 0
+		if !needWl {
+			banded := false
+			for _, r := range wd.Radios {
+				if r.Name != "" {
+					banded = true
+					break
+				}
+			}
+			needWl = !banded
+		}
+		if needWl {
+			if out := p.runBest(ctx, CmdWlWireless, 8*time.Second); out != "" {
+				wclients, wradios := ParseWlWireless(out)
+				if !any && len(wclients) > 0 {
+					wd.Clients = wclients
+					any = true
+				}
+				if len(wradios) > 0 {
+					p.radiosMu.Lock()
+					p.radiosCache, p.radiosAt = wradios, time.Now()
+					p.radiosMu.Unlock()
+					wd.Radios = wradios
+				}
+			}
+		}
+	}
 
 	// Radios: solo en el sondeo completo (canal/htmode/txpower casi nunca
 	// cambian); el path de eventos reutiliza la caché.

+ 124 - 0
agent/probe/wl.go

@@ -0,0 +1,124 @@
+// wl.go — último recurso para drivers Broadcom dhd (Asuswrt/Merlin con
+// wl): el nl80211 de esos drivers no expone NI la tabla de estaciones NI el
+// canal (`iw station dump` vacío, `iw info` sin Channel), así que una radio
+// con clientes reales parece vacía: el panel de radios no trae banda y los
+// clientes solo existen como hint wireless-side con banda "—". `wl` sí lo
+// sabe todo: band (a/b), channel, assoclist y rssi por estación.
+package probe
+
+import (
+	"strconv"
+	"strings"
+)
+
+// CmdWlWireless: un solo spawn. Por interfaz AP de `iw dev` emite
+// "==IF== <iface>", la dirección (BSSID), la letra de banda, la línea de
+// canal y una línea "STA <mac> <rssi>" por estación asociada. Sin `wl`
+// (no-Broadcom) o sin permisos la salida queda vacía: best-effort como el
+// resto de sondas. OJO: sin `command -v` (el ash de Merlin no lo tiene).
+const CmdWlWireless = `if [ -x /usr/sbin/wl ]; then for i in $(iw dev 2>/dev/null | awk '/^[ \t]*Interface /{n=$2} /^[ \t]*type AP/{print n}'); do echo "==IF== $i"; cat /sys/class/net/$i/address 2>/dev/null; /usr/sbin/wl -i "$i" band 2>/dev/null | head -1; /usr/sbin/wl -i "$i" channel 2>/dev/null | tail -1; echo "CHSPEC $(/usr/sbin/wl -i "$i" chanspec 2>/dev/null)"; for m in $(/usr/sbin/wl -i "$i" assoclist 2>/dev/null | awk '/^assoclist/{print $2}'); do echo "STA $m $(/usr/sbin/wl -i "$i" rssi "$m" 2>/dev/null)"; done; done; fi`
+
+// wlWidthMhz parses the channel width from a `wl chanspec` value: "48/80
+// (0xe32a)" -> 80, "11/40" -> 40, a bare "11" -> 20 (20 MHz is the implicit
+// default when no "/<width>" is present). Returns 0 only if the token is
+// unparseable, so the panel can show "—" instead of a fake number.
+func wlWidthMhz(chanspec string) int {
+	tok := strings.TrimSpace(chanspec)
+	if i := strings.IndexByte(tok, ' '); i >= 0 {
+		tok = tok[:i] // drop the "(0x....)" suffix
+	}
+	if s := strings.SplitN(tok, "/", 2); len(s) == 2 {
+		if w, err := strconv.Atoi(strings.TrimSpace(s[1])); err == nil && w > 0 {
+			return w
+		}
+		return 0
+	}
+	if tok == "" {
+		return 0
+	}
+	if _, err := strconv.Atoi(tok); err != nil {
+		return 0 // not a bare channel number: unknown, don't fake 20
+	}
+	return 20 // a bare channel number: 20 MHz
+}
+
+// wlBand: la letra de `wl band` a banda canónica. "b" = 2.4G (802.11b/g/n),
+// "a" = 5G; el resto (n/ax/be y variantes 6G) se decide por canal.
+func wlBand(letter string, channel int) string {
+	switch strings.TrimSpace(strings.ToLower(letter)) {
+	case "b":
+		return "2.4 GHz"
+	case "a":
+		return "5 GHz"
+	}
+	if channel > 0 {
+		if channel <= 14 {
+			return "2.4 GHz"
+		}
+		return "5 GHz"
+	}
+	return ""
+}
+
+// ParseWlWireless parsea la salida de CmdWlWireless. Contratos:
+//   - clientes: MAC upper → WirelessClient{SignalDbm, Band de su interfaz};
+//     rssi no numérico (estación recién ido) → cliente con señal 0 omitida
+//     vía SignalDbm=0 igualmente, la presencia es lo que importa.
+//   - radios: una entrada por interfaz con BSSID (dirección del puerto) y
+//     banda/canal conocidos; sin banda no se emite radio (mejor nada que
+//     una banda inventada).
+func ParseWlWireless(out string) (clients map[string]WirelessClient, radios []Radio) {
+	clients = map[string]WirelessClient{}
+	curBand := ""
+	curAddr := ""
+	curCh := 0
+	curWidth := 0
+	pending := 0
+	flush := func() {
+		if b := wlBand(curBand, curCh); b != "" && curAddr != "" {
+			radios = append(radios, Radio{Name: b, Channel: curCh, WidthMhz: curWidth, BSSID: strings.ToUpper(curAddr), Clients: pending})
+		}
+	}
+	for _, line := range strings.Split(out, "\n") {
+		t := strings.TrimSpace(line)
+		switch {
+		case t == "":
+		case strings.HasPrefix(t, "==IF== "):
+			flush()
+			curBand, curAddr, curCh, curWidth, pending = "", "", 0, 0, 0
+		case strings.HasPrefix(t, "CHSPEC"):
+			curWidth = wlWidthMhz(strings.TrimSpace(strings.TrimPrefix(t, "CHSPEC")))
+		case len(t) == 17 && !strings.ContainsAny(t, " "):
+			curAddr = t
+		case t == "a" || t == "b" || t == "n" || t == "ac" || t == "ax" || t == "be":
+			curBand = t
+		case strings.Contains(t, "channel"):
+			fs := strings.Fields(t)
+			if len(fs) > 0 {
+				if ch, err := strconv.Atoi(fs[len(fs)-1]); err == nil {
+					curCh = ch
+				}
+			}
+		case strings.HasPrefix(t, "STA "):
+			fs := strings.Fields(t)
+			if len(fs) < 3 {
+				continue
+			}
+			sig, err := strconv.Atoi(fs[2])
+			if err != nil || sig >= 0 {
+				continue // estación recién ida o rssi no numérico: sin señal fiable, no se inventa
+			}
+			band := wlBand(curBand, curCh)
+			if band == "" {
+				band = "—"
+			}
+			clients[strings.ToUpper(fs[1])] = WirelessClient{SignalDbm: sig, Band: band}
+			pending++
+		}
+	}
+	flush()
+	if len(clients) == 0 {
+		clients = nil
+	}
+	return clients, radios
+}

+ 89 - 0
agent/probe/wl_test.go

@@ -0,0 +1,89 @@
+// wl_test.go — ParseWlWireless con la salida real capturada de un RT-AX92U
+// con Merlin (dhd: nl80211 ciego, wl lo sabe todo).
+package probe
+
+import "testing"
+
+const wlFixture = `==IF== wl1.1
+04:d9:f5:27:76:04
+a
+target channel	48
+CHSPEC 48/80 (0xe32a)
+STA 48:B0:2D:03:B6:78 -73
+STA 00:41:0E:80:51:25 -52
+STA 38:B8:00:0A:24:37 -75
+==IF== wl0.1
+04:d9:f5:27:76:01
+b
+target channel	3
+CHSPEC 11 (0x100b)
+`
+
+func TestParseWlWireless(t *testing.T) {
+	clients, radios := ParseWlWireless(wlFixture)
+	if len(clients) != 3 {
+		t.Fatalf("clientes: %d, want 3", len(clients))
+	}
+	tv, ok := clients["38:B8:00:0A:24:37"]
+	if !ok || tv.Band != "5 GHz" || tv.SignalDbm != -75 {
+		t.Fatalf("TV: %+v", tv)
+	}
+	if len(radios) != 2 {
+		t.Fatalf("radios: %d, want 2", len(radios))
+	}
+	if radios[0].Name != "5 GHz" || radios[0].Channel != 48 || radios[0].WidthMhz != 80 || radios[0].BSSID != "04:D9:F5:27:76:04" || radios[0].Clients != 3 {
+		t.Fatalf("radio 5G: %+v", radios[0])
+	}
+	if radios[1].Name != "2.4 GHz" || radios[1].Channel != 3 || radios[1].WidthMhz != 20 || radios[1].BSSID != "04:D9:F5:27:76:01" {
+		t.Fatalf("radio 2.4G: %+v", radios[1])
+	}
+}
+
+func TestParseWlWirelessGarbage(t *testing.T) {
+	// rssi no numérico (estación yendo en este mismo instante): el cliente
+	// se omite en vez de inventar señal; la radio igualmente se emite.
+	out := `==IF== wl0.1
+04:d9:f5:27:76:01
+b
+target channel	3
+STA AA:BB:CC:DD:EE:FF 
+`
+	clients, radios := ParseWlWireless(out)
+	if len(clients) != 0 {
+		t.Fatalf("clientes: %+v, want ninguno", clients)
+	}
+	if len(radios) != 1 || radios[0].Name != "2.4 GHz" || radios[0].Clients != 0 {
+		t.Fatalf("radios: %+v", radios)
+	}
+}
+
+func TestWlWidthMhz(t *testing.T) {
+	cases := []struct {
+		in   string
+		want int
+	}{
+		{"48/80 (0xe32a)", 80},
+		{"11/40", 40},
+		{"36/160 (0x1a2b)", 160},
+		{"11 (0x100b)", 20},
+		{"", 0},
+		{"foo", 0},
+	}
+	for _, c := range cases {
+		if got := wlWidthMhz(c.in); got != c.want {
+			t.Fatalf("wlWidthMhz(%q)=%d, want %d", c.in, got, c.want)
+		}
+	}
+}
+
+func TestWlBandFromChannelFallback(t *testing.T) {
+	if b := wlBand("ax", 3); b != "2.4 GHz" {
+		t.Fatalf("canal 3: %q", b)
+	}
+	if b := wlBand("ax", 149); b != "5 GHz" {
+		t.Fatalf("canal 149: %q", b)
+	}
+	if b := wlBand("", 0); b != "" {
+		t.Fatalf("sin datos: %q, want vacío", b)
+	}
+}