modules/admin-full: add menupoints for wifi networks
[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 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 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         local value = self:formvalue(section)
108         net = nw:add_network(value, { proto = "dhcp" })
109
110         if has_firewall then
111                 local zval  = fwzone:formvalue(section)
112                 zone = fw:get_zone(zval)
113
114                 if not zone and zval == '-' then
115                         zval = m:formvalue(fwzone:cbid(section) .. ".newzone")
116                         if zval and #zval > 0 then
117                                 zone = fw:add_zone(zval)
118                         end
119                 end
120         end
121
122         if not net then
123                 self.error = { [section] = "missing" }
124         else
125                 local wdev = nw:get_wifidev(m.hidden.device)
126
127                 wdev:set("disabled", false)
128                 wdev:set("channel", m.hidden.channel)
129
130                 if replace:formvalue(section) then
131                         local n
132                         for _, n in ipairs(wdev:get_wifinets()) do
133                                 wdev:del_wifinet(n)
134                         end
135                 end
136
137                 local wconf = {
138                         device  = m.hidden.device,
139                         ssid    = m.hidden.join,
140                         mode    = (m.hidden.mode == "Ad-Hoc" and "adhoc" or "sta"),
141                         network = net:name()
142                 }
143
144                 if m.hidden.wep == "1" then
145                         wconf.encryption = "wep"
146                         wconf.key        = key and key:formvalue(section) or ""
147                 elseif (tonumber(m.hidden.wpa_version) or 0) > 0 then
148                         wconf.encryption = (tonumber(m.hidden.wpa_version) or 0) >= 2 and "psk2" or "psk"
149                         wconf.key        = key and key:formvalue(section) or ""
150                 else
151                         wconf.encryption = "none"
152                 end
153
154                 if wconf.mode == "adhoc" then
155                         wconf.bssid = m.hidden.bssid
156                 end
157
158                 local wnet = wdev:add_wifinet(wconf)
159                 if wnet then
160                         if zone then
161                                 fw:del_network(net:name())
162                                 zone:add_network(net:name())
163                         end
164
165                         uci:save("wireless")
166                         uci:save("network")
167                         uci:save("firewall")
168
169                         luci.http.redirect(luci.dispatcher.build_url(
170                                 "admin/network/wireless", wdev:name(), wnet:ifname()
171                         ))
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