Blame SOURCES/dhclient-script

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