Blame SOURCES/dhclient-script

fad460
#!/bin/bash
fad460
#
fad460
# dhclient-script: Network interface configuration script run by
fad460
#                  dhclient based on DHCP client communication
fad460
#
fad460
# Copyright (C) 2008-2014  Red Hat, Inc.
fad460
#
fad460
# This program is free software; you can redistribute it and/or modify
fad460
# it under the terms of the GNU General Public License as published by
fad460
# the Free Software Foundation; either version 2 of the License, or
fad460
# (at your option) any later version.
fad460
#
fad460
# This program is distributed in the hope that it will be useful,
fad460
# but WITHOUT ANY WARRANTY; without even the implied warranty of
fad460
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
fad460
# GNU General Public License for more details.
fad460
#
fad460
# You should have received a copy of the GNU General Public License
fad460
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
fad460
#
fad460
# Author(s): David Cantrell <dcantrell@redhat.com>
fad460
#            Jiri Popelka <jpopelka@redhat.com>
fad460
#
fad460
# ----------
fad460
# This script is a rewrite/reworking on dhclient-script originally
fad460
# included as part of dhcp-970306:
fad460
# dhclient-script for Linux. Dan Halbert, March, 1997.
fad460
# Updated for Linux 2.[12] by Brian J. Murrell, January 1999.
fad460
# Modified by David Cantrell <dcantrell@redhat.com> for Fedora and RHEL
fad460
# ----------
fad460
#
fad460
fad460
PATH=/bin:/usr/bin:/sbin
fad460
# scripts in dhclient.d/ use $SAVEDIR (#833054)
fad460
SAVEDIR=/var/lib/dhclient
fad460
fad460
LOGFACILITY="local7"
fad460
LOGLEVEL="notice"
fad460
fad460
ETCDIR="/etc/dhcp"
fad460
fad460
logmessage() {
fad460
    msg="${1}"
fad460
    logger -p ${LOGFACILITY}.${LOGLEVEL} -t "NET" "dhclient: ${msg}"
fad460
}
fad460
fad460
eventually_add_hostnames_domain_to_search() {
fad460
# For the case when hostname for this machine has a domain that is not in domain_search list
fad460
# 1) get a hostname with `ipcalc --hostname` or `hostname`
fad460
# 2) get the domain from this hostname
fad460
# 3) add this domain to search line in resolv.conf if it's not already
fad460
#    there (domain list that we have recently added there is a parameter of this function)
fad460
# We can't do this directly when generating resolv.conf in make_resolv_conf(), because
fad460
# we need to first save the resolv.conf with obtained values before we can call `ipcalc --hostname`.
fad460
# See bug 637763
fad460
    search="${1}"
fad460
    if need_hostname; then
fad460
        status=1
fad460
        OLD_HOSTNAME=${HOSTNAME}
fad460
        if [ -n "${new_ip_address}" ]; then
fad460
            eval $(/bin/ipcalc --silent --hostname ${new_ip_address} ; echo "status=$?")
fad460
        elif [ -n "${new_ip6_address}" ]; then
fad460
            eval $(/bin/ipcalc --silent --hostname ${new_ip6_address} ; echo "status=$?")
fad460
        fi
fad460
fad460
        if [ ${status} -eq 0 ]; then
fad460
            domain=$(echo $HOSTNAME | cut -s -d "." -f 2-)
fad460
        fi
fad460
        HOSTNAME=${OLD_HOSTNAME}
fad460
    else
fad460
          domain=$(hostname 2>/dev/null | cut -s -d "." -f 2-)
fad460
    fi
fad460
fad460
    if [ -n "${domain}" ] &&
fad460
       [ ! "${domain}" = "localdomain" ] &&
fad460
       [ ! "${domain}" = "localdomain6" ] &&
fad460
       [ ! "${domain}" = "(none)" ] &&
fad460
       [[ ! "${domain}" = *\ * ]]; then
fad460
       is_in="false"
fad460
       for s in ${search}; do
fad460
           if [ "${s}" = "${domain}" ] ||
fad460
              [ "${s}" = "${domain}." ]; then
fad460
              is_in="true"
fad460
           fi
fad460
       done
fad460
fad460
       if [ "${is_in}" = "false" ]; then
fad460
          # Add domain name to search list (#637763)
fad460
          sed -i -e "s/${search}/${search} ${domain}/" /etc/resolv.conf
fad460
       fi
fad460
    fi
fad460
}
fad460
fad460
make_resolv_conf() {
fad460
    [ "${PEERDNS}" = "no" ] && return
fad460
fad460
    if [ "${reason}" = "RENEW" ] &&
fad460
       [ "${new_domain_name}" = "${old_domain_name}" ] &&
fad460
       [ "${new_domain_name_servers}" = "${old_domain_name_servers}" ]; then
fad460
        return
fad460
    fi
fad460
fad460
    if [ -n "${new_domain_name}" ] ||
fad460
       [ -n "${new_domain_name_servers}" ] ||
fad460
       [ -n "${new_domain_search}" ]; then
fad460
        rscf="$(mktemp ${TMPDIR:-/tmp}/XXXXXX)"
fad460
        [[ -z "${rscf}" ]] && return
fad460
        echo "; generated by /usr/sbin/dhclient-script" > ${rscf}
fad460
fad460
        if [ -n "${SEARCH}" ]; then
fad460
            search="${SEARCH}"
fad460
        else
fad460
            if [ -n "${new_domain_search}" ]; then
fad460
                # Remove instaces of \032 (#450042)
fad460
                search="${new_domain_search//\\032/ }"
fad460
            elif [ -n "${new_domain_name}" ]; then
fad460
                # Note that the DHCP 'Domain Name Option' is really just a domain
fad460
                # name, and that this practice of using the domain name option as
fad460
                # a search path is both nonstandard and deprecated.
fad460
                search="${new_domain_name}"
fad460
            fi
fad460
        fi
fad460
fad460
        if [ -n "${search}" ]; then
fad460
            echo "search ${search}" >> $rscf
fad460
        fi
fad460
fad460
        if [ -n "${RES_OPTIONS}" ]; then
fad460
            echo "options ${RES_OPTIONS}" >> ${rscf}
fad460
        fi
fad460
fad460
        if [ -n "${new_domain_name_servers}" ]; then
fad460
            for nameserver in ${new_domain_name_servers} ; do
fad460
                echo "nameserver ${nameserver}" >> "${rscf}"
fad460
            done
fad460
        else # keep 'old' nameservers
fad460
            sed -n /^\w*[Nn][Aa][Mm][Ee][Ss][Ee][Rr][Vv][Ee][Rr]/p /etc/resolv.conf >> "${rscf}"
fad460
        fi
fad460
fad460
        change_resolv_conf ${rscf}
fad460
        rm -f ${rscf}
fad460
fad460
        if [ -n "${search}" ]; then
fad460
            eventually_add_hostnames_domain_to_search "${search}"
fad460
        fi
fad460
    elif [ -n "${new_dhcp6_name_servers}" ] ||
fad460
         [ -n "${new_dhcp6_domain_search}" ]; then
fad460
        rscf="$(mktemp ${TMPDIR:-/tmp}/XXXXXX)"
fad460
        [[ -z "${rscf}" ]] && return
fad460
        echo "; generated by /usr/sbin/dhclient-script" > ${rscf}
fad460
fad460
        if [ -n "${SEARCH}" ]; then
fad460
            search="${SEARCH}"
fad460
        else
fad460
            if [ -n "${new_dhcp6_domain_search}" ]; then
fad460
                search="${new_dhcp6_domain_search//\\032/ }"
fad460
            fi
fad460
        fi
fad460
fad460
        if [ -n "${search}" ]; then
fad460
            echo "search ${search}" >> $rscf
fad460
        fi
fad460
fad460
        if [ -n "${RES_OPTIONS}" ]; then
fad460
            echo "options ${RES_OPTIONS}" >> ${rscf}
fad460
        fi
fad460
fad460
        shopt -s nocasematch
fad460
        if [ -n "${new_dhcp6_name_servers}" ]; then
fad460
            for nameserver in ${new_dhcp6_name_servers} ; do
fad460
                # If the nameserver has a link-local address
fad460
                # add a <zone_id> (interface name) to it.
fad460
                if  [[ "$nameserver" =~ ^fe80:: ]]
fad460
                then
fad460
                    zone_id="%${interface}"
fad460
                else
fad460
                    zone_id=
fad460
                fi
fad460
                echo "nameserver ${nameserver}$zone_id" >> "${rscf}"
fad460
            done
fad460
        else # keep 'old' nameservers
fad460
            sed -n /^\w*[Nn][Aa][Mm][Ee][Ss][Ee][Rr][Vv][Ee][Rr]/p /etc/resolv.conf >> "${rscf}"
fad460
        fi
fad460
        shopt -u nocasematch
fad460
fad460
        change_resolv_conf ${rscf}
fad460
        rm -f ${rscf}
fad460
fad460
        if [ -n "${search}" ]; then
fad460
            eventually_add_hostnames_domain_to_search "${search}"
fad460
        fi
fad460
    fi
fad460
}
fad460
fad460
exit_with_hooks() {
fad460
    exit_status="${1}"
fad460
fad460
    if [ -x ${ETCDIR}/dhclient-exit-hooks ]; then
fad460
        . ${ETCDIR}/dhclient-exit-hooks
fad460
    fi
fad460
fad460
    if [ -d ${ETCDIR}/dhclient-exit-hooks.d ]; then
fad460
        for f in ${ETCDIR}/dhclient-exit-hooks.d/*.sh ; do
fad460
            if [ -x ${f} ]; then
fad460
                . ${f}
fad460
            fi
fad460
        done
fad460
    fi
fad460
fad460
    exit ${exit_status}
fad460
}
fad460
fad460
quad2num() {
fad460
    if [ $# -eq 4 ]; then
fad460
        let n="${1} << 24 | ${2} << 16 | ${3} << 8 | ${4}"
fad460
        echo "${n}"
fad460
        return 0
fad460
    else
fad460
        echo "0"
fad460
        return 1
fad460
    fi
fad460
}
fad460
fad460
ip2num() {
fad460
    IFS="." quad2num ${1}
fad460
}
fad460
fad460
num2ip() {
fad460
    let n="${1}"
fad460
    let o1="(n >> 24) & 0xff"
fad460
    let o2="(n >> 16) & 0xff"
fad460
    let o3="(n >> 8) & 0xff"
fad460
    let o4="n & 0xff"
fad460
    echo "${o1}.${o2}.${o3}.${o4}"
fad460
}
fad460
fad460
get_network_address() {
fad460
# get network address for the given IP address and (netmask or prefix)
fad460
    ip="${1}"
fad460
    nm="${2}"
fad460
fad460
    if [ -n "${ip}" -a -n "${nm}" ]; then
fad460
        if [[ "${nm}" = *.* ]]; then
fad460
            ipcalc -s -n ${ip} ${nm} | cut -d '=' -f 2
fad460
        else
fad460
            ipcalc -s -n ${ip}/${nm} | cut -d '=' -f 2
fad460
        fi
fad460
    fi
fad460
}
fad460
fad460
get_prefix() {
fad460
# get prefix for the given IP address and mask
fad460
    ip="${1}"
fad460
    nm="${2}"
fad460
fad460
    if [ -n "${ip}" -a -n "${nm}" ]; then
fad460
        ipcalc -s -p ${ip} ${nm} | cut -d '=' -f 2
fad460
    fi
fad460
}
fad460
fad460
class_bits() {
fad460
    let ip=$(IFS='.' ip2num $1)
fad460
    let bits=32
fad460
    let mask='255'
fad460
    for ((i=0; i <= 3; i++, 'mask<<=8')); do
fad460
        let v='ip&mask'
fad460
        if [ "$v" -eq 0 ] ; then
fad460
             let bits-=8
fad460
        else
fad460
             break
fad460
        fi
fad460
    done
fad460
    echo $bits
fad460
}
fad460
fad460
is_router_reachable() {
fad460
    # handle DHCP servers that give us a router not on our subnet
fad460
    router="${1}"
fad460
    routersubnet="$(get_network_address ${router} ${new_subnet_mask})"
fad460
    mysubnet="$(get_network_address ${new_ip_address} ${new_subnet_mask})"
fad460
fad460
    if [ ! "${routersubnet}" = "${mysubnet}" ]; then
fad460
        ip -4 route replace ${router}/32 dev ${interface}
fad460
        if [ "$?" -ne 0 ]; then
fad460
            logmessage "failed to create host route for ${router}"
fad460
            return 1
fad460
        fi
fad460
    fi
fad460
fad460
    return 0
fad460
}
fad460
fad460
add_default_gateway() {
fad460
    router="${1}"
fad460
fad460
    if is_router_reachable ${router} ; then
fad460
        metric=""
fad460
        if [ $# -gt 1 ] && [ ${2} -gt 0 ]; then
fad460
            metric="metric ${2}"
fad460
        fi
fad460
        ip -4 route replace default via ${router} dev ${interface} ${metric}
fad460
        if [ $? -ne 0 ]; then
fad460
            logmessage "failed to create default route: ${router} dev ${interface} ${metric}"
fad460
            return 1
fad460
        else
fad460
            return 0
fad460
        fi
fad460
    fi
fad460
fad460
    return 1
fad460
}
fad460
fad460
execute_client_side_configuration_scripts() {
fad460
# execute any additional client side configuration scripts we have
fad460
    if [ "${1}" == "config" ] || [ "${1}" == "restore" ]; then
fad460
        for f in ${ETCDIR}/dhclient.d/*.sh ; do
fad460
            if [ -x ${f} ]; then
fad460
                subsystem="${f%.sh}"
fad460
                subsystem="${subsystem##*/}"
