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"
8 local nx = require "nixio"
10 local has_pptp = fs.access("/usr/sbin/pptp")
11 local has_pppoe = fs.glob("/usr/lib/pppd/*/rp-pppoe.so")()
13 local network = luci.model.uci.cursor_state():get_all("network")
19 for k, v in ipairs(nx.getifaddrs()) do
20 if v.family == "packet" then
42 for k, v in pairs(network) do
43 if v[".type"] == "interface" and k ~= "loopback" then
44 table.insert(ifaces, v)
48 m = Map("network", translate("Network"))
49 s = m:section(Table, ifaces, translate("Status"))
50 s.parse = function() end
52 s:option(DummyValue, ".name", translate("Network"))
54 hwaddr = s:option(DummyValue, "_hwaddr",
55 translate("<abbr title=\"Media Access Control\">MAC</abbr>-Address"), translate("Hardware Address"))
56 function hwaddr.cfgvalue(self, section)
57 local ix = self.map:get(section, "ifname") or ""
58 local mac = fs.readfile("/sys/class/net/" .. ix .. "/address")
61 mac = luci.util.exec("ifconfig " .. ix)
62 mac = mac and mac:match(" ([A-F0-9:]+)%s*\n")
65 if mac and #mac > 0 then
73 s:option(DummyValue, "ipaddr", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
75 s:option(DummyValue, "netmask", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Netmask"))
78 txrx = s:option(DummyValue, "_txrx",
79 translate("Traffic"), translate("transmitted / received"))
81 function txrx.cfgvalue(self, section)
82 local ix = self.map:get(section, "ifname")
84 local rx = netstat and netstat[ix] and netstat[ix][1]
85 rx = rx and wa.byte_format(tonumber(rx)) or "-"
87 local tx = netstat and netstat[ix] and netstat[ix][9]
88 tx = tx and wa.byte_format(tonumber(tx)) or "-"
90 return string.format("%s / %s", tx, rx)
93 errors = s:option(DummyValue, "_err",
94 translate("Errors"), translate("TX / RX"))
96 function errors.cfgvalue(self, section)
97 local ix = self.map:get(section, "ifname")
99 local rx = netstat and netstat[ix] and netstat[ix][3]
100 local tx = netstat and netstat[ix] and netstat[ix][11]
102 rx = rx and tostring(rx) or "-"
103 tx = tx and tostring(tx) or "-"
105 return string.format("%s / %s", tx, rx)
110 s = m:section(NamedSection, "lan", "interface", translate("Local Network"))
112 s:option(Value, "ipaddr", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
114 nm = s:option(Value, "netmask", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Netmask"))
115 nm:value("255.255.255.0")
116 nm:value("255.255.0.0")
117 nm:value("255.0.0.0")
119 gw = s:option(Value, "gateway", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Gateway") .. translate(" (optional)"))
121 dns = s:option(Value, "dns", translate("<abbr title=\"Domain Name System\">DNS</abbr>-Server") .. translate(" (optional)"))
125 s = m:section(NamedSection, "wan", "interface", translate("Internet Connection"))
127 p = s:option(ListValue, "proto", translate("Protocol"))
128 p.override_values = true
129 p:value("none", "disabled")
130 p:value("static", translate("manual"))
131 p:value("dhcp", translate("automatic"))
132 if has_pppoe then p:value("pppoe", "PPPoE") end
133 if has_pptp then p:value("pptp", "PPTP") end
135 function p.write(self, section, value)
136 -- Always set defaultroute to PPP and use remote dns
137 -- Overwrite a bad variable behaviour in OpenWrt
138 if value == "pptp" or value == "pppoe" then
139 self.map:set(section, "peerdns", "1")
140 self.map:set(section, "defaultroute", "1")
142 return ListValue.write(self, section, value)
145 if not ( has_pppoe and has_pptp ) then
146 p.description = translate("You need to install \"ppp-mod-pppoe\" for PPPoE or \"pptp\" for PPtP support")
150 ip = s:option(Value, "ipaddr", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
151 ip:depends("proto", "static")
153 nm = s:option(Value, "netmask", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Netmask"))
154 nm:depends("proto", "static")
156 gw = s:option(Value, "gateway", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Gateway"))
157 gw:depends("proto", "static")
160 dns = s:option(Value, "dns", translate("<abbr title=\"Domain Name System\">DNS</abbr>-Server"))
161 dns:depends("proto", "static")
164 usr = s:option(Value, "username", translate("Username"))
165 usr:depends("proto", "pppoe")
166 usr:depends("proto", "pptp")
168 pwd = s:option(Value, "password", translate("Password"))
170 pwd:depends("proto", "pppoe")
171 pwd:depends("proto", "pptp")
174 -- Allow user to set MSS correction here if the UCI firewall is installed
175 -- This cures some cancer for providers with pre-war routers
176 if fs.access("/etc/config/firewall") then
177 mssfix = s:option(Flag, "_mssfix",
178 translate("Clamp Segment Size"), translate("Fixes problems with unreachable websites, submitting forms or other unexpected behaviour for some ISPs."))
179 mssfix.rmempty = false
181 function mssfix.cfgvalue(self)
183 m.uci:foreach("firewall", "forwarding", function(s)
184 if s.src == "lan" and s.dest == "wan" then
191 function mssfix.write(self, section, value)
192 m.uci:foreach("firewall", "forwarding", function(s)
193 if s.src == "lan" and s.dest == "wan" then
194 m.uci:set("firewall", s[".name"], "mtu_fix", value)
201 kea = s:option(Flag, "keepalive", translate("automatically reconnect"))
202 kea:depends("proto", "pppoe")
203 kea:depends("proto", "pptp")
208 cod = s:option(Value, "demand", translate("disconnect when idle for"), "s")
209 cod:depends("proto", "pppoe")
210 cod:depends("proto", "pptp")
213 srv = s:option(Value, "server", translate("<abbr title=\"Point-to-Point Tunneling Protocol\">PPTP</abbr>-Server"))
214 srv:depends("proto", "pptp")