#!/bin/sh
####################################################################
## activateVAP
##
## This script is used to activate a VAP that was created earlier.
## Activation involves bringing the interface up, associating with
## a bridge, and configuring the security mode.  The VAP MUST EXIST
## prior to calling the activate script.
##
## The form of the command is
##
## activateVAP <vap> <Security> <SEC Args>
##
## Where
##      vap:    Vap ID (e.g. ath0)
## Security:    Security mode (WEP,WPA,WSC,NONE)
## Sec Args:    File containing security configuration.  For WPA this is the hostapd
##              conf file.  For WEP this is a list of iwconfig commands setting the
##              keys.
##
## Examples:
##   Open Access Point
##      activateVAP ath0 NONE
##   WPA Access Point
##      activateVAP ath1 WPA wpa2-psk.conf
##   WEP Station
##      activateVAP ath0 WEP wep.conf
##
###################################################################

. ./apcfg

if [ "${1}" = "" ]; then
    echo "activateVAP usage"
    echo "activateVAP vapid Security Security_file"
    echo
    echo "vapid: e.g. ath0"
    echo "Security: [ WPA | WEP | WSC | NONE ]"
    echo "Security_file: Name of file in /etc/ath containing security config"
    echo
    exit
fi

APNAME=$1
SECMODE=$2
SECFILE=$3

ESSID=`iwconfig ${APNAME} | grep ESSID | cut -f2 -d\"` 

##
## First, let's see if the indicated VAP exists.  If not, it must be created
##

VAPLIST=`iwconfig | grep ${APNAME} | cut -b 1-4`

if [ "${VAPLIST}" = "" ]; then
    echo "VAP ${APNAME} must be created first!! (use makeVAP)"
    exit
fi

SECLOADED=`lsmod | grep -i wlan_xauth`
if [ "${SECLOADED}" = "" ]; then
    modprobe wlan_xauth
    modprobe wlan_ccmp
    modprobe wlan_tkip
    modprobe wlan_wep
    modprobe wlan_acl
fi

if [ "${AP_REQ_MAC}" != "" ]; then
    iwconfig $APNAME ap $AP_REQ_MAC
fi

#
# Bring the interface up at this point!!
# configure bridge, or set an IP address for the WLAN interface
#

ifconfig ${APNAME} up ${WAN_IPADDR}

if [ "${SECMODE}" = "WPA" ]; then
        #
        # This is a managed (station) node
        #
        wpa_supplicant -Dwext -i ${APNAME} -c ${SECFILE} -B
fi

if [ "${SECMODE}" = "WEP" ]; then
#
# Execute the commands in SECFILE
#
        . ./${SECFILE}
fi
