db449e
#!/bin/bash
ee7ca6
#
ee7ca6
# dhclient-script: Network interface configuration script run by
ee7ca6
#                  dhclient based on DHCP client communication
ee7ca6
#
543c1a
# Copyright (C) 2008-2014  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>
aa59a9
#            Jiri Popelka <jpopelka@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
8162f5
# scripts in dhclient.d/ use $SAVEDIR (#833054)
c191e7
export SAVEDIR=/var/lib/dhclient
db449e
ee7ca6
LOGFACILITY="local7"
ee7ca6
LOGLEVEL="notice"
db449e
580990
ETCDIR="/etc/dhcp"
580990
ee7ca6
logmessage() {
ee7ca6
    msg="${1}"
c191e7
    logger -p "${LOGFACILITY}.${LOGLEVEL}" -t "NET" "dhclient: ${msg}"
ee7ca6
}
db449e
94ec5d
eventually_add_hostnames_domain_to_search() {
94ec5d
# For the case when hostname for this machine has a domain that is not in domain_search list
94ec5d
# 1) get a hostname with `ipcalc --hostname` or `hostname`
94ec5d
# 2) get the domain from this hostname
94ec5d
# 3) add this domain to search line in resolv.conf if it's not already
94ec5d
#    there (domain list that we have recently added there is a parameter of this function)
94ec5d
# We can't do this directly when generating resolv.conf in make_resolv_conf(), because
94ec5d
# we need to first save the resolv.conf with obtained values before we can call `ipcalc --hostname`.
94ec5d
# See bug 637763
94ec5d
    search="${1}"
94ec5d
    if need_hostname; then
94ec5d
        status=1
94ec5d
        if [ -n "${new_ip_address}" ]; then
c191e7
            eval $(/usr/bin/ipcalc --silent --hostname "${new_ip_address}" ; echo "status=$?")
94ec5d
        elif [ -n "${new_ip6_address}" ]; then
c191e7
            eval $(/usr/bin/ipcalc --silent --hostname "${new_ip6_address}" ; echo "status=$?")
94ec5d
        fi
94ec5d
94ec5d
        if [ ${status} -eq 0 ]; then
c191e7
            domain=$(echo "${HOSTNAME}" | cut -s -d "." -f 2-)
94ec5d
        fi
94ec5d
    else
94ec5d
          domain=$(hostname 2>/dev/null | cut -s -d "." -f 2-)
94ec5d
    fi
94ec5d
94ec5d
    if [ -n "${domain}" ] &&
94ec5d
       [ ! "${domain}" = "localdomain" ] &&
94ec5d
       [ ! "${domain}" = "localdomain6" ] &&
94ec5d
       [ ! "${domain}" = "(none)" ] &&
94ec5d
       [[ ! "${domain}" = *\ * ]]; then
94ec5d
       is_in="false"
94ec5d
       for s in ${search}; do
94ec5d
           if [ "${s}" = "${domain}" ] ||
94ec5d
              [ "${s}" = "${domain}." ]; then
94ec5d
              is_in="true"
94ec5d
           fi
94ec5d
       done
94ec5d
94ec5d
       if [ "${is_in}" = "false" ]; then
94ec5d
          # Add domain name to search list (#637763)
94ec5d
          sed -i -e "s/${search}/${search} ${domain}/" /etc/resolv.conf
94ec5d
       fi
94ec5d
    fi
94ec5d
}
94ec5d
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
c191e7
        rscf="$(mktemp "${TMPDIR:-/tmp}/XXXXXX")"
e220d8
        [[ -z "${rscf}" ]] && return
c191e7
        echo "; generated by /usr/sbin/dhclient-script" > "${rscf}"
db449e
ee7ca6
        if [ -n "${SEARCH}" ]; then
94ec5d
            search="${SEARCH}"
6961ce
        else
ee7ca6
            if [ -n "${new_domain_search}" ]; then
94ec5d
                # Remove instaces of \032 (#450042)
94ec5d
                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.
94ec5d
                search="${new_domain_name}"
db449e
            fi
db449e
        fi
db449e
94ec5d
        if [ -n "${search}" ]; then
c191e7
            echo "search ${search}" >> "${rscf}"
94ec5d
        fi
94ec5d
ee7ca6
        if [ -n "${RES_OPTIONS}" ]; then
c191e7
            echo "options ${RES_OPTIONS}" >> "${rscf}"
