blob: a48bd733c898632d207f37b6d49aaac3fe442e9c [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#!/bin/sh
2
3path_sh=`nv get path_sh`
4. $path_sh/global.sh
5
6#echo "Info: psext_updown_ipv6.sh $1 $2 start" >> $test_log
7
8c_id=$2
9
10dhcp6s_conf=$path_conf/dhcp6s$c_id.conf
11radvd_conf=$path_conf/radvd$c_id.conf
12ndp_log=$path_conf/ndp$c_id.log
13radvd_pidfile=$path_tmp/radvd$c_id.pid
14
15ps_if=`nv get pswan`$c_id
16eth_if=`nv get "ps_ext"$c_id`
17ps_ext_mode=`nv get need_jilian`
18br_if="br"$c_id
19dnsconfig=0
20prefix_len=64
21echo "Info: psext_updown_ipv6.sh $ps_ext_mode $ps_if $eth_if $br_if start" >> $test_log
22
23b_dhcpv6stateEnabled=`nv get dhcpv6stateEnabled`
24b_dhcpv6statelessEnabled=`nv get dhcpv6statelessEnabled`
25
26#采用PDP激活方式,没有dhcp6c进程
27
28#############linkup dhcpserver set############
29linkup_add_dns_to_dhcp6s_radvd_conf()
30{
31 #直连模式没有手动DNS
32 ipv6_pridns_auto=`nv get $ps_if"_ipv6_pridns_auto"`
33 ipv6_secdns_auto=`nv get $ps_if"_ipv6_secdns_auto"`
34
35 if [ -n "$ipv6_pridns_auto" ] && [ "-$ipv6_pridns_auto" != "-::" ] && [ "-$ipv6_pridns_auto" != "-::0" ];then
36 ipv6_prefer_dns=$ipv6_pridns_auto
37 fi
38
39 if [ -n "$ipv6_secdns_auto" ] && [ "-$ipv6_secdns_auto" != "-::" ] && [ "-$ipv6_secdns_auto" != "-::0" ];then
40 ipv6_standby_dns=$ipv6_secdns_auto
41 fi
42
43 if [ "-$ipv6_prefer_dns" == "-" -a "-$ipv6_standby_dns" == "-" ]; then
44 return
45 else
46 if [ -n "$1" ] && [ "-$1" == "-dhcp6s" ] ;then
47 echo -e "\toption dns_servers $ipv6_prefer_dns $ipv6_standby_dns;" >> $dhcp6s_conf
48 elif [ -n "$1" ] && [ "-$1" == "-radvd" ] ;then
49 # del last line
50 sed -i '$d' $radvd_conf
51 echo -e "\tRDNSS $ipv6_prefer_dns $ipv6_standby_dns\n\t{" >> $radvd_conf
52 echo -e "\t\tAdvRDNSSPreference 15;" >> $radvd_conf
53 echo -e "\t\tAdvRDNSSOpen on;" >> $radvd_conf
54 echo -e "\t};\n};" >> $radvd_conf
55 fi
56
57 if [ "-$dnsconfig" == "-0" ]; then
58 echo "dnsconfig $1 $ipv6_prefer_dns, $ipv6_standby_dns" >> $test_log
59 if [ "-$ipv6_prefer_dns" != "-" ]; then
60 echo "nameserver $ipv6_prefer_dns" >> /etc/resolv.conf
61 fi
62 if [ "-$ipv6_standby_dns" != "-" ]; then
63 echo "nameserver $ipv6_standby_dns" >> /etc/resolv.conf
64 fi
65 dnsconfig=1
66 fi
67 fi
68}
69
70linkup_dhcpv6_set()
71{
72 dhcpv6_start=$pdp_ip
73 dhcpv6_end=$pdp_ip
74
75 #nv值非空则表示用户设置了静态ip、gw
76 gw=`nv get $ps_if"_ipv6_gw"`
77
78 echo -e "interface $br_if {" > $dhcp6s_conf
79 if [ "-$b_dhcpv6stateEnabled" = "-1" ];then
80 echo -e "\tserver-preference 255;\n\trenew-time 6000;" >> $dhcp6s_conf
81 echo -e "\trebind-time 9000;\n\tprefer-life-time 1300;" >> $dhcp6s_conf
82 echo -e "\tvalid-life-time 2000;\n\tallow rapid-commit;" >> $dhcp6s_conf
83 echo -e "\tlink $br_if {\n\t\tallow unicast;\n\t\tsend unicast;" >> $dhcp6s_conf
84 echo -e "\t\tpool {\n\t\t\trange $dhcpv6_start to $dhcpv6_end/$prefix_len;" >> $dhcp6s_conf
85 echo -e "\t\t};\n\t};" >> $dhcp6s_conf
86 linkup_add_dns_to_dhcp6s_radvd_conf dhcp6s
87 echo -e "};" >> $dhcp6s_conf
88 dhcp6s -dDf -c $dhcp6s_conf $br_if &
89 else
90 #slaac with dhcp statelessset dns info
91 if [ "-$b_dhcpv6statelessEnabled" = "-1" ];then
92 echo -e "\tlink $br_if {\n\t};" >> $dhcp6s_conf
93 linkup_add_dns_to_dhcp6s_radvd_conf dhcp6s
94 echo -e "};" >> $dhcp6s_conf
95 dhcp6s -dDf -c $dhcp6s_conf $br_if &
96 if [ $? -ne 0 ];then
97 echo "Error: dhcp6s -dDf -c $dhcp6s_conf $br_if failed." >> $test_log
98 fi
99 fi
100 fi
101}
102
103#获取ip并配置ps、eth
104linkup_get_addr()
105{
106 #disable the forwarding to send RS and not set the addr when receive ra packet
107 echo 0 > /proc/sys/net/ipv6/conf/all/forwarding
108 echo 0 > /proc/sys/net/ipv6/conf/$ps_if/accept_ra
109 echo 0 > /proc/sys/net/ipv6/conf/$eth_if/accept_ra
110 echo 0 > /proc/sys/net/ipv6/conf/$br_if/accept_ra
111 #call the slaac program to get the prefix addr
112 ifconfig $ps_if up 2>>$test_log
113 if [ $? -ne 0 ];then
114 echo "Error: ifconfig $ps_if up failed." >> $test_log
115 fi
116 prefix_info_temp=`nv get $ps_if"_ipv6_prefix_info"`
117 #如果当前没有前缀,则直接启动slaac,如果当前前缀已经设置,则说明已经获取到,直接配置IP地址即可
118 if [ -n "$prefix_info_temp" -a "-$prefix_info_temp" != "-::" ]; then
119 ret_code=0
120 else
121 sleep 1
122 interface_id_temp1=`nv get $ps_if"_ipv6_interface_id"`
123 local_ipv6_addr="fe80::"$interface_id_temp1
124 local_ipv6_addr_nv="$ps_if""_local_ipv6_addr"
125 nv set $local_ipv6_addr_nv=$local_ipv6_addr
126 ip -6 addr add $local_ipv6_addr/64 dev $ps_if 2>>$test_log
127 zte_ipv6_slaac -i "$ps_if"
128 ret_code=$?
129 fi
130 echo "Info: zte_ipv6_slaac return: $ret_code" >> $test_log
131 echo "the program zte_ipv6_slaac return = $ret_code"
132 if [ $ret_code -eq 0 ]; then
133 echo "the zte_ipv6_slaac success"
134 interface_id_temp=`nv get $ps_if"_ipv6_interface_id"`
135 prefix_info_temp=`nv get $ps_if"_ipv6_prefix_info"`
136 prefix_len=`nv get $ps_if"_ipv6_prefix_len"`
137 echo "##############1##########"
138 echo "$interface_id_temp"
139 echo "$prefix_info_temp $prefix_len"
140 echo "##############2##########"
141
142 #pdp 激活的ipv6地址
143 pdp_ip=$prefix_info_temp$interface_id_temp
144 #pdp激活地址,适配页面使用
145 nv set ipv6_wan_ipaddr="$pdp_ip"
146
147 ipv6_addr_conver $pdp_ip "$ps_if"
148
149 #给eth分配地址,使用ipv6_addr_conver在第15+1
150 br_ip=`nv get ipv6_br0_addr`
151 brctl addbr $br_if
152 brctl setfd $br_if 0.1
153 nv set $br_if"_ipv6_ip"=$br_ip
154 ifconfig $br_if up 2>>$test_log
155 if [ $? -ne 0 ];then
156 echo "Error: ifconfig $br_if up failed." >> $test_log
157 fi
158 ip -6 addr add $br_ip/64 dev $br_if
159 #给外网口分配地址,使用ipv6_addr_conver在第15+2
160 ps_ip=`nv get $ps_if"_dhcpv6_start"`
161 nv set $ps_if"_ipv6_ip"=$ps_ip
162 #ifconfig $ps_if $ps_ip up
163 #if [ $? -ne 0 ];then
164 # echo "Error: ifconfig $ps_ip up failed." >> $test_log
165 #fi
166 ip -6 addr add $ps_ip/126 dev $ps_if 2>>$test_log
167 if [ $? -ne 0 ];then
168 echo "Error: ip -6 addr add $ps_ip/126 dev $ps_if failed." >> $test_log
169 fi
170 nv set $ps_if"_ipv6_state"="working"
171
172 echo "Info: zte_ipv6_slaac pdp_ip: $pdp_ip" >> $test_log
173 echo "Info: zte_ipv6_slaac ps_ip: $ps_ip" >> $test_log
174 echo "Info: zte_ipv6_slaac br_ip: $br_ip" >> $test_log
175 else
176 echo "the zte_ipv6_slaac fail"
177 nv set $ps_if"_ipv6_state"="dead"
178 exit 1
179 fi
180}
181
182#路由规则,ps与eth级联
183linkup_route_set()
184{
185 echo 0 > /proc/sys/net/ipv6/conf/all/forwarding
186
187 marknum=`expr $c_id + 60`
188 ip6tables -t mangle -A PREROUTING -i $ps_if -j MARK --set-mark $marknum
189 rt_num=`expr $c_id + 160`
190 ip -6 route add default dev $br_if table $rt_num
191 ip -6 rule add to $pdp_ip/64 fwmark $marknum table $rt_num
192
193 marknum=`expr $c_id + 50`
194 ip6tables -t mangle -A PREROUTING -i $br_if -j MARK --set-mark $marknum
195 rt_num=`expr $c_id + 150`
196 ip -6 route add default dev $ps_if table $rt_num
197 ip -6 rule add from $pdp_ip/64 fwmark $marknum table $rt_num
198
199 ip6tables -t filter -A FORWARD -p icmpv6 --icmpv6-type 135 -j DROP
200
201 ip -6 route flush cache
202
203 #这句设完,里面可以ping通外网了
204 echo "Info: route_set ps_ip=$ps_ip" >> $test_log
205 #ip -6 route add default via $ps_ip dev $ps_if
206 ip -6 route add default dev $ps_if 2>>$test_log
207 if [ $? -ne 0 ];then
208 echo "Error: ip -6 route add default dev $ps_if failed." >> $test_log
209 fi
210
211 #enable ipv6 packet forwarding
212 echo 1 > /proc/sys/net/ipv6/conf/all/forwarding
213 echo 1 > /proc/sys/net/ipv6/conf/$ps_if/accept_ra
214 echo 1 > /proc/sys/net/ipv6/conf/$eth_if/accept_ra
215 echo 1 > /proc/sys/net/ipv6/conf/$br_if/accept_ra
216 #enable ipv6 neigh discovery proxy
217 echo 1 > /proc/sys/net/ipv6/conf/all/proxy_ndp
218
219 ndp_kill
220 zte_ndp -a -s $br_if -d $ps_if -l $ndp_log -p &
221}
222
223#############linkup radvd set############
224linkup_radvd_set()
225{
226 echo "enter linkup_radvd_set "
227
228 rm -rf $radvd_conf
229
230 if [ "-$b_dhcpv6stateEnabled" = "-1" ];then
231 echo -e "interface $br_if\n{\n\tAdvSendAdvert on;" > $radvd_conf
232 echo -e "\tAdvManagedFlag on;\n};" >> $radvd_conf
233 radvd_kill
234 rm -rf $radvd_pidfile
235 radvd -d 3 -C $radvd_conf -p $radvd_pidfile&
236 echo "leave linkup_radvd_set "
237 return
238 fi
239
240 echo "Info: psext_updown_ipv6.sh br_if:$br_if, prefix_len:$prefix_len" >> $test_log
241 echo "ipv6_br0_addr_tmp:$ipv6_br0_addr_tmp"
242 echo "prefix_len:$prefix_len"
243
244 cp $path_ro/radvd_template.conf $radvd_conf
245
246 sed -i -e 's/#ipv6_wan_addr#\/64/#ipv6_wan_addr#\/#prefix_len#/g' $radvd_conf
247 sed -i -e s/br0/$br_if/g $radvd_conf
248 sed -i -e s/#ipv6_wan_addr#/$br_ip/g $radvd_conf
249 sed -i -e s/#prefix_len#/$prefix_len/g $radvd_conf
250 sed -i -e s/#adv_switch#/on/g $radvd_conf
251
252 # TODO: replace br0's ipv6 address
253 #sed -i -e s/#ipv6_wan_addr#/$wan_addr/g $radvd_conf
254 #sed -i -e s/#adv_switch#/on/g $radvd_conf
255
256 #slaac with dns info
257 if [ "-$b_dhcpv6statelessEnabled" = "-1" ];then
258 echo "use dhcpv6stateless for dns"
259 else
260 sed -i -e 's/AdvOtherConfigFlag on;/AdvOtherConfigFlag off;/g' $radvd_conf
261 linkup_add_dns_to_dhcp6s_radvd_conf radvd
262 fi
263
264 radvd_kill
265 sleep 1
266 rm -rf $radvd_pidfile
267 radvd -d 3 -C $radvd_conf -p $radvd_pidfile &
268
269 echo "leave linkup_radvd_set "
270}
271
272#构建网桥
273br_up()
274{
275 br="br"$c_id
276 brctl addbr $br 2>>$test_log
277 if [ $? -ne 0 ];then
278 echo "Error: brctl addbr $br failed." >> $test_log
279 fi
280 brctl setfd $br 0.1 2>>$test_log
281 if [ $? -ne 0 ];then
282 echo "Error: brctl setfd $br 0.1 failed." >> $test_log
283 fi
284 ifconfig $br up 2>>$test_log
285 if [ $? -ne 0 ];then
286 echo "Error: ifconfig $br up failed." >> $test_log
287 fi
288
289 ifconfig $ps_if up 2>>$test_log
290 if [ $? -ne 0 ];then
291 echo "Error: ifconfig $ps_if up failed." >> $test_log
292 fi
293 brctl addif $br $ps_if 2>>$test_log
294 if [ $? -ne 0 ];then
295 echo "Error: brctl addif $br $ps_if failed." >> $test_log
296 fi
297
298 brctl addif $br $eth_if 2>>$test_log
299 if [ $? -ne 0 ];then
300 echo "Error: brctl addif $br $eth_if failed." >> $test_log
301 fi
302 ifconfig $eth_if up 2>>$test_log
303 if [ $? -ne 0 ];then
304 echo "Error: ifconfig $eth_if up failed." >> $test_log
305 fi
306}
307
308#删除网桥
309br_down()
310{
311 br="br"$c_id
312 brctl delif $br $eth_if 2>>$test_log
313 if [ $? -ne 0 ];then
314 echo "Error: brctl delif $br $eth_if failed." >> $test_log
315 fi
316 ifconfig $eth_if down 2>>$test_log
317 if [ $? -ne 0 ];then
318 echo "Error: ifconfig $eth_if down failed." >> $test_log
319 fi
320 brctl delif $br $ps_if 2>>$test_log
321 if [ $? -ne 0 ];then
322 echo "Error: brctl delif $br $ps_if failed." >> $test_log
323 fi
324 ifconfig $ps_if down 2>>$test_log
325 if [ $? -ne 0 ];then
326 echo "Error: ifconfig $ps_if down failed." >> $test_log
327 fi
328 ifconfig $br down 2>>$test_log
329 if [ $? -ne 0 ];then
330 echo "Error: ifconfig $br down failed." >> $test_log
331 fi
332 brctl delbr $br 2>>$test_log
333 if [ $? -ne 0 ];then
334 echo "Error: brctl delbr $br failed." >> $test_log
335 fi
336}
337
338#清除相应的radvd进程
339linkdown_radvd_set()
340{
341 #跟终端连接的网口直接会down,所以不用像ufi一样设置成1s有效期
342 radvd_kill
343}
344
345#清除相应的DHCPv6进程
346linkdown_dhcpv6_server_set()
347{
348 dhcp6s_kill
349}
350
351#删除相应的ipv6路由规则
352linkdown_route_set()
353{
354 br_ip=`nv get $br_if"_ipv6_ip"`
355 ps_ip=`nv get $ps_if"_ipv6_ip"`
356 pdp_ip=`nv get ipv6_wan_ipaddr`
357
358 ip6tables -t filter -D FORWARD -p icmpv6 --icmpv6-type 135 -j DROP
359
360 marknum=`expr $c_id + 50`
361 rt_num=`expr $c_id + 150`
362 ip -6 rule del from $pdp_ip/64 fwmark $marknum table $rt_num
363 ip6tables -t mangle -D PREROUTING -i $br_if -j MARK --set-mark $marknum
364 ip -6 route del default dev $ps_if table $rt_num
365
366 marknum=`expr $c_id + 60`
367 rt_num=`expr $c_id + 160`
368 ip -6 rule del to $pdp_ip/64 fwmark $marknum table $rt_num
369 ip6tables -t mangle -D PREROUTING -i $ps_if -j MARK --set-mark $marknum
370 ip -6 route del default dev $br_if table $rt_num
371
372 ip -6 addr del $br_ip/126 dev $br_if
373 #if [ $? -ne 0 ];then
374 # echo "Error: ip -6 addr del $eth_ip/126 dev $eth_if failed." >> $test_log
375 #fi
376 ip -6 addr del $ps_ip/126 dev $ps_if
377 #if [ $? -ne 0 ];then
378 # echo "Error: ip -6 addr del $ps_ip/126 dev $ps_if failed." >> $test_log
379 #fi
380 ip -6 route del default
381 #if [ $? -ne 0 ];then
382 # echo "Error: ip -6 route del default failed." >> $test_log
383 #fi
384
385 ifconfig $br_if down 2>>$test_log
386 if [ $? -ne 0 ];then
387 echo "Error: ifconfig $br_if down failed." >> $test_log
388 fi
389 ifconfig $ps_if down 2>>$test_log
390 if [ $? -ne 0 ];then
391 echo "Error: ifconfig $ps_if down failed." >> $test_log
392 fi
393
394 echo 0 > /proc/sys/net/ipv6/conf/$ps_if/accept_ra
395
396 #reset nv
397 nv set $br_if"_ipv6_ip"="::"
398 nv set $ps_if"_ipv6_ip"="::"
399 nv set $ps_if"_ipv6_pridns_auto"="::"
400 nv set $ps_if"_ipv6_secdns_auto"="::"
401 nv set $ps_if"_ipv6_gw"="::"
402 nv set $ps_if"_ipv6_interface_id"="::"
403 nv set $ps_if"_ipv6_prefix_info"="::"
404 nv set $ps_if"_dhcpv6_start"="::"
405 nv set $ps_if"_dhcpv6_end"="::"
406
407 #适配页面等其他地方使用老NV
408 nv set ipv6_wan_ipaddr="::"
409 nv set $ps_if"_ipv6_state"="dead"
410
411 local_ipv6_addr_nv="$ps_if""_local_ipv6_addr"
412 nv set $local_ipv6_addr_nv="::"
413
414 ndp_kill
415}
416
417if [ "$1" == "linkup" ]; then
418 mtu=`nv get mtu`
419 ifconfig $ps_if mtu $mtu
420 if [ "-$ps_ext_mode" == "-1" ]; then
421 linkup_get_addr
422 linkup_route_set
423 linkup_dhcpv6_set
424 linkup_radvd_set
425 brctl addif $br_if $eth_if
426 ifconfig $eth_if up
427 tc_tbf.sh up $c_id
428 elif [ "-$ps_ext_mode" == "-0" ]; then
429 br_up
430 fi
431elif [ "$1" == "linkdown" ]; then
432 if [ "-$ps_ext_mode" == "-1" ]; then
433 tc_tbf.sh down $c_id
434 linkdown_radvd_set
435 linkdown_dhcpv6_server_set
436 linkdown_route_set
437 slaac_kill
438 brctl delif $br_if $eth_if
439 ifconfig $eth_if down
440 echo "" > /etc/resolv.conf
441 elif [ "-$ps_ext_mode" == "-0" ]; then
442 br_down
443 fi
444fi
445
446echo "Info: psext_updown_ipv6.sh leave" >> $test_log