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