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

. /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 3 ]; 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

##
## Get the name of the bridge. WE ARE ASSUMING ONLY ONE FOR NOW.
##
#BRNAME=`brctl show | grep -v bridge | cut -b 1-4`
# brctl show does not work!
BRNAME=br0

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

            #
            # Remove from Bridge
            #

            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

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

    #
    # 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 
#
# Add the arping command to ensure all nodes are updated on the network!
#

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


