Update my email addresses in the license headers
[project/luci.git] / modules / luci-mod-admin-mini / luasrc / model / cbi / mini / network.lua
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
5 local wa  = require "luci.tools.webadmin"
6 local sys = require "luci.sys"
7 local fs  = require "nixio.fs"
8
9 local has_pptp  = fs.access("/usr/sbin/pptp")
10 local has_pppoe = fs.glob("/usr/lib/pppd/*/rp-pppoe.so")()
11
12 local network = luci.model.uci.cursor_state():get_all("network")
13
14 local netstat = sys.net.deviceinfo()
15 local ifaces = {}
16
17 for k, v in pairs(network) do
18         if v[".type"] == "interface" and k ~= "loopback" then
19                 table.insert(ifaces, v)
20         end
21 end
22
23 m = Map("network", translate("Network"))
24 s = m:section(Table, ifaces, translate("Status"))
25 s.parse = function() end
26
27 s:option(DummyValue, ".name", translate("Network"))
28
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")
34
35         if not mac then
36                 mac = luci.util.exec("ifconfig " .. ix)
37                 mac = mac and mac:match(" ([A-F0-9:]+)%s*\n")
38         end
39
40         if mac and #mac > 0 then
41                 return mac:upper()
42         end
43
44         return "?"
45 end
46
47
48 s:option(DummyValue, "ipaddr", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
49
50 s:option(DummyValue, "netmask", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Netmask"))
51
52
53 txrx = s:option(DummyValue, "_txrx",
54  translate("Traffic"), translate("transmitted / received"))
55
56 function txrx.cfgvalue(self, section)
57         local ix = self.map:get(section, "ifname")
58
59         local rx = netstat and netstat[ix] and netstat[ix][1]
60         rx = rx and wa.byte_format(tonumber(rx)) or "-"
61
62         local tx = netstat and netstat[ix] and netstat[ix][9]
63         tx = tx and wa.byte_format(tonumber(tx)) or "-"
64
65         return string.format("%s / %s", tx, rx)
66 end
67
68 errors = s:option(DummyValue, "_err",
69  translate("Errors"), translate("TX / RX"))
70
71 function errors.cfgvalue(self, section)
72         local ix = self.map:get(section, "ifname")
73
74         local rx = netstat and netstat[ix] and netstat[ix][3]
75         local tx = netstat and netstat[ix] and netstat[ix][11]
76
77         rx = rx and tostring(rx) or "-"
78         tx = tx and tostring(tx) or "-"
79
80         return string.format("%s / %s", tx, rx)
81 end
82
83
84
85 s = m:section(NamedSection, "lan", "interface", translate("Local Network"))
86 s.addremove = false
87 s:option(Value, "ipaddr", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
88
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")
92 nm:value("255.0.0.0")
93
94 gw = s:option(Value, "gateway", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Gateway") .. translate(" (optional)"))
95 gw.rmempty = true
96 dns = s:option(Value, "dns", translate("<abbr title=\"Domain Name System\">DNS</abbr>-Server") .. translate(" (optional)"))
97 dns.rmempty = true
98
99
100 s = m:section(NamedSection, "wan", "interface", translate("Internet Connection"))
101 s.addremove = false
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
109
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")
116         end
117         return ListValue.write(self, section, value)
118 end
119
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")
122 end
123
124
125 ip = s:option(Value, "ipaddr", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
126 ip:depends("proto", "static")
127
128 nm = s:option(Value, "netmask", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Netmask"))
129 nm:depends("proto", "static")
130
131 gw = s:option(Value, "gateway", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Gateway"))
132 gw:depends("proto", "static")
133 gw.rmempty = true
134
135 dns = s:option(Value, "dns", translate("<abbr title=\"Domain Name System\">DNS</abbr>-Server"))
136 dns:depends("proto", "static")
137 dns.rmempty = true
138
139 usr = s:option(Value, "username", translate("Username"))
140 usr:depends("proto", "pppoe")
141 usr:depends("proto", "pptp")
142
143 pwd = s:option(Value, "password", translate("Password"))
144 pwd.password = true
145 pwd:depends("proto", "pppoe")
146 pwd:depends("proto", "pptp")
147
148
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
155
156         function mssfix.cfgvalue(self)
157                 local value
158                 m.uci:foreach("firewall", "forwarding", function(s)
159                         if s.src == "lan" and s.dest == "wan" then
160                                 value = s.mtu_fix
161                         end
162                 end)
163                 return value
164         end
165
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)
170                                 m:chain("firewall")
171                         end
172                 end)
173         end
174 end
175
176 kea = s:option(Flag, "keepalive", translate("automatically reconnect"))
177 kea:depends("proto", "pppoe")
178 kea:depends("proto", "pptp")
179 kea.rmempty = true
180 kea.enabled = "10"
181
182
183 cod = s:option(Value, "demand", translate("disconnect when idle for"), "s")
184 cod:depends("proto", "pppoe")
185 cod:depends("proto", "pptp")
186 cod.rmempty = true
187
188 srv = s:option(Value, "server", translate("<abbr title=\"Point-to-Point Tunneling Protocol\">PPTP</abbr>-Server"))
189 srv:depends("proto", "pptp")
190 srv.rmempty = true
191
192
193
194 return m