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