cb4264886585b52ab1aaf812c77e1ca3527c8f5c
[project/luci.git] / modules / luci-mod-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 fs   = require "nixio.fs"
16 local nw   = require "luci.model.network"
17 local fw   = require "luci.model.firewall"
18 local uci  = require "luci.model.uci".cursor()
19 local http = require "luci.http"
20
21 local iw = luci.sys.wifi.getiwinfo(http.formvalue("device"))
22
23 local has_firewall = fs.access("/etc/config/firewall")
24
25 if not iw then
26         luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
27         return
28 end
29
30 m = SimpleForm("network", translate("Join Network: Settings"))
31 m.cancel = translate("Back to scan results")
32 m.reset = false
33
34 function m.on_cancel()
35         local dev = http.formvalue("device")
36         http.redirect(luci.dispatcher.build_url(
37                 dev and "admin/network/wireless_join?device=" .. dev
38                         or "admin/network/wireless"
39         ))
40 end
41
42 nw.init(uci)
43 fw.init(uci)
44
45 m.hidden = {
46         device      = http.formvalue("device"),
47         join        = http.formvalue("join"),
48         channel     = http.formvalue("channel"),
49         mode        = http.formvalue("mode"),
50         bssid       = http.formvalue("bssid"),
51         wep         = http.formvalue("wep"),
52         wpa_suites      = http.formvalue("wpa_suites"),
53         wpa_version = http.formvalue("wpa_version")
54 }
55
56 if iw and iw.mbssid_support then
57         replace = m:field(Flag, "replace", translate("Replace wireless configuration"),
58                 translate("An additional network will be created if you leave this unchecked."))
59
60         function replace.cfgvalue() return "1" end
61 else
62         replace = m:field(DummyValue, "replace", translate("Replace wireless configuration"))
63         replace.default = translate("The hardware is not multi-SSID capable and the existing " ..
64                 "configuration will be replaced if you proceed.")
65
66         function replace.formvalue() return "1" end
67 end
68
69 if http.formvalue("wep") == "1" then
70         key = m:field(Value, "key", translate("WEP passphrase"),
71                 translate("Specify the secret encryption key here."))
72
73         key.password = true
74         key.datatype = "wepkey"
75
76 elseif (tonumber(m.hidden.wpa_version) or 0) > 0 and
77         (m.hidden.wpa_suites == "PSK" or m.hidden.wpa_suites == "PSK2")
78 then
79         key = m:field(Value, "key", translate("WPA passphrase"),
80                 translate("Specify the secret encryption key here."))
81
82         key.password = true
83         key.datatype = "wpakey"
84         --m.hidden.wpa_suite = (tonumber(http.formvalue("wpa_version")) or 0) >= 2 and "psk2" or "psk"
85 end
86
87 newnet = m:field(Value, "_netname_new", translate("Name of the new network"),
88         translate("The allowed characters are: <code>A-Z</code>, <code>a-z</code>, " ..
89                 "<code>0-9</code> and <code>_</code>"
90         ))
91
92 newnet.default = m.hidden.mode == "Ad-Hoc" and "mesh" or "wwan"
93 newnet.datatype = "uciname"
94
95 if has_firewall then
96         fwzone = m:field(Value, "_fwzone",
97                 translate("Create / Assign firewall-zone"),
98                 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."))
99
100         fwzone.template = "cbi/firewall_zonelist"
101         fwzone.default = m.hidden.mode == "Ad-Hoc" and "mesh" or "wan"
102 end
103
104 function newnet.parse(self, section)
105         local net, zone
106
107         if has_firewall then
108                 local zval  = fwzone:formvalue(section)
109                 zone = fw:get_zone(zval)
110
111                 if not zone and zval == '-' then
112                         zval = m:formvalue(fwzone:cbid(section) .. ".newzone")
113                         if zval and #zval > 0 then
114                                 zone = fw:add_zone(zval)
115                         end
116                 end
117         end
118
119         local wdev = nw:get_wifidev(m.hidden.device)
120
121         wdev:set("disabled", false)
122         wdev:set("channel", m.hidden.channel)
123
124         if replace:formvalue(section) then
125                 local n
126                 for _, n in ipairs(wdev:get_wifinets()) do
127                         wdev:del_wifinet(n)
128                 end
129         end
130
131         local wconf = {
132                 device  = m.hidden.device,
133                 ssid    = m.hidden.join,
134                 mode    = (m.hidden.mode == "Ad-Hoc" and "adhoc" or "sta")
135         }
136
137         if m.hidden.wep == "1" then
138                 wconf.encryption = "wep-open"
139                 wconf.key        = "1"
140                 wconf.key1       = key and key:formvalue(section) or ""
141         elseif (tonumber(m.hidden.wpa_version) or 0) > 0 then
142                 wconf.encryption = (tonumber(m.hidden.wpa_version) or 0) >= 2 and "psk2" or "psk"
143                 wconf.key        = key and key:formvalue(section) or ""
144         else
145                 wconf.encryption = "none"
146         end
147
148         if wconf.mode == "adhoc" or wconf.mode == "sta" then
149                 wconf.bssid = m.hidden.bssid
150         end
151
152         local value = self:formvalue(section)
153         net = nw:add_network(value, { proto = "dhcp" })
154
155         if not net then
156                 self.error = { [section] = "missing" }
157         else
158                 wconf.network = net:name()
159
160                 local wnet = wdev:add_wifinet(wconf)
161                 if wnet then
162                         if zone then
163                                 fw:del_network(net:name())
164                                 zone:add_network(net:name())
165                         end
166
167                         uci:save("wireless")
168                         uci:save("network")
169                         uci:save("firewall")
170
171                         luci.http.redirect(wnet:adminlink())
172                 end
173         end
174 end
175
176 if has_firewall then
177         function fwzone.cfgvalue(self, section)
178                 self.iface = section
179                 local z = fw:get_zone_by_network(section)
180                 return z and z:name()
181         end
182 end
183
184 return m