]> git.ipfire.org Git - ipfire-2.x.git/commitdiff
hostapd: Generate the configuration in the initscript
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 8 May 2025 15:57:07 +0000 (17:57 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 22 Sep 2025 11:46:23 +0000 (11:46 +0000)
This will give us some more flexibility in the future.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
html/cgi-bin/wlanap.cgi
src/initscripts/packages/hostapd

index 11239928392460d4e0c234892b6bdf566242948f..2a3135977efef1de62a5c00a41ee003d83e1602c 100644 (file)
@@ -101,7 +101,6 @@ if ($cgiparams{'ACTION'} eq "$Lang::tr{'save'}") {
 
        if ($errormessage eq '') {
                &General::writehash("/var/ipfire/wlanap/settings", \%wlanapsettings);
-               &WriteConfig_hostapd();
 
                &General::system("/usr/local/bin/wlanapctrl", "restart");
        }
@@ -404,169 +403,6 @@ END
 &Header::closebigbox();
 &Header::closepage();
 
-sub WriteConfig_hostapd{
-       open (CONFIGFILE, ">/var/ipfire/wlanap/hostapd.conf");
-       print CONFIGFILE <<END
-driver=nl80211
-######################### basic hostapd configuration ##########################
-#
-country_code=$wlanapsettings{'COUNTRY'}
-country3=0x49 # indoor
-ieee80211d=1
-ieee80211h=1
-channel=$wlanapsettings{'CHANNEL'}
-
-# Always advertise TPC
-local_pwr_constraint=3
-spectrum_mgmt_required=1
-END
-;
- if ( $wlanapsettings{'HW_MODE'} eq 'an' ){
-       print CONFIGFILE <<END
-hw_mode=a
-ieee80211n=1
-wmm_enabled=1
-ht_capab=$wlanapsettings{'HTCAPS'}
-END
-;
-
- }elsif ( $wlanapsettings{'HW_MODE'} eq 'gn' ){
-       print CONFIGFILE <<END
-hw_mode=g
-ieee80211n=1
-wmm_enabled=1
-ht_capab=$wlanapsettings{'HTCAPS'}
-END
-;
-
- }elsif ( $wlanapsettings{'HW_MODE'} eq 'ac' ){
-       print CONFIGFILE <<END
-hw_mode=a
-ieee80211ac=1
-ieee80211n=1
-wmm_enabled=1
-ht_capab=$wlanapsettings{'HTCAPS'}
-vht_capab=$wlanapsettings{'VHTCAPS'}
-vht_oper_chwidth=1
-END
-;
-
- }else{
-       print CONFIGFILE <<END
-hw_mode=$wlanapsettings{'HW_MODE'}
-END
-;
-
- }
-
-print CONFIGFILE <<END;
-# Enable logging
-logger_syslog=-1
-logger_syslog_level=4
-auth_algs=1
-ctrl_interface=/var/run/hostapd
-ctrl_interface_group=0
-disassoc_low_ack=1
-
-# SSID
-ssid2=\"$wlanapsettings{'SSID'}\"
-utf8_ssid=1
-
-END
-
-if ( $wlanapsettings{'HIDESSID'} eq 'on' ){
-       print CONFIGFILE <<END
-ignore_broadcast_ssid=2
-END
-;
- }
-
- # https://forum.ipfire.org/viewtopic.php?f=22&t=12274&p=79070#p79070
- if ( $wlanapsettings{'CLIENTISOLATION'} eq 'on' ){
-       print CONFIGFILE <<END
-ap_isolate=1
-END
-;
- }
-
- if ( $wlanapsettings{'NOSCAN'} eq 'on' ){
-       print CONFIGFILE <<END
-noscan=1
-END
-;
-
- }else{
-       print CONFIGFILE <<END
-noscan=0
-END
-;
-
- }
-
- # Management Frame Protection (802.11w)
- if ($wlanapsettings{'IEEE80211W'} eq "on") {
-       print CONFIGFILE "ieee80211w=2\n";
- } elsif ($wlanapsettings{'IEEE80211W'} eq "optional") {
-       print CONFIGFILE "ieee80211w=1\n";
- } else {
-       print CONFIGFILE "ieee80211w=0\n";
- }
-
- if ( $wlanapsettings{'ENC'} eq 'wpa1'){
-       print CONFIGFILE <<END
-######################### wpa hostapd configuration ############################
-#
-wpa=1
-wpa_passphrase=$wlanapsettings{'PWD'}
-wpa_key_mgmt=WPA-PSK
-wpa_pairwise=TKIP
-END
-;
- }elsif ( $wlanapsettings{'ENC'} eq 'wpa2'){
-       print CONFIGFILE <<END
-######################### wpa hostapd configuration ############################
-#
-wpa=2
-wpa_passphrase=$wlanapsettings{'PWD'}
-wpa_key_mgmt=WPA-PSK
-rsn_pairwise=CCMP
-END
-;
- }elsif ( $wlanapsettings{'ENC'} eq 'wpa3'){
-       print CONFIGFILE <<END
-######################### wpa hostapd configuration ############################
-#
-wpa=2
-wpa_passphrase=$wlanapsettings{'PWD'}
-wpa_key_mgmt=SAE
-rsn_pairwise=CCMP
-END
-;
- } elsif ( $wlanapsettings{'ENC'} eq 'wpa1+2'){
-       print CONFIGFILE <<END
-######################### wpa hostapd configuration ############################
-#
-wpa=3
-wpa_passphrase=$wlanapsettings{'PWD'}
-wpa_key_mgmt=WPA-PSK
-wpa_pairwise=TKIP
-rsn_pairwise=CCMP
-END
-;
- }elsif ( $wlanapsettings{'ENC'} eq 'wpa2+3'){
-       print CONFIGFILE <<END
-######################### wpa hostapd configuration ############################
-#
-wpa=2
-wpa_passphrase=$wlanapsettings{'PWD'}
-wpa_key_mgmt=WPA-PSK SAE
-rsn_pairwise=CCMP
-END
-;
- }
-       close CONFIGFILE;
-}
-
 sub get_phy($) {
        my $intf = shift;
        my $phy;
index d2e5677a7233cd5891dc026e5041a01fb6e73c25..2b431620b3718d6ae2180af70a4983fbca1facbf 100644 (file)
@@ -36,6 +36,147 @@ find_interface() {
        return 1;
 }
 
+write_config() {
+       # Header
+       echo "# Automatically generated configuration"
+       echo "# DO NOT EDIT"
+
+       # Enable logging
+       echo "logger_syslog=-1"
+       echo "logger_syslog_level=4"
+
+       # Use Netlink
+       echo "driver=nl80211"
+
+       # Set the country code
+       echo "country_code=${COUNTRY}"
+       echo "country3=0x49"
+
+       # Enable 802.11d and 802.11h
+       echo "ieee80211d=1"
+       echo "ieee80211h=1"
+
+       # Set the channel (if chosen)
+       if [ -n "${CHANNEL}" ]; then
+               echo "channel=${CHANNEL}"
+       fi
+
+       # Always advertise TPC
+       echo "local_pwr_constraint=3"
+       echo "spectrum_mgmt_required=1"
+
+       # Set mode
+       case "${HW_MODE}" in
+               ac)
+                       echo "hw_mode=a"
+                       echo "ieee80211ac=1"
+                       echo "ieee80211n=1"
+                       echo "wmm_enabled=1"
+                       echo "vht_oper_chwidth=1"
+                       ;;
+               an|gn)
+                       echo "hw_mode=${HW_MODE:0:1}"
+                       echo "ieee80211n=1"
+                       echo "wmm_enabled=1"
+                       ;;
+               *)
+                       echo "Unsupported mode '${HW_MODE}'" >&2
+                       return 1
+                       ;;
+       esac
+
+       # Set HT capabilities
+       if [ -n "${HTCAPS}" ]; then
+               echo "ht_capab=${HTCAPS}"
+       fi
+
+       # Set VHT capabilities
+       if [ -n "${VHTCAPS}" ]; then
+               echo "vht_capab=${VHTCAPS}"
+       fi
+
+       # Enable authentication
+       echo "auth_algs=1"
+
+       # Enable the control interface
+       echo "ctrl_interface=/var/run/hostapd"
+       echo "ctrl_interface_group=0"
+
+       # Disconnect clients that are too far away
+       echo "disassoc_low_ack=1"
+
+       # SSID
+       echo "ssid2=\"${SSID}\""
+       echo "utf8_ssid=1"
+
+       # Hide the SSID?
+       if [ "${HIDESSID}" = "on" ]; then
+               echo "ignore_broadcast_ssid=2"
+       fi
+
+       # Isolate clients?
+       if [ "${CLIENTISOLATION}" = "on" ]; then
+               echo "ap_isolate=1"
+       fi
+
+       # Disable neighbour scan?
+       if [ "${NOSCAN}" = "on" ]; then
+               echo "noscan=1"
+       else
+               echo "noscan=0"
+       fi
+
+       # Management Frame Protection (802.11w)
+       case "${IEEE80211W}" in
+               on)
+                       echo "ieee80211w=2"
+                       ;;
+               optional)
+                       echo "ieee80211w=1"
+                       ;;
+               *)
+                       echo "ieee80211w=0"
+                       ;;
+       esac
+
+       # Encryption
+       case "${ENC}" in
+               wpa3)
+                       echo "wpa=2"
+                       echo "wpa_passphrase=${PWD}"
+                       echo "wpa_key_mgmt=SAE"
+                       echo "rsn_pairwise=CCMP"
+                       ;;
+               wpa2+3)
+                       echo "wpa=2"
+                       echo "wpa_passphrase=${PWD}"
+                       echo "wpa_key_mgmt=WPA-PSK SAE"
+                       echo "rsn_pairwise=CCMP"
+                       ;;
+               wpa2)
+                       echo "wpa=2"
+                       echo "wpa_passphrase=${PWD}"
+                       echo "wpa_key_mgmt=WPA-PSK"
+                       echo "rsn_pairwise=CCMP"
+                       ;;
+               wpa1+2)
+                       echo "wpa=3"
+                       echo "wpa_passphrase=${PWD}"
+                       echo "wpa_key_mgmt=WPA-PSK"
+                       echo "wpa_pairwise=TKIP"
+                       echo "rsn_pairwise=CCMP"
+                       ;;
+               wpa1)
+                       echo "wpa=1"
+                       echo "wpa_passphrase=${PWD}"
+                       echo "wpa_key_mgmt=WPA-PSK"
+                       echo "wpa_pairwise=TKIP"
+                       ;;
+       esac
+
+       return 0
+}
+
 eval $(/usr/local/bin/readhash /var/ipfire/wlanap/settings)
 
 case "${1}" in
@@ -47,6 +188,13 @@ case "${1}" in
                        exit 1
                fi
 
+               # Write the configuration
+               if ! write_config > /etc/hostapd.conf; then
+                       boot_mesg "Failed to generate configuration"
+                       echo_failure
+                       exit 1
+               fi
+
                boot_mesg "Starting hostapd... "
                loadproc /usr/bin/hostapd -s -B /etc/hostapd.conf -i "${interface}"
                ;;
@@ -67,6 +215,10 @@ case "${1}" in
                statusproc /usr/bin/hostapd
                ;;
 
+       show-config)
+               write_config
+               ;;
+
        *)
                echo "Usage: ${0} {start|stop|restart|status}"
                exit 1