modules/admin-full: fix wifi join when "wwan" (or other iface) already exists but...
[project/luci.git] / modules / admin-full / luasrc / model / cbi / admin_network / iface_add.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2009-2010 Jo-Philipp Wich <xm@subsignal.org>
5
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
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13
14 ]]--
15
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()
20
21 m = SimpleForm("network", translate("Create Interface"))
22 m.redirect = luci.dispatcher.build_url("admin/network/network")
23 m.reset = false
24
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>"
28         ))
29
30 newnet:depends("_attach", "")
31 newnet.default = arg[1] and "net_" .. arg[1]:gsub("[^%w_]+", "_")
32 newnet.datatype = "uciname"
33
34 newproto = m:field(ListValue, "_netproto", translate("Protocol of the new interface"))
35
36 netbridge = m:field(Flag, "_bridge", translate("Create a bridge over multiple interfaces"))
37
38
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."))
41
42 sifname.widget = "radio"
43 sifname.template  = "cbi/network_ifacelist"
44 sifname.nobridges = true
45
46
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."))
49
50 mifname.widget = "checkbox"
51 mifname.template  = "cbi/network_ifacelist"
52 mifname.nobridges = true
53
54
55 local _, p
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()})
63                 end
64         end
65 end
66
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"))
73         end
74
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
80                         return value
81                 end
82                 return nil, translate("The selected protocol needs a device assigned")
83         end
84         return value
85 end
86
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 })
92                 if net then
93                         local ifn
94                         for ifn in utl.imatch(
95                                 br and mifname:formvalue(section) or sifname:formvalue(section)
96                         ) do
97                                 net:add_interface(ifn)
98                         end
99                         nw:save("network")
100                         nw:save("wireless")
101                 end
102                 luci.http.redirect(luci.dispatcher.build_url("admin/network/network", name))
103         end
104 end
105
106 return m