blob: 1117d1c72ec1a27941a5366436e9c5486e623ea0 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#!/bin/sh
2
3path_sh=`nv get path_sh`
4. $path_sh/global.sh
5echo "Info: psext_updown.sh $1 $2 start" >> $test_log
6
7echo 1 > /proc/sys/net/ipv4/ip_forward
8c_id=$2
9ps_if=`nv get pswan`$c_id
10eth_if=`nv get "ps_ext"$c_id`
11ext_br="br"$c_id
12ps_ext_mode=`nv get need_jilian`
13#Óû§ÉèÖþ²Ì¬ip¡¢gw£¬nmÔÚ´Ë´¦¼ÆËã
14arp_proxy_kill()
15{
16 ps > ${path_tmp}/zte_arp_proxy.$ext_br.$$
17 arp_proxy_pid=`awk 'BEGIN{temp1="'"${ext_br}"'";temp2="zte_arp_proxy"}{if(index($0,temp1)>0 && index($0,temp2)>0){print $1}}' ${path_tmp}/zte_arp_proxy.$ext_br.$$`
18 rm -f ${path_tmp}/zte_arp_proxy.$ext_br.$$
19 [ -n "$arp_proxy_pid" ] && { kill $arp_proxy_pid; echo "test: kill udhcpc $arp_proxy_pid " >> $test_log ; }
20}
21
22arp_proxy_set()
23{
24 arp_proxy_kill
25 (zte_arp_proxy -i $ext_br 2>> $test_log || echo "Error: zte_arp_proxy -i $ext_br failed." >> $test_log) &
26}
27
28get_mask()
29{
30 mask=`echo ${ps_ip}"."${gw} | awk -F '.' '{
31 nm1=0;nm2=0;nm3=0
32 if($1 == $5)
33 nm1=255
34
35 if(255 == nm1 && $2 == $6)
36 nm2=255
37
38 if(255 == nm2 && $3 == $7)
39 nm3=255
40
41 printf nm1"."nm2"."nm3".0"
42 }'`
43}
44
45dhcp_set()
46{
47
48 start=$pdp_ip
49 end=$pdp_ip
50 #nvÖµ·Ç¿ÕÔò±íʾÓû§ÉèÖÃÁ˾²Ì¬ip¡¢gw
51 gw=`nv get $ps_if"_gw"`
52 if [ "-$gw" == "-" -o "-$gw" == "-0.0.0.0" ]; then
53 gw=$br_ip
54 mask=$valid_mask
55 echo psip $ps_ip br_ip $br_ip valid_mask $valid_mask $mask
56 else
57 echo "no mask"
58 get_mask
59 fi
60 nv set $ext_br"_nm"=$mask
61 ifconfig $ext_br netmask $mask 2>>$test_log
62 if [ $? -ne 0 ];then
63 echo "Error: ifconfig $eth_br netmask $mask failed." >> $test_log
64 fi
65 dns=`nv get $ps_if"_pridns"`
66 dns2=`nv get $ps_if"_secdns"`
67 lease="86400"
68
69 #±¾µØÍøÂçÅäÖÃ
70 if [ "-$dns" == "-" -o "-$dns" == "-0.0.0.0" ] && [ "-$dns2" == "-" -o "-$dns2" == "-0.0.0.0" ]; then
71 echo "Error: no valid dns, use default dns." >> $test_log
72 dns="114.114.114.114"
73 dns2="8.8.8.8"
74 fi
75 rm -rf $path_conf"/udhcpd"$c_id".conf"
76 touch $path_conf"/udhcpd"$c_id".conf"
77 echo " " > $path_conf"/udhcpd"$c_id".conf"
78
79 if [ "-$dns" != "-" -a "-$dns" != "-0.0.0.0" ]; then
80 echo "nameserver $dns" >> /etc/resolv.conf
81 fi
82 if [ "-$dns2" != "-" -a "-$dns2" != "-0.0.0.0" ]; then
83 echo "nameserver $dns2" >> /etc/resolv.conf
84 fi
85
86 pidfile=$path_conf"/udhcpd"$c_id".pid"
87 leasesfile=$path_conf"/udhcpd"$c_id".leases"
88
89 sh $path_sh/config-udhcpd.sh $c_id -s $start
90 sh $path_sh/config-udhcpd.sh $c_id -e $end
91 sh $path_sh/config-udhcpd.sh $c_id -i $ext_br
92 sh $path_sh/config-udhcpd.sh $c_id -m $mask
93 sh $path_sh/config-udhcpd.sh $c_id -d $dns $dns2
94 if [ "-$gw" != "-" ]; then
95 sh $path_sh/config-udhcpd.sh $c_id -g $gw
96 fi
97 if [ "-$lease" != "-" ]; then
98 sh $path_sh/config-udhcpd.sh $c_id -t $lease
99 fi
100 sh $path_sh/config-udhcpd.sh $c_id -p $pidfile
101 sh $path_sh/config-udhcpd.sh $c_id -l $leasesfile
102 sh $path_sh/config-udhcpd.sh $c_id -r 1
103}
104get_validnum()
105{
106 last4=${ipaddr##*.}
107 ipaddr=${ipaddr%.*}
108 mark=`expr $last4 % 2`
109 last3=0
110 frag=`expr $frag + 1`
111 echo $ipaddr $last4 $last3
112 if [ $last4 -eq 0 ] || [ $last4 -eq 255 ];
113 then
114 last3=${ipaddr##*.}
115 fi
116 if [ $last4 -eq 0 ] && [ $last3 -eq 0 ];
117 then
118 get_validnum
119 elif [ $last4 -eq 255 ] && [ $last3 -eq 255 ];
120 then
121 get_validnum
122 else
123 return
124 fi
125}
126get_netmask_btrunk()
127{
128 pdpip=$1
129 pdp_ip4=${pdpip##*.}
130 front3=${pdpip%.*}
131
132 if [ $pdp_ip4 -eq 1 ];then
133 tmp_ip1=`expr $pdp_ip4 + 1`
134 tmp_ip2=`expr $pdp_ip4 + 2`
135 ps_ip="$front3.$tmp_ip1"
136 br_ip="$front3.$tmp_ip2"
137 elif [ $pdp_ip4 -eq 254 ];then
138 tmp_ip1=`expr $pdp_ip4 - 2`
139 tmp_ip2=`expr $pdp_ip4 - 1`
140 ps_ip="$front3.$tmp_ip1"
141 br_ip="$front3.$tmp_ip2"
142 else
143 tmp_ip1=`expr $pdp_ip4 - 1`
144 tmp_ip2=`expr $pdp_ip4 + 1`
145 ps_ip="$front3.$tmp_ip1"
146 br_ip="$front3.$tmp_ip2"
147 fi
148
149 nv set $ps_if"_ip"=$ps_ip
150 valid_mask="255.255.0.0"
151}
152
153get_netmask()
154{
155 ipaddr=$1
156 last4=0
157 last3=0
158 mark=0
159 frag=0
160
161 get_validnum
162 echo $ipaddr $last4 $last3 $mark $frag
163 #½«ÓÐЧ¶ÎÖÐÓÐЧλÖÃΪ1£¬¸Ãλ×ó±ß¶¼ÖÃΪ0
164 valid_num=0
165
166 if [ $mark -eq 1 -a $last3 -eq 0 ];then
167 valid_num=`expr $last4 + 1`
168 elif [ $mark -eq 0 -a $last3 -eq 0 ];then
169 valid_num=$last4
170 elif [ $mark -eq 1 -a $last3 -ne 0 ];then
171 valid_num=`expr $last3 + 1`
172 elif [ $mark -eq 0 -a $last3 -ne 0 ];then
173 valid_num=$last3
174 fi
175 echo $valid_num
176 #´Ó×óÏòÓÒÕÒµ½ÓÐЧ¶ÎÖеÚÒ»¸ö1µÄλÖÃ
177 place=1
178 while [ `expr $valid_num % 2` -eq 0 ]
179 do
180 place=`expr $place + 1`
181 valid_num=`expr $valid_num / 2`
182 done
183
184 if [ $last3 -eq 0 ]
185 then
186 frag=`expr $frag - 1`
187 fi
188 count=1
189
190 if [ $place -gt 8 ]
191 then
192 place=`expr $place - 8`
193 frag=`expr $frag + 1`
194 fi
195
196 tmp_place=$place
197 while [ $tmp_place -gt 0 ]
198 do
199 count=`expr $count \* 2`
200 tmp_place=`expr $tmp_place - 1`
201 done
202
203 #ÖÁÉÙÐèÒªÈý¸öÓÐЧipµØÖ·£¨ps¿Ú£¬pc£¬Íø¹Ø£©£¬ÑÚÂë×î´óΪ255.255.255.248
204 if [ $frag -eq 0 -a $place -eq 2 ]
205 then
206 count=`expr $count + 3`
207 else
208 count=`expr $count - 1`
209 fi
210
211 before=$frag
212
213 while [ $before -lt 3 ]
214 do
215 valid_mask=$valid_mask"255."
216 before=`expr $before + 1`
217 done
218 valid_mask=$valid_mask"`expr 255 - $count`"
219 while [ $frag -gt 0 ]
220 do
221 valid_mask=$valid_mask".0"
222 frag=`expr $frag - 1`
223 done
224
225 pdpip=$1
226 pdp_ip4=${pdpip##*.}
227 pdp_ip4_tmp=$pdp_ip4
228 front3=${pdpip%.*}
229
230 tmp_count=0
231 com_num=0
232 while [ $tmp_count -lt 3 ]
233 do
234 valid_count=0
235
236 if [ `expr $pdp_ip4_tmp % 2` -eq 1 ]
237 then
238 tmp_count1=$tmp_count
239 valid_count=1
240 while [ $tmp_count1 -gt 0 ]
241 do
242 valid_count=`expr $valid_count \* 2`
243 tmp_count1=`expr $tmp_count1 - 1`
244 done
245 fi
246 com_num=`expr $com_num + $valid_count`
247 tmp_count=`expr $tmp_count + 1`
248 pdp_ip4_tmp=`expr $pdp_ip4_tmp / 2`
249 done
250 echo com_num $com_num
251
252 if [ $com_num -gt 2 ]
253 then
254 tmp_ip1=`expr $pdp_ip4 - 1`
255 tmp_ip2=`expr $pdp_ip4 - 2`
256 ps_ip="$front3.$tmp_ip1"
257 br_ip="$front3.$tmp_ip2"
258 else
259 tmp_ip1=`expr $pdp_ip4 + 1`
260 tmp_ip2=`expr $pdp_ip4 + 2`
261 ps_ip="$front3.$tmp_ip1"
262 br_ip="$front3.$tmp_ip2"
263 fi
264
265 echo ps_ip $ps_ip br_ip $br_ip valid_mask $valid_mask
266 nv set $ps_if"_ip"=$ps_ip
267}
268#»ñÈ¡ip²¢ÅäÖÃps¡¢eth
269get_ipaddr()
270{
271 pdp_ip=`nv get $ps_if"_pdp_ip"`
272#ÅжÏÊÇ·ÊÇñbtrunkÐͺŻú
273 btrunk=`cat /sys/module/fast_common/parameters/btrunk_fw`
274 if [ "$btrunk" -eq "0" ];then
275 get_netmask $pdp_ip
276 else
277 get_netmask_btrunk $pdp_ip
278 fi
279
xf.lidf7f8ba2024-09-12 23:53:34 -0700280 ifconfig $ps_if $ps_ip up 2>>$test_log
281 wan_nm=`nv get static_wan_netmask`
282 ifconfig $ps_if netmask $wan_nm 2>>$test_log
lh9ed821d2023-04-07 01:36:19 -0700283 if [ $? -ne 0 ];then
284 echo "Error: ifconfig $ps_if $ps_ip up failed." >> $test_log
285 fi
286 nv set default_wan_rel=$ps_if
xf.li742dd022023-06-08 01:43:32 -0700287 #nv set default_cid=$c_id
lh9ed821d2023-04-07 01:36:19 -0700288 nv set $ext_br"_ip"=$br_ip
289 ifconfig $ext_br $br_ip 2>>$test_log
290 if [ $? -ne 0 ];then
291 echo "Error: ifconfig $ext_br $br_ip up failed." >> $test_log
292 fi
293}
294#·ÓɹæÔò£¬psÓëeth¼¶Áª
295route_set()
296{
297 marknum=`expr $c_id + 20`
298 iptables -t mangle -A PREROUTING -i $ps_if -j MARK --set-mark $marknum
299 rt_num=`expr $c_id + 120`
300
301 ip route add default dev $ext_br table $rt_num
302
303 ip rule add to $pdp_ip fwmark $marknum table $rt_num
304
305 marknum=`expr $c_id + 10`
306 iptables -t mangle -A PREROUTING -i $ext_br -j MARK --set-mark $marknum
307 rt_num=`expr $c_id + 100`
308
309 ip route add default dev $ps_if table $rt_num
310 ip rule add from $pdp_ip fwmark $marknum table $rt_num
311
312 ip route flush cache
313
314 #±¾µØÍøÂçÅäÖÃ
315 iptables -t nat -I POSTROUTING -s $ps_ip -o $ps_if -j SNAT --to $pdp_ip
316
xf.li742dd022023-06-08 01:43:32 -0700317 route_info=`ip route|grep default`
lh9ed821d2023-04-07 01:36:19 -0700318
319 if [ "$route_info" == "" ];then
320 route add default dev $ps_if
321 else
322 echo "Debug: default route already exist." >> $test_log
323 fi
324}
325#¹¹½¨ÍøÇÅ
326br_up()
327{
328 br="br"$c_id
329 brctl addbr $br
330 brctl setfd $br 0.1
xf.lidf7f8ba2024-09-12 23:53:34 -0700331 if [ "$eth_if" != "zvnet"$c_id ]; then
332 ifconfig $br up 2>>$test_log
333 else
334 ifconfig $br -arp up 2>>$test_log
335 fi
lh9ed821d2023-04-07 01:36:19 -0700336 if [ $? -ne 0 ];then
337 echo "Error: ifconfig $br up failed." >> $test_log
338 fi
339
340 ifconfig $ps_if up 2>>$test_log
341 if [ $? -ne 0 ];then
342 echo "Error: ifconfig $ps_if up failed." >> $test_log
343 fi
344 brctl addif $br $ps_if 2>>$test_log
345 if [ $? -ne 0 ];then
346 echo "Error: brctl addif $br $ps_if failed." >> $test_log
347 fi
348 brctl addif $br $eth_if 2>>$test_log
349 if [ $? -ne 0 ];then
350 echo "Error: brctl addif $br $eth_if failed." >> $test_log
351 fi
352 ifconfig $eth_if up 2>>$test_log
353 if [ $? -ne 0 ];then
354 echo "Error: ifconfig $eth_if up failed." >> $test_log
355 fi
356
357}
358#ɾ³ýÍøÇÅ
359br_down()
360{
361 br="br"$c_id
362 brctl delif $br $eth_if 2>>$test_log
363 if [ $? -ne 0 ];then
364 echo "Error: brctl delif $br $eth_if failed." >> $test_log
365 fi
366 ifconfig $eth_if down 2>>$test_log
367 if [ $? -ne 0 ];then
368 echo "Error: ifconfig $eth_if down failed." >> $test_log
369 fi
370 brctl delif $br $ps_if 2>>$test_log
371 if [ $? -ne 0 ];then
372 echo "Error: brctl delif $br $ps_if failed." >> $test_log
373 fi
374 ifconfig $ps_if down 2>>$test_log
375 if [ $? -ne 0 ];then
376 echo "Error: ifconfig $ps_if down failed." >> $test_log
377 fi
378 ifconfig $br down 2>>$test_log
379 if [ $? -ne 0 ];then
380 echo "Error: ifconfig $br down failed." >> $test_log
381 fi
382 brctl delbr $br 2>>$test_log
383 if [ $? -ne 0 ];then
384 echo "Error: brctl delbr $br failed." >> $test_log
385 fi
386}
387#ɱËÀ¶ÔÓ¦µÄDHCP½ø³Ì
388dhcp_kill()
389{
390 pidfile=$path_conf"/udhcpd"$c_id".pid"
391 kill `cat $pidfile`
392}
393#ɾ³ý¶ÔÓ¦µÄ·ÓɹæÔò
394route_del()
395{
396 pdp_ip=`nv get $ps_if"_pdp_ip"`
397# ps_ip1=${pdp_ip%.*}
398# ps_ip2=${pdp_ip##*.}
399# [ "$ps_ip2" -ge "254" ] && { ps_ip2="250"; }
400# ps_ip2=`expr $ps_ip2 + 1`
401 ps_ip=`nv get $ps_if"_ip"`
402 br_ip=`nv get $ext_br"_ip"`
403 marknum=`expr $c_id + 10`
404 rt_num=`expr $c_id + 100`
405
406 iptables -t mangle -D PREROUTING -i $ext_br -j MARK --set-mark $marknum
407 ip rule del from $pdp_ip fwmark $marknum table $rt_num
408 ip route del default dev $ps_if table $rt_num
409
410 marknum=`expr $c_id + 20`
411 rt_num=`expr $c_id + 120`
412 iptables -t mangle -D PREROUTING -i $ps_if -j MARK --set-mark $marknum
413 ip rule del to $pdp_ip fwmark $marknum table $rt_num
414 ip route del default dev $ext_br table $rt_num
415 #±¾µØÍøÂçÅäÖÃ
416 iptables -t nat -D POSTROUTING -s $ps_ip -o $ps_if -j SNAT --to $pdp_ip
417 if [ $? -ne 0 ];then
418 echo "cmd <<iptables -t nat -D POSTROUTING -s $ps_ip -o $ps_if -j SNAT --to $pdp_ip>> exec failed" >> $test_log
419 fi
420 route delete default dev $ps_if
421 if [ $? -ne 0 ];then
422 echo "cmd <<route delete default dev $ps_if>> exec failed" >> $test_log
423 fi
424
425 ifconfig $ext_br 0.0.0.0
426 ifconfig $ext_br down 2>>$test_log
427 if [ $? -ne 0 ];then
428 echo "Error: ifconfig $ext_br down failed." >> $test_log
429 fi
430
431 ifconfig $ps_if 0.0.0.0
432 ifconfig $ps_if down 2>>$test_log
433 if [ $? -ne 0 ];then
434 echo "Error: ifconfig $ps_if down failed." >> $test_log
435 fi
436
437 #reset nv
438 nv set $ext_br"_ip"=0.0.0.0
439 nv set $ext_br"_nm"=0.0.0.0
440 nv set $ps_if"_pdp_ip"=0.0.0.0
441 nv set $ps_if"_pridns"=0.0.0.0
442 nv set $ps_if"_secdns"=0.0.0.0
443 nv set $ps_if"_ip"=0.0.0.0
444}
445
446if [ "-$1" == "-linkup" ]; then
xf.li7ccf8372024-03-07 00:08:02 -0800447 ra_mtu_enable=`nv get ra_mtu_enable`
448 if [ "$ra_mtu_enable" != "1" ]; then
449 mtu=`nv get mtu`
450 ifconfig $ps_if mtu $mtu
451 fi
lh9ed821d2023-04-07 01:36:19 -0700452 if [ "-$ps_ext_mode" == "-1" ]; then
453 brctl addbr $ext_br
454 brctl setfd $ext_br 0.1
455 brctl addif $ext_br $eth_if
xf.lidf7f8ba2024-09-12 23:53:34 -0700456 if [ "$eth_if" != "zvnet"$c_id ]; then
457 ifconfig $ext_br up 2>>$test_log
458 else
459 ifconfig $ext_br -arp up 2>>$test_log
460 fi
lh9ed821d2023-04-07 01:36:19 -0700461 get_ipaddr
462 dhcp_set
463 route_set
464 arp_proxy_set
465 ifconfig $eth_if up
466 ismbim=`ps |grep -v grep |grep -w mbim |awk '{printf $1}'`
467 if [ "-$ismbim" != "-" ]; then
468 eth_mac=`cat "/sys/class/net/"$eth_if"/address"`
469 arp -s $pdp_ip $eth_mac 2>>$test_log
470 fi
471 tc_tbf.sh up $c_id
472 elif [ "-$ps_ext_mode" == "-0" ]; then
473 br_up
474 fi
475elif [ "-$1" == "-linkdown" ]; then
476 if [ "-$ps_ext_mode" == "-1" ]; then
477 tc_tbf.sh down $c_id
478 arp_proxy_kill
479 dhcp_kill
480 route_del
481 ifconfig $eth_if down
482 ifconfig $ext_br down
483 brctl delif $ext_br $eth_if
484 brctl delbr $ext_br
485 echo "" > /etc/resolv.conf
486 elif [ "-$ps_ext_mode" == "-0" ]; then
487 br_down
488 fi
489fi