#!/bin/sh

case "$METHOD" in loopback) exit 0 ;; esac

#
# This script takes care of bringing up wlan-ng device.
# It is run by ifup, and gets information from the 
# /etc/network/interfaces file. It is derived from the
# init.d/wlan script in the wlan-ng distribution 

WLANCTL=/sbin/wlanctl-ng
PRIV_GENERATOR=/sbin/nwepgen

if [ ! -x $WLANCTL ] ; then 
	exit 0
fi

# Detect and act only on linux-wlan-ng interfaces
if [ "$IF_WIRELESS_TYPE" != "wlan-ng" ]; then
  exit 0
fi

# Create a lock file; remove on script exit. This is a fairly crummy lock,
# but all it needs to do is prevent hotplug from calling this script,
# which may happen when the module is loaded below. Since it should only
# happen when the module is loaded, I don't need a proper lock.
if [ -e /var/lock/wlan-$IFACE.lock ]; then
	exit 0
fi
touch /var/lock/wlan-$IFACE.lock
cleanup () {
	rm -f /var/lock/wlan-$IFACE.lock
}
trap cleanup 0

if [ -z "$IF_WIRELESS_MODE" ]; then
	IF_WIRELESS_MODE="ad_hoc"
fi

if [ -z "$IF_WLAN_NG_HOSTWEP" ]; then
	IF_WLAN_NG_HOSTWEP=false
fi

if [ -z "$IF_WLAN_NG_DEFAULT_KEY_ID" ]; then
	IF_WLAN_NG_DEFAULT_KEY_ID=0
fi

if [ -z "$IF_WLAN_NG_EXCLUDE_UNENCRYPTED" ]; then
	IF_WLAN_NG_EXCLUDE_UNENCRYPTED=true
fi

if [ -z "$IF_WLAN_NG_PRIV_KEY128" ]; then
	IF_WLAN_NG_PRIV_KEY128=false
fi

if [ -z "$IF_WLAN_NG_BCINT" ]; then
	IF_WLAN_NG_BCINT=100
fi

if [ -z "$IF_WLAN_NG_BASICRATES" ]; then
	IF_WLAN_NG_BASICRATES="2 4"
fi

if [ -z "$IF_WLAN_NG_OPRATES" ]; then
	IF_WLAN_NG_OPRATES="2 4 11 22"
fi

if [ -z "$IF_WLAN_NG_AUTHTYPE" ]; then
	IF_WLAN_NG_AUTHTYPE="opensystem"
fi

if [ -z "$IF_WIRELESS_CHANNEL" ]; then
	IF_WIRELESS_CHANNEL=1
fi

if ! modprobe p80211; then
	echo "Failed to load p80211.o." >&2
	exit 1
fi

# NOTE: We don't explicitly insmod the card driver here.  The
#  best thing to do is to specify an alias in /etc/modules.conf.
#  Then, the first time we call wlanctl with the named device,
#  the kernel module autoload stuff will take over.
#  But, if you prefer, you could modprobe it here.


# Bring the card up into an operable state.
result=`$WLANCTL $IFACE lnxreq_ifstate ifstate=enable`
if [ $? = 0 ] ; then
	eval $result
	if [ $resultcode != "success" ]; then
		echo "Failed to enable the device, resultcode=" $resultcode "." >&2
		exit 1
	fi
else
	echo "Failed to enable the device, exitcode=" $? "." >&2
	exit 1
fi

# Set user-specified MIB items.
for i in $IF_WLAN_NG_USER_MIBS; do
	result=`$WLANCTL $IFACE dot11req_mibset "mibattribute=$i"`

	if [ $? = 0 ] ; then
		eval $result
		if [ $resultcode != "success" ] ; then
			echo "Failed to set user MIB $i." >&2
			exit 1
		fi
	else
		echo "Failed to set user MIB $i." >&2
		exit 1
	fi
done

