1
0

network.lua 6.3 KB

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