#!/bin/bash
#
# Networking start script
#

# Source function library.
#. /etc/init.d/functions

RETVAL=0

umask 077

start() {
    echo -n $"Starting Networking: "
    ifconfig lo 127.0.0.1 netmask 255.255.255.0

    # Set dummy interface for Quagga OSPF
    ip addr add 10.1.0.1/32 dev dummy0
    ip link set dummy0 up

    # Set ethernet
    ifconfig eth0 192.168.254.1 netmask 255.255.255.0 broadcast 192.168.254.255 up
    
    # Set default gateway, if necessary
    #ip route add default via 192.168.254.254

    # Load Atheros Driver
    modprobe wlan
    modprobe ath_hal
    modprobe ath_pci

    # Set Wireless Settings, Master Mode
    ifconfig ath0 192.168.253.1 netmask 255.255.255.0 broadcast 192.168.253.255 up
    iwconfig ath0 mode Master
    iwconfig ath0 essid WRAP
    iwconfig ath0 channel 165

    # Set Wireless Settings, Managed Mode
    #ifconfig ath0 192.168.253.1 netmask 255.255.255.0 broadcast 192.168.253.255 up
    #iwconfig ath0 mode Managed
    #iwconfig ath0 essid WRAP

    # Set Atheros CTS/ACK Timeout    
    echo 180 > /proc/sys/dev/ath0/ctstimeout
    echo 180 > /proc/sys/dev/ath0/acktimeout
    echo 9 > /proc/sys/dev/ath0/slottime

    # Tune packet forwarding and conntrack
    echo "1" > /proc/sys/net/ipv4/ip_forward
    echo 65535 > /proc/sys/net/ipv4/ip_conntrack_max

    # Configure VLAN bridge eth0 <> ath0
    #vconfig add eth0 106
    #ifconfig eth0.106 0.0.0.0
    #ifconfig ath0 0.0.0.0
    #brctl addbr brath0
    #brctl stp brath0 off
    #brctl addif brath0 ath0
    #brctl addif brath0 eth0.106
    #ifconfig brath0 up

    # Set VLAN MAC interfaces for VLAN SNAT/DNAT
    #ebtables=/sbin/ebtables
    #$ebtables -t nat -F
    # Use MAC address of ethernet device behind eth0
    #$ebtables -t nat -A PREROUTING -i ath0 -j dnat --to-dst 00:50:8b:07:7c:a2 --dnat-target ACCEPT
    # Use MAC address of atheros module
    #$ebtables -t nat -A POSTROUTING -o ath0 -j snat --to-src 00:60:b3:23:c5:f8 --snat-target ACCEPT
									    	
    return $RETVAL
}	
stop() {
	echo -n $"Stopping Networking: "
	#ifconfig wlan0 down
	#rmmod hostap_pci
	return $RETVAL
}
restart() {
	stop
	start
}	

case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  restart|reload)
  	restart
	;;
  *)
	echo $"Usage: $0 {start|stop|restart}"
	exit 1
esac

exit $?
