db449ef
#!/bin/bash
ee7ca6e
#
ee7ca6e
# dhclient-script: Network interface configuration script run by
ee7ca6e
#                  dhclient based on DHCP client communication
ee7ca6e
#
5ec8ece
# Copyright (C) 2008, 2009, 2010  Red Hat, Inc.
ee7ca6e
#
ee7ca6e
# This program is free software; you can redistribute it and/or modify
ee7ca6e
# it under the terms of the GNU General Public License as published by
ee7ca6e
# the Free Software Foundation; either version 2 of the License, or
ee7ca6e
# (at your option) any later version.
ee7ca6e
#
ee7ca6e
# This program is distributed in the hope that it will be useful,
ee7ca6e
# but WITHOUT ANY WARRANTY; without even the implied warranty of
ee7ca6e
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ee7ca6e
# GNU General Public License for more details.
ee7ca6e
#
ee7ca6e
# You should have received a copy of the GNU General Public License
ee7ca6e
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
ee7ca6e
#
ee7ca6e
# Author(s): David Cantrell <dcantrell@redhat.com>
ee7ca6e
#
ee7ca6e
# ----------
ee7ca6e
# This script is a rewrite/reworking on dhclient-script originally
ee7ca6e
# included as part of dhcp-970306:
db449ef
# dhclient-script for Linux. Dan Halbert, March, 1997.
db449ef
# Updated for Linux 2.[12] by Brian J. Murrell, January 1999.
db449ef
# Modified by David Cantrell <dcantrell@redhat.com> for Fedora and RHEL
ee7ca6e
# ----------
ee7ca6e
#
db449ef
ee7ca6e
PATH=/bin:/usr/bin:/sbin
ee7ca6e
SAVEDIR=/var/lib/dhclient
db449ef
ee7ca6e
LOGFACILITY="local7"
ee7ca6e
LOGLEVEL="notice"
db449ef
580990a
ETCDIR="/etc/dhcp"
580990a
ee7ca6e
logmessage() {
ee7ca6e
    msg="${1}"
ee7ca6e
    logger -p ${LOGFACILITY}.${LOGLEVEL} -t "NET" "dhclient: ${msg}"
ee7ca6e
}
db449ef
dbecea2
fix_context() {
dbecea2
    if [ -x /sbin/restorecon ]; then
dbecea2
        /sbin/restorecon ${1} >/dev/null 2>&1
dbecea2
    fi
dbecea2
}
dbecea2
ee7ca6e
save_previous() {
ee7ca6e
    origfile="${1}"
ee7ca6e
    savefile="${SAVEDIR}/${origfile##*/}.predhclient.${interface}"
db449ef
ee7ca6e
    if [ ! -d ${SAVEDIR} ]; then
ee7ca6e
        mkdir -p ${SAVEDIR}
ee7ca6e
    fi
db449ef
ee7ca6e
    if [ -e ${origfile} ]; then
226011d
        contents="$(< ${origfile})"
226011d
        echo "${contents}" > ${savefile}
226011d
        rm -f ${origfile}
db449ef
    else
ee7ca6e
        echo > ${savefile}
db449ef
    fi
6961ce4
dbecea2
    fix_context ${savefile}
db449ef
}
db449ef
db449ef
make_resolv_conf() {
ee7ca6e
    [ "${PEERDNS}" = "no" ] && return
db449ef
ee7ca6e
    if [ "${reason}" = "RENEW" ] &&
ee7ca6e
       [ "${new_domain_name}" = "${old_domain_name}" ] &&
ee7ca6e
       [ "${new_domain_name_servers}" = "${old_domain_name_servers}" ]; then
db449ef
        return
db449ef
    fi
db449ef
ee7ca6e
    if [ -n "${new_domain_name}" ] ||
ee7ca6e
       [ -n "${new_domain_name_servers}" ] ||
ee7ca6e
       [ -n "${new_domain_search}" ]; then
6961ce4
        save_previous /etc/resolv.conf
ee7ca6e
        rscf="$(mktemp /tmp/XXXXXX)"
ee7ca6e
        echo "; generated by /sbin/dhclient-script" > ${rscf}
db449ef
ee7ca6e
        if [ -n "${SEARCH}" ]; then
ee7ca6e
            echo "search ${SEARCH}" >> $rscf
6961ce4
        else
ee7ca6e
            if [ -n "${new_domain_search}" ]; then
ee7ca6e
                echo "search ${new_domain_search//\\032/ }" >> ${rscf}
ee7ca6e
            elif [ -n "${new_domain_name}" ]; then
2d59159
                # Note that the DHCP 'Domain Name Option' is really just a domain
2d59159
                # name, and that this practice of using the domain name option as
2d59159
                # a search path is both nonstandard and deprecated.
ee7ca6e
                echo "search ${new_domain_name//\\032/ }" >> ${rscf}
db449ef
            fi
db449ef
        fi
db449ef
ee7ca6e
        if [ -n "${RES_OPTIONS}" ]; then
ee7ca6e
            echo "options ${RES_OPTIONS}" >> ${rscf}
d016d23
        fi
d016d23
ee7ca6e
        for nameserver in ${new_domain_name_servers} ; do
ee7ca6e
            echo "nameserver ${nameserver}" >> ${rscf}
db449ef
        done
db449ef
ee7ca6e
        change_resolv_conf ${rscf}
ee7ca6e
        rm -f ${rscf}
f1ade68
dbecea2
        fix_context /etc/resolv.conf
f195973
    elif [ -n "${new_dhcp6_name_servers}" ] ||
f195973
         [ -n "${new_dhcp6_domain_search}" ]; then
f195973
        save_previous /etc/resolv.conf
f195973
        rscf="$(mktemp /tmp/XXXXXX)"
f195973
        echo "; generated by /sbin/dhclient-script" > ${rscf}
f195973
f195973
        if [ -n "${SEARCH}" ]; then
f195973
            echo "search ${SEARCH}" >> $rscf
f195973
        else
f195973
            if [ -n "${new_dhcp6_domain_search}" ]; then
f195973
                echo "search ${new_dhcp6_domain_search//\\032/ }" >> ${rscf}
f195973
            fi
f195973
        fi
f195973
f195973
        if [ -n "${RES_OPTIONS}" ]; then
f195973
            echo "options ${RES_OPTIONS}" >> ${rscf}
f195973
        fi
f195973
f195973
        for nameserver in ${new_dhcp6_name_servers} ; do
f195973
            echo "nameserver ${nameserver}" >> ${rscf}
f195973
        done
f195973
f195973
        change_resolv_conf ${rscf}
f195973
        rm -f ${rscf}
f195973
f195973
        fix_context /etc/resolv.conf
db449ef
    fi
db449ef
}
db449ef
db449ef
exit_with_hooks() {
ee7ca6e
    exit_status="${1}"
db449ef
580990a
    if [ -x ${ETCDIR}/dhclient-exit-hooks ]; then
580990a
        . ${ETCDIR}/dhclient-exit-hooks
db449ef
    fi
db449ef
ee7ca6e
    exit ${exit_status}
db449ef
}
db449ef
ee7ca6e
quad2num() {
db449ef
    if [ $# -eq 4 ]; then
ee7ca6e
        let n="${1} << 24 | ${2} << 16 | ${3} << 8 | ${4}"
ee7ca6e
        echo "${n}"
db449ef
        return 0
ee7ca6e
    else
ee7ca6e
        echo "0"
ee7ca6e
        return 1
db449ef
    fi
db449ef
}
db449ef
ee7ca6e
ip2num() {
ee7ca6e
    IFS="." quad2num ${1}
db449ef
}
db449ef
ee7ca6e
num2ip() {
ee7ca6e
    let n="${1}"
ee7ca6e
    let o1="(n >> 24) & 0xff"
ee7ca6e
    let o2="(n >> 16) & 0xff"
ee7ca6e
    let o3="(n >> 8) & 0xff"
ee7ca6e
    let o4="n & 0xff"
ee7ca6e
    echo "${o1}.${o2}.${o3}.${o4}"
db449ef
}
db449ef
a21bc05
get_network_address() {
a21bc05
# get network address for the given IP address and (netmask or prefix)
ee7ca6e
    ip="${1}"
a21bc05
    nm="${2}"
a21bc05
a21bc05
    if [ -n "${ip}" -a -n "${nm}" ]; then
a21bc05
        if [[ "${nm}" = *.* ]]; then
a21bc05
            ipcalc -s -n ${ip} ${nm} | cut -d '=' -f 2
a21bc05
        else
a21bc05
            ipcalc -s -n ${ip}/${nm} | cut -d '=' -f 2
a21bc05
        fi
a21bc05
    fi
a21bc05
}
a21bc05
a21bc05
get_prefix() {
a21bc05
# get prefix for the given IP address and mask
a21bc05
    ip="${1}"
a21bc05
    nm="${2}"
a21bc05
a21bc05
    if [ -n "${ip}" -a -n "${nm}" ]; then
a21bc05
        ipcalc -s -p ${ip} ${nm} | cut -d '=' -f 2
a21bc05
    fi
db449ef
}
db449ef
ee7ca6e
class_bits() {
db449ef
    let ip=$(IFS='.' ip2num $1)
db449ef
    let bits=32
db449ef
    let mask='255'
db449ef
    for ((i=0; i <= 3; i++, 'mask<<=8')); do
db449ef
        let v='ip&mask'
db449ef
        if [ "$v" -eq 0 ] ; then
db449ef
             let bits-=8
db449ef
        else
db449ef
             break
db449ef
        fi
db449ef
    done
db449ef
    echo $bits
db449ef
}
db449ef
ee7ca6e
is_router_reachable() {
ee7ca6e
    # handle DHCP servers that give us a router not on our subnet
ee7ca6e
    router="${1}"
a21bc05
    routersubnet="$(get_network_address ${router} ${new_subnet_mask})"
a21bc05
    mysubnet="$(get_network_address ${new_ip_address} ${new_subnet_mask})"
ee7ca6e
ee7ca6e
    if [ ! "${routersubnet}" = "${mysubnet}" ]; then
b112ec7
        ip -4 route add ${router}/32 dev ${interface}
b112ec7
        if [ $? -eq 0 ]; then
b112ec7
            if ping -q -c1 -w2 -I ${interface} ${router}; then
b112ec7
                return 0
ee7ca6e
            else
b112ec7
                logmessage "DHCP router ${router} is unreachable on DHCP subnet ${mysubnet} router subnet ${routersubnet}"
b112ec7
                ip route del ${router}/32 dev ${interface}
b112ec7
                return 1
ee7ca6e
            fi
db449ef
        else
b112ec7
            logmessage "failed to create host router for unreachable router ${router} not on subnet ${mysubnet}"
b112ec7
            return 1
db449ef
        fi
db449ef
    fi
ee7ca6e
b112ec7
    return 0
db449ef
}
db449ef
ee7ca6e
add_default_gateway() {
ee7ca6e
    router="${1}"
ee7ca6e
    metric=""
ee7ca6e
f7db77f
    if [ $# -gt 1 ] && [ ${2} -gt 0 ]; then
ee7ca6e
        metric="metric ${2}"
db449ef
    fi
ee7ca6e
ee7ca6e
    if is_router_reachable ${router} ; then
5ec8ece
        ip -4 route replace default via ${router} dev ${interface} ${metric}
db449ef
        if [ $? -ne 0 ]; then
ee7ca6e
            logmessage "failed to create default route: ${router} dev ${interface} ${metric}"
db449ef
            return 1
db449ef
        else
db449ef
            return 0
db449ef
        fi
db449ef
    fi
ee7ca6e
db449ef
    return 1
db449ef
}
db449ef
d5a4ff8
flush_dev() {
d5a4ff8
# Instead of bringing the interface down (#574568)
d5a4ff8
# explicitly clear the ARP cache and flush all addresses & routes.
d5a4ff8
    ip -4 addr flush dev ${1} >/dev/null 2>&1
d5a4ff8
    ip -4 route flush dev ${1} >/dev/null 2>&1
d5a4ff8
    ip -4 neigh flush dev ${1} >/dev/null 2>&1
d5a4ff8
}
d5a4ff8
ee7ca6e
dhconfig() {
ee7ca6e
    if [ -n "${old_ip_address}" ] && [ -n "${alias_ip_address}" ] &&
ee7ca6e
       [ ! "${alias_ip_address}" = "${old_ip_address}" ]; then
ee7ca6e
        # possible new alias, remove old alias first
5ec8ece
        ip -4 addr del ${old_ip_address} dev ${interface}:0
db449ef
    fi
db449ef
ee7ca6e
    if [ -n "${old_ip_address}" ] &&
ee7ca6e
       [ ! "${old_ip_address}" = "${new_ip_address}" ]; then
d5a4ff8
        # IP address changed. Delete all routes, and clear the ARP cache.
d5a4ff8
        flush_dev ${interface}
db449ef
    fi
db449ef
f7db77f
    if [ "${reason}" = "BOUND" ] || [ "${reason}" = "REBOOT" ] ||
ee7ca6e
       [ ! "${old_ip_address}" = "${new_ip_address}" ] ||
ee7ca6e
       [ ! "${old_subnet_mask}" = "${new_subnet_mask}" ] ||
ee7ca6e
       [ ! "${old_network_number}" = "${new_network_number}" ] ||
ee7ca6e
       [ ! "${old_broadcast_address}" = "${new_broadcast_address}" ] ||
ee7ca6e
       [ ! "${old_routers}" = "${new_routers}" ] ||
ee7ca6e
       [ ! "${old_interface_mtu}" = "${new_interface_mtu}" ]; then
5ec8ece
        ip -4 addr add ${new_ip_address}/${new_prefix} broadcast ${new_broadcast_address} dev ${interface}
5ec8ece
        ip link set dev ${interface} up
ee7ca6e
c402567
        # The 576 MTU is only used for X.25 and dialup connections
c402567
        # where the admin wants low latency.  Such a low MTU can cause
c402567
        # problems with UDP traffic, among other things.  As such,
c402567
        # disallow MTUs from 576 and below by default, so that broken
c402567
        # MTUs are ignored, but higher stuff is allowed (1492, 1500, etc).
c402567
        if [ -n "${new_interface_mtu}" ] && [ ${new_interface_mtu} -gt 576 ]; then
ee7ca6e
            ip link set ${interface} mtu ${new_interface_mtu}
db449ef
        fi
db449ef
580990a
        if [ -x ${ETCDIR}/dhclient-${interface}-up-hooks ]; then
580990a
            . ${ETCDIR}/dhclient-${interface}-up-hooks
580990a
        elif [ -x ${ETCDIR}/dhclient-up-hooks ]; then
580990a
            . ${ETCDIR}/dhclient-up-hooks
db449ef
        fi
db449ef
a21bc05
        # static routes
a21bc05
        if [ -n "${new_classless_static_routes}" ] ||
a21bc05
           [ -n "${new_static_routes}" ]; then
a21bc05
            if [ -n "${new_classless_static_routes}" ]; then
a21bc05
                IFS=', |' static_routes=(${new_classless_static_routes})
a21bc05
            else
a21bc05
                IFS=', |' static_routes=(${new_static_routes})
a21bc05
            fi
a21bc05
            route_targets=()
a21bc05
a21bc05
            for((i=0; i<${#static_routes[@]}; i+=2)); do
a21bc05
                target=${static_routes[$i]}
a21bc05
                if [ -n "${new_classless_static_routes}" ]; then
2560aa7
                    if [ ${target} = "0" ]; then
2560aa7
                        # If the DHCP server returns both a Classless Static Routes option and
2560aa7
                        # a Router option, the DHCP client MUST ignore the Router option. (RFC3442)
2560aa7
                        new_routers=""
2560aa7
                        prefix="0"
2560aa7
                    else
2560aa7
                        prefix=$(echo ${target} | cut -d "." -f 1)
2560aa7
                        target=$(echo ${target} | cut -d "." -f 2-)
2560aa7
                        IFS="." target_arr=(${target})
2560aa7
                        unset IFS
2560aa7
                        ((pads=4-${#target_arr[@]}))
2560aa7
                        for j in $(seq $pads); do
2560aa7
                            target=${target}".0"
2560aa7
                        done
2560aa7
2560aa7
                        # Client MUST zero any bits in the subnet number where the corresponding bit in the mask is zero.
2560aa7
                        # In other words, the subnet number installed in the routing table is the logical AND of
2560aa7
                        # the subnet number and subnet mask given in the Classless Static Routes option. (RFC3442)
2560aa7
                        target="$(get_network_address ${target} ${prefix})"
2560aa7
                    fi
a21bc05
                else
a21bc05
                    prefix=$(class_bits ${target})
a21bc05
                fi
a21bc05
                gateway=${static_routes[$i+1]}
a21bc05
a21bc05
                metric=''
a21bc05
                for t in ${route_targets[@]}; do
a21bc05
                    if [ ${t} = ${target} ]; then
a21bc05
                        if [ -z "${metric}" ]; then
a21bc05
                            metric=1
a21bc05
                        else
a21bc05
                            ((metric=metric+1))
a21bc05
                        fi
a21bc05
                    fi
a21bc05
                done
a21bc05
a21bc05
                if [ -n "${metric}" ]; then
a21bc05
                    metric="metric ${metric}"
a21bc05
                fi
a21bc05
a21bc05
                if is_router_reachable ${gateway}; then
2560aa7
                    ip -4 route replace ${target}/${prefix} proto static via ${gateway} dev ${interface} ${metric}
a21bc05
a21bc05
                    if [ $? -ne 0 ]; then
a21bc05
                        logmessage "failed to create static route: ${target}/${prefix} via ${gateway} dev ${interface} ${metric}"
a21bc05
                    else
a21bc05
                        route_targets=(${route_targets[@]} ${target})
a21bc05
                    fi
a21bc05
                fi
a21bc05
            done
a21bc05
        fi
a21bc05
a21bc05
        # gateways
9ca442a
        if [[ ( "${DEFROUTE}" != "no") &&
9ca442a
              (( -z "${GATEWAYDEV}" ) ||
9ca442a
               ( "${GATEWAYDEV}" = "${interface}" )) ]]; then
9ca442a
            if [[ ( -z "$GATEWAY" ) ||
9ca442a
                  (( -n "$DHCLIENT_IGNORE_GATEWAY" ) &&
9ca442a
                   ( "$DHCLIENT_IGNORE_GATEWAY" = [Yy]* )) ]]; then
9ca442a
                metric="${METRIC:-}"
9ca442a
                let i="${METRIC:-0}"
9ca442a
                default_routers=()
9ca442a
9ca442a
                for router in ${new_routers} ; do
9ca442a
                    added_router=-
9ca442a
9ca442a
                    for r in ${default_routers[@]} ; do
9ca442a
                        if [ "${r}" = "${router}" ]; then
9ca442a
                            added_router=1
9ca442a
                        fi
9ca442a
                    done
9ca442a
9ca442a
                    if [ -z "${router}" ] ||
9ca442a
                       [ "${added_router}" = "1" ] ||
9ca442a
                       [ $(IFS=. ip2num ${router}) -le 0 ] ||
9ca442a
                       [[ ( "${router}" = "${new_broadcast_address}" ) &&
9ca442a
                          ( "${new_subnet_mask}" != "255.255.255.255" ) ]]; then
9ca442a
                        continue
db449ef
                    fi
9ca442a
9ca442a
                    default_routers=(${default_routers[@]} ${router})
9ca442a
                    add_default_gateway ${router} ${metric}
9ca442a
                    let i=i+1
9ca442a
                    metric=${i}
db449ef
                done
9ca442a
            elif [ -n "${GATEWAY}" ]; then
a21bc05
                routersubnet=$(get_network_address ${GATEWAY} ${new_subnet_mask})
a21bc05
                mysubnet=$(get_network_address ${new_ip_address} ${new_subnet_mask})
db449ef
9ca442a
                if [ "${routersubnet}" = "${mysubnet}" ]; then
5ec8ece
                    ip -4 route replace default via ${GATEWAY} dev ${interface}
db449ef
                fi
db449ef
            fi
db449ef
        fi
db449ef
db449ef
    fi
db449ef
ee7ca6e
    if [ ! "${new_ip_address}" = "${alias_ip_address}" ] &&
ee7ca6e
       [ -n "${alias_ip_address}" ]; then
5ec8ece
        ip -4 addr flush dev ${interface}:0 >/dev/null 2>&1
5ec8ece
        ip -4 addr add ${alias_ip_address}/${alias_prefix} dev ${interface}:0
5ec8ece
        ip -4 route replace ${alias_ip_address}/32 dev ${interface}:0
db449ef
    fi
db449ef
db449ef
    make_resolv_conf
db449ef
ee7ca6e
    if [ -n "${new_host_name}" ] && need_hostname; then
9115422
        hostname ${new_host_name} || echo "See -nc option in dhclient(8) man page."
db449ef
    fi
db449ef
ee7ca6e
    if [ -n "${DHCP_TIME_OFFSET_SETS_TIMEZONE}" ] &&
ee7ca6e
       [[ "${DHCP_TIME_OFFSET_SETS_TIMEZONE}" = [yY1]* ]]; then
ee7ca6e
        if [ -n "${new_time_offset}" ]; then
db449ef
            # DHCP option "time-offset" is requested by default and should be
db449ef
            # handled.  The geographical zone abbreviation cannot be determined
db449ef
            # from the GMT offset, but the $ZONEINFO/Etc/GMT$offset file can be
db449ef
            # used - note: this disables DST.
db449ef
            ((z=new_time_offset/3600))
db449ef
            ((hoursWest=$(printf '%+d' $z)))
db449ef
db449ef
            if (( $hoursWest < 0 )); then
db449ef
                # tzdata treats negative 'hours west' as positive 'gmtoff'!
db449ef
                ((hoursWest*=-1))
db449ef
            fi
db449ef
ee7ca6e
            tzfile=/usr/share/zoneinfo/Etc/GMT$(printf '%+d' ${hoursWest})
ee7ca6e
            if [ -e ${tzfile} ]; then
ee7ca6e
                save_previous /etc/localtime
ee7ca6e
                cp -fp ${tzfile} /etc/localtime
ee7ca6e
                touch /etc/localtime
dbecea2
                fix_context /etc/localtime
db449ef
            fi
db449ef
        fi
db449ef
    fi
db449ef
c3b67cd
    # execute any additional client side configuration scripts we have
580990a
    if [ -d ${ETCDIR}/dhclient.d ]; then
580990a
        for f in ${ETCDIR}/dhclient.d/*.sh ; do
c3b67cd
            if [ -x ${f} ]; then
2963207
                subsystem="${f%.sh}"
91ae67b
                subsystem="${subsystem##*/}"
91ae67b
                . ${f}
91ae67b
                "${subsystem}_config"
c3b67cd
            fi
db449ef
        done
db449ef
    fi
db449ef
}
db449ef
2d59159
# Section 18.1.8. (Receipt of Reply Messages) of RFC 3315 says:
2d59159
# The client SHOULD perform duplicate address detection on each of
2d59159
# the addresses in any IAs it receives in the Reply message before
2d59159
# using that address for traffic.
2d59159
add_ipv6_addr_with_DAD() {
5ec8ece
            ip -6 addr add ${new_ip6_address}/${new_ip6_prefixlen} \
f195973
                dev ${interface} scope global
21b8c45
21b8c45
            # repeatedly test whether newly added address passed
21b8c45
            # duplicate address detection (DAD)
21b8c45
            for i in $(seq 5); do
21b8c45
                sleep 1 # give the DAD some time
21b8c45
21b8c45
                # tentative flag = DAD is still not complete or failed
21b8c45
                duplicate=$(ip -6 addr show dev ${interface} tentative \
21b8c45
                              | grep ${new_ip6_address}/${new_ip6_prefixlen})
21b8c45
21b8c45
                # if there's no tentative flag, address passed DAD
21b8c45
                if [ -z "${duplicate}" ]; then
21b8c45
                    break
21b8c45
                fi
21b8c45
            done
21b8c45
21b8c45
            # if there's still tentative flag = address didn't pass DAD = 
21b8c45
            # = it's duplicate = remove it
21b8c45
            if [ -n "${duplicate}" ]; then
21b8c45
                ip -6 addr del ${new_ip6_address}/${new_ip6_prefixlen} dev ${interface}
21b8c45
                exit_with_hooks 3
21b8c45
            fi
2d59159
}
2d59159
2d59159
dh6config() {
2d59159
    case "${reason}" in
2d59159
        BOUND6)
b0cb903
            if [ -z "${new_ip6_address}" ] ||
2d59159
               [ -z "${new_ip6_prefixlen}" ]; then
2d59159
                exit_with_hooks 2
2d59159
            fi
2d59159
2d59159
            add_ipv6_addr_with_DAD
21b8c45
f195973
            make_resolv_conf
f195973
            ;;
f195973
f195973
        RENEW6|REBIND6)
b0cb903
            if [ -z "${new_ip6_address}" ] ||
b0cb903
               [ -z "${new_ip6_prefixlen}" ]; then
b0cb903
                exit_with_hooks 2
b0cb903
            fi
b0cb903
b0cb903
            if [  ! "${new_ip6_address}" = "${old_ip6_address}" ]; then
2d59159
                add_ipv6_addr_with_DAD
2d59159
            fi
2d59159
f195973
            if [ ! "${new_dhcp6_name_servers}" = "${old_dhcp6_name_servers}" ] ||
f195973
               [ ! "${new_dhcp6_domain_search}" = "${old_dhcp6_domain_search}" ]; then
f195973
                make_resolv_conf
f195973
            fi
f195973
            ;;
f195973
f195973
        DEPREF6)
9726e0f
            if [ -z "${new_ip6_prefixlen}" ]; then
f195973
                exit_with_hooks 2
f195973
            fi
f195973
5ec8ece
            ip -6 addr change ${new_ip6_address}/${new_ip6_prefixlen} \
f195973
                dev ${interface} scope global preferred_lft 0
f195973
            ;;
f195973
    esac
f195973
f195973
    # execute any additional client side configuration scripts we have
f195973
    if [ -d ${ETCDIR}/dhclient.d ]; then
f195973
        for f in ${ETCDIR}/dhclient.d/*.sh ; do
f195973
            if [ -x ${f} ]; then
f195973
                subsystem="${f%.sh}"
f195973
                subsystem="${subsystem##*/}"
f195973
                . ${f}
f195973
                "${subsystem}_config"
f195973
            fi
f195973
        done
f195973
    fi
f195973
}
f195973
db449ef
ee7ca6e
#
ee7ca6e
# ### MAIN
ee7ca6e
#
db449ef
580990a
if [ -x ${ETCDIR}/dhclient-enter-hooks ]; then
ee7ca6e
    exit_status=0
ee7ca6e
ee7ca6e
    # dhclient-enter-hooks can abort dhclient-script by setting
ee7ca6e
    # the exit_status variable to a non-zero value
580990a
    . ${ETCDIR}/dhclient-enter-hooks
ee7ca6e
    if [ ${exit_status} -ne 0 ]; then
ee7ca6e
        exit ${exit_status}
ee7ca6e
    fi
a51a2cf
fi
ee7ca6e
ee7ca6e
if [ ! -r /etc/sysconfig/network-scripts/network-functions ]; then
ee7ca6e
    echo "Missing /etc/sysconfig/network-scripts/network-functions, exiting." >&2
ee7ca6e
    exit 1
db449ef
fi
db449ef
ee7ca6e
if [ ! -r /etc/rc.d/init.d/functions ]; then
ee7ca6e
    echo "Missing /etc/rc.d/init.d/functions, exiting." >&2
ee7ca6e
    exit 1
db449ef
fi
db449ef
ee7ca6e
. /etc/sysconfig/network-scripts/network-functions
ee7ca6e
. /etc/rc.d/init.d/functions
ee7ca6e
ee7ca6e
if [ -f /etc/sysconfig/network ]; then
ee7ca6e
    . /etc/sysconfig/network
db449ef
fi
db449ef
ee7ca6e
if [ -f /etc/sysconfig/networking/network ]; then
ee7ca6e
    . /etc/sysconfig/networking/network
db449ef
fi
db449ef
cdcc09a
cd /etc/sysconfig/network-scripts
cdcc09a
CONFIG="ifcfg-${interface}"
ee7ca6e
need_config ${CONFIG}
5c6578c
source_config >/dev/null 2>&1
db449ef
5c6578c
new_prefix="$(get_prefix ${new_ip_address} ${new_subnet_mask})"
5ec8ece
old_prefix="$(get_prefix ${old_ip_address} ${old_subnet_mask})"
5c6578c
alias_prefix="$(get_prefix ${alias_ip_address} ${alias_subnet_mask})"
db449ef
ee7ca6e
case "${reason}" in
ee7ca6e
    MEDIUM)
ee7ca6e
        # Linux doesn't handle mediums (media)
ee7ca6e
        exit_with_hooks 0
ee7ca6e
        ;;
ee7ca6e
ee7ca6e
    PREINIT)
ee7ca6e
        if [ -n "${alias_ip_address}" ]; then
ee7ca6e
            # Bring down alias interface, its routes will disappear too.
5ec8ece
            ip link set ${interface}:0 down
3aac9e5
        fi
db449ef
ee7ca6e
        if [ "${keep_old_ip}" = "yes" ]; then
5ec8ece
            ip link set ${interface} up
ee7ca6e
        else
5ec8ece
            ip -4 addr flush dev ${interface} >/dev/null 2>&1
5ec8ece
            ip link set ${interface} up
db449ef
        fi
db449ef
ee7ca6e
        if [ -n "${DHCLIENT_DELAY}" ] && [ ${DHCLIENT_DELAY} -gt 0 ]; then
ee7ca6e
            sleep ${DHCLIENT_DELAY}
3aac9e5
        fi
db449ef
ee7ca6e
        exit_with_hooks 0
ee7ca6e
        ;;
db449ef
f195973
    PREINIT6)
