NIU: Minor fixes
[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 cursor = uci.inst
22 local state = uci.inst_state
23 cursor:unload("wireless")
24 state:unload("wireless")
25
26 local has_ipv6 = fs.access("/proc/net/ipv6_route")
27 local device = cursor:get("wireless", "client", "device")
28 local hwtype = cursor:get("wireless", device, "type")
29
30
31 -- Bring up interface and scan --
32
33 if not state:get("wireless", "client", "network") then
34         local olduci = uci.cursor(nil, "")
35         local oldcl = olduci:get_all("wireless", "client")
36         olduci:unload("wireless")
37         
38         local newuci = uci.cursor()
39         local newcl = newuci:get_all("wireless", "client")
40         newcl.network = "wan"
41         
42         local proc = nixio.fork()
43         if proc == 0 then
44                 newuci:delete("wireless", "client", "ssid")
45                 newuci:commit("wireless")
46                 nixio.exec("/sbin/wifi", "up", device)
47                 os.exit(1) 
48         end
49         nixio.wait(proc)
50         
51         newuci:delete("wireless", "client")
52         newuci:section("wireless", "wifi-iface", "client", oldcl)
53         newuci:commit("wireless")
54         newuci:tset("wireless", "client", newcl)
55         newuci:save("wireless")
56         newuci:unload("wireless")
57
58         state:unload("wireless")
59 end
60  
61 local ifname = state:get("wireless", "client", "ifname") or "wlan0dummy"
62 local iwlib = iwinfo.type(ifname) and iwinfo[iwinfo.type(ifname)]
63 local suggest = {}
64 local encrdep = {
65         none = {{["!default"] = 1}},
66         wep = {{["!default"] = 1}},
67         psk = {{["!default"] = 1}},
68         psk2 = {{["!default"] = 1}},
69         wpa = {{["!default"] = 1}},
70         wpa2 = {{["!default"] = 1}}
71 }
72
73 if iwlib then
74         suggest = iwlib.scanlist(ifname)
75 end
76
77
78
79 -- Form definition --
80
81 m2 = Map("wireless", "Configure WLAN-Adapter for Internet Connection")
82
83 s = m2:section(NamedSection, "client", "wifi-iface", "Wireless Settings")
84 s.addremove = false
85
86 s:tab("general", translate("General Settings"))
87 s:tab("expert", translate("Expert Settings"))
88
89 local ssid = s:taboption("general", Value, "ssid", translate("Network Name (<abbr title=\"Extended Service Set Identifier\">ESSID</abbr>)"))
90 ssid.rmempty = false
91
92 for _, v in ipairs(suggest) do
93         if v.mode == "Master" then
94                 ssid:value(v.ssid)
95                 
96                 if not v.wep then
97                         encrdep.wep[#encrdep.wep+1] = {ssid = v.ssid, ["!reverse"] = 1}
98                 end
99                 if v.wpa ~= 1 or (v.wpa == 1 and v.auth_suites[1] ~= "802.1x") then
100                         encrdep.wpa[#encrdep.wpa+1] = {ssid = v.ssid, ["!reverse"] = 1}
101                 end
102                 if v.wpa ~= 1 or (v.wpa == 1 and v.auth_suites[1] ~= "PSK") then                
103                         encrdep.psk[#encrdep.psk+1] = {ssid = v.ssid, ["!reverse"] = 1}
104                 end
105                 if not v.wpa or v.wpa < 2 or (v.wpa >= 2 and v.auth_suites[1] ~= "802.1x") then
106                         encrdep.wpa2[#encrdep.wpa2+1] = {ssid = v.ssid, ["!reverse"] = 1}
107                 end
108                 if not v.wpa or v.wpa < 2 or (v.wpa >= 2 and v.auth_suites[1] ~= "PSK") then
109                         encrdep.psk2[#encrdep.psk2+1] = {ssid = v.ssid, ["!reverse"] = 1}
110                 end
111                 if v.wpa or v.wep then
112                         encrdep.none[#encrdep.none+1] = {ssid = v.ssid, ["!reverse"] = 1} 
113                 end
114         end
115 end
116
117 mode = s:taboption("expert", ListValue, "mode", translate("Operating Mode"))
118 mode.override_values = true
119 mode:value("sta", translate("Client"))
120
121 encr = s:taboption("general", ListValue, "encryption", translate("Encryption"))
122
123
124 if hwtype == "mac80211" then
125         mode:value("mesh", translate("Mesh (802.11s)"))
126         local meshid = s:taboption("expert", Value, "mesh_id", translate("Mesh ID"))
127         meshid:depends("mode", "mesh")
128         
129         local ps = s:taboption("expert", Flag, "powersave", translate("Enable Powersaving"))
130         ps:depends("mode", "sta")
131 elseif hwtype == "atheros" then
132         s:taboption("expert", Flag, "bursting", translate("Allow Burst Transmissions"))
133 end
134
135
136
137 -- Encryption --
138
139 encr.override_values = true
140 encr.override_depends = true
141 encr:value("none", "No Encryption", unpack(encrdep.none))
142 encr:value("wep", "WEP", unpack(encrdep.wep))
143
144 if hwtype == "atheros" or hwtype == "mac80211" or hwtype == "prism2" then
145         local supplicant = fs.access("/usr/sbin/wpa_supplicant") or os.getenv("LUCI_SYSROOT")
146         if supplicant then              
147                 encr:value("psk", "WPA", unpack(encrdep.psk))
148                 encr:value("wpa", "WPA-EAP", unpack(encrdep.wpa))
149                 encr:value("psk2", "WPA2", unpack(encrdep.psk2))
150                 encr:value("wpa2", "WPA2-EAP (802.11i)", unpack(encrdep.wpa2))
151         end
152 elseif hwtype == "broadcom" then
153         encr:value("psk", "WPA", unpack(encrdep.psk))
154         encr:value("psk2", "WPA2", unpack(encrdep.psk2))
155 end
156
157 key = s:taboption("general", Value, "key", translate("Password"))
158 key:depends("encryption", "wep")
159 key:depends("encryption", "psk")
160 key:depends("encryption", "psk2")
161 key.rmempty = true
162 key.password = true
163
164 if hwtype == "atheros" or hwtype == "mac80211" or hwtype == "prism2" then
165         eaptype = s:taboption("general", ListValue, "eap_type", translate("EAP-Method"))
166         eaptype:value("TLS")
167         eaptype:value("TTLS")
168         eaptype:value("PEAP")
169         eaptype:depends({encryption="wpa"})
170         eaptype:depends({encryption="wpa2"})
171
172         cacert = s:taboption("general", FileUpload, "ca_cert", translate("Path to CA-Certificate"))
173         cacert:depends({encryption="wpa"})
174         cacert:depends({encryption="wpa2"})
175
176         privkey = s:taboption("general", FileUpload, "priv_key", translate("Path to Private Key"))
177         privkey:depends({eap_type="TLS", encryption="wpa2"})
178         privkey:depends({eap_type="TLS", encryption="wpa"})
179
180         privkeypwd = s:taboption("general", Value, "priv_key_pwd", translate("Password of Private Key"))
181         privkeypwd:depends({eap_type="TLS", encryption="wpa2"})
182         privkeypwd:depends({eap_type="TLS", encryption="wpa"})
183
184
185         auth = s:taboption("general", Value, "auth", translate("Authentication"))
186         auth:value("PAP")
187         auth:value("CHAP")
188         auth:value("MSCHAP")
189         auth:value("MSCHAPV2")
190         auth:depends({eap_type="PEAP", encryption="wpa2"})
191         auth:depends({eap_type="PEAP", encryption="wpa"})
192         auth:depends({eap_type="TTLS", encryption="wpa2"})
193         auth:depends({eap_type="TTLS", encryption="wpa"})
194
195
196         identity = s:taboption("general", Value, "identity", translate("Identity"))
197         identity:depends({eap_type="PEAP", encryption="wpa2"})
198         identity:depends({eap_type="PEAP", encryption="wpa"})
199         identity:depends({eap_type="TTLS", encryption="wpa2"})
200         identity:depends({eap_type="TTLS", encryption="wpa"})
201
202         password = s:taboption("general", Value, "password", translate("Password"))
203         password:depends({eap_type="PEAP", encryption="wpa2"})
204         password:depends({eap_type="PEAP", encryption="wpa"})
205         password:depends({eap_type="TTLS", encryption="wpa2"})
206         password:depends({eap_type="TTLS", encryption="wpa"})
207 end
208
209
210
211
212
213 m = Map("network")
214
215 s = m:section(NamedSection, "wan", "interface", translate("Address Settings"))
216 s.addremove = false
217
218 s:tab("general", translate("General Settings"))
219 s:tab("expert", translate("Expert Settings"))
220
221 p = s:taboption("general", ListValue, "proto", "Connection Type")
222 p.override_scheme = true
223 p.default = "dhcp"
224 p:value("dhcp", "Automatic Configuration (DHCP)")
225 p:value("static", "Static Configuration")
226
227
228
229 ipaddr = s:taboption("general", Value, "ipaddr", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
230 ipaddr.rmempty = true
231 ipaddr:depends("proto", "static")
232
233 nm = s:taboption("general", Value, "netmask", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Netmask"))
234 nm.rmempty = true
235 nm:depends("proto", "static")
236 nm:value("255.255.255.0")
237 nm:value("255.255.0.0")
238 nm:value("255.0.0.0")
239
240 gw = s:taboption("general", Value, "gateway", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Gateway"))
241 gw:depends("proto", "static")
242 gw.rmempty = true
243
244 bcast = s:taboption("expert", Value, "bcast", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Broadcast"))
245 bcast:depends("proto", "static")
246
247 if has_ipv6 then
248         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"))
249         ip6addr:depends("proto", "static")
250
251         ip6gw = s:taboption("expert", Value, "ip6gw", translate("<abbr title=\"Internet Protocol Version 6\">IPv6</abbr>-Gateway"))
252         ip6gw:depends("proto", "static")
253 end
254
255 dns = s:taboption("expert", Value, "dns", translate("<abbr title=\"Domain Name System\">DNS</abbr>-Server"))
256 dns:depends("peerdns", "")
257
258 mtu = s:taboption("expert", Value, "mtu", "MTU")
259 mtu.isinteger = true
260
261 mac = s:taboption("expert", Value, "macaddr", translate("<abbr title=\"Media Access Control\">MAC</abbr>-Address"))
262
263 return m2, m