d016d2
        fi
d016d2
ee7ca6
        for nameserver in ${new_domain_name_servers} ; do
c191e7
            echo "nameserver ${nameserver}" >> "${rscf}"
db449e
        done
db449e
c191e7
        change_resolv_conf "${rscf}"
c191e7
        rm -f "${rscf}"
f1ade6
94ec5d
        if [ -n "${search}" ]; then
94ec5d
            eventually_add_hostnames_domain_to_search "${search}"
94ec5d
        fi
f19597
    elif [ -n "${new_dhcp6_name_servers}" ] ||
f19597
         [ -n "${new_dhcp6_domain_search}" ]; then
c191e7
        rscf="$(mktemp "${TMPDIR:-/tmp}/XXXXXX")"
e220d8
        [[ -z "${rscf}" ]] && return
c191e7
        echo "; generated by /usr/sbin/dhclient-script" > "${rscf}"
f19597
f19597
        if [ -n "${SEARCH}" ]; then
94ec5d
            search="${SEARCH}"
f19597
        else
f19597
            if [ -n "${new_dhcp6_domain_search}" ]; then
94ec5d
                search="${new_dhcp6_domain_search//\\032/ }"
f19597
            fi
f19597
        fi
f19597
94ec5d
        if [ -n "${search}" ]; then
c191e7
            echo "search ${search}" >> "${rscf}"
94ec5d
        fi
94ec5d
f19597
        if [ -n "${RES_OPTIONS}" ]; then
c191e7
            echo "options ${RES_OPTIONS}" >> "${rscf}"
f19597
        fi
f19597
9b3b26
        shopt -s nocasematch 
f19597
        for nameserver in ${new_dhcp6_name_servers} ; do
9b3b26
            # If the nameserver has a link-local address
9b3b26
            # add a <zone_id> (interface name) to it.
9b3b26
            if  [[ "$nameserver" =~ ^fe80:: ]]
9b3b26
            then
9b3b26
                zone_id="%${interface}"
9b3b26
            else
9b3b26
                zone_id=
9b3b26
            fi
c191e7
            echo "nameserver ${nameserver}$zone_id" >> "${rscf}"
f19597
        done
9b3b26
        shopt -u nocasematch 
f19597
c191e7
        change_resolv_conf "${rscf}"
c191e7
        rm -f "${rscf}"
f19597
94ec5d
        if [ -n "${search}" ]; then
94ec5d
            eventually_add_hostnames_domain_to_search "${search}"
94ec5d
        fi
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
c191e7
    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() {
8568b7
    IFS='.' quad2num ${1}
db449e
}
db449e
ee7ca6
num2ip() {
ee7ca6
    let n="${1}"
c191e7
    let o1="(${n} >> 24) & 0xff"
c191e7
    let o2="(${n} >> 16) & 0xff"
c191e7
    let o3="(${n} >> 8) & 0xff"
c191e7
    let o4="${n} & 0xff"
ee7ca6
    echo "${o1}.${o2}.${o3}.${o4}"
db449e
}
db449e
a21bc0
get_network_address() {
a21bc0
# get network address for the given IP address and (netmask or prefix)
ee7ca6
    ip="${1}"
a21bc0
    nm="${2}"
a21bc0
a21bc0
    if [ -n "${ip}" -a -n "${nm}" ]; then
a21bc0
        if [[ "${nm}" = *.* ]]; then
c191e7
            ipcalc -s -n "${ip}" "${nm}" | cut -d '=' -f 2
a21bc0
        else
c191e7
            ipcalc -s -n "${ip}/${nm}" | cut -d '=' -f 2
a21bc0
        fi
a21bc0
    fi
a21bc0
}
a21bc0
a21bc0
get_prefix() {
a21bc0
# get prefix for the given IP address and mask
a21bc0
    ip="${1}"
a21bc0
    nm="${2}"
a21bc0
a21bc0
    if [ -n "${ip}" -a -n "${nm}" ]; then
c191e7
        ipcalc -s -p "${ip}" "${nm}" | cut -d '=' -f 2
a21bc0
    fi
db449e
}
db449e
ee7ca6
class_bits() {
c191e7
    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}"
c191e7
    routersubnet="$(get_network_address "${router}" "${new_subnet_mask}")"
c191e7
    mysubnet="$(get_network_address "${new_ip_address}" "${new_subnet_mask}")"
