#!/bin/sh
####################################################################
## killVAP
##
## This script is used to destroy a VAP, or if you want complete
## destruction, specify all.  Using the all option will also unload
## the wlan modules.
##
## The form of the command is
##
## makeVAP <VAP>
##
## Where VAP is the name of the VAP (e.g. ath0).  Specifying "ALL"
## for the VAP will cause all VAPs to be removed, and the module unload
## script to be executed.
##
## Examples:
##      killVAP ath1
##      killVAP all
##
###################################################################
apdownmulti() {
#
#check if we have multiple vaps configured with VLAN support, in such a case we
#need to remove the bridges also that are created as part of configuration
#For this we depend on the information in ifconfig. If vlans are configured
#we will have them listed in ifconfig.
#
ifconfig -a | grep 'eth0' | grep '\.'
if [ $? ]; then
    pidlist=`ps | grep 'hostapd' | cut -b1-5`
    for j in $pidlist
    do
        kill -9 $j
    done
    sleep 2

    pidlist=`ps | grep 'wpa_supplicant' | cut -b1-5`
    for j in $pidlist
    do
        kill -9 $j
    done
    sleep 2

	##
	## Bring down VLAN bridges
	## this loop only affects the VLANs and their associated bridges
	##

    bridges=`brctl show | grep -v 'bridge name' |  sed -e 's/br0//g' | cut -f1`
    for i in $bridges
    do
        athiftag=`brctl show | grep -v 'bridge name' | grep $i | cut -f6 `
        tag=`echo $athiftag | cut -f2 -d\.`
        ifname=`echo $athiftag|cut -f1 -d\.`

		##
		## Down the interfaces on the bridge
		##

        ifconfig $athiftag down
        ifconfig eth0.$tag down
        ifconfig eth1.$tag down
        ifconfig $i down

		##
		## Remove the interfaces from the bridge
		##

        brctl delif $i $athiftag
        brctl delif $i eth0.$tag
        brctl delif $i eth1.$tag
        #delete the briges last
        brctl delbr $i

		##
		## Remove the VLANs
		##

        vconfig rem $athiftag
        vconfig rem eth0.$tag
        vconfig rem eth1.$tag
        sleep 1
    done
	##
	##  Get the list of VAPs
	##

    VAPLIST=`iwconfig | grep ath | cut -f1 -d' '`

    ##
    ## ALL VAPs must be down before ANY can be destroyed
    ## Do this in two stages
	##

    for i in $VAPLIST
    do
        echo "downing $i"
        ifconfig $i down
    done

    sleep 1
    for i in $VAPLIST
    do
        echo "killing $i"
        wlanconfig $i destroy
    done

    sleep 1
	##
	## Reconstruct the original configuration
	##

    sleep 1

    ifconfig $LAN_IF up 0.0.0.0
    ifconfig $WAN_IF up 0.0.0.0

    /etc/rc.d/rc.network
    /etc/rc.d/rc.bridge
fi

}

. /etc/ath/apcfg

if [ "${1}" = "" ]; then
    echo "    killVAP usage"
    echo "    killVAP [VAP | ALL]"
    exit
fi

##
## SPE-profing
## Check to see if killVAP is already running.  If so, exit, because you should only
## be doing one at a time
##

SPEPROOF=`ps | grep -c killVAP`
if [ $SPEPROOF -gt 12 ]; then
   ps
   echo "Too much killing!!  Give peace a chance!!"
   exit
fi

##
## If the modules are already unloaded, we don't need to do anything
##

MODLIST=`lsmod | grep ath_hal`

if [ "${MODLIST}" = "" ]; then
   echo "Modules already unloaded"
   exit
fi

BRNAME=br0
##
## For VLANs bringing down the vaps is different. We should check
## vlans before. 
##
ifconfig -a | grep eth0 | grep '\.'
ISVALN=$?
APVLANMODE=${AP_VLAN_MODE:="0"}
if [ "$ISVALN" = "0" -a "$APVLANMODE" = "0" ]; then
    apdownmulti
else

    ##
    ## Get the name of the bridge. WE ARE ASSUMING ONLY ONE FOR NOW
    ##

    BRNAME=`brctl show | grep -v bridge | cut -b 1-4`

    ##
    ## Check for a kill all
    ##

    if [ "${1}" = "ALL" -o "${1}" = "all" ]; then
        #
        # List all VAPs
        #

        VAPLIST=`iwconfig | grep ath | cut -f1 -d' '`

        if [ "${VAPLIST}" != "" ]; then

            #
            # Do the same for all instances of hostapd, wpa_supplicant, and wsc
            #

            HOSTAPDLIST=`ps | grep hostapd | cut -b 1-5`
            if [ "${HOSTAPDLIST}" != "" ]; then
                for i in $HOSTAPDLIST ; do
                    echo "killing $i"
                    kill -9 $i
                done
            fi

            SUPPLIST=`ps | grep wpa_supplicant | cut -b 1-5`
            if [ "${SUPPLIST}" != "" ]; then
                for i in $SUPPLIST ; do
                    echo "killing $i"
                     kill -9 $i
                 done
            fi
			sleep 4

            for i in $VAPLIST
            do

                brctl delif $BRNAME $i

                #
                # Bring the interface down
                #
                ifconfig $i down
            done

            for i in $VAPLIST
            do
                echo "killing $i"
                wlanconfig $i destroy
            done
        fi

        #
        # Add the arping command to ensure all nodes are updated on the network!
        #

        arping -U -c 1 -I $BRNAME $AP_IPADDR


    else

        # Remove from Bridge

        brctl delif $BRNAME $1
        sleep 2

        #
        # Bring the interface down
        #

        ifconfig $1 down
        sleep 1
        echo "killing $1"
        wlanconfig $1 destroy

        #
        # Add the arping command to ensure all nodes are updated on the network!
        #

        arping -U -c 1 -I $BRNAME $AP_IPADDR

        #
        # If this is ath0, check for wsc, and kill it if it exists
        #

        if [ "${1}" = "ath0" ]; then
            WSCLIST=`ps | grep wsc | cut -b 1-5`
            if [ "${WSCLIST}" != "" ]; then
                for i in $WSCLIST ; do
                    echo "killing $i"
                    kill -9 $i
                done
            fi
        fi

        #
        # Check for hostapd or supplicant with a filename with the AP name in it
        #

        HOSTAPDLIST=`ps | grep sec$1 | cut -b 1-5`
        if [ "${HOSTAPDLIST}" != "" ]; then
            for i in $HOSTAPDLIST ; do
                echo "killing $i"
                kill -9 $i
            done
        fi

        SUPPLIST=`ps | grep sup$1 | cut -b 1-5`
        if [ "${SUPPLIST}" != "" ]; then
            for i in $SUPPLIST ; do
                echo "killing $i"
                kill -9 $i
            done
        fi
    fi
fi 

#
# Remove files in /tmp
#

rm -rf /tmp/br*
rm -rf /tmp/ap*
rm -rf /tmp/sta*
rm -rf /tmp/top*
rm -rf /tmp/conf*
rm -rf /tmp/sta_conf*
