]> git.ipfire.org Git - people/ms/network.git/blame - functions.wireless
hostapd should be monitored by systemd.
[people/ms/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
126function hostapd_init() {
127 mkdir -p $(hostapd_config_dir)
128}
129
130init_register hostapd_init
131
132function hostapd_config_dir() {
133 local device=${1}
134
135 echo "${RUN_DIR}/hostapd/${device}"
136}
137
138function hostapd_config_write() {
139 local device=${1}
140 shift
141
142 assert device_exists ${device}
143
144 local broadcast_ssid
145 local channel
146 local country_code
147 local mode
148 local ssid
149
150 while [ $# -gt 0 ]; do
151 case "${1}" in
152 --broadcast-ssid=*)
153 broadcast_ssid=${1#--broadcast-ssid=}
154 ;;
155 --channel=*)
156 channel=${1#--channel=}
157 ;;
158 --country-code=*)
159 country_code=${1#--country-code=}
160 ;;
161 --mode=*)
162 mode=${1#--mode=}
163 ;;
164 --ssid=*)
165 ssid=${1#--ssid=}
166 ;;
167 *)
168 warning_log "Ignoring unknown argument '${1}'."
169 ;;
170 esac
171 shift
172 done
173
174 assert isset broadcast_ssid
175 assert isbool broadcast_ssid
176
177 assert isset channel
178 assert isinteger channel
179
180 assert isset country_code
181 assert isset mode
182 assert isset ssid
183
184 local ignore_broadcast_ssid
185 if enabled broadcast_ssid; then
186 ignore_broadcast_ssid="0"
187 else
188 ignore_broadcast_ssid="1"
189 fi
190
191 cat <<EOF
192### Hostapd configuration for ${device}
193
194# Interface configuration
195driver=nl80211
196interface=${device}
197
198# Wireless configuration
199channel=${channel}
200country_code=${country_code}
201hw_mode=${mode}
202ignore_broadcast_ssid=${ignore_broadcast_ssid}
203ssid=${ssid}
204
205# Logging options
206logger_syslog=-1
207logger_syslog_level=2
208logger_stdout=-1
209logger_stdout_level=2
210
211# Dump file
212dump_file=$(hostapd_config_dir ${device}/dump
213
214ctrl_interface=/var/run/hostapd
215ctrl_interface_group=0
216EOF
217
218 return ${EXIT_OK}
219}
220
221function hostapd_start() {
222 local device=${1}
223 shift
224
225 assert isset device
226
227 local config_dir=$(hostapd_config_dir ${device})
228 mkdir -p ${config_dir}
229
230 local config_file=${config_dir}/config
231 hostapd_config_write ${device} $@ > ${config_file}
232
4243f0ca 233 service_start hostapd@${device}
d76f5107
MT
234 local ret=$?
235
236 case "${ret}" in
237 0)
238 log DEBUG "Hostapd was successfully started for '${device}'."
239 return ${EXIT_OK}
240 ;;
241 1)
242 error_log "Could not start hostapd properly for '${device}'."
243
244 error_log "Configuration file dump:"
245 local line
246 while read line; do
247 error_log " ${line}"
248 done < ${config_file}
249
250 return ${EXIT_ERROR}
251 ;;
252 esac
253}
254
255function hostapd_stop() {
256 local device=${1}
257
258 assert isset device
259
4243f0ca 260 service_stop hostapd@${device}
d76f5107
MT
261
262 rm -rf $(hostapd_config_dir ${device})
263}
264
265function hostapd_get_pid() {
266 local device=${1}
267
268 assert isset device
269
270 local pid_file="$(hostapd_config_dir ${device})/pid"
271
272 [ -e "${pid_file}" ] || return ${EXIT_ERROR}
273
274 cat ${pid_file} 2>/dev/null
275 return ${EXIT_OK}
276}
277
278function hostapd_is_running() {
279 local device=${1}
280
281 assert isset device
282
283 local pid=$(hostapd_get_pid ${device})
284
285 if isset pid && [ -d "/proc/${pid}" ]; then
286 return ${EXIT_OK}
287 fi
288
289 return ${EXIT_ERROR}
290}
f6ee6bb1
AF
291
292function wpa_supplicant_config_write() {
293 local device=${1}
294 shift
295
296 assert isset device
297
298 local ssid
299 local encryption
300 local key
301
302 while [ $# -gt 0 ]; do
303 case "${1}" in
304 --ssid=*)
305 ssid=${1#--ssid=}
306 ;;
307 --encryption=*)
308 encryption=${1#--encryption=}
309 ;;
310 --key=*)
311 key=${1#--key=}
312 ;;
313 esac
314 shift
315 done
316
317 assert isset ssid
318 assert isset encryption
319 assert isset key
320
321 cat <<EOF
322# WPA supplicant configuration for ${device}.
323# DO NOT EDIT.
324
325network={
326 ssid="${ssid}"
327 proto=RSN
328 key_mgmt=${encryption}
329 pairwise=CCMP
330 group=TKIP
331 psk="${key}"
332}
333
334EOF
335}
336
337function wpa_supplicant_config_dir() {
338 local device=${1}
339
340 assert isset device
341
342 echo "${RUN_DIR}/wireless/${device}"
343}
344
345function wpa_supplicant_start() {
346 local device=${1}
347 shift
348
349 assert device_exists ${device}
350
351 local config_dir=$(wpa_supplicant_config_dir ${device})
352 mkdir -p ${config_dir}
353
354 local config_file=${config_dir}/config
355 wpa_supplicant_config_write ${device} $@ > ${config_file}
356
357 wpa_supplicant -i ${device} -D wext -B -c ${config_file} \
358 -P ${config_dir}/pid
359}
360
361function wpa_supplicant_stop() {
362 local device=${1}
363
364 assert isset device
365
366 local pid=$(wpa_supplicant_get_pid ${device})
367
368 if isset pid; then
369 process_kill ${pid}
370 else
371 warning_log "Could not find pid file for wpa_supplicant process running for ${device}."
372 fi
373
374 rm -rf $(wpa_supplicant_config_dir ${device})
375}
376
377function wpa_supplicant_get_pid() {
378 local device=${1}
379
380 assert isset device
381
382 local pid_file="$(wpa_supplicant_config_dir ${device})/pid"
383
384 [ -e "${pid_file}" ] || return ${EXIT_ERROR}
385
386 cat ${pid_file} 2>/dev/null
387 return ${EXIT_OK}
388}
389
390function wpa_supplicant_is_running() {
391 local device=${1}
392
393 assert isset device
394
395 local pid=$(wpa_supplicant_get_pid ${device})
396
397 if isset pid && [ -d "/proc/${pid}" ]; then
398 return ${EXIT_OK}
399 fi
400
401 return ${EXIT_ERROR}
402}
403
404function wpa_supplicant_get_pid() {
405 local zone=${1}
406 shift
407
408
409}
410
411function wpa_supplicant_stop() {
412 local zone=${1}
413 shift
414
415 killall wpa_supplicant
416}