blob: e5f7446229c529b8259fe2cfb07b770e20eb798f [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
43 (router_msg_proxy ipv4 wan_ipv4.sh >> $test_log 2>&1 || echo "Error: router_msg_proxy ipv4 wan_ipv4.sh failed." >> $test_log) &
44 fi
45}
46
47linkup()
48{
49 #$1 :pswan / usbwan / ethwan / wifiwan
50 wan_name=$1
51 wan_mode=`nv get $wan_name"_mode"`
52
53 #»ñÈ¡wan_if±äÁ¿
54 get_wan_if $1 $2
55
56 if [ "-$wan_mode" == "-static" ]; then
57 wan_ip=`nv get "static_"$wan_name"_ip"`
58 wan_gw=`nv get "static_"$wan_name"_gw"`
59 wan_pridns=`nv get "static_"$wan_name"_pridns"`
60 wan_secdns=`nv get "static_"$wan_name"_secdns"`
61 wan_nm=`nv get "static_"$wan_name"_nm"`
62 if [ "-$wan_name" != "-wifiwan" ]; then
63 ifconfig $wan_if down 2>>$test_log
64 if [ $? -ne 0 ];then
65 echo "Error: ifconfig $wan_if down failed." >> $test_log
66 fi
67 ifconfig $wan_if $wan_ip up 2>>$test_log
68 if [ $? -ne 0 ];then
69 echo "Error: ifconfig $wan_if $wan_ip up failed." >> $test_log
70 fi
71 fi
72 if [ "-$wan_name" == "-ethwan" ]; then
73 nv set eth_curmode="static"
74 #rj45¾²Ì¬Á¬½Ó£¬¹Ø±Õ³¬Ê±¼ÆÊ±Æ÷
75 (router_msg_proxy del_timer ethwan >> $test_log 2>&1 || echo "Error: router_msg_proxy del_timer failed." >> $test_log) &
76 fi
77
78 #ÉèÖÃÍøÂçÉ豸״̬±ê־Ϊworking
79 state_set
80 #set ip¡¢gw¡¢dns to nv
81 nv set $wan_if"_ip"=$wan_ip
82 nv set $wan_if"_gw"=$wan_gw
83 nv set $wan_if"_pridns"=$wan_pridns
84 nv set $wan_if"_secdns"=$wan_secdns
85
86 wan_pri=`nv get $1"_priority"`
87 rt_num=`expr $wan_pri \* 10 + $c_id`
88 ip rule add from $wan_ip table $rt_num 2>>$test_log
89 if [ $? -ne 0 ];then
90 echo "Error: ip rule add from $wan_ip table $rt_num failed." >> $test_log
91 fi
92 ip route add default via $wan_gw table $rt_num 2>>$test_log
93 if [ $? -ne 0 ];then
94 echo "Error: ip route add default via $wan_gw table $rt_num failed." >> $test_log
95 fi
96 ip route flush cache 2>>$test_log
97 if [ $? -ne 0 ];then
98 echo "Error: ip route flush cache failed." >> $test_log
99 fi
100
101 #in this proxy ,we send msg to zte_mainctrl
102 msg_zte_router
103
104 # pswan PDP
105 elif [ "-$wan_mode" == "-pdp" ]; then
106 pswan_ip=`nv get $wan_if"_ip"`
107 pswan_gw=`nv get $wan_if"_gw"`
108 pswan_pridns=`nv get $wan_if"_pridns"`
109 pswan_secdns=`nv get $wan_if"_secdns"`
110 pswan_nm=`nv get $wan_if"_nm"`
111
112 ifconfig $wan_if down 2>>$test_log
113 if [ $? -ne 0 ];then
114 echo "Error: ifconfig $wan_if down failed." >> $test_log
115 fi
116 ifconfig $wan_if $pswan_ip up 2>>$test_log
117 if [ $? -ne 0 ];then
118 echo "Error: ifconfig $wan_if $pswan_ip up failed." >> $test_log
119 fi
120
121 pswan_pri=`nv get pswan_priority`
122 rt_num=`expr $pswan_pri \* 10 + $c_id`
123 ip rule add from $pswan_ip table $rt_num 2>>$test_log
124 if [ $? -ne 0 ];then
125 echo "Error: ip rule add from $pswan_ip table $rt_num failed." >> $test_log
126 fi
127 ip route add default via $pswan_gw table $rt_num 2>>$test_log
128 if [ $? -ne 0 ];then
129 echo "Error: ip route add default via $pswan_gw table $rt_num failed." >> $test_log
130 fi
131 ip route flush cache 2>>$test_log
132 if [ $? -ne 0 ];then
133 echo "Error: ip route flush cache failed." >> $test_log
134 fi
135
136 msg_zte_router
137
138 elif [ "-$wan_mode" == "-auto" ]; then
139 pppoe_user=`nv get pppoe_username`
140 pppoe_pass=`nv get pppoe_cc`
141 udhcpc_kill
142 pppoe_kill
143
144 if [ "-$wan_name" == "-ethwan" ]; then
145 nv set eth_curmode=""
146 fi
147 if [ "-$wan_name" != "-wifiwan" ]; then
148 ifconfig $wan_if down 2>>$test_log
149 if [ $? -ne 0 ];then
150 echo "Error: ifconfig $wan_if down failed." >> $test_log
151 fi
152 ifconfig $wan_if up 2>>$test_log
153 if [ $? -ne 0 ];then
154 echo "Error: ifconfig $wan_if up failed." >> $test_log
155 fi
156 fi
157
158 if [ "-${pppoe_user}" = "-" -a "-${pppoe_pass}" = "-" ];then
159 echo "auto wan_mode: pppoe_user is ${pppoe_user}, pppoe_pass is ${pppoe_pass}, so start dhcp client. " >> $test_log
160 udhcpc -i $wan_if -s $path_sh/udhcpc.sh &
161 else
162 sh $path_sh/pppoe_dail.sh connect
163 fi
164
165 elif [ "-$wan_mode" == "-dhcp" ]; then
166 udhcpc_kill
167 pppoe_kill
168
169 if [ "-$wan_name" == "-pswan" ]; then
170 ifconfig $wan_if arp 2>>$test_log
171 if [ $? -ne 0 ];then
172 echo "Error: ifconfig $wan_if arp failed." >> $test_log
173 fi
174 fi
175
176 if [ "-$wan_name" == "-ethwan" ]; then
177 nv set eth_curmode="dhcp"
178 fi
179
180 if [ "-$wan_name" != "-wifiwan" ]; then
181 ifconfig $wan_if down 2>>$test_log
182 if [ $? -ne 0 ];then
183 echo "Error: ifconfig $wan_if down failed." >> $test_log
184 fi
185 ifconfig $wan_if up 2>>$test_log
186 if [ $? -ne 0 ];then
187 echo "Error: ifconfig $wan_if up failed." >> $test_log
188 fi
189 fi
190 udhcpc -i $wan_if -s $path_sh/udhcpc.sh &
191
192 elif [ "-$wan_mode" == "-pppoe" ]; then
193 udhcpc_kill
194 pppoe_kill
195
196 if [ "-$wan_name" == "-ethwan" ]; then
197 nv set eth_curmode="pppoe"
198 fi
199
200 if [ "-$wan_name" != "-wifiwan" ]; then
201 ifconfig $wan_if down 2>>$test_log
202 if [ $? -ne 0 ];then
203 echo "Error: ifconfig $wan_if down failed." >> $test_log
204 fi
205 ifconfig $wan_if up 2>>$test_log
206 if [ $? -ne 0 ];then
207 echo "Error: ifconfig $wan_if up failed." >> $test_log
208 fi
209 fi
210 sh $path_sh/pppoe_dail.sh connect
211 fi
212
213}
214
215del_default_wan()
216{
217 default_wan_name=`nv get default_wan_name`
218 if [ "$1" == "$default_wan_name" ]; then
219 nv set default_wan_name=""
220 nv set default_wan_rel=""
221 fi
222}
223
224linkdown()
225{
226 wan_name=$1
227 get_wan_if $1 $2
228
229 udhcpc_kill
230 pppoe_kill
231
232 if [ "-$c_id" == "-0" -o "-$c_id" == "-$def_cid" ]; then
233 echo 0 > /proc/sys/net/ipv4/ip_forward
234 fi
235
236 ifconfig $wan_if 0.0.0.0 2>>$test_log
237 if [ $? -ne 0 ];then
238 echo "Error: ifconfig $wan_if 0.0.0.0 failed." >> $test_log
239 fi
240 if [ "$wan_name" == "wifiwan" ]; then
241 echo $wan_if > /proc/net/dev_down
242 else
243 ifconfig $wan_if down 2>>$test_log
244 if [ $? -ne 0 ];then
245 echo "Error: ifconfig $wan_if down failed." >> $test_log
246 fi
247 fi
248 wan_ip=`nv get $wan_if"_ip"`
249 wan_gw=`nv get $wan_if"_gw"`
250
251 route del default gw $wan_gw dev $wan_if
252 #if [ $? -ne 0 ];then
253 # echo "Error: route del default gw $wan_gw dev $wan_if failed." >> $test_log
254 #fi
255
256 #del policy router
257 wan_pri=`nv get $1"_priority"`
258 rt_num=`expr $wan_pri \* 10 + $c_id`
259 ip rule del from $wan_ip table $rt_num
260 #if [ $? -ne 0 ];then
261 # echo "Error: ip rule del from $wan_ip table $rt_num failed." >> $test_log
262 #fi
263 ip route del default via $wan_gw table $rt_num
264 #if [ $? -ne 0 ];then
265 # echo "Error: ip route del default via $wan_gw table $rt_num failed." >> $test_log
266 #fi
267
268 #wan_ipaddÐèÒªÊÊÅä
269 if [ "$wan_if" == "$defwan_if" ]; then
270 nv set wan_ipaddr=""
271 fi
272 nv set $wan_if"_ip"=0.0.0.0
273 nv set $wan_if"_nm"=0.0.0.0
274 nv set $wan_if"_gw"=0.0.0.0
275 nv set $wan_if"_pridns"=0.0.0.0
276 nv set $wan_if"_secdns"=0.0.0.0
277 pdp_type=`nv get "pdp_act_type"$def_cid`
278 if [ "$2" == "$def_cid" -a "-$pdp_type" != "-IPv4v6" ]; then
279 nv set default_cid=""
280 fi
281 del_default_wan $wan_if
282 msg_zte_router
283
284}
285
286echo "Info: wan_ipv4.sh $1 $2 $3 start" >> $test_log
287if [ "$1" == "linkup" ]; then
288 linkup $2 $3
289
290elif [ "$1" == "linkdown" ]; then
291 if [ "-$2" == "-pswan" ]; then
292 tc_tbf.sh down $def_cid
293 fi
294 linkdown $2 $3
295fi