2 LuCI - Lua Configuration Interface
4 Copyright 2009 Jo-Philipp Wich <xm@subsignal.org>
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
10 http://www.apache.org/licenses/LICENSE-2.0
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"
20 local iw = luci.sys.wifi.getiwinfo(http.formvalue("device"))
22 m = SimpleForm("network", translate("Join Network: Settings"))
23 m.cancel = translate("Back to scan results")
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"
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")
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."))
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.")
56 function replace.formvalue() return "1" end
59 if http.formvalue("wep") == "1" then
60 key = m:field(Value, "key", translate("WEP passphrase"),
61 translate("Specify the secret encryption key here."))
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."))
70 --m.hidden.wpa_suite = (tonumber(http.formvalue("wpa_version")) or 0) >= 2 and "psk2" or "psk"
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."
79 attachnet.rmempty = false
80 attachnet.default = http.formvalue("cbi.submit") and nil or "1"
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"
86 return Value.formvalue(self, section) and "1" or "0"
90 attachnet.cfgvalue = attachnet.formvalue
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>"
97 newnet:depends("_attach", "")
98 newnet.default = m.hidden.mode == "Ad-Hoc" and "mesh"
100 addnet = m:field(Value, "_netname_attach",
101 translate("Network to attach interface to"))
103 addnet.template = "cbi/network_netlist"
104 addnet.widget = "radio"
105 addnet.default = "wan"
106 addnet.nocreate = true
107 addnet:depends("_attach", "1")
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."))
113 fwzone.template = "cbi/firewall_zonelist"
114 fwzone:depends("_attach", "")
115 fwzone.default = m.hidden.mode == "Ad-Hoc" and "mesh"
117 function attachnet.parse(self, section)
118 Flag.parse(self, section)
120 if http.formvalue("cbi.submit") then
122 local value = self:formvalue(section)
125 net = nw:get_network(addnet:formvalue(section))
130 local zval = fwzone:formvalue(section)
132 net = nw:add_network(newnet:formvalue(section), { proto = "dhcp" })
133 zone = fw:get_zone(zval)
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)
144 self.error = { [section] = "missing" }
146 local wdev = nw:get_wifidev(m.hidden.device)
148 wdev:set("disabled", false)
149 wdev:set("channel", m.hidden.channel)
151 if replace:formvalue(section) then
153 for _, n in ipairs(wdev:get_wifinets()) do
159 device = m.hidden.device,
160 ssid = m.hidden.join,
161 mode = (m.hidden.mode == "Ad-Hoc" and "adhoc" or "sta"),
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 ""
172 wconf.encryption = "none"
175 if wconf.mode == "adhoc" then
176 wconf.bssid = m.hidden.bssid
179 local wnet = wdev:add_wifinet(wconf)
182 fw:del_network(net:name())
183 zone:add_network(net:name())
190 uci:commit("wireless")
191 uci:commit("network")
192 uci:commit("firewall")
194 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless",
195 wdev:name(), wnet:name()))
201 attachnet.remove = attachnet.write
203 function fwzone.cfgvalue(self, section)
205 local z = fw:get_zone_by_network(section)
206 return z and z:name()