#!/bin/sh
interface=$1
tty=$2
speed=$3
localIP=$4
remoteIP=$5
echo "The PPP interface going UP" > /dev/console
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 or 3G, we only allow the PPP0 to communicate with outside
#so I deny all inbound packets from eth2.2
wanMode=`nvram_get 2860 wanConnectionMode`
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`
wan_1="eth2.2"
wan_2="ppp0"

#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


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

#Ricky CAO 2010-01-04, add for re-initiate ddns client to force update IP.
DDNS_PROVIDER=`nvram_get 2860 DDNSProvider`
if [ ! $DDNS_PROVIDER = "none" -a ! $DDNS_PROVIDER = "" ]; then
	echo "re-initiate dynamic DNS daemon" > /dev/console
	ddns.sh

	#Since inadyn seems be terminated before it finish to initiate on sometime
	#So, confirm it is finish to run on background before do another procedure.
	#Ricky CAO
	INADYN_PID=`ps | grep inadyn | grep -v "grep" | awk '{print $1}'`
	while [ $INADYN_PID = "" ]
	do
		sleep 1
		INADYN_PID=`ps | grep inadyn | grep -v "grep" | awk '{print $1}'`
	done
fi


#Jacky.Yang 8-May-2008, when the ppp connected that need firewall_init()
send_event firewall_reload

#Expose the ppp interface start time
echo "===== PPP interface  UP  =====" >> /tmp/ppp_time_info
date | awk '{print $4}' >> /tmp/ppp_time_info

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