blob: 047373890aa223bf1b646d283f3c322227b7c3ce [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#!/bin/sh
2#usg: wan_ipv4 linkup pswan
3
4path_sh=`nv get path_sh`
5. $path_sh/global.sh
6
7#Èë²ÎÖÐÓÐc_idÔòÉèÖã¬Ã»ÓÐÔòÖÃÁ㣬ÓÃÓÚÉèÖòßÂÔ·ÓɱíºÅ
8if [ "-$3" == "-" ]; then
9 c_id="0"
10else
11 c_id=$3
12fi
13def_cid=`nv get default_cid`
14
15#»ñÈ¡ÍøÂçÉ豸Ãû
16get_wan_if()
17{
18 case $1 in
19 "pswan")
20 wan_if="$pswan_name"$2 ;;
21 "ethwan")
22 wan_if=$ethwan_if ;;
23 "wifiwan")
24 wan_if=$wifiwan_if ;;
25 esac
26
27 mtu=`nv get mtu`
28 ifconfig $wan_if mtu $mtu
29}
30#ÉèÖù¤×÷״̬ ÕýÔÚÁ¬½Ó£ºconnect ÒÑÁ¬½Ó£ºworking ÒѶϿª£ºdead
31state_set()
32{
33 if [ "-$wan_name" == "-wifiwan" ]; then
34 nv set wifi_state="working"
35 elif [ "-$wan_name" == "-ethwan" ]; then
36 nv set rj45_state="working"
37 fi
38}
39#¸ù¾ÝÍø¿ÚÊÇ·ñÓÃÓÚÆÕͨÉÏÍâÍø£¬È·¶¨ÊÇ·ñÐèÒª·¢ÏûÏ¢¸øzte_router
40msg_zte_router()
41{
42 if [ "-$c_id" == "-0" -o "-$c_id" == "-$def_cid" ]; then
xf.liaa4d92f2023-09-13 00:18:58 -070043 (router_msg_proxy ipv4 $c_id >> $test_log 2>&1 || echo "Error: router_msg_proxy ipv4 wan_ipv4.sh up failed." >> $test_log) &
44 fi
45}
46
47msg_zte_router_down()
48{
49 if [ "-$c_id" == "-0" -o "-$c_id" == "-$def_cid" ]; then
50 (router_msg_proxy ipv4 0 >> $test_log 2>&1 || echo "Error: router_msg_proxy ipv4 wan_ipv4.sh down failed." >> $test_log) &
lh9ed821d2023-04-07 01:36:19 -070051 fi
52}
53
54linkup()
55{
56 #$1 :pswan / usbwan / ethwan / wifiwan
57 wan_name=$1
58 wan_mode=`nv get $wan_name"_mode"`
59
60 #»ñÈ¡wan_if±äÁ¿
61 get_wan_if $1 $2
62
63 if [ "-$wan_mode" == "-static" ]; then
64 wan_ip=`nv get "static_"$wan_name"_ip"`
65 wan_gw=`nv get "static_"$wan_name"_gw"`
66 wan_pridns=`nv get "static_"$wan_name"_pridns"`
67 wan_secdns=`nv get "static_"$wan_name"_secdns"`
68 wan_nm=`nv get "static_"$wan_name"_nm"`
69 if [ "-$wan_name" != "-wifiwan" ]; then
70 ifconfig $wan_if down 2>>$test_log
71 if [ $? -ne 0 ];then
72 echo "Error: ifconfig $wan_if down failed." >> $test_log
73 fi
74 ifconfig $wan_if $wan_ip up 2>>$test_log
75 if [ $? -ne 0 ];then
76 echo "Error: ifconfig $wan_if $wan_ip up failed." >> $test_log
77 fi
78 fi
79 if [ "-$wan_name" == "-ethwan" ]; then
80 nv set eth_curmode="static"
81 #rj45¾²Ì¬Á¬½Ó£¬¹Ø±Õ³¬Ê±¼ÆÊ±Æ÷
82 (router_msg_proxy del_timer ethwan >> $test_log 2>&1 || echo "Error: router_msg_proxy del_timer failed." >> $test_log) &
83 fi
84
85 #ÉèÖÃÍøÂçÉ豸״̬±ê־Ϊworking
86 state_set
87 #set ip¡¢gw¡¢dns to nv
88 nv set $wan_if"_ip"=$wan_ip
89 nv set $wan_if"_gw"=$wan_gw
90 nv set $wan_if"_pridns"=$wan_pridns
91 nv set $wan_if"_secdns"=$wan_secdns
92
93 wan_pri=`nv get $1"_priority"`
94 rt_num=`expr $wan_pri \* 10 + $c_id`
95 ip rule add from $wan_ip table $rt_num 2>>$test_log
96 if [ $? -ne 0 ];then
97 echo "Error: ip rule add from $wan_ip table $rt_num failed." >> $test_log
98 fi
99 ip route add default via $wan_gw table $rt_num 2>>$test_log
100 if [ $? -ne 0 ];then
101 echo "Error: ip route add default via $wan_gw table $rt_num failed." >> $test_log
102 fi
103 ip route flush cache 2>>$test_log
104 if [ $? -ne 0 ];then
105 echo "Error: ip route flush cache failed." >> $test_log
106 fi
107
108 #in this proxy ,we send msg to zte_mainctrl
109 msg_zte_router
110
111 # pswan PDP
112 elif [ "-$wan_mode" == "-pdp" ]; then
113 pswan_ip=`nv get $wan_if"_ip"`
114 pswan_gw=`nv get $wan_if"_gw"`
115 pswan_pridns=`nv get $wan_if"_pridns"`
116 pswan_secdns=`nv get $wan_if"_secdns"`
117 pswan_nm=`nv get $wan_if"_nm"`
118
xf.lib5d48992024-01-24 23:56:18 -0800119# #ifconfig $wan_if down 2>>$test_log
120# ifconfig $wan_if $pswan_ip netmask 255.255.255.0 up 2>>$test_log
121# if [ $? -ne 0 ];then
122# echo "Error: ifconfig $wan_if $pswan_ip up failed." >> $test_log
123# fi
lh9ed821d2023-04-07 01:36:19 -0700124
125 pswan_pri=`nv get pswan_priority`
126 rt_num=`expr $pswan_pri \* 10 + $c_id`
127 ip rule add from $pswan_ip table $rt_num 2>>$test_log
128 if [ $? -ne 0 ];then
129 echo "Error: ip rule add from $pswan_ip table $rt_num failed." >> $test_log
130 fi
131 ip route add default via $pswan_gw table $rt_num 2>>$test_log
132 if [ $? -ne 0 ];then
133 echo "Error: ip route add default via $pswan_gw table $rt_num failed." >> $test_log
134 fi
135 ip route flush cache 2>>$test_log
136 if [ $? -ne 0 ];then
137 echo "Error: ip route flush cache failed." >> $test_log
138 fi
139
140 msg_zte_router
141
142 elif [ "-$wan_mode" == "-auto" ]; then
143 pppoe_user=`nv get pppoe_username`
144 pppoe_pass=`nv get pppoe_cc`
145 udhcpc_kill
146 pppoe_kill
147
148 if [ "-$wan_name" == "-ethwan" ]; then
149 nv set eth_curmode=""
150 fi
151 if [ "-$wan_name" != "-wifiwan" ]; then
152 ifconfig $wan_if down 2>>$test_log
153 if [ $? -ne 0 ];then
154 echo "Error: ifconfig $wan_if down failed." >> $test_log
155 fi
156 ifconfig $wan_if up 2>>$test_log
157 if [ $? -ne 0 ];then
158 echo "Error: ifconfig $wan_if up failed." >> $test_log
159 fi
160 fi
161
162 if [ "-${pppoe_user}" = "-" -a "-${pppoe_pass}" = "-" ];then
163 echo "auto wan_mode: pppoe_user is ${pppoe_user}, pppoe_pass is ${pppoe_pass}, so start dhcp client. " >> $test_log
164 udhcpc -i $wan_if -s $path_sh/udhcpc.sh &
165 else
166 sh $path_sh/pppoe_dail.sh connect
167 fi
168
169 elif [ "-$wan_mode" == "-dhcp" ]; then
170 udhcpc_kill
171 pppoe_kill
172
173 if [ "-$wan_name" == "-pswan" ]; then
174 ifconfig $wan_if arp 2>>$test_log
175 if [ $? -ne 0 ];then
176 echo "Error: ifconfig $wan_if arp failed." >> $test_log
177 fi
178 fi
179
180 if [ "-$wan_name" == "-ethwan" ]; then
181 nv set eth_curmode="dhcp"
182 fi
183
184 if [ "-$wan_name" != "-wifiwan" ]; then
185 ifconfig $wan_if down 2>>$test_log
186 if [ $? -ne 0 ];then
187 echo "Error: ifconfig $wan_if down failed." >> $test_log
188 fi
189 ifconfig $wan_if up 2>>$test_log
190 if [ $? -ne 0 ];then
191 echo "Error: ifconfig $wan_if up failed." >> $test_log
192 fi
193 fi
194 udhcpc -i $wan_if -s $path_sh/udhcpc.sh &
195
196 elif [ "-$wan_mode" == "-pppoe" ]; then
197 udhcpc_kill
198 pppoe_kill
199
200 if [ "-$wan_name" == "-ethwan" ]; then
201 nv set eth_curmode="pppoe"
202 fi
203
204 if [ "-$wan_name" != "-wifiwan" ]; then
205 ifconfig $wan_if down 2>>$test_log
206 if [ $? -ne 0 ];then
207 echo "Error: ifconfig $wan_if down failed." >> $test_log
208 fi
209 ifconfig $wan_if up 2>>$test_log
210 if [ $? -ne 0 ];then
211 echo "Error: ifconfig $wan_if up failed." >> $test_log
212 fi
213 fi
214 sh $path_sh/pppoe_dail.sh connect
215 fi
216
217}
218
219del_default_wan()
220{
221 default_wan_name=`nv get default_wan_name`
222 if [ "$1" == "$default_wan_name" ]; then
223 nv set default_wan_name=""
224 nv set default_wan_rel=""
225 fi
226}
227
228linkdown()
229{
230 wan_name=$1
231 get_wan_if $1 $2
232
233 udhcpc_kill
234 pppoe_kill
235
xf.lif1aed282024-02-06 00:31:51 -0800236 #if [ "-$c_id" == "-0" -o "-$c_id" == "-$def_cid" ]; then
237 #echo 0 > /proc/sys/net/ipv4/ip_forward
238 #fi
lh9ed821d2023-04-07 01:36:19 -0700239
240 ifconfig $wan_if 0.0.0.0 2>>$test_log
241 if [ $? -ne 0 ];then
242 echo "Error: ifconfig $wan_if 0.0.0.0 failed." >> $test_log
243 fi
244 if [ "$wan_name" == "wifiwan" ]; then
245 echo $wan_if > /proc/net/dev_down
246 else
247 ifconfig $wan_if down 2>>$test_log
248 if [ $? -ne 0 ];then
249 echo "Error: ifconfig $wan_if down failed." >> $test_log
250 fi
251 fi
252 wan_ip=`nv get $wan_if"_ip"`
253 wan_gw=`nv get $wan_if"_gw"`
254
255 route del default gw $wan_gw dev $wan_if
256 #if [ $? -ne 0 ];then
257 # echo "Error: route del default gw $wan_gw dev $wan_if failed." >> $test_log
258 #fi
259
260 #del policy router
261 wan_pri=`nv get $1"_priority"`
262 rt_num=`expr $wan_pri \* 10 + $c_id`
263 ip rule del from $wan_ip table $rt_num
264 #if [ $? -ne 0 ];then
265 # echo "Error: ip rule del from $wan_ip table $rt_num failed." >> $test_log
266 #fi
267 ip route del default via $wan_gw table $rt_num
268 #if [ $? -ne 0 ];then
269 # echo "Error: ip route del default via $wan_gw table $rt_num failed." >> $test_log
270 #fi
271
272 #wan_ipaddÐèÒªÊÊÅä
273 if [ "$wan_if" == "$defwan_if" ]; then
274 nv set wan_ipaddr=""
275 fi
276 nv set $wan_if"_ip"=0.0.0.0
277 nv set $wan_if"_nm"=0.0.0.0
278 nv set $wan_if"_gw"=0.0.0.0
279 nv set $wan_if"_pridns"=0.0.0.0
280 nv set $wan_if"_secdns"=0.0.0.0
281 pdp_type=`nv get "pdp_act_type"$def_cid`
282 if [ "$2" == "$def_cid" -a "-$pdp_type" != "-IPv4v6" ]; then
283 nv set default_cid=""
284 fi
285 del_default_wan $wan_if
xf.liaa4d92f2023-09-13 00:18:58 -0700286 msg_zte_router_down
lh9ed821d2023-04-07 01:36:19 -0700287
288}
289
290echo "Info: wan_ipv4.sh $1 $2 $3 start" >> $test_log
291if [ "$1" == "linkup" ]; then
292 linkup $2 $3
293
294elif [ "$1" == "linkdown" ]; then
295 if [ "-$2" == "-pswan" ]; then
296 tc_tbf.sh down $def_cid
297 fi
298 linkdown $2 $3
xf.lib5d48992024-01-24 23:56:18 -0800299fi