#!/bin/sh
interface=$1
tty=$2
speed=$3
localIP=$4
remoteIP=$5
echo "PPP started at $(date): 
interface = $interface; tty = $tty; speed = $speed; 
Local IP = $localIP; Remote IP = $remoteIP 
" >/dev/console
echo "Insert static route to routing table.." > /dev/console
/var/staticRoute

#When wan is set to PPPOE or L2TP or PPTP, we only allow the PPP0 to communicate with outside
#so I deny all inbound packets from eth2.2
lan_ip=`nvram_get 2860 lan_ipaddr`
lan_nm=`nvram_get 2860 lan_netmask`
wan_ip=`nvram_get 2860 wan_ipaddr`
wan_nm=`nvram_get 2860 wan_netmask`
wan_gw=`nvram_get 2860 wan_gateway`
wanMode=`/bin/nvram_get 2860 wanConnectionMode`
wan_1="eth2.2"
wan_2="ppp0"

#Jacky.Yang 30-Jul-2009, Begin - Drop NAT behavior and move to firewall.c
#Jacky.Yang 30-Jul-2009, End - Drop NAT behavior and move to firewall.c

#David Lin, 25/May/2009, This is for special case that the pptp/l2tp server ip is as the same as the gateway of ppp0.
#That may casue PPTP/L2TP connection fail.So we fixed it as below:
if [ "$wanMode" == "PPTP" -o "$wanMode" == "L2TP" -o "$wanMode" == "RussiaPPTP" -o "$wanMode" == "RussiaL2TP" ]; then
	ppp_def_gw=$remoteIP
	default_gw_count=`route -n | grep ^0.0.0.0 | grep -c $ppp_def_gw`
	if [ "$default_gw_count" -gt "1" ]; then
                route del -net $ppp_def_gw netmask 255.255.255.255 dev $wan_2
                route add -net $ppp_def_gw netmask 255.255.255.255 dev $wan_1
		route del default dev $wan_1
	fi
fi

#2009.10.30 Recover Rick Cao before setting ,for insert iptable rule when Wan mode is PPP.
if [ "$wanMode" = "PPPOE" -o "$wanMode" = "L2TP" -o "$wanMode" = "PPTP" ]; then
	iptables -I PREROUTING -t nat -i eth2.2 -s 0.0.0.0/0 -d ${lan_ip}/${lan_nm} -j DROP
	iptables -I PREROUTING -t nat -i eth2.2 -p udp --sport 53 -s 0.0.0.0/0 -d ${lan_ip}/${lan_nm} -j ACCEPT
fi

#David Lin, 25/May/2009
	
cat /etc/resolv.conf /etc/wan_resolv.conf > /etc/temp_resolv.conf
mv /etc/temp_resolv.conf /etc/resolv.conf

#Jacky.Yang 27-Feb-2009, support manually configure dns server
manuallyDNS=`nvram_get 2860 manuallyDNS`
if [ "$manuallyDNS" = "1" ]; then
	wan_primary_dns=`nvram_get 2860 wan_primary_dns`
	wan_secondary_dns=`nvram_get 2860 wan_secondary_dns`
	config-dns.sh $wan_primary_dns $wan_secondary_dns
fi


#when the ppp connected that need firewall_init()
send_event firewall_reload

#Tom.Hung 2009-11-19, add for GUI verify ppp is on or down
echo 1 > /var/ppp_status
