lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # $Id: config-udhcpd.sh,v 1.10 2010-06-18 06:33:21 steven Exp $ |
| 4 | # |
| 5 | # usage: see function usage() |
| 6 | |
| 7 | path_sh=`nv get path_sh` |
| 8 | . $path_sh/global.sh |
| 9 | |
| 10 | dhcp_type=$1 |
| 11 | |
| 12 | usage () { |
| 13 | echo "usage: config-udhcpd.sh [option]..." |
| 14 | echo "options:" |
| 15 | echo " -h : print this help" |
| 16 | echo " -s ipaddr : set ipaddr as start of the IP lease block" |
| 17 | echo " -e ipaddr : set ipaddr as end of the IP lease block" |
| 18 | echo " -i ifc : set ifc as the interface that udhcpd will use" |
| 19 | echo " -d dns1 [dns2] : set dns1 and dns2 as DNS" |
| 20 | echo " -m mask : set mask as subnet netmask" |
| 21 | echo " -g gateway : set gateway as router's IP address" |
| 22 | echo " -t time : set time seconds as the IP life time" |
| 23 | echo " -r [sleep_time] : run dhcp server" |
| 24 | echo " -k : kill the running dhcp server" |
| 25 | echo " -S [mac ipaddr] : statically assign IP to given MAC address" |
| 26 | |
| 27 | exit |
| 28 | } |
| 29 | |
| 30 | config () { |
| 31 | case "$1" in |
| 32 | "-s") |
| 33 | sed -e '/start/d' $fname > $fbak |
| 34 | echo "start $2" >> $fbak ;; |
| 35 | "-e") |
| 36 | sed -e '/end/d' $fname > $fbak |
| 37 | echo "end $2" >> $fbak ;; |
| 38 | "-i") |
| 39 | sed -e '/interface/d' $fname > $fbak |
| 40 | echo "interface $2" >> $fbak ;; |
| 41 | "-p") |
| 42 | sed -e '/pidfile/d' $fname > $fbak |
| 43 | echo "pidfile $2" >> $fbak ;; |
| 44 | "-l") |
| 45 | sed -e '/lease_file/d' $fname > $fbak |
| 46 | echo "lease_file $2" >> $fbak ;; |
| 47 | "-d") |
| 48 | sed -e '/option *dns/d' $fname > $fbak |
| 49 | echo "option dns $2 $3" >> $fbak ;; |
| 50 | "-m") |
| 51 | sed -e '/option *subnet/d' $fname > $fbak |
| 52 | echo "option subnet $2" >> $fbak ;; |
| 53 | "-g") |
| 54 | sed -e '/option *router/d' $fname > $fbak |
| 55 | echo "option router $2" >> $fbak ;; |
| 56 | "-t") |
| 57 | sed -e '/option *lease/d' $fname > $fbak |
| 58 | echo "option lease $2" >> $fbak ;; |
| 59 | "-S") |
| 60 | if [ "$2" = "" ]; then |
| 61 | sed -e '/static_lease/d' $fname > $fbak |
| 62 | elif [ "$3" = "" ]; then |
| 63 | echo "insufficient arguments.." |
| 64 | usage |
| 65 | else |
| 66 | sed -e "/$2/d" $fname > $fbak |
| 67 | echo "static_lease $2 $3" >> $fbak |
| 68 | cat $fbak > $fname |
| 69 | rm -f $fbak |
| 70 | sed -e "/$2/d" $fname_static > $fbak_static |
| 71 | echo "$2 $3" >> $fbak_static |
| 72 | cat $fbak_static > $fname_static |
| 73 | rm -f $fbak_static |
| 74 | return |
| 75 | fi |
| 76 | ;; |
| 77 | "-x") |
| 78 | sed -e '/static_netmask/d' $fname > $fbak |
| 79 | echo "static_netmask $2" >> $fbak ;; |
| 80 | "-y") |
| 81 | sed -e '/static_router/d' $fname > $fbak |
| 82 | echo "static_router $2" >> $fbak ;; |
| 83 | "-D") |
| 84 | sed -e "/$2/d" $fname > $fbak |
| 85 | sed -e "/$2/d" $fname_static > $fbak_static |
| 86 | cat $fbak_static > $fname_static |
| 87 | rm -f $fbak_static;; |
| 88 | "-E") |
| 89 | while read LINE |
| 90 | do |
| 91 | echo "static_lease $LINE" >> $fbak |
| 92 | done < $fname_static ;; |
| 93 | *) return;; |
| 94 | esac |
| 95 | cat $fbak > $fname |
| 96 | rm -f $fbak |
| 97 | return |
| 98 | } |
| 99 | |
| 100 | # arg1: phy address. |
| 101 | link_down() |
| 102 | { |
| 103 | # get original register value |
| 104 | get_mii=`mii_mgr -g -p $1 -r 0` |
| 105 | orig=`echo $get_mii | sed 's/^.....................//'` |
| 106 | |
| 107 | # stupid hex value calculation. |
| 108 | pre=`echo $orig | sed 's/...$//'` |
| 109 | post=`echo $orig | sed 's/^..//'` |
| 110 | num_hex=`echo $orig | sed 's/^.//' | sed 's/..$//'` |
| 111 | case $num_hex in |
| 112 | "0") rep="8" ;; |
| 113 | "1") rep="9" ;; |
| 114 | "2") rep="a" ;; |
| 115 | "3") rep="b" ;; |
| 116 | "4") rep="c" ;; |
| 117 | "5") rep="d" ;; |
| 118 | "6") rep="e" ;; |
| 119 | "7") rep="f" ;; |
| 120 | # The power is already down |
| 121 | *) echo "Port$1 is down. Skip.";return;; |
| 122 | esac |
| 123 | new=$pre$rep$post |
| 124 | # power down |
| 125 | mii_mgr -s -p $1 -r 0 -v $new 2>>$test_log |
| 126 | if [ $? -ne 0 ];then |
| 127 | echo "Error: mii_mgr -s -p $1 -r 0 -v $new" >> $test_log |
| 128 | fi |
| 129 | } |
| 130 | |
| 131 | link_up() |
| 132 | { |
| 133 | # get original register value |
| 134 | get_mii=`mii_mgr -g -p $1 -r 0` |
| 135 | orig=`echo $get_mii | sed 's/^.....................//'` |
| 136 | |
| 137 | # stupid hex value calculation. |
| 138 | pre=`echo $orig | sed 's/...$//'` |
| 139 | post=`echo $orig | sed 's/^..//'` |
| 140 | num_hex=`echo $orig | sed 's/^.//' | sed 's/..$//'` |
| 141 | case $num_hex in |
| 142 | "8") rep="0" ;; |
| 143 | "9") rep="1" ;; |
| 144 | "a") rep="2" ;; |
| 145 | "b") rep="3" ;; |
| 146 | "c") rep="4" ;; |
| 147 | "d") rep="5" ;; |
| 148 | "e") rep="6" ;; |
| 149 | "f") rep="7" ;; |
| 150 | # The power is already up |
| 151 | *) echo "Port$1 is up. Skip.";return;; |
| 152 | esac |
| 153 | new=$pre$rep$post |
| 154 | # power up |
| 155 | mii_mgr -s -p $1 -r 0 -v $new 2>>$test_log |
| 156 | if [ $? -ne 0 ];then |
| 157 | echo "Error: mii_mgr -s -p $1 -r 0 -v $new" >> $test_log |
| 158 | fi |
| 159 | } |
| 160 | |
| 161 | reset_all_phys() |
| 162 | { |
| 163 | sleep_time=$1 |
| 164 | |
| 165 | if [ "x$CONFIG_RAETH_ROUTER" != "xy" -a "x$CONFIG_RT_3052_ESW" != "xy" ]; then |
| 166 | return |
| 167 | fi |
| 168 | |
| 169 | opmode=`nvram_get 2860 OperationMode` |
| 170 | |
| 171 | #skip WAN port |
| 172 | if [ "x$opmode" != "x1" ]; then #no wan port |
| 173 | link_down 0 |
| 174 | link_down 4 |
| 175 | elif [ "x$CONFIG_WAN_AT_P4" = "xy" ]; then #wan port at port4 |
| 176 | link_down 0 |
| 177 | elif [ "x$CONFIG_WAN_AT_P0" = "xy" ]; then #wan port at port0 |
| 178 | link_down 4 |
| 179 | fi |
| 180 | link_down 1 |
| 181 | link_down 2 |
| 182 | link_down 3 |
| 183 | |
| 184 | #force Windows clients to renew IP and update DNS server |
| 185 | sleep $sleep_time |
| 186 | |
| 187 | #skip WAN port |
| 188 | if [ "x$opmode" != "x1" ]; then #no wan port |
| 189 | link_up 0 |
| 190 | link_up 4 |
| 191 | elif [ "x$CONFIG_WAN_AT_P4" = "xy" ]; then #wan port at port4 |
| 192 | link_up 0 |
| 193 | elif [ "x$CONFIG_WAN_AT_P0" = "xy" ]; then #wan port at port0 |
| 194 | link_up 4 |
| 195 | fi |
| 196 | link_up 1 |
| 197 | link_up 2 |
| 198 | link_up 3 |
| 199 | } |
| 200 | |
| 201 | # argv 1 is empty |
| 202 | if [ "$1" = "" ]; then |
| 203 | usage |
| 204 | fi |
| 205 | |
| 206 | # argv 2 is empty |
| 207 | if [ "$2" = "" ]; then |
| 208 | if [ "$1" != "-r" -a "$1" != "-k" -a "$1" != "-S" ]; then |
| 209 | usage |
| 210 | fi |
| 211 | fi |
| 212 | |
| 213 | |
| 214 | if [ "x$1" == "xlan" ]; then |
| 215 | fname=$path_conf"/udhcpd.conf" |
| 216 | fbak=$path_conf"/udhcpd.conf_bak" |
| 217 | pidfile=$path_conf"/udhcpd.pid" |
| 218 | leases=$path_conf"/udhcpd.leases" |
| 219 | else |
| 220 | fname=$path_conf"/udhcpd"$1".conf" |
| 221 | fbak=$path_conf"/udhcpd"$1".conf_bak" |
| 222 | pidfile=$path_conf"/udhcpd"$1".pid" |
| 223 | leases=$path_conf"/udhcpd"$1".leases" |
| 224 | fi |
| 225 | fname_static=$path_conf"/static_macip_file" |
| 226 | fbak_static=$path_conf"/static_macip_file_bak" |
| 227 | touch $fname |
| 228 | |
| 229 | case "$2" in |
| 230 | |
| 231 | "-r") |
| 232 | |
| 233 | if [ -e ${pidfile} ]; then |
| 234 | kill `cat $pidfile` |
| 235 | fi |
| 236 | rm -f $pidfile |
| 237 | touch $leases |
| 238 | sed '/^lease_file /d' $fname > $fbak |
| 239 | cat $fbak > $fname |
| 240 | echo "lease_file $leases" >> $fname |
| 241 | linenum=`cat $fname|wc -l` |
| 242 | if [ $linenum -ne 9 -a "$dhcp_type" == "lan" ]; then |
| 243 | . $path_sh/user-config-udhcpd.sh |
| 244 | fi |
| 245 | udhcpd -f $fname & |
| 246 | reset_all_phys $3 |
| 247 | ;; |
| 248 | "-h") usage;; |
| 249 | "-p") config "$2" "$3";; |
| 250 | "-l") config "$2" "$3";; |
| 251 | "-s") config "$2" "$3";; |
| 252 | "-e") config "$2" "$3";; |
| 253 | "-i") config "$2" "$3";; |
| 254 | "-d") config "$2" "$3" "$4";; |
| 255 | "-m") config "$2" "$3";; |
| 256 | "-g") config "$2" "$3";; |
| 257 | "-t") config "$2" "$3";; |
| 258 | "-S") config "$2" "$3" "$4";; |
| 259 | "-D") config "$2" "$3";; |
| 260 | "-E") config "$2";; |
| 261 | "-k") |
| 262 | if [ -e ${pidfile} ]; then |
| 263 | kill `cat $pidfile` |
| 264 | fi |
| 265 | rm -f $pidfile ;; |
| 266 | "-x") config "$2" "$3";; |
| 267 | "-y") config "$2" "$3";; |
| 268 | *) usage;; |
| 269 | esac |
| 270 | |
| 271 | |