2 LuCI - Lua Configuration Interface
4 Copyright 2009-2010 Jo-Philipp Wich <xm@subsignal.org>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
16 local nw = require "luci.model.network".init()
17 local fw = require "luci.model.firewall".init()
18 local utl = require "luci.util"
19 local uci = require "luci.model.uci".cursor()
21 m = SimpleForm("network", translate("Create Interface"))
22 m.redirect = luci.dispatcher.build_url("admin/network/network")
25 newnet = m:field(Value, "_netname", translate("Name of the new interface"),
26 translate("The allowed characters are: <code>A-Z</code>, <code>a-z</code>, " ..
27 "<code>0-9</code> and <code>_</code>"
30 newnet:depends("_attach", "")
31 newnet.default = arg[1] and "net_" .. arg[1]:gsub("[^%w_]+", "_")
32 newnet.datatype = "uciname"
34 newproto = m:field(ListValue, "_netproto", translate("Protocol of the new interface"))
36 netbridge = m:field(Flag, "_bridge", translate("Create a bridge over multiple interfaces"))
39 sifname = m:field(Value, "_ifname", translate("Cover the following interface"),
40 translate("Note: If you choose an interface here which is part of another network, it will be moved into this network."))
42 sifname.widget = "radio"
43 sifname.template = "cbi/network_ifacelist"
44 sifname.nobridges = true
47 mifname = m:field(Value, "_ifnames", translate("Cover the following interfaces"),
48 translate("Note: If you choose an interface here which is part of another network, it will be moved into this network."))
50 mifname.widget = "checkbox"
51 mifname.template = "cbi/network_ifacelist"
52 mifname.nobridges = true
56 for _, p in ipairs(nw:get_protocols()) do
57 if p:is_installed() then
58 newproto:value(p:proto(), p:get_i18n())
59 if not p:is_virtual() then netbridge:depends("_netproto", p:proto()) end
60 if not p:is_floating() then
61 sifname:depends({ _bridge = "", _netproto = p:proto()})
62 mifname:depends({ _bridge = "1", _netproto = p:proto()})
67 function newproto.validate(self, value, section)
68 local name = newnet:formvalue(section)
69 if not name or #name == 0 then
70 newnet:add_error(section, translate("No network name specified"))
71 elseif m:get(name) then
72 newnet:add_error(section, translate("The given network name is not unique"))
75 local proto = nw:get_protocol(value)
76 if proto and not proto:is_floating() then
77 local br = (netbridge:formvalue(section) == "1")
78 local ifn = br and mifname:formvalue(section) or sifname:formvalue(section)
79 for ifn in utl.imatch(ifn) do
82 return nil, translate("The selected protocol needs a device assigned")
87 function newproto.write(self, section, value)
88 local name = newnet:formvalue(section)
89 if name and #name > 0 then
90 local br = (netbridge:formvalue(section) == "1") and "bridge" or nil
91 local net = nw:add_network(name, { proto = value, type = br })
94 for ifn in utl.imatch(
95 br and mifname:formvalue(section) or sifname:formvalue(section)
97 net:add_interface(ifn)
102 luci.http.redirect(luci.dispatcher.build_url("admin/network/network", name))