Merge pull request #1691 from dibdot/travelmate
[project/luci.git] / modules / luci-mod-admin-full / luasrc / model / cbi / admin_network / wifi_add.lua
1 -- Copyright 2009 Jo-Philipp Wich <jow@openwrt.org>
2 -- Licensed to the public under the Apache License 2.0.
3
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"
9
10 local iw = luci.sys.wifi.getiwinfo(http.formvalue("device"))
11
12 local has_firewall = fs.access("/etc/config/firewall")
13
14 if not iw then
15 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
16 return
17 end
18
19 m = SimpleForm("network", translatef("Joining Network: %q", http.formvalue("join")))
20 m.cancel = translate("Back to scan results")
21 m.reset = false
22
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"
28 ))
29 end
30
31 nw.init(uci)
32 fw.init(uci)
33
34 m.hidden = {
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")
43 }
44
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."))
48
49 function replace.cfgvalue() return "0" end
50 else
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.")
54
55 function replace.formvalue() return "1" end
56 end
57
58 if http.formvalue("wep") == "1" then
59 key = m:field(Value, "key", translate("WEP passphrase"),
60 translate("Specify the secret encryption key here."))
61
62 key.password = true
63 key.datatype = "wepkey"
64
65 elseif (tonumber(m.hidden.wpa_version) or 0) > 0 and
66 (m.hidden.wpa_suites == "PSK" or m.hidden.wpa_suites == "PSK2")
67 then
68 key = m:field(Value, "key", translate("WPA passphrase"),
69 translate("Specify the secret encryption key here."))
70
71 key.password = true
72 key.datatype = "wpakey"
73 --m.hidden.wpa_suite = (tonumber(http.formvalue("wpa_version")) or 0) >= 2 and "psk2" or "psk"
74 end
75
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>"
79 ))
80
81 newnet.default = m.hidden.mode == "Ad-Hoc" and "mesh" or "wwan"
82 newnet.datatype = "uciname"
83
84 if has_firewall then
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."))
88
89 fwzone.template = "cbi/firewall_zonelist"
90 fwzone.default = m.hidden.mode == "Ad-Hoc" and "mesh" or "wan"
91 end
92
93 function newnet.parse(self, section)
94 local net, zone
95
96 if has_firewall then
97 local zval = fwzone:formvalue(section)
98 zone = fw:get_zone(zval)
99
100 if not zone and zval == '-' then
101 zval = m:formvalue(fwzone:cbid(section) .. ".newzone")
102 if zval and #zval > 0 then
103 zone = fw:add_zone(zval)
104 end
105 end
106 end
107
108 local wdev = nw:get_wifidev(m.hidden.device)
109
110 wdev:set("disabled", false)
111 wdev:set("channel", m.hidden.channel)
112
113 if replace:formvalue(section) then
114 local n
115 for _, n in ipairs(wdev:get_wifinets()) do
116 wdev:del_wifinet(n)
117 end
118 end
119
120 local wconf = {
121 device = m.hidden.device,
122 ssid = m.hidden.join,
123 mode = (m.hidden.mode == "Ad-Hoc" and "adhoc" or "sta")
124 }
125
126 if m.hidden.wep == "1" then
127 wconf.encryption = "wep-open"
128 wconf.key = "1"
129 wconf.key1 = key and key:formvalue(section) or ""
130 elseif (tonumber(m.hidden.wpa_version) or 0) > 0 then
131 wconf.encryption = (tonumber(m.hidden.wpa_version) or 0) >= 2 and "psk2" or "psk"
132 wconf.key = key and key:formvalue(section) or ""
133 else
134 wconf.encryption = "none"
135 end
136
137 if wconf.mode == "adhoc" or wconf.mode == "sta" then
138 wconf.bssid = m.hidden.bssid
139 end
140
141 local value = self:formvalue(section)
142 net = nw:add_network(value, { proto = "dhcp" })
143
144 if not net then
145 self.error = { [section] = "missing" }
146 else
147 wconf.network = net:name()
148
149 local wnet = wdev:add_wifinet(wconf)
150 if wnet then
151 if zone then
152 fw:del_network(net:name())
153 zone:add_network(net:name())
154 end
155
156 uci:save("wireless")
157 uci:save("network")
158 uci:save("firewall")
159
160 luci.http.redirect(wnet:adminlink())
161 end
162 end
163 end
164
165 if has_firewall then
166 function fwzone.cfgvalue(self, section)
167 self.iface = section
168 local z = fw:get_zone_by_network(section)
169 return z and z:name()
170 end
171 end
172
173 return m