network.lua 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. --[[
  2. Copyright 2014 Nils Schneider <nils@nilsschneider.net>
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. ]]--
  8. local uci = require("simple-uci").cursor()
  9. local sysconfig = require 'gluon.sysconfig'
  10. local util = require 'gluon.util'
  11. local wan = uci:get_all("network", "wan")
  12. local wan6 = uci:get_all("network", "wan6")
  13. local dns_static = uci:get_first("gluon-wan-dnsmasq", "static")
  14. local f = Form(translate("WAN connection"))
  15. local s = f:section(Section)
  16. local ipv4 = s:option(ListValue, "ipv4", translate("IPv4"))
  17. ipv4:value("dhcp", translate("Automatic (DHCP)"))
  18. ipv4:value("static", translate("Static"))
  19. ipv4:value("none", translate("Disabled"))
  20. ipv4.default = wan.proto
  21. local ipv4_addr = s:option(Value, "ipv4_addr", translate("IP address"))
  22. ipv4_addr:depends(ipv4, "static")
  23. ipv4_addr.default = wan.ipaddr
  24. ipv4_addr.datatype = "ip4addr"
  25. local ipv4_netmask = s:option(Value, "ipv4_netmask", translate("Netmask"))
  26. ipv4_netmask:depends(ipv4, "static")
  27. ipv4_netmask.default = wan.netmask or "255.255.255.0"
  28. ipv4_netmask.datatype = "ip4addr"
  29. local ipv4_gateway = s:option(Value, "ipv4_gateway", translate("Gateway"))
  30. ipv4_gateway:depends(ipv4, "static")
  31. ipv4_gateway.default = wan.gateway
  32. ipv4_gateway.datatype = "ip4addr"
  33. local s = f:section(Section)
  34. local ipv6 = s:option(ListValue, "ipv6", translate("IPv6"))
  35. ipv6:value("dhcpv6", translate("Automatic (RA/DHCPv6)"))
  36. ipv6:value("static", translate("Static"))
  37. ipv6:value("none", translate("Disabled"))
  38. ipv6.default = wan6.proto
  39. local ipv6_addr = s:option(Value, "ipv6_addr", translate("IP address"))
  40. ipv6_addr:depends(ipv6, "static")
  41. ipv6_addr.default = wan6.ip6addr
  42. ipv6_addr.datatype = "ip6addr"
  43. local ipv6_gateway = s:option(Value, "ipv6_gateway", translate("Gateway"))
  44. ipv6_gateway:depends(ipv6, "static")
  45. ipv6_gateway.default = wan6.ip6gw
  46. ipv6_gateway.datatype = "ip6addr"
  47. if dns_static then
  48. local s = f:section(Section)
  49. local dns = s:option(DynamicList, "dns", translate("Static DNS servers"))
  50. dns.default = uci:get_list("gluon-wan-dnsmasq", dns_static, "server")
  51. dns.datatype = "ipaddr"
  52. dns.optional = true
  53. function dns:write(data)
  54. uci:set_list("gluon-wan-dnsmasq", dns_static, "server", data)
  55. uci:commit("gluon-wan-dnsmasq")
  56. end
  57. end
  58. local s = f:section(Section)
  59. local mesh_wan = s:option(Flag, "mesh_wan", translate("Enable meshing on the WAN interface"))
  60. mesh_wan.default = not uci:get_bool("network", "mesh_wan", "disabled")
  61. function mesh_wan:write(data)
  62. uci:set("network", "mesh_wan", "disabled", not data)
  63. end
  64. local mesh_wan_legacy = s:option(Flag, "mesh_wan_legacy", translate("Use legacy mode for WAN meshing"))
  65. mesh_wan_legacy.default = mesh_wan.default and uci:get_bool("network", "mesh_wan", "legacy")
  66. mesh_wan_legacy:depends(mesh_wan, true)
  67. function mesh_wan_legacy:write(data)
  68. uci:set("network", "mesh_wan", "legacy", data)
  69. end
  70. if sysconfig.lan_ifname then
  71. local s = f:section(Section)
  72. local mesh_lan = s:option(Flag, "mesh_lan", translate("Enable meshing on the LAN interface"))
  73. mesh_lan.default = not uci:get_bool("network", "mesh_lan", "disabled")
  74. function mesh_lan:write(data)
  75. uci:set("network", "mesh_lan", "disabled", not data)
  76. local interfaces = uci:get_list("network", "client", "ifname")
  77. for lanif in sysconfig.lan_ifname:gmatch('%S+') do
  78. if data then
  79. util.remove_from_set(interfaces, lanif)
  80. else
  81. util.add_to_set(interfaces, lanif)
  82. end
  83. end
  84. uci:set_list("network", "client", "ifname", interfaces)
  85. end
  86. local mesh_lan_legacy = s:option(Flag, "mesh_lan_legacy", translate("Use legacy mode for LAN meshing"))
  87. mesh_lan_legacy.default = mesh_lan.default and uci:get_bool("network", "mesh_lan", "legacy")
  88. mesh_lan_legacy:depends(mesh_lan, true)
  89. function mesh_lan_legacy:write(data)
  90. uci:set("network", "mesh_lan", "legacy", data)
  91. end
  92. end
  93. local section
  94. uci:foreach("system", "gpio_switch", function(s)
  95. if s[".name"]:match("poe") then
  96. if not section then
  97. section = f:section(Section)
  98. end
  99. local port = s.name:match("^PoE Power Port(%d*)$")
  100. local name
  101. if port then
  102. name = translatef("Enable PoE Power Port %s", port)
  103. else
  104. name = translate("Enable " .. s.name)
  105. end
  106. local poe = section:option(Flag, s[".name"], name)
  107. poe.default = uci:get_bool("system", s[".name"], "value")
  108. function poe:write(data)
  109. uci:set("system", s[".name"], "value", data)
  110. end
  111. end
  112. end)
  113. function f:write()
  114. uci:set("network", "wan", "proto", ipv4.data)
  115. if ipv4.data == "static" then
  116. uci:set("network", "wan", "ipaddr", ipv4_addr.data)
  117. uci:set("network", "wan", "netmask", ipv4_netmask.data)
  118. uci:set("network", "wan", "gateway", ipv4_gateway.data)
  119. else
  120. uci:delete("network", "wan", "ipaddr")
  121. uci:delete("network", "wan", "netmask")
  122. uci:delete("network", "wan", "gateway")
  123. end
  124. uci:set("network", "wan6", "proto", ipv6.data)
  125. if ipv6.data == "static" then
  126. uci:set("network", "wan6", "ip6addr", ipv6_addr.data)
  127. uci:set("network", "wan6", "ip6gw", ipv6_gateway.data)
  128. else
  129. uci:delete("network", "wan6", "ip6addr")
  130. uci:delete("network", "wan6", "ip6gw")
  131. end
  132. uci:commit("network")
  133. uci:commit('system')
  134. end
  135. return f