if [ "$IF_WIRELESS_ENC" = "on" ]; then
	# Set up WEP.
	result=`$WLANCTL $IFACE dot11req_mibget mibattribute=dot11PrivacyOptionImplemented`
	if [ $? = 0 ] ; then
		eval $result
		eval $mibattribute
	else
		echo "mibget failed." >&2
		exit 1
	fi
	
	if [ "$dot11PrivacyOptionImplemented" != "true" ]; then
		echo "Cannot enable privacy, dot11PrivacyOptionImplemented=$dot11PrivacyOptionImplemented." >&2
		exit 1
	fi

	$WLANCTL $IFACE lnxreq_hostwep decrypt=$IF_WLAN_NG_HOSTWEP encrypt=$IF_WLAN_NG_HOSTWEP >/dev/null
	
	$WLANCTL $IFACE dot11req_mibset \
		mibattribute=dot11WEPDefaultKeyID=$IF_WLAN_NG_DEFAULT_KEY_ID \
             >/dev/null
	
	$WLANCTL $IFACE dot11req_mibset \
		mibattribute=dot11ExcludeUnencrypted=$IF_WLAN_NG_EXCLUDE_UNENCRYPTED \
            >/dev/null

	$WLANCTL $IFACE dot11req_mibset \
		mibattribute=dot11PrivacyInvoked=true >/dev/null

	if [ -e "$IF_WLAN_NG_KEYFILE" ]; then
		IF_WLAN_NG_PRIV_GENSTR=`cat $IF_WLAN_NG_KEYFILE`
	fi
	if [ ! -z "$IF_WLAN_NG_PRIV_GENSTR" ]; then
		if [ "$IF_WLAN_NG_PRIV_KEY128" = "false" ]; then
			keys=`$PRIV_GENERATOR "$IF_WLAN_NG_PRIV_GENSTR" 5`
		else
			keys=`$PRIV_GENERATOR "$IF_WLAN_NG_PRIV_GENSTR" 13`
		fi

		knum=0
		for i in $keys; do
			$WLANCTL $IFACE dot11req_mibset \
				mibattribute=dot11WEPDefaultKey$knum=$i \
                             >/dev/null
			knum=$(expr $knum + 1)
		done
	else
		$WLANCTL $IFACE dot11req_mibset \
			mibattribute=dot11WEPDefaultKey0=$IF_WLAN_NG_KEY0 \
		     >/dev/null
		$WLANCTL $IFACE dot11req_mibset \
			mibattribute=dot11WEPDefaultKey1=$IF_WLAN_NG_KEY1 \
		     >/dev/null
		$WLANCTL $IFACE dot11req_mibset \
			mibattribute=dot11WEPDefaultKey2=$IF_WLAN_NG_KEY2 \
		     >/dev/null
		$WLANCTL $IFACE dot11req_mibset \
			mibattribute=dot11WEPDefaultKey3=$IF_WLAN_NG_KEY3 \
		     >/dev/null
	fi
fi
	
# MAC startup
if [ "$IF_WIRELESS_MODE" = "ad_hoc" ] || 
   [ "$IF_WIRELESS_MODE" = "ad-hoc" ]; then
	startcmd="$WLANCTL $IFACE dot11req_start "
	startcmd="$startcmd ssid=$IF_WIRELESS_ESSID"
	startcmd="$startcmd bsstype=independent"
	startcmd="$startcmd beaconperiod=$IF_WLAN_NG_BCINT"
	startcmd="$startcmd dtimperiod=3"
	startcmd="$startcmd cfpollable=false"
	startcmd="$startcmd cfpollreq=false"
	startcmd="$startcmd cfpperiod=3"
	startcmd="$startcmd cfpmaxduration=100"
	startcmd="$startcmd probedelay=100"
	startcmd="$startcmd dschannel=$IF_WIRELESS_CHANNEL"
	j=1
	for i in $IF_WLAN_NG_BASICRATES; do
		startcmd="$startcmd basicrate$j=$i"
		j=$(expr $j + 1)
	done
	j=1
	for i in $IF_WLAN_NG_OPRATES; do
		startcmd="$startcmd operationalrate$j=$i"
		j=$(expr $j + 1)
	done

	results=`$startcmd`
	if [ $? = 0 ]; then 
		eval $results
		if [ $resultcode != "success" ] ; then
			echo "IBSS not started, resultcode=$resultcode" >&2
			exit 1
		fi
	else
		echo FAILED: $startcmd >&2
		exit 1
	fi
else
	# Infrastructure mode.
	results=`$WLANCTL $IFACE lnxreq_autojoin \
		"ssid=$IF_WIRELESS_ESSID" \
		authtype=$IF_WLAN_NG_AUTHTYPE | sed 's/\([^=]*\)=\(.*\)/\1="\2"/'`
	if [ $? = 0 ]; then
		eval $results
		if [ ${resultcode:-"failure"} != "success" ]; then
			echo 'error: Autojoin indicated failure!' >&2
			exit 1
		fi
	fi
fi