]> git.ipfire.org Git - people/ms/network.git/blobdiff - functions.hostapd
hostapd_config_write: Reorder arguments in alphabetical order.
[people/ms/network.git] / functions.hostapd
index e3465ab0fdca3dd5e011aa1807616bf0a451866e..f1a65769e5d3b2ddeaf21c743d28c31648d810c9 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
        local country_code
        local encryption
+       local ieee80211d="1"
        local key
        local mode
        local ssid
@@ -42,26 +43,34 @@ 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=}
-                               ;;
-                       --mode=*)
-                               mode=${1#--mode=}
-                               ;;
-                       --ssid=*)
-                               ssid=${1#--ssid=}
+                               country_code=$(cli_get_val ${1})
                                ;;
                        --encryption=*)
                                encryption=$(cli_get_val ${1})
                                ;;
+                       --ieee80211d=*)
+                               local val="$(cli_get_val "${1}")"
+                               if enabled val; then
+                                       ieee80211d="1"
+                               else
+                                       ieee80211d="0"
+                               fi
+                               ;;
                        --key=*)
                                key=$(cli_get_val ${1})
                                ;;
+                       --mode=*)
+                               mode=$(cli_get_val ${1})
+                               ;;
+                       --ssid=*)
+                               ssid=$(cli_get_val ${1})
+                               ;;
                        *)
                                warning_log "Ignoring unknown argument '${1}'."
                                ;;                      
@@ -85,6 +94,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"
@@ -100,31 +124,37 @@ function hostapd_config_write() {
                        hw_mode="a"
                fi
                ieee80211n="1"
+       else
+               hw_mode="${mode}"
        fi
 
-       cat <<EOF
-### Hostapd configuration for ${device}
-
-# Interface configuration
-driver=nl80211
-interface=${device}
-
-# 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)
+       (
+               print "# Wireless configuration"
+               print "channel=${channel}"
+               print "country_code=${country_code}"
+               print "hw_mode=${hw_mode}"
+               print "ieee80211d=${ieee80211d}"
+               print "ieee80211n=${ieee80211n}"
+               print "ignore_broadcast_ssid=${ignore_broadcast_ssid}"
+
+               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
@@ -139,13 +169,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}
@@ -153,36 +185,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() {
@@ -190,6 +205,4 @@ function hostapd_stop() {
        assert isset device
 
        service_stop "hostapd@${device}.service"
-
-       rm -rf $(hostapd_config_dir ${device})
 }