#!/bin/sh
echo "The PPP interface going DOWN" > /dev/console
#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 after ppp0 is up, 
#but after ppp0 become to down, we need allow all inbound packets from eth2.2 again for general operation
projectName=`cat sbin/projectName`
wanMode=`/bin/nvram_get 2860 wanConnectionMode`
pppoeMode=`nvram_get 2860 wan_pppoe_mode`
l2tpMode=`nvram_get 2860 wan_l2tp_mode`
pptpMode=`nvram_get 2860 wan_pptp_mode`
gw=`nvram_get 2860 wan_gateway`
lan_ip=`nvram_get 2860 lan_ipaddr`
lan_nm=`nvram_get 2860 lan_netmask`
if [ "$wanMode" = "PPPOE" -o "$wanMode" = "L2TP" -o "$wanMode" = "PPTP" -o "$wanMode" = "3G" ]; then
	iptables -D PREROUTING -t nat -i eth2.2 -s 0.0.0.0/0 -d ${lan_ip}/${lan_nm} -j DROP
	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
#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" ]; 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
#U-Media Ricky Cao on May 8 2008

#Remove the rule of set MSS to 1452 bytes - U-Media Ricky Cao on May 16 2008
wanmode=`nvram_get 2860 wanConnectionMode`
useDefaultMtu=`nvram_get 2860 wan_use_default_mtu`
#wanMtu=`nvram_get 2860 wan_mtu`
if [ "$wanmode" = "PPPOE" ]; then
	iptables -F tcp_mss_fix
fi

# Below procedures are for 3G modem
#1. Turn off project Link/Act indicate led
if [ "$projectName" == "FranklinR510" ]; then
	#Turn off M500 Link/Act indicate led - Ricky Cao
	gpio z 0 1
	gpio z 6 1
	gpio z 11 1
	gpio z 12 1
	gpio z 13 1
	gpio z 14 1
	gpio z 1 1
	#Reset M500 module - Ricky Cao
	gpio z 8 0
	gpio z 8 1
fi
if [ "$projectName" == "FranklinR520" ]; then
	gpio u 5
fi
if [ "$projectName" == "griin_WRT385L" ]; then
	#Turn off griin Link/Act indicate led - Ricky Cao
	gpio u 5
fi

#2. Report 3G carrier status - Ricky Cao
echo "0" > /tmp/carrierStatus

echo "===== PPP interface DOWN =====" >> /tmp/ppp_time_info
date | awk '{print $4}' >> /tmp/ppp_time_info 
echo "Connect time is $CONNECT_TIME seconds" >> /tmp/ppp_time_info
