1#the following line combines the last line to prevent this file from being sourced twice
2if [ "x$opmode_sh" = "x" ]; then opmode_sh="sourced"
3. /lib/cfgmgr/cfgmgr.sh
4. /lib/cfgmgr/enet.sh
5
6# name of NIF :
7#   Eth Lan NIF will always be "ethlan"
8#   Eth Wan NIF will always be "ethwan"
9#   Lan NIF will always be "br0"
10#   Wan NIF will be "brwan" or "ppp0"
11
12# vlan-iptv related configs :
13#
14#   enable_vlan (1/0) : enable / disable
15#     vlan_type (1/0) : vlan group / bridge group (iptv)
16#
17#   vlan group configs :
18#     vlan_tag_1 ~ vlan_tag_10 :
19#         Enable   Name      VID     Priority   lan-ports    wireless
20#          1/0    xxxxxx    1-4094     0-7      x (4bits)    x (4bits)
21#        lan-ports : 
22#            bit0 -> lan1       belongs to vlan group or not
23#            bit1 -> lan2       belongs to vlan group or not
24#            bit2 -> lan3       belongs to vlan group or not
25#            bit3 -> lan4       belongs to vlan group or not
26#        wireless :
27#            bit0 -> 2.4G       belongs to vlan group or not
28#            bit1 -> 5G         belongs to vlan group or not
29#            bit3 -> 2.4G-Guest belongs to vlan group or not
30#            bit4 -> 5G-Guest   belongs to vlan group or not
31#
32#   bridge group (iptv) configs :
33#     wan_brig_ssid1 (1/0)       : 2.4G       belongs to bridge group or not
34#     wan_brig_ssid2 (1/0)       : 5G         belongs to bridge group or not
35#     wan_brig_guest_ssid1 (1/0) : 2.4G-Guest belongs to bridge group or not
36#     wan_brig_guest_ssid2 (1/0) : 5G-Guest   belongs to bridge group or not
37#     iptv_mask (4 bits) : 
38#            bit0 -> lan1                     belongs to bridge group or not
39#            bit1 -> lan2                     belongs to bridge group or not
40#            bit2 -> lan3                     belongs to bridge group or not
41#            bit3 -> lan4                     belongs to bridge group or not
42#     iptv_mask_change :
43#
44#   wlg1_endis_guestNet (1/0) : 2.4G-Guest enable or not
45#   wla1_endis_guestNet (1/0) :   5G-Guest enable or not
46#
47#   opmode related induced configs : 
48#     i_opmode (normal/iptv/vlan/apmode/brmode) : device operation mode
49#     i_wlg_br                                  : the bridge wlg belongs to
50#     i_wla_br                                  : the bridge wla belongs to
51#     i_wlg_guest_br                            : the bridge wlg_guest belongs to
52#     i_wla_guest_br                            : the bridge wla_guest belongs to
53#     i_wlg_pri                                 : the priority of wlg
54#     i_wla_pri                                 : the priority of wla
55#     i_wlg_guest_pri                           : the priority of wlg_guest
56#     i_wla_guest_pri                           : the priority of wla_guest
57#
58
59op_opmode() # ret: factory / brmode / apmode / extmode / normal / vlan / iptv
60{
61	[ "$($CONFIG get factory_mode)" = "1" ] && echo "factory" && return
62	[ "$($CONFIG get extender_mode)" = "1" ] && echo "extmode" && return
63	[ $($CONFIG get bridge_mode) = "1" ] && echo "brmode" && return
64	[ $($CONFIG get ap_mode) = "1" ] && echo "apmode" && return
65	! [ $($CONFIG get enable_vlan) = "1" ] && echo "normal" && return
66	[ $($CONFIG get vlan_type) = "1" ] && echo "vlan" || echo "iptv"
67}
68
69op_set_induced_configs()
70{
71	local opmode=$(op_opmode)
72	local i_wlg_br="br0" i_wla_br="br0" i_wlg_guest_br="br0" i_wla_guest_br="br0"
73	local i_wlg_pri="" i_wla_pri="" i_wlg_guest_pri="" i_wla_guest_pri=""
74	local i tv i_vid
75
76	oc echo "opmode = $opmode"
77	$CONFIG set i_opmode=$opmode
78
79	case "$opmode" in
80	iptv)
81		[ "$($CONFIG get wan_brig_ssid1)" = "1" ] && i_wlg_br="brwan"
82		[ "$($CONFIG get wan_brig_ssid2)" = "1" ] && i_wla_br="brwan"
83		[ "$($CONFIG get wan_brig_guest_ssid1)" = "1" ] && i_wlg_guest_br="brwan"
84		[ "$($CONFIG get wan_brig_guest_ssid2)" = "1" ] && i_wla_guest_br="brwan"
85		;;
86
87	vlan)
88		for i in 1 2 3 4 5 6 7 8 9 10; do
89			tv=$($CONFIG get vlan_tag_$i)
90			[ -n "$tv" ] || continue
91			set - $(echo $tv)
92			# $1: enable, $2: name, $3: vid, $4: pri, $5:wports, $6:wlports
93			[ "$1" = "1" ] || continue
94			[ "$2" = "Internet" ] && i_vid=$3 && continue
95			[ $(( 1 & $6 )) -ne 0 ] && i_wlg_br="br$3" && i_wlg_pri="$4"
96			[ $(( 2 & $6 )) -ne 0 ] && i_wla_br="br$3" && i_wla_pri="$4"
97			[ $(( 4 & $6 )) -ne 0 ] && i_wlg_guest_br="br$3" && i_wlg_guest_pri="$4"
98			[ $(( 8 & $6 )) -ne 0 ] && i_wla_guest_br="br$3" && i_wla_guest_pri="$4"
99		done
100		[ "$i_vid" != "0" ] && {
101			[ "$i_wlg_br" = "br$i_vid" ] && i_wlg_br="brwan"
102			[ "$i_wla_br" = "br$i_vid" ] && i_wla_br="brwan"
103			[ "$i_wlg_guest_br" = "br$i_vid" ] && i_wlg_guest_br="brwan"
104			[ "$i_wla_guest_br" = "br$i_vid" ] && i_wla_guest_br="brwan"
105		}
106		;;
107	esac
108
109	$CONFIG set i_wlg_br="$i_wlg_br"
110	$CONFIG set i_wla_br="$i_wla_br"
111	$CONFIG set i_wlg_guest_br="$i_wlg_guest_br"
112	$CONFIG set i_wla_guest_br="$i_wla_guest_br"
113	$CONFIG set i_wlg_pri="$i_wlg_pri"
114	$CONFIG set i_wla_pri="$i_wla_pri"
115	$CONFIG set i_wlg_guest_pri="$i_wlg_guest_pri"
116	$CONFIG set i_wla_guest_pri="$i_wla_guest_pri"
117}
118
119bond_create_ifs()
120{
121	local landefmac=$($CONFIG get lan_factory_mac)
122	local wandefmac=$($CONFIG get cur_wanmac)
123
124	bond_init
125	[ "x$wandefmac" = "x" ] && wandefmac=$($CONFIG get wan_factory_mac)
126	ifconfig $RawEthLan hw ether $wandefmac
127	ifconfig $RawEthWan hw ether $wandefmac
128	ifconfig $BondEth hw ether $wandefmac
129	ifconfig $BondEth up
130	vconfig add $BondEth 1 && ifconfig $BondEth.1 down
131	vconfig add $BondEth 2 && ifconfig $BondEth.2 down
132	ip link set dev $BondEth.1 name ethlan
133	ip link set dev $BondEth.2 name ethwan
134}
135
136br_create() # $1: brname
137{
138	brctl addbr $1
139	brctl setfd $1 0
140	brctl stp $1 0
141	echo 0 > /sys/devices/virtual/net/$1/bridge/multicast_snooping
142}
143
144br_allbrs()
145{
146	awk '/br[0-9w]/ {print $1}' /proc/net/dev |sed 's/://g'
147}
148
149br_allnifs() # $1: brx
150{
151	brctl show $1 | awk '!/bridge/ {print $NF}' | grep "eth\|ath\|host0."
152}
153
154op_del_all_brs_vifs()
155{
156	local brx nif
157	local landefmac=$($CONFIG get lan_factory_mac)
158	local wandefmac=$($CONFIG get wan_factory_mac)
159
160	for brx in $(br_allbrs); do
161		ifconfig $brx down
162		for nif in $(br_allnifs $brx); do 
163			ifconfig $nif down
164			brctl delif $brx $nif
165			case "$nif" in
166			ethlan|ethwan)
167				;;
168			eth*)
169				vconfig rem $nif
170				;;
171			esac
172		done
173		[ "$brx" != br0 -a $brx != "brwan" ] && brctl delbr $brx
174	done
175
176	ifconfig ethlan down
177	vconfig rem ethlan || ip link set dev ethlan name $RawEthLan
178	ifconfig ethwan down
179	vconfig rem ethwan || ip link set dev ethwan name $RawEthWan
180	[ -n "$RawEth" ] && ifconfig $RawEth down || {
181		ifconfig $RawEthLan down
182		ifconfig $RawEthWan down
183	}
184        echo "-$RawEthWan" > /sys/devices/virtual/net/bond0/bonding/slaves
185        echo "-$RawEthLan" > /sys/devices/virtual/net/bond0/bonding/slaves
186	ifconfig $RawEthLan hw ether $landefmac
187	ifconfig $RawEthWan hw ether $wandefmac 
188	ifconfig $BondEth down
189}
190
191op_create_br0_brwan()
192{
193	br_create br0
194	br_create brwan
195}
196
197factory_mode_create_brs_and_vifs()
198{
199	local landefmac=$($CONFIG get lan_factory_mac)
200	local wandefmac=$($CONFIG get wan_factory_mac)
201
202	if [ -n "$RawEth" ]; then
203		ifconfig $RawEth hw ether $landefmac
204		ifconfig $RawEth up
205		vconfig add $RawEth 0 && ifconfig $RawEth.0 down
206		vconfig add $RawEth 1 && ifconfig $RawEth.1 down
207		ip link set dev $RawEth.0 name ethlan
208		ip link set dev $RawEth.1 name ethwan
209	else
210		ifconfig $RawEthLan hw ether $landefmac
211		ifconfig $RawEthWan hw ether $wandefmac
212		ip link set dev $RawEthLan name ethlan
213		ip link set dev $RawEthWan name ethwan
214	fi
215
216	brctl addif br0 ethlan
217	brctl addif br0 ethwan
218	ifconfig br0 hw ether $landefmac
219	sw_configvlan "factory"
220}
221
222apmode_create_brs_and_vifs()
223{
224	local landefmac=$($CONFIG get lan_factory_mac)
225	local wandefmac=$($CONFIG get wan_factory_mac)
226
227	if [ -n "$RawEth" ]; then
228		ifconfig $RawEth hw ether $landefmac
229		ifconfig $RawEth up
230		vconfig add $RawEth 0 && ifconfig $RawEth.0 down
231		vconfig add $RawEth 1 && ifconfig $RawEth.1 down
232		ip link set dev $RawEth.0 name ethlan
233		ip link set dev $RawEth.1 name ethwan
234	elif [ "$enable_bond" = "1" ]; then
235		bond_create_ifs
236	else
237		ifconfig $RawEthLan hw ether $landefmac
238		ifconfig $RawEthWan hw ether $wandefmac
239		ip link set dev $RawEthLan name ethlan
240		ip link set dev $RawEthWan name ethwan
241	fi
242
243	brctl addif br0 ethlan
244	brctl addif br0 ethwan
245	ifconfig br0 hw ether $landefmac
246	sw_configvlan "apmode"
247}
248
249normal_create_brs_and_vifs()
250{
251	local landefmac=$($CONFIG get lan_factory_mac)
252	local wandefmac=$($CONFIG get wan_factory_mac)
253
254	if [ -n "$RawEth" ]; then
255		ifconfig $RawEth hw ether $landefmac
256		ifconfig $RawEth up
257		vconfig add $RawEth 0 && ifconfig $RawEth.0 down
258		vconfig add $RawEth 1 && ifconfig $RawEth.1 down
259		ip link set dev $RawEth.0 name ethlan
260		ip link set dev $RawEth.1 name ethwan
261	elif [ "$enable_bond" = "1" ]; then
262		bond_create_ifs
263	else
264		ifconfig $RawEthLan hw ether $landefmac
265		ifconfig $RawEthWan hw ether $wandefmac
266		ip link set dev $RawEthLan name ethlan
267		ip link set dev $RawEthWan name ethwan
268	fi
269
270	brctl addif br0 ethlan
271	ifconfig br0 hw ether $landefmac
272	brctl addif brwan ethwan
273	sw_configvlan "normal"
274}
275
276iptv_create_brs_and_vifs()
277{
278	local landefmac=$($CONFIG get lan_factory_mac)
279	local wandefmac=$($CONFIG get wan_factory_mac)
280
281	if [ -n "$RawEth" ]; then
282		ifconfig $RawEth hw ether $landefmac
283		ifconfig $RawEth up
284		vconfig add $RawEth 0 && ifconfig $RawEth.0 down
285		vconfig add $RawEth 1 && ifconfig $RawEth.1 down
286		ip link set dev $RawEth.0 name ethlan
287		ip link set dev $RawEth.1 name ethwan
288	else
289		ifconfig $RawEthLan hw ether $landefmac
290		ifconfig $RawEthWan hw ether $wandefmac
291		if [ "$WanIndepPhy" = "0" ]; then
292			ip link set dev $RawEthLan name ethlan
293			ip link set dev $RawEthWan name ethwan
294		else
295			ifconfig $RawEthLan up
296			vconfig add $RawEthLan 0 && ifconfig $RawEthLan.0 down
297			vconfig add $RawEthLan 1 && ifconfig $RawEthLan.1 up
298			brctl addif brwan $RawEthLan.1
299			ip link set dev $RawEthLan.0 name ethlan
300			ip link set dev $RawEthWan name ethwan
301		fi
302	fi
303
304	brctl addif br0 ethlan
305	ifconfig br0 hw ether $landefmac
306	brctl addif brwan ethwan
307	sw_configvlan "iptv" $($CONFIG get iptv_mask)
308}
309
310vlan_set_vif_pri() # $1: vif, $2: pri
311{
312	local p
313
314	for p in 0 1 2 3 4 5 6 7; do
315		vconfig set_ingress_map $1 $p $p
316		vconfig set_egress_map $1 $p $2
317	done
318}
319
320nif_existed() # $1: nif
321{
322	ifconfig $1 >/dev/null 2>&1
323}
324
325vlan_create_br_and_vif() # $1: vid, $2: pri
326{
327	local brx="br$1"
328
329	nif_existed $brx && return
330
331	br_create $brx
332	if [ -n "$RawEth" ]; then
333		vconfig add $RawEth $1 && ifconfig $RawEth.$1 up
334		brctl addif $brx $RawEth.$1
335		vlan_set_vif_pri $RawEth.$1 $2
336	else
337		if [ "$WanIndepPhy" = "0" ]; then
338			vconfig add ethlan $1 && ifconfig ethlan.$1 up
339			vconfig add $RawEthWan $1 && ifconfig $RawEthWan.$1 up
340			brctl addif $brx ethlan.$1
341			brctl addif $brx $RawEthWan.$1
342		else
343			vconfig add $RawEthLan $1 && ifconfig $RawEthLan.$1 up
344			vconfig add $RawEthWan $1 && ifconfig $RawEthWan.$1 up
345			brctl addif $brx $RawEthLan.$1
346			brctl addif $brx $RawEthWan.$1
347		fi
348		vlan_set_vif_pri $RawEthWan.$1 $2
349		vlan_set_vif_pri ethlan.$1 $2
350		ifconfig $brx hw ether $wandefmac	
351		brctl stp $brx on
352	fi
353	ifconfig $brx up
354}
355
356vlan_create_internet_vif() # $1: vid, $2: pri
357{
358	local brx="br$1"
359
360	if nif_existed $brx; then
361		ifconfig $brx down
362		if [ -n "$RawEth" ]; then
363			ifconfig $RawEth.$1 down && brctl delif $brx $RawEth.$1
364			ip link set dev $RawEth.$1 name ethwan
365		else
366			if [ "$WanIndepPhy" = "0" ]; then
367				ifconfig $RawEthWan.$1 down && brctl delif $brx $RawEthWan.$1
368			else
369				ifconfig $RawEthLan.$1 down && brctl delif $brx $RawEthLan.$1
370				ifconfig $RawEthWan.$1 down && brctl delif $brx $RawEthWan.$1
371				brctl addif brwan $RawEthLan.$1
372			fi
373			ip link set dev $RawEthWan.$1 name ethwan
374		fi
375		brctl delbr $brx
376	else
377		if [ -n "$RawEth" ]; then
378			vconfig add $RawEth $1 && ifconfig $RawEth.$1 down
379			ip link set dev $RawEth.$1 name ethwan
380		else
381			vconfig add $RawEthWan $1 && ifconfig $RawEthWan.$1 down
382			ip link set dev $RawEthWan.$1 name ethwan
383		fi
384	fi
385	vlan_set_vif_pri ethwan $2
386	brctl addif brwan ethwan
387}
388
389vlan_create_intranet_vif() # $1: vid, $2: pri
390{
391	local brx="br$1"
392
393	nif_existed $brx && return
394
395	br_create $brx
396	if [ -n "$RawEth" ]; then
397		vconfig add $RawEth $1 && ifconfig $RawEth.$1 up
398		brctl addif $brx $RawEth.$1
399		vlan_set_vif_pri $RawEth.$1 $2
400	else
401		vconfig add $RawEthWan $1 && ifconfig $RawEthWan.$1 up
402		brctl addif $brx $RawEthWan.$1
403		vlan_set_vif_pri $RawEthWan.$1 $2
404	fi
405	ifconfig $brx up
406}
407
408vlan_get_freevid() # $1: up / down
409{
410	local updown=$1
411	local i tv vids
412	local freevid
413
414	for i in 0 1 2 3 4 5 6 7 8 9 10; do
415		tv=$($CONFIG get vlan_tag_$i)
416		[ -n "$tv" ] || continue
417		set - $(echo $tv)
418		# $1: enable, $2: name, $3: vid, $4: pri, $5:wports, $6:wlports
419		[ "$1" = "1" ] || continue
420		#[ "$2" = "Internet" -o "$2" = "Intranet" ] && continue
421		[ "$vids" = "" ] && vids="x${3}x" || vids="$vids x${3}x"
422	done
423
424	[ "$updown" = "up" ] && freevid=1 || freevid=4094
425	while true; do
426		echo $vids | grep -q "x${freevid}x" || break
427		[ "$updown" = "up" ] && freevid=$(($freevid + 1)) || freevid=$(($freevid - 1))
428	done
429	echo $freevid
430}
431
432vlan_get_lanvid()
433{
434	vlan_get_freevid up
435}
436
437vlan_get_wanvid()
438{
439	vlan_get_freevid down
440}
441
442vlan_create_brs_and_vifs()
443{
444	local landefmac=$($CONFIG get lan_factory_mac)
445	local wandefmac=$($CONFIG get wan_factory_mac)
446	local lanvid=$(vlan_get_lanvid)
447	local firmware_region=`cat /tmp/firmware_region | awk '{print $1}'`
448	local ru_feature=0
449
450	if [ "x$firmware_region" = "xWW" ] || [ "x$firmware_region" = "x" ] ;then
451		if [ "x$($CONFIG get GUI_Region)" = "xRussian" ] ;then
452			ru_feature=1
453		fi
454	elif [ "x$firmware_region" = "xRU" ] ;then
455		ru_feature=1
456	fi
457
458	local i tv used_wports=0
459	local i_vid i_pri
460
461	if [ -n "$RawEth" ]; then
462		ifconfig $RawEth hw ether $landefmac
463		ifconfig $RawEth up
464		vconfig add $RawEth 0 && ifconfig $RawEth.0 down
465		ip link set dev $RawEth.0 name ethlan
466	else
467		ifconfig $RawEthLan hw ether $landefmac
468		ifconfig $RawEthWan hw ether $wandefmac
469		if [ "$WanIndepPhy" = "0" ]; then
470			ip link set dev $RawEthLan name ethlan
471		else
472			ifconfig $RawEthLan up
473			vconfig add $RawEthLan $lanvid && ifconfig $RawEthLan.$lanvid down
474			ip link set dev $RawEthLan.$lanvid name ethlan
475		fi
476		ifconfig $RawEthWan up
477	fi
478	brctl addif br0 ethlan
479	ifconfig br0 hw ether $landefmac
480
481	sw_configvlan "vlan" "start"
482	for i in 0 1 2 3 4 5 6 7 8 9 10; do
483		tv=$($CONFIG get vlan_tag_$i)
484		[ -n "$tv" ] || continue
485		set - $(echo $tv)
486		# $1: enable, $2: name, $3: vid, $4: pri, $5:wports, $6:wlports
487		[ "$1" = "1" ] || continue
488		if [ "$2" = "Internet" ]; then 
489			i_vid=$3
490			i_pri=$4
491		elif [ "$2" = "Intranet" ]; then
492			if [ "$ru_feature" = "1" ]; then
493				vlan_create_intranet_vif $3 $4
494				sw_configvlan "vlan" "add" "br" "$3" "0" "$4"
495			fi
496		else
497			used_wports=$(($used_wports | $5))
498			vlan_create_br_and_vif $3 $4
499			sw_configvlan "vlan" "add" "vlan" $3 $5 $4
500		fi
501	done
502	if [ "$i_vid" = "0" ]; then
503		i_vid=$(vlan_get_wanvid) && i_pri=0
504		vlan_create_internet_vif $i_vid $i_pri
505		sw_configvlan "vlan" "add" "wan" "$i_vid" "0" "$i_pri"
506	else
507		vlan_create_internet_vif $i_vid $i_pri
508		sw_configvlan "vlan" "add" "br" "$i_vid" "0" "$i_pri"
509	fi
510	sw_configvlan "vlan" "add" "lan" "$lanvid" $(($used_wports ^ 0xf)) "0"
511	sw_configvlan "vlan" "end"
512}
513
514op_create_brs_and_vifs()
515{
516	local opmode=$($CONFIG get i_opmode)
517
518	op_del_all_brs_vifs  2>/dev/null
519	case "$opmode" in
520	normal) normal_create_brs_and_vifs ;;
521	iptv) iptv_create_brs_and_vifs ;;
522	vlan) vlan_create_brs_and_vifs ;;
523	factory) factory_mode_create_brs_and_vifs ;;
524	*) apmode_create_brs_and_vifs ;;
525	esac
526}
527
528fi #-------------------- this must be the last line -----------------------------
529