#!/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

#David Lin, 2009-08-06, for light on LED of modem signal strength
VP_1="1a8d,1009,/dev/ttyUSB2" 		#BandLuxe C170,C270				can get signal strength
VP_2="12d1,1003,/dev/ttyUSB1" 		#Huawei E219 E220 E156G, 	can get signal strength
VP_3="12d1,1001,/dev/ttyUSB2" 		#Huawei E169, 						can get signal strength
VP_4="19d2,0031,/dev/ttyUSB1" 		#ZTE MF626, 							can get signal strength
VP_5="19d2,0015,/dev/ttyUSB2" 		#ZTE MF628, 							can get signal strength
VP_6="1519,xxxx,/dev/ttyUSB1"			#M500, 										can get signal strength
VP_7="0408,ea02,/dev/ttyUSB2"			#MU-Q101,									can get signal strength
VP_8="1a8d,1002,/dev/ttyUSB0" 		#BandLuxe C120, 					can,t get signal streng
VP_9="0b05,0302,/dev/ttyUSB1" 		#ASUS T500, 							can,t get signal streng
VP_10="0fce,d0cf,/dev/ttyUSB1" 		#SonyEricsson MD300, 			can,t get signal streng
VP_11="16d8,xxxx,/dev/ttyUSBx"
VP_12="1199,xxxx,/dev/ttyUSBx"
VP_13="1fac,0042,/dev/ttyUSB2"		#Diffon ME100 ver3.0		can get signal strength - Ricky CAO

for VID in $VP_1 $VP_2 $VP_3 $VP_4 $VP_5 $VP_6 $VP_7 $VP_8 $VP_9 $VP_10 $VP_11 $VP_12 $VP_13
do
       echo "$VID" > /tmp/tempmodemquery
       vend=`cat /tmp/tempmodemquery | cut -d "," -f1`
       prod=`cat /tmp/tempmodemquery | cut -d "," -f2`
       usbp=`cat /tmp/tempmodemquery | cut -d "," -f3`
       if [ "$vend" == "1519" ]; then
       		foundit=`cat /proc/bus/usb/devices | grep Vendor=$vend`
       else
       		foundit=`cat /proc/bus/usb/devices | grep Vendor=$vend | grep ProdID=$prod`
       fi
       if [ "$foundit" != "" ]; then
       		echo "Found vend=$vend, prod=$prod, usbp=$usbp" > /dev/console
       		/bin/atcommand -i $usbp &
       fi
       rm /tmp/tempmodemquery
done 
#David Lin, 2009-08-06

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

PROJECT_NAME=`cat /sbin/projectName`
if [ "$PROJECT_NAME" = "FranklinR520" ]; then
	# light LED indicate current is working on 3G mode
	gpio u 2 1
fi

if [ -x /bin/qos_run ]; then
	echo "/bin/qos_run"
	/bin/qos_run
fi
