2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
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. #
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. #
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/>. #
20 ###############################################################################
22 PHY_DIR
="/sys/class/ieee80211"
27 echo "${PHY_DIR}/${phy}"
30 function phy_exists
() {
33 [ -d "$(phy_dir ${phy})" ]
38 for phy
in $
(phy_dir
)/*; do
39 phy
=$
(basename ${phy})
49 if listmatch
${info} $
(phy_list
); then
51 elif device_exists
${info}; then
52 info
=$
(device_get_address
${info})
55 if [ -z "${phy}" ] && mac_is_valid
${info}; then
57 for i
in $
(phy_list
); do
58 if [ "${info}" = "$(phy_get_address ${i})" ]; then
65 if [ -z "${phy}" ]; then
73 function phy_get_address
() {
78 cat $
(phy_dir
${phy})/macaddress
2>/dev
/null
81 function wireless_create
() {
83 local phy
=$
(phy_get
${2})
91 isset mac || mac
=$
(mac_generate
)
93 assert phy_exists
${phy}
94 assert isoneof
type managed __ap
96 iw phy
${phy} interface add ${device} type ${type}
98 if device_exists
${device}; then
99 device_set_address
${device} ${mac}
102 device_set_up
${device}
105 function wireless_remove
() {
108 assert device_exists
${device}
110 device_set_down
${device}
115 function wireless_set_channel
() {
120 assert device_exists
${device}
123 iw dev
${device} set channel
${channel} $@
126 function hostapd_config_dir
() {
129 echo "${RUN_DIR}/hostapd/${device}"
132 function hostapd_config_write
() {
136 assert device_exists
${device}
146 while [ $# -gt 0 ]; do
149 broadcast_ssid
=${1#--broadcast-ssid=}
152 channel
=${1#--channel=}
155 country_code
=${1#--country-code=}
164 encryption
=$
(cli_get_val
${1})
167 key
=$
(cli_get_val
${1})
170 warning_log
"Ignoring unknown argument '${1}'."
176 assert isset broadcast_ssid
177 assert isbool broadcast_ssid
180 assert isinteger channel
182 assert isset country_code
186 # Check if key is set when encryption is used.
187 if isset encryption
; then
188 assert isoneof encryption WPA WPA2 WPA
/WPA2
192 local ignore_broadcast_ssid
193 if enabled broadcast_ssid
; then
194 ignore_broadcast_ssid
="0"
196 ignore_broadcast_ssid
="1"
200 ### Hostapd configuration for ${device}
202 # Interface configuration
206 # Wireless configuration
208 country_code=${country_code}
210 ignore_broadcast_ssid=${ignore_broadcast_ssid}
214 dump_file=$(hostapd_config_dir ${device}/dump)
216 ctrl_interface=/var/run/hostapd
217 ctrl_interface_group=0
221 if isset encryption
; then
222 local encryption_mode
=0
223 case "${encryption}" in
235 print
"# Encryption settings."
236 print
"wpa=${encryption_mode}"
237 print
"wpa_passphrase=${key}"
238 print
"wpa_key_mgmt=WPA-PSK"
239 print
"wpa_pairwise=TKIP"
240 print
"rsn_pairwise=CCMP"
247 function hostapd_start
() {
253 local config_dir
=$
(hostapd_config_dir
${device})
254 mkdir
-p ${config_dir}
256 local config_file
=${config_dir}/config
257 hostapd_config_write
${device} $@
> ${config_file}
259 service_start
"hostapd@${device}.service"
264 log DEBUG
"Hostapd was successfully started for '${device}'."
268 error_log
"Could not start hostapd properly for '${device}'."
270 error_log
"Configuration file dump:"
274 done < ${config_file}
281 function hostapd_stop
() {
285 service_stop
"hostapd@${device}.service"
287 rm -rf $
(hostapd_config_dir
${device})
290 function hostapd_get_pid
() {
295 local pid_file
="$(hostapd_config_dir ${device})/pid"
297 [ -e "${pid_file}" ] ||
return ${EXIT_ERROR}
299 cat ${pid_file} 2>/dev
/null
303 function hostapd_is_running
() {
308 local pid
=$
(hostapd_get_pid
${device})
310 if isset pid
&& [ -d "/proc/${pid}" ]; then
317 function wpa_supplicant_config_write
() {
327 while [ $# -gt 0 ]; do
333 encryption
=${1#--encryption=}
343 assert isset encryption
347 # WPA supplicant configuration for ${device}.
353 key_mgmt=${encryption}
362 function wpa_supplicant_config_dir
() {
367 echo "${RUN_DIR}/wireless/${device}"
370 function wpa_supplicant_start
() {
374 assert device_exists
${device}
376 local config_dir
=$
(wpa_supplicant_config_dir
${device})
377 mkdir
-p ${config_dir}
379 local config_file
=${config_dir}/config
380 wpa_supplicant_config_write
${device} $@
> ${config_file}
382 wpa_supplicant
-i ${device} -D wext
-B -c ${config_file} \
386 function wpa_supplicant_stop
() {
391 local pid
=$
(wpa_supplicant_get_pid
${device})
396 warning_log
"Could not find pid file for wpa_supplicant process running for ${device}."
399 rm -rf $
(wpa_supplicant_config_dir
${device})
402 function wpa_supplicant_get_pid
() {
407 local pid_file
="$(wpa_supplicant_config_dir ${device})/pid"
409 [ -e "${pid_file}" ] ||
return ${EXIT_ERROR}
411 cat ${pid_file} 2>/dev
/null
415 function wpa_supplicant_is_running
() {
420 local pid
=$
(wpa_supplicant_get_pid
${device})
422 if isset pid
&& [ -d "/proc/${pid}" ]; then
429 function wpa_supplicant_get_pid
() {
436 function wpa_supplicant_stop
() {
440 killall wpa_supplicant