Add wireless client for RED to startup process.
authorStefan Schantl <stefan.schantl@ipfire.org>
Sun, 2 Jun 2013 13:00:25 +0000 (15:00 +0200)
committerStefan Schantl <stefan.schantl@ipfire.org>
Wed, 5 Jun 2013 17:33:29 +0000 (19:33 +0200)
http://wishlist.ipfire.org/wish/wireless-client-on-red

config/rootfiles/common/i586/initscripts
lfs/initscripts
src/initscripts/init.d/networking/functions.network [new file with mode: 0755]
src/initscripts/init.d/networking/red [changed mode: 0644->0755]
src/initscripts/init.d/networking/wpa_supplicant.exe [new file with mode: 0755]
src/initscripts/init.d/wlanclient

index 1863a7a..c7546e0 100644 (file)
@@ -64,6 +64,7 @@ etc/rc.d/init.d/network-vlans
 etc/rc.d/init.d/networking/any
 etc/rc.d/init.d/networking/blue
 etc/rc.d/init.d/networking/dhcpcd.exe
+etc/rc.d/init.d/networking/functions.network
 etc/rc.d/init.d/networking/green
 etc/rc.d/init.d/networking/orange
 etc/rc.d/init.d/networking/red
@@ -94,6 +95,7 @@ etc/rc.d/init.d/networking/red.up/98-leds
 etc/rc.d/init.d/networking/red.up/99-U-dialctrl.pl
 etc/rc.d/init.d/networking/red.up/99-fireinfo
 etc/rc.d/init.d/networking/red.up/99-pakfire-update
+etc/rc.d/init.d/networking/wpa_supplicant.exe
 #etc/rc.d/init.d/nfs-server
 etc/rc.d/init.d/ntp
 #etc/rc.d/init.d/nut
@@ -151,6 +153,7 @@ etc/rc.d/rc0.d/K49cyrus-sasl
 etc/rc.d/rc0.d/K78snort
 etc/rc.d/rc0.d/K79leds
 etc/rc.d/rc0.d/K80network
+etc/rc.d/rc0.d/K82wlanclient
 #etc/rc.d/rc0.d/K84bluetooth
 #etc/rc.d/rc0.d/K85messagebus
 etc/rc.d/rc0.d/K85tmpfs
@@ -169,6 +172,7 @@ etc/rc.d/rc3.d/S12acpid
 #etc/rc.d/rc3.d/S16bluetooth
 #etc/rc.d/rc3.d/S18cpufreq
 etc/rc.d/rc3.d/S19smartenabler
+etc/rc.d/rc3.d/S19wlanclient
 etc/rc.d/rc3.d/S20network
 etc/rc.d/rc3.d/S21leds
 etc/rc.d/rc3.d/S24cyrus-sasl
@@ -199,6 +203,7 @@ etc/rc.d/rc6.d/K49cyrus-sasl
 etc/rc.d/rc6.d/K78snort
 etc/rc.d/rc6.d/K79leds
 etc/rc.d/rc6.d/K80network
+etc/rc.d/rc6.d/K82wlanclient
 #etc/rc.d/rc6.d/K84bluetooth
 #etc/rc.d/rc6.d/K85messagebus
 etc/rc.d/rc6.d/K85tmpfs
index 213b46e..6549147 100644 (file)
@@ -173,6 +173,9 @@ $(TARGET) :
        ln -sf ../init.d/localnet    /etc/rc.d/rcsysinit.d/S80localnet
        ln -sf ../init.d/sysctl      /etc/rc.d/rcsysinit.d/S90sysctl
        ln -sf ../init.d/network-vlans /etc/rc.d/rcsysinit.d/S91network-vlans
+       ln -sf ../init.d/wlanclient  /etc/rc.d/rc0.d/K82wlanclient
+       ln -sf ../init.d/wlanclient  /etc/rc.d/rc3.d/S19wlanclient
+       ln -sf ../init.d/wlanclient  /etc/rc.d/rc6.d/K82wlanclient
 
        ln -sf ../../dnsmasq /etc/rc.d/init.d/networking/red.up/05-RS-dnsmasq
        ln -sf ../../firewall /etc/rc.d/init.d/networking/red.up/20-RL-firewall
