]> git.ipfire.org Git - people/stevee/network.git/blame - src/functions/functions.wpa_supplicant
wpa_supplicant: Drop config helper
[people/stevee/network.git] / src / functions / functions.wpa_supplicant
CommitLineData
6d4eec4c
MT
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
22a61046
MT
22WPA_SUPPLICANT_SOCKET_DIR="${RUN_DIR}/wpa_supplicant/ctrl"
23
1c6a4e30 24wpa_supplicant_config_write() {
02807ad2
MT
25 local device="${1}"
26 shift
6d4eec4c 27
02807ad2 28 assert isset device
6d4eec4c 29
02807ad2 30 local file="$(wpa_supplicant_config_dir "${device}")/wpa_supplicant.conf"
22a61046 31
31670741 32 local ap_scan=1 mode key ssid
4c1a5e6d 33 local channel
22a61046
MT
34
35 local arg
36 for arg in "$@"; do
37 case "${arg}" in
38 --ap-scan=*)
2212045f 39 ap_scan=$(cli_get_val "${arg}")
22a61046 40 ;;
4c1a5e6d
MT
41 --channel=*)
42 channel=$(cli_get_val "${arg}")
43 ;;
22a61046 44 --mode=*)
2212045f 45 mode=$(cli_get_val "${arg}")
22a61046
MT
46
47 # Empty signals no encryption.
48 isset mode || mode="NONE"
6d4eec4c 49 ;;
22a61046 50 --ssid=*)
2212045f 51 ssid=$(cli_get_val "${arg}")
6d4eec4c
MT
52 ;;
53 --key=*)
2212045f 54 key=$(cli_get_val "${arg}")
22a61046
MT
55 ;;
56 *)
57 error "Unrecognized argument: ${arg}"
58 return ${EXIT_ERROR}
6d4eec4c
MT
59 ;;
60 esac
6d4eec4c
MT
61 done
62
22a61046
MT
63 assert isinteger ap_scan
64 assert isset mode
65
66 local auth_alg key_mgmt proto ssid psk wep_key0 wep_tx_keyidx
4c1a5e6d 67 local operation_mode
31670741 68 local country_code="$(wireless_get_reg_domain)"
22a61046
MT
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
4c1a5e6d
MT
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
22a61046
MT
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
6d4eec4c 176
22a61046 177 if isset ssid; then
aaf34099 178 print " ssid=\"${ssid}\""
22a61046
MT
179 fi
180
181 if isset key; then
182 print " psk=\"${key}\""
183 fi
184
4c1a5e6d
MT
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
22a61046
MT
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}
6d4eec4c
MT
212}
213
02807ad2
MT
214wpa_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
1c6a4e30 223wpa_supplicant_config_dir() {
6d4eec4c 224 local device=${1}
6d4eec4c
MT
225 assert isset device
226
22a61046 227 echo "${RUN_DIR}/wpa_supplicant/${device}"
6d4eec4c
MT
228}
229
1c6a4e30 230wpa_supplicant_start() {
6d4eec4c 231 local device=${1}
22a61046 232 assert isset device
6d4eec4c 233
22a61046
MT
234 service_start "wpa_supplicant@${device}.service"
235}
6d4eec4c 236
1c6a4e30 237wpa_supplicant_stop() {
22a61046
MT
238 local device=${1}
239 assert isset device
6d4eec4c 240
22a61046
MT
241 service_stop "wpa_supplicant@${device}.service"
242}
243
1c6a4e30 244wpa_supplicant_client() {
22a61046
MT
245 local device=${1}
246 assert isset device
247 shift
6d4eec4c 248
22a61046
MT
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}
6d4eec4c
MT
254}
255
1c6a4e30 256wpa_cli_status() {
6d4eec4c 257 local device=${1}
22a61046
MT
258 assert isset device
259
260 wpa_supplicant_client ${device} status verbose
261}
6d4eec4c 262
1c6a4e30 263wpa_cli_status_get() {
22a61046 264 local device=${1}
6d4eec4c
MT
265 assert isset device
266
22a61046
MT
267 local arg=${2}
268 assert isset arg
6d4eec4c 269
22a61046
MT
270 local line key
271 while read -r line; do
272 key=$(cli_get_key ${line})
6d4eec4c 273
22a61046
MT
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}
6d4eec4c
MT
281}
282
1c6a4e30 283wpa_cli_bss() {
6d4eec4c 284 local device=${1}
22a61046
MT
285 assert isset device
286
287 local bss=${2}
288 assert isset bss
6d4eec4c 289
22a61046
MT
290 wpa_supplicant_client ${device} bss ${bss}
291}
292
1c6a4e30 293wpa_cli_bss_get() {
22a61046 294 local device=${1}
6d4eec4c
MT
295 assert isset device
296
22a61046
MT
297 local bss=${2}
298 assert isset bss
6d4eec4c 299
22a61046
MT
300 local arg=${3}
301 assert isset arg
6d4eec4c 302
22a61046
MT
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}
6d4eec4c
MT
314}
315
1c6a4e30 316wpa_cli_bss_get_frequency() {
6d4eec4c 317 local device=${1}
6d4eec4c
MT
318 assert isset device
319
22a61046
MT
320 local bssid=${2}
321 assert isset bssid
6d4eec4c 322
22a61046
MT
323 wpa_cli_bss_get ${device} ${bssid} freq
324}
6d4eec4c 325
1c6a4e30 326wpa_cli_bss_get_noise() {
22a61046
MT
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
6d4eec4c
MT
334}
335
1c6a4e30 336wpa_cli_bss_get_quality() {
22a61046
MT
337 local device=${1}
338 assert isset device
6d4eec4c 339
22a61046
MT
340 local bssid=${2}
341 assert isset bssid
342
324c09bc
MT
343 local quality=$(wpa_cli_bss_get ${device} ${bssid} qual)
344
345 # Convert to percent
346 print $(( ${quality} * 100 / 70 ))
6d4eec4c
MT
347}
348
1c6a4e30 349wpa_cli_bss_get_flags() {
22a61046
MT
350 local device=${1}
351 assert isset device
352
353 local bssid=${2}
354 assert isset bssid
6d4eec4c 355
22a61046 356 wpa_cli_bss_get ${device} ${bssid} flags
6d4eec4c 357}