43ad212cc36277f9df53cf3af3354e5517448419
[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         return fs.readfile("/sys/class/net/" .. ix .. "/address")
45                 or luci.util.exec("ifconfig " .. ix):match(" ([A-F0-9:]+)%s*\n")
46                 or "n/a"
47 end
48
49
50 s:option(DummyValue, "ipaddr", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
51
52 s:option(DummyValue, "netmask", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Netmask"))
53
54
55 txrx = s:option(DummyValue, "_txrx",
56  translate("Traffic"), translate("transmitted / received"))
57
58 function txrx.cfgvalue(self, section)
59         local ix = self.map:get(section, "ifname")
60
61         local rx = netstat and netstat[ix] and netstat[ix][1]
62         rx = rx and wa.byte_format(tonumber(rx)) or "-"
63
64         local tx = netstat and netstat[ix] and netstat[ix][9]
65         tx = tx and wa.byte_format(tonumber(tx)) or "-"
66
67         return string.format("%s / %s", tx, rx)
68 end
69
70 errors = s:option(DummyValue, "_err",
71  translate("Errors"), translate("TX / RX"))
72
73 function errors.cfgvalue(self, section)
74         local ix = self.map:get(section, "ifname")
75
76         local rx = netstat and netstat[ix] and netstat[ix][3]
77         local tx = netstat and netstat[ix] and netstat[ix][11]
78
79         rx = rx and tostring(rx) or "-"
80         tx = tx and tostring(tx) or "-"
81
82         return string.format("%s / %s", tx, rx)
83 end
84
85
86
87 s = m:section(NamedSection, "lan", "interface", translate("Local Network"))
88 s.addremove = false
89 s:option(Value, "ipaddr", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
90
91 nm = s:option(Value, "netmask", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Netmask"))
92 nm:value("255.255.255.0")
93 nm:value("255.255.0.0")
94 nm:value("255.0.0.0")
95
96 gw = s:option(Value, "gateway", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Gateway") .. translate(" (optional)"))
97 gw.rmempty = true
98 dns = s:option(Value, "dns", translate("<abbr title=\"Domain Name System\">DNS</abbr>-Server") .. translate(" (optional)"))
99 dns.rmempty = true
100
101
102 s = m:section(NamedSection, "wan", "interface", translate("Internet Connection"))
103 s.addremove = false
104 p = s:option(ListValue, "proto", translate("Protocol"))
105 p.override_values = true
106 p:value("none", "disabled")
107 p:value("static", translate("manual"))
108 p:value("dhcp", translate("automatic"))
109 if has_pppoe then p:value("pppoe", "PPPoE") end
110 if has_pptp  then p:value("pptp",  "PPTP")  end
111
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")
118         end
119         return ListValue.write(self, section, value)
120 end
121
122 if not ( has_pppoe and has_pptp ) then
123         p.description = translate("You need to install \"ppp-mod-pppoe\" for PPPoE or \"pptp\" for PPtP support")
124 end
125
126
127 ip = s:option(Value, "ipaddr", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
128 ip:depends("proto", "static")
129
130 nm = s:option(Value, "netmask", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Netmask"))
131 nm:depends("proto", "static")
132
133 gw = s:option(Value, "gateway", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Gateway"))
134 gw:depends("proto", "static")
135 gw.rmempty = true
136
137 dns = s:option(Value, "dns", translate("<abbr title=\"Domain Name System\">DNS</abbr>-Server"))
138 dns:depends("proto", "static")
139 dns.rmempty = true
140
141 usr = s:option(Value, "username", translate("Username"))
142 usr:depends("proto", "pppoe")
143 usr:depends("proto", "pptp")
144
145 pwd = s:option(Value, "password", translate("Password"))
146 pwd.password = true
147 pwd:depends("proto", "pppoe")
148 pwd:depends("proto", "pptp")
149
150
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 fs.access("/etc/config/firewall") then
154         mssfix = s:option(Flag, "_mssfix",
155                 translate("Clamp Segment Size"), translate("Fixes problems with unreachable websites, submitting forms or other unexpected behaviour for some ISPs."))
156         mssfix.rmempty = false
157
158         function mssfix.cfgvalue(self)
159                 local value
160                 m.uci:foreach("firewall", "forwarding", function(s)
161                         if s.src == "lan" and s.dest == "wan" then
162                                 value = s.mtu_fix
163                         end
164                 end)
165                 return value
166         end
167
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)
172                                 m:chain("firewall")
173                         end
174                 end)
175         end
176 end
177
178 kea = s:option(Flag, "keepalive", translate("automatically reconnect"))
179 kea:depends("proto", "pppoe")
180 kea:depends("proto", "pptp")
181 kea.rmempty = true
182 kea.enabled = "10"
183
184
185 cod = s:option(Value, "demand", translate("disconnect when idle for"), "s")
186 cod:depends("proto", "pppoe")
187 cod:depends("proto", "pptp")
188 cod.rmempty = true
189
190 srv = s:option(Value, "server", translate("<abbr title=\"Point-to-Point Tunneling Protocol\">PPTP</abbr>-Server"))
191 srv:depends("proto", "pptp")
192 srv.rmempty = true
193
194
195
196 return m