blob: 9f74350f1a27d6f3759d701b120ed6d44fc51886 [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.li6236ea72023-07-26 04:58:33 -0700280 ifconfig $ps_if $ps_ip netmask 255.255.255.0 up 2>>$test_log
lh9ed821d2023-04-07 01:36:19 -0700281 if [ $? -ne 0 ];then
282 echo "Error: ifconfig $ps_if $ps_ip up failed." >> $test_log
283 fi
284 nv set default_wan_rel=$ps_if
xf.li742dd022023-06-08 01:43:32 -0700285 #nv set default_cid=$c_id
lh9ed821d2023-04-07 01:36:19 -0700286 nv set $ext_br"_ip"=$br_ip
287 ifconfig $ext_br $br_ip 2>>$test_log
288 if [ $? -ne 0 ];then
289 echo "Error: ifconfig $ext_br $br_ip up failed." >> $test_log
290 fi
291}
292#·ÓɹæÔò£¬psÓëeth¼¶Áª
293route_set()
294{
295 marknum=`expr $c_id + 20`
296 iptables -t mangle -A PREROUTING -i $ps_if -j MARK --set-mark $marknum
297 rt_num=`expr $c_id + 120`
298
299 ip route add default dev $ext_br table $rt_num
300
301 ip rule add to $pdp_ip fwmark $marknum table $rt_num
302
303 marknum=`expr $c_id + 10`
304 iptables -t mangle -A PREROUTING -i $ext_br -j MARK --set-mark $marknum
305 rt_num=`expr $c_id + 100`
306
307 ip route add default dev $ps_if table $rt_num
308 ip rule add from $pdp_ip fwmark $marknum table $rt_num
309
310 ip route flush cache
311
312 #±¾µØÍøÂçÅäÖÃ
313 iptables -t nat -I POSTROUTING -s $ps_ip -o $ps_if -j SNAT --to $pdp_ip
314
xf.li742dd022023-06-08 01:43:32 -0700315 route_info=`ip route|grep default`
lh9ed821d2023-04-07 01:36:19 -0700316
317 if [ "$route_info" == "" ];then
318 route add default dev $ps_if
319 else
320 echo "Debug: default route already exist." >> $test_log
321 fi
322}
323#¹¹½¨ÍøÇÅ
324br_up()
325{
326 br="br"$c_id
327 brctl addbr $br
328 brctl setfd $br 0.1
329 ifconfig $br up 2>>$test_log
330 if [ $? -ne 0 ];then
331 echo "Error: ifconfig $br up failed." >> $test_log
332 fi
333
334 ifconfig $ps_if up 2>>$test_log
335 if [ $? -ne 0 ];then
336 echo "Error: ifconfig $ps_if up failed." >> $test_log
337 fi
338 brctl addif $br $ps_if 2>>$test_log
339 if [ $? -ne 0 ];then
340 echo "Error: brctl addif $br $ps_if failed." >> $test_log
341 fi
342 brctl addif $br $eth_if 2>>$test_log
343 if [ $? -ne 0 ];then
344 echo "Error: brctl addif $br $eth_if failed." >> $test_log
345 fi
346 ifconfig $eth_if up 2>>$test_log
347 if [ $? -ne 0 ];then
348 echo "Error: ifconfig $eth_if up failed." >> $test_log
349 fi
350
351}
352#ɾ³ýÍøÇÅ
353br_down()
354{
355 br="br"$c_id
356 brctl delif $br $eth_if 2>>$test_log
357 if [ $? -ne 0 ];then
358 echo "Error: brctl delif $br $eth_if failed." >> $test_log
359 fi
360 ifconfig $eth_if down 2>>$test_log
361 if [ $? -ne 0 ];then
362 echo "Error: ifconfig $eth_if down failed." >> $test_log
363 fi
364 brctl delif $br $ps_if 2>>$test_log
365 if [ $? -ne 0 ];then
366 echo "Error: brctl delif $br $ps_if failed." >> $test_log
367 fi
368 ifconfig $ps_if down 2>>$test_log
369 if [ $? -ne 0 ];then
370 echo "Error: ifconfig $ps_if down failed." >> $test_log
371 fi
372 ifconfig $br down 2>>$test_log
373 if [ $? -ne 0 ];then
374 echo "Error: ifconfig $br down failed." >> $test_log
375 fi
376 brctl delbr $br 2>>$test_log
377 if [ $? -ne 0 ];then
378 echo "Error: brctl delbr $br failed." >> $test_log
379 fi
380}
381#ɱËÀ¶ÔÓ¦µÄDHCP½ø³Ì
382dhcp_kill()
383{
384 pidfile=$path_conf"/udhcpd"$c_id".pid"
385 kill `cat $pidfile`
386}
387#ɾ³ý¶ÔÓ¦µÄ·ÓɹæÔò
388route_del()
389{
390 pdp_ip=`nv get $ps_if"_pdp_ip"`
391# ps_ip1=${pdp_ip%.*}
392# ps_ip2=${pdp_ip##*.}
393# [ "$ps_ip2" -ge "254" ] && { ps_ip2="250"; }
394# ps_ip2=`expr $ps_ip2 + 1`
395 ps_ip=`nv get $ps_if"_ip"`
396 br_ip=`nv get $ext_br"_ip"`
397 marknum=`expr $c_id + 10`
398 rt_num=`expr $c_id + 100`
399
400 iptables -t mangle -D PREROUTING -i $ext_br -j MARK --set-mark $marknum
401 ip rule del from $pdp_ip fwmark $marknum table $rt_num
402 ip route del default dev $ps_if table $rt_num
403
404 marknum=`expr $c_id + 20`
405 rt_num=`expr $c_id + 120`
406 iptables -t mangle -D PREROUTING -i $ps_if -j MARK --set-mark $marknum
407 ip rule del to $pdp_ip fwmark $marknum table $rt_num
408 ip route del default dev $ext_br table $rt_num
409 #±¾µØÍøÂçÅäÖÃ
410 iptables -t nat -D POSTROUTING -s $ps_ip -o $ps_if -j SNAT --to $pdp_ip
411 if [ $? -ne 0 ];then
412 echo "cmd <<iptables -t nat -D POSTROUTING -s $ps_ip -o $ps_if -j SNAT --to $pdp_ip>> exec failed" >> $test_log
413 fi
414 route delete default dev $ps_if
415 if [ $? -ne 0 ];then
416 echo "cmd <<route delete default dev $ps_if>> exec failed" >> $test_log
417 fi
418
419 ifconfig $ext_br 0.0.0.0
420 ifconfig $ext_br down 2>>$test_log
421 if [ $? -ne 0 ];then
422 echo "Error: ifconfig $ext_br down failed." >> $test_log
423 fi
424
425 ifconfig $ps_if 0.0.0.0
426 ifconfig $ps_if down 2>>$test_log
427 if [ $? -ne 0 ];then
428 echo "Error: ifconfig $ps_if down failed." >> $test_log
429 fi
430
431 #reset nv
432 nv set $ext_br"_ip"=0.0.0.0
433 nv set $ext_br"_nm"=0.0.0.0
434 nv set $ps_if"_pdp_ip"=0.0.0.0
435 nv set $ps_if"_pridns"=0.0.0.0
436 nv set $ps_if"_secdns"=0.0.0.0
437 nv set $ps_if"_ip"=0.0.0.0
438}
439
440if [ "-$1" == "-linkup" ]; then
xf.li7ccf8372024-03-07 00:08:02 -0800441 ra_mtu_enable=`nv get ra_mtu_enable`
442 if [ "$ra_mtu_enable" != "1" ]; then
443 mtu=`nv get mtu`
444 ifconfig $ps_if mtu $mtu
445 fi
lh9ed821d2023-04-07 01:36:19 -0700446 if [ "-$ps_ext_mode" == "-1" ]; then
447 brctl addbr $ext_br
448 brctl setfd $ext_br 0.1
449 brctl addif $ext_br $eth_if
450 ifconfig $ext_br up
451 get_ipaddr
452 dhcp_set
453 route_set
454 arp_proxy_set
455 ifconfig $eth_if up
456 ismbim=`ps |grep -v grep |grep -w mbim |awk '{printf $1}'`
457 if [ "-$ismbim" != "-" ]; then
458 eth_mac=`cat "/sys/class/net/"$eth_if"/address"`
459 arp -s $pdp_ip $eth_mac 2>>$test_log
460 fi
461 tc_tbf.sh up $c_id
462 elif [ "-$ps_ext_mode" == "-0" ]; then
463 br_up
464 fi
465elif [ "-$1" == "-linkdown" ]; then
466 if [ "-$ps_ext_mode" == "-1" ]; then
467 tc_tbf.sh down $c_id
468 arp_proxy_kill
469 dhcp_kill
470 route_del
471 ifconfig $eth_if down
472 ifconfig $ext_br down
473 brctl delif $ext_br $eth_if
474 brctl delbr $ext_br
475 echo "" > /etc/resolv.conf
476 elif [ "-$ps_ext_mode" == "-0" ]; then
477 br_down
478 fi
479fi