modules/admin-full: big vlan rework, now supports tagging, integrity checking and...
[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 local swc = io.popen("swconfig dev %q help 2>/dev/null" % switch_name)
29 if swc then
30
31 local is_port_attr = false
32 local is_vlan_attr = false
33
34 while true do
35 local line = swc:read("*l")
36 if not line then break end
37
38 if line:match("^%s+%-%-vlan") then
39 is_vlan_attr = true
40
41 elseif line:match("^%s+%-%-port") then
42 is_vlan_attr = false
43 is_port_attr = true
44
45 elseif line:match("^Switch %d+:") then
46 num_ports, cpu_port, num_vlans =
47 line:match("ports: (%d+) %(cpu @ (%d+)%), vlans: (%d+)")
48
49 num_ports = tonumber(num_ports or 5)
50 num_vlans = tonumber(num_vlans or 16)
51 cpu_port = tonumber(cpu_port or 5)
52
53 elseif line:match("%-%-pvid") or line:match("%-%-tag") or line:match("%-%-vid") then
54 if is_vlan_attr then has_vlan4k = line:match("%-%-(%w+)") end
55 if is_port_attr then has_ptpvid = line:match("%-%-(%w+)") end
56
57 end
58 end
59
60 swc:close()
61 end
62
63 -- Switch properties
64 s = m:section(NamedSection, x['.name'], "switch", "Switch %q" % switch_name)
65 s.addremove = false
66
67 s:option(Flag, "enable", "Enable this switch")
68 .cfgvalue = function(self, section) return Flag.cfgvalue(self, section) or self.enabled end
69
70 s:option(Flag, "enable_vlan", "Enable VLAN functionality")
71 .cfgvalue = function(self, section) return Flag.cfgvalue(self, section) or self.enabled end
72
73 s:option(Flag, "reset", "Reset switch during setup")
74 .cfgvalue = function(self, section) return Flag.cfgvalue(self, section) or self.enabled end
75
76
77 -- VLAN table
78 s = m:section(TypedSection, "switch_vlan", "VLANs on %q" % switch_name)
79 s.template = "cbi/tblsection"
80 s.addremove = true
81 s.anonymous = true
82
83 s.cfgsections = function(self)
84 local osections = TypedSection.cfgsections(self)
85 local sections = { }
86 local section
87
88 for _, section in luci.util.spairs(
89 osections,
90 function(a, b)
91 return (tonumber(m.uci:get("network", osections[a], has_vlan4k or "vlan") or 9999) or 0)
92 < (tonumber(m.uci:get("network", osections[b], has_vlan4k or "vlan") or 9999) or 0)
93 end
94 ) do
95 sections[#sections+1] = section
96 end
97
98 return sections
99 end
100
101 local port_opts = { }
102 local untagged = { }
103
104 local portvalue = function(self, section)
105 local pt
106 for pt in (m.uci:get("network", section, "ports") or ""):gmatch("%w+") do
107 local pc, tu = pt:match("^(%d+)([tu]*)")
108 if pc == self.option then return (#tu > 0) and tu or "u" end
109 end
110 return ""
111 end
112
113 local portvalidate = function(self, value, section)
114 if value == "u" then
115 if not untagged[self.option] then
116 untagged[self.option] = true
117 else
118 return nil,
119 translatef("Port %d is untagged in multiple VLANs!", tonumber(self.option) + 1)
120 end
121 end
122 return value
123 end
124
125
126 local vid = s:option(Value, has_vlan4k or "vlan", "VLAN ID")
127
128 vid.required = true
129 vid.optional = false
130
131 vid.validate = function(self, value, section)
132 local v = tonumber(value)
133 local m = has_vlan4k and 4094 or (num_vlans - 1)
134 if v ~= nil and v > 0 and v <= m then
135 return value
136 else
137 return nil,
138 translatef("Invalid VLAN ID given! Only IDs between 1 and %d are allowed.", m)
139 end
140 end
141
142 vid.parse = function(self, section)
143 local o
144 local p = { }
145
146 for _, o in ipairs(port_opts) do
147 local v = o:formvalue(section)
148 if v == "t" then
149 p[#p+1] = o.option .. v
150 elseif v == "u" then
151 p[#p+1] = o.option
152 end
153 end
154
155 m.uci:set("network", section, "ports", table.concat(p, " "))
156 return Value.parse(self, section)
157 end
158
159 vid.formvalue = function(self, section)
160 local v = Value.formvalue(self, section)
161 return (v and #v > 0) and v or "x"
162 end
163
164
165 local pt
166 for pt = 0, num_ports - 1 do
167 po = s:option(ListValue, tostring(pt),
168 (pt == cpu_port) and "CPU" or "Port %d" % (pt + 1))
169
170 po:value("", translate("off"))
171 po:value("u" % pt, translate("untagged"))
172 po:value("t" % pt, translate("tagged"))
173
174 po.cfgvalue = portvalue
175 po.validate = portvalidate
176
177 port_opts[#port_opts+1] = po
178 end
179 end
180 )
181
182 return m