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
# Load network interface card parameters
source /etc/sysconfig/network/ifconfig.${DEVICE}
+# Load DHCP client parameters
+source /etc/sysconfig/network/dhcp-client
+
+# Make sure DHCP client is available.
+if [ ! -x ${DHCP_PROG} ]; then
+ echo "Program \"${DHCP_PROG}\" is not executable."
+ exit ${EXIT_CODE_FAILURE}
+fi
+
# Check that the brctl program is present and executable.
check_brctl()
{
fi
# Check if a cable is plugged for wired interface
- if [ ! -d /sys/class/net/${DEVICE}/wireless ]; then
+ if echo "${DEVICE}" | grep -q "eth" ; then
count=0
while cat /sys/class/net/${DEVICE}/carrier | grep -q "0"; do
echo "Waiting for carrier to go up"
# Static IP address protocol
proto_static()
{
- cmd_run_log ip addr add ${IPADDR}/${PREFIX_LENGTH} dev ${DEVICE} brd + ${IFSCOPE} &&
+ cmd_run_log ip addr add ${IPADDR}/${PREFIX_LENGTH} dev ${DEVICE} \
+ brd + ${IFSCOPE} || exit ${EXIT_CODE_FAILURE}
+
+ # Bring interface up
bring_if_up
if [ -n "${GATEWAY}" ]; then
- if ip route | grep -q default; then
- msg_log "Gateway already setup; skipping."
- else
- cmd_run_log_box "Adding default route to gateway ${GATEWAY} via ${DEVICE}" \
- ip route add default via ${GATEWAY} dev ${DEVICE}
- fi
+ cmd_run_log ip route add default via ${GATEWAY} || \
+ exit ${EXIT_CODE_FAILURE}
fi
}
# Obtain IP address from DHCP
proto_dhcp()
{
+ # Bring interface up
bring_if_up
- # Load DHCP client parameters
- source /etc/sysconfig/network/dhcp-client
-
- # Make sure no file named `/var/run/dhclient.pid' remains.
- if [ -f "/var/run/dhclient.pid" ]; then
- rm -f /var/run/dhclient.pid
- fi
-
- if [ ! -x "${DHCP_PROG}" ]; then
- echo "Program \"${DHCP_PROG}\" is not executable."
- exit ${EXIT_CODE_FAILURE}
- fi
-
cmd_run_log ${DHCP_PROG} ${DHCP_START} ${DEVICE}
}
# 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
-
- # 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
+ # Check if a socket exists, meaning wpa_supplicant is running
+ if [ -S /var/run/wpa_supplicant/${DEVICE} ]; then
+ echo "wpa_supplicant already running"
- 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
- 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"
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
elif [ x${BOOTPROTO} = "xpppoe" ]; then
cmd_run_log pppoe-start
else
- echo "Invalid or absent BOOTPROTO variable"
+ echo "Invalid BOOTPROTO variable"
exit 1
fi