f195973
        # ensure interface is up
f195973
        ip link set ${interface} up
f195973
f195973
        # remove any stale addresses from aborted clients
5ec8ece
        ip -6 addr flush dev ${interface} scope global permanent
f195973
f195973
        exit_with_hooks 0
f195973
        ;;
f195973
ee7ca6e
    ARPCHECK|ARPSEND)
ee7ca6e
        if [ -z "${new_ip_address}" ] || [ -z "${interface}" ] ||
ee7ca6e
           arping -q -f -c 2 -w 3 -D -I ${interface} ${new_ip_address}; then
ee7ca6e
            exit_with_hooks 0
ee7ca6e
        else
ee7ca6e
            exit_with_hooks 1
db449ef
        fi
ee7ca6e
        ;;
db449ef
ee7ca6e
    BOUND|RENEW|REBIND|REBOOT)
ee7ca6e
        dhconfig
ee7ca6e
        exit_with_hooks 0
ee7ca6e
        ;;
ee7ca6e
f195973
    BOUND6|RENEW6|REBIND6|DEPREF6)
f195973
        dh6config
f195973
        exit_with_hooks 0
f195973
        ;;
f195973
f195973
    EXPIRE6|RELEASE6|STOP6)
152a496
        if [ -z "${old_ip6_address}" ] || [ -z "${old_ip6_prefixlen}" ]; then
