]> git.ipfire.org Git - people/stevee/network.git/blob - src/functions/functions.wpa_supplicant
b64bf2e2768995480d5b9d0acaec922af4117e3c
[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_config_dir "${device}")/wpa_supplicant.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 psk; 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 local config_dir=$(dirname ${file})
146 mkdir -p ${config_dir} 2>/dev/null
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 local file="$(wpa_supplicant_config_dir "${device}")/wpa_supplicant.conf"
219
220 file_delete "${file}"
221 }
222
223 wpa_supplicant_config_dir() {
224 local device=${1}
225 assert isset device
226
227 echo "${RUN_DIR}/wpa_supplicant/${device}"
228 }
229
230 wpa_supplicant_start() {
231 local device=${1}
232 assert isset device
233
234 service_start "wpa_supplicant@${device}.service"
235 }
236
237 wpa_supplicant_stop() {
238 local device=${1}
239 assert isset device
240
241 service_stop "wpa_supplicant@${device}.service"
242 }
243
244 wpa_supplicant_client() {
245 local device=${1}
246 assert isset device
247 shift
248
249 local cmd="$@"
250 assert isset cmd
251
252 # Run the command and return the output.
253 cmd wpa_cli -p${WPA_SUPPLICANT_SOCKET_DIR} -i${device} ${cmd}
254 }
255
256 wpa_cli_status() {
257 local device=${1}
258 assert isset device
259
260 wpa_supplicant_client ${device} status verbose
261 }
262
263 wpa_cli_status_get() {
264 local device=${1}
265 assert isset device
266
267 local arg=${2}
268 assert isset arg
269
270 local line key
271 while read -r line; do
272 key=$(cli_get_key ${line})
273
274 if [ "${key}" = "${arg}" ]; then
275 cli_get_val "${line}"
276 return ${EXIT_OK}
277 fi
278 done <<< "$(wpa_cli_status ${device})"
279
280 return ${EXIT_ERROR}
281 }
282
283 wpa_cli_bss() {
284 local device=${1}
285 assert isset device
286
287 local bss=${2}
288 assert isset bss
289
290 wpa_supplicant_client ${device} bss ${bss}
291 }
292
293 wpa_cli_bss_get() {
294 local device=${1}
295 assert isset device
296
297 local bss=${2}
298 assert isset bss
299
300 local arg=${3}
301 assert isset arg
302
303 local line key
304 while read -r line; do
305 key=$(cli_get_key ${line})
306
307 if [ "${key}" = "${arg}" ]; then
308 cli_get_val "${line}"
309 return ${EXIT_OK}
310 fi
311 done <<< "$(wpa_cli_bss ${device} ${bss})"
312
313 return ${EXIT_ERROR}
314 }
315
316 wpa_cli_bss_get_frequency() {
317 local device=${1}
318 assert isset device
319
320 local bssid=${2}
321 assert isset bssid
322
323 wpa_cli_bss_get ${device} ${bssid} freq
324 }
325
326 wpa_cli_bss_get_noise() {
327 local device=${1}
328 assert isset device
329
330 local bssid=${2}
331 assert isset bssid
332
333 wpa_cli_bss_get ${device} ${bssid} noise
334 }
335
336 wpa_cli_bss_get_quality() {
337 local device=${1}
338 assert isset device
339
340 local bssid=${2}
341 assert isset bssid
342
343 local quality=$(wpa_cli_bss_get ${device} ${bssid} qual)
344
345 # Convert to percent
346 print $(( ${quality} * 100 / 70 ))
347 }
348
349 wpa_cli_bss_get_flags() {
350 local device=${1}
351 assert isset device
352
353 local bssid=${2}
354 assert isset bssid
355
356 wpa_cli_bss_get ${device} ${bssid} flags
357 }