]> git.ipfire.org Git - people/stevee/network.git/commitdiff
hostapd: Rework starting this service.
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 4 Oct 2012 18:21:42 +0000 (18:21 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 4 Oct 2012 18:21:42 +0000 (18:21 +0000)
Now it works in the same way as all the other systemd-monitored
services do.

functions.hostapd
functions.ports
helpers/hostapd-config-helper [new file with mode: 0755]
hooks/ports/wireless-ap

index c1a6cab78989a193cef8ee58efed3b86ea76da98..d481707a3761a1b89dd73b77b0c81eb2542a07c4 100644 (file)
 #                                                                             #
 ###############################################################################
 
-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 <<EOF
-### Hostapd configuration for ${device}
-
-# Interface configuration
-driver=nl80211
-interface=${device}
+       (
+               print "# Wireless configuration"
+               print "channel=${channel}"
+               print "country_code=${country_code}"
+               print "hw_mode=${hw_mode}"
+               print "ieee80211n=${ieee80211n}"
+               print "ignore_broadcast_ssid=${ignore_broadcast_ssid}"
 
-# Wireless configuration
-channel=${channel}
-country_code=${country_code}
-hw_mode=${hw_mode}
-ieee80211n=${ieee80211n}
-ignore_broadcast_ssid=${ignore_broadcast_ssid}
-ssid=${ssid}
-
-# Dump file
-dump_file=$(hostapd_config_dir ${device}/dump)
+               if contains_spaces "${ssid}"; then
+                       print "ssid=\"${ssid}\""
+               else
+                       print "ssid=${ssid}"
+               fi
 
-ctrl_interface=/var/run/hostapd
-ctrl_interface_group=0
+               print
+       ) >> ${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})
 }
index 89ab98f63b3608226209b719d644a1b66b458b83..198a08ba3694576e1102c41087c8b04971907958 100644 (file)
@@ -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 (executable)
index 0000000..668945b
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
+
+. /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}
index 8f4532e30a7fa849410205e25cac5cb803511b4e..33e0f975f9b29123e862191002f4798d316af42e 100755 (executable)
@@ -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}