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