]> git.ipfire.org Git - people/stevee/network.git/blame - functions.wireless
ipv{6,4}: Simplify some functions and introduce new ones.
[people/stevee/network.git] / functions.wireless
CommitLineData
d76f5107 1#!/bin/bash
1578dae9
MT
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2010 Michael Tremer & Christian Schmidt #
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###############################################################################
d76f5107
MT
21
22PHY_DIR="/sys/class/ieee80211"
23
24function phy_dir() {
25 local phy=${1}
26
27 echo "${PHY_DIR}/${phy}"
28}
29
30function phy_exists() {
31 local phy=${1}
32
33 [ -d "$(phy_dir ${phy})" ]
34}
35
36function phy_list() {
37 local phy
38 for phy in $(phy_dir)/*; do
39 phy=$(basename ${phy})
40 echo "${phy}"
41 done
42}
43
44function phy_get() {
45 local info=${1}
46
47 local phy
48
49 if listmatch ${info} $(phy_list); then
50 phy="${info}"
51 elif device_exists ${info}; then
52 info=$(device_get_address ${info})
53 fi
54
55 if [ -z "${phy}" ] && mac_is_valid ${info}; then
56 local i
57 for i in $(phy_list); do
58 if [ "${info}" = "$(phy_get_address ${i})" ]; then
59 phy=${i}
60 break
61 fi
62 done
63 fi
64
65 if [ -z "${phy}" ]; then
66 return ${EXIT_ERROR}
67 fi
68
69 echo "${phy}"
70 return ${EXIT_OK}
71}
72
73function phy_get_address() {
74 local phy=${1}
75
76 assert isset phy
77
78 cat $(phy_dir ${phy})/macaddress 2>/dev/null
79}
80
81function wireless_create() {
82 local device=${1}
83 local phy=$(phy_get ${2})
84 local type=${3}
85 local mac=${4}
86
87 assert isset device
88 assert isset phy
89 assert isset type
90
91 isset mac || mac=$(mac_generate)
92
93 assert phy_exists ${phy}
94 assert isoneof type managed __ap
95
96 iw phy ${phy} interface add ${device} type ${type}
97
98 if device_exists ${device}; then
99 device_set_address ${device} ${mac}
100 fi
101
102 device_set_up ${device}
103}
104
105function wireless_remove() {
106 local device=${1}
107
108 assert device_exists ${device}
109
110 device_set_down ${device}
111
112 iw dev ${device} del
113}
114
115function wireless_set_channel() {
116 local device=${1}
117 local channel=${2}
118
119 assert isset device
120 assert device_exists ${device}
121 assert isset channel
122
123 iw dev ${device} set channel ${channel} $@
124}
125
d76f5107
MT
126function hostapd_config_dir() {
127 local device=${1}
128
129 echo "${RUN_DIR}/hostapd/${device}"
130}
131
132function hostapd_config_write() {
133 local device=${1}
134 shift
135
136 assert device_exists ${device}
137
138 local broadcast_ssid
139 local channel
140 local country_code
25e32463
MT
141 local encryption
142 local key
d76f5107
MT
143 local mode
144 local ssid
145
146 while [ $# -gt 0 ]; do
147 case "${1}" in
148 --broadcast-ssid=*)
149 broadcast_ssid=${1#--broadcast-ssid=}
150 ;;
151 --channel=*)
152 channel=${1#--channel=}
153 ;;
154 --country-code=*)
155 country_code=${1#--country-code=}
156 ;;
157 --mode=*)
158 mode=${1#--mode=}
159 ;;
160 --ssid=*)
161 ssid=${1#--ssid=}
162 ;;
25e32463
MT
163 --encryption=*)
164 encryption=$(cli_get_val ${1})
165 ;;
166 --key=*)
167 key=$(cli_get_val ${1})
168 ;;
d76f5107
MT
169 *)
170 warning_log "Ignoring unknown argument '${1}'."
171 ;;
172 esac
173 shift
174 done
175
176 assert isset broadcast_ssid
177 assert isbool broadcast_ssid
178
179 assert isset channel
180 assert isinteger channel
181
182 assert isset country_code
183 assert isset mode
184 assert isset ssid
185
25e32463
MT
186 # Check if key is set when encryption is used.
187 if isset encryption; then
188 assert isoneof encryption WPA WPA2 WPA/WPA2
189 assert isset key
190 fi
191
d76f5107
MT
192 local ignore_broadcast_ssid
193 if enabled broadcast_ssid; then
194 ignore_broadcast_ssid="0"
195 else
196 ignore_broadcast_ssid="1"
197 fi
198
199 cat <<EOF
200### Hostapd configuration for ${device}
201
202# Interface configuration
203driver=nl80211
204interface=${device}
205
206# Wireless configuration
207channel=${channel}
208country_code=${country_code}
209hw_mode=${mode}
210ignore_broadcast_ssid=${ignore_broadcast_ssid}
211ssid=${ssid}
212
d76f5107 213# Dump file
25e32463 214dump_file=$(hostapd_config_dir ${device}/dump)
d76f5107
MT
215
216ctrl_interface=/var/run/hostapd
217ctrl_interface_group=0
25e32463 218
d76f5107
MT
219EOF
220
25e32463
MT
221 if isset encryption; then
222 local encryption_mode=0
223 case "${encryption}" in
224 WPA)
225 encryption_mode=1
226 ;;
227 WPA2)
228 encryption_mode=2
229 ;;
230 WPA/WPA2)
231 encryption_mode=3
232 ;;
233 esac
234
235 print "# Encryption settings."
236 print "wpa=${encryption_mode}"
237 print "wpa_passphrase=${key}"
238 print "wpa_key_mgmt=WPA-PSK"
239 print "wpa_pairwise=TKIP"
240 print "rsn_pairwise=CCMP"
241 print
242 fi
243
d76f5107
MT
244 return ${EXIT_OK}
245}
246
247function hostapd_start() {
248 local device=${1}
249 shift
250
251 assert isset device
252
253 local config_dir=$(hostapd_config_dir ${device})
254 mkdir -p ${config_dir}
255
256 local config_file=${config_dir}/config
257 hostapd_config_write ${device} $@ > ${config_file}
258
1d08b9b3 259 service_start "hostapd@${device}.service"
d76f5107
MT
260 local ret=$?
261
262 case "${ret}" in
263 0)
264 log DEBUG "Hostapd was successfully started for '${device}'."
265 return ${EXIT_OK}
266 ;;
267 1)
268 error_log "Could not start hostapd properly for '${device}'."
269
270 error_log "Configuration file dump:"
271 local line
272 while read line; do
273 error_log " ${line}"
274 done < ${config_file}
275
276 return ${EXIT_ERROR}
277 ;;
278 esac
279}
280
281function hostapd_stop() {
282 local device=${1}
d76f5107
MT
283 assert isset device
284
1d08b9b3 285 service_stop "hostapd@${device}.service"
d76f5107
MT
286
287 rm -rf $(hostapd_config_dir ${device})
288}
289
290function hostapd_get_pid() {
291 local device=${1}
292
293 assert isset device
294
295 local pid_file="$(hostapd_config_dir ${device})/pid"
296
297 [ -e "${pid_file}" ] || return ${EXIT_ERROR}
298
299 cat ${pid_file} 2>/dev/null
300 return ${EXIT_OK}
301}
302
303function hostapd_is_running() {
304 local device=${1}
305
306 assert isset device
307
308 local pid=$(hostapd_get_pid ${device})
309
310 if isset pid && [ -d "/proc/${pid}" ]; then
311 return ${EXIT_OK}
312 fi
313
314 return ${EXIT_ERROR}
315}
f6ee6bb1
AF
316
317function wpa_supplicant_config_write() {
318 local device=${1}
319 shift
320
321 assert isset device
322
323 local ssid
324 local encryption
325 local key
326
327 while [ $# -gt 0 ]; do
328 case "${1}" in
329 --ssid=*)
330 ssid=${1#--ssid=}
331 ;;
332 --encryption=*)
333 encryption=${1#--encryption=}
334 ;;
335 --key=*)
336 key=${1#--key=}
337 ;;
338 esac
339 shift
340 done
341
342 assert isset ssid
343 assert isset encryption
344 assert isset key
345
346 cat <<EOF
347# WPA supplicant configuration for ${device}.
348# DO NOT EDIT.
349
350network={
351 ssid="${ssid}"
352 proto=RSN
353 key_mgmt=${encryption}
354 pairwise=CCMP
355 group=TKIP
356 psk="${key}"
357}
358
359EOF
360}
361
362function wpa_supplicant_config_dir() {
363 local device=${1}
364
365 assert isset device
366
367 echo "${RUN_DIR}/wireless/${device}"
368}
369
370function wpa_supplicant_start() {
371 local device=${1}
372 shift
373
374 assert device_exists ${device}
375
376 local config_dir=$(wpa_supplicant_config_dir ${device})
377 mkdir -p ${config_dir}
378
379 local config_file=${config_dir}/config
380 wpa_supplicant_config_write ${device} $@ > ${config_file}
381
382 wpa_supplicant -i ${device} -D wext -B -c ${config_file} \
383 -P ${config_dir}/pid
384}
385
386function wpa_supplicant_stop() {
387 local device=${1}
388
389 assert isset device
390
391 local pid=$(wpa_supplicant_get_pid ${device})
392
393 if isset pid; then
394 process_kill ${pid}
395 else
396 warning_log "Could not find pid file for wpa_supplicant process running for ${device}."
397 fi
398
399 rm -rf $(wpa_supplicant_config_dir ${device})
400}
401
402function wpa_supplicant_get_pid() {
403 local device=${1}
404
405 assert isset device
406
407 local pid_file="$(wpa_supplicant_config_dir ${device})/pid"
408
409 [ -e "${pid_file}" ] || return ${EXIT_ERROR}
410
411 cat ${pid_file} 2>/dev/null
412 return ${EXIT_OK}
413}
414
415function wpa_supplicant_is_running() {
416 local device=${1}
417
418 assert isset device
419
420 local pid=$(wpa_supplicant_get_pid ${device})
421
422 if isset pid && [ -d "/proc/${pid}" ]; then
423 return ${EXIT_OK}
424 fi
425
426 return ${EXIT_ERROR}
427}
428
429function wpa_supplicant_get_pid() {
430 local zone=${1}
431 shift
432
433
434}
435
436function wpa_supplicant_stop() {
437 local zone=${1}
438 shift
439
440 killall wpa_supplicant
441}