lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | #usg: wan_ipv4 linkup pswan |
| 3 | |
| 4 | path_sh=`nv get path_sh` |
| 5 | . $path_sh/global.sh |
| 6 | |
| 7 | #Èë²ÎÖÐÓÐc_idÔòÉèÖã¬Ã»ÓÐÔòÖÃÁ㣬ÓÃÓÚÉèÖòßÂÔ·ÓɱíºÅ |
| 8 | if [ "-$3" == "-" ]; then |
| 9 | c_id="0" |
| 10 | else |
| 11 | c_id=$3 |
| 12 | fi |
| 13 | def_cid=`nv get default_cid` |
| 14 | |
| 15 | #»ñÈ¡ÍøÂçÉ豸Ãû |
| 16 | get_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 |
| 31 | state_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 |
| 40 | msg_zte_router() |
| 41 | { |
| 42 | if [ "-$c_id" == "-0" -o "-$c_id" == "-$def_cid" ]; then |
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame^] | 43 | (router_msg_proxy ipv4 $c_id >> $test_log 2>&1 || echo "Error: router_msg_proxy ipv4 wan_ipv4.sh up failed." >> $test_log) & |
| 44 | fi |
| 45 | } |
| 46 | |
| 47 | msg_zte_router_down() |
| 48 | { |
| 49 | if [ "-$c_id" == "-0" -o "-$c_id" == "-$def_cid" ]; then |
| 50 | (router_msg_proxy ipv4 0 >> $test_log 2>&1 || echo "Error: router_msg_proxy ipv4 wan_ipv4.sh down failed." >> $test_log) & |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 51 | fi |
| 52 | } |
| 53 | |
| 54 | linkup() |
| 55 | { |
| 56 | #$1 :pswan / usbwan / ethwan / wifiwan |
| 57 | wan_name=$1 |
| 58 | wan_mode=`nv get $wan_name"_mode"` |
| 59 | |
| 60 | #»ñÈ¡wan_if±äÁ¿ |
| 61 | get_wan_if $1 $2 |
| 62 | |
| 63 | if [ "-$wan_mode" == "-static" ]; then |
| 64 | wan_ip=`nv get "static_"$wan_name"_ip"` |
| 65 | wan_gw=`nv get "static_"$wan_name"_gw"` |
| 66 | wan_pridns=`nv get "static_"$wan_name"_pridns"` |
| 67 | wan_secdns=`nv get "static_"$wan_name"_secdns"` |
| 68 | wan_nm=`nv get "static_"$wan_name"_nm"` |
| 69 | if [ "-$wan_name" != "-wifiwan" ]; then |
| 70 | ifconfig $wan_if down 2>>$test_log |
| 71 | if [ $? -ne 0 ];then |
| 72 | echo "Error: ifconfig $wan_if down failed." >> $test_log |
| 73 | fi |
| 74 | ifconfig $wan_if $wan_ip up 2>>$test_log |
| 75 | if [ $? -ne 0 ];then |
| 76 | echo "Error: ifconfig $wan_if $wan_ip up failed." >> $test_log |
| 77 | fi |
| 78 | fi |
| 79 | if [ "-$wan_name" == "-ethwan" ]; then |
| 80 | nv set eth_curmode="static" |
| 81 | #rj45¾²Ì¬Á¬½Ó£¬¹Ø±Õ³¬Ê±¼ÆÊ±Æ÷ |
| 82 | (router_msg_proxy del_timer ethwan >> $test_log 2>&1 || echo "Error: router_msg_proxy del_timer failed." >> $test_log) & |
| 83 | fi |
| 84 | |
| 85 | #ÉèÖÃÍøÂçÉ豸״̬±ê־Ϊworking |
| 86 | state_set |
| 87 | #set ip¡¢gw¡¢dns to nv |
| 88 | nv set $wan_if"_ip"=$wan_ip |
| 89 | nv set $wan_if"_gw"=$wan_gw |
| 90 | nv set $wan_if"_pridns"=$wan_pridns |
| 91 | nv set $wan_if"_secdns"=$wan_secdns |
| 92 | |
| 93 | wan_pri=`nv get $1"_priority"` |
| 94 | rt_num=`expr $wan_pri \* 10 + $c_id` |
| 95 | ip rule add from $wan_ip table $rt_num 2>>$test_log |
| 96 | if [ $? -ne 0 ];then |
| 97 | echo "Error: ip rule add from $wan_ip table $rt_num failed." >> $test_log |
| 98 | fi |
| 99 | ip route add default via $wan_gw table $rt_num 2>>$test_log |
| 100 | if [ $? -ne 0 ];then |
| 101 | echo "Error: ip route add default via $wan_gw table $rt_num failed." >> $test_log |
| 102 | fi |
| 103 | ip route flush cache 2>>$test_log |
| 104 | if [ $? -ne 0 ];then |
| 105 | echo "Error: ip route flush cache failed." >> $test_log |
| 106 | fi |
| 107 | |
| 108 | #in this proxy ,we send msg to zte_mainctrl |
| 109 | msg_zte_router |
| 110 | |
| 111 | # pswan PDP |
| 112 | elif [ "-$wan_mode" == "-pdp" ]; then |
| 113 | pswan_ip=`nv get $wan_if"_ip"` |
| 114 | pswan_gw=`nv get $wan_if"_gw"` |
| 115 | pswan_pridns=`nv get $wan_if"_pridns"` |
| 116 | pswan_secdns=`nv get $wan_if"_secdns"` |
| 117 | pswan_nm=`nv get $wan_if"_nm"` |
| 118 | |
| 119 | ifconfig $wan_if down 2>>$test_log |
| 120 | if [ $? -ne 0 ];then |
| 121 | echo "Error: ifconfig $wan_if down failed." >> $test_log |
| 122 | fi |
xf.li | 6236ea7 | 2023-07-26 04:58:33 -0700 | [diff] [blame] | 123 | ifconfig $wan_if $pswan_ip netmask 255.255.255.0 up 2>>$test_log |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 124 | if [ $? -ne 0 ];then |
| 125 | echo "Error: ifconfig $wan_if $pswan_ip up failed." >> $test_log |
| 126 | fi |
| 127 | |
| 128 | pswan_pri=`nv get pswan_priority` |
| 129 | rt_num=`expr $pswan_pri \* 10 + $c_id` |
| 130 | ip rule add from $pswan_ip table $rt_num 2>>$test_log |
| 131 | if [ $? -ne 0 ];then |
| 132 | echo "Error: ip rule add from $pswan_ip table $rt_num failed." >> $test_log |
| 133 | fi |
| 134 | ip route add default via $pswan_gw table $rt_num 2>>$test_log |
| 135 | if [ $? -ne 0 ];then |
| 136 | echo "Error: ip route add default via $pswan_gw table $rt_num failed." >> $test_log |
| 137 | fi |
| 138 | ip route flush cache 2>>$test_log |
| 139 | if [ $? -ne 0 ];then |
| 140 | echo "Error: ip route flush cache failed." >> $test_log |
| 141 | fi |
| 142 | |
| 143 | msg_zte_router |
| 144 | |
| 145 | elif [ "-$wan_mode" == "-auto" ]; then |
| 146 | pppoe_user=`nv get pppoe_username` |
| 147 | pppoe_pass=`nv get pppoe_cc` |
| 148 | udhcpc_kill |
| 149 | pppoe_kill |
| 150 | |
| 151 | if [ "-$wan_name" == "-ethwan" ]; then |
| 152 | nv set eth_curmode="" |
| 153 | fi |
| 154 | if [ "-$wan_name" != "-wifiwan" ]; then |
| 155 | ifconfig $wan_if down 2>>$test_log |
| 156 | if [ $? -ne 0 ];then |
| 157 | echo "Error: ifconfig $wan_if down failed." >> $test_log |
| 158 | fi |
| 159 | ifconfig $wan_if up 2>>$test_log |
| 160 | if [ $? -ne 0 ];then |
| 161 | echo "Error: ifconfig $wan_if up failed." >> $test_log |
| 162 | fi |
| 163 | fi |
| 164 | |
| 165 | if [ "-${pppoe_user}" = "-" -a "-${pppoe_pass}" = "-" ];then |
| 166 | echo "auto wan_mode: pppoe_user is ${pppoe_user}, pppoe_pass is ${pppoe_pass}, so start dhcp client. " >> $test_log |
| 167 | udhcpc -i $wan_if -s $path_sh/udhcpc.sh & |
| 168 | else |
| 169 | sh $path_sh/pppoe_dail.sh connect |
| 170 | fi |
| 171 | |
| 172 | elif [ "-$wan_mode" == "-dhcp" ]; then |
| 173 | udhcpc_kill |
| 174 | pppoe_kill |
| 175 | |
| 176 | if [ "-$wan_name" == "-pswan" ]; then |
| 177 | ifconfig $wan_if arp 2>>$test_log |
| 178 | if [ $? -ne 0 ];then |
| 179 | echo "Error: ifconfig $wan_if arp failed." >> $test_log |
| 180 | fi |
| 181 | fi |
| 182 | |
| 183 | if [ "-$wan_name" == "-ethwan" ]; then |
| 184 | nv set eth_curmode="dhcp" |
| 185 | fi |
| 186 | |
| 187 | if [ "-$wan_name" != "-wifiwan" ]; then |
| 188 | ifconfig $wan_if down 2>>$test_log |
| 189 | if [ $? -ne 0 ];then |
| 190 | echo "Error: ifconfig $wan_if down failed." >> $test_log |
| 191 | fi |
| 192 | ifconfig $wan_if up 2>>$test_log |
| 193 | if [ $? -ne 0 ];then |
| 194 | echo "Error: ifconfig $wan_if up failed." >> $test_log |
| 195 | fi |
| 196 | fi |
| 197 | udhcpc -i $wan_if -s $path_sh/udhcpc.sh & |
| 198 | |
| 199 | elif [ "-$wan_mode" == "-pppoe" ]; then |
| 200 | udhcpc_kill |
| 201 | pppoe_kill |
| 202 | |
| 203 | if [ "-$wan_name" == "-ethwan" ]; then |
| 204 | nv set eth_curmode="pppoe" |
| 205 | fi |
| 206 | |
| 207 | if [ "-$wan_name" != "-wifiwan" ]; then |
| 208 | ifconfig $wan_if down 2>>$test_log |
| 209 | if [ $? -ne 0 ];then |
| 210 | echo "Error: ifconfig $wan_if down failed." >> $test_log |
| 211 | fi |
| 212 | ifconfig $wan_if up 2>>$test_log |
| 213 | if [ $? -ne 0 ];then |
| 214 | echo "Error: ifconfig $wan_if up failed." >> $test_log |
| 215 | fi |
| 216 | fi |
| 217 | sh $path_sh/pppoe_dail.sh connect |
| 218 | fi |
| 219 | |
| 220 | } |
| 221 | |
| 222 | del_default_wan() |
| 223 | { |
| 224 | default_wan_name=`nv get default_wan_name` |
| 225 | if [ "$1" == "$default_wan_name" ]; then |
| 226 | nv set default_wan_name="" |
| 227 | nv set default_wan_rel="" |
| 228 | fi |
| 229 | } |
| 230 | |
| 231 | linkdown() |
| 232 | { |
| 233 | wan_name=$1 |
| 234 | get_wan_if $1 $2 |
| 235 | |
| 236 | udhcpc_kill |
| 237 | pppoe_kill |
| 238 | |
| 239 | if [ "-$c_id" == "-0" -o "-$c_id" == "-$def_cid" ]; then |
| 240 | echo 0 > /proc/sys/net/ipv4/ip_forward |
| 241 | fi |
| 242 | |
| 243 | ifconfig $wan_if 0.0.0.0 2>>$test_log |
| 244 | if [ $? -ne 0 ];then |
| 245 | echo "Error: ifconfig $wan_if 0.0.0.0 failed." >> $test_log |
| 246 | fi |
| 247 | if [ "$wan_name" == "wifiwan" ]; then |
| 248 | echo $wan_if > /proc/net/dev_down |
| 249 | else |
| 250 | ifconfig $wan_if down 2>>$test_log |
| 251 | if [ $? -ne 0 ];then |
| 252 | echo "Error: ifconfig $wan_if down failed." >> $test_log |
| 253 | fi |
| 254 | fi |
| 255 | wan_ip=`nv get $wan_if"_ip"` |
| 256 | wan_gw=`nv get $wan_if"_gw"` |
| 257 | |
| 258 | route del default gw $wan_gw dev $wan_if |
| 259 | #if [ $? -ne 0 ];then |
| 260 | # echo "Error: route del default gw $wan_gw dev $wan_if failed." >> $test_log |
| 261 | #fi |
| 262 | |
| 263 | #del policy router |
| 264 | wan_pri=`nv get $1"_priority"` |
| 265 | rt_num=`expr $wan_pri \* 10 + $c_id` |
| 266 | ip rule del from $wan_ip table $rt_num |
| 267 | #if [ $? -ne 0 ];then |
| 268 | # echo "Error: ip rule del from $wan_ip table $rt_num failed." >> $test_log |
| 269 | #fi |
| 270 | ip route del default via $wan_gw table $rt_num |
| 271 | #if [ $? -ne 0 ];then |
| 272 | # echo "Error: ip route del default via $wan_gw table $rt_num failed." >> $test_log |
| 273 | #fi |
| 274 | |
| 275 | #wan_ipaddÐèÒªÊÊÅä |
| 276 | if [ "$wan_if" == "$defwan_if" ]; then |
| 277 | nv set wan_ipaddr="" |
| 278 | fi |
| 279 | nv set $wan_if"_ip"=0.0.0.0 |
| 280 | nv set $wan_if"_nm"=0.0.0.0 |
| 281 | nv set $wan_if"_gw"=0.0.0.0 |
| 282 | nv set $wan_if"_pridns"=0.0.0.0 |
| 283 | nv set $wan_if"_secdns"=0.0.0.0 |
| 284 | pdp_type=`nv get "pdp_act_type"$def_cid` |
| 285 | if [ "$2" == "$def_cid" -a "-$pdp_type" != "-IPv4v6" ]; then |
| 286 | nv set default_cid="" |
| 287 | fi |
| 288 | del_default_wan $wan_if |
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame^] | 289 | msg_zte_router_down |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 290 | |
| 291 | } |
| 292 | |
| 293 | echo "Info: wan_ipv4.sh $1 $2 $3 start" >> $test_log |
| 294 | if [ "$1" == "linkup" ]; then |
| 295 | linkup $2 $3 |
| 296 | |
| 297 | elif [ "$1" == "linkdown" ]; then |
| 298 | if [ "-$2" == "-pswan" ]; then |
| 299 | tc_tbf.sh down $def_cid |
| 300 | fi |
| 301 | linkdown $2 $3 |
| 302 | fi |