1#!/bin/sh /etc/rc.common
2#START=20
3
4FIREWALL="/www/cgi-bin/firewall.sh"
5
6check_qca_nss() {
7	local proto=$($CONFIG get wan_proto)
8	if [ "$proto" = "pptp" ] || [ "$proto" = "l2tp" ] || [ "$($CONFIG get qos_endis_on)" = "1" ] ; then
9		/etc/init.d/qca-nss-ecm stop &
10	else
11		pppmod=`lsmod |grep pppoe`
12		[ "x$pppmod" = "x" ] && insmod pppox && insmod pppoe
13		/etc/init.d/qca-nss-ecm start
14	fi
15}
16
17wanmac()
18{
19	local mac typesign
20	local wantype=$($CONFIG get wan_proto)
21
22	case "$wantype" in
23		dhcp|static) 	typesign="ether" ;;
24		pppoe) 		typesign="pppoe" ;;
25		pptp) 		typesign="pptp" ;;
26		bigpond)	typesign="bpa" ;;
27		l2tp) 		typesign="l2tp" ;;
28		*) 		typesign="ether" ;;
29	esac
30
31	case "$($CONFIG get wan_${typesign}_mac_assign)" in
32		1) mac="wan_remote_mac" ;;
33		2) mac="wan_${typesign}_this_mac" ;;
34		*) mac="wan_factory_mac" ;;
35	esac
36
37	echo $($CONFIG get $mac)
38}
39
40same_subnet() # $1: ip1, $2: ip2, $3:netmask
41{
42	local i ip1 ip2 mask
43
44	for i in 1 2 3 4; do
45		ip1=$(echo $1 | cut -f$i -d ".")
46		ip2=$(echo $2 | cut -f$i -d ".")
47		mask=$(echo $3 | cut -f$i -d ".")
48		[ $(($ip1 & $mask)) -eq $((ip2 & $mask)) ] || return 1
49	done
50
51	return 0
52}
53
54
55setup_interface_static_ip()
56{
57	local mtu=$($CONFIG get wan_dhcp_mtu)
58	ifconfig $WAN_IF mtu ${mtu:-1500}
59	ifconfig $WAN_IF $($CONFIG get wan_ipaddr) netmask $($CONFIG get wan_netmask)
60	if ! same_subnet $($CONFIG get wan_ipaddr) $($CONFIG get wan_gateway) $($CONFIG get wan_netmask); then
61		route add -net $($CONFIG get wan_gateway) netmask 255.255.255.255 dev $WAN_IF
62	fi
63	route add default gw $($CONFIG get wan_gateway)
64
65	$FIREWALL restart
66
67	# static route & ripd
68	/sbin/cmdroute stop
69	/usr/bin/killall -SIGINT ripd
70	/sbin/cmdroute start
71	/usr/sbin/ripd
72	/sbin/cmdigmp stop
73	/sbin/cmdigmp start
74
75	/sbin/ledcontrol -n wan -c green -s on
76	# if IP address re-assigned, we will check qos bandwidth.
77	local qos_enable=`$CONFIG get qos_endis_on`
78	local qos_bandwidth_enable=`$CONFIG get qos_threshold`
79	local qos_bandwidth_type=`$CONFIG get qos_bandwidth_type`
80	if [ "x$qos_enable" = "x1" -a "x$qos_bandwidth_enable" = "x1" ]; then
81		if [ "x$qos_bandwidth_type" = "x1" ]; then
82			/etc/bandcheck/band-check &
83		fi
84	fi
85
86	# log for static mode when wan gets ip.
87	local wan_log="[Internet connected] IP address: "$($CONFIG get wan_ipaddr)","
88	/usr/bin/logger "$wan_log"
89}
90
91get_wan_hostname()
92{
93	local wan_hostname="$($CONFIG get wan_hostname)"
94	[ -z "$wan_hostname" ] && wan_hostname="$($CONFIG get Device_name)"
95	echo "$wan_hostname"
96}
97
98setup_interface_dhcp()
99{
100	local mtu
101	local u_hostname
102	local u_wan_domain=$($CONFIG get wan_domain)
103
104	mtu=$($CONFIG get wan_dhcp_mtu)
105	ifconfig $WAN_IF mtu ${mtu:-1500}
106	
107	if [ "x$($CONFIG get wan_hostname)" != "x" ];then
108		u_hostname=$($CONFIG get wan_hostname)
109	else
110		u_hostname=$($CONFIG get Device_name)
111	fi
112	if [ "$changing_mode" = "1" ]; then
113		udhcpc -b -i $WAN_IF -h $u_hostname -r $($CONFIG get wan_dhcp_ipaddr) -N $($CONFIG get wan_dhcp_oldip) ${u_wan_domain:+-d $u_wan_domain} &
114    	else
115		udhcpc -b -i $WAN_IF -h $u_hostname -r $($CONFIG get wan_dhcp_ipaddr) -N $($CONFIG get wan_dhcp_oldip) ${u_wan_domain:+-d $u_wan_domain}
116    	fi	
117
118}
119
120setup_interface_mulpppoe()
121{
122	echo "start multipppoe session1" > /dev/console
123
124	mknod /dev/ppp c 180 0
125	load_modules /etc/modules.d/60-pppoe-mod
126
127	if [ ! -d /etc/ppp/peers ] ;then
128		rm -rf /etc/ppp
129		mkdir -p /etc/ppp/peers
130	fi
131
132	local manul=$($CONFIG get wan_endis_dod)
133
134	# start session1 if it's not manully connect
135	if [ "x$manul" != "x2" ] ;then
136		ppp-nas pppoe-session1 start
137	fi
138
139	# write static dns
140	if [ "x$($CONFIG get wan_mulpppoe1_dns_assign)" = "x1" ];then
141		local dns1=$($CONFIG get wan_ether_dns1)
142		local dns2=$($CONFIG get wan_ether_dns2)
143		if [ "x$dns1" != "x" ] ;then
144			echo $dns1 > /etc/ppp/pppoe1-dns.conf
145			echo "nameserver $dns1" > /tmp/resolv.conf
146		fi
147		if [ "x$dns2" != "x" ] ;then
148			echo $dns2 >> /etc/ppp/pppoe1-dns.conf
149			echo "nameserver $dns2" >> /tmp/resolv.conf
150		fi
151	fi
152
153	local session2=$($CONFIG get wan_enable_session2)
154	if [ "x$session2" != "x" ] ;then
155		sleep 2
156		echo "start multipppoe session2" > /dev/console
157		ppp-nas pppoe-session2 start &
158		touch /etc/ppp/enable_ppp1
159		# write static dns
160		if [ "x$($CONFIG get wan_mulpppoe2_dns_assign)" = "x1" ];then
161			local dns3=$($CONFIG get wan_ether2_dns1)
162			local dns4=$($CONFIG get wan_ether2_dns2)
163			if [ "x$dns3" != "x" ] ;then
164				echo $dns3 > /etc/ppp/pppoe2-dns.conf
165				echo "nameserver $dns3" >> /tmp/resolv.conf
166			fi
167			if [ "x$dns4" != "x" ] ;then
168				echo $dns4 >> /etc/ppp/pppoe2-dns.conf
169				echo "nameserver $dns4" >> /tmp/resolv.conf
170			fi
171		fi
172	fi
173}
174
175set_dns()
176{
177	echo "nameserver $($CONFIG get wan_ether_dns1)" > /tmp/resolv.conf
178	[ "x$($CONFIG get wan_ether_dns2)" != "x" ] && \
179		echo "nameserver $($CONFIG get wan_ether_dns2)" >> /tmp/resolv.conf
180	[ "x$($CONFIG get wan_ether_dns3)" != "x" ] && \
181		echo "nameserver $($CONFIG get wan_ether_dns3)" >> /tmp/resolv.conf
182}
183
184restart_ipv6() {
185	if [ "$($CONFIG get wan_proto)" != "dhcp" ] || [ "$($CONFIG get ipv6_type)" != "6to4" ]; then
186		[ "$1" = "boot" ] && sleep 10 #Fix net6conf start too early, radvd can not up.
187		killall net6conf
188		/etc/init.d/init6 start 
189	fi
190}
191
192setup_wan_interface() # $1: manually
193{
194	local proto=$($CONFIG get wan_proto)
195	local traffic_month_limit=$($CONFIG get trafficmeter_monthly_limit_reached)
196	local traffic_block_all=$($CONFIG get traffic_block_all)
197	local firmware_region=`cat /tmp/firmware_region | awk '{print $1}'`
198	local ru_feature=0
199	
200	if [ "x$firmware_region" = "xWW" ] || [ "x$firmware_region" = "x" ] ;then
201		if [ "x$($CONFIG get GUI_Region)" = "xRussian" ] ;then
202			ru_feature=1
203		fi
204	elif [ "x$firmware_region" = "xRU" ] ;then
205		ru_feature=1
206    fi
207
208	case "$proto" in
209	pppoe)
210		. /lib/network/ppp.sh
211		#if traffic meter monthly limit is not reached or don't check "Disconnect and disable the Internet connection".
212		if [ "$traffic_month_limit" != "1" -o "$traffic_block_all" != "1" ]; then
213			if [ "$($CONFIG get wan_pppoe_intranet_wan_assign)" = "0" ]; then
214				# Confiure the PPP parameters firstly, then started PPPD by UDHCPC
215				setup_interface_ppp $1
216				if [ "$ru_feature" = "1" ]; then
217					if [ "$($CONFIG get enable_vlan )" = "0" ]; then
218						udhcpc -b -i $WAN_IF
219					else
220						udhcpc -b -i br11
221					fi
222					# when intranet dns exists ,then append it
223					cat /tmp/dhcpc_resolv.conf >> /tmp/resolv.conf
224				fi
225			else
226				if [ "$($CONFIG get wan_pppoe_intranet_wan_assign)" = "1" -a "$ru_feature" = "1" ]; then
227					netmask=$($CONFIG get wan_pppoe_intranet_mask)
228					if [ "x$netmask" != "x" -a "x$netmask" != "x0.0.0.0" -a "x$netmask" != "x255.255.255.255" ]; then
229						ifconfig $WAN_IF $($CONFIG get wan_pppoe_intranet_ip) netmask $netmask
230					else
231						ifconfig $WAN_IF $($CONFIG get wan_pppoe_intranet_ip)
232					fi
233				fi
234				setup_interface_ppp $1
235			fi
236			[ "$($CONFIG get wan_pppoe_dns_assign)" = "1" ] && set_dns
237		fi
238		;;
239	pptp)
240		. /lib/network/ppp.sh
241		#if traffic meter monthly limit is not reached or don't check "Disconnect and disable the Internet connection".
242		if [ "$traffic_month_limit" != "1" -o "$traffic_block_all" != "1" ]; then
243			# [ "$($CONFIG get wan_pptp_dns_assign)" = "1" ] && set_dns
244			# Setup WAN Ethernet interface IP address
245			if [ "$($CONFIG get wan_pptp_wan_assign)" = "0" ]; then
246				# Confiure the PPP parameters firstly, then started PPPD by UDHCPC
247				setup_interface_ppp $1
248				udhcpc -b -i $WAN_IF
249			else
250				# Configure the interface address, then started PPPD in `setup_interface_ppp`
251				if [ "x$($CONFIG get wan_pptp_eth_mask)" != "x" ]; then
252					ifconfig $WAN_IF $($CONFIG get wan_pptp_local_ip) netmask $($CONFIG get wan_pptp_eth_mask)
253				else
254					ifconfig $WAN_IF $($CONFIG get wan_pptp_local_ip)
255				fi
256
257				setup_interface_ppp $1
258			fi
259		fi
260		;;
261	l2tp)
262		. /lib/network/ppp.sh
263		# [ "$($CONFIG get wan_l2tp_dns_assign)" = "1" ] && set_dns
264		if [ "$($CONFIG get wan_l2tp_wan_assign)" = "0" ]; then
265			# Confiure the PPP parameters firstly, then started PPPD by UDHCPC
266			setup_interface_ppp $1
267			udhcpc -b -i $WAN_IF
268		else
269			# Configure the interface address, then started PPPD in `setup_interface_ppp`
270			if [ "x$($CONFIG get wan_l2tp_eth_mask)" != "x" ]; then
271				ifconfig $WAN_IF $($CONFIG get wan_l2tp_local_ip) netmask $($CONFIG get wan_l2tp_eth_mask)
272			else
273				ifconfig $WAN_IF $($CONFIG get wan_l2tp_local_ip)
274			fi
275
276			setup_interface_ppp $1
277		fi
278		;;
279	static)
280		setup_interface_static_ip
281		set_dns
282		# If upgrade FW, need to update stremboost database
283		sb_update_database
284		;;
285	bigpond)
286		# calling package/bpalogin/files/bigpond.sh
287		setup_interface_bigpond $1
288		[ "$($CONFIG get wan_bpa_dns_assign)" = "1" ] && set_dns
289		;;
290	mulpppoe1)
291		# calling package/ppp-nas/files/mulpppoe.sh
292		setup_interface_mulpppoe
293		;;
294	*)
295		[ "$($CONFIG get wan_ether_dns_assign)" = "1" ] && set_dns
296		setup_interface_dhcp
297        ;;
298	esac
299}
300
301start_stage0() # $1: boot/start, $2: manually
302{
303	[ "x$1" = "xmanually" ] && echo "***** The net-wan command is called manually!!! *****"
304
305	mac_value=$(wanmac)
306	$CONFIG set cur_wanmac="$mac_value"
307
308	bond_enabled=`ifconfig |grep bond0`
309	if [ "x$bond_enabled" != "x" ]; then	
310		ifconfig eth0 hw ether $mac_value
311		ifconfig eth1 hw ether $mac_value
312		ifconfig bond0 hw ether $mac_value
313		ifconfig ethlan hw ether $mac_value
314	fi
315	ifconfig ethwan hw ether $mac_value
316	ifconfig $WAN_IF hw ether $mac_value
317	ifconfig ethwan up
318	ifconfig $WAN_IF up
319
320	/etc/init.d/dni-qos $1
321	check_qca_nss
322
323	# Bring up the wan interface first time when sytem is Rebooted, it needs to wait 
324	# for a moment after executing the command `ifconfig ethwan up`, then ethwan
325	# PHY is up. 3 seconds MAY be acceptable, and it works.
326	# If not wait, the `udhcpc` CAN'T obtain IP address in its selecting time.
327	[ "$changing_mode" = "1" ] && sleep 5
328	setup_wan_interface $2
329
330	/etc/init.d/upnp $1 &                # upnp daemon
331	/etc/init.d/ddns $1 &                # DDNS daemon
332	restart_ipv6 $1 &
333}
334
335start() # $1: manually
336{
337	[ "$1" = "mode" ] && changing_mode=1
338	start_stage0 start $1
339	/etc/init.d/ntpclient start
340	/etc/init.d/cron restart
341	/etc/init.d/dnsmasq start
342}
343
344stop()
345{
346	/etc/init.d/dnsmasq stop
347	/etc/init.d/ntpclient stop
348	/etc/init.d/init6 stop
349
350	/etc/init.d/ddns stop
351	/etc/init.d/upnp stop
352
353	# log for static mode when wan disconnects.
354	local wan_proto=$($CONFIG get wan_proto)
355	[ "x$wan_proto" = "xstatic" ] && /usr/bin/logger "[Internet disconnected]"
356	/sbin/ledcontrol -n wan -c amber -s on
357
358	/sbin/cmdigmp stop
359	/usr/bin/killall -SIGINT ripd
360	/sbin/cmdroute restart         # Restart static route for LAN
361	$FIREWALL stop
362
363	killall bpalogin
364	local ipv4_pppd=`ps | grep "pppd call dial-provider updetach" | grep -v "grep" |awk '{print $1}'`
365	if [ "x$ipv4_pppd" != "x" ]; then
366		/bin/kill -SIGHUP $ipv4_pppd
367		/bin/kill $ipv4_pppd
368	fi
369	
370	local cdma_pppd=`ps | grep "pppd call cdma" | grep -v "grep" |awk '{print $1}'`
371	if [ "x$cdma_pppd" != "x" ]; then
372		/bin/kill -SIGHUP $cdma_pppd
373		/bin/kill $cdma_pppd
374	fi
375
376	killall -SIGUSR2 udhcpc; killall udhcpc; killall udhcpc; sleep 1
377
378	/sbin/rmmod pptp; /sbin/rmmod pppoe; /sbin/rmmod pppox; sleep 2
379	/sbin/rmmod ppp_synctty; /sbin/rmmod ppp_async; /sbin/rmmod crc-ccitt; sleep 2
380	/sbin/rmmod ppp_generic; /sbin/rmmod slhc
381
382	# stop multipppoe
383	ppp-nas pppoe-session1 stop; ppp-nas pppoe-session2 stop
384	# Clear dns in /tmp/resolv.conf
385	echo "" > /tmp/resolv.conf; rm /etc/ppp/pppoe2-domain.conf; rm /etc/ppp/enable_ppp1
386
387	ifconfig $WAN_IF 0.0.0.0 down
388	ifconfig $WAN_IF mtu 1500
389	ifconfig ethwan down
390
391	/etc/init.d/dni-qos stop
392}
393
394restart()
395{
396	stop
397	start $1
398}
399
400boot()
401{
402	[ "$1" = "mode" ] && changing_mode=1
403	start_stage0 boot
404}
405
406#Only for setup wizard
407EXTRA_COMMANDS="stop_for_detwan restart_from_detwan"
408
409stop_for_detwan()
410{
411         /etc/init.d/ntpclient stop
412
413         # log for static mode when wan disconnects.
414         local wan_proto=$($CONFIG get wan_proto)
415         [ "x$wan_proto" = "xstatic" ] && /usr/bin/logger "[Internet disconnected]"
416         /sbin/ledcontrol -n wan -c amber -s on
417
418         /usr/bin/killall -SIGINT ripd
419         /sbin/cmdroute restart         # Restart static route for LAN
420
421         killall bpalogin
422         # Because the ipv6 pppoe may be connecting.
423         local ipv4_pppd=`ps | grep "pppd call dial-provider updetach" | grep -v "grep" |awk '{print $1}'`
424         if [ "x$ipv4_pppd" != "x" ]; then
425                 /bin/kill -SIGHUP $ipv4_pppd
426                 /bin/kill $ipv4_pppd
427         fi
428
429         local cdma_pppd=`ps | grep "pppd call cdma" | grep -v "grep" |awk '{print $1}'`
430         if [ "x$cdma_pppd" != "x" ]; then
431                 /bin/kill -SIGHUP $cdma_pppd
432                 /bin/kill $cdma_pppd
433         fi
434
435         killall -SIGUSR2 udhcpc; killall udhcpc; killall udhcpc; sleep 1
436
437         /sbin/rmmod pptp; /sbin/rmmod pppoe; /sbin/rmmod pppox; sleep 2
438         /sbin/rmmod ppp_synctty; /sbin/rmmod ppp_async; /sbin/rmmod crc-ccitt; sleep 2
439         /sbin/rmmod ppp_generic; /sbin/rmmod slhc
440
441         # stop multipppoe
442         ppp-nas pppoe-session1 stop; ppp-nas pppoe-session2 stop
443          # Clear dns in /tmp/resolv.conf
444         echo "" > /tmp/resolv.conf; rm /etc/ppp/pppoe2-domain.conf; rm /etc/ppp/enable_ppp1
445
446         ifconfig $WAN_IF 0.0.0.0 down
447         ifconfig $WAN_IF mtu 1500 
448}
449
450restart_from_detwan()
451{
452         stop_for_detwan
453	 start_qca_nss_connmgr_if_needed 
454	 /etc/init.d/dni-qos start  
455
456         ifconfig $WAN_IF hw ether $(wanmac)
457         ifconfig $WAN_IF up
458
459         # Bring up the wan interface first time when sytem is Rebooted, it needs to wait
460         # for a moment after executing the command `ifconfig ethwan up`, then ethwan
461         # PHY is up. 3 seconds MAY be acceptable, and it works.
462         # If not wait, the `udhcpc` CAN'T obtain IP address in its selecting time.
463         sleep 5
464         setup_wan_interface
465         /etc/init.d/dnsmasq stop
466         /etc/init.d/dnsmasq start
467         /etc/init.d/ddns restart
468         /etc/init.d/upnp restart
469         /etc/init.d/ntpclient start
470         /etc/init.d/cron restart
471         local wan6_type=$($CONFIG get ipv6_type)
472         boot_status=`cat /tmp/boot_status`
473         if [ "$boot_status" != "1" ]; then
474            if [ "x$wan6_type" != "x" -a "$wan6_type" != "disabled" ]; then
475                    /etc/net6conf/net6conf restart
476            fi
477         fi
478
479         sleep 8                       #wait for setting wan ip over
480}
481
482