b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | #!/bin/sh |
| 2 | |
| 3 | make_resolv_conf() { |
| 4 | if [ x"$new_domain_name_servers" != x ]; then |
| 5 | cat /dev/null > /etc/resolv.conf.dhclient |
| 6 | chmod 644 /etc/resolv.conf.dhclient |
| 7 | if [ x"$new_domain_search" != x ]; then |
| 8 | echo search $new_domain_search >> /etc/resolv.conf.dhclient |
| 9 | elif [ x"$new_domain_name" != x ]; then |
| 10 | # Note that the DHCP 'Domain Name Option' is really just a domain |
| 11 | # name, and that this practice of using the domain name option as |
| 12 | # a search path is both nonstandard and deprecated. |
| 13 | echo search $new_domain_name >> /etc/resolv.conf.dhclient |
| 14 | fi |
| 15 | for nameserver in $new_domain_name_servers; do |
| 16 | echo nameserver $nameserver >>/etc/resolv.conf.dhclient |
| 17 | done |
| 18 | |
| 19 | elif [ "x${new_dhcp6_name_servers}" != x ] ; then |
| 20 | cat /dev/null > /etc/resolv.conf.dhclient6 |
| 21 | chmod 644 /etc/resolv.conf.dhclient6 |
| 22 | |
| 23 | if [ "x${new_dhcp6_domain_search}" != x ] ; then |
| 24 | echo search ${new_dhcp6_domain_search} >> /etc/resolv.conf.dhclient6 |
| 25 | fi |
| 26 | for nameserver in ${new_dhcp6_name_servers} ; do |
| 27 | echo nameserver ${nameserver} >> /etc/resolv.conf.dhclient6 |
| 28 | done |
| 29 | fi |
| 30 | |
| 31 | # if both v4 and v6 clients are running, concatenate results |
| 32 | cat /etc/resolv.conf.* > /etc/resolv.conf |
| 33 | } |
| 34 | |
| 35 | # Must be used on exit. Invokes the local dhcp client exit hooks, if any. |
| 36 | exit_with_hooks() { |
| 37 | exit_status=$1 |
| 38 | if [ -f /etc/dhclient-exit-hooks ]; then |
| 39 | . /etc/dhclient-exit-hooks |
| 40 | fi |
| 41 | # probably should do something with exit status of the local script |
| 42 | exit $exit_status |
| 43 | } |
| 44 | |
| 45 | # Invoke the local dhcp client enter hooks, if they exist. |
| 46 | if [ -f /etc/dhclient-enter-hooks ]; then |
| 47 | exit_status=0 |
| 48 | . /etc/dhclient-enter-hooks |
| 49 | # allow the local script to abort processing of this state |
| 50 | # local script must set exit_status variable to nonzero. |
| 51 | if [ $exit_status -ne 0 ]; then |
| 52 | exit $exit_status |
| 53 | fi |
| 54 | fi |
| 55 | |
| 56 | ### |
| 57 | ### DHCPv4 Handlers |
| 58 | ### |
| 59 | |
| 60 | if [ x$new_broadcast_address != x ]; then |
| 61 | new_broadcast_arg="broadcast $new_broadcast_address" |
| 62 | fi |
| 63 | if [ x$new_subnet_mask != x ]; then |
| 64 | new_subnet_arg="netmask $new_subnet_mask" |
| 65 | fi |
| 66 | if [ x$alias_subnet_mask != x ]; then |
| 67 | alias_subnet_arg="netmask $alias_subnet_mask" |
| 68 | fi |
| 69 | |
| 70 | if [ x$reason = xMEDIUM ]; then |
| 71 | # Linux doesn't do mediums (ok, ok, media). |
| 72 | exit_with_hooks 0 |
| 73 | fi |
| 74 | |
| 75 | if [ x$reason = xPREINIT ]; then |
| 76 | if [ x$alias_ip_address != x ]; then |
| 77 | # Bring down alias interface. Its routes will disappear too. |
| 78 | ifconfig $interface:0- 0.0.0.0 |
| 79 | fi |
| 80 | ifconfig $interface 0.0.0.0 up |
| 81 | |
| 82 | # We need to give the kernel some time to get the interface up. |
| 83 | sleep 1 |
| 84 | |
| 85 | exit_with_hooks 0 |
| 86 | fi |
| 87 | |
| 88 | if [ x$reason = xARPCHECK ] || [ x$reason = xARPSEND ]; then |
| 89 | exit_with_hooks 0 |
| 90 | fi |
| 91 | |
| 92 | if [ x$reason = xBOUND ] || [ x$reason = xRENEW ] || \ |
| 93 | [ x$reason = xREBIND ] || [ x$reason = xREBOOT ]; then |
| 94 | current_hostname=`hostname` |
| 95 | if [ x$current_hostname = x ] || \ |
| 96 | [ x$current_hostname = x$old_host_name ]; then |
| 97 | if [ x$current_hostname = x ] || \ |
| 98 | [ x$new_host_name != x$old_host_name ]; then |
| 99 | hostname $new_host_name |
| 100 | fi |
| 101 | fi |
| 102 | |
| 103 | if [ x$old_ip_address != x ] && [ x$alias_ip_address != x ] && \ |
| 104 | [ x$alias_ip_address != x$old_ip_address ]; then |
| 105 | # Possible new alias. Remove old alias. |
| 106 | ifconfig $interface:0- 0.0.0.0 |
| 107 | fi |
| 108 | if [ x$old_ip_address != x ] && [ x$old_ip_address != x$new_ip_address ]; then |
| 109 | # IP address changed. Bringing down the interface will delete all routes, |
| 110 | # and clear the ARP cache. |
| 111 | ifconfig $interface 0.0.0.0 down |
| 112 | |
| 113 | fi |
| 114 | if [ x$old_ip_address = x ] || [ x$old_ip_address != x$new_ip_address ] || \ |
| 115 | [ x$reason = xBOUND ] || [ x$reason = xREBOOT ]; then |
| 116 | |
| 117 | ifconfig $interface $new_ip_address $new_subnet_arg \ |
| 118 | $new_broadcast_arg |
| 119 | for router in $new_routers; do |
| 120 | if [ "x$new_subnet_mask" = "x255.255.255.255" ] ; then |
| 121 | route add -host $router dev $interface |
| 122 | fi |
| 123 | route add default gw $router |
| 124 | done |
| 125 | fi |
| 126 | if [ x$new_ip_address != x$alias_ip_address ] && [ x$alias_ip_address != x ]; |
| 127 | then |
| 128 | ifconfig $interface:0- 0.0.0.0 |
| 129 | ifconfig $interface:0 $alias_ip_address $alias_subnet_arg |
| 130 | route add -host $alias_ip_address $interface:0 |
| 131 | fi |
| 132 | make_resolv_conf |
| 133 | exit_with_hooks 0 |
| 134 | fi |
| 135 | |
| 136 | if [ x$reason = xEXPIRE ] || [ x$reason = xFAIL ] || [ x$reason = xRELEASE ] \ |
| 137 | || [ x$reason = xSTOP ]; then |
| 138 | if [ x$alias_ip_address != x ]; then |
| 139 | # Turn off alias interface. |
| 140 | ifconfig $interface:0- 0.0.0.0 |
| 141 | fi |
| 142 | if [ x$old_ip_address != x ]; then |
| 143 | # Shut down interface, which will delete routes and clear arp cache. |
| 144 | ifconfig $interface 0.0.0.0 down |
| 145 | fi |
| 146 | if [ x$alias_ip_address != x ]; then |
| 147 | ifconfig $interface:0 $alias_ip_address $alias_subnet_arg |
| 148 | route add -host $alias_ip_address $interface:0 |
| 149 | fi |
| 150 | |
| 151 | # remove v4 dns configuration for this interface |
| 152 | rm /etc/resolv.conf.dhclient |
| 153 | cat /etc/resolv.conf.* > /etc/resolv.conf |
| 154 | |
| 155 | exit_with_hooks 0 |
| 156 | fi |
| 157 | |
| 158 | if [ x$reason = xTIMEOUT ]; then |
| 159 | if [ x$alias_ip_address != x ]; then |
| 160 | ifconfig $interface:0- 0.0.0.0 |
| 161 | fi |
| 162 | ifconfig $interface $new_ip_address $new_subnet_arg \ |
| 163 | $new_broadcast_arg |
| 164 | set $new_routers |
| 165 | if ping -q -c 1 $1; then |
| 166 | if [ x$new_ip_address != x$alias_ip_address ] && \ |
| 167 | [ x$alias_ip_address != x ]; then |
| 168 | ifconfig $interface:0 $alias_ip_address $alias_subnet_arg |
| 169 | route add -host $alias_ip_address dev $interface:0 |
| 170 | fi |
| 171 | for router in $new_routers; do |
| 172 | if [ "x$new_subnet_mask" = "x255.255.255.255" ] ; then |
| 173 | route add -host $router dev $interface |
| 174 | fi |
| 175 | route add default gw $router |
| 176 | done |
| 177 | make_resolv_conf |
| 178 | exit_with_hooks 0 |
| 179 | fi |
| 180 | ifconfig $interface 0.0.0.0 down |
| 181 | exit_with_hooks 1 |
| 182 | fi |
| 183 | |
| 184 | ### |
| 185 | ### DHCPv6 Handlers |
| 186 | ### |
| 187 | |
| 188 | if [ x$reason = xPREINIT6 ]; then |
| 189 | # Ensure interface is up. |
| 190 | ifconfig ${interface} up |
| 191 | |
| 192 | # Remove any stale addresses from aborted clients. |
| 193 | ip -f inet6 addr flush dev ${interface} scope global |
| 194 | |
| 195 | exit_with_hooks 0 |
| 196 | fi |
| 197 | |
| 198 | if [ x${old_ip6_prefix} != x ] || [ x${new_ip6_prefix} != x ] ; then |
| 199 | echo Prefix ${reason} old=${old_ip6_prefix} new=${new_ip6_prefix} |
| 200 | |
| 201 | exit_with_hooks 0 |
| 202 | fi |
| 203 | |
| 204 | if [ x$reason = xBOUND6 ]; then |
| 205 | if [ x${new_ip6_address} = x ] || [ x${new_ip6_prefixlen} = x ] ; then |
| 206 | exit_with_hooks 2; |
| 207 | fi |
| 208 | |
| 209 | ifconfig ${interface} add ${new_ip6_address}/${new_ip6_prefixlen} |
| 210 | |
| 211 | # Check for nameserver options. |
| 212 | make_resolv_conf |
| 213 | |
| 214 | ### << |
| 215 | # Set up softwire tunnel |
| 216 | if [ x${new_dhcp6_softwire} != x ] ; then |
| 217 | /etc/init.d/dhclient stop |
| 218 | ifconfig ${interface} 0.0.0.0 |
| 219 | ip -6 tunnel add tun0 mode ipip6 \ |
| 220 | remote ${new_dhcp6_softwire} \ |
| 221 | local ${new_ip6_address} \ |
| 222 | dev ${interface} encaplimit none |
| 223 | ip link set tun0 up |
| 224 | ip route add default dev tun0 |
| 225 | fi |
| 226 | ### >> |
| 227 | |
| 228 | exit_with_hooks 0 |
| 229 | fi |
| 230 | |
| 231 | if [ x$reason = xRENEW6 ] || [ x$reason = xREBIND6 ]; then |
| 232 | if [ x${new_ip6_address} = x ] || [ x${new_ip6_prefixlen} = x ] ; then |
| 233 | exit_with_hooks 2; |
| 234 | fi |
| 235 | |
| 236 | ifconfig ${interface} add ${new_ip6_address}/${new_ip6_prefixlen} |
| 237 | |
| 238 | # Make sure nothing has moved around on us. |
| 239 | |
| 240 | # Nameservers/domains/etc. |
| 241 | if [ "x${new_dhcp6_name_servers}" != "x${old_dhcp6_name_servers}" ] || |
| 242 | [ "x${new_dhcp6_domain_search}" != "x${old_dhcp6_domain_search}" ] ; then |
| 243 | make_resolv_conf |
| 244 | fi |
| 245 | |
| 246 | exit_with_hooks 0 |
| 247 | fi |
| 248 | |
| 249 | if [ x$reason = xDEPREF6 ]; then |
| 250 | if [ x${new_ip6_address} = x ] ; then |
| 251 | exit_with_hooks 2; |
| 252 | fi |
| 253 | |
| 254 | # Busybox ifconfig has no way to communicate this to the kernel, so ignore it |
| 255 | |
| 256 | exit_with_hooks 0 |
| 257 | fi |
| 258 | |
| 259 | if [ x$reason = xEXPIRE6 -o x$reason = xRELEASE6 -o x$reason = xSTOP6 ]; then |
| 260 | if [ x${old_ip6_address} = x ] || [ x${old_ip6_prefixlen} = x ] ; then |
| 261 | exit_with_hooks 2; |
| 262 | fi |
| 263 | |
| 264 | ifconfig ${interface} del ${old_ip6_address}/${old_ip6_prefixlen} |
| 265 | |
| 266 | # remove v6 dns configuration for this interface |
| 267 | rm /etc/resolv.conf.dhclient6 |
| 268 | cat /etc/resolv.conf.* > /etc/resolv.conf |
| 269 | |
| 270 | ### << |
| 271 | # Tear down softwire tunnel |
| 272 | if [ x${old_dhcp6_softwire} != x ] ; then |
| 273 | ip link set tun0 down |
| 274 | ip tunnel del tun0 |
| 275 | fi |
| 276 | ### >> |
| 277 | |
| 278 | exit_with_hooks 0 |
| 279 | fi |
| 280 | |
| 281 | exit_with_hooks 0 |