Merge pull request #1250 from dibdot/luci-app-travelmate
[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 local nx  = require "nixio"
9
10 local has_pptp  = fs.access("/usr/sbin/pptp")
11 local has_pppoe = fs.glob("/usr/lib/pppd/*/rp-pppoe.so")()
12
13 local network = luci.model.uci.cursor_state():get_all("network")
14
15 local netstat = {}
16 local ifaces = {}
17
18 local k, v
19 for k, v in ipairs(nx.getifaddrs()) do
20         if v.family == "packet" then
21                 local d = v.data
22                 d[1] = d.rx_bytes
23                 d[2] = d.rx_packets
24                 d[3] = d.rx_errors
25                 d[4] = d.rx_dropped
26                 d[5] = 0
27                 d[6] = 0
28                 d[7] = 0
29                 d[8] = d.multicast
30                 d[9] = d.tx_bytes
31                 d[10] = d.tx_packets
32                 d[11] = d.tx_errors
33                 d[12] = d.tx_dropped
34                 d[13] = 0
35                 d[14] = d.collisions
36                 d[15] = 0
37                 d[16] = 0
38                 netstat[v.name] = d
39         end
40 end
41
42 for k, v in pairs(network) do
43         if v[".type"] == "interface" and k ~= "loopback" then
44                 table.insert(ifaces, v)
45         end
46 end
47
48 m = Map("network", translate("Network"))
49 s = m:section(Table, ifaces, translate("Status"))
50 s.parse = function() end
51
52 s:option(DummyValue, ".name", translate("Network"))
53
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")
59
60         if not mac then
61                 mac = luci.util.exec("ifconfig " .. ix)
62                 mac = mac and mac:match(" ([A-F0-9:]+)%s*\n")
63         end
64
65         if mac and #mac > 0 then
66                 return mac:upper()
67         end
68
69         return "?"
70 end
71
72
73 s:option(DummyValue, "ipaddr", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
74
75 s:option(DummyValue, "netmask", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Netmask"))
76
77
78 txrx = s:option(DummyValue, "_txrx",
79  translate("Traffic"), translate("transmitted / received"))
80
81 function txrx.cfgvalue(self, section)
82         local ix = self.map:get(section, "ifname")
83
84         local rx = netstat and netstat[ix] and netstat[ix][1]
85         rx = rx and wa.byte_format(tonumber(rx)) or "-"
86
87         local tx = netstat and netstat[ix] and netstat[ix][9]
88         tx = tx and wa.byte_format(tonumber(tx)) or "-"
89
90         return string.format("%s / %s", tx, rx)
91 end
92
93 errors = s:option(DummyValue, "_err",
94  translate("Errors"), translate("TX / RX"))
95
96 function errors.cfgvalue(self, section)
97         local ix = self.map:get(section, "ifname")
98
99         local rx = netstat and netstat[ix] and netstat[ix][3]
100         local tx = netstat and netstat[ix] and netstat[ix][11]
101
102         rx = rx and tostring(rx) or "-"
103         tx = tx and tostring(tx) or "-"
104
105         return string.format("%s / %s", tx, rx)
106 end
107
108
109
110 s = m:section(NamedSection, "lan", "interface", translate("Local Network"))
111 s.addremove = false
112 s:option(Value, "ipaddr", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
113
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")
118
119 gw = s:option(Value, "gateway", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Gateway") .. translate(" (optional)"))
120 gw.rmempty = true
121 dns = s:option(Value, "dns", translate("<abbr title=\"Domain Name System\">DNS</abbr>-Server") .. translate(" (optional)"))
122 dns.rmempty = true
123
124
125 s = m:section(NamedSection, "wan", "interface", translate("Internet Connection"))
126 s.addremove = false
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
134
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")
141         end
142         return ListValue.write(self, section, value)
143 end
144
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")
147 end
148
149
150 ip = s:option(Value, "ipaddr", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
151 ip:depends("proto", "static")
152
153 nm = s:option(Value, "netmask", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Netmask"))
154 nm:depends("proto", "static")
155
156 gw = s:option(Value, "gateway", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Gateway"))
157 gw:depends("proto", "static")
158 gw.rmempty = true
159
160 dns = s:option(Value, "dns", translate("<abbr title=\"Domain Name System\">DNS</abbr>-Server"))
161 dns:depends("proto", "static")
162 dns.rmempty = true
163
164 usr = s:option(Value, "username", translate("Username"))
165 usr:depends("proto", "pppoe")
166 usr:depends("proto", "pptp")
167
168 pwd = s:option(Value, "password", translate("Password"))
169 pwd.password = true
170 pwd:depends("proto", "pppoe")
171 pwd:depends("proto", "pptp")
172
173
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
180
181         function mssfix.cfgvalue(self)
182                 local value
183                 m.uci:foreach("firewall", "forwarding", function(s)
184                         if s.src == "lan" and s.dest == "wan" then
185                                 value = s.mtu_fix
186                         end
187                 end)
188                 return value
189         end
190
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)
195                                 m:chain("firewall")
196                         end
197                 end)
198         end
199 end
200
201 kea = s:option(Flag, "keepalive", translate("automatically reconnect"))
202 kea:depends("proto", "pppoe")
203 kea:depends("proto", "pptp")
204 kea.rmempty = true
205 kea.enabled = "10"
206
207
208 cod = s:option(Value, "demand", translate("disconnect when idle for"), "s")
209 cod:depends("proto", "pppoe")
210 cod:depends("proto", "pptp")
211 cod.rmempty = true
212
213 srv = s:option(Value, "server", translate("<abbr title=\"Point-to-Point Tunneling Protocol\">PPTP</abbr>-Server"))
214 srv:depends("proto", "pptp")
215 srv.rmempty = true
216
217
218
219 return m