f195973
            exit_with_hooks 2
f195973
        fi
f195973
5ec8ece
        ip -6 addr del ${old_ip6_address}/${old_ip6_prefixlen} \
f195973
            dev ${interface}
f195973
f195973
        # execute any additional client side configuration scripts we have
f195973
        if [ -d ${ETCDIR}/dhclient.d ]; then
f195973
            for f in ${ETCDIR}/dhclient.d/*.sh ; do
f195973
                if [ -x ${f} ]; then
f195973
                    subsystem="${f%.sh}"
f195973
                    subsystem="${subsystem##*/}"
f195973
                    . ${f}
f195973
                    "${subsystem}_restore"
f195973
                fi
f195973
            done
f195973
        fi
f195973
f195973
        if [ -x ${ETCDIR}/dhclient-${interface}-down-hooks ]; then
f195973
            . ${ETCDIR}/dhclient-${interface}-down-hooks
f195973
        elif [ -x ${ETCDIR}/dhclient-down-hooks ]; then
f195973
            . ${ETCDIR}/dhclient-down-hooks
f195973
        fi
f195973
f195973
        exit_with_hooks 0
f195973
        ;;
f195973
ee7ca6e
    EXPIRE|FAIL|RELEASE|STOP)
ee7ca6e
        # only restore config files if there are no other dhclient processes
