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