ee7ca6
ee7ca6
    if [ ! "${routersubnet}" = "${mysubnet}" ]; then
Filipe Brandenburger 6b7e12
        # TODO: This function should not have side effects such as adding or
Filipe Brandenburger 6b7e12
        # removing routes. Can this be done with "ip route get" or similar
Filipe Brandenburger 6b7e12
        # instead? Are there cases that rely on this route being created here?
c191e7
        ip -4 route replace "${router}/32" dev "${interface}"
e7bd38
        if [ "$?" -ne 0 ]; then
e7bd38
            logmessage "failed to create host route for ${router}"
b112ec
            return 1
db449e
        fi
db449e
    fi
ee7ca6
b112ec
    return 0
db449e
}
db449e
ee7ca6
add_default_gateway() {
ee7ca6
    router="${1}"
ee7ca6
c191e7
    if is_router_reachable "${router}" ; then
8568b7
        if [ $# -gt 1 ] && [ -n "${2}" ] && [[ "${2}" -gt 0 ]]; then
8568b7
            ip -4 route replace default via "${router}" dev "${interface}" metric "${2}"
8568b7
        else
8568b7
            ip -4 route replace default via "${router}" dev "${interface}"
a2aab0
        fi
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
a2aab0
execute_client_side_configuration_scripts() {
a2aab0
# execute any additional client side configuration scripts we have
a2aab0
    if [ "${1}" == "config" ] || [ "${1}" == "restore" ]; then
a2aab0
        for f in ${ETCDIR}/dhclient.d/*.sh ; do
c191e7
            if [ -x "${f}" ]; then
a2aab0
                subsystem="${f%.sh}"
a2aab0
                subsystem="${subsystem##*/}"
c191e7
                . "${f}"
a2aab0
                "${subsystem}_${1}"
a2aab0
            fi
a2aab0
        done
a2aab0
    fi
a2aab0
}
a2aab0
d5a4ff
flush_dev() {
d5a4ff
# Instead of bringing the interface down (#574568)
d80cfc
# explicitly clear ARP cache and flush all addresses & routes.
c191e7
    ip -4 addr flush dev "${1}" >/dev/null 2>&1
c191e7
    ip -4 route flush dev "${1}" >/dev/null 2>&1
c191e7
    ip -4 neigh flush dev "${1}" >/dev/null 2>&1
d5a4ff
}
d5a4ff
d80cfc
remove_old_addr() {
d80cfc
    if [ -n "${old_ip_address}" ]; then
d80cfc
        if [ -n "${old_prefix}" ]; then
c191e7
            ip -4 addr del "${old_ip_address}/${old_prefix}" dev "${interface}" >/dev/null 2>&1
d80cfc
        else
c191e7
            ip -4 addr del "${old_ip_address}" dev "${interface}" >/dev/null 2>&1
d80cfc
        fi
d80cfc
    fi
d80cfc
}
d80cfc
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
c191e7
        ip -4 addr del "${old_ip_address}" dev "${interface}" label "${interface}:0"
db449e
    fi
db449e
ee7ca6
    if [ -n "${old_ip_address}" ] &&
ee7ca6
       [ ! "${old_ip_address}" = "${new_ip_address}" ]; then
d5a4ff
        # IP address changed. Delete all routes, and clear the ARP cache.
c191e7
        flush_dev "${interface}"
db449e
    fi
db449e
986801
    # make sure the interface is up
c191e7
    ip link set dev "${interface}" up
986801
986801
    # replace = add if it doesn't exist or override (update lifetimes) if it's there
c191e7
    ip -4 addr replace "${new_ip_address}/${new_prefix}" broadcast "${new_broadcast_address}" dev "${interface}" \
c191e7
       valid_lft "${new_dhcp_lease_time}" preferred_lft "${new_dhcp_lease_time}" >/dev/null 2>&1
986801
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
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).
c191e7
        if [ -n "${new_interface_mtu}" ] && [ "${new_interface_mtu}" -gt 576 ]; then
c191e7
            ip link set dev "${interface}" mtu "${new_interface_mtu}"
db449e
        fi
db449e
a21bc0
        # static routes
a21bc0
        if [ -n "${new_classless_static_routes}" ] ||
a21bc0
           [ -n "${new_static_routes}" ]; then
a21bc0
            if [ -n "${new_classless_static_routes}" ]; then