ee7ca6e
        # running (#306381)
ee7ca6e
        any_other_clients="$(ps -eo pid,ppid,comm | grep dhclient | grep -v ${PPID})"
ee7ca6e
        if [ -n "${any_other_clients}" ]; then
ee7ca6e
            if [ -f ${SAVEDIR}/resolv.conf.predhclient.${interface} ]; then
ee7ca6e
                change_resolv_conf ${SAVEDIR}/resolv.conf.predhclient.${interface}
ee7ca6e
                rm -f ${SAVEDIR}/resolv.conf.predhclient.${interface}
ee7ca6e
            fi
db449ef
ee7ca6e
            if [ -n "${DHCP_TIME_OFFSET_SETS_TIMEZONE}" ] &&
ee7ca6e
               [[ "${DHCP_TIME_OFFSET_SETS_TIMEZONE}" = [yY1]* ]]; then
ee7ca6e
                if [ -e ${SAVEDIR}/localtime.predhclient.${interface} ]; then
ee7ca6e
                    rm -f /etc/localtime
226011d
                    contents="$(< ${SAVEDIR}/localtime.predhclient.${interface})"
226011d
                    echo "${contents}" > /etc/localtime
226011d
                    rm -f ${SAVEDIR}/localtime.predhclient.${interface}
