]> git.ipfire.org Git - people/stevee/network.git/blob - src/functions/functions.wpa_supplicant
wpa_supplicant: Fix typo in variable name
[people/stevee/network.git] / src / functions / functions.wpa_supplicant
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2012 IPFire Network Development Team #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 WPA_SUPPLICANT_SOCKET_DIR="${RUN_DIR}/wpa_supplicant/ctrl"
23
24 wpa_supplicant_config_write() {
25 local device="${1}"
26 shift
27
28 assert isset device
29
30 local file="${WPA_SUPPLICANT_CONF_DIR}/${device}.conf"
31
32 local ap_scan=1 mode key ssid
33 local channel
34
35 local arg
36 for arg in "$@"; do
37 case "${arg}" in
38 --ap-scan=*)
39 ap_scan=$(cli_get_val "${arg}")
40 ;;
41 --channel=*)
42 channel=$(cli_get_val "${arg}")
43 ;;
44 --mode=*)
45 mode=$(cli_get_val "${arg}")
46
47 # Empty signals no encryption.
48 isset mode || mode="NONE"
49 ;;
50 --ssid=*)
51 ssid=$(cli_get_val "${arg}")
52 ;;
53 --key=*)
54 key=$(cli_get_val "${arg}")
55 ;;
56 *)
57 error "Unrecognized argument: ${arg}"
58 return ${EXIT_ERROR}
59 ;;
60 esac
61 done
62
63 assert isinteger ap_scan
64 assert isset mode
65
66 local auth_alg key_mgmt proto ssid psk wep_key0 wep_tx_keyidx
67 local operation_mode
68 local country_code="$(wireless_get_reg_domain)"
69
70 case "${mode}" in
71 # Normal WPA.
72 WPA-PSK)
73 auth_alg="OPEN"
74 key_mgmt="WPA-PSK"
75 proto="WPA"
76 pairwise="CCMP TKIP"
77 group="CCMP TKIP WEP104 WEP40"
78 ;;
79
80 # WPA with stronger algorithms.
81 WPA-PSK-SHA256)
82 auth_alg="OPEN"
83 key_mgmt="WPA-PSK-SHA256"
84 proto="WPA"
85 pairwise="CCMP TKIP"
86 group="CCMP TKIP WEP104 WEP40"
87 ;;
88
89 # Normal WPA2 (802.11i).
90 WPA2-PSK)
91 auth_alg="OPEN"
92 key_mgmt="WPA-PSK"
93 proto="RSN"
94 pairwise="CCMP TKIP"
95 group="CCMP TKIP WEP104 WEP40"
96 ;;
97
98 # WPA2 with stronger algorithms.
99 WPA2-PSK-SHA256)
100 auth_alg="OPEN"
101 key_mgmt="WPA-PSK-SHA256"
102 proto="RSN"
103 pairwise="CCMP TKIP"
104 group="CCMP TKIP WEP104 WEP40"
105 ;;
106
107 # WEP.
108 WEP)
109 auth_alg="SHARED"
110 wep_key0="${key}"
111 wep_tx_keyidx="0"
112
113 # Reset PSK.
114 psk=""
115 ;;
116
117 # IEEE 802.1X
118 8021X)
119 key_mgmt="IEEE8021X"
120 ;;
121
122 # IEEE 802.11s without authentication
123 802.11s)
124 operation_mode="mesh"
125
126 # Use SAE when we got a PSK
127 if isset key; then
128 key_mgmt="SAE"
129 else
130 key_mgmt="NONE"
131 fi
132 ;;
133
134 # No encryption. DANGEROUS!
135 NONE)
136 auth_alg="OPEN"
137 key_mgmt="NONE"
138 ;;
139 *)
140 log ERROR "Unknown mode: ${mode}"
141 return ${EXIT_ERROR}
142 ;;
143 esac
144
145 # Ensure we can write the file
146 make_parent_dir "${file}"
147
148 config_header "WPA supplicant configuration file" > ${file}
149
150 # AP scanning/selection
151 print "ap_scan=${ap_scan}" >> ${file}
152
153 # Set country code, if known.
154 if isset country_code; then
155 print "country=\"${country_code}\"" >> ${file}
156 fi
157
158 # Set control socket directory.
159 print "ctrl_interface=${WPA_SUPPLICANT_SOCKET_DIR}" >> ${file}
160
161 (
162 print # Network section
163 print "network={"
164
165 if isset auth_alg; then
166 print " auth_alg=${auth_alg}"
167 fi
168
169 if isset key_mgmt; then
170 print " key_mgmt=${key_mgmt}"
171 fi
172
173 if isset proto; then
174 print " proto=${proto}"
175 fi
176
177 if isset ssid; then
178 print " ssid=\"${ssid}\""
179 fi
180
181 if isset key; then
182 print " psk=\"${key}\""
183 fi
184
185 # Operation Mode
186 case "${operation_mode}" in
187 ibss)
188 print " mode=1"
189 ;;
190 mesh)
191 print " mode=5"
192 ;;
193 esac
194
195 # Frequency
196 if isset channel; then
197 print " frequency=$(wireless_channel_to_frequency "${channel}")"
198 fi
199
200 if isset wep_key0; then
201 print " wep_key0=\"${wep_key0}\""
202 fi
203
204 if isset wep_tx_keyidx; then
205 print " wep_tx_keyidx=${wep_tx_keyidx}"
206 fi
207
208 print "}"
209 ) >> ${file}
210
211 return ${EXIT_OK}
212 }
213
214 wpa_supplicant_config_destroy() {
215 local device="${1}"
216 assert isset device
217
218 file_delete "${WPA_SUPPLICANT_CONF_DIR}/${device}.conf"
219 }
220
221 wpa_supplicant_start() {
222 local device=${1}
223 assert isset device
224
225 service_start "wpa_supplicant@${device}.service"
226 }
227
228 wpa_supplicant_stop() {
229 local device=${1}
230 assert isset device
231
232 service_stop "wpa_supplicant@${device}.service"
233 }
234
235 wpa_supplicant_client() {
236 local device=${1}
237 assert isset device
238 shift
239
240 local cmd="$@"
241 assert isset cmd
242
243 # Run the command and return the output.
244 cmd wpa_cli -p${WPA_SUPPLICANT_SOCKET_DIR} -i${device} ${cmd}
245 }
246
247 wpa_cli_status() {
248 local device=${1}
249 assert isset device
250
251 wpa_supplicant_client ${device} status verbose
252 }
253
254 wpa_cli_status_get() {
255 local device=${1}
256 assert isset device
257
258 local arg=${2}
259 assert isset arg
260
261 local line key
262 while read -r line; do
263 key=$(cli_get_key ${line})
264
265 if [ "${key}" = "${arg}" ]; then
266 cli_get_val "${line}"
267 return ${EXIT_OK}
268 fi
269 done <<< "$(wpa_cli_status ${device})"
270
271 return ${EXIT_ERROR}
272 }
273
274 wpa_cli_bss() {
275 local device=${1}
276 assert isset device
277
278 local bss=${2}
279 assert isset bss
280
281 wpa_supplicant_client ${device} bss ${bss}
282 }
283
284 wpa_cli_bss_get() {
285 local device=${1}
286 assert isset device
287
288 local bss=${2}
289 assert isset bss
290
291 local arg=${3}
292 assert isset arg
293
294 local line key
295 while read -r line; do
296 key=$(cli_get_key ${line})
297
298 if [ "${key}" = "${arg}" ]; then
299 cli_get_val "${line}"
300 return ${EXIT_OK}
301 fi
302 done <<< "$(wpa_cli_bss ${device} ${bss})"
303
304 return ${EXIT_ERROR}
305 }
306
307 wpa_cli_bss_get_frequency() {
308 local device=${1}
309 assert isset device
310
311 local bssid=${2}
312 assert isset bssid
313
314 wpa_cli_bss_get ${device} ${bssid} freq
315 }
316
317 wpa_cli_bss_get_noise() {
318 local device=${1}
319 assert isset device
320
321 local bssid=${2}
322 assert isset bssid
323
324 wpa_cli_bss_get ${device} ${bssid} noise
325 }
326
327 wpa_cli_bss_get_quality() {
328 local device=${1}
329 assert isset device
330
331 local bssid=${2}
332 assert isset bssid
333
334 local quality=$(wpa_cli_bss_get ${device} ${bssid} qual)
335
336 # Convert to percent
337 print $(( ${quality} * 100 / 70 ))
338 }
339
340 wpa_cli_bss_get_flags() {
341 local device=${1}
342 assert isset device
343
344 local bssid=${2}
345 assert isset bssid
346
347 wpa_cli_bss_get ${device} ${bssid} flags
348 }