Blame SOURCES/dhclient-script

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