Do not manually scan wifi networks before starting wpa_supplicant
authorHugo Villeneuve <hugo@hugovil.com>
Fri, 30 Aug 2013 12:39:57 +0000 (08:39 -0400)
committerHugo Villeneuve <hugo@hugovil.com>
Thu, 31 Jul 2014 02:15:24 +0000 (22:15 -0400)
Read SSID from status output of wpa_supplicant. This lowers the connection
time from from 8 to 4 seconds.

stage2/bootscripts/ifdown
stage2/bootscripts/ifup

index a6fabf9..3634b36 100755 (executable)
@@ -51,6 +51,7 @@ else
 fi
 
 if [ "x${BOOTPROTO}" = "xwifi" ]; then
+    echo "Stopping wpa_supplicant"
     killall wpa_supplicant
 fi
 
index 6c39a7a..5434264 100755 (executable)
@@ -6,6 +6,8 @@ DEVICE=${1}
 
 BRCTL=/usr/sbin/brctl
 
+WPA_SUPP_OPTS="-Dnl80211 -B -s -c /etc/wpa_supplicant.conf"
+
 # Source functions library
 source /etc/rc.d/init.d/functions
 
@@ -95,38 +97,24 @@ proto_wireless()
     # Bring interface up
     bring_if_up
 
-    if [ ! -d /etc/sysconfig/network/ssid ]; then
-        echo "Missing \"/etc/sysconfig/network/ssid\" directory"
-        exit ${EXIT_CODE_WARNING}
-    fi
+    # Check if a socket exists, meaning wpa_supplicant is running
+    if [ -S /var/run/wpa_supplicant/${DEVICE} ]; then
+        echo "wpa_supplicant already running"
 
-    # Spaces will be converted to underscores in ESSID field
-    for wnet in $(iwlist ${DEVICE} scan | grep ESSID |
-        sed -e "s!.*SSID:\"\(.*\)\"!\1!" -e "s!\ !_!g"); do
-        if [ -f "/etc/sysconfig/network/ssid/${wnet}" ]; then
-            ESSID=${wnet}
-            source "/etc/sysconfig/network/ssid/${wnet}"
-           break
-       fi
-    done
-
-    if [ -z "${ESSID}" ]; then
-        echo "No known wifi networks"
-        exit ${EXIT_CODE_WARNING}
+        # Make sure wpa_supplicant is responding
+        if ! wpa_cli -i ${DEVICE} status; then
+            echo "Stopping unresponding wpa_supplicant"
+            killall wpa_supplicant
+            rm /var/run/wpa_supplicant/${DEVICE}
+        fi
     fi
 
-    # Warning: there is no guarantee that wpa_supplicant will try that network,
-    # if two or more networks are listed in wpa_supplicant.conf and are
-    # all simultaneously reachable (if they appear in iwlist).
-    echo "Trying to connect to wifi network \"${ESSID}\""
-
-    if [ -f /var/run/wpa_supplicant/${DEVICE} ]; then
-        echo "Stopping previous wpa_supplicant"
-        killall wpa_supplicant
-        rm /var/run/wpa_supplicant/${DEVICE}
+    # Start wpa_supplicant if it is not running
+    if [ ! -S /var/run/wpa_supplicant/${DEVICE} ]; then
+        echo "Starting wpa_supplicant"
+        wpa_supplicant ${WPA_SUPP_OPTS} -i ${DEVICE}
     fi
 
-    wpa_supplicant -B -c /etc/wpa_supplicant.conf -i ${DEVICE}
     count=0
     while ! wpa_cli -i ${DEVICE} status | grep "wpa_state=COMPLETED"; do
         echo "Waiting for wpa_supplicant to complete"
@@ -138,6 +126,36 @@ proto_wireless()
             exit ${EXIT_CODE_WARNING}
         fi
     done
+
+    # Obtain SSID that wpa_supplicant connected to, if applicable
+    SSID=$(wpa_cli -i ${DEVICE} status | egrep "^ssid=" | \
+        sed 's!^ssid=\(.*\)!\1!')
+
+    # Convert spaces to underscores in SSID field
+    SSID=$(echo ${SSID} | sed -e "s!\ !_!g")
+
+    echo "wpa_supplicant connected to network \"${SSID}\""
+
+    if [ -z "${SSID}" ]; then
+        echo "Not connected to any network"
+        exit ${EXIT_CODE_WARNING}
+    fi
+
+    if [ ! -d /etc/sysconfig/network/ssid ]; then
+        echo "Missing \"/etc/sysconfig/network/ssid\" directory"
+        exit ${EXIT_CODE_WARNING}
+    fi
+
+    # Warning:
+    # wpa_supplicant will connect to a given network SSID even if the case is incorrect
+    # (MyNetwork and MYNETWORK will work). But the ssid network file configuration
+    # case must match exactly what was returned by wpa_supplicant
+    if [ ! -f "/etc/sysconfig/network/ssid/${SSID}" ]; then
+        echo "${0}: Missing network configuration file \"/etc/sysconfig/network/ssid/${SSID}\""
+        exit ${EXIT_CODE_FAILURE}
+    fi
+
+    source "/etc/sysconfig/network/ssid/${SSID}"
 }
 
 # Make sure interface is available
@@ -173,7 +191,7 @@ elif [ "x${BOOTPROTO}" = "xstatic" ]; then
 elif [ x${BOOTPROTO} = "xpppoe" ]; then
     cmd_run_log pppoe-start
 else
-    echo "Invalid or absent BOOTPROTO variable"
+    echo "Invalid BOOTPROTO variable"
     exit 1
 fi