From: Michael Tremer Date: Thu, 4 Oct 2012 18:21:42 +0000 (+0000) Subject: hostapd: Rework starting this service. X-Git-Tag: 006~27 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=49ec20d862879e9ddc98355b8cedb3c9a3b91f18;p=network.git hostapd: Rework starting this service. Now it works in the same way as all the other systemd-monitored services do. --- diff --git a/functions.hostapd b/functions.hostapd index c1a6cab7..d481707a 100644 --- a/functions.hostapd +++ b/functions.hostapd @@ -19,17 +19,17 @@ # # ############################################################################### -function hostapd_config_dir() { - local device=${1} - - echo "${RUN_DIR}/hostapd/${device}" -} +HOSTAPD_CONTROL_INTERFACE_DIR="/run/hostapd/ctrl" function hostapd_config_write() { local device=${1} - shift + assert isset device - assert device_exists ${device} + local file=${2} + assert isset file + + # Shift the device and file argument. + shift 2 local broadcast_ssid local channel @@ -42,19 +42,19 @@ function hostapd_config_write() { while [ $# -gt 0 ]; do case "${1}" in --broadcast-ssid=*) - broadcast_ssid=${1#--broadcast-ssid=} + broadcast_ssid=$(cli_get_val ${1}) ;; --channel=*) - channel=${1#--channel=} + channel=$(cli_get_val ${1}) ;; --country-code=*) - country_code=${1#--country-code=} + country_code=$(cli_get_val ${1}) ;; --mode=*) - mode=${1#--mode=} + mode=$(cli_get_val ${1}) ;; --ssid=*) - ssid=${1#--ssid=} + ssid=$(cli_get_val ${1}) ;; --encryption=*) encryption=$(cli_get_val ${1}) @@ -85,6 +85,21 @@ function hostapd_config_write() { assert isset key fi + # Create configuration directory. + local config_dir=$(dirname ${file}) + mkdir -p ${HOSTAPD_CONTROL_INTERFACE_DIR} ${config_dir} 2>/dev/null + + config_header "hostapd" > ${file} + + # Interface configuration + ( + print "# Interface configuration" + print "driver=nl80211" + print "interface=${device}" + print + ) >> ${file} + + # Wireless configuration local ignore_broadcast_ssid if enabled broadcast_ssid; then ignore_broadcast_ssid="0" @@ -104,29 +119,32 @@ function hostapd_config_write() { hw_mode="${mode}" fi - cat <> ${file} -EOF + # Control interface. + ( + print "# Control interface" + print "ctrl_interface=${HOSTAPD_CONTROL_INTERFACE_DIR}" + print "ctrl_interface_group=0" + print + ) >> ${file} + # Encryption settings if isset encryption; then local encryption_mode=0 case "${encryption}" in @@ -141,13 +159,15 @@ EOF ;; esac - print "# Encryption settings." - print "wpa=${encryption_mode}" - print "wpa_passphrase=${key}" - print "wpa_key_mgmt=WPA-PSK" - print "wpa_pairwise=TKIP" - print "rsn_pairwise=CCMP" - print + ( + print "# Encryption settings" + print "wpa=${encryption_mode}" + print "wpa_passphrase=${key}" + print "wpa_key_mgmt=WPA-PSK" + print "wpa_pairwise=TKIP" + print "rsn_pairwise=CCMP" + print + ) >> ${file} fi return ${EXIT_OK} @@ -155,36 +175,19 @@ EOF function hostapd_start() { local device=${1} - shift - assert isset device - local config_dir=$(hostapd_config_dir ${device}) - mkdir -p ${config_dir} - - local config_file=${config_dir}/config - hostapd_config_write ${device} $@ > ${config_file} - service_start "hostapd@${device}.service" local ret=$? - case "${ret}" in - 0) - log DEBUG "Hostapd was successfully started for '${device}'." - return ${EXIT_OK} - ;; - 1) - error_log "Could not start hostapd properly for '${device}'." - - error_log "Configuration file dump:" - local line - while read line; do - error_log " ${line}" - done < ${config_file} - - return ${EXIT_ERROR} - ;; - esac + if [ ${ret} -eq ${EXIT_OK} ]; then + log DEBUG "hostapd has been successfully started on '${device}'" + else + log ERROR "Could not start hostapd on '${device}': ${ret}" + return ${EXIT_ERROR} + fi + + return ${EXIT_OK} } function hostapd_stop() { @@ -192,6 +195,4 @@ function hostapd_stop() { assert isset device service_stop "hostapd@${device}.service" - - rm -rf $(hostapd_config_dir ${device}) } diff --git a/functions.ports b/functions.ports index 89ab98f6..198a08ba 100644 --- a/functions.ports +++ b/functions.ports @@ -23,6 +23,40 @@ function port_dir() { echo "${NETWORK_CONFIG_DIR}/ports" } +function port_get_hook() { + local port=${1} + assert isset port + + config_get_hook $(port_file ${port}) +} + +function port_config_dir() { + local port=${1} + + print "${RUN_DIR}/ports/${port}" + return ${EXIT_OK} +} + +function port_config_read() { + local port=${1} + assert isset port + + # Save the HOOK variable. + local hook="${HOOK}" + + config_read $(port_file ${port}) + + # Restore hook. + HOOK="${hook}" +} + +function port_config_write() { + local port=${1} + assert isset port + + config_write $(port_file ${port}) +} + function ports_get_all() { local port @@ -35,7 +69,6 @@ function ports_get_all() { function port_file() { local port=${1} - assert isset port echo "$(port_dir)/${port}" diff --git a/helpers/hostapd-config-helper b/helpers/hostapd-config-helper new file mode 100755 index 00000000..668945b0 --- /dev/null +++ b/helpers/hostapd-config-helper @@ -0,0 +1,58 @@ +#!/bin/bash +############################################################################### +# # +# IPFire.org - A linux based firewall # +# Copyright (C) 2012 IPFire Network Development Team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +############################################################################### + +. /usr/lib/network/functions + +action="${1}" +assert isset action + +port="${2}" +assert isset port + +config_file="$(port_config_dir ${port})/hostapd.conf" + +case "${action}" in + create) + # Create the configuration file for this port. + port_config_read ${port} || exit $? + + hostapd_config_write ${port} ${config_file} \ + --broadcast-ssid="${BROADCAST_SSID}" \ + --channel="${CHANNEL}" \ + --country-code="${COUNTRY_CODE}" \ + --encryption="${ENCRYPTION}" \ + --key="${KEY}" \ + --mode="${MODE}" \ + --ssid="${SSID}" \ + || exit $? + ;; + + remove) + rm -f ${config_file} + ;; + + *) + log ERROR "Unknown action passed: ${action}" + exit ${EXIT_ERROR} + ;; +esac + +exit ${EXIT_OK} diff --git a/hooks/ports/wireless-ap b/hooks/ports/wireless-ap index 8f4532e3..33e0f975 100755 --- a/hooks/ports/wireless-ap +++ b/hooks/ports/wireless-ap @@ -154,27 +154,26 @@ function _up() { config_read $(port_file ${port}) + # Check if the PHY is present. + local phy=$(phy_get ${PHY}) + if ! isset phy; then + log DEBUG "phy '${PHY}' is not present" + exit ${EXIT_ERROR} + fi + + # Create the wireless device, if it does not exist, yet. if ! device_exists ${port}; then - wireless_create ${port} ${PHY} __ap ${ADDRESS} + wireless_create ${port} --phy="${phy}" --type="ap" \ + --address="${ADDRESS}" fi - if ! hostapd_is_running ${port}; then - hostapd_start ${port} \ - --broadcast-ssid="${BROADCAST_SSID}" \ - --channel="${CHANNEL}" \ - --country-code="${COUNTRY_CODE}" \ - --encryption="${ENCRYPTION}" \ - --key="${KEY}" \ - --mode="${MODE}" \ - --ssid="${SSID}" - - local ret=$? - - if [ ${ret} -eq ${EXIT_ERROR} ]; then - error_log "Could not start '${port}' because hostapd crashed previously." - ( _down ${port} ) - exit ${EXIT_ERROR} - fi + # Start the hostapd service. + hostapd_start ${port} + local ret=$? + + if [ ${ret} -ne ${EXIT_OK} ]; then + log ERROR "Could not start hostapd on port '${port}': ${ret}" + exit ${EXIT_ERROR} fi exit ${EXIT_OK} @@ -184,8 +183,6 @@ function _down() { local port=${1} assert isset port - config_read $(port_file ${port}) - # Stop the hostapd daemon. hostapd_stop ${port} @@ -199,10 +196,11 @@ function _down() { function _hotplug() { local port=${1} - local phy=${2} - assert isset port + + local phy=${2} assert isset phy + assert port_exists ${port} # Read configuration of port. @@ -214,7 +212,8 @@ function _hotplug() { # Check if the phy is the same we have # read from the configuration file. if [ "${PHY}" = "${phy_address}" ]; then - wireless_create ${port} ${PHY} __ap ${ADDRESS} + wireless_create ${port} --phy="${phy_address}" --type="ap" \ + --address="${ADDRESS}" fi exit ${EXIT_OK}