1 1.1 christos #!/bin/sh 2 1.1 christos 3 1.1 christos make_resolv_conf() { 4 1.1 christos if [ x"$new_domain_name_servers" != x ]; then 5 1.1 christos cat /dev/null > /etc/resolv.conf.dhclient 6 1.1 christos if [ x"$new_domain_search" != x ]; then 7 1.1 christos echo search $new_domain_search >> /etc/resolv.conf.dhclient 8 1.1 christos elif [ x"$new_domain_name" != x ]; then 9 1.1 christos # Note that the DHCP 'Domain Name Option' is really just a domain 10 1.1 christos # name, and that this practice of using the domain name option as 11 1.1 christos # a search path is both nonstandard and deprecated. 12 1.1 christos echo search $new_domain_name >> /etc/resolv.conf.dhclient 13 1.1 christos fi 14 1.1 christos for nameserver in $new_domain_name_servers; do 15 1.1 christos echo nameserver $nameserver >>/etc/resolv.conf.dhclient 16 1.1 christos done 17 1.1 christos 18 1.1 christos mv /etc/resolv.conf.dhclient /etc/resolv.conf 19 1.1 christos elif [ "x${new_dhcp6_name_servers}" != x ] ; then 20 1.1 christos cat /dev/null > /etc/resolv.conf.dhclient6 21 1.1 christos chmod 644 /etc/resolv.conf.dhclient6 22 1.1 christos 23 1.1 christos if [ "x${new_dhcp6_domain_search}" != x ] ; then 24 1.1 christos echo search ${new_dhcp6_domain_search} >> /etc/resolv.conf.dhclient6 25 1.1 christos fi 26 1.1 christos for nameserver in ${new_dhcp6_name_servers} ; do 27 1.1 christos # If the nameserver has a link-local address 28 1.1 christos # add a <zone_id> (interface name) to it. 29 1.1 christos case $nameserver in 30 1.1 christos fe80:*) zone_id="%$interface";; 31 1.1 christos FE80:*) zone_id="%$interface";; 32 1.1 christos *) zone_id="";; 33 1.1 christos esac 34 1.1 christos echo nameserver ${nameserver}$zone_id >> /etc/resolv.conf.dhclient6 35 1.1 christos done 36 1.1 christos 37 1.1 christos mv /etc/resolv.conf.dhclient6 /etc/resolv.conf 38 1.1 christos fi 39 1.1 christos } 40 1.1 christos 41 1.1 christos # Must be used on exit. Invokes the local dhcp client exit hooks, if any. 42 1.1 christos exit_with_hooks() { 43 1.1 christos exit_status=$1 44 1.1 christos if [ -f /etc/dhclient-exit-hooks ]; then 45 1.1 christos . /etc/dhclient-exit-hooks 46 1.1 christos fi 47 1.1 christos # probably should do something with exit status of the local script 48 1.1 christos exit $exit_status 49 1.1 christos } 50 1.1 christos 51 1.1 christos # This function was largely borrowed from dhclient-script that 52 1.1 christos # ships with Centos, authored by Jiri Popelka and David Cantrell 53 1.1 christos # of Redhat. Thanks guys. 54 1.1 christos add_ipv6_addr_with_DAD() { 55 1.1 christos ifconfig ${interface} inet6 ${new_ip6_address}/${new_ip6_prefixlen} alias 56 1.1 christos 57 1.1 christos if [ ${dad_wait_time} -le 0 ] 58 1.1 christos then 59 1.1 christos # if we're not waiting for DAD, assume we're good 60 1.1 christos return 0 61 1.1 christos fi 62 1.1 christos 63 1.1 christos # Repeatedly test whether newly added address passed 64 1.1 christos # duplicate address detection (DAD) 65 1.1 christos i=0 66 1.1 christos while [ $i -lt ${dad_wait_time} ]; do 67 1.1 christos sleep 1 # give the DAD some time 68 1.1 christos 69 1.1 christos addr=$(ifconfig ${interface} \ 70 1.1 christos | grep "${new_ip6_address} prefixlen ${new_ip6_prefixlen}") 71 1.1 christos 72 1.1 christos # tentative flag == DAD is still not complete 73 1.1 christos tentative=$(echo "${addr}" | grep tentative) 74 1.1 christos # dadfailed flag == address is already in use somewhere else 75 1.1 christos dadfailed=$(echo "${addr}" | grep duplicated) 76 1.1 christos 77 1.1 christos if [ -n "${dadfailed}" ] ; then 78 1.1 christos # dad failed, remove the address 79 1.1 christos ifconfig ${interface} inet6 ${new_ip6_address}/${new_ip6_prefixlen} -alias 80 1.1 christos exit_with_hooks 3 81 1.1 christos fi 82 1.1 christos 83 1.1 christos if [ -z "${tentative}" ] ; then 84 1.1 christos if [ -n "${addr}" ]; then 85 1.1 christos # DAD is over 86 1.1 christos return 0 87 1.1 christos else 88 1.1 christos # address was auto-removed (or not added at all) 89 1.1 christos exit_with_hooks 3 90 1.1 christos fi 91 1.1 christos fi 92 1.1 christos true $(( i++ )) 93 1.1 christos done 94 1.1 christos 95 1.1 christos return 0 96 1.1 christos } 97 1.1 christos 98 1.1 christos # Invoke the local dhcp client enter hooks, if they exist. 99 1.1 christos if [ -f /etc/dhclient-enter-hooks ]; then 100 1.1 christos exit_status=0 101 1.1 christos . /etc/dhclient-enter-hooks 102 1.1 christos # allow the local script to abort processing of this state 103 1.1 christos # local script must set exit_status variable to nonzero. 104 1.1 christos if [ $exit_status -ne 0 ]; then 105 1.1 christos exit $exit_status 106 1.1 christos fi 107 1.1 christos fi 108 1.1 christos 109 1.1 christos if [ x$new_network_number != x ]; then 110 1.1 christos echo New Network Number: $new_network_number 111 1.1 christos fi 112 1.1 christos 113 1.1 christos if [ x$new_broadcast_address != x ]; then 114 1.1 christos echo New Broadcast Address: $new_broadcast_address 115 1.1 christos new_broadcast_arg="broadcast $new_broadcast_address" 116 1.1 christos fi 117 1.1 christos if [ x$old_broadcast_address != x ]; then 118 1.1 christos old_broadcast_arg="broadcast $old_broadcast_address" 119 1.1 christos fi 120 1.1 christos if [ x$new_subnet_mask != x ]; then 121 1.1 christos new_netmask_arg="netmask $new_subnet_mask" 122 1.1 christos fi 123 1.1 christos if [ x$old_subnet_mask != x ]; then 124 1.1 christos old_netmask_arg="netmask $old_subnet_mask" 125 1.1 christos fi 126 1.1 christos if [ x$alias_subnet_mask != x ]; then 127 1.1 christos alias_subnet_arg="netmask $alias_subnet_mask" 128 1.1 christos fi 129 1.1 christos 130 1.1 christos if [ x$reason = xMEDIUM ]; then 131 1.1 christos eval "ifconfig $interface $medium" 132 1.1 christos eval "ifconfig $interface inet -alias 0.0.0.0 $medium" >/dev/null 2>&1 133 1.1 christos sleep 1 134 1.1 christos exit_with_hooks 0 135 1.1 christos fi 136 1.1 christos 137 1.1 christos ### 138 1.1 christos ### DHCPv4 Handlers 139 1.1 christos ### 140 1.1 christos 141 1.1 christos if [ x$reason = xPREINIT ]; then 142 1.1 christos if [ x$alias_ip_address != x ]; then 143 1.1 christos ifconfig $interface inet -alias $alias_ip_address > /dev/null 2>&1 144 1.1 christos route delete $alias_ip_address 127.0.0.1 > /dev/null 2>&1 145 1.1 christos fi 146 1.1 christos ifconfig $interface inet 0.0.0.0 netmask 0.0.0.0 \ 147 1.1 christos broadcast 255.255.255.255 up 148 1.1 christos exit_with_hooks 0 149 1.1 christos fi 150 1.1 christos 151 1.1 christos if [ x$reason = xARPCHECK ] || [ x$reason = xARPSEND ]; then 152 1.1 christos exit_with_hooks 0; 153 1.1 christos fi 154 1.1 christos 155 1.1 christos if [ x$reason = xBOUND ] || [ x$reason = xRENEW ] || \ 156 1.1 christos [ x$reason = xREBIND ] || [ x$reason = xREBOOT ]; then 157 1.1 christos current_hostname=`hostname` 158 1.1 christos if [ x$current_hostname = x ] || \ 159 1.1 christos [ x$current_hostname = x$old_host_name ]; then 160 1.1 christos if [ x$current_hostname = x ] || \ 161 1.1 christos [ x$new_host_name != x$old_host_name ]; then 162 1.1 christos hostname $new_host_name 163 1.1 christos fi 164 1.1 christos fi 165 1.1 christos 166 1.1 christos if [ x$old_ip_address != x ] && [ x$alias_ip_address != x ] && \ 167 1.1 christos [ x$alias_ip_address != x$old_ip_address ]; then 168 1.1 christos ifconfig $interface inet -alias $alias_ip_address > /dev/null 2>&1 169 1.1 christos route delete $alias_ip_address 127.0.0.1 > /dev/null 2>&1 170 1.1 christos fi 171 1.1 christos if [ x$old_ip_address != x ] && [ x$old_ip_address != x$new_ip_address ] 172 1.1 christos then 173 1.1 christos eval "ifconfig $interface inet -alias $old_ip_address $medium" 174 1.1 christos route delete $old_ip_address 127.1 >/dev/null 2>&1 175 1.1 christos for router in $old_routers; do 176 1.1 christos route delete default $router >/dev/null 2>&1 177 1.1 christos done 178 1.1 christos if [ "$old_static_routes" != "" ]; then 179 1.1 christos set $old_static_routes 180 1.1 christos while [ $# -gt 1 ]; do 181 1.1 christos route delete $1 $2 182 1.1 christos shift; shift 183 1.1 christos done 184 1.1 christos fi 185 1.1 christos arp -n -a | sed -n -e 's/^.*(\(.*\)) at .*$/arp -n -d \1/p' |sh 186 1.1 christos fi 187 1.1 christos if [ x$old_ip_address = x ] || [ x$old_ip_address != x$new_ip_address ] || \ 188 1.1 christos [ x$reason = xBOUND ] || [ x$reason = xREBOOT ]; then 189 1.1 christos eval "ifconfig $interface inet $new_ip_address $new_netmask_arg \ 190 1.1 christos $new_broadcast_arg $medium" 191 1.1 christos route add $new_ip_address 127.1 >/dev/null 2>&1 192 1.1 christos for router in $new_routers; do 193 1.1 christos route add default $router >/dev/null 2>&1 194 1.1 christos done 195 1.1 christos if [ "$new_static_routes" != "" ]; then 196 1.1 christos set $new_static_routes 197 1.1 christos while [ $# -gt 1 ]; do 198 1.1 christos route add $1 $2 199 1.1 christos shift; shift 200 1.1 christos done 201 1.1 christos fi 202 1.1 christos else 203 1.1 christos # we haven't changed the address, have we changed other options 204 1.1 christos # that we wish to update? 205 1.1 christos if [ x$new_routers != x ] && [ x$new_routers != x$old_routers ] ; then 206 1.1 christos # if we've changed routers delete the old and add the new. 207 1.1 christos $LOGGER "New Routers: $new_routers" 208 1.1 christos for router in $old_routers; do 209 1.1 christos route delete default $router >/dev/null 2>&1 210 1.1 christos done 211 1.1 christos for router in $new_routers; do 212 1.1 christos route add default $router >/dev/null 2>&1 213 1.1 christos done 214 1.1 christos fi 215 1.1 christos fi 216 1.1 christos if [ x$new_ip_address != x$alias_ip_address ] && [ x$alias_ip_address != x ]; 217 1.1 christos then 218 1.1 christos ifconfig $interface inet alias $alias_ip_address $alias_subnet_arg 219 1.1 christos route add $alias_ip_address 127.0.0.1 220 1.1 christos fi 221 1.1 christos make_resolv_conf 222 1.1 christos exit_with_hooks 0 223 1.1 christos fi 224 1.1 christos 225 1.1 christos if [ x$reason = xEXPIRE ] || [ x$reason = xFAIL ] || [ x$reason = xRELEASE ] \ 226 1.1 christos || [ x$reason = xSTOP ]; then 227 1.1 christos if [ x$alias_ip_address != x ]; then 228 1.1 christos ifconfig $interface inet -alias $alias_ip_address > /dev/null 2>&1 229 1.1 christos route delete $alias_ip_address 127.0.0.1 > /dev/null 2>&1 230 1.1 christos fi 231 1.1 christos if [ x$old_ip_address != x ]; then 232 1.1 christos eval "ifconfig $interface inet -alias $old_ip_address $medium" 233 1.1 christos route delete $old_ip_address 127.1 >/dev/null 2>&1 234 1.1 christos for router in $old_routers; do 235 1.1 christos route delete default $router >/dev/null 2>&1 236 1.1 christos done 237 1.1 christos if [ "$old_static_routes" != "" ]; then 238 1.1 christos set $old_static_routes 239 1.1 christos while [ $# -gt 1 ]; do 240 1.1 christos route delete $1 $2 241 1.1 christos shift; shift 242 1.1 christos done 243 1.1 christos fi 244 1.1 christos arp -n -a | sed -n -e 's/^.*(\(.*\)) at .*$/arp -n -d \1/p' \ 245 1.1 christos |sh >/dev/null 2>&1 246 1.1 christos fi 247 1.1 christos if [ x$alias_ip_address != x ]; then 248 1.1 christos ifconfig $interface inet alias $alias_ip_address $alias_subnet_arg 249 1.1 christos route add $alias_ip_address 127.0.0.1 250 1.1 christos fi 251 1.1 christos exit_with_hooks 0 252 1.1 christos fi 253 1.1 christos 254 1.1 christos if [ x$reason = xTIMEOUT ]; then 255 1.1 christos if [ x$alias_ip_address != x ]; then 256 1.1 christos ifconfig $interface inet -alias $alias_ip_address > /dev/null 2>&1 257 1.1 christos route delete $alias_ip_address 127.0.0.1 > /dev/null 2>&1 258 1.1 christos fi 259 1.1 christos eval "ifconfig $interface inet $new_ip_address $new_netmask_arg \ 260 1.1 christos $new_broadcast_arg $medium" 261 1.1 christos sleep 1 262 1.1 christos if [ "$new_routers" != "" ]; then 263 1.1 christos set $new_routers 264 1.1 christos if ping -q -c 1 -w 1 $1; then 265 1.1 christos if [ x$new_ip_address != x$alias_ip_address ] && \ 266 1.1 christos [ x$alias_ip_address != x ]; then 267 1.1 christos ifconfig $interface inet alias $alias_ip_address $alias_subnet_arg 268 1.1 christos route add $alias_ip_address 127.0.0.1 269 1.1 christos fi 270 1.1 christos route add $new_ip_address 127.1 >/dev/null 2>&1 271 1.1 christos for router in $new_routers; do 272 1.1 christos route add default $router >/dev/null 2>&1 273 1.1 christos done 274 1.1 christos set $new_static_routes 275 1.1 christos while [ $# -gt 1 ]; do 276 1.1 christos route add $0 $1 277 1.1 christos shift; shift 278 1.1 christos done 279 1.1 christos make_resolv_conf 280 1.1 christos exit_with_hooks 0 281 1.1 christos fi 282 1.1 christos fi 283 1.1 christos eval "ifconfig $interface inet -alias $new_ip_address $medium" 284 1.1 christos for router in $old_routers; do 285 1.1 christos route delete default $router >/dev/null 2>&1 286 1.1 christos done 287 1.1 christos if [ "$old_static_routes" != "" ]; then 288 1.1 christos set $old_static_routes 289 1.1 christos while [ $# -gt 1 ]; do 290 1.1 christos route delete $1 $2 291 1.1 christos shift; shift 292 1.1 christos done 293 1.1 christos fi 294 1.1 christos arp -n -a | sed -n -e 's/^.*(\(.*\)) at .*$/arp -n -d \1/p' \ 295 1.1 christos |sh >/dev/null 2>&1 296 1.1 christos exit_with_hooks 1 297 1.1 christos fi 298 1.1 christos 299 1.1 christos ### 300 1.1 christos ### DHCPv6 Handlers 301 1.1 christos ### 302 1.1 christos 303 1.1 christos if [ ${reason} = PREINIT6 ] ; then 304 1.1 christos # Ensure interface is up. 305 1.1 christos ifconfig ${interface} up 306 1.1 christos 307 1.1 christos # XXX: Remove any stale addresses from aborted clients. 308 1.1 christos 309 1.1 christos # We need to give the kernel some time to active interface 310 1.1 christos interface_up_wait_time=5 311 1.1 christos i=0 312 1.1 christos while [ $i -lt ${interface_up_wait_time} ]; do 313 1.1 christos ifconfig ${interface} | grep inactive >/dev/null 2>&1 314 1.1 christos if [ $? -ne 0 ]; then 315 1.1 christos break; 316 1.1 christos fi 317 1.1 christos sleep 1 318 1.1 christos true $(( i++ )) 319 1.1 christos done 320 1.1 christos 321 1.1 christos # Wait for duplicate address detection for this interface if the 322 1.1 christos # --dad-wait-time parameter has been specified and is greater than 323 1.1 christos # zero. 324 1.1 christos if [ ${dad_wait_time} -gt 0 ]; then 325 1.1 christos # Check if any IPv6 address on this interface is marked as 326 1.1 christos # tentative. 327 1.1 christos ifconfig ${interface} | grep inet6 | grep tentative \ 328 1.1 christos >/dev/null 2>&1 329 1.1 christos if [ $? -eq 0 ]; then 330 1.1 christos # Wait for duplicate address detection to complete or for 331 1.1 christos # the timeout specified as --dad-wait-time. 332 1.1 christos i=0 333 1.1 christos while [ $i -lt ${dad_wait_time} ]; do 334 1.1 christos # We're going to poll for the tentative flag every second. 335 1.1 christos sleep 1 336 1.1 christos ifconfig ${interface} | grep inet6 | grep tentative \ 337 1.1 christos >/dev/null 2>&1 338 1.1 christos if [ $? -ne 0 ]; then 339 1.1 christos break; 340 1.1 christos fi 341 1.1 christos true $(( i++ )) 342 1.1 christos done 343 1.1 christos fi 344 1.1 christos fi 345 1.1 christos 346 1.1 christos exit_with_hooks 0 347 1.1 christos fi 348 1.1 christos 349 1.1 christos if [ x${old_ip6_prefix} != x ] || [ x${new_ip6_prefix} != x ] ; then 350 1.1 christos echo Prefix ${reason} old=${old_ip6_prefix} new=${new_ip6_prefix} 351 1.1 christos 352 1.1 christos exit_with_hooks 0 353 1.1 christos fi 354 1.1 christos 355 1.1 christos if [ ${reason} = BOUND6 ] ; then 356 1.1 christos if [ x${new_ip6_address} = x ] || [ x${new_ip6_prefixlen} = x ] ; then 357 1.1 christos exit_with_hooks 2; 358 1.1 christos fi 359 1.1 christos 360 1.1 christos # Add address to interface, check for DAD if dad_wait_time > 0 361 1.1 christos add_ipv6_addr_with_DAD 362 1.1 christos 363 1.1 christos # Check for nameserver options. 364 1.1 christos make_resolv_conf 365 1.1 christos 366 1.1 christos exit_with_hooks 0 367 1.1 christos fi 368 1.1 christos 369 1.1 christos if [ ${reason} = RENEW6 ] || [ ${reason} = REBIND6 ] ; then 370 1.1 christos # Make sure nothing has moved around on us. 371 1.1 christos 372 1.1 christos # Nameservers/domains/etc. 373 1.1 christos if [ "x${new_dhcp6_name_servers}" != "x${old_dhcp6_name_servers}" ] || 374 1.1 christos [ "x${new_dhcp6_domain_search}" != "x${old_dhcp6_domain_search}" ] ; then 375 1.1 christos make_resolv_conf 376 1.1 christos fi 377 1.1 christos 378 1.1 christos exit_with_hooks 0 379 1.1 christos fi 380 1.1 christos 381 1.1 christos if [ ${reason} = DEPREF6 ] ; then 382 1.1 christos if [ x${new_ip6_prefixlen} = x ] ; then 383 1.1 christos exit_with_hooks 2; 384 1.1 christos fi 385 1.1 christos 386 1.1 christos # XXX: 387 1.1 christos # There doesn't appear to be a way to update an addr to indicate 388 1.1 christos # preference. 389 1.1 christos 390 1.1 christos exit_with_hooks 0 391 1.1 christos fi 392 1.1 christos 393 1.1 christos if [ ${reason} = EXPIRE6 -o ${reason} = RELEASE6 -o ${reason} = STOP6 ] ; then 394 1.1 christos if [ x${old_ip6_address} = x ] || [ x${old_ip6_prefixlen} = x ] ; then 395 1.1 christos exit_with_hooks 2; 396 1.1 christos fi 397 1.1 christos 398 1.1 christos ifconfig ${interface} inet6 -alias ${old_ip6_address}/${old_ip6_prefixlen} 399 1.1 christos 400 1.1 christos exit_with_hooks 0 401 1.1 christos fi 402 1.1 christos 403 1.1 christos exit_with_hooks 0 404