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
230 delegate = s:taboption("advanced", Flag, "delegate", translate("Use builtin IPv6-management"))
231 delegate.default = delegate.enabled
234 if not net:is_virtual() then
235 br = s:taboption("physical", Flag, "type", translate("Bridge interfaces"), translate("creates a bridge over specified interface(s)"))
236 br.enabled = "bridge"
238 br:depends("proto", "static")
239 br:depends("proto", "dhcp")
240 br:depends("proto", "none")
242 stp = s:taboption("physical", Flag, "stp", translate("Enable <abbr title=\"Spanning Tree Protocol\">STP</abbr>"),
243 translate("Enables the Spanning Tree Protocol on this bridge"))
244 stp:depends("type", "bridge")
249 if not net:is_floating() then
250 ifname_single = s:taboption("physical", Value, "ifname_single", translate("Interface"))
251 ifname_single.template = "cbi/network_ifacelist"
252 ifname_single.widget = "radio"
253 ifname_single.nobridges = true
254 ifname_single.rmempty = false
255 ifname_single.network = arg[1]
256 ifname_single:depends("type", "")
258 function ifname_single.cfgvalue(self, s)
259 -- let the template figure out the related ifaces through the network model
263 function ifname_single.write(self, s, val)
268 for _, i in ipairs(net:get_interfaces() or { net:get_interface() }) do
269 old_ifs[#old_ifs+1] = i:name()
272 for i in ut.imatch(val) do
273 new_ifs[#new_ifs+1] = i
275 -- if this is not a bridge, only assign first interface
276 if self.option == "ifname_single" then
284 for i = 1, math.max(#old_ifs, #new_ifs) do
285 if old_ifs[i] ~= new_ifs[i] then
287 for i = 1, #old_ifs do
288 net:del_interface(old_ifs[i])
290 for i = 1, #new_ifs do
291 net:add_interface(new_ifs[i])
300 if not net:is_virtual() then
301 ifname_multi = s:taboption("physical", Value, "ifname_multi", translate("Interface"))
302 ifname_multi.template = "cbi/network_ifacelist"
303 ifname_multi.nobridges = true
304 ifname_multi.rmempty = false
305 ifname_multi.network = arg[1]
306 ifname_multi.widget = "checkbox"
307 ifname_multi:depends("type", "bridge")
308 ifname_multi.cfgvalue = ifname_single.cfgvalue
309 ifname_multi.write = ifname_single.write
314 fwzone = s:taboption("firewall", Value, "_fwzone",
315 translate("Create / Assign firewall-zone"),
316 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."))
318 fwzone.template = "cbi/firewall_zonelist"
319 fwzone.network = arg[1]
320 fwzone.rmempty = false
322 function fwzone.cfgvalue(self, section)
324 local z = fw:get_zone_by_network(section)
325 return z and z:name()
328 function fwzone.write(self, section, value)
329 local zone = fw:get_zone(value)
331 if not zone and value == '-' then
332 value = m:formvalue(self:cbid(section) .. ".newzone")
333 if value and #value > 0 then
334 zone = fw:add_zone(value)
336 fw:del_network(section)
341 fw:del_network(section)
342 zone:add_network(section)
348 function p.write() end
349 function p.remove() end
350 function p.validate(self, value, section)
351 if value == net:proto() then
352 if not net:is_floating() and net:is_empty() then
353 local ifn = ((br and (br:formvalue(section) == "bridge"))
354 and ifname_multi:formvalue(section)
355 or ifname_single:formvalue(section))
357 for ifn in ut.imatch(ifn) do
360 return nil, translate("The selected protocol needs a device assigned")
367 local form, ferr = loadfile(
368 ut.libpath() .. "/model/cbi/admin_network/proto_%s.lua" % net:proto()
372 s:taboption("general", DummyValue, "_error",
373 translate("Missing protocol extension for proto %q" % net:proto())
376 setfenv(form, getfenv(1))(m, s, net)
381 for _, field in ipairs(s.children) do
382 if field ~= st and field ~= p and field ~= p_install and field ~= p_switch then
383 if next(field.deps) then
385 for _, dep in ipairs(field.deps) do
386 dep.deps.proto = net:proto()
389 field:depends("proto", net:proto())
396 -- Display DNS settings if dnsmasq is available
399 if has_dnsmasq and net:proto() == "static" then
400 m2 = Map("dhcp", "", "")
402 local has_section = false
404 m2.uci:foreach("dhcp", "dhcp", function(s)
405 if s.interface == arg[1] then
411 if not has_section and has_dnsmasq then
413 s = m2:section(TypedSection, "dhcp", translate("DHCP Server"))
415 s.cfgsections = function() return { "_enable" } end
417 x = s:option(Button, "_enable")
418 x.title = translate("No DHCP Server configured for this interface")
419 x.inputtitle = translate("Setup DHCP Server")
420 x.inputstyle = "apply"
422 elseif has_section then
424 s = m2:section(TypedSection, "dhcp", translate("DHCP Server"))
427 s:tab("general", translate("General Setup"))
428 s:tab("advanced", translate("Advanced Settings"))
429 s:tab("ipv6", translate("IPv6 Settings"))
431 function s.filter(self, section)
432 return m2.uci:get("dhcp", section, "interface") == arg[1]
435 local ignore = s:taboption("general", Flag, "ignore",
436 translate("Ignore interface"),
437 translate("Disable <abbr title=\"Dynamic Host Configuration Protocol\">DHCP</abbr> for " ..
440 local start = s:taboption("general", Value, "start", translate("Start"),
441 translate("Lowest leased address as offset from the network address."))
442 start.optional = true
443 start.datatype = "or(uinteger,ip4addr)"
444 start.default = "100"
446 local limit = s:taboption("general", Value, "limit", translate("Limit"),
447 translate("Maximum number of leased addresses."))
448 limit.optional = true
449 limit.datatype = "uinteger"
450 limit.default = "150"
452 local ltime = s:taboption("general", Value, "leasetime", translate("Leasetime"),
453 translate("Expiry time of leased addresses, minimum is 2 minutes (<code>2m</code>)."))
455 ltime.default = "12h"
457 local dd = s:taboption("advanced", Flag, "dynamicdhcp",
458 translate("Dynamic <abbr title=\"Dynamic Host Configuration Protocol\">DHCP</abbr>"),
459 translate("Dynamically allocate DHCP addresses for clients. If disabled, only " ..
460 "clients having static leases will be served."))
461 dd.default = dd.enabled
463 s:taboption("advanced", Flag, "force", translate("Force"),
464 translate("Force DHCP on this network even if another server is detected."))
466 -- XXX: is this actually useful?
467 --s:taboption("advanced", Value, "name", translate("Name"),
468 -- translate("Define a name for this network."))
470 mask = s:taboption("advanced", Value, "netmask",
471 translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Netmask"),
472 translate("Override the netmask sent to clients. Normally it is calculated " ..
473 "from the subnet that is served."))
476 mask.datatype = "ip4addr"
478 s:taboption("advanced", DynamicList, "dhcp_option", translate("DHCP-Options"),
479 translate("Define additional DHCP options, for example \"<code>6,192.168.2.1," ..
480 "192.168.2.2</code>\" which advertises different DNS servers to clients."))
482 for i, n in ipairs(s.children) do
484 n:depends("ignore", "")
488 o = s:taboption("ipv6", ListValue, "ra", translate("Router Advertisement-Service"))
489 o:value("", translate("disabled"))
490 o:value("server", translate("server mode"))
491 o:value("relay", translate("relay mode"))
492 o:value("hybrid", translate("hybrid mode"))
494 o = s:taboption("ipv6", ListValue, "dhcpv6", translate("DHCPv6-Service"))
495 o:value("", translate("disabled"))
496 o:value("server", translate("server mode"))
497 o:value("relay", translate("relay mode"))
498 o:value("hybrid", translate("hybrid mode"))
500 o = s:taboption("ipv6", ListValue, "ndp", translate("NDP-Proxy"))
501 o:value("", translate("disabled"))
502 o:value("relay", translate("relay mode"))
503 o:value("hybrid", translate("hybrid mode"))
505 o = s:taboption("ipv6", ListValue, "ra_management", translate("DHCPv6-Mode"))
506 o:value("", translate("stateless"))
507 o:value("1", translate("stateless + stateful"))
508 o:value("2", translate("stateful-only"))
509 o:depends("dhcpv6", "server")
510 o:depends("dhcpv6", "hybrid")
513 o = s:taboption("ipv6", Flag, "ra_default", translate("Always announce default router"),
514 translate("Announce as default router even if no public prefix is available."))
515 o:depends("ra", "server")
516 o:depends("ra", "hybrid")
518 s:taboption("ipv6", DynamicList, "dns", translate("Announced DNS servers"))
519 s:taboption("ipv6", DynamicList, "domain", translate("Announced DNS domains"))