a21bc0
                IFS=', |' static_routes=(${new_classless_static_routes})
a21bc0
            else
a21bc0
                IFS=', |' static_routes=(${new_static_routes})
a21bc0
            fi
a21bc0
            route_targets=()
a21bc0
a21bc0
            for((i=0; i<${#static_routes[@]}; i+=2)); do
a21bc0
                target=${static_routes[$i]}
a21bc0
                if [ -n "${new_classless_static_routes}" ]; then
c191e7
                    if [ "${target}" = "0" ]; then
2560aa
                        # If the DHCP server returns both a Classless Static Routes option and
2560aa
                        # a Router option, the DHCP client MUST ignore the Router option. (RFC3442)
2560aa
                        new_routers=""
2560aa
                        prefix="0"
2560aa
                    else
a2aab0
                        prefix=${target%%.*}
a2aab0
                        target=${target#*.}
2560aa
                        IFS="." target_arr=(${target})
2560aa
                        unset IFS
2560aa
                        ((pads=4-${#target_arr[@]}))
2560aa
                        for j in $(seq $pads); do
5d3b86
                            target="${target}.0"
2560aa
                        done
2560aa
2560aa
                        # Client MUST zero any bits in the subnet number where the corresponding bit in the mask is zero.
2560aa
                        # In other words, the subnet number installed in the routing table is the logical AND of
2560aa
                        # the subnet number and subnet mask given in the Classless Static Routes option. (RFC3442)
c191e7
                        target="$(get_network_address "${target}" "${prefix}")"
2560aa
                    fi
a21bc0
                else
c191e7
                    prefix=$(class_bits "${target}")
a21bc0
                fi
a21bc0
                gateway=${static_routes[$i+1]}
a21bc0
0acc1b
                # special case 0.0.0.0 to allow static routing for link-local addresses
4e9262
                # (including IPv4 multicast) which will not have a next-hop (#769463, #787318)
4e9262
                if [ "${gateway}" = "0.0.0.0" ]; then
4e9262
                    valid_gateway=0
4e9262
                    scope='scope link'
4e9262
                else
c191e7
                    is_router_reachable "${gateway}"
4e9262
                    valid_gateway=$?
4e9262
                    scope=''
4e9262
                fi
c191e7
                if [ "${valid_gateway}" -eq 0 ]; then
a2aab0
                    metric=''
c191e7
                    for t in "${route_targets[@]}"; do
c191e7
                        if [ "${t}" = "${target}" ]; then
a2aab0
                            if [ -z "${metric}" ]; then
a2aab0
                                metric=1
a2aab0
                            else
a2aab0
                                ((metric=metric+1))
a2aab0
                            fi
a21bc0
                        fi
a2aab0
                    done
a21bc0
a2aab0
                    if [ -n "${metric}" ]; then
a2aab0
                        metric="metric ${metric}"
a2aab0
                    fi
a21bc0
8568b7
                    ip -4 route replace "${target}/${prefix}" proto static via "${gateway}" dev "${interface}" ${metric} ${scope}
a21bc0
a21bc0
                    if [ $? -ne 0 ]; then
a21bc0
                        logmessage "failed to create static route: ${target}/${prefix} via ${gateway} dev ${interface} ${metric}"
a21bc0
                    else
a21bc0
                        route_targets=(${route_targets[@]} ${target})
a21bc0
                    fi
a21bc0
                fi
a21bc0
            done
a21bc0
        fi
a21bc0
a21bc0
        # gateways
a2aab0
        if [[ ( "${DEFROUTE}" != "no" ) &&
0acc1b
              (( -z "${GATEWAYDEV}" ) || ( "${GATEWAYDEV}" = "${interface}" )) ]]; then
c191e7
            if [[ ( -z "${GATEWAY}" ) ||
c191e7
                  (( -n "${DHCLIENT_IGNORE_GATEWAY}" ) && ( "${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
c191e7
                    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" ] ||
8568b7
                       [ "$(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})
c191e7
                    add_default_gateway "${router}" "${metric}"
9ca442
                    let i=i+1
9ca442
                    metric=${i}
db449e
                done
9ca442
            elif [ -n "${GATEWAY}" ]; then
c191e7
                routersubnet=$(get_network_address "${GATEWAY}" "${new_subnet_mask}")
c191e7
                mysubnet=$(get_network_address "${new_ip_address}" "${new_subnet_mask}")
db449e
9ca442
                if [ "${routersubnet}" = "${mysubnet}" ]; then
c191e7
                    ip -4 route replace default via "${GATEWAY}" dev "${interface}"
db449e
                fi
db449e
            fi
db449e
        fi
db449e
    fi
db449e
ee7ca6
    if [ ! "${new_ip_address}" = "${alias_ip_address}" ] &&
ee7ca6
       [ -n "${alias_ip_address}" ]; then
5c5bbc
        # Reset the alias address (fix: this should really only do this on changes)
c191e7
        ip -4 addr flush dev "${interface}" label "${interface}:0" >/dev/null 2>&1
c191e7
        ip -4 addr replace "${alias_ip_address}/${alias_prefix}" broadcast "${alias_broadcast_address}" dev "${interface}" label "${interface}:0"
c191e7
        ip -4 route replace "${alias_ip_address}/32" dev "${interface}"
db449e
    fi
60e971
    
60e971
    # After dhclient brings an interface UP with a new IP address, subnet mask, 
60e971
    # and routes, in the REBOOT/BOUND states -> search for "dhclient-up-hooks".
60e971
    if [ "${reason}" = "BOUND" ] || [ "${reason}" = "REBOOT" ] ||
60e971
       [ ! "${old_ip_address}" = "${new_ip_address}" ] ||
60e971
       [ ! "${old_subnet_mask}" = "${new_subnet_mask}" ] ||
60e971
       [ ! "${old_network_number}" = "${new_network_number}" ] ||
60e971
       [ ! "${old_broadcast_address}" = "${new_broadcast_address}" ] ||
60e971
       [ ! "${old_routers}" = "${new_routers}" ] ||
60e971
       [ ! "${old_interface_mtu}" = "${new_interface_mtu}" ]; then
60e971
        
c191e7
        if [ -x "${ETCDIR}/dhclient-${interface}-up-hooks" ]; then
c191e7
            . "${ETCDIR}/dhclient-${interface}-up-hooks"
60e971
        elif [ -x ${ETCDIR}/dhclient-up-hooks ]; then
60e971
            . ${ETCDIR}/dhclient-up-hooks
60e971
        fi
60e971
    fi
db449e
db449e
    make_resolv_conf
db449e
ee7ca6
    if [ -n "${new_host_name}" ] && need_hostname; then
c191e7
        hostname "${new_host_name}" || echo "See -nc option in dhclient(8) man page."
db449e
    fi
db449e
a2aab0
    if [[ ( "${DHCP_TIME_OFFSET_SETS_TIMEZONE}" = [yY1]* ) &&
a2aab0
          ( -n "${new_time_offset}" ) ]]; then
a2aab0
        # DHCP option "time-offset" is requested by default and should be
a2aab0
        # handled.  The geographical zone abbreviation cannot be determined
a2aab0
        # from the GMT offset, but the $ZONEINFO/Etc/GMT$offset file can be
a2aab0
        # used - note: this disables DST.
a2aab0
        ((z=new_time_offset/3600))
a2aab0
        ((hoursWest=$(printf '%+d' $z)))
a2aab0
a2aab0
        if (( $hoursWest < 0 )); then
a2aab0
            # tzdata treats negative 'hours west' as positive 'gmtoff'!
a2aab0
            ((hoursWest*=-1))
a2aab0
        fi
db449e
a2aab0
        tzfile=/usr/share/zoneinfo/Etc/GMT$(printf '%+d' ${hoursWest})
c191e7
        if [ -e "${tzfile}" ]; then
c191e7
            cp -fp "${tzfile}" /etc/localtime
a2aab0
            touch /etc/localtime
db449e
        fi
db449e
    fi
db449e
a2aab0
    execute_client_side_configuration_scripts "config"
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() {
c191e7
            ip -6 addr replace "${new_ip6_address}/${new_ip6_prefixlen}" \
c191e7
                dev "${interface}" scope global valid_lft "${new_max_life}" \
c191e7
                                          preferred_lft "${new_preferred_life}"
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
c191e7
                addr=$(ip -6 addr show dev "${interface}" \
c191e7
                       | grep "${new_ip6_address}/${new_ip6_prefixlen}")
499202
367b44
                # tentative flag == DAD is still not complete
499202
                tentative=$(echo "${addr}" | grep tentative)
367b44
                # dadfailed flag == address is already in use somewhere else
499202
                dadfailed=$(echo "${addr}" | grep dadfailed)
367b44
367b44
                if [ -n "${dadfailed}" ] ; then
499202
                    # address was added with valid_lft/preferred_lft 'forever', remove it
c191e7
                    ip -6 addr del "${new_ip6_address}/${new_ip6_prefixlen}" dev "${interface}"
367b44
                    exit_with_hooks 3
367b44
                fi
367b44
                if [ -z "${tentative}" ] ; then
5bd17c
                    if [ -n "${addr}" ]; then
5bd17c
                        # DAD is over
5bd17c
                        return 0
5bd17c
                    else
5bd17c
                        # address was auto-removed (or not added at all)
5bd17c
                        exit_with_hooks 3
5bd17c
                    fi
21b8c4
                fi
21b8c4
            done
499202
            return 0
2d5915
}
2d5915
2d5915
dh6config() {
5d3b86
    if [ -n "${old_ip6_prefix}" ] ||
5d3b86
       [ -n "${new_ip6_prefix}" ]; then
c191e7
        echo "Prefix ${reason} old=${old_ip6_prefix} new=${new_ip6_prefix}"
76c2a1
        exit_with_hooks 0
76c2a1
    fi
76c2a1
2d5915
    case "${reason}" in
2d5915
        BOUND6)
b0cb90
            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)
4533a8
            if [[ -n "${new_ip6_address}" ]] &&
4533a8
               [[ -n "${new_ip6_prefixlen}" ]]; then
4533a8
               if [[  ! "${new_ip6_address}" = "${old_ip6_address}" ]]; then
c191e7
                   [[ -n "${old_ip6_address}" ]] && ip -6 addr del "${old_ip6_address}" dev "${interface}"
4533a8
               fi
986801
               # call it even if new_ip6_address = old_ip6_address to update lifetimes
986801
               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
c191e7
            ip -6 addr change "${new_ip6_address}/${new_ip6_prefixlen}" \
c191e7
                dev "${interface}" scope global preferred_lft 0
f19597
            ;;
f19597
    esac
f19597
a2aab0
    execute_client_side_configuration_scripts "config"
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
5a6fff
CONFIG="${interface}"
c191e7
need_config "${CONFIG}"
5c6578
source_config >/dev/null 2>&1
db449e
d12e0e
# In case there's some delay in rebinding, it might happen, that the valid_lft drops to 0,
d12e0e
# address is removed by kernel and then re-added few seconds later by dhclient-script.
d12e0e
# With this work-around the address lives a minute longer.
d12e0e
# "4294967235" = infinite (forever) - 60
d12e0e
[[ "${new_dhcp_lease_time}" -lt "4294967235" ]] && new_dhcp_lease_time=$((new_dhcp_lease_time + 60))
d12e0e
[[ "${new_max_life}" -lt "4294967235" ]] && new_max_life=$((new_max_life + 60))
d12e0e
c191e7
new_prefix="$(get_prefix "${new_ip_address}" "${new_subnet_mask}")"
c191e7
old_prefix="$(get_prefix "${old_ip_address}" "${old_subnet_mask}")"
c191e7
alias_prefix="$(get_prefix "${alias_ip_address}" "${alias_subnet_mask}")"
db449e
ee7ca6
case "${reason}" in
9af3b8
    MEDIUM|ARPCHECK|ARPSEND)
9af3b8
        # Do nothing
ee7ca6
        exit_with_hooks 0
ee7ca6
        ;;
ee7ca6
ee7ca6
    PREINIT)
ee7ca6
        if [ -n "${alias_ip_address}" ]; then
5c5bbc
            # Flush alias, its routes will disappear too.
c191e7
            ip -4 addr flush dev "${interface}" label "${interface}:0" >/dev/null 2>&1
3aac9e
        fi
db449e
efe27a
        # upstream dhclient-script removes (ifconfig $interface 0 up) old adresses in PREINIT,
efe27a
        # but we sometimes (#125298) need (for iSCSI/nfs root to have a dhcp interface) to keep the existing ip
efe27a
        # flush_dev ${interface}
c191e7
        ip link set dev "${interface}" up
c191e7
        if [ -n "${DHCLIENT_DELAY}" ] && [ "${DHCLIENT_DELAY}" -gt 0 ]; then
634087
            # We need to give the kernel some time to get the interface up.
c191e7
            sleep "${DHCLIENT_DELAY}"
3aac9e
        fi
db449e
ee7ca6
        exit_with_hooks 0
ee7ca6
        ;;
db449e
f19597
    PREINIT6)
f19597
        # ensure interface is up
c191e7
        ip link set dev "${interface}" up
f19597
efdf4e
        # remove any stale addresses from aborted clients
c191e7
        ip -6 addr flush dev "${interface}" scope global permanent
f19597
991bd3
        # we need a link-local address to be ready (not tentative)
991bd3
        for i in $(seq 50); do
c191e7
            linklocal=$(ip -6 addr show dev "${interface}" scope link)
b81510
            # tentative flag means DAD is still not complete
b81510
            tentative=$(echo "${linklocal}" | grep tentative)
b81510
            [[ -n "${linklocal}" && -z "${tentative}" ]] && exit_with_hooks 0
991bd3
            sleep 0.1
991bd3
        done
991bd3
f19597
        exit_with_hooks 0
f19597
        ;;
f19597
9af3b8
    BOUND|RENEW|REBIND|REBOOT)
9af3b8
        if [ -z "${interface}" ] || [ -z "${new_ip_address}" ]; then
9af3b8
            exit_with_hooks 2
9af3b8
        fi
c191e7
        if arping -D -q -c2 -I "${interface}" "${new_ip_address}"; then
9af3b8
            dhconfig
ee7ca6
            exit_with_hooks 0
593801
        else  # DAD failed, i.e. address is already in use
c191e7
            ARP_REPLY=$(arping -D -c2 -I "${interface}" "${new_ip_address}" | grep reply | awk '{print toupper($5)}' | cut -d "[" -f2 | cut -d "]" -f1)
593801
            OUR_MACS=$(ip link show | grep link | awk '{print toupper($2)}' | uniq)
593801
            if [[ "${OUR_MACS}" = *"${ARP_REPLY}"* ]]; then
593801
                # the reply can come from our system, that's OK (#1116004#c33)
593801
                dhconfig
593801
                exit_with_hooks 0
593801
            else
593801
                exit_with_hooks 1
593801
            fi
db449e
        fi
ee7ca6
        ;;
db449e
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
c191e7
        ip -6 addr del "${old_ip6_address}/${old_ip6_prefixlen}" \
c191e7
            dev "${interface}"
f19597
a2aab0
        execute_client_side_configuration_scripts "restore"
f19597
c191e7
        if [ -x "${ETCDIR}/dhclient-${interface}-down-hooks" ]; then
c191e7
            . "${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)
a2aab0
        execute_client_side_configuration_scripts "restore"
db449e
c191e7
        if [ -x "${ETCDIR}/dhclient-${interface}-down-hooks" ]; then
c191e7
            . "${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
5c5bbc
            # Flush alias
c191e7
            ip -4 addr flush dev "${interface}" label "${interface}:0" >/dev/null 2>&1
ee7ca6
        fi
db449e
d80cfc
        # upstream script sets interface down here,
d80cfc
        # we only remove old ip address
d80cfc
        #flush_dev ${interface}
d80cfc
        remove_old_addr
ee7ca6
ee7ca6
        if [ -n "${alias_ip_address}" ]; then
c191e7
            ip -4 addr replace "${alias_ip_address}/${alias_prefix}" broadcast "${alias_broadcast_address}" dev "${interface}" label "${interface}:0"
c191e7
            ip -4 route replace "${alias_ip_address}/32" dev "${interface}"
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
c191e7
                ip -4 addr flush dev "${interface}" label "${interface}:0" >/dev/null 2>&1
ee7ca6
            fi
ee7ca6
c191e7
            ip -4 addr replace "${new_ip_address}/${new_prefix}" \
c191e7
                broadcast "${new_broadcast_address}" dev "${interface}" \
c191e7
                valid_lft "${new_dhcp_lease_time}" preferred_lft "${new_dhcp_lease_time}"
ee7ca6
            set ${new_routers}
ee7ca6
c191e7
            if ping -q -c 1 -w 10 -I "${interface}" "${1}"; then
ee7ca6
                dhconfig
ee7ca6
                exit_with_hooks 0
ee7ca6
            fi
ee7ca6
d80cfc
            #flush_dev ${interface}
d80cfc
            remove_old_addr
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