2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008-2011 Jo-Philipp Wich <xm@subsignal.org>
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
11 http://www.apache.org/licenses/LICENSE-2.0
16 local fs = require "nixio.fs"
17 local ut = require "luci.util"
18 local pt = require "luci.tools.proto"
19 local nw = require "luci.model.network"
20 local fw = require "luci.model.firewall"
24 local has_dnsmasq = fs.access("/etc/config/dhcp")
25 local has_firewall = fs.access("/etc/config/firewall")
27 m = Map("network", translate("Interfaces") .. " - " .. arg[1]:upper(), translate("On this page you can configure the network interfaces. You can bridge several interfaces by ticking the \"bridge interfaces\" field and enter the names of several network interfaces separated by spaces. You can also use <abbr title=\"Virtual Local Area Network\">VLAN</abbr> notation <samp>INTERFACE.VLANNR</samp> (<abbr title=\"for example\">e.g.</abbr>: <samp>eth0.1</samp>)."))
38 local net = nw:get_network(arg[1])
40 local function backup_ifnames(is_bridge)
41 if not net:is_floating() and not m:get(net:name(), "_orig_ifname") then
42 local ifcs = net:get_interfaces() or { net:get_interface() }
46 for _, ifn in ipairs(ifcs) do
47 ifns[#ifns+1] = ifn:name()
50 m:set(net:name(), "_orig_ifname", table.concat(ifns, " "))
51 m:set(net:name(), "_orig_bridge", tostring(net:is_bridge()))
58 -- redirect to overview page if network does not exist anymore (e.g. after a revert)
60 luci.http.redirect(luci.dispatcher.build_url("admin/network/network"))
64 -- protocol switch was requested, rebuild interface config and reload page
65 if m:formvalue("cbid.network.%s._switch" % net:name()) then
67 local ptype = m:formvalue("cbid.network.%s.proto" % net:name()) or "-"
68 local proto = nw:get_protocol(ptype, net:name())
73 -- if current proto is not floating and target proto is not floating,
74 -- then attempt to retain the ifnames
75 --error(net:proto() .. " > " .. proto:proto())
76 if not net:is_floating() and not proto:is_floating() then
77 -- if old proto is a bridge and new proto not, then clip the
78 -- interface list to the first ifname only
79 if net:is_bridge() and proto:is_virtual() then
82 for _, ifn in ipairs(net:get_interfaces() or { net:get_interface() }) do
86 net:del_interface(ifn)
89 m:del(net:name(), "type")
92 -- if the current proto is floating, the target proto not floating,
93 -- then attempt to restore ifnames from backup
94 elseif net:is_floating() and not proto:is_floating() then
95 -- if we have backup data, then re-add all orphaned interfaces
96 -- from it and restore the bridge choice
97 local br = (m:get(net:name(), "_orig_bridge") == "true")
100 for ifn in ut.imatch(m:get(net:name(), "_orig_ifname")) do
101 ifn = nw:get_interface(ifn)
102 if ifn and not ifn:get_network() then
103 proto:add_interface(ifn)
110 m:set(net:name(), "type", "bridge")
113 -- in all other cases clear the ifnames
116 for _, ifc in ipairs(net:get_interfaces() or { net:get_interface() }) do
117 net:del_interface(ifc)
119 m:del(net:name(), "type")
124 for k, v in pairs(m:get(net:name())) do
125 if k:sub(1,1) ~= "." and
128 k ~= "_orig_ifname" and
136 m:set(net:name(), "proto", proto:proto())
137 m.uci:save("network")
138 m.uci:save("wireless")
141 luci.http.redirect(luci.dispatcher.build_url("admin/network/network", arg[1]))
146 -- dhcp setup was requested, create section and reload page
147 if m:formvalue("cbid.dhcp._enable._enable") then
148 m.uci:section("dhcp", "dhcp", nil, {
156 luci.http.redirect(luci.dispatcher.build_url("admin/network/network", arg[1]))
160 local ifc = net:get_interface()
162 s = m:section(NamedSection, arg[1], "interface", translate("Common Configuration"))
165 s:tab("general", translate("General Setup"))
166 s:tab("advanced", translate("Advanced Settings"))
167 s:tab("physical", translate("Physical Settings"))
170 s:tab("firewall", translate("Firewall Settings"))
174 st = s:taboption("general", DummyValue, "__status", translate("Status"))
176 local function set_status()
177 -- if current network is empty, print a warning
178 if not net:is_floating() and net:is_empty() then
179 st.template = "cbi/dvalue"
181 st.value = translate("There is no device assigned yet, please attach a network device in the \"Physical Settings\" tab")
183 st.template = "admin_network/iface_status"
189 m.on_init = set_status
190 m.on_after_save = set_status
193 p = s:taboption("general", ListValue, "proto", translate("Protocol"))
194 p.default = net:proto()
197 if not net:is_installed() then
198 p_install = s:taboption("general", Button, "_install")
199 p_install.title = translate("Protocol support is not installed")
200 p_install.inputtitle = translate("Install package %q" % net:opkg_package())
201 p_install.inputstyle = "apply"
202 p_install:depends("proto", net:proto())
204 function p_install.write()
205 return luci.http.redirect(
206 luci.dispatcher.build_url("admin/system/packages") ..
207 "?submit=1&install=%s" % net:opkg_package()
213 p_switch = s:taboption("general", Button, "_switch")
214 p_switch.title = translate("Really switch protocol?")
215 p_switch.inputtitle = translate("Switch protocol")
216 p_switch.inputstyle = "apply"
219 for _, pr in ipairs(nw:get_protocols()) do
220 p:value(pr:proto(), pr:get_i18n())
221 if pr:proto() ~= net:proto() then
222 p_switch:depends("proto", pr:proto())
227 auto = s:taboption("advanced", Flag, "auto", translate("Bring up on boot"))
228 auto.default = (net:proto() == "none") and auto.disabled or auto.enabled
231 if not net:is_virtual() then
232 br = s:taboption("physical", Flag, "type", translate("Bridge interfaces"), translate("creates a bridge over specified interface(s)"))
233 br.enabled = "bridge"
235 br:depends("proto", "static")
236 br:depends("proto", "dhcp")
237 br:depends("proto", "none")
239 stp = s:taboption("physical", Flag, "stp", translate("Enable <abbr title=\"Spanning Tree Protocol\">STP</abbr>"),
240 translate("Enables the Spanning Tree Protocol on this bridge"))
241 stp:depends("type", "bridge")
246 if not net:is_floating() then
247 ifname_single = s:taboption("physical", Value, "ifname_single", translate("Interface"))
248 ifname_single.template = "cbi/network_ifacelist"
249 ifname_single.widget = "radio"
250 ifname_single.nobridges = true
251 ifname_single.rmempty = false
252 ifname_single.network = arg[1]
253 ifname_single:depends("type", "")
255 function ifname_single.cfgvalue(self, s)
256 -- let the template figure out the related ifaces through the network model
260 function ifname_single.write(self, s, val)
265 for _, i in ipairs(net:get_interfaces() or { net:get_interface() }) do
266 old_ifs[#old_ifs+1] = i:name()
269 for i in ut.imatch(val) do
270 new_ifs[#new_ifs+1] = i
272 -- if this is not a bridge, only assign first interface
273 if self.option == "ifname_single" then
281 for i = 1, math.max(#old_ifs, #new_ifs) do
282 if old_ifs[i] ~= new_ifs[i] then
284 for i = 1, #old_ifs do
285 net:del_interface(old_ifs[i])
287 for i = 1, #new_ifs do
288 net:add_interface(new_ifs[i])
297 if not net:is_virtual() then
298 ifname_multi = s:taboption("physical", Value, "ifname_multi", translate("Interface"))
299 ifname_multi.template = "cbi/network_ifacelist"
300 ifname_multi.nobridges = true
301 ifname_multi.rmempty = false
302 ifname_multi.network = arg[1]
303 ifname_multi.widget = "checkbox"
304 ifname_multi:depends("type", "bridge")
305 ifname_multi.cfgvalue = ifname_single.cfgvalue
306 ifname_multi.write = ifname_single.write
311 fwzone = s:taboption("firewall", Value, "_fwzone",
312 translate("Create / Assign firewall-zone"),
313 translate("Choose the firewall zone you want to assign to this interface. Select <em>unspecified</em> to remove the interface from the associated zone or fill out the <em>create</em> field to define a new zone and attach the interface to it."))
315 fwzone.template = "cbi/firewall_zonelist"
316 fwzone.network = arg[1]
317 fwzone.rmempty = false
319 function fwzone.cfgvalue(self, section)
321 local z = fw:get_zone_by_network(section)
322 return z and z:name()
325 function fwzone.write(self, section, value)
326 local zone = fw:get_zone(value)
328 if not zone and value == '-' then
329 value = m:formvalue(self:cbid(section) .. ".newzone")
330 if value and #value > 0 then
331 zone = fw:add_zone(value)
333 fw:del_network(section)
338 fw:del_network(section)
339 zone:add_network(section)
345 function p.write() end
346 function p.remove() end
347 function p.validate(self, value, section)
348 if value == net:proto() then
349 if not net:is_floating() and net:is_empty() then
350 local ifn = ((br and (br:formvalue(section) == "bridge"))
351 and ifname_multi:formvalue(section)
352 or ifname_single:formvalue(section))
354 for ifn in ut.imatch(ifn) do
357 return nil, translate("The selected protocol needs a device assigned")
364 local form, ferr = loadfile(
365 ut.libpath() .. "/model/cbi/admin_network/proto_%s.lua" % net:proto()
369 s:taboption("general", DummyValue, "_error",
370 translate("Missing protocol extension for proto %q" % net:proto())
373 setfenv(form, getfenv(1))(m, s, net)
378 for _, field in ipairs(s.children) do
379 if field ~= st and field ~= p and field ~= p_install and field ~= p_switch then
380 if next(field.deps) then
382 for _, dep in ipairs(field.deps) do
383 dep.deps.proto = net:proto()
386 field:depends("proto", net:proto())
393 -- Display DNS settings if dnsmasq is available
396 if has_dnsmasq and net:proto() == "static" then
397 m2 = Map("dhcp", "", "")
399 local has_section = false
401 m2.uci:foreach("dhcp", "dhcp", function(s)
402 if s.interface == arg[1] then
408 if not has_section then
410 s = m2:section(TypedSection, "dhcp", translate("DHCP Server"))
412 s.cfgsections = function() return { "_enable" } end
414 x = s:option(Button, "_enable")
415 x.title = translate("No DHCP Server configured for this interface")
416 x.inputtitle = translate("Setup DHCP Server")
417 x.inputstyle = "apply"
421 s = m2:section(TypedSection, "dhcp", translate("DHCP Server"))
424 s:tab("general", translate("General Setup"))
425 s:tab("advanced", translate("Advanced Settings"))
427 function s.filter(self, section)
428 return m2.uci:get("dhcp", section, "interface") == arg[1]
431 local ignore = s:taboption("general", Flag, "ignore",
432 translate("Ignore interface"),
433 translate("Disable <abbr title=\"Dynamic Host Configuration Protocol\">DHCP</abbr> for " ..
436 local start = s:taboption("general", Value, "start", translate("Start"),
437 translate("Lowest leased address as offset from the network address."))
438 start.optional = true
439 start.datatype = "or(uinteger,ip4addr)"
440 start.default = "100"
442 local limit = s:taboption("general", Value, "limit", translate("Limit"),
443 translate("Maximum number of leased addresses."))
444 limit.optional = true
445 limit.datatype = "uinteger"
446 limit.default = "150"
448 local ltime = s:taboption("general", Value, "leasetime", translate("Leasetime"),
449 translate("Expiry time of leased addresses, minimum is 2 Minutes (<code>2m</code>)."))
451 ltime.default = "12h"
453 local dd = s:taboption("advanced", Flag, "dynamicdhcp",
454 translate("Dynamic <abbr title=\"Dynamic Host Configuration Protocol\">DHCP</abbr>"),
455 translate("Dynamically allocate DHCP addresses for clients. If disabled, only " ..
456 "clients having static leases will be served."))
457 dd.default = dd.enabled
459 s:taboption("advanced", Flag, "force", translate("Force"),
460 translate("Force DHCP on this network even if another server is detected."))
462 -- XXX: is this actually useful?
463 --s:taboption("advanced", Value, "name", translate("Name"),
464 -- translate("Define a name for this network."))
466 mask = s:taboption("advanced", Value, "netmask",
467 translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Netmask"),
468 translate("Override the netmask sent to clients. Normally it is calculated " ..
469 "from the subnet that is served."))
472 mask.datatype = "ip4addr"
474 s:taboption("advanced", DynamicList, "dhcp_option", translate("DHCP-Options"),
475 translate("Define additional DHCP options, for example \"<code>6,192.168.2.1," ..
476 "192.168.2.2</code>\" which advertises different DNS servers to clients."))
478 for i, n in ipairs(s.children) do
480 n:depends("ignore", "")