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 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"
21 local iw = luci.sys.wifi.getiwinfo(http.formvalue("device"))
23 local has_firewall = fs.access("/etc/config/firewall")
26 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
30 m = SimpleForm("network", translate("Join Network: Settings"))
31 m.cancel = translate("Back to scan results")
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"
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")
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."))
60 function replace.cfgvalue() return "1" end
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.")
66 function replace.formvalue() return "1" end
69 if http.formvalue("wep") == "1" then
70 key = m:field(Value, "key", translate("WEP passphrase"),
71 translate("Specify the secret encryption key here."))
74 key.datatype = "wepkey"
76 elseif (tonumber(m.hidden.wpa_version) or 0) > 0 and
77 (m.hidden.wpa_suites == "PSK" or m.hidden.wpa_suites == "PSK2")
79 key = m:field(Value, "key", translate("WPA passphrase"),
80 translate("Specify the secret encryption key here."))
83 key.datatype = "wpakey"
84 --m.hidden.wpa_suite = (tonumber(http.formvalue("wpa_version")) or 0) >= 2 and "psk2" or "psk"
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>"
92 newnet.default = m.hidden.mode == "Ad-Hoc" and "mesh" or "wwan"
93 newnet.datatype = "uciname"
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."))
100 fwzone.template = "cbi/firewall_zonelist"
101 fwzone.default = m.hidden.mode == "Ad-Hoc" and "mesh" or "wan"
104 function newnet.parse(self, section)
108 local zval = fwzone:formvalue(section)
109 zone = fw:get_zone(zval)
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)
119 local wdev = nw:get_wifidev(m.hidden.device)
121 wdev:set("disabled", false)
122 wdev:set("channel", m.hidden.channel)
124 if replace:formvalue(section) then
126 for _, n in ipairs(wdev:get_wifinets()) do
132 device = m.hidden.device,
133 ssid = m.hidden.join,
134 mode = (m.hidden.mode == "Ad-Hoc" and "adhoc" or "sta")
137 if m.hidden.wep == "1" then
138 wconf.encryption = "wep-open"
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 ""
145 wconf.encryption = "none"
148 if wconf.mode == "adhoc" then
149 wconf.bssid = m.hidden.bssid
152 local value = self:formvalue(section)
153 net = nw:add_network(value, { proto = "dhcp" })
156 self.error = { [section] = "missing" }
158 wconf.network = net:name()
160 local wnet = wdev:add_wifinet(wconf)
163 fw:del_network(net:name())
164 zone:add_network(net:name())
171 luci.http.redirect(wnet:adminlink())
177 function fwzone.cfgvalue(self, section)
179 local z = fw:get_zone_by_network(section)
180 return z and z:name()