* luci/modules/admin-mini: port selective protocol handling from admin-full
[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 require("luci.tools.webadmin")
17 require("luci.sys")
18 require("luci.fs")
19
20 local has_pptp = luci.fs.mtime("/usr/sbin/pptp")
21 local has_pppoe = luci.fs.glob("/usr/lib/pppd/*/rp-pppoe.so")
22
23 local network = luci.model.uci.cursor_state():get_all("network")
24
25 local netstat = luci.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("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")
46 or "n/a"
47 end
48
49
50 s:option(DummyValue, "ipaddr", translate("ipaddress"))
51
52 s:option(DummyValue, "netmask", translate("netmask"))
53
54
55 txrx = s:option(DummyValue, "_txrx",
56 translate("network_interface_txrx"), translate("network_interface_txrx_desc"))
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 luci.tools.webadmin.byte_format(tonumber(rx)) or "-"
63
64 local tx = netstat and netstat[ix] and netstat[ix][9]
65 tx = tx and luci.tools.webadmin.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("network_interface_err"), translate("network_interface_err_desc"))
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("m_n_local"))
88 s.addremove = false
89 s:option(Value, "ipaddr", translate("ipaddress"))
90
91 nm = s:option(Value, "netmask", translate("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("gateway") .. translate("cbi_optional"))
97 gw.rmempty = true
98 dns = s:option(Value, "dns", translate("dnsserver") .. translate("cbi_optional"))
99 dns.rmempty = true
100
101
102 s = m:section(NamedSection, "wan", "interface", translate("m_n_inet"))
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", "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
111
112 if not ( has_pppoe and has_pptp ) then
113 p.description = translate("network_interface_prereq_mini")
114 end
115
116
117 ip = s:option(Value, "ipaddr", translate("ipaddress"))
118 ip:depends("proto", "static")
119
120 nm = s:option(Value, "netmask", translate("netmask"))
121 nm:depends("proto", "static")
122
123 gw = s:option(Value, "gateway", translate("gateway"))
124 gw:depends("proto", "static")
125 gw.rmempty = true
126
127 dns = s:option(Value, "dns", translate("dnsserver"))
128 dns:depends("proto", "static")
129 dns.rmempty = true
130
131 usr = s:option(Value, "username", translate("username"))
132 usr:depends("proto", "pppoe")
133 usr:depends("proto", "pptp")
134
135 pwd = s:option(Value, "password", translate("password"))
136 pwd.password = true
137 pwd:depends("proto", "pppoe")
138 pwd:depends("proto", "pptp")
139
140 kea = s:option(Flag, "keepalive", translate("m_n_keepalive"))
141 kea:depends("proto", "pppoe")
142 kea:depends("proto", "pptp")
143 kea.rmempty = true
144 kea.enabled = "10"
145
146
147 cod = s:option(Value, "demand", translate("m_n_dialondemand"), "s")
148 cod:depends("proto", "pppoe")
149 cod:depends("proto", "pptp")
150 cod.rmempty = true
151
152 srv = s:option(Value, "server", translate("m_n_pptp_server"))
153 srv:depends("proto", "pptp")
154 srv.rmempty = true
155
156
157
158 return m