diff --git a/src/initscripts/init.d/networking/functions.network b/src/initscripts/init.d/networking/functions.network
new file mode 100755 (executable)
index 0000000..26cc65c
--- /dev/null
@@ -0,0 +1,154 @@
+#!/bin/sh
+#
+########################################################################
+# Begin
+#
+# Description : A collection of functions for the IPFire network scripts
+#
+# Authors     : IPFire Development Team <developers@ipfire.org>
+#
+# Version     : 01.00
+#
+# Notes       : 
+#
+########################################################################
+
+. /etc/sysconfig/rc
+. $rc_functions
+
+
+eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
+eval $(/usr/local/bin/readhash /var/ipfire/dns/settings)
+
+dhcpcd_get_pid() {
+               # This function returns the pid of a dhcpcd by a given
+               # network device, if a pidfile exists.
+
+               local device="$1"
+               local pidfile="/var/run/dhcpcd-${device}.pid"
+
+               # Check if a pid file exists.
+               if [ -f "${pidfile}" ] ; then
+
+                       # Get the pid from the file.
+                       local pid="$(<"${pidfile}")"
+
+                       echo "${pid}"
+               fi
+}
+
+dhcpcd_is_running() {
+       # This functions checks if a dhcpcd is running by a given pid.
+
+       local pid="$1"
+
+       # Check if a dhcpcd is running.
+       if [ -n "${pid}" -a -d "/proc/${pid}" ]; then
+               # Return "0" (True) if a dhcpcd is running.
+               return 0
+       fi
+
+       # Return 1 (False) no dhcpcd is running.
+       return 1
+}
+
+dhcpcd_start() {
+       # This function will start a dhcpcd on a speciefied device.
+
+       local device="$1"
+       local dhcp_start=""
+
+       boot_mesg -n "Starting dhcpcd on the ${device} interface..."
+
+       # Check if a dhcpcd is already running.
+       local pid="$(dhcpcd_get_pid "${device}")"
+
+       if  dhcpcd_is_running "${pid}"; then
+               boot_mesg "dhcpcd already running!" ${WARNING}
+               echo_warning
+               exit 2
+       fi
+
+       # Check if a DHCP hostname has been set.
+       if [ -n "${RED_DHCP_HOSTNAME}" ]; then
+               dhcp_start+="-h ${RED_DHCP_HOSTNAME}"
+       fi
+
+       # Start dhcpcd.
+       /sbin/dhcpcd "${device}" "${dhcp_start}" >/dev/null 2>&1
+       ret="$?"
+
+       if [ "${ret}" -eq 0 ]; then
+               . /var/ipfire/dhcpc/dhcpcd-"${device}".info
+               echo ""
+               echo_ok
+               boot_mesg "           DHCP Assigned Settings for ${device}:"
+               boot_mesg_flush
+               boot_mesg "           IP Address:      $ip_address"
+               boot_mesg_flush
+
+               if [ -n "${RED_DHCP_HOSTNAME}" ]; then
+                       boot_mesg "           Hostname:        $RED_DHCP_HOSTNAME"
+                       boot_mesg_flush
+               fi
+
+               boot_mesg "           Subnet Mask:     $subnet_mask"
+               boot_mesg_flush
+               boot_mesg "           Default Gateway: $routers"
+               boot_mesg_flush
+               boot_mesg "           DNS Server:      $domain_name_servers"
+               boot_mesg_flush
+       else
+               echo ""
+               $(exit "${ret}")
+               evaluate_retval
+       fi
+}
+
+dhcpcd_stop() {
+       # This function stops a previously started dhcpcd on a given device.
+
+       local device="$1"
+       local dhcp_stop="-k"
+       local leaseinfo="/var/ipfire/dhcpc/dhcpcd-${device}.info"
+
+       boot_mesg -n "Stopping dhcpcd on the ${device} interface..."
+
+       # Check if a dhcpcd is running.
+       local pid="$(dhcpcd_get_pid "${device}")"
+
+       if ! dhcpcd_is_running "${pid}"; then
+               boot_mesg "dhcpcd not running!" ${WARNING}
+               echo_warning
+               exit 1
+       fi
+
+       # Check if we got a valid lease.
+       if [ -e $leaseinfo ]; then
+               . $leaseinfo
+               if [ "$dchp_lease_time" = "4294967295" ]; then
+                       # do nothing, just echo ok
+                       echo_ok
+               else
+                       # Stop dhcpcd.
+                       /sbin/dhcpcd "${device}" "${dhcp_stop}" &> /dev/null
+                       ret="$?"
+
+                       # Wait until dhcpd has stopped.
+                       while [ -d "/proc/${pid}" ]; do
+                               sleep 1
+                       done
+
+                       # Display console message, depended on the exit code
+                       # of the stopped dhcpcd.
+                       if [ "${ret}" -eq 0 ]; then
+                               echo_ok
+                       elif [ "${ret}" -eq 1 ]; then
+                               boot_mesg "failed to stop dhcpcd!" ${WARNING}
+                               echo_warning
+                       else
+                               echo_failure
+                       fi
+               fi
+       fi
+}
old mode 100644 (file)
new mode 100755 (executable)
index 421c6f6..e28b2e6
@@ -17,6 +17,7 @@
 
 . /etc/sysconfig/rc 
 . ${rc_functions}
