vlan_network_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package probe
  2. import "testing"
  3. // The shape `uci show network` prints on a router that segments with 802.1Q
  4. // subinterfaces (issue #1757): one bridge per network, each fed a tagged
  5. // subinterface of the physical port, and one interface per bridge carrying
  6. // the name and the address of the network. No bridge-vlan sections at all,
  7. // which is why `bridge vlan show` reports every port on VLAN 1.
  8. const uciSubifNetworks = `network.Lan=interface
  9. network.Lan.device='br-lan'
  10. network.Lan.proto='static'
  11. network.Lan.ipaddr='192.168.1.1'
  12. network.Lan.netmask='255.255.255.0'
  13. network.IOT=interface
  14. network.IOT.device='br-iot'
  15. network.IOT.proto='static'
  16. network.IOT.ipaddr='192.168.111.1'
  17. network.IOT.netmask='255.255.255.0'
  18. network.Services=interface
  19. network.Services.device='br-services'
  20. network.Services.proto='static'
  21. network.Services.ipaddr='192.168.222.1'
  22. network.Services.netmask='255.255.255.0'
  23. network.@device[0]=device
  24. network.@device[0].name='br-lan'
  25. network.@device[0].type='bridge'
  26. network.@device[0].ports='eth1' 'eth0'
  27. network.@device[1]=device
  28. network.@device[1].name='br-iot'
  29. network.@device[1].type='bridge'
  30. network.@device[1].ports='eth0.999'
  31. network.@device[2]=device
  32. network.@device[2].name='br-services'
  33. network.@device[2].type='bridge'
  34. network.@device[2].ports='eth0.555'
  35. `
  36. func TestParseUciNetworkVlansReadsSubinterfaces(t *testing.T) {
  37. ports := ParseUciNetworkVlans(uciSubifNetworks)
  38. iot := vlansOf(ports, "br-iot")
  39. if len(iot) != 1 {
  40. t.Fatalf("br-iot = %+v, want one VLAN", iot)
  41. }
  42. if iot[0].ID != 999 || !iot[0].PVID || iot[0].Tagged {
  43. t.Fatalf("br-iot vlan = %+v, want 999 as the native VLAN of the bridge", iot[0])
  44. }
  45. if iot[0].Name != "IOT" || iot[0].Subnet != "192.168.111.1/24" {
  46. t.Fatalf("br-iot name/subnet = %q %q", iot[0].Name, iot[0].Subnet)
  47. }
  48. // The physical port carries it tagged. This is what tells a client behind
  49. // eth0 which VLAN it is on.
  50. eth0 := vlansOf(ports, "eth0")
  51. if len(eth0) != 2 {
  52. t.Fatalf("eth0 = %+v, want the tagged VLANs 999 and 555", eth0)
  53. }
  54. if eth0[0].ID != 999 || !eth0[0].Tagged {
  55. t.Fatalf("eth0 vlan = %+v, want 999 tagged", eth0[0])
  56. }
  57. // A bridge with no subinterface is a plain untagged network, and still
  58. // has a name and a subnet worth showing.
  59. lan := vlansOf(ports, "br-lan")
  60. if len(lan) != 1 || lan[0].ID != 1 || lan[0].Name != "Lan" || lan[0].Subnet != "192.168.1.1/24" {
  61. t.Fatalf("br-lan = %+v", lan)
  62. }
  63. // The forwarding database names the bridge port, which here is the
  64. // subinterface: it has to resolve to the same VLAN as the bridge or a
  65. // device behind eth0.999 shows no VLAN at all (#1757).
  66. sub := vlansOf(ports, "eth0.999")
  67. if len(sub) != 1 || sub[0].ID != 999 || sub[0].Name != "IOT" || !sub[0].PVID {
  68. t.Fatalf("eth0.999 = %+v, want VLAN 999 (the port the FDB reports)", sub)
  69. }
  70. }
  71. func TestParseUciNetworkVlansIgnoresNonBridges(t *testing.T) {
  72. noise := `network.wan=interface
  73. network.wan.device='eth1.836'
  74. network.wan.proto='dhcp'
  75. network.@device[0]=device
  76. network.@device[0].name='br-lan'
  77. network.@device[0].type='bridge'
  78. network.@device[0].ports='eth0' 'eth1'
  79. `
  80. ports := ParseUciNetworkVlans(noise)
  81. // The bridge is untagged, so it is LAN 1. The WAN is bound straight to a
  82. // subinterface, with no bridge in between, and it is still VLAN 836.
  83. if got := vlansOf(ports, "eth1.836"); len(got) != 1 || got[0].ID != 836 || !got[0].PVID {
  84. t.Fatalf("eth1.836 = %+v, want the WAN VLAN", got)
  85. }
  86. if got := vlansOf(ports, "eth1"); len(got) != 0 {
  87. t.Fatalf("eth1 = %+v, want nothing: it is a plain member, not a VLAN", got)
  88. }
  89. }
  90. func TestParseUciNetworkVlansNeedsNoMasquerade(t *testing.T) {
  91. // `uci show` order is stable but nothing guarantees the section header
  92. // comes before its options, and a missing netmask must not invent a /24.
  93. out := `network.iot.proto='static'
  94. network.iot.device='br-iot'
  95. network.iot=interface
  96. network.iot.ipaddr='10.0.0.1'
  97. network.dev.ports='eth0.42'
  98. network.dev.type='bridge'
  99. network.dev.name='br-iot'
  100. network.dev=device
  101. `
  102. ports := ParseUciNetworkVlans(out)
  103. iot := vlansOf(ports, "br-iot")
  104. if len(iot) != 1 || iot[0].ID != 42 || iot[0].Subnet != "10.0.0.1" {
  105. t.Fatalf("br-iot = %+v", iot)
  106. }
  107. }
  108. // A router without vlan_filtering answers "VLAN 1" for every port. That is a
  109. // valid answer to a question nobody asked, and it must not shadow the real
  110. // segmentation.
  111. const ruttoBridgeVlan = `port vlan-id
  112. eth0 1 PVID Egress Untagged
  113. eth0.999 1 PVID Egress Untagged
  114. br-iot 1 PVID Egress Untagged
  115. `
  116. func TestMergeVlanPortsDropsTheDefaultVlan(t *testing.T) {
  117. kernel := ParseBridgeVlan(ruttoBridgeVlan)
  118. declared := ParseUciNetworkVlans(uciSubifNetworks)
  119. merged := MergeVlanPorts(kernel, declared)
  120. iot := vlansOf(merged, "br-iot")
  121. if len(iot) != 1 || iot[0].ID != 999 {
  122. t.Fatalf("br-iot = %+v, want only the real VLAN", iot)
  123. }
  124. // Ports the kernel knows and the config does not are kept untouched.
  125. if got := vlansOf(merged, "eth0"); len(got) != 2 || got[0].ID != 999 || got[1].ID != 555 {
  126. t.Fatalf("eth0 = %+v, want the declared VLANs behind the kernel ones", got)
  127. }
  128. }
  129. func TestMergeVlanPortsKeepsKernelFlags(t *testing.T) {
  130. kernel := []VlanPort{{Port: "lan2", Vlans: []VlanEntry{{ID: 10, Tagged: true}}}}
  131. declared := []VlanPort{{Port: "lan2", Vlans: []VlanEntry{{ID: 10, Name: "IoT", Subnet: "10.0.0.1/24"}}}}
  132. merged := MergeVlanPorts(kernel, declared)
  133. if len(merged) != 1 || len(merged[0].Vlans) != 1 {
  134. t.Fatalf("merged = %+v", merged)
  135. }
  136. e := merged[0].Vlans[0]
  137. if !e.Tagged || e.Name != "IoT" || e.Subnet != "10.0.0.1/24" {
  138. t.Fatalf("entry = %+v, want the kernel flags and the declared name", e)
  139. }
  140. }