]> git.ipfire.org Git - people/ms/network.git/blob - functions.wireless
114e217af4b060ffb4da38ac62243b030438a717
[people/ms/network.git] / functions.wireless
1 #!/bin/bash
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 ###############################################################################
21
22 PHY_DIR="/sys/class/ieee80211"
23
24 function phy_dir() {
25 local phy=${1}
26
27 echo "${PHY_DIR}/${phy}"
28 }
29
30 function phy_exists() {
31 local phy=${1}
32
33 [ -d "$(phy_dir ${phy})" ]
34 }
35
36 function phy_list() {
37 local phy
38 for phy in $(phy_dir)/*; do
39 phy=$(basename ${phy})
40 echo "${phy}"
41 done
42 }
43
44 function 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
73 function phy_get_address() {
74 local phy=${1}
75
76 assert isset phy
77
78 cat $(phy_dir ${phy})/macaddress 2>/dev/null
79 }
80
81 function 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
105 function 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
115 function 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
126 function hostapd_init() {
127 mkdir -p $(hostapd_config_dir)
128 }
129
130 init_register hostapd_init
131
132 function hostapd_config_dir() {
133 local device=${1}
134
135 echo "${RUN_DIR}/hostapd/${device}"
136 }
137
138 function 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
195 driver=nl80211
196 interface=${device}
197
198 # Wireless configuration
199 channel=${channel}
200 country_code=${country_code}
201 hw_mode=${mode}
202 ignore_broadcast_ssid=${ignore_broadcast_ssid}
203 ssid=${ssid}
204
205 # Logging options
206 logger_syslog=-1
207 logger_syslog_level=2
208 logger_stdout=-1
209 logger_stdout_level=2
210
211 # Dump file
212 dump_file=$(hostapd_config_dir ${device}/dump
213
214 ctrl_interface=/var/run/hostapd
215 ctrl_interface_group=0
216 EOF
217
218 return ${EXIT_OK}
219 }
220
221 function 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
233 hostapd -dd -B -P ${config_dir}/pid ${config_file}
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
255 function hostapd_stop() {
256 local device=${1}
257
258 assert isset device
259
260 local pid=$(hostapd_get_pid ${device})
261
262 if isset pid; then
263 process_kill ${pid}
264 else
265 warning_log "Could not find pid file for hostapd process running for ${device}."
266 fi
267
268 rm -rf $(hostapd_config_dir ${device})
269 }
270
271 function hostapd_get_pid() {
272 local device=${1}
273
274 assert isset device
275
276 local pid_file="$(hostapd_config_dir ${device})/pid"
277
278 [ -e "${pid_file}" ] || return ${EXIT_ERROR}
279
280 cat ${pid_file} 2>/dev/null
281 return ${EXIT_OK}
282 }
283
284 function hostapd_is_running() {
285 local device=${1}
286
287 assert isset device
288
289 local pid=$(hostapd_get_pid ${device})
290
291 if isset pid && [ -d "/proc/${pid}" ]; then
292 return ${EXIT_OK}
293 fi
294
295 return ${EXIT_ERROR}
296 }
297
298 function wpa_supplicant_config_write() {
299 local device=${1}
300 shift
301
302 assert isset device
303
304 local ssid
305 local encryption
306 local key
307
308 while [ $# -gt 0 ]; do
309 case "${1}" in
310 --ssid=*)
311 ssid=${1#--ssid=}
312 ;;
313 --encryption=*)
314 encryption=${1#--encryption=}
315 ;;
316 --key=*)
317 key=${1#--key=}
318 ;;
319 esac
320 shift
321 done
322
323 assert isset ssid
324 assert isset encryption
325 assert isset key
326
327 cat <<EOF
328 # WPA supplicant configuration for ${device}.
329 # DO NOT EDIT.
330
331 network={
332 ssid="${ssid}"
333 proto=RSN
334 key_mgmt=${encryption}
335 pairwise=CCMP
336 group=TKIP
337 psk="${key}"
338 }
339
340 EOF
341 }
342
343 function wpa_supplicant_config_dir() {
344 local device=${1}
345
346 assert isset device
347
348 echo "${RUN_DIR}/wireless/${device}"
349 }
350
351 function wpa_supplicant_start() {
352 local device=${1}
353 shift
354
355 assert device_exists ${device}
356
357 local config_dir=$(wpa_supplicant_config_dir ${device})
358 mkdir -p ${config_dir}
359
360 local config_file=${config_dir}/config
361 wpa_supplicant_config_write ${device} $@ > ${config_file}
362
363 wpa_supplicant -i ${device} -D wext -B -c ${config_file} \
364 -P ${config_dir}/pid
365 }
366
367 function wpa_supplicant_stop() {
368 local device=${1}
369
370 assert isset device
371
372 local pid=$(wpa_supplicant_get_pid ${device})
373
374 if isset pid; then
375 process_kill ${pid}
376 else
377 warning_log "Could not find pid file for wpa_supplicant process running for ${device}."
378 fi
379
380 rm -rf $(wpa_supplicant_config_dir ${device})
381 }
382
383 function wpa_supplicant_get_pid() {
384 local device=${1}
385
386 assert isset device
387
388 local pid_file="$(wpa_supplicant_config_dir ${device})/pid"
389
390 [ -e "${pid_file}" ] || return ${EXIT_ERROR}
391
392 cat ${pid_file} 2>/dev/null
393 return ${EXIT_OK}
394 }
395
396 function wpa_supplicant_is_running() {
397 local device=${1}
398
399 assert isset device
400
401 local pid=$(wpa_supplicant_get_pid ${device})
402
403 if isset pid && [ -d "/proc/${pid}" ]; then
404 return ${EXIT_OK}
405 fi
406
407 return ${EXIT_ERROR}
408 }
409
410 function wpa_supplicant_get_pid() {
411 local zone=${1}
412 shift
413
414
415 }
416
417 function wpa_supplicant_stop() {
418 local zone=${1}
419 shift
420
421 killall wpa_supplicant
422 }