| 1 |
#!/bin/bash
|
| 2 |
#
|
| 3 |
# dhclient-script: Network interface configuration script run by
|
| 4 |
# dhclient based on DHCP client communication
|
| 5 |
#
|
| 6 |
# Copyright (C) 2008, 2009 Red Hat, Inc.
|
| 7 |
#
|
| 8 |
# This program is free software; you can redistribute it and/or modify
|
| 9 |
# it under the terms of the GNU General Public License as published by
|
| 10 |
# the Free Software Foundation; either version 2 of the License, or
|
| 11 |
# (at your option) any later version.
|
| 12 |
#
|
| 13 |
# This program is distributed in the hope that it will be useful,
|
| 14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 16 |
# GNU General Public License for more details.
|
| 17 |
#
|
| 18 |
# You should have received a copy of the GNU General Public License
|
| 19 |
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
| 20 |
#
|
| 21 |
# Author(s): David Cantrell <dcantrell@redhat.com>
|
| 22 |
#
|
| 23 |
# ----------
|
| 24 |
# This script is a rewrite/reworking on dhclient-script originally
|
| 25 |
# included as part of dhcp-970306:
|
| 26 |
# dhclient-script for Linux. Dan Halbert, March, 1997.
|
| 27 |
# Updated for Linux 2.[12] by Brian J. Murrell, January 1999.
|
| 28 |
# Modified by David Cantrell <dcantrell@redhat.com> for Fedora and RHEL
|
| 29 |
# ----------
|
| 30 |
#
|
| 31 |
|
| 32 |
PATH=/bin:/usr/bin:/sbin
|
| 33 |
SAVEDIR=/var/lib/dhclient
|
| 34 |
|
| 35 |
LOGFACILITY="local7"
|
| 36 |
LOGLEVEL="notice"
|
| 37 |
|
| 38 |
ETCDIR="/etc/dhcp"
|
| 39 |
|
| 40 |
logmessage() {
|
| 41 |
msg="${1}"
|
| 42 |
logger -p ${LOGFACILITY}.${LOGLEVEL} -t "NET" "dhclient: ${msg}"
|
| 43 |
}
|
| 44 |
|
| 45 |
fix_context() {
|
| 46 |
if [ -x /sbin/restorecon ]; then
|
| 47 |
/sbin/restorecon ${1} >/dev/null 2>&1
|
| 48 |
fi
|
| 49 |
}
|
| 50 |
|
| 51 |
save_previous() {
|
| 52 |
origfile="${1}"
|
| 53 |
savefile="${SAVEDIR}/${origfile##*/}.predhclient.${interface}"
|
| 54 |
|
| 55 |
if [ ! -d ${SAVEDIR} ]; then
|
| 56 |
mkdir -p ${SAVEDIR}
|
| 57 |
fi
|
| 58 |
|
| 59 |
if [ -e ${origfile} ]; then
|
| 60 |
contents="$(< ${origfile})"
|
| 61 |
echo "${contents}" > ${savefile}
|
| 62 |
rm -f ${origfile}
|
| 63 |
else
|
| 64 |
echo > ${savefile}
|
| 65 |
fi
|
| 66 |
|
| 67 |
fix_context ${savefile}
|
| 68 |
}
|
| 69 |
|
| 70 |
make_resolv_conf() {
|
| 71 |
[ "${PEERDNS}" = "no" ] && return
|
| 72 |
|
| 73 |
if [ "${reason}" = "RENEW" ] &&
|
| 74 |
[ "${new_domain_name}" = "${old_domain_name}" ] &&
|
| 75 |
[ "${new_domain_name_servers}" = "${old_domain_name_servers}" ]; then
|
| 76 |
return
|
| 77 |
fi
|
| 78 |
|
| 79 |
if [ -n "${new_domain_name}" ] ||
|
| 80 |
[ -n "${new_domain_name_servers}" ] ||
|
| 81 |
[ -n "${new_domain_search}" ]; then
|
| 82 |
save_previous /etc/resolv.conf
|
| 83 |
rscf="$(mktemp /tmp/XXXXXX)"
|
| 84 |
echo "; generated by /sbin/dhclient-script" > ${rscf}
|
| 85 |
|
| 86 |
if [ -n "${SEARCH}" ]; then
|
| 87 |
echo "search ${SEARCH}" >> $rscf
|
| 88 |
else
|
| 89 |
if [ -n "${new_domain_search}" ]; then
|
| 90 |
echo "search ${new_domain_search//\\032/ }" >> ${rscf}
|
| 91 |
elif [ -n "${new_domain_name}" ]; then
|
| 92 |
echo "search ${new_domain_name//\\032/ }" >> ${rscf}
|
| 93 |
fi
|
| 94 |
fi
|
| 95 |
|
| 96 |
if [ -n "${RES_OPTIONS}" ]; then
|
| 97 |
echo "options ${RES_OPTIONS}" >> ${rscf}
|
| 98 |
fi
|
| 99 |
|
| 100 |
for nameserver in ${new_domain_name_servers} ; do
|
| 101 |
echo "nameserver ${nameserver}" >> ${rscf}
|
| 102 |
done
|
| 103 |
|
| 104 |
change_resolv_conf ${rscf}
|
| 105 |
rm -f ${rscf}
|
| 106 |
|
| 107 |
fix_context /etc/resolv.conf
|
| 108 |
elif [ -n "${new_dhcp6_name_servers}" ] ||
|
| 109 |
[ -n "${new_dhcp6_domain_search}" ]; then
|
| 110 |
save_previous /etc/resolv.conf
|
| 111 |
rscf="$(mktemp /tmp/XXXXXX)"
|
| 112 |
echo "; generated by /sbin/dhclient-script" > ${rscf}
|
| 113 |
|
| 114 |
if [ -n "${SEARCH}" ]; then
|
| 115 |
echo "search ${SEARCH}" >> $rscf
|
| 116 |
else
|
| 117 |
if [ -n "${new_dhcp6_domain_search}" ]; then
|
| 118 |
echo "search ${new_dhcp6_domain_search//\\032/ }" >> ${rscf}
|
| 119 |
fi
|
| 120 |
fi
|
| 121 |
|
| 122 |
if [ -n "${RES_OPTIONS}" ]; then
|
| 123 |
echo "options ${RES_OPTIONS}" >> ${rscf}
|
| 124 |
fi
|
| 125 |
|
| 126 |
for nameserver in ${new_dhcp6_name_servers} ; do
|
| 127 |
echo "nameserver ${nameserver}" >> ${rscf}
|
| 128 |
done
|
| 129 |
|
| 130 |
change_resolv_conf ${rscf}
|
| 131 |
rm -f ${rscf}
|
| 132 |
|
| 133 |
fix_context /etc/resolv.conf
|
| 134 |
fi
|
| 135 |
}
|
| 136 |
|
| 137 |
exit_with_hooks() {
|
| 138 |
exit_status="${1}"
|
| 139 |
|
| 140 |
if [ -x ${ETCDIR}/dhclient-exit-hooks ]; then
|
| 141 |
. ${ETCDIR}/dhclient-exit-hooks
|
| 142 |
fi
|
| 143 |
|
| 144 |
exit ${exit_status}
|
| 145 |
}
|
| 146 |
|
| 147 |
quad2num() {
|
| 148 |
if [ $# -eq 4 ]; then
|
| 149 |
let n="${1} << 24 | ${2} << 16 | ${3} << 8 | ${4}"
|
| 150 |
echo "${n}"
|
| 151 |
return 0
|
| 152 |
else
|
| 153 |
echo "0"
|
| 154 |
return 1
|
| 155 |
fi
|
| 156 |
}
|
| 157 |
|
| 158 |
ip2num() {
|
| 159 |
IFS="." quad2num ${1}
|
| 160 |
}
|
| 161 |
|
| 162 |
num2ip() {
|
| 163 |
let n="${1}"
|
| 164 |
let o1="(n >> 24) & 0xff"
|
| 165 |
let o2="(n >> 16) & 0xff"
|
| 166 |
let o3="(n >> 8) & 0xff"
|
| 167 |
let o4="n & 0xff"
|
| 168 |
echo "${o1}.${o2}.${o3}.${o4}"
|
| 169 |
}
|
| 170 |
|
| 171 |
mask() {
|
| 172 |
ip="${1}"
|
| 173 |
m="${2}"
|
| 174 |
let ip="$(IFS="." ip2num ${ip})"
|
| 175 |
let m="$(IFS="." ip2num ${m})"
|
| 176 |
let n="ip & m"
|
| 177 |
num2ip ${n}
|
| 178 |
}
|
| 179 |
|
| 180 |
class_bits() {
|
| 181 |
let ip=$(IFS='.' ip2num $1)
|
| 182 |
let bits=32
|
| 183 |
let mask='255'
|
| 184 |
for ((i=0; i <= 3; i++, 'mask<<=8')); do
|
| 185 |
let v='ip&mask'
|
| 186 |
if [ "$v" -eq 0 ] ; then
|
| 187 |
let bits-=8
|
| 188 |
else
|
| 189 |
break
|
| 190 |
fi
|
| 191 |
done
|
| 192 |
echo $bits
|
| 193 |
}
|
| 194 |
|
| 195 |
is_router_reachable() {
|
| 196 |
# handle DHCP servers that give us a router not on our subnet
|
| 197 |
router="${1}"
|
| 198 |
routersubnet="$(mask ${router} ${new_subnet_mask})"
|
| 199 |
mysubnet="$(mask ${new_ip_address} ${new_subnet_mask})"
|
| 200 |
unreachable=0
|
| 201 |
|
| 202 |
if [ ! "${routersubnet}" = "${mysubnet}" ]; then
|
| 203 |
unreachable=1
|
| 204 |
if arping -f -q -I ${interface} -w2 ${router}; then
|
| 205 |
ip route add ${router}/32 dev ${interface}
|
| 206 |
if [ $? -eq 0 ]; then
|
| 207 |
unreachable=0
|
| 208 |
else
|
| 209 |
logmessage "failed to create host router for unreachable router ${router} not on subnet ${mysubnet}"
|
| 210 |
fi
|
| 211 |
else
|
| 212 |
unreachable=1
|
| 213 |
logmessage "DHCP router ${router} is unreachable on DHCP subnet ${mysubnet} router subnet ${routersubnet}"
|
| 214 |
fi
|
| 215 |
fi
|
| 216 |
|
| 217 |
return ${unreachable}
|
| 218 |
}
|
| 219 |
|
| 220 |
add_default_gateway() {
|
| 221 |
router="${1}"
|
| 222 |
metric=""
|
| 223 |
|
| 224 |
if [ $# -gt 1 ] && [ ${2} -gt 0 ]; then
|
| 225 |
metric="metric ${2}"
|
| 226 |
fi
|
| 227 |
|
| 228 |
if is_router_reachable ${router} ; then
|
| 229 |
ip route replace default via ${router} dev ${interface} ${metric}
|
| 230 |
if [ $? -ne 0 ]; then
|
| 231 |
logmessage "failed to create default route: ${router} dev ${interface} ${metric}"
|
| 232 |
return 1
|
| 233 |
else
|
| 234 |
return 0
|
| 235 |
fi
|
| 236 |
fi
|
| 237 |
|
| 238 |
return 1
|
| 239 |
}
|
| 240 |
|
| 241 |
dhconfig() {
|
| 242 |
if [ -n "${old_ip_address}" ] && [ -n "${alias_ip_address}" ] &&
|
| 243 |
[ ! "${alias_ip_address}" = "${old_ip_address}" ]; then
|
| 244 |
# possible new alias, remove old alias first
|
| 245 |
ip -family inet addr del ${old_ip_address} dev ${interface}:0
|
| 246 |
fi
|
| 247 |
|
| 248 |
if [ -n "${old_ip_address}" ] &&
|
| 249 |
[ ! "${old_ip_address}" = "${new_ip_address}" ]; then
|
| 250 |
# IP address changed. Bringing down the interface will delete all
|
| 251 |
# routes, and clear the ARP cache.
|
| 252 |
ip -family inet addr flush dev ${interface} >/dev/null 2>&1
|
| 253 |
fi
|
| 254 |
|
| 255 |
if [ "${reason}" = "BOUND" ] || [ "${reason}" = "REBOOT" ] ||
|
| 256 |
[ ! "${old_ip_address}" = "${new_ip_address}" ] ||
|
| 257 |
[ ! "${old_subnet_mask}" = "${new_subnet_mask}" ] ||
|
| 258 |
[ ! "${old_network_number}" = "${new_network_number}" ] ||
|
| 259 |
[ ! "${old_broadcast_address}" = "${new_broadcast_address}" ] ||
|
| 260 |
[ ! "${old_routers}" = "${new_routers}" ] ||
|
| 261 |
[ ! "${old_interface_mtu}" = "${new_interface_mtu}" ]; then
|
| 262 |
ip -family inet addr add ${new_ip_address}/${new_prefix} broadcast ${new_broadcast_address} dev ${interface}
|
| 263 |
ip -family inet link set dev ${interface} up
|
| 264 |
|
| 265 |
if [ -n "${new_interface_mtu}" ]; then
|
| 266 |
ip link set ${interface} mtu ${new_interface_mtu}
|
| 267 |
fi
|
| 268 |
|
| 269 |
if [ -x ${ETCDIR}/dhclient-${interface}-up-hooks ]; then
|
| 270 |
. ${ETCDIR}/dhclient-${interface}-up-hooks
|
| 271 |
elif [ -x ${ETCDIR}/dhclient-up-hooks ]; then
|
| 272 |
. ${ETCDIR}/dhclient-up-hooks
|
| 273 |
fi
|
| 274 |
|
| 275 |
if [[ (( -z "${GATEWAYDEV}" ) ||
|
| 276 |
( "${GATEWAYDEV}" = "${interface}" )) &&
|
| 277 |
(( -z "$GATEWAY" ) ||
|
| 278 |
(( -n "$DHCLIENT_IGNORE_GATEWAY" ) &&
|
| 279 |
( "$DHCLIENT_IGNORE_GATEWAY" = [Yy]* ))) ]]; then
|
| 280 |
metric="${METRIC:-}"
|
| 281 |
let i="${METRIC:-0}"
|
| 282 |
default_routers=()
|
| 283 |
|
| 284 |
for router in ${new_routers} ; do
|
| 285 |
added_router=-
|
| 286 |
|
| 287 |
for r in ${default_routers[@]} ; do
|
| 288 |
if [ "${r}" = "${router}" ]; then
|
| 289 |
added_router=1
|
| 290 |
fi
|
| 291 |
done
|
| 292 |
|
| 293 |
if [ -z "${router}" ] ||
|
| 294 |
[ "${added_router}" = "1" ] ||
|
| 295 |
[ $(IFS=. ip2num ${router}) -le 0 ] ||
|
| 296 |
[[ ( "${router}" = "${new_broadcast_address}" ) &&
|
| 297 |
( "${new_subnet_mask}" != "255.255.255.255" ) ]]; then
|
| 298 |
continue
|
| 299 |
fi
|
| 300 |
|
| 301 |
default_routers=(${default_routers[@]} ${router})
|
| 302 |
add_default_gateway ${router} ${metric}
|
| 303 |
let i=i+1
|
| 304 |
metric=${i}
|
| 305 |
done
|
| 306 |
elif [[ (( -z "${GATEWAYDEV}" ) ||
|
| 307 |
( "${GATEWAYDEV}" = "${interface}" )) &&
|
| 308 |
( -n "${GATEWAY}" ) ]]; then
|
| 309 |
routersubnet=$(mask ${GATEWAY} ${new_subnet_mask})
|
| 310 |
mysubnet=$(mask ${new_ip_address} ${new_subnet_mask})
|
| 311 |
|
| 312 |
if [ "${routersubnet}" = "${mysubnet}" ]; then
|
| 313 |
ip route replace default via ${GATEWAY} dev ${interface}
|
| 314 |
fi
|
| 315 |
fi
|
| 316 |
|
| 317 |
# static routes
|
| 318 |
if [ -n "${new_static_routes}" ]; then
|
| 319 |
IFS=', |' static_routes=(${new_static_routes})
|
| 320 |
route_targets=()
|
| 321 |
|
| 322 |
for((i=0; i<${#static_routes[@]}; i+=2)); do
|
| 323 |
target=${static_routes[$i]}
|
| 324 |
gateway=${static_routes[$i+1]}
|
| 325 |
metric=''
|
| 326 |
|
| 327 |
for t in ${route_targets[@]}; do
|
| 328 |
if [ ${t} = ${target} ]; then
|
| 329 |
if [ -z "${metric}" ]; then
|
| 330 |
metric=1
|
| 331 |
else
|
| 332 |
((metric=metric+1))
|
| 333 |
fi
|
| 334 |
fi
|
| 335 |
done
|
| 336 |
|
| 337 |
if [ -n "${metric}" ]; then
|
| 338 |
metric="metric ${metric}"
|
| 339 |
fi
|
| 340 |
|
| 341 |
if is_router_reachable ${gateway}; then
|
| 342 |
ip route replace ${target}/$(class_bits ${target}) via ${gateway} dev ${interface} ${metric}
|
| 343 |
|
| 344 |
if [ $? -ne 0 ]; then
|
| 345 |
logmessage "failed to create static route: ${target}/$(class_bits ${target}) via ${gateway} dev ${interface} ${metric}"
|
| 346 |
else
|
| 347 |
route_targets=(${route_targets[@]} ${target})
|
| 348 |
fi
|
| 349 |
fi
|
| 350 |
done
|
| 351 |
fi
|
| 352 |
fi
|
| 353 |
|
| 354 |
if [ ! "${new_ip_address}" = "${alias_ip_address}" ] &&
|
| 355 |
[ -n "${alias_ip_address}" ]; then
|
| 356 |
ip -family inet addr flush dev ${interface}:0 >/dev/null 2>&1
|
| 357 |
ip -family inet addr add ${alias_ip_address}/${alias_prefix} dev ${interface}:0
|
| 358 |
ip route replace ${alias_ip_address}/32 dev ${interface}:0
|
| 359 |
fi
|
| 360 |
|
| 361 |
make_resolv_conf
|
| 362 |
|
| 363 |
if [ -n "${new_host_name}" ] && need_hostname; then
|
| 364 |
hostname ${new_host_name}
|
| 365 |
fi
|
| 366 |
|
| 367 |
if [ -n "${DHCP_TIME_OFFSET_SETS_TIMEZONE}" ] &&
|
| 368 |
[[ "${DHCP_TIME_OFFSET_SETS_TIMEZONE}" = [yY1]* ]]; then
|
| 369 |
if [ -n "${new_time_offset}" ]; then
|
| 370 |
# DHCP option "time-offset" is requested by default and should be
|
| 371 |
# handled. The geographical zone abbreviation cannot be determined
|
| 372 |
# from the GMT offset, but the $ZONEINFO/Etc/GMT$offset file can be
|
| 373 |
# used - note: this disables DST.
|
| 374 |
((z=new_time_offset/3600))
|
| 375 |
((hoursWest=$(printf '%+d' $z)))
|
| 376 |
|
| 377 |
if (( $hoursWest < 0 )); then
|
| 378 |
# tzdata treats negative 'hours west' as positive 'gmtoff'!
|
| 379 |
((hoursWest*=-1))
|
| 380 |
fi
|
| 381 |
|
| 382 |
tzfile=/usr/share/zoneinfo/Etc/GMT$(printf '%+d' ${hoursWest})
|
| 383 |
if [ -e ${tzfile} ]; then
|
| 384 |
save_previous /etc/localtime
|
| 385 |
cp -fp ${tzfile} /etc/localtime
|
| 386 |
touch /etc/localtime
|
| 387 |
fix_context /etc/localtime
|
| 388 |
fi
|
| 389 |
fi
|
| 390 |
fi
|
| 391 |
|
| 392 |
# execute any additional client side configuration scripts we have
|
| 393 |
if [ -d ${ETCDIR}/dhclient.d ]; then
|
| 394 |
for f in ${ETCDIR}/dhclient.d/*.sh ; do
|
| 395 |
if [ -x ${f} ]; then
|
| 396 |
subsystem="${f%.sh}"
|
| 397 |
subsystem="${subsystem##*/}"
|
| 398 |
. ${f}
|
| 399 |
"${subsystem}_config"
|
| 400 |
fi
|
| 401 |
done
|
| 402 |
fi
|
| 403 |
}
|
| 404 |
|
| 405 |
dh6config() {
|
| 406 |
case "${reason}" in
|
| 407 |
BOUND6)
|
| 408 |
if [ -z "${new_ip6_address}" ] &&
|
| 409 |
[ -z "${new_ip6_prefixlen}" ]; then
|
| 410 |
exit_with_hooks 2
|
| 411 |
fi
|
| 412 |
|
| 413 |
ip -f inet6 addr add ${new_ip6_address}/${new_ip6_prefixlen} \
|
| 414 |
dev ${interface} scope global
|
| 415 |
make_resolv_conf
|
| 416 |
;;
|
| 417 |
|
| 418 |
RENEW6|REBIND6)
|
| 419 |
if [ ! "${new_dhcp6_name_servers}" = "${old_dhcp6_name_servers}" ] ||
|
| 420 |
[ ! "${new_dhcp6_domain_search}" = "${old_dhcp6_domain_search}" ]; then
|
| 421 |
make_resolv_conf
|
| 422 |
fi
|
| 423 |
;;
|
| 424 |
|
| 425 |
DEPREF6)
|
| 426 |
if [ -z "${new_ip6_prefixlen}" ]; then
|
| 427 |
exit_with_hooks 2
|
| 428 |
fi
|
| 429 |
|
| 430 |
ip -f inet6 addr change ${new_ip6_address}/${new_ip6_prefixlen} \
|
| 431 |
dev ${interface} scope global preferred_lft 0
|
| 432 |
;;
|
| 433 |
esac
|
| 434 |
|
| 435 |
# execute any additional client side configuration scripts we have
|
| 436 |
if [ -d ${ETCDIR}/dhclient.d ]; then
|
| 437 |
for f in ${ETCDIR}/dhclient.d/*.sh ; do
|
| 438 |
if [ -x ${f} ]; then
|
| 439 |
subsystem="${f%.sh}"
|
| 440 |
subsystem="${subsystem##*/}"
|
| 441 |
. ${f}
|
| 442 |
"${subsystem}_config"
|
| 443 |
fi
|
| 444 |
done
|
| 445 |
fi
|
| 446 |
}
|
| 447 |
|
| 448 |
get_prefix() {
|
| 449 |
ip="${1}"
|
| 450 |
nm="${2}"
|
| 451 |
|
| 452 |
if [ -n "${ip}" -a -n "${nm}" ]; then
|
| 453 |
ipcalc -s -p ${ip} ${nm} | cut -d '=' -f 2
|
| 454 |
fi
|
| 455 |
}
|
| 456 |
|
| 457 |
|
| 458 |
#
|
| 459 |
# ### MAIN
|
| 460 |
#
|
| 461 |
|
| 462 |
if [ -x ${ETCDIR}/dhclient-enter-hooks ]; then
|
| 463 |
exit_status=0
|
| 464 |
|
| 465 |
# dhclient-enter-hooks can abort dhclient-script by setting
|
| 466 |
# the exit_status variable to a non-zero value
|
| 467 |
. ${ETCDIR}/dhclient-enter-hooks
|
| 468 |
if [ ${exit_status} -ne 0 ]; then
|
| 469 |
exit ${exit_status}
|
| 470 |
fi
|
| 471 |
fi
|
| 472 |
|
| 473 |
if [ ! -r /etc/sysconfig/network-scripts/network-functions ]; then
|
| 474 |
echo "Missing /etc/sysconfig/network-scripts/network-functions, exiting." >&2
|
| 475 |
exit 1
|
| 476 |
fi
|
| 477 |
|
| 478 |
if [ ! -r /etc/rc.d/init.d/functions ]; then
|
| 479 |
echo "Missing /etc/rc.d/init.d/functions, exiting." >&2
|
| 480 |
exit 1
|
| 481 |
fi
|
| 482 |
|
| 483 |
. /etc/sysconfig/network-scripts/network-functions
|
| 484 |
. /etc/rc.d/init.d/functions
|
| 485 |
|
| 486 |
if [ -f /etc/sysconfig/network ]; then
|
| 487 |
. /etc/sysconfig/network
|
| 488 |
fi
|
| 489 |
|
| 490 |
if [ -f /etc/sysconfig/networking/network ]; then
|
| 491 |
. /etc/sysconfig/networking/network
|
| 492 |
fi
|
| 493 |
|
| 494 |
cd /etc/sysconfig/network-scripts
|
| 495 |
CONFIG="ifcfg-${interface}"
|
| 496 |
need_config ${CONFIG}
|
| 497 |
source_config >/dev/null 2>&1
|
| 498 |
|
| 499 |
new_prefix="$(get_prefix ${new_ip_address} ${new_subnet_mask})"
|
| 500 |
old_prefix="$(get_prefix ${old_ip_address} ${new_subnet_mask})"
|
| 501 |
alias_prefix="$(get_prefix ${alias_ip_address} ${alias_subnet_mask})"
|
| 502 |
|
| 503 |
case "${reason}" in
|
| 504 |
MEDIUM)
|
| 505 |
# Linux doesn't handle mediums (media)
|
| 506 |
exit_with_hooks 0
|
| 507 |
;;
|
| 508 |
|
| 509 |
PREINIT)
|
| 510 |
if [ -n "${alias_ip_address}" ]; then
|
| 511 |
# Bring down alias interface, its routes will disappear too.
|
| 512 |
ip -family inet link set ${interface}:0 down
|
| 513 |
fi
|
| 514 |
|
| 515 |
if [ "${keep_old_ip}" = "yes" ]; then
|
| 516 |
ip -family inet link set ${interface} up
|
| 517 |
else
|
| 518 |
ip -family inet addr flush dev ${interface} >/dev/null 2>&1
|
| 519 |
ip -family inet link set ${interface} up
|
| 520 |
fi
|
| 521 |
|
| 522 |
if [ -n "${DHCLIENT_DELAY}" ] && [ ${DHCLIENT_DELAY} -gt 0 ]; then
|
| 523 |
sleep ${DHCLIENT_DELAY}
|
| 524 |
fi
|
| 525 |
|
| 526 |
exit_with_hooks 0
|
| 527 |
;;
|
| 528 |
|
| 529 |
PREINIT6)
|
| 530 |
# ensure interface is up
|
| 531 |
ip link set ${interface} up
|
| 532 |
|
| 533 |
# remove any stale addresses from aborted clients
|
| 534 |
ip -f inet6 addr flush dev ${interface} scope global permanent
|
| 535 |
|
| 536 |
exit_with_hooks 0
|
| 537 |
;;
|
| 538 |
|
| 539 |
ARPCHECK|ARPSEND)
|
| 540 |
if [ -z "${new_ip_address}" ] || [ -z "${interface}" ] ||
|
| 541 |
arping -q -f -c 2 -w 3 -D -I ${interface} ${new_ip_address}; then
|
| 542 |
exit_with_hooks 0
|
| 543 |
else
|
| 544 |
exit_with_hooks 1
|
| 545 |
fi
|
| 546 |
;;
|
| 547 |
|
| 548 |
BOUND|RENEW|REBIND|REBOOT)
|
| 549 |
dhconfig
|
| 550 |
exit_with_hooks 0
|
| 551 |
;;
|
| 552 |
|
| 553 |
BOUND6|RENEW6|REBIND6|DEPREF6)
|
| 554 |
dh6config
|
| 555 |
exit_with_hooks 0
|
| 556 |
;;
|
| 557 |
|
| 558 |
EXPIRE6|RELEASE6|STOP6)
|
| 559 |
if [ -n "${old_ip6_address}" ] || [ -n "${old_ip6_prefixlen}" ]; then
|
| 560 |
exit_with_hooks 2
|
| 561 |
fi
|
| 562 |
|
| 563 |
ip -f inet6 addr del ${old_ip6_address}/${old_ip6_prefixlen} \
|
| 564 |
dev ${interface}
|
| 565 |
|
| 566 |
# execute any additional client side configuration scripts we have
|
| 567 |
if [ -d ${ETCDIR}/dhclient.d ]; then
|
| 568 |
for f in ${ETCDIR}/dhclient.d/*.sh ; do
|
| 569 |
if [ -x ${f} ]; then
|
| 570 |
subsystem="${f%.sh}"
|
| 571 |
subsystem="${subsystem##*/}"
|
| 572 |
. ${f}
|
| 573 |
"${subsystem}_restore"
|
| 574 |
fi
|
| 575 |
done
|
| 576 |
fi
|
| 577 |
|
| 578 |
if [ -x ${ETCDIR}/dhclient-${interface}-down-hooks ]; then
|
| 579 |
. ${ETCDIR}/dhclient-${interface}-down-hooks
|
| 580 |
elif [ -x ${ETCDIR}/dhclient-down-hooks ]; then
|
| 581 |
. ${ETCDIR}/dhclient-down-hooks
|
| 582 |
fi
|
| 583 |
|
| 584 |
exit_with_hooks 0
|
| 585 |
;;
|
| 586 |
|
| 587 |
EXPIRE|FAIL|RELEASE|STOP)
|
| 588 |
# only restore config files if there are no other dhclient processes
|
| 589 |
# running (#306381)
|
| 590 |
any_other_clients="$(ps -eo pid,ppid,comm | grep dhclient | grep -v ${PPID})"
|
| 591 |
if [ -n "${any_other_clients}" ]; then
|
| 592 |
if [ -f ${SAVEDIR}/resolv.conf.predhclient.${interface} ]; then
|
| 593 |
change_resolv_conf ${SAVEDIR}/resolv.conf.predhclient.${interface}
|
| 594 |
rm -f ${SAVEDIR}/resolv.conf.predhclient.${interface}
|
| 595 |
fi
|
| 596 |
|
| 597 |
if [ -n "${DHCP_TIME_OFFSET_SETS_TIMEZONE}" ] &&
|
| 598 |
[[ "${DHCP_TIME_OFFSET_SETS_TIMEZONE}" = [yY1]* ]]; then
|
| 599 |
if [ -e ${SAVEDIR}/localtime.predhclient.${interface} ]; then
|
| 600 |
rm -f /etc/localtime
|
| 601 |
contents="$(< ${SAVEDIR}/localtime.predhclient.${interface})"
|
| 602 |
echo "${contents}" > /etc/localtime
|
| 603 |
rm -f ${SAVEDIR}/localtime.predhclient.${interface}
|
| 604 |
touch /etc/localtime
|
| 605 |
fix_context /etc/localtime
|
| 606 |
fi
|
| 607 |
fi
|
| 608 |
fi
|
| 609 |
|
| 610 |
# execute any additional client side configuration scripts we have
|
| 611 |
if [ -d ${ETCDIR}/dhclient.d ]; then
|
| 612 |
for f in ${ETCDIR}/dhclient.d/*.sh ; do
|
| 613 |
if [ -x ${f} ]; then
|
| 614 |
subsystem="${f%.sh}"
|
| 615 |
subsystem="${subsystem##*/}"
|
| 616 |
. ${f}
|
| 617 |
"${subsystem}_restore"
|
| 618 |
fi
|
| 619 |
done
|
| 620 |
fi
|
| 621 |
|
| 622 |
if [ -x ${ETCDIR}/dhclient-${interface}-down-hooks ]; then
|
| 623 |
. ${ETCDIR}/dhclient-${interface}-down-hooks
|
| 624 |
elif [ -x ${ETCDIR}/dhclient-down-hooks ]; then
|
| 625 |
. ${ETCDIR}/dhclient-down-hooks
|
| 626 |
fi
|
| 627 |
|
| 628 |
if [ -n "${alias_ip_address}" ]; then
|
| 629 |
# Turn off alias interface
|
| 630 |
ip -family inet link set ${interface}:0 down
|
| 631 |
fi
|
| 632 |
|
| 633 |
if [ -n "${old_ip_address}" ]; then
|
| 634 |
# Shut down interface, which will delete routes and clear arp cache.
|
| 635 |
ip -family inet addr flush dev ${interface} >/dev/null 2>&1
|
| 636 |
ip -family inet link set ${interface} down
|
| 637 |
fi
|
| 638 |
|
| 639 |
if [ -n "${alias_ip_address}" ]; then
|
| 640 |
ip -family inet addr add ${alias_ip_address}/${alias_prefix} dev ${interface}:0
|
| 641 |
ip -family inet route replace ${alias_ip_address}/32 ${interface}:0
|
| 642 |
fi
|
| 643 |
|
| 644 |
exit_with_hooks 0
|
| 645 |
;;
|
| 646 |
|
| 647 |
TIMEOUT)
|
| 648 |
if [ -n "${new_routers}" ]; then
|
| 649 |
if [ -n "${alias_ip_address}" ]; then
|
| 650 |
ip -family inet addr flush dev ${interface}:0 >/dev/null 2>&1
|
| 651 |
fi
|
| 652 |
|
| 653 |
ip -family inet addr add ${new_ip_address}/${new_prefix} broadcast ${new_broadcast_address} dev ${interface}
|
| 654 |
set ${new_routers}
|
| 655 |
|
| 656 |
if ping -q -c 1 -w 10 -I ${interface} ${1}; then
|
| 657 |
dhconfig
|
| 658 |
exit_with_hooks 0
|
| 659 |
fi
|
| 660 |
|
| 661 |
ip -family inet addr flush dev ${interface} >/dev/null 2>&1
|
| 662 |
ip -family inet link set ${interface} down
|
| 663 |
exit_with_hooks 1
|
| 664 |
else
|
| 665 |
exit_with_hooks 1
|
| 666 |
fi
|
| 667 |
;;
|
| 668 |
|
| 669 |
*)
|
| 670 |
logmessage "unhandled state: ${reason}"
|
| 671 |
exit_with_hooks 1
|
| 672 |
;;
|
| 673 |
esac
|
| 674 |
|
| 675 |
exit_with_hooks 0
|