ee7ca6e
                    touch /etc/localtime
dbecea2
                    fix_context /etc/localtime
ee7ca6e
                fi
ee7ca6e
            fi
dbecea2
        fi
db449ef
dbecea2
        # execute any additional client side configuration scripts we have
580990a
        if [ -d ${ETCDIR}/dhclient.d ]; then
580990a
            for f in ${ETCDIR}/dhclient.d/*.sh ; do
dbecea2
                if [ -x ${f} ]; then
dbecea2
                    subsystem="${f%.sh}"
91ae67b
                    subsystem="${subsystem##*/}"
91ae67b
                    . ${f}
91ae67b
                    "${subsystem}_restore"
ee7ca6e
                fi
dbecea2
            done
ee7ca6e
        fi
db449ef
580990a
        if [ -x ${ETCDIR}/dhclient-${interface}-down-hooks ]; then
580990a
            . ${ETCDIR}/dhclient-${interface}-down-hooks
580990a
        elif [ -x ${ETCDIR}/dhclient-down-hooks ]; then
580990a
            . ${ETCDIR}/dhclient-down-hooks
ee7ca6e
        fi
db449ef
ee7ca6e
        if [ -n "${alias_ip_address}" ]; then
ee7ca6e
            # Turn off alias interface
5ec8ece
            ip link set ${interface}:0 down
