+local fs = require "nixio.fs"
+local ut = require "luci.util"
+local pt = require "luci.tools.proto"
+local nw = require "luci.model.network"
+local fw = require "luci.model.firewall"
+
+arg[1] = arg[1] or ""
+
+local has_dnsmasq = fs.access("/etc/config/dhcp")
+local has_firewall = fs.access("/etc/config/firewall")
+
+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>)."))
+m:chain("wireless")
+
+if has_firewall then
+ m:chain("firewall")
+end
+
+nw.init(m.uci)
+fw.init(m.uci)
+
+
+local net = nw:get_network(arg[1])
+
+local function backup_ifnames(is_bridge)
+ if not net:is_floating() and not m:get(net:name(), "_orig_ifname") then
+ local ifcs = net:get_interfaces() or { net:get_interface() }
+ if ifcs then
+ local _, ifn
+ local ifns = { }
+ for _, ifn in ipairs(ifcs) do
+ ifns[#ifns+1] = ifn:name()
+ end
+ if #ifns > 0 then
+ m:set(net:name(), "_orig_ifname", table.concat(ifns, " "))
+ m:set(net:name(), "_orig_bridge", tostring(net:is_bridge()))
+ end
+ end
+ end
+end
+
+
+-- redirect to overview page if network does not exist anymore (e.g. after a revert)
+if not net then
+ luci.http.redirect(luci.dispatcher.build_url("admin/network/network"))
+ return
+end
+
+-- protocol switch was requested, rebuild interface config and reload page
+if m:formvalue("cbid.network.%s._switch" % net:name()) then
+ -- get new protocol
+ local ptype = m:formvalue("cbid.network.%s.proto" % net:name()) or "-"
+ local proto = nw:get_protocol(ptype, net:name())
+ if proto then
+ -- backup default
+ backup_ifnames()
+
+ -- if current proto is not floating and target proto is not floating,
+ -- then attempt to retain the ifnames
+ --error(net:proto() .. " > " .. proto:proto())
+ if not net:is_floating() and not proto:is_floating() then
+ -- if old proto is a bridge and new proto not, then clip the
+ -- interface list to the first ifname only
+ if net:is_bridge() and proto:is_virtual() then
+ local _, ifn
+ local first = true
+ for _, ifn in ipairs(net:get_interfaces() or { net:get_interface() }) do
+ if first then
+ first = false
+ else
+ net:del_interface(ifn)
+ end
+ end
+ m:del(net:name(), "type")
+ end
+
+ -- if the current proto is floating, the target proto not floating,
+ -- then attempt to restore ifnames from backup
+ elseif net:is_floating() and not proto:is_floating() then
+ -- if we have backup data, then re-add all orphaned interfaces
+ -- from it and restore the bridge choice
+ local br = (m:get(net:name(), "_orig_bridge") == "true")
+ local ifn
+ local ifns = { }
+ for ifn in ut.imatch(m:get(net:name(), "_orig_ifname")) do
+ ifn = nw:get_interface(ifn)
+ if ifn and not ifn:get_network() then
+ proto:add_interface(ifn)
+ if not br then
+ break
+ end
+ end
+ end
+ if br then
+ m:set(net:name(), "type", "bridge")
+ end
+
+ -- in all other cases clear the ifnames
+ else
+ local _, ifc
+ for _, ifc in ipairs(net:get_interfaces() or { net:get_interface() }) do
+ net:del_interface(ifc)
+ end
+ m:del(net:name(), "type")
+ end
+
+ -- clear options
+ local k, v
+ for k, v in pairs(m:get(net:name())) do
+ if k:sub(1,1) ~= "." and
+ k ~= "type" and
+ k ~= "ifname" and
+ k ~= "_orig_ifname" and
+ k ~= "_orig_bridge"
+ then
+ m:del(net:name(), k)
+ end
+ end
+
+ -- set proto
+ m:set(net:name(), "proto", proto:proto())
+ m.uci:save("network")
+ m.uci:save("wireless")
+
+ -- reload page
+ luci.http.redirect(luci.dispatcher.build_url("admin/network/network", arg[1]))
+ return
+ end
+end
+
+-- dhcp setup was requested, create section and reload page
+if m:formvalue("cbid.dhcp._enable._enable") then
+ m.uci:section("dhcp", "dhcp", nil, {
+ interface = arg[1],
+ start = "100",
+ limit = "150",
+ leasetime = "12h"
+ })
+
+ m.uci:save("dhcp")
+ luci.http.redirect(luci.dispatcher.build_url("admin/network/network", arg[1]))
+ return
+end
+
+local ifc = net:get_interface()
+
+s = m:section(NamedSection, arg[1], "interface", translate("Common Configuration"))
+s.addremove = false
+
+s:tab("general", translate("General Setup"))
+s:tab("advanced", translate("Advanced Settings"))
+s:tab("physical", translate("Physical Settings"))
+
+if has_firewall then
+ s:tab("firewall", translate("Firewall Settings"))
+end
+
+
+st = s:taboption("general", DummyValue, "__status", translate("Status"))
+
+local function set_status()
+ -- if current network is empty, print a warning
+ if not net:is_floating() and net:is_empty() then
+ st.template = "cbi/dvalue"
+ st.network = nil
+ st.value = translate("There is no device assigned yet, please attach a network device in the \"Physical Settings\" tab")
+ else
+ st.template = "admin_network/iface_status"
+ st.network = arg[1]
+ st.value = nil
+ end
+end
+
+m.on_init = set_status
+m.on_after_save = set_status
+
+
+p = s:taboption("general", ListValue, "proto", translate("Protocol"))
+p.default = net:proto()
+
+
+if not net:is_installed() then
+ p_install = s:taboption("general", Button, "_install")
+ p_install.title = translate("Protocol support is not installed")
+ p_install.inputtitle = translate("Install package %q" % net:opkg_package())
+ p_install.inputstyle = "apply"
+ p_install:depends("proto", net:proto())
+
+ function p_install.write()
+ return luci.http.redirect(
+ luci.dispatcher.build_url("admin/system/packages") ..
+ "?submit=1&install=%s" % net:opkg_package()
+ )
+ end
+end
+
+
+p_switch = s:taboption("general", Button, "_switch")
+p_switch.title = translate("Really switch protocol?")
+p_switch.inputtitle = translate("Switch protocol")
+p_switch.inputstyle = "apply"
+
+local _, pr
+for _, pr in ipairs(nw:get_protocols()) do
+ p:value(pr:proto(), pr:get_i18n())
+ if pr:proto() ~= net:proto() then
+ p_switch:depends("proto", pr:proto())
+ end
+end
+
+
+auto = s:taboption("advanced", Flag, "auto", translate("Bring up on boot"))
+auto.default = (net:proto() == "none") and auto.disabled or auto.enabled
+
+delegate = s:taboption("advanced", Flag, "delegate", translate("Use builtin IPv6-management"))
+delegate.default = delegate.enabled
+
+
+if not net:is_virtual() then
+ br = s:taboption("physical", Flag, "type", translate("Bridge interfaces"), translate("creates a bridge over specified interface(s)"))
+ br.enabled = "bridge"
+ br.rmempty = true
+ br:depends("proto", "static")
+ br:depends("proto", "dhcp")
+ br:depends("proto", "none")
+
+ stp = s:taboption("physical", Flag, "stp", translate("Enable <abbr title=\"Spanning Tree Protocol\">STP</abbr>"),
+ translate("Enables the Spanning Tree Protocol on this bridge"))
+ stp:depends("type", "bridge")
+ stp.rmempty = true