Blame SOURCES/dhclient-script

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