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 function m.on_cancel()
14 luci.http.redirect(luci.dispatcher.build_url("admin/network/network"))
17 newnet = m:field(Value, "_netname", translate("Name of the new interface"),
18 translate("The allowed characters are: <code>A-Z</code>, <code>a-z</code>, " ..
19 "<code>0-9</code> and <code>_</code>"
22 newnet:depends("_attach", "")
23 newnet.default = arg[1] and "net_" .. arg[1]:gsub("[^%w_]+", "_")
24 newnet.datatype = "and(uciname,maxlength(15))"
26 advice = m:field(DummyValue, "d1", translate("Note: interface name length"),
27 translate("Maximum length of the name is 15 characters including " ..
28 "the automatic protocol/bridge prefix (br-, 6in4-, pppoe- etc.)"
31 newproto = m:field(ListValue, "_netproto", translate("Protocol of the new interface"))
33 netbridge = m:field(Flag, "_bridge", translate("Create a bridge over multiple interfaces"))
36 sifname = m:field(Value, "_ifname", translate("Cover the following interface"))
38 sifname.widget = "radio"
39 sifname.template = "cbi/network_ifacelist"
40 sifname.nobridges = true
43 mifname = m:field(Value, "_ifnames", translate("Cover the following interfaces"))
45 mifname.widget = "checkbox"
46 mifname.template = "cbi/network_ifacelist"
47 mifname.nobridges = true
51 for _, p in ipairs(nw:get_protocols()) do
52 if p:is_installed() then
53 newproto:value(p:proto(), p:get_i18n())
54 if not p:is_virtual() then netbridge:depends("_netproto", p:proto()) end
55 if not p:is_floating() then
56 sifname:depends({ _bridge = "", _netproto = p:proto()})
57 mifname:depends({ _bridge = "1", _netproto = p:proto()})
62 function newproto.validate(self, value, section)
63 local name = newnet:formvalue(section)
64 if not name or #name == 0 then
65 newnet:add_error(section, translate("No network name specified"))
66 elseif m:get(name) then
67 newnet:add_error(section, translate("The given network name is not unique"))
70 local proto = nw:get_protocol(value)
71 if proto and not proto:is_floating() then
72 local br = (netbridge:formvalue(section) == "1")
73 local ifn = br and mifname:formvalue(section) or sifname:formvalue(section)
74 for ifn in utl.imatch(ifn) do
77 return nil, translate("The selected protocol needs a device assigned")
82 function newproto.write(self, section, value)
83 local name = newnet:formvalue(section)
84 if name and #name > 0 then
85 local br = (netbridge:formvalue(section) == "1") and "bridge" or nil
86 local net = nw:add_network(name, { proto = value, type = br })
89 for ifn in utl.imatch(
90 br and mifname:formvalue(section) or sifname:formvalue(section)
92 net:add_interface(ifn)
97 luci.http.redirect(luci.dispatcher.build_url("admin/network/network", name))