1 -- Copyright 2009-2010 Jo-Philipp Wich <jow@openwrt.org>
2 -- Licensed to the public under the Apache License 2.0.
4 local nw = require "luci.model.network".init()
5 local fw = require "luci.model.firewall".init()
6 local utl = require "luci.util"
7 local uci = require "luci.model.uci".cursor()
9 m = SimpleForm("network", translate("Create Interface"))
10 m.redirect = luci.dispatcher.build_url("admin/network/network")
13 newnet = m:field(Value, "_netname", translate("Name of the new interface"),
14 translate("The allowed characters are: <code>A-Z</code>, <code>a-z</code>, " ..
15 "<code>0-9</code> and <code>_</code>"
18 newnet:depends("_attach", "")
19 newnet.default = arg[1] and "net_" .. arg[1]:gsub("[^%w_]+", "_")
20 newnet.datatype = "uciname"
22 newproto = m:field(ListValue, "_netproto", translate("Protocol of the new interface"))
24 netbridge = m:field(Flag, "_bridge", translate("Create a bridge over multiple interfaces"))
27 sifname = m:field(Value, "_ifname", translate("Cover the following interface"))
29 sifname.widget = "radio"
30 sifname.template = "cbi/network_ifacelist"
31 sifname.nobridges = true
34 mifname = m:field(Value, "_ifnames", translate("Cover the following interfaces"))
36 mifname.widget = "checkbox"
37 mifname.template = "cbi/network_ifacelist"
38 mifname.nobridges = true
42 for _, p in ipairs(nw:get_protocols()) do
43 if p:is_installed() then
44 newproto:value(p:proto(), p:get_i18n())
45 if not p:is_virtual() then netbridge:depends("_netproto", p:proto()) end
46 if not p:is_floating() then
47 sifname:depends({ _bridge = "", _netproto = p:proto()})
48 mifname:depends({ _bridge = "1", _netproto = p:proto()})
53 function newproto.validate(self, value, section)
54 local name = newnet:formvalue(section)
55 if not name or #name == 0 then
56 newnet:add_error(section, translate("No network name specified"))
57 elseif m:get(name) then
58 newnet:add_error(section, translate("The given network name is not unique"))
61 local proto = nw:get_protocol(value)
62 if proto and not proto:is_floating() then
63 local br = (netbridge:formvalue(section) == "1")
64 local ifn = br and mifname:formvalue(section) or sifname:formvalue(section)
65 for ifn in utl.imatch(ifn) do
68 return nil, translate("The selected protocol needs a device assigned")
73 function newproto.write(self, section, value)
74 local name = newnet:formvalue(section)
75 if name and #name > 0 then
76 local br = (netbridge:formvalue(section) == "1") and "bridge" or nil
77 local net = nw:add_network(name, { proto = value, type = br })
80 for ifn in utl.imatch(
81 br and mifname:formvalue(section) or sifname:formvalue(section)
83 net:add_interface(ifn)
88 luci.http.redirect(luci.dispatcher.build_url("admin/network/network", name))