#!/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> <BR> <Security> <SEC Args> <Guest Zone> <WSC>  <VAP_TIE>
##
## Where
##      vap:    Vap ID (e.g. ath0)
##       BR:    Bridge to join (or - if not bridged)
## 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 br0 NONE
##   WPA Access Point
##      activateVAP ath1 br0 WPA wpa2-psk.conf
##   WEP Station
##      activateVAP ath0 br0 WEP wep.conf
##
###################################################################

. /etc/ath/apcfg

if [ "${1}" = "" ]; then
    echo "activateVAP usage"
    echo "activateVAP VAPid:Radio bridge Security Security_file"
    echo
    echo "vapid: e.g. ath0"
    echo "bridge:  Name of bridge to add to,(typically br0)"
    echo "Security: [ WPA | WEP | WSC | NONE ]"
    echo "Security_file: Name of file in /etc/ath containing security config"
    echo
    exit
fi

BRIDGE=$2
SECMODE=$3
SECFILE=$4
#NickChou add
GUESTZONE=$5
WSCMODE=$6
VAPTIE=$7

APNAME=`echo $1 | cut -b 1-4`
RADIO=`echo $1 | cut -b 6-6`

if [ "$RADIO" = "" ]; then
    RADIO="0"
fi

KVER=`uname -r | cut -f 1 -d '-'`
MODULE_PATH=/lib/modules/$KVER/net
#ESSID=`iwconfig ${APNAME} | grep ESSID | cut -f2 -d\"` 
#MODE=`iwconfig ${APNAME} | grep "Mode:Master"`

##
## Create an AP index, based on the VAP (ath) number
##

#APINDEX=`echo ${APNAME}| cut -b 4-4`

#if [ "$APINDEX" != "0" ]; then
#    APINDEX=`expr ${APINDEX} + 1`
#fi

#NickChou Add, parse ath0 to 0
#APNAME_INDEX=`ifconfig ${APNAME} | grep ${APNAME} | cut -c 4`

##
## First, let us 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

##
## Must determine if the scan modules need to be loaded.  Remember, only once!
## This is in station mode if the MODE value is blank
##

CHANNELSCAN=`lsmod | grep wlan_scan_ap`

#if [ "${MODE}" != "" -a "${CHANNELSCAN}" = "" ]; then
if [ "${CHANNELSCAN}" = "" ]; then
    insmod $MODULE_PATH/wlan_scan_ap.ko
fi

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

if [ "${BRIDGE}" != "none" -a "${BRIDGE}" != "-" ]; then
    ifconfig ${APNAME} up
    brctl addif ${BRIDGE} ${APNAME}
    #echo -e "\tinterface ${APNAME}" >> /var/tmp/ath/br0
    #
    # Add the arping command to ensure all nodes are updated on the network!
    #
    
    ##NickChou comment out, move to wlan_ath_ap94.c
    #arping -U -c 1 -I ${BRIDGE} $AP_IPADDR

else
    ifconfig ${APNAME} up ${WAN_IPADDR}
fi

#NickChou add for wireless guest zone
if [ "${GUESTZONE}" = "1" ]; then
	#echo "NickChou: activateVAP: ${APNAME} is guest zone"
	brctl setguestzone ${BRIDGE} ${APNAME}
fi

#
# We need to determine if WSC is enabled or not.  If not, we do the standard "stuff"
#

if [ "${WSCMODE}" = "1" ]; then
	echo ">>>>> WPS ENABLED, ${SECFILE}"
    if [ "${SECFILE}" = "EAP" ]; then
        echo "Cannot use EAP modes with WPS"
        exit 255
    fi
fi

if [ "${SECMODE}" = "WEP" ]; then
    #
    # Insert the keys as required
    #
    if [ "${AP_WEP_MODE}" != "" -a "${AP_WEP_MODE}" != "1" ]; then
        iwpriv ${APNAME} authmode ${AP_WEP_MODE}
    fi
    if [ "${WEPKEY_1}" != "" ]; then
        iwconfig ${APNAME} enc ${WEPKEY_1} [1]
    fi
    if [ "${WEPKEY_2}" != "" ]; then
        iwconfig ${APNAME} enc ${WEPKEY_2} [2]
    fi
    if [ "${WEPKEY_3}" != "" ]; then
        iwconfig ${APNAME} enc ${WEPKEY_3} [3]
    fi
    if [ "${WEPKEY_4}" != "" ]; then
        iwconfig ${APNAME} enc ${WEPKEY_4} [4]
    fi
    if [ "${AP_PRIMARY_KEY}" != "" ]; then
        iwconfig ${APNAME} enc [${AP_PRIMARY_KEY}]
    fi
fi
