2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
16 require("luci.tools.webadmin")
20 local has_pptp = luci.fs.mtime("/usr/sbin/pptp")
21 local has_pppoe = luci.fs.glob("/usr/lib/pppd/*/rp-pppoe.so")
23 local network = luci.model.uci.cursor_state():get_all("network")
25 local netstat = luci.sys.net.deviceinfo()
28 for k, v in pairs(network) do
29 if v[".type"] == "interface" and k ~= "loopback" then
30 table.insert(ifaces, v)
34 m = Map("network", translate("network"))
35 s = m:section(Table, ifaces, translate("status"))
36 s.parse = function() end
38 s:option(DummyValue, ".name", translate("network"))
40 hwaddr = s:option(DummyValue, "_hwaddr",
41 translate("network_interface_hwaddr"), translate("network_interface_hwaddr_desc"))
42 function hwaddr.cfgvalue(self, section)
43 local ix = self.map:get(section, "ifname") or ""
44 return luci.fs.readfile("/sys/class/net/" .. ix .. "/address")
45 or luci.util.exec("ifconfig " .. ix):match(" ([A-F0-9:]+)%s*\n")
50 s:option(DummyValue, "ipaddr", translate("ipaddress"))
52 s:option(DummyValue, "netmask", translate("netmask"))
55 txrx = s:option(DummyValue, "_txrx",
56 translate("network_interface_txrx"), translate("network_interface_txrx_desc"))
58 function txrx.cfgvalue(self, section)
59 local ix = self.map:get(section, "ifname")
61 local rx = netstat and netstat[ix] and netstat[ix][1]
62 rx = rx and luci.tools.webadmin.byte_format(tonumber(rx)) or "-"
64 local tx = netstat and netstat[ix] and netstat[ix][9]
65 tx = tx and luci.tools.webadmin.byte_format(tonumber(tx)) or "-"
67 return string.format("%s / %s", tx, rx)
70 errors = s:option(DummyValue, "_err",
71 translate("network_interface_err"), translate("network_interface_err_desc"))
73 function errors.cfgvalue(self, section)
74 local ix = self.map:get(section, "ifname")
76 local rx = netstat and netstat[ix] and netstat[ix][3]
77 local tx = netstat and netstat[ix] and netstat[ix][11]
79 rx = rx and tostring(rx) or "-"
80 tx = tx and tostring(tx) or "-"
82 return string.format("%s / %s", tx, rx)
87 s = m:section(NamedSection, "lan", "interface", translate("m_n_local"))
89 s:option(Value, "ipaddr", translate("ipaddress"))
91 nm = s:option(Value, "netmask", translate("netmask"))
92 nm:value("255.255.255.0")
93 nm:value("255.255.0.0")
96 gw = s:option(Value, "gateway", translate("gateway") .. translate("cbi_optional"))
98 dns = s:option(Value, "dns", translate("dnsserver") .. translate("cbi_optional"))
102 s = m:section(NamedSection, "wan", "interface", translate("m_n_inet"))
104 p = s:option(ListValue, "proto", translate("protocol"))
105 p.override_values = true
106 p:value("none", "disabled")
107 p:value("static", translate("manual", "manual"))
108 p:value("dhcp", translate("automatic", "automatic"))
109 if has_pppoe then p:value("pppoe", "PPPoE") end
110 if has_pptp then p:value("pptp", "PPTP") end
112 function p.write(self, section, value)
113 -- Always set defaultroute to PPP and use remote dns
114 -- Overwrite a bad variable behaviour in OpenWrt
115 if value == "pptp" or value == "pppoe" then
116 self.map:set(section, "peerdns", "1")
117 self.map:set(section, "defaultroute", "1")
119 return ListValue.write(self, section, value)
122 if not ( has_pppoe and has_pptp ) then
123 p.description = translate("network_interface_prereq_mini")
127 ip = s:option(Value, "ipaddr", translate("ipaddress"))
128 ip:depends("proto", "static")
130 nm = s:option(Value, "netmask", translate("netmask"))
131 nm:depends("proto", "static")
133 gw = s:option(Value, "gateway", translate("gateway"))
134 gw:depends("proto", "static")
137 dns = s:option(Value, "dns", translate("dnsserver"))
138 dns:depends("proto", "static")
141 usr = s:option(Value, "username", translate("username"))
142 usr:depends("proto", "pppoe")
143 usr:depends("proto", "pptp")
145 pwd = s:option(Value, "password", translate("password"))
147 pwd:depends("proto", "pppoe")
148 pwd:depends("proto", "pptp")
151 -- Allow user to set MSS correction here if the UCI firewall is installed
152 -- This cures some cancer for providers with pre-war routers
153 if luci.fs.access("/etc/config/firewall") then
154 mssfix = s:option(Flag, "_mssfix",
155 translate("m_n_mssfix"), translate("m_n_mssfix_desc"))
156 mssfix.rmempty = false
158 function mssfix.cfgvalue(self)
160 m.uci:foreach("firewall", "forwarding", function(s)
161 if s.src == "lan" and s.dest == "wan" then
168 function mssfix.write(self, section, value)
169 m.uci:foreach("firewall", "forwarding", function(s)
170 if s.src == "lan" and s.dest == "wan" then
171 m.uci:set("firewall", s[".name"], "mtu_fix", value)
178 kea = s:option(Flag, "keepalive", translate("m_n_keepalive"))
179 kea:depends("proto", "pppoe")
180 kea:depends("proto", "pptp")
185 cod = s:option(Value, "demand", translate("m_n_dialondemand"), "s")
186 cod:depends("proto", "pppoe")
187 cod:depends("proto", "pptp")
190 srv = s:option(Value, "server", translate("m_n_pptp_server"))
191 srv:depends("proto", "pptp")