iwdev.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // iwdev.go - `iw dev` parsing with the physical radio of every interface
  2. // (#1560). The neighbour scan is the only probe that takes a radio off-channel,
  3. // so it must run once per radio, not once per interface: a radio carrying
  4. // several BSSIDs (LAN plus a VLAN SSID) used to be scanned once per BSSID in
  5. // the same cycle, 2-4s each.
  6. package probe
  7. import (
  8. "strconv"
  9. "strings"
  10. )
  11. // IwDevIface is one interface from `iw dev` with the radio it belongs to.
  12. type IwDevIface struct {
  13. Iface string
  14. Phy int // -1 when the output had no phy# section
  15. Type string // "AP", "managed", "monitor", "P2P-device", "" if unknown
  16. }
  17. // ParseIwDevDetailed parses `iw dev` keeping the phy# section each interface
  18. // sits under and its mode. iw prints one phy#N header followed by every
  19. // interface of that radio, so the section in effect is the radio.
  20. func ParseIwDevDetailed(out string) []IwDevIface {
  21. phy := -1
  22. var res []IwDevIface
  23. for _, raw := range strings.Split(out, "\n") {
  24. fields := strings.Fields(strings.TrimSpace(raw))
  25. if len(fields) == 0 {
  26. continue
  27. }
  28. switch {
  29. case strings.HasPrefix(fields[0], "phy#"):
  30. if n, err := strconv.Atoi(strings.TrimPrefix(fields[0], "phy#")); err == nil {
  31. phy = n
  32. } else {
  33. phy = -1
  34. }
  35. case fields[0] == "Interface" && len(fields) >= 2:
  36. res = append(res, IwDevIface{Iface: fields[1], Phy: phy})
  37. case fields[0] == "type" && len(fields) >= 2 && len(res) > 0:
  38. // First `type` wins: some iw builds repeat the line once per
  39. // virtual interface inside the same block.
  40. if res[len(res)-1].Type == "" {
  41. res[len(res)-1].Type = fields[1]
  42. }
  43. }
  44. }
  45. return res
  46. }
  47. // IwDevScanTargets picks the interface to scan for neighbours on every
  48. // physical radio, preferring one in AP mode. What a scan returns depends on
  49. // the radio, not on the BSSID it is run from, so scanning the second BSSID of
  50. // the same radio only keeps that radio off-channel for another 2-4s (#1557).
  51. // Without phy information (old iw, trimmed output) it falls back to one target
  52. // per interface, which is what the agent did before #1560.
  53. func IwDevScanTargets(ifaces []IwDevIface) []IwDevIface {
  54. hasPhy := false
  55. for _, i := range ifaces {
  56. if i.Phy >= 0 {
  57. hasPhy = true
  58. break
  59. }
  60. }
  61. if !hasPhy {
  62. return ifaces
  63. }
  64. var order []int
  65. groups := make(map[int][]IwDevIface, len(ifaces))
  66. var loose []IwDevIface
  67. for _, i := range ifaces {
  68. if i.Phy < 0 {
  69. loose = append(loose, i)
  70. continue
  71. }
  72. if _, ok := groups[i.Phy]; !ok {
  73. order = append(order, i.Phy)
  74. }
  75. groups[i.Phy] = append(groups[i.Phy], i)
  76. }
  77. res := make([]IwDevIface, 0, len(order)+len(loose))
  78. for _, phy := range order {
  79. res = append(res, pickScanIface(groups[phy]))
  80. }
  81. return append(res, loose...)
  82. }
  83. // pickScanIface prefers an AP interface because it is the one carrying
  84. // clients, which keeps the interface name in wifi_scans meaningful. Monitor
  85. // and P2P-device interfaces can also leave the radio off-channel without the
  86. // AP, so they are the last resort.
  87. func pickScanIface(group []IwDevIface) IwDevIface {
  88. for _, i := range group {
  89. if strings.EqualFold(i.Type, "AP") {
  90. return i
  91. }
  92. }
  93. for _, i := range group {
  94. if !strings.EqualFold(i.Type, "monitor") && !strings.EqualFold(i.Type, "P2P-device") {
  95. return i
  96. }
  97. }
  98. return group[0]
  99. }