NIU:
[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 encr = s:taboption("general", ListValue, "encryption", translate("Encryption"))
118
119
120 if hwtype == "mac80211" then
121         s:taboption("expert", Flag, "powersave", "Enable Powersaving")
122 elseif hwtype == "atheros" then
123         s:taboption("expert", Flag, "bursting", translate("Allow Burst Transmissions"))
124 end
125
126
127
128 -- Encryption --
129
130 encr.override_values = true
131 encr.override_depends = true
132 encr:value("none", "No Encryption", unpack(encrdep.none))
133 encr:value("wep", "WEP", unpack(encrdep.wep))
134
135 if hwtype == "atheros" or hwtype == "mac80211" or hwtype == "prism2" then
136         local supplicant = fs.access("/usr/sbin/wpa_supplicant") or os.getenv("LUCI_SYSROOT")
137         if supplicant then              
138                 encr:value("psk", "WPA", unpack(encrdep.psk))
139                 encr:value("wpa", "WPA-EAP", unpack(encrdep.wpa))
140                 encr:value("psk2", "WPA2", unpack(encrdep.psk2))
141                 encr:value("wpa2", "WPA2-EAP (802.11i)", unpack(encrdep.wpa2))
142         end
143 elseif hwtype == "broadcom" then
144         encr:value("psk", "WPA", unpack(encrdep.psk))
145         encr:value("psk2", "WPA2", unpack(encrdep.psk2))
146 end
147
148 key = s:taboption("general", Value, "key", translate("Password"))
149 key:depends("encryption", "wep")
150 key:depends("encryption", "psk")
151 key:depends("encryption", "psk2")
152 key.rmempty = true
153 key.password = true
154
155 if hwtype == "atheros" or hwtype == "mac80211" or hwtype == "prism2" then
156         eaptype = s:taboption("general", ListValue, "eap_type", translate("EAP-Method"))
157         eaptype:value("TLS")
158         eaptype:value("TTLS")
159         eaptype:value("PEAP")
160         eaptype:depends({encryption="wpa"})
161         eaptype:depends({encryption="wpa2"})
162
163         cacert = s:taboption("general", FileUpload, "ca_cert", translate("Path to CA-Certificate"))
164         cacert:depends({encryption="wpa"})
165         cacert:depends({encryption="wpa2"})
166
167         privkey = s:taboption("general", FileUpload, "priv_key", translate("Path to Private Key"))
168         privkey:depends({eap_type="TLS", encryption="wpa2"})
169         privkey:depends({eap_type="TLS", encryption="wpa"})
170
171         privkeypwd = s:taboption("general", Value, "priv_key_pwd", translate("Password of Private Key"))
172         privkeypwd:depends({eap_type="TLS", encryption="wpa2"})
173         privkeypwd:depends({eap_type="TLS", encryption="wpa"})
174
175
176         auth = s:taboption("general", Value, "auth", translate("Authentication"))
177         auth:value("PAP")
178         auth:value("CHAP")
179         auth:value("MSCHAP")
180         auth:value("MSCHAPV2")
181         auth:depends({eap_type="PEAP", encryption="wpa2"})
182         auth:depends({eap_type="PEAP", encryption="wpa"})
183         auth:depends({eap_type="TTLS", encryption="wpa2"})
184         auth:depends({eap_type="TTLS", encryption="wpa"})
185
186
187         identity = s:taboption("general", Value, "identity", translate("Identity"))
188         identity:depends({eap_type="PEAP", encryption="wpa2"})
189         identity:depends({eap_type="PEAP", encryption="wpa"})
190         identity:depends({eap_type="TTLS", encryption="wpa2"})
191         identity:depends({eap_type="TTLS", encryption="wpa"})
192
193         password = s:taboption("general", Value, "password", translate("Password"))
194         password:depends({eap_type="PEAP", encryption="wpa2"})
195         password:depends({eap_type="PEAP", encryption="wpa"})
196         password:depends({eap_type="TTLS", encryption="wpa2"})
197         password:depends({eap_type="TTLS", encryption="wpa"})
198 end
199
200
201
202
203
204 m = Map("network")
205
206 s = m:section(NamedSection, "wan", "interface", translate("Address Settings"))
207 s.addremove = false
208
209 s:tab("general", translate("General Settings"))
210 s:tab("expert", translate("Expert Settings"))
211
212 p = s:taboption("general", ListValue, "proto", "Connection Type")
213 p.override_scheme = true
214 p.default = "dhcp"
215 p:value("dhcp", "Automatic Configuration (DHCP)")
216 p:value("static", "Static Configuration")
217
218
219
220 ipaddr = s:taboption("general", Value, "ipaddr", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
221 ipaddr.rmempty = true
222 ipaddr:depends("proto", "static")
223
224 nm = s:taboption("general", Value, "netmask", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Netmask"))
225 nm.rmempty = true
226 nm:depends("proto", "static")
227 nm:value("255.255.255.0")
228 nm:value("255.255.0.0")
229 nm:value("255.0.0.0")
230
231 gw = s:taboption("general", Value, "gateway", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Gateway"))
232 gw:depends("proto", "static")
233 gw.rmempty = true
234
235 bcast = s:taboption("expert", Value, "bcast", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Broadcast"))
236 bcast:depends("proto", "static")
237
238 if has_ipv6 then
239         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"))
240         ip6addr:depends("proto", "static")
241
242         ip6gw = s:taboption("expert", Value, "ip6gw", translate("<abbr title=\"Internet Protocol Version 6\">IPv6</abbr>-Gateway"))
243         ip6gw:depends("proto", "static")
244 end
245
246 dns = s:taboption("expert", Value, "dns", translate("<abbr title=\"Domain Name System\">DNS</abbr>-Server"))
247 dns:depends("peerdns", "")
248
249 mtu = s:taboption("expert", Value, "mtu", "MTU")
250 mtu.isinteger = true
251
252 mac = s:taboption("expert", Value, "macaddr", translate("<abbr title=\"Media Access Control\">MAC</abbr>-Address"))
253
254 return m2, m