luci-mod-admin-full: Fix dhcpv6 ra_management option stateless value
[project/luci.git] / modules / luci-mod-admin-full / luasrc / model / cbi / admin_network / iface_add.lua
1 -- Copyright 2009-2010 Jo-Philipp Wich <jow@openwrt.org>
2 -- Licensed to the public under the Apache License 2.0.
3
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()
8
9 m = SimpleForm("network", translate("Create Interface"))
10 m.redirect = luci.dispatcher.build_url("admin/network/network")
11 m.reset = false
12
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>"
16         ))
17
18 newnet:depends("_attach", "")
19 newnet.default = arg[1] and "net_" .. arg[1]:gsub("[^%w_]+", "_")
20 newnet.datatype = "and(uciname,maxlength(15))"
21
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.)"
25         ))
26
27 newproto = m:field(ListValue, "_netproto", translate("Protocol of the new interface"))
28
29 netbridge = m:field(Flag, "_bridge", translate("Create a bridge over multiple interfaces"))
30
31
32 sifname = m:field(Value, "_ifname", translate("Cover the following interface"))
33
34 sifname.widget = "radio"
35 sifname.template  = "cbi/network_ifacelist"
36 sifname.nobridges = true
37
38
39 mifname = m:field(Value, "_ifnames", translate("Cover the following interfaces"))
40
41 mifname.widget = "checkbox"
42 mifname.template  = "cbi/network_ifacelist"
43 mifname.nobridges = true
44
45
46 local _, p
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()})
54                 end
55         end
56 end
57
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"))
64         end
65
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
71                         return value
72                 end
73                 return nil, translate("The selected protocol needs a device assigned")
74         end
75         return value
76 end
77
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 })
83                 if net then
84                         local ifn
85                         for ifn in utl.imatch(
86                                 br and mifname:formvalue(section) or sifname:formvalue(section)
87                         ) do
88                                 net:add_interface(ifn)
89                         end
90                         nw:save("network")
91                         nw:save("wireless")
92                 end
93                 luci.http.redirect(luci.dispatcher.build_url("admin/network/network", name))
94         end
95 end
96
97 return m