modules/admin-{mini,full}: prevent crash in iface overview page if ifconfig does...
[project/luci.git] / modules / admin-mini / luasrc / model / cbi / mini / network.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
6
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
10
11         http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14 ]]--
15
16 local wa  = require "luci.tools.webadmin"
17 local sys = require "luci.sys"
18 local fs  = require "nixio.fs"
19
20 local has_pptp  = fs.access("/usr/sbin/pptp")
21 local has_pppoe = fs.glob("/usr/lib/pppd/*/rp-pppoe.so")()
22
23 local network = luci.model.uci.cursor_state():get_all("network")
24
25 local netstat = sys.net.deviceinfo()
26 local ifaces = {}
27
28 for k, v in pairs(network) do
29         if v[".type"] == "interface" and k ~= "loopback" then
30                 table.insert(ifaces, v)
31         end
32 end
33
34 m = Map("network", translate("Network"))
35 s = m:section(Table, ifaces, translate("Status"))
36 s.parse = function() end
37
38 s:option(DummyValue, ".name", translate("Network"))
39
40 hwaddr = s:option(DummyValue, "_hwaddr",
41  translate("<abbr title=\"Media Access Control\">MAC</abbr>-Address"), translate("Hardware Address"))
42 function hwaddr.cfgvalue(self, section)
43         local ix = self.map:get(section, "ifname") or ""
44         local mac = fs.readfile("/sys/class/net/" .. ix .. "/address")
45
46         if not mac then
47                 mac = luci.util.exec("ifconfig " .. ix)
48                 mac = mac and mac:match(" ([A-F0-9:]+)%s*\n")
49         end
50
51         if mac and #mac > 0 then
52                 return mac:upper()
53         end
54
55         return "?"
56 end
57
58
59 s:option(DummyValue, "ipaddr", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
60
61 s:option(DummyValue, "netmask", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Netmask"))
62
63
64 txrx = s:option(DummyValue, "_txrx",
65  translate("Traffic"), translate("transmitted / received"))
66
67 function txrx.cfgvalue(self, section)
68         local ix = self.map:get(section, "ifname")
69
70         local rx = netstat and netstat[ix] and netstat[ix][1]
71         rx = rx and wa.byte_format(tonumber(rx)) or "-"
72
73         local tx = netstat and netstat[ix] and netstat[ix][9]
74         tx = tx and wa.byte_format(tonumber(tx)) or "-"
75
76         return string.format("%s / %s", tx, rx)
77 end
78
79 errors = s:option(DummyValue, "_err",
80  translate("Errors"), translate("TX / RX"))
81
82 function errors.cfgvalue(self, section)
83         local ix = self.map:get(section, "ifname")
84
85         local rx = netstat and netstat[ix] and netstat[ix][3]
86         local tx = netstat and netstat[ix] and netstat[ix][11]
87
88         rx = rx and tostring(rx) or "-"
89         tx = tx and tostring(tx) or "-"
90
91         return string.format("%s / %s", tx, rx)
92 end
93
94
95
96 s = m:section(NamedSection, "lan", "interface", translate("Local Network"))
97 s.addremove = false
98 s:option(Value, "ipaddr", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
99
100 nm = s:option(Value, "netmask", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Netmask"))
101 nm:value("255.255.255.0")
102 nm:value("255.255.0.0")
103 nm:value("255.0.0.0")
104
105 gw = s:option(Value, "gateway", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Gateway") .. translate(" (optional)"))
106 gw.rmempty = true
107 dns = s:option(Value, "dns", translate("<abbr title=\"Domain Name System\">DNS</abbr>-Server") .. translate(" (optional)"))
108 dns.rmempty = true
109
110
111 s = m:section(NamedSection, "wan", "interface", translate("Internet Connection"))
112 s.addremove = false
113 p = s:option(ListValue, "proto", translate("Protocol"))
114 p.override_values = true
115 p:value("none", "disabled")
116 p:value("static", translate("manual"))
117 p:value("dhcp", translate("automatic"))
118 if has_pppoe then p:value("pppoe", "PPPoE") end
119 if has_pptp  then p:value("pptp",  "PPTP")  end
120
121 function p.write(self, section, value)
122         -- Always set defaultroute to PPP and use remote dns
123         -- Overwrite a bad variable behaviour in OpenWrt
124         if value == "pptp" or value == "pppoe" then
125                 self.map:set(section, "peerdns", "1")
126                 self.map:set(section, "defaultroute", "1")
127         end
128         return ListValue.write(self, section, value)
129 end
130
131 if not ( has_pppoe and has_pptp ) then
132         p.description = translate("You need to install \"ppp-mod-pppoe\" for PPPoE or \"pptp\" for PPtP support")
133 end
134
135
136 ip = s:option(Value, "ipaddr", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
137 ip:depends("proto", "static")
138
139 nm = s:option(Value, "netmask", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Netmask"))
140 nm:depends("proto", "static")
141
142 gw = s:option(Value, "gateway", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Gateway"))
143 gw:depends("proto", "static")
144 gw.rmempty = true
145
146 dns = s:option(Value, "dns", translate("<abbr title=\"Domain Name System\">DNS</abbr>-Server"))
147 dns:depends("proto", "static")
148 dns.rmempty = true
149
150 usr = s:option(Value, "username", translate("Username"))
151 usr:depends("proto", "pppoe")
152 usr:depends("proto", "pptp")
153
154 pwd = s:option(Value, "password", translate("Password"))
155 pwd.password = true
156 pwd:depends("proto", "pppoe")
157 pwd:depends("proto", "pptp")
158
159
160 -- Allow user to set MSS correction here if the UCI firewall is installed
161 -- This cures some cancer for providers with pre-war routers
162 if fs.access("/etc/config/firewall") then
163         mssfix = s:option(Flag, "_mssfix",
164                 translate("Clamp Segment Size"), translate("Fixes problems with unreachable websites, submitting forms or other unexpected behaviour for some ISPs."))
165         mssfix.rmempty = false
166
167         function mssfix.cfgvalue(self)
168                 local value
169                 m.uci:foreach("firewall", "forwarding", function(s)
170                         if s.src == "lan" and s.dest == "wan" then
171                                 value = s.mtu_fix
172                         end
173                 end)
174                 return value
175         end
176
177         function mssfix.write(self, section, value)
178                 m.uci:foreach("firewall", "forwarding", function(s)
179                         if s.src == "lan" and s.dest == "wan" then
180                                 m.uci:set("firewall", s[".name"], "mtu_fix", value)
181                                 m:chain("firewall")
182                         end
183                 end)
184         end
185 end
186
187 kea = s:option(Flag, "keepalive", translate("automatically reconnect"))
188 kea:depends("proto", "pppoe")
189 kea:depends("proto", "pptp")
190 kea.rmempty = true
191 kea.enabled = "10"
192
193
194 cod = s:option(Value, "demand", translate("disconnect when idle for"), "s")
195 cod:depends("proto", "pppoe")
196 cod:depends("proto", "pptp")
197 cod.rmempty = true
198
199 srv = s:option(Value, "server", translate("<abbr title=\"Point-to-Point Tunneling Protocol\">PPTP</abbr>-Server"))
200 srv:depends("proto", "pptp")
201 srv.rmempty = true
202
203
204
205 return m