Blame SOURCES/dhclient-script

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