philipp / rpms / dhcp

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