ee7ca6e
        fi
db449ef
ee7ca6e
        if [ -n "${old_ip_address}" ]; then
d5a4ff8
            # Delete addresses/routes/arp cache.
d5a4ff8
            flush_dev ${interface}
ee7ca6e
        fi
ee7ca6e
ee7ca6e
        if [ -n "${alias_ip_address}" ]; then
5ec8ece
            ip -4 addr add ${alias_ip_address}/${alias_prefix} dev ${interface}:0
5ec8ece
            ip -4 route replace ${alias_ip_address}/32 ${interface}:0
ee7ca6e
        fi
db449ef
db449ef
        exit_with_hooks 0
ee7ca6e
        ;;
db449ef
ee7ca6e
    TIMEOUT)
ee7ca6e
        if [ -n "${new_routers}" ]; then
ee7ca6e
            if [ -n "${alias_ip_address}" ]; then
5ec8ece
                ip -4 addr flush dev ${interface}:0 >/dev/null 2>&1
ee7ca6e
            fi
ee7ca6e
5ec8ece
            ip -4 addr add ${new_ip_address}/${new_prefix} broadcast ${new_broadcast_address} dev ${interface}
ee7ca6e
            set ${new_routers}
ee7ca6e
ee7ca6e
            if ping -q -c 1 -w 10 -I ${interface} ${1}; then
ee7ca6e
                dhconfig
ee7ca6e
                exit_with_hooks 0
ee7ca6e
            fi
ee7ca6e
d5a4ff8
            flush_dev ${interface}
ee7ca6e
            exit_with_hooks 1
ee7ca6e
        else
ee7ca6e
            exit_with_hooks 1
ee7ca6e
        fi
ee7ca6e
        ;;
ee7ca6e
ee7ca6e
    *)
ee7ca6e
        logmessage "unhandled state: ${reason}"
ee7ca6e
        exit_with_hooks 1
ee7ca6e
        ;;
ee7ca6e
esac
db449ef
db449ef
exit_with_hooks 0