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 wl = require "luci.model.wireless"
18 local uci = require "luci.model.uci".cursor()
19 local http = require "luci.http"
21 local iw = luci.sys.wifi.getiwinfo(http.formvalue("device"))
23 m = SimpleForm("network", translate("Join Network: Settings"))
24 m.cancel = translate("Back to scan results")
27 function m.on_cancel()
28 local dev = http.formvalue("device")
29 http.redirect(luci.dispatcher.build_url(
30 dev and "admin/network/wireless_join?device=" .. dev
31 or "admin/network/wireless"
40 device = http.formvalue("device"),
41 join = http.formvalue("join"),
42 channel = http.formvalue("channel"),
43 mode = http.formvalue("mode"),
44 bssid = http.formvalue("bssid"),
45 wep = http.formvalue("wep"),
46 wpa_suites = http.formvalue("wpa_suites"),
47 wpa_version = http.formvalue("wpa_version")
50 if iw and iw.mbssid_support then
51 replace = m:field(Flag, "replace", translate("Replace wireless configuration"),
52 translate("An additional network will be created if you leave this unchecked."))
54 replace = m:field(DummyValue, "replace", translate("Replace wireless configuration"))
55 replace.default = translate("The hardware is not multi-SSID capable and existing " ..
56 "configuration will be replaced if you proceed.")
58 function replace.formvalue() return "1" end
61 if http.formvalue("wep") == "1" then
62 key = m:field(Value, "key", translate("WEP passphrase"),
63 translate("Specify the secret encryption key here."))
67 elseif (tonumber(m.hidden.wpa_version) or 0) > 0 and m.hidden.wpa_suites == "PSK" then
68 key = m:field(Value, "key", translate("WPA passphrase"),
69 translate("Specify the secret encryption key here."))
72 --m.hidden.wpa_suite = (tonumber(http.formvalue("wpa_version")) or 0) >= 2 and "psk2" or "psk"
75 attachnet = m:field(Flag, "_attach", translate("Attach to existing network"),
76 translate("If the interface is attached to an existing network it will be <em>bridged</em> " ..
77 "to the existing interfaces and is covered by the firewall zone of the choosen network. " ..
78 "Uncheck this option to define a new standalone network."
81 attachnet.rmempty = false
82 attachnet.default = http.formvalue("cbi.submit") and nil or "1"
84 function attachnet.formvalue(self, section)
85 if not http.formvalue("cbi.submit") then
86 return m.hidden.mode == "Ad-Hoc" and "0" or "1"
88 return Value.formvalue(self, section) and "1" or "0"
92 attachnet.cfgvalue = attachnet.formvalue
94 newnet = m:field(Value, "_netname_new", translate("Name of the new network"),
95 translate("The allowed characters are: <code>A-Z</code>, <code>a-z</code>, " ..
96 "<code>0-9</code> and <code>_</code>"
99 newnet:depends("_attach", "")
100 newnet.default = m.hidden.mode == "Ad-Hoc" and "mesh"
102 addnet = m:field(Value, "_netname_attach",
103 translate("Network to attach interface to"))
105 addnet.template = "cbi/network_netlist"
106 addnet.widget = "radio"
107 addnet.default = "wan"
108 addnet.nocreate = true
109 addnet:depends("_attach", "1")
111 fwzone = m:field(Value, "_fwzone",
112 translate("Create / Assign firewall-zone"),
113 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."))
115 fwzone.template = "cbi/firewall_zonelist"
116 fwzone:depends("_attach", "")
117 fwzone.default = m.hidden.mode == "Ad-Hoc" and "mesh"
119 function attachnet.parse(self, section)
120 Flag.parse(self, section)
122 if http.formvalue("cbi.submit") then
124 local value = self:formvalue(section)
127 net = nw:get_network(addnet:formvalue(section))
132 local zval = fwzone:formvalue(section)
134 net = nw:add_network(newnet:formvalue(section), { proto = "dhcp" })
135 zone = fw:get_zone(zval)
137 if not zone and zval == '-' then
138 zval = m:formvalue(fwzone:cbid(section) .. ".newzone")
139 if zval and #zval > 0 then
140 zone = fw:add_zone(zval)
146 self.error = { [section] = "missing" }
148 local wdev = wl:get_device(m.hidden.device)
150 wdev:channel(m.hidden.channel)
152 if replace:formvalue(section) then
154 for _, n in ipairs(wdev:get_networks()) do
155 wl:del_network(n:name())
160 device = m.hidden.device,
161 ssid = m.hidden.join,
162 mode = (m.hidden.mode == "Ad-Hoc" and "adhoc" or "sta"),
166 if m.hidden.wep == "1" then
167 wconf.encryption = "wep"
168 wconf.key = key and key:formvalue(section) or ""
169 elseif (tonumber(m.hidden.wpa_version) or 0) > 0 then
170 wconf.encryption = (tonumber(m.hidden.wpa_version) or 0) >= 2 and "psk2" or "psk"
171 wconf.key = key and key:formvalue(section) or ""
173 wconf.encryption = "none"
176 if wconf.mode == "adhoc" then
177 wconf.bssid = m.hidden.bssid
180 local wnet = wl:add_network(wconf)
184 fw:del_network(net:name())
185 zone:add_network(net:name())
192 uci:commit("wireless")
193 uci:commit("network")
194 uci:commit("firewall")
196 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless",
197 wdev:name(), wnet:name()))
203 attachnet.remove = attachnet.write
205 function fwzone.cfgvalue(self, section)
207 local z = fw:get_zone_by_network(section)
208 return z and z:name()