blob: 209ed189e92e035b08dab9fb46526625bf635aed [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -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 ifconfig $wan_if $pswan_ip netmask 255.255.255.0 up 2>>$test_log
114 if [ $? -ne 0 ];then
115 echo "Error: ifconfig $wan_if $pswan_ip up failed." >> $test_log
116 fi
117
118 pswan_pri=`nv get pswan_priority`
119 rt_num=`expr $pswan_pri \* 10 + $c_id`
120 ip rule add from $pswan_ip table $rt_num 2>>$test_log
121 if [ $? -ne 0 ];then
122 echo "Error: ip rule add from $pswan_ip table $rt_num failed." >> $test_log
123 fi
124 ip route add default via $pswan_gw table $rt_num 2>>$test_log
125 if [ $? -ne 0 ];then
126 echo "Error: ip route add default via $pswan_gw table $rt_num failed." >> $test_log
127 fi
128 ip route flush cache 2>>$test_log
129 if [ $? -ne 0 ];then
130 echo "Error: ip route flush cache failed." >> $test_log
131 fi
132
133 msg_zte_router
134
135 elif [ "-$wan_mode" == "-auto" ]; then
136 pppoe_user=`nv get pppoe_username`
137 pppoe_pass=`nv get pppoe_cc`
138 udhcpc_kill
139 pppoe_kill
140
141 if [ "-$wan_name" == "-ethwan" ]; then
142 nv set eth_curmode=""
143 fi
144 if [ "-$wan_name" != "-wifiwan" ]; then
145 ifconfig $wan_if down 2>>$test_log
146 if [ $? -ne 0 ];then
147 echo "Error: ifconfig $wan_if down failed." >> $test_log
148 fi
149 ifconfig $wan_if up 2>>$test_log
150 if [ $? -ne 0 ];then
151 echo "Error: ifconfig $wan_if up failed." >> $test_log
152 fi
153 fi
154
155 if [ "-${pppoe_user}" = "-" -a "-${pppoe_pass}" = "-" ];then
156 echo "auto wan_mode: pppoe_user is ${pppoe_user}, pppoe_pass is ${pppoe_pass}, so start dhcp client. " >> $test_log
157 udhcpc -i $wan_if -s $path_sh/udhcpc.sh &
158 else
159 sh $path_sh/pppoe_dail.sh connect
160 fi
161
162 elif [ "-$wan_mode" == "-dhcp" ]; then
163 udhcpc_kill
164 pppoe_kill
165
166 if [ "-$wan_name" == "-pswan" ]; then
167 ifconfig $wan_if arp 2>>$test_log
168 if [ $? -ne 0 ];then
169 echo "Error: ifconfig $wan_if arp failed." >> $test_log
170 fi
171 fi
172
173 if [ "-$wan_name" == "-ethwan" ]; then
174 nv set eth_curmode="dhcp"
175 fi
176
177 if [ "-$wan_name" != "-wifiwan" ]; then
178 ifconfig $wan_if down 2>>$test_log
179 if [ $? -ne 0 ];then
180 echo "Error: ifconfig $wan_if down failed." >> $test_log
181 fi
182 ifconfig $wan_if up 2>>$test_log
183 if [ $? -ne 0 ];then
184 echo "Error: ifconfig $wan_if up failed." >> $test_log
185 fi
186 fi
187 udhcpc -i $wan_if -s $path_sh/udhcpc.sh &
188
189 elif [ "-$wan_mode" == "-pppoe" ]; then
190 udhcpc_kill
191 pppoe_kill
192
193 if [ "-$wan_name" == "-ethwan" ]; then
194 nv set eth_curmode="pppoe"
195 fi
196
197 if [ "-$wan_name" != "-wifiwan" ]; then
198 ifconfig $wan_if down 2>>$test_log
199 if [ $? -ne 0 ];then
200 echo "Error: ifconfig $wan_if down failed." >> $test_log
201 fi
202 ifconfig $wan_if up 2>>$test_log
203 if [ $? -ne 0 ];then
204 echo "Error: ifconfig $wan_if up failed." >> $test_log
205 fi
206 fi
207 sh $path_sh/pppoe_dail.sh connect
208 fi
209
210}
211
212del_default_wan()
213{
214 default_wan_name=`nv get default_wan_name`
215 if [ "$1" == "$default_wan_name" ]; then
216 nv set default_wan_name=""
217 nv set default_wan_rel=""
218 fi
219}
220
221linkdown()
222{
223 wan_name=$1
224 get_wan_if $1 $2
225
226 udhcpc_kill
227 pppoe_kill
228
229 #if [ "-$c_id" == "-0" -o "-$c_id" == "-$def_cid" ]; then
230 #echo 0 > /proc/sys/net/ipv4/ip_forward
231 #fi
232
233 ifconfig $wan_if 0.0.0.0 2>>$test_log
234 if [ $? -ne 0 ];then
235 echo "Error: ifconfig $wan_if 0.0.0.0 failed." >> $test_log
236 fi
237 if [ "$wan_name" == "wifiwan" ]; then
238 echo $wan_if > /proc/net/dev_down
239 else
240 ifconfig $wan_if down 2>>$test_log
241 if [ $? -ne 0 ];then
242 echo "Error: ifconfig $wan_if down failed." >> $test_log
243 fi
244 fi
245 wan_ip=`nv get $wan_if"_ip"`
246 wan_gw=`nv get $wan_if"_gw"`
247
248 route del default gw $wan_gw dev $wan_if
249 #if [ $? -ne 0 ];then
250 # echo "Error: route del default gw $wan_gw dev $wan_if failed." >> $test_log
251 #fi
252
253 #del policy router
254 wan_pri=`nv get $1"_priority"`
255 rt_num=`expr $wan_pri \* 10 + $c_id`
256 ip rule del from $wan_ip table $rt_num
257 #if [ $? -ne 0 ];then
258 # echo "Error: ip rule del from $wan_ip table $rt_num failed." >> $test_log
259 #fi
260 ip route del default via $wan_gw table $rt_num
261 #if [ $? -ne 0 ];then
262 # echo "Error: ip route del default via $wan_gw table $rt_num failed." >> $test_log
263 #fi
264
265 #wan_ipaddÐèÒªÊÊÅä
266 if [ "$wan_if" == "$defwan_if" ]; then
267 nv set wan_ipaddr=""
268 fi
269 nv set $wan_if"_ip"=0.0.0.0
270 nv set $wan_if"_nm"=0.0.0.0
271 nv set $wan_if"_gw"=0.0.0.0
272 nv set $wan_if"_pridns"=0.0.0.0
273 nv set $wan_if"_secdns"=0.0.0.0
274 pdp_type=`nv get "pdp_act_type"$def_cid`
275 if [ "$2" == "$def_cid" -a "-$pdp_type" != "-IPv4v6" ]; then
276 nv set default_cid=""
277 fi
278 del_default_wan $wan_if
279 msg_zte_router
280
281}
282
283echo "Info: wan_ipv4.sh $1 $2 $3 start" >> $test_log
284if [ "$1" == "linkup" ]; then
285 linkup $2 $3
286
287elif [ "$1" == "linkdown" ]; then
288 if [ "-$2" == "-pswan" ]; then
289 tc_tbf.sh down $def_cid
290 fi
291 linkdown $2 $3
292fi