+. /etc/init.d/networking/functions.network
 
 #Define some defaults
 INET_VLAN=7
@@ -77,11 +78,6 @@ if [ "${TYPE}" == "STATIC" ]; then
        fi
 fi
 
-PIDFILE="/var/run/dhcpcd-${DEVICE}.pid"
-LEASEINFO="/var/ipfire/dhcpc/dhcpcd-${DEVICE}.info"
-DHCP_START=" "
-DHCP_STOP="-k "
-
 case "${1}" in
        start)
                if [ "${DEVICE}" != "${GREEN_DEV}" ] && [ "${DEVICE}" != "" ]; then
@@ -137,87 +133,24 @@ case "${1}" in
                        /usr/local/bin/setaliases
 
                elif [ "${TYPE}" == "DHCP" ]; then
+                       # Add firewall rules to allow comunication with the dhcp server on red.
+                       iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i ${DEVICE} -j ACCEPT
+                       iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i ${DEVICE} -j ACCEPT
 
-                       if [ -e $LEASEINFO ]; then
-                               boot_mesg -n "Stopping dhcpcd on the ${DEVICE} interface..."
-                               . $LEASEINFO
-                               if [ "$dhcp_lease_time" = "4294967295" ]; then
-                                       # do nothing, just echo ok
-                                       echo ""
-                                       echo_ok
-                               else
-                                       if [ -n "$DHCP_STOP" ]; then
-                                               /sbin/dhcpcd ${DEVICE} $DHCP_STOP &> /dev/null
-                                               RET="$?"
-                                               if [ "$RET" -eq 0 ]; then
-                                                       echo ""
-                                                       echo_ok
-                                               elif [ "$RET" -eq 1 ]; then
-                                                       boot_mesg "dhcpcd not running!" ${WARNING}
-                                                       echo_warning
-                                               else
-                                                       echo ""
-                                                       echo_failure
-                                               fi
-                                       else
-                                               echo ""
-                                               killproc dhcpcd
-                                       fi
-                               fi
-                       fi
-
-                       boot_mesg -n "Starting dhcpcd on the ${DEVICE} interface..."
                        echo -n "${DEVICE}" > /var/ipfire/red/iface
 
+                       # Check if the wlan-client is used on red.
+                       # To determine this we check if a wpa_supplicant is running.
+                       pid="$(pidof wpa_supplicant)"
+
+                       if [ -z "${pid}" ]; then
+                               # No wpa_supplicant is running. So it's save to start dhcpcd.
+                               dhcpcd_start "${DEVICE}"
+                       fi
+
                        ## Create & Enable vnstat
                        /usr/bin/vnstat -u -i ${DEVICE} -r --enable --force > /dev/null 2>&1
 
-                       # Test to see if there is a stale pid file
-                       if [ -f "$PIDFILE" ]; then
-                               ps `cat "$PIDFILE"` | grep dhcpcd > /dev/null
-                               if [ $? != 0 ]; then
-                                       rm -f /var/run/dhcpcd-${DEVICE}.pid > /dev/null
-                               else
-                                       boot_mesg "dhcpcd already running!" ${WARNING}
-                                       echo_warning
-                                       exit 2
-                               fi
-                       fi
-                       
-                       iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i ${DEVICE} -j ACCEPT
-                       iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i ${DEVICE} -j ACCEPT
-                       
-                       if [ -n "${RED_DHCP_HOSTNAME}" ]; then
-                               DHCP_START+="-h ${RED_DHCP_HOSTNAME} "
-               fi
-               
-               /sbin/dhcpcd ${DEVICE} ${DHCP_START} >/dev/null 2>&1
-               RET="$?"
-               
-               if [ "$RET" = "0" ]; then
-                               . /var/ipfire/dhcpc/dhcpcd-${DEVICE}.info
-                               echo ""
-                               echo_ok
-                               boot_mesg "           DHCP Assigned Settings for ${DEVICE}:"
-                               boot_mesg_flush
-                               boot_mesg "           IP Address:      $ip_address"
-                               boot_mesg_flush
-                               if [ -n "${RED_DHCP_HOSTNAME}" ]; then
-                                       boot_mesg "           Hostname:        $RED_DHCP_HOSTNAME"
-                                       boot_mesg_flush
-                               fi
-                               boot_mesg "           Subnet Mask:     $subnet_mask"
-                               boot_mesg_flush
-                               boot_mesg "           Default Gateway: $routers"
-                               boot_mesg_flush
-                               boot_mesg "           DNS Server:      $domain_name_servers"
-                               boot_mesg_flush
-                       else
-                               echo ""
-                               $(exit "$RET")
-                               evaluate_retval
-                       fi
-               
                elif [ "$TYPE" == "PPPOE" ]; then
 
                        if ( ps ax | grep -q [p]ppd ); then 
