network_uci.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package probe
  2. import (
  3. "strconv"
  4. "strings"
  5. )
  6. // UciNetwork is one logical network as `uci show network` declares it (#1889):
  7. // the way the box is configured, not the way its jacks look. A segmented
  8. // OpenWrt router has one of these per network, with its own device, subnet and
  9. // member ports, and that is the view an operator recognises.
  10. type UciNetwork struct {
  11. Name string `json:"name"`
  12. Device string `json:"device,omitempty"`
  13. Proto string `json:"proto,omitempty"`
  14. Subnet string `json:"subnet,omitempty"`
  15. Vlan int `json:"vlan,omitempty"`
  16. Ports []string `json:"ports,omitempty"`
  17. }
  18. // ParseUciNetworks reads the interface sections of `uci show network` and
  19. // resolves each one against the device sections, so a row carries the members
  20. // of its bridge and the VLAN it lives on. Both models are read the same way:
  21. // a bridge with VLAN filtering declares br_switch.999 as the device, and a box
  22. // that segments with subinterfaces declares br-iot fed by eth0.999.
  23. func ParseUciNetworks(out string) []UciNetwork {
  24. sections, order := uciNetworkSections(out)
  25. var nets []UciNetwork
  26. for _, name := range order {
  27. s := sections[name]
  28. if s["@type"] != "interface" {
  29. continue
  30. }
  31. dev := s["device"]
  32. if dev == "" {
  33. dev = s["ifname"]
  34. }
  35. if dev == "" {
  36. continue
  37. }
  38. n := UciNetwork{
  39. Name: name,
  40. Device: dev,
  41. Proto: s["proto"],
  42. Subnet: vlanSubnet(s["ipaddr"], s["netmask"]),
  43. }
  44. // An address written as a prefix already says its subnet.
  45. if ip := s["ipaddr"]; n.Subnet == "" && strings.Contains(ip, "/") {
  46. n.Subnet = ip
  47. }
  48. n.Vlan = subifVlan(dev)
  49. // The device section may name the device itself (br-iot) or its parent
  50. // bridge when the network sits on a subinterface (br_switch.999).
  51. base := dev
  52. if i := strings.IndexByte(dev, '.'); i > 0 {
  53. base = dev[:i]
  54. }
  55. var devSec uciSection
  56. for _, dname := range order {
  57. d := sections[dname]
  58. if d["@type"] != "device" {
  59. continue
  60. }
  61. if d["name"] == dev {
  62. devSec = d
  63. break
  64. }
  65. if d["name"] == base && devSec == nil {
  66. devSec = d
  67. }
  68. }
  69. if devSec != nil {
  70. n.Ports = splitUciList(devSec["ports"])
  71. // The bridge itself has no VLAN in its name: the tagged
  72. // subinterface it is fed with says which one it carries.
  73. if n.Vlan == 0 {
  74. for _, port := range n.Ports {
  75. if vid := subifVlan(port); vid > 0 {
  76. n.Vlan = vid
  77. break
  78. }
  79. }
  80. }
  81. }
  82. // With bridge VLAN filtering the members of one network are the ports
  83. // its bridge-vlan section lists, not every port of the bridge.
  84. if n.Vlan > 0 && base != dev {
  85. for _, bname := range order {
  86. b := sections[bname]
  87. if b["@type"] != "bridge-vlan" || b["device"] != base || b["vlan"] != strconv.Itoa(n.Vlan) {
  88. continue
  89. }
  90. ports := splitUciList(b["ports"])
  91. for i, port := range ports {
  92. // eth1:t means tagged, eth1:u* untagged and primary.
  93. if j := strings.IndexByte(port, ':'); j > 0 {
  94. ports[i] = port[:j]
  95. }
  96. }
  97. n.Ports = ports
  98. }
  99. }
  100. nets = append(nets, n)
  101. }
  102. return nets
  103. }
  104. // subifVlan returns the VLAN id of an 802.1Q subinterface name, 0 otherwise.
  105. func subifVlan(name string) int {
  106. m := vlanSubifRe.FindStringSubmatch(name)
  107. if m == nil {
  108. return 0
  109. }
  110. vid, err := strconv.Atoi(m[2])
  111. if err != nil {
  112. return 0
  113. }
  114. return vid
  115. }