network.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. -- Copyright 2008 Steven Barth <steven@midlink.org>
  2. -- Copyright 2008 Jo-Philipp Wich <jow@openwrt.org>
  3. -- Licensed to the public under the Apache License 2.0.
  4. local wa = require "luci.tools.webadmin"
  5. local sys = require "luci.sys"
  6. local fs = require "nixio.fs"
  7. local has_pptp = fs.access("/usr/sbin/pptp")
  8. local has_pppoe = fs.glob("/usr/lib/pppd/*/rp-pppoe.so")()
  9. local network = luci.model.uci.cursor_state():get_all("network")
  10. local netstat = sys.net.deviceinfo()
  11. local ifaces = {}
  12. for k, v in pairs(network) do
  13. if v[".type"] == "interface" and k ~= "loopback" then
  14. table.insert(ifaces, v)
  15. end
  16. end
  17. m = Map("network", translate("Network"))
  18. s = m:section(Table, ifaces, translate("Status"))
  19. s.parse = function() end
  20. s:option(DummyValue, ".name", translate("Network"))
  21. hwaddr = s:option(DummyValue, "_hwaddr",
  22. translate("<abbr title=\"Media Access Control\">MAC</abbr>-Address"), translate("Hardware Address"))
  23. function hwaddr.cfgvalue(self, section)
  24. local ix = self.map:get(section, "ifname") or ""
  25. local mac = fs.readfile("/sys/class/net/" .. ix .. "/address")
  26. if not mac then
  27. mac = luci.util.exec("ifconfig " .. ix)
  28. mac = mac and mac:match(" ([A-F0-9:]+)%s*\n")
  29. end
  30. if mac and #mac > 0 then
  31. return mac:upper()
  32. end
  33. return "?"
  34. end
  35. s:option(DummyValue, "ipaddr", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
  36. s:option(DummyValue, "netmask", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Netmask"))
  37. txrx = s:option(DummyValue, "_txrx",
  38. translate("Traffic"), translate("transmitted / received"))
  39. function txrx.cfgvalue(self, section)
  40. local ix = self.map:get(section, "ifname")
  41. local rx = netstat and netstat[ix] and netstat[ix][1]
  42. rx = rx and wa.byte_format(tonumber(rx)) or "-"
  43. local tx = netstat and netstat[ix] and netstat[ix][9]
  44. tx = tx and wa.byte_format(tonumber(tx)) or "-"
  45. return string.format("%s / %s", tx, rx)
  46. end
  47. errors = s:option(DummyValue, "_err",
  48. translate("Errors"), translate("TX / RX"))
  49. function errors.cfgvalue(self, section)
  50. local ix = self.map:get(section, "ifname")
  51. local rx = netstat and netstat[ix] and netstat[ix][3]
  52. local tx = netstat and netstat[ix] and netstat[ix][11]
  53. rx = rx and tostring(rx) or "-"
  54. tx = tx and tostring(tx) or "-"
  55. return string.format("%s / %s", tx, rx)
  56. end
  57. s = m:section(NamedSection, "lan", "interface", translate("Local Network"))
  58. s.addremove = false
  59. s:option(Value, "ipaddr", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
  60. nm = s:option(Value, "netmask", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Netmask"))
  61. nm:value("255.255.255.0")
  62. nm:value("255.255.0.0")
  63. nm:value("255.0.0.0")
  64. gw = s:option(Value, "gateway", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Gateway") .. translate(" (optional)"))
  65. gw.rmempty = true
  66. dns = s:option(Value, "dns", translate("<abbr title=\"Domain Name System\">DNS</abbr>-Server") .. translate(" (optional)"))
  67. dns.rmempty = true
  68. s = m:section(NamedSection, "wan", "interface", translate("Internet Connection"))
  69. s.addremove = false
  70. p = s:option(ListValue, "proto", translate("Protocol"))
  71. p.override_values = true
  72. p:value("none", "disabled")
  73. p:value("static", translate("manual"))
  74. p:value("dhcp", translate("automatic"))
  75. if has_pppoe then p:value("pppoe", "PPPoE") end
  76. if has_pptp then p:value("pptp", "PPTP") end
  77. function p.write(self, section, value)
  78. -- Always set defaultroute to PPP and use remote dns
  79. -- Overwrite a bad variable behaviour in OpenWrt
  80. if value == "pptp" or value == "pppoe" then
  81. self.map:set(section, "peerdns", "1")
  82. self.map:set(section, "defaultroute", "1")
  83. end
  84. return ListValue.write(self, section, value)
  85. end
  86. if not ( has_pppoe and has_pptp ) then
  87. p.description = translate("You need to install \"ppp-mod-pppoe\" for PPPoE or \"pptp\" for PPtP support")
  88. end
  89. ip = s:option(Value, "ipaddr", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
  90. ip:depends("proto", "static")
  91. nm = s:option(Value, "netmask", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Netmask"))
  92. nm:depends("proto", "static")
  93. gw = s:option(Value, "gateway", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Gateway"))
  94. gw:depends("proto", "static")
  95. gw.rmempty = true
  96. dns = s:option(Value, "dns", translate("<abbr title=\"Domain Name System\">DNS</abbr>-Server"))
  97. dns:depends("proto", "static")
  98. dns.rmempty = true
  99. usr = s:option(Value, "username", translate("Username"))
  100. usr:depends("proto", "pppoe")
  101. usr:depends("proto", "pptp")
  102. pwd = s:option(Value, "password", translate("Password"))
  103. pwd.password = true
  104. pwd:depends("proto", "pppoe")
  105. pwd:depends("proto", "pptp")
  106. -- Allow user to set MSS correction here if the UCI firewall is installed
  107. -- This cures some cancer for providers with pre-war routers
  108. if fs.access("/etc/config/firewall") then
  109. mssfix = s:option(Flag, "_mssfix",
  110. translate("Clamp Segment Size"), translate("Fixes problems with unreachable websites, submitting forms or other unexpected behaviour for some ISPs."))
  111. mssfix.rmempty = false
  112. function mssfix.cfgvalue(self)
  113. local value
  114. m.uci:foreach("firewall", "forwarding", function(s)
  115. if s.src == "lan" and s.dest == "wan" then
  116. value = s.mtu_fix
  117. end
  118. end)
  119. return value
  120. end
  121. function mssfix.write(self, section, value)
  122. m.uci:foreach("firewall", "forwarding", function(s)
  123. if s.src == "lan" and s.dest == "wan" then
  124. m.uci:set("firewall", s[".name"], "mtu_fix", value)
  125. m:chain("firewall")
  126. end
  127. end)
  128. end
  129. end
  130. kea = s:option(Flag, "keepalive", translate("automatically reconnect"))
  131. kea:depends("proto", "pppoe")
  132. kea:depends("proto", "pptp")
  133. kea.rmempty = true
  134. kea.enabled = "10"
  135. cod = s:option(Value, "demand", translate("disconnect when idle for"), "s")
  136. cod:depends("proto", "pppoe")
  137. cod:depends("proto", "pptp")
  138. cod.rmempty = true
  139. srv = s:option(Value, "server", translate("<abbr title=\"Point-to-Point Tunneling Protocol\">PPTP</abbr>-Server"))
  140. srv:depends("proto", "pptp")
  141. srv.rmempty = true
  142. return m