@@ -525,33 +458,15 @@ case "${1}" in
                        # Add a NaN value to ppp0 rrd to supress spikes at reconnect
                        rrdtool update $RRDLOG/collectd/localhost/interface/if_octets-ppp0.rrd \
                                $(date +%s):: > /dev/null 2>&1
-               fi
 
-               if [ -e $LEASEINFO ]; then
-                       boot_mesg -n "Stopping dhcpcd on the ${DEVICE} interface..."
-                       . $LEASEINFO
-                       if [ "$dchp_lease_time" = "4294967295" ]; then
-                               # do nothing, just echo ok
-                               echo ""
-                               echo_ok
-                       else
-                               if [ -n "$DHCP_STOP" ]; then
-                                       /sbin/dhcpcd ${DEVICE} $DHCP_STOP &> /dev/null
-                                       RET="$?"
-                                       if [ "$RET" -eq 0 ]; then
-                                               echo ""
-                                               echo_ok
-                                       elif [ "$RET" -eq 1 ]; then
-                                               boot_mesg "dhcpcd not running!" ${WARNING}
-                                               echo_warning
-                                       else
-                                               echo ""
-                                               echo_failure
-                                       fi
-                               else
-                                       echo ""
-                                       killproc dhcpcd
-                               fi
+               elif [ "$TYPE" == "DHCP" ]; then
+                       # Check if the wlan-client is used on red.
+                        # To determine this we check if a wpa_supplicant is running.
+                        pid="$(pidof wpa_supplicant)"
+
+                        if [ -z "${pid}" ]; then
+                               # Stop dhcpcd.
+                               dhcpcd_stop "${DEVICE}"
                        fi
                fi
 
diff --git a/src/initscripts/init.d/networking/wpa_supplicant.exe b/src/initscripts/init.d/networking/wpa_supplicant.exe
new file mode 100755 (executable)
index 0000000..b5ad8ff
--- /dev/null
@@ -0,0 +1,50 @@
+#!/bin/bash
+########################################################################
+# Begin
+#
+# Description : wpa_supplicant Script
+#
+# Authors     : IPFire Development Team <developers@ipfire.org>
+#
+# Version     : 01.00
+#
+# Notes       : This script starts/stops the dhclient if a WPA/WPA2
+#               connection to an AP successfull has been established
+#               or disconnected.
+#
+########################################################################
+. /etc/sysconfig/rc 
+. ${rc_functions}
+. /etc/init.d/networking/functions.network
+
+# Gather required information from wpa_cli.
+device="$1"
+wpa_state="$2"
+
+# Check if the RED device has been configured to use DHCP or exit immediately.
+eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
+if [ ! "${RED_TYPE}" == "DHCP" ] ; then
+       exit 0
+fi
+
+case "${wpa_state}" in
+       CONNECTED)
+               # Start dhcpcd.
+               dhcpcd_start "${device}"
+
+               exit 0
+               ;;
+
+       DISCONNECTED)
+               # Stop dhcpcd.
+               dhcpcd_stop "${device}"
+
+               exit 0
+               ;;
+
+       *)
+               # When we ever got here, there is a really big problem.
+               exit 1
+               ;;
+esac
index 80be8f9..ee24c43 100755 (executable)
@@ -240,8 +240,15 @@ function wpa_supplicant_stop() {
        boot_mesg "Stopping wireless event handler on ${RED_DEV}..."
        killproc wpa_cli
 
+       # wpa_cli does not send a disconnect event when get stopped.
+       # So we manually have to send it to the wpa_supplicant.exe.
+       /etc/rc.d/init.d/networking/wpa_supplicant.exe "${RED_DEV}" DISCONNECTED
+
        boot_mesg "Stopping wireless client on ${RED_DEV}..."
        killproc wpa_supplicant
+       
+       # Tidy up /tmp directory.
+       rm -f /tmp/wpa_ctrl_*
 }
 
 case "${1}" in