blob: 83d471c67665c30c7f431d94d25b6287b2bd1069 [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
xf.li9d1a0e12023-09-20 01:43:20 -0700202
203 route_info=`ip -6 route|grep default`
lh9ed821d2023-04-07 01:36:19 -0700204
xf.li9d1a0e12023-09-20 01:43:20 -0700205 if [ "$route_info" == "" ];then
206 #这句设完,里面可以ping通外网了
207 echo "Info: route_set ps_ip=$ps_ip" >> $test_log
208 #ip -6 route add default via $ps_ip dev $ps_if
209 ip -6 route add default dev $ps_if 2>>$test_log
210 else
211 echo "Debug: default route6 already exist." >> $test_log
212 fi
213
lh9ed821d2023-04-07 01:36:19 -0700214 if [ $? -ne 0 ];then
215 echo "Error: ip -6 route add default dev $ps_if failed." >> $test_log
216 fi
217
218 #enable ipv6 packet forwarding
219 echo 1 > /proc/sys/net/ipv6/conf/all/forwarding
220 echo 1 > /proc/sys/net/ipv6/conf/$ps_if/accept_ra
221 echo 1 > /proc/sys/net/ipv6/conf/$eth_if/accept_ra
222 echo 1 > /proc/sys/net/ipv6/conf/$br_if/accept_ra
223 #enable ipv6 neigh discovery proxy
224 echo 1 > /proc/sys/net/ipv6/conf/all/proxy_ndp
225
226 ndp_kill
227 zte_ndp -a -s $br_if -d $ps_if -l $ndp_log -p &
228}
229
230#############linkup radvd set############
231linkup_radvd_set()
232{
233 echo "enter linkup_radvd_set "
234
235 rm -rf $radvd_conf
236
237 if [ "-$b_dhcpv6stateEnabled" = "-1" ];then
238 echo -e "interface $br_if\n{\n\tAdvSendAdvert on;" > $radvd_conf
239 echo -e "\tAdvManagedFlag on;\n};" >> $radvd_conf
240 radvd_kill
241 rm -rf $radvd_pidfile
242 radvd -d 3 -C $radvd_conf -p $radvd_pidfile&
243 echo "leave linkup_radvd_set "
244 return
245 fi
246
247 echo "Info: psext_updown_ipv6.sh br_if:$br_if, prefix_len:$prefix_len" >> $test_log
248 echo "ipv6_br0_addr_tmp:$ipv6_br0_addr_tmp"
249 echo "prefix_len:$prefix_len"
250
251 cp $path_ro/radvd_template.conf $radvd_conf
252
253 sed -i -e 's/#ipv6_wan_addr#\/64/#ipv6_wan_addr#\/#prefix_len#/g' $radvd_conf
254 sed -i -e s/br0/$br_if/g $radvd_conf
255 sed -i -e s/#ipv6_wan_addr#/$br_ip/g $radvd_conf
256 sed -i -e s/#prefix_len#/$prefix_len/g $radvd_conf
257 sed -i -e s/#adv_switch#/on/g $radvd_conf
258
259 # TODO: replace br0's ipv6 address
260 #sed -i -e s/#ipv6_wan_addr#/$wan_addr/g $radvd_conf
261 #sed -i -e s/#adv_switch#/on/g $radvd_conf
262
263 #slaac with dns info
264 if [ "-$b_dhcpv6statelessEnabled" = "-1" ];then
265 echo "use dhcpv6stateless for dns"
266 else
267 sed -i -e 's/AdvOtherConfigFlag on;/AdvOtherConfigFlag off;/g' $radvd_conf
268 linkup_add_dns_to_dhcp6s_radvd_conf radvd
269 fi
270
271 radvd_kill
272 sleep 1
273 rm -rf $radvd_pidfile
274 radvd -d 3 -C $radvd_conf -p $radvd_pidfile &
275
276 echo "leave linkup_radvd_set "
277}
278
279#构建网桥
280br_up()
281{
282 br="br"$c_id
283 brctl addbr $br 2>>$test_log
284 if [ $? -ne 0 ];then
285 echo "Error: brctl addbr $br failed." >> $test_log
286 fi
287 brctl setfd $br 0.1 2>>$test_log
288 if [ $? -ne 0 ];then
289 echo "Error: brctl setfd $br 0.1 failed." >> $test_log
290 fi
291 ifconfig $br up 2>>$test_log
292 if [ $? -ne 0 ];then
293 echo "Error: ifconfig $br up failed." >> $test_log
294 fi
295
296 ifconfig $ps_if up 2>>$test_log
297 if [ $? -ne 0 ];then
298 echo "Error: ifconfig $ps_if up failed." >> $test_log
299 fi
300 brctl addif $br $ps_if 2>>$test_log
301 if [ $? -ne 0 ];then
302 echo "Error: brctl addif $br $ps_if failed." >> $test_log
303 fi
304
305 brctl addif $br $eth_if 2>>$test_log
306 if [ $? -ne 0 ];then
307 echo "Error: brctl addif $br $eth_if failed." >> $test_log
308 fi
309 ifconfig $eth_if up 2>>$test_log
310 if [ $? -ne 0 ];then
311 echo "Error: ifconfig $eth_if up failed." >> $test_log
312 fi
313}
314
315#删除网桥
316br_down()
317{
318 br="br"$c_id
319 brctl delif $br $eth_if 2>>$test_log
320 if [ $? -ne 0 ];then
321 echo "Error: brctl delif $br $eth_if failed." >> $test_log
322 fi
323 ifconfig $eth_if down 2>>$test_log
324 if [ $? -ne 0 ];then
325 echo "Error: ifconfig $eth_if down failed." >> $test_log
326 fi
327 brctl delif $br $ps_if 2>>$test_log
328 if [ $? -ne 0 ];then
329 echo "Error: brctl delif $br $ps_if failed." >> $test_log
330 fi
331 ifconfig $ps_if down 2>>$test_log
332 if [ $? -ne 0 ];then
333 echo "Error: ifconfig $ps_if down failed." >> $test_log
334 fi
335 ifconfig $br down 2>>$test_log
336 if [ $? -ne 0 ];then
337 echo "Error: ifconfig $br down failed." >> $test_log
338 fi
339 brctl delbr $br 2>>$test_log
340 if [ $? -ne 0 ];then
341 echo "Error: brctl delbr $br failed." >> $test_log
342 fi
343}
344
345#清除相应的radvd进程
346linkdown_radvd_set()
347{
348 #跟终端连接的网口直接会down,所以不用像ufi一样设置成1s有效期
349 radvd_kill
350}
351
352#清除相应的DHCPv6进程
353linkdown_dhcpv6_server_set()
354{
355 dhcp6s_kill
356}
357
358#删除相应的ipv6路由规则
359linkdown_route_set()
360{
361 br_ip=`nv get $br_if"_ipv6_ip"`
362 ps_ip=`nv get $ps_if"_ipv6_ip"`
363 pdp_ip=`nv get ipv6_wan_ipaddr`
364
365 ip6tables -t filter -D FORWARD -p icmpv6 --icmpv6-type 135 -j DROP
366
367 marknum=`expr $c_id + 50`
368 rt_num=`expr $c_id + 150`
369 ip -6 rule del from $pdp_ip/64 fwmark $marknum table $rt_num
370 ip6tables -t mangle -D PREROUTING -i $br_if -j MARK --set-mark $marknum
371 ip -6 route del default dev $ps_if table $rt_num
372
373 marknum=`expr $c_id + 60`
374 rt_num=`expr $c_id + 160`
375 ip -6 rule del to $pdp_ip/64 fwmark $marknum table $rt_num
376 ip6tables -t mangle -D PREROUTING -i $ps_if -j MARK --set-mark $marknum
377 ip -6 route del default dev $br_if table $rt_num
378
379 ip -6 addr del $br_ip/126 dev $br_if
380 #if [ $? -ne 0 ];then
381 # echo "Error: ip -6 addr del $eth_ip/126 dev $eth_if failed." >> $test_log
382 #fi
383 ip -6 addr del $ps_ip/126 dev $ps_if
384 #if [ $? -ne 0 ];then
385 # echo "Error: ip -6 addr del $ps_ip/126 dev $ps_if failed." >> $test_log
386 #fi
xf.li9d1a0e12023-09-20 01:43:20 -0700387 ip -6 route del default dev $ps_if
lh9ed821d2023-04-07 01:36:19 -0700388 #if [ $? -ne 0 ];then
389 # echo "Error: ip -6 route del default failed." >> $test_log
390 #fi
391
392 ifconfig $br_if down 2>>$test_log
393 if [ $? -ne 0 ];then
394 echo "Error: ifconfig $br_if down failed." >> $test_log
395 fi
396 ifconfig $ps_if down 2>>$test_log
397 if [ $? -ne 0 ];then
398 echo "Error: ifconfig $ps_if down failed." >> $test_log
399 fi
400
401 echo 0 > /proc/sys/net/ipv6/conf/$ps_if/accept_ra
402
403 #reset nv
404 nv set $br_if"_ipv6_ip"="::"
405 nv set $ps_if"_ipv6_ip"="::"
406 nv set $ps_if"_ipv6_pridns_auto"="::"
407 nv set $ps_if"_ipv6_secdns_auto"="::"
408 nv set $ps_if"_ipv6_gw"="::"
409 nv set $ps_if"_ipv6_interface_id"="::"
410 nv set $ps_if"_ipv6_prefix_info"="::"
411 nv set $ps_if"_dhcpv6_start"="::"
412 nv set $ps_if"_dhcpv6_end"="::"
413
414 #适配页面等其他地方使用老NV
415 nv set ipv6_wan_ipaddr="::"
416 nv set $ps_if"_ipv6_state"="dead"
417
418 local_ipv6_addr_nv="$ps_if""_local_ipv6_addr"
419 nv set $local_ipv6_addr_nv="::"
420
421 ndp_kill
422}
423
424if [ "$1" == "linkup" ]; then
xf.li7ccf8372024-03-07 00:08:02 -0800425 ra_mtu_enable=`nv get ra_mtu_enable`
426 if [ "$ra_mtu_enable" != "1" ]; then
427 mtu=`nv get mtu`
428 ifconfig $ps_if mtu $mtu
429 fi
lh9ed821d2023-04-07 01:36:19 -0700430 if [ "-$ps_ext_mode" == "-1" ]; then
431 linkup_get_addr
432 linkup_route_set
433 linkup_dhcpv6_set
434 linkup_radvd_set
435 brctl addif $br_if $eth_if
436 ifconfig $eth_if up
437 tc_tbf.sh up $c_id
438 elif [ "-$ps_ext_mode" == "-0" ]; then
439 br_up
440 fi
441elif [ "$1" == "linkdown" ]; then
442 if [ "-$ps_ext_mode" == "-1" ]; then
443 tc_tbf.sh down $c_id
444 linkdown_radvd_set
445 linkdown_dhcpv6_server_set
446 linkdown_route_set
447 slaac_kill
448 brctl delif $br_if $eth_if
449 ifconfig $eth_if down
450 echo "" > /etc/resolv.conf
451 elif [ "-$ps_ext_mode" == "-0" ]; then
452 br_down
453 fi
454fi
455
456echo "Info: psext_updown_ipv6.sh leave" >> $test_log