#!/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=`/bin/nvram_get 2860 wanConnectionMode`
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 -I PREROUTING -t nat -i eth2.2 -s 0.0.0.0/0 -d ${lan_ip}/${lan_nm} -j DROP
	# Jacky.Yang 25-Dec-2008, drop DNS port 53 nat
	#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
	#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

	#Because rp-pppoe module in pppd seem have bug that can not configure MTU correctly
	#so I need revised the MTU of ppp interface to user defined value by ourself
	# - Ricky Cao on Jan. 12 2009
	if [ "$wanMode" = "PPPOE" ]; then
		useDefaultMTU=`bin/nvram_get 2860 wan_use_default_mtu`
		if [ "$useDefaultMTU" = "0" ]; then
			wanMTU=`/bin/nvram_get 2860 wan_mtu`
			ifconfig ppp0 mtu $wanMTU
		fi
	fi
	#- Ricky Cao on Jan. 12 2009
fi
cp /etc_ro/resolv.conf /etc
if [ "$wanMode" = "RussiaPPTP" ]; 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
	cat /etc/resolv.conf /etc/wan_resolv.conf > /etc/temp_resolv.conf
	mv /etc/temp_resolv.conf /etc/resolv.conf
fi

#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
#Jacky.Yang 8-May-2008, when the ppp connected that need firewall_init()
send_event firewall_reload

# Light M500 Link/Act indicate led - Ricky Cao
gpio z 0 0
#gpio z 11 0
#gpio z 12 0
#gpio z 13 0
#gpio z 14 1

# Run daemon for monitor signal strength and control indication LED
ProdID=`cat /proc/bus/usb/devices | grep 'Vendor=1519' | sed 's/^.*ProdID=//g' | sed 's/ Rev=.*$//g'`
if [ -e /dev/ttyUSB1 ] && [ $ProdID != "" ]; then
	/bin/atcommand -i /dev/ttyUSB1 > /dev/console &
else
	echo "Can not found ttyUSB1 for query signal strength" > /dev/console
fi

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