1 -- Copyright 2009 Jo-Philipp Wich <jow@openwrt.org>
2 -- Licensed to the public under the Apache License 2.0.
4 local fs = require "nixio.fs"
5 local nw = require "luci.model.network"
6 local fw = require "luci.model.firewall"
7 local uci = require "luci.model.uci".cursor()
8 local http = require "luci.http"
10 local iw = luci.sys.wifi.getiwinfo(http.formvalue("device"))
12 local has_firewall = fs.access("/etc/config/firewall")
15 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
19 m = SimpleForm("network", translatef("Joining Network: %q", http.formvalue("join")))
20 m.cancel = translate("Back to scan results")
23 function m.on_cancel()
24 local dev = http.formvalue("device")
25 http.redirect(luci.dispatcher.build_url(
26 dev and "admin/network/wireless_join?device=" .. dev
27 or "admin/network/wireless"
35 device = http.formvalue("device"),
36 join = http.formvalue("join"),
37 channel = http.formvalue("channel"),
38 mode = http.formvalue("mode"),
39 bssid = http.formvalue("bssid"),
40 wep = http.formvalue("wep"),
41 wpa_suites = http.formvalue("wpa_suites"),
42 wpa_version = http.formvalue("wpa_version")
45 if iw and iw.mbssid_support then
46 replace = m:field(Flag, "replace", translate("Replace wireless configuration"),
47 translate("Check this option to delete the existing networks from this radio."))
49 function replace.cfgvalue() return "0" end
51 replace = m:field(DummyValue, "replace", translate("Replace wireless configuration"))
52 replace.default = translate("The hardware is not multi-SSID capable and the existing " ..
53 "configuration will be replaced if you proceed.")
55 function replace.formvalue() return "1" end
58 if http.formvalue("wep") == "1" then
59 key = m:field(Value, "key", translate("WEP passphrase"),
60 translate("Specify the secret encryption key here."))
63 key.datatype = "wepkey"
65 elseif (tonumber(m.hidden.wpa_version) or 0) > 0 and
66 (m.hidden.wpa_suites == "PSK" or m.hidden.wpa_suites == "PSK2")
68 key = m:field(Value, "key", translate("WPA passphrase"),
69 translate("Specify the secret encryption key here."))
72 key.datatype = "wpakey"
73 --m.hidden.wpa_suite = (tonumber(http.formvalue("wpa_version")) or 0) >= 2 and "psk2" or "psk"
76 newnet = m:field(Value, "_netname_new", translate("Name of the new network"),
77 translate("The allowed characters are: <code>A-Z</code>, <code>a-z</code>, " ..
78 "<code>0-9</code> and <code>_</code>"
81 newnet.default = m.hidden.mode == "Ad-Hoc" and "mesh" or "wwan"
82 newnet.datatype = "uciname"
85 fwzone = m:field(Value, "_fwzone",
86 translate("Create / Assign firewall-zone"),
87 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."))
89 fwzone.template = "cbi/firewall_zonelist"
90 fwzone.default = m.hidden.mode == "Ad-Hoc" and "mesh" or "wan"
93 function newnet.parse(self, section)
97 local value = fwzone:formvalue(section)
98 if value and #value > 0 then
99 zone = fw:get_zone(value) or fw:add_zone(value)
103 local wdev = nw:get_wifidev(m.hidden.device)
105 wdev:set("disabled", false)
106 wdev:set("channel", m.hidden.channel)
108 if replace:formvalue(section) then
110 for _, n in ipairs(wdev:get_wifinets()) do
116 device = m.hidden.device,
117 ssid = m.hidden.join,
118 mode = (m.hidden.mode == "Ad-Hoc" and "adhoc" or "sta")
121 if m.hidden.wep == "1" then
122 wconf.encryption = "wep-open"
124 wconf.key1 = key and key:formvalue(section) or ""
125 elseif (tonumber(m.hidden.wpa_version) or 0) > 0 then
126 wconf.encryption = (tonumber(m.hidden.wpa_version) or 0) >= 2 and "psk2" or "psk"
127 wconf.key = key and key:formvalue(section) or ""
129 wconf.encryption = "none"
132 if wconf.mode == "adhoc" or wconf.mode == "sta" then
133 wconf.bssid = m.hidden.bssid
136 local value = self:formvalue(section)
137 net = nw:add_network(value, { proto = "dhcp" })
140 self.error = { [section] = "missing" }
142 wconf.network = net:name()
144 local wnet = wdev:add_wifinet(wconf)
147 fw:del_network(net:name())
148 zone:add_network(net:name())
155 luci.http.redirect(wnet:adminlink())
161 function fwzone.cfgvalue(self, section)
163 local z = fw:get_zone_by_network(section)
164 return z and z:name()