508037fb036e6c2fb7b94c7c12ebf26a5d2480ad
[project/luci.git] / modules / admin-full / luasrc / model / cbi / admin_network / 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 sys = require "luci.sys"
17 local wa  = require "luci.tools.webadmin"
18 local fs  = require "nixio.fs"
19
20 local netstate = luci.model.uci.cursor_state():get_all("network")
21 m = Map("network", translate("Interfaces"))
22
23 local created
24 local netstat = sys.net.deviceinfo()
25
26 s = m:section(TypedSection, "interface", "")
27 s.addremove = true
28 s.extedit   = luci.dispatcher.build_url("admin", "network", "network") .. "/%s"
29 s.template  = "cbi/tblsection"
30 s.override_scheme = true
31
32 function s.filter(self, section)
33         return section ~= "loopback" and section
34 end
35
36 function s.create(self, section)
37         if TypedSection.create(self, section) then
38                 created = section
39         else
40                 self.invalid_cts = true
41         end
42 end
43
44 function s.parse(self, ...)
45         TypedSection.parse(self, ...)
46         if created then
47                 m.uci:save("network")
48                 luci.http.redirect(luci.dispatcher.build_url("admin", "network", "network")
49                  .. "/" .. created)
50         end
51 end
52
53 up = s:option(Flag, "up")
54 function up.cfgvalue(self, section)
55         return netstate[section] and netstate[section].up or "0"
56 end
57
58 function up.write(self, section, value)
59         local call
60         if value == "1" then
61                 call = "ifup"
62         elseif value == "0" then
63                 call = "ifdown"
64         end
65         os.execute(call .. " " .. section .. " >/dev/null 2>&1")
66 end
67
68 ifname = s:option(DummyValue, "ifname", translate("Device"))
69 function ifname.cfgvalue(self, section)
70         return netstate[section] and netstate[section].ifname
71 end
72
73 ifname.titleref = luci.dispatcher.build_url("admin", "network", "vlan")
74
75
76 if luci.model.uci.cursor():load("firewall") then
77         zone = s:option(DummyValue, "_zone", translate("Zone"))
78         zone.titleref = luci.dispatcher.build_url("admin", "network", "firewall", "zones")
79
80         function zone.cfgvalue(self, section)
81                 return table.concat(wa.network_get_zones(section) or { "-" }, ", ")
82         end
83 end
84
85 hwaddr = s:option(DummyValue, "_hwaddr")
86 function hwaddr.cfgvalue(self, section)
87         local ix = self.map:get(section, "ifname") or ""
88         return fs.readfile("/sys/class/net/" .. ix .. "/address")
89          or luci.util.exec("ifconfig " .. ix):match(" ([A-F0-9:]+)%s*\n")
90          or "n/a"
91 end
92
93
94 ipaddr = s:option(DummyValue, "ipaddr", translate("Addresses"))
95 function ipaddr.cfgvalue(self, section)
96         return table.concat(wa.network_get_addresses(section), ", ")
97 end
98
99 txrx = s:option(DummyValue, "_txrx")
100
101 function txrx.cfgvalue(self, section)
102         local ix = self.map:get(section, "ifname")
103
104         local rx = netstat and netstat[ix] and netstat[ix][1]
105         rx = rx and wa.byte_format(tonumber(rx)) or "-"
106
107         local tx = netstat and netstat[ix] and netstat[ix][9]
108         tx = tx and wa.byte_format(tonumber(tx)) or "-"
109
110         return string.format("%s / %s", tx, rx)
111 end
112
113 errors = s:option(DummyValue, "_err")
114
115 function errors.cfgvalue(self, section)
116         local ix = self.map:get(section, "ifname")
117
118         local rx = netstat and netstat[ix] and netstat[ix][3]
119         local tx = netstat and netstat[ix] and netstat[ix][11]
120
121         rx = rx and tostring(rx) or "-"
122         tx = tx and tostring(tx) or "-"
123
124         return string.format("%s / %s", tx, rx)
125 end
126
127 return m