1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2008-2011 Jo-Philipp Wich <jow@openwrt.org>
3 -- Licensed to the public under the Apache License 2.0.
5 local fs = require "nixio.fs"
6 local ut = require "luci.util"
7 local pt = require "luci.tools.proto"
8 local nw = require "luci.model.network"
9 local fw = require "luci.model.firewall"
13 local has_dnsmasq = fs.access("/etc/config/dhcp")
14 local has_firewall = fs.access("/etc/config/firewall")
16 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>)."))
17 m.redirect = luci.dispatcher.build_url("admin", "network", "network")
29 local net = nw:get_network(arg[1])
31 local function set_ifstate(name, option, value)
34 m.uci:foreach("luci", "ifstate", function (s)
35 if s.interface == name then
36 m.uci:set("luci", s[".name"], option, value)
43 local sid = m.uci:add("luci", "ifstate")
44 m.uci:set("luci", sid, "interface", name)
45 m.uci:set("luci", sid, option, value)
51 local function get_ifstate(name, option)
54 m.uci:foreach("luci", "ifstate", function (s)
55 if s.interface == name then
56 val = m.uci:get("luci", s[".name"], option)
64 local function backup_ifnames(is_bridge)
65 if not net:is_floating() and not get_ifstate(net:name(), "ifname") then
66 local ifcs = net:get_interfaces() or { net:get_interface() }
70 for _, ifn in ipairs(ifcs) do
71 local wif = ifn:get_wifinet()
72 ifns[#ifns+1] = wif and wif:id() or ifn:name()
75 set_ifstate(net:name(), "ifname", table.concat(ifns, " "))
76 set_ifstate(net:name(), "bridge", tostring(net:is_bridge()))
83 -- redirect to overview page if network does not exist anymore (e.g. after a revert)
85 luci.http.redirect(luci.dispatcher.build_url("admin/network/network"))
89 -- protocol switch was requested, rebuild interface config and reload page
90 if m:formvalue("cbid.network.%s._switch" % net:name()) then
92 local ptype = m:formvalue("cbid.network.%s.proto" % net:name()) or "-"
93 local proto = nw:get_protocol(ptype, net:name())
98 -- if current proto is not floating and target proto is not floating,
99 -- then attempt to retain the ifnames
100 --error(net:proto() .. " > " .. proto:proto())
101 if not net:is_floating() and not proto:is_floating() then
102 -- if old proto is a bridge and new proto not, then clip the
103 -- interface list to the first ifname only
104 if net:is_bridge() and proto:is_virtual() then
107 for _, ifn in ipairs(net:get_interfaces() or { net:get_interface() }) do
111 net:del_interface(ifn)
114 m:del(net:name(), "type")
117 -- if the current proto is floating, the target proto not floating,
118 -- then attempt to restore ifnames from backup
119 elseif net:is_floating() and not proto:is_floating() then
120 -- if we have backup data, then re-add all orphaned interfaces
121 -- from it and restore the bridge choice
122 local br = (get_ifstate(net:name(), "bridge") == "true")
125 for ifn in ut.imatch(get_ifstate(net:name(), "ifname")) do
126 ifn = nw:get_interface(ifn)
127 if ifn and not ifn:get_network() then
128 proto:add_interface(ifn)
135 m:set(net:name(), "type", "bridge")
138 -- in all other cases clear the ifnames
141 for _, ifc in ipairs(net:get_interfaces() or { net:get_interface() }) do
142 net:del_interface(ifc)
144 m:del(net:name(), "type")
149 for k, v in pairs(m:get(net:name())) do
150 if k:sub(1,1) ~= "." and
159 m:set(net:name(), "proto", proto:proto())
160 m.uci:save("network")
161 m.uci:save("wireless")
164 luci.http.redirect(luci.dispatcher.build_url("admin/network/network", arg[1]))
169 -- dhcp setup was requested, create section and reload page
170 if m:formvalue("cbid.dhcp._enable._enable") then
171 m.uci:section("dhcp", "dhcp", arg[1], {
179 luci.http.redirect(luci.dispatcher.build_url("admin/network/network", arg[1]))
183 local ifc = net:get_interface()
185 s = m:section(NamedSection, arg[1], "interface", translate("Common Configuration"))
188 s:tab("general", translate("General Setup"))
189 s:tab("advanced", translate("Advanced Settings"))
190 s:tab("physical", translate("Physical Settings"))
193 s:tab("firewall", translate("Firewall Settings"))
197 st = s:taboption("general", DummyValue, "__status", translate("Status"))
199 local function set_status()
200 -- if current network is empty, print a warning
201 if not net:is_floating() and net:is_empty() then
202 st.template = "cbi/dvalue"
204 st.value = translate("There is no device assigned yet, please attach a network device in the \"Physical Settings\" tab")
206 st.template = "admin_network/iface_status"
212 m.on_init = set_status
213 m.on_after_save = set_status
216 p = s:taboption("general", ListValue, "proto", translate("Protocol"))
217 p.default = net:proto()
220 if not net:is_installed() then
221 p_install = s:taboption("general", Button, "_install")
222 p_install.title = translate("Protocol support is not installed")
223 p_install.inputtitle = translate("Install package %q" % net:opkg_package())
224 p_install.inputstyle = "apply"
225 p_install:depends("proto", net:proto())
227 function p_install.write()
228 return luci.http.redirect(
229 luci.dispatcher.build_url("admin/system/packages") ..
230 "?submit=1&install=%s" % net:opkg_package()
236 p_switch = s:taboption("general", Button, "_switch")
237 p_switch.title = translate("Really switch protocol?")
238 p_switch.inputtitle = translate("Switch protocol")
239 p_switch.inputstyle = "apply"
242 for _, pr in ipairs(nw:get_protocols()) do
243 p:value(pr:proto(), pr:get_i18n())
244 if pr:proto() ~= net:proto() then
245 p_switch:depends("proto", pr:proto())
250 auto = s:taboption("advanced", Flag, "auto", translate("Bring up on boot"))
251 auto.default = (net:proto() == "none") and auto.disabled or auto.enabled
253 delegate = s:taboption("advanced", Flag, "delegate", translate("Use builtin IPv6-management"))
254 delegate.default = delegate.enabled
256 force_link = s:taboption("advanced", Flag, "force_link",
257 translate("Force link"),
258 translate("Set interface properties regardless of the link carrier (If set, carrier sense events do not invoke hotplug handlers)."))
260 force_link.default = (net:proto() == "static") and force_link.enabled or force_link.disabled
263 if not net:is_virtual() then
264 br = s:taboption("physical", Flag, "type", translate("Bridge interfaces"), translate("creates a bridge over specified interface(s)"))
265 br.enabled = "bridge"
267 br:depends("proto", "static")
268 br:depends("proto", "dhcp")
269 br:depends("proto", "none")
271 stp = s:taboption("physical", Flag, "stp", translate("Enable <abbr title=\"Spanning Tree Protocol\">STP</abbr>"),
272 translate("Enables the Spanning Tree Protocol on this bridge"))
273 stp:depends("type", "bridge")
276 igmp = s:taboption("physical", Flag, "igmp_snooping", translate("Enable <abbr title=\"Internet Group Management Protocol\">IGMP</abbr> snooping"),
277 translate("Enables IGMP snooping on this bridge"))
278 igmp:depends("type", "bridge")
283 if not net:is_floating() then
284 ifname_single = s:taboption("physical", Value, "ifname_single", translate("Interface"))
285 ifname_single.template = "cbi/network_ifacelist"
286 ifname_single.widget = "radio"
287 ifname_single.nobridges = true
288 ifname_single.rmempty = false
289 ifname_single.network = arg[1]
290 ifname_single:depends("type", "")
292 function ifname_single.cfgvalue(self, s)
293 -- let the template figure out the related ifaces through the network model
297 function ifname_single.write(self, s, val)
302 for _, i in ipairs(net:get_interfaces() or { net:get_interface() }) do
303 old_ifs[#old_ifs+1] = i:name()
306 for i in ut.imatch(val) do
307 new_ifs[#new_ifs+1] = i
309 -- if this is not a bridge, only assign first interface
310 if self.option == "ifname_single" then
318 for i = 1, math.max(#old_ifs, #new_ifs) do
319 if old_ifs[i] ~= new_ifs[i] then
321 for i = 1, #old_ifs do
322 net:del_interface(old_ifs[i])
324 for i = 1, #new_ifs do
325 net:add_interface(new_ifs[i])
334 if not net:is_virtual() then
335 ifname_multi = s:taboption("physical", Value, "ifname_multi", translate("Interface"))
336 ifname_multi.template = "cbi/network_ifacelist"
337 ifname_multi.nobridges = true
338 ifname_multi.rmempty = false
339 ifname_multi.network = arg[1]
340 ifname_multi.widget = "checkbox"
341 ifname_multi:depends("type", "bridge")
342 ifname_multi.cfgvalue = ifname_single.cfgvalue
343 ifname_multi.write = ifname_single.write
348 fwzone = s:taboption("firewall", Value, "_fwzone",
349 translate("Create / Assign firewall-zone"),
350 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."))
352 fwzone.template = "cbi/firewall_zonelist"
353 fwzone.network = arg[1]
355 function fwzone.cfgvalue(self, section)
357 local z = fw:get_zone_by_network(section)
358 return z and z:name()
361 function fwzone.write(self, section, value)
362 local zone = fw:get_zone(value) or fw:add_zone(value)
364 fw:del_network(section)
365 zone:add_network(section)
369 function fwzone.remove(self, section)
370 fw:del_network(section)
375 function p.write() end
376 function p.remove() end
377 function p.validate(self, value, section)
378 if value == net:proto() then
379 if not net:is_floating() and net:is_empty() then
380 local ifn = ((br and (br:formvalue(section) == "bridge"))
381 and ifname_multi:formvalue(section)
382 or ifname_single:formvalue(section))
384 for ifn in ut.imatch(ifn) do
387 return nil, translate("The selected protocol needs a device assigned")
394 local form, ferr = loadfile(
395 ut.libpath() .. "/model/cbi/admin_network/proto_%s.lua" % net:proto()
399 s:taboption("general", DummyValue, "_error",
400 translate("Missing protocol extension for proto %q" % net:proto())
403 setfenv(form, getfenv(1))(m, s, net)
408 for _, field in ipairs(s.children) do
409 if field ~= st and field ~= p and field ~= p_install and field ~= p_switch then
410 if next(field.deps) then
412 for _, dep in ipairs(field.deps) do
413 dep.proto = net:proto()
416 field:depends("proto", net:proto())
423 -- Display DNS settings if dnsmasq is available
426 if has_dnsmasq and net:proto() == "static" then
427 m2 = Map("dhcp", "", "")
429 local has_section = false
431 m2.uci:foreach("dhcp", "dhcp", function(s)
432 if s.interface == arg[1] then
438 if not has_section and has_dnsmasq then
440 s = m2:section(TypedSection, "dhcp", translate("DHCP Server"))
442 s.cfgsections = function() return { "_enable" } end
444 x = s:option(Button, "_enable")
445 x.title = translate("No DHCP Server configured for this interface")
446 x.inputtitle = translate("Setup DHCP Server")
447 x.inputstyle = "apply"
449 elseif has_section then
451 s = m2:section(TypedSection, "dhcp", translate("DHCP Server"))
454 s:tab("general", translate("General Setup"))
455 s:tab("advanced", translate("Advanced Settings"))
456 s:tab("ipv6", translate("IPv6 Settings"))
458 function s.filter(self, section)
459 return m2.uci:get("dhcp", section, "interface") == arg[1]
462 local ignore = s:taboption("general", Flag, "ignore",
463 translate("Ignore interface"),
464 translate("Disable <abbr title=\"Dynamic Host Configuration Protocol\">DHCP</abbr> for " ..
467 local start = s:taboption("general", Value, "start", translate("Start"),
468 translate("Lowest leased address as offset from the network address."))
469 start.optional = true
470 start.datatype = "or(uinteger,ip4addr)"
471 start.default = "100"
473 local limit = s:taboption("general", Value, "limit", translate("Limit"),
474 translate("Maximum number of leased addresses."))
475 limit.optional = true
476 limit.datatype = "uinteger"
477 limit.default = "150"
479 local ltime = s:taboption("general", Value, "leasetime", translate("Lease time"),
480 translate("Expiry time of leased addresses, minimum is 2 minutes (<code>2m</code>)."))
482 ltime.default = "12h"
484 local dd = s:taboption("advanced", Flag, "dynamicdhcp",
485 translate("Dynamic <abbr title=\"Dynamic Host Configuration Protocol\">DHCP</abbr>"),
486 translate("Dynamically allocate DHCP addresses for clients. If disabled, only " ..
487 "clients having static leases will be served."))
488 dd.default = dd.enabled
490 s:taboption("advanced", Flag, "force", translate("Force"),
491 translate("Force DHCP on this network even if another server is detected."))
493 -- XXX: is this actually useful?
494 --s:taboption("advanced", Value, "name", translate("Name"),
495 -- translate("Define a name for this network."))
497 mask = s:taboption("advanced", Value, "netmask",
498 translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Netmask"),
499 translate("Override the netmask sent to clients. Normally it is calculated " ..
500 "from the subnet that is served."))
503 mask.datatype = "ip4addr"
505 s:taboption("advanced", DynamicList, "dhcp_option", translate("DHCP-Options"),
506 translate("Define additional DHCP options, for example \"<code>6,192.168.2.1," ..
507 "192.168.2.2</code>\" which advertises different DNS servers to clients."))
509 for i, n in ipairs(s.children) do
511 n:depends("ignore", "")
515 o = s:taboption("ipv6", ListValue, "ra", translate("Router Advertisement-Service"))
516 o:value("", translate("disabled"))
517 o:value("server", translate("server mode"))
518 o:value("relay", translate("relay mode"))
519 o:value("hybrid", translate("hybrid mode"))
521 o = s:taboption("ipv6", ListValue, "dhcpv6", translate("DHCPv6-Service"))
522 o:value("", translate("disabled"))
523 o:value("server", translate("server mode"))
524 o:value("relay", translate("relay mode"))
525 o:value("hybrid", translate("hybrid mode"))
527 o = s:taboption("ipv6", ListValue, "ndp", translate("NDP-Proxy"))
528 o:value("", translate("disabled"))
529 o:value("relay", translate("relay mode"))
530 o:value("hybrid", translate("hybrid mode"))
532 o = s:taboption("ipv6", ListValue, "ra_management", translate("DHCPv6-Mode"),
533 translate("Default is stateless + stateful"))
534 o:value("0", translate("stateless"))
535 o:value("1", translate("stateless + stateful"))
536 o:value("2", translate("stateful-only"))
537 o:depends("dhcpv6", "server")
538 o:depends("dhcpv6", "hybrid")
541 o = s:taboption("ipv6", Flag, "ra_default", translate("Always announce default router"),
542 translate("Announce as default router even if no public prefix is available."))
543 o:depends("ra", "server")
544 o:depends("ra", "hybrid")
546 s:taboption("ipv6", DynamicList, "dns", translate("Announced DNS servers"))
547 s:taboption("ipv6", DynamicList, "domain", translate("Announced DNS domains"))