iw.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // iw.go — fallback nl80211 portable (#1481): `iw dev` + `iw station dump`
  2. // por interfaz para targets sin ubus hostapd (Asuswrt con driver compatible
  3. // nl80211, Linux genérico). Mismo shape que el camino hostapd/iwinfo:
  4. // clientes keyed por MAC mayúscula y radios agrupadas por phy.
  5. package probe
  6. import (
  7. "strconv"
  8. "strings"
  9. )
  10. // CmdIwStationDump: un SOLO spawn: el bucle de shell itera las interfaces de
  11. // `iw dev` y vuelca sus estaciones. La sección ==DEV== lleva la topología
  12. // (phy → interfaces, banda, BSSID) y cada ==IF== su station dump.
  13. const CmdIwStationDump = `echo "==DEV=="; iw dev 2>/dev/null; for i in $(iw dev 2>/dev/null | awk '/^[ \t]*Interface /{print $2}'); do echo "==IF==$i"; iw dev "$i" station dump 2>/dev/null; done`
  14. // bandFromMHz convierte una frecuencia MHz al nombre de banda del payload
  15. // ("2.4 GHz"|"5 GHz"|"6 GHz"); "" si no cae en ninguna conocida.
  16. func bandFromMHz(mhz int) string {
  17. switch {
  18. case mhz >= 2300 && mhz <= 2500:
  19. return "2.4 GHz"
  20. case mhz >= 4900 && mhz < 5950:
  21. return "5 GHz"
  22. case mhz >= 5950 && mhz <= 7125:
  23. return "6 GHz"
  24. }
  25. return ""
  26. }
  27. // iwIface: una interfaz de `iw dev` con lo que hace falta para bandas y
  28. // radios (solo interesan las tipo AP para las radios del equipo).
  29. type iwIface struct {
  30. phy string
  31. name string
  32. addr string
  33. kind string
  34. band string
  35. channel int
  36. width int
  37. // ssid: `iw dev` la imprime en las interfaces AP/STA activas. Vacia en
  38. // interfaces down o sin ESSID, y en kernels que no la publican.
  39. ssid string
  40. }
  41. // ParseIwWireless parsea la salida combinada de CmdIwStationDump: clientes
  42. // asociados (signal, contadores, banda de su interfaz) y radios por phy
  43. // (una por phy con interfaces AP: banda/canal/ancho/BSSID y conteo de
  44. // estaciones). Defensivo: líneas desconocidas se ignoran; sin ==DEV== los
  45. // clientes salen sin banda y sin radios.
  46. func ParseIwWireless(out string) (map[string]WirelessClient, []Radio) {
  47. clients := map[string]WirelessClient{}
  48. stationIface := map[string]string{}
  49. ifaces := map[string]iwIface{}
  50. phyOrder := []string{}
  51. phyIfaces := map[string][]iwIface{}
  52. section := "dev" // nombre de interfaz del ==IF== actual
  53. curPhy := ""
  54. var cur *iwIface
  55. flush := func() {
  56. if cur != nil && cur.name != "" {
  57. ifaces[cur.name] = *cur
  58. phyIfaces[cur.phy] = append(phyIfaces[cur.phy], *cur)
  59. }
  60. cur = nil
  61. }
  62. station := ""
  63. for _, line := range strings.Split(out, "\n") {
  64. trim := strings.TrimSpace(line)
  65. switch {
  66. case trim == "==DEV==":
  67. flush()
  68. section = "dev"
  69. station = ""
  70. continue
  71. case strings.HasPrefix(trim, "==IF=="):
  72. flush()
  73. section = strings.TrimPrefix(trim, "==IF==")
  74. station = ""
  75. continue
  76. }
  77. if section == "dev" {
  78. switch {
  79. case strings.HasPrefix(trim, "phy#"):
  80. curPhy = trim
  81. phyOrder = append(phyOrder, curPhy)
  82. case strings.HasPrefix(trim, "Interface "):
  83. flush()
  84. cur = &iwIface{phy: curPhy, name: strings.TrimPrefix(trim, "Interface ")}
  85. case cur != nil && strings.HasPrefix(trim, "addr "):
  86. cur.addr = strings.ToUpper(strings.TrimPrefix(trim, "addr "))
  87. case cur != nil && strings.HasPrefix(trim, "type "):
  88. cur.kind = strings.TrimPrefix(trim, "type ")
  89. case cur != nil && strings.HasPrefix(trim, "ssid "):
  90. cur.ssid = strings.TrimSpace(strings.TrimPrefix(trim, "ssid "))
  91. case cur != nil && strings.HasPrefix(trim, "channel "):
  92. f := strings.Fields(trim)
  93. if len(f) >= 2 {
  94. cur.channel, _ = strconv.Atoi(f[1])
  95. }
  96. if open := strings.Index(trim, "("); open >= 0 {
  97. rest := trim[open+1:]
  98. if close := strings.Index(rest, ")"); close > 0 {
  99. if mhz, err := strconv.Atoi(strings.Fields(rest[:close])[0]); err == nil {
  100. cur.band = bandFromMHz(mhz)
  101. }
  102. }
  103. }
  104. if w := strings.Index(trim, "width:"); w >= 0 {
  105. if wf := strings.Fields(trim[w:]); len(wf) >= 2 {
  106. cur.width, _ = strconv.Atoi(wf[1])
  107. }
  108. }
  109. }
  110. continue
  111. }
  112. // Sección de station dump.
  113. if strings.HasPrefix(trim, "Station ") {
  114. // Station 24:9e:7d:0a:c3:1a (on wl3.1)
  115. f := strings.Fields(trim)
  116. if len(f) >= 4 {
  117. station = strings.ToUpper(f[1])
  118. iface := strings.TrimSuffix(strings.TrimPrefix(f[3], "("), ")")
  119. stationIface[station] = iface
  120. if _, ok := clients[station]; !ok {
  121. c := WirelessClient{Band: ifaces[iface].band, Iface: iface}
  122. // #1757: sin SSID no se puede ubicar la estacion en una
  123. // VLAN; se deja vacio antes que inventarlo.
  124. c.SSID = ifaces[iface].ssid
  125. clients[station] = c
  126. }
  127. }
  128. continue
  129. }
  130. if station == "" {
  131. continue
  132. }
  133. c := clients[station]
  134. switch {
  135. case strings.HasPrefix(trim, "signal:"):
  136. // signal: -56 [-62] dBm (el primero es el agregado)
  137. if f := strings.Fields(trim); len(f) >= 2 {
  138. if dbm, err := strconv.Atoi(f[1]); err == nil {
  139. c.SignalDbm = dbm
  140. }
  141. }
  142. case strings.HasPrefix(trim, "rx bytes:"):
  143. if f := strings.Fields(trim); len(f) >= 3 {
  144. c.RxBytes, _ = strconv.ParseUint(f[2], 10, 64)
  145. }
  146. case strings.HasPrefix(trim, "tx bytes:"):
  147. if f := strings.Fields(trim); len(f) >= 3 {
  148. c.TxBytes, _ = strconv.ParseUint(f[2], 10, 64)
  149. }
  150. }
  151. clients[station] = c
  152. }
  153. flush()
  154. radios := []Radio{}
  155. for _, phy := range phyOrder {
  156. aps := []iwIface{}
  157. for _, ifc := range phyIfaces[phy] {
  158. if ifc.kind == "AP" {
  159. aps = append(aps, ifc)
  160. }
  161. }
  162. if len(aps) == 0 {
  163. continue
  164. }
  165. inPhy := map[string]bool{}
  166. for _, ifc := range aps {
  167. inPhy[ifc.name] = true
  168. }
  169. n := 0
  170. for mac, iface := range stationIface {
  171. if _, ok := clients[mac]; ok && inPhy[iface] {
  172. n++
  173. }
  174. }
  175. radios = append(radios, Radio{
  176. Name: aps[0].band, Channel: aps[0].channel,
  177. WidthMhz: aps[0].width, BSSID: aps[0].addr, Clients: n,
  178. })
  179. }
  180. return clients, radios
  181. }