modules/admin-full: convert cbi maps to new network model
[project/luci.git] / modules / admin-full / luasrc / model / cbi / admin_network / wifi_add.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2009 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 local nw   = require "luci.model.network"
16 local fw   = require "luci.model.firewall"
17 local uci  = require "luci.model.uci".cursor()
18 local http = require "luci.http"
19
20 local iw = luci.sys.wifi.getiwinfo(http.formvalue("device"))
21
22 m = SimpleForm("network", translate("Join Network: Settings"))
23 m.cancel = translate("Back to scan results")
24 m.reset = false
25
26 function m.on_cancel()
27         local dev = http.formvalue("device")
28         http.redirect(luci.dispatcher.build_url(
29                 dev and "admin/network/wireless_join?device=" .. dev
30                         or "admin/network/wireless"
31         ))
32 end
33
34 nw.init(uci)
35 fw.init(uci)
36
37 m.hidden = {
38         device      = http.formvalue("device"),
39         join        = http.formvalue("join"),
40         channel     = http.formvalue("channel"),
41         mode        = http.formvalue("mode"),
42         bssid       = http.formvalue("bssid"),
43         wep         = http.formvalue("wep"),
44         wpa_suites      = http.formvalue("wpa_suites"),
45         wpa_version = http.formvalue("wpa_version")
46 }
47
48 if iw and iw.mbssid_support then
49         replace = m:field(Flag, "replace", translate("Replace wireless configuration"),
50                 translate("An additional network will be created if you leave this unchecked."))
51 else
52         replace = m:field(DummyValue, "replace", translate("Replace wireless configuration"))
53         replace.default = translate("The hardware is not multi-SSID capable and existing " ..
54                 "configuration will be replaced if you proceed.")
55
56         function replace.formvalue() return "1" end
57 end
58
59 if http.formvalue("wep") == "1" then
60         key = m:field(Value, "key", translate("WEP passphrase"),
61                 translate("Specify the secret encryption key here."))
62
63         key.password = true
64
65 elseif (tonumber(m.hidden.wpa_version) or 0) > 0 and m.hidden.wpa_suites == "PSK" then
66         key = m:field(Value, "key", translate("WPA passphrase"),
67                 translate("Specify the secret encryption key here."))
68
69         key.password = true
70         --m.hidden.wpa_suite = (tonumber(http.formvalue("wpa_version")) or 0) >= 2 and "psk2" or "psk"
71 end
72
73 attachnet = m:field(Flag, "_attach", translate("Attach to existing network"),
74         translate("If the interface is attached to an existing network it will be <em>bridged</em> " ..
75                 "to the existing interfaces and is covered by the firewall zone of the choosen network. " ..
76                 "Uncheck this option to define a new standalone network."
77         ))
78
79 attachnet.rmempty = false
80 attachnet.default = http.formvalue("cbi.submit") and nil or "1"
81
82 function attachnet.formvalue(self, section)
83         if not http.formvalue("cbi.submit") then
84                 return m.hidden.mode == "Ad-Hoc" and "0" or "1"
85         else
86                 return Value.formvalue(self, section) and "1" or "0"
87         end
88 end
89
90 attachnet.cfgvalue = attachnet.formvalue
91
92 newnet = m:field(Value, "_netname_new", translate("Name of the new network"),
93         translate("The allowed characters are: <code>A-Z</code>, <code>a-z</code>, " ..
94                 "<code>0-9</code> and <code>_</code>"
95         ))
96
97 newnet:depends("_attach", "")
98 newnet.default = m.hidden.mode == "Ad-Hoc" and "mesh"
99
100 addnet = m:field(Value, "_netname_attach",
101         translate("Network to attach interface to"))
102
103 addnet.template = "cbi/network_netlist"
104 addnet.widget = "radio"
105 addnet.default = "wan"
106 addnet.nocreate = true
107 addnet:depends("_attach", "1")
108
109 fwzone = m:field(Value, "_fwzone",
110         translate("Create / Assign firewall-zone"),
111         translate("Choose the firewall zone you want to assign to this interface. Select <em>unspecified</em> to remove the interface from the associated zone or fill out the <em>create</em> field to define a new zone and attach the interface to it."))
112
113 fwzone.template = "cbi/firewall_zonelist"
114 fwzone:depends("_attach", "")
115 fwzone.default = m.hidden.mode == "Ad-Hoc" and "mesh"
116
117 function attachnet.parse(self, section)
118         Flag.parse(self, section)
119
120         if http.formvalue("cbi.submit") then
121                 local net, zone
122                 local value = self:formvalue(section)
123
124                 if value == "1" then
125                         net = nw:get_network(addnet:formvalue(section))
126                         if net then
127                                 net:type("bridge")
128                         end
129                 else
130                         local zval = fwzone:formvalue(section)
131
132                         net = nw:add_network(newnet:formvalue(section), { proto = "dhcp" })
133                         zone = fw:get_zone(zval)
134
135                         if not zone and zval == '-' then
136                                 zval = m:formvalue(fwzone:cbid(section) .. ".newzone")
137                                 if zval and #zval > 0 then
138                                         zone = fw:add_zone(zval)
139                                 end
140                         end
141                 end
142
143                 if not net then
144                         self.error = { [section] = "missing" }
145                 else
146                         local wdev = nw:get_wifidev(m.hidden.device)
147
148                         wdev:set("disabled", false)
149                         wdev:set("channel", m.hidden.channel)
150
151                         if replace:formvalue(section) then
152                                 local n
153                                 for _, n in ipairs(wdev:get_wifinets()) do
154                                         wdev:del_wifinet(n)
155                                 end
156                         end
157
158                         local wconf = {
159                                 device  = m.hidden.device,
160                                 ssid    = m.hidden.join,
161                                 mode    = (m.hidden.mode == "Ad-Hoc" and "adhoc" or "sta"),
162                                 network = net:name()
163                         }
164
165                         if m.hidden.wep == "1" then
166                                 wconf.encryption = "wep"
167                                 wconf.key        = key and key:formvalue(section) or ""
168                         elseif (tonumber(m.hidden.wpa_version) or 0) > 0 then
169                                 wconf.encryption = (tonumber(m.hidden.wpa_version) or 0) >= 2 and "psk2" or "psk"
170                                 wconf.key        = key and key:formvalue(section) or ""
171                         else
172                                 wconf.encryption = "none"
173                         end
174
175                         if wconf.mode == "adhoc" then
176                                 wconf.bssid = m.hidden.bssid
177                         end
178
179                         local wnet = wdev:add_wifinet(wconf)
180                         if wnet then
181                                 if zone then
182                                         fw:del_network(net:name())
183                                         zone:add_network(net:name())
184                                 end
185
186                                 uci:save("wireless")
187                                 uci:save("network")
188                                 uci:save("firewall")
189
190                                 uci:commit("wireless")
191                                 uci:commit("network")
192                                 uci:commit("firewall")
193
194                                 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless",
195                                         wdev:name(), wnet:name()))
196                         end
197                 end
198         end
199 end
200
201 attachnet.remove = attachnet.write
202
203 function fwzone.cfgvalue(self, section)
204         self.iface = section
205         local z = fw:get_zone_by_network(section)
206         return z and z:name()
207 end
208
209 return m