#!/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 5

    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\.`
        #before removing any bridges, bring down all the interfaces and kill
        #hostapd first

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

        #bring down the interfaces second
        ifconfig $athiftag down
        ifconfig eth0.$tag down
        ifconfig eth1.$tag down
        ifconfig $i down
        #remove interfaces third
        brctl delif $i $athiftag
        brctl delif $i eth0.$tag
        brctl delif $i eth1.$tag
        #delete the briges last
        brctl delbr $i
        vconfig rem $athiftag
        vconfig rem eth0.$tag
        vconfig rem eth1.$tag
        sleep 1
        wlanconfig $ifname destroy
        sleep 1

    done

    sleep 3
    /etc/rc.d/rc.wlan down
    sleep 1
    ip_addr=`ifconfig eth0 | grep 'inet addr' | cut -f2 -d':' | cut -f1 -d' '`
    if [ "${ip_addr}" = "" ]; then
        #LAN port would have the address
        ip_addr=`ifconfig eth1 | grep 'inet addr' | cut -f2 -d':' | cut -f1 -d' '`
    fi
    ifconfig eth0 0.0.0.0 up
    ifconfig eth1 0.0.0.0 up
    brctl addif br0 eth0
    brctl addif br0 eth1
    ifconfig br0 $ip_addr up
    BRNAME=br0
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 6 ]; 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

##
## 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 -b 1-4`

        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

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

				if [ "$APVLANMODE" = "1" ]
				then
					vapvlanif="$i.1"
					checkint=`ifconfig | grep $vapvlanif | grep -v grep`
					ifvalid=${checkint:="0"}

					if [ "$ifvalid" != "0" ] 
					then  
						brctl delif $BRNAME $vapvlanif
						ifconfig $vapvlanif down
						vconfig rem $vapvlanif
					fi
				else
					#
					# Remove from Bridge
					#
					brctl delif $BRNAME $i
				fi

                #
                # 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


        #
        # Finally, unload all modules
        #
        sleep 3
        /etc/rc.d/rc.wlan down

    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*
