#Network options
HAVE_IP="yes"				#Does this machine have an IP-Address? (Needed for routing)
MYIP="192.168.100.17"
NETMASK="255.255.255.0"
GATEWAY="192.168.100.20"

#MODE="routing"				#routing/bridging
MODE="bridging"

#Config for bridging
STP="off"				#Spanning tree protocol

#Config for routing
WLAN_IP="192.168.105.1"
WLAN_NM="255.255.255.0"
FW_RULES="iptables -P FORWARD DROP
iptables -A FORWARD -p tcp -s 192.168.105.0/24 -d 0/0 --dport 80 -j ACCEPT
iptables -A FORWARD -p tcp -s 192.168.105.0/24 -d 0/0 --dport 22 -j ACCEPT
iptables -A FORWARD -p tcp -s 192.168.105.0/24 -d 0/0 --dport 53 -j ACCEPT
iptables -A FORWARD -p udp -s 192.168.105.0/24 -d 0/0 --dport 53 -j ACCEPT
iptables -A FORWARD -p tcp -s 0/0 -d 192.168.105.0/24 -j ACCEPT
iptables -A FORWARD -p udp -s 0/0 -d 192.168.105.0/24 -j ACCEPT
iptables -A FORWARD -p icmp -s 0/0 -d 0/0 -j ACCEPT"

#Wireless config
ESSID="public"				#Also called "network name"
CHANNEL="1"
WEP_KEY="off"
TXPOWER="20"				#20=maximum/-1=do not touch

#Antenna selection
# 0 = do not touch TX/RX AntSel, i.e., use card default (default)
# 1 = use antenna diversity
# 2 = force TX/RX AntSel pin low
# 3 = force TX/RX AntSel pin high

ANTSEL_TX="2"
ANTSEL_RX="2"

#WDS - To be implemented

#########################################
###### Implementation follows here ######
#########################################

if [ "$HAVE_IP" != "yes" -o "$MYIP" = "" ]; then
  MYIP="0.0.0.0"
fi

ifconfig eth0 "$MYIP" netmask "$NETMASK" up
ifconfig ${WLDEV} 0.0.0.0 up
iwconfig ${WLDEV} channel "$CHANNEL"
iwconfig ${WLDEV} essid "$ESSID"

if [ "$CARDTYPE" = "prism" ]; then
  if [ "$TXPOWER" != "-1" ]; then
    iwconfig ${WLDEV} txpower fixed
    iwconfig ${WLDEV} txpower "$TXPOWER"
  fi
  prism2_param ${WLDEV} antsel_tx "$ANTSEL_TX"
  prism2_param ${WLDEV} antsel_rx "$ANTSEL_RX"
fi

iwconfig ${WLDEV} enc "$WEP_KEY"

if [ "$MODE" = "routing" ]; then
  ifconfig eth0 "$MYIP" netmask "$NETMASK" up
  ifconfig ${WLDEV} "$WLAN_IP" netmask "$WLAN_NM" up
  echo 1 > /proc/sys/net/ipv4/ip_forward
  if [ "$FW_RULES" != "" ]; then
    OLDIFS="$IFS"
    IFS="`echo`"
    for i in $FW_RULES; do
      eval $i
    done
    IFS="$OLDIFS"
  fi
  iptables -L
else
  # Bridging
  echo "Adding bridge..."
  brctl addbr br0
  echo "Switching spanning tree $STP"
  brctl stp br0 "$STP"
  #This is ugly
  echo "Setting IP of br0 to 0.0.0.0 (needed for bridging on nfsroot)"
  ifconfig br0 0.0.0.0
  echo "Adding eth0 to bridge"
  brctl addif br0 eth0
  ifconfig ${WLDEV} 0.0.0.0 up
  if [ "$HAVE_IP" = "yes" ]; then
    ifconfig br0 "$MYIP" netmask "$NETMASK" up
    echo "Zeroing eth0 IP"
    ifconfig eth0 0.0.0.0 up
  else
    ifconfig eth0 0.0.0.0 up
    ifconfig br0 0.0.0.0 up
  fi
  echo "Adding ${WLDEV} to bridge"
  brctl addif br0 ${WLDEV}
fi

if [ "$GATEWAY" != "" ]; then
  route add default gw "$GATEWAY"
fi
