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 = "and(uciname,maxlength(15))"
22 advice = m:field(DummyValue, "d1", translate("Note: interface name length"),
23 translate("Maximum length of the name is 15 characters including " ..
24 "the automatic protocol/bridge prefix (br-, 6in4-, pppoe- etc.)"
27 newproto = m:field(ListValue, "_netproto", translate("Protocol of the new interface"))
29 netbridge = m:field(Flag, "_bridge", translate("Create a bridge over multiple interfaces"))
32 sifname = m:field(Value, "_ifname", translate("Cover the following interface"))
34 sifname.widget = "radio"
35 sifname.template = "cbi/network_ifacelist"
36 sifname.nobridges = true
39 mifname = m:field(Value, "_ifnames", translate("Cover the following interfaces"))
41 mifname.widget = "checkbox"
42 mifname.template = "cbi/network_ifacelist"
43 mifname.nobridges = true
47 for _, p in ipairs(nw:get_protocols()) do
48 if p:is_installed() then
49 newproto:value(p:proto(), p:get_i18n())
50 if not p:is_virtual() then netbridge:depends("_netproto", p:proto()) end
51 if not p:is_floating() then
52 sifname:depends({ _bridge = "", _netproto = p:proto()})
53 mifname:depends({ _bridge = "1", _netproto = p:proto()})
58 function newproto.validate(self, value, section)
59 local name = newnet:formvalue(section)
60 if not name or #name == 0 then
61 newnet:add_error(section, translate("No network name specified"))
62 elseif m:get(name) then
63 newnet:add_error(section, translate("The given network name is not unique"))
66 local proto = nw:get_protocol(value)
67 if proto and not proto:is_floating() then
68 local br = (netbridge:formvalue(section) == "1")
69 local ifn = br and mifname:formvalue(section) or sifname:formvalue(section)
70 for ifn in utl.imatch(ifn) do
73 return nil, translate("The selected protocol needs a device assigned")
78 function newproto.write(self, section, value)
79 local name = newnet:formvalue(section)
80 if name and #name > 0 then
81 local br = (netbridge:formvalue(section) == "1") and "bridge" or nil
82 local net = nw:add_network(name, { proto = value, type = br })
85 for ifn in utl.imatch(
86 br and mifname:formvalue(section) or sifname:formvalue(section)
88 net:add_interface(ifn)
93 luci.http.redirect(luci.dispatcher.build_url("admin/network/network", name))