40d2486bc43b29ff31a1fc27ac23eb63905e34b1
[project/luci.git] / modules / niu / luasrc / model / cbi / niu / network / wlanwan.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2009 Steven Barth <steven@midlink.org>
5 Copyright 2009 Jo-Philipp Wich <xm@subsignal.org>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11         http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14 ]]--
15
16 local fs = require "nixio.fs"
17 local uci = require "luci.model.uci"
18 local nixio = require "nixio"
19 local iwinfo = require "iwinfo"
20
21 local bridge
22 local iface = "client"
23 local net = "wan"
24 if arg[1] == "bridge" then
25         bridge = true
26         iface = "bridge"
27         net = "lan"
28 end
29
30 local cursor = uci.inst
31 local state = uci.inst_state
32 cursor:unload("wireless")
33 state:unload("wireless")
34
35 local has_ipv6 = fs.access("/proc/net/ipv6_route")
36 local device = cursor:get("wireless", iface, "device")
37 local hwtype = cursor:get("wireless", device, "type")
38
39
40 -- Bring up interface and scan --
41
42 if not state:get("wireless", iface, "network") then
43         local olduci = uci.cursor(nil, "")
44         local oldcl = olduci:get_all("wireless", iface)
45         olduci:unload("wireless")
46         
47         local newuci = uci.cursor()
48         local newcl = newuci:get_all("wireless", iface)
49         newcl.network = net
50         
51         local proc = nixio.fork()
52         if proc == 0 then
53                 newuci:delete("wireless", iface, "ssid")
54                 newuci:commit("wireless")
55                 nixio.exec("/sbin/wifi", "up", device)
56                 os.exit(1) 
57         end
58         nixio.wait(proc)
59         
60         newuci:delete("wireless", iface)
61         newuci:section("wireless", "wifi-iface", iface, oldcl)
62         newuci:commit("wireless")
63         newuci:tset("wireless", iface, newcl)
64         newuci:save("wireless")
65         newuci:unload("wireless")
66
67         state:unload("wireless")
68 end
69  
70 local ifname = state:get("wireless", iface, "ifname") or "wlan0dummy"
71 local iwlib = iwinfo.type(ifname) and iwinfo[iwinfo.type(ifname)]
72 local suggest = {}
73 local encrdep = {
74         none = {{["!default"] = 1}},
75         wep = {{["!default"] = 1}},
76         psk = {{["!default"] = 1}},
77         psk2 = {{["!default"] = 1}},
78         wpa = {{["!default"] = 1}},
79         wpa2 = {{["!default"] = 1}}
80 }
81
82 if iwlib then
83         suggest = iwlib.scanlist(ifname)
84 end
85
86
87
88 -- Form definition --
89
90 m2 = Map("wireless", translate("Configure WLAN-Adapter for Client Connection"),
91 bridge and ("<strong>" .. translate([[It is absolutely necessary that the network you are joining
92 supports and allows bridging (WDS) otherwise your connection will fail.]]) .. "</strong> " .. 
93 translate([[Note: You can use the access point wizard to configure your
94 private access point to increase the range of the network you are connected to.]])) or "")
95
96 s = m2:section(NamedSection, iface, "wifi-iface", translate("Wireless Settings"))
97 s.addremove = false
98
99 s:tab("general", translate("General Settings"))
100 s:tab("expert", translate("Expert Settings"))
101
102 local ssid = s:taboption("general", Value, "ssid", translate("Network Name (<abbr title=\"Extended Service Set Identifier\">ESSID</abbr>)"))
103 ssid.rmempty = false
104
105 for _, v in ipairs(suggest) do
106         if v.mode == "Master" then
107                 ssid:value(v.ssid)
108                 
109                 if not v.wep then
110                         encrdep.wep[#encrdep.wep+1] = {ssid = v.ssid, ["!reverse"] = 1}
111                 end
112                 if v.wpa ~= 1 or (v.wpa == 1 and v.auth_suites[1] ~= "802.1x") then
113                         encrdep.wpa[#encrdep.wpa+1] = {ssid = v.ssid, ["!reverse"] = 1}
114                 end
115                 if v.wpa ~= 1 or (v.wpa == 1 and v.auth_suites[1] ~= "PSK") then                
116                         encrdep.psk[#encrdep.psk+1] = {ssid = v.ssid, ["!reverse"] = 1}
117                 end
118                 if not v.wpa or v.wpa < 2 or (v.wpa >= 2 and v.auth_suites[1] ~= "802.1x") then
119                         encrdep.wpa2[#encrdep.wpa2+1] = {ssid = v.ssid, ["!reverse"] = 1}
120                 end
121                 if not v.wpa or v.wpa < 2 or (v.wpa >= 2 and v.auth_suites[1] ~= "PSK") then
122                         encrdep.psk2[#encrdep.psk2+1] = {ssid = v.ssid, ["!reverse"] = 1}
123                 end
124                 if v.wpa or v.wep then
125                         encrdep.none[#encrdep.none+1] = {ssid = v.ssid, ["!reverse"] = 1} 
126                 end
127         end
128 end
129
130 mode = s:taboption("expert", ListValue, "mode", translate("Operating Mode"))
131 mode.override_values = true
132 mode:value("sta", translate("Client"))
133
134 encr = s:taboption("general", ListValue, "encryption", translate("Encryption"))
135
136
137 if hwtype == "mac80211" then
138         if not bridge then
139                 mode:value("mesh", translate("Mesh (802.11s)"))
140                 local meshid = s:taboption("expert", Value, "mesh_id", translate("Mesh ID"))
141                 meshid:depends("mode", "mesh")
142         end
143         
144         local ps = s:taboption("expert", Flag, "powersave", translate("Enable Powersaving"))
145         ps:depends("mode", "sta")
146 elseif hwtype == "atheros" then
147         s:taboption("expert", Flag, "bursting", translate("Allow Burst Transmissions"))
148 end
149
150
151
152 -- Encryption --
153
154 encr.override_values = true
155 encr.override_depends = true
156 encr:value("none", "No Encryption", unpack(encrdep.none))
157 encr:value("wep", "WEP", unpack(encrdep.wep))
158
159 if hwtype == "atheros" or hwtype == "mac80211" or hwtype == "prism2" then
160         local supplicant = fs.access("/usr/sbin/wpa_supplicant") or os.getenv("LUCI_SYSROOT")
161         if supplicant then              
162                 encr:value("psk", "WPA", unpack(encrdep.psk))
163                 encr:value("wpa", "WPA-EAP", unpack(encrdep.wpa))
164                 encr:value("psk2", "WPA2", unpack(encrdep.psk2))
165                 encr:value("wpa2", "WPA2-EAP (802.11i)", unpack(encrdep.wpa2))
166         end
167 elseif hwtype == "broadcom" then
168         encr:value("psk", "WPA", unpack(encrdep.psk))
169         encr:value("psk2", "WPA2", unpack(encrdep.psk2))
170 end
171
172 key = s:taboption("general", Value, "key", translate("Password"))
173 key:depends("encryption", "wep")
174 key:depends("encryption", "psk")
175 key:depends("encryption", "psk2")
176 key.rmempty = true
177 key.password = true
178
179 if hwtype == "atheros" or hwtype == "mac80211" or hwtype == "prism2" then
180         eaptype = s:taboption("general", ListValue, "eap_type", translate("EAP-Method"))
181         eaptype:value("TLS")
182         eaptype:value("TTLS")
183         eaptype:value("PEAP")
184         eaptype:depends({encryption="wpa"})
185         eaptype:depends({encryption="wpa2"})
186
187         cacert = s:taboption("general", FileUpload, "ca_cert", translate("Path to CA-Certificate"))
188         cacert:depends({encryption="wpa"})
189         cacert:depends({encryption="wpa2"})
190
191         privkey = s:taboption("general", FileUpload, "priv_key", translate("Path to Private Key"))
192         privkey:depends({eap_type="TLS", encryption="wpa2"})
193         privkey:depends({eap_type="TLS", encryption="wpa"})
194
195         privkeypwd = s:taboption("general", Value, "priv_key_pwd", translate("Password of Private Key"))
196         privkeypwd:depends({eap_type="TLS", encryption="wpa2"})
197         privkeypwd:depends({eap_type="TLS", encryption="wpa"})
198
199
200         auth = s:taboption("general", Value, "auth", translate("Authentication"))
201         auth:value("PAP")
202         auth:value("CHAP")
203         auth:value("MSCHAP")
204         auth:value("MSCHAPV2")
205         auth:depends({eap_type="PEAP", encryption="wpa2"})
206         auth:depends({eap_type="PEAP", encryption="wpa"})
207         auth:depends({eap_type="TTLS", encryption="wpa2"})
208         auth:depends({eap_type="TTLS", encryption="wpa"})
209
210
211         identity = s:taboption("general", Value, "identity", translate("Identity"))
212         identity:depends({eap_type="PEAP", encryption="wpa2"})
213         identity:depends({eap_type="PEAP", encryption="wpa"})
214         identity:depends({eap_type="TTLS", encryption="wpa2"})
215         identity:depends({eap_type="TTLS", encryption="wpa"})
216
217         password = s:taboption("general", Value, "password", translate("Password"))
218         password:depends({eap_type="PEAP", encryption="wpa2"})
219         password:depends({eap_type="PEAP", encryption="wpa"})
220         password:depends({eap_type="TTLS", encryption="wpa2"})
221         password:depends({eap_type="TTLS", encryption="wpa"})
222 end
223
224
225
226 if not bridge then
227
228 m = Map("network")
229
230 s = m:section(NamedSection, net, "interface", translate("Address Settings"))
231 s.addremove = false
232
233 s:tab("general", translate("General Settings"))
234 s:tab("expert", translate("Expert Settings"))
235
236 p = s:taboption("general", ListValue, "proto", "Connection Type")
237 p.override_scheme = true
238 p.default = "dhcp"
239 p:value("dhcp", "Automatic Configuration (DHCP)")
240 p:value("static", "Static Configuration")
241
242
243
244 ipaddr = s:taboption("general", Value, "ipaddr", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
245 ipaddr.rmempty = true
246 ipaddr:depends("proto", "static")
247
248 nm = s:taboption("general", Value, "netmask", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Netmask"))
249 nm.rmempty = true
250 nm:depends("proto", "static")
251 nm:value("255.255.255.0")
252 nm:value("255.255.0.0")
253 nm:value("255.0.0.0")
254
255 gw = s:taboption("general", Value, "gateway", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Gateway"))
256 gw:depends("proto", "static")
257 gw.rmempty = true
258
259 bcast = s:taboption("expert", Value, "bcast", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Broadcast"))
260 bcast:depends("proto", "static")
261
262 if has_ipv6 then
263         ip6addr = s:taboption("expert", Value, "ip6addr", translate("<abbr title=\"Internet Protocol Version 6\">IPv6</abbr>-Address"), translate("<abbr title=\"Classless Inter-Domain Routing\">CIDR</abbr>-Notation: address/prefix"))
264         ip6addr:depends("proto", "static")
265
266         ip6gw = s:taboption("expert", Value, "ip6gw", translate("<abbr title=\"Internet Protocol Version 6\">IPv6</abbr>-Gateway"))
267         ip6gw:depends("proto", "static")
268 end
269
270 dns = s:taboption("expert", Value, "dns", translate("<abbr title=\"Domain Name System\">DNS</abbr>-Server"))
271 dns:depends("peerdns", "")
272
273 mtu = s:taboption("expert", Value, "mtu", "MTU")
274 mtu.isinteger = true
275
276 mac = s:taboption("expert", Value, "macaddr", translate("<abbr title=\"Media Access Control\">MAC</abbr>-Address"))
277
278 return m2, m
279
280 else
281
282 return m2
283
284 end