fad460
                . ${f}
fad460
                "${subsystem}_${1}"
fad460
            fi
fad460
        done
fad460
    fi
fad460
}
fad460
fad460
flush_dev() {
fad460
# Instead of bringing the interface down (#574568)
fad460
# explicitly clear the ARP cache and flush all addresses & routes.
fad460
    ip -4 addr flush dev ${1} >/dev/null 2>&1
fad460
    ip -4 route flush dev ${1} >/dev/null 2>&1
fad460
    ip -4 neigh flush dev ${1} >/dev/null 2>&1
fad460
}
fad460
fad460
dhconfig() {
fad460
    if [ -n "${old_ip_address}" ] && [ -n "${alias_ip_address}" ] &&
fad460
       [ ! "${alias_ip_address}" = "${old_ip_address}" ]; then
fad460
        # possible new alias, remove old alias first
fad460
        ip -4 addr del ${old_ip_address} dev ${interface} label ${interface}:0
fad460
    fi
fad460
fad460
    if [ -n "${old_ip_address}" ] &&
fad460
       [ ! "${old_ip_address}" = "${new_ip_address}" ]; then
fad460
        # IP address changed. Delete all routes, and clear the ARP cache.
fad460
        flush_dev ${interface}
fad460
    fi
fad460
fad460
    # make sure the interface is up
fad460
    ip link set dev ${interface} up
fad460
fad460
    # replace = add if it doesn't exist or override (update lifetimes) if it's there
fad460
    ip -4 addr replace ${new_ip_address}/${new_prefix} broadcast ${new_broadcast_address} dev ${interface} \
fad460
       valid_lft ${new_dhcp_lease_time} preferred_lft ${new_dhcp_lease_time} >/dev/null 2>&1
fad460
fad460
fad460
    if [ "${reason}" = "BOUND" ] || [ "${reason}" = "REBOOT" ] ||
fad460
       [ ! "${old_classless_static_routes}" = "${new_classless_static_routes}" ] ||
fad460
       [ ! "${old_static_routes}" = "${new_static_routes}" ] ||
fad460
       [ ! "${old_ip_address}" = "${new_ip_address}" ] ||
fad460
       [ ! "${old_subnet_mask}" = "${new_subnet_mask}" ] ||
fad460
       [ ! "${old_network_number}" = "${new_network_number}" ] ||
fad460
       [ ! "${old_broadcast_address}" = "${new_broadcast_address}" ] ||
fad460
       [ ! "${old_routers}" = "${new_routers}" ] ||
fad460
       [ ! "${old_interface_mtu}" = "${new_interface_mtu}" ]; then
fad460
        
fad460
        # The 576 MTU is only used for X.25 and dialup connections
fad460
        # where the admin wants low latency.  Such a low MTU can cause
fad460
        # problems with UDP traffic, among other things.  As such,
fad460
        # disallow MTUs from 576 and below by default, so that broken
fad460
        # MTUs are ignored, but higher stuff is allowed (1492, 1500, etc).
fad460
        if [ -n "${new_interface_mtu}" ] && [ ${new_interface_mtu} -gt 576 ]; then
fad460
            ip link set dev ${interface} mtu ${new_interface_mtu}
fad460
        fi
fad460
fad460
        # Remove old static routes if no new static routes are provided
fad460
        if [ -n "${old_classless_static_routes}" ] ||
fad460
           [ -n "${old_static_routes}" ]; then
fad460
            remove_routes=0
fad460
            if [ -n "${old_classless_static_routes}" ]; then
fad460
                if [ -z "${new_classless_static_routes}" ]; then
fad460
                        IFS=', |' old_static_routes=(${old_classless_static_routes})
fad460
                        remove_routes=1
fad460
                fi
fad460
            else
fad460
                if [ -z "${new_static_routes}" ]; then
fad460
                        IFS=', |' old_static_routes=(${old_static_routes})
fad460
                        remove_routes=1
fad460
                fi
fad460
            fi
fad460
            if [ $remove_routes = "1" ] ; then
fad460
                for((i=0; i<${#old_static_routes[@]}; i+=2)); do
fad460
                    old_target=${old_static_routes[$i]}
fad460
                    old_prefix=$(echo ${old_target} | cut -d "." -f 1)
fad460
                    old_target=$(echo ${old_target} | cut -d "." -f 2-)
fad460
                    old_gateway=${old_static_routes[$i+1]}
fad460
                    ip -4 route del ${old_target}/${old_prefix} proto static via ${old_gateway} dev ${interface}
fad460
                done
fad460
            fi
fad460
        fi
fad460
        
fad460
        # static routes
fad460
        if [ -n "${new_classless_static_routes}" ] ||
fad460
           [ -n "${new_static_routes}" ]; then
fad460
            if [ -n "${new_classless_static_routes}" ]; then
fad460
                IFS=', |' static_routes=(${new_classless_static_routes})
fad460
		# If the DHCP server returns both a Classless Static Routes option and
fad460
                # a Router option, the DHCP client MUST ignore the Router option. (RFC3442)
fad460
                new_routers=""
fad460
            else
fad460
                IFS=', |' static_routes=(${new_static_routes})
fad460
            fi
fad460
            route_targets=()
fad460
fad460
            # Remove old static routes if no matching target is provided in the new static routes
fad460
            if [ -n "${old_classless_static_routes}" ] ||
fad460
               [ -n "${old_static_routes}" ]; then
fad460
                if [ -n "${old_classless_static_routes}" ]; then
fad460
                    IFS=', |' old_static_routes=(${old_classless_static_routes})
fad460
                else
fad460
                    IFS=', |' old_static_routes=(${old_static_routes})
fad460
                fi
fad460
                for((i=0; i<${#old_static_routes[@]}; i+=2)); do
fad460
                    old_target=${old_static_routes[$i]}
fad460
                    remove_route=1
fad460
                    for((j=0; j<${#static_routes[@]}; j+=2)); do
fad460
                        if [ $old_target = ${static_routes[$j]} ]; then
fad460
                            remove_route=0
fad460
                        fi
fad460
                    done
fad460
                    old_prefix=$(echo ${old_target} | cut -d "." -f 1)
fad460
                    old_target=$(echo ${old_target} | cut -d "." -f 2-)
fad460
                    old_gateway=${old_static_routes[$i+1]}
fad460
                    if [ $remove_route = "1" ]; then
fad460
                        ip -4 route del ${old_target}/${old_prefix} proto static via ${old_gateway} dev ${interface}
fad460
                    fi
fad460
                done
fad460
            fi
fad460
            
fad460
            for((i=0; i<${#static_routes[@]}; i+=2)); do
fad460
                target=${static_routes[$i]}
fad460
                if [ -n "${new_classless_static_routes}" ]; then
fad460
		    if [ ${target} = "0" ]; then
fad460
                        new_routers="${static_routes[$i+1]}"
fad460
                        continue
fad460
                    else
fad460
                        prefix=${target%%.*}
fad460
                        target=${target#*.}
fad460
                        IFS="." target_arr=(${target})
fad460
                        unset IFS
fad460
                        ((pads=4-${#target_arr[@]}))
fad460
                        for j in $(seq $pads); do
fad460
                            target="${target}.0"
fad460
                        done
fad460
fad460
                        # Client MUST zero any bits in the subnet number where the corresponding bit in the mask is zero.
fad460
                        # In other words, the subnet number installed in the routing table is the logical AND of
fad460
                        # the subnet number and subnet mask given in the Classless Static Routes option. (RFC3442)
fad460
                        target="$(get_network_address ${target} ${prefix})"
fad460
                    fi
fad460
                else
fad460
                    prefix=$(class_bits ${target})
fad460
                fi
fad460
                gateway=${static_routes[$i+1]}
fad460
fad460
                # special case 0.0.0.0 to allow static routing for link-local addresses
fad460
                # (including IPv4 multicast) which will not have a next-hop (#769463, #787318)
fad460
                if [ "${gateway}" = "0.0.0.0" ]; then
fad460
                    valid_gateway=0
fad460
                    scope='scope link'
fad460
                else
fad460
                    is_router_reachable ${gateway}
fad460
                    valid_gateway=$?
fad460
                    scope=''
fad460
                fi
fad460
                if [ ${valid_gateway} -eq 0 ]; then
fad460
                    metric=''
fad460
                    for t in ${route_targets[@]}; do
fad460
                        if [ ${t} = ${target} ]; then
fad460
                            if [ -z "${metric}" ]; then
fad460
                                metric=1
fad460
                            else
fad460
                                ((metric=metric+1))
fad460
                            fi
fad460
                        fi
fad460
                    done
fad460
fad460
                    if [ -n "${metric}" ]; then
fad460
                        metric="metric ${metric}"
fad460
                    fi
fad460
fad460
                    ip -4 route replace ${target}/${prefix} proto static via ${gateway} dev ${interface} ${metric} ${scope}
fad460
fad460
                    if [ $? -ne 0 ]; then
fad460
                        logmessage "failed to create static route: ${target}/${prefix} via ${gateway} dev ${interface} ${metric}"
fad460
                    else
fad460
                        route_targets=(${route_targets[@]} ${target})
fad460
                    fi
fad460
                fi
fad460
            done
fad460
        fi
fad460
fad460
        # gateways
fad460
        if [[ ( "${DEFROUTE}" != "no" ) &&
fad460
              (( -z "${GATEWAYDEV}" ) || ( "${GATEWAYDEV}" = "${interface}" )) ]]; then
fad460
            if [[ ( -z "$GATEWAY" ) ||
fad460
                  (( -n "$DHCLIENT_IGNORE_GATEWAY" ) && ( "$DHCLIENT_IGNORE_GATEWAY" = [Yy]* )) ]]; then
fad460
                metric="${METRIC:-}"
fad460
                let i="${METRIC:-0}"
fad460
                default_routers=()
fad460
fad460
                for router in ${new_routers} ; do
fad460
                    added_router=-
fad460
fad460
                    for r in ${default_routers[@]} ; do
fad460
                        if [ "${r}" = "${router}" ]; then
fad460
                            added_router=1
fad460
                        fi
fad460
                    done
fad460
fad460
                    if [ -z "${router}" ] ||
fad460
                       [ "${added_router}" = "1" ] ||
fad460
                       [ $(IFS=. ip2num ${router}) -le 0 ] ||
fad460
                       [[ ( "${router}" = "${new_broadcast_address}" ) &&
fad460
                          ( "${new_subnet_mask}" != "255.255.255.255" ) ]]; then
fad460
                        continue
fad460
                    fi
fad460
fad460
                    default_routers=(${default_routers[@]} ${router})
fad460
                    add_default_gateway ${router} ${metric}
fad460
                    let i=i+1
fad460
                    metric=${i}
fad460
                done
fad460
            elif [ -n "${GATEWAY}" ]; then
fad460
                routersubnet=$(get_network_address ${GATEWAY} ${new_subnet_mask})
fad460
                mysubnet=$(get_network_address ${new_ip_address} ${new_subnet_mask})
fad460
fad460
                if [ "${routersubnet}" = "${mysubnet}" ]; then
fad460
                    ip -4 route replace default via ${GATEWAY} dev ${interface}
fad460
                fi
fad460
            fi
fad460
        fi
fad460
    fi
fad460
fad460
    if [ ! "${new_ip_address}" = "${alias_ip_address}" ] &&
fad460
       [ -n "${alias_ip_address}" ]; then
fad460
        # Reset the alias address (fix: this should really only do this on changes)
fad460
        ip -4 addr flush dev ${interface} label ${interface}:0 >/dev/null 2>&1
fad460
        ip -4 addr replace ${alias_ip_address}/${alias_prefix} broadcast ${alias_broadcast_address} dev ${interface} label ${interface}:0
fad460
        ip -4 route replace ${alias_ip_address}/32 dev ${interface}
fad460
    fi
fad460
    
fad460
    # After dhclient brings an interface UP with a new IP address, subnet mask, 
fad460
    # and routes, in the REBOOT/BOUND states -> search for "dhclient-up-hooks".
fad460
    if [ "${reason}" = "BOUND" ] || [ "${reason}" = "REBOOT" ] ||
fad460
       [ ! "${old_ip_address}" = "${new_ip_address}" ] ||
fad460
       [ ! "${old_subnet_mask}" = "${new_subnet_mask}" ] ||
fad460
       [ ! "${old_network_number}" = "${new_network_number}" ] ||
fad460
       [ ! "${old_broadcast_address}" = "${new_broadcast_address}" ] ||
fad460
       [ ! "${old_routers}" = "${new_routers}" ] ||
fad460
       [ ! "${old_interface_mtu}" = "${new_interface_mtu}" ]; then
fad460
        
fad460
        if [ -x ${ETCDIR}/dhclient-${interface}-up-hooks ]; then
fad460
            . ${ETCDIR}/dhclient-${interface}-up-hooks
fad460
        elif [ -x ${ETCDIR}/dhclient-up-hooks ]; then
fad460
            . ${ETCDIR}/dhclient-up-hooks
fad460
        fi
fad460
    fi
fad460
fad460
    make_resolv_conf
fad460
fad460
    if [ -n "${new_host_name}" ] && need_hostname; then
fad460
        hostname ${new_host_name} || echo "See -nc option in dhclient(8) man page."
fad460
    fi
fad460
fad460
    if [[ ( "${DHCP_TIME_OFFSET_SETS_TIMEZONE}" = [yY1]* ) &&
fad460
          ( -n "${new_time_offset}" ) ]]; then
fad460
        # DHCP option "time-offset" is requested by default and should be
fad460
        # handled.  The geographical zone abbreviation cannot be determined
fad460
        # from the GMT offset, but the $ZONEINFO/Etc/GMT$offset file can be
fad460
        # used - note: this disables DST.
fad460
        ((z=new_time_offset/3600))
fad460
        ((hoursWest=$(printf '%+d' $z)))
fad460
fad460
        if (( $hoursWest < 0 )); then
fad460
            # tzdata treats negative 'hours west' as positive 'gmtoff'!
fad460
            ((hoursWest*=-1))
fad460
        fi
fad460
fad460
        tzfile=/usr/share/zoneinfo/Etc/GMT$(printf '%+d' ${hoursWest})
fad460
        if [ -e ${tzfile} ]; then
fad460
            cp -fp ${tzfile} /etc/localtime
fad460
            touch /etc/localtime
fad460
        fi
fad460
    fi
fad460
fad460
    execute_client_side_configuration_scripts "config"
fad460
}
fad460
fad460
# Section 18.1.8. (Receipt of Reply Messages) of RFC 3315 says:
fad460
# The client SHOULD perform duplicate address detection on each of
fad460
# the addresses in any IAs it receives in the Reply message before
fad460
# using that address for traffic.
fad460
add_ipv6_addr_with_DAD() {
fad460
            ip -6 addr replace ${new_ip6_address}/${new_ip6_prefixlen} \
fad460
                dev ${interface} scope global valid_lft ${new_max_life} \
fad460
                                          preferred_lft ${new_preferred_life}
fad460
fad460
            # repeatedly test whether newly added address passed
fad460
            # duplicate address detection (DAD)
fad460
            for i in $(seq 5); do
fad460
                sleep 1 # give the DAD some time
fad460
fad460
                addr=$(ip -6 addr show dev ${interface} \
fad460
                       | grep ${new_ip6_address}/${new_ip6_prefixlen})
fad460
fad460
                # tentative flag == DAD is still not complete
fad460
                tentative=$(echo "${addr}" | grep tentative)
fad460
                # dadfailed flag == address is already in use somewhere else
fad460
                dadfailed=$(echo "${addr}" | grep dadfailed)
fad460
fad460
                if [ -n "${dadfailed}" ] ; then
fad460
                    # address was added with valid_lft/preferred_lft 'forever', remove it
fad460
                    ip -6 addr del ${new_ip6_address}/${new_ip6_prefixlen} dev ${interface}
fad460
                    exit_with_hooks 3
fad460
                fi
fad460
                if [ -z "${tentative}" ] ; then
fad460
                    if [ -n "${addr}" ]; then
fad460
                        # DAD is over
fad460
                        return 0
fad460
                    else
fad460
                        # address was auto-removed (or not added at all)
fad460
                        exit_with_hooks 3
fad460
                    fi
fad460
                fi
fad460
            done
fad460
            return 0
fad460
}
fad460
fad460
dh6config() {
fad460
    if [ -n "${old_ip6_prefix}" ] ||
fad460
       [ -n "${new_ip6_prefix}" ]; then
fad460
        echo Prefix ${reason} old=${old_ip6_prefix} new=${new_ip6_prefix}
fad460
        exit_with_hooks 0
fad460
    fi
fad460
fad460
    case "${reason}" in
fad460
        BOUND6)
fad460
            if [ -z "${new_ip6_address}" ] ||
fad460
               [ -z "${new_ip6_prefixlen}" ]; then
fad460
                exit_with_hooks 2
fad460
            fi
fad460
fad460
            add_ipv6_addr_with_DAD
fad460
fad460
            make_resolv_conf
fad460
            ;;
fad460
fad460
        RENEW6|REBIND6)
fad460
            if [[ -n "${new_ip6_address}" ]] &&
fad460
               [[ -n "${new_ip6_prefixlen}" ]]; then
fad460
               if [[  ! "${new_ip6_address}" = "${old_ip6_address}" ]]; then
fad460
		   [[ -n "${old_ip6_address}" ]] && ip -6 addr del ${old_ip6_address} dev ${interface}
fad460
                   add_ipv6_addr_with_DAD
fad460
               fi
fad460
               # call it even if new_ip6_address = old_ip6_address to update lifetimes
fad460
               add_ipv6_addr_with_DAD
fad460
             fi
fad460
fad460
            if [ ! "${new_dhcp6_name_servers}" = "${old_dhcp6_name_servers}" ] ||
fad460
               [ ! "${new_dhcp6_domain_search}" = "${old_dhcp6_domain_search}" ]; then
fad460
                make_resolv_conf
fad460
            fi
fad460
            ;;
fad460
fad460
        DEPREF6)
fad460
            if [ -z "${new_ip6_prefixlen}" ]; then
fad460
                exit_with_hooks 2
fad460
            fi
fad460
fad460
            ip -6 addr change ${new_ip6_address}/${new_ip6_prefixlen} \
fad460
                dev ${interface} scope global preferred_lft 0
fad460
            ;;
fad460
    esac
fad460
fad460
    execute_client_side_configuration_scripts "config"
fad460
}
fad460
fad460
fad460
#
fad460
# ### MAIN
fad460
#
fad460
fad460
if [ -x ${ETCDIR}/dhclient-enter-hooks ]; then
fad460
    exit_status=0
fad460
fad460
    # dhclient-enter-hooks can abort dhclient-script by setting
fad460
    # the exit_status variable to a non-zero value
fad460
    . ${ETCDIR}/dhclient-enter-hooks
fad460
    if [ ${exit_status} -ne 0 ]; then
fad460
        exit ${exit_status}
fad460
    fi
fad460
fi
fad460
fad460
if [ ! -r /etc/sysconfig/network-scripts/network-functions ]; then
fad460
    echo "Missing /etc/sysconfig/network-scripts/network-functions, exiting." >&2
fad460
    exit 1
fad460
fi
fad460
fad460
if [ ! -r /etc/rc.d/init.d/functions ]; then
fad460
    echo "Missing /etc/rc.d/init.d/functions, exiting." >&2
fad460
    exit 1
fad460
fi
fad460
fad460
. /etc/sysconfig/network-scripts/network-functions
fad460
. /etc/rc.d/init.d/functions
fad460
fad460
if [ -f /etc/sysconfig/network ]; then
fad460
    . /etc/sysconfig/network
fad460
fi
fad460
fad460
if [ -f /etc/sysconfig/networking/network ]; then
fad460
    . /etc/sysconfig/networking/network
fad460
fi
fad460
fad460
cd /etc/sysconfig/network-scripts
fad460
CONFIG="${interface}"
fad460
need_config ${CONFIG}
fad460
source_config >/dev/null 2>&1
fad460
fad460
new_prefix="$(get_prefix ${new_ip_address} ${new_subnet_mask})"
fad460
old_prefix="$(get_prefix ${old_ip_address} ${old_subnet_mask})"
fad460
alias_prefix="$(get_prefix ${alias_ip_address} ${alias_subnet_mask})"
fad460
fad460
case "${reason}" in
fad460
    MEDIUM|ARPCHECK|ARPSEND)
fad460
        # Do nothing
fad460
        exit_with_hooks 0
fad460
        ;;
fad460
fad460
    PREINIT)
fad460
        if [ -n "${alias_ip_address}" ]; then
fad460
            # Flush alias, its routes will disappear too.
fad460
            ip -4 addr flush dev ${interface} label ${interface}:0 >/dev/null 2>&1
fad460
        fi
fad460
fad460
        # upstream dhclient-script removes (ifconfig $interface 0 up) old adresses in PREINIT,
fad460
        # but we sometimes (#125298) need (for iSCSI/nfs root to have a dhcp interface) to keep the existing ip
fad460
        # flush_dev ${interface}
fad460
        ip link set dev ${interface} up
fad460
        if [ -n "${DHCLIENT_DELAY}" ] && [ ${DHCLIENT_DELAY} -gt 0 ]; then
fad460
            # We need to give the kernel some time to get the interface up.
fad460
            sleep ${DHCLIENT_DELAY}
fad460
        fi
fad460
fad460
        exit_with_hooks 0
fad460
        ;;
fad460
fad460
    PREINIT6)
fad460
        # ensure interface is up
fad460
        ip link set dev ${interface} up
fad460
fad460
        # remove any stale addresses from aborted clients
fad460
        ip -6 addr flush dev ${interface} scope global permanent
fad460
fad460
        # we need a link-local address to be ready (not tentative)
fad460
        for i in $(seq 50); do
fad460
            linklocal=$(ip -6 addr show dev ${interface} scope link)
fad460
            # tentative flag means DAD is still not complete
fad460
            tentative=$(echo "${linklocal}" | grep tentative)
fad460
            [[ -n "${linklocal}" && -z "${tentative}" ]] && exit_with_hooks 0
fad460
            sleep 0.1
fad460
        done
fad460
fad460
        exit_with_hooks 0
fad460
        ;;
fad460
fad460
    BOUND|RENEW|REBIND|REBOOT)
fad460
        if [ -z "${interface}" ] || [ -z "${new_ip_address}" ]; then
fad460
            exit_with_hooks 2
fad460
        fi
fad460
        if arping -D -q -c2 -I ${interface} ${new_ip_address}; then
fad460
            dhconfig
fad460
            exit_with_hooks 0
fad460
        else  # DAD failed, i.e. address is already in use
fad460
            ARP_REPLY=$(arping -D -c2 -I ${interface} ${new_ip_address} | grep reply | awk '{print toupper($5)}' | cut -d "[" -f2 | cut -d "]" -f1)
fad460
            OUR_MACS=$(ip link show | grep link | awk '{print toupper($2)}' | uniq)
fad460
            if [[ "${OUR_MACS}" = *"${ARP_REPLY}"* ]]; then
fad460
                # in RENEW the reply can come from our system, that's OK
fad460
                dhconfig
fad460
                exit_with_hooks 0
fad460
            else
fad460
                exit_with_hooks 1
fad460
            fi
fad460
        fi
fad460
        ;;
fad460
fad460
    BOUND6|RENEW6|REBIND6|DEPREF6)
fad460
        dh6config
fad460
        exit_with_hooks 0
fad460
        ;;
fad460
fad460
    EXPIRE6|RELEASE6|STOP6)
fad460
        if [ -z "${old_ip6_address}" ] || [ -z "${old_ip6_prefixlen}" ]; then
fad460
            exit_with_hooks 2
fad460
        fi
fad460
fad460
        ip -6 addr del ${old_ip6_address}/${old_ip6_prefixlen} \
fad460
            dev ${interface}
fad460
fad460
        execute_client_side_configuration_scripts "restore"
fad460
fad460
        if [ -x ${ETCDIR}/dhclient-${interface}-down-hooks ]; then
fad460
            . ${ETCDIR}/dhclient-${interface}-down-hooks
fad460
        elif [ -x ${ETCDIR}/dhclient-down-hooks ]; then
fad460
            . ${ETCDIR}/dhclient-down-hooks
fad460
        fi
fad460
fad460
        exit_with_hooks 0
fad460
        ;;
fad460
fad460
    EXPIRE|FAIL|RELEASE|STOP)
fad460
        execute_client_side_configuration_scripts "restore"
fad460
fad460
        if [ -x ${ETCDIR}/dhclient-${interface}-down-hooks ]; then
fad460
            . ${ETCDIR}/dhclient-${interface}-down-hooks
fad460
        elif [ -x ${ETCDIR}/dhclient-down-hooks ]; then
fad460
            . ${ETCDIR}/dhclient-down-hooks
fad460
        fi
fad460
fad460
        if [ -n "${alias_ip_address}" ]; then
fad460
            # Flush alias
fad460
            ip -4 addr flush dev ${interface} label ${interface}:0 >/dev/null 2>&1
fad460
        fi
fad460
fad460
        if [ -n "${old_ip_address}" ]; then
fad460
            # Delete addresses/routes/arp cache.
fad460
            flush_dev ${interface}
fad460
        fi
fad460
fad460
        if [ -n "${alias_ip_address}" ]; then
fad460
            ip -4 addr replace ${alias_ip_address}/${alias_prefix} broadcast ${alias_broadcast_address} dev ${interface} label ${interface}:0
fad460
            ip -4 route replace ${alias_ip_address}/32 dev ${interface}
fad460
        fi
fad460
fad460
        exit_with_hooks 0
fad460
        ;;
fad460
fad460
    TIMEOUT)
fad460
        if [ -n "${new_routers}" ]; then
fad460
            if [ -n "${alias_ip_address}" ]; then
fad460
                ip -4 addr flush dev ${interface} label ${interface}:0 >/dev/null 2>&1
fad460
            fi
fad460
fad460
            ip -4 addr replace ${new_ip_address}/${new_prefix} \
fad460
                broadcast ${new_broadcast_address} dev ${interface} \
fad460
                valid_lft ${new_dhcp_lease_time} preferred_lft ${new_dhcp_lease_time}
fad460
            set ${new_routers}
fad460
fad460
            if ping -q -c 1 -w 10 -I ${interface} ${1}; then
fad460
                dhconfig
fad460
                exit_with_hooks 0
fad460
            fi
fad460
fad460
            flush_dev ${interface}
fad460
            exit_with_hooks 1
fad460
        else
fad460
            exit_with_hooks 1
fad460
        fi
fad460
        ;;
fad460
fad460
    *)
fad460
        logmessage "unhandled state: ${reason}"
fad460
        exit_with_hooks 1
fad460
        ;;
fad460
esac
fad460
fad460
exit_with_hooks 0