iwdev.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. "sort"
  9. "strconv"
  10. "strings"
  11. )
  12. // IwDevIface is one interface from `iw dev` with the radio it belongs to.
  13. type IwDevIface struct {
  14. Iface string
  15. Phy int // -1 when the output had no phy# section
  16. Type string // "AP", "managed", "monitor", "P2P-device", "" if unknown
  17. }
  18. // ParseIwDevDetailed parses `iw dev` keeping the phy# section each interface
  19. // sits under and its mode. iw prints one phy#N header followed by every
  20. // interface of that radio, so the section in effect is the radio.
  21. func ParseIwDevDetailed(out string) []IwDevIface {
  22. phy := -1
  23. var res []IwDevIface
  24. for _, raw := range strings.Split(out, "\n") {
  25. fields := strings.Fields(strings.TrimSpace(raw))
  26. if len(fields) == 0 {
  27. continue
  28. }
  29. switch {
  30. case strings.HasPrefix(fields[0], "phy#"):
  31. if n, err := strconv.Atoi(strings.TrimPrefix(fields[0], "phy#")); err == nil {
  32. phy = n
  33. } else {
  34. phy = -1
  35. }
  36. case fields[0] == "Interface" && len(fields) >= 2:
  37. res = append(res, IwDevIface{Iface: fields[1], Phy: phy})
  38. case fields[0] == "type" && len(fields) >= 2 && len(res) > 0:
  39. // First `type` wins: some iw builds repeat the line once per
  40. // virtual interface inside the same block.
  41. if res[len(res)-1].Type == "" {
  42. res[len(res)-1].Type = fields[1]
  43. }
  44. }
  45. }
  46. return res
  47. }
  48. // IwDevScanTargets picks the interface to scan for neighbours on every
  49. // physical radio, preferring one in AP mode. What a scan returns depends on
  50. // the radio, not on the BSSID it is run from, so scanning the second BSSID of
  51. // the same radio only keeps that radio off-channel for another 2-4s (#1557).
  52. // Without phy information (old iw, trimmed output) it falls back to one target
  53. // per interface, which is what the agent did before #1560.
  54. func IwDevScanTargets(ifaces []IwDevIface) []IwDevIface {
  55. hasPhy := false
  56. for _, i := range ifaces {
  57. if i.Phy >= 0 {
  58. hasPhy = true
  59. break
  60. }
  61. }
  62. if !hasPhy {
  63. return ifaces
  64. }
  65. var order []int
  66. groups := make(map[int][]IwDevIface, len(ifaces))
  67. var loose []IwDevIface
  68. for _, i := range ifaces {
  69. if i.Phy < 0 {
  70. loose = append(loose, i)
  71. continue
  72. }
  73. if _, ok := groups[i.Phy]; !ok {
  74. order = append(order, i.Phy)
  75. }
  76. groups[i.Phy] = append(groups[i.Phy], i)
  77. }
  78. res := make([]IwDevIface, 0, len(order)+len(loose))
  79. for _, phy := range order {
  80. res = append(res, pickScanIface(groups[phy]))
  81. }
  82. return append(res, loose...)
  83. }
  84. // pickScanIface prefers an AP interface because it is the one carrying
  85. // clients, which keeps the interface name in wifi_scans meaningful. Monitor
  86. // and P2P-device interfaces can also leave the radio off-channel without the
  87. // AP, so they are the last resort.
  88. func pickScanIface(group []IwDevIface) IwDevIface {
  89. for _, i := range group {
  90. if strings.EqualFold(i.Type, "AP") {
  91. return i
  92. }
  93. }
  94. for _, i := range group {
  95. if !strings.EqualFold(i.Type, "monitor") && !strings.EqualFold(i.Type, "P2P-device") {
  96. return i
  97. }
  98. }
  99. return group[0]
  100. }
  101. // IwDevScanAlternates returns the other interfaces of the same radio, in the
  102. // order worth trying when the chosen one refuses the scan (#1839). On
  103. // Asuswrt/dhd every phy exposes an AP VAP that answers
  104. // NL80211_CMD_TRIGGER_SCAN with "Operation not supported" while the managed
  105. // VAP of the same phy accepts it and returns BSSes.
  106. func IwDevScanAlternates(ifaces []IwDevIface, chosen IwDevIface) []IwDevIface {
  107. if chosen.Phy < 0 {
  108. return nil
  109. }
  110. var out []IwDevIface
  111. for _, i := range ifaces {
  112. if i.Phy != chosen.Phy || i.Iface == chosen.Iface {
  113. continue
  114. }
  115. out = append(out, i)
  116. }
  117. sort.SliceStable(out, func(a, b int) bool { return altRank(out[a]) < altRank(out[b]) })
  118. return out
  119. }
  120. // altRank orders scan fallbacks: a managed VAP first, then anything else, and
  121. // monitor and P2P-device last because they are not expected to answer either.
  122. func altRank(i IwDevIface) int {
  123. switch {
  124. case strings.EqualFold(i.Type, "managed"):
  125. return 0
  126. case strings.EqualFold(i.Type, "monitor"):
  127. return 3
  128. case strings.EqualFold(i.Type, "P2P-device"):
  129. return 4
  130. default:
  131. return 1
  132. }
  133. }