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.
5 local wa = require "luci.tools.webadmin"
6 local sys = require "luci.sys"
7 local fs = require "nixio.fs"
9 local has_pptp = fs.access("/usr/sbin/pptp")
10 local has_pppoe = fs.glob("/usr/lib/pppd/*/rp-pppoe.so")()
12 local network = luci.model.uci.cursor_state():get_all("network")
14 local netstat = sys.net.deviceinfo()
17 for k, v in pairs(network) do
18 if v[".type"] == "interface" and k ~= "loopback" then
19 table.insert(ifaces, v)
23 m = Map("network", translate("Network"))
24 s = m:section(Table, ifaces, translate("Status"))
25 s.parse = function() end
27 s:option(DummyValue, ".name", translate("Network"))
29 hwaddr = s:option(DummyValue, "_hwaddr",
30 translate("<abbr title=\"Media Access Control\">MAC</abbr>-Address"), translate("Hardware Address"))
31 function hwaddr.cfgvalue(self, section)
32 local ix = self.map:get(section, "ifname") or ""
33 local mac = fs.readfile("/sys/class/net/" .. ix .. "/address")
36 mac = luci.util.exec("ifconfig " .. ix)
37 mac = mac and mac:match(" ([A-F0-9:]+)%s*\n")
40 if mac and #mac > 0 then
48 s:option(DummyValue, "ipaddr", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
50 s:option(DummyValue, "netmask", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Netmask"))
53 txrx = s:option(DummyValue, "_txrx",
54 translate("Traffic"), translate("transmitted / received"))
56 function txrx.cfgvalue(self, section)
57 local ix = self.map:get(section, "ifname")
59 local rx = netstat and netstat[ix] and netstat[ix][1]
60 rx = rx and wa.byte_format(tonumber(rx)) or "-"
62 local tx = netstat and netstat[ix] and netstat[ix][9]
63 tx = tx and wa.byte_format(tonumber(tx)) or "-"
65 return string.format("%s / %s", tx, rx)
68 errors = s:option(DummyValue, "_err",
69 translate("Errors"), translate("TX / RX"))
71 function errors.cfgvalue(self, section)
72 local ix = self.map:get(section, "ifname")
74 local rx = netstat and netstat[ix] and netstat[ix][3]
75 local tx = netstat and netstat[ix] and netstat[ix][11]
77 rx = rx and tostring(rx) or "-"
78 tx = tx and tostring(tx) or "-"
80 return string.format("%s / %s", tx, rx)
85 s = m:section(NamedSection, "lan", "interface", translate("Local Network"))
87 s:option(Value, "ipaddr", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
89 nm = s:option(Value, "netmask", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Netmask"))
90 nm:value("255.255.255.0")
91 nm:value("255.255.0.0")
94 gw = s:option(Value, "gateway", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Gateway") .. translate(" (optional)"))
96 dns = s:option(Value, "dns", translate("<abbr title=\"Domain Name System\">DNS</abbr>-Server") .. translate(" (optional)"))
100 s = m:section(NamedSection, "wan", "interface", translate("Internet Connection"))
102 p = s:option(ListValue, "proto", translate("Protocol"))
103 p.override_values = true
104 p:value("none", "disabled")
105 p:value("static", translate("manual"))
106 p:value("dhcp", translate("automatic"))
107 if has_pppoe then p:value("pppoe", "PPPoE") end
108 if has_pptp then p:value("pptp", "PPTP") end
110 function p.write(self, section, value)
111 -- Always set defaultroute to PPP and use remote dns
112 -- Overwrite a bad variable behaviour in OpenWrt
113 if value == "pptp" or value == "pppoe" then
114 self.map:set(section, "peerdns", "1")
115 self.map:set(section, "defaultroute", "1")
117 return ListValue.write(self, section, value)
120 if not ( has_pppoe and has_pptp ) then
121 p.description = translate("You need to install \"ppp-mod-pppoe\" for PPPoE or \"pptp\" for PPtP support")
125 ip = s:option(Value, "ipaddr", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
126 ip:depends("proto", "static")
128 nm = s:option(Value, "netmask", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Netmask"))
129 nm:depends("proto", "static")
131 gw = s:option(Value, "gateway", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Gateway"))
132 gw:depends("proto", "static")
135 dns = s:option(Value, "dns", translate("<abbr title=\"Domain Name System\">DNS</abbr>-Server"))
136 dns:depends("proto", "static")
139 usr = s:option(Value, "username", translate("Username"))
140 usr:depends("proto", "pppoe")
141 usr:depends("proto", "pptp")
143 pwd = s:option(Value, "password", translate("Password"))
145 pwd:depends("proto", "pppoe")
146 pwd:depends("proto", "pptp")
149 -- Allow user to set MSS correction here if the UCI firewall is installed
150 -- This cures some cancer for providers with pre-war routers
151 if fs.access("/etc/config/firewall") then
152 mssfix = s:option(Flag, "_mssfix",
153 translate("Clamp Segment Size"), translate("Fixes problems with unreachable websites, submitting forms or other unexpected behaviour for some ISPs."))
154 mssfix.rmempty = false
156 function mssfix.cfgvalue(self)
158 m.uci:foreach("firewall", "forwarding", function(s)
159 if s.src == "lan" and s.dest == "wan" then
166 function mssfix.write(self, section, value)
167 m.uci:foreach("firewall", "forwarding", function(s)
168 if s.src == "lan" and s.dest == "wan" then
169 m.uci:set("firewall", s[".name"], "mtu_fix", value)
176 kea = s:option(Flag, "keepalive", translate("automatically reconnect"))
177 kea:depends("proto", "pppoe")
178 kea:depends("proto", "pptp")
183 cod = s:option(Value, "demand", translate("disconnect when idle for"), "s")
184 cod:depends("proto", "pppoe")
185 cod:depends("proto", "pptp")
188 srv = s:option(Value, "server", translate("<abbr title=\"Point-to-Point Tunneling Protocol\">PPTP</abbr>-Server"))
189 srv:depends("proto", "pptp")