#!/bin/sh
echo "The PPP interface going DOWN" > /dev/console
#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 after ppp0 is up, 
#but after ppp0 become to down, we need allow all inbound packets from eth2.2 again for general operation

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`
wanMode=`/bin/nvram_get 2860 wanConnectionMode`
gw=`nvram_get 2860 wan_gateway`
wan_1="eth2.2"
wan_2="ppp0"

#Add for recove the default after PPP disconnec - U-Media Ricky Cao on May 8 2008
if [ "$wanMode" = "RussiaPPTP" -a "$pptpMode" = "0" ]; then
	route add default gw $gw
elif [ "$wanMode" = "RussiaPPTP" -a "$pptpMode" = "1" ]; then
	#This file is generate when get IP information from DHCP before PPP session active
	var/addDefaultRoute
elif [ "$wanMode" = "L2TP" -o "$wanMode" = "RussiaL2TP" ]; then
	#L2TP will bring up another pppd daemon when ppp interface going UP.
	#So I need kill all pppd after ppp0 going DOWN otherwise system will appear duplicate pppd daemon
	#Ricky Cao on Oct. 30 2008
	killall -9 pppd
else
	:
fi
#2009.10.30 Recover Rick Cao before setting ,for delete iptable rule when Wan mode is PPP.
if [ "$wanMode" = "L2TP" -o "$wanMode" = "PPTP" ]; then
#2010.01.07 Joan.Huang End
	iptables -D 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, when PPP down, delete all iptable rule of about PPP. 
#2010.01.07 Joan.Huang disable becasue the delete rule already change,and when wan ip chang or wan mode change firewall will initialization
#iptables -t nat -D POSTROUTING -s $lan_ip/$lan_nm -o $wan_2 -j MASQUERADE
#iptables -t nat -D POSTROUTING -s $lan_ip/$lan_nm -o ! $wan_2 -j DROP
#iptables -t nat -D POSTROUTING -s $lan_ip/$lan_nm -d ! $wan_ip/$wan_nm -o $wan_2 -j MASQUERADE
#iptables -t nat -D POSTROUTING -s $lan_ip/$lan_nm -d $wan_ip/$wan_nm -o $wan_1 -j MASQUERADE
#iptables -D OUTPUT -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
#iptables -D FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
#David Lin, 25/May/2009

#Only L2TP, PPTP and RussiaL2TP need do NAT from LAN to WAN before wan connection is UP - Ricky CAO
#if [ "$wanMode" = "L2TP" -o "$wanMode" = "PPTP" -o "$wanMode" = "RussiaL2TP" ]; then
#	iptables -t nat -D POSTROUTING -s $lan_ip/$lan_nm -o $wan_1 -j MASQUERADE
#	iptables -t nat -A POSTROUTING -s $lan_ip/$lan_nm -o $wan_1 -j MASQUERADE	
#fi	

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