modules/admin-full: live status, validation for dhcp leases
[project/luci.git] / modules / admin-full / luasrc / model / cbi / admin_network / vlan.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2010 Jo-Philipp Wich <xm@subsignal.org>
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 m = Map("network", translate("Switch"), translate("The network ports on your router can be combined to several <abbr title=\"Virtual Local Area Network\">VLAN</abbr>s in which computers can communicate directly with each other. <abbr title=\"Virtual Local Area Network\">VLAN</abbr>s are often used to separate different network segments. Often there is by default one Uplink port for a connection to the next greater network like the internet and other ports for a local network."))
17
18 m.uci:foreach("network", "switch",
19         function(x)
20                 local switch_name = x.name or x['.name']
21                 local has_vlan4k  = nil
22                 local has_ptpvid  = nil
23                 local max_vid     = 16
24                 local num_vlans   = 16
25                 local num_ports   = 5
26                 local cpu_port    = 5
27
28                 -- Parse some common switch properties from swconfig help output.
29                 local swc = io.popen("swconfig dev %q help 2>/dev/null" % switch_name)
30                 if swc then
31
32                         local is_port_attr = false
33                         local is_vlan_attr = false
34
35                         while true do
36                                 local line = swc:read("*l")
37                                 if not line then break end
38
39                                 if line:match("^%s+%-%-vlan") then
40                                         is_vlan_attr = true
41
42                                 elseif line:match("^%s+%-%-port") then
43                                         is_vlan_attr = false
44                                         is_port_attr = true
45
46                                 elseif line:match("^Switch %d+:") then
47                                         num_ports, cpu_port, num_vlans =
48                                                 line:match("ports: (%d+) %(cpu @ (%d+)%), vlans: (%d+)")
49
50                                         num_ports = tonumber(num_ports or  5)
51                                         num_vlans = tonumber(num_vlans or 16)
52                                         cpu_port  = tonumber(cpu_port  or  5)
53
54                                 elseif line:match(": pvid") or line:match(": tag") or line:match(": vid") then
55                                         if is_vlan_attr then has_vlan4k = line:match(": (%w+)") end
56                                         if is_port_attr then has_ptpvid = line:match(": (%w+)") end
57
58                                 end
59                         end
60
61                         swc:close()
62                 end
63
64
65                 -- The PVID options (if any) are added to this table so that
66                 -- section create below can add the just created vlan to the
67                 -- choice list of the PVID options...
68                 local pvid_opts = { }
69
70                 -- This function re-reads all existing vlan ids and populates
71                 -- PVID options choice lists
72                 local function populate_pvids()
73                         local vlan_ids = { }
74                         m.uci:foreach("network", "switch_vlan",
75                                 function(s)
76                                         local vid = s[has_vlan4k or "vlan"] or s["vlan"]
77                                         if vid ~= nil then
78                                                 vlan_ids[#vlan_ids+1] = vid
79                                         end
80                                 end)
81
82                         local opt, vid
83                         for _, opt in ipairs(pvid_opts) do
84                                 opt:reset_values()
85                                 opt:value("", translate("none"))
86                                 for _, vid in luci.util.vspairs(vlan_ids) do
87                                         opt:value(vid, translatef("VLAN %d", tonumber(vid)))
88                                 end
89                         end
90                 end
91
92                 -- Switch properties
93                 s = m:section(NamedSection, x['.name'], "switch", translatef("Switch %q", switch_name))
94                 s.addremove = false
95
96                 s:option(Flag, "enable", "Enable this switch")
97                         .cfgvalue = function(self, section) return Flag.cfgvalue(self, section) or self.enabled end
98
99                 s:option(Flag, "enable_vlan", "Enable VLAN functionality")
100                         .cfgvalue = function(self, section) return Flag.cfgvalue(self, section) or self.enabled end
101
102                 s:option(Flag, "reset", "Reset switch during setup")
103                         .cfgvalue = function(self, section) return Flag.cfgvalue(self, section) or self.enabled end
104
105
106                 -- VLAN table
107                 s = m:section(TypedSection, "switch_vlan", translatef("VLANs on %q", switch_name))
108                 s.template = "cbi/tblsection"
109                 s.addremove = true
110                 s.anonymous = true
111
112                 -- Override cfgsections callback to enforce row ordering by vlan id.
113                 s.cfgsections = function(self)
114                         local osections = TypedSection.cfgsections(self)
115                         local sections = { }
116                         local section
117
118                         for _, section in luci.util.spairs(
119                                 osections,
120                                 function(a, b)
121                                         return (tonumber(m.uci:get("network", osections[a], has_vlan4k or "vlan")) or 9999)
122                                                 <  (tonumber(m.uci:get("network", osections[b], has_vlan4k or "vlan")) or 9999)
123                                 end
124                         ) do
125                                 sections[#sections+1] = section
126                         end
127
128                         return sections
129                 end
130
131                 -- When creating a new vlan, preset it with the highest found vid + 1.
132                 -- Repopulate the PVID choice lists afterwards.
133                 s.create = function(self, section)
134                         local sid = TypedSection.create(self, section)
135
136                         local max_nr = 0
137                         local max_id = 0
138
139                         m.uci:foreach("network", "switch_vlan",
140                                 function(s)
141                                         local nr = tonumber(s.vlan)
142                                         local id = has_vlan4k and tonumber(s[has_vlan4k])
143                                         if nr ~= nil and nr > max_nr then max_nr = nr end
144                                         if id ~= nil and id > max_id then max_id = id end
145                                 end)
146
147                         m.uci:set("network", sid, "vlan", max_nr + 1)
148
149                         if has_vlan4k then
150                                 m.uci:set("network", sid, has_vlan4k, max_id + 1)
151                         end
152
153                         -- add newly created vlan to the pvid choice list
154                         populate_pvids()
155
156                         return sid
157                 end
158
159                 -- Repopulate PVId choice lists if a vlan gets removed.
160                 s.remove = function(self, section)
161                         local rv = TypedSection.remove(self, section)
162
163                         -- repopulate pvid choices
164                         populate_pvids()
165
166                         return rv
167                 end
168
169
170                 local port_opts = { }
171                 local untagged  = { }
172
173                 -- Parse current tagging state from the "ports" option.
174                 local portvalue = function(self, section)
175                         local pt
176                         for pt in (m.uci:get("network", section, "ports") or ""):gmatch("%w+") do
177                                 local pc, tu = pt:match("^(%d+)([tu]*)")
178                                 if pc == self.option then return (#tu > 0) and tu or "u" end
179                         end
180                         return ""
181                 end
182
183                 -- Validate port tagging. Ensure that a port is only untagged once,
184                 -- bail out if not.
185                 local portvalidate = function(self, value, section)
186                         -- ensure that the ports appears untagged only once
187                         if value == "u" then
188                                 if not untagged[self.option] then
189                                         untagged[self.option] = true
190                                 else
191                                         return nil,
192                                                 translatef("Port %d is untagged in multiple VLANs!", tonumber(self.option) + 1)
193                                 end
194                         end
195                         return value
196                 end
197
198
199                 local vid = s:option(Value, has_vlan4k or "vlan", "VLAN ID")
200
201                 vid.rmempty = false
202                 vid.forcewrite = true
203
204                 -- Validate user provided VLAN ID, make sure its within the bounds
205                 -- allowed by the switch.
206                 vid.validate = function(self, value, section)
207                         local v = tonumber(value)
208                         local m = has_vlan4k and 4094 or (num_vlans - 1)
209                         if v ~= nil and v > 0 and v <= m then
210                                 return value
211                         else
212                                 return nil,
213                                         translatef("Invalid VLAN ID given! Only IDs between %d and %d are allowed.", 1, m)
214                         end
215                 end
216
217                 -- When writing the "vid" or "vlan" option, serialize the port states
218                 -- as well and write them as "ports" option to uci.
219                 vid.write = function(self, section, value)
220                         local o
221                         local p = { }
222
223                         for _, o in ipairs(port_opts) do
224                                 local v = o:formvalue(section)
225                                 if v == "t" then
226                                         p[#p+1] = o.option .. v
227                                 elseif v == "u" then
228                                         p[#p+1] = o.option
229                                 end
230                         end
231
232                         m.uci:set("network", section, "ports", table.concat(p, " "))
233                         return Value.write(self, section, value)
234                 end
235
236                 -- Fallback to "vlan" option if "vid" option is supported but unset.
237                 vid.cfgvalue = function(self, section)
238                         return m.uci:get("network", section, has_vlan4k or "vlan")
239                                 or m.uci:get("network", section, "vlan")
240                 end
241
242                 -- Build per-port off/untagged/tagged choice lists.
243                 local pt
244                 for pt = 0, num_ports - 1 do
245                         local po = s:option(ListValue, tostring(pt),
246                                 (pt == cpu_port) and translate("CPU") or translatef("Port %d", (pt + 1)))
247
248                         po:value("", translate("off"))
249                         po:value("u" % pt, translate("untagged"))
250                         po:value("t" % pt, translate("tagged"))
251
252                         po.cfgvalue = portvalue
253                         po.validate = portvalidate
254                         po.write    = function() end
255
256                         port_opts[#port_opts+1] = po
257                 end
258
259
260                 -- Does this switch support PVIDs?
261                 if has_ptpvid then
262
263                         -- Spawn a "virtual" section. We just attach it to the global
264                         -- switch section here, the overrides below take care of writing
265                         -- the actual values to the correct uci sections.
266                         s = m:section(TypedSection, "switch",
267                                 translatef("Port PVIDs on %q", switch_name),
268                                 translate("Port <abbr title=\"Primary VLAN IDs\">PVIDs</abbr> specify " ..
269                                         "the default VLAN ID added to received untagged frames.<br />" ..
270                                         "Leave the ID field empty to disable auto tagging on the associated port."))
271
272                         s.template  = "cbi/tblsection"
273                         s.addremove = false
274                         s.anonymous = true
275
276                         -- Build port list, store pointers to the option objects in the
277                         -- pvid_opts array so that other callbacks can repopulate their
278                         -- choice lists.
279                         local pt
280                         for pt = 0, num_ports - 1 do
281                                 local po = s:option(ListValue, tostring(pt),
282                                         (pt == cpu_port) and translate("CPU") or translatef("Port %d", (pt + 1)))
283
284                                 -- When cbi queries the current config value for this post,
285                                 -- lookup the associated switch_port section (if any) and
286                                 -- return its "pvid" or "vlan" option value.
287                                 po.cfgvalue = function(self, section)
288                                         local val
289                                         m.uci:foreach("network", "switch_port",
290                                                 function(s)
291                                                         if s.port == self.option then
292                                                                 val = s[has_ptpvid]
293                                                                 return false
294                                                         end
295                                                 end)
296                                         return val
297                                 end
298
299                                 -- On write, find the actual switch_port section associated
300                                 -- to this port and set the value there. Create a new
301                                 -- switch_port section for this port if there is none yet.
302                                 po.write = function(self, section, value)
303                                         local found = false
304
305                                         m.uci:foreach("network", "switch_port",
306                                                 function(s)
307                                                         if s.port == self.option then
308                                                                 m.uci:set("network", s['.name'], has_ptpvid, value)
309                                                                 found = true
310                                                                 return false
311                                                         end
312                                                 end)
313
314                                         if not found then
315                                                 m.uci:section("network", "switch_port", nil, {
316                                                         ["port"]     = self.option,
317                                                         [has_ptpvid] = value
318                                                 })
319                                         end
320                                 end
321
322                                 -- If the user cleared the PVID value on this port, find
323                                 -- the associated switch_port section and clear it.
324                                 -- If the section does not contain any other unrelated
325                                 -- options (like led or blinkrate) then remove it completely,
326                                 -- else just clear out the "pvid" option.
327                                 po.remove = function(self, section)
328                                         m.uci:foreach("network", "switch_port",
329                                                 function(s)
330                                                         if s.port == self.option then
331                                                                 local k, found
332                                                                 local empty = true
333
334                                                                 for k, _ in pairs(s) do
335                                                                         if k:sub(1,1) ~= "." and k ~= "port" and k ~= has_ptpvid then
336                                                                                 empty = false
337                                                                                 break
338                                                                         end
339                                                                 end
340
341                                                                 if empty then
342                                                                         m.uci:delete("network", s['.name'])
343                                                                 else
344                                                                         m.uci:delete("network", s['.name'], has_ptpvid)
345                                                                 end
346
347                                                                 return false
348                                                         end
349                                                 end)
350                                 end
351
352                                 -- The referenced VLAN might just have been removed, simply
353                                 -- return "" (none) in this case to avoid triggering a
354                                 -- validation error.
355                                 po.validate = function(...)
356                                         return ListValue.validate(...) or ""
357                                 end
358
359                                 pvid_opts[#pvid_opts+1] = po
360                         end
361
362                         populate_pvids()
363                 end
364         end
365 )
366
367 return m