]> git.ipfire.org Git - people/ms/network.git/blob - functions.wireless
wireless-ap: Add support for 802.11a and 802.11n APs.
[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_config_dir() {
127 local device=${1}
128
129 echo "${RUN_DIR}/hostapd/${device}"
130 }
131
132 function 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 encryption
142 local key
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 ;;
163 --encryption=*)
164 encryption=$(cli_get_val ${1})
165 ;;
166 --key=*)
167 key=$(cli_get_val ${1})
168 ;;
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
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
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 local hw_mode ieee80211n="0"
200 if [ "${mode}" = "n" ]; then
201 if [ ${channel} -le 15 ]; then
202 hw_mode="g"
203 else
204 hw_mode="a"
205 fi
206 ieee80211n="1"
207 fi
208
209 cat <<EOF
210 ### Hostapd configuration for ${device}
211
212 # Interface configuration
213 driver=nl80211
214 interface=${device}
215
216 # Wireless configuration
217 channel=${channel}
218 country_code=${country_code}
219 hw_mode=${hw_mode}
220 ieee80211n=${ieee80211n}
221 ignore_broadcast_ssid=${ignore_broadcast_ssid}
222 ssid=${ssid}
223
224 # Dump file
225 dump_file=$(hostapd_config_dir ${device}/dump)
226
227 ctrl_interface=/var/run/hostapd
228 ctrl_interface_group=0
229
230 EOF
231
232 if isset encryption; then
233 local encryption_mode=0
234 case "${encryption}" in
235 WPA)
236 encryption_mode=1
237 ;;
238 WPA2)
239 encryption_mode=2
240 ;;
241 WPA/WPA2)
242 encryption_mode=3
243 ;;
244 esac
245
246 print "# Encryption settings."
247 print "wpa=${encryption_mode}"
248 print "wpa_passphrase=${key}"
249 print "wpa_key_mgmt=WPA-PSK"
250 print "wpa_pairwise=TKIP"
251 print "rsn_pairwise=CCMP"
252 print
253 fi
254
255 return ${EXIT_OK}
256 }
257
258 function hostapd_start() {
259 local device=${1}
260 shift
261
262 assert isset device
263
264 local config_dir=$(hostapd_config_dir ${device})
265 mkdir -p ${config_dir}
266
267 local config_file=${config_dir}/config
268 hostapd_config_write ${device} $@ > ${config_file}
269
270 service_start "hostapd@${device}.service"
271 local ret=$?
272
273 case "${ret}" in
274 0)
275 log DEBUG "Hostapd was successfully started for '${device}'."
276 return ${EXIT_OK}
277 ;;
278 1)
279 error_log "Could not start hostapd properly for '${device}'."
280
281 error_log "Configuration file dump:"
282 local line
283 while read line; do
284 error_log " ${line}"
285 done < ${config_file}
286
287 return ${EXIT_ERROR}
288 ;;
289 esac
290 }
291
292 function hostapd_stop() {
293 local device=${1}
294 assert isset device
295
296 service_stop "hostapd@${device}.service"
297
298 rm -rf $(hostapd_config_dir ${device})
299 }
300
301 function hostapd_get_pid() {
302 local device=${1}
303
304 assert isset device
305
306 local pid_file="$(hostapd_config_dir ${device})/pid"
307
308 [ -e "${pid_file}" ] || return ${EXIT_ERROR}
309
310 cat ${pid_file} 2>/dev/null
311 return ${EXIT_OK}
312 }
313
314 function hostapd_is_running() {
315 local device=${1}
316
317 assert isset device
318
319 local pid=$(hostapd_get_pid ${device})
320
321 if isset pid && [ -d "/proc/${pid}" ]; then
322 return ${EXIT_OK}
323 fi
324
325 return ${EXIT_ERROR}
326 }
327
328 function wpa_supplicant_config_write() {
329 local device=${1}
330 shift
331
332 assert isset device
333
334 local ssid
335 local encryption
336 local key
337
338 while [ $# -gt 0 ]; do
339 case "${1}" in
340 --ssid=*)
341 ssid=${1#--ssid=}
342 ;;
343 --encryption=*)
344 encryption=${1#--encryption=}
345 ;;
346 --key=*)
347 key=${1#--key=}
348 ;;
349 esac
350 shift
351 done
352
353 assert isset ssid
354 assert isset encryption
355 assert isset key
356
357 cat <<EOF
358 # WPA supplicant configuration for ${device}.
359 # DO NOT EDIT.
360
361 network={
362 ssid="${ssid}"
363 proto=RSN
364 key_mgmt=${encryption}
365 pairwise=CCMP
366 group=TKIP
367 psk="${key}"
368 }
369
370 EOF
371 }
372
373 function wpa_supplicant_config_dir() {
374 local device=${1}
375
376 assert isset device
377
378 echo "${RUN_DIR}/wireless/${device}"
379 }
380
381 function wpa_supplicant_start() {
382 local device=${1}
383 shift
384
385 assert device_exists ${device}
386
387 local config_dir=$(wpa_supplicant_config_dir ${device})
388 mkdir -p ${config_dir}
389
390 local config_file=${config_dir}/config
391 wpa_supplicant_config_write ${device} $@ > ${config_file}
392
393 wpa_supplicant -i ${device} -D wext -B -c ${config_file} \
394 -P ${config_dir}/pid
395 }
396
397 function wpa_supplicant_stop() {
398 local device=${1}
399
400 assert isset device
401
402 local pid=$(wpa_supplicant_get_pid ${device})
403
404 if isset pid; then
405 process_kill ${pid}
406 else
407 warning_log "Could not find pid file for wpa_supplicant process running for ${device}."
408 fi
409
410 rm -rf $(wpa_supplicant_config_dir ${device})
411 }
412
413 function wpa_supplicant_get_pid() {
414 local device=${1}
415
416 assert isset device
417
418 local pid_file="$(wpa_supplicant_config_dir ${device})/pid"
419
420 [ -e "${pid_file}" ] || return ${EXIT_ERROR}
421
422 cat ${pid_file} 2>/dev/null
423 return ${EXIT_OK}
424 }
425
426 function wpa_supplicant_is_running() {
427 local device=${1}
428
429 assert isset device
430
431 local pid=$(wpa_supplicant_get_pid ${device})
432
433 if isset pid && [ -d "/proc/${pid}" ]; then
434 return ${EXIT_OK}
435 fi
436
437 return ${EXIT_ERROR}
438 }
439
440 function wpa_supplicant_get_pid() {
441 local zone=${1}
442 shift
443
444
445 }
446
447 function wpa_supplicant_stop() {
448 local zone=${1}
449 shift
450
451 killall wpa_supplicant
452 }