#!/bin/sh
#        
# WDS configuration script, WDS needs to be activated after bridge
#                                                                
[ "$IF_WIRELESS_TYPE" != "broadcom" ] && exit 0
[ "$IF_WIRELESS_MODE" != "wds" ] && exit 0
logger -t wireless "setting lazywds to $IF_WIRELESS_LAZYWDS"
[ "$IF_WIRELESS_LAZYWDS" ] && wl lazywds $IF_WIRELESS_LAZYWDS
logger -t wireless "adding wds nodes $IF_WIRELESS_WDS"
wl wds $IF_WIRELESS_WDS
[ "$IF_WIRELESS_SECURITY" = "wpa-psk" ] && {
  # wpa rekeying
  gtk_rekey=$IF_WIRELESS_WPA_GTK_REKEY
  gtk_rekey=${gtk_rekey#0}
  gtk_rekey=${gtk_rekey:-3600}
  case "$IF_WIRELESS_ENCRYPTION" in
  	wep)
  		crypto_num=1
  		;;
  	tkip)
  		crypto_num=2
  		;;
  	aes)
  		crypto_num=4
  		;;
  	aes+tkip)
  		crypto_num=6
  		;;
  esac
  case "$IF_WIRELESS_AUTHORIZATION" in
  	psk)
  		auth=4
  		;;
  	psk2)
  		auth=128
  		;;
  	"psk psk2")
  		auth=132
  		;;
  esac
  wl ssid $IF_WIRELESS_SSID

  # backwards compatibility hack for 1.0-stabke
  [ -z "$IF_WIRELESS_WDS_INTERFACE" ] && IF_WIRELESS_WDS_INTERFACE=$IF_BRIDGE_IFACES

  /usr/sbin/nas -P /var/run/nas.lan.pid -l $IF_WIRELESS_WDS_BRIDGE -H 34954 -i $IF_WIRELESS_WDS_INTERFACE -A -m $auth -k "$IF_WIRELESS_WPA_KEY" -s $IF_WIRELESS_SSID -w $crypto_num -g $gtk_rekey &
  [ "$IF_WIRELESS_WDS_SECURITY" = "wpa-psk" ] && {
  	sleep 10 && 
	for i in $(ip link show|awk -F : '/wds/ {print $2}'); do /usr/sbin/nas4not lan $i up auto $IF_WIRELESS_WDS_ENCRYPTION psk "$IF_WIRELESS_WDS_WPA_KEY" $IF_WIRELESS_SSID;done &
  }
}
exit 0
