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