3 # dhclient-script: Network interface configuration script run by
4 # dhclient based on DHCP client communication
6 # Copyright (C) 2008 Red Hat, Inc.
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.
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.
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/>.
21 # Author(s): David Cantrell <dcantrell@redhat.com>
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
32 PATH
=/bin
:/usr
/bin
:/sbin
33 SAVEDIR
=/var
/lib
/dhclient
40 logger
-p ${LOGFACILITY}.${LOGLEVEL} -t "NET" "dhclient: ${msg}"
45 savefile="${SAVEDIR}/${origfile##*/}.predhclient.${interface}"
47 if [ ! -d ${SAVEDIR} ]; then
51 if [ -e ${origfile} ]; then
52 mv ${origfile} ${savefile}
59 [ "${PEERDNS}" = "no
" ] && return
61 if [ "${reason}" = "RENEW
" ] &&
62 [ "${new_domain_name}" = "${old_domain_name}" ] &&
63 [ "${new_domain_name_servers}" = "${old_domain_name_servers}" ]; then
67 if [ -n "${new_domain_name}" ] ||
68 [ -n "${new_domain_name_servers}" ] ||
69 [ -n "${new_domain_search}" ]; then
70 save_previous /etc/resolv.conf
71 rscf="$
(mktemp
/tmp
/XXXXXX
)"
72 echo "; generated by
/sbin
/dhclient-script
" > ${rscf}
74 if [ -n "${SEARCH}" ]; then
75 echo "search
${SEARCH}" >> $rscf
77 if [ -n "${new_domain_search}" ]; then
78 echo "search
${new_domain_search//\\032/ }" >> ${rscf}
79 elif [ -n "${new_domain_name}" ]; then
80 echo "search
${new_domain_name//\\032/ }" >> ${rscf}
84 if [ -n "${RES_OPTIONS}" ]; then
85 echo "options
${RES_OPTIONS}" >> ${rscf}
88 for nameserver in ${new_domain_name_servers} ; do
89 echo "nameserver
${nameserver}" >> ${rscf}
92 change_resolv_conf ${rscf}
100 if [ -x /etc/dhclient-exit-hooks ]; then
101 . /etc/dhclient-exit-hooks
108 if [ $# -eq 4 ]; then
109 let n="${1} << 24 | ${2} << 16 | ${3} << 8 | ${4}"
119 IFS=".
" quad2num ${1}
124 let o1="(n
>> 24) & 0xff"
125 let o2="(n
>> 16) & 0xff"
126 let o3="(n
>> 8) & 0xff"
128 echo "${o1}.${o2}.${o3}.${o4}"
134 let ip="$
(IFS
="." ip2num
${ip})"
135 let m="$
(IFS
="." ip2num
${m})"
141 let ip=$(IFS='.' ip2num $1)
144 for ((i=0; i <= 3; i++, 'mask<<=8')); do
146 if [ "$v" -eq 0 ] ; then
155 is_router_reachable() {
156 # handle DHCP servers that give us a router not on our subnet
158 routersubnet="$
(mask
${router} ${new_subnet_mask})"
159 mysubnet="$
(mask
${new_ip_address} ${new_subnet_mask})"
162 if [ ! "${routersubnet}" = "${mysubnet}" ]; then
164 if arping -f -q -I ${interface} -w2 ${router}; then
165 ip route add ${router}/32 dev ${interface}
166 if [ $? -eq 0 ]; then
169 logmessage "failed to create
host router
for unreachable router
${router} not on subnet
${mysubnet}"
173 logmessage "DHCP router
${router} is unreachable on DHCP subnet ${mysubnet} router subnet ${routersubnet}"
177 return ${unreachable}
180 add_default_gateway() {
184 if [ $# -gt 1 ] && [ ${2} -gt 0 ]; then
188 if is_router_reachable ${router} ; then
189 ip route replace default via ${router} dev ${interface} ${metric}
190 if [ $? -ne 0 ]; then
191 logmessage "failed to create default route
: ${router} dev ${interface} ${metric}"
202 if [ -n "${old_ip_address}" ] && [ -n "${alias_ip_address}" ] &&
203 [ ! "${alias_ip_address}" = "${old_ip_address}" ]; then
204 # possible new alias, remove old alias first
205 ip -family inet addr del ${old_ip_address} dev ${interface}:0
208 if [ -n "${old_ip_address}" ] &&
209 [ ! "${old_ip_address}" = "${new_ip_address}" ]; then
210 # IP address changed. Bringing down the interface will delete all
211 # routes, and clear the ARP cache.
212 ip -family inet addr flush dev ${interface} >/dev/null 2>&1
213 ip -family inet link set dev ${interface} down
216 if [ "${reason}" = "BOUND
" ] || [ "${reason}" = "REBOOT
" ] ||
217 [ ! "${old_ip_address}" = "${new_ip_address}" ] ||
218 [ ! "${old_subnet_mask}" = "${new_subnet_mask}" ] ||
219 [ ! "${old_network_number}" = "${new_network_number}" ] ||
220 [ ! "${old_broadcast_address}" = "${new_broadcast_address}" ] ||
221 [ ! "${old_routers}" = "${new_routers}" ] ||
222 [ ! "${old_interface_mtu}" = "${new_interface_mtu}" ]; then
223 ip -family inet addr add ${new_ip_address}/${new_prefix} broadcast ${new_broadcast_address} dev ${interface}
225 if [ -n "${new_interface_mtu}" ]; then
226 ip link set ${interface} mtu ${new_interface_mtu}
229 if [ -x /etc/dhclient-${interface}-up-hooks ]; then
230 . /etc/dhclient-${interface}-up-hooks
231 elif [ -x /etc/dhclient-up-hooks ]; then
232 . /etc/dhclient-up-hooks
235 if [[ (( -z "${GATEWAYDEV}" ) ||
236 ( "${GATEWAYDEV}" = "${interface}" )) &&
237 (( -z "$GATEWAY" ) ||
238 (( -n "$DHCLIENT_IGNORE_GATEWAY" ) &&
239 ( "$DHCLIENT_IGNORE_GATEWAY" = [Yy]* ))) ]]; then
244 for router in ${new_routers} ; do
247 for r in ${default_routers[@]} ; do
248 if [ "${r}" = "${router}" ]; then
253 if [ -z "${router}" ] ||
254 [ "${added_router}" = "1" ] ||
255 [ $(IFS=. ip2num ${router}) -le 0 ] ||
256 [[ ( "${router}" = "${new_broadcast_address}" ) &&
257 ( "${new_subnet_mask}" != "255.255.255.255" ) ]]; then
261 default_routers=(${default_routers[@]} ${router})
262 add_default_gateway ${router} ${metric}
266 elif [[ (( -z "${GATEWAYDEV}" ) ||
267 ( "${GATEWAYDEV}" = "${interface}" )) &&
268 ( -n "${GATEWAY}" ) ]]; then
269 routersubnet=$(mask ${GATEWAY} ${new_subnet_mask})
270 mysubnet=$(mask ${new_ip_address} ${new_subnet_mask})
272 if [ "${routersubnet}" = "${mysubnet}" ]; then
273 ip route replace default via ${GATEWAY} dev ${interface}
278 if [ -n "${new_static_routes}" ]; then
279 IFS=', |' static_routes=(${new_static_routes})
282 for((i=0; i<${#static_routes[@]}; i+=2)); do
283 target=${static_routes[$i]}
284 gateway=${static_routes[$i+1]}
287 for t in ${route_targets[@]}; do
288 if [ ${t} = ${target} ]; then
289 if [ -z "${metric}" ]; then
297 if [ -n "${metric}" ]; then
298 metric="metric
${metric}"
301 if is_router_reachable ${gateway}; then
302 ip route replace ${target}/$(class_bits ${target}) via ${gateway} dev ${interface} ${metric}
304 if [ $? -ne 0 ]; then
305 logmessage "failed to create static route
: ${target}/$(class_bits ${target}) via ${gateway} dev ${interface} ${metric}"
307 route_targets=(${route_targets[@]} ${target})
314 if [ ! "${new_ip_address}" = "${alias_ip_address}" ] &&
315 [ -n "${alias_ip_address}" ]; then
316 ip -family inet addr flush dev ${interface}:0 >/dev/null 2>&1
317 ip -family inet addr add ${alias_ip_address}/${alias_prefix} dev ${interface}:0
318 ip route replace ${alias_ip_address}/32 dev ${interface}:0
323 if [ -n "${new_host_name}" ] && need_hostname; then
324 hostname ${new_host_name}
327 if [ ! "${PEERNIS}" = "no
" ]; then
328 if [ -n "${new_nis_domain}" ]; then
329 domainname "${new_nis_domain}"
330 save_previous /etc/yp.conf
332 echo '# generated by /sbin/dhclient-script' > /etc/yp.conf
334 if [ -n "${new_nis_servers}" ]; then
335 for i in ${new_nis_servers} ; do
336 echo "domain
${new_nis_domain} server
${i}" >> /etc/yp.conf
337 let contents=contents+1
340 echo "domain
${new_nis_domain} broadcast
" >> /etc/yp.conf
341 let contents=contents+1
344 if [ ${contents} -gt 0 ]; then
345 if [ -x /etc/rc.d/init.d/ypbind ] &&
346 [ -r /var/run/ypbind.pid ]; then
347 service ypbind restart >/dev/null 2>&1
350 elif [ -n "${new_nis_servers}" ]; then
351 save_previous /etc/yp.conf
352 echo '# generated by /sbin/dhclient-script' > /etc/yp.conf
355 for i in ${new_nis_servers} ; do
356 echo "ypserver
${i}" >> /etc/yp.conf
357 let contents=contents+1
360 if [ $contents -gt 0 ]; then
361 if [ -x /etc/rc.d/init.d/ypbind ] &&
362 [ -r /var/run/ypbind.pid ]; then
363 service ypbind restart >/dev/null 2>&1
369 if [ -n "${DHCP_TIME_OFFSET_SETS_TIMEZONE}" ] &&
370 [[ "${DHCP_TIME_OFFSET_SETS_TIMEZONE}" = [yY1]* ]]; then
371 if [ -n "${new_time_offset}" ]; then
372 # DHCP option "time-offset
" is requested by default and should be
373 # handled. The geographical zone abbreviation cannot be determined
374 # from the GMT offset, but the $ZONEINFO/Etc/GMT$offset file can be
375 # used - note: this disables DST.
376 ((z=new_time_offset/3600))
377 ((hoursWest=$(printf '%+d' $z)))
379 if (( $hoursWest < 0 )); then
380 # tzdata treats negative 'hours west' as positive 'gmtoff'!
384 tzfile=/usr/share/zoneinfo/Etc/GMT$(printf '%+d' ${hoursWest})
385 if [ -e ${tzfile} ]; then
386 save_previous /etc/localtime
387 cp -fp ${tzfile} /etc/localtime
393 if [ ! "${PEERNTP}" = "no
" ] &&
394 [ -n "${new_ntp_servers}" ] && [ -e /etc/ntp.conf ]; then
395 save_previous /etc/ntp.conf
396 egrep -v '^server .* # added by /sbin/dhclient-script$'< ${SAVEDIR}/ntp.conf.predhclient.${interface} > /etc/ntp.conf
398 for s in ${new_ntp_servers} ; do
399 echo "server
${s} # added by /sbin/dhclient-script" >> /etc/ntp.conf
402 diff -q /etc
/ntp.conf
${SAVEDIR}/ntp.conf.predhclient.
${interface} >/dev
/null
2>&1
403 if [ $?
-eq 1 ]; then
404 service ntpd condrestart
>/dev
/null
2>&1
413 if [ -n "${ip}" -a -n "${nm}" ]; then
414 ipcalc
-s -p ${ip} ${nm} | cut
-d '=' -f 2
423 if [ -x /etc
/dhclient-enter-hooks
]; then
426 # dhclient-enter-hooks can abort dhclient-script by setting
427 # the exit_status variable to a non-zero value
428 .
/etc
/dhclient-enter-hooks
429 if [ ${exit_status} -ne 0 ]; then
434 if [ ! -r /etc
/sysconfig
/network-scripts
/network-functions
]; then
435 echo "Missing /etc/sysconfig/network-scripts/network-functions, exiting." >&2
439 if [ ! -r /etc
/rc.d
/init.d
/functions
]; then
440 echo "Missing /etc/rc.d/init.d/functions, exiting." >&2
444 .
/etc
/sysconfig
/network-scripts
/network-functions
445 .
/etc
/rc.d
/init.d
/functions
447 if [ -f /etc
/sysconfig
/network
]; then
448 .
/etc
/sysconfig
/network
451 if [ -f /etc
/sysconfig
/networking
/network
]; then
452 .
/etc
/sysconfig
/networking
/network
455 CONFIG
="/etc/sysconfig/network-scripts/ifcfg-${interface}"
456 need_config
${CONFIG}
457 source_config
>/dev
/null
2>&1
459 new_prefix
="$(get_prefix ${new_ip_address} ${new_subnet_mask})"
460 old_prefix
="$(get_prefix ${old_ip_address} ${new_subnet_mask})"
461 alias_prefix
="$(get_prefix ${alias_ip_address} ${alias_subnet_mask})"
465 # Linux doesn't handle mediums (media)
470 if [ -n "${alias_ip_address}" ]; then
471 # Bring down alias interface, its routes will disappear too.
472 ip
-family inet link
set ${interface}:0 down
475 if [ "${keep_old_ip}" = "yes" ]; then
476 ip
-family inet link
set ${interface} up
478 ip
-family inet addr flush dev
${interface} >/dev
/null
2>&1
479 ip
-family inet link
set ${interface} up
482 if [ -n "${DHCLIENT_DELAY}" ] && [ ${DHCLIENT_DELAY} -gt 0 ]; then
483 sleep ${DHCLIENT_DELAY}
490 if [ -z "${new_ip_address}" ] ||
[ -z "${interface}" ] ||
491 arping
-q -f -c 2 -w 3 -D -I ${interface} ${new_ip_address}; then
498 BOUND|RENEW|REBIND|REBOOT
)
503 EXPIRE|FAIL|RELEASE|STOP
)
504 # only restore config files if there are no other dhclient processes
506 any_other_clients
="$(ps -eo pid,ppid,comm | grep dhclient | grep -v ${PPID})"
507 if [ -n "${any_other_clients}" ]; then
508 if [ -f ${SAVEDIR}/resolv.conf.predhclient.
${interface} ]; then
509 change_resolv_conf
${SAVEDIR}/resolv.conf.predhclient.
${interface}
510 rm -f ${SAVEDIR}/resolv.conf.predhclient.
${interface}
513 if [ -n "${DHCP_TIME_OFFSET_SETS_TIMEZONE}" ] &&
514 [[ "${DHCP_TIME_OFFSET_SETS_TIMEZONE}" = [yY1
]* ]]; then
515 if [ -e ${SAVEDIR}/localtime.predhclient.
${interface} ]; then
517 mv ${SAVEDIR}/localtime.predhclient.
${interface} /etc
/localtime
522 if [ -f ${SAVEDIR}/ntp.conf.predhclient.
${interface} ]; then
524 mv -f ${SAVEDIR}/ntp.conf.predhclient.
${interface} /etc
/ntp.conf
525 service ntpd condrestart
>/dev
/null
2>&1
528 if [ -f ${SAVEDIR}/yp.conf.predhclient.
${interface} ]; then
530 mv -f ${SAVEDIR}/yp.conf.predhclient.
${interface} /etc
/yp.conf
531 if [ -x /etc
/rc.d
/init.d
/ypbind
] && [ -r /var
/run
/ypbind.pid
]; then
532 service ypbind restart
>/dev
/null
2>&1
537 if [ -x /etc
/dhclient-
${interface}-down-hooks ]; then
538 .
/etc
/dhclient-
${interface}-down-hooks
539 elif [ -x /etc
/dhclient-down-hooks
]; then
540 .
/etc
/dhclient-down-hooks
543 if [ -n "${alias_ip_address}" ]; then
544 # Turn off alias interface
545 ip
-family inet link
set ${interface}:0 down
548 if [ -n "${old_ip_address}" ]; then
549 # Shut down interface, which will delete routes and clear arp cache.
550 ip
-family inet addr flush dev
${interface} >/dev
/null
2>&1
551 ip
-family inet link
set ${interface} down
554 if [ -n "${alias_ip_address}" ]; then
555 ip
-family inet addr add
${alias_ip_address}/${alias_prefix} dev ${interface}:0
556 ip
-family inet route replace
${alias_ip_address}/32 ${interface}:0
563 if [ -n "${new_routers}" ]; then
564 if [ -n "${alias_ip_address}" ]; then
565 ip
-family inet addr flush dev
${interface}:0 >/dev
/null
2>&1
568 ip
-family inet addr add
${new_ip_address}/${new_prefix} broadcast ${new_broadcast_address} dev ${interface}
571 if ping -q -c 1 -w 10 -I ${interface} ${1}; then
576 ip
-family inet addr flush dev
${interface} >/dev
/null
2>&1
577 ip
-family inet link
set ${interface} down
585 logmessage
"unhandled state: ${reason}"